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 #define NONAMELESSUNION
31 #include "wine/debug.h"
32 #include "wine/unicode.h"
38 #include "msiserver.h"
43 WINE_DEFAULT_DEBUG_CHANNEL(msi
);
48 * An .msi file is a structured storage file.
49 * It contains a number of streams.
50 * A stream for each table in the database.
51 * Two streams for the string table in the database.
52 * Any binary data in a table is a reference to a stream.
55 #define IS_INTMSIDBOPEN(x) (((ULONG_PTR)(x) >> 16) == 0)
57 typedef struct tagMSITRANSFORM
{
62 typedef struct tagMSISTREAM
{
68 static UINT
find_open_stream( MSIDATABASE
*db
, IStorage
*stg
, LPCWSTR name
, IStream
**stm
)
72 LIST_FOR_EACH_ENTRY( stream
, &db
->streams
, MSISTREAM
, entry
)
77 if (stream
->stg
!= stg
) continue;
79 r
= IStream_Stat( stream
->stm
, &stat
, 0 );
82 WARN("failed to stat stream r = %08x!\n", r
);
86 if( !strcmpW( name
, stat
.pwcsName
) )
88 TRACE("found %s\n", debugstr_w(name
));
90 CoTaskMemFree( stat
.pwcsName
);
94 CoTaskMemFree( stat
.pwcsName
);
97 return ERROR_FUNCTION_FAILED
;
100 UINT
msi_clone_open_stream( MSIDATABASE
*db
, IStorage
*stg
, LPCWSTR name
, IStream
**stm
)
104 if (find_open_stream( db
, stg
, name
, &stream
) == ERROR_SUCCESS
)
109 r
= IStream_Clone( stream
, stm
);
112 WARN("failed to clone stream r = %08x!\n", r
);
113 return ERROR_FUNCTION_FAILED
;
117 r
= IStream_Seek( *stm
, pos
, STREAM_SEEK_SET
, NULL
);
120 IStream_Release( *stm
);
121 return ERROR_FUNCTION_FAILED
;
124 return ERROR_SUCCESS
;
127 return ERROR_FUNCTION_FAILED
;
130 UINT
msi_get_raw_stream( MSIDATABASE
*db
, LPCWSTR stname
, IStream
**stm
)
134 WCHAR decoded
[MAX_STREAM_NAME_LEN
];
136 decode_streamname( stname
, decoded
);
137 TRACE("%s -> %s\n", debugstr_w(stname
), debugstr_w(decoded
));
139 if (msi_clone_open_stream( db
, db
->storage
, stname
, stm
) == ERROR_SUCCESS
)
140 return ERROR_SUCCESS
;
142 r
= IStorage_OpenStream( db
->storage
, stname
, NULL
,
143 STGM_READ
| STGM_SHARE_EXCLUSIVE
, 0, stm
);
146 MSITRANSFORM
*transform
;
148 LIST_FOR_EACH_ENTRY( transform
, &db
->transforms
, MSITRANSFORM
, entry
)
150 r
= IStorage_OpenStream( transform
->stg
, stname
, NULL
,
151 STGM_READ
| STGM_SHARE_EXCLUSIVE
, 0, stm
);
154 stg
= transform
->stg
;
159 else stg
= db
->storage
;
165 if (!(stream
= msi_alloc( sizeof(MSISTREAM
) ))) return ERROR_NOT_ENOUGH_MEMORY
;
167 IStream_AddRef( stg
);
169 IStream_AddRef( *stm
);
170 list_add_tail( &db
->streams
, &stream
->entry
);
173 return SUCCEEDED(r
) ? ERROR_SUCCESS
: ERROR_FUNCTION_FAILED
;
176 static void free_transforms( MSIDATABASE
*db
)
178 while( !list_empty( &db
->transforms
) )
180 MSITRANSFORM
*t
= LIST_ENTRY( list_head( &db
->transforms
),
181 MSITRANSFORM
, entry
);
182 list_remove( &t
->entry
);
183 IStorage_Release( t
->stg
);
188 void msi_destroy_stream( MSIDATABASE
*db
, const WCHAR
*stname
)
190 MSISTREAM
*stream
, *stream2
;
192 LIST_FOR_EACH_ENTRY_SAFE( stream
, stream2
, &db
->streams
, MSISTREAM
, entry
)
197 r
= IStream_Stat( stream
->stm
, &stat
, 0 );
200 WARN("failed to stat stream r = %08x\n", r
);
204 if (!strcmpW( stname
, stat
.pwcsName
))
206 TRACE("destroying %s\n", debugstr_w(stname
));
208 list_remove( &stream
->entry
);
209 IStream_Release( stream
->stm
);
210 IStream_Release( stream
->stg
);
211 IStorage_DestroyElement( stream
->stg
, stname
);
213 CoTaskMemFree( stat
.pwcsName
);
216 CoTaskMemFree( stat
.pwcsName
);
220 static void free_streams( MSIDATABASE
*db
)
222 while( !list_empty( &db
->streams
) )
224 MSISTREAM
*s
= LIST_ENTRY(list_head( &db
->streams
), MSISTREAM
, entry
);
225 list_remove( &s
->entry
);
226 IStream_Release( s
->stm
);
227 IStream_Release( s
->stg
);
232 void append_storage_to_db( MSIDATABASE
*db
, IStorage
*stg
)
236 t
= msi_alloc( sizeof *t
);
238 IStorage_AddRef( stg
);
239 list_add_head( &db
->transforms
, &t
->entry
);
241 /* the transform may add or replace streams */
245 static VOID
MSI_CloseDatabase( MSIOBJECTHDR
*arg
)
247 MSIDATABASE
*db
= (MSIDATABASE
*) arg
;
250 free_cached_tables( db
);
252 free_transforms( db
);
253 if (db
->strings
) msi_destroy_stringtable( db
->strings
);
254 IStorage_Release( db
->storage
);
257 DeleteFileW( db
->deletefile
);
258 msi_free( db
->deletefile
);
262 static HRESULT
db_initialize( IStorage
*stg
, const GUID
*clsid
)
264 static const WCHAR szTables
[] = { '_','T','a','b','l','e','s',0 };
267 hr
= IStorage_SetClass( stg
, clsid
);
270 WARN("failed to set class id 0x%08x\n", hr
);
274 /* create the _Tables stream */
275 hr
= write_stream_data( stg
, szTables
, NULL
, 0, TRUE
);
278 WARN("failed to create _Tables stream 0x%08x\n", hr
);
282 hr
= msi_init_string_table( stg
);
285 WARN("failed to initialize string table 0x%08x\n", hr
);
289 hr
= IStorage_Commit( stg
, 0 );
292 WARN("failed to commit changes 0x%08x\n", hr
);
299 UINT
MSI_OpenDatabaseW(LPCWSTR szDBPath
, LPCWSTR szPersist
, MSIDATABASE
**pdb
)
301 IStorage
*stg
= NULL
;
303 MSIDATABASE
*db
= NULL
;
304 UINT ret
= ERROR_FUNCTION_FAILED
;
305 LPCWSTR szMode
, save_path
;
307 BOOL created
= FALSE
, patch
= FALSE
;
308 WCHAR path
[MAX_PATH
];
310 TRACE("%s %s\n",debugstr_w(szDBPath
),debugstr_w(szPersist
) );
313 return ERROR_INVALID_PARAMETER
;
315 if (szPersist
- MSIDBOPEN_PATCHFILE
>= MSIDBOPEN_READONLY
&&
316 szPersist
- MSIDBOPEN_PATCHFILE
<= MSIDBOPEN_CREATEDIRECT
)
318 TRACE("Database is a patch\n");
319 szPersist
-= MSIDBOPEN_PATCHFILE
;
323 save_path
= szDBPath
;
325 if( !IS_INTMSIDBOPEN(szPersist
) )
327 if (!CopyFileW( szDBPath
, szPersist
, FALSE
))
328 return ERROR_OPEN_FAILED
;
330 szDBPath
= szPersist
;
331 szPersist
= MSIDBOPEN_TRANSACT
;
335 if( szPersist
== MSIDBOPEN_READONLY
)
337 r
= StgOpenStorage( szDBPath
, NULL
,
338 STGM_DIRECT
|STGM_READ
|STGM_SHARE_DENY_WRITE
, NULL
, 0, &stg
);
340 else if( szPersist
== MSIDBOPEN_CREATE
)
342 r
= StgCreateDocfile( szDBPath
,
343 STGM_CREATE
|STGM_TRANSACTED
|STGM_READWRITE
|STGM_SHARE_EXCLUSIVE
, 0, &stg
);
346 r
= db_initialize( stg
, patch
? &CLSID_MsiPatch
: &CLSID_MsiDatabase
);
349 else if( szPersist
== MSIDBOPEN_CREATEDIRECT
)
351 r
= StgCreateDocfile( szDBPath
,
352 STGM_CREATE
|STGM_DIRECT
|STGM_READWRITE
|STGM_SHARE_EXCLUSIVE
, 0, &stg
);
355 r
= db_initialize( stg
, patch
? &CLSID_MsiPatch
: &CLSID_MsiDatabase
);
358 else if( szPersist
== MSIDBOPEN_TRANSACT
)
360 r
= StgOpenStorage( szDBPath
, NULL
,
361 STGM_TRANSACTED
|STGM_READWRITE
|STGM_SHARE_DENY_WRITE
, NULL
, 0, &stg
);
363 else if( szPersist
== MSIDBOPEN_DIRECT
)
365 r
= StgOpenStorage( szDBPath
, NULL
,
366 STGM_DIRECT
|STGM_READWRITE
|STGM_SHARE_EXCLUSIVE
, NULL
, 0, &stg
);
370 ERR("unknown flag %p\n",szPersist
);
371 return ERROR_INVALID_PARAMETER
;
374 if( FAILED( r
) || !stg
)
376 WARN("open failed r = %08x for %s\n", r
, debugstr_w(szDBPath
));
377 return ERROR_FUNCTION_FAILED
;
380 r
= IStorage_Stat( stg
, &stat
, STATFLAG_NONAME
);
383 FIXME("Failed to stat storage\n");
387 if ( !IsEqualGUID( &stat
.clsid
, &CLSID_MsiDatabase
) &&
388 !IsEqualGUID( &stat
.clsid
, &CLSID_MsiPatch
) &&
389 !IsEqualGUID( &stat
.clsid
, &CLSID_MsiTransform
) )
391 ERR("storage GUID is not a MSI database GUID %s\n",
392 debugstr_guid(&stat
.clsid
) );
396 if ( patch
&& !IsEqualGUID( &stat
.clsid
, &CLSID_MsiPatch
) )
398 ERR("storage GUID is not the MSI patch GUID %s\n",
399 debugstr_guid(&stat
.clsid
) );
400 ret
= ERROR_OPEN_FAILED
;
404 db
= alloc_msiobject( MSIHANDLETYPE_DATABASE
, sizeof (MSIDATABASE
),
408 FIXME("Failed to allocate a handle\n");
412 if (!strchrW( save_path
, '\\' ))
414 GetCurrentDirectoryW( MAX_PATH
, path
);
415 lstrcatW( path
, szBackSlash
);
416 lstrcatW( path
, save_path
);
419 lstrcpyW( path
, save_path
);
421 db
->path
= strdupW( path
);
422 db
->media_transform_offset
= MSI_INITIAL_MEDIA_TRANSFORM_OFFSET
;
423 db
->media_transform_disk_id
= MSI_INITIAL_MEDIA_TRANSFORM_DISKID
;
425 if( TRACE_ON( msi
) )
426 enum_stream_names( stg
);
431 db
->deletefile
= strdupW( szDBPath
);
432 list_init( &db
->tables
);
433 list_init( &db
->transforms
);
434 list_init( &db
->streams
);
436 db
->strings
= msi_load_string_table( stg
, &db
->bytes_per_strref
);
442 msiobj_addref( &db
->hdr
);
443 IStorage_AddRef( stg
);
448 msiobj_release( &db
->hdr
);
450 IStorage_Release( stg
);
455 UINT WINAPI
MsiOpenDatabaseW(LPCWSTR szDBPath
, LPCWSTR szPersist
, MSIHANDLE
*phDB
)
460 TRACE("%s %s %p\n",debugstr_w(szDBPath
),debugstr_w(szPersist
), phDB
);
462 ret
= MSI_OpenDatabaseW( szDBPath
, szPersist
, &db
);
463 if( ret
== ERROR_SUCCESS
)
465 *phDB
= alloc_msihandle( &db
->hdr
);
467 ret
= ERROR_NOT_ENOUGH_MEMORY
;
468 msiobj_release( &db
->hdr
);
474 UINT WINAPI
MsiOpenDatabaseA(LPCSTR szDBPath
, LPCSTR szPersist
, MSIHANDLE
*phDB
)
476 HRESULT r
= ERROR_FUNCTION_FAILED
;
477 LPWSTR szwDBPath
= NULL
, szwPersist
= NULL
;
479 TRACE("%s %s %p\n", debugstr_a(szDBPath
), debugstr_a(szPersist
), phDB
);
483 szwDBPath
= strdupAtoW( szDBPath
);
488 if( !IS_INTMSIDBOPEN(szPersist
) )
490 szwPersist
= strdupAtoW( szPersist
);
495 szwPersist
= (LPWSTR
)(DWORD_PTR
)szPersist
;
497 r
= MsiOpenDatabaseW( szwDBPath
, szwPersist
, phDB
);
500 if( !IS_INTMSIDBOPEN(szPersist
) )
501 msi_free( szwPersist
);
502 msi_free( szwDBPath
);
507 static LPWSTR
msi_read_text_archive(LPCWSTR path
, DWORD
*len
)
512 DWORD read
, size
= 0;
514 file
= CreateFileW( path
, GENERIC_READ
, FILE_SHARE_READ
, NULL
, OPEN_EXISTING
, 0, NULL
);
515 if (file
== INVALID_HANDLE_VALUE
)
518 size
= GetFileSize( file
, NULL
);
519 if (!(data
= msi_alloc( size
))) goto done
;
521 if (!ReadFile( file
, data
, size
, &read
, NULL
) || read
!= size
) goto done
;
523 while (!data
[size
- 1]) size
--;
524 *len
= MultiByteToWideChar( CP_ACP
, 0, data
, size
, NULL
, 0 );
525 if ((wdata
= msi_alloc( (*len
+ 1) * sizeof(WCHAR
) )))
527 MultiByteToWideChar( CP_ACP
, 0, data
, size
, wdata
, *len
);
537 static void msi_parse_line(LPWSTR
*line
, LPWSTR
**entries
, DWORD
*num_entries
, DWORD
*len
)
539 LPWSTR ptr
= *line
, save
;
540 DWORD i
, count
= 1, chars_left
= *len
;
544 /* stay on this line */
545 while (chars_left
&& *ptr
!= '\n')
547 /* entries are separated by tabs */
555 *entries
= msi_alloc(count
* sizeof(LPWSTR
));
559 /* store pointers into the data */
561 for (i
= 0, ptr
= *line
; i
< count
; i
++)
563 while (chars_left
&& *ptr
== '\r')
570 while (chars_left
&& *ptr
!= '\t' && *ptr
!= '\n' && *ptr
!= '\r')
572 if (!*ptr
) *ptr
= '\n'; /* convert embedded nulls to \n */
573 if (ptr
> *line
&& *ptr
== '\x19' && *(ptr
- 1) == '\x11')
582 /* NULL-separate the data */
583 if (*ptr
== '\n' || *ptr
== '\r')
585 while (chars_left
&& (*ptr
== '\n' || *ptr
== '\r'))
596 (*entries
)[i
] = save
;
599 /* move to the next line if there's more, else EOF */
603 *num_entries
= count
;
606 static LPWSTR
msi_build_createsql_prelude(LPWSTR table
)
611 static const WCHAR create_fmt
[] = {'C','R','E','A','T','E',' ','T','A','B','L','E',' ','`','%','s','`',' ','(',' ',0};
613 size
= sizeof(create_fmt
)/sizeof(create_fmt
[0]) + lstrlenW(table
) - 2;
614 prelude
= msi_alloc(size
* sizeof(WCHAR
));
618 sprintfW(prelude
, create_fmt
, table
);
622 static LPWSTR
msi_build_createsql_columns(LPWSTR
*columns_data
, LPWSTR
*types
, DWORD num_columns
)
626 DWORD sql_size
= 1, i
, len
;
627 WCHAR expanded
[128], *ptr
;
628 WCHAR size
[10], comma
[2], extra
[30];
630 static const WCHAR column_fmt
[] = {'`','%','s','`',' ','%','s','%','s','%','s','%','s',' ',0};
631 static const WCHAR size_fmt
[] = {'(','%','s',')',0};
632 static const WCHAR type_char
[] = {'C','H','A','R',0};
633 static const WCHAR type_int
[] = {'I','N','T',0};
634 static const WCHAR type_long
[] = {'L','O','N','G',0};
635 static const WCHAR type_object
[] = {'O','B','J','E','C','T',0};
636 static const WCHAR type_notnull
[] = {' ','N','O','T',' ','N','U','L','L',0};
637 static const WCHAR localizable
[] = {' ','L','O','C','A','L','I','Z','A','B','L','E',0};
639 columns
= msi_alloc_zero(sql_size
* sizeof(WCHAR
));
643 for (i
= 0; i
< num_columns
; i
++)
646 comma
[1] = size
[0] = extra
[0] = '\0';
648 if (i
== num_columns
- 1)
660 lstrcpyW(extra
, type_notnull
);
662 lstrcatW(extra
, localizable
);
664 sprintfW(size
, size_fmt
, ptr
);
667 lstrcpyW(extra
, type_notnull
);
670 sprintfW(size
, size_fmt
, ptr
);
673 lstrcpyW(extra
, type_notnull
);
681 WARN("invalid int width %u\n", len
);
687 lstrcpyW(extra
, type_notnull
);
692 ERR("Unknown type: %c\n", types
[i
][0]);
697 sprintfW(expanded
, column_fmt
, columns_data
[i
], type
, size
, extra
, comma
);
698 sql_size
+= lstrlenW(expanded
);
700 p
= msi_realloc(columns
, sql_size
* sizeof(WCHAR
));
708 lstrcatW(columns
, expanded
);
714 static LPWSTR
msi_build_createsql_postlude(LPWSTR
*primary_keys
, DWORD num_keys
)
716 LPWSTR postlude
, keys
, ptr
;
717 DWORD size
, key_size
, i
;
719 static const WCHAR key_fmt
[] = {'`','%','s','`',',',' ',0};
720 static const WCHAR postlude_fmt
[] = {'P','R','I','M','A','R','Y',' ','K','E','Y',' ','%','s',')',0};
722 for (i
= 0, size
= 1; i
< num_keys
; i
++)
723 size
+= lstrlenW(key_fmt
) + lstrlenW(primary_keys
[i
]) - 2;
725 keys
= msi_alloc(size
* sizeof(WCHAR
));
729 for (i
= 0, ptr
= keys
; i
< num_keys
; i
++)
731 key_size
= lstrlenW(key_fmt
) + lstrlenW(primary_keys
[i
]) -2;
732 sprintfW(ptr
, key_fmt
, primary_keys
[i
]);
736 /* remove final ', ' */
739 size
= lstrlenW(postlude_fmt
) + size
- 1;
740 postlude
= msi_alloc(size
* sizeof(WCHAR
));
744 sprintfW(postlude
, postlude_fmt
, keys
);
751 static UINT
msi_add_table_to_db(MSIDATABASE
*db
, LPWSTR
*columns
, LPWSTR
*types
, LPWSTR
*labels
, DWORD num_labels
, DWORD num_columns
)
753 UINT r
= ERROR_OUTOFMEMORY
;
756 LPWSTR create_sql
= NULL
;
757 LPWSTR prelude
, columns_sql
, postlude
;
759 prelude
= msi_build_createsql_prelude(labels
[0]);
760 columns_sql
= msi_build_createsql_columns(columns
, types
, num_columns
);
761 postlude
= msi_build_createsql_postlude(labels
+ 1, num_labels
- 1); /* skip over table name */
763 if (!prelude
|| !columns_sql
|| !postlude
)
766 size
= lstrlenW(prelude
) + lstrlenW(columns_sql
) + lstrlenW(postlude
) + 1;
767 create_sql
= msi_alloc(size
* sizeof(WCHAR
));
771 lstrcpyW(create_sql
, prelude
);
772 lstrcatW(create_sql
, columns_sql
);
773 lstrcatW(create_sql
, postlude
);
775 r
= MSI_DatabaseOpenViewW( db
, create_sql
, &view
);
776 if (r
!= ERROR_SUCCESS
)
779 r
= MSI_ViewExecute(view
, NULL
);
781 msiobj_release(&view
->hdr
);
785 msi_free(columns_sql
);
787 msi_free(create_sql
);
791 static LPWSTR
msi_import_stream_filename(LPCWSTR path
, LPCWSTR name
)
794 LPWSTR fullname
, ptr
;
796 len
= lstrlenW(path
) + lstrlenW(name
) + 1;
797 fullname
= msi_alloc(len
*sizeof(WCHAR
));
801 lstrcpyW( fullname
, path
);
803 /* chop off extension from path */
804 ptr
= strrchrW(fullname
, '.');
811 lstrcpyW( ptr
, name
);
815 static UINT
construct_record(DWORD num_columns
, LPWSTR
*types
,
816 LPWSTR
*data
, LPWSTR path
, MSIRECORD
**rec
)
820 *rec
= MSI_CreateRecord(num_columns
);
822 return ERROR_OUTOFMEMORY
;
824 for (i
= 0; i
< num_columns
; i
++)
828 case 'L': case 'l': case 'S': case 's':
829 MSI_RecordSetStringW(*rec
, i
+ 1, data
[i
]);
833 MSI_RecordSetInteger(*rec
, i
+ 1, atoiW(data
[i
]));
839 LPWSTR file
= msi_import_stream_filename(path
, data
[i
]);
841 return ERROR_FUNCTION_FAILED
;
843 r
= MSI_RecordSetStreamFromFileW(*rec
, i
+ 1, file
);
845 if (r
!= ERROR_SUCCESS
)
846 return ERROR_FUNCTION_FAILED
;
850 ERR("Unhandled column type: %c\n", types
[i
][0]);
851 msiobj_release(&(*rec
)->hdr
);
852 return ERROR_FUNCTION_FAILED
;
856 return ERROR_SUCCESS
;
859 static UINT
msi_add_records_to_table(MSIDATABASE
*db
, LPWSTR
*columns
, LPWSTR
*types
,
860 LPWSTR
*labels
, LPWSTR
**records
,
861 int num_columns
, int num_records
,
869 static const WCHAR select
[] = {
870 'S','E','L','E','C','T',' ','*',' ',
871 'F','R','O','M',' ','`','%','s','`',0
874 r
= MSI_OpenQuery(db
, &view
, select
, labels
[0]);
875 if (r
!= ERROR_SUCCESS
)
878 while (MSI_ViewFetch(view
, &rec
) != ERROR_NO_MORE_ITEMS
)
880 r
= MSI_ViewModify(view
, MSIMODIFY_DELETE
, rec
);
881 msiobj_release(&rec
->hdr
);
882 if (r
!= ERROR_SUCCESS
)
886 for (i
= 0; i
< num_records
; i
++)
888 r
= construct_record(num_columns
, types
, records
[i
], path
, &rec
);
889 if (r
!= ERROR_SUCCESS
)
892 r
= MSI_ViewModify(view
, MSIMODIFY_INSERT
, rec
);
893 if (r
!= ERROR_SUCCESS
)
895 msiobj_release(&rec
->hdr
);
899 msiobj_release(&rec
->hdr
);
903 msiobj_release(&view
->hdr
);
907 static UINT
MSI_DatabaseImport(MSIDATABASE
*db
, LPCWSTR folder
, LPCWSTR file
)
911 DWORD num_labels
, num_types
;
912 DWORD num_columns
, num_records
= 0;
913 LPWSTR
*columns
, *types
, *labels
;
914 LPWSTR path
, ptr
, data
;
915 LPWSTR
**records
= NULL
;
916 LPWSTR
**temp_records
;
918 static const WCHAR suminfo
[] =
919 {'_','S','u','m','m','a','r','y','I','n','f','o','r','m','a','t','i','o','n',0};
920 static const WCHAR forcecodepage
[] =
921 {'_','F','o','r','c','e','C','o','d','e','p','a','g','e',0};
923 TRACE("%p %s %s\n", db
, debugstr_w(folder
), debugstr_w(file
) );
925 if( folder
== NULL
|| file
== NULL
)
926 return ERROR_INVALID_PARAMETER
;
928 len
= lstrlenW(folder
) + lstrlenW(szBackSlash
) + lstrlenW(file
) + 1;
929 path
= msi_alloc( len
* sizeof(WCHAR
) );
931 return ERROR_OUTOFMEMORY
;
933 lstrcpyW( path
, folder
);
934 lstrcatW( path
, szBackSlash
);
935 lstrcatW( path
, file
);
937 data
= msi_read_text_archive( path
, &len
);
940 msi_parse_line( &ptr
, &columns
, &num_columns
, &len
);
941 msi_parse_line( &ptr
, &types
, &num_types
, &len
);
942 msi_parse_line( &ptr
, &labels
, &num_labels
, &len
);
944 if (num_columns
== 1 && !columns
[0][0] && num_labels
== 1 && !labels
[0][0] &&
945 num_types
== 2 && !strcmpW( types
[1], forcecodepage
))
947 r
= msi_set_string_table_codepage( db
->strings
, atoiW( types
[0] ) );
951 if (num_columns
!= num_types
)
953 r
= ERROR_FUNCTION_FAILED
;
957 records
= msi_alloc(sizeof(LPWSTR
*));
960 r
= ERROR_OUTOFMEMORY
;
964 /* read in the table records */
967 msi_parse_line( &ptr
, &records
[num_records
], NULL
, &len
);
970 temp_records
= msi_realloc(records
, (num_records
+ 1) * sizeof(LPWSTR
*));
973 r
= ERROR_OUTOFMEMORY
;
976 records
= temp_records
;
979 if (!strcmpW(labels
[0], suminfo
))
981 r
= msi_add_suminfo( db
, records
, num_records
, num_columns
);
982 if (r
!= ERROR_SUCCESS
)
984 r
= ERROR_FUNCTION_FAILED
;
990 if (!TABLE_Exists(db
, labels
[0]))
992 r
= msi_add_table_to_db( db
, columns
, types
, labels
, num_labels
, num_columns
);
993 if (r
!= ERROR_SUCCESS
)
995 r
= ERROR_FUNCTION_FAILED
;
1000 r
= msi_add_records_to_table( db
, columns
, types
, labels
, records
, num_columns
, num_records
, path
);
1010 for (i
= 0; i
< num_records
; i
++)
1011 msi_free(records
[i
]);
1018 UINT WINAPI
MsiDatabaseImportW(MSIHANDLE handle
, LPCWSTR szFolder
, LPCWSTR szFilename
)
1023 TRACE("%x %s %s\n",handle
,debugstr_w(szFolder
), debugstr_w(szFilename
));
1025 db
= msihandle2msiinfo( handle
, MSIHANDLETYPE_DATABASE
);
1028 IWineMsiRemoteDatabase
*remote_database
;
1030 remote_database
= (IWineMsiRemoteDatabase
*)msi_get_remote( handle
);
1031 if ( !remote_database
)
1032 return ERROR_INVALID_HANDLE
;
1034 IWineMsiRemoteDatabase_Release( remote_database
);
1035 WARN("MsiDatabaseImport not allowed during a custom action!\n");
1037 return ERROR_SUCCESS
;
1040 r
= MSI_DatabaseImport( db
, szFolder
, szFilename
);
1041 msiobj_release( &db
->hdr
);
1045 UINT WINAPI
MsiDatabaseImportA( MSIHANDLE handle
,
1046 LPCSTR szFolder
, LPCSTR szFilename
)
1048 LPWSTR path
= NULL
, file
= NULL
;
1049 UINT r
= ERROR_OUTOFMEMORY
;
1051 TRACE("%x %s %s\n", handle
, debugstr_a(szFolder
), debugstr_a(szFilename
));
1055 path
= strdupAtoW( szFolder
);
1062 file
= strdupAtoW( szFilename
);
1067 r
= MsiDatabaseImportW( handle
, path
, file
);
1076 static UINT
msi_export_record( HANDLE handle
, MSIRECORD
*row
, UINT start
)
1078 UINT i
, count
, len
, r
= ERROR_SUCCESS
;
1084 buffer
= msi_alloc( len
);
1086 return ERROR_OUTOFMEMORY
;
1088 count
= MSI_RecordGetFieldCount( row
);
1089 for ( i
=start
; i
<=count
; i
++ )
1092 r
= MSI_RecordGetStringA( row
, i
, buffer
, &sz
);
1093 if (r
== ERROR_MORE_DATA
)
1095 char *p
= msi_realloc( buffer
, sz
+ 1 );
1102 r
= MSI_RecordGetStringA( row
, i
, buffer
, &sz
);
1103 if (r
!= ERROR_SUCCESS
)
1106 if (!WriteFile( handle
, buffer
, sz
, &sz
, NULL
))
1108 r
= ERROR_FUNCTION_FAILED
;
1112 sep
= (i
< count
) ? "\t" : "\r\n";
1113 if (!WriteFile( handle
, sep
, strlen(sep
), &sz
, NULL
))
1115 r
= ERROR_FUNCTION_FAILED
;
1123 static UINT
msi_export_row( MSIRECORD
*row
, void *arg
)
1125 return msi_export_record( arg
, row
, 1 );
1128 static UINT
msi_export_forcecodepage( HANDLE handle
, UINT codepage
)
1130 static const char fmt
[] = "\r\n\r\n%u\t_ForceCodepage\r\n";
1131 char data
[sizeof(fmt
) + 10];
1134 sprintf( data
, fmt
, codepage
);
1136 sz
= lstrlenA(data
) + 1;
1137 if (!WriteFile(handle
, data
, sz
, &sz
, NULL
))
1138 return ERROR_FUNCTION_FAILED
;
1140 return ERROR_SUCCESS
;
1143 static UINT
MSI_DatabaseExport( MSIDATABASE
*db
, LPCWSTR table
,
1144 LPCWSTR folder
, LPCWSTR file
)
1146 static const WCHAR query
[] = {
1147 's','e','l','e','c','t',' ','*',' ','f','r','o','m',' ','%','s',0 };
1148 static const WCHAR forcecodepage
[] = {
1149 '_','F','o','r','c','e','C','o','d','e','p','a','g','e',0 };
1150 MSIRECORD
*rec
= NULL
;
1151 MSIQUERY
*view
= NULL
;
1156 TRACE("%p %s %s %s\n", db
, debugstr_w(table
),
1157 debugstr_w(folder
), debugstr_w(file
) );
1159 if( folder
== NULL
|| file
== NULL
)
1160 return ERROR_INVALID_PARAMETER
;
1162 len
= lstrlenW(folder
) + lstrlenW(file
) + 2;
1163 filename
= msi_alloc(len
* sizeof (WCHAR
));
1165 return ERROR_OUTOFMEMORY
;
1167 lstrcpyW( filename
, folder
);
1168 lstrcatW( filename
, szBackSlash
);
1169 lstrcatW( filename
, file
);
1171 handle
= CreateFileW( filename
, GENERIC_READ
| GENERIC_WRITE
, 0,
1172 NULL
, CREATE_ALWAYS
, FILE_ATTRIBUTE_NORMAL
, NULL
);
1173 msi_free( filename
);
1174 if (handle
== INVALID_HANDLE_VALUE
)
1175 return ERROR_FUNCTION_FAILED
;
1177 if (!strcmpW( table
, forcecodepage
))
1179 UINT codepage
= msi_get_string_table_codepage( db
->strings
);
1180 r
= msi_export_forcecodepage( handle
, codepage
);
1184 r
= MSI_OpenQuery( db
, &view
, query
, table
);
1185 if (r
== ERROR_SUCCESS
)
1187 /* write out row 1, the column names */
1188 r
= MSI_ViewGetColumnInfo(view
, MSICOLINFO_NAMES
, &rec
);
1189 if (r
== ERROR_SUCCESS
)
1191 msi_export_record( handle
, rec
, 1 );
1192 msiobj_release( &rec
->hdr
);
1195 /* write out row 2, the column types */
1196 r
= MSI_ViewGetColumnInfo(view
, MSICOLINFO_TYPES
, &rec
);
1197 if (r
== ERROR_SUCCESS
)
1199 msi_export_record( handle
, rec
, 1 );
1200 msiobj_release( &rec
->hdr
);
1203 /* write out row 3, the table name + keys */
1204 r
= MSI_DatabaseGetPrimaryKeys( db
, table
, &rec
);
1205 if (r
== ERROR_SUCCESS
)
1207 MSI_RecordSetStringW( rec
, 0, table
);
1208 msi_export_record( handle
, rec
, 0 );
1209 msiobj_release( &rec
->hdr
);
1212 /* write out row 4 onwards, the data */
1213 r
= MSI_IterateRecords( view
, 0, msi_export_row
, handle
);
1214 msiobj_release( &view
->hdr
);
1218 CloseHandle( handle
);
1222 /***********************************************************************
1223 * MsiExportDatabaseW [MSI.@]
1225 * Writes a file containing the table data as tab separated ASCII.
1227 * The format is as follows:
1229 * row1 : colname1 <tab> colname2 <tab> .... colnameN <cr> <lf>
1230 * row2 : coltype1 <tab> coltype2 <tab> .... coltypeN <cr> <lf>
1231 * row3 : tablename <tab> key1 <tab> key2 <tab> ... keyM <cr> <lf>
1233 * Followed by the data, starting at row 1 with one row per line
1235 * row4 : data <tab> data <tab> data <tab> ... data <cr> <lf>
1237 UINT WINAPI
MsiDatabaseExportW( MSIHANDLE handle
, LPCWSTR szTable
,
1238 LPCWSTR szFolder
, LPCWSTR szFilename
)
1243 TRACE("%x %s %s %s\n", handle
, debugstr_w(szTable
),
1244 debugstr_w(szFolder
), debugstr_w(szFilename
));
1246 db
= msihandle2msiinfo( handle
, MSIHANDLETYPE_DATABASE
);
1249 IWineMsiRemoteDatabase
*remote_database
;
1251 remote_database
= (IWineMsiRemoteDatabase
*)msi_get_remote( handle
);
1252 if ( !remote_database
)
1253 return ERROR_INVALID_HANDLE
;
1255 IWineMsiRemoteDatabase_Release( remote_database
);
1256 WARN("MsiDatabaseExport not allowed during a custom action!\n");
1258 return ERROR_SUCCESS
;
1261 r
= MSI_DatabaseExport( db
, szTable
, szFolder
, szFilename
);
1262 msiobj_release( &db
->hdr
);
1266 UINT WINAPI
MsiDatabaseExportA( MSIHANDLE handle
, LPCSTR szTable
,
1267 LPCSTR szFolder
, LPCSTR szFilename
)
1269 LPWSTR path
= NULL
, file
= NULL
, table
= NULL
;
1270 UINT r
= ERROR_OUTOFMEMORY
;
1272 TRACE("%x %s %s %s\n", handle
, debugstr_a(szTable
),
1273 debugstr_a(szFolder
), debugstr_a(szFilename
));
1277 table
= strdupAtoW( szTable
);
1284 path
= strdupAtoW( szFolder
);
1291 file
= strdupAtoW( szFilename
);
1296 r
= MsiDatabaseExportW( handle
, table
, path
, file
);
1306 UINT WINAPI
MsiDatabaseMergeA(MSIHANDLE hDatabase
, MSIHANDLE hDatabaseMerge
,
1312 TRACE("(%d, %d, %s)\n", hDatabase
, hDatabaseMerge
,
1313 debugstr_a(szTableName
));
1315 table
= strdupAtoW(szTableName
);
1316 r
= MsiDatabaseMergeW(hDatabase
, hDatabaseMerge
, table
);
1322 typedef struct _tagMERGETABLE
1336 typedef struct _tagMERGEROW
1342 typedef struct _tagMERGEDATA
1346 MERGETABLE
*curtable
;
1348 struct list
*tabledata
;
1351 static BOOL
merge_type_match(LPCWSTR type1
, LPCWSTR type2
)
1353 if (((type1
[0] == 'l') || (type1
[0] == 's')) &&
1354 ((type2
[0] == 'l') || (type2
[0] == 's')))
1357 if (((type1
[0] == 'L') || (type1
[0] == 'S')) &&
1358 ((type2
[0] == 'L') || (type2
[0] == 'S')))
1361 return !strcmpW( type1
, type2
);
1364 static UINT
merge_verify_colnames(MSIQUERY
*dbview
, MSIQUERY
*mergeview
)
1366 MSIRECORD
*dbrec
, *mergerec
;
1369 r
= MSI_ViewGetColumnInfo(dbview
, MSICOLINFO_NAMES
, &dbrec
);
1370 if (r
!= ERROR_SUCCESS
)
1373 r
= MSI_ViewGetColumnInfo(mergeview
, MSICOLINFO_NAMES
, &mergerec
);
1374 if (r
!= ERROR_SUCCESS
)
1377 count
= MSI_RecordGetFieldCount(dbrec
);
1378 for (i
= 1; i
<= count
; i
++)
1380 if (!MSI_RecordGetString(mergerec
, i
))
1383 if (strcmpW( MSI_RecordGetString( dbrec
, i
), MSI_RecordGetString( mergerec
, i
) ))
1385 r
= ERROR_DATATYPE_MISMATCH
;
1390 msiobj_release(&dbrec
->hdr
);
1391 msiobj_release(&mergerec
->hdr
);
1392 dbrec
= mergerec
= NULL
;
1394 r
= MSI_ViewGetColumnInfo(dbview
, MSICOLINFO_TYPES
, &dbrec
);
1395 if (r
!= ERROR_SUCCESS
)
1398 r
= MSI_ViewGetColumnInfo(mergeview
, MSICOLINFO_TYPES
, &mergerec
);
1399 if (r
!= ERROR_SUCCESS
)
1402 count
= MSI_RecordGetFieldCount(dbrec
);
1403 for (i
= 1; i
<= count
; i
++)
1405 if (!MSI_RecordGetString(mergerec
, i
))
1408 if (!merge_type_match(MSI_RecordGetString(dbrec
, i
),
1409 MSI_RecordGetString(mergerec
, i
)))
1411 r
= ERROR_DATATYPE_MISMATCH
;
1417 msiobj_release(&dbrec
->hdr
);
1418 msiobj_release(&mergerec
->hdr
);
1423 static UINT
merge_verify_primary_keys(MSIDATABASE
*db
, MSIDATABASE
*mergedb
,
1426 MSIRECORD
*dbrec
, *mergerec
= NULL
;
1429 r
= MSI_DatabaseGetPrimaryKeys(db
, table
, &dbrec
);
1430 if (r
!= ERROR_SUCCESS
)
1433 r
= MSI_DatabaseGetPrimaryKeys(mergedb
, table
, &mergerec
);
1434 if (r
!= ERROR_SUCCESS
)
1437 count
= MSI_RecordGetFieldCount(dbrec
);
1438 if (count
!= MSI_RecordGetFieldCount(mergerec
))
1440 r
= ERROR_DATATYPE_MISMATCH
;
1444 for (i
= 1; i
<= count
; i
++)
1446 if (strcmpW( MSI_RecordGetString( dbrec
, i
), MSI_RecordGetString( mergerec
, i
) ))
1448 r
= ERROR_DATATYPE_MISMATCH
;
1454 msiobj_release(&dbrec
->hdr
);
1455 msiobj_release(&mergerec
->hdr
);
1460 static LPWSTR
get_key_value(MSIQUERY
*view
, LPCWSTR key
, MSIRECORD
*rec
)
1462 MSIRECORD
*colnames
;
1464 UINT r
, i
= 0, sz
= 0;
1467 r
= MSI_ViewGetColumnInfo(view
, MSICOLINFO_NAMES
, &colnames
);
1468 if (r
!= ERROR_SUCCESS
)
1473 str
= msi_dup_record_field(colnames
, ++i
);
1474 cmp
= strcmpW( key
, str
);
1478 msiobj_release(&colnames
->hdr
);
1480 r
= MSI_RecordGetStringW(rec
, i
, NULL
, &sz
);
1481 if (r
!= ERROR_SUCCESS
)
1485 if (MSI_RecordGetString(rec
, i
)) /* check record field is a string */
1487 /* quote string record fields */
1488 const WCHAR szQuote
[] = {'\'', 0};
1490 val
= msi_alloc(sz
*sizeof(WCHAR
));
1494 lstrcpyW(val
, szQuote
);
1495 r
= MSI_RecordGetStringW(rec
, i
, val
+1, &sz
);
1496 lstrcpyW(val
+1+sz
, szQuote
);
1500 /* do not quote integer record fields */
1501 val
= msi_alloc(sz
*sizeof(WCHAR
));
1505 r
= MSI_RecordGetStringW(rec
, i
, val
, &sz
);
1508 if (r
!= ERROR_SUCCESS
)
1510 ERR("failed to get string!\n");
1518 static LPWSTR
create_diff_row_query(MSIDATABASE
*merge
, MSIQUERY
*view
,
1519 LPWSTR table
, MSIRECORD
*rec
)
1521 LPWSTR query
= NULL
, clause
= NULL
, val
;
1522 LPCWSTR setptr
, key
;
1523 DWORD size
, oldsize
;
1527 static const WCHAR keyset
[] = {
1528 '`','%','s','`',' ','=',' ','%','s',' ','A','N','D',' ',0};
1529 static const WCHAR lastkeyset
[] = {
1530 '`','%','s','`',' ','=',' ','%','s',' ',0};
1531 static const WCHAR fmt
[] = {'S','E','L','E','C','T',' ','*',' ',
1532 'F','R','O','M',' ','`','%','s','`',' ',
1533 'W','H','E','R','E',' ','%','s',0};
1535 r
= MSI_DatabaseGetPrimaryKeys(merge
, table
, &keys
);
1536 if (r
!= ERROR_SUCCESS
)
1539 clause
= msi_alloc_zero(sizeof(WCHAR
));
1544 count
= MSI_RecordGetFieldCount(keys
);
1545 for (i
= 1; i
<= count
; i
++)
1547 key
= MSI_RecordGetString(keys
, i
);
1548 val
= get_key_value(view
, key
, rec
);
1551 setptr
= lastkeyset
;
1556 size
+= lstrlenW(setptr
) + lstrlenW(key
) + lstrlenW(val
) - 4;
1557 clause
= msi_realloc(clause
, size
* sizeof (WCHAR
));
1564 sprintfW(clause
+ oldsize
- 1, setptr
, key
, val
);
1568 size
= lstrlenW(fmt
) + lstrlenW(table
) + lstrlenW(clause
) + 1;
1569 query
= msi_alloc(size
* sizeof(WCHAR
));
1573 sprintfW(query
, fmt
, table
, clause
);
1577 msiobj_release(&keys
->hdr
);
1581 static UINT
merge_diff_row(MSIRECORD
*rec
, LPVOID param
)
1583 MERGEDATA
*data
= param
;
1584 MERGETABLE
*table
= data
->curtable
;
1586 MSIQUERY
*dbview
= NULL
;
1587 MSIRECORD
*row
= NULL
;
1588 LPWSTR query
= NULL
;
1589 UINT r
= ERROR_SUCCESS
;
1591 if (TABLE_Exists(data
->db
, table
->name
))
1593 query
= create_diff_row_query(data
->merge
, data
->curview
, table
->name
, rec
);
1595 return ERROR_OUTOFMEMORY
;
1597 r
= MSI_DatabaseOpenViewW(data
->db
, query
, &dbview
);
1598 if (r
!= ERROR_SUCCESS
)
1601 r
= MSI_ViewExecute(dbview
, NULL
);
1602 if (r
!= ERROR_SUCCESS
)
1605 r
= MSI_ViewFetch(dbview
, &row
);
1606 if (r
== ERROR_SUCCESS
&& !MSI_RecordsAreEqual(rec
, row
))
1608 table
->numconflicts
++;
1611 else if (r
!= ERROR_NO_MORE_ITEMS
)
1617 mergerow
= msi_alloc(sizeof(MERGEROW
));
1620 r
= ERROR_OUTOFMEMORY
;
1624 mergerow
->data
= MSI_CloneRecord(rec
);
1625 if (!mergerow
->data
)
1627 r
= ERROR_OUTOFMEMORY
;
1632 list_add_tail(&table
->rows
, &mergerow
->entry
);
1636 msiobj_release(&row
->hdr
);
1637 msiobj_release(&dbview
->hdr
);
1641 static UINT
msi_get_table_labels(MSIDATABASE
*db
, LPCWSTR table
, LPWSTR
**labels
, DWORD
*numlabels
)
1644 MSIRECORD
*prec
= NULL
;
1646 r
= MSI_DatabaseGetPrimaryKeys(db
, table
, &prec
);
1647 if (r
!= ERROR_SUCCESS
)
1650 count
= MSI_RecordGetFieldCount(prec
);
1651 *numlabels
= count
+ 1;
1652 *labels
= msi_alloc((*numlabels
)*sizeof(LPWSTR
));
1655 r
= ERROR_OUTOFMEMORY
;
1659 (*labels
)[0] = strdupW(table
);
1660 for (i
=1; i
<=count
; i
++ )
1662 (*labels
)[i
] = strdupW(MSI_RecordGetString(prec
, i
));
1666 msiobj_release( &prec
->hdr
);
1670 static UINT
msi_get_query_columns(MSIQUERY
*query
, LPWSTR
**columns
, DWORD
*numcolumns
)
1673 MSIRECORD
*prec
= NULL
;
1675 r
= MSI_ViewGetColumnInfo(query
, MSICOLINFO_NAMES
, &prec
);
1676 if (r
!= ERROR_SUCCESS
)
1679 count
= MSI_RecordGetFieldCount(prec
);
1680 *columns
= msi_alloc(count
*sizeof(LPWSTR
));
1683 r
= ERROR_OUTOFMEMORY
;
1687 for (i
=1; i
<=count
; i
++ )
1689 (*columns
)[i
-1] = strdupW(MSI_RecordGetString(prec
, i
));
1692 *numcolumns
= count
;
1695 msiobj_release( &prec
->hdr
);
1699 static UINT
msi_get_query_types(MSIQUERY
*query
, LPWSTR
**types
, DWORD
*numtypes
)
1702 MSIRECORD
*prec
= NULL
;
1704 r
= MSI_ViewGetColumnInfo(query
, MSICOLINFO_TYPES
, &prec
);
1705 if (r
!= ERROR_SUCCESS
)
1708 count
= MSI_RecordGetFieldCount(prec
);
1709 *types
= msi_alloc(count
*sizeof(LPWSTR
));
1712 r
= ERROR_OUTOFMEMORY
;
1717 for (i
=1; i
<=count
; i
++ )
1719 (*types
)[i
-1] = strdupW(MSI_RecordGetString(prec
, i
));
1723 msiobj_release( &prec
->hdr
);
1727 static void merge_free_rows(MERGETABLE
*table
)
1729 struct list
*item
, *cursor
;
1731 LIST_FOR_EACH_SAFE(item
, cursor
, &table
->rows
)
1733 MERGEROW
*row
= LIST_ENTRY(item
, MERGEROW
, entry
);
1735 list_remove(&row
->entry
);
1736 msiobj_release(&row
->data
->hdr
);
1741 static void free_merge_table(MERGETABLE
*table
)
1745 if (table
->labels
!= NULL
)
1747 for (i
= 0; i
< table
->numlabels
; i
++)
1748 msi_free(table
->labels
[i
]);
1750 msi_free(table
->labels
);
1753 if (table
->columns
!= NULL
)
1755 for (i
= 0; i
< table
->numcolumns
; i
++)
1756 msi_free(table
->columns
[i
]);
1758 msi_free(table
->columns
);
1761 if (table
->types
!= NULL
)
1763 for (i
= 0; i
< table
->numtypes
; i
++)
1764 msi_free(table
->types
[i
]);
1766 msi_free(table
->types
);
1769 msi_free(table
->name
);
1770 merge_free_rows(table
);
1775 static UINT
msi_get_merge_table (MSIDATABASE
*db
, LPCWSTR name
, MERGETABLE
**ptable
)
1779 MSIQUERY
*mergeview
= NULL
;
1781 static const WCHAR query
[] = {'S','E','L','E','C','T',' ','*',' ',
1782 'F','R','O','M',' ','`','%','s','`',0};
1784 table
= msi_alloc_zero(sizeof(MERGETABLE
));
1788 return ERROR_OUTOFMEMORY
;
1791 r
= msi_get_table_labels(db
, name
, &table
->labels
, &table
->numlabels
);
1792 if (r
!= ERROR_SUCCESS
)
1795 r
= MSI_OpenQuery(db
, &mergeview
, query
, name
);
1796 if (r
!= ERROR_SUCCESS
)
1799 r
= msi_get_query_columns(mergeview
, &table
->columns
, &table
->numcolumns
);
1800 if (r
!= ERROR_SUCCESS
)
1803 r
= msi_get_query_types(mergeview
, &table
->types
, &table
->numtypes
);
1804 if (r
!= ERROR_SUCCESS
)
1807 list_init(&table
->rows
);
1809 table
->name
= strdupW(name
);
1810 table
->numconflicts
= 0;
1812 msiobj_release(&mergeview
->hdr
);
1814 return ERROR_SUCCESS
;
1817 msiobj_release(&mergeview
->hdr
);
1818 free_merge_table(table
);
1823 static UINT
merge_diff_tables(MSIRECORD
*rec
, LPVOID param
)
1825 MERGEDATA
*data
= param
;
1827 MSIQUERY
*dbview
= NULL
;
1828 MSIQUERY
*mergeview
= NULL
;
1832 static const WCHAR query
[] = {'S','E','L','E','C','T',' ','*',' ',
1833 'F','R','O','M',' ','`','%','s','`',0};
1835 name
= MSI_RecordGetString(rec
, 1);
1837 r
= MSI_OpenQuery(data
->merge
, &mergeview
, query
, name
);
1838 if (r
!= ERROR_SUCCESS
)
1841 if (TABLE_Exists(data
->db
, name
))
1843 r
= MSI_OpenQuery(data
->db
, &dbview
, query
, name
);
1844 if (r
!= ERROR_SUCCESS
)
1847 r
= merge_verify_colnames(dbview
, mergeview
);
1848 if (r
!= ERROR_SUCCESS
)
1851 r
= merge_verify_primary_keys(data
->db
, data
->merge
, name
);
1852 if (r
!= ERROR_SUCCESS
)
1856 r
= msi_get_merge_table(data
->merge
, name
, &table
);
1857 if (r
!= ERROR_SUCCESS
)
1860 data
->curtable
= table
;
1861 data
->curview
= mergeview
;
1862 r
= MSI_IterateRecords(mergeview
, NULL
, merge_diff_row
, data
);
1863 if (r
!= ERROR_SUCCESS
)
1865 free_merge_table(table
);
1869 list_add_tail(data
->tabledata
, &table
->entry
);
1872 msiobj_release(&dbview
->hdr
);
1873 msiobj_release(&mergeview
->hdr
);
1877 static UINT
gather_merge_data(MSIDATABASE
*db
, MSIDATABASE
*merge
,
1878 struct list
*tabledata
)
1880 static const WCHAR query
[] = {
1881 'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
1882 '`','_','T','a','b','l','e','s','`',0};
1887 r
= MSI_DatabaseOpenViewW(merge
, query
, &view
);
1888 if (r
!= ERROR_SUCCESS
)
1893 data
.tabledata
= tabledata
;
1894 r
= MSI_IterateRecords(view
, NULL
, merge_diff_tables
, &data
);
1895 msiobj_release(&view
->hdr
);
1899 static UINT
merge_table(MSIDATABASE
*db
, MERGETABLE
*table
)
1905 if (!TABLE_Exists(db
, table
->name
))
1907 r
= msi_add_table_to_db(db
, table
->columns
, table
->types
,
1908 table
->labels
, table
->numlabels
, table
->numcolumns
);
1909 if (r
!= ERROR_SUCCESS
)
1910 return ERROR_FUNCTION_FAILED
;
1913 LIST_FOR_EACH_ENTRY(row
, &table
->rows
, MERGEROW
, entry
)
1915 r
= TABLE_CreateView(db
, table
->name
, &tv
);
1916 if (r
!= ERROR_SUCCESS
)
1919 r
= tv
->ops
->insert_row(tv
, row
->data
, -1, FALSE
);
1920 tv
->ops
->delete(tv
);
1922 if (r
!= ERROR_SUCCESS
)
1926 return ERROR_SUCCESS
;
1929 static UINT
update_merge_errors(MSIDATABASE
*db
, LPCWSTR error
,
1930 LPWSTR table
, DWORD numconflicts
)
1935 static const WCHAR create
[] = {
1936 'C','R','E','A','T','E',' ','T','A','B','L','E',' ',
1937 '`','%','s','`',' ','(','`','T','a','b','l','e','`',' ',
1938 'C','H','A','R','(','2','5','5',')',' ','N','O','T',' ',
1939 'N','U','L','L',',',' ','`','N','u','m','R','o','w','M','e','r','g','e',
1940 'C','o','n','f','l','i','c','t','s','`',' ','S','H','O','R','T',' ',
1941 'N','O','T',' ','N','U','L','L',' ','P','R','I','M','A','R','Y',' ',
1942 'K','E','Y',' ','`','T','a','b','l','e','`',')',0};
1943 static const WCHAR insert
[] = {
1944 'I','N','S','E','R','T',' ','I','N','T','O',' ',
1945 '`','%','s','`',' ','(','`','T','a','b','l','e','`',',',' ',
1946 '`','N','u','m','R','o','w','M','e','r','g','e',
1947 'C','o','n','f','l','i','c','t','s','`',')',' ','V','A','L','U','E','S',
1948 ' ','(','\'','%','s','\'',',',' ','%','d',')',0};
1950 if (!TABLE_Exists(db
, error
))
1952 r
= MSI_OpenQuery(db
, &view
, create
, error
);
1953 if (r
!= ERROR_SUCCESS
)
1956 r
= MSI_ViewExecute(view
, NULL
);
1957 msiobj_release(&view
->hdr
);
1958 if (r
!= ERROR_SUCCESS
)
1962 r
= MSI_OpenQuery(db
, &view
, insert
, error
, table
, numconflicts
);
1963 if (r
!= ERROR_SUCCESS
)
1966 r
= MSI_ViewExecute(view
, NULL
);
1967 msiobj_release(&view
->hdr
);
1971 UINT WINAPI
MsiDatabaseMergeW(MSIHANDLE hDatabase
, MSIHANDLE hDatabaseMerge
,
1972 LPCWSTR szTableName
)
1974 struct list tabledata
= LIST_INIT(tabledata
);
1975 struct list
*item
, *cursor
;
1976 MSIDATABASE
*db
, *merge
;
1981 TRACE("(%d, %d, %s)\n", hDatabase
, hDatabaseMerge
,
1982 debugstr_w(szTableName
));
1984 if (szTableName
&& !*szTableName
)
1985 return ERROR_INVALID_TABLE
;
1987 db
= msihandle2msiinfo(hDatabase
, MSIHANDLETYPE_DATABASE
);
1988 merge
= msihandle2msiinfo(hDatabaseMerge
, MSIHANDLETYPE_DATABASE
);
1991 r
= ERROR_INVALID_HANDLE
;
1995 r
= gather_merge_data(db
, merge
, &tabledata
);
1996 if (r
!= ERROR_SUCCESS
)
2000 LIST_FOR_EACH_ENTRY(table
, &tabledata
, MERGETABLE
, entry
)
2002 if (table
->numconflicts
)
2006 r
= update_merge_errors(db
, szTableName
, table
->name
,
2007 table
->numconflicts
);
2008 if (r
!= ERROR_SUCCESS
)
2013 r
= merge_table(db
, table
);
2014 if (r
!= ERROR_SUCCESS
)
2019 LIST_FOR_EACH_SAFE(item
, cursor
, &tabledata
)
2021 MERGETABLE
*table
= LIST_ENTRY(item
, MERGETABLE
, entry
);
2022 list_remove(&table
->entry
);
2023 free_merge_table(table
);
2027 r
= ERROR_FUNCTION_FAILED
;
2030 msiobj_release(&db
->hdr
);
2031 msiobj_release(&merge
->hdr
);
2035 MSIDBSTATE WINAPI
MsiGetDatabaseState( MSIHANDLE handle
)
2037 MSIDBSTATE ret
= MSIDBSTATE_READ
;
2040 TRACE("%d\n", handle
);
2042 db
= msihandle2msiinfo( handle
, MSIHANDLETYPE_DATABASE
);
2045 IWineMsiRemoteDatabase
*remote_database
;
2047 remote_database
= (IWineMsiRemoteDatabase
*)msi_get_remote( handle
);
2048 if ( !remote_database
)
2049 return MSIDBSTATE_ERROR
;
2051 IWineMsiRemoteDatabase_Release( remote_database
);
2052 WARN("MsiGetDatabaseState not allowed during a custom action!\n");
2054 return MSIDBSTATE_READ
;
2057 if (db
->mode
!= MSIDBOPEN_READONLY
)
2058 ret
= MSIDBSTATE_WRITE
;
2059 msiobj_release( &db
->hdr
);
2064 typedef struct _msi_remote_database_impl
{
2065 IWineMsiRemoteDatabase IWineMsiRemoteDatabase_iface
;
2068 } msi_remote_database_impl
;
2070 static inline msi_remote_database_impl
*impl_from_IWineMsiRemoteDatabase( IWineMsiRemoteDatabase
*iface
)
2072 return CONTAINING_RECORD(iface
, msi_remote_database_impl
, IWineMsiRemoteDatabase_iface
);
2075 static HRESULT WINAPI
mrd_QueryInterface( IWineMsiRemoteDatabase
*iface
,
2076 REFIID riid
,LPVOID
*ppobj
)
2078 if( IsEqualCLSID( riid
, &IID_IUnknown
) ||
2079 IsEqualCLSID( riid
, &IID_IWineMsiRemoteDatabase
) )
2081 IUnknown_AddRef( iface
);
2086 return E_NOINTERFACE
;
2089 static ULONG WINAPI
mrd_AddRef( IWineMsiRemoteDatabase
*iface
)
2091 msi_remote_database_impl
* This
= impl_from_IWineMsiRemoteDatabase( iface
);
2093 return InterlockedIncrement( &This
->refs
);
2096 static ULONG WINAPI
mrd_Release( IWineMsiRemoteDatabase
*iface
)
2098 msi_remote_database_impl
* This
= impl_from_IWineMsiRemoteDatabase( iface
);
2101 r
= InterlockedDecrement( &This
->refs
);
2104 MsiCloseHandle( This
->database
);
2110 static HRESULT WINAPI
mrd_IsTablePersistent( IWineMsiRemoteDatabase
*iface
,
2111 LPCWSTR table
, MSICONDITION
*persistent
)
2113 msi_remote_database_impl
*This
= impl_from_IWineMsiRemoteDatabase( iface
);
2114 *persistent
= MsiDatabaseIsTablePersistentW(This
->database
, table
);
2118 static HRESULT WINAPI
mrd_GetPrimaryKeys( IWineMsiRemoteDatabase
*iface
,
2119 LPCWSTR table
, MSIHANDLE
*keys
)
2121 msi_remote_database_impl
*This
= impl_from_IWineMsiRemoteDatabase( iface
);
2122 UINT r
= MsiDatabaseGetPrimaryKeysW(This
->database
, table
, keys
);
2123 return HRESULT_FROM_WIN32(r
);
2126 static HRESULT WINAPI
mrd_GetSummaryInformation( IWineMsiRemoteDatabase
*iface
,
2127 UINT updatecount
, MSIHANDLE
*suminfo
)
2129 msi_remote_database_impl
*This
= impl_from_IWineMsiRemoteDatabase( iface
);
2130 UINT r
= MsiGetSummaryInformationW(This
->database
, NULL
, updatecount
, suminfo
);
2131 return HRESULT_FROM_WIN32(r
);
2134 static HRESULT WINAPI
mrd_OpenView( IWineMsiRemoteDatabase
*iface
,
2135 LPCWSTR query
, MSIHANDLE
*view
)
2137 msi_remote_database_impl
*This
= impl_from_IWineMsiRemoteDatabase( iface
);
2138 UINT r
= MsiDatabaseOpenViewW(This
->database
, query
, view
);
2139 return HRESULT_FROM_WIN32(r
);
2142 static HRESULT WINAPI
mrd_SetMsiHandle( IWineMsiRemoteDatabase
*iface
, MSIHANDLE handle
)
2144 msi_remote_database_impl
* This
= impl_from_IWineMsiRemoteDatabase( iface
);
2145 This
->database
= handle
;
2149 static const IWineMsiRemoteDatabaseVtbl msi_remote_database_vtbl
=
2154 mrd_IsTablePersistent
,
2156 mrd_GetSummaryInformation
,
2161 HRESULT
create_msi_remote_database( IUnknown
*pOuter
, LPVOID
*ppObj
)
2163 msi_remote_database_impl
*This
;
2165 This
= msi_alloc( sizeof *This
);
2167 return E_OUTOFMEMORY
;
2169 This
->IWineMsiRemoteDatabase_iface
.lpVtbl
= &msi_remote_database_vtbl
;