push 014043c4937c940c54cd1214c96e33a3b3c8cf7d
[wine/hacks.git] / dlls / msi / database.c
blob875d2d0b681e80175bd1653cc15670e93fce6df7
1 /*
2 * Implementation of the Microsoft Installer (msi.dll)
4 * Copyright 2002,2003,2004,2005 Mike McCormack for CodeWeavers
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #include <stdarg.h>
23 #define COBJMACROS
24 #define NONAMELESSUNION
26 #include "windef.h"
27 #include "winbase.h"
28 #include "winreg.h"
29 #include "winnls.h"
30 #include "wine/debug.h"
31 #include "wine/unicode.h"
32 #include "msi.h"
33 #include "msiquery.h"
34 #include "msipriv.h"
35 #include "objidl.h"
36 #include "objbase.h"
37 #include "msiserver.h"
39 #include "initguid.h"
41 WINE_DEFAULT_DEBUG_CHANNEL(msi);
43 DEFINE_GUID( CLSID_MsiDatabase, 0x000c1084, 0x0000, 0x0000,
44 0xc0,0x00,0x00,0x00,0x00,0x00,0x00,0x46);
45 DEFINE_GUID( CLSID_MsiPatch, 0x000c1086, 0x0000, 0x0000,
46 0xc0,0x00,0x00,0x00,0x00,0x00,0x00,0x46);
49 * .MSI file format
51 * An .msi file is a structured storage file.
52 * It contains a number of streams.
53 * A stream for each table in the database.
54 * Two streams for the string table in the database.
55 * Any binary data in a table is a reference to a stream.
58 static VOID MSI_CloseDatabase( MSIOBJECTHDR *arg )
60 MSIDATABASE *db = (MSIDATABASE *) arg;
62 msi_free(db->path);
63 free_cached_tables( db );
64 msi_free_transforms( db );
65 msi_destroy_stringtable( db->strings );
66 IStorage_Release( db->storage );
67 if (db->deletefile)
69 DeleteFileW( db->deletefile );
70 msi_free( db->deletefile );
74 UINT MSI_OpenDatabaseW(LPCWSTR szDBPath, LPCWSTR szPersist, MSIDATABASE **pdb)
76 IStorage *stg = NULL;
77 HRESULT r;
78 MSIDATABASE *db = NULL;
79 UINT ret = ERROR_FUNCTION_FAILED;
80 LPCWSTR szMode, save_path;
81 STATSTG stat;
82 BOOL created = FALSE;
83 WCHAR path[MAX_PATH];
85 static const WCHAR backslash[] = {'\\',0};
86 static WCHAR szTables[] = { '_','T','a','b','l','e','s',0 };
88 TRACE("%s %s\n",debugstr_w(szDBPath),debugstr_w(szPersist) );
90 if( !pdb )
91 return ERROR_INVALID_PARAMETER;
93 save_path = szDBPath;
94 szMode = szPersist;
95 if( HIWORD( szPersist ) )
97 if (!CopyFileW( szDBPath, szPersist, FALSE ))
98 return ERROR_OPEN_FAILED;
100 szDBPath = szPersist;
101 szPersist = MSIDBOPEN_TRANSACT;
102 created = TRUE;
105 if( szPersist == MSIDBOPEN_READONLY )
107 r = StgOpenStorage( szDBPath, NULL,
108 STGM_DIRECT|STGM_READ|STGM_SHARE_DENY_WRITE, NULL, 0, &stg);
110 else if( szPersist == MSIDBOPEN_CREATE || szPersist == MSIDBOPEN_CREATEDIRECT )
112 /* FIXME: MSIDBOPEN_CREATE should case STGM_TRANSACTED flag to be
113 * used here: */
114 r = StgCreateDocfile( szDBPath,
115 STGM_CREATE|STGM_DIRECT|STGM_READWRITE|STGM_SHARE_EXCLUSIVE, 0, &stg);
116 if( r == ERROR_SUCCESS )
118 IStorage_SetClass( stg, &CLSID_MsiDatabase );
119 /* create the _Tables stream */
120 r = write_stream_data(stg, szTables, NULL, 0, TRUE);
121 if (!FAILED(r))
122 r = msi_init_string_table( stg );
124 created = TRUE;
126 else if( szPersist == MSIDBOPEN_TRANSACT )
128 /* FIXME: MSIDBOPEN_TRANSACT should case STGM_TRANSACTED flag to be
129 * used here: */
130 r = StgOpenStorage( szDBPath, NULL,
131 STGM_DIRECT|STGM_READWRITE|STGM_SHARE_EXCLUSIVE, NULL, 0, &stg);
133 else if( szPersist == MSIDBOPEN_DIRECT )
135 r = StgOpenStorage( szDBPath, NULL,
136 STGM_DIRECT|STGM_READWRITE|STGM_SHARE_EXCLUSIVE, NULL, 0, &stg);
138 else
140 ERR("unknown flag %p\n",szPersist);
141 return ERROR_INVALID_PARAMETER;
144 if( FAILED( r ) || !stg )
146 FIXME("open failed r = %08x for %s\n", r, debugstr_w(szDBPath));
147 return ERROR_FUNCTION_FAILED;
150 r = IStorage_Stat( stg, &stat, STATFLAG_NONAME );
151 if( FAILED( r ) )
153 FIXME("Failed to stat storage\n");
154 goto end;
157 if ( !IsEqualGUID( &stat.clsid, &CLSID_MsiDatabase ) &&
158 !IsEqualGUID( &stat.clsid, &CLSID_MsiPatch ) )
160 ERR("storage GUID is not a MSI database GUID %s\n",
161 debugstr_guid(&stat.clsid) );
162 goto end;
165 db = alloc_msiobject( MSIHANDLETYPE_DATABASE, sizeof (MSIDATABASE),
166 MSI_CloseDatabase );
167 if( !db )
169 FIXME("Failed to allocate a handle\n");
170 goto end;
173 if (!strchrW( save_path, '\\' ))
175 GetCurrentDirectoryW( MAX_PATH, path );
176 lstrcatW( path, backslash );
177 lstrcatW( path, save_path );
179 else
180 lstrcpyW( path, save_path );
182 db->path = strdupW( path );
184 if( TRACE_ON( msi ) )
185 enum_stream_names( stg );
187 db->storage = stg;
188 db->mode = szMode;
189 if (created)
190 db->deletefile = strdupW( szDBPath );
191 else
192 db->deletefile = NULL;
193 list_init( &db->tables );
194 list_init( &db->transforms );
196 db->strings = msi_load_string_table( stg, &db->bytes_per_strref );
197 if( !db->strings )
198 goto end;
200 msi_table_set_strref( db->bytes_per_strref );
201 ret = ERROR_SUCCESS;
203 msiobj_addref( &db->hdr );
204 IStorage_AddRef( stg );
205 *pdb = db;
207 end:
208 if( db )
209 msiobj_release( &db->hdr );
210 if( stg )
211 IStorage_Release( stg );
213 return ret;
216 UINT WINAPI MsiOpenDatabaseW(LPCWSTR szDBPath, LPCWSTR szPersist, MSIHANDLE *phDB)
218 MSIDATABASE *db;
219 UINT ret;
221 TRACE("%s %s %p\n",debugstr_w(szDBPath),debugstr_w(szPersist), phDB);
223 ret = MSI_OpenDatabaseW( szDBPath, szPersist, &db );
224 if( ret == ERROR_SUCCESS )
226 *phDB = alloc_msihandle( &db->hdr );
227 if (! *phDB)
228 ret = ERROR_NOT_ENOUGH_MEMORY;
229 msiobj_release( &db->hdr );
232 return ret;
235 UINT WINAPI MsiOpenDatabaseA(LPCSTR szDBPath, LPCSTR szPersist, MSIHANDLE *phDB)
237 HRESULT r = ERROR_FUNCTION_FAILED;
238 LPWSTR szwDBPath = NULL, szwPersist = NULL;
240 TRACE("%s %s %p\n", debugstr_a(szDBPath), debugstr_a(szPersist), phDB);
242 if( szDBPath )
244 szwDBPath = strdupAtoW( szDBPath );
245 if( !szwDBPath )
246 goto end;
249 if( HIWORD(szPersist) )
251 szwPersist = strdupAtoW( szPersist );
252 if( !szwPersist )
253 goto end;
255 else
256 szwPersist = (LPWSTR)(DWORD_PTR)szPersist;
258 r = MsiOpenDatabaseW( szwDBPath, szwPersist, phDB );
260 end:
261 if( HIWORD(szPersist) )
262 msi_free( szwPersist );
263 msi_free( szwDBPath );
265 return r;
268 static LPWSTR msi_read_text_archive(LPCWSTR path)
270 HANDLE file;
271 LPSTR data = NULL;
272 LPWSTR wdata = NULL;
273 DWORD read, size = 0;
275 file = CreateFileW( path, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL );
276 if (file == INVALID_HANDLE_VALUE)
277 return NULL;
279 size = GetFileSize( file, NULL );
280 data = msi_alloc( size + 1 );
281 if (!data)
282 goto done;
284 if (!ReadFile( file, data, size, &read, NULL ))
285 goto done;
287 data[size] = '\0';
288 wdata = strdupAtoW( data );
290 done:
291 CloseHandle( file );
292 msi_free( data );
293 return wdata;
296 static void msi_parse_line(LPWSTR *line, LPWSTR **entries, DWORD *num_entries)
298 LPWSTR ptr = *line, save;
299 DWORD i, count = 1;
301 *entries = NULL;
303 /* stay on this line */
304 while (*ptr && *ptr != '\n')
306 /* entries are separated by tabs */
307 if (*ptr == '\t')
308 count++;
310 ptr++;
313 *entries = msi_alloc(count * sizeof(LPWSTR));
314 if (!*entries)
315 return;
317 /* store pointers into the data */
318 for (i = 0, ptr = *line; i < count; i++)
320 while (*ptr && *ptr == '\r') ptr++;
321 save = ptr;
323 while (*ptr && *ptr != '\t' && *ptr != '\n' && *ptr != '\r') ptr++;
325 /* NULL-separate the data */
326 if (*ptr == '\n' || *ptr == '\r')
328 while (*ptr == '\n' || *ptr == '\r')
329 *(ptr++) = '\0';
331 else if (*ptr)
332 *ptr++ = '\0';
334 (*entries)[i] = save;
337 /* move to the next line if there's more, else EOF */
338 *line = ptr;
340 if (num_entries)
341 *num_entries = count;
344 static LPWSTR msi_build_createsql_prelude(LPWSTR table)
346 LPWSTR prelude;
347 DWORD size;
349 static const WCHAR create_fmt[] = {'C','R','E','A','T','E',' ','T','A','B','L','E',' ','`','%','s','`',' ','(',' ',0};
351 size = sizeof(create_fmt)/sizeof(create_fmt[0]) + lstrlenW(table) - 2;
352 prelude = msi_alloc(size * sizeof(WCHAR));
353 if (!prelude)
354 return NULL;
356 sprintfW(prelude, create_fmt, table);
357 return prelude;
360 static LPWSTR msi_build_createsql_columns(LPWSTR *columns_data, LPWSTR *types, DWORD num_columns)
362 LPWSTR columns, p;
363 LPCWSTR type;
364 DWORD sql_size = 1, i, len;
365 WCHAR expanded[128], *ptr;
366 WCHAR size[10], comma[2], extra[30];
368 static const WCHAR column_fmt[] = {'`','%','s','`',' ','%','s','%','s','%','s','%','s',' ',0};
369 static const WCHAR size_fmt[] = {'(','%','s',')',0};
370 static const WCHAR type_char[] = {'C','H','A','R',0};
371 static const WCHAR type_int[] = {'I','N','T',0};
372 static const WCHAR type_long[] = {'L','O','N','G',0};
373 static const WCHAR type_notnull[] = {' ','N','O','T',' ','N','U','L','L',0};
374 static const WCHAR localizable[] = {' ','L','O','C','A','L','I','Z','A','B','L','E',0};
376 columns = msi_alloc_zero(sql_size * sizeof(WCHAR));
377 if (!columns)
378 return NULL;
380 for (i = 0; i < num_columns; i++)
382 type = NULL;
383 comma[1] = size[0] = extra[0] = '\0';
385 if (i == num_columns - 1)
386 comma[0] = '\0';
387 else
388 comma[0] = ',';
390 ptr = &types[i][1];
391 len = atolW(ptr);
392 extra[0] = '\0';
394 switch (types[i][0])
396 case 'l':
397 lstrcpyW(extra, type_notnull);
398 case 'L':
399 lstrcatW(extra, localizable);
400 type = type_char;
401 sprintfW(size, size_fmt, ptr);
402 break;
403 case 's':
404 lstrcpyW(extra, type_notnull);
405 case 'S':
406 type = type_char;
407 sprintfW(size, size_fmt, ptr);
408 break;
409 case 'i':
410 lstrcpyW(extra, type_notnull);
411 case 'I':
412 if (len == 2)
413 type = type_int;
414 else
415 type = type_long;
416 break;
417 default:
418 ERR("Unknown type: %c\n", types[i][0]);
419 msi_free(columns);
420 return NULL;
423 sprintfW(expanded, column_fmt, columns_data[i], type, size, extra, comma);
424 sql_size += lstrlenW(expanded);
426 p = msi_realloc(columns, sql_size * sizeof(WCHAR));
427 if (!p)
429 msi_free(columns);
430 return NULL;
432 columns = p;
434 lstrcatW(columns, expanded);
437 return columns;
440 static LPWSTR msi_build_createsql_postlude(LPWSTR *primary_keys, DWORD num_keys)
442 LPWSTR postlude, keys, ptr;
443 DWORD size, key_size, i;
445 static const WCHAR key_fmt[] = {'`','%','s','`',',',' ',0};
446 static const WCHAR postlude_fmt[] = {'P','R','I','M','A','R','Y',' ','K','E','Y',' ','%','s',')',0};
448 for (i = 0, size = 1; i < num_keys; i++)
449 size += lstrlenW(key_fmt) + lstrlenW(primary_keys[i]) - 2;
451 keys = msi_alloc(size * sizeof(WCHAR));
452 if (!keys)
453 return NULL;
455 for (i = 0, ptr = keys; i < num_keys; i++)
457 key_size = lstrlenW(key_fmt) + lstrlenW(primary_keys[i]) -2;
458 sprintfW(ptr, key_fmt, primary_keys[i]);
459 ptr += key_size;
462 /* remove final ', ' */
463 *(ptr - 2) = '\0';
465 size = lstrlenW(postlude_fmt) + size - 1;
466 postlude = msi_alloc(size * sizeof(WCHAR));
467 if (!postlude)
468 goto done;
470 sprintfW(postlude, postlude_fmt, keys);
472 done:
473 msi_free(keys);
474 return postlude;
477 static UINT msi_add_table_to_db(MSIDATABASE *db, LPWSTR *columns, LPWSTR *types, LPWSTR *labels, DWORD num_labels, DWORD num_columns)
479 UINT r;
480 DWORD size;
481 MSIQUERY *view;
482 LPWSTR create_sql;
483 LPWSTR prelude, columns_sql, postlude;
485 prelude = msi_build_createsql_prelude(labels[0]);
486 columns_sql = msi_build_createsql_columns(columns, types, num_columns);
487 postlude = msi_build_createsql_postlude(labels + 1, num_labels - 1); /* skip over table name */
489 if (!prelude || !columns_sql || !postlude)
490 return ERROR_OUTOFMEMORY;
492 size = lstrlenW(prelude) + lstrlenW(columns_sql) + lstrlenW(postlude) + 1;
493 create_sql = msi_alloc(size * sizeof(WCHAR));
494 if (!create_sql)
495 return ERROR_OUTOFMEMORY;
497 lstrcpyW(create_sql, prelude);
498 lstrcatW(create_sql, columns_sql);
499 lstrcatW(create_sql, postlude);
501 msi_free(prelude);
502 msi_free(columns_sql);
503 msi_free(postlude);
505 r = MSI_DatabaseOpenViewW( db, create_sql, &view );
506 msi_free(create_sql);
508 if (r != ERROR_SUCCESS)
509 return r;
511 r = MSI_ViewExecute(view, NULL);
512 MSI_ViewClose(view);
513 msiobj_release(&view->hdr);
515 return r;
518 static UINT construct_record(DWORD num_columns, LPWSTR *types,
519 LPWSTR *data, MSIRECORD **rec)
521 UINT i;
523 *rec = MSI_CreateRecord(num_columns);
524 if (!*rec)
525 return ERROR_OUTOFMEMORY;
527 for (i = 0; i < num_columns; i++)
529 switch (types[i][0])
531 case 'L': case 'l': case 'S': case 's':
532 MSI_RecordSetStringW(*rec, i + 1, data[i]);
533 break;
534 case 'I': case 'i':
535 if (*data[i])
536 MSI_RecordSetInteger(*rec, i + 1, atoiW(data[i]));
537 break;
538 default:
539 ERR("Unhandled column type: %c\n", types[i][0]);
540 msiobj_release(&(*rec)->hdr);
541 return ERROR_FUNCTION_FAILED;
545 return ERROR_SUCCESS;
548 static UINT msi_add_records_to_table(MSIDATABASE *db, LPWSTR *columns, LPWSTR *types,
549 LPWSTR *labels, LPWSTR **records,
550 int num_columns, int num_records)
552 UINT r;
553 DWORD i, size;
554 MSIQUERY *view;
555 MSIRECORD *rec;
556 LPWSTR query;
558 static const WCHAR select[] = {
559 'S','E','L','E','C','T',' ','*',' ',
560 'F','R','O','M',' ','`','%','s','`',0
563 size = lstrlenW(select) + lstrlenW(labels[0]) - 1;
564 query = msi_alloc(size * sizeof(WCHAR));
565 if (!query)
566 return ERROR_OUTOFMEMORY;
568 sprintfW(query, select, labels[0]);
570 r = MSI_DatabaseOpenViewW(db, query, &view);
571 msi_free(query);
572 if (r != ERROR_SUCCESS)
573 return r;
575 while (MSI_ViewFetch(view, &rec) != ERROR_NO_MORE_ITEMS)
577 r = MSI_ViewModify(view, MSIMODIFY_DELETE, rec);
578 if (r != ERROR_SUCCESS)
579 goto done;
582 for (i = 0; i < num_records; i++)
584 r = construct_record(num_columns, types, records[i], &rec);
585 if (r != ERROR_SUCCESS)
586 goto done;
588 r = MSI_ViewModify(view, MSIMODIFY_INSERT, rec);
589 if (r != ERROR_SUCCESS)
591 msiobj_release(&rec->hdr);
592 goto done;
595 msiobj_release(&rec->hdr);
598 done:
599 msiobj_release(&view->hdr);
600 return r;
603 UINT MSI_DatabaseImport(MSIDATABASE *db, LPCWSTR folder, LPCWSTR file)
605 UINT r;
606 DWORD len, i;
607 DWORD num_labels, num_types;
608 DWORD num_columns, num_records = 0;
609 LPWSTR *columns, *types, *labels;
610 LPWSTR path, ptr, data;
611 LPWSTR **records = NULL;
612 LPWSTR **temp_records;
614 static const WCHAR backslash[] = {'\\',0};
616 TRACE("%p %s %s\n", db, debugstr_w(folder), debugstr_w(file) );
618 if( folder == NULL || file == NULL )
619 return ERROR_INVALID_PARAMETER;
621 len = lstrlenW(folder) + lstrlenW(backslash) + lstrlenW(file) + 1;
622 path = msi_alloc( len * sizeof(WCHAR) );
623 if (!path)
624 return ERROR_OUTOFMEMORY;
626 lstrcpyW( path, folder );
627 lstrcatW( path, backslash );
628 lstrcatW( path, file );
630 data = msi_read_text_archive( path );
632 ptr = data;
633 msi_parse_line( &ptr, &columns, &num_columns );
634 msi_parse_line( &ptr, &types, &num_types );
635 msi_parse_line( &ptr, &labels, &num_labels );
637 if (num_columns != num_types)
639 r = ERROR_FUNCTION_FAILED;
640 goto done;
643 records = msi_alloc(sizeof(LPWSTR *));
644 if (!records)
646 r = ERROR_OUTOFMEMORY;
647 goto done;
650 /* read in the table records */
651 while (*ptr)
653 msi_parse_line( &ptr, &records[num_records], NULL );
655 num_records++;
656 temp_records = msi_realloc(records, (num_records + 1) * sizeof(LPWSTR *));
657 if (!temp_records)
659 r = ERROR_OUTOFMEMORY;
660 goto done;
662 records = temp_records;
665 if (!TABLE_Exists(db, labels[0]))
667 r = msi_add_table_to_db( db, columns, types, labels, num_labels, num_columns );
668 if (r != ERROR_SUCCESS)
670 r = ERROR_FUNCTION_FAILED;
671 goto done;
675 r = msi_add_records_to_table( db, columns, types, labels, records, num_columns, num_records );
677 done:
678 msi_free(path);
679 msi_free(data);
680 msi_free(columns);
681 msi_free(types);
682 msi_free(labels);
684 for (i = 0; i < num_records; i++)
685 msi_free(records[i]);
687 msi_free(records);
689 return r;
692 UINT WINAPI MsiDatabaseImportW(MSIHANDLE handle, LPCWSTR szFolder, LPCWSTR szFilename)
694 MSIDATABASE *db;
695 UINT r;
697 TRACE("%lx %s %s\n",handle,debugstr_w(szFolder), debugstr_w(szFilename));
699 db = msihandle2msiinfo( handle, MSIHANDLETYPE_DATABASE );
700 if( !db )
702 IWineMsiRemoteDatabase *remote_database;
704 remote_database = (IWineMsiRemoteDatabase *)msi_get_remote( handle );
705 if ( !remote_database )
706 return ERROR_INVALID_HANDLE;
708 IWineMsiRemoteDatabase_Release( remote_database );
709 WARN("MsiDatabaseImport not allowed during a custom action!\n");
711 return ERROR_SUCCESS;
714 r = MSI_DatabaseImport( db, szFolder, szFilename );
715 msiobj_release( &db->hdr );
716 return r;
719 UINT WINAPI MsiDatabaseImportA( MSIHANDLE handle,
720 LPCSTR szFolder, LPCSTR szFilename )
722 LPWSTR path = NULL, file = NULL;
723 UINT r = ERROR_OUTOFMEMORY;
725 TRACE("%lx %s %s\n", handle, debugstr_a(szFolder), debugstr_a(szFilename));
727 if( szFolder )
729 path = strdupAtoW( szFolder );
730 if( !path )
731 goto end;
734 if( szFilename )
736 file = strdupAtoW( szFilename );
737 if( !file )
738 goto end;
741 r = MsiDatabaseImportW( handle, path, file );
743 end:
744 msi_free( path );
745 msi_free( file );
747 return r;
750 static UINT msi_export_record( HANDLE handle, MSIRECORD *row, UINT start )
752 UINT i, count, len, r = ERROR_SUCCESS;
753 const char *sep;
754 char *buffer;
755 DWORD sz;
757 len = 0x100;
758 buffer = msi_alloc( len );
759 if ( !buffer )
760 return ERROR_OUTOFMEMORY;
762 count = MSI_RecordGetFieldCount( row );
763 for ( i=start; i<=count; i++ )
765 sz = len;
766 r = MSI_RecordGetStringA( row, i, buffer, &sz );
767 if (r == ERROR_MORE_DATA)
769 char *p = msi_realloc( buffer, sz + 1 );
770 if (!p)
771 break;
772 len = sz + 1;
773 buffer = p;
775 sz = len;
776 r = MSI_RecordGetStringA( row, i, buffer, &sz );
777 if (r != ERROR_SUCCESS)
778 break;
780 if (!WriteFile( handle, buffer, sz, &sz, NULL ))
782 r = ERROR_FUNCTION_FAILED;
783 break;
786 sep = (i < count) ? "\t" : "\r\n";
787 if (!WriteFile( handle, sep, strlen(sep), &sz, NULL ))
789 r = ERROR_FUNCTION_FAILED;
790 break;
793 msi_free( buffer );
794 return r;
797 static UINT msi_export_row( MSIRECORD *row, void *arg )
799 return msi_export_record( arg, row, 1 );
802 static UINT msi_export_forcecodepage( HANDLE handle )
804 DWORD sz;
806 static const char data[] = "\r\n\r\n0\t_ForceCodepage\r\n";
808 FIXME("Read the codepage from the strings table!\n");
810 sz = lstrlenA(data) + 1;
811 if (!WriteFile(handle, data, sz, &sz, NULL))
812 return ERROR_FUNCTION_FAILED;
814 return ERROR_SUCCESS;
817 UINT MSI_DatabaseExport( MSIDATABASE *db, LPCWSTR table,
818 LPCWSTR folder, LPCWSTR file )
820 static const WCHAR query[] = {
821 's','e','l','e','c','t',' ','*',' ','f','r','o','m',' ','%','s',0 };
822 static const WCHAR szbs[] = { '\\', 0 };
823 static const WCHAR forcecodepage[] = {
824 '_','F','o','r','c','e','C','o','d','e','p','a','g','e',0 };
825 MSIRECORD *rec = NULL;
826 MSIQUERY *view = NULL;
827 LPWSTR filename;
828 HANDLE handle;
829 UINT len, r;
831 TRACE("%p %s %s %s\n", db, debugstr_w(table),
832 debugstr_w(folder), debugstr_w(file) );
834 if( folder == NULL || file == NULL )
835 return ERROR_INVALID_PARAMETER;
837 len = lstrlenW(folder) + lstrlenW(file) + 2;
838 filename = msi_alloc(len * sizeof (WCHAR));
839 if (!filename)
840 return ERROR_OUTOFMEMORY;
842 lstrcpyW( filename, folder );
843 lstrcatW( filename, szbs );
844 lstrcatW( filename, file );
846 handle = CreateFileW( filename, GENERIC_READ | GENERIC_WRITE, 0,
847 NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL );
848 msi_free( filename );
849 if (handle == INVALID_HANDLE_VALUE)
850 return ERROR_FUNCTION_FAILED;
852 if (!lstrcmpW( table, forcecodepage ))
854 r = msi_export_forcecodepage( handle );
855 goto done;
858 r = MSI_OpenQuery( db, &view, query, table );
859 if (r == ERROR_SUCCESS)
861 /* write out row 1, the column names */
862 r = MSI_ViewGetColumnInfo(view, MSICOLINFO_NAMES, &rec);
863 if (r == ERROR_SUCCESS)
865 msi_export_record( handle, rec, 1 );
866 msiobj_release( &rec->hdr );
869 /* write out row 2, the column types */
870 r = MSI_ViewGetColumnInfo(view, MSICOLINFO_TYPES, &rec);
871 if (r == ERROR_SUCCESS)
873 msi_export_record( handle, rec, 1 );
874 msiobj_release( &rec->hdr );
877 /* write out row 3, the table name + keys */
878 r = MSI_DatabaseGetPrimaryKeys( db, table, &rec );
879 if (r == ERROR_SUCCESS)
881 MSI_RecordSetStringW( rec, 0, table );
882 msi_export_record( handle, rec, 0 );
883 msiobj_release( &rec->hdr );
886 /* write out row 4 onwards, the data */
887 r = MSI_IterateRecords( view, 0, msi_export_row, handle );
888 msiobj_release( &view->hdr );
891 done:
892 CloseHandle( handle );
893 return r;
896 /***********************************************************************
897 * MsiExportDatabaseW [MSI.@]
899 * Writes a file containing the table data as tab separated ASCII.
901 * The format is as follows:
903 * row1 : colname1 <tab> colname2 <tab> .... colnameN <cr> <lf>
904 * row2 : coltype1 <tab> coltype2 <tab> .... coltypeN <cr> <lf>
905 * row3 : tablename <tab> key1 <tab> key2 <tab> ... keyM <cr> <lf>
907 * Followed by the data, starting at row 1 with one row per line
909 * row4 : data <tab> data <tab> data <tab> ... data <cr> <lf>
911 UINT WINAPI MsiDatabaseExportW( MSIHANDLE handle, LPCWSTR szTable,
912 LPCWSTR szFolder, LPCWSTR szFilename )
914 MSIDATABASE *db;
915 UINT r;
917 TRACE("%lx %s %s %s\n", handle, debugstr_w(szTable),
918 debugstr_w(szFolder), debugstr_w(szFilename));
920 db = msihandle2msiinfo( handle, MSIHANDLETYPE_DATABASE );
921 if( !db )
923 IWineMsiRemoteDatabase *remote_database;
925 remote_database = (IWineMsiRemoteDatabase *)msi_get_remote( handle );
926 if ( !remote_database )
927 return ERROR_INVALID_HANDLE;
929 IWineMsiRemoteDatabase_Release( remote_database );
930 WARN("MsiDatabaseExport not allowed during a custom action!\n");
932 return ERROR_SUCCESS;
935 r = MSI_DatabaseExport( db, szTable, szFolder, szFilename );
936 msiobj_release( &db->hdr );
937 return r;
940 UINT WINAPI MsiDatabaseExportA( MSIHANDLE handle, LPCSTR szTable,
941 LPCSTR szFolder, LPCSTR szFilename )
943 LPWSTR path = NULL, file = NULL, table = NULL;
944 UINT r = ERROR_OUTOFMEMORY;
946 TRACE("%lx %s %s %s\n", handle, debugstr_a(szTable),
947 debugstr_a(szFolder), debugstr_a(szFilename));
949 if( szTable )
951 table = strdupAtoW( szTable );
952 if( !table )
953 goto end;
956 if( szFolder )
958 path = strdupAtoW( szFolder );
959 if( !path )
960 goto end;
963 if( szFilename )
965 file = strdupAtoW( szFilename );
966 if( !file )
967 goto end;
970 r = MsiDatabaseExportW( handle, table, path, file );
972 end:
973 msi_free( table );
974 msi_free( path );
975 msi_free( file );
977 return r;
980 MSIDBSTATE WINAPI MsiGetDatabaseState( MSIHANDLE handle )
982 MSIDBSTATE ret = MSIDBSTATE_READ;
983 MSIDATABASE *db;
985 TRACE("%ld\n", handle);
987 db = msihandle2msiinfo( handle, MSIHANDLETYPE_DATABASE );
988 if( !db )
990 IWineMsiRemoteDatabase *remote_database;
992 remote_database = (IWineMsiRemoteDatabase *)msi_get_remote( handle );
993 if ( !remote_database )
994 return MSIDBSTATE_ERROR;
996 IWineMsiRemoteDatabase_Release( remote_database );
997 WARN("MsiGetDatabaseState not allowed during a custom action!\n");
999 return MSIDBSTATE_READ;
1002 if (db->mode != MSIDBOPEN_READONLY )
1003 ret = MSIDBSTATE_WRITE;
1004 msiobj_release( &db->hdr );
1006 return ret;
1009 typedef struct _msi_remote_database_impl {
1010 const IWineMsiRemoteDatabaseVtbl *lpVtbl;
1011 MSIHANDLE database;
1012 LONG refs;
1013 } msi_remote_database_impl;
1015 static inline msi_remote_database_impl* mrd_from_IWineMsiRemoteDatabase( IWineMsiRemoteDatabase* iface )
1017 return (msi_remote_database_impl *)iface;
1020 static HRESULT WINAPI mrd_QueryInterface( IWineMsiRemoteDatabase *iface,
1021 REFIID riid,LPVOID *ppobj)
1023 if( IsEqualCLSID( riid, &IID_IUnknown ) ||
1024 IsEqualCLSID( riid, &IID_IWineMsiRemoteDatabase ) )
1026 IUnknown_AddRef( iface );
1027 *ppobj = iface;
1028 return S_OK;
1031 return E_NOINTERFACE;
1034 static ULONG WINAPI mrd_AddRef( IWineMsiRemoteDatabase *iface )
1036 msi_remote_database_impl* This = mrd_from_IWineMsiRemoteDatabase( iface );
1038 return InterlockedIncrement( &This->refs );
1041 static ULONG WINAPI mrd_Release( IWineMsiRemoteDatabase *iface )
1043 msi_remote_database_impl* This = mrd_from_IWineMsiRemoteDatabase( iface );
1044 ULONG r;
1046 r = InterlockedDecrement( &This->refs );
1047 if (r == 0)
1049 MsiCloseHandle( This->database );
1050 msi_free( This );
1052 return r;
1055 static HRESULT WINAPI mrd_IsTablePersistent( IWineMsiRemoteDatabase *iface,
1056 BSTR table, MSICONDITION *persistent )
1058 msi_remote_database_impl *This = mrd_from_IWineMsiRemoteDatabase( iface );
1059 *persistent = MsiDatabaseIsTablePersistentW(This->database, (LPWSTR)table);
1060 return S_OK;
1063 static HRESULT WINAPI mrd_GetPrimaryKeys( IWineMsiRemoteDatabase *iface,
1064 BSTR table, MSIHANDLE *keys )
1066 msi_remote_database_impl *This = mrd_from_IWineMsiRemoteDatabase( iface );
1067 UINT r = MsiDatabaseGetPrimaryKeysW(This->database, (LPWSTR)table, keys);
1068 return HRESULT_FROM_WIN32(r);
1071 static HRESULT WINAPI mrd_GetSummaryInformation( IWineMsiRemoteDatabase *iface,
1072 UINT updatecount, MSIHANDLE *suminfo )
1074 msi_remote_database_impl *This = mrd_from_IWineMsiRemoteDatabase( iface );
1075 UINT r = MsiGetSummaryInformationW(This->database, NULL, updatecount, suminfo);
1076 return HRESULT_FROM_WIN32(r);
1079 static HRESULT WINAPI mrd_OpenView( IWineMsiRemoteDatabase *iface,
1080 BSTR query, MSIHANDLE *view )
1082 msi_remote_database_impl *This = mrd_from_IWineMsiRemoteDatabase( iface );
1083 UINT r = MsiDatabaseOpenViewW(This->database, (LPWSTR)query, view);
1084 return HRESULT_FROM_WIN32(r);
1087 static HRESULT WINAPI mrd_SetMsiHandle( IWineMsiRemoteDatabase *iface, MSIHANDLE handle )
1089 msi_remote_database_impl* This = mrd_from_IWineMsiRemoteDatabase( iface );
1090 This->database = handle;
1091 return S_OK;
1094 static const IWineMsiRemoteDatabaseVtbl msi_remote_database_vtbl =
1096 mrd_QueryInterface,
1097 mrd_AddRef,
1098 mrd_Release,
1099 mrd_IsTablePersistent,
1100 mrd_GetPrimaryKeys,
1101 mrd_GetSummaryInformation,
1102 mrd_OpenView,
1103 mrd_SetMsiHandle,
1106 HRESULT create_msi_remote_database( IUnknown *pOuter, LPVOID *ppObj )
1108 msi_remote_database_impl *This;
1110 This = msi_alloc( sizeof *This );
1111 if (!This)
1112 return E_OUTOFMEMORY;
1114 This->lpVtbl = &msi_remote_database_vtbl;
1115 This->database = 0;
1116 This->refs = 1;
1118 *ppObj = This;
1120 return S_OK;