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
35 #include "wine/debug.h"
37 WINE_DEFAULT_DEBUG_CHANNEL(msidb
);
39 #define NUM_STREAMS_COLS 2
41 typedef struct tagMSISTREAMSVIEW
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
;
56 while (size
>= db
->num_streams_allocated
)
59 UINT new_size
= db
->num_streams_allocated
* 2;
60 if (!(tmp
= msi_realloc_zero( db
->streams
, new_size
* sizeof(MSISTREAM
) ))) return FALSE
;
62 db
->num_streams_allocated
= new_size
;
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
);
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
;
84 static UINT
STREAMS_fetch_stream(struct tagMSIVIEW
*view
, UINT row
, UINT col
, IStream
**stm
)
86 MSISTREAMSVIEW
*sv
= (MSISTREAMSVIEW
*)view
;
90 TRACE("(%p, %d, %d, %p)\n", view
, row
, col
, stm
);
92 if (row
>= sv
->db
->num_streams
)
93 return ERROR_FUNCTION_FAILED
;
96 hr
= IStream_Seek( sv
->db
->streams
[row
].stream
, pos
, STREAM_SEEK_SET
, NULL
);
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
;
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
;
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
);
147 r
= MSI_RecordGetIStream( rec
, 2, &new );
148 if (r
!= ERROR_SUCCESS
)
151 old
= sv
->db
->streams
[row
].stream
;
152 hr
= IStream_QueryInterface( new, &IID_IStream
, (void **)&sv
->db
->streams
[row
].stream
);
153 IStream_Release( new );
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
)
169 str
= MSI_RecordGetString( rec
, 1 );
170 r
= msi_string2id( sv
->db
->strings
, str
, -1, &id
);
171 if (r
!= ERROR_SUCCESS
)
174 for (i
= 0; i
< sv
->db
->num_streams
; i
++)
176 STREAMS_fetch_int( &sv
->view
, i
, 1, &val
);
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
;
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
;
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;
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
);
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
;
281 if (name
) *name
= szName
;
282 if (type
) *type
= MSITYPE_STRING
| MSITYPE_VALID
| MAX_STREAM_NAME_LEN
;
286 if (name
) *name
= szData
;
287 if (type
) *type
= MSITYPE_STRING
| MSITYPE_VALID
| MSITYPE_NULLABLE
;
290 if (table_name
) *table_name
= szStreams
;
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
;
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
;
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
)
323 TRACE("%p %d %p\n", view
, eModifyMode
, rec
);
327 case MSIMODIFY_ASSIGN
:
328 r
= streams_modify_assign(view
, rec
);
331 case MSIMODIFY_INSERT
:
332 r
= STREAMS_insert_row(view
, rec
, -1, FALSE
);
335 case MSIMODIFY_UPDATE
:
336 r
= streams_modify_update(view
, rec
);
339 case MSIMODIFY_DELETE
:
340 r
= STREAMS_delete_row(view
, row
- 1);
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
;
356 r
= ERROR_INVALID_DATA
;
362 static UINT
STREAMS_delete(struct tagMSIVIEW
*view
)
364 MSISTREAMSVIEW
*sv
= (MSISTREAMSVIEW
*)view
;
366 TRACE("(%p)\n", view
);
369 return ERROR_SUCCESS
;
372 static const MSIVIEWOPS streams_ops
=
375 STREAMS_fetch_stream
,
384 STREAMS_get_dimensions
,
385 STREAMS_get_column_info
,
395 static HRESULT
open_stream( MSIDATABASE
*db
, const WCHAR
*name
, IStream
**stream
)
399 hr
= IStorage_OpenStream( db
->storage
, name
, NULL
, STGM_READ
|STGM_SHARE_EXCLUSIVE
, 0, stream
);
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
);
414 static MSISTREAM
*find_stream( MSIDATABASE
*db
, const WCHAR
*name
)
418 r
= msi_string2id( db
->strings
, name
, -1, &id
);
419 if (r
!= ERROR_SUCCESS
)
422 for (i
= 0; i
< db
->num_streams
; i
++)
424 if (db
->streams
[i
].str_index
== id
) return &db
->streams
[i
];
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
;
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
;
450 UINT count
, r
= ERROR_SUCCESS
;
453 hr
= IStorage_EnumElements( db
->storage
, 0, NULL
, 0, &stgenum
);
455 return ERROR_FUNCTION_FAILED
;
460 hr
= IEnumSTATSTG_Next( stgenum
, 1, &stat
, &count
);
461 if (FAILED( hr
) || !count
)
464 /* table streams are not in the _Streams table */
465 if (stat
.type
!= STGTY_STREAM
|| *stat
.pwcsName
== 0x4840)
467 CoTaskMemFree( stat
.pwcsName
);
470 decode_streamname( stat
.pwcsName
, decoded
);
471 if (find_stream( db
, decoded
))
473 CoTaskMemFree( stat
.pwcsName
);
476 TRACE("found new stream %s\n", debugstr_w( decoded
));
478 hr
= open_stream( db
, stat
.pwcsName
, &stream
);
479 CoTaskMemFree( stat
.pwcsName
);
482 ERR("unable to open stream %08x\n", hr
);
483 r
= ERROR_FUNCTION_FAILED
;
487 r
= append_stream( db
, decoded
, stream
);
488 if (r
!= ERROR_SUCCESS
)
492 TRACE("loaded %u streams\n", db
->num_streams
);
493 IEnumSTATSTG_Release( stgenum
);
497 UINT
msi_get_stream( MSIDATABASE
*db
, const WCHAR
*name
, IStream
**ret
)
504 if ((stream
= find_stream( db
, name
)))
509 hr
= IStream_Seek( stream
->stream
, pos
, STREAM_SEEK_SET
, NULL
);
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
);
524 return ERROR_FUNCTION_FAILED
;
526 r
= append_stream( db
, name
, *ret
);
527 if (r
!= ERROR_SUCCESS
)
529 IStream_Release( *ret
);
533 IStream_AddRef( *ret
);
534 return ERROR_SUCCESS
;
537 UINT
STREAMS_CreateView(MSIDATABASE
*db
, MSIVIEW
**view
)
542 TRACE("(%p, %p)\n", db
, view
);
544 r
= load_streams( db
);
545 if (r
!= ERROR_SUCCESS
)
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
;
555 *view
= (MSIVIEW
*)sv
;
557 return ERROR_SUCCESS
;
560 static HRESULT
write_stream( IStream
*dst
, IStream
*src
)
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
;
575 hr
= IStream_Seek( dst
, pos
, STREAM_SEEK_SET
, NULL
);
576 if (FAILED( hr
)) return hr
;
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
);
587 stat
.cbSize
.QuadPart
-= count
;
591 hr
= IStream_Write( dst
, buf
, size
, &count
);
592 if (FAILED( hr
) || count
!= size
)
594 WARN("failed to write stream: %08x\n", hr
);
598 if (!stat
.cbSize
.QuadPart
) break;
604 UINT
msi_commit_streams( MSIDATABASE
*db
)
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
, szSumInfo
)) 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
);
625 hr
= write_stream( stream
, db
->streams
[i
].stream
);
628 ERR("failed to write stream %s (hr = %08x)\n", debugstr_w(encname
), hr
);
630 IStream_Release( stream
);
631 return ERROR_FUNCTION_FAILED
;
633 hr
= IStream_Commit( stream
, 0 );
634 IStream_Release( stream
);
637 ERR("failed to commit stream %s (hr = %08x)\n", debugstr_w(encname
), hr
);
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
);
646 return ERROR_FUNCTION_FAILED
;
651 return ERROR_SUCCESS
;