msxml3: Use url monikers to load xml.
[wine/multimedia.git] / dlls / msi / custom.c
blob2ed4b81519d51935b282c95c868715fabed0bdd1
1 /*
2 * Custom Action processing for the Microsoft Installer (msi.dll)
4 * Copyright 2005 Aric Stewart 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 * Pages I need
24 http://msdn.microsoft.com/library/default.asp?url=/library/en-us/msi/setup/summary_list_of_all_custom_action_types.asp
27 #include <stdarg.h>
28 #include <stdio.h>
30 #define COBJMACROS
32 #include "windef.h"
33 #include "winbase.h"
34 #include "winerror.h"
35 #include "winreg.h"
36 #include "wine/debug.h"
37 #include "fdi.h"
38 #include "msi.h"
39 #include "msidefs.h"
40 #include "msiquery.h"
41 #include "msvcrt/fcntl.h"
42 #include "objbase.h"
43 #include "objidl.h"
44 #include "msipriv.h"
45 #include "winnls.h"
46 #include "winuser.h"
47 #include "shlobj.h"
48 #include "wine/unicode.h"
49 #include "winver.h"
50 #include "action.h"
52 WINE_DEFAULT_DEBUG_CHANNEL(msi);
54 #define CUSTOM_ACTION_TYPE_MASK 0x3F
55 static const WCHAR c_collen[] = {'C',':','\\',0};
56 static const WCHAR cszTempFolder[]= {'T','e','m','p','F','o','l','d','e','r',0};
58 typedef struct tagMSIRUNNINGACTION
60 struct list entry;
61 HANDLE handle;
62 BOOL process;
63 LPWSTR name;
64 } MSIRUNNINGACTION;
66 static UINT HANDLE_CustomType1(MSIPACKAGE *package, LPCWSTR source,
67 LPCWSTR target, const INT type, LPCWSTR action);
68 static UINT HANDLE_CustomType2(MSIPACKAGE *package, LPCWSTR source,
69 LPCWSTR target, const INT type, LPCWSTR action);
70 static UINT HANDLE_CustomType18(MSIPACKAGE *package, LPCWSTR source,
71 LPCWSTR target, const INT type, LPCWSTR action);
72 static UINT HANDLE_CustomType19(MSIPACKAGE *package, LPCWSTR source,
73 LPCWSTR target, const INT type, LPCWSTR action);
74 static UINT HANDLE_CustomType50(MSIPACKAGE *package, LPCWSTR source,
75 LPCWSTR target, const INT type, LPCWSTR action);
76 static UINT HANDLE_CustomType34(MSIPACKAGE *package, LPCWSTR source,
77 LPCWSTR target, const INT type, LPCWSTR action);
80 static BOOL check_execution_scheduling_options(MSIPACKAGE *package, LPCWSTR action, UINT options)
82 if (!package->script)
83 return TRUE;
85 if ((options & msidbCustomActionTypeClientRepeat) ==
86 msidbCustomActionTypeClientRepeat)
88 if (!(package->script->InWhatSequence & SEQUENCE_UI &&
89 package->script->InWhatSequence & SEQUENCE_EXEC))
91 TRACE("Skipping action due to dbCustomActionTypeClientRepeat option.\n");
92 return FALSE;
95 else if (options & msidbCustomActionTypeFirstSequence)
97 if (package->script->InWhatSequence & SEQUENCE_UI &&
98 package->script->InWhatSequence & SEQUENCE_EXEC )
100 TRACE("Skipping action due to msidbCustomActionTypeFirstSequence option.\n");
101 return FALSE;
104 else if (options & msidbCustomActionTypeOncePerProcess)
106 if (check_unique_action(package,action))
108 TRACE("Skipping action due to msidbCustomActionTypeOncePerProcess option.\n");
109 return FALSE;
111 else
112 register_unique_action(package,action);
115 return TRUE;
118 UINT ACTION_CustomAction(MSIPACKAGE *package,LPCWSTR action, BOOL execute)
120 UINT rc = ERROR_SUCCESS;
121 MSIRECORD * row = 0;
122 static const WCHAR ExecSeqQuery[] =
123 {'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
124 '`','C','u','s','t','o' ,'m','A','c','t','i','o','n','`',
125 ' ','W','H','E','R','E',' ','`','A','c','t','i' ,'o','n','`',' ',
126 '=',' ','\'','%','s','\'',0};
127 UINT type;
128 LPCWSTR source, target;
129 WCHAR *deformated=NULL;
131 row = MSI_QueryGetRecord( package->db, ExecSeqQuery, action );
132 if (!row)
133 return ERROR_CALL_NOT_IMPLEMENTED;
135 type = MSI_RecordGetInteger(row,2);
137 source = MSI_RecordGetString(row,3);
138 target = MSI_RecordGetString(row,4);
140 TRACE("Handling custom action %s (%x %s %s)\n",debugstr_w(action),type,
141 debugstr_w(source), debugstr_w(target));
143 /* handle some of the deferred actions */
144 if (type & msidbCustomActionTypeTSAware)
145 FIXME("msidbCustomActionTypeTSAware not handled\n");
147 if (type & msidbCustomActionTypeInScript)
149 if (type & msidbCustomActionTypeNoImpersonate)
150 FIXME("msidbCustomActionTypeNoImpersonate not handled\n");
152 if (type & msidbCustomActionTypeRollback)
154 FIXME("Rollback only action... rollbacks not supported yet\n");
155 schedule_action(package, ROLLBACK_SCRIPT, action);
156 rc = ERROR_SUCCESS;
157 goto end;
159 if (!execute)
161 if (type & msidbCustomActionTypeCommit)
163 TRACE("Deferring Commit Action!\n");
164 schedule_action(package, COMMIT_SCRIPT, action);
166 else
168 TRACE("Deferring Action!\n");
169 schedule_action(package, INSTALL_SCRIPT, action);
172 rc = ERROR_SUCCESS;
173 goto end;
175 else
177 /*Set ActionData*/
179 static const WCHAR szActionData[] = {
180 'C','u','s','t','o','m','A','c','t','i','o','n','D','a','t','a',0};
181 static const WCHAR szBlank[] = {0};
182 LPWSTR actiondata = msi_dup_property( package, action );
183 if (actiondata)
184 MSI_SetPropertyW(package,szActionData,actiondata);
185 else
186 MSI_SetPropertyW(package,szActionData,szBlank);
187 msi_free(actiondata);
190 else if (!check_execution_scheduling_options(package,action,type))
192 rc = ERROR_SUCCESS;
193 goto end;
196 switch (type & CUSTOM_ACTION_TYPE_MASK)
198 case 1: /* DLL file stored in a Binary table stream */
199 rc = HANDLE_CustomType1(package,source,target,type,action);
200 break;
201 case 2: /* EXE file stored in a Binary table strem */
202 rc = HANDLE_CustomType2(package,source,target,type,action);
203 break;
204 case 18: /*EXE file installed with package */
205 rc = HANDLE_CustomType18(package,source,target,type,action);
206 break;
207 case 19: /* Error that halts install */
208 rc = HANDLE_CustomType19(package,source,target,type,action);
209 break;
210 case 50: /*EXE file specified by a property value */
211 rc = HANDLE_CustomType50(package,source,target,type,action);
212 break;
213 case 34: /*EXE to be run in specified directory */
214 rc = HANDLE_CustomType34(package,source,target,type,action);
215 break;
216 case 35: /* Directory set with formatted text. */
217 deformat_string(package,target,&deformated);
218 MSI_SetTargetPathW(package, source, deformated);
219 msi_free(deformated);
220 break;
221 case 51: /* Property set with formatted text. */
222 deformat_string(package,target,&deformated);
223 rc = MSI_SetPropertyW(package,source,deformated);
224 msi_free(deformated);
225 break;
226 default:
227 FIXME("UNHANDLED ACTION TYPE %i (%s %s)\n",
228 type & CUSTOM_ACTION_TYPE_MASK, debugstr_w(source),
229 debugstr_w(target));
232 end:
233 msiobj_release(&row->hdr);
234 return rc;
238 static UINT store_binary_to_temp(MSIPACKAGE *package, LPCWSTR source,
239 LPWSTR tmp_file)
241 DWORD sz=MAX_PATH;
242 static const WCHAR f1[] = {'m','s','i',0};
243 WCHAR fmt[MAX_PATH];
245 if (MSI_GetPropertyW(package, cszTempFolder, fmt, &sz) != ERROR_SUCCESS)
246 GetTempPathW(MAX_PATH,fmt);
248 if (GetTempFileNameW(fmt,f1,0,tmp_file) == 0)
250 TRACE("Unable to create file\n");
251 return ERROR_FUNCTION_FAILED;
253 else
255 /* write out the file */
256 UINT rc;
257 MSIRECORD * row = 0;
258 static const WCHAR fmt[] =
259 {'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
260 '`','B','i' ,'n','a','r','y','`',' ','W','H','E','R','E',
261 ' ','`','N','a','m','e','`',' ','=',' ','\'','%','s','\'',0};
262 HANDLE the_file;
263 CHAR buffer[1024];
265 the_file = CreateFileW(tmp_file, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS,
266 FILE_ATTRIBUTE_NORMAL, NULL);
268 if (the_file == INVALID_HANDLE_VALUE)
269 return ERROR_FUNCTION_FAILED;
271 row = MSI_QueryGetRecord(package->db, fmt, source);
272 if (!row)
273 return ERROR_FUNCTION_FAILED;
277 DWORD write;
278 sz = 1024;
279 rc = MSI_RecordReadStream(row,2,buffer,&sz);
280 if (rc != ERROR_SUCCESS)
282 ERR("Failed to get stream\n");
283 CloseHandle(the_file);
284 DeleteFileW(tmp_file);
285 break;
287 WriteFile(the_file,buffer,sz,&write,NULL);
288 } while (sz == 1024);
290 CloseHandle(the_file);
292 msiobj_release(&row->hdr);
295 return ERROR_SUCCESS;
298 static void file_running_action(MSIPACKAGE* package, HANDLE Handle,
299 BOOL process, LPCWSTR name)
301 MSIRUNNINGACTION *action;
303 action = msi_alloc( sizeof(MSIRUNNINGACTION) );
305 action->handle = Handle;
306 action->process = process;
307 action->name = strdupW(name);
309 list_add_tail( &package->RunningActions, &action->entry );
312 static UINT process_action_return_value(UINT type, HANDLE ThreadHandle)
314 DWORD rc=0;
316 if (type == 2)
318 GetExitCodeProcess(ThreadHandle,&rc);
320 if (rc == 0)
321 return ERROR_SUCCESS;
322 else
323 return ERROR_FUNCTION_FAILED;
326 GetExitCodeThread(ThreadHandle,&rc);
328 switch (rc)
330 case ERROR_FUNCTION_NOT_CALLED:
331 case ERROR_SUCCESS:
332 case ERROR_INSTALL_USEREXIT:
333 case ERROR_INSTALL_FAILURE:
334 return rc;
335 case ERROR_NO_MORE_ITEMS:
336 return ERROR_SUCCESS;
337 default:
338 ERR("Invalid Return Code %ld\n",rc);
339 return ERROR_INSTALL_FAILURE;
343 static UINT process_handle(MSIPACKAGE* package, UINT type,
344 HANDLE ThreadHandle, HANDLE ProcessHandle,
345 LPCWSTR Name, BOOL *finished)
347 UINT rc = ERROR_SUCCESS;
349 if (!(type & msidbCustomActionTypeAsync))
351 /* synchronous */
352 TRACE("Synchronous Execution of action %s\n",debugstr_w(Name));
353 if (ProcessHandle)
354 msi_dialog_check_messages(ProcessHandle);
355 else
356 msi_dialog_check_messages(ThreadHandle);
358 if (!(type & msidbCustomActionTypeContinue))
360 if (ProcessHandle)
361 rc = process_action_return_value(2,ProcessHandle);
362 else
363 rc = process_action_return_value(1,ThreadHandle);
366 CloseHandle(ThreadHandle);
367 if (ProcessHandle)
368 CloseHandle(ProcessHandle);
369 if (finished)
370 *finished = TRUE;
372 else
374 TRACE("Asynchronous Execution of action %s\n",debugstr_w(Name));
375 /* asynchronous */
376 if (type & msidbCustomActionTypeContinue)
378 if (ProcessHandle)
380 file_running_action(package, ProcessHandle, TRUE, Name);
381 CloseHandle(ThreadHandle);
383 else
384 file_running_action(package, ThreadHandle, FALSE, Name);
386 else
388 CloseHandle(ThreadHandle);
389 if (ProcessHandle)
390 CloseHandle(ProcessHandle);
392 if (finished)
393 *finished = FALSE;
396 return rc;
400 typedef UINT __stdcall CustomEntry(MSIHANDLE);
402 typedef struct
404 MSIPACKAGE *package;
405 WCHAR *target;
406 WCHAR *source;
407 } thread_struct;
409 static DWORD WINAPI ACTION_CallDllFunction(thread_struct *stuff)
411 HANDLE hModule;
412 LPSTR proc;
413 CustomEntry *fn;
414 DWORD rc = ERROR_SUCCESS;
416 TRACE("calling function (%s, %s)\n", debugstr_w(stuff->source),
417 debugstr_w(stuff->target));
419 hModule = LoadLibraryW(stuff->source);
420 if (hModule)
422 proc = strdupWtoA( stuff->target );
423 fn = (CustomEntry*)GetProcAddress(hModule,proc);
424 if (fn)
426 MSIHANDLE hPackage;
427 MSIPACKAGE *package = stuff->package;
429 TRACE("Calling function %s\n", proc);
430 hPackage = alloc_msihandle( &package->hdr );
431 if (hPackage)
433 rc = fn( hPackage );
434 MsiCloseHandle( hPackage );
436 else
437 ERR("Handle for object %p not found\n", package );
439 else
440 ERR("Cannot load functon\n");
442 msi_free(proc);
443 FreeLibrary(hModule);
445 else
446 ERR("Unable to load library\n");
447 msiobj_release( &stuff->package->hdr );
448 msi_free(stuff->source);
449 msi_free(stuff->target);
450 msi_free(stuff);
451 return rc;
454 static DWORD WINAPI DllThread(LPVOID info)
456 thread_struct *stuff;
457 DWORD rc = 0;
459 TRACE("MSI Thread (0x%lx) started for custom action\n",
460 GetCurrentThreadId());
462 stuff = (thread_struct*)info;
463 rc = ACTION_CallDllFunction(stuff);
465 TRACE("MSI Thread (0x%lx) finished (rc %li)\n",GetCurrentThreadId(), rc);
466 /* clse all handles for this thread */
467 MsiCloseAllHandles();
468 return rc;
471 static UINT HANDLE_CustomType1(MSIPACKAGE *package, LPCWSTR source,
472 LPCWSTR target, const INT type, LPCWSTR action)
474 WCHAR tmp_file[MAX_PATH];
475 thread_struct *info;
476 DWORD ThreadId;
477 HANDLE ThreadHandle;
478 UINT rc = ERROR_SUCCESS;
479 BOOL finished = FALSE;
481 store_binary_to_temp(package, source, tmp_file);
483 TRACE("Calling function %s from %s\n",debugstr_w(target),
484 debugstr_w(tmp_file));
486 if (!strchrW(tmp_file,'.'))
488 static const WCHAR dot[]={'.',0};
489 strcatW(tmp_file,dot);
492 info = msi_alloc( sizeof(*info) );
493 msiobj_addref( &package->hdr );
494 info->package = package;
495 info->target = strdupW(target);
496 info->source = strdupW(tmp_file);
498 ThreadHandle = CreateThread(NULL,0,DllThread,(LPVOID)info,0,&ThreadId);
500 rc = process_handle(package, type, ThreadHandle, NULL, action, &finished );
502 if (!finished)
503 track_tempfile(package, tmp_file, tmp_file);
504 else
505 DeleteFileW(tmp_file);
507 return rc;
510 static UINT HANDLE_CustomType2(MSIPACKAGE *package, LPCWSTR source,
511 LPCWSTR target, const INT type, LPCWSTR action)
513 WCHAR tmp_file[MAX_PATH];
514 STARTUPINFOW si;
515 PROCESS_INFORMATION info;
516 BOOL rc;
517 INT len;
518 WCHAR *deformated;
519 WCHAR *cmd;
520 static const WCHAR spc[] = {' ',0};
521 UINT prc = ERROR_SUCCESS;
522 BOOL finished = FALSE;
524 memset(&si,0,sizeof(STARTUPINFOW));
526 store_binary_to_temp(package, source, tmp_file);
528 deformat_string(package,target,&deformated);
530 len = strlenW(tmp_file)+2;
532 if (deformated)
533 len += strlenW(deformated);
535 cmd = msi_alloc(sizeof(WCHAR)*len);
537 strcpyW(cmd,tmp_file);
538 if (deformated)
540 strcatW(cmd,spc);
541 strcatW(cmd,deformated);
543 msi_free(deformated);
546 TRACE("executing exe %s\n", debugstr_w(cmd));
548 rc = CreateProcessW(NULL, cmd, NULL, NULL, FALSE, 0, NULL,
549 c_collen, &si, &info);
552 if ( !rc )
554 ERR("Unable to execute command %s\n", debugstr_w(cmd));
555 msi_free(cmd);
556 return ERROR_SUCCESS;
558 msi_free(cmd);
560 prc = process_handle(package, type, info.hThread, info.hProcess, action,
561 &finished);
563 if (!finished)
564 track_tempfile(package, tmp_file, tmp_file);
565 else
566 DeleteFileW(tmp_file);
568 return prc;
571 static UINT HANDLE_CustomType18(MSIPACKAGE *package, LPCWSTR source,
572 LPCWSTR target, const INT type, LPCWSTR action)
574 STARTUPINFOW si;
575 PROCESS_INFORMATION info;
576 BOOL rc;
577 WCHAR *deformated;
578 WCHAR *cmd;
579 INT len;
580 static const WCHAR spc[] = {' ',0};
581 MSIFILE *file;
582 UINT prc;
584 memset(&si,0,sizeof(STARTUPINFOW));
586 file = get_loaded_file(package,source);
587 if( !file )
588 return ERROR_FUNCTION_FAILED;
590 len = lstrlenW( file->TargetPath );
592 deformat_string(package,target,&deformated);
593 if (deformated)
594 len += strlenW(deformated);
595 len += 2;
597 cmd = msi_alloc(len * sizeof(WCHAR));
599 lstrcpyW( cmd, file->TargetPath);
600 if (deformated)
602 strcatW(cmd, spc);
603 strcatW(cmd, deformated);
605 msi_free(deformated);
608 TRACE("executing exe %s\n", debugstr_w(cmd));
610 rc = CreateProcessW(NULL, cmd, NULL, NULL, FALSE, 0, NULL,
611 c_collen, &si, &info);
614 if ( !rc )
616 ERR("Unable to execute command %s\n", debugstr_w(cmd));
617 msi_free(cmd);
618 return ERROR_SUCCESS;
620 msi_free(cmd);
622 prc = process_handle(package, type, info.hThread, info.hProcess, action,
623 NULL);
625 return prc;
628 static UINT HANDLE_CustomType19(MSIPACKAGE *package, LPCWSTR source,
629 LPCWSTR target, const INT type, LPCWSTR action)
631 static const WCHAR query[] = {
632 'S','E','L','E','C','T',' ','`','M','e','s','s','a','g','e','`',' ',
633 'F','R','O','M',' ','`','E','r','r','o','r','`',' ',
634 'W','H','E','R','E',' ','`','E','r','r','o','r','`',' ','=',' ',
635 '%','s',0
637 MSIRECORD *row = 0;
638 LPWSTR deformated = NULL;
640 deformat_string( package, target, &deformated );
642 /* first try treat the error as a number */
643 row = MSI_QueryGetRecord( package->db, query, deformated );
644 if( row )
646 LPCWSTR error = MSI_RecordGetString( row, 1 );
647 MessageBoxW( NULL, error, NULL, MB_OK );
648 msiobj_release( &row->hdr );
650 else
651 MessageBoxW( NULL, deformated, NULL, MB_OK );
653 msi_free( deformated );
655 return ERROR_FUNCTION_FAILED;
658 static UINT HANDLE_CustomType50(MSIPACKAGE *package, LPCWSTR source,
659 LPCWSTR target, const INT type, LPCWSTR action)
661 STARTUPINFOW si;
662 PROCESS_INFORMATION info;
663 WCHAR *prop;
664 BOOL rc;
665 WCHAR *deformated;
666 WCHAR *cmd;
667 INT len;
668 static const WCHAR spc[] = {' ',0};
670 memset(&si,0,sizeof(STARTUPINFOW));
671 memset(&info,0,sizeof(PROCESS_INFORMATION));
673 prop = msi_dup_property( package, source );
674 if (!prop)
675 return ERROR_SUCCESS;
677 deformat_string(package,target,&deformated);
678 len = strlenW(prop) + 2;
679 if (deformated)
680 len += strlenW(deformated);
682 cmd = msi_alloc(sizeof(WCHAR)*len);
684 strcpyW(cmd,prop);
685 if (deformated)
687 strcatW(cmd,spc);
688 strcatW(cmd,deformated);
690 msi_free(deformated);
692 msi_free(prop);
694 TRACE("executing exe %s\n", debugstr_w(cmd));
696 rc = CreateProcessW(NULL, cmd, NULL, NULL, FALSE, 0, NULL,
697 c_collen, &si, &info);
700 if ( !rc )
702 ERR("Unable to execute command %s\n", debugstr_w(cmd));
703 msi_free(cmd);
704 return ERROR_SUCCESS;
706 msi_free(cmd);
708 return process_handle(package, type, info.hThread, info.hProcess, action, NULL);
711 static UINT HANDLE_CustomType34(MSIPACKAGE *package, LPCWSTR source,
712 LPCWSTR target, const INT type, LPCWSTR action)
714 LPWSTR filename, deformated;
715 STARTUPINFOW si;
716 PROCESS_INFORMATION info;
717 BOOL rc;
718 UINT prc;
720 memset(&si,0,sizeof(STARTUPINFOW));
722 filename = resolve_folder(package, source, FALSE, FALSE, NULL);
724 if (!filename)
725 return ERROR_FUNCTION_FAILED;
727 SetCurrentDirectoryW(filename);
728 msi_free(filename);
730 deformat_string(package,target,&deformated);
732 if (!deformated)
733 return ERROR_FUNCTION_FAILED;
735 TRACE("executing exe %s\n", debugstr_w(deformated));
737 rc = CreateProcessW(NULL, deformated, NULL, NULL, FALSE, 0, NULL,
738 c_collen, &si, &info);
740 if ( !rc )
742 ERR("Unable to execute command %s\n", debugstr_w(deformated));
743 msi_free(deformated);
744 return ERROR_SUCCESS;
746 msi_free(deformated);
748 prc = process_handle(package, type, info.hThread, info.hProcess, action,
749 NULL);
751 return prc;
755 void ACTION_FinishCustomActions(MSIPACKAGE* package)
757 struct list *item, *cursor;
758 DWORD rc;
760 LIST_FOR_EACH_SAFE( item, cursor, &package->RunningActions )
762 MSIRUNNINGACTION *action = LIST_ENTRY( item, MSIRUNNINGACTION, entry );
764 TRACE("Checking on action %s\n", debugstr_w(action->name));
766 list_remove( &action->entry );
768 if (action->process)
769 GetExitCodeProcess( action->handle, &rc );
770 else
771 GetExitCodeThread( action->handle, &rc );
773 if (rc == STILL_ACTIVE)
775 TRACE("Waiting on action %s\n", debugstr_w( action->name) );
776 msi_dialog_check_messages( action->handle );
779 CloseHandle( action->handle );
780 msi_free( action->name );
781 msi_free( action );