msvcrt/tests: Remove a space before a '\n'.
[wine/gsoc-2012-control.git] / dlls / msi / storages.c
blob302906b8466c0bbc08235d70355fc739c83c33ca
1 /*
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
21 #include <stdarg.h>
23 #define COBJMACROS
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"
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
45 UINT str_index;
46 LPWSTR name;
47 IStorage *storage;
48 } STORAGE;
50 typedef struct tagMSISTORAGESVIEW
52 MSIVIEW view;
53 MSIDATABASE *db;
54 STORAGE **storages;
55 UINT max_storages;
56 UINT num_rows;
57 UINT row_size;
58 } MSISTORAGESVIEW;
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 *));
66 if (!sv->storages)
67 return FALSE;
70 return TRUE;
73 static STORAGE *create_storage(MSISTORAGESVIEW *sv, LPWSTR name, IStorage *stg)
75 STORAGE *storage;
77 storage = msi_alloc(sizeof(STORAGE));
78 if (!storage)
79 return NULL;
81 storage->name = strdupW(name);
82 if (!storage->name)
84 msi_free(storage);
85 return NULL;
88 storage->str_index = msi_addstringW(sv->db->strings, 0, storage->name, -1, 1, StringNonPersistent);
89 storage->storage = stg;
91 if (storage->storage)
92 IStorage_AddRef(storage->storage);
94 return 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);
103 if (col != 1)
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;
138 STATSTG stat;
139 LPVOID data;
140 HRESULT hr;
141 DWORD size, read;
142 ULARGE_INTEGER offset;
144 hr = IStream_Stat(stm, &stat, STATFLAG_NONAME);
145 if (FAILED(hr))
146 return hr;
148 if (stat.cbSize.QuadPart >> 32)
150 ERR("Storage is too large\n");
151 return E_FAIL;
154 size = stat.cbSize.QuadPart;
155 data = msi_alloc(size);
156 if (!data)
157 return E_OUTOFMEMORY;
159 hr = IStream_Read(stm, data, size, &read);
160 if (FAILED(hr) || read != size)
161 goto done;
163 hr = CreateILockBytesOnHGlobal(NULL, TRUE, &lockbytes);
164 if (FAILED(hr))
165 goto done;
167 ZeroMemory(&offset, sizeof(ULARGE_INTEGER));
168 hr = ILockBytes_WriteAt(lockbytes, offset, data, size, &read);
169 if (FAILED(hr) || read != size)
170 goto done;
172 hr = StgOpenStorageOnILockBytes(lockbytes, NULL,
173 STGM_READWRITE | STGM_SHARE_DENY_NONE,
174 NULL, 0, stg);
175 if (FAILED(hr))
176 goto done;
178 done:
179 msi_free(data);
180 if (lockbytes) ILockBytes_Release(lockbytes);
181 return hr;
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;
188 IStream *stm;
189 LPWSTR name = NULL;
190 HRESULT hr;
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)
200 return r;
202 r = stream_to_storage(stm, &stg);
203 if (r != ERROR_SUCCESS)
205 IStream_Release(stm);
206 return r;
209 name = strdupW(MSI_RecordGetString(rec, 1));
210 if (!name)
212 r = ERROR_OUTOFMEMORY;
213 goto done;
216 hr = IStorage_CreateStorage(sv->db->storage, name,
217 STGM_WRITE | STGM_SHARE_EXCLUSIVE,
218 0, 0, &substg);
219 if (FAILED(hr))
221 r = ERROR_FUNCTION_FAILED;
222 goto done;
225 hr = IStorage_CopyTo(stg, 0, NULL, NULL, substg);
226 if (FAILED(hr))
228 r = ERROR_FUNCTION_FAILED;
229 goto done;
232 sv->storages[row] = create_storage(sv, name, stg);
233 if (!sv->storages[row])
234 r = ERROR_FUNCTION_FAILED;
236 done:
237 msi_free(name);
239 if (substg) IStorage_Release(substg);
240 IStorage_Release(stg);
241 IStream_Release(stm);
243 return r;
246 static UINT STORAGES_insert_row(struct tagMSIVIEW *view, MSIRECORD *rec, BOOL temporary)
248 MSISTORAGESVIEW *sv = (MSISTORAGESVIEW *)view;
250 if (!storages_set_table_size(sv, ++sv->num_rows))
251 return ERROR_FUNCTION_FAILED;
253 return STORAGES_set_row(view, sv->num_rows - 1, rec, 0);
256 static UINT STORAGES_delete_row(struct tagMSIVIEW *view, UINT row)
258 FIXME("(%p %d): stub!\n", view, row);
259 return ERROR_SUCCESS;
262 static UINT STORAGES_execute(struct tagMSIVIEW *view, MSIRECORD *record)
264 TRACE("(%p, %p)\n", view, record);
265 return ERROR_SUCCESS;
268 static UINT STORAGES_close(struct tagMSIVIEW *view)
270 TRACE("(%p)\n", view);
271 return ERROR_SUCCESS;
274 static UINT STORAGES_get_dimensions(struct tagMSIVIEW *view, UINT *rows, UINT *cols)
276 MSISTORAGESVIEW *sv = (MSISTORAGESVIEW *)view;
278 TRACE("(%p, %p, %p)\n", view, rows, cols);
280 if (cols) *cols = NUM_STORAGES_COLS;
281 if (rows) *rows = sv->num_rows;
283 return ERROR_SUCCESS;
286 static UINT STORAGES_get_column_info(struct tagMSIVIEW *view,
287 UINT n, LPWSTR *name, UINT *type)
289 LPCWSTR name_ptr = NULL;
291 static const WCHAR Name[] = {'N','a','m','e',0};
292 static const WCHAR Data[] = {'D','a','t','a',0};
294 TRACE("(%p, %d, %p, %p)\n", view, n, name, type);
296 if (n == 0 || n > NUM_STORAGES_COLS)
297 return ERROR_INVALID_PARAMETER;
299 switch (n)
301 case 1:
302 name_ptr = Name;
303 if (type) *type = MSITYPE_STRING | MAX_STORAGES_NAME_LEN;
304 break;
306 case 2:
307 name_ptr = Data;
308 if (type) *type = MSITYPE_STRING | MSITYPE_VALID | MSITYPE_NULLABLE;
309 break;
312 if (name)
314 *name = strdupW(name_ptr);
315 if (!*name) return ERROR_FUNCTION_FAILED;
318 return ERROR_SUCCESS;
321 static UINT storages_find_row(MSISTORAGESVIEW *sv, MSIRECORD *rec, UINT *row)
323 LPCWSTR str;
324 UINT i, id, data;
326 str = MSI_RecordGetString(rec, 1);
327 msi_string2idW(sv->db->strings, str, &id);
329 for (i = 0; i < sv->num_rows; i++)
331 STORAGES_fetch_int(&sv->view, i, 1, &data);
333 if (data == id)
335 *row = i;
336 return ERROR_SUCCESS;
340 return ERROR_FUNCTION_FAILED;
343 static UINT storages_modify_update(struct tagMSIVIEW *view, MSIRECORD *rec)
345 MSISTORAGESVIEW *sv = (MSISTORAGESVIEW *)view;
346 UINT r, row;
348 r = storages_find_row(sv, rec, &row);
349 if (r != ERROR_SUCCESS)
350 return ERROR_FUNCTION_FAILED;
352 return STORAGES_set_row(view, row, rec, 0);
355 static UINT storages_modify_assign(struct tagMSIVIEW *view, MSIRECORD *rec)
357 MSISTORAGESVIEW *sv = (MSISTORAGESVIEW *)view;
358 UINT r, row;
360 r = storages_find_row(sv, rec, &row);
361 if (r == ERROR_SUCCESS)
362 return storages_modify_update(view, rec);
364 return STORAGES_insert_row(view, rec, FALSE);
367 static UINT STORAGES_modify(struct tagMSIVIEW *view, MSIMODIFY eModifyMode, MSIRECORD *rec, UINT row)
369 UINT r;
371 TRACE("%p %d %p\n", view, eModifyMode, rec);
373 switch (eModifyMode)
375 case MSIMODIFY_ASSIGN:
376 r = storages_modify_assign(view, rec);
377 break;
379 case MSIMODIFY_INSERT:
380 r = STORAGES_insert_row(view, rec, FALSE);
381 break;
383 case MSIMODIFY_UPDATE:
384 r = storages_modify_update(view, rec);
385 break;
387 case MSIMODIFY_VALIDATE_NEW:
388 case MSIMODIFY_INSERT_TEMPORARY:
389 case MSIMODIFY_REFRESH:
390 case MSIMODIFY_REPLACE:
391 case MSIMODIFY_MERGE:
392 case MSIMODIFY_DELETE:
393 case MSIMODIFY_VALIDATE:
394 case MSIMODIFY_VALIDATE_FIELD:
395 case MSIMODIFY_VALIDATE_DELETE:
396 FIXME("%p %d %p - mode not implemented\n", view, eModifyMode, rec );
397 r = ERROR_CALL_NOT_IMPLEMENTED;
398 break;
400 default:
401 r = ERROR_INVALID_DATA;
404 return r;
407 static UINT STORAGES_delete(struct tagMSIVIEW *view)
409 MSISTORAGESVIEW *sv = (MSISTORAGESVIEW *)view;
410 UINT i;
412 TRACE("(%p)\n", view);
414 for (i = 0; i < sv->num_rows; i++)
416 if (sv->storages[i]->storage)
417 IStorage_Release(sv->storages[i]->storage);
419 msi_free(sv->storages[i]);
422 msi_free(sv->storages);
423 sv->storages = NULL;
425 return ERROR_SUCCESS;
428 static UINT STORAGES_find_matching_rows(struct tagMSIVIEW *view, UINT col,
429 UINT val, UINT *row, MSIITERHANDLE *handle)
431 MSISTORAGESVIEW *sv = (MSISTORAGESVIEW *)view;
432 UINT index = (UINT)*handle;
434 TRACE("(%d, %d): %d\n", *row, col, val);
436 if (col == 0 || col > NUM_STORAGES_COLS)
437 return ERROR_INVALID_PARAMETER;
439 while (index < sv->num_rows)
441 if (sv->storages[index]->str_index == val)
443 *row = index;
444 break;
447 index++;
450 *handle = (MSIITERHANDLE)++index;
451 if (index >= sv->num_rows)
452 return ERROR_NO_MORE_ITEMS;
454 return ERROR_SUCCESS;
457 static const MSIVIEWOPS storages_ops =
459 STORAGES_fetch_int,
460 STORAGES_fetch_stream,
461 STORAGES_get_row,
462 STORAGES_set_row,
463 STORAGES_insert_row,
464 STORAGES_delete_row,
465 STORAGES_execute,
466 STORAGES_close,
467 STORAGES_get_dimensions,
468 STORAGES_get_column_info,
469 STORAGES_modify,
470 STORAGES_delete,
471 STORAGES_find_matching_rows,
472 NULL,
473 NULL,
474 NULL,
475 NULL,
476 NULL,
477 NULL,
480 static INT add_storages_to_table(MSISTORAGESVIEW *sv)
482 STORAGE *storage = NULL;
483 IEnumSTATSTG *stgenum = NULL;
484 STATSTG stat;
485 HRESULT hr;
486 UINT count = 0, size;
488 hr = IStorage_EnumElements(sv->db->storage, 0, NULL, 0, &stgenum);
489 if (FAILED(hr))
490 return -1;
492 sv->max_storages = 1;
493 sv->storages = msi_alloc(sizeof(STORAGE *));
494 if (!sv->storages)
495 return -1;
497 while (TRUE)
499 size = 0;
500 hr = IEnumSTATSTG_Next(stgenum, 1, &stat, &size);
501 if (FAILED(hr) || !size)
502 break;
504 if (stat.type != STGTY_STORAGE)
506 CoTaskMemFree(stat.pwcsName);
507 continue;
510 TRACE("enumerated storage %s\n", debugstr_w(stat.pwcsName));
512 storage = create_storage(sv, stat.pwcsName, NULL);
513 if (!storage)
515 count = -1;
516 CoTaskMemFree(stat.pwcsName);
517 break;
520 IStorage_OpenStorage(sv->db->storage, stat.pwcsName, NULL,
521 STGM_READ | STGM_SHARE_EXCLUSIVE, NULL, 0,
522 &storage->storage);
523 CoTaskMemFree(stat.pwcsName);
525 if (!storages_set_table_size(sv, ++count))
527 count = -1;
528 break;
531 sv->storages[count - 1] = storage;
534 IEnumSTATSTG_Release(stgenum);
535 return count;
538 UINT STORAGES_CreateView(MSIDATABASE *db, MSIVIEW **view)
540 MSISTORAGESVIEW *sv;
541 INT rows;
543 TRACE("(%p, %p)\n", db, view);
545 sv = msi_alloc(sizeof(MSISTORAGESVIEW));
546 if (!sv)
547 return ERROR_FUNCTION_FAILED;
549 sv->view.ops = &storages_ops;
550 sv->db = db;
552 rows = add_storages_to_table(sv);
553 if (rows < 0)
554 return ERROR_FUNCTION_FAILED;
556 sv->num_rows = rows;
558 *view = (MSIVIEW *)sv;
560 return ERROR_SUCCESS;