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(STORAGE
*));
72 static STORAGE
*create_storage(MSISTORAGESVIEW
*sv
, LPCWSTR name
, IStorage
*stg
)
76 storage
= msi_alloc(sizeof(STORAGE
));
80 storage
->str_index
= msi_addstringW(sv
->db
->strings
, name
, -1, 1, StringNonPersistent
);
81 storage
->storage
= stg
;
84 IStorage_AddRef(storage
->storage
);
89 static UINT
STORAGES_fetch_int(struct tagMSIVIEW
*view
, UINT row
, UINT col
, UINT
*val
)
91 MSISTORAGESVIEW
*sv
= (MSISTORAGESVIEW
*)view
;
93 TRACE("(%p, %d, %d, %p)\n", view
, row
, col
, val
);
96 return ERROR_INVALID_PARAMETER
;
98 if (row
>= sv
->num_rows
)
99 return ERROR_NO_MORE_ITEMS
;
101 *val
= sv
->storages
[row
]->str_index
;
103 return ERROR_SUCCESS
;
106 static UINT
STORAGES_fetch_stream(struct tagMSIVIEW
*view
, UINT row
, UINT col
, IStream
**stm
)
108 MSISTORAGESVIEW
*sv
= (MSISTORAGESVIEW
*)view
;
110 TRACE("(%p, %d, %d, %p)\n", view
, row
, col
, stm
);
112 if (row
>= sv
->num_rows
)
113 return ERROR_FUNCTION_FAILED
;
115 return ERROR_INVALID_DATA
;
118 static UINT
STORAGES_get_row( struct tagMSIVIEW
*view
, UINT row
, MSIRECORD
**rec
)
120 MSISTORAGESVIEW
*sv
= (MSISTORAGESVIEW
*)view
;
122 FIXME("%p %d %p\n", sv
, row
, rec
);
124 return ERROR_CALL_NOT_IMPLEMENTED
;
127 static HRESULT
stream_to_storage(IStream
*stm
, IStorage
**stg
)
129 ILockBytes
*lockbytes
= NULL
;
134 ULARGE_INTEGER offset
;
136 hr
= IStream_Stat(stm
, &stat
, STATFLAG_NONAME
);
140 if (stat
.cbSize
.QuadPart
>> 32)
142 ERR("Storage is too large\n");
146 size
= stat
.cbSize
.QuadPart
;
147 data
= msi_alloc(size
);
149 return E_OUTOFMEMORY
;
151 hr
= IStream_Read(stm
, data
, size
, &read
);
152 if (FAILED(hr
) || read
!= size
)
155 hr
= CreateILockBytesOnHGlobal(NULL
, TRUE
, &lockbytes
);
159 ZeroMemory(&offset
, sizeof(ULARGE_INTEGER
));
160 hr
= ILockBytes_WriteAt(lockbytes
, offset
, data
, size
, &read
);
161 if (FAILED(hr
) || read
!= size
)
164 hr
= StgOpenStorageOnILockBytes(lockbytes
, NULL
,
165 STGM_READWRITE
| STGM_SHARE_DENY_NONE
,
172 if (lockbytes
) ILockBytes_Release(lockbytes
);
176 static UINT
STORAGES_set_row(struct tagMSIVIEW
*view
, UINT row
, MSIRECORD
*rec
, UINT mask
)
178 MSISTORAGESVIEW
*sv
= (MSISTORAGESVIEW
*)view
;
179 IStorage
*stg
, *substg
= NULL
;
183 UINT r
= ERROR_FUNCTION_FAILED
;
185 TRACE("(%p, %p)\n", view
, rec
);
187 if (row
> sv
->num_rows
)
188 return ERROR_FUNCTION_FAILED
;
190 r
= MSI_RecordGetIStream(rec
, 2, &stm
);
191 if (r
!= ERROR_SUCCESS
)
194 r
= stream_to_storage(stm
, &stg
);
195 if (r
!= ERROR_SUCCESS
)
197 IStream_Release(stm
);
201 name
= strdupW(MSI_RecordGetString(rec
, 1));
204 r
= ERROR_OUTOFMEMORY
;
208 hr
= IStorage_CreateStorage(sv
->db
->storage
, name
,
209 STGM_WRITE
| STGM_SHARE_EXCLUSIVE
,
213 r
= ERROR_FUNCTION_FAILED
;
217 hr
= IStorage_CopyTo(stg
, 0, NULL
, NULL
, substg
);
220 r
= ERROR_FUNCTION_FAILED
;
224 sv
->storages
[row
] = create_storage(sv
, name
, stg
);
225 if (!sv
->storages
[row
])
226 r
= ERROR_FUNCTION_FAILED
;
231 if (substg
) IStorage_Release(substg
);
232 IStorage_Release(stg
);
233 IStream_Release(stm
);
238 static UINT
STORAGES_insert_row(struct tagMSIVIEW
*view
, MSIRECORD
*rec
, UINT row
, BOOL temporary
)
240 MSISTORAGESVIEW
*sv
= (MSISTORAGESVIEW
*)view
;
242 if (!storages_set_table_size(sv
, ++sv
->num_rows
))
243 return ERROR_FUNCTION_FAILED
;
246 row
= sv
->num_rows
- 1;
248 /* FIXME have to readjust rows */
250 return STORAGES_set_row(view
, row
, rec
, 0);
253 static UINT
STORAGES_delete_row(struct tagMSIVIEW
*view
, UINT row
)
255 FIXME("(%p %d): stub!\n", view
, row
);
256 return ERROR_SUCCESS
;
259 static UINT
STORAGES_execute(struct tagMSIVIEW
*view
, MSIRECORD
*record
)
261 TRACE("(%p, %p)\n", view
, record
);
262 return ERROR_SUCCESS
;
265 static UINT
STORAGES_close(struct tagMSIVIEW
*view
)
267 TRACE("(%p)\n", view
);
268 return ERROR_SUCCESS
;
271 static UINT
STORAGES_get_dimensions(struct tagMSIVIEW
*view
, UINT
*rows
, UINT
*cols
)
273 MSISTORAGESVIEW
*sv
= (MSISTORAGESVIEW
*)view
;
275 TRACE("(%p, %p, %p)\n", view
, rows
, cols
);
277 if (cols
) *cols
= NUM_STORAGES_COLS
;
278 if (rows
) *rows
= sv
->num_rows
;
280 return ERROR_SUCCESS
;
283 static UINT
STORAGES_get_column_info(struct tagMSIVIEW
*view
, UINT n
,
284 LPWSTR
*name
, UINT
*type
, BOOL
*temporary
,
287 LPCWSTR name_ptr
= NULL
;
289 static const WCHAR Name
[] = {'N','a','m','e',0};
290 static const WCHAR Data
[] = {'D','a','t','a',0};
291 static const WCHAR _Storages
[] = {'_','S','t','o','r','a','g','e','s',0};
293 TRACE("(%p, %d, %p, %p, %p, %p)\n", view
, n
, name
, type
, temporary
,
296 if (n
== 0 || n
> NUM_STORAGES_COLS
)
297 return ERROR_INVALID_PARAMETER
;
303 if (type
) *type
= MSITYPE_STRING
| MAX_STORAGES_NAME_LEN
;
308 if (type
) *type
= MSITYPE_STRING
| MSITYPE_VALID
| MSITYPE_NULLABLE
;
314 *name
= strdupW(name_ptr
);
315 if (!*name
) return ERROR_FUNCTION_FAILED
;
320 *table_name
= strdupW(_Storages
);
324 return ERROR_FUNCTION_FAILED
;
331 return ERROR_SUCCESS
;
334 static UINT
storages_find_row(MSISTORAGESVIEW
*sv
, MSIRECORD
*rec
, UINT
*row
)
339 str
= MSI_RecordGetString(rec
, 1);
340 r
= msi_string2idW(sv
->db
->strings
, str
, &id
);
341 if (r
!= ERROR_SUCCESS
)
344 for (i
= 0; i
< sv
->num_rows
; i
++)
346 STORAGES_fetch_int(&sv
->view
, i
, 1, &data
);
351 return ERROR_SUCCESS
;
355 return ERROR_FUNCTION_FAILED
;
358 static UINT
storages_modify_update(struct tagMSIVIEW
*view
, MSIRECORD
*rec
)
360 MSISTORAGESVIEW
*sv
= (MSISTORAGESVIEW
*)view
;
363 r
= storages_find_row(sv
, rec
, &row
);
364 if (r
!= ERROR_SUCCESS
)
365 return ERROR_FUNCTION_FAILED
;
367 return STORAGES_set_row(view
, row
, rec
, 0);
370 static UINT
storages_modify_assign(struct tagMSIVIEW
*view
, MSIRECORD
*rec
)
372 MSISTORAGESVIEW
*sv
= (MSISTORAGESVIEW
*)view
;
375 r
= storages_find_row(sv
, rec
, &row
);
376 if (r
== ERROR_SUCCESS
)
377 return storages_modify_update(view
, rec
);
379 return STORAGES_insert_row(view
, rec
, -1, FALSE
);
382 static UINT
STORAGES_modify(struct tagMSIVIEW
*view
, MSIMODIFY eModifyMode
, MSIRECORD
*rec
, UINT row
)
386 TRACE("%p %d %p\n", view
, eModifyMode
, rec
);
390 case MSIMODIFY_ASSIGN
:
391 r
= storages_modify_assign(view
, rec
);
394 case MSIMODIFY_INSERT
:
395 r
= STORAGES_insert_row(view
, rec
, -1, FALSE
);
398 case MSIMODIFY_UPDATE
:
399 r
= storages_modify_update(view
, rec
);
402 case MSIMODIFY_VALIDATE_NEW
:
403 case MSIMODIFY_INSERT_TEMPORARY
:
404 case MSIMODIFY_REFRESH
:
405 case MSIMODIFY_REPLACE
:
406 case MSIMODIFY_MERGE
:
407 case MSIMODIFY_DELETE
:
408 case MSIMODIFY_VALIDATE
:
409 case MSIMODIFY_VALIDATE_FIELD
:
410 case MSIMODIFY_VALIDATE_DELETE
:
411 FIXME("%p %d %p - mode not implemented\n", view
, eModifyMode
, rec
);
412 r
= ERROR_CALL_NOT_IMPLEMENTED
;
416 r
= ERROR_INVALID_DATA
;
422 static UINT
STORAGES_delete(struct tagMSIVIEW
*view
)
424 MSISTORAGESVIEW
*sv
= (MSISTORAGESVIEW
*)view
;
427 TRACE("(%p)\n", view
);
429 for (i
= 0; i
< sv
->num_rows
; i
++)
431 if (sv
->storages
[i
]->storage
)
432 IStorage_Release(sv
->storages
[i
]->storage
);
433 msi_free(sv
->storages
[i
]);
436 msi_free(sv
->storages
);
440 return ERROR_SUCCESS
;
443 static UINT
STORAGES_find_matching_rows(struct tagMSIVIEW
*view
, UINT col
,
444 UINT val
, UINT
*row
, MSIITERHANDLE
*handle
)
446 MSISTORAGESVIEW
*sv
= (MSISTORAGESVIEW
*)view
;
447 UINT index
= PtrToUlong(*handle
);
449 TRACE("(%d, %d): %d\n", *row
, col
, val
);
451 if (col
== 0 || col
> NUM_STORAGES_COLS
)
452 return ERROR_INVALID_PARAMETER
;
454 while (index
< sv
->num_rows
)
456 if (sv
->storages
[index
]->str_index
== val
)
465 *handle
= UlongToPtr(++index
);
466 if (index
>= sv
->num_rows
)
467 return ERROR_NO_MORE_ITEMS
;
469 return ERROR_SUCCESS
;
472 static const MSIVIEWOPS storages_ops
=
475 STORAGES_fetch_stream
,
482 STORAGES_get_dimensions
,
483 STORAGES_get_column_info
,
486 STORAGES_find_matching_rows
,
495 static INT
add_storages_to_table(MSISTORAGESVIEW
*sv
)
497 STORAGE
*storage
= NULL
;
498 IEnumSTATSTG
*stgenum
= NULL
;
501 UINT count
= 0, size
;
503 hr
= IStorage_EnumElements(sv
->db
->storage
, 0, NULL
, 0, &stgenum
);
507 sv
->max_storages
= 1;
508 sv
->storages
= msi_alloc(sizeof(STORAGE
*));
515 hr
= IEnumSTATSTG_Next(stgenum
, 1, &stat
, &size
);
516 if (FAILED(hr
) || !size
)
519 if (stat
.type
!= STGTY_STORAGE
)
521 CoTaskMemFree(stat
.pwcsName
);
525 TRACE("enumerated storage %s\n", debugstr_w(stat
.pwcsName
));
527 storage
= create_storage(sv
, stat
.pwcsName
, NULL
);
531 CoTaskMemFree(stat
.pwcsName
);
535 IStorage_OpenStorage(sv
->db
->storage
, stat
.pwcsName
, NULL
,
536 STGM_READ
| STGM_SHARE_EXCLUSIVE
, NULL
, 0,
538 CoTaskMemFree(stat
.pwcsName
);
540 if (!storages_set_table_size(sv
, ++count
))
546 sv
->storages
[count
- 1] = storage
;
549 IEnumSTATSTG_Release(stgenum
);
553 UINT
STORAGES_CreateView(MSIDATABASE
*db
, MSIVIEW
**view
)
558 TRACE("(%p, %p)\n", db
, view
);
560 sv
= msi_alloc(sizeof(MSISTORAGESVIEW
));
562 return ERROR_FUNCTION_FAILED
;
564 sv
->view
.ops
= &storages_ops
;
567 rows
= add_storages_to_table(sv
);
571 return ERROR_FUNCTION_FAILED
;
575 *view
= (MSIVIEW
*)sv
;
577 return ERROR_SUCCESS
;