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
49 typedef struct tagMSISTORAGESVIEW
59 static BOOL
storages_set_table_size(MSISTORAGESVIEW
*sv
, UINT size
)
61 if (size
>= sv
->max_storages
)
63 sv
->max_storages
*= 2;
64 sv
->storages
= msi_realloc(sv
->storages
, sv
->max_storages
* sizeof(*sv
->storages
));
72 static UINT
STORAGES_fetch_int(struct tagMSIVIEW
*view
, UINT row
, UINT col
, UINT
*val
)
74 MSISTORAGESVIEW
*sv
= (MSISTORAGESVIEW
*)view
;
76 TRACE("(%p, %d, %d, %p)\n", view
, row
, col
, val
);
79 return ERROR_INVALID_PARAMETER
;
81 if (row
>= sv
->num_rows
)
82 return ERROR_NO_MORE_ITEMS
;
84 *val
= sv
->storages
[row
].str_index
;
89 static UINT
STORAGES_fetch_stream(struct tagMSIVIEW
*view
, UINT row
, UINT col
, IStream
**stm
)
91 MSISTORAGESVIEW
*sv
= (MSISTORAGESVIEW
*)view
;
93 TRACE("(%p, %d, %d, %p)\n", view
, row
, col
, stm
);
95 if (row
>= sv
->num_rows
)
96 return ERROR_FUNCTION_FAILED
;
98 return ERROR_INVALID_DATA
;
101 static UINT
STORAGES_set_string( struct tagMSIVIEW
*view
, UINT row
, UINT col
, const WCHAR
*val
, int len
)
103 ERR("Cannot modify primary key.\n");
104 return ERROR_FUNCTION_FAILED
;
107 static HRESULT
stream_to_storage(IStream
*stm
, IStorage
**stg
)
109 ILockBytes
*lockbytes
= NULL
;
114 ULARGE_INTEGER offset
;
116 hr
= IStream_Stat(stm
, &stat
, STATFLAG_NONAME
);
120 if (stat
.cbSize
.QuadPart
>> 32)
122 ERR("Storage is too large\n");
126 size
= stat
.cbSize
.QuadPart
;
127 data
= msi_alloc(size
);
129 return E_OUTOFMEMORY
;
131 hr
= IStream_Read(stm
, data
, size
, &read
);
132 if (FAILED(hr
) || read
!= size
)
135 hr
= CreateILockBytesOnHGlobal(NULL
, TRUE
, &lockbytes
);
139 ZeroMemory(&offset
, sizeof(ULARGE_INTEGER
));
140 hr
= ILockBytes_WriteAt(lockbytes
, offset
, data
, size
, &read
);
141 if (FAILED(hr
) || read
!= size
)
144 hr
= StgOpenStorageOnILockBytes(lockbytes
, NULL
,
145 STGM_READWRITE
| STGM_SHARE_DENY_NONE
,
152 if (lockbytes
) ILockBytes_Release(lockbytes
);
156 static UINT
STORAGES_set_stream( MSIVIEW
*view
, UINT row
, UINT col
, IStream
*stream
)
158 MSISTORAGESVIEW
*sv
= (MSISTORAGESVIEW
*)view
;
159 IStorage
*stg
, *substg
, *prev
;
164 TRACE("view %p, row %u, col %u, stream %p.\n", view
, row
, col
, stream
);
166 if ((r
= stream_to_storage(stream
, &stg
)))
169 name
= msi_string_lookup(sv
->db
->strings
, sv
->storages
[row
].str_index
, NULL
);
171 hr
= IStorage_CreateStorage(sv
->db
->storage
, name
,
172 STGM_WRITE
| STGM_SHARE_EXCLUSIVE
,
176 IStorage_Release(stg
);
177 return ERROR_FUNCTION_FAILED
;
180 hr
= IStorage_CopyTo(stg
, 0, NULL
, NULL
, substg
);
183 IStorage_Release(substg
);
184 IStorage_Release(stg
);
185 return ERROR_FUNCTION_FAILED
;
187 IStorage_Release(substg
);
189 prev
= sv
->storages
[row
].storage
;
190 sv
->storages
[row
].storage
= stg
;
191 if (prev
) IStorage_Release(prev
);
193 return ERROR_SUCCESS
;
196 static UINT
STORAGES_set_row(struct tagMSIVIEW
*view
, UINT row
, MSIRECORD
*rec
, UINT mask
)
198 MSISTORAGESVIEW
*sv
= (MSISTORAGESVIEW
*)view
;
199 IStorage
*stg
, *substg
= NULL
, *prev
;
203 UINT r
= ERROR_FUNCTION_FAILED
;
205 TRACE("(%p, %p)\n", view
, rec
);
207 if (row
>= sv
->num_rows
)
208 return ERROR_FUNCTION_FAILED
;
210 r
= MSI_RecordGetIStream(rec
, 2, &stm
);
211 if (r
!= ERROR_SUCCESS
)
214 r
= stream_to_storage(stm
, &stg
);
215 if (r
!= ERROR_SUCCESS
)
217 IStream_Release(stm
);
221 name
= strdupW(MSI_RecordGetString(rec
, 1));
224 r
= ERROR_OUTOFMEMORY
;
228 hr
= IStorage_CreateStorage(sv
->db
->storage
, name
,
229 STGM_WRITE
| STGM_SHARE_EXCLUSIVE
,
233 r
= ERROR_FUNCTION_FAILED
;
237 hr
= IStorage_CopyTo(stg
, 0, NULL
, NULL
, substg
);
240 r
= ERROR_FUNCTION_FAILED
;
244 prev
= sv
->storages
[row
].storage
;
245 sv
->storages
[row
].str_index
= msi_add_string(sv
->db
->strings
, name
, -1, FALSE
);
246 IStorage_AddRef(stg
);
247 sv
->storages
[row
].storage
= stg
;
248 if (prev
) IStorage_Release(prev
);
253 if (substg
) IStorage_Release(substg
);
254 IStorage_Release(stg
);
255 IStream_Release(stm
);
260 static UINT
STORAGES_insert_row(struct tagMSIVIEW
*view
, MSIRECORD
*rec
, UINT row
, BOOL temporary
)
262 MSISTORAGESVIEW
*sv
= (MSISTORAGESVIEW
*)view
;
264 if (!storages_set_table_size(sv
, ++sv
->num_rows
))
265 return ERROR_FUNCTION_FAILED
;
268 row
= sv
->num_rows
- 1;
270 memset(&sv
->storages
[row
], 0, sizeof(sv
->storages
[row
]));
272 /* FIXME have to readjust rows */
274 return STORAGES_set_row(view
, row
, rec
, 0);
277 static UINT
STORAGES_delete_row(struct tagMSIVIEW
*view
, UINT row
)
279 FIXME("(%p %d): stub!\n", view
, row
);
280 return ERROR_SUCCESS
;
283 static UINT
STORAGES_execute(struct tagMSIVIEW
*view
, MSIRECORD
*record
)
285 TRACE("(%p, %p)\n", view
, record
);
286 return ERROR_SUCCESS
;
289 static UINT
STORAGES_close(struct tagMSIVIEW
*view
)
291 TRACE("(%p)\n", view
);
292 return ERROR_SUCCESS
;
295 static UINT
STORAGES_get_dimensions(struct tagMSIVIEW
*view
, UINT
*rows
, UINT
*cols
)
297 MSISTORAGESVIEW
*sv
= (MSISTORAGESVIEW
*)view
;
299 TRACE("(%p, %p, %p)\n", view
, rows
, cols
);
301 if (cols
) *cols
= NUM_STORAGES_COLS
;
302 if (rows
) *rows
= sv
->num_rows
;
304 return ERROR_SUCCESS
;
307 static UINT
STORAGES_get_column_info( struct tagMSIVIEW
*view
, UINT n
, LPCWSTR
*name
,
308 UINT
*type
, BOOL
*temporary
, LPCWSTR
*table_name
)
310 TRACE("(%p, %d, %p, %p, %p, %p)\n", view
, n
, name
, type
, temporary
,
313 if (n
== 0 || n
> NUM_STORAGES_COLS
)
314 return ERROR_INVALID_PARAMETER
;
319 if (name
) *name
= szName
;
320 if (type
) *type
= MSITYPE_STRING
| MSITYPE_VALID
| MAX_STORAGES_NAME_LEN
;
324 if (name
) *name
= szData
;
325 if (type
) *type
= MSITYPE_STRING
| MSITYPE_VALID
| MSITYPE_NULLABLE
;
328 if (table_name
) *table_name
= szStorages
;
329 if (temporary
) *temporary
= FALSE
;
330 return ERROR_SUCCESS
;
333 static UINT
storages_find_row(MSISTORAGESVIEW
*sv
, MSIRECORD
*rec
, UINT
*row
)
338 str
= MSI_RecordGetString(rec
, 1);
339 r
= msi_string2id(sv
->db
->strings
, str
, -1, &id
);
340 if (r
!= ERROR_SUCCESS
)
343 for (i
= 0; i
< sv
->num_rows
; i
++)
345 STORAGES_fetch_int(&sv
->view
, i
, 1, &data
);
350 return ERROR_SUCCESS
;
354 return ERROR_FUNCTION_FAILED
;
357 static UINT
storages_modify_update(struct tagMSIVIEW
*view
, MSIRECORD
*rec
)
359 MSISTORAGESVIEW
*sv
= (MSISTORAGESVIEW
*)view
;
362 r
= storages_find_row(sv
, rec
, &row
);
363 if (r
!= ERROR_SUCCESS
)
364 return ERROR_FUNCTION_FAILED
;
366 return STORAGES_set_row(view
, row
, rec
, 0);
369 static UINT
storages_modify_assign(struct tagMSIVIEW
*view
, MSIRECORD
*rec
)
371 MSISTORAGESVIEW
*sv
= (MSISTORAGESVIEW
*)view
;
374 r
= storages_find_row(sv
, rec
, &row
);
375 if (r
== ERROR_SUCCESS
)
376 return storages_modify_update(view
, rec
);
378 return STORAGES_insert_row(view
, rec
, -1, FALSE
);
381 static UINT
STORAGES_modify(struct tagMSIVIEW
*view
, MSIMODIFY eModifyMode
, MSIRECORD
*rec
, UINT row
)
385 TRACE("%p %d %p\n", view
, eModifyMode
, rec
);
389 case MSIMODIFY_ASSIGN
:
390 r
= storages_modify_assign(view
, rec
);
393 case MSIMODIFY_INSERT
:
394 r
= STORAGES_insert_row(view
, rec
, -1, FALSE
);
397 case MSIMODIFY_UPDATE
:
398 r
= storages_modify_update(view
, rec
);
401 case MSIMODIFY_VALIDATE_NEW
:
402 case MSIMODIFY_INSERT_TEMPORARY
:
403 case MSIMODIFY_REFRESH
:
404 case MSIMODIFY_REPLACE
:
405 case MSIMODIFY_MERGE
:
406 case MSIMODIFY_DELETE
:
407 case MSIMODIFY_VALIDATE
:
408 case MSIMODIFY_VALIDATE_FIELD
:
409 case MSIMODIFY_VALIDATE_DELETE
:
410 FIXME("%p %d %p - mode not implemented\n", view
, eModifyMode
, rec
);
411 r
= ERROR_CALL_NOT_IMPLEMENTED
;
415 r
= ERROR_INVALID_DATA
;
421 static UINT
STORAGES_delete(struct tagMSIVIEW
*view
)
423 MSISTORAGESVIEW
*sv
= (MSISTORAGESVIEW
*)view
;
426 TRACE("(%p)\n", view
);
428 for (i
= 0; i
< sv
->num_rows
; i
++)
430 if (sv
->storages
[i
].storage
)
431 IStorage_Release(sv
->storages
[i
].storage
);
434 msi_free(sv
->storages
);
438 return ERROR_SUCCESS
;
441 static const MSIVIEWOPS storages_ops
=
444 STORAGES_fetch_stream
,
453 STORAGES_get_dimensions
,
454 STORAGES_get_column_info
,
464 static INT
add_storages_to_table(MSISTORAGESVIEW
*sv
)
466 IEnumSTATSTG
*stgenum
= NULL
;
469 UINT count
= 0, size
;
471 hr
= IStorage_EnumElements(sv
->db
->storage
, 0, NULL
, 0, &stgenum
);
475 sv
->max_storages
= 1;
476 sv
->storages
= msi_alloc(sizeof(*sv
->storages
));
483 hr
= IEnumSTATSTG_Next(stgenum
, 1, &stat
, &size
);
484 if (FAILED(hr
) || !size
)
487 if (stat
.type
!= STGTY_STORAGE
)
489 CoTaskMemFree(stat
.pwcsName
);
493 TRACE("enumerated storage %s\n", debugstr_w(stat
.pwcsName
));
495 if (!storages_set_table_size(sv
, ++count
))
501 sv
->storages
[count
- 1].str_index
= msi_add_string(sv
->db
->strings
, stat
.pwcsName
, -1, FALSE
);
502 sv
->storages
[count
- 1].storage
= NULL
;
504 IStorage_OpenStorage(sv
->db
->storage
, stat
.pwcsName
, NULL
,
505 STGM_READ
| STGM_SHARE_EXCLUSIVE
, NULL
, 0,
506 &sv
->storages
[count
- 1].storage
);
507 CoTaskMemFree(stat
.pwcsName
);
510 IEnumSTATSTG_Release(stgenum
);
514 UINT
STORAGES_CreateView(MSIDATABASE
*db
, MSIVIEW
**view
)
519 TRACE("(%p, %p)\n", db
, view
);
521 sv
= msi_alloc_zero( sizeof(MSISTORAGESVIEW
) );
523 return ERROR_FUNCTION_FAILED
;
525 sv
->view
.ops
= &storages_ops
;
528 rows
= add_storages_to_table(sv
);
532 return ERROR_FUNCTION_FAILED
;
536 *view
= (MSIVIEW
*)sv
;
538 return ERROR_SUCCESS
;