[MSI] Sync with Wine 3.0. CORE-14225
[reactos.git] / dll / win32 / msi / streams.c
1 /*
2 * Implementation of the Microsoft Installer (msi.dll)
3 *
4 * Copyright 2007 James Hawkins
5 * Copyright 2015 Hans Leidekker for CodeWeavers
6 *
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.
11 *
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.
16 *
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
20 */
21
22 #include "msipriv.h"
23
24 WINE_DEFAULT_DEBUG_CHANNEL(msidb);
25
26 #define NUM_STREAMS_COLS 2
27
28 typedef struct tagMSISTREAMSVIEW
29 {
30 MSIVIEW view;
31 MSIDATABASE *db;
32 UINT num_cols;
33 } MSISTREAMSVIEW;
34
35 static BOOL streams_resize_table( MSIDATABASE *db, UINT size )
36 {
37 if (!db->num_streams_allocated)
38 {
39 if (!(db->streams = msi_alloc_zero( size * sizeof(MSISTREAM) ))) return FALSE;
40 db->num_streams_allocated = size;
41 return TRUE;
42 }
43 while (size >= db->num_streams_allocated)
44 {
45 MSISTREAM *tmp;
46 UINT new_size = db->num_streams_allocated * 2;
47 if (!(tmp = msi_realloc_zero( db->streams, new_size * sizeof(MSISTREAM) ))) return FALSE;
48 db->streams = tmp;
49 db->num_streams_allocated = new_size;
50 }
51 return TRUE;
52 }
53
54 static UINT STREAMS_fetch_int(struct tagMSIVIEW *view, UINT row, UINT col, UINT *val)
55 {
56 MSISTREAMSVIEW *sv = (MSISTREAMSVIEW *)view;
57
58 TRACE("(%p, %d, %d, %p)\n", view, row, col, val);
59
60 if (col != 1)
61 return ERROR_INVALID_PARAMETER;
62
63 if (row >= sv->db->num_streams)
64 return ERROR_NO_MORE_ITEMS;
65
66 *val = sv->db->streams[row].str_index;
67
68 return ERROR_SUCCESS;
69 }
70
71 static UINT STREAMS_fetch_stream(struct tagMSIVIEW *view, UINT row, UINT col, IStream **stm)
72 {
73 MSISTREAMSVIEW *sv = (MSISTREAMSVIEW *)view;
74 LARGE_INTEGER pos;
75 HRESULT hr;
76
77 TRACE("(%p, %d, %d, %p)\n", view, row, col, stm);
78
79 if (row >= sv->db->num_streams)
80 return ERROR_FUNCTION_FAILED;
81
82 pos.QuadPart = 0;
83 hr = IStream_Seek( sv->db->streams[row].stream, pos, STREAM_SEEK_SET, NULL );
84 if (FAILED( hr ))
85 return ERROR_FUNCTION_FAILED;
86
87 *stm = sv->db->streams[row].stream;
88 IStream_AddRef( *stm );
89
90 return ERROR_SUCCESS;
91 }
92
93 static UINT STREAMS_get_row( struct tagMSIVIEW *view, UINT row, MSIRECORD **rec )
94 {
95 MSISTREAMSVIEW *sv = (MSISTREAMSVIEW *)view;
96
97 TRACE("%p %d %p\n", sv, row, rec);
98
99 return msi_view_get_row( sv->db, view, row, rec );
100 }
101
102 static UINT STREAMS_set_row(struct tagMSIVIEW *view, UINT row, MSIRECORD *rec, UINT mask)
103 {
104 MSISTREAMSVIEW *sv = (MSISTREAMSVIEW *)view;
105
106 TRACE("(%p, %d, %p, %08x)\n", view, row, rec, mask);
107
108 if (row > sv->db->num_streams || mask >= (1 << sv->num_cols))
109 return ERROR_INVALID_PARAMETER;
110
111 if (mask & 1)
112 {
113 const WCHAR *name = MSI_RecordGetString( rec, 1 );
114
115 if (!name) return ERROR_INVALID_PARAMETER;
116 sv->db->streams[row].str_index = msi_add_string( sv->db->strings, name, -1, StringNonPersistent );
117 }
118 if (mask & 2)
119 {
120 IStream *old, *new;
121 HRESULT hr;
122 UINT r;
123
124 r = MSI_RecordGetIStream( rec, 2, &new );
125 if (r != ERROR_SUCCESS)
126 return r;
127
128 old = sv->db->streams[row].stream;
129 hr = IStream_QueryInterface( new, &IID_IStream, (void **)&sv->db->streams[row].stream );
130 if (FAILED( hr ))
131 {
132 IStream_Release( new );
133 return ERROR_FUNCTION_FAILED;
134 }
135 if (old) IStream_Release( old );
136 }
137
138 return ERROR_SUCCESS;
139 }
140
141 static UINT streams_find_row( MSISTREAMSVIEW *sv, MSIRECORD *rec, UINT *row )
142 {
143 const WCHAR *str;
144 UINT r, i, id, val;
145
146 str = MSI_RecordGetString( rec, 1 );
147 r = msi_string2id( sv->db->strings, str, -1, &id );
148 if (r != ERROR_SUCCESS)
149 return r;
150
151 for (i = 0; i < sv->db->num_streams; i++)
152 {
153 STREAMS_fetch_int( &sv->view, i, 1, &val );
154
155 if (val == id)
156 {
157 if (row) *row = i;
158 return ERROR_SUCCESS;
159 }
160 }
161
162 return ERROR_FUNCTION_FAILED;
163 }
164
165 static UINT STREAMS_insert_row(struct tagMSIVIEW *view, MSIRECORD *rec, UINT row, BOOL temporary)
166 {
167 MSISTREAMSVIEW *sv = (MSISTREAMSVIEW *)view;
168 UINT i, r, num_rows = sv->db->num_streams + 1;
169
170 TRACE("(%p, %p, %d, %d)\n", view, rec, row, temporary);
171
172 r = streams_find_row( sv, rec, NULL );
173 if (r == ERROR_SUCCESS)
174 return ERROR_FUNCTION_FAILED;
175
176 if (!streams_resize_table( sv->db, num_rows ))
177 return ERROR_FUNCTION_FAILED;
178
179 if (row == -1)
180 row = num_rows - 1;
181
182 /* shift the rows to make room for the new row */
183 for (i = num_rows - 1; i > row; i--)
184 {
185 sv->db->streams[i] = sv->db->streams[i - 1];
186 }
187
188 r = STREAMS_set_row( view, row, rec, (1 << sv->num_cols) - 1 );
189 if (r == ERROR_SUCCESS)
190 sv->db->num_streams = num_rows;
191
192 return r;
193 }
194
195 static UINT STREAMS_delete_row(struct tagMSIVIEW *view, UINT row)
196 {
197 FIXME("(%p %d): stub!\n", view, row);
198 return ERROR_SUCCESS;
199 }
200
201 static UINT STREAMS_execute(struct tagMSIVIEW *view, MSIRECORD *record)
202 {
203 TRACE("(%p, %p)\n", view, record);
204 return ERROR_SUCCESS;
205 }
206
207 static UINT STREAMS_close(struct tagMSIVIEW *view)
208 {
209 TRACE("(%p)\n", view);
210 return ERROR_SUCCESS;
211 }
212
213 static UINT STREAMS_get_dimensions(struct tagMSIVIEW *view, UINT *rows, UINT *cols)
214 {
215 MSISTREAMSVIEW *sv = (MSISTREAMSVIEW *)view;
216
217 TRACE("(%p, %p, %p)\n", view, rows, cols);
218
219 if (cols) *cols = sv->num_cols;
220 if (rows) *rows = sv->db->num_streams;
221
222 return ERROR_SUCCESS;
223 }
224
225 static UINT STREAMS_get_column_info( struct tagMSIVIEW *view, UINT n, LPCWSTR *name,
226 UINT *type, BOOL *temporary, LPCWSTR *table_name )
227 {
228 MSISTREAMSVIEW *sv = (MSISTREAMSVIEW *)view;
229
230 TRACE("(%p, %d, %p, %p, %p, %p)\n", view, n, name, type, temporary, table_name);
231
232 if (!n || n > sv->num_cols)
233 return ERROR_INVALID_PARAMETER;
234
235 switch (n)
236 {
237 case 1:
238 if (name) *name = szName;
239 if (type) *type = MSITYPE_STRING | MSITYPE_VALID | MAX_STREAM_NAME_LEN;
240 break;
241
242 case 2:
243 if (name) *name = szData;
244 if (type) *type = MSITYPE_STRING | MSITYPE_VALID | MSITYPE_NULLABLE;
245 break;
246 }
247 if (table_name) *table_name = szStreams;
248 if (temporary) *temporary = FALSE;
249 return ERROR_SUCCESS;
250 }
251
252 static UINT streams_modify_update(struct tagMSIVIEW *view, MSIRECORD *rec)
253 {
254 MSISTREAMSVIEW *sv = (MSISTREAMSVIEW *)view;
255 UINT r, row;
256
257 r = streams_find_row(sv, rec, &row);
258 if (r != ERROR_SUCCESS)
259 return ERROR_FUNCTION_FAILED;
260
261 return STREAMS_set_row( view, row, rec, (1 << sv->num_cols) - 1 );
262 }
263
264 static UINT streams_modify_assign(struct tagMSIVIEW *view, MSIRECORD *rec)
265 {
266 MSISTREAMSVIEW *sv = (MSISTREAMSVIEW *)view;
267 UINT r;
268
269 r = streams_find_row( sv, rec, NULL );
270 if (r == ERROR_SUCCESS)
271 return streams_modify_update(view, rec);
272
273 return STREAMS_insert_row(view, rec, -1, FALSE);
274 }
275
276 static UINT STREAMS_modify(struct tagMSIVIEW *view, MSIMODIFY eModifyMode, MSIRECORD *rec, UINT row)
277 {
278 UINT r;
279
280 TRACE("%p %d %p\n", view, eModifyMode, rec);
281
282 switch (eModifyMode)
283 {
284 case MSIMODIFY_ASSIGN:
285 r = streams_modify_assign(view, rec);
286 break;
287
288 case MSIMODIFY_INSERT:
289 r = STREAMS_insert_row(view, rec, -1, FALSE);
290 break;
291
292 case MSIMODIFY_UPDATE:
293 r = streams_modify_update(view, rec);
294 break;
295
296 case MSIMODIFY_VALIDATE_NEW:
297 case MSIMODIFY_INSERT_TEMPORARY:
298 case MSIMODIFY_REFRESH:
299 case MSIMODIFY_REPLACE:
300 case MSIMODIFY_MERGE:
301 case MSIMODIFY_DELETE:
302 case MSIMODIFY_VALIDATE:
303 case MSIMODIFY_VALIDATE_FIELD:
304 case MSIMODIFY_VALIDATE_DELETE:
305 FIXME("%p %d %p - mode not implemented\n", view, eModifyMode, rec );
306 r = ERROR_CALL_NOT_IMPLEMENTED;
307 break;
308
309 default:
310 r = ERROR_INVALID_DATA;
311 }
312
313 return r;
314 }
315
316 static UINT STREAMS_delete(struct tagMSIVIEW *view)
317 {
318 MSISTREAMSVIEW *sv = (MSISTREAMSVIEW *)view;
319
320 TRACE("(%p)\n", view);
321
322 msi_free(sv);
323 return ERROR_SUCCESS;
324 }
325
326 static UINT STREAMS_find_matching_rows(struct tagMSIVIEW *view, UINT col,
327 UINT val, UINT *row, MSIITERHANDLE *handle)
328 {
329 MSISTREAMSVIEW *sv = (MSISTREAMSVIEW *)view;
330 UINT index = PtrToUlong(*handle);
331
332 TRACE("(%p, %d, %d, %p, %p)\n", view, col, val, row, handle);
333
334 if (!col || col > sv->num_cols)
335 return ERROR_INVALID_PARAMETER;
336
337 while (index < sv->db->num_streams)
338 {
339 if (sv->db->streams[index].str_index == val)
340 {
341 *row = index;
342 break;
343 }
344 index++;
345 }
346
347 *handle = UlongToPtr(++index);
348
349 if (index > sv->db->num_streams)
350 return ERROR_NO_MORE_ITEMS;
351
352 return ERROR_SUCCESS;
353 }
354
355 static const MSIVIEWOPS streams_ops =
356 {
357 STREAMS_fetch_int,
358 STREAMS_fetch_stream,
359 STREAMS_get_row,
360 STREAMS_set_row,
361 STREAMS_insert_row,
362 STREAMS_delete_row,
363 STREAMS_execute,
364 STREAMS_close,
365 STREAMS_get_dimensions,
366 STREAMS_get_column_info,
367 STREAMS_modify,
368 STREAMS_delete,
369 STREAMS_find_matching_rows,
370 NULL,
371 NULL,
372 NULL,
373 NULL,
374 NULL,
375 NULL,
376 };
377
378 static HRESULT open_stream( MSIDATABASE *db, const WCHAR *name, IStream **stream )
379 {
380 HRESULT hr;
381
382 hr = IStorage_OpenStream( db->storage, name, NULL, STGM_READ|STGM_SHARE_EXCLUSIVE, 0, stream );
383 if (FAILED( hr ))
384 {
385 MSITRANSFORM *transform;
386
387 LIST_FOR_EACH_ENTRY( transform, &db->transforms, MSITRANSFORM, entry )
388 {
389 hr = IStorage_OpenStream( transform->stg, name, NULL, STGM_READ|STGM_SHARE_EXCLUSIVE, 0, stream );
390 if (SUCCEEDED( hr ))
391 break;
392 }
393 }
394 return hr;
395 }
396
397 static MSISTREAM *find_stream( MSIDATABASE *db, const WCHAR *name )
398 {
399 UINT r, id, i;
400
401 r = msi_string2id( db->strings, name, -1, &id );
402 if (r != ERROR_SUCCESS)
403 return NULL;
404
405 for (i = 0; i < db->num_streams; i++)
406 {
407 if (db->streams[i].str_index == id) return &db->streams[i];
408 }
409 return NULL;
410 }
411
412 static UINT append_stream( MSIDATABASE *db, const WCHAR *name, IStream *stream )
413 {
414 UINT i = db->num_streams;
415
416 if (!streams_resize_table( db, db->num_streams + 1 ))
417 return ERROR_OUTOFMEMORY;
418
419 db->streams[i].str_index = msi_add_string( db->strings, name, -1, StringNonPersistent );
420 db->streams[i].stream = stream;
421 db->num_streams++;
422
423 TRACE("added %s\n", debugstr_w( name ));
424 return ERROR_SUCCESS;
425 }
426
427 static UINT load_streams( MSIDATABASE *db )
428 {
429 WCHAR decoded[MAX_STREAM_NAME_LEN + 1];
430 IEnumSTATSTG *stgenum;
431 STATSTG stat;
432 HRESULT hr;
433 UINT count, r = ERROR_SUCCESS;
434 IStream *stream;
435
436 hr = IStorage_EnumElements( db->storage, 0, NULL, 0, &stgenum );
437 if (FAILED( hr ))
438 return ERROR_FUNCTION_FAILED;
439
440 for (;;)
441 {
442 count = 0;
443 hr = IEnumSTATSTG_Next( stgenum, 1, &stat, &count );
444 if (FAILED( hr ) || !count)
445 break;
446
447 /* table streams are not in the _Streams table */
448 if (stat.type != STGTY_STREAM || *stat.pwcsName == 0x4840)
449 {
450 CoTaskMemFree( stat.pwcsName );
451 continue;
452 }
453 decode_streamname( stat.pwcsName, decoded );
454 if (find_stream( db, decoded ))
455 {
456 CoTaskMemFree( stat.pwcsName );
457 continue;
458 }
459 TRACE("found new stream %s\n", debugstr_w( decoded ));
460
461 hr = open_stream( db, stat.pwcsName, &stream );
462 CoTaskMemFree( stat.pwcsName );
463 if (FAILED( hr ))
464 {
465 ERR("unable to open stream %08x\n", hr);
466 r = ERROR_FUNCTION_FAILED;
467 break;
468 }
469
470 r = append_stream( db, decoded, stream );
471 if (r != ERROR_SUCCESS)
472 break;
473 }
474
475 TRACE("loaded %u streams\n", db->num_streams);
476 IEnumSTATSTG_Release( stgenum );
477 return r;
478 }
479
480 UINT msi_get_stream( MSIDATABASE *db, const WCHAR *name, IStream **ret )
481 {
482 MSISTREAM *stream;
483 WCHAR *encname;
484 HRESULT hr;
485 UINT r;
486
487 if ((stream = find_stream( db, name )))
488 {
489 LARGE_INTEGER pos;
490
491 pos.QuadPart = 0;
492 hr = IStream_Seek( stream->stream, pos, STREAM_SEEK_SET, NULL );
493 if (FAILED( hr ))
494 return ERROR_FUNCTION_FAILED;
495
496 *ret = stream->stream;
497 IStream_AddRef( *ret );
498 return ERROR_SUCCESS;
499 }
500
501 if (!(encname = encode_streamname( FALSE, name )))
502 return ERROR_OUTOFMEMORY;
503
504 hr = open_stream( db, encname, ret );
505 msi_free( encname );
506 if (FAILED( hr ))
507 return ERROR_FUNCTION_FAILED;
508
509 r = append_stream( db, name, *ret );
510 if (r != ERROR_SUCCESS)
511 {
512 IStream_Release( *ret );
513 return r;
514 }
515
516 IStream_AddRef( *ret );
517 return ERROR_SUCCESS;
518 }
519
520 UINT STREAMS_CreateView(MSIDATABASE *db, MSIVIEW **view)
521 {
522 MSISTREAMSVIEW *sv;
523 UINT r;
524
525 TRACE("(%p, %p)\n", db, view);
526
527 r = load_streams( db );
528 if (r != ERROR_SUCCESS)
529 return r;
530
531 if (!(sv = msi_alloc_zero( sizeof(MSISTREAMSVIEW) )))
532 return ERROR_OUTOFMEMORY;
533
534 sv->view.ops = &streams_ops;
535 sv->num_cols = NUM_STREAMS_COLS;
536 sv->db = db;
537
538 *view = (MSIVIEW *)sv;
539
540 return ERROR_SUCCESS;
541 }
542
543 static HRESULT write_stream( IStream *dst, IStream *src )
544 {
545 HRESULT hr;
546 char buf[4096];
547 STATSTG stat;
548 LARGE_INTEGER pos;
549 UINT count, size;
550
551 hr = IStream_Stat( src, &stat, STATFLAG_NONAME );
552 if (FAILED( hr )) return hr;
553
554 hr = IStream_SetSize( dst, stat.cbSize );
555 if (FAILED( hr )) return hr;
556
557 pos.QuadPart = 0;
558 hr = IStream_Seek( dst, pos, STREAM_SEEK_SET, NULL );
559 if (FAILED( hr )) return hr;
560
561 for (;;)
562 {
563 size = min( sizeof(buf), stat.cbSize.QuadPart );
564 hr = IStream_Read( src, buf, size, &count );
565 if (FAILED( hr ) || count != size)
566 {
567 WARN("failed to read stream: %08x\n", hr);
568 return E_INVALIDARG;
569 }
570 stat.cbSize.QuadPart -= count;
571 if (count)
572 {
573 size = count;
574 hr = IStream_Write( dst, buf, size, &count );
575 if (FAILED( hr ) || count != size)
576 {
577 WARN("failed to write stream: %08x\n", hr);
578 return E_INVALIDARG;
579 }
580 }
581 if (!stat.cbSize.QuadPart) break;
582 }
583
584 return S_OK;
585 }
586
587 UINT msi_commit_streams( MSIDATABASE *db )
588 {
589 UINT i;
590 const WCHAR *name;
591 WCHAR *encname;
592 IStream *stream;
593 HRESULT hr;
594
595 TRACE("got %u streams\n", db->num_streams);
596
597 for (i = 0; i < db->num_streams; i++)
598 {
599 name = msi_string_lookup( db->strings, db->streams[i].str_index, NULL );
600 if (!(encname = encode_streamname( FALSE, name ))) return ERROR_OUTOFMEMORY;
601
602 hr = IStorage_CreateStream( db->storage, encname, STGM_WRITE|STGM_SHARE_EXCLUSIVE, 0, 0, &stream );
603 if (SUCCEEDED( hr ))
604 {
605 hr = write_stream( stream, db->streams[i].stream );
606 if (FAILED( hr ))
607 {
608 ERR("failed to write stream %s (hr = %08x)\n", debugstr_w(encname), hr);
609 msi_free( encname );
610 IStream_Release( stream );
611 return ERROR_FUNCTION_FAILED;
612 }
613 hr = IStream_Commit( stream, 0 );
614 IStream_Release( stream );
615 if (FAILED( hr ))
616 {
617 ERR("failed to commit stream %s (hr = %08x)\n", debugstr_w(encname), hr);
618 msi_free( encname );
619 return ERROR_FUNCTION_FAILED;
620 }
621 }
622 else if (hr != STG_E_FILEALREADYEXISTS)
623 {
624 ERR("failed to create stream %s (hr = %08x)\n", debugstr_w(encname), hr);
625 msi_free( encname );
626 return ERROR_FUNCTION_FAILED;
627 }
628 msi_free( encname );
629 }
630
631 return ERROR_SUCCESS;
632 }