- action.c is getting too big, so split out all the handling of
[wine.git] / dlls / msi / custom.c
blobe9a2e6c725801a4efefaa4400dcb0d1688b7c942
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 "msiquery.h"
40 #include "msvcrt/fcntl.h"
41 #include "objbase.h"
42 #include "objidl.h"
43 #include "msipriv.h"
44 #include "winnls.h"
45 #include "winuser.h"
46 #include "shlobj.h"
47 #include "wine/unicode.h"
48 #include "ver.h"
49 #include "action.h"
51 WINE_DEFAULT_DEBUG_CHANNEL(msi);
53 #define CUSTOM_ACTION_TYPE_MASK 0x3F
54 static const WCHAR c_collen[] = {'C',':','\\',0};
55 static const WCHAR cszTempFolder[]= {'T','e','m','p','F','o','l','d','e','r',0};
57 typedef struct tagMSIRUNNINGACTION
59 HANDLE handle;
60 BOOL process;
61 LPWSTR name;
62 } MSIRUNNINGACTION;
64 static UINT HANDLE_CustomType1(MSIPACKAGE *package, LPCWSTR source,
65 LPCWSTR target, const INT type, LPCWSTR action);
66 static UINT HANDLE_CustomType2(MSIPACKAGE *package, LPCWSTR source,
67 LPCWSTR target, const INT type, LPCWSTR action);
68 static UINT HANDLE_CustomType18(MSIPACKAGE *package, LPCWSTR source,
69 LPCWSTR target, const INT type, LPCWSTR action);
70 static UINT HANDLE_CustomType50(MSIPACKAGE *package, LPCWSTR source,
71 LPCWSTR target, const INT type, LPCWSTR action);
72 static UINT HANDLE_CustomType34(MSIPACKAGE *package, LPCWSTR source,
73 LPCWSTR target, const INT type, LPCWSTR action);
75 UINT ACTION_CustomAction(MSIPACKAGE *package,LPCWSTR action, BOOL execute)
77 UINT rc = ERROR_SUCCESS;
78 MSIQUERY * view;
79 MSIRECORD * row = 0;
80 static const WCHAR ExecSeqQuery[] =
81 {'s','e','l','e','c','t',' ','*',' ','f','r','o','m',' ','C','u','s','t','o'
82 ,'m','A','c','t','i','o','n',' ','w','h','e','r','e',' ','`','A','c','t','i'
83 ,'o','n','`',' ','=',' ','`','%','s','`',0};
84 UINT type;
85 LPWSTR source;
86 LPWSTR target;
87 WCHAR *deformated=NULL;
89 rc = MSI_OpenQuery(package->db, &view, ExecSeqQuery, action);
90 if (rc != ERROR_SUCCESS)
91 return rc;
93 rc = MSI_ViewExecute(view, 0);
94 if (rc != ERROR_SUCCESS)
96 MSI_ViewClose(view);
97 msiobj_release(&view->hdr);
98 return rc;
101 rc = MSI_ViewFetch(view,&row);
102 if (rc != ERROR_SUCCESS)
104 MSI_ViewClose(view);
105 msiobj_release(&view->hdr);
106 return rc;
109 type = MSI_RecordGetInteger(row,2);
111 source = load_dynamic_stringW(row,3);
112 target = load_dynamic_stringW(row,4);
114 TRACE("Handling custom action %s (%x %s %s)\n",debugstr_w(action),type,
115 debugstr_w(source), debugstr_w(target));
117 /* handle some of the deferred actions */
118 if (type & 0x400)
120 if (type & 0x100)
122 FIXME("Rollback only action... rollbacks not supported yet\n");
123 HeapFree(GetProcessHeap(),0,source);
124 HeapFree(GetProcessHeap(),0,target);
125 msiobj_release(&row->hdr);
126 MSI_ViewClose(view);
127 msiobj_release(&view->hdr);
128 return ERROR_SUCCESS;
130 if (!execute)
132 LPWSTR *newbuf = NULL;
133 INT count;
134 if (type & 0x200)
136 TRACE("Deferring Commit Action!\n");
137 count = package->CommitActionCount;
138 package->CommitActionCount++;
139 if (count != 0)
140 newbuf = HeapReAlloc(GetProcessHeap(),0,
141 package->CommitAction,
142 package->CommitActionCount * sizeof(LPWSTR));
143 else
144 newbuf = HeapAlloc(GetProcessHeap(),0, sizeof(LPWSTR));
146 newbuf[count] = dupstrW(action);
147 package->CommitAction = newbuf;
149 else
151 TRACE("Deferring Action!\n");
152 count = package->DeferredActionCount;
153 package->DeferredActionCount++;
154 if (count != 0)
155 newbuf = HeapReAlloc(GetProcessHeap(),0,
156 package->DeferredAction,
157 package->DeferredActionCount * sizeof(LPWSTR));
158 else
159 newbuf = HeapAlloc(GetProcessHeap(),0, sizeof(LPWSTR));
161 newbuf[count] = dupstrW(action);
162 package->DeferredAction = newbuf;
165 HeapFree(GetProcessHeap(),0,source);
166 HeapFree(GetProcessHeap(),0,target);
167 msiobj_release(&row->hdr);
168 MSI_ViewClose(view);
169 msiobj_release(&view->hdr);
170 return ERROR_SUCCESS;
172 else
174 /*Set ActionData*/
176 static const WCHAR szActionData[] = {
177 'C','u','s','t','o','m','A','c','t','i','o','n','D','a','t','a',0};
178 LPWSTR actiondata = load_dynamic_property(package,action,NULL);
179 if (actiondata)
180 MSI_SetPropertyW(package,szActionData,actiondata);
184 switch (type & CUSTOM_ACTION_TYPE_MASK)
186 case 1: /* DLL file stored in a Binary table stream */
187 rc = HANDLE_CustomType1(package,source,target,type,action);
188 break;
189 case 2: /* EXE file stored in a Binary table strem */
190 rc = HANDLE_CustomType2(package,source,target,type,action);
191 break;
192 case 18: /*EXE file installed with package */
193 rc = HANDLE_CustomType18(package,source,target,type,action);
194 break;
195 case 50: /*EXE file specified by a property value */
196 rc = HANDLE_CustomType50(package,source,target,type,action);
197 break;
198 case 34: /*EXE to be run in specified directory */
199 rc = HANDLE_CustomType34(package,source,target,type,action);
200 break;
201 case 35: /* Directory set with formatted text. */
202 deformat_string(package,target,&deformated);
203 MSI_SetTargetPathW(package, source, deformated);
204 HeapFree(GetProcessHeap(),0,deformated);
205 break;
206 case 51: /* Property set with formatted text. */
207 deformat_string(package,target,&deformated);
208 rc = MSI_SetPropertyW(package,source,deformated);
209 HeapFree(GetProcessHeap(),0,deformated);
210 break;
211 default:
212 FIXME("UNHANDLED ACTION TYPE %i (%s %s)\n",
213 type & CUSTOM_ACTION_TYPE_MASK, debugstr_w(source),
214 debugstr_w(target));
217 HeapFree(GetProcessHeap(),0,source);
218 HeapFree(GetProcessHeap(),0,target);
219 msiobj_release(&row->hdr);
220 MSI_ViewClose(view);
221 msiobj_release(&view->hdr);
222 return rc;
226 static UINT store_binary_to_temp(MSIPACKAGE *package, LPCWSTR source,
227 LPWSTR tmp_file)
229 DWORD sz=MAX_PATH;
231 if (MSI_GetPropertyW(package, cszTempFolder, tmp_file, &sz)
232 != ERROR_SUCCESS)
233 GetTempPathW(MAX_PATH,tmp_file);
235 strcatW(tmp_file,source);
237 if (GetFileAttributesW(tmp_file) != INVALID_FILE_ATTRIBUTES)
239 TRACE("File already exists\n");
240 return ERROR_SUCCESS;
242 else
244 /* write out the file */
245 UINT rc;
246 MSIQUERY * view;
247 MSIRECORD * row = 0;
248 static const WCHAR fmt[] =
249 {'s','e','l','e','c','t',' ','*',' ','f','r','o','m',' ','B','i'
250 ,'n','a','r','y',' ','w','h','e','r','e',' ','N','a','m','e','=','`','%','s','`',0};
251 HANDLE the_file;
252 CHAR buffer[1024];
254 if (track_tempfile(package, source, tmp_file)!=0)
255 FIXME("File Name in temp tracking collision\n");
257 the_file = CreateFileW(tmp_file, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS,
258 FILE_ATTRIBUTE_NORMAL, NULL);
260 if (the_file == INVALID_HANDLE_VALUE)
261 return ERROR_FUNCTION_FAILED;
263 rc = MSI_OpenQuery(package->db, &view, fmt, source);
264 if (rc != ERROR_SUCCESS)
265 return rc;
267 rc = MSI_ViewExecute(view, 0);
268 if (rc != ERROR_SUCCESS)
270 MSI_ViewClose(view);
271 msiobj_release(&view->hdr);
272 return rc;
275 rc = MSI_ViewFetch(view,&row);
276 if (rc != ERROR_SUCCESS)
278 MSI_ViewClose(view);
279 msiobj_release(&view->hdr);
280 return rc;
285 DWORD write;
286 sz = 1024;
287 rc = MSI_RecordReadStream(row,2,buffer,&sz);
288 if (rc != ERROR_SUCCESS)
290 ERR("Failed to get stream\n");
291 CloseHandle(the_file);
292 DeleteFileW(tmp_file);
293 break;
295 WriteFile(the_file,buffer,sz,&write,NULL);
296 } while (sz == 1024);
298 CloseHandle(the_file);
300 msiobj_release(&row->hdr);
301 MSI_ViewClose(view);
302 msiobj_release(&view->hdr);
305 return ERROR_SUCCESS;
308 static void file_running_action(MSIPACKAGE* package, HANDLE Handle,
309 BOOL process, LPCWSTR name)
311 MSIRUNNINGACTION *newbuf = NULL;
312 INT count;
313 count = package->RunningActionCount;
314 package->RunningActionCount++;
315 if (count != 0)
316 newbuf = HeapReAlloc(GetProcessHeap(),0,
317 package->RunningAction,
318 package->RunningActionCount * sizeof(MSIRUNNINGACTION));
319 else
320 newbuf = HeapAlloc(GetProcessHeap(),0, sizeof(MSIRUNNINGACTION));
322 newbuf[count].handle = Handle;
323 newbuf[count].process = process;
324 newbuf[count].name = dupstrW(name);
326 package->RunningAction = newbuf;
329 static UINT process_action_return_value(UINT type, HANDLE ThreadHandle)
331 DWORD rc;
333 if (type == 2)
335 GetExitCodeProcess(ThreadHandle,&rc);
337 if (rc == 0)
338 return ERROR_SUCCESS;
339 else
340 return ERROR_FUNCTION_FAILED;
343 GetExitCodeThread(ThreadHandle,&rc);
345 switch (rc)
347 case ERROR_FUNCTION_NOT_CALLED:
348 case ERROR_SUCCESS:
349 case ERROR_INSTALL_USEREXIT:
350 case ERROR_INSTALL_FAILURE:
351 return rc;
352 case ERROR_NO_MORE_ITEMS:
353 return ERROR_SUCCESS;
354 default:
355 return ERROR_FUNCTION_FAILED;
359 static UINT process_handle(MSIPACKAGE* package, UINT type,
360 HANDLE ThreadHandle, HANDLE ProcessHandle,
361 LPCWSTR Name)
363 UINT rc = ERROR_SUCCESS;
365 if (!(type & 0x80))
367 /* syncronous */
368 TRACE("Syncronous Execution of action %s\n",debugstr_w(Name));
369 if (ProcessHandle)
370 WaitForSingleObject(ProcessHandle,INFINITE);
371 else
372 WaitForSingleObject(ThreadHandle,INFINITE);
374 if (!(type & 0x40))
376 if (ProcessHandle)
377 rc = process_action_return_value(2,ProcessHandle);
378 else
379 rc = process_action_return_value(1,ThreadHandle);
382 CloseHandle(ThreadHandle);
383 if (ProcessHandle);
384 CloseHandle(ProcessHandle);
386 else
388 TRACE("Asyncronous Execution of action %s\n",debugstr_w(Name));
389 /* asyncronous */
390 if (type & 0x40)
392 if (ProcessHandle)
394 file_running_action(package, ProcessHandle, TRUE, Name);
395 CloseHandle(ThreadHandle);
397 else
398 file_running_action(package, ThreadHandle, FALSE, Name);
400 else
402 CloseHandle(ThreadHandle);
403 if (ProcessHandle);
404 CloseHandle(ProcessHandle);
408 return rc;
412 typedef UINT __stdcall CustomEntry(MSIHANDLE);
414 typedef struct
416 MSIPACKAGE *package;
417 WCHAR *target;
418 WCHAR *source;
419 } thread_struct;
421 static DWORD WINAPI ACTION_CallDllFunction(thread_struct *stuff)
423 HANDLE hModule;
424 LPSTR proc;
425 CustomEntry *fn;
426 DWORD rc = ERROR_SUCCESS;
428 TRACE("calling function (%s, %s) \n", debugstr_w(stuff->source),
429 debugstr_w(stuff->target));
431 hModule = LoadLibraryW(stuff->source);
432 if (hModule)
434 proc = strdupWtoA( stuff->target );
435 fn = (CustomEntry*)GetProcAddress(hModule,proc);
436 if (fn)
438 MSIHANDLE hPackage;
439 MSIPACKAGE *package = stuff->package;
441 TRACE("Calling function %s\n", proc);
442 hPackage = msiobj_findhandle( &package->hdr );
443 if (hPackage )
445 rc = fn(hPackage);
446 msiobj_release( &package->hdr );
448 else
449 ERR("Handle for object %p not found\n", package );
451 else
452 ERR("Cannot load functon\n");
454 HeapFree(GetProcessHeap(),0,proc);
455 FreeLibrary(hModule);
457 else
458 ERR("Unable to load library\n");
459 msiobj_release( &stuff->package->hdr );
460 HeapFree(GetProcessHeap(),0,stuff->source);
461 HeapFree(GetProcessHeap(),0,stuff->target);
462 HeapFree(GetProcessHeap(), 0, stuff);
463 return rc;
466 static DWORD WINAPI DllThread(LPVOID info)
468 thread_struct *stuff;
469 DWORD rc = 0;
471 TRACE("MSI Thread (0x%lx) started for custom action\n",
472 GetCurrentThreadId());
474 stuff = (thread_struct*)info;
475 rc = ACTION_CallDllFunction(stuff);
477 TRACE("MSI Thread (0x%lx) finished\n",GetCurrentThreadId());
478 /* clse all handles for this thread */
479 MsiCloseAllHandles();
480 return rc;
483 static UINT HANDLE_CustomType1(MSIPACKAGE *package, LPCWSTR source,
484 LPCWSTR target, const INT type, LPCWSTR action)
486 WCHAR tmp_file[MAX_PATH];
487 thread_struct *info;
488 DWORD ThreadId;
489 HANDLE ThreadHandle;
490 UINT rc = ERROR_SUCCESS;
492 store_binary_to_temp(package, source, tmp_file);
494 TRACE("Calling function %s from %s\n",debugstr_w(target),
495 debugstr_w(tmp_file));
497 if (!strchrW(tmp_file,'.'))
499 static const WCHAR dot[]={'.',0};
500 strcatW(tmp_file,dot);
503 info = HeapAlloc( GetProcessHeap(), 0, sizeof(*info) );
504 msiobj_addref( &package->hdr );
505 info->package = package;
506 info->target = dupstrW(target);
507 info->source = dupstrW(tmp_file);
509 ThreadHandle = CreateThread(NULL,0,DllThread,(LPVOID)info,0,&ThreadId);
511 rc = process_handle(package, type, ThreadHandle, NULL, action);
513 return rc;
516 static UINT HANDLE_CustomType2(MSIPACKAGE *package, LPCWSTR source,
517 LPCWSTR target, const INT type, LPCWSTR action)
519 WCHAR tmp_file[MAX_PATH];
520 STARTUPINFOW si;
521 PROCESS_INFORMATION info;
522 BOOL rc;
523 INT len;
524 WCHAR *deformated;
525 WCHAR *cmd;
526 static const WCHAR spc[] = {' ',0};
527 UINT prc = ERROR_SUCCESS;
529 memset(&si,0,sizeof(STARTUPINFOW));
531 store_binary_to_temp(package, source, tmp_file);
533 deformat_string(package,target,&deformated);
535 len = strlenW(tmp_file)+2;
537 if (deformated)
538 len += strlenW(deformated);
540 cmd = (WCHAR*)HeapAlloc(GetProcessHeap(),0,sizeof(WCHAR)*len);
542 strcpyW(cmd,tmp_file);
543 if (deformated)
545 strcatW(cmd,spc);
546 strcatW(cmd,deformated);
548 HeapFree(GetProcessHeap(),0,deformated);
551 TRACE("executing exe %s \n",debugstr_w(cmd));
553 rc = CreateProcessW(NULL, cmd, NULL, NULL, FALSE, 0, NULL,
554 c_collen, &si, &info);
556 HeapFree(GetProcessHeap(),0,cmd);
558 if ( !rc )
560 ERR("Unable to execute command\n");
561 return ERROR_SUCCESS;
564 prc = process_handle(package, type, info.hThread, info.hProcess, action);
566 return prc;
569 static UINT HANDLE_CustomType18(MSIPACKAGE *package, LPCWSTR source,
570 LPCWSTR target, const INT type, LPCWSTR action)
572 STARTUPINFOW si;
573 PROCESS_INFORMATION info;
574 BOOL rc;
575 WCHAR *deformated;
576 WCHAR *cmd;
577 INT len;
578 static const WCHAR spc[] = {' ',0};
579 int index;
580 UINT prc;
582 memset(&si,0,sizeof(STARTUPINFOW));
584 index = get_loaded_file(package,source);
586 len = strlenW(package->files[index].TargetPath);
588 deformat_string(package,target,&deformated);
589 if (deformated)
590 len += strlenW(deformated);
591 len += 2;
593 cmd = (WCHAR*)HeapAlloc(GetProcessHeap(),0,len * sizeof(WCHAR));
595 strcpyW(cmd, package->files[index].TargetPath);
596 if (deformated)
598 strcatW(cmd, spc);
599 strcatW(cmd, deformated);
601 HeapFree(GetProcessHeap(),0,deformated);
604 TRACE("executing exe %s \n",debugstr_w(cmd));
606 rc = CreateProcessW(NULL, cmd, NULL, NULL, FALSE, 0, NULL,
607 c_collen, &si, &info);
609 HeapFree(GetProcessHeap(),0,cmd);
611 if ( !rc )
613 ERR("Unable to execute command\n");
614 return ERROR_SUCCESS;
617 prc = process_handle(package, type, info.hThread, info.hProcess, action);
619 return prc;
622 static UINT HANDLE_CustomType50(MSIPACKAGE *package, LPCWSTR source,
623 LPCWSTR target, const INT type, LPCWSTR action)
625 STARTUPINFOW si;
626 PROCESS_INFORMATION info;
627 WCHAR *prop;
628 BOOL rc;
629 WCHAR *deformated;
630 WCHAR *cmd;
631 INT len;
632 UINT prc;
633 static const WCHAR spc[] = {' ',0};
635 memset(&si,0,sizeof(STARTUPINFOW));
636 memset(&info,0,sizeof(PROCESS_INFORMATION));
638 prop = load_dynamic_property(package,source,&prc);
639 if (!prop)
640 return prc;
642 deformat_string(package,target,&deformated);
643 len = strlenW(prop) + 2;
644 if (deformated)
645 len += strlenW(deformated);
647 cmd = (WCHAR*)HeapAlloc(GetProcessHeap(),0,sizeof(WCHAR)*len);
649 strcpyW(cmd,prop);
650 if (deformated)
652 strcatW(cmd,spc);
653 strcatW(cmd,deformated);
655 HeapFree(GetProcessHeap(),0,deformated);
658 TRACE("executing exe %s \n",debugstr_w(cmd));
660 rc = CreateProcessW(NULL, cmd, NULL, NULL, FALSE, 0, NULL,
661 c_collen, &si, &info);
663 HeapFree(GetProcessHeap(),0,cmd);
665 if ( !rc )
667 ERR("Unable to execute command\n");
668 return ERROR_SUCCESS;
671 prc = process_handle(package, type, info.hThread, info.hProcess, action);
673 return prc;
676 static UINT HANDLE_CustomType34(MSIPACKAGE *package, LPCWSTR source,
677 LPCWSTR target, const INT type, LPCWSTR action)
679 LPWSTR filename, deformated;
680 STARTUPINFOW si;
681 PROCESS_INFORMATION info;
682 BOOL rc;
683 UINT prc;
685 memset(&si,0,sizeof(STARTUPINFOW));
687 filename = resolve_folder(package, source, FALSE, FALSE, NULL);
689 if (!filename)
690 return ERROR_FUNCTION_FAILED;
692 SetCurrentDirectoryW(filename);
693 HeapFree(GetProcessHeap(),0,filename);
695 deformat_string(package,target,&deformated);
697 if (!deformated)
698 return ERROR_FUNCTION_FAILED;
700 TRACE("executing exe %s \n",debugstr_w(deformated));
702 rc = CreateProcessW(NULL, deformated, NULL, NULL, FALSE, 0, NULL,
703 c_collen, &si, &info);
704 HeapFree(GetProcessHeap(),0,deformated);
706 if ( !rc )
708 ERR("Unable to execute command\n");
709 return ERROR_SUCCESS;
712 prc = process_handle(package, type, info.hThread, info.hProcess, action);
714 return prc;
718 void ACTION_FinishCustomActions(MSIPACKAGE* package)
720 INT i;
721 DWORD rc;
723 for (i = 0; i < package->RunningActionCount; i++)
725 TRACE("Checking on action %s\n",
726 debugstr_w(package->RunningAction[i].name));
728 if (package->RunningAction[i].process)
729 GetExitCodeProcess(package->RunningAction[i].handle, &rc);
730 else
731 GetExitCodeThread(package->RunningAction[i].handle, &rc);
733 if (rc == STILL_ACTIVE)
735 TRACE("Waiting on action %s\n",
736 debugstr_w(package->RunningAction[i].name));
737 WaitForSingleObject(package->RunningAction[i].handle, INFINITE);
740 HeapFree(GetProcessHeap(),0,package->RunningAction[i].name);
741 CloseHandle(package->RunningAction[i].handle);
744 HeapFree(GetProcessHeap(),0,package->RunningAction);