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
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
47 typedef struct tagMSISTREAMSVIEW
57 static BOOL
streams_set_table_size(MSISTREAMSVIEW
*sv
, UINT size
)
59 if (size
>= sv
->max_streams
)
62 sv
->streams
= msi_realloc(sv
->streams
, sv
->max_streams
* sizeof(STREAM
*));
70 static STREAM
*create_stream(MSISTREAMSVIEW
*sv
, LPWSTR name
, BOOL encoded
, IStream
*stm
)
73 WCHAR decoded
[MAX_STREAM_NAME_LEN
];
76 stream
= msi_alloc(sizeof(STREAM
));
82 decode_streamname(name
, decoded
);
84 TRACE("stream -> %s %s\n", debugstr_w(name
), debugstr_w(decoded
));
87 stream
->name
= strdupW(ptr
);
94 stream
->str_index
= msi_addstringW(sv
->db
->strings
, 0, stream
->name
, -1, 1, StringNonPersistent
);
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
);
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
;
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
)
161 hr
= IStream_Stat(stm
, &stat
, STATFLAG_NONAME
);
164 WARN("failed to stat stream: %08x\n", hr
);
168 if (stat
.cbSize
.QuadPart
>> 32)
171 data
= msi_alloc(stat
.cbSize
.QuadPart
);
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
);
182 name
= strdupW(MSI_RecordGetString(rec
, 1));
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
);
193 stream
= create_stream(sv
, name
, FALSE
, NULL
);
197 IStorage_OpenStream(sv
->db
->storage
, name
, 0,
198 STGM_READ
| STGM_SHARE_EXCLUSIVE
, 0, &stream
->stream
);
200 sv
->streams
[row
] = stream
;
206 IStream_Release(stm
);
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
;
268 if (type
) *type
= MSITYPE_STRING
| MAX_STREAM_NAME_LEN
;
273 if (type
) *type
= MSITYPE_STRING
| MSITYPE_VALID
| MSITYPE_NULLABLE
;
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
)
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
);
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
;
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
;
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
)
336 TRACE("%p %d %p\n", view
, eModifyMode
, rec
);
340 case MSIMODIFY_ASSIGN
:
341 r
= streams_modify_assign(view
, rec
);
344 case MSIMODIFY_INSERT
:
345 r
= STREAMS_insert_row(view
, rec
, FALSE
);
348 case MSIMODIFY_UPDATE
:
349 r
= streams_modify_update(view
, rec
);
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
;
366 r
= ERROR_INVALID_DATA
;
372 static UINT
STREAMS_delete(struct tagMSIVIEW
*view
)
374 MSISTREAMSVIEW
*sv
= (MSISTREAMSVIEW
*)view
;
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
)
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
=
423 STREAMS_fetch_stream
,
430 STREAMS_get_dimensions
,
431 STREAMS_get_column_info
,
434 STREAMS_find_matching_rows
,
442 static INT
add_streams_to_table(MSISTREAMSVIEW
*sv
)
444 IEnumSTATSTG
*stgenum
= NULL
;
446 STREAM
*stream
= NULL
;
448 UINT count
= 0, size
;
450 hr
= IStorage_EnumElements(sv
->db
->storage
, 0, NULL
, 0, &stgenum
);
455 sv
->streams
= msi_alloc(sizeof(STREAM
*));
462 hr
= IEnumSTATSTG_Next(stgenum
, 1, &stat
, &size
);
463 if (FAILED(hr
) || !size
)
466 /* table streams are not in the _Streams table */
467 if (*stat
.pwcsName
== 0x4840)
469 CoTaskMemFree(stat
.pwcsName
);
473 stream
= create_stream(sv
, stat
.pwcsName
, TRUE
, NULL
);
477 CoTaskMemFree(stat
.pwcsName
);
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
))
491 sv
->streams
[count
- 1] = stream
;
494 IEnumSTATSTG_Release(stgenum
);
498 UINT
STREAMS_CreateView(MSIDATABASE
*db
, MSIVIEW
**view
)
503 TRACE("(%p, %p)\n", db
, view
);
505 sv
= msi_alloc(sizeof(MSISTREAMSVIEW
));
507 return ERROR_FUNCTION_FAILED
;
509 sv
->view
.ops
= &streams_ops
;
511 rows
= add_streams_to_table(sv
);
513 return ERROR_FUNCTION_FAILED
;
516 *view
= (MSIVIEW
*)sv
;
518 return ERROR_SUCCESS
;