2 * Implementation of the Microsoft Installer (msi.dll)
4 * Copyright 2008 James Hawkins
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
36 #include "wine/debug.h"
38 WINE_DEFAULT_DEBUG_CHANNEL(msidb
);
40 #define NUM_STORAGES_COLS 2
41 #define MAX_STORAGES_NAME_LEN 62
43 typedef struct tabSTORAGE
50 typedef struct tagMSISTORAGESVIEW
60 static BOOL
storages_set_table_size(MSISTORAGESVIEW
*sv
, UINT size
)
62 if (size
>= sv
->max_storages
)
64 sv
->max_storages
*= 2;
65 sv
->storages
= msi_realloc(sv
->storages
, sv
->max_storages
* sizeof(STORAGE
*));
73 static STORAGE
*create_storage(MSISTORAGESVIEW
*sv
, LPWSTR name
, IStorage
*stg
)
77 storage
= msi_alloc(sizeof(STORAGE
));
81 storage
->name
= strdupW(name
);
88 storage
->str_index
= msi_addstringW(sv
->db
->strings
, 0, storage
->name
, -1, 1, StringNonPersistent
);
89 storage
->storage
= stg
;
92 IStorage_AddRef(storage
->storage
);
97 static UINT
STORAGES_fetch_int(struct tagMSIVIEW
*view
, UINT row
, UINT col
, UINT
*val
)
99 MSISTORAGESVIEW
*sv
= (MSISTORAGESVIEW
*)view
;
101 TRACE("(%p, %d, %d, %p)\n", view
, row
, col
, val
);
104 return ERROR_INVALID_PARAMETER
;
106 if (row
>= sv
->num_rows
)
107 return ERROR_NO_MORE_ITEMS
;
109 *val
= sv
->storages
[row
]->str_index
;
111 return ERROR_SUCCESS
;
114 static UINT
STORAGES_fetch_stream(struct tagMSIVIEW
*view
, UINT row
, UINT col
, IStream
**stm
)
116 MSISTORAGESVIEW
*sv
= (MSISTORAGESVIEW
*)view
;
118 TRACE("(%p, %d, %d, %p)\n", view
, row
, col
, stm
);
120 if (row
>= sv
->num_rows
)
121 return ERROR_FUNCTION_FAILED
;
123 return ERROR_INVALID_DATA
;
126 static UINT
STORAGES_get_row( struct tagMSIVIEW
*view
, UINT row
, MSIRECORD
**rec
)
128 MSISTORAGESVIEW
*sv
= (MSISTORAGESVIEW
*)view
;
130 FIXME("%p %d %p\n", sv
, row
, rec
);
132 return ERROR_CALL_NOT_IMPLEMENTED
;
135 static HRESULT
stream_to_storage(IStream
*stm
, IStorage
**stg
)
137 ILockBytes
*lockbytes
= NULL
;
142 ULARGE_INTEGER offset
;
144 hr
= IStream_Stat(stm
, &stat
, STATFLAG_NONAME
);
148 if (stat
.cbSize
.QuadPart
>> 32)
150 ERR("Storage is too large\n");
154 size
= stat
.cbSize
.QuadPart
;
155 data
= msi_alloc(size
);
157 return E_OUTOFMEMORY
;
159 hr
= IStream_Read(stm
, data
, size
, &read
);
160 if (FAILED(hr
) || read
!= size
)
163 hr
= CreateILockBytesOnHGlobal(NULL
, TRUE
, &lockbytes
);
167 ZeroMemory(&offset
, sizeof(ULARGE_INTEGER
));
168 hr
= ILockBytes_WriteAt(lockbytes
, offset
, data
, size
, &read
);
169 if (FAILED(hr
) || read
!= size
)
172 hr
= StgOpenStorageOnILockBytes(lockbytes
, NULL
,
173 STGM_READWRITE
| STGM_SHARE_DENY_NONE
,
180 if (lockbytes
) ILockBytes_Release(lockbytes
);
184 static UINT
STORAGES_set_row(struct tagMSIVIEW
*view
, UINT row
, MSIRECORD
*rec
, UINT mask
)
186 MSISTORAGESVIEW
*sv
= (MSISTORAGESVIEW
*)view
;
187 IStorage
*stg
, *substg
= NULL
;
191 UINT r
= ERROR_FUNCTION_FAILED
;
193 TRACE("(%p, %p)\n", view
, rec
);
195 if (row
> sv
->num_rows
)
196 return ERROR_FUNCTION_FAILED
;
198 r
= MSI_RecordGetIStream(rec
, 2, &stm
);
199 if (r
!= ERROR_SUCCESS
)
202 r
= stream_to_storage(stm
, &stg
);
203 if (r
!= ERROR_SUCCESS
)
205 IStream_Release(stm
);
209 name
= strdupW(MSI_RecordGetString(rec
, 1));
212 r
= ERROR_OUTOFMEMORY
;
216 hr
= IStorage_CreateStorage(sv
->db
->storage
, name
,
217 STGM_WRITE
| STGM_SHARE_EXCLUSIVE
,
221 r
= ERROR_FUNCTION_FAILED
;
225 hr
= IStorage_CopyTo(stg
, 0, NULL
, NULL
, substg
);
228 r
= ERROR_FUNCTION_FAILED
;
232 sv
->storages
[row
] = create_storage(sv
, name
, stg
);
233 if (!sv
->storages
[row
])
234 r
= ERROR_FUNCTION_FAILED
;
239 if (substg
) IStorage_Release(substg
);
240 IStorage_Release(stg
);
241 IStream_Release(stm
);
246 static UINT
STORAGES_insert_row(struct tagMSIVIEW
*view
, MSIRECORD
*rec
, UINT row
, BOOL temporary
)
248 MSISTORAGESVIEW
*sv
= (MSISTORAGESVIEW
*)view
;
250 if (!storages_set_table_size(sv
, ++sv
->num_rows
))
251 return ERROR_FUNCTION_FAILED
;
254 row
= sv
->num_rows
- 1;
256 /* FIXME have to readjust rows */
258 return STORAGES_set_row(view
, row
, rec
, 0);
261 static UINT
STORAGES_delete_row(struct tagMSIVIEW
*view
, UINT row
)
263 FIXME("(%p %d): stub!\n", view
, row
);
264 return ERROR_SUCCESS
;
267 static UINT
STORAGES_execute(struct tagMSIVIEW
*view
, MSIRECORD
*record
)
269 TRACE("(%p, %p)\n", view
, record
);
270 return ERROR_SUCCESS
;
273 static UINT
STORAGES_close(struct tagMSIVIEW
*view
)
275 TRACE("(%p)\n", view
);
276 return ERROR_SUCCESS
;
279 static UINT
STORAGES_get_dimensions(struct tagMSIVIEW
*view
, UINT
*rows
, UINT
*cols
)
281 MSISTORAGESVIEW
*sv
= (MSISTORAGESVIEW
*)view
;
283 TRACE("(%p, %p, %p)\n", view
, rows
, cols
);
285 if (cols
) *cols
= NUM_STORAGES_COLS
;
286 if (rows
) *rows
= sv
->num_rows
;
288 return ERROR_SUCCESS
;
291 static UINT
STORAGES_get_column_info(struct tagMSIVIEW
*view
, UINT n
,
292 LPWSTR
*name
, UINT
*type
, BOOL
*temporary
,
295 LPCWSTR name_ptr
= NULL
;
297 static const WCHAR Name
[] = {'N','a','m','e',0};
298 static const WCHAR Data
[] = {'D','a','t','a',0};
299 static const WCHAR _Storages
[] = {'_','S','t','o','r','a','g','e','s',0};
301 TRACE("(%p, %d, %p, %p, %p, %p)\n", view
, n
, name
, type
, temporary
,
304 if (n
== 0 || n
> NUM_STORAGES_COLS
)
305 return ERROR_INVALID_PARAMETER
;
311 if (type
) *type
= MSITYPE_STRING
| MAX_STORAGES_NAME_LEN
;
316 if (type
) *type
= MSITYPE_STRING
| MSITYPE_VALID
| MSITYPE_NULLABLE
;
322 *name
= strdupW(name_ptr
);
323 if (!*name
) return ERROR_FUNCTION_FAILED
;
328 *table_name
= strdupW(_Storages
);
332 return ERROR_FUNCTION_FAILED
;
339 return ERROR_SUCCESS
;
342 static UINT
storages_find_row(MSISTORAGESVIEW
*sv
, MSIRECORD
*rec
, UINT
*row
)
347 str
= MSI_RecordGetString(rec
, 1);
348 msi_string2idW(sv
->db
->strings
, str
, &id
);
350 for (i
= 0; i
< sv
->num_rows
; i
++)
352 STORAGES_fetch_int(&sv
->view
, i
, 1, &data
);
357 return ERROR_SUCCESS
;
361 return ERROR_FUNCTION_FAILED
;
364 static UINT
storages_modify_update(struct tagMSIVIEW
*view
, MSIRECORD
*rec
)
366 MSISTORAGESVIEW
*sv
= (MSISTORAGESVIEW
*)view
;
369 r
= storages_find_row(sv
, rec
, &row
);
370 if (r
!= ERROR_SUCCESS
)
371 return ERROR_FUNCTION_FAILED
;
373 return STORAGES_set_row(view
, row
, rec
, 0);
376 static UINT
storages_modify_assign(struct tagMSIVIEW
*view
, MSIRECORD
*rec
)
378 MSISTORAGESVIEW
*sv
= (MSISTORAGESVIEW
*)view
;
381 r
= storages_find_row(sv
, rec
, &row
);
382 if (r
== ERROR_SUCCESS
)
383 return storages_modify_update(view
, rec
);
385 return STORAGES_insert_row(view
, rec
, -1, FALSE
);
388 static UINT
STORAGES_modify(struct tagMSIVIEW
*view
, MSIMODIFY eModifyMode
, MSIRECORD
*rec
, UINT row
)
392 TRACE("%p %d %p\n", view
, eModifyMode
, rec
);
396 case MSIMODIFY_ASSIGN
:
397 r
= storages_modify_assign(view
, rec
);
400 case MSIMODIFY_INSERT
:
401 r
= STORAGES_insert_row(view
, rec
, -1, FALSE
);
404 case MSIMODIFY_UPDATE
:
405 r
= storages_modify_update(view
, rec
);
408 case MSIMODIFY_VALIDATE_NEW
:
409 case MSIMODIFY_INSERT_TEMPORARY
:
410 case MSIMODIFY_REFRESH
:
411 case MSIMODIFY_REPLACE
:
412 case MSIMODIFY_MERGE
:
413 case MSIMODIFY_DELETE
:
414 case MSIMODIFY_VALIDATE
:
415 case MSIMODIFY_VALIDATE_FIELD
:
416 case MSIMODIFY_VALIDATE_DELETE
:
417 FIXME("%p %d %p - mode not implemented\n", view
, eModifyMode
, rec
);
418 r
= ERROR_CALL_NOT_IMPLEMENTED
;
422 r
= ERROR_INVALID_DATA
;
428 static UINT
STORAGES_delete(struct tagMSIVIEW
*view
)
430 MSISTORAGESVIEW
*sv
= (MSISTORAGESVIEW
*)view
;
433 TRACE("(%p)\n", view
);
435 for (i
= 0; i
< sv
->num_rows
; i
++)
437 if (sv
->storages
[i
]->storage
)
438 IStorage_Release(sv
->storages
[i
]->storage
);
440 msi_free(sv
->storages
[i
]->name
);
441 msi_free(sv
->storages
[i
]);
444 msi_free(sv
->storages
);
448 return ERROR_SUCCESS
;
451 static UINT
STORAGES_find_matching_rows(struct tagMSIVIEW
*view
, UINT col
,
452 UINT val
, UINT
*row
, MSIITERHANDLE
*handle
)
454 MSISTORAGESVIEW
*sv
= (MSISTORAGESVIEW
*)view
;
455 UINT index
= PtrToUlong(*handle
);
457 TRACE("(%d, %d): %d\n", *row
, col
, val
);
459 if (col
== 0 || col
> NUM_STORAGES_COLS
)
460 return ERROR_INVALID_PARAMETER
;
462 while (index
< sv
->num_rows
)
464 if (sv
->storages
[index
]->str_index
== val
)
473 *handle
= UlongToPtr(++index
);
474 if (index
>= sv
->num_rows
)
475 return ERROR_NO_MORE_ITEMS
;
477 return ERROR_SUCCESS
;
480 static const MSIVIEWOPS storages_ops
=
483 STORAGES_fetch_stream
,
490 STORAGES_get_dimensions
,
491 STORAGES_get_column_info
,
494 STORAGES_find_matching_rows
,
503 static INT
add_storages_to_table(MSISTORAGESVIEW
*sv
)
505 STORAGE
*storage
= NULL
;
506 IEnumSTATSTG
*stgenum
= NULL
;
509 UINT count
= 0, size
;
511 hr
= IStorage_EnumElements(sv
->db
->storage
, 0, NULL
, 0, &stgenum
);
515 sv
->max_storages
= 1;
516 sv
->storages
= msi_alloc(sizeof(STORAGE
*));
523 hr
= IEnumSTATSTG_Next(stgenum
, 1, &stat
, &size
);
524 if (FAILED(hr
) || !size
)
527 if (stat
.type
!= STGTY_STORAGE
)
529 CoTaskMemFree(stat
.pwcsName
);
533 TRACE("enumerated storage %s\n", debugstr_w(stat
.pwcsName
));
535 storage
= create_storage(sv
, stat
.pwcsName
, NULL
);
539 CoTaskMemFree(stat
.pwcsName
);
543 IStorage_OpenStorage(sv
->db
->storage
, stat
.pwcsName
, NULL
,
544 STGM_READ
| STGM_SHARE_EXCLUSIVE
, NULL
, 0,
546 CoTaskMemFree(stat
.pwcsName
);
548 if (!storages_set_table_size(sv
, ++count
))
554 sv
->storages
[count
- 1] = storage
;
557 IEnumSTATSTG_Release(stgenum
);
561 UINT
STORAGES_CreateView(MSIDATABASE
*db
, MSIVIEW
**view
)
566 TRACE("(%p, %p)\n", db
, view
);
568 sv
= msi_alloc(sizeof(MSISTORAGESVIEW
));
570 return ERROR_FUNCTION_FAILED
;
572 sv
->view
.ops
= &storages_ops
;
575 rows
= add_storages_to_table(sv
);
579 return ERROR_FUNCTION_FAILED
;
583 *view
= (MSIVIEW
*)sv
;
585 return ERROR_SUCCESS
;