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
34 #include "wine/debug.h"
36 WINE_DEFAULT_DEBUG_CHANNEL(msidb);
38 #define NUM_STREAMS_COLS 2
39 #define MAX_STREAM_NAME_LEN 62
41 typedef struct tabSTREAM
48 typedef struct tagMSISTREAMSVIEW
58 static BOOL streams_set_table_size(MSISTREAMSVIEW *sv, UINT size)
60 if (size >= sv->max_streams)
63 sv->streams = msi_realloc_zero(sv->streams, sv->max_streams * sizeof(STREAM *));
71 static STREAM *create_stream(MSISTREAMSVIEW *sv, LPWSTR name, BOOL encoded, IStream *stm)
74 WCHAR decoded[MAX_STREAM_NAME_LEN];
77 stream = msi_alloc(sizeof(STREAM));
83 decode_streamname(name, decoded);
85 TRACE("stream -> %s %s\n", debugstr_w(name), debugstr_w(decoded));
88 stream->name = strdupW(ptr);
95 stream->str_index = msi_addstringW(sv->db->strings, 0, stream->name, -1, 1, StringNonPersistent);
100 static UINT STREAMS_fetch_int(struct tagMSIVIEW *view, UINT row, UINT col, UINT *val)
102 MSISTREAMSVIEW *sv = (MSISTREAMSVIEW *)view;
104 TRACE("(%p, %d, %d, %p)\n", view, row, col, val);
107 return ERROR_INVALID_PARAMETER;
109 if (row >= sv->num_rows)
110 return ERROR_NO_MORE_ITEMS;
112 *val = sv->streams[row]->str_index;
114 return ERROR_SUCCESS;
117 static UINT STREAMS_fetch_stream(struct tagMSIVIEW *view, UINT row, UINT col, IStream **stm)
119 MSISTREAMSVIEW *sv = (MSISTREAMSVIEW *)view;
121 TRACE("(%p, %d, %d, %p)\n", view, row, col, stm);
123 if (row >= sv->num_rows)
124 return ERROR_FUNCTION_FAILED;
126 IStream_AddRef(sv->streams[row]->stream);
127 *stm = sv->streams[row]->stream;
129 return ERROR_SUCCESS;
132 static UINT STREAMS_get_row( struct tagMSIVIEW *view, UINT row, MSIRECORD **rec )
134 MSISTREAMSVIEW *sv = (MSISTREAMSVIEW *)view;
136 FIXME("%p %d %p\n", sv, row, rec);
138 return ERROR_CALL_NOT_IMPLEMENTED;
141 static UINT STREAMS_set_row(struct tagMSIVIEW *view, UINT row, MSIRECORD *rec, UINT mask)
143 MSISTREAMSVIEW *sv = (MSISTREAMSVIEW *)view;
151 UINT r = ERROR_FUNCTION_FAILED;
153 TRACE("(%p, %p)\n", view, rec);
155 if (row > sv->num_rows)
156 return ERROR_FUNCTION_FAILED;
158 r = MSI_RecordGetIStream(rec, 2, &stm);
159 if (r != ERROR_SUCCESS)
162 hr = IStream_Stat(stm, &stat, STATFLAG_NONAME);
165 WARN("failed to stat stream: %08x\n", hr);
169 if (stat.cbSize.QuadPart >> 32)
172 data = msi_alloc(stat.cbSize.QuadPart);
176 hr = IStream_Read(stm, data, stat.cbSize.QuadPart, &count);
177 if (FAILED(hr) || count != stat.cbSize.QuadPart)
179 WARN("failed to read stream: %08x\n", hr);
183 name = strdupW(MSI_RecordGetString(rec, 1));
187 r = write_stream_data(sv->db->storage, name, data, count, FALSE);
188 if (r != ERROR_SUCCESS)
190 WARN("failed to write stream data: %d\n", r);
194 stream = create_stream(sv, name, FALSE, NULL);
198 IStorage_OpenStream(sv->db->storage, name, 0,
199 STGM_READ | STGM_SHARE_EXCLUSIVE, 0, &stream->stream);
201 sv->streams[row] = stream;
207 IStream_Release(stm);
212 static UINT STREAMS_insert_row(struct tagMSIVIEW *view, MSIRECORD *rec, UINT row, BOOL temporary)
214 MSISTREAMSVIEW *sv = (MSISTREAMSVIEW *)view;
216 if (!streams_set_table_size(sv, ++sv->num_rows))
217 return ERROR_FUNCTION_FAILED;
220 row = sv->num_rows - 1;
222 /* FIXME have to readjust rows */
224 return STREAMS_set_row(view, row, rec, 0);
227 static UINT STREAMS_delete_row(struct tagMSIVIEW *view, UINT row)
229 FIXME("(%p %d): stub!\n", view, row);
230 return ERROR_SUCCESS;
233 static UINT STREAMS_execute(struct tagMSIVIEW *view, MSIRECORD *record)
235 TRACE("(%p, %p)\n", view, record);
236 return ERROR_SUCCESS;
239 static UINT STREAMS_close(struct tagMSIVIEW *view)
241 TRACE("(%p)\n", view);
242 return ERROR_SUCCESS;
245 static UINT STREAMS_get_dimensions(struct tagMSIVIEW *view, UINT *rows, UINT *cols)
247 MSISTREAMSVIEW *sv = (MSISTREAMSVIEW *)view;
249 TRACE("(%p, %p, %p)\n", view, rows, cols);
251 if (cols) *cols = NUM_STREAMS_COLS;
252 if (rows) *rows = sv->num_rows;
254 return ERROR_SUCCESS;
257 static UINT STREAMS_get_column_info(struct tagMSIVIEW *view, UINT n,
258 LPWSTR *name, UINT *type, BOOL *temporary,
261 LPCWSTR name_ptr = NULL;
263 static const WCHAR Name[] = {'N','a','m','e',0};
264 static const WCHAR Data[] = {'D','a','t','a',0};
265 static const WCHAR _Streams[] = {'_','S','t','r','e','a','m','s',0};
267 TRACE("(%p, %d, %p, %p, %p, %p)\n", view, n, name, type, temporary,
270 if (n == 0 || n > NUM_STREAMS_COLS)
271 return ERROR_INVALID_PARAMETER;
277 if (type) *type = MSITYPE_STRING | MAX_STREAM_NAME_LEN;
282 if (type) *type = MSITYPE_STRING | MSITYPE_VALID | MSITYPE_NULLABLE;
288 *name = strdupW(name_ptr);
289 if (!*name) return ERROR_FUNCTION_FAILED;
294 *table_name = strdupW(_Streams);
298 return ERROR_FUNCTION_FAILED;
305 return ERROR_SUCCESS;
308 static UINT streams_find_row(MSISTREAMSVIEW *sv, MSIRECORD *rec, UINT *row)
313 str = MSI_RecordGetString(rec, 1);
314 msi_string2idW(sv->db->strings, str, &id);
316 for (i = 0; i < sv->num_rows; i++)
318 STREAMS_fetch_int(&sv->view, i, 1, &data);
323 return ERROR_SUCCESS;
327 return ERROR_FUNCTION_FAILED;
330 static UINT streams_modify_update(struct tagMSIVIEW *view, MSIRECORD *rec)
332 MSISTREAMSVIEW *sv = (MSISTREAMSVIEW *)view;
335 r = streams_find_row(sv, rec, &row);
336 if (r != ERROR_SUCCESS)
337 return ERROR_FUNCTION_FAILED;
339 return STREAMS_set_row(view, row, rec, 0);
342 static UINT streams_modify_assign(struct tagMSIVIEW *view, MSIRECORD *rec)
344 MSISTREAMSVIEW *sv = (MSISTREAMSVIEW *)view;
347 r = streams_find_row(sv, rec, &row);
348 if (r == ERROR_SUCCESS)
349 return streams_modify_update(view, rec);
351 return STREAMS_insert_row(view, rec, -1, FALSE);
354 static UINT STREAMS_modify(struct tagMSIVIEW *view, MSIMODIFY eModifyMode, MSIRECORD *rec, UINT row)
358 TRACE("%p %d %p\n", view, eModifyMode, rec);
362 case MSIMODIFY_ASSIGN:
363 r = streams_modify_assign(view, rec);
366 case MSIMODIFY_INSERT:
367 r = STREAMS_insert_row(view, rec, -1, FALSE);
370 case MSIMODIFY_UPDATE:
371 r = streams_modify_update(view, rec);
374 case MSIMODIFY_VALIDATE_NEW:
375 case MSIMODIFY_INSERT_TEMPORARY:
376 case MSIMODIFY_REFRESH:
377 case MSIMODIFY_REPLACE:
378 case MSIMODIFY_MERGE:
379 case MSIMODIFY_DELETE:
380 case MSIMODIFY_VALIDATE:
381 case MSIMODIFY_VALIDATE_FIELD:
382 case MSIMODIFY_VALIDATE_DELETE:
383 FIXME("%p %d %p - mode not implemented\n", view, eModifyMode, rec );
384 r = ERROR_CALL_NOT_IMPLEMENTED;
388 r = ERROR_INVALID_DATA;
394 static UINT STREAMS_delete(struct tagMSIVIEW *view)
396 MSISTREAMSVIEW *sv = (MSISTREAMSVIEW *)view;
399 TRACE("(%p)\n", view);
401 for (i = 0; i < sv->num_rows; i++)
403 if (sv->streams[i] && sv->streams[i]->stream)
404 IStream_Release(sv->streams[i]->stream);
405 msi_free(sv->streams[i]);
408 msi_free(sv->streams);
410 return ERROR_SUCCESS;
413 static UINT STREAMS_find_matching_rows(struct tagMSIVIEW *view, UINT col,
414 UINT val, UINT *row, MSIITERHANDLE *handle)
416 MSISTREAMSVIEW *sv = (MSISTREAMSVIEW *)view;
417 UINT index = (UINT)*handle;
419 TRACE("(%d, %d): %d\n", *row, col, val);
421 if (col == 0 || col > NUM_STREAMS_COLS)
422 return ERROR_INVALID_PARAMETER;
424 while (index < sv->num_rows)
426 if (sv->streams[index]->str_index == val)
435 *handle = (MSIITERHANDLE)++index;
436 if (index >= sv->num_rows)
437 return ERROR_NO_MORE_ITEMS;
439 return ERROR_SUCCESS;
442 static const MSIVIEWOPS streams_ops =
445 STREAMS_fetch_stream,
452 STREAMS_get_dimensions,
453 STREAMS_get_column_info,
456 STREAMS_find_matching_rows,
465 static INT add_streams_to_table(MSISTREAMSVIEW *sv)
467 IEnumSTATSTG *stgenum = NULL;
469 STREAM *stream = NULL;
471 UINT count = 0, size;
473 hr = IStorage_EnumElements(sv->db->storage, 0, NULL, 0, &stgenum);
478 sv->streams = msi_alloc_zero(sizeof(STREAM *));
485 hr = IEnumSTATSTG_Next(stgenum, 1, &stat, &size);
486 if (FAILED(hr) || !size)
489 if (stat.type != STGTY_STREAM)
492 /* table streams are not in the _Streams table */
493 if (*stat.pwcsName == 0x4840)
495 CoTaskMemFree(stat.pwcsName);
499 stream = create_stream(sv, stat.pwcsName, TRUE, NULL);
503 CoTaskMemFree(stat.pwcsName);
507 IStorage_OpenStream(sv->db->storage, stat.pwcsName, 0,
508 STGM_READ | STGM_SHARE_EXCLUSIVE, 0, &stream->stream);
509 CoTaskMemFree(stat.pwcsName);
511 if (!streams_set_table_size(sv, ++count))
517 sv->streams[count - 1] = stream;
520 IEnumSTATSTG_Release(stgenum);
524 UINT STREAMS_CreateView(MSIDATABASE *db, MSIVIEW **view)
529 TRACE("(%p, %p)\n", db, view);
531 sv = msi_alloc(sizeof(MSISTREAMSVIEW));
533 return ERROR_FUNCTION_FAILED;
535 sv->view.ops = &streams_ops;
537 rows = add_streams_to_table(sv);
541 return ERROR_FUNCTION_FAILED;
545 *view = (MSIVIEW *)sv;
547 return ERROR_SUCCESS;