Autosyncing with Wine HEAD
[reactos.git] / reactos / dll / win32 / msi / database.c
1 /*
2 * Implementation of the Microsoft Installer (msi.dll)
3 *
4 * Copyright 2002,2003,2004,2005 Mike McCormack for CodeWeavers
5 *
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.
10 *
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.
15 *
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
19 */
20
21 #include <stdarg.h>
22
23 #define COBJMACROS
24 #define NONAMELESSUNION
25
26 #include "windef.h"
27 #include "winbase.h"
28 #include "winreg.h"
29 #include "winnls.h"
30 #include "wine/debug.h"
31 #include "wine/unicode.h"
32 #include "msi.h"
33 #include "msiquery.h"
34 #include "msipriv.h"
35 #include "objidl.h"
36 #include "objbase.h"
37 #include "msiserver.h"
38
39 #include "initguid.h"
40
41 WINE_DEFAULT_DEBUG_CHANNEL(msi);
42
43 DEFINE_GUID( CLSID_MsiDatabase, 0x000c1084, 0x0000, 0x0000,
44 0xc0,0x00,0x00,0x00,0x00,0x00,0x00,0x46);
45 DEFINE_GUID( CLSID_MsiPatch, 0x000c1086, 0x0000, 0x0000,
46 0xc0,0x00,0x00,0x00,0x00,0x00,0x00,0x46);
47
48 /*
49 * .MSI file format
50 *
51 * An .msi file is a structured storage file.
52 * It contains a number of streams.
53 * A stream for each table in the database.
54 * Two streams for the string table in the database.
55 * Any binary data in a table is a reference to a stream.
56 */
57
58 static VOID MSI_CloseDatabase( MSIOBJECTHDR *arg )
59 {
60 MSIDATABASE *db = (MSIDATABASE *) arg;
61
62 msi_free(db->path);
63 free_cached_tables( db );
64 msi_free_transforms( db );
65 msi_destroy_stringtable( db->strings );
66 IStorage_Release( db->storage );
67 if (db->deletefile)
68 {
69 DeleteFileW( db->deletefile );
70 msi_free( db->deletefile );
71 }
72 }
73
74 UINT MSI_OpenDatabaseW(LPCWSTR szDBPath, LPCWSTR szPersist, MSIDATABASE **pdb)
75 {
76 IStorage *stg = NULL;
77 HRESULT r;
78 MSIDATABASE *db = NULL;
79 UINT ret = ERROR_FUNCTION_FAILED;
80 LPCWSTR szMode, save_path;
81 STATSTG stat;
82 BOOL created = FALSE;
83 WCHAR path[MAX_PATH];
84
85 static const WCHAR backslash[] = {'\\',0};
86 static const WCHAR szTables[] = { '_','T','a','b','l','e','s',0 };
87
88 TRACE("%s %s\n",debugstr_w(szDBPath),debugstr_w(szPersist) );
89
90 if( !pdb )
91 return ERROR_INVALID_PARAMETER;
92
93 save_path = szDBPath;
94 szMode = szPersist;
95 if( HIWORD( szPersist ) )
96 {
97 if (!CopyFileW( szDBPath, szPersist, FALSE ))
98 return ERROR_OPEN_FAILED;
99
100 szDBPath = szPersist;
101 szPersist = MSIDBOPEN_TRANSACT;
102 created = TRUE;
103 }
104
105 if( szPersist == MSIDBOPEN_READONLY )
106 {
107 r = StgOpenStorage( szDBPath, NULL,
108 STGM_DIRECT|STGM_READ|STGM_SHARE_DENY_WRITE, NULL, 0, &stg);
109 }
110 else if( szPersist == MSIDBOPEN_CREATE || szPersist == MSIDBOPEN_CREATEDIRECT )
111 {
112 /* FIXME: MSIDBOPEN_CREATE should case STGM_TRANSACTED flag to be
113 * used here: */
114 r = StgCreateDocfile( szDBPath,
115 STGM_CREATE|STGM_DIRECT|STGM_READWRITE|STGM_SHARE_EXCLUSIVE, 0, &stg);
116 if( r == ERROR_SUCCESS )
117 {
118 IStorage_SetClass( stg, &CLSID_MsiDatabase );
119 /* create the _Tables stream */
120 r = write_stream_data(stg, szTables, NULL, 0, TRUE);
121 if (!FAILED(r))
122 r = msi_init_string_table( stg );
123 }
124 created = TRUE;
125 }
126 else if( szPersist == MSIDBOPEN_TRANSACT )
127 {
128 /* FIXME: MSIDBOPEN_TRANSACT should case STGM_TRANSACTED flag to be
129 * used here: */
130 r = StgOpenStorage( szDBPath, NULL,
131 STGM_DIRECT|STGM_READWRITE|STGM_SHARE_EXCLUSIVE, NULL, 0, &stg);
132 }
133 else if( szPersist == MSIDBOPEN_DIRECT )
134 {
135 r = StgOpenStorage( szDBPath, NULL,
136 STGM_DIRECT|STGM_READWRITE|STGM_SHARE_EXCLUSIVE, NULL, 0, &stg);
137 }
138 else
139 {
140 ERR("unknown flag %p\n",szPersist);
141 return ERROR_INVALID_PARAMETER;
142 }
143
144 if( FAILED( r ) || !stg )
145 {
146 FIXME("open failed r = %08x for %s\n", r, debugstr_w(szDBPath));
147 return ERROR_FUNCTION_FAILED;
148 }
149
150 r = IStorage_Stat( stg, &stat, STATFLAG_NONAME );
151 if( FAILED( r ) )
152 {
153 FIXME("Failed to stat storage\n");
154 goto end;
155 }
156
157 if ( !IsEqualGUID( &stat.clsid, &CLSID_MsiDatabase ) &&
158 !IsEqualGUID( &stat.clsid, &CLSID_MsiPatch ) )
159 {
160 ERR("storage GUID is not a MSI database GUID %s\n",
161 debugstr_guid(&stat.clsid) );
162 goto end;
163 }
164
165 db = alloc_msiobject( MSIHANDLETYPE_DATABASE, sizeof (MSIDATABASE),
166 MSI_CloseDatabase );
167 if( !db )
168 {
169 FIXME("Failed to allocate a handle\n");
170 goto end;
171 }
172
173 if (!strchrW( save_path, '\\' ))
174 {
175 GetCurrentDirectoryW( MAX_PATH, path );
176 lstrcatW( path, backslash );
177 lstrcatW( path, save_path );
178 }
179 else
180 lstrcpyW( path, save_path );
181
182 db->path = strdupW( path );
183
184 if( TRACE_ON( msi ) )
185 enum_stream_names( stg );
186
187 db->storage = stg;
188 db->mode = szMode;
189 if (created)
190 db->deletefile = strdupW( szDBPath );
191 else
192 db->deletefile = NULL;
193 list_init( &db->tables );
194 list_init( &db->transforms );
195
196 db->strings = msi_load_string_table( stg, &db->bytes_per_strref );
197 if( !db->strings )
198 goto end;
199
200 msi_table_set_strref( db->bytes_per_strref );
201 ret = ERROR_SUCCESS;
202
203 msiobj_addref( &db->hdr );
204 IStorage_AddRef( stg );
205 *pdb = db;
206
207 end:
208 if( db )
209 msiobj_release( &db->hdr );
210 if( stg )
211 IStorage_Release( stg );
212
213 return ret;
214 }
215
216 UINT WINAPI MsiOpenDatabaseW(LPCWSTR szDBPath, LPCWSTR szPersist, MSIHANDLE *phDB)
217 {
218 MSIDATABASE *db;
219 UINT ret;
220
221 TRACE("%s %s %p\n",debugstr_w(szDBPath),debugstr_w(szPersist), phDB);
222
223 ret = MSI_OpenDatabaseW( szDBPath, szPersist, &db );
224 if( ret == ERROR_SUCCESS )
225 {
226 *phDB = alloc_msihandle( &db->hdr );
227 if (! *phDB)
228 ret = ERROR_NOT_ENOUGH_MEMORY;
229 msiobj_release( &db->hdr );
230 }
231
232 return ret;
233 }
234
235 UINT WINAPI MsiOpenDatabaseA(LPCSTR szDBPath, LPCSTR szPersist, MSIHANDLE *phDB)
236 {
237 HRESULT r = ERROR_FUNCTION_FAILED;
238 LPWSTR szwDBPath = NULL, szwPersist = NULL;
239
240 TRACE("%s %s %p\n", debugstr_a(szDBPath), debugstr_a(szPersist), phDB);
241
242 if( szDBPath )
243 {
244 szwDBPath = strdupAtoW( szDBPath );
245 if( !szwDBPath )
246 goto end;
247 }
248
249 if( HIWORD(szPersist) )
250 {
251 szwPersist = strdupAtoW( szPersist );
252 if( !szwPersist )
253 goto end;
254 }
255 else
256 szwPersist = (LPWSTR)(DWORD_PTR)szPersist;
257
258 r = MsiOpenDatabaseW( szwDBPath, szwPersist, phDB );
259
260 end:
261 if( HIWORD(szPersist) )
262 msi_free( szwPersist );
263 msi_free( szwDBPath );
264
265 return r;
266 }
267
268 static LPWSTR msi_read_text_archive(LPCWSTR path)
269 {
270 HANDLE file;
271 LPSTR data = NULL;
272 LPWSTR wdata = NULL;
273 DWORD read, size = 0;
274
275 file = CreateFileW( path, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL );
276 if (file == INVALID_HANDLE_VALUE)
277 return NULL;
278
279 size = GetFileSize( file, NULL );
280 data = msi_alloc( size + 1 );
281 if (!data)
282 goto done;
283
284 if (!ReadFile( file, data, size, &read, NULL ))
285 goto done;
286
287 data[size] = '\0';
288 wdata = strdupAtoW( data );
289
290 done:
291 CloseHandle( file );
292 msi_free( data );
293 return wdata;
294 }
295
296 static void msi_parse_line(LPWSTR *line, LPWSTR **entries, DWORD *num_entries)
297 {
298 LPWSTR ptr = *line, save;
299 DWORD i, count = 1;
300
301 *entries = NULL;
302
303 /* stay on this line */
304 while (*ptr && *ptr != '\n')
305 {
306 /* entries are separated by tabs */
307 if (*ptr == '\t')
308 count++;
309
310 ptr++;
311 }
312
313 *entries = msi_alloc(count * sizeof(LPWSTR));
314 if (!*entries)
315 return;
316
317 /* store pointers into the data */
318 for (i = 0, ptr = *line; i < count; i++)
319 {
320 while (*ptr && *ptr == '\r') ptr++;
321 save = ptr;
322
323 while (*ptr && *ptr != '\t' && *ptr != '\n' && *ptr != '\r') ptr++;
324
325 /* NULL-separate the data */
326 if (*ptr == '\n' || *ptr == '\r')
327 {
328 while (*ptr == '\n' || *ptr == '\r')
329 *(ptr++) = '\0';
330 }
331 else if (*ptr)
332 *ptr++ = '\0';
333
334 (*entries)[i] = save;
335 }
336
337 /* move to the next line if there's more, else EOF */
338 *line = ptr;
339
340 if (num_entries)
341 *num_entries = count;
342 }
343
344 static LPWSTR msi_build_createsql_prelude(LPWSTR table)
345 {
346 LPWSTR prelude;
347 DWORD size;
348
349 static const WCHAR create_fmt[] = {'C','R','E','A','T','E',' ','T','A','B','L','E',' ','`','%','s','`',' ','(',' ',0};
350
351 size = sizeof(create_fmt)/sizeof(create_fmt[0]) + lstrlenW(table) - 2;
352 prelude = msi_alloc(size * sizeof(WCHAR));
353 if (!prelude)
354 return NULL;
355
356 sprintfW(prelude, create_fmt, table);
357 return prelude;
358 }
359
360 static LPWSTR msi_build_createsql_columns(LPWSTR *columns_data, LPWSTR *types, DWORD num_columns)
361 {
362 LPWSTR columns, p;
363 LPCWSTR type;
364 DWORD sql_size = 1, i, len;
365 WCHAR expanded[128], *ptr;
366 WCHAR size[10], comma[2], extra[30];
367
368 static const WCHAR column_fmt[] = {'`','%','s','`',' ','%','s','%','s','%','s','%','s',' ',0};
369 static const WCHAR size_fmt[] = {'(','%','s',')',0};
370 static const WCHAR type_char[] = {'C','H','A','R',0};
371 static const WCHAR type_int[] = {'I','N','T',0};
372 static const WCHAR type_long[] = {'L','O','N','G',0};
373 static const WCHAR type_notnull[] = {' ','N','O','T',' ','N','U','L','L',0};
374 static const WCHAR localizable[] = {' ','L','O','C','A','L','I','Z','A','B','L','E',0};
375
376 columns = msi_alloc_zero(sql_size * sizeof(WCHAR));
377 if (!columns)
378 return NULL;
379
380 for (i = 0; i < num_columns; i++)
381 {
382 type = NULL;
383 comma[1] = size[0] = extra[0] = '\0';
384
385 if (i == num_columns - 1)
386 comma[0] = '\0';
387 else
388 comma[0] = ',';
389
390 ptr = &types[i][1];
391 len = atolW(ptr);
392 extra[0] = '\0';
393
394 switch (types[i][0])
395 {
396 case 'l':
397 lstrcpyW(extra, type_notnull);
398 case 'L':
399 lstrcatW(extra, localizable);
400 type = type_char;
401 sprintfW(size, size_fmt, ptr);
402 break;
403 case 's':
404 lstrcpyW(extra, type_notnull);
405 case 'S':
406 type = type_char;
407 sprintfW(size, size_fmt, ptr);
408 break;
409 case 'i':
410 lstrcpyW(extra, type_notnull);
411 case 'I':
412 if (len == 2)
413 type = type_int;
414 else
415 type = type_long;
416 break;
417 default:
418 ERR("Unknown type: %c\n", types[i][0]);
419 msi_free(columns);
420 return NULL;
421 }
422
423 sprintfW(expanded, column_fmt, columns_data[i], type, size, extra, comma);
424 sql_size += lstrlenW(expanded);
425
426 p = msi_realloc(columns, sql_size * sizeof(WCHAR));
427 if (!p)
428 {
429 msi_free(columns);
430 return NULL;
431 }
432 columns = p;
433
434 lstrcatW(columns, expanded);
435 }
436
437 return columns;
438 }
439
440 static LPWSTR msi_build_createsql_postlude(LPWSTR *primary_keys, DWORD num_keys)
441 {
442 LPWSTR postlude, keys, ptr;
443 DWORD size, key_size, i;
444
445 static const WCHAR key_fmt[] = {'`','%','s','`',',',' ',0};
446 static const WCHAR postlude_fmt[] = {'P','R','I','M','A','R','Y',' ','K','E','Y',' ','%','s',')',0};
447
448 for (i = 0, size = 1; i < num_keys; i++)
449 size += lstrlenW(key_fmt) + lstrlenW(primary_keys[i]) - 2;
450
451 keys = msi_alloc(size * sizeof(WCHAR));
452 if (!keys)
453 return NULL;
454
455 for (i = 0, ptr = keys; i < num_keys; i++)
456 {
457 key_size = lstrlenW(key_fmt) + lstrlenW(primary_keys[i]) -2;
458 sprintfW(ptr, key_fmt, primary_keys[i]);
459 ptr += key_size;
460 }
461
462 /* remove final ', ' */
463 *(ptr - 2) = '\0';
464
465 size = lstrlenW(postlude_fmt) + size - 1;
466 postlude = msi_alloc(size * sizeof(WCHAR));
467 if (!postlude)
468 goto done;
469
470 sprintfW(postlude, postlude_fmt, keys);
471
472 done:
473 msi_free(keys);
474 return postlude;
475 }
476
477 static UINT msi_add_table_to_db(MSIDATABASE *db, LPWSTR *columns, LPWSTR *types, LPWSTR *labels, DWORD num_labels, DWORD num_columns)
478 {
479 UINT r;
480 DWORD size;
481 MSIQUERY *view;
482 LPWSTR create_sql;
483 LPWSTR prelude, columns_sql, postlude;
484
485 prelude = msi_build_createsql_prelude(labels[0]);
486 columns_sql = msi_build_createsql_columns(columns, types, num_columns);
487 postlude = msi_build_createsql_postlude(labels + 1, num_labels - 1); /* skip over table name */
488
489 if (!prelude || !columns_sql || !postlude)
490 return ERROR_OUTOFMEMORY;
491
492 size = lstrlenW(prelude) + lstrlenW(columns_sql) + lstrlenW(postlude) + 1;
493 create_sql = msi_alloc(size * sizeof(WCHAR));
494 if (!create_sql)
495 return ERROR_OUTOFMEMORY;
496
497 lstrcpyW(create_sql, prelude);
498 lstrcatW(create_sql, columns_sql);
499 lstrcatW(create_sql, postlude);
500
501 msi_free(prelude);
502 msi_free(columns_sql);
503 msi_free(postlude);
504
505 r = MSI_DatabaseOpenViewW( db, create_sql, &view );
506 msi_free(create_sql);
507
508 if (r != ERROR_SUCCESS)
509 return r;
510
511 r = MSI_ViewExecute(view, NULL);
512 MSI_ViewClose(view);
513 msiobj_release(&view->hdr);
514
515 return r;
516 }
517
518 static UINT construct_record(DWORD num_columns, LPWSTR *types,
519 LPWSTR *data, MSIRECORD **rec)
520 {
521 UINT i;
522
523 *rec = MSI_CreateRecord(num_columns);
524 if (!*rec)
525 return ERROR_OUTOFMEMORY;
526
527 for (i = 0; i < num_columns; i++)
528 {
529 switch (types[i][0])
530 {
531 case 'L': case 'l': case 'S': case 's':
532 MSI_RecordSetStringW(*rec, i + 1, data[i]);
533 break;
534 case 'I': case 'i':
535 if (*data[i])
536 MSI_RecordSetInteger(*rec, i + 1, atoiW(data[i]));
537 break;
538 default:
539 ERR("Unhandled column type: %c\n", types[i][0]);
540 msiobj_release(&(*rec)->hdr);
541 return ERROR_FUNCTION_FAILED;
542 }
543 }
544
545 return ERROR_SUCCESS;
546 }
547
548 static UINT msi_add_records_to_table(MSIDATABASE *db, LPWSTR *columns, LPWSTR *types,
549 LPWSTR *labels, LPWSTR **records,
550 int num_columns, int num_records)
551 {
552 UINT r;
553 DWORD i, size;
554 MSIQUERY *view;
555 MSIRECORD *rec;
556 LPWSTR query;
557
558 static const WCHAR select[] = {
559 'S','E','L','E','C','T',' ','*',' ',
560 'F','R','O','M',' ','`','%','s','`',0
561 };
562
563 size = lstrlenW(select) + lstrlenW(labels[0]) - 1;
564 query = msi_alloc(size * sizeof(WCHAR));
565 if (!query)
566 return ERROR_OUTOFMEMORY;
567
568 sprintfW(query, select, labels[0]);
569
570 r = MSI_DatabaseOpenViewW(db, query, &view);
571 msi_free(query);
572 if (r != ERROR_SUCCESS)
573 return r;
574
575 while (MSI_ViewFetch(view, &rec) != ERROR_NO_MORE_ITEMS)
576 {
577 r = MSI_ViewModify(view, MSIMODIFY_DELETE, rec);
578 if (r != ERROR_SUCCESS)
579 goto done;
580 }
581
582 for (i = 0; i < num_records; i++)
583 {
584 r = construct_record(num_columns, types, records[i], &rec);
585 if (r != ERROR_SUCCESS)
586 goto done;
587
588 r = MSI_ViewModify(view, MSIMODIFY_INSERT, rec);
589 if (r != ERROR_SUCCESS)
590 {
591 msiobj_release(&rec->hdr);
592 goto done;
593 }
594
595 msiobj_release(&rec->hdr);
596 }
597
598 done:
599 msiobj_release(&view->hdr);
600 return r;
601 }
602
603 UINT MSI_DatabaseImport(MSIDATABASE *db, LPCWSTR folder, LPCWSTR file)
604 {
605 UINT r;
606 DWORD len, i;
607 DWORD num_labels, num_types;
608 DWORD num_columns, num_records = 0;
609 LPWSTR *columns, *types, *labels;
610 LPWSTR path, ptr, data;
611 LPWSTR **records = NULL;
612 LPWSTR **temp_records;
613
614 static const WCHAR backslash[] = {'\\',0};
615
616 TRACE("%p %s %s\n", db, debugstr_w(folder), debugstr_w(file) );
617
618 if( folder == NULL || file == NULL )
619 return ERROR_INVALID_PARAMETER;
620
621 len = lstrlenW(folder) + lstrlenW(backslash) + lstrlenW(file) + 1;
622 path = msi_alloc( len * sizeof(WCHAR) );
623 if (!path)
624 return ERROR_OUTOFMEMORY;
625
626 lstrcpyW( path, folder );
627 lstrcatW( path, backslash );
628 lstrcatW( path, file );
629
630 data = msi_read_text_archive( path );
631
632 ptr = data;
633 msi_parse_line( &ptr, &columns, &num_columns );
634 msi_parse_line( &ptr, &types, &num_types );
635 msi_parse_line( &ptr, &labels, &num_labels );
636
637 if (num_columns != num_types)
638 {
639 r = ERROR_FUNCTION_FAILED;
640 goto done;
641 }
642
643 records = msi_alloc(sizeof(LPWSTR *));
644 if (!records)
645 {
646 r = ERROR_OUTOFMEMORY;
647 goto done;
648 }
649
650 /* read in the table records */
651 while (*ptr)
652 {
653 msi_parse_line( &ptr, &records[num_records], NULL );
654
655 num_records++;
656 temp_records = msi_realloc(records, (num_records + 1) * sizeof(LPWSTR *));
657 if (!temp_records)
658 {
659 r = ERROR_OUTOFMEMORY;
660 goto done;
661 }
662 records = temp_records;
663 }
664
665 if (!TABLE_Exists(db, labels[0]))
666 {
667 r = msi_add_table_to_db( db, columns, types, labels, num_labels, num_columns );
668 if (r != ERROR_SUCCESS)
669 {
670 r = ERROR_FUNCTION_FAILED;
671 goto done;
672 }
673 }
674
675 r = msi_add_records_to_table( db, columns, types, labels, records, num_columns, num_records );
676
677 done:
678 msi_free(path);
679 msi_free(data);
680 msi_free(columns);
681 msi_free(types);
682 msi_free(labels);
683
684 for (i = 0; i < num_records; i++)
685 msi_free(records[i]);
686
687 msi_free(records);
688
689 return r;
690 }
691
692 UINT WINAPI MsiDatabaseImportW(MSIHANDLE handle, LPCWSTR szFolder, LPCWSTR szFilename)
693 {
694 MSIDATABASE *db;
695 UINT r;
696
697 TRACE("%lx %s %s\n",handle,debugstr_w(szFolder), debugstr_w(szFilename));
698
699 db = msihandle2msiinfo( handle, MSIHANDLETYPE_DATABASE );
700 if( !db )
701 {
702 IWineMsiRemoteDatabase *remote_database;
703
704 remote_database = (IWineMsiRemoteDatabase *)msi_get_remote( handle );
705 if ( !remote_database )
706 return ERROR_INVALID_HANDLE;
707
708 IWineMsiRemoteDatabase_Release( remote_database );
709 WARN("MsiDatabaseImport not allowed during a custom action!\n");
710
711 return ERROR_SUCCESS;
712 }
713
714 r = MSI_DatabaseImport( db, szFolder, szFilename );
715 msiobj_release( &db->hdr );
716 return r;
717 }
718
719 UINT WINAPI MsiDatabaseImportA( MSIHANDLE handle,
720 LPCSTR szFolder, LPCSTR szFilename )
721 {
722 LPWSTR path = NULL, file = NULL;
723 UINT r = ERROR_OUTOFMEMORY;
724
725 TRACE("%lx %s %s\n", handle, debugstr_a(szFolder), debugstr_a(szFilename));
726
727 if( szFolder )
728 {
729 path = strdupAtoW( szFolder );
730 if( !path )
731 goto end;
732 }
733
734 if( szFilename )
735 {
736 file = strdupAtoW( szFilename );
737 if( !file )
738 goto end;
739 }
740
741 r = MsiDatabaseImportW( handle, path, file );
742
743 end:
744 msi_free( path );
745 msi_free( file );
746
747 return r;
748 }
749
750 static UINT msi_export_record( HANDLE handle, MSIRECORD *row, UINT start )
751 {
752 UINT i, count, len, r = ERROR_SUCCESS;
753 const char *sep;
754 char *buffer;
755 DWORD sz;
756
757 len = 0x100;
758 buffer = msi_alloc( len );
759 if ( !buffer )
760 return ERROR_OUTOFMEMORY;
761
762 count = MSI_RecordGetFieldCount( row );
763 for ( i=start; i<=count; i++ )
764 {
765 sz = len;
766 r = MSI_RecordGetStringA( row, i, buffer, &sz );
767 if (r == ERROR_MORE_DATA)
768 {
769 char *p = msi_realloc( buffer, sz + 1 );
770 if (!p)
771 break;
772 len = sz + 1;
773 buffer = p;
774 }
775 sz = len;
776 r = MSI_RecordGetStringA( row, i, buffer, &sz );
777 if (r != ERROR_SUCCESS)
778 break;
779
780 if (!WriteFile( handle, buffer, sz, &sz, NULL ))
781 {
782 r = ERROR_FUNCTION_FAILED;
783 break;
784 }
785
786 sep = (i < count) ? "\t" : "\r\n";
787 if (!WriteFile( handle, sep, strlen(sep), &sz, NULL ))
788 {
789 r = ERROR_FUNCTION_FAILED;
790 break;
791 }
792 }
793 msi_free( buffer );
794 return r;
795 }
796
797 static UINT msi_export_row( MSIRECORD *row, void *arg )
798 {
799 return msi_export_record( arg, row, 1 );
800 }
801
802 static UINT msi_export_forcecodepage( HANDLE handle )
803 {
804 DWORD sz;
805
806 static const char data[] = "\r\n\r\n0\t_ForceCodepage\r\n";
807
808 FIXME("Read the codepage from the strings table!\n");
809
810 sz = lstrlenA(data) + 1;
811 if (!WriteFile(handle, data, sz, &sz, NULL))
812 return ERROR_FUNCTION_FAILED;
813
814 return ERROR_SUCCESS;
815 }
816
817 UINT MSI_DatabaseExport( MSIDATABASE *db, LPCWSTR table,
818 LPCWSTR folder, LPCWSTR file )
819 {
820 static const WCHAR query[] = {
821 's','e','l','e','c','t',' ','*',' ','f','r','o','m',' ','%','s',0 };
822 static const WCHAR szbs[] = { '\\', 0 };
823 static const WCHAR forcecodepage[] = {
824 '_','F','o','r','c','e','C','o','d','e','p','a','g','e',0 };
825 MSIRECORD *rec = NULL;
826 MSIQUERY *view = NULL;
827 LPWSTR filename;
828 HANDLE handle;
829 UINT len, r;
830
831 TRACE("%p %s %s %s\n", db, debugstr_w(table),
832 debugstr_w(folder), debugstr_w(file) );
833
834 if( folder == NULL || file == NULL )
835 return ERROR_INVALID_PARAMETER;
836
837 len = lstrlenW(folder) + lstrlenW(file) + 2;
838 filename = msi_alloc(len * sizeof (WCHAR));
839 if (!filename)
840 return ERROR_OUTOFMEMORY;
841
842 lstrcpyW( filename, folder );
843 lstrcatW( filename, szbs );
844 lstrcatW( filename, file );
845
846 handle = CreateFileW( filename, GENERIC_READ | GENERIC_WRITE, 0,
847 NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL );
848 msi_free( filename );
849 if (handle == INVALID_HANDLE_VALUE)
850 return ERROR_FUNCTION_FAILED;
851
852 if (!lstrcmpW( table, forcecodepage ))
853 {
854 r = msi_export_forcecodepage( handle );
855 goto done;
856 }
857
858 r = MSI_OpenQuery( db, &view, query, table );
859 if (r == ERROR_SUCCESS)
860 {
861 /* write out row 1, the column names */
862 r = MSI_ViewGetColumnInfo(view, MSICOLINFO_NAMES, &rec);
863 if (r == ERROR_SUCCESS)
864 {
865 msi_export_record( handle, rec, 1 );
866 msiobj_release( &rec->hdr );
867 }
868
869 /* write out row 2, the column types */
870 r = MSI_ViewGetColumnInfo(view, MSICOLINFO_TYPES, &rec);
871 if (r == ERROR_SUCCESS)
872 {
873 msi_export_record( handle, rec, 1 );
874 msiobj_release( &rec->hdr );
875 }
876
877 /* write out row 3, the table name + keys */
878 r = MSI_DatabaseGetPrimaryKeys( db, table, &rec );
879 if (r == ERROR_SUCCESS)
880 {
881 MSI_RecordSetStringW( rec, 0, table );
882 msi_export_record( handle, rec, 0 );
883 msiobj_release( &rec->hdr );
884 }
885
886 /* write out row 4 onwards, the data */
887 r = MSI_IterateRecords( view, 0, msi_export_row, handle );
888 msiobj_release( &view->hdr );
889 }
890
891 done:
892 CloseHandle( handle );
893 return r;
894 }
895
896 /***********************************************************************
897 * MsiExportDatabaseW [MSI.@]
898 *
899 * Writes a file containing the table data as tab separated ASCII.
900 *
901 * The format is as follows:
902 *
903 * row1 : colname1 <tab> colname2 <tab> .... colnameN <cr> <lf>
904 * row2 : coltype1 <tab> coltype2 <tab> .... coltypeN <cr> <lf>
905 * row3 : tablename <tab> key1 <tab> key2 <tab> ... keyM <cr> <lf>
906 *
907 * Followed by the data, starting at row 1 with one row per line
908 *
909 * row4 : data <tab> data <tab> data <tab> ... data <cr> <lf>
910 */
911 UINT WINAPI MsiDatabaseExportW( MSIHANDLE handle, LPCWSTR szTable,
912 LPCWSTR szFolder, LPCWSTR szFilename )
913 {
914 MSIDATABASE *db;
915 UINT r;
916
917 TRACE("%lx %s %s %s\n", handle, debugstr_w(szTable),
918 debugstr_w(szFolder), debugstr_w(szFilename));
919
920 db = msihandle2msiinfo( handle, MSIHANDLETYPE_DATABASE );
921 if( !db )
922 {
923 IWineMsiRemoteDatabase *remote_database;
924
925 remote_database = (IWineMsiRemoteDatabase *)msi_get_remote( handle );
926 if ( !remote_database )
927 return ERROR_INVALID_HANDLE;
928
929 IWineMsiRemoteDatabase_Release( remote_database );
930 WARN("MsiDatabaseExport not allowed during a custom action!\n");
931
932 return ERROR_SUCCESS;
933 }
934
935 r = MSI_DatabaseExport( db, szTable, szFolder, szFilename );
936 msiobj_release( &db->hdr );
937 return r;
938 }
939
940 UINT WINAPI MsiDatabaseExportA( MSIHANDLE handle, LPCSTR szTable,
941 LPCSTR szFolder, LPCSTR szFilename )
942 {
943 LPWSTR path = NULL, file = NULL, table = NULL;
944 UINT r = ERROR_OUTOFMEMORY;
945
946 TRACE("%lx %s %s %s\n", handle, debugstr_a(szTable),
947 debugstr_a(szFolder), debugstr_a(szFilename));
948
949 if( szTable )
950 {
951 table = strdupAtoW( szTable );
952 if( !table )
953 goto end;
954 }
955
956 if( szFolder )
957 {
958 path = strdupAtoW( szFolder );
959 if( !path )
960 goto end;
961 }
962
963 if( szFilename )
964 {
965 file = strdupAtoW( szFilename );
966 if( !file )
967 goto end;
968 }
969
970 r = MsiDatabaseExportW( handle, table, path, file );
971
972 end:
973 msi_free( table );
974 msi_free( path );
975 msi_free( file );
976
977 return r;
978 }
979
980 MSIDBSTATE WINAPI MsiGetDatabaseState( MSIHANDLE handle )
981 {
982 MSIDBSTATE ret = MSIDBSTATE_READ;
983 MSIDATABASE *db;
984
985 TRACE("%ld\n", handle);
986
987 db = msihandle2msiinfo( handle, MSIHANDLETYPE_DATABASE );
988 if( !db )
989 {
990 IWineMsiRemoteDatabase *remote_database;
991
992 remote_database = (IWineMsiRemoteDatabase *)msi_get_remote( handle );
993 if ( !remote_database )
994 return MSIDBSTATE_ERROR;
995
996 IWineMsiRemoteDatabase_Release( remote_database );
997 WARN("MsiGetDatabaseState not allowed during a custom action!\n");
998
999 return MSIDBSTATE_READ;
1000 }
1001
1002 if (db->mode != MSIDBOPEN_READONLY )
1003 ret = MSIDBSTATE_WRITE;
1004 msiobj_release( &db->hdr );
1005
1006 return ret;
1007 }
1008
1009 typedef struct _msi_remote_database_impl {
1010 const IWineMsiRemoteDatabaseVtbl *lpVtbl;
1011 MSIHANDLE database;
1012 LONG refs;
1013 } msi_remote_database_impl;
1014
1015 static inline msi_remote_database_impl* mrd_from_IWineMsiRemoteDatabase( IWineMsiRemoteDatabase* iface )
1016 {
1017 return (msi_remote_database_impl *)iface;
1018 }
1019
1020 static HRESULT WINAPI mrd_QueryInterface( IWineMsiRemoteDatabase *iface,
1021 REFIID riid,LPVOID *ppobj)
1022 {
1023 if( IsEqualCLSID( riid, &IID_IUnknown ) ||
1024 IsEqualCLSID( riid, &IID_IWineMsiRemoteDatabase ) )
1025 {
1026 IUnknown_AddRef( iface );
1027 *ppobj = iface;
1028 return S_OK;
1029 }
1030
1031 return E_NOINTERFACE;
1032 }
1033
1034 static ULONG WINAPI mrd_AddRef( IWineMsiRemoteDatabase *iface )
1035 {
1036 msi_remote_database_impl* This = mrd_from_IWineMsiRemoteDatabase( iface );
1037
1038 return InterlockedIncrement( &This->refs );
1039 }
1040
1041 static ULONG WINAPI mrd_Release( IWineMsiRemoteDatabase *iface )
1042 {
1043 msi_remote_database_impl* This = mrd_from_IWineMsiRemoteDatabase( iface );
1044 ULONG r;
1045
1046 r = InterlockedDecrement( &This->refs );
1047 if (r == 0)
1048 {
1049 MsiCloseHandle( This->database );
1050 msi_free( This );
1051 }
1052 return r;
1053 }
1054
1055 static HRESULT WINAPI mrd_IsTablePersistent( IWineMsiRemoteDatabase *iface,
1056 BSTR table, MSICONDITION *persistent )
1057 {
1058 msi_remote_database_impl *This = mrd_from_IWineMsiRemoteDatabase( iface );
1059 *persistent = MsiDatabaseIsTablePersistentW(This->database, (LPWSTR)table);
1060 return S_OK;
1061 }
1062
1063 static HRESULT WINAPI mrd_GetPrimaryKeys( IWineMsiRemoteDatabase *iface,
1064 BSTR table, MSIHANDLE *keys )
1065 {
1066 msi_remote_database_impl *This = mrd_from_IWineMsiRemoteDatabase( iface );
1067 UINT r = MsiDatabaseGetPrimaryKeysW(This->database, (LPWSTR)table, keys);
1068 return HRESULT_FROM_WIN32(r);
1069 }
1070
1071 static HRESULT WINAPI mrd_GetSummaryInformation( IWineMsiRemoteDatabase *iface,
1072 UINT updatecount, MSIHANDLE *suminfo )
1073 {
1074 msi_remote_database_impl *This = mrd_from_IWineMsiRemoteDatabase( iface );
1075 UINT r = MsiGetSummaryInformationW(This->database, NULL, updatecount, suminfo);
1076 return HRESULT_FROM_WIN32(r);
1077 }
1078
1079 static HRESULT WINAPI mrd_OpenView( IWineMsiRemoteDatabase *iface,
1080 BSTR query, MSIHANDLE *view )
1081 {
1082 msi_remote_database_impl *This = mrd_from_IWineMsiRemoteDatabase( iface );
1083 UINT r = MsiDatabaseOpenViewW(This->database, (LPWSTR)query, view);
1084 return HRESULT_FROM_WIN32(r);
1085 }
1086
1087 static HRESULT WINAPI mrd_SetMsiHandle( IWineMsiRemoteDatabase *iface, MSIHANDLE handle )
1088 {
1089 msi_remote_database_impl* This = mrd_from_IWineMsiRemoteDatabase( iface );
1090 This->database = handle;
1091 return S_OK;
1092 }
1093
1094 static const IWineMsiRemoteDatabaseVtbl msi_remote_database_vtbl =
1095 {
1096 mrd_QueryInterface,
1097 mrd_AddRef,
1098 mrd_Release,
1099 mrd_IsTablePersistent,
1100 mrd_GetPrimaryKeys,
1101 mrd_GetSummaryInformation,
1102 mrd_OpenView,
1103 mrd_SetMsiHandle,
1104 };
1105
1106 HRESULT create_msi_remote_database( IUnknown *pOuter, LPVOID *ppObj )
1107 {
1108 msi_remote_database_impl *This;
1109
1110 This = msi_alloc( sizeof *This );
1111 if (!This)
1112 return E_OUTOFMEMORY;
1113
1114 This->lpVtbl = &msi_remote_database_vtbl;
1115 This->database = 0;
1116 This->refs = 1;
1117
1118 *ppObj = This;
1119
1120 return S_OK;
1121 }