msi: Read the disk prompt source list property from the user-unmanaged context.
[wine/wine-kai.git] / dlls / msi / streams.c
blobdefcad508bf7c1fc9a86a007c30328be52723cfa
1 /*
2 * Implementation of the Microsoft Installer (msi.dll)
4 * Copyright 2007 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 "winerror.h"
28 #include "msi.h"
29 #include "msiquery.h"
30 #include "objbase.h"
31 #include "msipriv.h"
33 #include "wine/debug.h"
35 WINE_DEFAULT_DEBUG_CHANNEL(msidb);
37 #define NUM_STREAMS_COLS 2
38 #define MAX_STREAM_NAME_LEN 62
40 typedef struct tabSTREAM
42 UINT str_index;
43 LPWSTR name;
44 IStream *stream;
45 } STREAM;
47 typedef struct tagMSISTREAMSVIEW
49 MSIVIEW view;
50 MSIDATABASE *db;
51 STREAM **streams;
52 UINT max_streams;
53 UINT num_rows;
54 UINT row_size;
55 } MSISTREAMSVIEW;
57 static BOOL streams_set_table_size(MSISTREAMSVIEW *sv, UINT size)
59 if (size >= sv->max_streams)
61 sv->max_streams *= 2;
62 sv->streams = msi_realloc(sv->streams, sv->max_streams * sizeof(STREAM *));
63 if (!sv->streams)
64 return FALSE;
67 return TRUE;
70 static STREAM *create_stream(MSISTREAMSVIEW *sv, LPWSTR name, BOOL encoded, IStream *stm)
72 STREAM *stream;
73 WCHAR decoded[MAX_STREAM_NAME_LEN];
74 LPWSTR ptr = name;
76 stream = msi_alloc(sizeof(STREAM));
77 if (!stream)
78 return NULL;
80 if (encoded)
82 decode_streamname(name, decoded);
83 ptr = decoded;
84 TRACE("stream -> %s %s\n", debugstr_w(name), debugstr_w(decoded));
87 stream->name = strdupW(ptr);
88 if (!stream->name)
90 msi_free(stream);
91 return NULL;
94 stream->str_index = msi_addstringW(sv->db->strings, 0, stream->name, -1, 1, StringNonPersistent);
95 stream->stream = stm;
96 return stream;
99 static UINT STREAMS_fetch_int(struct tagMSIVIEW *view, UINT row, UINT col, UINT *val)
101 MSISTREAMSVIEW *sv = (MSISTREAMSVIEW *)view;
103 TRACE("(%p, %d, %d, %p)\n", view, row, col, val);
105 if (col != 1)
106 return ERROR_INVALID_PARAMETER;
108 if (row >= sv->num_rows)
109 return ERROR_NO_MORE_ITEMS;
111 *val = sv->streams[row]->str_index;
113 return ERROR_SUCCESS;
116 static UINT STREAMS_fetch_stream(struct tagMSIVIEW *view, UINT row, UINT col, IStream **stm)
118 MSISTREAMSVIEW *sv = (MSISTREAMSVIEW *)view;
120 TRACE("(%p, %d, %d, %p)\n", view, row, col, stm);
122 if (row >= sv->num_rows)
123 return ERROR_FUNCTION_FAILED;
125 IStream_AddRef(sv->streams[row]->stream);
126 *stm = sv->streams[row]->stream;
128 return ERROR_SUCCESS;
131 static UINT STREAMS_get_row( struct tagMSIVIEW *view, UINT row, MSIRECORD **rec )
133 MSISTREAMSVIEW *sv = (MSISTREAMSVIEW *)view;
135 FIXME("%p %d %p\n", sv, row, rec);
137 return ERROR_CALL_NOT_IMPLEMENTED;
140 static UINT STREAMS_set_row(struct tagMSIVIEW *view, UINT row, MSIRECORD *rec, UINT mask)
142 MSISTREAMSVIEW *sv = (MSISTREAMSVIEW *)view;
143 STREAM *stream;
144 IStream *stm;
145 STATSTG stat;
146 LPWSTR name = NULL;
147 USHORT *data = NULL;
148 HRESULT hr;
149 ULONG count;
150 UINT r = ERROR_FUNCTION_FAILED;
152 TRACE("(%p, %p)\n", view, rec);
154 if (row > sv->num_rows)
155 return ERROR_FUNCTION_FAILED;
157 r = MSI_RecordGetIStream(rec, 2, &stm);
158 if (r != ERROR_SUCCESS)
159 return r;
161 hr = IStream_Stat(stm, &stat, STATFLAG_NONAME);
162 if (FAILED(hr))
164 WARN("failed to stat stream: %08x\n", hr);
165 goto done;
168 if (stat.cbSize.QuadPart >> 32)
169 goto done;
171 data = msi_alloc(stat.cbSize.QuadPart);
172 if (!data)
173 goto done;
175 hr = IStream_Read(stm, data, stat.cbSize.QuadPart, &count);
176 if (FAILED(hr) || count != stat.cbSize.QuadPart)
178 WARN("failed to read stream: %08x\n", hr);
179 goto done;
182 name = strdupW(MSI_RecordGetString(rec, 1));
183 if (!name)
184 goto done;
186 r = write_stream_data(sv->db->storage, name, data, count, FALSE);
187 if (r != ERROR_SUCCESS)
189 WARN("failed to write stream data: %d\n", r);
190 goto done;
193 stream = create_stream(sv, name, FALSE, NULL);
194 if (!stream)
195 goto done;
197 IStorage_OpenStream(sv->db->storage, name, 0,
198 STGM_READ | STGM_SHARE_EXCLUSIVE, 0, &stream->stream);
200 sv->streams[row] = stream;
202 done:
203 msi_free(name);
204 msi_free(data);
206 IStream_Release(stm);
208 return r;
211 static UINT STREAMS_insert_row(struct tagMSIVIEW *view, MSIRECORD *rec, BOOL temporary)
213 MSISTREAMSVIEW *sv = (MSISTREAMSVIEW *)view;
215 if (!streams_set_table_size(sv, ++sv->num_rows))
216 return ERROR_FUNCTION_FAILED;
218 return STREAMS_set_row(view, sv->num_rows - 1, rec, 0);
221 static UINT STREAMS_delete_row(struct tagMSIVIEW *view, UINT row)
223 FIXME("(%p %d): stub!\n", view, row);
224 return ERROR_SUCCESS;
227 static UINT STREAMS_execute(struct tagMSIVIEW *view, MSIRECORD *record)
229 TRACE("(%p, %p)\n", view, record);
230 return ERROR_SUCCESS;
233 static UINT STREAMS_close(struct tagMSIVIEW *view)
235 TRACE("(%p)\n", view);
236 return ERROR_SUCCESS;
239 static UINT STREAMS_get_dimensions(struct tagMSIVIEW *view, UINT *rows, UINT *cols)
241 MSISTREAMSVIEW *sv = (MSISTREAMSVIEW *)view;
243 TRACE("(%p, %p, %p)\n", view, rows, cols);
245 if (cols) *cols = NUM_STREAMS_COLS;
246 if (rows) *rows = sv->num_rows;
248 return ERROR_SUCCESS;
251 static UINT STREAMS_get_column_info(struct tagMSIVIEW *view,
252 UINT n, LPWSTR *name, UINT *type)
254 LPCWSTR name_ptr = NULL;
256 static const WCHAR Name[] = {'N','a','m','e',0};
257 static const WCHAR Data[] = {'D','a','t','a',0};
259 TRACE("(%p, %d, %p, %p)\n", view, n, name, type);
261 if (n == 0 || n > NUM_STREAMS_COLS)
262 return ERROR_INVALID_PARAMETER;
264 switch (n)
266 case 1:
267 name_ptr = Name;
268 if (type) *type = MSITYPE_STRING | MAX_STREAM_NAME_LEN;
269 break;
271 case 2:
272 name_ptr = Data;
273 if (type) *type = MSITYPE_STRING | MSITYPE_VALID | MSITYPE_NULLABLE;
274 break;
277 if (name)
279 *name = strdupW(name_ptr);
280 if (!*name) return ERROR_FUNCTION_FAILED;
283 return ERROR_SUCCESS;
286 static UINT streams_find_row(MSISTREAMSVIEW *sv, MSIRECORD *rec, UINT *row)
288 LPCWSTR str;
289 UINT i, id, data;
291 str = MSI_RecordGetString(rec, 1);
292 msi_string2idW(sv->db->strings, str, &id);
294 for (i = 0; i < sv->num_rows; i++)
296 STREAMS_fetch_int(&sv->view, i, 1, &data);
298 if (data == id)
300 *row = i;
301 return ERROR_SUCCESS;
305 return ERROR_FUNCTION_FAILED;
308 static UINT streams_modify_update(struct tagMSIVIEW *view, MSIRECORD *rec)
310 MSISTREAMSVIEW *sv = (MSISTREAMSVIEW *)view;
311 UINT r, row;
313 r = streams_find_row(sv, rec, &row);
314 if (r != ERROR_SUCCESS)
315 return ERROR_FUNCTION_FAILED;
317 return STREAMS_set_row(view, row, rec, 0);
320 static UINT streams_modify_assign(struct tagMSIVIEW *view, MSIRECORD *rec)
322 MSISTREAMSVIEW *sv = (MSISTREAMSVIEW *)view;
323 UINT r, row;
325 r = streams_find_row(sv, rec, &row);
326 if (r == ERROR_SUCCESS)
327 return streams_modify_update(view, rec);
329 return STREAMS_insert_row(view, rec, FALSE);
332 static UINT STREAMS_modify(struct tagMSIVIEW *view, MSIMODIFY eModifyMode, MSIRECORD *rec, UINT row)
334 UINT r;
336 TRACE("%p %d %p\n", view, eModifyMode, rec);
338 switch (eModifyMode)
340 case MSIMODIFY_ASSIGN:
341 r = streams_modify_assign(view, rec);
342 break;
344 case MSIMODIFY_INSERT:
345 r = STREAMS_insert_row(view, rec, FALSE);
346 break;
348 case MSIMODIFY_UPDATE:
349 r = streams_modify_update(view, rec);
350 break;
352 case MSIMODIFY_VALIDATE_NEW:
353 case MSIMODIFY_INSERT_TEMPORARY:
354 case MSIMODIFY_REFRESH:
355 case MSIMODIFY_REPLACE:
356 case MSIMODIFY_MERGE:
357 case MSIMODIFY_DELETE:
358 case MSIMODIFY_VALIDATE:
359 case MSIMODIFY_VALIDATE_FIELD:
360 case MSIMODIFY_VALIDATE_DELETE:
361 FIXME("%p %d %p - mode not implemented\n", view, eModifyMode, rec );
362 r = ERROR_CALL_NOT_IMPLEMENTED;
363 break;
365 default:
366 r = ERROR_INVALID_DATA;
369 return r;
372 static UINT STREAMS_delete(struct tagMSIVIEW *view)
374 MSISTREAMSVIEW *sv = (MSISTREAMSVIEW *)view;
375 UINT i;
377 TRACE("(%p)\n", view);
379 for (i = 0; i < sv->num_rows; i++)
381 if (sv->streams[i]->stream)
382 IStream_Release(sv->streams[i]->stream);
383 msi_free(sv->streams[i]);
386 msi_free(sv->streams);
388 return ERROR_SUCCESS;
391 static UINT STREAMS_find_matching_rows(struct tagMSIVIEW *view, UINT col,
392 UINT val, UINT *row, MSIITERHANDLE *handle)
394 MSISTREAMSVIEW *sv = (MSISTREAMSVIEW *)view;
395 UINT index = (UINT)*handle;
397 TRACE("(%d, %d): %d\n", *row, col, val);
399 if (col == 0 || col > NUM_STREAMS_COLS)
400 return ERROR_INVALID_PARAMETER;
402 while (index < sv->num_rows)
404 if (sv->streams[index]->str_index == val)
406 *row = index;
407 break;
410 index++;
413 *handle = (MSIITERHANDLE)++index;
414 if (index >= sv->num_rows)
415 return ERROR_NO_MORE_ITEMS;
417 return ERROR_SUCCESS;
420 static const MSIVIEWOPS streams_ops =
422 STREAMS_fetch_int,
423 STREAMS_fetch_stream,
424 STREAMS_get_row,
425 STREAMS_set_row,
426 STREAMS_insert_row,
427 STREAMS_delete_row,
428 STREAMS_execute,
429 STREAMS_close,
430 STREAMS_get_dimensions,
431 STREAMS_get_column_info,
432 STREAMS_modify,
433 STREAMS_delete,
434 STREAMS_find_matching_rows,
435 NULL,
436 NULL,
437 NULL,
438 NULL,
439 NULL,
442 static INT add_streams_to_table(MSISTREAMSVIEW *sv)
444 IEnumSTATSTG *stgenum = NULL;
445 STATSTG stat;
446 STREAM *stream = NULL;
447 HRESULT hr;
448 UINT count = 0, size;
450 hr = IStorage_EnumElements(sv->db->storage, 0, NULL, 0, &stgenum);
451 if (FAILED(hr))
452 return -1;
454 sv->max_streams = 1;
455 sv->streams = msi_alloc(sizeof(STREAM *));
456 if (!sv->streams)
457 return -1;
459 while (TRUE)
461 size = 0;
462 hr = IEnumSTATSTG_Next(stgenum, 1, &stat, &size);
463 if (FAILED(hr) || !size)
464 break;
466 /* table streams are not in the _Streams table */
467 if (*stat.pwcsName == 0x4840)
469 CoTaskMemFree(stat.pwcsName);
470 continue;
473 stream = create_stream(sv, stat.pwcsName, TRUE, NULL);
474 if (!stream)
476 count = -1;
477 CoTaskMemFree(stat.pwcsName);
478 break;
481 IStorage_OpenStream(sv->db->storage, stat.pwcsName, 0,
482 STGM_READ | STGM_SHARE_EXCLUSIVE, 0, &stream->stream);
483 CoTaskMemFree(stat.pwcsName);
485 if (!streams_set_table_size(sv, ++count))
487 count = -1;
488 break;
491 sv->streams[count - 1] = stream;
494 IEnumSTATSTG_Release(stgenum);
495 return count;
498 UINT STREAMS_CreateView(MSIDATABASE *db, MSIVIEW **view)
500 MSISTREAMSVIEW *sv;
501 INT rows;
503 TRACE("(%p, %p)\n", db, view);
505 sv = msi_alloc(sizeof(MSISTREAMSVIEW));
506 if (!sv)
507 return ERROR_FUNCTION_FAILED;
509 sv->view.ops = &streams_ops;
510 sv->db = db;
511 rows = add_streams_to_table(sv);
512 if (rows < 0)
513 return ERROR_FUNCTION_FAILED;
514 sv->num_rows = rows;
516 *view = (MSIVIEW *)sv;
518 return ERROR_SUCCESS;