kernel32/tests: Correctly skip tests when the Unicode drive functions are not available.
[wine] / dlls / msi / storages.c
1 /*
2  * Implementation of the Microsoft Installer (msi.dll)
3  *
4  * Copyright 2008 James Hawkins
5  *
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.
10  *
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.
15  *
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
19  */
20
21 #include <stdarg.h>
22
23 #define COBJMACROS
24
25 #include "windef.h"
26 #include "winbase.h"
27 #include "winuser.h"
28 #include "winerror.h"
29 #include "ole2.h"
30 #include "msi.h"
31 #include "msiquery.h"
32 #include "objbase.h"
33 #include "msipriv.h"
34 #include "query.h"
35
36 #include "wine/debug.h"
37
38 WINE_DEFAULT_DEBUG_CHANNEL(msidb);
39
40 #define NUM_STORAGES_COLS    2
41 #define MAX_STORAGES_NAME_LEN 62
42
43 typedef struct tabSTORAGE
44 {
45     UINT str_index;
46     LPWSTR name;
47     IStorage *storage;
48 } STORAGE;
49
50 typedef struct tagMSISTORAGESVIEW
51 {
52     MSIVIEW view;
53     MSIDATABASE *db;
54     STORAGE **storages;
55     UINT max_storages;
56     UINT num_rows;
57     UINT row_size;
58 } MSISTORAGESVIEW;
59
60 static BOOL storages_set_table_size(MSISTORAGESVIEW *sv, UINT size)
61 {
62     if (size >= sv->max_storages)
63     {
64         sv->max_storages *= 2;
65         sv->storages = msi_realloc(sv->storages, sv->max_storages * sizeof(STORAGE *));
66         if (!sv->storages)
67             return FALSE;
68     }
69
70     return TRUE;
71 }
72
73 static STORAGE *create_storage(MSISTORAGESVIEW *sv, LPWSTR name, IStorage *stg)
74 {
75     STORAGE *storage;
76
77     storage = msi_alloc(sizeof(STORAGE));
78     if (!storage)
79         return NULL;
80
81     storage->name = strdupW(name);
82     if (!storage->name)
83     {
84         msi_free(storage);
85         return NULL;
86     }
87
88     storage->str_index = msi_addstringW(sv->db->strings, 0, storage->name, -1, 1, StringNonPersistent);
89     storage->storage = stg;
90
91     if (storage->storage)
92         IStorage_AddRef(storage->storage);
93
94     return storage;
95 }
96
97 static UINT STORAGES_fetch_int(struct tagMSIVIEW *view, UINT row, UINT col, UINT *val)
98 {
99     MSISTORAGESVIEW *sv = (MSISTORAGESVIEW *)view;
100
101     TRACE("(%p, %d, %d, %p)\n", view, row, col, val);
102
103     if (col != 1)
104         return ERROR_INVALID_PARAMETER;
105
106     if (row >= sv->num_rows)
107         return ERROR_NO_MORE_ITEMS;
108
109     *val = sv->storages[row]->str_index;
110
111     return ERROR_SUCCESS;
112 }
113
114 static UINT STORAGES_fetch_stream(struct tagMSIVIEW *view, UINT row, UINT col, IStream **stm)
115 {
116     MSISTORAGESVIEW *sv = (MSISTORAGESVIEW *)view;
117
118     TRACE("(%p, %d, %d, %p)\n", view, row, col, stm);
119
120     if (row >= sv->num_rows)
121         return ERROR_FUNCTION_FAILED;
122
123     return ERROR_INVALID_DATA;
124 }
125
126 static UINT STORAGES_get_row( struct tagMSIVIEW *view, UINT row, MSIRECORD **rec )
127 {
128     MSISTORAGESVIEW *sv = (MSISTORAGESVIEW *)view;
129
130     FIXME("%p %d %p\n", sv, row, rec);
131
132     return ERROR_CALL_NOT_IMPLEMENTED;
133 }
134
135 static HRESULT stream_to_storage(IStream *stm, IStorage **stg)
136 {
137     ILockBytes *lockbytes = NULL;
138     STATSTG stat;
139     LPVOID data;
140     HRESULT hr;
141     DWORD size, read;
142     ULARGE_INTEGER offset;
143
144     hr = IStream_Stat(stm, &stat, STATFLAG_NONAME);
145     if (FAILED(hr))
146         return hr;
147
148     if (stat.cbSize.QuadPart >> 32)
149     {
150         ERR("Storage is too large\n");
151         return E_FAIL;
152     }
153
154     size = stat.cbSize.QuadPart;
155     data = msi_alloc(size);
156     if (!data)
157         return E_OUTOFMEMORY;
158
159     hr = IStream_Read(stm, data, size, &read);
160     if (FAILED(hr) || read != size)
161         goto done;
162
163     hr = CreateILockBytesOnHGlobal(NULL, TRUE, &lockbytes);
164     if (FAILED(hr))
165         goto done;
166
167     ZeroMemory(&offset, sizeof(ULARGE_INTEGER));
168     hr = ILockBytes_WriteAt(lockbytes, offset, data, size, &read);
169     if (FAILED(hr) || read != size)
170         goto done;
171
172     hr = StgOpenStorageOnILockBytes(lockbytes, NULL,
173                                     STGM_READWRITE | STGM_SHARE_DENY_NONE,
174                                     NULL, 0, stg);
175     if (FAILED(hr))
176         goto done;
177
178 done:
179     msi_free(data);
180     if (lockbytes) ILockBytes_Release(lockbytes);
181     return hr;
182 }
183
184 static UINT STORAGES_set_row(struct tagMSIVIEW *view, UINT row, MSIRECORD *rec, UINT mask)
185 {
186     MSISTORAGESVIEW *sv = (MSISTORAGESVIEW *)view;
187     IStorage *stg, *substg = NULL;
188     IStream *stm;
189     LPWSTR name = NULL;
190     HRESULT hr;
191     UINT r = ERROR_FUNCTION_FAILED;
192
193     TRACE("(%p, %p)\n", view, rec);
194
195     if (row > sv->num_rows)
196         return ERROR_FUNCTION_FAILED;
197
198     r = MSI_RecordGetIStream(rec, 2, &stm);
199     if (r != ERROR_SUCCESS)
200         return r;
201
202     r = stream_to_storage(stm, &stg);
203     if (r != ERROR_SUCCESS)
204     {
205         IStream_Release(stm);
206         return r;
207     }
208
209     name = strdupW(MSI_RecordGetString(rec, 1));
210     if (!name)
211     {
212         r = ERROR_OUTOFMEMORY;
213         goto done;
214     }
215
216     hr = IStorage_CreateStorage(sv->db->storage, name,
217                                 STGM_WRITE | STGM_SHARE_EXCLUSIVE,
218                                 0, 0, &substg);
219     if (FAILED(hr))
220     {
221         r = ERROR_FUNCTION_FAILED;
222         goto done;
223     }
224
225     hr = IStorage_CopyTo(stg, 0, NULL, NULL, substg);
226     if (FAILED(hr))
227     {
228         r = ERROR_FUNCTION_FAILED;
229         goto done;
230     }
231
232     sv->storages[row] = create_storage(sv, name, stg);
233     if (!sv->storages[row])
234         r = ERROR_FUNCTION_FAILED;
235
236 done:
237     msi_free(name);
238
239     if (substg) IStorage_Release(substg);
240     IStorage_Release(stg);
241     IStream_Release(stm);
242
243     return r;
244 }
245
246 static UINT STORAGES_insert_row(struct tagMSIVIEW *view, MSIRECORD *rec, UINT row, BOOL temporary)
247 {
248     MSISTORAGESVIEW *sv = (MSISTORAGESVIEW *)view;
249
250     if (!storages_set_table_size(sv, ++sv->num_rows))
251         return ERROR_FUNCTION_FAILED;
252
253     if (row == -1)
254         row = sv->num_rows - 1;
255
256     /* FIXME have to readjust rows */
257
258     return STORAGES_set_row(view, row, rec, 0);
259 }
260
261 static UINT STORAGES_delete_row(struct tagMSIVIEW *view, UINT row)
262 {
263     FIXME("(%p %d): stub!\n", view, row);
264     return ERROR_SUCCESS;
265 }
266
267 static UINT STORAGES_execute(struct tagMSIVIEW *view, MSIRECORD *record)
268 {
269     TRACE("(%p, %p)\n", view, record);
270     return ERROR_SUCCESS;
271 }
272
273 static UINT STORAGES_close(struct tagMSIVIEW *view)
274 {
275     TRACE("(%p)\n", view);
276     return ERROR_SUCCESS;
277 }
278
279 static UINT STORAGES_get_dimensions(struct tagMSIVIEW *view, UINT *rows, UINT *cols)
280 {
281     MSISTORAGESVIEW *sv = (MSISTORAGESVIEW *)view;
282
283     TRACE("(%p, %p, %p)\n", view, rows, cols);
284
285     if (cols) *cols = NUM_STORAGES_COLS;
286     if (rows) *rows = sv->num_rows;
287
288     return ERROR_SUCCESS;
289 }
290
291 static UINT STORAGES_get_column_info(struct tagMSIVIEW *view, UINT n,
292                                      LPWSTR *name, UINT *type, BOOL *temporary)
293 {
294     LPCWSTR name_ptr = NULL;
295
296     static const WCHAR Name[] = {'N','a','m','e',0};
297     static const WCHAR Data[] = {'D','a','t','a',0};
298
299     TRACE("(%p, %d, %p, %p, %p)\n", view, n, name, type, temporary);
300
301     if (n == 0 || n > NUM_STORAGES_COLS)
302         return ERROR_INVALID_PARAMETER;
303
304     switch (n)
305     {
306     case 1:
307         name_ptr = Name;
308         if (type) *type = MSITYPE_STRING | MAX_STORAGES_NAME_LEN;
309         break;
310
311     case 2:
312         name_ptr = Data;
313         if (type) *type = MSITYPE_STRING | MSITYPE_VALID | MSITYPE_NULLABLE;
314         break;
315     }
316
317     if (name)
318     {
319         *name = strdupW(name_ptr);
320         if (!*name) return ERROR_FUNCTION_FAILED;
321     }
322
323     if (temporary)
324         *temporary = FALSE;
325
326     return ERROR_SUCCESS;
327 }
328
329 static UINT storages_find_row(MSISTORAGESVIEW *sv, MSIRECORD *rec, UINT *row)
330 {
331     LPCWSTR str;
332     UINT i, id, data;
333
334     str = MSI_RecordGetString(rec, 1);
335     msi_string2idW(sv->db->strings, str, &id);
336
337     for (i = 0; i < sv->num_rows; i++)
338     {
339         STORAGES_fetch_int(&sv->view, i, 1, &data);
340
341         if (data == id)
342         {
343             *row = i;
344             return ERROR_SUCCESS;
345         }
346     }
347
348     return ERROR_FUNCTION_FAILED;
349 }
350
351 static UINT storages_modify_update(struct tagMSIVIEW *view, MSIRECORD *rec)
352 {
353     MSISTORAGESVIEW *sv = (MSISTORAGESVIEW *)view;
354     UINT r, row;
355
356     r = storages_find_row(sv, rec, &row);
357     if (r != ERROR_SUCCESS)
358         return ERROR_FUNCTION_FAILED;
359
360     return STORAGES_set_row(view, row, rec, 0);
361 }
362
363 static UINT storages_modify_assign(struct tagMSIVIEW *view, MSIRECORD *rec)
364 {
365     MSISTORAGESVIEW *sv = (MSISTORAGESVIEW *)view;
366     UINT r, row;
367
368     r = storages_find_row(sv, rec, &row);
369     if (r == ERROR_SUCCESS)
370         return storages_modify_update(view, rec);
371
372     return STORAGES_insert_row(view, rec, -1, FALSE);
373 }
374
375 static UINT STORAGES_modify(struct tagMSIVIEW *view, MSIMODIFY eModifyMode, MSIRECORD *rec, UINT row)
376 {
377     UINT r;
378
379     TRACE("%p %d %p\n", view, eModifyMode, rec);
380
381     switch (eModifyMode)
382     {
383     case MSIMODIFY_ASSIGN:
384         r = storages_modify_assign(view, rec);
385         break;
386
387     case MSIMODIFY_INSERT:
388         r = STORAGES_insert_row(view, rec, -1, FALSE);
389         break;
390
391     case MSIMODIFY_UPDATE:
392         r = storages_modify_update(view, rec);
393         break;
394
395     case MSIMODIFY_VALIDATE_NEW:
396     case MSIMODIFY_INSERT_TEMPORARY:
397     case MSIMODIFY_REFRESH:
398     case MSIMODIFY_REPLACE:
399     case MSIMODIFY_MERGE:
400     case MSIMODIFY_DELETE:
401     case MSIMODIFY_VALIDATE:
402     case MSIMODIFY_VALIDATE_FIELD:
403     case MSIMODIFY_VALIDATE_DELETE:
404         FIXME("%p %d %p - mode not implemented\n", view, eModifyMode, rec );
405         r = ERROR_CALL_NOT_IMPLEMENTED;
406         break;
407
408     default:
409         r = ERROR_INVALID_DATA;
410     }
411
412     return r;
413 }
414
415 static UINT STORAGES_delete(struct tagMSIVIEW *view)
416 {
417     MSISTORAGESVIEW *sv = (MSISTORAGESVIEW *)view;
418     UINT i;
419
420     TRACE("(%p)\n", view);
421
422     for (i = 0; i < sv->num_rows; i++)
423     {
424         if (sv->storages[i]->storage)
425             IStorage_Release(sv->storages[i]->storage);
426
427         msi_free(sv->storages[i]);
428     }
429
430     msi_free(sv->storages);
431     sv->storages = NULL;
432
433     return ERROR_SUCCESS;
434 }
435
436 static UINT STORAGES_find_matching_rows(struct tagMSIVIEW *view, UINT col,
437                                        UINT val, UINT *row, MSIITERHANDLE *handle)
438 {
439     MSISTORAGESVIEW *sv = (MSISTORAGESVIEW *)view;
440     UINT index = (UINT)*handle;
441
442     TRACE("(%d, %d): %d\n", *row, col, val);
443
444     if (col == 0 || col > NUM_STORAGES_COLS)
445         return ERROR_INVALID_PARAMETER;
446
447     while (index < sv->num_rows)
448     {
449         if (sv->storages[index]->str_index == val)
450         {
451             *row = index;
452             break;
453         }
454
455         index++;
456     }
457
458     *handle = (MSIITERHANDLE)++index;
459     if (index >= sv->num_rows)
460         return ERROR_NO_MORE_ITEMS;
461
462     return ERROR_SUCCESS;
463 }
464
465 static const MSIVIEWOPS storages_ops =
466 {
467     STORAGES_fetch_int,
468     STORAGES_fetch_stream,
469     STORAGES_get_row,
470     STORAGES_set_row,
471     STORAGES_insert_row,
472     STORAGES_delete_row,
473     STORAGES_execute,
474     STORAGES_close,
475     STORAGES_get_dimensions,
476     STORAGES_get_column_info,
477     STORAGES_modify,
478     STORAGES_delete,
479     STORAGES_find_matching_rows,
480     NULL,
481     NULL,
482     NULL,
483     NULL,
484     NULL,
485     NULL,
486 };
487
488 static INT add_storages_to_table(MSISTORAGESVIEW *sv)
489 {
490     STORAGE *storage = NULL;
491     IEnumSTATSTG *stgenum = NULL;
492     STATSTG stat;
493     HRESULT hr;
494     UINT count = 0, size;
495
496     hr = IStorage_EnumElements(sv->db->storage, 0, NULL, 0, &stgenum);
497     if (FAILED(hr))
498         return -1;
499
500     sv->max_storages = 1;
501     sv->storages = msi_alloc(sizeof(STORAGE *));
502     if (!sv->storages)
503         return -1;
504
505     while (TRUE)
506     {
507         size = 0;
508         hr = IEnumSTATSTG_Next(stgenum, 1, &stat, &size);
509         if (FAILED(hr) || !size)
510             break;
511
512         if (stat.type != STGTY_STORAGE)
513         {
514             CoTaskMemFree(stat.pwcsName);
515             continue;
516         }
517
518         TRACE("enumerated storage %s\n", debugstr_w(stat.pwcsName));
519
520         storage = create_storage(sv, stat.pwcsName, NULL);
521         if (!storage)
522         {
523             count = -1;
524             CoTaskMemFree(stat.pwcsName);
525             break;
526         }
527
528         IStorage_OpenStorage(sv->db->storage, stat.pwcsName, NULL,
529                              STGM_READ | STGM_SHARE_EXCLUSIVE, NULL, 0,
530                              &storage->storage);
531         CoTaskMemFree(stat.pwcsName);
532
533         if (!storages_set_table_size(sv, ++count))
534         {
535             count = -1;
536             break;
537         }
538
539         sv->storages[count - 1] = storage;
540     }
541
542     IEnumSTATSTG_Release(stgenum);
543     return count;
544 }
545
546 UINT STORAGES_CreateView(MSIDATABASE *db, MSIVIEW **view)
547 {
548     MSISTORAGESVIEW *sv;
549     INT rows;
550
551     TRACE("(%p, %p)\n", db, view);
552
553     sv = msi_alloc(sizeof(MSISTORAGESVIEW));
554     if (!sv)
555         return ERROR_FUNCTION_FAILED;
556
557     sv->view.ops = &storages_ops;
558     sv->db = db;
559
560     rows = add_storages_to_table(sv);
561     if (rows < 0)
562         return ERROR_FUNCTION_FAILED;
563
564     sv->num_rows = rows;
565
566     *view = (MSIVIEW *)sv;
567
568     return ERROR_SUCCESS;
569 }