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
25 WINE_DEFAULT_DEBUG_CHANNEL(msi
);
30 * An .msi file is a structured storage file.
31 * It contains a number of streams.
32 * A stream for each table in the database.
33 * Two streams for the string table in the database.
34 * Any binary data in a table is a reference to a stream.
37 #define IS_INTMSIDBOPEN(x) (((ULONG_PTR)(x) >> 16) == 0)
39 struct row_export_info
46 static void free_transforms( MSIDATABASE
*db
)
48 while( !list_empty( &db
->transforms
) )
50 MSITRANSFORM
*t
= LIST_ENTRY( list_head( &db
->transforms
), MSITRANSFORM
, entry
);
51 list_remove( &t
->entry
);
52 IStorage_Release( t
->stg
);
57 static void free_streams( MSIDATABASE
*db
)
60 for (i
= 0; i
< db
->num_streams
; i
++)
62 if (db
->streams
[i
].stream
) IStream_Release( db
->streams
[i
].stream
);
64 msi_free( db
->streams
);
67 void append_storage_to_db( MSIDATABASE
*db
, IStorage
*stg
)
71 t
= msi_alloc( sizeof *t
);
73 IStorage_AddRef( stg
);
74 list_add_head( &db
->transforms
, &t
->entry
);
77 static VOID
MSI_CloseDatabase( MSIOBJECTHDR
*arg
)
79 MSIDATABASE
*db
= (MSIDATABASE
*) arg
;
83 free_cached_tables( db
);
84 free_transforms( db
);
85 if (db
->strings
) msi_destroy_stringtable( db
->strings
);
86 IStorage_Release( db
->storage
);
89 DeleteFileW( db
->deletefile
);
90 msi_free( db
->deletefile
);
92 msi_free( db
->tempfolder
);
95 static HRESULT
db_initialize( IStorage
*stg
, const GUID
*clsid
)
97 static const WCHAR szTables
[] = { '_','T','a','b','l','e','s',0 };
100 hr
= IStorage_SetClass( stg
, clsid
);
103 WARN("failed to set class id 0x%08x\n", hr
);
107 /* create the _Tables stream */
108 hr
= write_stream_data( stg
, szTables
, NULL
, 0, TRUE
);
111 WARN("failed to create _Tables stream 0x%08x\n", hr
);
115 hr
= msi_init_string_table( stg
);
118 WARN("failed to initialize string table 0x%08x\n", hr
);
122 hr
= IStorage_Commit( stg
, 0 );
125 WARN("failed to commit changes 0x%08x\n", hr
);
132 UINT
MSI_OpenDatabaseW(LPCWSTR szDBPath
, LPCWSTR szPersist
, MSIDATABASE
**pdb
)
134 IStorage
*stg
= NULL
;
136 MSIDATABASE
*db
= NULL
;
137 UINT ret
= ERROR_FUNCTION_FAILED
;
138 LPCWSTR szMode
, save_path
;
140 BOOL created
= FALSE
, patch
= FALSE
;
141 WCHAR path
[MAX_PATH
];
143 TRACE("%s %s\n",debugstr_w(szDBPath
),debugstr_w(szPersist
) );
146 return ERROR_INVALID_PARAMETER
;
148 if (szPersist
- MSIDBOPEN_PATCHFILE
<= MSIDBOPEN_CREATEDIRECT
)
150 TRACE("Database is a patch\n");
151 szPersist
-= MSIDBOPEN_PATCHFILE
;
155 save_path
= szDBPath
;
157 if( !IS_INTMSIDBOPEN(szPersist
) )
159 if (!CopyFileW( szDBPath
, szPersist
, FALSE
))
160 return ERROR_OPEN_FAILED
;
162 szDBPath
= szPersist
;
163 szPersist
= MSIDBOPEN_TRANSACT
;
167 if( szPersist
== MSIDBOPEN_READONLY
)
169 r
= StgOpenStorage( szDBPath
, NULL
,
170 STGM_DIRECT
|STGM_READ
|STGM_SHARE_DENY_WRITE
, NULL
, 0, &stg
);
172 else if( szPersist
== MSIDBOPEN_CREATE
)
174 r
= StgCreateDocfile( szDBPath
,
175 STGM_CREATE
|STGM_TRANSACTED
|STGM_READWRITE
|STGM_SHARE_EXCLUSIVE
, 0, &stg
);
178 r
= db_initialize( stg
, patch
? &CLSID_MsiPatch
: &CLSID_MsiDatabase
);
181 else if( szPersist
== MSIDBOPEN_CREATEDIRECT
)
183 r
= StgCreateDocfile( szDBPath
,
184 STGM_CREATE
|STGM_DIRECT
|STGM_READWRITE
|STGM_SHARE_EXCLUSIVE
, 0, &stg
);
187 r
= db_initialize( stg
, patch
? &CLSID_MsiPatch
: &CLSID_MsiDatabase
);
190 else if( szPersist
== MSIDBOPEN_TRANSACT
)
192 r
= StgOpenStorage( szDBPath
, NULL
,
193 STGM_TRANSACTED
|STGM_READWRITE
|STGM_SHARE_DENY_WRITE
, NULL
, 0, &stg
);
195 else if( szPersist
== MSIDBOPEN_DIRECT
)
197 r
= StgOpenStorage( szDBPath
, NULL
,
198 STGM_DIRECT
|STGM_READWRITE
|STGM_SHARE_EXCLUSIVE
, NULL
, 0, &stg
);
202 ERR("unknown flag %p\n",szPersist
);
203 return ERROR_INVALID_PARAMETER
;
206 if( FAILED( r
) || !stg
)
208 WARN("open failed r = %08x for %s\n", r
, debugstr_w(szDBPath
));
209 return ERROR_FUNCTION_FAILED
;
212 r
= IStorage_Stat( stg
, &stat
, STATFLAG_NONAME
);
215 FIXME("Failed to stat storage\n");
219 if ( !IsEqualGUID( &stat
.clsid
, &CLSID_MsiDatabase
) &&
220 !IsEqualGUID( &stat
.clsid
, &CLSID_MsiPatch
) &&
221 !IsEqualGUID( &stat
.clsid
, &CLSID_MsiTransform
) )
223 ERR("storage GUID is not a MSI database GUID %s\n",
224 debugstr_guid(&stat
.clsid
) );
228 if ( patch
&& !IsEqualGUID( &stat
.clsid
, &CLSID_MsiPatch
) )
230 ERR("storage GUID is not the MSI patch GUID %s\n",
231 debugstr_guid(&stat
.clsid
) );
232 ret
= ERROR_OPEN_FAILED
;
236 db
= alloc_msiobject( MSIHANDLETYPE_DATABASE
, sizeof (MSIDATABASE
),
240 FIXME("Failed to allocate a handle\n");
244 if (!strchrW( save_path
, '\\' ))
246 GetCurrentDirectoryW( MAX_PATH
, path
);
247 lstrcatW( path
, szBackSlash
);
248 lstrcatW( path
, save_path
);
251 lstrcpyW( path
, save_path
);
253 db
->path
= strdupW( path
);
254 db
->media_transform_offset
= MSI_INITIAL_MEDIA_TRANSFORM_OFFSET
;
255 db
->media_transform_disk_id
= MSI_INITIAL_MEDIA_TRANSFORM_DISKID
;
257 if( TRACE_ON( msi
) )
258 enum_stream_names( stg
);
263 db
->deletefile
= strdupW( szDBPath
);
264 list_init( &db
->tables
);
265 list_init( &db
->transforms
);
267 db
->strings
= msi_load_string_table( stg
, &db
->bytes_per_strref
);
273 msiobj_addref( &db
->hdr
);
274 IStorage_AddRef( stg
);
279 msiobj_release( &db
->hdr
);
281 IStorage_Release( stg
);
286 UINT WINAPI
MsiOpenDatabaseW(LPCWSTR szDBPath
, LPCWSTR szPersist
, MSIHANDLE
*phDB
)
291 TRACE("%s %s %p\n",debugstr_w(szDBPath
),debugstr_w(szPersist
), phDB
);
293 ret
= MSI_OpenDatabaseW( szDBPath
, szPersist
, &db
);
294 if( ret
== ERROR_SUCCESS
)
296 *phDB
= alloc_msihandle( &db
->hdr
);
298 ret
= ERROR_NOT_ENOUGH_MEMORY
;
299 msiobj_release( &db
->hdr
);
305 UINT WINAPI
MsiOpenDatabaseA(LPCSTR szDBPath
, LPCSTR szPersist
, MSIHANDLE
*phDB
)
307 HRESULT r
= ERROR_FUNCTION_FAILED
;
308 LPWSTR szwDBPath
= NULL
, szwPersist
= NULL
;
310 TRACE("%s %s %p\n", debugstr_a(szDBPath
), debugstr_a(szPersist
), phDB
);
314 szwDBPath
= strdupAtoW( szDBPath
);
319 if( !IS_INTMSIDBOPEN(szPersist
) )
321 szwPersist
= strdupAtoW( szPersist
);
326 szwPersist
= (LPWSTR
)(DWORD_PTR
)szPersist
;
328 r
= MsiOpenDatabaseW( szwDBPath
, szwPersist
, phDB
);
331 if( !IS_INTMSIDBOPEN(szPersist
) )
332 msi_free( szwPersist
);
333 msi_free( szwDBPath
);
338 static LPWSTR
msi_read_text_archive(LPCWSTR path
, DWORD
*len
)
343 DWORD read
, size
= 0;
345 file
= CreateFileW( path
, GENERIC_READ
, FILE_SHARE_READ
, NULL
, OPEN_EXISTING
, 0, NULL
);
346 if (file
== INVALID_HANDLE_VALUE
)
349 size
= GetFileSize( file
, NULL
);
350 if (!(data
= msi_alloc( size
))) goto done
;
352 if (!ReadFile( file
, data
, size
, &read
, NULL
) || read
!= size
) goto done
;
354 while (!data
[size
- 1]) size
--;
355 *len
= MultiByteToWideChar( CP_ACP
, 0, data
, size
, NULL
, 0 );
356 if ((wdata
= msi_alloc( (*len
+ 1) * sizeof(WCHAR
) )))
358 MultiByteToWideChar( CP_ACP
, 0, data
, size
, wdata
, *len
);
368 static void msi_parse_line(LPWSTR
*line
, LPWSTR
**entries
, DWORD
*num_entries
, DWORD
*len
)
370 LPWSTR ptr
= *line
, save
;
371 DWORD i
, count
= 1, chars_left
= *len
;
375 /* stay on this line */
376 while (chars_left
&& *ptr
!= '\n')
378 /* entries are separated by tabs */
386 *entries
= msi_alloc(count
* sizeof(LPWSTR
));
390 /* store pointers into the data */
392 for (i
= 0, ptr
= *line
; i
< count
; i
++)
394 while (chars_left
&& *ptr
== '\r')
401 while (chars_left
&& *ptr
!= '\t' && *ptr
!= '\n' && *ptr
!= '\r')
403 if (!*ptr
) *ptr
= '\n'; /* convert embedded nulls to \n */
404 if (ptr
> *line
&& *ptr
== '\x19' && *(ptr
- 1) == '\x11')
413 /* NULL-separate the data */
414 if (*ptr
== '\n' || *ptr
== '\r')
416 while (chars_left
&& (*ptr
== '\n' || *ptr
== '\r'))
427 (*entries
)[i
] = save
;
430 /* move to the next line if there's more, else EOF */
434 *num_entries
= count
;
437 static LPWSTR
msi_build_createsql_prelude(LPWSTR table
)
442 static const WCHAR create_fmt
[] = {'C','R','E','A','T','E',' ','T','A','B','L','E',' ','`','%','s','`',' ','(',' ',0};
444 size
= sizeof(create_fmt
)/sizeof(create_fmt
[0]) + lstrlenW(table
) - 2;
445 prelude
= msi_alloc(size
* sizeof(WCHAR
));
449 sprintfW(prelude
, create_fmt
, table
);
453 static LPWSTR
msi_build_createsql_columns(LPWSTR
*columns_data
, LPWSTR
*types
, DWORD num_columns
)
457 DWORD sql_size
= 1, i
, len
;
458 WCHAR expanded
[128], *ptr
;
459 WCHAR size
[10], comma
[2], extra
[30];
461 static const WCHAR column_fmt
[] = {'`','%','s','`',' ','%','s','%','s','%','s','%','s',' ',0};
462 static const WCHAR size_fmt
[] = {'(','%','s',')',0};
463 static const WCHAR type_char
[] = {'C','H','A','R',0};
464 static const WCHAR type_int
[] = {'I','N','T',0};
465 static const WCHAR type_long
[] = {'L','O','N','G',0};
466 static const WCHAR type_object
[] = {'O','B','J','E','C','T',0};
467 static const WCHAR type_notnull
[] = {' ','N','O','T',' ','N','U','L','L',0};
468 static const WCHAR localizable
[] = {' ','L','O','C','A','L','I','Z','A','B','L','E',0};
470 columns
= msi_alloc_zero(sql_size
* sizeof(WCHAR
));
474 for (i
= 0; i
< num_columns
; i
++)
477 comma
[1] = size
[0] = extra
[0] = '\0';
479 if (i
== num_columns
- 1)
491 lstrcpyW(extra
, type_notnull
);
494 lstrcatW(extra
, localizable
);
496 sprintfW(size
, size_fmt
, ptr
);
499 lstrcpyW(extra
, type_notnull
);
503 sprintfW(size
, size_fmt
, ptr
);
506 lstrcpyW(extra
, type_notnull
);
515 WARN("invalid int width %u\n", len
);
521 lstrcpyW(extra
, type_notnull
);
527 ERR("Unknown type: %c\n", types
[i
][0]);
532 sprintfW(expanded
, column_fmt
, columns_data
[i
], type
, size
, extra
, comma
);
533 sql_size
+= lstrlenW(expanded
);
535 p
= msi_realloc(columns
, sql_size
* sizeof(WCHAR
));
543 lstrcatW(columns
, expanded
);
549 static LPWSTR
msi_build_createsql_postlude(LPWSTR
*primary_keys
, DWORD num_keys
)
551 LPWSTR postlude
, keys
, ptr
;
554 static const WCHAR key_fmt
[] = {'`','%','s','`',',',' ',0};
555 static const WCHAR postlude_fmt
[] = {'P','R','I','M','A','R','Y',' ','K','E','Y',' ','%','s',')',0};
557 for (i
= 0, size
= 1; i
< num_keys
; i
++)
558 size
+= lstrlenW(key_fmt
) + lstrlenW(primary_keys
[i
]) - 2;
560 keys
= msi_alloc(size
* sizeof(WCHAR
));
564 for (i
= 0, ptr
= keys
; i
< num_keys
; i
++)
566 ptr
+= sprintfW(ptr
, key_fmt
, primary_keys
[i
]);
569 /* remove final ', ' */
572 size
= lstrlenW(postlude_fmt
) + size
- 1;
573 postlude
= msi_alloc(size
* sizeof(WCHAR
));
577 sprintfW(postlude
, postlude_fmt
, keys
);
584 static UINT
msi_add_table_to_db(MSIDATABASE
*db
, LPWSTR
*columns
, LPWSTR
*types
, LPWSTR
*labels
, DWORD num_labels
, DWORD num_columns
)
586 UINT r
= ERROR_OUTOFMEMORY
;
589 LPWSTR create_sql
= NULL
;
590 LPWSTR prelude
, columns_sql
, postlude
;
592 prelude
= msi_build_createsql_prelude(labels
[0]);
593 columns_sql
= msi_build_createsql_columns(columns
, types
, num_columns
);
594 postlude
= msi_build_createsql_postlude(labels
+ 1, num_labels
- 1); /* skip over table name */
596 if (!prelude
|| !columns_sql
|| !postlude
)
599 size
= lstrlenW(prelude
) + lstrlenW(columns_sql
) + lstrlenW(postlude
) + 1;
600 create_sql
= msi_alloc(size
* sizeof(WCHAR
));
604 lstrcpyW(create_sql
, prelude
);
605 lstrcatW(create_sql
, columns_sql
);
606 lstrcatW(create_sql
, postlude
);
608 r
= MSI_DatabaseOpenViewW( db
, create_sql
, &view
);
609 if (r
!= ERROR_SUCCESS
)
612 r
= MSI_ViewExecute(view
, NULL
);
614 msiobj_release(&view
->hdr
);
618 msi_free(columns_sql
);
620 msi_free(create_sql
);
624 static LPWSTR
msi_import_stream_filename(LPCWSTR path
, LPCWSTR name
)
627 LPWSTR fullname
, ptr
;
629 len
= lstrlenW(path
) + lstrlenW(name
) + 1;
630 fullname
= msi_alloc(len
*sizeof(WCHAR
));
634 lstrcpyW( fullname
, path
);
636 /* chop off extension from path */
637 ptr
= strrchrW(fullname
, '.');
644 lstrcpyW( ptr
, name
);
648 static UINT
construct_record(DWORD num_columns
, LPWSTR
*types
,
649 LPWSTR
*data
, LPWSTR path
, MSIRECORD
**rec
)
653 *rec
= MSI_CreateRecord(num_columns
);
655 return ERROR_OUTOFMEMORY
;
657 for (i
= 0; i
< num_columns
; i
++)
661 case 'L': case 'l': case 'S': case 's':
662 MSI_RecordSetStringW(*rec
, i
+ 1, data
[i
]);
666 MSI_RecordSetInteger(*rec
, i
+ 1, atoiW(data
[i
]));
672 LPWSTR file
= msi_import_stream_filename(path
, data
[i
]);
674 return ERROR_FUNCTION_FAILED
;
676 r
= MSI_RecordSetStreamFromFileW(*rec
, i
+ 1, file
);
678 if (r
!= ERROR_SUCCESS
)
679 return ERROR_FUNCTION_FAILED
;
683 ERR("Unhandled column type: %c\n", types
[i
][0]);
684 msiobj_release(&(*rec
)->hdr
);
685 return ERROR_FUNCTION_FAILED
;
689 return ERROR_SUCCESS
;
692 static UINT
msi_add_records_to_table(MSIDATABASE
*db
, LPWSTR
*columns
, LPWSTR
*types
,
693 LPWSTR
*labels
, LPWSTR
**records
,
694 int num_columns
, int num_records
,
702 static const WCHAR select
[] = {
703 'S','E','L','E','C','T',' ','*',' ',
704 'F','R','O','M',' ','`','%','s','`',0
707 r
= MSI_OpenQuery(db
, &view
, select
, labels
[0]);
708 if (r
!= ERROR_SUCCESS
)
711 while (MSI_ViewFetch(view
, &rec
) != ERROR_NO_MORE_ITEMS
)
713 r
= MSI_ViewModify(view
, MSIMODIFY_DELETE
, rec
);
714 msiobj_release(&rec
->hdr
);
715 if (r
!= ERROR_SUCCESS
)
719 for (i
= 0; i
< num_records
; i
++)
721 r
= construct_record(num_columns
, types
, records
[i
], path
, &rec
);
722 if (r
!= ERROR_SUCCESS
)
725 r
= MSI_ViewModify(view
, MSIMODIFY_INSERT
, rec
);
726 if (r
!= ERROR_SUCCESS
)
728 msiobj_release(&rec
->hdr
);
732 msiobj_release(&rec
->hdr
);
736 msiobj_release(&view
->hdr
);
740 static UINT
MSI_DatabaseImport(MSIDATABASE
*db
, LPCWSTR folder
, LPCWSTR file
)
744 DWORD num_labels
, num_types
;
745 DWORD num_columns
, num_records
= 0;
746 LPWSTR
*columns
, *types
, *labels
;
747 LPWSTR path
, ptr
, data
;
748 LPWSTR
**records
= NULL
;
749 LPWSTR
**temp_records
;
751 static const WCHAR suminfo
[] =
752 {'_','S','u','m','m','a','r','y','I','n','f','o','r','m','a','t','i','o','n',0};
753 static const WCHAR forcecodepage
[] =
754 {'_','F','o','r','c','e','C','o','d','e','p','a','g','e',0};
756 TRACE("%p %s %s\n", db
, debugstr_w(folder
), debugstr_w(file
) );
758 if( folder
== NULL
|| file
== NULL
)
759 return ERROR_INVALID_PARAMETER
;
761 len
= lstrlenW(folder
) + lstrlenW(szBackSlash
) + lstrlenW(file
) + 1;
762 path
= msi_alloc( len
* sizeof(WCHAR
) );
764 return ERROR_OUTOFMEMORY
;
766 lstrcpyW( path
, folder
);
767 lstrcatW( path
, szBackSlash
);
768 lstrcatW( path
, file
);
770 data
= msi_read_text_archive( path
, &len
);
774 return ERROR_FUNCTION_FAILED
;
778 msi_parse_line( &ptr
, &columns
, &num_columns
, &len
);
779 msi_parse_line( &ptr
, &types
, &num_types
, &len
);
780 msi_parse_line( &ptr
, &labels
, &num_labels
, &len
);
782 if (num_columns
== 1 && !columns
[0][0] && num_labels
== 1 && !labels
[0][0] &&
783 num_types
== 2 && !strcmpW( types
[1], forcecodepage
))
785 r
= msi_set_string_table_codepage( db
->strings
, atoiW( types
[0] ) );
789 if (num_columns
!= num_types
)
791 r
= ERROR_FUNCTION_FAILED
;
795 records
= msi_alloc(sizeof(LPWSTR
*));
798 r
= ERROR_OUTOFMEMORY
;
802 /* read in the table records */
805 msi_parse_line( &ptr
, &records
[num_records
], NULL
, &len
);
808 temp_records
= msi_realloc(records
, (num_records
+ 1) * sizeof(LPWSTR
*));
811 r
= ERROR_OUTOFMEMORY
;
814 records
= temp_records
;
817 if (!strcmpW(labels
[0], suminfo
))
819 r
= msi_add_suminfo( db
, records
, num_records
, num_columns
);
820 if (r
!= ERROR_SUCCESS
)
822 r
= ERROR_FUNCTION_FAILED
;
828 if (!TABLE_Exists(db
, labels
[0]))
830 r
= msi_add_table_to_db( db
, columns
, types
, labels
, num_labels
, num_columns
);
831 if (r
!= ERROR_SUCCESS
)
833 r
= ERROR_FUNCTION_FAILED
;
838 r
= msi_add_records_to_table( db
, columns
, types
, labels
, records
, num_columns
, num_records
, path
);
848 for (i
= 0; i
< num_records
; i
++)
849 msi_free(records
[i
]);
856 UINT WINAPI
MsiDatabaseImportW(MSIHANDLE handle
, LPCWSTR szFolder
, LPCWSTR szFilename
)
861 TRACE("%x %s %s\n",handle
,debugstr_w(szFolder
), debugstr_w(szFilename
));
863 db
= msihandle2msiinfo( handle
, MSIHANDLETYPE_DATABASE
);
866 IWineMsiRemoteDatabase
*remote_database
;
868 remote_database
= (IWineMsiRemoteDatabase
*)msi_get_remote( handle
);
869 if ( !remote_database
)
870 return ERROR_INVALID_HANDLE
;
872 IWineMsiRemoteDatabase_Release( remote_database
);
873 WARN("MsiDatabaseImport not allowed during a custom action!\n");
875 return ERROR_SUCCESS
;
878 r
= MSI_DatabaseImport( db
, szFolder
, szFilename
);
879 msiobj_release( &db
->hdr
);
883 UINT WINAPI
MsiDatabaseImportA( MSIHANDLE handle
,
884 LPCSTR szFolder
, LPCSTR szFilename
)
886 LPWSTR path
= NULL
, file
= NULL
;
887 UINT r
= ERROR_OUTOFMEMORY
;
889 TRACE("%x %s %s\n", handle
, debugstr_a(szFolder
), debugstr_a(szFilename
));
893 path
= strdupAtoW( szFolder
);
900 file
= strdupAtoW( szFilename
);
905 r
= MsiDatabaseImportW( handle
, path
, file
);
914 static UINT
msi_export_field( HANDLE handle
, MSIRECORD
*row
, UINT field
)
922 buffer
= msi_alloc( sz
);
924 return ERROR_OUTOFMEMORY
;
926 r
= MSI_RecordGetStringA( row
, field
, buffer
, &sz
);
927 if (r
== ERROR_MORE_DATA
)
931 sz
++; /* leave room for NULL terminator */
932 p
= msi_realloc( buffer
, sz
);
936 return ERROR_OUTOFMEMORY
;
940 r
= MSI_RecordGetStringA( row
, field
, buffer
, &sz
);
941 if (r
!= ERROR_SUCCESS
)
947 else if (r
!= ERROR_SUCCESS
)
950 bret
= WriteFile( handle
, buffer
, sz
, &sz
, NULL
);
953 return ERROR_FUNCTION_FAILED
;
958 static UINT
msi_export_stream( LPCWSTR folder
, LPCWSTR table
, MSIRECORD
*row
, UINT field
,
961 static const WCHAR fmt_file
[] = { '%','s','/','%','s','/','%','s',0 };
962 static const WCHAR fmt_folder
[] = { '%','s','/','%','s',0 };
963 WCHAR stream_name
[256], stream_filename
[MAX_PATH
];
964 DWORD sz
, read_size
, write_size
;
969 /* get the name of the file */
970 sz
= sizeof(stream_name
)/sizeof(WCHAR
);
971 r
= MSI_RecordGetStringW( row
, start
, stream_name
, &sz
);
972 if (r
!= ERROR_SUCCESS
)
975 /* if the destination folder does not exist then create it (folder name = table name) */
976 snprintfW( stream_filename
, sizeof(stream_filename
)/sizeof(WCHAR
), fmt_folder
, folder
, table
);
977 if (GetFileAttributesW( stream_filename
) == INVALID_FILE_ATTRIBUTES
)
979 if (!CreateDirectoryW( stream_filename
, NULL
))
980 return ERROR_PATH_NOT_FOUND
;
983 /* actually create the file */
984 snprintfW( stream_filename
, sizeof(stream_filename
)/sizeof(WCHAR
), fmt_file
, folder
, table
, stream_name
);
985 file
= CreateFileW( stream_filename
, GENERIC_WRITE
, FILE_SHARE_READ
| FILE_SHARE_WRITE
,
986 NULL
, CREATE_ALWAYS
, FILE_ATTRIBUTE_NORMAL
, NULL
);
987 if (file
== INVALID_HANDLE_VALUE
)
988 return ERROR_FILE_NOT_FOUND
;
990 /* copy the stream to the file */
991 read_size
= sizeof(buffer
);
992 while (read_size
== sizeof(buffer
))
994 r
= MSI_RecordReadStream( row
, field
, buffer
, &read_size
);
995 if (r
!= ERROR_SUCCESS
)
1000 if (!WriteFile( file
, buffer
, read_size
, &write_size
, NULL
) || read_size
!= write_size
)
1002 CloseHandle( file
);
1003 return ERROR_WRITE_FAULT
;
1006 CloseHandle( file
);
1010 static UINT
msi_export_record( struct row_export_info
*row_export_info
, MSIRECORD
*row
, UINT start
)
1012 HANDLE handle
= row_export_info
->handle
;
1013 UINT i
, count
, r
= ERROR_SUCCESS
;
1017 count
= MSI_RecordGetFieldCount( row
);
1018 for (i
= start
; i
<= count
; i
++)
1020 r
= msi_export_field( handle
, row
, i
);
1021 if (r
== ERROR_INVALID_PARAMETER
)
1023 r
= msi_export_stream( row_export_info
->folder
, row_export_info
->table
, row
, i
, start
);
1024 if (r
!= ERROR_SUCCESS
)
1027 /* exporting a binary stream, repeat the "Name" field */
1028 r
= msi_export_field( handle
, row
, start
);
1029 if (r
!= ERROR_SUCCESS
)
1032 else if (r
!= ERROR_SUCCESS
)
1035 sep
= (i
< count
) ? "\t" : "\r\n";
1036 if (!WriteFile( handle
, sep
, strlen(sep
), &sz
, NULL
))
1037 return ERROR_FUNCTION_FAILED
;
1042 static UINT
msi_export_row( MSIRECORD
*row
, void *arg
)
1044 return msi_export_record( arg
, row
, 1 );
1047 static UINT
msi_export_forcecodepage( HANDLE handle
, UINT codepage
)
1049 static const char fmt
[] = "\r\n\r\n%u\t_ForceCodepage\r\n";
1050 char data
[sizeof(fmt
) + 10];
1053 sprintf( data
, fmt
, codepage
);
1055 sz
= lstrlenA(data
) + 1;
1056 if (!WriteFile(handle
, data
, sz
, &sz
, NULL
))
1057 return ERROR_FUNCTION_FAILED
;
1059 return ERROR_SUCCESS
;
1062 static UINT
msi_export_summaryinformation( MSIDATABASE
*db
, HANDLE handle
)
1064 static const char header
[] = "PropertyId\tValue\r\n"
1066 "_SummaryInformation\tPropertyId\r\n";
1069 sz
= lstrlenA(header
);
1070 if (!WriteFile(handle
, header
, sz
, &sz
, NULL
))
1071 return ERROR_WRITE_FAULT
;
1073 return msi_export_suminfo( db
, handle
);
1076 static UINT
MSI_DatabaseExport( MSIDATABASE
*db
, LPCWSTR table
,
1077 LPCWSTR folder
, LPCWSTR file
)
1079 static const WCHAR summaryinformation
[] = {
1080 '_','S','u','m','m','a','r','y','I','n','f','o','r','m','a','t','i','o','n',0 };
1081 static const WCHAR query
[] = {
1082 's','e','l','e','c','t',' ','*',' ','f','r','o','m',' ','%','s',0 };
1083 static const WCHAR forcecodepage
[] = {
1084 '_','F','o','r','c','e','C','o','d','e','p','a','g','e',0 };
1085 MSIRECORD
*rec
= NULL
;
1086 MSIQUERY
*view
= NULL
;
1091 TRACE("%p %s %s %s\n", db
, debugstr_w(table
),
1092 debugstr_w(folder
), debugstr_w(file
) );
1094 if( folder
== NULL
|| file
== NULL
)
1095 return ERROR_INVALID_PARAMETER
;
1097 len
= lstrlenW(folder
) + lstrlenW(file
) + 2;
1098 filename
= msi_alloc(len
* sizeof (WCHAR
));
1100 return ERROR_OUTOFMEMORY
;
1102 lstrcpyW( filename
, folder
);
1103 lstrcatW( filename
, szBackSlash
);
1104 lstrcatW( filename
, file
);
1106 handle
= CreateFileW( filename
, GENERIC_READ
| GENERIC_WRITE
, 0,
1107 NULL
, CREATE_ALWAYS
, FILE_ATTRIBUTE_NORMAL
, NULL
);
1108 msi_free( filename
);
1109 if (handle
== INVALID_HANDLE_VALUE
)
1110 return ERROR_FUNCTION_FAILED
;
1112 if (!strcmpW( table
, forcecodepage
))
1114 UINT codepage
= msi_get_string_table_codepage( db
->strings
);
1115 r
= msi_export_forcecodepage( handle
, codepage
);
1119 if (!strcmpW( table
, summaryinformation
))
1121 r
= msi_export_summaryinformation( db
, handle
);
1125 r
= MSI_OpenQuery( db
, &view
, query
, table
);
1126 if (r
== ERROR_SUCCESS
)
1128 struct row_export_info row_export_info
= { handle
, folder
, table
};
1130 /* write out row 1, the column names */
1131 r
= MSI_ViewGetColumnInfo(view
, MSICOLINFO_NAMES
, &rec
);
1132 if (r
== ERROR_SUCCESS
)
1134 msi_export_record( &row_export_info
, rec
, 1 );
1135 msiobj_release( &rec
->hdr
);
1138 /* write out row 2, the column types */
1139 r
= MSI_ViewGetColumnInfo(view
, MSICOLINFO_TYPES
, &rec
);
1140 if (r
== ERROR_SUCCESS
)
1142 msi_export_record( &row_export_info
, rec
, 1 );
1143 msiobj_release( &rec
->hdr
);
1146 /* write out row 3, the table name + keys */
1147 r
= MSI_DatabaseGetPrimaryKeys( db
, table
, &rec
);
1148 if (r
== ERROR_SUCCESS
)
1150 MSI_RecordSetStringW( rec
, 0, table
);
1151 msi_export_record( &row_export_info
, rec
, 0 );
1152 msiobj_release( &rec
->hdr
);
1155 /* write out row 4 onwards, the data */
1156 r
= MSI_IterateRecords( view
, 0, msi_export_row
, &row_export_info
);
1157 msiobj_release( &view
->hdr
);
1161 CloseHandle( handle
);
1165 /***********************************************************************
1166 * MsiExportDatabaseW [MSI.@]
1168 * Writes a file containing the table data as tab separated ASCII.
1170 * The format is as follows:
1172 * row1 : colname1 <tab> colname2 <tab> .... colnameN <cr> <lf>
1173 * row2 : coltype1 <tab> coltype2 <tab> .... coltypeN <cr> <lf>
1174 * row3 : tablename <tab> key1 <tab> key2 <tab> ... keyM <cr> <lf>
1176 * Followed by the data, starting at row 1 with one row per line
1178 * row4 : data <tab> data <tab> data <tab> ... data <cr> <lf>
1180 UINT WINAPI
MsiDatabaseExportW( MSIHANDLE handle
, LPCWSTR szTable
,
1181 LPCWSTR szFolder
, LPCWSTR szFilename
)
1186 TRACE("%x %s %s %s\n", handle
, debugstr_w(szTable
),
1187 debugstr_w(szFolder
), debugstr_w(szFilename
));
1189 db
= msihandle2msiinfo( handle
, MSIHANDLETYPE_DATABASE
);
1192 IWineMsiRemoteDatabase
*remote_database
;
1194 remote_database
= (IWineMsiRemoteDatabase
*)msi_get_remote( handle
);
1195 if ( !remote_database
)
1196 return ERROR_INVALID_HANDLE
;
1198 IWineMsiRemoteDatabase_Release( remote_database
);
1199 WARN("MsiDatabaseExport not allowed during a custom action!\n");
1201 return ERROR_SUCCESS
;
1204 r
= MSI_DatabaseExport( db
, szTable
, szFolder
, szFilename
);
1205 msiobj_release( &db
->hdr
);
1209 UINT WINAPI
MsiDatabaseExportA( MSIHANDLE handle
, LPCSTR szTable
,
1210 LPCSTR szFolder
, LPCSTR szFilename
)
1212 LPWSTR path
= NULL
, file
= NULL
, table
= NULL
;
1213 UINT r
= ERROR_OUTOFMEMORY
;
1215 TRACE("%x %s %s %s\n", handle
, debugstr_a(szTable
),
1216 debugstr_a(szFolder
), debugstr_a(szFilename
));
1220 table
= strdupAtoW( szTable
);
1227 path
= strdupAtoW( szFolder
);
1234 file
= strdupAtoW( szFilename
);
1239 r
= MsiDatabaseExportW( handle
, table
, path
, file
);
1249 UINT WINAPI
MsiDatabaseMergeA(MSIHANDLE hDatabase
, MSIHANDLE hDatabaseMerge
,
1255 TRACE("(%d, %d, %s)\n", hDatabase
, hDatabaseMerge
,
1256 debugstr_a(szTableName
));
1258 table
= strdupAtoW(szTableName
);
1259 r
= MsiDatabaseMergeW(hDatabase
, hDatabaseMerge
, table
);
1265 typedef struct _tagMERGETABLE
1279 typedef struct _tagMERGEROW
1285 typedef struct _tagMERGEDATA
1289 MERGETABLE
*curtable
;
1291 struct list
*tabledata
;
1294 static BOOL
merge_type_match(LPCWSTR type1
, LPCWSTR type2
)
1296 if (((type1
[0] == 'l') || (type1
[0] == 's')) &&
1297 ((type2
[0] == 'l') || (type2
[0] == 's')))
1300 if (((type1
[0] == 'L') || (type1
[0] == 'S')) &&
1301 ((type2
[0] == 'L') || (type2
[0] == 'S')))
1304 return !strcmpW( type1
, type2
);
1307 static UINT
merge_verify_colnames(MSIQUERY
*dbview
, MSIQUERY
*mergeview
)
1309 MSIRECORD
*dbrec
, *mergerec
;
1312 r
= MSI_ViewGetColumnInfo(dbview
, MSICOLINFO_NAMES
, &dbrec
);
1313 if (r
!= ERROR_SUCCESS
)
1316 r
= MSI_ViewGetColumnInfo(mergeview
, MSICOLINFO_NAMES
, &mergerec
);
1317 if (r
!= ERROR_SUCCESS
)
1319 msiobj_release(&dbrec
->hdr
);
1323 count
= MSI_RecordGetFieldCount(dbrec
);
1324 for (i
= 1; i
<= count
; i
++)
1326 if (!MSI_RecordGetString(mergerec
, i
))
1329 if (strcmpW( MSI_RecordGetString( dbrec
, i
), MSI_RecordGetString( mergerec
, i
) ))
1331 r
= ERROR_DATATYPE_MISMATCH
;
1336 msiobj_release(&dbrec
->hdr
);
1337 msiobj_release(&mergerec
->hdr
);
1338 dbrec
= mergerec
= NULL
;
1340 r
= MSI_ViewGetColumnInfo(dbview
, MSICOLINFO_TYPES
, &dbrec
);
1341 if (r
!= ERROR_SUCCESS
)
1344 r
= MSI_ViewGetColumnInfo(mergeview
, MSICOLINFO_TYPES
, &mergerec
);
1345 if (r
!= ERROR_SUCCESS
)
1347 msiobj_release(&dbrec
->hdr
);
1351 count
= MSI_RecordGetFieldCount(dbrec
);
1352 for (i
= 1; i
<= count
; i
++)
1354 if (!MSI_RecordGetString(mergerec
, i
))
1357 if (!merge_type_match(MSI_RecordGetString(dbrec
, i
),
1358 MSI_RecordGetString(mergerec
, i
)))
1360 r
= ERROR_DATATYPE_MISMATCH
;
1366 msiobj_release(&dbrec
->hdr
);
1367 msiobj_release(&mergerec
->hdr
);
1372 static UINT
merge_verify_primary_keys(MSIDATABASE
*db
, MSIDATABASE
*mergedb
,
1375 MSIRECORD
*dbrec
, *mergerec
= NULL
;
1378 r
= MSI_DatabaseGetPrimaryKeys(db
, table
, &dbrec
);
1379 if (r
!= ERROR_SUCCESS
)
1382 r
= MSI_DatabaseGetPrimaryKeys(mergedb
, table
, &mergerec
);
1383 if (r
!= ERROR_SUCCESS
)
1386 count
= MSI_RecordGetFieldCount(dbrec
);
1387 if (count
!= MSI_RecordGetFieldCount(mergerec
))
1389 r
= ERROR_DATATYPE_MISMATCH
;
1393 for (i
= 1; i
<= count
; i
++)
1395 if (strcmpW( MSI_RecordGetString( dbrec
, i
), MSI_RecordGetString( mergerec
, i
) ))
1397 r
= ERROR_DATATYPE_MISMATCH
;
1403 msiobj_release(&dbrec
->hdr
);
1404 msiobj_release(&mergerec
->hdr
);
1409 static LPWSTR
get_key_value(MSIQUERY
*view
, LPCWSTR key
, MSIRECORD
*rec
)
1411 MSIRECORD
*colnames
;
1413 UINT r
, i
= 0, sz
= 0;
1416 r
= MSI_ViewGetColumnInfo(view
, MSICOLINFO_NAMES
, &colnames
);
1417 if (r
!= ERROR_SUCCESS
)
1422 str
= msi_dup_record_field(colnames
, ++i
);
1423 cmp
= strcmpW( key
, str
);
1427 msiobj_release(&colnames
->hdr
);
1429 r
= MSI_RecordGetStringW(rec
, i
, NULL
, &sz
);
1430 if (r
!= ERROR_SUCCESS
)
1434 if (MSI_RecordGetString(rec
, i
)) /* check record field is a string */
1436 /* quote string record fields */
1437 const WCHAR szQuote
[] = {'\'', 0};
1439 val
= msi_alloc(sz
*sizeof(WCHAR
));
1443 lstrcpyW(val
, szQuote
);
1444 r
= MSI_RecordGetStringW(rec
, i
, val
+1, &sz
);
1445 lstrcpyW(val
+1+sz
, szQuote
);
1449 /* do not quote integer record fields */
1450 val
= msi_alloc(sz
*sizeof(WCHAR
));
1454 r
= MSI_RecordGetStringW(rec
, i
, val
, &sz
);
1457 if (r
!= ERROR_SUCCESS
)
1459 ERR("failed to get string!\n");
1467 static LPWSTR
create_diff_row_query(MSIDATABASE
*merge
, MSIQUERY
*view
,
1468 LPWSTR table
, MSIRECORD
*rec
)
1470 LPWSTR query
= NULL
, clause
= NULL
, val
;
1471 LPCWSTR setptr
, key
;
1472 DWORD size
, oldsize
;
1476 static const WCHAR keyset
[] = {
1477 '`','%','s','`',' ','=',' ','%','s',' ','A','N','D',' ',0};
1478 static const WCHAR lastkeyset
[] = {
1479 '`','%','s','`',' ','=',' ','%','s',' ',0};
1480 static const WCHAR fmt
[] = {'S','E','L','E','C','T',' ','*',' ',
1481 'F','R','O','M',' ','`','%','s','`',' ',
1482 'W','H','E','R','E',' ','%','s',0};
1484 r
= MSI_DatabaseGetPrimaryKeys(merge
, table
, &keys
);
1485 if (r
!= ERROR_SUCCESS
)
1488 clause
= msi_alloc_zero(sizeof(WCHAR
));
1493 count
= MSI_RecordGetFieldCount(keys
);
1494 for (i
= 1; i
<= count
; i
++)
1496 key
= MSI_RecordGetString(keys
, i
);
1497 val
= get_key_value(view
, key
, rec
);
1500 setptr
= lastkeyset
;
1505 size
+= lstrlenW(setptr
) + lstrlenW(key
) + lstrlenW(val
) - 4;
1506 clause
= msi_realloc(clause
, size
* sizeof (WCHAR
));
1513 sprintfW(clause
+ oldsize
- 1, setptr
, key
, val
);
1517 size
= lstrlenW(fmt
) + lstrlenW(table
) + lstrlenW(clause
) + 1;
1518 query
= msi_alloc(size
* sizeof(WCHAR
));
1522 sprintfW(query
, fmt
, table
, clause
);
1526 msiobj_release(&keys
->hdr
);
1530 static UINT
merge_diff_row(MSIRECORD
*rec
, LPVOID param
)
1532 MERGEDATA
*data
= param
;
1533 MERGETABLE
*table
= data
->curtable
;
1535 MSIQUERY
*dbview
= NULL
;
1536 MSIRECORD
*row
= NULL
;
1537 LPWSTR query
= NULL
;
1538 UINT r
= ERROR_SUCCESS
;
1540 if (TABLE_Exists(data
->db
, table
->name
))
1542 query
= create_diff_row_query(data
->merge
, data
->curview
, table
->name
, rec
);
1544 return ERROR_OUTOFMEMORY
;
1546 r
= MSI_DatabaseOpenViewW(data
->db
, query
, &dbview
);
1547 if (r
!= ERROR_SUCCESS
)
1550 r
= MSI_ViewExecute(dbview
, NULL
);
1551 if (r
!= ERROR_SUCCESS
)
1554 r
= MSI_ViewFetch(dbview
, &row
);
1555 if (r
== ERROR_SUCCESS
&& !MSI_RecordsAreEqual(rec
, row
))
1557 table
->numconflicts
++;
1560 else if (r
!= ERROR_NO_MORE_ITEMS
)
1566 mergerow
= msi_alloc(sizeof(MERGEROW
));
1569 r
= ERROR_OUTOFMEMORY
;
1573 mergerow
->data
= MSI_CloneRecord(rec
);
1574 if (!mergerow
->data
)
1576 r
= ERROR_OUTOFMEMORY
;
1581 list_add_tail(&table
->rows
, &mergerow
->entry
);
1585 msiobj_release(&row
->hdr
);
1586 msiobj_release(&dbview
->hdr
);
1590 static UINT
msi_get_table_labels(MSIDATABASE
*db
, LPCWSTR table
, LPWSTR
**labels
, DWORD
*numlabels
)
1593 MSIRECORD
*prec
= NULL
;
1595 r
= MSI_DatabaseGetPrimaryKeys(db
, table
, &prec
);
1596 if (r
!= ERROR_SUCCESS
)
1599 count
= MSI_RecordGetFieldCount(prec
);
1600 *numlabels
= count
+ 1;
1601 *labels
= msi_alloc((*numlabels
)*sizeof(LPWSTR
));
1604 r
= ERROR_OUTOFMEMORY
;
1608 (*labels
)[0] = strdupW(table
);
1609 for (i
=1; i
<=count
; i
++ )
1611 (*labels
)[i
] = strdupW(MSI_RecordGetString(prec
, i
));
1615 msiobj_release( &prec
->hdr
);
1619 static UINT
msi_get_query_columns(MSIQUERY
*query
, LPWSTR
**columns
, DWORD
*numcolumns
)
1622 MSIRECORD
*prec
= NULL
;
1624 r
= MSI_ViewGetColumnInfo(query
, MSICOLINFO_NAMES
, &prec
);
1625 if (r
!= ERROR_SUCCESS
)
1628 count
= MSI_RecordGetFieldCount(prec
);
1629 *columns
= msi_alloc(count
*sizeof(LPWSTR
));
1632 r
= ERROR_OUTOFMEMORY
;
1636 for (i
=1; i
<=count
; i
++ )
1638 (*columns
)[i
-1] = strdupW(MSI_RecordGetString(prec
, i
));
1641 *numcolumns
= count
;
1644 msiobj_release( &prec
->hdr
);
1648 static UINT
msi_get_query_types(MSIQUERY
*query
, LPWSTR
**types
, DWORD
*numtypes
)
1651 MSIRECORD
*prec
= NULL
;
1653 r
= MSI_ViewGetColumnInfo(query
, MSICOLINFO_TYPES
, &prec
);
1654 if (r
!= ERROR_SUCCESS
)
1657 count
= MSI_RecordGetFieldCount(prec
);
1658 *types
= msi_alloc(count
*sizeof(LPWSTR
));
1661 r
= ERROR_OUTOFMEMORY
;
1666 for (i
=1; i
<=count
; i
++ )
1668 (*types
)[i
-1] = strdupW(MSI_RecordGetString(prec
, i
));
1672 msiobj_release( &prec
->hdr
);
1676 static void merge_free_rows(MERGETABLE
*table
)
1678 struct list
*item
, *cursor
;
1680 LIST_FOR_EACH_SAFE(item
, cursor
, &table
->rows
)
1682 MERGEROW
*row
= LIST_ENTRY(item
, MERGEROW
, entry
);
1684 list_remove(&row
->entry
);
1685 msiobj_release(&row
->data
->hdr
);
1690 static void free_merge_table(MERGETABLE
*table
)
1694 if (table
->labels
!= NULL
)
1696 for (i
= 0; i
< table
->numlabels
; i
++)
1697 msi_free(table
->labels
[i
]);
1699 msi_free(table
->labels
);
1702 if (table
->columns
!= NULL
)
1704 for (i
= 0; i
< table
->numcolumns
; i
++)
1705 msi_free(table
->columns
[i
]);
1707 msi_free(table
->columns
);
1710 if (table
->types
!= NULL
)
1712 for (i
= 0; i
< table
->numtypes
; i
++)
1713 msi_free(table
->types
[i
]);
1715 msi_free(table
->types
);
1718 msi_free(table
->name
);
1719 merge_free_rows(table
);
1724 static UINT
msi_get_merge_table (MSIDATABASE
*db
, LPCWSTR name
, MERGETABLE
**ptable
)
1728 MSIQUERY
*mergeview
= NULL
;
1730 static const WCHAR query
[] = {'S','E','L','E','C','T',' ','*',' ',
1731 'F','R','O','M',' ','`','%','s','`',0};
1733 table
= msi_alloc_zero(sizeof(MERGETABLE
));
1737 return ERROR_OUTOFMEMORY
;
1740 r
= msi_get_table_labels(db
, name
, &table
->labels
, &table
->numlabels
);
1741 if (r
!= ERROR_SUCCESS
)
1744 r
= MSI_OpenQuery(db
, &mergeview
, query
, name
);
1745 if (r
!= ERROR_SUCCESS
)
1748 r
= msi_get_query_columns(mergeview
, &table
->columns
, &table
->numcolumns
);
1749 if (r
!= ERROR_SUCCESS
)
1752 r
= msi_get_query_types(mergeview
, &table
->types
, &table
->numtypes
);
1753 if (r
!= ERROR_SUCCESS
)
1756 list_init(&table
->rows
);
1758 table
->name
= strdupW(name
);
1759 table
->numconflicts
= 0;
1761 msiobj_release(&mergeview
->hdr
);
1763 return ERROR_SUCCESS
;
1766 msiobj_release(&mergeview
->hdr
);
1767 free_merge_table(table
);
1772 static UINT
merge_diff_tables(MSIRECORD
*rec
, LPVOID param
)
1774 MERGEDATA
*data
= param
;
1776 MSIQUERY
*dbview
= NULL
;
1777 MSIQUERY
*mergeview
= NULL
;
1781 static const WCHAR query
[] = {'S','E','L','E','C','T',' ','*',' ',
1782 'F','R','O','M',' ','`','%','s','`',0};
1784 name
= MSI_RecordGetString(rec
, 1);
1786 r
= MSI_OpenQuery(data
->merge
, &mergeview
, query
, name
);
1787 if (r
!= ERROR_SUCCESS
)
1790 if (TABLE_Exists(data
->db
, name
))
1792 r
= MSI_OpenQuery(data
->db
, &dbview
, query
, name
);
1793 if (r
!= ERROR_SUCCESS
)
1796 r
= merge_verify_colnames(dbview
, mergeview
);
1797 if (r
!= ERROR_SUCCESS
)
1800 r
= merge_verify_primary_keys(data
->db
, data
->merge
, name
);
1801 if (r
!= ERROR_SUCCESS
)
1805 r
= msi_get_merge_table(data
->merge
, name
, &table
);
1806 if (r
!= ERROR_SUCCESS
)
1809 data
->curtable
= table
;
1810 data
->curview
= mergeview
;
1811 r
= MSI_IterateRecords(mergeview
, NULL
, merge_diff_row
, data
);
1812 if (r
!= ERROR_SUCCESS
)
1814 free_merge_table(table
);
1818 list_add_tail(data
->tabledata
, &table
->entry
);
1821 msiobj_release(&dbview
->hdr
);
1822 msiobj_release(&mergeview
->hdr
);
1826 static UINT
gather_merge_data(MSIDATABASE
*db
, MSIDATABASE
*merge
,
1827 struct list
*tabledata
)
1829 static const WCHAR query
[] = {
1830 'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
1831 '`','_','T','a','b','l','e','s','`',0};
1836 r
= MSI_DatabaseOpenViewW(merge
, query
, &view
);
1837 if (r
!= ERROR_SUCCESS
)
1842 data
.tabledata
= tabledata
;
1843 r
= MSI_IterateRecords(view
, NULL
, merge_diff_tables
, &data
);
1844 msiobj_release(&view
->hdr
);
1848 static UINT
merge_table(MSIDATABASE
*db
, MERGETABLE
*table
)
1854 if (!TABLE_Exists(db
, table
->name
))
1856 r
= msi_add_table_to_db(db
, table
->columns
, table
->types
,
1857 table
->labels
, table
->numlabels
, table
->numcolumns
);
1858 if (r
!= ERROR_SUCCESS
)
1859 return ERROR_FUNCTION_FAILED
;
1862 LIST_FOR_EACH_ENTRY(row
, &table
->rows
, MERGEROW
, entry
)
1864 r
= TABLE_CreateView(db
, table
->name
, &tv
);
1865 if (r
!= ERROR_SUCCESS
)
1868 r
= tv
->ops
->insert_row(tv
, row
->data
, -1, FALSE
);
1869 tv
->ops
->delete(tv
);
1871 if (r
!= ERROR_SUCCESS
)
1875 return ERROR_SUCCESS
;
1878 static UINT
update_merge_errors(MSIDATABASE
*db
, LPCWSTR error
,
1879 LPWSTR table
, DWORD numconflicts
)
1884 static const WCHAR create
[] = {
1885 'C','R','E','A','T','E',' ','T','A','B','L','E',' ',
1886 '`','%','s','`',' ','(','`','T','a','b','l','e','`',' ',
1887 'C','H','A','R','(','2','5','5',')',' ','N','O','T',' ',
1888 'N','U','L','L',',',' ','`','N','u','m','R','o','w','M','e','r','g','e',
1889 'C','o','n','f','l','i','c','t','s','`',' ','S','H','O','R','T',' ',
1890 'N','O','T',' ','N','U','L','L',' ','P','R','I','M','A','R','Y',' ',
1891 'K','E','Y',' ','`','T','a','b','l','e','`',')',0};
1892 static const WCHAR insert
[] = {
1893 'I','N','S','E','R','T',' ','I','N','T','O',' ',
1894 '`','%','s','`',' ','(','`','T','a','b','l','e','`',',',' ',
1895 '`','N','u','m','R','o','w','M','e','r','g','e',
1896 'C','o','n','f','l','i','c','t','s','`',')',' ','V','A','L','U','E','S',
1897 ' ','(','\'','%','s','\'',',',' ','%','d',')',0};
1899 if (!TABLE_Exists(db
, error
))
1901 r
= MSI_OpenQuery(db
, &view
, create
, error
);
1902 if (r
!= ERROR_SUCCESS
)
1905 r
= MSI_ViewExecute(view
, NULL
);
1906 msiobj_release(&view
->hdr
);
1907 if (r
!= ERROR_SUCCESS
)
1911 r
= MSI_OpenQuery(db
, &view
, insert
, error
, table
, numconflicts
);
1912 if (r
!= ERROR_SUCCESS
)
1915 r
= MSI_ViewExecute(view
, NULL
);
1916 msiobj_release(&view
->hdr
);
1920 UINT WINAPI
MsiDatabaseMergeW(MSIHANDLE hDatabase
, MSIHANDLE hDatabaseMerge
,
1921 LPCWSTR szTableName
)
1923 struct list tabledata
= LIST_INIT(tabledata
);
1924 struct list
*item
, *cursor
;
1925 MSIDATABASE
*db
, *merge
;
1930 TRACE("(%d, %d, %s)\n", hDatabase
, hDatabaseMerge
,
1931 debugstr_w(szTableName
));
1933 if (szTableName
&& !*szTableName
)
1934 return ERROR_INVALID_TABLE
;
1936 db
= msihandle2msiinfo(hDatabase
, MSIHANDLETYPE_DATABASE
);
1937 merge
= msihandle2msiinfo(hDatabaseMerge
, MSIHANDLETYPE_DATABASE
);
1940 r
= ERROR_INVALID_HANDLE
;
1944 r
= gather_merge_data(db
, merge
, &tabledata
);
1945 if (r
!= ERROR_SUCCESS
)
1949 LIST_FOR_EACH_ENTRY(table
, &tabledata
, MERGETABLE
, entry
)
1951 if (table
->numconflicts
)
1955 r
= update_merge_errors(db
, szTableName
, table
->name
,
1956 table
->numconflicts
);
1957 if (r
!= ERROR_SUCCESS
)
1962 r
= merge_table(db
, table
);
1963 if (r
!= ERROR_SUCCESS
)
1968 LIST_FOR_EACH_SAFE(item
, cursor
, &tabledata
)
1970 MERGETABLE
*table
= LIST_ENTRY(item
, MERGETABLE
, entry
);
1971 list_remove(&table
->entry
);
1972 free_merge_table(table
);
1976 r
= ERROR_FUNCTION_FAILED
;
1979 msiobj_release(&db
->hdr
);
1980 msiobj_release(&merge
->hdr
);
1984 MSIDBSTATE WINAPI
MsiGetDatabaseState( MSIHANDLE handle
)
1986 MSIDBSTATE ret
= MSIDBSTATE_READ
;
1989 TRACE("%d\n", handle
);
1991 db
= msihandle2msiinfo( handle
, MSIHANDLETYPE_DATABASE
);
1994 WARN("MsiGetDatabaseState not allowed during a custom action!\n");
1995 return MSIDBSTATE_ERROR
;
1998 if (db
->mode
!= MSIDBOPEN_READONLY
)
1999 ret
= MSIDBSTATE_WRITE
;
2000 msiobj_release( &db
->hdr
);
2005 typedef struct _msi_remote_database_impl
{
2006 IWineMsiRemoteDatabase IWineMsiRemoteDatabase_iface
;
2009 } msi_remote_database_impl
;
2011 static inline msi_remote_database_impl
*impl_from_IWineMsiRemoteDatabase( IWineMsiRemoteDatabase
*iface
)
2013 return CONTAINING_RECORD(iface
, msi_remote_database_impl
, IWineMsiRemoteDatabase_iface
);
2016 static HRESULT WINAPI
mrd_QueryInterface( IWineMsiRemoteDatabase
*iface
,
2017 REFIID riid
,LPVOID
*ppobj
)
2019 if( IsEqualCLSID( riid
, &IID_IUnknown
) ||
2020 IsEqualCLSID( riid
, &IID_IWineMsiRemoteDatabase
) )
2022 IWineMsiRemoteDatabase_AddRef( iface
);
2027 return E_NOINTERFACE
;
2030 static ULONG WINAPI
mrd_AddRef( IWineMsiRemoteDatabase
*iface
)
2032 msi_remote_database_impl
* This
= impl_from_IWineMsiRemoteDatabase( iface
);
2034 return InterlockedIncrement( &This
->refs
);
2037 static ULONG WINAPI
mrd_Release( IWineMsiRemoteDatabase
*iface
)
2039 msi_remote_database_impl
* This
= impl_from_IWineMsiRemoteDatabase( iface
);
2042 r
= InterlockedDecrement( &This
->refs
);
2045 MsiCloseHandle( This
->database
);
2051 static HRESULT WINAPI
mrd_IsTablePersistent( IWineMsiRemoteDatabase
*iface
,
2052 LPCWSTR table
, MSICONDITION
*persistent
)
2054 msi_remote_database_impl
*This
= impl_from_IWineMsiRemoteDatabase( iface
);
2055 *persistent
= MsiDatabaseIsTablePersistentW(This
->database
, table
);
2059 static HRESULT WINAPI
mrd_GetPrimaryKeys( IWineMsiRemoteDatabase
*iface
,
2060 LPCWSTR table
, MSIHANDLE
*keys
)
2062 msi_remote_database_impl
*This
= impl_from_IWineMsiRemoteDatabase( iface
);
2063 UINT r
= MsiDatabaseGetPrimaryKeysW(This
->database
, table
, keys
);
2064 return HRESULT_FROM_WIN32(r
);
2067 static HRESULT WINAPI
mrd_GetSummaryInformation( IWineMsiRemoteDatabase
*iface
,
2068 UINT updatecount
, MSIHANDLE
*suminfo
)
2070 msi_remote_database_impl
*This
= impl_from_IWineMsiRemoteDatabase( iface
);
2071 UINT r
= MsiGetSummaryInformationW(This
->database
, NULL
, updatecount
, suminfo
);
2072 return HRESULT_FROM_WIN32(r
);
2075 static HRESULT WINAPI
mrd_OpenView( IWineMsiRemoteDatabase
*iface
,
2076 LPCWSTR query
, MSIHANDLE
*view
)
2078 msi_remote_database_impl
*This
= impl_from_IWineMsiRemoteDatabase( iface
);
2079 UINT r
= MsiDatabaseOpenViewW(This
->database
, query
, view
);
2080 return HRESULT_FROM_WIN32(r
);
2083 static HRESULT WINAPI
mrd_SetMsiHandle( IWineMsiRemoteDatabase
*iface
, MSIHANDLE handle
)
2085 msi_remote_database_impl
* This
= impl_from_IWineMsiRemoteDatabase( iface
);
2086 This
->database
= handle
;
2090 static const IWineMsiRemoteDatabaseVtbl msi_remote_database_vtbl
=
2095 mrd_IsTablePersistent
,
2097 mrd_GetSummaryInformation
,
2102 HRESULT
create_msi_remote_database( IUnknown
*pOuter
, LPVOID
*ppObj
)
2104 msi_remote_database_impl
*This
;
2106 This
= msi_alloc( sizeof *This
);
2108 return E_OUTOFMEMORY
;
2110 This
->IWineMsiRemoteDatabase_iface
.lpVtbl
= &msi_remote_database_vtbl
;
2114 *ppObj
= &This
->IWineMsiRemoteDatabase_iface
;