Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
wine-mirror
GitHub Repository: wine-mirror/wine
Path: blob/master/programs/msidb/main.c
4388 views
1
/*
2
* msidb - command line tool for assembling MSI packages
3
*
4
* Copyright 2015 Erich E. Hoover
5
*
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.
10
*
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.
15
*
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
19
*/
20
21
#define WIN32_LEAN_AND_MEAN
22
23
#include <stdlib.h>
24
#include <stdio.h>
25
#include <windows.h>
26
#include <msi.h>
27
#include <msiquery.h>
28
#include <shlwapi.h>
29
30
#include "wine/debug.h"
31
#include "wine/list.h"
32
33
WINE_DEFAULT_DEBUG_CHANNEL(msidb);
34
35
struct msidb_listentry
36
{
37
struct list entry;
38
WCHAR *name;
39
};
40
41
struct msidb_state
42
{
43
WCHAR *database_file;
44
WCHAR *table_folder;
45
MSIHANDLE database_handle;
46
BOOL add_streams;
47
BOOL extract_streams;
48
BOOL kill_streams;
49
BOOL create_database;
50
BOOL import_tables;
51
BOOL export_tables;
52
BOOL short_filenames;
53
struct list add_stream_list;
54
struct list extract_stream_list;
55
struct list kill_stream_list;
56
struct list table_list;
57
};
58
59
static void list_free( struct list *list )
60
{
61
struct msidb_listentry *data, *next;
62
63
LIST_FOR_EACH_ENTRY_SAFE( data, next, list, struct msidb_listentry, entry )
64
{
65
list_remove( &data->entry );
66
free( data );
67
}
68
}
69
70
static void list_append( struct list *list, WCHAR *name )
71
{
72
struct msidb_listentry *data;
73
74
data = calloc( 1, sizeof(*data) );
75
if (!data)
76
{
77
ERR( "Out of memory for list.\n" );
78
exit( 1 );
79
}
80
data->name = name;
81
list_add_tail( list, &data->entry );
82
}
83
84
static void show_usage( void )
85
{
86
WINE_MESSAGE(
87
"Usage: msidb [OPTION]...[OPTION]... [TABLE]...[TABLE]\n"
88
"Options:\n"
89
" -? Show this usage message and exit.\n"
90
" -a file.cab Add stream/cabinet file to _Streams table.\n"
91
" -c Create database file (instead of opening existing file).\n"
92
" -d package.msi Path to the database file.\n"
93
" -e Export tables from database.\n"
94
" -f folder Folder in which to open/save the tables.\n"
95
" -i Import tables into database.\n"
96
" -k file.cab Kill (remove) stream/cabinet file from _Streams table.\n"
97
" -s Export with short filenames (eight character max).\n"
98
" -x file.cab Extract stream/cabinet file from _Streams table.\n"
99
);
100
}
101
102
static int valid_state( struct msidb_state *state )
103
{
104
if (state->database_file == NULL)
105
{
106
FIXME( "GUI operation is not currently supported.\n" );
107
return 0;
108
}
109
if (state->table_folder == NULL && !state->add_streams && !state->kill_streams
110
&& !state->extract_streams)
111
{
112
ERR( "No table folder specified (-f option).\n" );
113
show_usage();
114
return 0;
115
}
116
if (!state->create_database && !state->import_tables && !state->export_tables
117
&& !state->add_streams&& !state->kill_streams && !state->extract_streams)
118
{
119
ERR( "No mode flag specified (-a, -c, -e, -i, -k, -x).\n" );
120
show_usage();
121
return 0;
122
}
123
if (list_empty( &state->table_list ) && !state->add_streams && !state->kill_streams
124
&& !state->extract_streams)
125
{
126
ERR( "No tables specified.\n" );
127
return 0;
128
}
129
return 1;
130
}
131
132
static int process_argument( struct msidb_state *state, int i, int argc, WCHAR *argv[] )
133
{
134
/* msidb accepts either "-" or "/" style flags */
135
if (lstrlenW(argv[i]) != 2 || (argv[i][0] != '-' && argv[i][0] != '/'))
136
return 0;
137
switch( argv[i][1] )
138
{
139
case '?':
140
show_usage();
141
exit( 0 );
142
case 'a':
143
if (i + 1 >= argc) return 0;
144
state->add_streams = TRUE;
145
list_append( &state->add_stream_list, argv[i + 1] );
146
return 2;
147
case 'c':
148
state->create_database = TRUE;
149
return 1;
150
case 'd':
151
if (i + 1 >= argc) return 0;
152
state->database_file = argv[i + 1];
153
return 2;
154
case 'e':
155
state->export_tables = TRUE;
156
return 1;
157
case 'f':
158
if (i + 1 >= argc) return 0;
159
state->table_folder = argv[i + 1];
160
return 2;
161
case 'i':
162
state->import_tables = TRUE;
163
return 1;
164
case 'k':
165
if (i + 1 >= argc) return 0;
166
state->kill_streams = TRUE;
167
list_append( &state->kill_stream_list, argv[i + 1] );
168
return 2;
169
case 's':
170
state->short_filenames = TRUE;
171
return 1;
172
case 'x':
173
if (i + 1 >= argc) return 0;
174
state->extract_streams = TRUE;
175
list_append( &state->extract_stream_list, argv[i + 1] );
176
return 2;
177
default:
178
break;
179
}
180
show_usage();
181
exit( 1 );
182
}
183
184
static int open_database( struct msidb_state *state )
185
{
186
LPCWSTR db_mode = state->create_database ? MSIDBOPEN_CREATE : MSIDBOPEN_TRANSACT;
187
UINT ret;
188
189
ret = MsiOpenDatabaseW( state->database_file, db_mode, &state->database_handle );
190
if (ret != ERROR_SUCCESS)
191
{
192
ERR( "Failed to open database '%s', error %d\n", wine_dbgstr_w(state->database_file), ret );
193
return 0;
194
}
195
return 1;
196
}
197
198
static void close_database( struct msidb_state *state, BOOL commit_changes )
199
{
200
UINT ret = ERROR_SUCCESS;
201
202
if (state->database_handle == 0)
203
return;
204
if (commit_changes)
205
ret = MsiDatabaseCommit( state->database_handle );
206
if (ret != ERROR_SUCCESS)
207
{
208
ERR( "Failed to commit changes to database.\n" );
209
return;
210
}
211
ret = MsiCloseHandle( state->database_handle );
212
if (ret != ERROR_SUCCESS)
213
{
214
WARN( "Failed to close database handle.\n" );
215
return;
216
}
217
}
218
219
static const WCHAR *basenameW( const WCHAR *filename )
220
{
221
const WCHAR *dir_end;
222
223
dir_end = wcsrchr( filename, '/' );
224
if (dir_end) return dir_end + 1;
225
dir_end = wcsrchr( filename, '\\' );
226
if (dir_end) return dir_end + 1;
227
return filename;
228
}
229
230
static int add_stream( struct msidb_state *state, const WCHAR *stream_filename )
231
{
232
MSIHANDLE view = 0, record = 0;
233
UINT ret;
234
235
ret = MsiDatabaseOpenViewW( state->database_handle, L"INSERT INTO _Streams (Name, Data) VALUES (?, ?)", &view );
236
if (ret != ERROR_SUCCESS)
237
{
238
ERR( "Failed to open _Streams table.\n" );
239
goto cleanup;
240
}
241
record = MsiCreateRecord( 2 );
242
if (record == 0)
243
{
244
ERR( "Failed to create MSI record.\n" );
245
ret = ERROR_OUTOFMEMORY;
246
goto cleanup;
247
}
248
ret = MsiRecordSetStringW( record, 1, basenameW( stream_filename ) );
249
if (ret != ERROR_SUCCESS)
250
{
251
ERR( "Failed to add stream filename to MSI record.\n" );
252
goto cleanup;
253
}
254
ret = MsiRecordSetStreamW( record, 2, stream_filename );
255
if (ret != ERROR_SUCCESS)
256
{
257
ERR( "Failed to add stream to MSI record.\n" );
258
goto cleanup;
259
}
260
ret = MsiViewExecute( view, record );
261
if (ret != ERROR_SUCCESS)
262
{
263
ERR( "Failed to add stream to _Streams table.\n" );
264
goto cleanup;
265
}
266
267
cleanup:
268
if (record)
269
MsiCloseHandle( record );
270
if (view)
271
MsiViewClose( view );
272
273
return (ret == ERROR_SUCCESS);
274
}
275
276
static int add_streams( struct msidb_state *state )
277
{
278
struct msidb_listentry *data;
279
280
LIST_FOR_EACH_ENTRY( data, &state->add_stream_list, struct msidb_listentry, entry )
281
{
282
if (!add_stream( state, data->name ))
283
return 0; /* failed, do not commit changes */
284
}
285
return 1;
286
}
287
288
static int kill_stream( struct msidb_state *state, const WCHAR *stream_filename )
289
{
290
MSIHANDLE view = 0, record = 0;
291
UINT ret;
292
293
ret = MsiDatabaseOpenViewW( state->database_handle, L"DELETE FROM _Streams WHERE Name = ?", &view );
294
if (ret != ERROR_SUCCESS)
295
{
296
ERR( "Failed to open _Streams table.\n" );
297
goto cleanup;
298
}
299
record = MsiCreateRecord( 1 );
300
if (record == 0)
301
{
302
ERR( "Failed to create MSI record.\n" );
303
ret = ERROR_OUTOFMEMORY;
304
goto cleanup;
305
}
306
ret = MsiRecordSetStringW( record, 1, stream_filename );
307
if (ret != ERROR_SUCCESS)
308
{
309
ERR( "Failed to add stream filename to MSI record.\n" );
310
goto cleanup;
311
}
312
ret = MsiViewExecute( view, record );
313
if (ret != ERROR_SUCCESS)
314
{
315
ERR( "Failed to delete stream from _Streams table.\n" );
316
goto cleanup;
317
}
318
319
cleanup:
320
if (record)
321
MsiCloseHandle( record );
322
if (view)
323
MsiViewClose( view );
324
325
return (ret == ERROR_SUCCESS);
326
}
327
328
static int kill_streams( struct msidb_state *state )
329
{
330
struct msidb_listentry *data;
331
332
LIST_FOR_EACH_ENTRY( data, &state->kill_stream_list, struct msidb_listentry, entry )
333
{
334
if (!kill_stream( state, data->name ))
335
return 0; /* failed, do not commit changes */
336
}
337
return 1;
338
}
339
340
static int extract_stream( struct msidb_state *state, const WCHAR *stream_filename )
341
{
342
HANDLE file = INVALID_HANDLE_VALUE;
343
MSIHANDLE view = 0, record = 0;
344
DWORD read_size, write_size;
345
char buffer[1024];
346
UINT ret;
347
348
file = CreateFileW( stream_filename, GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE,
349
NULL, CREATE_NEW, FILE_ATTRIBUTE_NORMAL, NULL );
350
if (file == INVALID_HANDLE_VALUE)
351
{
352
ret = ERROR_FILE_NOT_FOUND;
353
ERR( "Failed to open destination file %s.\n", wine_dbgstr_w(stream_filename) );
354
goto cleanup;
355
}
356
ret = MsiDatabaseOpenViewW( state->database_handle, L"SELECT Data FROM _Streams WHERE Name = ?", &view );
357
if (ret != ERROR_SUCCESS)
358
{
359
ERR( "Failed to open _Streams table.\n" );
360
goto cleanup;
361
}
362
record = MsiCreateRecord( 1 );
363
if (record == 0)
364
{
365
ERR( "Failed to create MSI record.\n" );
366
ret = ERROR_OUTOFMEMORY;
367
goto cleanup;
368
}
369
ret = MsiRecordSetStringW( record, 1, stream_filename );
370
if (ret != ERROR_SUCCESS)
371
{
372
ERR( "Failed to add stream filename to MSI record.\n" );
373
goto cleanup;
374
}
375
ret = MsiViewExecute( view, record );
376
if (ret != ERROR_SUCCESS)
377
{
378
ERR( "Failed to query stream from _Streams table.\n" );
379
goto cleanup;
380
}
381
MsiCloseHandle( record );
382
record = 0;
383
ret = MsiViewFetch( view, &record );
384
if (ret != ERROR_SUCCESS)
385
{
386
ERR( "Failed to query row from _Streams table.\n" );
387
goto cleanup;
388
}
389
read_size = sizeof(buffer);
390
while (read_size == sizeof(buffer))
391
{
392
ret = MsiRecordReadStream( record, 1, buffer, &read_size );
393
if (ret != ERROR_SUCCESS)
394
{
395
ERR( "Failed to read stream from _Streams table.\n" );
396
goto cleanup;
397
}
398
if (!WriteFile( file, buffer, read_size, &write_size, NULL ) || read_size != write_size)
399
{
400
ret = ERROR_WRITE_FAULT;
401
ERR( "Failed to write stream to destination file.\n" );
402
goto cleanup;
403
}
404
}
405
406
cleanup:
407
if (record)
408
MsiCloseHandle( record );
409
if (view)
410
MsiViewClose( view );
411
if (file != INVALID_HANDLE_VALUE)
412
CloseHandle( file );
413
return (ret == ERROR_SUCCESS);
414
}
415
416
static int extract_streams( struct msidb_state *state )
417
{
418
struct msidb_listentry *data;
419
420
LIST_FOR_EACH_ENTRY( data, &state->extract_stream_list, struct msidb_listentry, entry )
421
{
422
if (!extract_stream( state, data->name ))
423
return 0; /* failed, do not commit changes */
424
}
425
return 1;
426
}
427
428
static int import_table( struct msidb_state *state, const WCHAR *table_path )
429
{
430
UINT ret;
431
432
ret = MsiDatabaseImportW( state->database_handle, state->table_folder, table_path );
433
if (ret != ERROR_SUCCESS)
434
{
435
ERR( "Failed to import table '%s', error %d.\n", wine_dbgstr_w(table_path), ret );
436
return 0;
437
}
438
return 1;
439
}
440
441
static int import_tables( struct msidb_state *state )
442
{
443
struct msidb_listentry *data;
444
445
LIST_FOR_EACH_ENTRY( data, &state->table_list, struct msidb_listentry, entry )
446
{
447
WCHAR *table_name = data->name;
448
WCHAR table_path[MAX_PATH];
449
WCHAR *ext;
450
451
/* permit specifying tables with wildcards ('Feature*') */
452
if (wcsstr( table_name, L"*" ) != NULL)
453
{
454
WIN32_FIND_DATAW f;
455
HANDLE handle;
456
WCHAR *path;
457
DWORD len;
458
459
len = lstrlenW( state->table_folder ) + 1 + lstrlenW( table_name ) + 1; /* %s/%s\0 */
460
path = malloc( len * sizeof(WCHAR) );
461
if (path == NULL)
462
return 0;
463
lstrcpyW( path, state->table_folder );
464
PathAddBackslashW( path );
465
lstrcatW( path, table_name );
466
handle = FindFirstFileW( path, &f );
467
free( path );
468
if (handle == INVALID_HANDLE_VALUE)
469
return 0;
470
do
471
{
472
if (f.cFileName[0] == '.' && !f.cFileName[1]) continue;
473
if (f.cFileName[0] == '.' && f.cFileName[1] == '.' && !f.cFileName[2]) continue;
474
if (f.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) continue;
475
if ((ext = PathFindExtensionW( f.cFileName )) == NULL) continue;
476
if (lstrcmpW( ext, L".idt" ) != 0) continue;
477
if (!import_table( state, f.cFileName ))
478
{
479
FindClose( handle );
480
return 0; /* failed, do not commit changes */
481
}
482
} while (FindNextFileW( handle, &f ));
483
FindClose( handle );
484
continue;
485
}
486
/* permit specifying tables by filename (*.idt) */
487
if ((ext = PathFindExtensionW( table_name )) == NULL || lstrcmpW( ext, L".idt" ) != 0)
488
{
489
/* truncate to 8 characters */
490
swprintf( table_path, ARRAY_SIZE(table_path), L"%.8s.idt", table_name );
491
table_name = table_path;
492
}
493
if (!import_table( state, table_name ))
494
return 0; /* failed, do not commit changes */
495
}
496
return 1;
497
}
498
499
static int export_table( struct msidb_state *state, const WCHAR *table_name )
500
{
501
const WCHAR *format = (state->short_filenames ? L"%.8s.idt" : L"%s.idt");
502
WCHAR table_path[MAX_PATH];
503
UINT ret;
504
505
swprintf( table_path, ARRAY_SIZE(table_path), format, table_name );
506
ret = MsiDatabaseExportW( state->database_handle, table_name, state->table_folder, table_path );
507
if (ret != ERROR_SUCCESS)
508
{
509
ERR( "Failed to export table '%s', error %d.\n", wine_dbgstr_w(table_name), ret );
510
return 0;
511
}
512
return 1;
513
}
514
515
static int export_all_tables( struct msidb_state *state )
516
{
517
MSIHANDLE view = 0;
518
UINT ret;
519
520
ret = MsiDatabaseOpenViewW( state->database_handle, L"SELECT Name FROM _Tables", &view );
521
if (ret != ERROR_SUCCESS)
522
{
523
ERR( "Failed to open _Tables table.\n" );
524
goto cleanup;
525
}
526
ret = MsiViewExecute( view, 0 );
527
if (ret != ERROR_SUCCESS)
528
{
529
ERR( "Failed to query list from _Tables table.\n" );
530
goto cleanup;
531
}
532
while( 1 )
533
{
534
MSIHANDLE record = 0;
535
WCHAR table[256];
536
DWORD size;
537
538
ret = MsiViewFetch( view, &record );
539
if (ret == ERROR_NO_MORE_ITEMS)
540
break;
541
if (ret != ERROR_SUCCESS)
542
{
543
ERR( "Failed to query row from _Tables table.\n" );
544
goto cleanup;
545
}
546
size = ARRAY_SIZE(table);
547
ret = MsiRecordGetStringW( record, 1, table, &size );
548
if (ret != ERROR_SUCCESS)
549
{
550
ERR( "Failed to retrieve name string.\n" );
551
goto cleanup;
552
}
553
if (!export_table( state, table ))
554
{
555
ret = ERROR_FUNCTION_FAILED;
556
goto cleanup;
557
}
558
ret = MsiCloseHandle( record );
559
if (ret != ERROR_SUCCESS)
560
{
561
ERR( "Failed to close record handle.\n" );
562
goto cleanup;
563
}
564
}
565
ret = ERROR_SUCCESS;
566
/* the _SummaryInformation table is not listed in _Tables */
567
if (!export_table( state, L"_SummaryInformation" ))
568
{
569
ret = ERROR_FUNCTION_FAILED;
570
goto cleanup;
571
}
572
573
cleanup:
574
if (view && MsiViewClose( view ) != ERROR_SUCCESS)
575
{
576
ERR( "Failed to close _Streams table.\n" );
577
return 0;
578
}
579
return (ret == ERROR_SUCCESS);
580
}
581
582
static int export_tables( struct msidb_state *state )
583
{
584
struct msidb_listentry *data;
585
586
LIST_FOR_EACH_ENTRY( data, &state->table_list, struct msidb_listentry, entry )
587
{
588
if (lstrcmpW( data->name, L"*" ) == 0)
589
{
590
if (!export_all_tables( state ))
591
return 0; /* failed, do not commit changes */
592
continue;
593
}
594
if (!export_table( state, data->name ))
595
return 0; /* failed, do not commit changes */
596
}
597
return 1;
598
}
599
600
int __cdecl wmain( int argc, WCHAR *argv[] )
601
{
602
struct msidb_state state;
603
int i, n = 1;
604
int ret = 1;
605
606
memset( &state, 0x0, sizeof(state) );
607
list_init( &state.add_stream_list );
608
list_init( &state.extract_stream_list );
609
list_init( &state.kill_stream_list );
610
list_init( &state.table_list );
611
/* process and validate all the command line flags */
612
for (i = 1; n && i < argc; i += n)
613
n = process_argument( &state, i, argc, argv );
614
/* process all remaining arguments as table names */
615
for (; i < argc; i++)
616
list_append( &state.table_list, argv[i] );
617
if (!valid_state( &state ))
618
goto cleanup;
619
620
/* perform the requested operations */
621
if (!open_database( &state ))
622
{
623
ERR( "Failed to open database '%s'.\n", wine_dbgstr_w(state.database_file) );
624
goto cleanup;
625
}
626
if (state.add_streams && !add_streams( &state ))
627
goto cleanup; /* failed, do not commit changes */
628
if (state.export_tables && !export_tables( &state ))
629
goto cleanup; /* failed, do not commit changes */
630
if (state.extract_streams && !extract_streams( &state ))
631
goto cleanup; /* failed, do not commit changes */
632
if (state.import_tables && !import_tables( &state ))
633
goto cleanup; /* failed, do not commit changes */
634
if (state.kill_streams && !kill_streams( &state ))
635
goto cleanup; /* failed, do not commit changes */
636
ret = 0;
637
638
cleanup:
639
close_database( &state, ret == 0 );
640
list_free( &state.add_stream_list );
641
list_free( &state.extract_stream_list );
642
list_free( &state.kill_stream_list );
643
list_free( &state.table_list );
644
return ret;
645
}
646
647