kerberos: Add support for SECPKG_CRED_BOTH.
[wine.git] / programs / msidb / main.c
blob8570b1f9f3d47cb25d712e8234f543fd17d8f6cd
1 /*
2 * msidb - command line tool for assembling MSI packages
4 * Copyright 2015 Erich E. Hoover
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 #define WIN32_LEAN_AND_MEAN
23 #include <stdlib.h>
24 #include <stdio.h>
25 #include <windows.h>
26 #include <msi.h>
27 #include <msiquery.h>
28 #include <shlwapi.h>
30 #include "wine/debug.h"
31 #include "wine/list.h"
33 WINE_DEFAULT_DEBUG_CHANNEL(msidb);
35 struct msidb_listentry
37 struct list entry;
38 WCHAR *name;
41 struct msidb_state
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;
59 static void list_free( struct list *list )
61 struct msidb_listentry *data, *next;
63 LIST_FOR_EACH_ENTRY_SAFE( data, next, list, struct msidb_listentry, entry )
65 list_remove( &data->entry );
66 free( data );
70 static void list_append( struct list *list, WCHAR *name )
72 struct msidb_listentry *data;
74 data = calloc( 1, sizeof(*data) );
75 if (!data)
77 ERR( "Out of memory for list.\n" );
78 exit( 1 );
80 data->name = name;
81 list_add_tail( list, &data->entry );
84 static void show_usage( void )
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"
102 static int valid_state( struct msidb_state *state )
104 if (state->database_file == NULL)
106 FIXME( "GUI operation is not currently supported.\n" );
107 return 0;
109 if (state->table_folder == NULL && !state->add_streams && !state->kill_streams
110 && !state->extract_streams)
112 ERR( "No table folder specified (-f option).\n" );
113 show_usage();
114 return 0;
116 if (!state->create_database && !state->import_tables && !state->export_tables
117 && !state->add_streams&& !state->kill_streams && !state->extract_streams)
119 ERR( "No mode flag specified (-a, -c, -e, -i, -k, -x).\n" );
120 show_usage();
121 return 0;
123 if (list_empty( &state->table_list ) && !state->add_streams && !state->kill_streams
124 && !state->extract_streams)
126 ERR( "No tables specified.\n" );
127 return 0;
129 return 1;
132 static int process_argument( struct msidb_state *state, int i, int argc, WCHAR *argv[] )
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] )
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;
180 show_usage();
181 exit( 1 );
184 static int open_database( struct msidb_state *state )
186 LPCWSTR db_mode = state->create_database ? MSIDBOPEN_CREATE : MSIDBOPEN_TRANSACT;
187 UINT ret;
189 ret = MsiOpenDatabaseW( state->database_file, db_mode, &state->database_handle );
190 if (ret != ERROR_SUCCESS)
192 ERR( "Failed to open database '%s', error %d\n", wine_dbgstr_w(state->database_file), ret );
193 return 0;
195 return 1;
198 static void close_database( struct msidb_state *state, BOOL commit_changes )
200 UINT ret = ERROR_SUCCESS;
202 if (state->database_handle == 0)
203 return;
204 if (commit_changes)
205 ret = MsiDatabaseCommit( state->database_handle );
206 if (ret != ERROR_SUCCESS)
208 ERR( "Failed to commit changes to database.\n" );
209 return;
211 ret = MsiCloseHandle( state->database_handle );
212 if (ret != ERROR_SUCCESS)
214 WARN( "Failed to close database handle.\n" );
215 return;
219 static const WCHAR *basenameW( const WCHAR *filename )
221 const WCHAR *dir_end;
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;
230 static int add_stream( struct msidb_state *state, const WCHAR *stream_filename )
232 MSIHANDLE view = 0, record = 0;
233 UINT ret;
235 ret = MsiDatabaseOpenViewW( state->database_handle, L"INSERT INTO _Streams (Name, Data) VALUES (?, ?)", &view );
236 if (ret != ERROR_SUCCESS)
238 ERR( "Failed to open _Streams table.\n" );
239 goto cleanup;
241 record = MsiCreateRecord( 2 );
242 if (record == 0)
244 ERR( "Failed to create MSI record.\n" );
245 ret = ERROR_OUTOFMEMORY;
246 goto cleanup;
248 ret = MsiRecordSetStringW( record, 1, basenameW( stream_filename ) );
249 if (ret != ERROR_SUCCESS)
251 ERR( "Failed to add stream filename to MSI record.\n" );
252 goto cleanup;
254 ret = MsiRecordSetStreamW( record, 2, stream_filename );
255 if (ret != ERROR_SUCCESS)
257 ERR( "Failed to add stream to MSI record.\n" );
258 goto cleanup;
260 ret = MsiViewExecute( view, record );
261 if (ret != ERROR_SUCCESS)
263 ERR( "Failed to add stream to _Streams table.\n" );
264 goto cleanup;
267 cleanup:
268 if (record)
269 MsiCloseHandle( record );
270 if (view)
271 MsiViewClose( view );
273 return (ret == ERROR_SUCCESS);
276 static int add_streams( struct msidb_state *state )
278 struct msidb_listentry *data;
280 LIST_FOR_EACH_ENTRY( data, &state->add_stream_list, struct msidb_listentry, entry )
282 if (!add_stream( state, data->name ))
283 return 0; /* failed, do not commit changes */
285 return 1;
288 static int kill_stream( struct msidb_state *state, const WCHAR *stream_filename )
290 MSIHANDLE view = 0, record = 0;
291 UINT ret;
293 ret = MsiDatabaseOpenViewW( state->database_handle, L"DELETE FROM _Streams WHERE Name = ?", &view );
294 if (ret != ERROR_SUCCESS)
296 ERR( "Failed to open _Streams table.\n" );
297 goto cleanup;
299 record = MsiCreateRecord( 1 );
300 if (record == 0)
302 ERR( "Failed to create MSI record.\n" );
303 ret = ERROR_OUTOFMEMORY;
304 goto cleanup;
306 ret = MsiRecordSetStringW( record, 1, stream_filename );
307 if (ret != ERROR_SUCCESS)
309 ERR( "Failed to add stream filename to MSI record.\n" );
310 goto cleanup;
312 ret = MsiViewExecute( view, record );
313 if (ret != ERROR_SUCCESS)
315 ERR( "Failed to delete stream from _Streams table.\n" );
316 goto cleanup;
319 cleanup:
320 if (record)
321 MsiCloseHandle( record );
322 if (view)
323 MsiViewClose( view );
325 return (ret == ERROR_SUCCESS);
328 static int kill_streams( struct msidb_state *state )
330 struct msidb_listentry *data;
332 LIST_FOR_EACH_ENTRY( data, &state->kill_stream_list, struct msidb_listentry, entry )
334 if (!kill_stream( state, data->name ))
335 return 0; /* failed, do not commit changes */
337 return 1;
340 static int extract_stream( struct msidb_state *state, const WCHAR *stream_filename )
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;
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)
352 ret = ERROR_FILE_NOT_FOUND;
353 ERR( "Failed to open destination file %s.\n", wine_dbgstr_w(stream_filename) );
354 goto cleanup;
356 ret = MsiDatabaseOpenViewW( state->database_handle, L"SELECT Data FROM _Streams WHERE Name = ?", &view );
357 if (ret != ERROR_SUCCESS)
359 ERR( "Failed to open _Streams table.\n" );
360 goto cleanup;
362 record = MsiCreateRecord( 1 );
363 if (record == 0)
365 ERR( "Failed to create MSI record.\n" );
366 ret = ERROR_OUTOFMEMORY;
367 goto cleanup;
369 ret = MsiRecordSetStringW( record, 1, stream_filename );
370 if (ret != ERROR_SUCCESS)
372 ERR( "Failed to add stream filename to MSI record.\n" );
373 goto cleanup;
375 ret = MsiViewExecute( view, record );
376 if (ret != ERROR_SUCCESS)
378 ERR( "Failed to query stream from _Streams table.\n" );
379 goto cleanup;
381 MsiCloseHandle( record );
382 record = 0;
383 ret = MsiViewFetch( view, &record );
384 if (ret != ERROR_SUCCESS)
386 ERR( "Failed to query row from _Streams table.\n" );
387 goto cleanup;
389 read_size = sizeof(buffer);
390 while (read_size == sizeof(buffer))
392 ret = MsiRecordReadStream( record, 1, buffer, &read_size );
393 if (ret != ERROR_SUCCESS)
395 ERR( "Failed to read stream from _Streams table.\n" );
396 goto cleanup;
398 if (!WriteFile( file, buffer, read_size, &write_size, NULL ) || read_size != write_size)
400 ret = ERROR_WRITE_FAULT;
401 ERR( "Failed to write stream to destination file.\n" );
402 goto cleanup;
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);
416 static int extract_streams( struct msidb_state *state )
418 struct msidb_listentry *data;
420 LIST_FOR_EACH_ENTRY( data, &state->extract_stream_list, struct msidb_listentry, entry )
422 if (!extract_stream( state, data->name ))
423 return 0; /* failed, do not commit changes */
425 return 1;
428 static int import_table( struct msidb_state *state, const WCHAR *table_path )
430 UINT ret;
432 ret = MsiDatabaseImportW( state->database_handle, state->table_folder, table_path );
433 if (ret != ERROR_SUCCESS)
435 ERR( "Failed to import table '%s', error %d.\n", wine_dbgstr_w(table_path), ret );
436 return 0;
438 return 1;
441 static int import_tables( struct msidb_state *state )
443 struct msidb_listentry *data;
445 LIST_FOR_EACH_ENTRY( data, &state->table_list, struct msidb_listentry, entry )
447 WCHAR *table_name = data->name;
448 WCHAR table_path[MAX_PATH];
449 WCHAR *ext;
451 /* permit specifying tables with wildcards ('Feature*') */
452 if (wcsstr( table_name, L"*" ) != NULL)
454 WIN32_FIND_DATAW f;
455 HANDLE handle;
456 WCHAR *path;
457 DWORD len;
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;
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 ))
479 FindClose( handle );
480 return 0; /* failed, do not commit changes */
482 } while (FindNextFileW( handle, &f ));
483 FindClose( handle );
484 continue;
486 /* permit specifying tables by filename (*.idt) */
487 if ((ext = PathFindExtensionW( table_name )) == NULL || lstrcmpW( ext, L".idt" ) != 0)
489 /* truncate to 8 characters */
490 swprintf( table_path, ARRAY_SIZE(table_path), L"%.8s.idt", table_name );
491 table_name = table_path;
493 if (!import_table( state, table_name ))
494 return 0; /* failed, do not commit changes */
496 return 1;
499 static int export_table( struct msidb_state *state, const WCHAR *table_name )
501 const WCHAR *format = (state->short_filenames ? L"%.8s.idt" : L"%s.idt");
502 WCHAR table_path[MAX_PATH];
503 UINT ret;
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)
509 ERR( "Failed to export table '%s', error %d.\n", wine_dbgstr_w(table_name), ret );
510 return 0;
512 return 1;
515 static int export_all_tables( struct msidb_state *state )
517 MSIHANDLE view = 0;
518 UINT ret;
520 ret = MsiDatabaseOpenViewW( state->database_handle, L"SELECT Name FROM _Tables", &view );
521 if (ret != ERROR_SUCCESS)
523 ERR( "Failed to open _Tables table.\n" );
524 goto cleanup;
526 ret = MsiViewExecute( view, 0 );
527 if (ret != ERROR_SUCCESS)
529 ERR( "Failed to query list from _Tables table.\n" );
530 goto cleanup;
532 while( 1 )
534 MSIHANDLE record = 0;
535 WCHAR table[256];
536 DWORD size;
538 ret = MsiViewFetch( view, &record );
539 if (ret == ERROR_NO_MORE_ITEMS)
540 break;
541 if (ret != ERROR_SUCCESS)
543 ERR( "Failed to query row from _Tables table.\n" );
544 goto cleanup;
546 size = ARRAY_SIZE(table);
547 ret = MsiRecordGetStringW( record, 1, table, &size );
548 if (ret != ERROR_SUCCESS)
550 ERR( "Failed to retrieve name string.\n" );
551 goto cleanup;
553 if (!export_table( state, table ))
555 ret = ERROR_FUNCTION_FAILED;
556 goto cleanup;
558 ret = MsiCloseHandle( record );
559 if (ret != ERROR_SUCCESS)
561 ERR( "Failed to close record handle.\n" );
562 goto cleanup;
565 ret = ERROR_SUCCESS;
566 /* the _SummaryInformation table is not listed in _Tables */
567 if (!export_table( state, L"_SummaryInformation" ))
569 ret = ERROR_FUNCTION_FAILED;
570 goto cleanup;
573 cleanup:
574 if (view && MsiViewClose( view ) != ERROR_SUCCESS)
576 ERR( "Failed to close _Streams table.\n" );
577 return 0;
579 return (ret == ERROR_SUCCESS);
582 static int export_tables( struct msidb_state *state )
584 struct msidb_listentry *data;
586 LIST_FOR_EACH_ENTRY( data, &state->table_list, struct msidb_listentry, entry )
588 if (lstrcmpW( data->name, L"*" ) == 0)
590 if (!export_all_tables( state ))
591 return 0; /* failed, do not commit changes */
592 continue;
594 if (!export_table( state, data->name ))
595 return 0; /* failed, do not commit changes */
597 return 1;
600 int __cdecl wmain( int argc, WCHAR *argv[] )
602 struct msidb_state state;
603 int i, n = 1;
604 int ret = 1;
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;
620 /* perform the requested operations */
621 if (!open_database( &state ))
623 ERR( "Failed to open database '%s'.\n", wine_dbgstr_w(state.database_file) );
624 goto cleanup;
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;
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;