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
add_stream_to_table(MSISTREAMSVIEW
*sv
, STREAM
*stream
, int index
)
59 if (index
>= sv
->max_streams
)
62 sv
->streams
= msi_realloc(sv
->streams
, sv
->max_streams
* sizeof(STREAM
*));
67 sv
->streams
[index
] = 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_set_row(struct tagMSIVIEW
*view
, UINT row
, MSIRECORD
*rec
, UINT mask
)
134 FIXME("(%p, %d, %p, %d): stub!\n", view
, row
, rec
, mask
);
135 return ERROR_SUCCESS
;
138 static UINT
STREAMS_insert_row(struct tagMSIVIEW
*view
, MSIRECORD
*rec
, BOOL temporary
)
140 MSISTREAMSVIEW
*sv
= (MSISTREAMSVIEW
*)view
;
148 UINT r
= ERROR_FUNCTION_FAILED
;
150 TRACE("(%p, %p, %d)\n", view
, rec
, temporary
);
152 r
= MSI_RecordGetIStream(rec
, 2, &stm
);
153 if (r
!= ERROR_SUCCESS
)
156 hr
= IStream_Stat(stm
, &stat
, STATFLAG_NONAME
);
159 WARN("failed to stat stream: %08x\n", hr
);
163 if (stat
.cbSize
.QuadPart
>> 32)
166 data
= msi_alloc(stat
.cbSize
.QuadPart
);
170 hr
= IStream_Read(stm
, data
, stat
.cbSize
.QuadPart
, &count
);
171 if (FAILED(hr
) || count
!= stat
.cbSize
.QuadPart
)
173 WARN("failed to read stream: %08x\n", hr
);
177 name
= strdupW(MSI_RecordGetString(rec
, 1));
181 r
= write_stream_data(sv
->db
->storage
, name
, data
, count
, FALSE
);
182 if (r
!= ERROR_SUCCESS
)
184 WARN("failed to write stream data: %d\n", r
);
188 stream
= create_stream(sv
, name
, FALSE
, NULL
);
192 IStorage_OpenStream(sv
->db
->storage
, name
, 0,
193 STGM_READ
| STGM_SHARE_EXCLUSIVE
, 0, &stream
->stream
);
195 if (!add_stream_to_table(sv
, stream
, sv
->num_rows
++))
202 IStream_Release(stm
);
207 static UINT
STREAMS_execute(struct tagMSIVIEW
*view
, MSIRECORD
*record
)
209 TRACE("(%p, %p)\n", view
, record
);
210 return ERROR_SUCCESS
;
213 static UINT
STREAMS_close(struct tagMSIVIEW
*view
)
215 TRACE("(%p)\n", view
);
216 return ERROR_SUCCESS
;
219 static UINT
STREAMS_get_dimensions(struct tagMSIVIEW
*view
, UINT
*rows
, UINT
*cols
)
221 MSISTREAMSVIEW
*sv
= (MSISTREAMSVIEW
*)view
;
223 TRACE("(%p, %p, %p)\n", view
, rows
, cols
);
225 if (cols
) *cols
= NUM_STREAMS_COLS
;
226 if (rows
) *rows
= sv
->num_rows
;
228 return ERROR_SUCCESS
;
231 static UINT
STREAMS_get_column_info(struct tagMSIVIEW
*view
,
232 UINT n
, LPWSTR
*name
, UINT
*type
)
234 LPCWSTR name_ptr
= NULL
;
236 static const WCHAR Name
[] = {'N','a','m','e',0};
237 static const WCHAR Data
[] = {'D','a','t','a',0};
239 TRACE("(%p, %d, %p, %p)\n", view
, n
, name
, type
);
241 if (n
== 0 || n
> NUM_STREAMS_COLS
)
242 return ERROR_INVALID_PARAMETER
;
248 if (type
) *type
= MSITYPE_STRING
| MAX_STREAM_NAME_LEN
;
253 if (type
) *type
= MSITYPE_STRING
| MSITYPE_VALID
| MSITYPE_NULLABLE
;
259 *name
= strdupW(name_ptr
);
260 if (!*name
) return ERROR_FUNCTION_FAILED
;
263 return ERROR_SUCCESS
;
266 static UINT
STREAMS_modify(struct tagMSIVIEW
*view
, MSIMODIFY eModifyMode
, MSIRECORD
*rec
)
268 FIXME("(%p, %d, %p): stub!\n", view
, eModifyMode
, rec
);
269 return ERROR_SUCCESS
;
272 static UINT
STREAMS_delete(struct tagMSIVIEW
*view
)
274 MSISTREAMSVIEW
*sv
= (MSISTREAMSVIEW
*)view
;
277 TRACE("(%p)\n", view
);
279 for (i
= 0; i
< sv
->num_rows
; i
++)
281 if (sv
->streams
[i
]->stream
)
282 IStream_Release(sv
->streams
[i
]->stream
);
283 msi_free(sv
->streams
[i
]);
286 msi_free(sv
->streams
);
288 return ERROR_SUCCESS
;
291 static UINT
STREAMS_find_matching_rows(struct tagMSIVIEW
*view
, UINT col
,
292 UINT val
, UINT
*row
, MSIITERHANDLE
*handle
)
294 MSISTREAMSVIEW
*sv
= (MSISTREAMSVIEW
*)view
;
295 UINT index
= (UINT
)*handle
;
297 TRACE("(%d, %d): %d\n", *row
, col
, val
);
299 if (col
== 0 || col
> NUM_STREAMS_COLS
)
300 return ERROR_INVALID_PARAMETER
;
302 while (index
< sv
->num_rows
)
304 if (sv
->streams
[index
]->str_index
== val
)
313 *handle
= (MSIITERHANDLE
)++index
;
314 if (index
>= sv
->num_rows
)
315 return ERROR_NO_MORE_ITEMS
;
317 return ERROR_SUCCESS
;
320 static const MSIVIEWOPS streams_ops
=
323 STREAMS_fetch_stream
,
328 STREAMS_get_dimensions
,
329 STREAMS_get_column_info
,
332 STREAMS_find_matching_rows
335 static UINT
add_streams_to_table(MSISTREAMSVIEW
*sv
)
337 IEnumSTATSTG
*stgenum
= NULL
;
339 STREAM
*stream
= NULL
;
341 UINT count
= 0, size
;
343 hr
= IStorage_EnumElements(sv
->db
->storage
, 0, NULL
, 0, &stgenum
);
348 sv
->streams
= msi_alloc(sizeof(STREAM
*));
355 hr
= IEnumSTATSTG_Next(stgenum
, 1, &stat
, &size
);
356 if (FAILED(hr
) || !size
)
359 /* table streams are not in the _Streams table */
360 if (*stat
.pwcsName
== 0x4840)
363 stream
= create_stream(sv
, stat
.pwcsName
, TRUE
, NULL
);
370 IStorage_OpenStream(sv
->db
->storage
, stat
.pwcsName
, 0,
371 STGM_READ
| STGM_SHARE_EXCLUSIVE
, 0, &stream
->stream
);
373 if (!add_stream_to_table(sv
, stream
, count
++))
380 IEnumSTATSTG_Release(stgenum
);
384 UINT
STREAMS_CreateView(MSIDATABASE
*db
, MSIVIEW
**view
)
388 TRACE("(%p, %p)\n", db
, view
);
390 sv
= msi_alloc(sizeof(MSISTREAMSVIEW
));
392 return ERROR_FUNCTION_FAILED
;
394 sv
->view
.ops
= &streams_ops
;
396 sv
->num_rows
= add_streams_to_table(sv
);
398 if (sv
->num_rows
< 0)
399 return ERROR_FUNCTION_FAILED
;
401 *view
= (MSIVIEW
*)sv
;
403 return ERROR_SUCCESS
;