msi: Handle the special table _ForceCodepage in MsiDatabaseExport.
[wine/wine64.git] / dlls / msi / database.c
blob65db00b101b088b08a57f16e5d88cbb5d543c555
1 /*
2 * Implementation of the Microsoft Installer (msi.dll)
4 * Copyright 2002,2003,2004,2005 Mike McCormack for CodeWeavers
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
21 #include <stdarg.h>
23 #define COBJMACROS
24 #define NONAMELESSUNION
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"
39 #include "initguid.h"
41 WINE_DEFAULT_DEBUG_CHANNEL(msi);
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);
49 * .MSI file format
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.
58 static VOID MSI_CloseDatabase( MSIOBJECTHDR *arg )
60 MSIDATABASE *db = (MSIDATABASE *) arg;
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)
69 DeleteFileW( db->deletefile );
70 msi_free( db->deletefile );
74 UINT MSI_OpenDatabaseW(LPCWSTR szDBPath, LPCWSTR szPersist, MSIDATABASE **pdb)
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];
85 static const WCHAR backslash[] = {'\\',0};
86 static WCHAR szTables[] = { '_','T','a','b','l','e','s',0 };
88 TRACE("%s %s\n",debugstr_w(szDBPath),debugstr_w(szPersist) );
90 if( !pdb )
91 return ERROR_INVALID_PARAMETER;
93 save_path = szDBPath;
94 szMode = szPersist;
95 if( HIWORD( szPersist ) )
97 if (!CopyFileW( szDBPath, szPersist, FALSE ))
98 return ERROR_OPEN_FAILED;
100 szDBPath = szPersist;
101 szPersist = MSIDBOPEN_TRANSACT;
102 created = TRUE;
105 if( szPersist == MSIDBOPEN_READONLY )
107 r = StgOpenStorage( szDBPath, NULL,
108 STGM_DIRECT|STGM_READ|STGM_SHARE_DENY_WRITE, NULL, 0, &stg);
110 else if( szPersist == MSIDBOPEN_CREATE || szPersist == MSIDBOPEN_CREATEDIRECT )
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 )
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 );
124 created = TRUE;
126 else if( szPersist == MSIDBOPEN_TRANSACT )
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);
133 else if( szPersist == MSIDBOPEN_DIRECT )
135 r = StgOpenStorage( szDBPath, NULL,
136 STGM_DIRECT|STGM_READWRITE|STGM_SHARE_EXCLUSIVE, NULL, 0, &stg);
138 else
140 ERR("unknown flag %p\n",szPersist);
141 return ERROR_INVALID_PARAMETER;
144 if( FAILED( r ) || !stg )
146 FIXME("open failed r = %08x for %s\n", r, debugstr_w(szDBPath));
147 return ERROR_FUNCTION_FAILED;
150 r = IStorage_Stat( stg, &stat, STATFLAG_NONAME );
151 if( FAILED( r ) )
153 FIXME("Failed to stat storage\n");
154 goto end;
157 if ( !IsEqualGUID( &stat.clsid, &CLSID_MsiDatabase ) &&
158 !IsEqualGUID( &stat.clsid, &CLSID_MsiPatch ) )
160 ERR("storage GUID is not a MSI database GUID %s\n",
161 debugstr_guid(&stat.clsid) );
162 goto end;
165 db = alloc_msiobject( MSIHANDLETYPE_DATABASE, sizeof (MSIDATABASE),
166 MSI_CloseDatabase );
167 if( !db )
169 FIXME("Failed to allocate a handle\n");
170 goto end;
173 if (!strchrW( save_path, '\\' ))
175 GetCurrentDirectoryW( MAX_PATH, path );
176 lstrcatW( path, backslash );
177 lstrcatW( path, save_path );
179 else
180 lstrcpyW( path, save_path );
182 db->path = strdupW( path );
184 if( TRACE_ON( msi ) )
185 enum_stream_names( stg );
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 );
196 db->strings = msi_load_string_table( stg, &db->bytes_per_strref );
197 if( !db->strings )
198 goto end;
200 msi_table_set_strref( db->bytes_per_strref );
201 ret = ERROR_SUCCESS;
203 msiobj_addref( &db->hdr );
204 IStorage_AddRef( stg );
205 *pdb = db;
207 end:
208 if( db )
209 msiobj_release( &db->hdr );
210 if( stg )
211 IStorage_Release( stg );
213 return ret;
216 UINT WINAPI MsiOpenDatabaseW(LPCWSTR szDBPath, LPCWSTR szPersist, MSIHANDLE *phDB)
218 MSIDATABASE *db;
219 UINT ret;
221 TRACE("%s %s %p\n",debugstr_w(szDBPath),debugstr_w(szPersist), phDB);
223 ret = MSI_OpenDatabaseW( szDBPath, szPersist, &db );
224 if( ret == ERROR_SUCCESS )
226 *phDB = alloc_msihandle( &db->hdr );
227 if (! *phDB)
228 ret = ERROR_NOT_ENOUGH_MEMORY;
229 msiobj_release( &db->hdr );
232 return ret;
235 UINT WINAPI MsiOpenDatabaseA(LPCSTR szDBPath, LPCSTR szPersist, MSIHANDLE *phDB)
237 HRESULT r = ERROR_FUNCTION_FAILED;
238 LPWSTR szwDBPath = NULL, szwPersist = NULL;
240 TRACE("%s %s %p\n", debugstr_a(szDBPath), debugstr_a(szPersist), phDB);
242 if( szDBPath )
244 szwDBPath = strdupAtoW( szDBPath );
245 if( !szwDBPath )
246 goto end;
249 if( HIWORD(szPersist) )
251 szwPersist = strdupAtoW( szPersist );
252 if( !szwPersist )
253 goto end;
255 else
256 szwPersist = (LPWSTR)(DWORD_PTR)szPersist;
258 r = MsiOpenDatabaseW( szwDBPath, szwPersist, phDB );
260 end:
261 if( HIWORD(szPersist) )
262 msi_free( szwPersist );
263 msi_free( szwDBPath );
265 return r;
268 static LPWSTR msi_read_text_archive(LPCWSTR path)
270 HANDLE file;
271 LPSTR data = NULL;
272 LPWSTR wdata = NULL;
273 DWORD read, size = 0;
275 file = CreateFileW( path, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL );
276 if (file == INVALID_HANDLE_VALUE)
277 return NULL;
279 size = GetFileSize( file, NULL );
280 data = msi_alloc( size + 1 );
281 if (!data)
282 goto done;
284 if (!ReadFile( file, data, size, &read, NULL ))
285 goto done;
287 data[size] = '\0';
288 wdata = strdupAtoW( data );
290 done:
291 CloseHandle( file );
292 msi_free( data );
293 return wdata;
296 static void msi_parse_line(LPWSTR *line, LPWSTR **entries, DWORD *num_entries)
298 LPWSTR ptr = *line, save;
299 DWORD i, count = 1;
301 *entries = NULL;
303 /* stay on this line */
304 while (*ptr && *ptr != '\n')
306 /* entries are separated by tabs */
307 if (*ptr == '\t')
308 count++;
310 ptr++;
313 *entries = msi_alloc(count * sizeof(LPWSTR));
314 if (!*entries)
315 return;
317 /* store pointers into the data */
318 for (i = 0, ptr = *line; i < count; i++)
320 save = ptr;
322 while (*ptr && *ptr != '\t' && *ptr != '\n') ptr++;
324 /* NULL-separate the data */
325 if (*ptr)
326 *ptr++ = '\0';
328 (*entries)[i] = save;
331 /* move to the next line if there's more, else EOF */
332 *line = ptr;
334 if (num_entries)
335 *num_entries = count;
338 static LPWSTR msi_build_createsql_prelude(LPWSTR table)
340 LPWSTR prelude;
341 DWORD size;
343 static const WCHAR create_fmt[] = {'C','R','E','A','T','E',' ','T','A','B','L','E',' ','`','%','s','`',' ','(',' ',0};
345 size = sizeof(create_fmt) + lstrlenW(table) - 2;
346 prelude = msi_alloc(size * sizeof(WCHAR));
347 if (!prelude)
348 return NULL;
350 sprintfW(prelude, create_fmt, table);
351 return prelude;
354 static LPWSTR msi_build_createsql_columns(LPWSTR *columns_data, LPWSTR *types, DWORD num_columns)
356 LPWSTR columns, p;
357 LPCWSTR type;
358 DWORD sql_size = 1, i, len;
359 WCHAR expanded[128], *ptr;
360 WCHAR size[10], comma[2], extra[30];
362 static const WCHAR column_fmt[] = {'`','%','s','`',' ','%','s','%','s','%','s','%','s',' ',0};
363 static const WCHAR size_fmt[] = {'(','%','s',')',0};
364 static const WCHAR type_char[] = {'C','H','A','R',0};
365 static const WCHAR type_int[] = {'I','N','T',0};
366 static const WCHAR type_long[] = {'L','O','N','G',0};
367 static const WCHAR type_notnull[] = {' ','N','O','T',' ','N','U','L','L',0};
368 static const WCHAR localizable[] = {' ','L','O','C','A','L','I','Z','A','B','L','E',0};
370 columns = msi_alloc_zero(sql_size * sizeof(WCHAR));
371 if (!columns)
372 return NULL;
374 for (i = 0; i < num_columns; i++)
376 type = NULL;
377 comma[1] = size[0] = extra[0] = '\0';
379 if (i == num_columns - 1)
380 comma[0] = '\0';
381 else
382 comma[0] = ',';
384 ptr = &types[i][1];
385 len = atolW(ptr);
386 extra[0] = '\0';
388 switch (types[i][0])
390 case 'l':
391 lstrcpyW(extra, type_notnull);
392 case 'L':
393 lstrcatW(extra, localizable);
394 type = type_char;
395 sprintfW(size, size_fmt, ptr);
396 break;
397 case 's':
398 lstrcpyW(extra, type_notnull);
399 case 'S':
400 type = type_char;
401 sprintfW(size, size_fmt, ptr);
402 break;
403 case 'i':
404 lstrcpyW(extra, type_notnull);
405 case 'I':
406 if (len == 2)
407 type = type_int;
408 else
409 type = type_long;
410 break;
411 default:
412 ERR("Unknown type: %c\n", types[i][0]);
413 msi_free(columns);
414 return NULL;
417 sprintfW(expanded, column_fmt, columns_data[i], type, size, extra, comma);
418 sql_size += lstrlenW(expanded);
420 p = msi_realloc(columns, sql_size * sizeof(WCHAR));
421 if (!p)
423 msi_free(columns);
424 return NULL;
426 columns = p;
428 lstrcatW(columns, expanded);
431 return columns;
434 static LPWSTR msi_build_createsql_postlude(LPWSTR *primary_keys, DWORD num_keys)
436 LPWSTR postlude, keys, ptr;
437 DWORD size, key_size, i;
439 static const WCHAR key_fmt[] = {'`','%','s','`',',',' ',0};
440 static const WCHAR postlude_fmt[] = {'P','R','I','M','A','R','Y',' ','K','E','Y',' ','%','s',')',0};
442 for (i = 0, size = 1; i < num_keys; i++)
443 size += lstrlenW(key_fmt) + lstrlenW(primary_keys[i]) - 2;
445 keys = msi_alloc(size * sizeof(WCHAR));
446 if (!keys)
447 return NULL;
449 for (i = 0, ptr = keys; i < num_keys; i++)
451 key_size = lstrlenW(key_fmt) + lstrlenW(primary_keys[i]) -2;
452 sprintfW(ptr, key_fmt, primary_keys[i]);
453 ptr += key_size;
456 /* remove final ', ' */
457 *(ptr - 2) = '\0';
459 size = lstrlenW(postlude_fmt) + size - 1;
460 postlude = msi_alloc(size * sizeof(WCHAR));
461 if (!postlude)
462 goto done;
464 sprintfW(postlude, postlude_fmt, keys);
466 done:
467 msi_free(keys);
468 return postlude;
471 static UINT msi_add_table_to_db(MSIDATABASE *db, LPWSTR *columns, LPWSTR *types, LPWSTR *labels, DWORD num_labels, DWORD num_columns)
473 UINT r;
474 DWORD size;
475 MSIQUERY *view;
476 LPWSTR create_sql;
477 LPWSTR prelude, columns_sql, postlude;
479 prelude = msi_build_createsql_prelude(labels[0]);
480 columns_sql = msi_build_createsql_columns(columns, types, num_columns);
481 postlude = msi_build_createsql_postlude(labels + 1, num_labels - 1); /* skip over table name */
483 if (!prelude || !columns_sql || !postlude)
484 return ERROR_OUTOFMEMORY;
486 size = lstrlenW(prelude) + lstrlenW(columns_sql) + lstrlenW(postlude) + 1;
487 create_sql = msi_alloc(size * sizeof(WCHAR));
488 if (!create_sql)
489 return ERROR_OUTOFMEMORY;
491 lstrcpyW(create_sql, prelude);
492 lstrcatW(create_sql, columns_sql);
493 lstrcatW(create_sql, postlude);
495 msi_free(prelude);
496 msi_free(columns_sql);
497 msi_free(postlude);
499 r = MSI_DatabaseOpenViewW( db, create_sql, &view );
500 msi_free(create_sql);
502 if (r != ERROR_SUCCESS)
503 return r;
505 r = MSI_ViewExecute(view, NULL);
506 MSI_ViewClose(view);
507 msiobj_release(&view->hdr);
509 return r;
512 static LPWSTR msi_build_insertsql_prelude(LPWSTR table)
514 LPWSTR prelude;
515 DWORD size;
517 static const WCHAR insert_fmt[] = {'I','N','S','E','R','T',' ','I','N','T','O',' ','`','%','s','`',' ','(',' ',0};
519 size = sizeof(insert_fmt) + lstrlenW(table) - 2;
520 prelude = msi_alloc(size * sizeof(WCHAR));
521 if (!prelude)
522 return NULL;
524 sprintfW(prelude, insert_fmt, table);
525 return prelude;
528 static LPWSTR msi_build_insertsql_columns(LPWSTR *columns_data, LPWSTR *types, DWORD num_columns)
530 LPWSTR columns, p;
531 DWORD sql_size = 1, i;
532 WCHAR expanded[128];
534 static const WCHAR column_fmt[] = {'`','%','s','`',',',' ',0};
536 columns = msi_alloc_zero(sql_size * sizeof(WCHAR));
537 if (!columns)
538 return NULL;
540 for (i = 0; i < num_columns; i++)
542 sprintfW(expanded, column_fmt, columns_data[i]);
543 sql_size += lstrlenW(expanded);
545 if (i == num_columns - 1)
547 sql_size -= 2;
548 expanded[lstrlenW(expanded) - 2] = '\0';
551 p = msi_realloc(columns, sql_size * sizeof(WCHAR));
552 if (!p)
554 msi_free(columns);
555 return NULL;
557 columns = p;
559 lstrcatW(columns, expanded);
562 return columns;
565 static LPWSTR msi_build_insertsql_data(LPWSTR **records, LPWSTR *types, DWORD num_columns, DWORD irec)
567 LPWSTR columns, temp_columns;
568 DWORD sql_size = 1, i;
569 WCHAR expanded[128];
571 static const WCHAR str_fmt[] = {'\'','%','s','\'',',',' ',0};
572 static const WCHAR int_fmt[] = {'%','s',',',' ',0};
573 static const WCHAR empty[] = {'\'','\'',',',' ',0};
575 columns = msi_alloc_zero(sql_size * sizeof(WCHAR));
576 if (!columns)
577 return NULL;
579 for (i = 0; i < num_columns; i++)
581 switch (types[i][0])
583 case 'L': case 'l': case 'S': case 's':
584 sprintfW(expanded, str_fmt, records[irec][i]);
585 break;
586 case 'I': case 'i':
587 if (*records[0][i])
588 sprintfW(expanded, int_fmt, records[irec][i]);
589 else
590 lstrcpyW(expanded, empty);
591 break;
592 default:
593 HeapFree( GetProcessHeap(), 0, columns );
594 return NULL;
597 if (i == num_columns - 1)
598 expanded[lstrlenW(expanded) - 2] = '\0';
600 sql_size += lstrlenW(expanded);
601 temp_columns = msi_realloc(columns, sql_size * sizeof(WCHAR));
602 if (!temp_columns)
604 HeapFree( GetProcessHeap(), 0, columns);
605 return NULL;
607 columns = temp_columns;
609 lstrcatW(columns, expanded);
612 return columns;
615 static UINT msi_add_records_to_table(MSIDATABASE *db, LPWSTR *columns, LPWSTR *types,
616 LPWSTR *labels, LPWSTR **records,
617 int num_columns, int num_records)
619 MSIQUERY *view;
620 LPWSTR insert_sql;
621 DWORD size, i;
622 UINT r = ERROR_SUCCESS;
624 static const WCHAR mid[] = {' ',')',' ','V','A','L','U','E','S',' ','(',' ',0};
625 static const WCHAR end[] = {' ',')',0};
627 LPWSTR prelude = msi_build_insertsql_prelude(labels[0]);
628 LPWSTR columns_sql = msi_build_insertsql_columns(columns, types, num_columns);
630 for (i = 0; i < num_records; i++)
632 LPWSTR data = msi_build_insertsql_data(records, types, num_columns, i);
634 size = lstrlenW(prelude) + lstrlenW(columns_sql) + sizeof(mid) + lstrlenW(data) + sizeof(end) - 1;
635 insert_sql = msi_alloc(size * sizeof(WCHAR));
636 if (!insert_sql)
637 return ERROR_OUTOFMEMORY;
639 lstrcpyW(insert_sql, prelude);
640 lstrcatW(insert_sql, columns_sql);
641 lstrcatW(insert_sql, mid);
642 lstrcatW(insert_sql, data);
643 lstrcatW(insert_sql, end);
645 msi_free(data);
647 r = MSI_DatabaseOpenViewW( db, insert_sql, &view );
648 msi_free(insert_sql);
650 if (r != ERROR_SUCCESS)
651 goto done;
653 r = MSI_ViewExecute(view, NULL);
654 MSI_ViewClose(view);
655 msiobj_release(&view->hdr);
658 done:
659 msi_free(prelude);
660 msi_free(columns_sql);
662 return r;
665 UINT MSI_DatabaseImport(MSIDATABASE *db, LPCWSTR folder, LPCWSTR file)
667 UINT r;
668 DWORD len, i;
669 DWORD num_labels;
670 DWORD num_columns, num_records = 0;
671 LPWSTR *columns, *types, *labels;
672 LPWSTR path, ptr, data;
673 LPWSTR **records;
674 LPWSTR **temp_records;
676 static const WCHAR backslash[] = {'\\',0};
678 TRACE("%p %s %s\n", db, debugstr_w(folder), debugstr_w(file) );
680 if( folder == NULL || file == NULL )
681 return ERROR_INVALID_PARAMETER;
683 len = lstrlenW(folder) + lstrlenW(backslash) + lstrlenW(file) + 1;
684 path = msi_alloc( len * sizeof(WCHAR) );
685 if (!path)
686 return ERROR_OUTOFMEMORY;
688 lstrcpyW( path, folder );
689 lstrcatW( path, backslash );
690 lstrcatW( path, file );
692 data = msi_read_text_archive( path );
694 ptr = data;
695 msi_parse_line( &ptr, &columns, &num_columns );
696 msi_parse_line( &ptr, &types, NULL );
697 msi_parse_line( &ptr, &labels, &num_labels );
699 records = msi_alloc(sizeof(LPWSTR *));
700 if (!records)
702 r = ERROR_OUTOFMEMORY;
703 goto done;
706 /* read in the table records */
707 while (*ptr)
709 msi_parse_line( &ptr, &records[num_records], NULL );
711 num_records++;
712 temp_records = msi_realloc(records, (num_records + 1) * sizeof(LPWSTR *));
713 if (!temp_records)
715 r = ERROR_OUTOFMEMORY;
716 goto done;
718 records = temp_records;
721 r = msi_add_table_to_db( db, columns, types, labels, num_labels, num_columns );
722 if (r != ERROR_SUCCESS)
723 goto done;
725 r = msi_add_records_to_table( db, columns, types, labels, records, num_columns, num_records );
727 done:
728 msi_free(path);
729 msi_free(data);
730 msi_free(columns);
731 msi_free(types);
732 msi_free(labels);
734 for (i = 0; i < num_records; i++)
735 msi_free(records[i]);
737 msi_free(records);
739 return r;
742 UINT WINAPI MsiDatabaseImportW(MSIHANDLE handle, LPCWSTR szFolder, LPCWSTR szFilename)
744 MSIDATABASE *db;
745 UINT r;
747 TRACE("%lx %s %s\n",handle,debugstr_w(szFolder), debugstr_w(szFilename));
749 db = msihandle2msiinfo( handle, MSIHANDLETYPE_DATABASE );
750 if( !db )
752 IWineMsiRemoteDatabase *remote_database;
754 remote_database = (IWineMsiRemoteDatabase *)msi_get_remote( handle );
755 if ( !remote_database )
756 return ERROR_INVALID_HANDLE;
758 IWineMsiRemoteDatabase_Release( remote_database );
759 WARN("MsiDatabaseImport not allowed during a custom action!\n");
761 return ERROR_SUCCESS;
764 r = MSI_DatabaseImport( db, szFolder, szFilename );
765 msiobj_release( &db->hdr );
766 return r;
769 UINT WINAPI MsiDatabaseImportA( MSIHANDLE handle,
770 LPCSTR szFolder, LPCSTR szFilename )
772 LPWSTR path = NULL, file = NULL;
773 UINT r = ERROR_OUTOFMEMORY;
775 TRACE("%lx %s %s\n", handle, debugstr_a(szFolder), debugstr_a(szFilename));
777 if( szFolder )
779 path = strdupAtoW( szFolder );
780 if( !path )
781 goto end;
784 if( szFilename )
786 file = strdupAtoW( szFilename );
787 if( !file )
788 goto end;
791 r = MsiDatabaseImportW( handle, path, file );
793 end:
794 msi_free( path );
795 msi_free( file );
797 return r;
800 static UINT msi_export_record( HANDLE handle, MSIRECORD *row, UINT start )
802 UINT i, count, len, r = ERROR_SUCCESS;
803 const char *sep;
804 char *buffer;
805 DWORD sz;
807 len = 0x100;
808 buffer = msi_alloc( len );
809 if ( !buffer )
810 return ERROR_OUTOFMEMORY;
812 count = MSI_RecordGetFieldCount( row );
813 for ( i=start; i<=count; i++ )
815 sz = len;
816 r = MSI_RecordGetStringA( row, i, buffer, &sz );
817 if (r == ERROR_MORE_DATA)
819 char *p = msi_realloc( buffer, sz + 1 );
820 if (!p)
821 break;
822 len = sz + 1;
823 buffer = p;
825 sz = len;
826 r = MSI_RecordGetStringA( row, i, buffer, &sz );
827 if (r != ERROR_SUCCESS)
828 break;
830 if (!WriteFile( handle, buffer, sz, &sz, NULL ))
832 r = ERROR_FUNCTION_FAILED;
833 break;
836 sep = (i < count) ? "\t" : "\r\n";
837 if (!WriteFile( handle, sep, strlen(sep), &sz, NULL ))
839 r = ERROR_FUNCTION_FAILED;
840 break;
843 msi_free( buffer );
844 return r;
847 static UINT msi_export_row( MSIRECORD *row, void *arg )
849 return msi_export_record( arg, row, 1 );
852 static UINT msi_export_forcecodepage( HANDLE handle )
854 DWORD sz;
856 static const char data[] = "\r\n\r\n0\t_ForceCodepage\r\n";
858 FIXME("Read the codepage from the strings table!\n");
860 sz = lstrlenA(data) + 1;
861 if (!WriteFile(handle, data, sz, &sz, NULL))
862 return ERROR_FUNCTION_FAILED;
864 return ERROR_SUCCESS;
867 UINT MSI_DatabaseExport( MSIDATABASE *db, LPCWSTR table,
868 LPCWSTR folder, LPCWSTR file )
870 static const WCHAR query[] = {
871 's','e','l','e','c','t',' ','*',' ','f','r','o','m',' ','%','s',0 };
872 static const WCHAR szbs[] = { '\\', 0 };
873 static const WCHAR forcecodepage[] = {
874 '_','F','o','r','c','e','C','o','d','e','p','a','g','e',0 };
875 MSIRECORD *rec = NULL;
876 MSIQUERY *view = NULL;
877 LPWSTR filename;
878 HANDLE handle;
879 UINT len, r;
881 TRACE("%p %s %s %s\n", db, debugstr_w(table),
882 debugstr_w(folder), debugstr_w(file) );
884 if( folder == NULL || file == NULL )
885 return ERROR_INVALID_PARAMETER;
887 len = lstrlenW(folder) + lstrlenW(file) + 2;
888 filename = msi_alloc(len * sizeof (WCHAR));
889 if (!filename)
890 return ERROR_OUTOFMEMORY;
892 lstrcpyW( filename, folder );
893 lstrcatW( filename, szbs );
894 lstrcatW( filename, file );
896 handle = CreateFileW( filename, GENERIC_READ | GENERIC_WRITE, 0,
897 NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL );
898 msi_free( filename );
899 if (handle == INVALID_HANDLE_VALUE)
900 return ERROR_FUNCTION_FAILED;
902 if (!lstrcmpW( table, forcecodepage ))
904 r = msi_export_forcecodepage( handle );
905 goto done;
908 r = MSI_OpenQuery( db, &view, query, table );
909 if (r == ERROR_SUCCESS)
911 /* write out row 1, the column names */
912 r = MSI_ViewGetColumnInfo(view, MSICOLINFO_NAMES, &rec);
913 if (r == ERROR_SUCCESS)
915 msi_export_record( handle, rec, 1 );
916 msiobj_release( &rec->hdr );
919 /* write out row 2, the column types */
920 r = MSI_ViewGetColumnInfo(view, MSICOLINFO_TYPES, &rec);
921 if (r == ERROR_SUCCESS)
923 msi_export_record( handle, rec, 1 );
924 msiobj_release( &rec->hdr );
927 /* write out row 3, the table name + keys */
928 r = MSI_DatabaseGetPrimaryKeys( db, table, &rec );
929 if (r == ERROR_SUCCESS)
931 MSI_RecordSetStringW( rec, 0, table );
932 msi_export_record( handle, rec, 0 );
933 msiobj_release( &rec->hdr );
936 /* write out row 4 onwards, the data */
937 r = MSI_IterateRecords( view, 0, msi_export_row, handle );
938 msiobj_release( &view->hdr );
941 done:
942 CloseHandle( handle );
943 return r;
946 /***********************************************************************
947 * MsiExportDatabaseW [MSI.@]
949 * Writes a file containing the table data as tab separated ASCII.
951 * The format is as follows:
953 * row1 : colname1 <tab> colname2 <tab> .... colnameN <cr> <lf>
954 * row2 : coltype1 <tab> coltype2 <tab> .... coltypeN <cr> <lf>
955 * row3 : tablename <tab> key1 <tab> key2 <tab> ... keyM <cr> <lf>
957 * Followed by the data, starting at row 1 with one row per line
959 * row4 : data <tab> data <tab> data <tab> ... data <cr> <lf>
961 UINT WINAPI MsiDatabaseExportW( MSIHANDLE handle, LPCWSTR szTable,
962 LPCWSTR szFolder, LPCWSTR szFilename )
964 MSIDATABASE *db;
965 UINT r;
967 TRACE("%lx %s %s %s\n", handle, debugstr_w(szTable),
968 debugstr_w(szFolder), debugstr_w(szFilename));
970 db = msihandle2msiinfo( handle, MSIHANDLETYPE_DATABASE );
971 if( !db )
973 IWineMsiRemoteDatabase *remote_database;
975 remote_database = (IWineMsiRemoteDatabase *)msi_get_remote( handle );
976 if ( !remote_database )
977 return ERROR_INVALID_HANDLE;
979 IWineMsiRemoteDatabase_Release( remote_database );
980 WARN("MsiDatabaseExport not allowed during a custom action!\n");
982 return ERROR_SUCCESS;
985 r = MSI_DatabaseExport( db, szTable, szFolder, szFilename );
986 msiobj_release( &db->hdr );
987 return r;
990 UINT WINAPI MsiDatabaseExportA( MSIHANDLE handle, LPCSTR szTable,
991 LPCSTR szFolder, LPCSTR szFilename )
993 LPWSTR path = NULL, file = NULL, table = NULL;
994 UINT r = ERROR_OUTOFMEMORY;
996 TRACE("%lx %s %s %s\n", handle, debugstr_a(szTable),
997 debugstr_a(szFolder), debugstr_a(szFilename));
999 if( szTable )
1001 table = strdupAtoW( szTable );
1002 if( !table )
1003 goto end;
1006 if( szFolder )
1008 path = strdupAtoW( szFolder );
1009 if( !path )
1010 goto end;
1013 if( szFilename )
1015 file = strdupAtoW( szFilename );
1016 if( !file )
1017 goto end;
1020 r = MsiDatabaseExportW( handle, table, path, file );
1022 end:
1023 msi_free( table );
1024 msi_free( path );
1025 msi_free( file );
1027 return r;
1030 MSIDBSTATE WINAPI MsiGetDatabaseState( MSIHANDLE handle )
1032 MSIDBSTATE ret = MSIDBSTATE_READ;
1033 MSIDATABASE *db;
1035 TRACE("%ld\n", handle);
1037 db = msihandle2msiinfo( handle, MSIHANDLETYPE_DATABASE );
1038 if( !db )
1040 IWineMsiRemoteDatabase *remote_database;
1042 remote_database = (IWineMsiRemoteDatabase *)msi_get_remote( handle );
1043 if ( !remote_database )
1044 return MSIDBSTATE_ERROR;
1046 IWineMsiRemoteDatabase_Release( remote_database );
1047 WARN("MsiGetDatabaseState not allowed during a custom action!\n");
1049 return MSIDBSTATE_READ;
1052 if (db->mode != MSIDBOPEN_READONLY )
1053 ret = MSIDBSTATE_WRITE;
1054 msiobj_release( &db->hdr );
1056 return ret;
1059 typedef struct _msi_remote_database_impl {
1060 const IWineMsiRemoteDatabaseVtbl *lpVtbl;
1061 MSIHANDLE database;
1062 LONG refs;
1063 } msi_remote_database_impl;
1065 static inline msi_remote_database_impl* mrd_from_IWineMsiRemoteDatabase( IWineMsiRemoteDatabase* iface )
1067 return (msi_remote_database_impl *)iface;
1070 static HRESULT WINAPI mrd_QueryInterface( IWineMsiRemoteDatabase *iface,
1071 REFIID riid,LPVOID *ppobj)
1073 if( IsEqualCLSID( riid, &IID_IUnknown ) ||
1074 IsEqualCLSID( riid, &IID_IWineMsiRemoteDatabase ) )
1076 IUnknown_AddRef( iface );
1077 *ppobj = iface;
1078 return S_OK;
1081 return E_NOINTERFACE;
1084 static ULONG WINAPI mrd_AddRef( IWineMsiRemoteDatabase *iface )
1086 msi_remote_database_impl* This = mrd_from_IWineMsiRemoteDatabase( iface );
1088 return InterlockedIncrement( &This->refs );
1091 static ULONG WINAPI mrd_Release( IWineMsiRemoteDatabase *iface )
1093 msi_remote_database_impl* This = mrd_from_IWineMsiRemoteDatabase( iface );
1094 ULONG r;
1096 r = InterlockedDecrement( &This->refs );
1097 if (r == 0)
1099 MsiCloseHandle( This->database );
1100 msi_free( This );
1102 return r;
1105 HRESULT WINAPI mrd_IsTablePersistent( IWineMsiRemoteDatabase *iface,
1106 BSTR table, MSICONDITION *persistent )
1108 msi_remote_database_impl *This = mrd_from_IWineMsiRemoteDatabase( iface );
1109 *persistent = MsiDatabaseIsTablePersistentW(This->database, (LPWSTR)table);
1110 return S_OK;
1113 HRESULT WINAPI mrd_GetPrimaryKeys( IWineMsiRemoteDatabase *iface,
1114 BSTR table, MSIHANDLE *keys )
1116 msi_remote_database_impl *This = mrd_from_IWineMsiRemoteDatabase( iface );
1117 UINT r = MsiDatabaseGetPrimaryKeysW(This->database, (LPWSTR)table, keys);
1118 return HRESULT_FROM_WIN32(r);
1121 HRESULT WINAPI mrd_GetSummaryInformation( IWineMsiRemoteDatabase *iface,
1122 UINT updatecount, MSIHANDLE *suminfo )
1124 msi_remote_database_impl *This = mrd_from_IWineMsiRemoteDatabase( iface );
1125 UINT r = MsiGetSummaryInformationW(This->database, NULL, updatecount, suminfo);
1126 return HRESULT_FROM_WIN32(r);
1129 HRESULT WINAPI mrd_OpenView( IWineMsiRemoteDatabase *iface,
1130 BSTR query, MSIHANDLE *view )
1132 msi_remote_database_impl *This = mrd_from_IWineMsiRemoteDatabase( iface );
1133 UINT r = MsiDatabaseOpenViewW(This->database, (LPWSTR)query, view);
1134 return HRESULT_FROM_WIN32(r);
1137 static HRESULT WINAPI mrd_SetMsiHandle( IWineMsiRemoteDatabase *iface, MSIHANDLE handle )
1139 msi_remote_database_impl* This = mrd_from_IWineMsiRemoteDatabase( iface );
1140 This->database = handle;
1141 return S_OK;
1144 static const IWineMsiRemoteDatabaseVtbl msi_remote_database_vtbl =
1146 mrd_QueryInterface,
1147 mrd_AddRef,
1148 mrd_Release,
1149 mrd_IsTablePersistent,
1150 mrd_GetPrimaryKeys,
1151 mrd_GetSummaryInformation,
1152 mrd_OpenView,
1153 mrd_SetMsiHandle,
1156 HRESULT create_msi_remote_database( IUnknown *pOuter, LPVOID *ppObj )
1158 msi_remote_database_impl *This;
1160 This = msi_alloc( sizeof *This );
1161 if (!This)
1162 return E_OUTOFMEMORY;
1164 This->lpVtbl = &msi_remote_database_vtbl;
1165 This->database = 0;
1166 This->refs = 1;
1168 *ppObj = This;
1170 return S_OK;