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;
63 free_cached_tables( db );
64 msi_free_transforms( db );
65 msi_destroy_stringtable( db->strings );
66 r = IStorage_Release( db->storage );
68 ERR("database reference count was not zero (%ld)\n", r);
71 DeleteFileW( db->deletefile );
72 msi_free( db->deletefile );
76 UINT MSI_OpenDatabaseW(LPCWSTR szDBPath, LPCWSTR szPersist, MSIDATABASE **pdb)
80 MSIDATABASE *db = NULL;
81 UINT ret = ERROR_FUNCTION_FAILED;
82 LPCWSTR szMode, save_path;
87 static const WCHAR backslash[] = {'\\',0};
89 TRACE("%s %s\n",debugstr_w(szDBPath),debugstr_w(szPersist) );
92 return ERROR_INVALID_PARAMETER;
96 if( HIWORD( szPersist ) )
98 if (!CopyFileW( szDBPath, szPersist, FALSE ))
99 return ERROR_OPEN_FAILED;
101 szDBPath = szPersist;
102 szPersist = MSIDBOPEN_TRANSACT;
106 if( szPersist == MSIDBOPEN_READONLY )
108 r = StgOpenStorage( szDBPath, NULL,
109 STGM_DIRECT|STGM_READ|STGM_SHARE_DENY_WRITE, NULL, 0, &stg);
111 else if( szPersist == MSIDBOPEN_CREATE || szPersist == MSIDBOPEN_CREATEDIRECT )
113 /* FIXME: MSIDBOPEN_CREATE should case STGM_TRANSACTED flag to be
115 r = StgCreateDocfile( szDBPath,
116 STGM_CREATE|STGM_DIRECT|STGM_READWRITE|STGM_SHARE_EXCLUSIVE, 0, &stg);
117 if( r == ERROR_SUCCESS )
119 IStorage_SetClass( stg, &CLSID_MsiDatabase );
120 r = init_string_table( stg );
124 else if( szPersist == MSIDBOPEN_TRANSACT )
126 /* FIXME: MSIDBOPEN_TRANSACT should case STGM_TRANSACTED flag to be
128 r = StgOpenStorage( szDBPath, NULL,
129 STGM_DIRECT|STGM_READWRITE|STGM_SHARE_EXCLUSIVE, NULL, 0, &stg);
131 else if( szPersist == MSIDBOPEN_DIRECT )
133 r = StgOpenStorage( szDBPath, NULL,
134 STGM_DIRECT|STGM_READWRITE|STGM_SHARE_EXCLUSIVE, NULL, 0, &stg);
138 ERR("unknown flag %p\n",szPersist);
139 return ERROR_INVALID_PARAMETER;
144 FIXME("open failed r = %08lx!\n",r);
145 return ERROR_FUNCTION_FAILED;
148 r = IStorage_Stat( stg, &stat, STATFLAG_NONAME );
151 FIXME("Failed to stat storage\n");
155 if ( !IsEqualGUID( &stat.clsid, &CLSID_MsiDatabase ) &&
156 !IsEqualGUID( &stat.clsid, &CLSID_MsiPatch ) )
158 ERR("storage GUID is not a MSI database GUID %s\n",
159 debugstr_guid(&stat.clsid) );
163 db = alloc_msiobject( MSIHANDLETYPE_DATABASE, sizeof (MSIDATABASE),
167 FIXME("Failed to allocate a handle\n");
171 if (!strchrW( save_path, '\\' ))
173 GetCurrentDirectoryW( MAX_PATH, path );
174 lstrcatW( path, backslash );
175 lstrcatW( path, save_path );
178 lstrcpyW( path, save_path );
180 db->path = strdupW( path );
182 if( TRACE_ON( msi ) )
183 enum_stream_names( stg );
188 db->deletefile = strdupW( szDBPath );
190 db->deletefile = NULL;
191 list_init( &db->tables );
192 list_init( &db->transforms );
194 db->strings = load_string_table( stg );
200 msiobj_addref( &db->hdr );
201 IStorage_AddRef( stg );
206 msiobj_release( &db->hdr );
208 IStorage_Release( stg );
213 UINT WINAPI MsiOpenDatabaseW(LPCWSTR szDBPath, LPCWSTR szPersist, MSIHANDLE *phDB)
218 TRACE("%s %s %p\n",debugstr_w(szDBPath),debugstr_w(szPersist), phDB);
220 ret = MSI_OpenDatabaseW( szDBPath, szPersist, &db );
221 if( ret == ERROR_SUCCESS )
223 *phDB = alloc_msihandle( &db->hdr );
225 ret = ERROR_NOT_ENOUGH_MEMORY;
226 msiobj_release( &db->hdr );
232 UINT WINAPI MsiOpenDatabaseA(LPCSTR szDBPath, LPCSTR szPersist, MSIHANDLE *phDB)
234 HRESULT r = ERROR_FUNCTION_FAILED;
235 LPWSTR szwDBPath = NULL, szwPersist = NULL;
237 TRACE("%s %s %p\n", debugstr_a(szDBPath), debugstr_a(szPersist), phDB);
241 szwDBPath = strdupAtoW( szDBPath );
246 if( HIWORD(szPersist) )
248 szwPersist = strdupAtoW( szPersist );
253 szwPersist = (LPWSTR)(DWORD_PTR)szPersist;
255 r = MsiOpenDatabaseW( szwDBPath, szwPersist, phDB );
258 if( HIWORD(szPersist) )
259 msi_free( szwPersist );
260 msi_free( szwDBPath );
265 UINT MSI_DatabaseImport( MSIDATABASE *db, LPCWSTR folder, LPCWSTR file )
267 FIXME("%p %s %s\n", db, debugstr_w(folder), debugstr_w(file) );
269 if( folder == NULL || file == NULL )
270 return ERROR_INVALID_PARAMETER;
272 return ERROR_CALL_NOT_IMPLEMENTED;
275 UINT WINAPI MsiDatabaseImportW(MSIHANDLE handle, LPCWSTR szFolder, LPCWSTR szFilename)
280 TRACE("%lx %s %s\n",handle,debugstr_w(szFolder), debugstr_w(szFilename));
282 db = msihandle2msiinfo( handle, MSIHANDLETYPE_DATABASE );
284 return ERROR_INVALID_HANDLE;
285 r = MSI_DatabaseImport( db, szFolder, szFilename );
286 msiobj_release( &db->hdr );
290 UINT WINAPI MsiDatabaseImportA( MSIHANDLE handle,
291 LPCSTR szFolder, LPCSTR szFilename )
293 LPWSTR path = NULL, file = NULL;
294 UINT r = ERROR_OUTOFMEMORY;
296 TRACE("%lx %s %s\n", handle, debugstr_a(szFolder), debugstr_a(szFilename));
300 path = strdupAtoW( szFolder );
307 file = strdupAtoW( szFilename );
312 r = MsiDatabaseImportW( handle, path, file );
321 static UINT msi_export_record( HANDLE handle, MSIRECORD *row, UINT start )
323 UINT i, count, len, r = ERROR_SUCCESS;
329 buffer = msi_alloc( len );
331 return ERROR_OUTOFMEMORY;
333 count = MSI_RecordGetFieldCount( row );
334 for ( i=start; i<=count; i++ )
337 r = MSI_RecordGetStringA( row, i, buffer, &sz );
338 if (r == ERROR_MORE_DATA)
340 char *p = msi_realloc( buffer, sz + 1 );
347 r = MSI_RecordGetStringA( row, i, buffer, &sz );
348 if (r != ERROR_SUCCESS)
351 if (!WriteFile( handle, buffer, sz, &sz, NULL ))
353 r = ERROR_FUNCTION_FAILED;
357 sep = (i < count) ? "\t" : "\r\n";
358 if (!WriteFile( handle, sep, strlen(sep), &sz, NULL ))
360 r = ERROR_FUNCTION_FAILED;
368 static UINT msi_export_row( MSIRECORD *row, void *arg )
370 return msi_export_record( arg, row, 1 );
373 UINT MSI_DatabaseExport( MSIDATABASE *db, LPCWSTR table,
374 LPCWSTR folder, LPCWSTR file )
376 static const WCHAR query[] = {
377 's','e','l','e','c','t',' ','*',' ','f','r','o','m',' ','%','s',0 };
378 static const WCHAR szbs[] = { '\\', 0 };
379 MSIRECORD *rec = NULL;
380 MSIQUERY *view = NULL;
385 TRACE("%p %s %s %s\n", db, debugstr_w(table),
386 debugstr_w(folder), debugstr_w(file) );
388 if( folder == NULL || file == NULL )
389 return ERROR_INVALID_PARAMETER;
391 len = lstrlenW(folder) + lstrlenW(file) + 2;
392 filename = msi_alloc(len * sizeof (WCHAR));
394 return ERROR_OUTOFMEMORY;
396 lstrcpyW( filename, folder );
397 lstrcatW( filename, szbs );
398 lstrcatW( filename, file );
400 handle = CreateFileW( filename, GENERIC_READ | GENERIC_WRITE, 0,
401 NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL );
402 msi_free( filename );
403 if (handle == INVALID_HANDLE_VALUE)
404 return ERROR_FUNCTION_FAILED;
406 r = MSI_OpenQuery( db, &view, query, table );
407 if (r == ERROR_SUCCESS)
409 /* write out row 1, the column names */
410 r = MSI_ViewGetColumnInfo(view, MSICOLINFO_NAMES, &rec);
411 if (r == ERROR_SUCCESS)
413 msi_export_record( handle, rec, 1 );
414 msiobj_release( &rec->hdr );
417 /* write out row 2, the column types */
418 r = MSI_ViewGetColumnInfo(view, MSICOLINFO_TYPES, &rec);
419 if (r == ERROR_SUCCESS)
421 msi_export_record( handle, rec, 1 );
422 msiobj_release( &rec->hdr );
425 /* write out row 3, the table name + keys */
426 r = MSI_DatabaseGetPrimaryKeys( db, table, &rec );
427 if (r == ERROR_SUCCESS)
429 MSI_RecordSetStringW( rec, 0, table );
430 msi_export_record( handle, rec, 0 );
431 msiobj_release( &rec->hdr );
434 /* write out row 4 onwards, the data */
435 r = MSI_IterateRecords( view, 0, msi_export_row, handle );
436 msiobj_release( &view->hdr );
439 CloseHandle( handle );
444 /***********************************************************************
445 * MsiExportDatabaseW [MSI.@]
447 * Writes a file containing the table data as tab separated ASCII.
449 * The format is as follows:
451 * row1 : colname1 <tab> colname2 <tab> .... colnameN <cr> <lf>
452 * row2 : coltype1 <tab> coltype2 <tab> .... coltypeN <cr> <lf>
453 * row3 : tablename <tab> key1 <tab> key2 <tab> ... keyM <cr> <lf>
455 * Followed by the data, starting at row 1 with one row per line
457 * row4 : data <tab> data <tab> data <tab> ... data <cr> <lf>
459 UINT WINAPI MsiDatabaseExportW( MSIHANDLE handle, LPCWSTR szTable,
460 LPCWSTR szFolder, LPCWSTR szFilename )
465 TRACE("%lx %s %s %s\n", handle, debugstr_w(szTable),
466 debugstr_w(szFolder), debugstr_w(szFilename));
468 db = msihandle2msiinfo( handle, MSIHANDLETYPE_DATABASE );
470 return ERROR_INVALID_HANDLE;
471 r = MSI_DatabaseExport( db, szTable, szFolder, szFilename );
472 msiobj_release( &db->hdr );
476 UINT WINAPI MsiDatabaseExportA( MSIHANDLE handle, LPCSTR szTable,
477 LPCSTR szFolder, LPCSTR szFilename )
479 LPWSTR path = NULL, file = NULL, table = NULL;
480 UINT r = ERROR_OUTOFMEMORY;
482 TRACE("%lx %s %s %s\n", handle, debugstr_a(szTable),
483 debugstr_a(szFolder), debugstr_a(szFilename));
487 table = strdupAtoW( szTable );
494 path = strdupAtoW( szFolder );
501 file = strdupAtoW( szFilename );
506 r = MsiDatabaseExportW( handle, table, path, file );
516 MSIDBSTATE WINAPI MsiGetDatabaseState( MSIHANDLE handle )
518 MSIDBSTATE ret = MSIDBSTATE_READ;
521 TRACE("%ld\n", handle);
523 db = msihandle2msiinfo( handle, MSIHANDLETYPE_DATABASE );
525 return MSIDBSTATE_ERROR;
526 if (db->mode != MSIDBOPEN_READONLY )
527 ret = MSIDBSTATE_WRITE;
528 msiobj_release( &db->hdr );