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
24 #define NONAMELESSUNION
30 #include "wine/debug.h"
31 #include "wine/unicode.h"
40 WINE_DEFAULT_DEBUG_CHANNEL(msi);
42 DEFINE_GUID( CLSID_MsiDatabase, 0x000c1084, 0x0000, 0x0000,
43 0xc0,0x00,0x00,0x00,0x00,0x00,0x00,0x46);
44 DEFINE_GUID( CLSID_MsiPatch, 0x000c1086, 0x0000, 0x0000,
45 0xc0,0x00,0x00,0x00,0x00,0x00,0x00,0x46);
50 * An .msi file is a structured storage file.
51 * It contains a number of streams.
52 * A stream for each table in the database.
53 * Two streams for the string table in the database.
54 * Any binary data in a table is a reference to a stream.
57 static VOID MSI_CloseDatabase( MSIOBJECTHDR *arg )
59 MSIDATABASE *db = (MSIDATABASE *) arg;
62 free_cached_tables( db );
63 msi_free_transforms( db );
64 msi_destroy_stringtable( db->strings );
65 IStorage_Release( db->storage );
68 DeleteFileW( db->deletefile );
69 msi_free( db->deletefile );
73 UINT MSI_OpenDatabaseW(LPCWSTR szDBPath, LPCWSTR szPersist, MSIDATABASE **pdb)
77 MSIDATABASE *db = NULL;
78 UINT ret = ERROR_FUNCTION_FAILED;
79 LPCWSTR szMode, save_path;
84 static const WCHAR backslash[] = {'\\',0};
86 TRACE("%s %s\n",debugstr_w(szDBPath),debugstr_w(szPersist) );
89 return ERROR_INVALID_PARAMETER;
93 if( HIWORD( szPersist ) )
95 if (!CopyFileW( szDBPath, szPersist, FALSE ))
96 return ERROR_OPEN_FAILED;
99 szPersist = MSIDBOPEN_TRANSACT;
103 if( szPersist == MSIDBOPEN_READONLY )
105 r = StgOpenStorage( szDBPath, NULL,
106 STGM_DIRECT|STGM_READ|STGM_SHARE_DENY_WRITE, NULL, 0, &stg);
108 else if( szPersist == MSIDBOPEN_CREATE || szPersist == MSIDBOPEN_CREATEDIRECT )
110 /* FIXME: MSIDBOPEN_CREATE should case STGM_TRANSACTED flag to be
112 r = StgCreateDocfile( szDBPath,
113 STGM_CREATE|STGM_DIRECT|STGM_READWRITE|STGM_SHARE_EXCLUSIVE, 0, &stg);
114 if( r == ERROR_SUCCESS )
116 IStorage_SetClass( stg, &CLSID_MsiDatabase );
117 r = init_string_table( stg );
121 else if( szPersist == MSIDBOPEN_TRANSACT )
123 /* FIXME: MSIDBOPEN_TRANSACT should case STGM_TRANSACTED flag to be
125 r = StgOpenStorage( szDBPath, NULL,
126 STGM_DIRECT|STGM_READWRITE|STGM_SHARE_EXCLUSIVE, NULL, 0, &stg);
128 else if( szPersist == MSIDBOPEN_DIRECT )
130 r = StgOpenStorage( szDBPath, NULL,
131 STGM_DIRECT|STGM_READWRITE|STGM_SHARE_EXCLUSIVE, NULL, 0, &stg);
135 ERR("unknown flag %p\n",szPersist);
136 return ERROR_INVALID_PARAMETER;
139 if( FAILED( r ) || !stg )
141 FIXME("open failed r = %08x!\n",r);
142 return ERROR_FUNCTION_FAILED;
145 r = IStorage_Stat( stg, &stat, STATFLAG_NONAME );
148 FIXME("Failed to stat storage\n");
152 if ( !IsEqualGUID( &stat.clsid, &CLSID_MsiDatabase ) &&
153 !IsEqualGUID( &stat.clsid, &CLSID_MsiPatch ) )
155 ERR("storage GUID is not a MSI database GUID %s\n",
156 debugstr_guid(&stat.clsid) );
160 db = alloc_msiobject( MSIHANDLETYPE_DATABASE, sizeof (MSIDATABASE),
164 FIXME("Failed to allocate a handle\n");
168 if (!strchrW( save_path, '\\' ))
170 GetCurrentDirectoryW( MAX_PATH, path );
171 lstrcatW( path, backslash );
172 lstrcatW( path, save_path );
175 lstrcpyW( path, save_path );
177 db->path = strdupW( path );
179 if( TRACE_ON( msi ) )
180 enum_stream_names( stg );
185 db->deletefile = strdupW( szDBPath );
187 db->deletefile = NULL;
188 list_init( &db->tables );
189 list_init( &db->transforms );
191 db->strings = load_string_table( stg );
197 msiobj_addref( &db->hdr );
198 IStorage_AddRef( stg );
203 msiobj_release( &db->hdr );
205 IStorage_Release( stg );
210 UINT WINAPI MsiOpenDatabaseW(LPCWSTR szDBPath, LPCWSTR szPersist, MSIHANDLE *phDB)
215 TRACE("%s %s %p\n",debugstr_w(szDBPath),debugstr_w(szPersist), phDB);
217 ret = MSI_OpenDatabaseW( szDBPath, szPersist, &db );
218 if( ret == ERROR_SUCCESS )
220 *phDB = alloc_msihandle( &db->hdr );
222 ret = ERROR_NOT_ENOUGH_MEMORY;
223 msiobj_release( &db->hdr );
229 UINT WINAPI MsiOpenDatabaseA(LPCSTR szDBPath, LPCSTR szPersist, MSIHANDLE *phDB)
231 HRESULT r = ERROR_FUNCTION_FAILED;
232 LPWSTR szwDBPath = NULL, szwPersist = NULL;
234 TRACE("%s %s %p\n", debugstr_a(szDBPath), debugstr_a(szPersist), phDB);
238 szwDBPath = strdupAtoW( szDBPath );
243 if( HIWORD(szPersist) )
245 szwPersist = strdupAtoW( szPersist );
250 szwPersist = (LPWSTR)(DWORD_PTR)szPersist;
252 r = MsiOpenDatabaseW( szwDBPath, szwPersist, phDB );
255 if( HIWORD(szPersist) )
256 msi_free( szwPersist );
257 msi_free( szwDBPath );
262 static LPWSTR msi_read_text_archive(LPCWSTR path)
267 DWORD read, size = 0;
269 file = CreateFileW( path, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL );
270 if (file == INVALID_HANDLE_VALUE)
273 size = GetFileSize( file, NULL );
274 data = msi_alloc( size + 1 );
278 if (!ReadFile( file, data, size, &read, NULL ))
282 wdata = strdupAtoW( data );
290 static void msi_parse_line(LPWSTR *line, LPWSTR **entries, DWORD *num_entries)
292 LPWSTR ptr = *line, save;
297 /* stay on this line */
298 while (*ptr && *ptr != '\n')
300 /* entries are separated by tabs */
307 *entries = msi_alloc(count * sizeof(LPWSTR));
311 /* store pointers into the data */
312 for (i = 0, ptr = *line; i < count; i++)
316 while (*ptr && *ptr != '\t' && *ptr != '\n') ptr++;
318 /* NULL-separate the data */
322 (*entries)[i] = save;
325 /* move to the next line if there's more, else EOF */
329 *num_entries = count;
332 static LPWSTR msi_build_createsql_prelude(LPWSTR table)
337 static const WCHAR create_fmt[] = {'C','R','E','A','T','E',' ','T','A','B','L','E',' ','`','%','s','`',' ','(',' ',0};
339 size = sizeof(create_fmt) + lstrlenW(table) - 2;
340 prelude = msi_alloc(size * sizeof(WCHAR));
344 sprintfW(prelude, create_fmt, table);
348 static LPWSTR msi_build_createsql_columns(LPWSTR *columns_data, LPWSTR *types, DWORD num_columns)
352 DWORD sql_size = 1, i, len;
353 WCHAR expanded[128], *ptr;
354 WCHAR size[10], comma[2], extra[30];
356 static const WCHAR column_fmt[] = {'`','%','s','`',' ','%','s','%','s','%','s','%','s',' ',0};
357 static const WCHAR size_fmt[] = {'(','%','s',')',0};
358 static const WCHAR type_char[] = {'C','H','A','R',0};
359 static const WCHAR type_int[] = {'I','N','T',0};
360 static const WCHAR type_long[] = {'L','O','N','G',0};
361 static const WCHAR type_notnull[] = {' ','N','O','T',' ','N','U','L','L',0};
362 static const WCHAR localizable[] = {' ','L','O','C','A','L','I','Z','A','B','L','E',0};
364 columns = msi_alloc_zero(sql_size * sizeof(WCHAR));
368 for (i = 0; i < num_columns; i++)
371 comma[1] = size[0] = extra[0] = '\0';
373 if (i == num_columns - 1)
385 lstrcpyW(extra, type_notnull);
387 lstrcatW(extra, localizable);
389 sprintfW(size, size_fmt, ptr);
392 lstrcpyW(extra, type_notnull);
395 sprintfW(size, size_fmt, ptr);
398 lstrcpyW(extra, type_notnull);
407 sprintfW(expanded, column_fmt, columns_data[i], type, size, extra, comma);
408 sql_size += lstrlenW(expanded);
410 columns = msi_realloc(columns, sql_size * sizeof(WCHAR));
414 lstrcatW(columns, expanded);
420 static LPWSTR msi_build_createsql_postlude(LPWSTR *primary_keys, DWORD num_keys)
422 LPWSTR postlude, keys, ptr;
423 DWORD size, key_size, i;
425 static const WCHAR key_fmt[] = {'`','%','s','`',',',' ',0};
426 static const WCHAR postlude_fmt[] = {'P','R','I','M','A','R','Y',' ','K','E','Y',' ','%','s',')',' ','H','O','L','D',0};
428 for (i = 0, size = 1; i < num_keys; i++)
429 size += lstrlenW(key_fmt) + lstrlenW(primary_keys[i]) - 2;
431 keys = msi_alloc(size * sizeof(WCHAR));
435 for (i = 0, ptr = keys; i < num_keys; i++)
437 key_size = lstrlenW(key_fmt) + lstrlenW(primary_keys[i]) -2;
438 sprintfW(ptr, key_fmt, primary_keys[i]);
442 /* remove final ', ' */
445 size = lstrlenW(postlude_fmt) + size - 1;
446 postlude = msi_alloc(size * sizeof(WCHAR));
450 sprintfW(postlude, postlude_fmt, keys);
457 static UINT msi_add_table_to_db(MSIDATABASE *db, LPWSTR *columns, LPWSTR *types, LPWSTR *labels, DWORD num_labels, DWORD num_columns)
463 LPWSTR prelude, columns_sql, postlude;
465 prelude = msi_build_createsql_prelude(labels[0]);
466 columns_sql = msi_build_createsql_columns(columns, types, num_columns);
467 postlude = msi_build_createsql_postlude(labels + 1, num_labels - 1); /* skip over table name */
469 if (!prelude || !columns_sql || !postlude)
470 return ERROR_OUTOFMEMORY;
472 size = lstrlenW(prelude) + lstrlenW(columns_sql) + lstrlenW(postlude) + 1;
473 create_sql = msi_alloc(size * sizeof(WCHAR));
475 return ERROR_OUTOFMEMORY;
477 lstrcpyW(create_sql, prelude);
478 lstrcatW(create_sql, columns_sql);
479 lstrcatW(create_sql, postlude);
482 msi_free(columns_sql);
485 r = MSI_DatabaseOpenViewW( db, create_sql, &view );
486 msi_free(create_sql);
488 if (r != ERROR_SUCCESS)
491 r = MSI_ViewExecute(view, NULL);
493 msiobj_release(&view->hdr);
498 static LPWSTR msi_build_insertsql_prelude(LPWSTR table)
503 static const WCHAR insert_fmt[] = {'I','N','S','E','R','T',' ','I','N','T','O',' ','`','%','s','`',' ','(',' ',0};
505 size = sizeof(insert_fmt) + lstrlenW(table) - 2;
506 prelude = msi_alloc(size * sizeof(WCHAR));
510 sprintfW(prelude, insert_fmt, table);
514 static LPWSTR msi_build_insertsql_columns(LPWSTR *columns_data, LPWSTR *types, DWORD num_columns)
517 DWORD sql_size = 1, i;
520 static const WCHAR column_fmt[] = {'`','%','s','`',',',' ',0};
522 columns = msi_alloc_zero(sql_size * sizeof(WCHAR));
526 for (i = 0; i < num_columns; i++)
528 sprintfW(expanded, column_fmt, columns_data[i]);
529 sql_size += lstrlenW(expanded);
531 if (i == num_columns - 1)
534 expanded[lstrlenW(expanded) - 2] = '\0';
537 columns = msi_realloc(columns, sql_size * sizeof(WCHAR));
541 lstrcatW(columns, expanded);
547 static LPWSTR msi_build_insertsql_data(LPWSTR **records, LPWSTR *types, DWORD num_columns, DWORD irec)
550 DWORD sql_size = 1, i;
553 static const WCHAR str_fmt[] = {'\'','%','s','\'',',',' ',0};
554 static const WCHAR int_fmt[] = {'%','s',',',' ',0};
555 static const WCHAR empty[] = {'\'','\'',',',' ',0};
557 columns = msi_alloc_zero(sql_size * sizeof(WCHAR));
561 for (i = 0; i < num_columns; i++)
565 case 'L': case 'l': case 'S': case 's':
566 sprintfW(expanded, str_fmt, records[irec][i]);
570 sprintfW(expanded, int_fmt, records[irec][i]);
572 lstrcpyW(expanded, empty);
578 if (i == num_columns - 1)
579 expanded[lstrlenW(expanded) - 2] = '\0';
581 sql_size += lstrlenW(expanded);
582 columns = msi_realloc(columns, sql_size * sizeof(WCHAR));
586 lstrcatW(columns, expanded);
592 static UINT msi_add_records_to_table(MSIDATABASE *db, LPWSTR *columns, LPWSTR *types,
593 LPWSTR *labels, LPWSTR **records,
594 int num_columns, int num_records)
599 UINT r = ERROR_SUCCESS;
601 static const WCHAR mid[] = {' ',')',' ','V','A','L','U','E','S',' ','(',' ',0};
602 static const WCHAR end[] = {' ',')',0};
604 LPWSTR prelude = msi_build_insertsql_prelude(labels[0]);
605 LPWSTR columns_sql = msi_build_insertsql_columns(columns, types, num_columns);
607 for (i = 0; i < num_records; i++)
609 LPWSTR data = msi_build_insertsql_data(records, types, num_columns, i);
611 size = lstrlenW(prelude) + lstrlenW(columns_sql) + sizeof(mid) + lstrlenW(data) + sizeof(end) - 1;
612 insert_sql = msi_alloc(size * sizeof(WCHAR));
614 return ERROR_OUTOFMEMORY;
616 lstrcpyW(insert_sql, prelude);
617 lstrcatW(insert_sql, columns_sql);
618 lstrcatW(insert_sql, mid);
619 lstrcatW(insert_sql, data);
620 lstrcatW(insert_sql, end);
624 r = MSI_DatabaseOpenViewW( db, insert_sql, &view );
625 msi_free(insert_sql);
627 if (r != ERROR_SUCCESS)
630 r = MSI_ViewExecute(view, NULL);
632 msiobj_release(&view->hdr);
637 msi_free(columns_sql);
642 UINT MSI_DatabaseImport(MSIDATABASE *db, LPCWSTR folder, LPCWSTR file)
647 DWORD num_columns, num_records = 0;
648 LPWSTR *columns, *types, *labels;
649 LPWSTR path, ptr, data;
652 static const WCHAR backslash[] = {'\\',0};
654 TRACE("%p %s %s\n", db, debugstr_w(folder), debugstr_w(file) );
656 if( folder == NULL || file == NULL )
657 return ERROR_INVALID_PARAMETER;
659 len = lstrlenW(folder) + lstrlenW(backslash) + lstrlenW(file) + 1;
660 path = msi_alloc( len * sizeof(WCHAR) );
662 return ERROR_OUTOFMEMORY;
664 lstrcpyW( path, folder );
665 lstrcatW( path, backslash );
666 lstrcatW( path, file );
668 data = msi_read_text_archive( path );
671 msi_parse_line( &ptr, &columns, &num_columns );
672 msi_parse_line( &ptr, &types, NULL );
673 msi_parse_line( &ptr, &labels, &num_labels );
675 records = msi_alloc(sizeof(LPWSTR *));
677 return ERROR_OUTOFMEMORY;
679 /* read in the table records */
682 msi_parse_line( &ptr, &records[num_records], NULL );
685 records = msi_realloc(records, (num_records + 1) * sizeof(LPWSTR *));
687 return ERROR_OUTOFMEMORY;
690 r = msi_add_table_to_db( db, columns, types, labels, num_labels, num_columns );
691 if (r != ERROR_SUCCESS)
694 r = msi_add_records_to_table( db, columns, types, labels, records, num_columns, num_records );
703 for (i = 0; i < num_records; i++)
704 msi_free(records[i]);
711 UINT WINAPI MsiDatabaseImportW(MSIHANDLE handle, LPCWSTR szFolder, LPCWSTR szFilename)
716 TRACE("%lx %s %s\n",handle,debugstr_w(szFolder), debugstr_w(szFilename));
718 db = msihandle2msiinfo( handle, MSIHANDLETYPE_DATABASE );
720 return ERROR_INVALID_HANDLE;
721 r = MSI_DatabaseImport( db, szFolder, szFilename );
722 msiobj_release( &db->hdr );
726 UINT WINAPI MsiDatabaseImportA( MSIHANDLE handle,
727 LPCSTR szFolder, LPCSTR szFilename )
729 LPWSTR path = NULL, file = NULL;
730 UINT r = ERROR_OUTOFMEMORY;
732 TRACE("%lx %s %s\n", handle, debugstr_a(szFolder), debugstr_a(szFilename));
736 path = strdupAtoW( szFolder );
743 file = strdupAtoW( szFilename );
748 r = MsiDatabaseImportW( handle, path, file );
757 static UINT msi_export_record( HANDLE handle, MSIRECORD *row, UINT start )
759 UINT i, count, len, r = ERROR_SUCCESS;
765 buffer = msi_alloc( len );
767 return ERROR_OUTOFMEMORY;
769 count = MSI_RecordGetFieldCount( row );
770 for ( i=start; i<=count; i++ )
773 r = MSI_RecordGetStringA( row, i, buffer, &sz );
774 if (r == ERROR_MORE_DATA)
776 char *p = msi_realloc( buffer, sz + 1 );
783 r = MSI_RecordGetStringA( row, i, buffer, &sz );
784 if (r != ERROR_SUCCESS)
787 if (!WriteFile( handle, buffer, sz, &sz, NULL ))
789 r = ERROR_FUNCTION_FAILED;
793 sep = (i < count) ? "\t" : "\r\n";
794 if (!WriteFile( handle, sep, strlen(sep), &sz, NULL ))
796 r = ERROR_FUNCTION_FAILED;
804 static UINT msi_export_row( MSIRECORD *row, void *arg )
806 return msi_export_record( arg, row, 1 );
809 UINT MSI_DatabaseExport( MSIDATABASE *db, LPCWSTR table,
810 LPCWSTR folder, LPCWSTR file )
812 static const WCHAR query[] = {
813 's','e','l','e','c','t',' ','*',' ','f','r','o','m',' ','%','s',0 };
814 static const WCHAR szbs[] = { '\\', 0 };
815 MSIRECORD *rec = NULL;
816 MSIQUERY *view = NULL;
821 TRACE("%p %s %s %s\n", db, debugstr_w(table),
822 debugstr_w(folder), debugstr_w(file) );
824 if( folder == NULL || file == NULL )
825 return ERROR_INVALID_PARAMETER;
827 len = lstrlenW(folder) + lstrlenW(file) + 2;
828 filename = msi_alloc(len * sizeof (WCHAR));
830 return ERROR_OUTOFMEMORY;
832 lstrcpyW( filename, folder );
833 lstrcatW( filename, szbs );
834 lstrcatW( filename, file );
836 handle = CreateFileW( filename, GENERIC_READ | GENERIC_WRITE, 0,
837 NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL );
838 msi_free( filename );
839 if (handle == INVALID_HANDLE_VALUE)
840 return ERROR_FUNCTION_FAILED;
842 r = MSI_OpenQuery( db, &view, query, table );
843 if (r == ERROR_SUCCESS)
845 /* write out row 1, the column names */
846 r = MSI_ViewGetColumnInfo(view, MSICOLINFO_NAMES, &rec);
847 if (r == ERROR_SUCCESS)
849 msi_export_record( handle, rec, 1 );
850 msiobj_release( &rec->hdr );
853 /* write out row 2, the column types */
854 r = MSI_ViewGetColumnInfo(view, MSICOLINFO_TYPES, &rec);
855 if (r == ERROR_SUCCESS)
857 msi_export_record( handle, rec, 1 );
858 msiobj_release( &rec->hdr );
861 /* write out row 3, the table name + keys */
862 r = MSI_DatabaseGetPrimaryKeys( db, table, &rec );
863 if (r == ERROR_SUCCESS)
865 MSI_RecordSetStringW( rec, 0, table );
866 msi_export_record( handle, rec, 0 );
867 msiobj_release( &rec->hdr );
870 /* write out row 4 onwards, the data */
871 r = MSI_IterateRecords( view, 0, msi_export_row, handle );
872 msiobj_release( &view->hdr );
875 CloseHandle( handle );
880 /***********************************************************************
881 * MsiExportDatabaseW [MSI.@]
883 * Writes a file containing the table data as tab separated ASCII.
885 * The format is as follows:
887 * row1 : colname1 <tab> colname2 <tab> .... colnameN <cr> <lf>
888 * row2 : coltype1 <tab> coltype2 <tab> .... coltypeN <cr> <lf>
889 * row3 : tablename <tab> key1 <tab> key2 <tab> ... keyM <cr> <lf>
891 * Followed by the data, starting at row 1 with one row per line
893 * row4 : data <tab> data <tab> data <tab> ... data <cr> <lf>
895 UINT WINAPI MsiDatabaseExportW( MSIHANDLE handle, LPCWSTR szTable,
896 LPCWSTR szFolder, LPCWSTR szFilename )
901 TRACE("%lx %s %s %s\n", handle, debugstr_w(szTable),
902 debugstr_w(szFolder), debugstr_w(szFilename));
904 db = msihandle2msiinfo( handle, MSIHANDLETYPE_DATABASE );
906 return ERROR_INVALID_HANDLE;
907 r = MSI_DatabaseExport( db, szTable, szFolder, szFilename );
908 msiobj_release( &db->hdr );
912 UINT WINAPI MsiDatabaseExportA( MSIHANDLE handle, LPCSTR szTable,
913 LPCSTR szFolder, LPCSTR szFilename )
915 LPWSTR path = NULL, file = NULL, table = NULL;
916 UINT r = ERROR_OUTOFMEMORY;
918 TRACE("%lx %s %s %s\n", handle, debugstr_a(szTable),
919 debugstr_a(szFolder), debugstr_a(szFilename));
923 table = strdupAtoW( szTable );
930 path = strdupAtoW( szFolder );
937 file = strdupAtoW( szFilename );
942 r = MsiDatabaseExportW( handle, table, path, file );
952 MSIDBSTATE WINAPI MsiGetDatabaseState( MSIHANDLE handle )
954 MSIDBSTATE ret = MSIDBSTATE_READ;
957 TRACE("%ld\n", handle);
959 db = msihandle2msiinfo( handle, MSIHANDLETYPE_DATABASE );
961 return MSIDBSTATE_ERROR;
962 if (db->mode != MSIDBOPEN_READONLY )
963 ret = MSIDBSTATE_WRITE;
964 msiobj_release( &db->hdr );