winepulse: Set pulse master volume to 0 when session is muted.
[wine.git] / dlls / msi / streams.c
blob1ef99c2ab05c1ba913ca3b50c40c0ae7d57b7139
1 /*
2 * Implementation of the Microsoft Installer (msi.dll)
4 * Copyright 2007 James Hawkins
5 * Copyright 2015 Hans Leidekker for CodeWeavers
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 #include <stdarg.h>
24 #define COBJMACROS
26 #include "windef.h"
27 #include "winbase.h"
28 #include "winerror.h"
29 #include "msi.h"
30 #include "msiquery.h"
31 #include "objbase.h"
32 #include "msipriv.h"
33 #include "query.h"
35 #include "wine/debug.h"
37 WINE_DEFAULT_DEBUG_CHANNEL(msidb);
39 #define NUM_STREAMS_COLS 2
41 typedef struct tagMSISTREAMSVIEW
43 MSIVIEW view;
44 MSIDATABASE *db;
45 UINT num_cols;
46 } MSISTREAMSVIEW;
48 static BOOL streams_resize_table( MSIDATABASE *db, UINT size )
50 if (!db->num_streams_allocated)
52 if (!(db->streams = msi_alloc_zero( size * sizeof(MSISTREAM) ))) return FALSE;
53 db->num_streams_allocated = size;
54 return TRUE;
56 while (size >= db->num_streams_allocated)
58 MSISTREAM *tmp;
59 UINT new_size = db->num_streams_allocated * 2;
60 if (!(tmp = msi_realloc_zero( db->streams, new_size * sizeof(MSISTREAM) ))) return FALSE;
61 db->streams = tmp;
62 db->num_streams_allocated = new_size;
64 return TRUE;
67 static UINT STREAMS_fetch_int(struct tagMSIVIEW *view, UINT row, UINT col, UINT *val)
69 MSISTREAMSVIEW *sv = (MSISTREAMSVIEW *)view;
71 TRACE("(%p, %d, %d, %p)\n", view, row, col, val);
73 if (col != 1)
74 return ERROR_INVALID_PARAMETER;
76 if (row >= sv->db->num_streams)
77 return ERROR_NO_MORE_ITEMS;
79 *val = sv->db->streams[row].str_index;
81 return ERROR_SUCCESS;
84 static UINT STREAMS_fetch_stream(struct tagMSIVIEW *view, UINT row, UINT col, IStream **stm)
86 MSISTREAMSVIEW *sv = (MSISTREAMSVIEW *)view;
87 LARGE_INTEGER pos;
88 HRESULT hr;
90 TRACE("(%p, %d, %d, %p)\n", view, row, col, stm);
92 if (row >= sv->db->num_streams)
93 return ERROR_FUNCTION_FAILED;
95 pos.QuadPart = 0;
96 hr = IStream_Seek( sv->db->streams[row].stream, pos, STREAM_SEEK_SET, NULL );
97 if (FAILED( hr ))
98 return ERROR_FUNCTION_FAILED;
100 *stm = sv->db->streams[row].stream;
101 IStream_AddRef( *stm );
103 return ERROR_SUCCESS;
106 static UINT STREAMS_set_string( struct tagMSIVIEW *view, UINT row, UINT col, const WCHAR *val, int len )
108 ERR("Cannot modify primary key.\n");
109 return ERROR_FUNCTION_FAILED;
112 static UINT STREAMS_set_stream( MSIVIEW *view, UINT row, UINT col, IStream *stream )
114 MSISTREAMSVIEW *sv = (MSISTREAMSVIEW *)view;
115 IStream *prev;
117 TRACE("view %p, row %u, col %u, stream %p.\n", view, row, col, stream);
119 prev = sv->db->streams[row].stream;
120 IStream_AddRef(sv->db->streams[row].stream = stream);
121 if (prev) IStream_Release(prev);
122 return ERROR_SUCCESS;
125 static UINT STREAMS_set_row(struct tagMSIVIEW *view, UINT row, MSIRECORD *rec, UINT mask)
127 MSISTREAMSVIEW *sv = (MSISTREAMSVIEW *)view;
129 TRACE("(%p, %d, %p, %08x)\n", view, row, rec, mask);
131 if (row > sv->db->num_streams || mask >= (1 << sv->num_cols))
132 return ERROR_INVALID_PARAMETER;
134 if (mask & 1)
136 const WCHAR *name = MSI_RecordGetString( rec, 1 );
138 if (!name) return ERROR_INVALID_PARAMETER;
139 sv->db->streams[row].str_index = msi_add_string( sv->db->strings, name, -1, FALSE );
141 if (mask & 2)
143 IStream *old, *new;
144 HRESULT hr;
145 UINT r;
147 r = MSI_RecordGetIStream( rec, 2, &new );
148 if (r != ERROR_SUCCESS)
149 return r;
151 old = sv->db->streams[row].stream;
152 hr = IStream_QueryInterface( new, &IID_IStream, (void **)&sv->db->streams[row].stream );
153 IStream_Release( new );
154 if (FAILED( hr ))
156 return ERROR_FUNCTION_FAILED;
158 if (old) IStream_Release( old );
161 return ERROR_SUCCESS;
164 static UINT streams_find_row( MSISTREAMSVIEW *sv, MSIRECORD *rec, UINT *row )
166 const WCHAR *str;
167 UINT r, i, id, val;
169 str = MSI_RecordGetString( rec, 1 );
170 r = msi_string2id( sv->db->strings, str, -1, &id );
171 if (r != ERROR_SUCCESS)
172 return r;
174 for (i = 0; i < sv->db->num_streams; i++)
176 STREAMS_fetch_int( &sv->view, i, 1, &val );
178 if (val == id)
180 if (row) *row = i;
181 return ERROR_SUCCESS;
185 return ERROR_FUNCTION_FAILED;
188 static UINT STREAMS_insert_row(struct tagMSIVIEW *view, MSIRECORD *rec, UINT row, BOOL temporary)
190 MSISTREAMSVIEW *sv = (MSISTREAMSVIEW *)view;
191 UINT i, r, num_rows = sv->db->num_streams + 1;
193 TRACE("(%p, %p, %d, %d)\n", view, rec, row, temporary);
195 r = streams_find_row( sv, rec, NULL );
196 if (r == ERROR_SUCCESS)
197 return ERROR_FUNCTION_FAILED;
199 if (!streams_resize_table( sv->db, num_rows ))
200 return ERROR_FUNCTION_FAILED;
202 if (row == -1)
203 row = num_rows - 1;
205 /* shift the rows to make room for the new row */
206 for (i = num_rows - 1; i > row; i--)
208 sv->db->streams[i] = sv->db->streams[i - 1];
211 r = STREAMS_set_row( view, row, rec, (1 << sv->num_cols) - 1 );
212 if (r == ERROR_SUCCESS)
213 sv->db->num_streams = num_rows;
215 return r;
218 static UINT STREAMS_delete_row(struct tagMSIVIEW *view, UINT row)
220 MSIDATABASE *db = ((MSISTREAMSVIEW *)view)->db;
221 UINT i, num_rows = db->num_streams - 1;
222 const WCHAR *name;
223 WCHAR *encname;
224 HRESULT hr;
226 TRACE("(%p %d)\n", view, row);
228 if (!db->num_streams || row > num_rows)
229 return ERROR_FUNCTION_FAILED;
231 name = msi_string_lookup( db->strings, db->streams[row].str_index, NULL );
232 if (!(encname = encode_streamname( FALSE, name ))) return ERROR_OUTOFMEMORY;
233 IStream_Release( db->streams[row].stream );
235 for (i = row; i < num_rows; i++)
236 db->streams[i] = db->streams[i + 1];
237 db->num_streams = num_rows;
239 hr = IStorage_DestroyElement( db->storage, encname );
240 msi_free( encname );
241 return FAILED( hr ) ? ERROR_FUNCTION_FAILED : ERROR_SUCCESS;
244 static UINT STREAMS_execute(struct tagMSIVIEW *view, MSIRECORD *record)
246 TRACE("(%p, %p)\n", view, record);
247 return ERROR_SUCCESS;
250 static UINT STREAMS_close(struct tagMSIVIEW *view)
252 TRACE("(%p)\n", view);
253 return ERROR_SUCCESS;
256 static UINT STREAMS_get_dimensions(struct tagMSIVIEW *view, UINT *rows, UINT *cols)
258 MSISTREAMSVIEW *sv = (MSISTREAMSVIEW *)view;
260 TRACE("(%p, %p, %p)\n", view, rows, cols);
262 if (cols) *cols = sv->num_cols;
263 if (rows) *rows = sv->db->num_streams;
265 return ERROR_SUCCESS;
268 static UINT STREAMS_get_column_info( struct tagMSIVIEW *view, UINT n, LPCWSTR *name,
269 UINT *type, BOOL *temporary, LPCWSTR *table_name )
271 MSISTREAMSVIEW *sv = (MSISTREAMSVIEW *)view;
273 TRACE("(%p, %d, %p, %p, %p, %p)\n", view, n, name, type, temporary, table_name);
275 if (!n || n > sv->num_cols)
276 return ERROR_INVALID_PARAMETER;
278 switch (n)
280 case 1:
281 if (name) *name = L"Name";
282 if (type) *type = MSITYPE_STRING | MSITYPE_VALID | MAX_STREAM_NAME_LEN;
283 break;
285 case 2:
286 if (name) *name = L"Data";
287 if (type) *type = MSITYPE_STRING | MSITYPE_VALID | MSITYPE_NULLABLE;
288 break;
290 if (table_name) *table_name = L"_Streams";
291 if (temporary) *temporary = FALSE;
292 return ERROR_SUCCESS;
295 static UINT streams_modify_update(struct tagMSIVIEW *view, MSIRECORD *rec)
297 MSISTREAMSVIEW *sv = (MSISTREAMSVIEW *)view;
298 UINT r, row;
300 r = streams_find_row(sv, rec, &row);
301 if (r != ERROR_SUCCESS)
302 return ERROR_FUNCTION_FAILED;
304 return STREAMS_set_row( view, row, rec, (1 << sv->num_cols) - 1 );
307 static UINT streams_modify_assign(struct tagMSIVIEW *view, MSIRECORD *rec)
309 MSISTREAMSVIEW *sv = (MSISTREAMSVIEW *)view;
310 UINT r;
312 r = streams_find_row( sv, rec, NULL );
313 if (r == ERROR_SUCCESS)
314 return streams_modify_update(view, rec);
316 return STREAMS_insert_row(view, rec, -1, FALSE);
319 static UINT STREAMS_modify(struct tagMSIVIEW *view, MSIMODIFY eModifyMode, MSIRECORD *rec, UINT row)
321 UINT r;
323 TRACE("%p %d %p\n", view, eModifyMode, rec);
325 switch (eModifyMode)
327 case MSIMODIFY_ASSIGN:
328 r = streams_modify_assign(view, rec);
329 break;
331 case MSIMODIFY_INSERT:
332 r = STREAMS_insert_row(view, rec, -1, FALSE);
333 break;
335 case MSIMODIFY_UPDATE:
336 r = streams_modify_update(view, rec);
337 break;
339 case MSIMODIFY_DELETE:
340 r = STREAMS_delete_row(view, row - 1);
341 break;
343 case MSIMODIFY_VALIDATE_NEW:
344 case MSIMODIFY_INSERT_TEMPORARY:
345 case MSIMODIFY_REFRESH:
346 case MSIMODIFY_REPLACE:
347 case MSIMODIFY_MERGE:
348 case MSIMODIFY_VALIDATE:
349 case MSIMODIFY_VALIDATE_FIELD:
350 case MSIMODIFY_VALIDATE_DELETE:
351 FIXME("%p %d %p - mode not implemented\n", view, eModifyMode, rec );
352 r = ERROR_CALL_NOT_IMPLEMENTED;
353 break;
355 default:
356 r = ERROR_INVALID_DATA;
359 return r;
362 static UINT STREAMS_delete(struct tagMSIVIEW *view)
364 MSISTREAMSVIEW *sv = (MSISTREAMSVIEW *)view;
366 TRACE("(%p)\n", view);
368 msi_free(sv);
369 return ERROR_SUCCESS;
372 static const MSIVIEWOPS streams_ops =
374 STREAMS_fetch_int,
375 STREAMS_fetch_stream,
376 NULL,
377 STREAMS_set_string,
378 STREAMS_set_stream,
379 STREAMS_set_row,
380 STREAMS_insert_row,
381 STREAMS_delete_row,
382 STREAMS_execute,
383 STREAMS_close,
384 STREAMS_get_dimensions,
385 STREAMS_get_column_info,
386 STREAMS_modify,
387 STREAMS_delete,
388 NULL,
389 NULL,
390 NULL,
391 NULL,
392 NULL,
395 static HRESULT open_stream( MSIDATABASE *db, const WCHAR *name, IStream **stream )
397 HRESULT hr;
399 hr = IStorage_OpenStream( db->storage, name, NULL, STGM_READ|STGM_SHARE_EXCLUSIVE, 0, stream );
400 if (FAILED( hr ))
402 MSITRANSFORM *transform;
404 LIST_FOR_EACH_ENTRY( transform, &db->transforms, MSITRANSFORM, entry )
406 hr = IStorage_OpenStream( transform->stg, name, NULL, STGM_READ|STGM_SHARE_EXCLUSIVE, 0, stream );
407 if (SUCCEEDED( hr ))
408 break;
411 return hr;
414 static MSISTREAM *find_stream( MSIDATABASE *db, const WCHAR *name )
416 UINT r, id, i;
418 r = msi_string2id( db->strings, name, -1, &id );
419 if (r != ERROR_SUCCESS)
420 return NULL;
422 for (i = 0; i < db->num_streams; i++)
424 if (db->streams[i].str_index == id) return &db->streams[i];
426 return NULL;
429 static UINT append_stream( MSIDATABASE *db, const WCHAR *name, IStream *stream )
431 UINT i = db->num_streams;
433 if (!streams_resize_table( db, db->num_streams + 1 ))
434 return ERROR_OUTOFMEMORY;
436 db->streams[i].str_index = msi_add_string( db->strings, name, -1, FALSE );
437 db->streams[i].stream = stream;
438 db->num_streams++;
440 TRACE("added %s\n", debugstr_w( name ));
441 return ERROR_SUCCESS;
444 static UINT load_streams( MSIDATABASE *db )
446 WCHAR decoded[MAX_STREAM_NAME_LEN + 1];
447 IEnumSTATSTG *stgenum;
448 STATSTG stat;
449 HRESULT hr;
450 UINT count, r = ERROR_SUCCESS;
451 IStream *stream;
453 hr = IStorage_EnumElements( db->storage, 0, NULL, 0, &stgenum );
454 if (FAILED( hr ))
455 return ERROR_FUNCTION_FAILED;
457 for (;;)
459 count = 0;
460 hr = IEnumSTATSTG_Next( stgenum, 1, &stat, &count );
461 if (FAILED( hr ) || !count)
462 break;
464 /* table streams are not in the _Streams table */
465 if (stat.type != STGTY_STREAM || *stat.pwcsName == 0x4840)
467 CoTaskMemFree( stat.pwcsName );
468 continue;
470 decode_streamname( stat.pwcsName, decoded );
471 if (find_stream( db, decoded ))
473 CoTaskMemFree( stat.pwcsName );
474 continue;
476 TRACE("found new stream %s\n", debugstr_w( decoded ));
478 hr = open_stream( db, stat.pwcsName, &stream );
479 CoTaskMemFree( stat.pwcsName );
480 if (FAILED( hr ))
482 ERR("unable to open stream %08x\n", hr);
483 r = ERROR_FUNCTION_FAILED;
484 break;
487 r = append_stream( db, decoded, stream );
488 if (r != ERROR_SUCCESS)
489 break;
492 TRACE("loaded %u streams\n", db->num_streams);
493 IEnumSTATSTG_Release( stgenum );
494 return r;
497 UINT msi_get_stream( MSIDATABASE *db, const WCHAR *name, IStream **ret )
499 MSISTREAM *stream;
500 WCHAR *encname;
501 HRESULT hr;
502 UINT r;
504 if ((stream = find_stream( db, name )))
506 LARGE_INTEGER pos;
508 pos.QuadPart = 0;
509 hr = IStream_Seek( stream->stream, pos, STREAM_SEEK_SET, NULL );
510 if (FAILED( hr ))
511 return ERROR_FUNCTION_FAILED;
513 *ret = stream->stream;
514 IStream_AddRef( *ret );
515 return ERROR_SUCCESS;
518 if (!(encname = encode_streamname( FALSE, name )))
519 return ERROR_OUTOFMEMORY;
521 hr = open_stream( db, encname, ret );
522 msi_free( encname );
523 if (FAILED( hr ))
524 return ERROR_FUNCTION_FAILED;
526 r = append_stream( db, name, *ret );
527 if (r != ERROR_SUCCESS)
529 IStream_Release( *ret );
530 return r;
533 IStream_AddRef( *ret );
534 return ERROR_SUCCESS;
537 UINT STREAMS_CreateView(MSIDATABASE *db, MSIVIEW **view)
539 MSISTREAMSVIEW *sv;
540 UINT r;
542 TRACE("(%p, %p)\n", db, view);
544 r = load_streams( db );
545 if (r != ERROR_SUCCESS)
546 return r;
548 if (!(sv = msi_alloc_zero( sizeof(MSISTREAMSVIEW) )))
549 return ERROR_OUTOFMEMORY;
551 sv->view.ops = &streams_ops;
552 sv->num_cols = NUM_STREAMS_COLS;
553 sv->db = db;
555 *view = (MSIVIEW *)sv;
557 return ERROR_SUCCESS;
560 static HRESULT write_stream( IStream *dst, IStream *src )
562 HRESULT hr;
563 char buf[4096];
564 STATSTG stat;
565 LARGE_INTEGER pos;
566 UINT count, size;
568 hr = IStream_Stat( src, &stat, STATFLAG_NONAME );
569 if (FAILED( hr )) return hr;
571 hr = IStream_SetSize( dst, stat.cbSize );
572 if (FAILED( hr )) return hr;
574 pos.QuadPart = 0;
575 hr = IStream_Seek( dst, pos, STREAM_SEEK_SET, NULL );
576 if (FAILED( hr )) return hr;
578 for (;;)
580 size = min( sizeof(buf), stat.cbSize.QuadPart );
581 hr = IStream_Read( src, buf, size, &count );
582 if (FAILED( hr ) || count != size)
584 WARN("failed to read stream: %08x\n", hr);
585 return E_INVALIDARG;
587 stat.cbSize.QuadPart -= count;
588 if (count)
590 size = count;
591 hr = IStream_Write( dst, buf, size, &count );
592 if (FAILED( hr ) || count != size)
594 WARN("failed to write stream: %08x\n", hr);
595 return E_INVALIDARG;
598 if (!stat.cbSize.QuadPart) break;
601 return S_OK;
604 UINT msi_commit_streams( MSIDATABASE *db )
606 UINT i;
607 const WCHAR *name;
608 WCHAR *encname;
609 IStream *stream;
610 HRESULT hr;
612 TRACE("got %u streams\n", db->num_streams);
614 for (i = 0; i < db->num_streams; i++)
616 name = msi_string_lookup( db->strings, db->streams[i].str_index, NULL );
617 if (!wcscmp( name, L"\5SummaryInformation" )) continue;
619 if (!(encname = encode_streamname( FALSE, name ))) return ERROR_OUTOFMEMORY;
620 TRACE("saving stream %s as %s\n", debugstr_w(name), debugstr_w(encname));
622 hr = IStorage_CreateStream( db->storage, encname, STGM_WRITE|STGM_SHARE_EXCLUSIVE, 0, 0, &stream );
623 if (SUCCEEDED( hr ))
625 hr = write_stream( stream, db->streams[i].stream );
626 if (FAILED( hr ))
628 ERR("failed to write stream %s (hr = %08x)\n", debugstr_w(encname), hr);
629 msi_free( encname );
630 IStream_Release( stream );
631 return ERROR_FUNCTION_FAILED;
633 hr = IStream_Commit( stream, 0 );
634 IStream_Release( stream );
635 if (FAILED( hr ))
637 ERR("failed to commit stream %s (hr = %08x)\n", debugstr_w(encname), hr);
638 msi_free( encname );
639 return ERROR_FUNCTION_FAILED;
642 else if (hr != STG_E_FILEALREADYEXISTS)
644 ERR("failed to create stream %s (hr = %08x)\n", debugstr_w(encname), hr);
645 msi_free( encname );
646 return ERROR_FUNCTION_FAILED;
648 msi_free( encname );
651 return ERROR_SUCCESS;