Add logic to be able to resolve the SourceDir based on
[wine] / dlls / msi / action.c
1 /*
2  * Implementation of the Microsoft Installer (msi.dll)
3  *
4  * Copyright 2004 Aric Stewart for CodeWeavers
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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
20
21 /*
22  * Pages I need
23  *
24 http://msdn.microsoft.com/library/default.asp?url=/library/en-us/msi/setup/installexecutesequence_table.asp
25
26 http://msdn.microsoft.com/library/default.asp?url=/library/en-us/msi/setup/standard_actions_reference.asp
27  */
28
29 #include <stdarg.h>
30 #include <stdio.h>
31
32 #define COBJMACROS
33
34 #include "windef.h"
35 #include "winbase.h"
36 #include "winerror.h"
37 #include "winreg.h"
38 #include "wine/debug.h"
39 #include "fdi.h"
40 #include "msi.h"
41 #include "msiquery.h"
42 #include "msvcrt/fcntl.h"
43 #include "objbase.h"
44 #include "objidl.h"
45 #include "msipriv.h"
46 #include "winnls.h"
47 #include "winuser.h"
48 #include "shlobj.h"
49 #include "wine/unicode.h"
50 #include "ver.h"
51 #include "action.h"
52
53 #define REG_PROGRESS_VALUE 13200
54 #define COMPONENT_PROGRESS_VALUE 24000
55
56 WINE_DEFAULT_DEBUG_CHANNEL(msi);
57
58 /*
59  * Prototypes
60  */
61 static UINT ACTION_ProcessExecSequence(MSIPACKAGE *package, BOOL UIran);
62 static UINT ACTION_ProcessUISequence(MSIPACKAGE *package);
63 static UINT ACTION_PerformActionSequence(MSIPACKAGE *package, UINT seq);
64 static UINT build_icon_path(MSIPACKAGE *package, LPCWSTR icon_name, 
65                             LPWSTR *FilePath);
66
67 /* 
68  * action handlers
69  */
70 typedef UINT (*STANDARDACTIONHANDLER)(MSIPACKAGE*);
71
72 static UINT ACTION_LaunchConditions(MSIPACKAGE *package);
73 static UINT ACTION_CostInitialize(MSIPACKAGE *package);
74 static UINT ACTION_CreateFolders(MSIPACKAGE *package);
75 static UINT ACTION_CostFinalize(MSIPACKAGE *package);
76 static UINT ACTION_FileCost(MSIPACKAGE *package);
77 static UINT ACTION_InstallFiles(MSIPACKAGE *package);
78 static UINT ACTION_DuplicateFiles(MSIPACKAGE *package);
79 static UINT ACTION_WriteRegistryValues(MSIPACKAGE *package);
80 static UINT ACTION_InstallInitialize(MSIPACKAGE *package);
81 static UINT ACTION_InstallValidate(MSIPACKAGE *package);
82 static UINT ACTION_ProcessComponents(MSIPACKAGE *package);
83 static UINT ACTION_RegisterTypeLibraries(MSIPACKAGE *package);
84 static UINT ACTION_RegisterClassInfo(MSIPACKAGE *package);
85 static UINT ACTION_RegisterProgIdInfo(MSIPACKAGE *package);
86 static UINT ACTION_CreateShortcuts(MSIPACKAGE *package);
87 static UINT ACTION_PublishProduct(MSIPACKAGE *package);
88 static UINT ACTION_WriteIniValues(MSIPACKAGE *package);
89 static UINT ACTION_SelfRegModules(MSIPACKAGE *package);
90 static UINT ACTION_PublishFeatures(MSIPACKAGE *package);
91 static UINT ACTION_RegisterProduct(MSIPACKAGE *package);
92 static UINT ACTION_InstallExecute(MSIPACKAGE *package);
93 static UINT ACTION_InstallFinalize(MSIPACKAGE *package);
94 static UINT ACTION_ForceReboot(MSIPACKAGE *package);
95 static UINT ACTION_ResolveSource(MSIPACKAGE *package);
96
97  
98 /*
99  * consts and values used
100  */
101 static const WCHAR cszSourceDir[] = {'S','o','u','r','c','e','D','i','r',0};
102 static const WCHAR cszRootDrive[] = {'R','O','O','T','D','R','I','V','E',0};
103 static const WCHAR cszTargetDir[] = {'T','A','R','G','E','T','D','I','R',0};
104 static const WCHAR cszTempFolder[]= {'T','e','m','p','F','o','l','d','e','r',0};
105 static const WCHAR cszDatabase[]={'D','A','T','A','B','A','S','E',0};
106 static const WCHAR c_collen[] = {'C',':','\\',0};
107  
108 static const WCHAR cszbs[]={'\\',0};
109
110 const static WCHAR szCreateFolders[] =
111 {'C','r','e','a','t','e','F','o','l','d','e','r','s',0};
112 const static WCHAR szCostFinalize[] =
113 {'C','o','s','t','F','i','n','a','l','i','z','e',0};
114 const static WCHAR szInstallFiles[] =
115 {'I','n','s','t','a','l','l','F','i','l','e','s',0};
116 const static WCHAR szDuplicateFiles[] =
117 {'D','u','p','l','i','c','a','t','e','F','i','l','e','s',0};
118 const static WCHAR szWriteRegistryValues[] =
119 {'W','r','i','t','e','R','e','g','i','s','t','r','y','V','a','l','u','e','s',0};
120 const static WCHAR szCostInitialize[] =
121 {'C','o','s','t','I','n','i','t','i','a','l','i','z','e',0};
122 const static WCHAR szFileCost[] = 
123 {'F','i','l','e','C','o','s','t',0};
124 const static WCHAR szInstallInitialize[] = 
125 {'I','n','s','t','a','l','l','I','n','i','t','i','a','l','i','z','e',0};
126 const static WCHAR szInstallValidate[] = 
127 {'I','n','s','t','a','l','l','V','a','l','i','d','a','t','e',0};
128 const static WCHAR szLaunchConditions[] = 
129 {'L','a','u','n','c','h','C','o','n','d','i','t','i','o','n','s',0};
130 const static WCHAR szProcessComponents[] = 
131 {'P','r','o','c','e','s','s','C','o','m','p','o','n','e','n','t','s',0};
132 const static WCHAR szRegisterTypeLibraries[] = 
133 {'R','e','g','i','s','t','e','r','T','y','p','e','L','i','b','r','a','r',
134 'i','e','s',0};
135 const static WCHAR szRegisterClassInfo[] = 
136 {'R','e','g','i','s','t','e','r','C','l','a','s','s','I','n','f','o',0};
137 const static WCHAR szRegisterProgIdInfo[] = 
138 {'R','e','g','i','s','t','e','r','P','r','o','g','I','d','I','n','f','o',0};
139 const static WCHAR szCreateShortcuts[] = 
140 {'C','r','e','a','t','e','S','h','o','r','t','c','u','t','s',0};
141 const static WCHAR szPublishProduct[] = 
142 {'P','u','b','l','i','s','h','P','r','o','d','u','c','t',0};
143 const static WCHAR szWriteIniValues[] = 
144 {'W','r','i','t','e','I','n','i','V','a','l','u','e','s',0};
145 const static WCHAR szSelfRegModules[] = 
146 {'S','e','l','f','R','e','g','M','o','d','u','l','e','s',0};
147 const static WCHAR szPublishFeatures[] = 
148 {'P','u','b','l','i','s','h','F','e','a','t','u','r','e','s',0};
149 const static WCHAR szRegisterProduct[] = 
150 {'R','e','g','i','s','t','e','r','P','r','o','d','u','c','t',0};
151 const static WCHAR szInstallExecute[] = 
152 {'I','n','s','t','a','l','l','E','x','e','c','u','t','e',0};
153 const static WCHAR szInstallExecuteAgain[] = 
154 {'I','n','s','t','a','l','l','E','x','e','c','u','t','e','A','g','a','i','n',0};
155 const static WCHAR szInstallFinalize[] = 
156 {'I','n','s','t','a','l','l','F','i','n','a','l','i','z','e',0};
157 const static WCHAR szForceReboot[] = 
158 {'F','o','r','c','e','R','e','b','o','o','t',0};
159 const static WCHAR szResolveSource[] =
160 {'R','e','s','o','l','v','e','S','o','u','r','c','e',0};
161 const static WCHAR szAppSearch[] = 
162 {'A','p','p','S','e','a','r','c','h',0};
163 const static WCHAR szAllocateRegistrySpace[] = 
164 {'A','l','l','o','c','a','t','e','R','e','g','i','s','t','r','y','S','p','a','c','e',0};
165 const static WCHAR szBindImage[] = 
166 {'B','i','n','d','I','m','a','g','e',0};
167 const static WCHAR szCCPSearch[] = 
168 {'C','C','P','S','e','a','r','c','h',0};
169 const static WCHAR szDeleteServices[] = 
170 {'D','e','l','e','t','e','S','e','r','v','i','c','e','s',0};
171 const static WCHAR szDisableRollback[] = 
172 {'D','i','s','a','b','l','e','R','o','l','l','b','a','c','k',0};
173 const static WCHAR szExecuteAction[] = 
174 {'E','x','e','c','u','t','e','A','c','t','i','o','n',0};
175 const static WCHAR szFindRelatedProducts[] = 
176 {'F','i','n','d','R','e','l','a','t','e','d','P','r','o','d','u','c','t','s',0};
177 const static WCHAR szInstallAdminPackage[] = 
178 {'I','n','s','t','a','l','l','A','d','m','i','n','P','a','c','k','a','g','e',0};
179 const static WCHAR szInstallSFPCatalogFile[] = 
180 {'I','n','s','t','a','l','l','S','F','P','C','a','t','a','l','o','g','F','i','l','e',0};
181 const static WCHAR szIsolateComponents[] = 
182 {'I','s','o','l','a','t','e','C','o','m','p','o','n','e','n','t','s',0};
183 const static WCHAR szMigrateFeatureStates[] = 
184 {'M','i','g','r','a','t','e','F','e','a','t','u','r','e','S','t','a','t','e','s',0};
185 const static WCHAR szMoveFiles[] = 
186 {'M','o','v','e','F','i','l','e','s',0};
187 const static WCHAR szMsiPublishAssemblies[] = 
188 {'M','s','i','P','u','b','l','i','s','h','A','s','s','e','m','b','l','i','e','s',0};
189 const static WCHAR szMsiUnpublishAssemblies[] = 
190 {'M','s','i','U','n','p','u','b','l','i','s','h','A','s','s','e','m','b','l','i','e','s',0};
191 const static WCHAR szInstallODBC[] = 
192 {'I','n','s','t','a','l','l','O','D','B','C',0};
193 const static WCHAR szInstallServices[] = 
194 {'I','n','s','t','a','l','l','S','e','r','v','i','c','e','s',0};
195 const static WCHAR szPatchFiles[] = 
196 {'P','a','t','c','h','F','i','l','e','s',0};
197 const static WCHAR szPublishComponents[] = 
198 {'P','u','b','l','i','s','h','C','o','m','p','o','n','e','n','t','s',0};
199 const static WCHAR szRegisterComPlus[] =
200 {'R','e','g','i','s','t','e','r','C','o','m','P','l','u','s',0};
201 const static WCHAR szRegisterExtensionInfo[] =
202 {'R','e','g','i','s','t','e','r','E','x','t','e','n','s','i','o','n','I','n','f','o',0};
203 const static WCHAR szRegisterFonts[] =
204 {'R','e','g','i','s','t','e','r','F','o','n','t','s',0};
205 const static WCHAR szRegisterMIMEInfo[] =
206 {'R','e','g','i','s','t','e','r','M','I','M','E','I','n','f','o',0};
207 const static WCHAR szRegisterUser[] =
208 {'R','e','g','i','s','t','e','r','U','s','e','r',0};
209 const static WCHAR szRemoveDuplicateFiles[] =
210 {'R','e','m','o','v','e','D','u','p','l','i','c','a','t','e','F','i','l','e','s',0};
211 const static WCHAR szRemoveEnvironmentStrings[] =
212 {'R','e','m','o','v','e','E','n','v','i','r','o','n','m','e','n','t','S','t','r','i','n','g','s',0};
213 const static WCHAR szRemoveExistingProducts[] =
214 {'R','e','m','o','v','e','E','x','i','s','t','i','n','g','P','r','o','d','u','c','t','s',0};
215 const static WCHAR szRemoveFiles[] =
216 {'R','e','m','o','v','e','F','i','l','e','s',0};
217 const static WCHAR szRemoveFolders[] =
218 {'R','e','m','o','v','e','F','o','l','d','e','r','s',0};
219 const static WCHAR szRemoveIniValues[] =
220 {'R','e','m','o','v','e','I','n','i','V','a','l','u','e','s',0};
221 const static WCHAR szRemoveODBC[] =
222 {'R','e','m','o','v','e','O','D','B','C',0};
223 const static WCHAR szRemoveRegistryValues[] =
224 {'R','e','m','o','v','e','R','e','g','i','s','t','r','y','V','a','l','u','e','s',0};
225 const static WCHAR szRemoveShortcuts[] =
226 {'R','e','m','o','v','e','S','h','o','r','t','c','u','t','s',0};
227 const static WCHAR szRMCCPSearch[] =
228 {'R','M','C','C','P','S','e','a','r','c','h',0};
229 const static WCHAR szScheduleReboot[] =
230 {'S','c','h','e','d','u','l','e','R','e','b','o','o','t',0};
231 const static WCHAR szSelfUnregModules[] =
232 {'S','e','l','f','U','n','r','e','g','M','o','d','u','l','e','s',0};
233 const static WCHAR szSetODBCFolders[] =
234 {'S','e','t','O','D','B','C','F','o','l','d','e','r','s',0};
235 const static WCHAR szStartServices[] =
236 {'S','t','a','r','t','S','e','r','v','i','c','e','s',0};
237 const static WCHAR szStopServices[] =
238 {'S','t','o','p','S','e','r','v','i','c','e','s',0};
239 const static WCHAR szUnpublishComponents[] =
240 {'U','n','p','u','b','l','i','s','h','C','o','m','p','o','n','e','n','t','s',0};
241 const static WCHAR szUnpublishFeatures[] =
242 {'U','n','p','u','b','l','i','s','h','F','e','a','t','u','r','e','s',0};
243 const static WCHAR szUnregisterClassInfo[] =
244 {'U','n','r','e','g','i','s','t','e','r','C','l','a','s','s','I','n','f','o',0};
245 const static WCHAR szUnregisterComPlus[] =
246 {'U','n','r','e','g','i','s','t','e','r','C','o','m','P','l','u','s',0};
247 const static WCHAR szUnregisterExtensionInfo[] =
248 {'U','n','r','e','g','i','s','t','e','r','E','x','t','e','n','s','i','o','n','I','n','f','o',0};
249 const static WCHAR szUnregisterFonts[] =
250 {'U','n','r','e','g','i','s','t','e','r','F','o','n','t','s',0};
251 const static WCHAR szUnregisterMIMEInfo[] =
252 {'U','n','r','e','g','i','s','t','e','r','M','I','M','E','I','n','f','o',0};
253 const static WCHAR szUnregisterProgIdInfo[] =
254 {'U','n','r','e','g','i','s','t','e','r','P','r','o','g','I','d','I','n','f','o',0};
255 const static WCHAR szUnregisterTypeLibraries[] =
256 {'U','n','r','e','g','i','s','t','e','r','T','y','p','e','L','i','b','r','a','r','i','e','s',0};
257 const static WCHAR szValidateProductID[] =
258 {'V','a','l','i','d','a','t','e','P','r','o','d','u','c','t','I','D',0};
259 const static WCHAR szWriteEnvironmentStrings[] =
260 {'W','r','i','t','e','E','n','v','i','r','o','n','m','e','n','t','S','t','r','i','n','g','s',0};
261
262 struct _actions {
263     LPCWSTR action;
264     STANDARDACTIONHANDLER handler;
265 };
266
267 struct _actions StandardActions[] = {
268     { szAllocateRegistrySpace, NULL},
269     { szAppSearch, ACTION_AppSearch },
270     { szBindImage, NULL},
271     { szCCPSearch, NULL},
272     { szCostFinalize, ACTION_CostFinalize },
273     { szCostInitialize, ACTION_CostInitialize },
274     { szCreateFolders, ACTION_CreateFolders },
275     { szCreateShortcuts, ACTION_CreateShortcuts },
276     { szDeleteServices, NULL},
277     { szDisableRollback, NULL},
278     { szDuplicateFiles, ACTION_DuplicateFiles},
279     { szExecuteAction, NULL},
280     { szFileCost, ACTION_FileCost },
281     { szFindRelatedProducts, NULL},
282     { szForceReboot, ACTION_ForceReboot },
283     { szInstallAdminPackage, NULL},
284     { szInstallExecute, ACTION_InstallExecute },
285     { szInstallExecuteAgain, ACTION_InstallExecute },
286     { szInstallFiles, ACTION_InstallFiles},
287     { szInstallFinalize, ACTION_InstallFinalize },
288     { szInstallInitialize, ACTION_InstallInitialize },
289     { szInstallSFPCatalogFile, NULL},
290     { szInstallValidate, ACTION_InstallValidate },
291     { szIsolateComponents, NULL},
292     { szLaunchConditions, ACTION_LaunchConditions },
293     { szMigrateFeatureStates, NULL},
294     { szMoveFiles, NULL},
295     { szMsiPublishAssemblies, NULL},
296     { szMsiUnpublishAssemblies, NULL},
297     { szInstallODBC, NULL},
298     { szInstallServices, NULL},
299     { szPatchFiles, NULL},
300     { szProcessComponents, ACTION_ProcessComponents },
301     { szPublishComponents, NULL},
302     { szPublishFeatures, ACTION_PublishFeatures },
303     { szPublishProduct, ACTION_PublishProduct },
304     { szRegisterClassInfo, ACTION_RegisterClassInfo },
305     { szRegisterComPlus, NULL},
306     { szRegisterExtensionInfo, NULL},
307     { szRegisterFonts, NULL},
308     { szRegisterMIMEInfo, NULL},
309     { szRegisterProduct, ACTION_RegisterProduct },
310     { szRegisterProgIdInfo, ACTION_RegisterProgIdInfo },
311     { szRegisterTypeLibraries, ACTION_RegisterTypeLibraries },
312     { szRegisterUser, NULL},
313     { szRemoveDuplicateFiles, NULL},
314     { szRemoveEnvironmentStrings, NULL},
315     { szRemoveExistingProducts, NULL},
316     { szRemoveFiles, NULL},
317     { szRemoveFolders, NULL},
318     { szRemoveIniValues, NULL},
319     { szRemoveODBC, NULL},
320     { szRemoveRegistryValues, NULL},
321     { szRemoveShortcuts, NULL},
322     { szResolveSource, ACTION_ResolveSource},
323     { szRMCCPSearch, NULL},
324     { szScheduleReboot, NULL},
325     { szSelfRegModules, ACTION_SelfRegModules },
326     { szSelfUnregModules, NULL},
327     { szSetODBCFolders, NULL},
328     { szStartServices, NULL},
329     { szStopServices, NULL},
330     { szUnpublishComponents, NULL},
331     { szUnpublishFeatures, NULL},
332     { szUnregisterClassInfo, NULL},
333     { szUnregisterComPlus, NULL},
334     { szUnregisterExtensionInfo, NULL},
335     { szUnregisterFonts, NULL},
336     { szUnregisterMIMEInfo, NULL},
337     { szUnregisterProgIdInfo, NULL},
338     { szUnregisterTypeLibraries, NULL},
339     { szValidateProductID, NULL},
340     { szWriteEnvironmentStrings, NULL},
341     { szWriteIniValues, ACTION_WriteIniValues },
342     { szWriteRegistryValues, ACTION_WriteRegistryValues},
343     { NULL, NULL},
344 };
345
346
347 /******************************************************** 
348  * helper functions to get around current HACKS and such
349  ********************************************************/
350 inline static void reduce_to_longfilename(WCHAR* filename)
351 {
352     LPWSTR p = strchrW(filename,'|');
353     if (p)
354         memmove(filename, p+1, (strlenW(p+1)+1)*sizeof(WCHAR));
355 }
356
357 WCHAR *load_dynamic_stringW(MSIRECORD *row, INT index)
358 {
359     UINT rc;
360     DWORD sz;
361     LPWSTR ret;
362    
363     sz = 0; 
364     if (MSI_RecordIsNull(row,index))
365         return NULL;
366
367     rc = MSI_RecordGetStringW(row,index,NULL,&sz);
368
369     /* having an empty string is different than NULL */
370     if (sz == 0)
371     {
372         ret = HeapAlloc(GetProcessHeap(),0,sizeof(WCHAR));
373         ret[0] = 0;
374         return ret;
375     }
376
377     sz ++;
378     ret = HeapAlloc(GetProcessHeap(),0,sz * sizeof (WCHAR));
379     rc = MSI_RecordGetStringW(row,index,ret,&sz);
380     if (rc!=ERROR_SUCCESS)
381     {
382         ERR("Unable to load dynamic string\n");
383         HeapFree(GetProcessHeap(), 0, ret);
384         ret = NULL;
385     }
386     return ret;
387 }
388
389 LPWSTR load_dynamic_property(MSIPACKAGE *package, LPCWSTR prop, UINT* rc)
390 {
391     DWORD sz = 0;
392     LPWSTR str;
393     UINT r;
394
395     r = MSI_GetPropertyW(package, prop, NULL, &sz);
396     if (r != ERROR_SUCCESS && r != ERROR_MORE_DATA)
397     {
398         if (rc)
399             *rc = r;
400         return NULL;
401     }
402     sz++;
403     str = HeapAlloc(GetProcessHeap(),0,sz*sizeof(WCHAR));
404     r = MSI_GetPropertyW(package, prop, str, &sz);
405     if (r != ERROR_SUCCESS)
406     {
407         HeapFree(GetProcessHeap(),0,str);
408         str = NULL;
409     }
410     if (rc)
411         *rc = r;
412     return str;
413 }
414
415 int get_loaded_component(MSIPACKAGE* package, LPCWSTR Component )
416 {
417     int rc = -1;
418     DWORD i;
419
420     for (i = 0; i < package->loaded_components; i++)
421     {
422         if (strcmpW(Component,package->components[i].Component)==0)
423         {
424             rc = i;
425             break;
426         }
427     }
428     return rc;
429 }
430
431 int get_loaded_feature(MSIPACKAGE* package, LPCWSTR Feature )
432 {
433     int rc = -1;
434     DWORD i;
435
436     for (i = 0; i < package->loaded_features; i++)
437     {
438         if (strcmpW(Feature,package->features[i].Feature)==0)
439         {
440             rc = i;
441             break;
442         }
443     }
444     return rc;
445 }
446
447 int get_loaded_file(MSIPACKAGE* package, LPCWSTR file)
448 {
449     int rc = -1;
450     DWORD i;
451
452     for (i = 0; i < package->loaded_files; i++)
453     {
454         if (strcmpW(file,package->files[i].File)==0)
455         {
456             rc = i;
457             break;
458         }
459     }
460     return rc;
461 }
462
463 int track_tempfile(MSIPACKAGE *package, LPCWSTR name, LPCWSTR path)
464 {
465     DWORD i;
466     DWORD index;
467
468     if (!package)
469         return -2;
470
471     for (i=0; i < package->loaded_files; i++)
472         if (strcmpW(package->files[i].File,name)==0)
473             return -1;
474
475     index = package->loaded_files;
476     package->loaded_files++;
477     if (package->loaded_files== 1)
478         package->files = HeapAlloc(GetProcessHeap(),0,sizeof(MSIFILE));
479     else
480         package->files = HeapReAlloc(GetProcessHeap(),0,
481             package->files , package->loaded_files * sizeof(MSIFILE));
482
483     memset(&package->files[index],0,sizeof(MSIFILE));
484
485     package->files[index].File = dupstrW(name);
486     package->files[index].TargetPath = dupstrW(path);
487     package->files[index].Temporary = TRUE;
488
489     TRACE("Tracking tempfile (%s)\n",debugstr_w(package->files[index].File));  
490
491     return 0;
492 }
493
494 static void remove_tracked_tempfiles(MSIPACKAGE* package)
495 {
496     DWORD i;
497
498     if (!package)
499         return;
500
501     for (i = 0; i < package->loaded_files; i++)
502     {
503         if (package->files[i].Temporary)
504         {
505             TRACE("Cleaning up %s\n",debugstr_w(package->files[i].TargetPath));
506             DeleteFileW(package->files[i].TargetPath);
507         }
508
509     }
510 }
511
512 /* wrapper to resist a need for a full rewrite right now */
513 DWORD deformat_string(MSIPACKAGE *package, LPCWSTR ptr, WCHAR** data )
514 {
515     if (ptr)
516     {
517         MSIRECORD *rec = MSI_CreateRecord(1);
518         DWORD size = 0;
519         WCHAR size_buf[2] = {' ',0};
520
521         MSI_RecordSetStringW(rec,0,ptr);
522         MSI_FormatRecordW(package,rec,size_buf,&size);
523         if (size >= 0)
524         {
525             size++;
526             *data = HeapAlloc(GetProcessHeap(),0,size*sizeof(WCHAR));
527             if (size > 1)
528                 MSI_FormatRecordW(package,rec,*data,&size);
529             else
530                 *data[0] = 0;
531             msiobj_release( &rec->hdr );
532             return sizeof(WCHAR)*size;
533         }
534         msiobj_release( &rec->hdr );
535     }
536
537     *data = NULL;
538     return 0;
539 }
540
541 /* Called when the package is being closed */
542 void ACTION_free_package_structures( MSIPACKAGE* package)
543 {
544     INT i;
545     
546     TRACE("Freeing package action data\n");
547
548     remove_tracked_tempfiles(package);
549
550     /* No dynamic buffers in features */
551     if (package->features && package->loaded_features > 0)
552         HeapFree(GetProcessHeap(),0,package->features);
553
554     for (i = 0; i < package->loaded_folders; i++)
555     {
556         HeapFree(GetProcessHeap(),0,package->folders[i].Directory);
557         HeapFree(GetProcessHeap(),0,package->folders[i].TargetDefault);
558         HeapFree(GetProcessHeap(),0,package->folders[i].SourceDefault);
559         HeapFree(GetProcessHeap(),0,package->folders[i].ResolvedTarget);
560         HeapFree(GetProcessHeap(),0,package->folders[i].ResolvedSource);
561         HeapFree(GetProcessHeap(),0,package->folders[i].Property);
562     }
563     if (package->folders && package->loaded_folders > 0)
564         HeapFree(GetProcessHeap(),0,package->folders);
565
566     /* no dynamic buffers in components */ 
567     if (package->components && package->loaded_components > 0)
568         HeapFree(GetProcessHeap(),0,package->components);
569
570     for (i = 0; i < package->loaded_files; i++)
571     {
572         HeapFree(GetProcessHeap(),0,package->files[i].File);
573         HeapFree(GetProcessHeap(),0,package->files[i].FileName);
574         HeapFree(GetProcessHeap(),0,package->files[i].Version);
575         HeapFree(GetProcessHeap(),0,package->files[i].Language);
576         HeapFree(GetProcessHeap(),0,package->files[i].SourcePath);
577         HeapFree(GetProcessHeap(),0,package->files[i].TargetPath);
578     }
579
580     if (package->files && package->loaded_files > 0)
581         HeapFree(GetProcessHeap(),0,package->files);
582
583     for (i = 0; i < package->DeferredActionCount; i++)
584         HeapFree(GetProcessHeap(),0,package->DeferredAction[i]);
585     HeapFree(GetProcessHeap(),0,package->DeferredAction);
586
587     for (i = 0; i < package->CommitActionCount; i++)
588         HeapFree(GetProcessHeap(),0,package->CommitAction[i]);
589     HeapFree(GetProcessHeap(),0,package->CommitAction);
590
591     HeapFree(GetProcessHeap(),0,package->PackagePath);
592 }
593
594 static void ui_progress(MSIPACKAGE *package, int a, int b, int c, int d )
595 {
596     MSIRECORD * row;
597
598     row = MSI_CreateRecord(4);
599     MSI_RecordSetInteger(row,1,a);
600     MSI_RecordSetInteger(row,2,b);
601     MSI_RecordSetInteger(row,3,c);
602     MSI_RecordSetInteger(row,4,d);
603     MSI_ProcessMessage(package, INSTALLMESSAGE_PROGRESS, row);
604     msiobj_release(&row->hdr);
605 }
606
607 static void ui_actiondata(MSIPACKAGE *package, LPCWSTR action, MSIRECORD * record)
608 {
609     static const WCHAR Query_t[] = 
610 {'S','E','L','E','C','T',' ','*',' ','f','r','o','m',' ','A','c','t','i','o',
611 'n','T','e','x','t',' ','w','h','e','r','e',' ','A','c','t','i','o','n',' ','=',
612 ' ','\'','%','s','\'',0};
613     WCHAR message[1024];
614     UINT rc;
615     MSIQUERY * view;
616     MSIRECORD * row = 0;
617     DWORD size;
618
619     if (!package->LastAction || strcmpW(package->LastAction,action))
620     {
621         rc = MSI_OpenQuery(package->db, &view, Query_t, action);
622         if (rc != ERROR_SUCCESS)
623             return;
624
625         rc = MSI_ViewExecute(view, 0);
626         if (rc != ERROR_SUCCESS)
627         {
628             MSI_ViewClose(view);
629             return;
630         }
631         rc = MSI_ViewFetch(view,&row);
632         if (rc != ERROR_SUCCESS)
633         {
634             MSI_ViewClose(view);
635             return;
636         }
637
638         if (MSI_RecordIsNull(row,3))
639         {
640             msiobj_release(&row->hdr);
641             MSI_ViewClose(view);
642             msiobj_release(&view->hdr);
643             return;
644         }
645
646         /* update the cached actionformat */
647         HeapFree(GetProcessHeap(),0,package->ActionFormat);
648         package->ActionFormat = load_dynamic_stringW(row,3);
649
650         HeapFree(GetProcessHeap(),0,package->LastAction);
651         package->LastAction = dupstrW(action);
652
653         msiobj_release(&row->hdr);
654         MSI_ViewClose(view);
655         msiobj_release(&view->hdr);
656     }
657
658     MSI_RecordSetStringW(record,0,package->ActionFormat);
659     size = 1024;
660     MSI_FormatRecordW(package,record,message,&size);
661
662     row = MSI_CreateRecord(1);
663     MSI_RecordSetStringW(row,1,message);
664  
665     MSI_ProcessMessage(package, INSTALLMESSAGE_ACTIONDATA, row);
666     msiobj_release(&row->hdr);
667 }
668
669
670 static void ui_actionstart(MSIPACKAGE *package, LPCWSTR action)
671 {
672     static const WCHAR template_s[]=
673 {'A','c','t','i','o','n',' ','%','s',':',' ','%','s','.',' ','%','s','.',0};
674     static const WCHAR format[] = 
675 {'H','H','\'',':','\'','m','m','\'',':','\'','s','s',0};
676     static const WCHAR Query_t[] = 
677 {'S','E','L','E','C','T',' ','*',' ','f','r','o','m',' ','A','c','t','i','o',
678 'n','T','e','x','t',' ','w','h','e','r','e',' ','A','c','t','i','o','n',' ','=',
679 ' ','\'','%','s','\'',0};
680     WCHAR message[1024];
681     WCHAR timet[0x100];
682     UINT rc;
683     MSIQUERY * view;
684     MSIRECORD * row = 0;
685     WCHAR *ActionText=NULL;
686
687     GetTimeFormatW(LOCALE_USER_DEFAULT, 0, NULL, format, timet, 0x100);
688
689     rc = MSI_OpenQuery(package->db, &view, Query_t, action);
690     if (rc != ERROR_SUCCESS)
691         return;
692     rc = MSI_ViewExecute(view, 0);
693     if (rc != ERROR_SUCCESS)
694     {
695         MSI_ViewClose(view);
696         msiobj_release(&view->hdr);
697         return;
698     }
699     rc = MSI_ViewFetch(view,&row);
700     if (rc != ERROR_SUCCESS)
701     {
702         MSI_ViewClose(view);
703         msiobj_release(&view->hdr);
704         return;
705     }
706
707     ActionText = load_dynamic_stringW(row,2);
708     msiobj_release(&row->hdr);
709     MSI_ViewClose(view);
710     msiobj_release(&view->hdr);
711
712     sprintfW(message,template_s,timet,action,ActionText);
713
714     row = MSI_CreateRecord(1);
715     MSI_RecordSetStringW(row,1,message);
716  
717     MSI_ProcessMessage(package, INSTALLMESSAGE_ACTIONSTART, row);
718     msiobj_release(&row->hdr);
719     HeapFree(GetProcessHeap(),0,ActionText);
720 }
721
722 static void ui_actioninfo(MSIPACKAGE *package, LPCWSTR action, BOOL start, 
723                           UINT rc)
724 {
725     MSIRECORD * row;
726     static const WCHAR template_s[]=
727 {'A','c','t','i','o','n',' ','s','t','a','r','t',' ','%','s',':',' ','%','s',
728 '.',0};
729     static const WCHAR template_e[]=
730 {'A','c','t','i','o','n',' ','e','n','d','e','d',' ','%','s',':',' ','%','s',
731 '.',' ','R','e','t','u','r','n',' ','v','a','l','u','e',' ','%','i','.',0};
732     static const WCHAR format[] = 
733 {'H','H','\'',':','\'','m','m','\'',':','\'','s','s',0};
734     WCHAR message[1024];
735     WCHAR timet[0x100];
736
737     GetTimeFormatW(LOCALE_USER_DEFAULT, 0, NULL, format, timet, 0x100);
738     if (start)
739         sprintfW(message,template_s,timet,action);
740     else
741         sprintfW(message,template_e,timet,action,rc);
742     
743     row = MSI_CreateRecord(1);
744     MSI_RecordSetStringW(row,1,message);
745  
746     MSI_ProcessMessage(package, INSTALLMESSAGE_INFO, row);
747     msiobj_release(&row->hdr);
748 }
749
750 /*
751  *  build_directory_name()
752  *
753  *  This function is to save messing round with directory names
754  *  It handles adding backslashes between path segments, 
755  *   and can add \ at the end of the directory name if told to.
756  *
757  *  It takes a variable number of arguments.
758  *  It always allocates a new string for the result, so make sure
759  *   to free the return value when finished with it.
760  *
761  *  The first arg is the number of path segments that follow.
762  *  The arguments following count are a list of path segments.
763  *  A path segment may be NULL.
764  *
765  *  Path segments will be added with a \ separating them.
766  *  A \ will not be added after the last segment, however if the
767  *    last segment is NULL, then the last character will be a \
768  * 
769  */
770 static LPWSTR build_directory_name(DWORD count, ...)
771 {
772     DWORD sz = 1, i;
773     LPWSTR dir;
774     va_list va;
775
776     va_start(va,count);
777     for(i=0; i<count; i++)
778     {
779         LPCWSTR str = va_arg(va,LPCWSTR);
780         if (str)
781             sz += strlenW(str) + 1;
782     }
783     va_end(va);
784
785     dir = HeapAlloc(GetProcessHeap(), 0, sz*sizeof(WCHAR));
786     dir[0]=0;
787
788     va_start(va,count);
789     for(i=0; i<count; i++)
790     {
791         LPCWSTR str = va_arg(va,LPCWSTR);
792         if (!str)
793             continue;
794         strcatW(dir, str);
795         if( ((i+1)!=count) && dir[strlenW(dir)-1]!='\\')
796             strcatW(dir, cszbs);
797     }
798     return dir;
799 }
800
801
802 /****************************************************
803  * TOP level entry points 
804  *****************************************************/
805
806 UINT ACTION_DoTopLevelINSTALL(MSIPACKAGE *package, LPCWSTR szPackagePath,
807                               LPCWSTR szCommandLine)
808 {
809     DWORD sz;
810     WCHAR buffer[10];
811     UINT rc;
812     static const WCHAR szUILevel[] = {'U','I','L','e','v','e','l',0};
813     static const WCHAR szAction[] = {'A','C','T','I','O','N',0};
814     static const WCHAR szInstall[] = {'I','N','S','T','A','L','L',0};
815
816     MSI_SetPropertyW(package, szAction, szInstall);
817
818     if (szPackagePath)   
819     {
820         LPWSTR p, check, path;
821  
822         package->PackagePath = dupstrW(szPackagePath);
823         path = dupstrW(szPackagePath);
824         p = strrchrW(path,'\\');    
825         if (p)
826         {
827             p++;
828             *p=0;
829         }
830         else
831         {
832             HeapFree(GetProcessHeap(),0,path);
833             path = HeapAlloc(GetProcessHeap(),0,MAX_PATH*sizeof(WCHAR));
834             GetCurrentDirectoryW(MAX_PATH,path);
835             strcatW(path,cszbs);
836         }
837
838         check = load_dynamic_property(package, cszSourceDir,NULL);
839         if (!check)
840             MSI_SetPropertyW(package, cszSourceDir, path);
841         else
842             HeapFree(GetProcessHeap(), 0, check);
843
844         HeapFree(GetProcessHeap(), 0, path);
845     }
846
847     if (szCommandLine)
848     {
849         LPWSTR ptr,ptr2;
850         ptr = (LPWSTR)szCommandLine;
851        
852         while (*ptr)
853         {
854             WCHAR *prop = NULL;
855             WCHAR *val = NULL;
856
857             TRACE("Looking at %s\n",debugstr_w(ptr));
858
859             ptr2 = strchrW(ptr,'=');
860             if (ptr2)
861             {
862                 BOOL quote=FALSE;
863                 DWORD len = 0;
864
865                 while (*ptr == ' ') ptr++;
866                 len = ptr2-ptr;
867                 prop = HeapAlloc(GetProcessHeap(),0,(len+1)*sizeof(WCHAR));
868                 strncpyW(prop,ptr,len);
869                 prop[len]=0;
870                 ptr2++;
871            
872                 len = 0; 
873                 ptr = ptr2; 
874                 while (*ptr && (quote || (!quote && *ptr!=' ')))
875                 {
876                     if (*ptr == '"')
877                         quote = !quote;
878                     ptr++;
879                     len++;
880                 }
881                
882                 if (*ptr2=='"')
883                 {
884                     ptr2++;
885                     len -= 2;
886                 }
887                 val = HeapAlloc(GetProcessHeap(),0,(len+1)*sizeof(WCHAR));
888                 strncpyW(val,ptr2,len);
889                 val[len] = 0;
890
891                 if (strlenW(prop) > 0)
892                 {
893                     TRACE("Found commandline property (%s) = (%s)\n", 
894                                        debugstr_w(prop), debugstr_w(val));
895                     MSI_SetPropertyW(package,prop,val);
896                 }
897                 HeapFree(GetProcessHeap(),0,val);
898                 HeapFree(GetProcessHeap(),0,prop);
899             }
900             ptr++;
901         }
902     }
903   
904     sz = 10; 
905     if (MSI_GetPropertyW(package,szUILevel,buffer,&sz) == ERROR_SUCCESS)
906     {
907         if (atoiW(buffer) >= INSTALLUILEVEL_REDUCED)
908         {
909             rc = ACTION_ProcessUISequence(package);
910             if (rc == ERROR_SUCCESS)
911                 rc = ACTION_ProcessExecSequence(package,TRUE);
912         }
913         else
914             rc = ACTION_ProcessExecSequence(package,FALSE);
915     }
916     else
917         rc = ACTION_ProcessExecSequence(package,FALSE);
918     
919     if (rc == -1)
920     {
921         /* install was halted but should be considered a success */
922         rc = ERROR_SUCCESS;
923     }
924
925     /* process the ending type action */
926     if (rc == ERROR_SUCCESS)
927         ACTION_PerformActionSequence(package,-1);
928     else if (rc == ERROR_INSTALL_USEREXIT) 
929         ACTION_PerformActionSequence(package,-2);
930     else if (rc == ERROR_FUNCTION_FAILED) 
931         ACTION_PerformActionSequence(package,-3);
932     else if (rc == ERROR_INSTALL_SUSPEND) 
933         ACTION_PerformActionSequence(package,-4);
934
935     /* finish up running custom actions */
936     ACTION_FinishCustomActions(package);
937     
938     return rc;
939 }
940
941 static UINT ACTION_PerformActionSequence(MSIPACKAGE *package, UINT seq)
942 {
943     MSIQUERY * view;
944     UINT rc;
945     WCHAR buffer[0x100];
946     DWORD sz = 0x100;
947     MSIRECORD * row = 0;
948     static const WCHAR ExecSeqQuery[] =  {
949    's','e','l','e','c','t',' ','*',' ',
950    'f','r','o','m',' ',
951        'I','n','s','t','a','l','l','E','x','e','c','u','t','e',
952        'S','e','q','u','e','n','c','e',' ',
953    'w','h','e','r','e',' ','S','e','q','u','e','n','c','e',' ',
954        '=',' ','%','i',0};
955
956     rc = MSI_OpenQuery(package->db, &view, ExecSeqQuery, seq);
957
958     if (rc == ERROR_SUCCESS)
959     {
960         rc = MSI_ViewExecute(view, 0);
961
962         if (rc != ERROR_SUCCESS)
963         {
964             MSI_ViewClose(view);
965             msiobj_release(&view->hdr);
966             goto end;
967         }
968        
969         TRACE("Running the actions\n"); 
970
971         rc = MSI_ViewFetch(view,&row);
972         if (rc != ERROR_SUCCESS)
973         {
974             rc = ERROR_SUCCESS;
975             goto end;
976         }
977
978         /* check conditions */
979         if (!MSI_RecordIsNull(row,2))
980         {
981             LPWSTR cond = NULL;
982             cond = load_dynamic_stringW(row,2);
983
984             if (cond)
985             {
986                 /* this is a hack to skip errors in the condition code */
987                 if (MSI_EvaluateConditionW(package, cond) == MSICONDITION_FALSE)
988                 {
989                     HeapFree(GetProcessHeap(),0,cond);
990                     msiobj_release(&row->hdr);
991                     goto end;
992                 }
993                 else
994                     HeapFree(GetProcessHeap(),0,cond);
995             }
996         }
997
998         sz=0x100;
999         rc =  MSI_RecordGetStringW(row,1,buffer,&sz);
1000         if (rc != ERROR_SUCCESS)
1001         {
1002             ERR("Error is %x\n",rc);
1003             msiobj_release(&row->hdr);
1004             goto end;
1005         }
1006
1007         rc = ACTION_PerformAction(package,buffer);
1008         msiobj_release(&row->hdr);
1009 end:
1010         MSI_ViewClose(view);
1011         msiobj_release(&view->hdr);
1012     }
1013     else
1014         rc = ERROR_SUCCESS;
1015
1016     return rc;
1017 }
1018
1019 static UINT ACTION_ProcessExecSequence(MSIPACKAGE *package, BOOL UIran)
1020 {
1021     MSIQUERY * view;
1022     UINT rc;
1023     static const WCHAR ExecSeqQuery[] =  {
1024        's','e','l','e','c','t',' ','*',' ',
1025        'f','r','o','m',' ',
1026            'I','n','s','t','a','l','l','E','x','e','c','u','t','e',
1027            'S','e','q','u','e','n','c','e',' ',
1028        'w','h','e','r','e',' ','S','e','q','u','e','n','c','e',' ',
1029            '>',' ','%','i',' ','o','r','d','e','r',' ',
1030        'b','y',' ','S','e','q','u','e','n','c','e',0 };
1031     MSIRECORD * row = 0;
1032     static const WCHAR IVQuery[] = {
1033        's','e','l','e','c','t',' ','S','e','q','u','e','n','c','e',' ',
1034        'f','r','o','m',' ','I','n','s','t','a','l','l',
1035            'E','x','e','c','u','t','e','S','e','q','u','e','n','c','e',' ',
1036        'w','h','e','r','e',' ','A','c','t','i','o','n',' ','=',' ',
1037            '`','I','n','s','t','a','l','l','V','a','l','i','d','a','t','e','`',
1038        0};
1039     INT seq = 0;
1040
1041     /* get the sequence number */
1042     if (UIran)
1043     {
1044         rc = MSI_DatabaseOpenViewW(package->db, IVQuery, &view);
1045         if (rc != ERROR_SUCCESS)
1046             return rc;
1047         rc = MSI_ViewExecute(view, 0);
1048         if (rc != ERROR_SUCCESS)
1049         {
1050             MSI_ViewClose(view);
1051             msiobj_release(&view->hdr);
1052             return rc;
1053         }
1054         rc = MSI_ViewFetch(view,&row);
1055         if (rc != ERROR_SUCCESS)
1056         {
1057             MSI_ViewClose(view);
1058             msiobj_release(&view->hdr);
1059             return rc;
1060         }
1061         seq = MSI_RecordGetInteger(row,1);
1062         msiobj_release(&row->hdr);
1063         MSI_ViewClose(view);
1064         msiobj_release(&view->hdr);
1065     }
1066
1067     rc = MSI_OpenQuery(package->db, &view, ExecSeqQuery, seq);
1068     if (rc == ERROR_SUCCESS)
1069     {
1070         rc = MSI_ViewExecute(view, 0);
1071
1072         if (rc != ERROR_SUCCESS)
1073         {
1074             MSI_ViewClose(view);
1075             msiobj_release(&view->hdr);
1076             goto end;
1077         }
1078        
1079         TRACE("Running the actions\n"); 
1080
1081         while (1)
1082         {
1083             WCHAR buffer[0x100];
1084             DWORD sz = 0x100;
1085
1086             rc = MSI_ViewFetch(view,&row);
1087             if (rc != ERROR_SUCCESS)
1088             {
1089                 rc = ERROR_SUCCESS;
1090                 break;
1091             }
1092
1093             /* check conditions */
1094             if (!MSI_RecordIsNull(row,2))
1095             {
1096                 LPWSTR cond = NULL;
1097                 cond = load_dynamic_stringW(row,2);
1098
1099                 if (cond)
1100                 {
1101                     /* this is a hack to skip errors in the condition code */
1102                     if (MSI_EvaluateConditionW(package, cond) ==
1103                             MSICONDITION_FALSE)
1104                     {
1105                         HeapFree(GetProcessHeap(),0,cond);
1106                         msiobj_release(&row->hdr);
1107                         continue; 
1108                     }
1109                     else
1110                         HeapFree(GetProcessHeap(),0,cond);
1111                 }
1112             }
1113
1114             sz=0x100;
1115             rc =  MSI_RecordGetStringW(row,1,buffer,&sz);
1116             if (rc != ERROR_SUCCESS)
1117             {
1118                 ERR("Error is %x\n",rc);
1119                 msiobj_release(&row->hdr);
1120                 break;
1121             }
1122
1123             rc = ACTION_PerformAction(package,buffer);
1124
1125             if (rc == ERROR_FUNCTION_NOT_CALLED)
1126                 rc = ERROR_SUCCESS;
1127
1128             if (rc != ERROR_SUCCESS)
1129             {
1130                 ERR("Execution halted due to error (%i)\n",rc);
1131                 msiobj_release(&row->hdr);
1132                 break;
1133             }
1134
1135             msiobj_release(&row->hdr);
1136         }
1137
1138         MSI_ViewClose(view);
1139         msiobj_release(&view->hdr);
1140     }
1141
1142 end:
1143     return rc;
1144 }
1145
1146
1147 static UINT ACTION_ProcessUISequence(MSIPACKAGE *package)
1148 {
1149     MSIQUERY * view;
1150     UINT rc;
1151     static const WCHAR ExecSeqQuery [] = {
1152       's','e','l','e','c','t',' ','*',' ',
1153       'f','r','o','m',' ','I','n','s','t','a','l','l',
1154             'U','I','S','e','q','u','e','n','c','e',' ',
1155       'w','h','e','r','e',' ','S','e','q','u','e','n','c','e',' ', '>',' ','0',' ',
1156       'o','r','d','e','r',' ','b','y',' ','S','e','q','u','e','n','c','e',0};
1157     
1158     rc = MSI_DatabaseOpenViewW(package->db, ExecSeqQuery, &view);
1159     
1160     if (rc == ERROR_SUCCESS)
1161     {
1162         rc = MSI_ViewExecute(view, 0);
1163
1164         if (rc != ERROR_SUCCESS)
1165         {
1166             MSI_ViewClose(view);
1167             msiobj_release(&view->hdr);
1168             goto end;
1169         }
1170        
1171         TRACE("Running the actions \n"); 
1172
1173         while (1)
1174         {
1175             WCHAR buffer[0x100];
1176             DWORD sz = 0x100;
1177             MSIRECORD * row = 0;
1178
1179             rc = MSI_ViewFetch(view,&row);
1180             if (rc != ERROR_SUCCESS)
1181             {
1182                 rc = ERROR_SUCCESS;
1183                 break;
1184             }
1185
1186             /* check conditions */
1187             if (!MSI_RecordIsNull(row,2))
1188             {
1189                 LPWSTR cond = NULL;
1190                 cond = load_dynamic_stringW(row,2);
1191
1192                 if (cond)
1193                 {
1194                     /* this is a hack to skip errors in the condition code */
1195                     if (MSI_EvaluateConditionW(package, cond) ==
1196                             MSICONDITION_FALSE)
1197                     {
1198                         HeapFree(GetProcessHeap(),0,cond);
1199                         msiobj_release(&row->hdr);
1200                         continue; 
1201                     }
1202                     else
1203                         HeapFree(GetProcessHeap(),0,cond);
1204                 }
1205             }
1206
1207             sz=0x100;
1208             rc =  MSI_RecordGetStringW(row,1,buffer,&sz);
1209             if (rc != ERROR_SUCCESS)
1210             {
1211                 ERR("Error is %x\n",rc);
1212                 msiobj_release(&row->hdr);
1213                 break;
1214             }
1215
1216             rc = ACTION_PerformUIAction(package,buffer);
1217
1218             if (rc == ERROR_FUNCTION_NOT_CALLED)
1219                 rc = ERROR_SUCCESS;
1220
1221             if (rc != ERROR_SUCCESS)
1222             {
1223                 ERR("Execution halted due to error (%i)\n",rc);
1224                 msiobj_release(&row->hdr);
1225                 break;
1226             }
1227
1228             msiobj_release(&row->hdr);
1229         }
1230
1231         MSI_ViewClose(view);
1232         msiobj_release(&view->hdr);
1233     }
1234
1235 end:
1236     return rc;
1237 }
1238
1239 /********************************************************
1240  * ACTION helper functions and functions that perform the actions
1241  *******************************************************/
1242 BOOL ACTION_HandleStandardAction(MSIPACKAGE *package, LPCWSTR action, UINT* rc)
1243 {
1244     BOOL ret = FALSE; 
1245
1246     int i;
1247     i = 0;
1248     while (StandardActions[i].action != NULL)
1249     {
1250         if (strcmpW(StandardActions[i].action, action)==0)
1251         {
1252             ui_actioninfo(package, action, TRUE, 0);
1253             ui_actionstart(package, action);
1254             if (StandardActions[i].handler)
1255             {
1256                 *rc = StandardActions[i].handler(package);
1257             }
1258             else
1259             {
1260                 FIXME("UNHANDLED Standard Action %s\n",debugstr_w(action));
1261                 *rc = ERROR_SUCCESS;
1262             }
1263             ui_actioninfo(package, action, FALSE, *rc);
1264             ret = TRUE;
1265             break;
1266         }
1267         i++;
1268     }
1269     return ret;
1270 }
1271
1272 BOOL ACTION_HandleDialogBox(MSIPACKAGE *package, LPCWSTR dialog, UINT* rc)
1273 {
1274     BOOL ret = FALSE;
1275
1276     /*
1277      * for the UI when we get that working
1278      *
1279     if (ACTION_DialogBox(package,dialog) == ERROR_SUCCESS)
1280     {
1281         *rc = package->CurrentInstallState;
1282         ret = TRUE;
1283     }
1284     */
1285     return ret;
1286 }
1287
1288 BOOL ACTION_HandleCustomAction(MSIPACKAGE* package, LPCWSTR action, UINT* rc)
1289 {
1290     BOOL ret=FALSE;
1291     UINT arc;
1292
1293     arc = ACTION_CustomAction(package,action,FALSE);
1294
1295     if (arc != ERROR_CALL_NOT_IMPLEMENTED)
1296     {
1297         *rc = arc;
1298         ret = TRUE;
1299     }
1300     return ret;
1301 }
1302
1303 /* 
1304  * A lot of actions are really important even if they don't do anything
1305  * explicit... Lots of properties are set at the beginning of the installation
1306  * CostFinalize does a bunch of work to translate the directories and such
1307  * 
1308  * But until I get write access to the database that is hard, so I am going to
1309  * hack it to see if I can get something to run.
1310  */
1311 UINT ACTION_PerformAction(MSIPACKAGE *package, const WCHAR *action)
1312 {
1313     UINT rc = ERROR_SUCCESS; 
1314     BOOL handled;
1315
1316     TRACE("Performing action (%s)\n",debugstr_w(action));
1317
1318     handled = ACTION_HandleStandardAction(package, action, &rc);
1319
1320     if (!handled)
1321         handled = ACTION_HandleCustomAction(package, action, &rc);
1322
1323     if (!handled)
1324     {
1325         FIXME("UNHANDLED MSI ACTION %s\n",debugstr_w(action));
1326         rc = ERROR_FUNCTION_NOT_CALLED;
1327     }
1328
1329     package->CurrentInstallState = rc;
1330     return rc;
1331 }
1332
1333 UINT ACTION_PerformUIAction(MSIPACKAGE *package, const WCHAR *action)
1334 {
1335     UINT rc = ERROR_SUCCESS;
1336     BOOL handled = FALSE;
1337
1338     TRACE("Performing action (%s)\n",debugstr_w(action));
1339
1340     handled = ACTION_HandleStandardAction(package, action, &rc);
1341
1342     if (!handled)
1343         handled = ACTION_HandleCustomAction(package, action, &rc);
1344
1345     if (!handled)
1346         handled = ACTION_HandleDialogBox(package, action, &rc);
1347
1348     if (!handled)
1349     {
1350         FIXME("UNHANDLED MSI ACTION %s\n",debugstr_w(action));
1351         rc = ERROR_FUNCTION_NOT_CALLED;
1352     }
1353
1354     package->CurrentInstallState = rc;
1355     return rc;
1356 }
1357
1358 /***********************************************************************
1359  *            create_full_pathW
1360  *
1361  * Recursively create all directories in the path.
1362  *
1363  * shamelessly stolen from setupapi/queue.c
1364  */
1365 static BOOL create_full_pathW(const WCHAR *path)
1366 {
1367     BOOL ret = TRUE;
1368     int len;
1369     WCHAR *new_path;
1370
1371     new_path = HeapAlloc(GetProcessHeap(), 0, (strlenW(path) + 1) *
1372                                               sizeof(WCHAR));
1373
1374     strcpyW(new_path, path);
1375
1376     while((len = strlenW(new_path)) && new_path[len - 1] == '\\')
1377     new_path[len - 1] = 0;
1378
1379     while(!CreateDirectoryW(new_path, NULL))
1380     {
1381         WCHAR *slash;
1382         DWORD last_error = GetLastError();
1383         if(last_error == ERROR_ALREADY_EXISTS)
1384             break;
1385
1386         if(last_error != ERROR_PATH_NOT_FOUND)
1387         {
1388             ret = FALSE;
1389             break;
1390         }
1391
1392         if(!(slash = strrchrW(new_path, '\\')))
1393         {
1394             ret = FALSE;
1395             break;
1396         }
1397
1398         len = slash - new_path;
1399         new_path[len] = 0;
1400         if(!create_full_pathW(new_path))
1401         {
1402             ret = FALSE;
1403             break;
1404         }
1405         new_path[len] = '\\';
1406     }
1407
1408     HeapFree(GetProcessHeap(), 0, new_path);
1409     return ret;
1410 }
1411
1412 /*
1413  * Also we cannot enable/disable components either, so for now I am just going 
1414  * to do all the directories for all the components.
1415  */
1416 static UINT ACTION_CreateFolders(MSIPACKAGE *package)
1417 {
1418     static const WCHAR ExecSeqQuery[] = {
1419         's','e','l','e','c','t',' ','D','i','r','e','c','t','o','r','y','_',' ',
1420         'f','r','o','m',' ','C','r','e','a','t','e','F','o','l','d','e','r',0 };
1421     UINT rc;
1422     MSIQUERY *view;
1423     MSIFOLDER *folder;
1424
1425     rc = MSI_DatabaseOpenViewW(package->db, ExecSeqQuery, &view );
1426     if (rc != ERROR_SUCCESS)
1427         return ERROR_SUCCESS;
1428
1429     rc = MSI_ViewExecute(view, 0);
1430     if (rc != ERROR_SUCCESS)
1431     {
1432         MSI_ViewClose(view);
1433         msiobj_release(&view->hdr);
1434         return rc;
1435     }
1436     
1437     while (1)
1438     {
1439         WCHAR dir[0x100];
1440         LPWSTR full_path;
1441         DWORD sz;
1442         MSIRECORD *row = NULL, *uirow;
1443
1444         rc = MSI_ViewFetch(view,&row);
1445         if (rc != ERROR_SUCCESS)
1446         {
1447             rc = ERROR_SUCCESS;
1448             break;
1449         }
1450
1451         sz=0x100;
1452         rc = MSI_RecordGetStringW(row,1,dir,&sz);
1453
1454         if (rc!= ERROR_SUCCESS)
1455         {
1456             ERR("Unable to get folder id \n");
1457             msiobj_release(&row->hdr);
1458             continue;
1459         }
1460
1461         sz = MAX_PATH;
1462         full_path = resolve_folder(package,dir,FALSE,FALSE,&folder);
1463         if (!full_path)
1464         {
1465             ERR("Unable to resolve folder id %s\n",debugstr_w(dir));
1466             msiobj_release(&row->hdr);
1467             continue;
1468         }
1469
1470         TRACE("Folder is %s\n",debugstr_w(full_path));
1471
1472         /* UI stuff */
1473         uirow = MSI_CreateRecord(1);
1474         MSI_RecordSetStringW(uirow,1,full_path);
1475         ui_actiondata(package,szCreateFolders,uirow);
1476         msiobj_release( &uirow->hdr );
1477
1478         if (folder->State == 0)
1479             create_full_pathW(full_path);
1480
1481         folder->State = 3;
1482
1483         msiobj_release(&row->hdr);
1484         HeapFree(GetProcessHeap(),0,full_path);
1485     }
1486     MSI_ViewClose(view);
1487     msiobj_release(&view->hdr);
1488    
1489     return rc;
1490 }
1491
1492 static int load_component(MSIPACKAGE* package, MSIRECORD * row)
1493 {
1494     int index = package->loaded_components;
1495     DWORD sz;
1496
1497     /* fill in the data */
1498
1499     package->loaded_components++;
1500     if (package->loaded_components == 1)
1501         package->components = HeapAlloc(GetProcessHeap(),0,
1502                                         sizeof(MSICOMPONENT));
1503     else
1504         package->components = HeapReAlloc(GetProcessHeap(),0,
1505             package->components, package->loaded_components * 
1506             sizeof(MSICOMPONENT));
1507
1508     memset(&package->components[index],0,sizeof(MSICOMPONENT));
1509
1510     sz = 96;       
1511     MSI_RecordGetStringW(row,1,package->components[index].Component,&sz);
1512
1513     TRACE("Loading Component %s\n",
1514            debugstr_w(package->components[index].Component));
1515
1516     sz = 0x100;
1517     if (!MSI_RecordIsNull(row,2))
1518         MSI_RecordGetStringW(row,2,package->components[index].ComponentId,&sz);
1519             
1520     sz = 96;       
1521     MSI_RecordGetStringW(row,3,package->components[index].Directory,&sz);
1522
1523     package->components[index].Attributes = MSI_RecordGetInteger(row,4);
1524
1525     sz = 0x100;       
1526     MSI_RecordGetStringW(row,5,package->components[index].Condition,&sz);
1527
1528     sz = 96;       
1529     MSI_RecordGetStringW(row,6,package->components[index].KeyPath,&sz);
1530
1531     package->components[index].Installed = INSTALLSTATE_ABSENT;
1532     package->components[index].Action = INSTALLSTATE_UNKNOWN;
1533     package->components[index].ActionRequest = INSTALLSTATE_UNKNOWN;
1534
1535     package->components[index].Enabled = TRUE;
1536
1537     return index;
1538 }
1539
1540 static void load_feature(MSIPACKAGE* package, MSIRECORD * row)
1541 {
1542     int index = package->loaded_features;
1543     DWORD sz;
1544     static const WCHAR Query1[] = {'S','E','L','E','C','T',' ','C','o','m','p',
1545         'o','n','e','n','t','_',' ','F','R','O','M',' ','F','e','a','t','u','r','e',
1546         'C','o','m','p','o','n','e','n','t','s',' ','W','H','E','R','E',' ','F','e',
1547         'a','t','u','r','e','_','=','\'','%','s','\'',0};
1548     static const WCHAR Query2[] = {'S','E','L','E','C','T',' ','*',' ','F','R',
1549         'O','M',' ','C','o','m','p','o','n','e','n','t',' ','W','H','E','R','E',' ','C',
1550         'o','m','p','o','n','e','n','t','=','\'','%','s','\'',0};
1551     MSIQUERY * view;
1552     MSIQUERY * view2;
1553     MSIRECORD * row2;
1554     MSIRECORD * row3;
1555     UINT    rc;
1556
1557     /* fill in the data */
1558
1559     package->loaded_features ++;
1560     if (package->loaded_features == 1)
1561         package->features = HeapAlloc(GetProcessHeap(),0,sizeof(MSIFEATURE));
1562     else
1563         package->features = HeapReAlloc(GetProcessHeap(),0,package->features,
1564                                 package->loaded_features * sizeof(MSIFEATURE));
1565
1566     memset(&package->features[index],0,sizeof(MSIFEATURE));
1567     
1568     sz = 96;       
1569     MSI_RecordGetStringW(row,1,package->features[index].Feature,&sz);
1570
1571     TRACE("Loading feature %s\n",debugstr_w(package->features[index].Feature));
1572
1573     sz = 96;
1574     if (!MSI_RecordIsNull(row,2))
1575         MSI_RecordGetStringW(row,2,package->features[index].Feature_Parent,&sz);
1576
1577     sz = 0x100;
1578      if (!MSI_RecordIsNull(row,3))
1579         MSI_RecordGetStringW(row,3,package->features[index].Title,&sz);
1580
1581      sz = 0x100;
1582      if (!MSI_RecordIsNull(row,4))
1583         MSI_RecordGetStringW(row,4,package->features[index].Description,&sz);
1584
1585     if (!MSI_RecordIsNull(row,5))
1586         package->features[index].Display = MSI_RecordGetInteger(row,5);
1587   
1588     package->features[index].Level= MSI_RecordGetInteger(row,6);
1589
1590      sz = 96;
1591      if (!MSI_RecordIsNull(row,7))
1592         MSI_RecordGetStringW(row,7,package->features[index].Directory,&sz);
1593
1594     package->features[index].Attributes= MSI_RecordGetInteger(row,8);
1595
1596     package->features[index].Installed = INSTALLSTATE_ABSENT;
1597     package->features[index].Action = INSTALLSTATE_UNKNOWN;
1598     package->features[index].ActionRequest = INSTALLSTATE_UNKNOWN;
1599
1600     /* load feature components */
1601
1602     rc = MSI_OpenQuery(package->db, &view, Query1, package->features[index].Feature);
1603     if (rc != ERROR_SUCCESS)
1604         return;
1605     rc = MSI_ViewExecute(view,0);
1606     if (rc != ERROR_SUCCESS)
1607     {
1608         MSI_ViewClose(view);
1609         msiobj_release(&view->hdr);
1610         return;
1611     }
1612     while (1)
1613     {
1614         DWORD sz = 0x100;
1615         WCHAR buffer[0x100];
1616         DWORD rc;
1617         INT c_indx;
1618         INT cnt = package->features[index].ComponentCount;
1619
1620         rc = MSI_ViewFetch(view,&row2);
1621         if (rc != ERROR_SUCCESS)
1622             break;
1623
1624         sz = 0x100;
1625         MSI_RecordGetStringW(row2,1,buffer,&sz);
1626
1627         /* check to see if the component is already loaded */
1628         c_indx = get_loaded_component(package,buffer);
1629         if (c_indx != -1)
1630         {
1631             TRACE("Component %s already loaded at %i\n", debugstr_w(buffer),
1632                   c_indx);
1633             package->features[index].Components[cnt] = c_indx;
1634             package->features[index].ComponentCount ++;
1635             continue;
1636         }
1637
1638         rc = MSI_OpenQuery(package->db, &view2, Query2, buffer);
1639         if (rc != ERROR_SUCCESS)
1640         {
1641             msiobj_release( &row2->hdr );
1642             continue;
1643         }
1644         rc = MSI_ViewExecute(view2,0);
1645         if (rc != ERROR_SUCCESS)
1646         {
1647             msiobj_release( &row2->hdr );
1648             MSI_ViewClose(view2);
1649             msiobj_release( &view2->hdr );  
1650             continue;
1651         }
1652         while (1)
1653         {
1654             DWORD rc;
1655
1656             rc = MSI_ViewFetch(view2,&row3);
1657             if (rc != ERROR_SUCCESS)
1658                 break;
1659             c_indx = load_component(package,row3);
1660             msiobj_release( &row3->hdr );
1661
1662             package->features[index].Components[cnt] = c_indx;
1663             package->features[index].ComponentCount ++;
1664             TRACE("Loaded new component to index %i\n",c_indx);
1665         }
1666         MSI_ViewClose(view2);
1667         msiobj_release( &view2->hdr );
1668         msiobj_release( &row2->hdr );
1669     }
1670     MSI_ViewClose(view);
1671     msiobj_release(&view->hdr);
1672 }
1673
1674 /*
1675  * I am not doing any of the costing functionality yet. 
1676  * Mostly looking at doing the Component and Feature loading
1677  *
1678  * The native MSI does A LOT of modification to tables here. Mostly adding
1679  * a lot of temporary columns to the Feature and Component tables. 
1680  *
1681  *    note: Native msi also tracks the short filename. But I am only going to
1682  *          track the long ones.  Also looking at this directory table
1683  *          it appears that the directory table does not get the parents
1684  *          resolved base on property only based on their entries in the 
1685  *          directory table.
1686  */
1687 static UINT ACTION_CostInitialize(MSIPACKAGE *package)
1688 {
1689     MSIQUERY * view;
1690     MSIRECORD * row;
1691     UINT rc;
1692     static const WCHAR Query_all[] = {
1693        'S','E','L','E','C','T',' ','*',' ',
1694        'F','R','O','M',' ','F','e','a','t','u','r','e',0};
1695     static const WCHAR szCosting[] = {
1696        'C','o','s','t','i','n','g','C','o','m','p','l','e','t','e',0 };
1697     static const WCHAR szZero[] = { '0', 0 };
1698
1699     MSI_SetPropertyW(package, szCosting, szZero);
1700     MSI_SetPropertyW(package, cszRootDrive , c_collen);
1701
1702     rc = MSI_DatabaseOpenViewW(package->db,Query_all,&view);
1703     if (rc != ERROR_SUCCESS)
1704         return rc;
1705     rc = MSI_ViewExecute(view,0);
1706     if (rc != ERROR_SUCCESS)
1707     {
1708         MSI_ViewClose(view);
1709         msiobj_release(&view->hdr);
1710         return rc;
1711     }
1712     while (1)
1713     {
1714         DWORD rc;
1715
1716         rc = MSI_ViewFetch(view,&row);
1717         if (rc != ERROR_SUCCESS)
1718             break;
1719        
1720         load_feature(package,row); 
1721         msiobj_release(&row->hdr);
1722     }
1723     MSI_ViewClose(view);
1724     msiobj_release(&view->hdr);
1725
1726     return ERROR_SUCCESS;
1727 }
1728
1729 static UINT load_file(MSIPACKAGE* package, MSIRECORD * row)
1730 {
1731     DWORD index = package->loaded_files;
1732     DWORD i;
1733     LPWSTR buffer;
1734
1735     /* fill in the data */
1736
1737     package->loaded_files++;
1738     if (package->loaded_files== 1)
1739         package->files = HeapAlloc(GetProcessHeap(),0,sizeof(MSIFILE));
1740     else
1741         package->files = HeapReAlloc(GetProcessHeap(),0,
1742             package->files , package->loaded_files * sizeof(MSIFILE));
1743
1744     memset(&package->files[index],0,sizeof(MSIFILE));
1745  
1746     package->files[index].File = load_dynamic_stringW(row, 1);
1747     buffer = load_dynamic_stringW(row, 2);
1748
1749     package->files[index].ComponentIndex = -1;
1750     for (i = 0; i < package->loaded_components; i++)
1751         if (strcmpW(package->components[i].Component,buffer)==0)
1752         {
1753             package->files[index].ComponentIndex = i;
1754             break;
1755         }
1756     if (package->files[index].ComponentIndex == -1)
1757         ERR("Unfound Component %s\n",debugstr_w(buffer));
1758     HeapFree(GetProcessHeap(), 0, buffer);
1759
1760     package->files[index].FileName = load_dynamic_stringW(row,3);
1761
1762     reduce_to_longfilename(package->files[index].FileName);
1763     
1764     package->files[index].FileSize = MSI_RecordGetInteger(row,4);
1765     package->files[index].Version = load_dynamic_stringW(row, 5);
1766     package->files[index].Language = load_dynamic_stringW(row, 6);
1767     package->files[index].Attributes= MSI_RecordGetInteger(row,7);
1768     package->files[index].Sequence= MSI_RecordGetInteger(row,8);
1769
1770     package->files[index].Temporary = FALSE;
1771     package->files[index].State = 0;
1772
1773     TRACE("File Loaded (%s)\n",debugstr_w(package->files[index].File));  
1774  
1775     return ERROR_SUCCESS;
1776 }
1777
1778 static UINT ACTION_FileCost(MSIPACKAGE *package)
1779 {
1780     MSIQUERY * view;
1781     MSIRECORD * row;
1782     UINT rc;
1783     static const WCHAR Query[] = {
1784         'S','E','L','E','C','T',' ','*',' ',
1785         'F','R','O','M',' ','F','i','l','e',' ',
1786         'O','r','d','e','r',' ','b','y',' ','S','e','q','u','e','n','c','e', 0};
1787
1788     if (!package)
1789         return ERROR_INVALID_HANDLE;
1790
1791     rc = MSI_DatabaseOpenViewW(package->db, Query, &view);
1792     if (rc != ERROR_SUCCESS)
1793         return ERROR_SUCCESS;
1794    
1795     rc = MSI_ViewExecute(view, 0);
1796     if (rc != ERROR_SUCCESS)
1797     {
1798         MSI_ViewClose(view);
1799         msiobj_release(&view->hdr);
1800         return ERROR_SUCCESS;
1801     }
1802
1803     while (1)
1804     {
1805         rc = MSI_ViewFetch(view,&row);
1806         if (rc != ERROR_SUCCESS)
1807         {
1808             rc = ERROR_SUCCESS;
1809             break;
1810         }
1811         load_file(package,row);
1812         msiobj_release(&row->hdr);
1813     }
1814     MSI_ViewClose(view);
1815     msiobj_release(&view->hdr);
1816
1817     return ERROR_SUCCESS;
1818 }
1819
1820 static INT load_folder(MSIPACKAGE *package, const WCHAR* dir)
1821
1822 {
1823     static const WCHAR Query[] =
1824         {'s','e','l','e','c','t',' ','*',' ','f','r','o','m',' ','D','i','r','e','c',
1825          't','o','r','y',' ','w','h','e','r','e',' ','`','D','i','r','e','c','t',
1826          'o','r','y','`',' ','=',' ','`','%','s','`',0};
1827     UINT rc;
1828     MSIQUERY * view;
1829     LPWSTR targetdir, parent, srcdir;
1830     MSIRECORD * row = 0;
1831     INT index = -1;
1832     DWORD i;
1833
1834     TRACE("Looking for dir %s\n",debugstr_w(dir));
1835
1836     for (i = 0; i < package->loaded_folders; i++)
1837     {
1838         if (strcmpW(package->folders[i].Directory,dir)==0)
1839         {
1840             TRACE(" %s retuning on index %lu\n",debugstr_w(dir),i);
1841             return i;
1842         }
1843     }
1844
1845     TRACE("Working to load %s\n",debugstr_w(dir));
1846
1847     index = package->loaded_folders++;
1848     if (package->loaded_folders==1)
1849         package->folders = HeapAlloc(GetProcessHeap(),0,
1850                                         sizeof(MSIFOLDER));
1851     else
1852         package->folders= HeapReAlloc(GetProcessHeap(),0,
1853             package->folders, package->loaded_folders* 
1854             sizeof(MSIFOLDER));
1855
1856     memset(&package->folders[index],0,sizeof(MSIFOLDER));
1857
1858     package->folders[index].Directory = dupstrW(dir);
1859
1860     rc = MSI_OpenQuery(package->db, &view, Query, dir);
1861     if (rc != ERROR_SUCCESS)
1862         return -1;
1863
1864     rc = MSI_ViewExecute(view, 0);
1865     if (rc != ERROR_SUCCESS)
1866     {
1867         MSI_ViewClose(view);
1868         msiobj_release(&view->hdr);
1869         return -1;
1870     }
1871
1872     rc = MSI_ViewFetch(view,&row);
1873     if (rc != ERROR_SUCCESS)
1874     {
1875         MSI_ViewClose(view);
1876         msiobj_release(&view->hdr);
1877         return -1;
1878     }
1879
1880     targetdir = load_dynamic_stringW(row,3);
1881
1882     /* split src and target dir */
1883     if (strchrW(targetdir,':'))
1884     {
1885         srcdir=strchrW(targetdir,':');
1886         *srcdir=0;
1887         srcdir ++;
1888     }
1889     else
1890         srcdir=NULL;
1891
1892     /* for now only pick long filename versions */
1893     if (strchrW(targetdir,'|'))
1894     {
1895         targetdir = strchrW(targetdir,'|'); 
1896         *targetdir = 0;
1897         targetdir ++;
1898     }
1899     if (srcdir && strchrW(srcdir,'|'))
1900     {
1901         srcdir= strchrW(srcdir,'|'); 
1902         *srcdir= 0;
1903         srcdir ++;
1904     }
1905
1906     /* now check for root dirs */
1907     if (targetdir[0] == '.' && targetdir[1] == 0)
1908         targetdir = NULL;
1909         
1910     if (srcdir && srcdir[0] == '.' && srcdir[1] == 0)
1911         srcdir = NULL;
1912
1913     if (targetdir)
1914     {
1915         TRACE("   TargetDefault = %s\n",debugstr_w(targetdir));
1916         HeapFree(GetProcessHeap(),0, package->folders[index].TargetDefault);
1917         package->folders[index].TargetDefault = dupstrW(targetdir);
1918     }
1919
1920     if (srcdir)
1921        package->folders[index].SourceDefault = dupstrW(srcdir);
1922     else if (targetdir)
1923         package->folders[index].SourceDefault = dupstrW(targetdir);
1924     HeapFree(GetProcessHeap(), 0, targetdir);
1925
1926     parent = load_dynamic_stringW(row,2);
1927     if (parent) 
1928     {
1929         i = load_folder(package,parent);
1930         package->folders[index].ParentIndex = i;
1931         TRACE("Parent is index %i... %s %s\n",
1932                     package->folders[index].ParentIndex,
1933         debugstr_w(package->folders[package->folders[index].ParentIndex].Directory),
1934                     debugstr_w(parent));
1935     }
1936     else
1937         package->folders[index].ParentIndex = -2;
1938     HeapFree(GetProcessHeap(), 0, parent);
1939
1940     package->folders[index].Property = load_dynamic_property(package, dir,NULL);
1941
1942     msiobj_release(&row->hdr);
1943     MSI_ViewClose(view);
1944     msiobj_release(&view->hdr);
1945     TRACE(" %s retuning on index %i\n",debugstr_w(dir),index);
1946     return index;
1947 }
1948
1949
1950 LPWSTR resolve_folder(MSIPACKAGE *package, LPCWSTR name, BOOL source, 
1951                       BOOL set_prop, MSIFOLDER **folder)
1952 {
1953     DWORD i;
1954     LPWSTR p, path = NULL;
1955
1956     TRACE("Working to resolve %s\n",debugstr_w(name));
1957
1958     /* special resolving for Target and Source root dir */
1959     if (strcmpW(name,cszTargetDir)==0 || strcmpW(name,cszSourceDir)==0)
1960     {
1961         if (!source)
1962         {
1963             path = load_dynamic_property(package,cszTargetDir,NULL);
1964             if (!path)
1965             {
1966                 path = load_dynamic_property(package,cszRootDrive,NULL);
1967                 if (set_prop)
1968                     MSI_SetPropertyW(package,cszTargetDir,path);
1969             }
1970             if (folder)
1971             {
1972                 for (i = 0; i < package->loaded_folders; i++)
1973                 {
1974                     if (strcmpW(package->folders[i].Directory,name)==0)
1975                         break;
1976                 }
1977                 *folder = &(package->folders[i]);
1978             }
1979             return path;
1980         }
1981         else
1982         {
1983             path = load_dynamic_property(package,cszSourceDir,NULL);
1984             if (!path)
1985             {
1986                 path = load_dynamic_property(package,cszDatabase,NULL);
1987                 if (path)
1988                 {
1989                     p = strrchrW(path,'\\');
1990                     if (p)
1991                         *(p+1) = 0;
1992                 }
1993             }
1994             if (folder)
1995             {
1996                 for (i = 0; i < package->loaded_folders; i++)
1997                 {
1998                     if (strcmpW(package->folders[i].Directory,name)==0)
1999                         break;
2000                 }
2001                 *folder = &(package->folders[i]);
2002             }
2003             return path;
2004         }
2005     }
2006
2007     for (i = 0; i < package->loaded_folders; i++)
2008     {
2009         if (strcmpW(package->folders[i].Directory,name)==0)
2010             break;
2011     }
2012
2013     if (i >= package->loaded_folders)
2014         return NULL;
2015
2016     if (folder)
2017         *folder = &(package->folders[i]);
2018
2019     if (!source && package->folders[i].ResolvedTarget)
2020     {
2021         path = dupstrW(package->folders[i].ResolvedTarget);
2022         TRACE("   already resolved to %s\n",debugstr_w(path));
2023         return path;
2024     }
2025     else if (source && package->folders[i].ResolvedSource)
2026     {
2027         path = dupstrW(package->folders[i].ResolvedSource);
2028         return path;
2029     }
2030     else if (!source && package->folders[i].Property)
2031     {
2032         path = build_directory_name(2, package->folders[i].Property, NULL);
2033                     
2034         TRACE("   internally set to %s\n",debugstr_w(path));
2035         if (set_prop)
2036             MSI_SetPropertyW(package,name,path);
2037         return path;
2038     }
2039
2040     if (package->folders[i].ParentIndex >= 0)
2041     {
2042         LPWSTR parent = package->folders[package->folders[i].ParentIndex].Directory;
2043
2044         TRACE(" ! Parent is %s\n", debugstr_w(parent));
2045
2046         p = resolve_folder(package, parent, source, set_prop, NULL);
2047         if (!source)
2048         {
2049             TRACE("   TargetDefault = %s\n",debugstr_w(package->folders[i].TargetDefault));
2050             path = build_directory_name(3, p, package->folders[i].TargetDefault, NULL);
2051             package->folders[i].ResolvedTarget = dupstrW(path);
2052             TRACE("   resolved into %s\n",debugstr_w(path));
2053             if (set_prop)
2054                 MSI_SetPropertyW(package,name,path);
2055         }
2056         else 
2057         {
2058             path = build_directory_name(3, p, package->folders[i].SourceDefault, NULL);
2059             package->folders[i].ResolvedSource = dupstrW(path);
2060         }
2061         HeapFree(GetProcessHeap(),0,p);
2062     }
2063     return path;
2064 }
2065
2066 /* update compoennt state based on a feature change */
2067 void ACTION_UpdateComponentStates(MSIPACKAGE *package, LPCWSTR szFeature)
2068 {
2069     int i;
2070     INSTALLSTATE newstate;
2071     MSIFEATURE *feature;
2072
2073     i = get_loaded_feature(package,szFeature);
2074     if (i < 0)
2075         return;
2076
2077     feature = &package->features[i];
2078     newstate = feature->ActionRequest;
2079
2080     for( i = 0; i < feature->ComponentCount; i++)
2081     {
2082         MSICOMPONENT* component = &package->components[feature->Components[i]];
2083
2084         if (!component->Enabled)
2085             continue;
2086         else
2087         {
2088             if (newstate == INSTALLSTATE_LOCAL)
2089                 component->ActionRequest = INSTALLSTATE_LOCAL;
2090             else 
2091             {
2092                 int j,k;
2093
2094                 component->ActionRequest = newstate;
2095
2096                 /*if any other feature wants is local we need to set it local*/
2097                 for (j = 0; 
2098                      j < package->loaded_features &&
2099                      component->ActionRequest != INSTALLSTATE_LOCAL; 
2100                      j++)
2101                 {
2102                     for (k = 0; k < package->features[j].ComponentCount; k++)
2103                         if ( package->features[j].Components[k] ==
2104                              feature->Components[i] )
2105                         {
2106                             if (package->features[j].ActionRequest == 
2107                                 INSTALLSTATE_LOCAL)
2108                                 component->ActionRequest = INSTALLSTATE_LOCAL;
2109                             break;
2110                         }
2111                 }
2112             }
2113         }
2114     } 
2115 }
2116
2117 static UINT SetFeatureStates(MSIPACKAGE *package)
2118 {
2119     LPWSTR level;
2120     INT install_level;
2121     DWORD i;
2122     INT j;
2123     LPWSTR override = NULL;
2124     static const WCHAR all[]={'A','L','L',0};
2125     static const WCHAR szlevel[] = {
2126         'I','N','S','T','A','L','L','L','E','V','E','L',0};
2127     static const WCHAR szAddLocal[] = {
2128         'A','D','D','L','O','C','A','L',0};
2129
2130     /* I do not know if this is where it should happen.. but */
2131
2132     TRACE("Checking Install Level\n");
2133
2134     level = load_dynamic_property(package,szlevel,NULL);
2135     if (level)
2136     {
2137         install_level = atoiW(level);
2138         HeapFree(GetProcessHeap(), 0, level);
2139     }
2140     else
2141         install_level = 1;
2142
2143     /* ok hereis the rub
2144      * ADDLOCAL and its friend OVERRIDE INSTALLLEVLE
2145      * I have confirmed this if ADDLOCALis stated then the INSTALLLEVEL is
2146      * itnored for all the features. seems strange, epsecially since it is not
2147      * documented anywhere, but it is how it works. 
2148      */
2149     
2150     override = load_dynamic_property(package,szAddLocal,NULL);
2151   
2152     if (override)
2153     {
2154         for(i = 0; i < package->loaded_features; i++)
2155         {
2156             if (strcmpiW(override,all)==0)
2157             {
2158                 package->features[i].ActionRequest= INSTALLSTATE_LOCAL;
2159                 package->features[i].Action = INSTALLSTATE_LOCAL;
2160             }
2161             else
2162             {
2163                 LPWSTR ptr = override;
2164                 LPWSTR ptr2 = strchrW(override,',');
2165
2166                 while (ptr)
2167                 {
2168                     if ((ptr2 && 
2169                         strncmpW(ptr,package->features[i].Feature, ptr2-ptr)==0)
2170                         || (!ptr2 &&
2171                         strcmpW(ptr,package->features[i].Feature)==0))
2172                     {
2173                         package->features[i].ActionRequest= INSTALLSTATE_LOCAL;
2174                         package->features[i].Action = INSTALLSTATE_LOCAL;
2175                         break;
2176                     }
2177                     if (ptr2)
2178                     {
2179                         ptr=ptr2+1;
2180                         ptr2 = strchrW(ptr,',');
2181                     }
2182                     else
2183                         break;
2184                 }
2185             }
2186         }
2187         HeapFree(GetProcessHeap(),0,override);
2188     } 
2189     else
2190     {
2191         for(i = 0; i < package->loaded_features; i++)
2192         {
2193             BOOL feature_state= ((package->features[i].Level > 0) &&
2194                              (package->features[i].Level <= install_level));
2195
2196             if (feature_state)
2197             {
2198                 package->features[i].ActionRequest= INSTALLSTATE_LOCAL;
2199                 package->features[i].Action = INSTALLSTATE_LOCAL;
2200             }
2201         }
2202     }
2203
2204     /*
2205      * now we want to enable or disable components base on feature 
2206     */
2207
2208     for(i = 0; i < package->loaded_features; i++)
2209     {
2210         MSIFEATURE* feature = &package->features[i];
2211         TRACE("Examining Feature %s (Installed %i, Action %i, Request %i)\n",
2212             debugstr_w(feature->Feature), feature->Installed, feature->Action,
2213             feature->ActionRequest);
2214
2215         for( j = 0; j < feature->ComponentCount; j++)
2216         {
2217             MSICOMPONENT* component = &package->components[
2218                                                     feature->Components[j]];
2219
2220             if (!component->Enabled)
2221             {
2222                 component->Action = INSTALLSTATE_ABSENT;
2223                 component->ActionRequest = INSTALLSTATE_ABSENT;
2224             }
2225             else
2226             {
2227                 if (feature->Action == INSTALLSTATE_LOCAL)
2228                     component->Action = INSTALLSTATE_LOCAL;
2229                 if (feature->ActionRequest == INSTALLSTATE_LOCAL)
2230                     component->ActionRequest = INSTALLSTATE_LOCAL;
2231             }
2232         }
2233     } 
2234
2235     for(i = 0; i < package->loaded_components; i++)
2236     {
2237         MSICOMPONENT* component= &package->components[i];
2238
2239         TRACE("Result: Component %s (Installed %i, Action %i, Request %i)\n",
2240             debugstr_w(component->Component), component->Installed, 
2241             component->Action, component->ActionRequest);
2242     }
2243
2244
2245     return ERROR_SUCCESS;
2246 }
2247
2248 /* 
2249  * A lot is done in this function aside from just the costing.
2250  * The costing needs to be implemented at some point but for now I am going
2251  * to focus on the directory building
2252  *
2253  */
2254 static UINT ACTION_CostFinalize(MSIPACKAGE *package)
2255 {
2256     static const WCHAR ExecSeqQuery[] = {
2257         's','e','l','e','c','t',' ','*',' ','f','r','o','m',' ',
2258         'D','i','r','e','c','t','o','r','y',0};
2259     static const WCHAR ConditionQuery[] = {
2260         's','e','l','e','c','t',' ','*',' ','f','r','o','m',' ',
2261         'C','o','n','d','i','t','i','o','n',0};
2262     static const WCHAR szCosting[] = {
2263        'C','o','s','t','i','n','g','C','o','m','p','l','e','t','e',0 };
2264     static const WCHAR szlevel[] = {
2265         'I','N','S','T','A','L','L','L','E','V','E','L',0};
2266     static const WCHAR szOne[] = { '1', 0 };
2267     UINT rc;
2268     MSIQUERY * view;
2269     DWORD i;
2270     LPWSTR level;
2271
2272     TRACE("Building Directory properties\n");
2273
2274     rc = MSI_DatabaseOpenViewW(package->db, ExecSeqQuery, &view);
2275     if (rc == ERROR_SUCCESS)
2276     {
2277         rc = MSI_ViewExecute(view, 0);
2278         if (rc != ERROR_SUCCESS)
2279         {
2280             MSI_ViewClose(view);
2281             msiobj_release(&view->hdr);
2282             return rc;
2283         }
2284
2285         while (1)
2286         {
2287             WCHAR name[0x100];
2288             LPWSTR path;
2289             MSIRECORD * row = 0;
2290             DWORD sz;
2291
2292             rc = MSI_ViewFetch(view,&row);
2293             if (rc != ERROR_SUCCESS)
2294             {
2295                 rc = ERROR_SUCCESS;
2296                 break;
2297             }
2298
2299             sz=0x100;
2300             MSI_RecordGetStringW(row,1,name,&sz);
2301
2302             /* This helper function now does ALL the work */
2303             TRACE("Dir %s ...\n",debugstr_w(name));
2304             load_folder(package,name);
2305             path = resolve_folder(package,name,FALSE,TRUE,NULL);
2306             TRACE("resolves to %s\n",debugstr_w(path));
2307             HeapFree( GetProcessHeap(), 0, path);
2308
2309             msiobj_release(&row->hdr);
2310         }
2311         MSI_ViewClose(view);
2312         msiobj_release(&view->hdr);
2313     }
2314
2315     TRACE("File calculations %i files\n",package->loaded_files);
2316
2317     for (i = 0; i < package->loaded_files; i++)
2318     {
2319         MSICOMPONENT* comp = NULL;
2320         MSIFILE* file= NULL;
2321
2322         file = &package->files[i];
2323         if (file->ComponentIndex >= 0)
2324             comp = &package->components[file->ComponentIndex];
2325
2326         if (file->Temporary == TRUE)
2327             continue;
2328
2329         if (comp)
2330         {
2331             LPWSTR p;
2332
2333             /* calculate target */
2334             p = resolve_folder(package, comp->Directory, FALSE, FALSE, NULL);
2335
2336             HeapFree(GetProcessHeap(),0,file->TargetPath);
2337
2338             TRACE("file %s is named %s\n",
2339                    debugstr_w(file->File),debugstr_w(file->FileName));       
2340
2341             file->TargetPath = build_directory_name(2, p, file->FileName);
2342
2343             HeapFree(GetProcessHeap(),0,p);
2344
2345             TRACE("file %s resolves to %s\n",
2346                    debugstr_w(file->File),debugstr_w(file->TargetPath));       
2347
2348             if (GetFileAttributesW(file->TargetPath) == INVALID_FILE_ATTRIBUTES)
2349             {
2350                 file->State = 1;
2351                 comp->Cost += file->FileSize;
2352             }
2353             else
2354             {
2355                 if (file->Version)
2356                 {
2357                     DWORD handle;
2358                     DWORD versize;
2359                     UINT sz;
2360                     LPVOID version;
2361                     static const WCHAR name[] = 
2362                     {'\\',0};
2363                     static const WCHAR name_fmt[] = 
2364                     {'%','u','.','%','u','.','%','u','.','%','u',0};
2365                     WCHAR filever[0x100];
2366                     VS_FIXEDFILEINFO *lpVer;
2367
2368                     TRACE("Version comparison.. \n");
2369                     versize = GetFileVersionInfoSizeW(file->TargetPath,&handle);
2370                     version = HeapAlloc(GetProcessHeap(),0,versize);
2371                     GetFileVersionInfoW(file->TargetPath, 0, versize, version);
2372
2373                     VerQueryValueW(version, name, (LPVOID*)&lpVer, &sz);
2374
2375                     sprintfW(filever,name_fmt,
2376                         HIWORD(lpVer->dwFileVersionMS),
2377                         LOWORD(lpVer->dwFileVersionMS),
2378                         HIWORD(lpVer->dwFileVersionLS),
2379                         LOWORD(lpVer->dwFileVersionLS));
2380
2381                     TRACE("new %s old %s\n", debugstr_w(file->Version),
2382                           debugstr_w(filever));
2383                     if (strcmpiW(filever,file->Version)<0)
2384                     {
2385                         file->State = 2;
2386                         FIXME("cost should be diff in size\n");
2387                         comp->Cost += file->FileSize;
2388                     }
2389                     else
2390                         file->State = 3;
2391                     HeapFree(GetProcessHeap(),0,version);
2392                 }
2393                 else
2394                     file->State = 3;
2395             }
2396         } 
2397     }
2398
2399     TRACE("Evaluating Condition Table\n");
2400
2401     rc = MSI_DatabaseOpenViewW(package->db, ConditionQuery, &view);
2402     if (rc == ERROR_SUCCESS)
2403     {
2404         rc = MSI_ViewExecute(view, 0);
2405         if (rc != ERROR_SUCCESS)
2406         {
2407             MSI_ViewClose(view);
2408             msiobj_release(&view->hdr);
2409             return rc;
2410         }
2411     
2412         while (1)
2413         {
2414             WCHAR Feature[0x100];
2415             MSIRECORD * row = 0;
2416             DWORD sz;
2417             int feature_index;
2418
2419             rc = MSI_ViewFetch(view,&row);
2420
2421             if (rc != ERROR_SUCCESS)
2422             {
2423                 rc = ERROR_SUCCESS;
2424                 break;
2425             }
2426
2427             sz = 0x100;
2428             MSI_RecordGetStringW(row,1,Feature,&sz);
2429
2430             feature_index = get_loaded_feature(package,Feature);
2431             if (feature_index < 0)
2432                 ERR("FAILED to find loaded feature %s\n",debugstr_w(Feature));
2433             else
2434             {
2435                 LPWSTR Condition;
2436                 Condition = load_dynamic_stringW(row,3);
2437
2438                 if (MSI_EvaluateConditionW(package,Condition) == 
2439                     MSICONDITION_TRUE)
2440                 {
2441                     int level = MSI_RecordGetInteger(row,2);
2442                     TRACE("Reseting feature %s to level %i\n",
2443                            debugstr_w(Feature), level);
2444                     package->features[feature_index].Level = level;
2445                 }
2446                 HeapFree(GetProcessHeap(),0,Condition);
2447             }
2448
2449             msiobj_release(&row->hdr);
2450         }
2451         MSI_ViewClose(view);
2452         msiobj_release(&view->hdr);
2453     }
2454
2455     TRACE("Enabling or Disabling Components\n");
2456     for (i = 0; i < package->loaded_components; i++)
2457     {
2458         if (package->components[i].Condition[0])
2459         {
2460             if (MSI_EvaluateConditionW(package,
2461                 package->components[i].Condition) == MSICONDITION_FALSE)
2462             {
2463                 TRACE("Disabling component %s\n",
2464                       debugstr_w(package->components[i].Component));
2465                 package->components[i].Enabled = FALSE;
2466             }
2467         }
2468     }
2469
2470     MSI_SetPropertyW(package,szCosting,szOne);
2471     /* set default run level if not set */
2472     level = load_dynamic_property(package,szlevel,NULL);
2473     if (!level)
2474         MSI_SetPropertyW(package,szlevel, szOne);
2475     else
2476         HeapFree(GetProcessHeap(),0,level);
2477
2478     return SetFeatureStates(package);
2479
2480 }
2481
2482 /*
2483  * This is a helper function for handling embedded cabinet media
2484  */
2485 static UINT writeout_cabinet_stream(MSIPACKAGE *package, WCHAR* stream_name,
2486                                     WCHAR* source)
2487 {
2488     UINT rc;
2489     USHORT* data;
2490     UINT    size;
2491     DWORD   write;
2492     HANDLE  the_file;
2493     WCHAR tmp[MAX_PATH];
2494
2495     rc = read_raw_stream_data(package->db,stream_name,&data,&size); 
2496     if (rc != ERROR_SUCCESS)
2497         return rc;
2498
2499     write = MAX_PATH;
2500     if (MSI_GetPropertyW(package, cszTempFolder, tmp, &write))
2501         GetTempPathW(MAX_PATH,tmp);
2502
2503     GetTempFileNameW(tmp,stream_name,0,source);
2504
2505     track_tempfile(package,strrchrW(source,'\\'), source);
2506     the_file = CreateFileW(source, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS,
2507                            FILE_ATTRIBUTE_NORMAL, NULL);
2508
2509     if (the_file == INVALID_HANDLE_VALUE)
2510     {
2511         rc = ERROR_FUNCTION_FAILED;
2512         goto end;
2513     }
2514
2515     WriteFile(the_file,data,size,&write,NULL);
2516     CloseHandle(the_file);
2517     TRACE("wrote %li bytes to %s\n",write,debugstr_w(source));
2518 end:
2519     HeapFree(GetProcessHeap(),0,data);
2520     return rc;
2521 }
2522
2523
2524 /* Support functions for FDI functions */
2525 typedef struct
2526 {
2527     MSIPACKAGE* package;
2528     LPCSTR cab_path;
2529     LPCSTR file_name;
2530 } CabData;
2531
2532 static void * cabinet_alloc(ULONG cb)
2533 {
2534     return HeapAlloc(GetProcessHeap(), 0, cb);
2535 }
2536
2537 static void cabinet_free(void *pv)
2538 {
2539     HeapFree(GetProcessHeap(), 0, pv);
2540 }
2541
2542 static INT_PTR cabinet_open(char *pszFile, int oflag, int pmode)
2543 {
2544     DWORD dwAccess = 0;
2545     DWORD dwShareMode = 0;
2546     DWORD dwCreateDisposition = OPEN_EXISTING;
2547     switch (oflag & _O_ACCMODE)
2548     {
2549     case _O_RDONLY:
2550         dwAccess = GENERIC_READ;
2551         dwShareMode = FILE_SHARE_READ | FILE_SHARE_DELETE;
2552         break;
2553     case _O_WRONLY:
2554         dwAccess = GENERIC_WRITE;
2555         dwShareMode = FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE;
2556         break;
2557     case _O_RDWR:
2558         dwAccess = GENERIC_READ | GENERIC_WRITE;
2559         dwShareMode = FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE;
2560         break;
2561     }
2562     if ((oflag & (_O_CREAT | _O_EXCL)) == (_O_CREAT | _O_EXCL))
2563         dwCreateDisposition = CREATE_NEW;
2564     else if (oflag & _O_CREAT)
2565         dwCreateDisposition = CREATE_ALWAYS;
2566     return (INT_PTR)CreateFileA(pszFile, dwAccess, dwShareMode, NULL, dwCreateDisposition, 0, NULL);
2567 }
2568
2569 static UINT cabinet_read(INT_PTR hf, void *pv, UINT cb)
2570 {
2571     DWORD dwRead;
2572     if (ReadFile((HANDLE)hf, pv, cb, &dwRead, NULL))
2573         return dwRead;
2574     return 0;
2575 }
2576
2577 static UINT cabinet_write(INT_PTR hf, void *pv, UINT cb)
2578 {
2579     DWORD dwWritten;
2580     if (WriteFile((HANDLE)hf, pv, cb, &dwWritten, NULL))
2581         return dwWritten;
2582     return 0;
2583 }
2584
2585 static int cabinet_close(INT_PTR hf)
2586 {
2587     return CloseHandle((HANDLE)hf) ? 0 : -1;
2588 }
2589
2590 static long cabinet_seek(INT_PTR hf, long dist, int seektype)
2591 {
2592     /* flags are compatible and so are passed straight through */
2593     return SetFilePointer((HANDLE)hf, dist, NULL, seektype);
2594 }
2595
2596 static INT_PTR cabinet_notify(FDINOTIFICATIONTYPE fdint, PFDINOTIFICATION pfdin)
2597 {
2598     /* FIXME: try to do more processing in this function */
2599     switch (fdint)
2600     {
2601     case fdintCOPY_FILE:
2602     {
2603         CabData *data = (CabData*) pfdin->pv;
2604         ULONG len = strlen(data->cab_path) + strlen(pfdin->psz1);
2605         char *file;
2606
2607         LPWSTR trackname;
2608         LPWSTR trackpath;
2609         LPWSTR tracknametmp;
2610         static const WCHAR tmpprefix[] = {'C','A','B','T','M','P','_',0};
2611        
2612         if (data->file_name && strcmp(data->file_name,pfdin->psz1))
2613                 return 0;
2614         
2615         file = cabinet_alloc((len+1)*sizeof(char));
2616         strcpy(file, data->cab_path);
2617         strcat(file, pfdin->psz1);
2618
2619         TRACE("file: %s\n", debugstr_a(file));
2620
2621         /* track this file so it can be deleted if not installed */
2622         trackpath=strdupAtoW(file);
2623         tracknametmp=strdupAtoW(strrchr(file,'\\')+1);
2624         trackname = HeapAlloc(GetProcessHeap(),0,(strlenW(tracknametmp) + 
2625                                   strlenW(tmpprefix)+1) * sizeof(WCHAR));
2626
2627         strcpyW(trackname,tmpprefix);
2628         strcatW(trackname,tracknametmp);
2629
2630         track_tempfile(data->package, trackname, trackpath);
2631
2632         HeapFree(GetProcessHeap(),0,trackpath);
2633         HeapFree(GetProcessHeap(),0,trackname);
2634         HeapFree(GetProcessHeap(),0,tracknametmp);
2635
2636         return cabinet_open(file, _O_WRONLY | _O_CREAT, 0);
2637     }
2638     case fdintCLOSE_FILE_INFO:
2639     {
2640         FILETIME ft;
2641             FILETIME ftLocal;
2642         if (!DosDateTimeToFileTime(pfdin->date, pfdin->time, &ft))
2643             return -1;
2644         if (!LocalFileTimeToFileTime(&ft, &ftLocal))
2645             return -1;
2646         if (!SetFileTime((HANDLE)pfdin->hf, &ftLocal, 0, &ftLocal))
2647             return -1;
2648
2649         cabinet_close(pfdin->hf);
2650         return 1;
2651     }
2652     default:
2653         return 0;
2654     }
2655 }
2656
2657 /***********************************************************************
2658  *            extract_cabinet_file
2659  *
2660  * Extract files from a cab file.
2661  */
2662 static BOOL extract_a_cabinet_file(MSIPACKAGE* package, const WCHAR* source, 
2663                                  const WCHAR* path, const WCHAR* file)
2664 {
2665     HFDI hfdi;
2666     ERF erf;
2667     BOOL ret;
2668     char *cabinet;
2669     char *cab_path;
2670     char *file_name;
2671     CabData data;
2672
2673     TRACE("Extracting %s (%s) to %s\n",debugstr_w(source), 
2674                     debugstr_w(file), debugstr_w(path));
2675
2676     hfdi = FDICreate(cabinet_alloc,
2677                      cabinet_free,
2678                      cabinet_open,
2679                      cabinet_read,
2680                      cabinet_write,
2681                      cabinet_close,
2682                      cabinet_seek,
2683                      0,
2684                      &erf);
2685     if (!hfdi)
2686     {
2687         ERR("FDICreate failed\n");
2688         return FALSE;
2689     }
2690
2691     if (!(cabinet = strdupWtoA( source )))
2692     {
2693         FDIDestroy(hfdi);
2694         return FALSE;
2695     }
2696     if (!(cab_path = strdupWtoA( path )))
2697     {
2698         FDIDestroy(hfdi);
2699         HeapFree(GetProcessHeap(), 0, cabinet);
2700         return FALSE;
2701     }
2702
2703     data.package = package;
2704     data.cab_path = cab_path;
2705     if (file)
2706         file_name = strdupWtoA(file);
2707     else
2708         file_name = NULL;
2709     data.file_name = file_name;
2710
2711     ret = FDICopy(hfdi, cabinet, "", 0, cabinet_notify, NULL, &data);
2712
2713     if (!ret)
2714         ERR("FDICopy failed\n");
2715
2716     FDIDestroy(hfdi);
2717
2718     HeapFree(GetProcessHeap(), 0, cabinet);
2719     HeapFree(GetProcessHeap(), 0, cab_path);
2720     HeapFree(GetProcessHeap(), 0, file_name);
2721
2722     return ret;
2723 }
2724
2725 static UINT ready_media_for_file(MSIPACKAGE *package, UINT sequence, 
2726                                  WCHAR* path, WCHAR* file)
2727 {
2728     UINT rc;
2729     MSIQUERY * view;
2730     MSIRECORD * row = 0;
2731     static WCHAR source[MAX_PATH];
2732     static const WCHAR ExecSeqQuery[] = {
2733         's','e','l','e','c','t',' ','*',' ',
2734         'f','r','o','m',' ','M','e','d','i','a',' ',
2735         'w','h','e','r','e',' ','L','a','s','t','S','e','q','u','e','n','c','e',' ','>','=',' ','%','i',' ',
2736         'o','r','d','e','r',' ','b','y',' ','L','a','s','t','S','e','q','u','e','n','c','e',0};
2737     WCHAR Query[1024];
2738     WCHAR cab[0x100];
2739     DWORD sz=0x100;
2740     INT seq;
2741     static UINT last_sequence = 0; 
2742
2743     if (sequence <= last_sequence)
2744     {
2745         TRACE("Media already ready (%u, %u)\n",sequence,last_sequence);
2746         /*extract_a_cabinet_file(package, source,path,file); */
2747         return ERROR_SUCCESS;
2748     }
2749
2750     sprintfW(Query,ExecSeqQuery,sequence);
2751
2752     rc = MSI_DatabaseOpenViewW(package->db, Query, &view);
2753     if (rc != ERROR_SUCCESS)
2754         return rc;
2755
2756     rc = MSI_ViewExecute(view, 0);
2757     if (rc != ERROR_SUCCESS)
2758     {
2759         MSI_ViewClose(view);
2760         msiobj_release(&view->hdr);
2761         return rc;
2762     }
2763
2764     rc = MSI_ViewFetch(view,&row);
2765     if (rc != ERROR_SUCCESS)
2766     {
2767         MSI_ViewClose(view);
2768         msiobj_release(&view->hdr);
2769         return rc;
2770     }
2771     seq = MSI_RecordGetInteger(row,2);
2772     last_sequence = seq;
2773
2774     if (!MSI_RecordIsNull(row,4))
2775     {
2776         sz=0x100;
2777         MSI_RecordGetStringW(row,4,cab,&sz);
2778         TRACE("Source is CAB %s\n",debugstr_w(cab));
2779         /* the stream does not contain the # character */
2780         if (cab[0]=='#')
2781         {
2782             writeout_cabinet_stream(package,&cab[1],source);
2783             strcpyW(path,source);
2784             *(strrchrW(path,'\\')+1)=0;
2785         }
2786         else
2787         {
2788             sz = MAX_PATH;
2789             if (MSI_GetPropertyW(package, cszSourceDir, source, &sz))
2790             {
2791                 ERR("No Source dir defined \n");
2792                 rc = ERROR_FUNCTION_FAILED;
2793             }
2794             else
2795             {
2796                 strcpyW(path,source);
2797                 strcatW(source,cab);
2798                 /* extract the cab file into a folder in the temp folder */
2799                 sz = MAX_PATH;
2800                 if (MSI_GetPropertyW(package, cszTempFolder,path, &sz) 
2801                                     != ERROR_SUCCESS)
2802                     GetTempPathW(MAX_PATH,path);
2803             }
2804         }
2805         rc = !extract_a_cabinet_file(package, source,path,NULL);
2806     }
2807     else
2808     {
2809         sz = MAX_PATH;
2810         MSI_GetPropertyW(package,cszSourceDir,source,&sz);
2811         strcpyW(path,source);
2812     }
2813     msiobj_release(&row->hdr);
2814     MSI_ViewClose(view);
2815     msiobj_release(&view->hdr);
2816     return rc;
2817 }
2818
2819 inline static UINT create_component_directory ( MSIPACKAGE* package, INT component)
2820 {
2821     UINT rc = ERROR_SUCCESS;
2822     MSIFOLDER *folder;
2823     LPWSTR install_path;
2824
2825     install_path = resolve_folder(package, package->components[component].Directory,
2826                         FALSE, FALSE, &folder);
2827     if (!install_path)
2828         return ERROR_FUNCTION_FAILED; 
2829
2830     /* create the path */
2831     if (folder->State == 0)
2832     {
2833         create_full_pathW(install_path);
2834         folder->State = 2;
2835     }
2836     HeapFree(GetProcessHeap(), 0, install_path);
2837
2838     return rc;
2839 }
2840
2841 static UINT ACTION_InstallFiles(MSIPACKAGE *package)
2842 {
2843     UINT rc = ERROR_SUCCESS;
2844     DWORD index;
2845     MSIRECORD * uirow;
2846     WCHAR uipath[MAX_PATH];
2847
2848     if (!package)
2849         return ERROR_INVALID_HANDLE;
2850
2851     /* increment progress bar each time action data is sent */
2852     ui_progress(package,1,1,0,0);
2853
2854     for (index = 0; index < package->loaded_files; index++)
2855     {
2856         WCHAR path_to_source[MAX_PATH];
2857         MSIFILE *file;
2858         
2859         file = &package->files[index];
2860
2861         if (file->Temporary)
2862             continue;
2863
2864         if (package->components[file->ComponentIndex].ActionRequest != 
2865              INSTALLSTATE_LOCAL)
2866         {
2867             ui_progress(package,2,file->FileSize,0,0);
2868             TRACE("File %s is not scheduled for install\n",
2869                    debugstr_w(file->File));
2870
2871             continue;
2872         }
2873
2874         if ((file->State == 1) || (file->State == 2))
2875         {
2876             LPWSTR p;
2877             INT len;
2878             MSICOMPONENT* comp = NULL;
2879
2880             TRACE("Installing %s\n",debugstr_w(file->File));
2881             rc = ready_media_for_file(package,file->Sequence,path_to_source,
2882                             file->File);
2883             /* 
2884              * WARNING!
2885              * our file table could change here because a new temp file
2886              * may have been created
2887              */
2888             file = &package->files[index];
2889             if (rc != ERROR_SUCCESS)
2890             {
2891                 ERR("Unable to ready media\n");
2892                 rc = ERROR_FUNCTION_FAILED;
2893                 break;
2894             }
2895
2896             create_component_directory( package, file->ComponentIndex);
2897
2898             /* recalculate file paths because things may have changed */
2899
2900             if (file->ComponentIndex >= 0)
2901                 comp = &package->components[file->ComponentIndex];
2902
2903             p = resolve_folder(package, comp->Directory, FALSE, FALSE, NULL);
2904             HeapFree(GetProcessHeap(),0,file->TargetPath);
2905
2906             file->TargetPath = build_directory_name(2, p, file->FileName);
2907
2908             len = strlenW(path_to_source) + strlenW(file->File) + 2;
2909             file->SourcePath = HeapAlloc(GetProcessHeap(),0,len*sizeof(WCHAR));
2910             strcpyW(file->SourcePath, path_to_source);
2911             strcatW(file->SourcePath, file->File);
2912
2913             TRACE("file paths %s to %s\n",debugstr_w(file->SourcePath),
2914                   debugstr_w(file->TargetPath));
2915
2916             /* the UI chunk */
2917             uirow=MSI_CreateRecord(9);
2918             MSI_RecordSetStringW(uirow,1,file->File);
2919             strcpyW(uipath,file->TargetPath);
2920             *(strrchrW(uipath,'\\')+1)=0;
2921             MSI_RecordSetStringW(uirow,9,uipath);
2922             MSI_RecordSetInteger(uirow,6,file->FileSize);
2923             ui_actiondata(package,szInstallFiles,uirow);
2924             msiobj_release( &uirow->hdr );
2925             ui_progress(package,2,file->FileSize,0,0);
2926
2927             if (!MoveFileW(file->SourcePath,file->TargetPath))
2928             {
2929                 rc = GetLastError();
2930                 ERR("Unable to move file (%s -> %s) (error %d)\n",
2931                      debugstr_w(file->SourcePath), debugstr_w(file->TargetPath),
2932                       rc);
2933                 if (rc == ERROR_ALREADY_EXISTS && file->State == 2)
2934                 {
2935                     CopyFileW(file->SourcePath,file->TargetPath,FALSE);
2936                     DeleteFileW(file->SourcePath);
2937                     rc = 0;
2938                 }
2939                 else if (rc == ERROR_FILE_NOT_FOUND)
2940                 {
2941                     ERR("Source File Not Found!  Continueing\n");
2942                     rc = 0;
2943                 }
2944                 else
2945                 {
2946                     ERR("Ignoring Error and continuing...\n");
2947                     rc = 0;
2948                 }
2949             }
2950             else
2951                 file->State = 4;
2952         }
2953     }
2954
2955     return rc;
2956 }
2957
2958 inline static UINT get_file_target(MSIPACKAGE *package, LPCWSTR file_key, 
2959                                    LPWSTR* file_source)
2960 {
2961     DWORD index;
2962
2963     if (!package)
2964         return ERROR_INVALID_HANDLE;
2965
2966     for (index = 0; index < package->loaded_files; index ++)
2967     {
2968         if (strcmpW(file_key,package->files[index].File)==0)
2969         {
2970             if (package->files[index].State >= 2)
2971             {
2972                 *file_source = dupstrW(package->files[index].TargetPath);
2973                 return ERROR_SUCCESS;
2974             }
2975             else
2976                 return ERROR_FILE_NOT_FOUND;
2977         }
2978     }
2979
2980     return ERROR_FUNCTION_FAILED;
2981 }
2982
2983 static UINT ACTION_DuplicateFiles(MSIPACKAGE *package)
2984 {
2985     UINT rc;
2986     MSIQUERY * view;
2987     MSIRECORD * row = 0;
2988     static const WCHAR ExecSeqQuery[] = {
2989         's','e','l','e','c','t',' ','*',' ','f','r','o','m',' ',
2990         'D','u','p','l','i','c','a','t','e','F','i','l','e',0};
2991
2992     if (!package)
2993         return ERROR_INVALID_HANDLE;
2994
2995     rc = MSI_DatabaseOpenViewW(package->db, ExecSeqQuery, &view);
2996     if (rc != ERROR_SUCCESS)
2997         return ERROR_SUCCESS;
2998
2999     rc = MSI_ViewExecute(view, 0);
3000     if (rc != ERROR_SUCCESS)
3001     {
3002         MSI_ViewClose(view);
3003         msiobj_release(&view->hdr);
3004         return rc;
3005     }
3006
3007     while (1)
3008     {
3009         WCHAR file_key[0x100];
3010         WCHAR *file_source = NULL;
3011         WCHAR dest_name[0x100];
3012         LPWSTR dest_path, dest;
3013         WCHAR component[0x100];
3014         INT component_index;
3015
3016         DWORD sz=0x100;
3017
3018         rc = MSI_ViewFetch(view,&row);
3019         if (rc != ERROR_SUCCESS)
3020         {
3021             rc = ERROR_SUCCESS;
3022             break;
3023         }
3024
3025         sz=0x100;
3026         rc = MSI_RecordGetStringW(row,2,component,&sz);
3027         if (rc != ERROR_SUCCESS)
3028         {
3029             ERR("Unable to get component\n");
3030             msiobj_release(&row->hdr);
3031             break;
3032         }
3033
3034         component_index = get_loaded_component(package,component);
3035         if (package->components[component_index].ActionRequest != 
3036              INSTALLSTATE_LOCAL)
3037         {
3038             TRACE("Skipping copy due to disabled component\n");
3039
3040             /* the action taken was the same as the current install state */        
3041             package->components[component_index].Action =
3042                 package->components[component_index].Installed;
3043
3044             msiobj_release(&row->hdr);
3045             continue;
3046         }
3047
3048         package->components[component_index].Action = INSTALLSTATE_LOCAL;
3049         package->components[component_index].Installed = INSTALLSTATE_LOCAL;
3050
3051         sz=0x100;
3052         rc = MSI_RecordGetStringW(row,3,file_key,&sz);
3053         if (rc != ERROR_SUCCESS)
3054         {
3055             ERR("Unable to get file key\n");
3056             msiobj_release(&row->hdr);
3057             break;
3058         }
3059
3060         rc = get_file_target(package,file_key,&file_source);
3061
3062         if (rc != ERROR_SUCCESS)
3063         {
3064             ERR("Original file unknown %s\n",debugstr_w(file_key));
3065             msiobj_release(&row->hdr);
3066             HeapFree(GetProcessHeap(),0,file_source);
3067             continue;
3068         }
3069
3070         if (MSI_RecordIsNull(row,4))
3071         {
3072             strcpyW(dest_name,strrchrW(file_source,'\\')+1);
3073         }
3074         else
3075         {
3076             sz=0x100;
3077             MSI_RecordGetStringW(row,4,dest_name,&sz);
3078             reduce_to_longfilename(dest_name);
3079          }
3080
3081         if (MSI_RecordIsNull(row,5))
3082         {
3083             LPWSTR p;
3084             dest_path = dupstrW(file_source);
3085             p = strrchrW(dest_path,'\\');
3086             if (p)
3087                 *p=0;
3088         }
3089         else
3090         {
3091             WCHAR destkey[0x100];
3092             sz=0x100;
3093             MSI_RecordGetStringW(row,5,destkey,&sz);
3094             sz = 0x100;
3095             dest_path = resolve_folder(package, destkey, FALSE,FALSE,NULL);
3096             if (!dest_path)
3097             {
3098                 ERR("Unable to get destination folder\n");
3099                 msiobj_release(&row->hdr);
3100                 HeapFree(GetProcessHeap(),0,file_source);
3101                 break;
3102             }
3103         }
3104
3105         dest = build_directory_name(2, dest_path, dest_name);
3106         HeapFree(GetProcessHeap(), 0, dest_path);
3107            
3108         TRACE("Duplicating file %s to %s\n",debugstr_w(file_source),
3109               debugstr_w(dest)); 
3110         
3111         if (strcmpW(file_source,dest))
3112             rc = !CopyFileW(file_source,dest,TRUE);
3113         else
3114             rc = ERROR_SUCCESS;
3115         
3116         if (rc != ERROR_SUCCESS)
3117             ERR("Failed to copy file\n");
3118
3119         FIXME("We should track these duplicate files as well\n");   
3120  
3121         msiobj_release(&row->hdr);
3122         HeapFree(GetProcessHeap(),0,dest);
3123         HeapFree(GetProcessHeap(),0,file_source);
3124     }
3125     MSI_ViewClose(view);
3126     msiobj_release(&view->hdr);
3127     return rc;
3128 }
3129
3130
3131 /* OK this value is "interpreted" and then formatted based on the 
3132    first few characters */
3133 static LPSTR parse_value(MSIPACKAGE *package, WCHAR *value, DWORD *type, 
3134                          DWORD *size)
3135 {
3136     LPSTR data = NULL;
3137     if (value[0]=='#' && value[1]!='#' && value[1]!='%')
3138     {
3139         if (value[1]=='x')
3140         {
3141             LPWSTR ptr;
3142             CHAR byte[5];
3143             LPWSTR deformated;
3144             int count;
3145
3146             deformat_string(package, &value[2], &deformated);
3147
3148             /* binary value type */
3149             ptr = deformated; 
3150             *type=REG_BINARY;
3151             *size = strlenW(ptr)/2;
3152             data = HeapAlloc(GetProcessHeap(),0,*size);
3153           
3154             byte[0] = '0'; 
3155             byte[1] = 'x'; 
3156             byte[4] = 0; 
3157             count = 0;
3158             while (*ptr)
3159             {
3160                 byte[2]= *ptr;
3161                 ptr++;
3162                 byte[3]= *ptr;
3163                 ptr++;
3164                 data[count] = (BYTE)strtol(byte,NULL,0);
3165                 count ++;
3166             }
3167             HeapFree(GetProcessHeap(),0,deformated);
3168
3169             TRACE("Data %li bytes(%i)\n",*size,count);
3170         }
3171         else
3172         {
3173             LPWSTR deformated;
3174             deformat_string(package, &value[1], &deformated);
3175
3176             *type=REG_DWORD; 
3177             *size = sizeof(DWORD);
3178             data = HeapAlloc(GetProcessHeap(),0,*size);
3179             *(LPDWORD)data = atoiW(deformated); 
3180             TRACE("DWORD %i\n",*data);
3181
3182             HeapFree(GetProcessHeap(),0,deformated);
3183         }
3184     }
3185     else
3186     {
3187         static const WCHAR szMulti[] = {'[','~',']',0};
3188         WCHAR *ptr;
3189         *type=REG_SZ;
3190
3191         if (value[0]=='#')
3192         {
3193             if (value[1]=='%')
3194             {
3195                 ptr = &value[2];
3196                 *type=REG_EXPAND_SZ;
3197             }
3198             else
3199                 ptr = &value[1];
3200          }
3201          else
3202             ptr=value;
3203
3204         if (strstrW(value,szMulti))
3205             *type = REG_MULTI_SZ;
3206
3207         *size = deformat_string(package, ptr,(LPWSTR*)&data);
3208     }
3209     return data;
3210 }
3211
3212 static UINT ACTION_WriteRegistryValues(MSIPACKAGE *package)
3213 {
3214     UINT rc;
3215     MSIQUERY * view;
3216     MSIRECORD * row = 0;
3217     static const WCHAR ExecSeqQuery[] = {
3218         's','e','l','e','c','t',' ','*',' ',
3219         'f','r','o','m',' ','R','e','g','i','s','t','r','y',0 };
3220
3221     if (!package)
3222         return ERROR_INVALID_HANDLE;
3223
3224     rc = MSI_DatabaseOpenViewW(package->db, ExecSeqQuery, &view);
3225     if (rc != ERROR_SUCCESS)
3226         return ERROR_SUCCESS;
3227
3228     rc = MSI_ViewExecute(view, 0);
3229     if (rc != ERROR_SUCCESS)
3230     {
3231         MSI_ViewClose(view);
3232         msiobj_release(&view->hdr);
3233         return rc;
3234     }
3235
3236     /* increment progress bar each time action data is sent */
3237     ui_progress(package,1,REG_PROGRESS_VALUE,1,0);
3238
3239     while (1)
3240     {
3241         static const WCHAR szHCR[] = 
3242 {'H','K','E','Y','_','C','L','A','S','S','E','S','_','R','O','O','T','\\',0};
3243         static const WCHAR szHCU[] =
3244 {'H','K','E','Y','_','C','U','R','R','E','N','T','_','U','S','E','R','\\',0};
3245         static const WCHAR szHLM[] =
3246 {'H','K','E','Y','_','L','O','C','A','L','_','M','A','C','H','I','N','E',
3247 '\\',0};
3248         static const WCHAR szHU[] =
3249 {'H','K','E','Y','_','U','S','E','R','S','\\',0};
3250
3251         LPSTR value_data = NULL;
3252         HKEY  root_key, hkey;
3253         DWORD type,size;
3254         LPWSTR value, key, name, component, deformated;
3255         LPCWSTR szRoot;
3256         INT component_index;
3257         MSIRECORD * uirow;
3258         LPWSTR uikey;
3259         INT   root;
3260
3261         rc = MSI_ViewFetch(view,&row);
3262         if (rc != ERROR_SUCCESS)
3263         {
3264             rc = ERROR_SUCCESS;
3265             break;
3266         }
3267         ui_progress(package,2,0,0,0);
3268
3269         value = NULL;
3270         key = NULL;
3271         uikey = NULL;
3272         name = NULL;
3273
3274         component = load_dynamic_stringW(row, 6);
3275         component_index = get_loaded_component(package,component);
3276
3277         if (package->components[component_index].ActionRequest != 
3278              INSTALLSTATE_LOCAL)
3279         {
3280             TRACE("Skipping write due to disabled component\n");
3281             msiobj_release(&row->hdr);
3282
3283             package->components[component_index].Action =
3284                 package->components[component_index].Installed;
3285
3286             goto next;
3287         }
3288
3289         package->components[component_index].Action = INSTALLSTATE_LOCAL;
3290         package->components[component_index].Installed = INSTALLSTATE_LOCAL;
3291
3292         /* null values have special meanings during uninstalls and such */
3293         
3294         if(MSI_RecordIsNull(row,5))
3295         {
3296             msiobj_release(&row->hdr);
3297             goto next;
3298         }
3299
3300         root = MSI_RecordGetInteger(row,2);
3301         key = load_dynamic_stringW(row, 3);
3302       
3303         name = load_dynamic_stringW(row, 4);
3304    
3305         /* get the root key */
3306         switch (root)
3307         {
3308             case 0:  root_key = HKEY_CLASSES_ROOT; 
3309                      szRoot = szHCR;
3310                      break;
3311             case 1:  root_key = HKEY_CURRENT_USER;
3312                      szRoot = szHCU;
3313                      break;
3314             case 2:  root_key = HKEY_LOCAL_MACHINE;
3315                      szRoot = szHLM;
3316                      break;
3317             case 3:  root_key = HKEY_USERS; 
3318                      szRoot = szHU;
3319                      break;
3320             default:
3321                  ERR("Unknown root %i\n",root);
3322                  root_key=NULL;
3323                  szRoot = NULL;
3324                  break;
3325         }
3326         if (!root_key)
3327         {
3328             msiobj_release(&row->hdr);
3329             goto next;
3330         }
3331
3332         deformat_string(package, key , &deformated);
3333         size = strlenW(deformated) + strlenW(szRoot) + 1;
3334         uikey = HeapAlloc(GetProcessHeap(), 0, size*sizeof(WCHAR));
3335         strcpyW(uikey,szRoot);
3336         strcatW(uikey,deformated);
3337
3338         if (RegCreateKeyW( root_key, deformated, &hkey))
3339         {
3340             ERR("Could not create key %s\n",debugstr_w(deformated));
3341             msiobj_release(&row->hdr);
3342             HeapFree(GetProcessHeap(),0,deformated);
3343             goto next;
3344         }
3345         HeapFree(GetProcessHeap(),0,deformated);
3346
3347         value = load_dynamic_stringW(row,5);
3348         value_data = parse_value(package, value, &type, &size); 
3349
3350         deformat_string(package, name, &deformated);
3351
3352         if (value_data)
3353         {
3354             TRACE("Setting value %s\n",debugstr_w(deformated));
3355             RegSetValueExW(hkey, deformated, 0, type, value_data, size);
3356
3357             uirow = MSI_CreateRecord(3);
3358             MSI_RecordSetStringW(uirow,2,deformated);
3359             MSI_RecordSetStringW(uirow,1,uikey);
3360
3361             if (type == REG_SZ)
3362                 MSI_RecordSetStringW(uirow,3,(LPWSTR)value_data);
3363             else
3364                 MSI_RecordSetStringW(uirow,3,value);
3365
3366             ui_actiondata(package,szWriteRegistryValues,uirow);
3367             msiobj_release( &uirow->hdr );
3368
3369             HeapFree(GetProcessHeap(),0,value_data);
3370         }
3371         HeapFree(GetProcessHeap(),0,value);
3372         HeapFree(GetProcessHeap(),0,deformated);
3373
3374         msiobj_release(&row->hdr);
3375         RegCloseKey(hkey);
3376 next:
3377         HeapFree(GetProcessHeap(),0,uikey);
3378         HeapFree(GetProcessHeap(),0,key);
3379         HeapFree(GetProcessHeap(),0,name);
3380         HeapFree(GetProcessHeap(),0,component);
3381     }
3382     MSI_ViewClose(view);
3383     msiobj_release(&view->hdr);
3384     return rc;
3385 }
3386
3387 static UINT ACTION_InstallInitialize(MSIPACKAGE *package)
3388 {
3389     return ERROR_SUCCESS;
3390 }
3391
3392
3393 static UINT ACTION_InstallValidate(MSIPACKAGE *package)
3394 {
3395     DWORD progress = 0;
3396     DWORD total = 0;
3397     static const WCHAR q1[]={
3398         'S','E','L','E','C','T',' ','*',' ',
3399         'F','R','O','M',' ','R','e','g','i','s','t','r','y',0};
3400     UINT rc;
3401     MSIQUERY * view;
3402     MSIRECORD * row = 0;
3403     int i;
3404
3405     TRACE(" InstallValidate \n");
3406
3407     rc = MSI_DatabaseOpenViewW(package->db, q1, &view);
3408     if (rc != ERROR_SUCCESS)
3409         return ERROR_SUCCESS;
3410
3411     rc = MSI_ViewExecute(view, 0);
3412     if (rc != ERROR_SUCCESS)
3413     {
3414         MSI_ViewClose(view);
3415         msiobj_release(&view->hdr);
3416         return rc;
3417     }
3418     while (1)
3419     {
3420         rc = MSI_ViewFetch(view,&row);
3421         if (rc != ERROR_SUCCESS)
3422         {
3423             rc = ERROR_SUCCESS;
3424             break;
3425         }
3426         progress +=1;
3427
3428         msiobj_release(&row->hdr);
3429     }
3430     MSI_ViewClose(view);
3431     msiobj_release(&view->hdr);
3432
3433     total = total + progress * REG_PROGRESS_VALUE;
3434     total = total + package->loaded_components * COMPONENT_PROGRESS_VALUE;
3435     for (i=0; i < package->loaded_files; i++)
3436         total += package->files[i].FileSize;
3437     ui_progress(package,0,total,0,0);
3438
3439     return ERROR_SUCCESS;
3440 }
3441
3442 static UINT ACTION_LaunchConditions(MSIPACKAGE *package)
3443 {
3444     UINT rc;
3445     MSIQUERY * view = NULL;
3446     MSIRECORD * row = 0;
3447     static const WCHAR ExecSeqQuery[] = {
3448         'S','E','L','E','C','T',' ','*',' ',
3449         'f','r','o','m',' ','L','a','u','n','c','h','C','o','n','d','i','t','i','o','n',0};
3450     static const WCHAR title[]=
3451             {'I','n','s','t','a','l','l',' ','F','a', 'i','l','e','d',0};
3452
3453     TRACE("Checking launch conditions\n");
3454
3455     rc = MSI_DatabaseOpenViewW(package->db, ExecSeqQuery, &view);
3456     if (rc != ERROR_SUCCESS)
3457         return ERROR_SUCCESS;
3458
3459     rc = MSI_ViewExecute(view, 0);
3460     if (rc != ERROR_SUCCESS)
3461     {
3462         MSI_ViewClose(view);
3463         msiobj_release(&view->hdr);
3464         return rc;
3465     }
3466
3467     rc = ERROR_SUCCESS;
3468     while (rc == ERROR_SUCCESS)
3469     {
3470         LPWSTR cond = NULL; 
3471         LPWSTR message = NULL;
3472
3473         rc = MSI_ViewFetch(view,&row);
3474         if (rc != ERROR_SUCCESS)
3475         {
3476             rc = ERROR_SUCCESS;
3477             break;
3478         }
3479
3480         cond = load_dynamic_stringW(row,1);
3481
3482         if (MSI_EvaluateConditionW(package,cond) != MSICONDITION_TRUE)
3483         {
3484             LPWSTR deformated;
3485             message = load_dynamic_stringW(row,2);
3486             deformat_string(package,message,&deformated); 
3487             MessageBoxW(NULL,deformated,title,MB_OK);
3488             HeapFree(GetProcessHeap(),0,message);
3489             HeapFree(GetProcessHeap(),0,deformated);
3490             rc = ERROR_FUNCTION_FAILED;
3491         }
3492         HeapFree(GetProcessHeap(),0,cond);
3493         msiobj_release(&row->hdr);
3494     }
3495     MSI_ViewClose(view);
3496     msiobj_release(&view->hdr);
3497     return rc;
3498 }
3499
3500 static LPWSTR resolve_keypath( MSIPACKAGE* package, INT
3501                             component_index)
3502 {
3503     MSICOMPONENT* cmp = &package->components[component_index];
3504
3505     if (cmp->KeyPath[0]==0)
3506     {
3507         LPWSTR p = resolve_folder(package,cmp->Directory,FALSE,FALSE,NULL);
3508         return p;
3509     }
3510     if (cmp->Attributes & 0x4)
3511     {
3512         MSIQUERY * view;
3513         MSIRECORD * row = 0;
3514         UINT rc,root,len;
3515         LPWSTR key,deformated,buffer,name,deformated_name;
3516         static const WCHAR ExecSeqQuery[] = {
3517         's','e','l','e','c','t',' ','*',' ',
3518         'f','r','o','m',' ','R','e','g','i','s','t','r','y',' ',
3519 'w','h','e','r','e',' ','R','e','g','i','s','t','r','y',' ','=',' '
3520 ,'`','%','s','`',0 };
3521         static const WCHAR fmt[]={'%','0','2','i',':','%','s',0};
3522         static const WCHAR fmt2[]={'%','0','2','i',':','%','s','\\','%','s',0};
3523
3524         rc = MSI_OpenQuery(package->db,&view,ExecSeqQuery,cmp->KeyPath);
3525
3526         if (rc!=ERROR_SUCCESS)
3527             return NULL;
3528
3529         rc = MSI_ViewExecute(view, 0);
3530         if (rc != ERROR_SUCCESS)
3531         {
3532             MSI_ViewClose(view);
3533             msiobj_release(&view->hdr);
3534             return NULL;
3535         }
3536
3537         rc = MSI_ViewFetch(view,&row);
3538         if (rc != ERROR_SUCCESS)
3539         {
3540             MSI_ViewClose(view);
3541             msiobj_release(&view->hdr);
3542             return NULL;
3543         }
3544
3545         root = MSI_RecordGetInteger(row,2);
3546         key = load_dynamic_stringW(row, 3);
3547         name = load_dynamic_stringW(row, 4);
3548         deformat_string(package, key , &deformated);
3549         deformat_string(package, name, &deformated_name);
3550
3551         len = strlenW(deformated) + 5;
3552         if (deformated_name)
3553             len+=strlenW(deformated_name);
3554
3555         buffer = HeapAlloc(GetProcessHeap(),0, len *sizeof(WCHAR));
3556
3557         if (deformated_name)
3558             sprintfW(buffer,fmt2,root,deformated,deformated_name);
3559         else
3560             sprintfW(buffer,fmt,root,deformated);
3561
3562         HeapFree(GetProcessHeap(),0,key);
3563         HeapFree(GetProcessHeap(),0,deformated);
3564         HeapFree(GetProcessHeap(),0,name);
3565         HeapFree(GetProcessHeap(),0,deformated_name);
3566         msiobj_release(&row->hdr);
3567         MSI_ViewClose(view);
3568         msiobj_release(&view->hdr);
3569
3570         return buffer;
3571     }
3572     else if (cmp->Attributes & 0x20)
3573     {
3574         FIXME("UNIMPLEMENTED keypath as ODBC Source\n");
3575         return NULL;
3576     }
3577     else
3578     {
3579         int j;
3580         j = get_loaded_file(package,cmp->KeyPath);
3581
3582         if (j>=0)
3583         {
3584             LPWSTR p = dupstrW(package->files[j].TargetPath);
3585             return p;
3586         }
3587     }
3588     return NULL;
3589 }
3590
3591 /*
3592  * Ok further analysis makes me think that this work is
3593  * actually done in the PublishComponents and PublishFeatures
3594  * step, and not here.  It appears like the keypath and all that is
3595  * resolved in this step, however actually written in the Publish steps.
3596  * But we will leave it here for now because it is unclear
3597  */
3598 static UINT ACTION_ProcessComponents(MSIPACKAGE *package)
3599 {
3600     LPWSTR productcode;
3601     WCHAR squished_pc[GUID_SIZE];
3602     WCHAR squished_cc[GUID_SIZE];
3603     UINT rc;
3604     DWORD i;
3605     HKEY hkey=0,hkey2=0;
3606     static const WCHAR szProductCode[]=
3607          {'P','r','o','d','u','c','t','C','o','d','e',0};
3608
3609     if (!package)
3610         return ERROR_INVALID_HANDLE;
3611
3612     /* writes the Component and Features values to the registry */
3613     productcode = load_dynamic_property(package,szProductCode,&rc);
3614     if (!productcode)
3615         return rc;
3616
3617     rc = MSIREG_OpenComponents(&hkey);
3618     if (rc != ERROR_SUCCESS)
3619         goto end;
3620       
3621     squash_guid(productcode,squished_pc);
3622     ui_progress(package,1,COMPONENT_PROGRESS_VALUE,1,0);
3623     for (i = 0; i < package->loaded_components; i++)
3624     {
3625         ui_progress(package,2,0,0,0);
3626         if (package->components[i].ComponentId[0]!=0)
3627         {
3628             WCHAR *keypath = NULL;
3629             MSIRECORD * uirow;
3630
3631             squash_guid(package->components[i].ComponentId,squished_cc);
3632             rc = RegCreateKeyW(hkey,squished_cc,&hkey2);
3633             if (rc != ERROR_SUCCESS)
3634                 continue;
3635            
3636             keypath = resolve_keypath(package,i);
3637             if (keypath)
3638             {
3639                 RegSetValueExW(hkey2,squished_pc,0,REG_SZ,(LPVOID)keypath,
3640                             (strlenW(keypath)+1)*sizeof(WCHAR));
3641                 RegCloseKey(hkey2);
3642         
3643                 /* UI stuff */
3644                 uirow = MSI_CreateRecord(3);
3645                 MSI_RecordSetStringW(uirow,1,productcode);
3646                 MSI_RecordSetStringW(uirow,2,package->components[i].
3647                                                         ComponentId);
3648                 MSI_RecordSetStringW(uirow,3,keypath);
3649                 ui_actiondata(package,szProcessComponents,uirow);
3650                 msiobj_release( &uirow->hdr );
3651                 HeapFree(GetProcessHeap(),0,keypath);
3652             }
3653         }
3654     } 
3655 end:
3656     HeapFree(GetProcessHeap(), 0, productcode);
3657     RegCloseKey(hkey);
3658     return rc;
3659 }
3660
3661 static UINT ACTION_RegisterTypeLibraries(MSIPACKAGE *package)
3662 {
3663     /* 
3664      * OK this is a bit confusing.. I am given a _Component key and I believe
3665      * that the file that is being registered as a type library is the "key file
3666      * of that component" which I interpret to mean "The file in the KeyPath of
3667      * that component".
3668      */
3669     UINT rc;
3670     MSIQUERY * view;
3671     MSIRECORD * row = 0;
3672     static const WCHAR Query[] = {
3673         'S','E','L','E','C','T',' ','*',' ',
3674         'f','r','o','m',' ','T','y','p','e','L','i','b',0};
3675     ITypeLib *ptLib;
3676     HRESULT res;
3677
3678     if (!package)
3679         return ERROR_INVALID_HANDLE;
3680
3681     rc = MSI_DatabaseOpenViewW(package->db, Query, &view);
3682     if (rc != ERROR_SUCCESS)
3683         return ERROR_SUCCESS;
3684
3685     rc = MSI_ViewExecute(view, 0);
3686     if (rc != ERROR_SUCCESS)
3687     {
3688         MSI_ViewClose(view);
3689         msiobj_release(&view->hdr);
3690         return rc;
3691     }
3692
3693     while (1)
3694     {   
3695         WCHAR component[0x100];
3696         DWORD sz;
3697         INT index;
3698
3699         rc = MSI_ViewFetch(view,&row);
3700         if (rc != ERROR_SUCCESS)
3701         {
3702             rc = ERROR_SUCCESS;
3703             break;
3704         }
3705
3706         sz = 0x100;
3707         MSI_RecordGetStringW(row,3,component,&sz);
3708
3709         index = get_loaded_component(package,component);
3710         if (index < 0)
3711         {
3712             msiobj_release(&row->hdr);
3713             continue;
3714         }
3715
3716         if (package->components[index].ActionRequest != INSTALLSTATE_LOCAL)
3717         {
3718             TRACE("Skipping typelib reg due to disabled component\n");
3719             msiobj_release(&row->hdr);
3720
3721             package->components[index].Action =
3722                 package->components[index].Installed;
3723
3724             continue;
3725         }
3726
3727         package->components[index].Action = INSTALLSTATE_LOCAL;
3728         package->components[index].Installed = INSTALLSTATE_LOCAL;
3729
3730         index = get_loaded_file(package,package->components[index].KeyPath); 
3731    
3732         if (index < 0)
3733         {
3734             msiobj_release(&row->hdr);
3735             continue;
3736         }
3737
3738         res = LoadTypeLib(package->files[index].TargetPath,&ptLib);
3739         if (SUCCEEDED(res))
3740         {
3741             LPWSTR help;
3742             WCHAR helpid[0x100];
3743
3744             sz = 0x100;
3745             MSI_RecordGetStringW(row,6,helpid,&sz);
3746
3747             help = resolve_folder(package,helpid,FALSE,FALSE,NULL);
3748             res = RegisterTypeLib(ptLib,package->files[index].TargetPath,help);
3749             HeapFree(GetProcessHeap(),0,help);
3750
3751             if (!SUCCEEDED(res))
3752                 ERR("Failed to register type library %s\n",
3753                      debugstr_w(package->files[index].TargetPath));
3754             else
3755             {
3756                 /* Yes the row has more fields than I need, but #1 is 
3757                    correct and the only one I need. Why make a new row? */
3758
3759                 ui_actiondata(package,szRegisterTypeLibraries,row);
3760                 
3761                 TRACE("Registered %s\n",
3762                        debugstr_w(package->files[index].TargetPath));
3763             }
3764
3765             if (ptLib)
3766                 ITypeLib_Release(ptLib);
3767         }
3768         else
3769             ERR("Failed to load type library %s\n",
3770                 debugstr_w(package->files[index].TargetPath));
3771         
3772         msiobj_release(&row->hdr);
3773     }
3774     MSI_ViewClose(view);
3775     msiobj_release(&view->hdr);
3776     return rc;
3777    
3778 }
3779
3780 static UINT register_appid(MSIPACKAGE *package, LPCWSTR clsid, LPCWSTR app )
3781 {
3782     static const WCHAR szAppID[] = { 'A','p','p','I','D',0 };
3783     UINT rc;
3784     MSIQUERY * view;
3785     MSIRECORD * row = 0;
3786     static const WCHAR ExecSeqQuery[] = 
3787         {'S','E','L','E','C','T',' ','*',' ','f','r','o','m',' ','A','p','p','I'
3788         ,'d',' ','w','h','e','r','e',' ','A','p','p','I','d','=','`','%','s','`',0};
3789     HKEY hkey2,hkey3;
3790     LPWSTR buffer=0;
3791
3792     if (!package)
3793         return ERROR_INVALID_HANDLE;
3794
3795     rc = MSI_OpenQuery(package->db, &view, ExecSeqQuery, clsid);
3796     if (rc != ERROR_SUCCESS)
3797         return rc;
3798
3799     rc = MSI_ViewExecute(view, 0);
3800     if (rc != ERROR_SUCCESS)
3801     {
3802         MSI_ViewClose(view);
3803         msiobj_release(&view->hdr);
3804         return rc;
3805     }
3806
3807     RegCreateKeyW(HKEY_CLASSES_ROOT,szAppID,&hkey2);
3808     RegCreateKeyW(hkey2,clsid,&hkey3);
3809     RegSetValueExW(hkey3,NULL,0,REG_SZ,(LPVOID)app,
3810                    (strlenW(app)+1)*sizeof(WCHAR));
3811
3812     rc = MSI_ViewFetch(view,&row);
3813     if (rc != ERROR_SUCCESS)
3814     {
3815         MSI_ViewClose(view);
3816         msiobj_release(&view->hdr);
3817         return rc;
3818     }
3819
3820     if (!MSI_RecordIsNull(row,2)) 
3821     {
3822         LPWSTR deformated=0;
3823         UINT size; 
3824         static const WCHAR szRemoteServerName[] =
3825              {'R','e','m','o','t','e','S','e','r','v','e','r','N','a','m','e',0};
3826         buffer = load_dynamic_stringW(row,2);
3827         size = deformat_string(package,buffer,&deformated);
3828         RegSetValueExW(hkey3,szRemoteServerName,0,REG_SZ,(LPVOID)deformated,
3829                        size);
3830         HeapFree(GetProcessHeap(),0,deformated);
3831         HeapFree(GetProcessHeap(),0,buffer);
3832     }
3833
3834     if (!MSI_RecordIsNull(row,3)) 
3835     {
3836         static const WCHAR szLocalService[] =
3837              {'L','o','c','a','l','S','e','r','v','i','c','e',0};
3838         UINT size;
3839         buffer = load_dynamic_stringW(row,3);
3840         size = (strlenW(buffer)+1) * sizeof(WCHAR);
3841         RegSetValueExW(hkey3,szLocalService,0,REG_SZ,(LPVOID)buffer,size);
3842         HeapFree(GetProcessHeap(),0,buffer);
3843     }
3844
3845     if (!MSI_RecordIsNull(row,4)) 
3846     {
3847         static const WCHAR szService[] =
3848              {'S','e','r','v','i','c','e','P','a','r','a','m','e','t','e','r','s',0};
3849         UINT size;
3850         buffer = load_dynamic_stringW(row,4);
3851         size = (strlenW(buffer)+1) * sizeof(WCHAR);
3852         RegSetValueExW(hkey3,szService,0,REG_SZ,(LPVOID)buffer,size);
3853         HeapFree(GetProcessHeap(),0,buffer);
3854     }
3855
3856     if (!MSI_RecordIsNull(row,5)) 
3857     {
3858         static const WCHAR szDLL[] =
3859              {'D','l','l','S','u','r','r','o','g','a','t','e',0};
3860         UINT size;
3861         buffer = load_dynamic_stringW(row,5);
3862         size = (strlenW(buffer)+1) * sizeof(WCHAR);
3863         RegSetValueExW(hkey3,szDLL,0,REG_SZ,(LPVOID)buffer,size);
3864         HeapFree(GetProcessHeap(),0,buffer);
3865     }
3866
3867     if (!MSI_RecordIsNull(row,6)) 
3868     {
3869         static const WCHAR szActivate[] =
3870              {'A','c','t','i','v','a','t','e','A','s','S','t','o','r','a','g','e',0};
3871         static const WCHAR szY[] = {'Y',0};
3872
3873         if (MSI_RecordGetInteger(row,6))
3874             RegSetValueExW(hkey3,szActivate,0,REG_SZ,(LPVOID)szY,4);
3875     }
3876
3877     if (!MSI_RecordIsNull(row,7)) 
3878     {
3879         static const WCHAR szRunAs[] = {'R','u','n','A','s',0};
3880         static const WCHAR szUser[] = 
3881              {'I','n','t','e','r','a','c','t','i','v','e',' ','U','s','e','r',0};
3882
3883         if (MSI_RecordGetInteger(row,7))
3884             RegSetValueExW(hkey3,szRunAs,0,REG_SZ,(LPVOID)szUser,34);
3885     }
3886
3887     msiobj_release(&row->hdr);
3888     MSI_ViewClose(view);
3889     msiobj_release(&view->hdr);
3890     RegCloseKey(hkey3);
3891     RegCloseKey(hkey2);
3892     return rc;
3893 }
3894
3895 static UINT ACTION_RegisterClassInfo(MSIPACKAGE *package)
3896 {
3897     /* 
3898      * Again I am assuming the words, "Whose key file represents" when referring
3899      * to a Component as to meaning that Components KeyPath file
3900      *
3901      * Also there is a very strong connection between ClassInfo and ProgID
3902      * that I am mostly glossing over.  
3903      * What would be more propper is to load the ClassInfo and the ProgID info
3904      * into memory data structures and then be able to enable and disable them
3905      * based on component. 
3906      */
3907     
3908     UINT rc;
3909     MSIQUERY * view;
3910     MSIRECORD * row = 0;
3911     static const WCHAR ExecSeqQuery[] = {
3912         'S','E','L','E','C','T',' ','*',' ',
3913         'f','r','o','m',' ','C','l','a','s','s',0};
3914     static const WCHAR szCLSID[] = { 'C','L','S','I','D',0 };
3915     static const WCHAR szProgID[] = { 'P','r','o','g','I','D',0 };
3916     static const WCHAR szAppID[] = { 'A','p','p','I','D',0 };
3917     static const WCHAR szSpace[] = {' ',0};
3918     HKEY hkey,hkey2,hkey3;
3919     LPWSTR argument,deformated;
3920
3921     if (!package)
3922         return ERROR_INVALID_HANDLE;
3923
3924     rc = RegCreateKeyW(HKEY_CLASSES_ROOT,szCLSID,&hkey);
3925     if (rc != ERROR_SUCCESS)
3926         return ERROR_FUNCTION_FAILED;
3927
3928     rc = MSI_DatabaseOpenViewW(package->db, ExecSeqQuery, &view);
3929     if (rc != ERROR_SUCCESS)
3930     {
3931         rc = ERROR_SUCCESS;
3932         goto end;
3933     }
3934
3935     rc = MSI_ViewExecute(view, 0);
3936     if (rc != ERROR_SUCCESS)
3937     {
3938         MSI_ViewClose(view);
3939         msiobj_release(&view->hdr);
3940         goto end;
3941     }
3942
3943     while (1)
3944     {
3945         WCHAR clsid[0x100];
3946         WCHAR buffer[0x100];
3947         WCHAR desc[0x100];
3948         DWORD sz;
3949         INT index;
3950         DWORD size;
3951      
3952         rc = MSI_ViewFetch(view,&row);
3953         if (rc != ERROR_SUCCESS)
3954         {
3955             rc = ERROR_SUCCESS;
3956             break;
3957         }
3958
3959         sz=0x100;
3960         MSI_RecordGetStringW(row,3,buffer,&sz);
3961
3962         index = get_loaded_component(package,buffer);
3963
3964         if (index < 0)
3965         {
3966             msiobj_release(&row->hdr);
3967             continue;
3968         }
3969
3970         if (package->components[index].ActionRequest != INSTALLSTATE_LOCAL)
3971         {
3972             TRACE("Skipping class reg due to disabled component\n");
3973             msiobj_release(&row->hdr);
3974
3975             package->components[index].Action =
3976                 package->components[index].Installed;
3977
3978             continue;
3979         }
3980
3981         package->components[index].Action = INSTALLSTATE_LOCAL;
3982         package->components[index].Installed = INSTALLSTATE_LOCAL;
3983
3984         sz=0x100;
3985         MSI_RecordGetStringW(row,1,clsid,&sz);
3986         RegCreateKeyW(hkey,clsid,&hkey2);
3987
3988         if (!MSI_RecordIsNull(row,5))
3989         {
3990             sz=0x100;
3991             MSI_RecordGetStringW(row,5,desc,&sz);
3992
3993             RegSetValueExW(hkey2,NULL,0,REG_SZ,(LPVOID)desc,
3994                            (strlenW(desc)+1)*sizeof(WCHAR));
3995         }
3996         else
3997             desc[0]=0;
3998
3999         sz=0x100;
4000         MSI_RecordGetStringW(row,2,buffer,&sz);
4001
4002         RegCreateKeyW(hkey2,buffer,&hkey3);
4003
4004         index = get_loaded_file(package,package->components[index].KeyPath);
4005
4006         argument = load_dynamic_stringW(row,11); 
4007         size = deformat_string(package,argument,&deformated);
4008         if (deformated)
4009             size+=sizeof(WCHAR);
4010         HeapFree(GetProcessHeap(),0,argument);
4011         size += (strlenW(package->files[index].TargetPath))*sizeof(WCHAR);
4012
4013         argument = (LPWSTR)HeapAlloc(GetProcessHeap(),0,size+sizeof(WCHAR));
4014         strcpyW(argument,package->files[index].TargetPath);
4015         if (deformated)
4016         {
4017             strcatW(argument,szSpace);
4018             strcatW(argument,deformated);
4019         }
4020
4021         RegSetValueExW(hkey3,NULL,0,REG_SZ, (LPVOID)argument, size);
4022         HeapFree(GetProcessHeap(),0,deformated);
4023         HeapFree(GetProcessHeap(),0,argument);
4024
4025         RegCloseKey(hkey3);
4026
4027         if (!MSI_RecordIsNull(row,4))
4028         {
4029             sz=0x100;
4030             MSI_RecordGetStringW(row,4,buffer,&sz);
4031
4032             RegCreateKeyW(hkey2,szProgID,&hkey3);
4033     
4034             RegSetValueExW(hkey3,NULL,0,REG_SZ,(LPVOID)buffer,
4035                        (strlenW(buffer)+1)*sizeof(WCHAR));
4036
4037             RegCloseKey(hkey3);
4038         }
4039
4040         if (!MSI_RecordIsNull(row,6))
4041         { 
4042             sz=0x100;
4043             MSI_RecordGetStringW(row,6,buffer,&sz);
4044
4045             RegSetValueExW(hkey2,szAppID,0,REG_SZ,(LPVOID)buffer,
4046                        (strlenW(buffer)+1)*sizeof(WCHAR));
4047
4048             register_appid(package,buffer,desc);
4049         }
4050
4051
4052         if (!MSI_RecordIsNull(row,7))
4053         {
4054             FIXME("Process field 7\n");
4055         }
4056
4057         if (!MSI_RecordIsNull(row,8))
4058         {
4059             static const WCHAR szDefaultIcon[] = 
4060             {'D','e','f','a','u','l','t','I','c','o','n',0};
4061
4062             LPWSTR FileName = load_dynamic_stringW(row,8);
4063             LPWSTR FilePath;
4064             INT index;
4065
4066             RegCreateKeyW(hkey2,szDefaultIcon,&hkey3);
4067             build_icon_path(package,FileName,&FilePath);
4068             if (!MSI_RecordIsNull(row,9))
4069             {
4070                 static const WCHAR index_fmt[] = {',','%','i',0};
4071                 WCHAR index_buf[20];
4072                 index = MSI_RecordGetInteger(row,9);
4073                 sprintfW(index_buf,index_fmt,index);
4074                 size = strlenW(FilePath)+strlenW(index_buf)+1;
4075                 size *= sizeof(WCHAR);
4076                 HeapReAlloc(GetProcessHeap(),0,FilePath,size);
4077             }
4078             RegSetValueExW(hkey3,NULL,0,REG_SZ,(LPVOID)FilePath,
4079                            (strlenW(FilePath)+1) * sizeof(WCHAR));
4080             HeapFree(GetProcessHeap(),0,FilePath);
4081             HeapFree(GetProcessHeap(),0,FileName);
4082             RegCloseKey(hkey3);
4083         }
4084
4085         if (!MSI_RecordIsNull(row,10))
4086         {
4087             static const WCHAR szInproc32[] = {
4088             'I','n','p','r','o','c','H','a','n','d','l','e','r','3','2',0};
4089             static const WCHAR szInproc[] = {
4090             'I','n','p','r','o','c','H','a','n','d','l','e','r',0};
4091             INT i = MSI_RecordGetInteger(row,10);
4092             if (i != MSI_NULL_INTEGER && i > 0 &&  i < 4)
4093             {
4094                 static const WCHAR ole2[] = {'o','l','e','2','.','d','l','l',0};
4095                 static const WCHAR ole32[] = {
4096                         'o','l','e','3','2','.','d','l','l',0};
4097                 switch(i)
4098                 {
4099                     case 1:
4100                         size = strlenW(ole2) * sizeof(WCHAR);
4101                         RegCreateKeyW(hkey2,szInproc,&hkey3);
4102                         RegSetValueExW(hkey3,NULL,0,REG_SZ, (LPVOID)ole2, size);
4103                         RegCloseKey(hkey3);
4104                         break;
4105                     case 2:
4106                         size = strlenW(ole32) * sizeof(WCHAR);
4107                         RegCreateKeyW(hkey2,szInproc32,&hkey3);
4108                         RegSetValueExW(hkey3,NULL,0,REG_SZ, (LPVOID)ole32,size);
4109                         RegCloseKey(hkey3);
4110                         break;
4111                     case 3:
4112                         size = strlenW(ole2) * sizeof(WCHAR);
4113                         RegCreateKeyW(hkey2,szInproc,&hkey3);
4114                         RegSetValueExW(hkey3,NULL,0,REG_SZ, (LPVOID)ole2, size);
4115                         RegCloseKey(hkey3);
4116                         size = strlenW(ole32) * sizeof(WCHAR);
4117                         RegCreateKeyW(hkey2,szInproc32,&hkey3);
4118                         RegSetValueExW(hkey3,NULL,0,REG_SZ, (LPVOID)ole32,size);
4119                         RegCloseKey(hkey3);
4120                         break;
4121                 }
4122                 
4123             }
4124             else
4125             {
4126                 RegCreateKeyW(hkey2,szInproc32,&hkey3);
4127                 argument = load_dynamic_stringW(row,10);
4128                 reduce_to_longfilename(argument);
4129                 size = strlenW(argument)*sizeof(WCHAR);
4130
4131                 RegSetValueExW(hkey3,NULL,0,REG_SZ, (LPVOID)argument, size);
4132                 HeapFree(GetProcessHeap(),0,argument);
4133
4134                 RegCloseKey(hkey3);
4135             }
4136         }
4137
4138         RegCloseKey(hkey2);
4139
4140         ui_actiondata(package,szRegisterClassInfo,row);
4141
4142         msiobj_release(&row->hdr);
4143     }
4144     MSI_ViewClose(view);
4145     msiobj_release(&view->hdr);
4146
4147 end:
4148     RegCloseKey(hkey);
4149     return rc;
4150 }
4151
4152 static UINT register_progid_base(MSIPACKAGE* package, MSIRECORD * row, 
4153                                 LPWSTR clsid)
4154 {
4155     static const WCHAR szCLSID[] = { 'C','L','S','I','D',0 };
4156     static const WCHAR szDefaultIcon[] = {
4157         'D','e','f','a','u','l','t','I','c','o','n',0};
4158     HKEY hkey,hkey2;
4159     WCHAR buffer[0x100];
4160     DWORD sz;
4161
4162
4163     sz = 0x100;
4164     MSI_RecordGetStringW(row,1,buffer,&sz);
4165     RegCreateKeyW(HKEY_CLASSES_ROOT,buffer,&hkey);
4166
4167     if (!MSI_RecordIsNull(row,4))
4168     {
4169         sz = 0x100;
4170         MSI_RecordGetStringW(row,4,buffer,&sz);
4171         RegSetValueExW(hkey,NULL,0,REG_SZ,(LPVOID)buffer, (strlenW(buffer)+1) *
4172                        sizeof(WCHAR));
4173     }
4174
4175     if (!MSI_RecordIsNull(row,3))
4176     {   
4177         sz = 0x100;
4178     
4179         MSI_RecordGetStringW(row,3,buffer,&sz);
4180         RegCreateKeyW(hkey,szCLSID,&hkey2);
4181         RegSetValueExW(hkey2,NULL,0,REG_SZ,(LPVOID)buffer, (strlenW(buffer)+1) *
4182                        sizeof(WCHAR));
4183
4184         if (clsid)
4185             strcpyW(clsid,buffer);
4186
4187         RegCloseKey(hkey2);
4188     }
4189     else
4190     {
4191         FIXME("UNHANDLED case, Parent progid but classid is NULL\n");
4192         return ERROR_FUNCTION_FAILED;
4193     }
4194     if (!MSI_RecordIsNull(row,5))
4195     {
4196         INT index = MSI_RecordGetInteger(row,6); 
4197         LPWSTR FileName = load_dynamic_stringW(row,5);
4198         LPWSTR FilePath,IconPath;
4199         static const WCHAR fmt[] = {'%','s',',','%','i',0};
4200
4201         RegCreateKeyW(hkey,szDefaultIcon,&hkey2);
4202         build_icon_path(package,FileName,&FilePath);
4203        
4204         IconPath = HeapAlloc(GetProcessHeap(),0,(strlenW(FilePath)+5)*
4205                     sizeof(WCHAR));
4206
4207         sprintfW(IconPath,fmt,FilePath,index);
4208         RegSetValueExW(hkey2,NULL,0,REG_SZ,(LPVOID)IconPath,
4209                            (strlenW(IconPath)+1) * sizeof(WCHAR));
4210         HeapFree(GetProcessHeap(),0,FilePath);
4211         HeapFree(GetProcessHeap(),0,FileName);
4212         RegCloseKey(hkey2);
4213     }
4214     return ERROR_SUCCESS;
4215 }
4216
4217 static UINT register_progid(MSIPACKAGE *package, MSIRECORD * row, LPWSTR clsid);
4218
4219 static UINT register_parent_progid(MSIPACKAGE *package, LPCWSTR parent, 
4220                                    LPWSTR clsid)
4221 {
4222     UINT rc;
4223     MSIQUERY * view;
4224     MSIRECORD * row = 0;
4225     static const WCHAR Query_t[] = 
4226         {'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ','P','r','o','g'
4227         ,'I','d',' ','w','h','e','r','e',' ','P','r','o','g','I','d',' ','=',' ','`'
4228         ,'%','s','`',0};
4229
4230     if (!package)
4231         return ERROR_INVALID_HANDLE;
4232
4233     rc = MSI_OpenQuery(package->db, &view, Query_t, parent);
4234     if (rc != ERROR_SUCCESS)
4235         return rc;
4236
4237     rc = MSI_ViewExecute(view, 0);
4238     if (rc != ERROR_SUCCESS)
4239     {
4240         MSI_ViewClose(view);
4241         msiobj_release(&view->hdr);
4242         return rc;
4243     }
4244
4245     rc = MSI_ViewFetch(view,&row);
4246     if (rc != ERROR_SUCCESS)
4247     {
4248         MSI_ViewClose(view);
4249         msiobj_release(&view->hdr);
4250         return rc;
4251     }
4252
4253     register_progid(package,row,clsid);
4254
4255     msiobj_release(&row->hdr);
4256     MSI_ViewClose(view);
4257     msiobj_release(&view->hdr);
4258     return rc;
4259 }
4260
4261 static UINT register_progid(MSIPACKAGE *package, MSIRECORD * row, LPWSTR clsid)
4262 {
4263     UINT rc = ERROR_SUCCESS; 
4264
4265     if (MSI_RecordIsNull(row,2))
4266         rc = register_progid_base(package,row,clsid);
4267     else
4268     {
4269         WCHAR buffer[0x1000];
4270         DWORD sz, disp;
4271         HKEY hkey,hkey2;
4272         static const WCHAR szCLSID[] = { 'C','L','S','I','D',0 };
4273         static const WCHAR szDefaultIcon[] = {
4274                 'D','e','f','a','u','l','t','I','c','o','n',0};
4275
4276         /* check if already registered */
4277         sz = 0x100;
4278         MSI_RecordGetStringW(row,1,buffer,&sz);
4279         RegCreateKeyExW(HKEY_CLASSES_ROOT, buffer, 0, NULL, 0,
4280                         KEY_ALL_ACCESS, NULL, &hkey, &disp );
4281         if (disp == REG_OPENED_EXISTING_KEY)
4282         {
4283             TRACE("Key already registered\n");
4284             RegCloseKey(hkey);
4285             return rc;
4286         }
4287
4288         sz = 0x100;
4289         MSI_RecordGetStringW(row,2,buffer,&sz);
4290         rc = register_parent_progid(package,buffer,clsid);
4291
4292         /* clsid is same as parent */
4293         RegCreateKeyW(hkey,szCLSID,&hkey2);
4294         RegSetValueExW(hkey2,NULL,0,REG_SZ,(LPVOID)clsid, (strlenW(clsid)+1) *
4295                        sizeof(WCHAR));
4296
4297         RegCloseKey(hkey2);
4298
4299
4300         if (!MSI_RecordIsNull(row,4))
4301         {
4302             sz = 0x100;
4303             MSI_RecordGetStringW(row,4,buffer,&sz);
4304             RegSetValueExW(hkey,NULL,0,REG_SZ,(LPVOID)buffer,
4305                            (strlenW(buffer)+1) * sizeof(WCHAR));
4306         }
4307
4308         if (!MSI_RecordIsNull(row,5))
4309         {
4310             LPWSTR FileName = load_dynamic_stringW(row,5);
4311             LPWSTR FilePath;
4312             RegCreateKeyW(hkey,szDefaultIcon,&hkey2);
4313             build_icon_path(package,FileName,&FilePath);
4314             RegSetValueExW(hkey2,NULL,0,REG_SZ,(LPVOID)FilePath,
4315                            (strlenW(FilePath)+1) * sizeof(WCHAR));
4316             HeapFree(GetProcessHeap(),0,FilePath);
4317             HeapFree(GetProcessHeap(),0,FileName);
4318             RegCloseKey(hkey2);
4319         }
4320
4321         RegCloseKey(hkey);
4322     }
4323     return rc;
4324 }
4325
4326 static UINT ACTION_RegisterProgIdInfo(MSIPACKAGE *package)
4327 {
4328     /* 
4329      * Sigh, here I am just brute force registering all progids
4330      * this needs to be linked to the Classes that have been registered
4331      * but the easiest way to do that is to load all these stuff into
4332      * memory for easy checking.
4333      *
4334      * Gives me something to continue to work toward.
4335      */
4336     UINT rc;
4337     MSIQUERY * view;
4338     MSIRECORD * row = 0;
4339     static const WCHAR Query[] = {
4340         'S','E','L','E','C','T',' ','*',' ',
4341         'F','R','O','M',' ','P','r','o','g','I','d',0};
4342
4343     if (!package)
4344         return ERROR_INVALID_HANDLE;
4345
4346     rc = MSI_DatabaseOpenViewW(package->db, Query, &view);
4347     if (rc != ERROR_SUCCESS)
4348         return ERROR_SUCCESS;
4349
4350     rc = MSI_ViewExecute(view, 0);
4351     if (rc != ERROR_SUCCESS)
4352     {
4353         MSI_ViewClose(view);
4354         msiobj_release(&view->hdr);
4355         return rc;
4356     }
4357
4358     while (1)
4359     {
4360         WCHAR clsid[0x1000];
4361
4362         rc = MSI_ViewFetch(view,&row);
4363         if (rc != ERROR_SUCCESS)
4364         {
4365             rc = ERROR_SUCCESS;
4366             break;
4367         }
4368         
4369         register_progid(package,row,clsid);
4370         ui_actiondata(package,szRegisterProgIdInfo,row);
4371
4372         msiobj_release(&row->hdr);
4373     }
4374     MSI_ViewClose(view);
4375     msiobj_release(&view->hdr);
4376     return rc;
4377 }
4378
4379 static UINT build_icon_path(MSIPACKAGE *package, LPCWSTR icon_name, 
4380                             LPWSTR *FilePath)
4381 {
4382     LPWSTR ProductCode;
4383     LPWSTR SystemFolder;
4384     LPWSTR dest;
4385     UINT rc;
4386
4387     static const WCHAR szInstaller[] = 
4388         {'I','n','s','t','a','l','l','e','r','\\',0};
4389     static const WCHAR szProductCode[] =
4390         {'P','r','o','d','u','c','t','C','o','d','e',0};
4391     static const WCHAR szFolder[] =
4392         {'W','i','n','d','o','w','s','F','o','l','d','e','r',0};
4393
4394     ProductCode = load_dynamic_property(package,szProductCode,&rc);
4395     if (!ProductCode)
4396         return rc;
4397
4398     SystemFolder = load_dynamic_property(package,szFolder,NULL);
4399
4400     dest = build_directory_name(3, SystemFolder, szInstaller, ProductCode);
4401
4402     create_full_pathW(dest);
4403
4404     *FilePath = build_directory_name(2, dest, icon_name);
4405
4406     HeapFree(GetProcessHeap(),0,SystemFolder);
4407     HeapFree(GetProcessHeap(),0,ProductCode);
4408     HeapFree(GetProcessHeap(),0,dest);
4409     return ERROR_SUCCESS;
4410 }
4411
4412 static UINT ACTION_CreateShortcuts(MSIPACKAGE *package)
4413 {
4414     UINT rc;
4415     MSIQUERY * view;
4416     MSIRECORD * row = 0;
4417     static const WCHAR Query[] = {
4418        'S','E','L','E','C','T',' ','*',' ','f','r','o','m',' ',
4419        'S','h','o','r','t','c','u','t',0};
4420     IShellLinkW *sl;
4421     IPersistFile *pf;
4422     HRESULT res;
4423
4424     if (!package)
4425         return ERROR_INVALID_HANDLE;
4426
4427     res = CoInitialize( NULL );
4428     if (FAILED (res))
4429     {
4430         ERR("CoInitialize failed\n");
4431         return ERROR_FUNCTION_FAILED;
4432     }
4433
4434     rc = MSI_DatabaseOpenViewW(package->db, Query, &view);
4435     if (rc != ERROR_SUCCESS)
4436         return ERROR_SUCCESS;
4437
4438     rc = MSI_ViewExecute(view, 0);
4439     if (rc != ERROR_SUCCESS)
4440     {
4441         MSI_ViewClose(view);
4442         msiobj_release(&view->hdr);
4443         return rc;
4444     }
4445
4446     while (1)
4447     {
4448         LPWSTR target_file, target_folder;
4449         WCHAR buffer[0x100];
4450         DWORD sz;
4451         DWORD index;
4452         static const WCHAR szlnk[]={'.','l','n','k',0};
4453
4454         rc = MSI_ViewFetch(view,&row);
4455         if (rc != ERROR_SUCCESS)
4456         {
4457             rc = ERROR_SUCCESS;
4458             break;
4459         }
4460         
4461         sz = 0x100;
4462         MSI_RecordGetStringW(row,4,buffer,&sz);
4463
4464         index = get_loaded_component(package,buffer);
4465
4466         if (index < 0)
4467         {
4468             msiobj_release(&row->hdr);
4469             continue;
4470         }
4471
4472         if (package->components[index].ActionRequest != INSTALLSTATE_LOCAL)
4473         {
4474             TRACE("Skipping shortcut creation due to disabled component\n");
4475             msiobj_release(&row->hdr);
4476
4477             package->components[index].Action =
4478                 package->components[index].Installed;
4479
4480             continue;
4481         }
4482
4483         package->components[index].Action = INSTALLSTATE_LOCAL;
4484         package->components[index].Installed = INSTALLSTATE_LOCAL;
4485
4486         ui_actiondata(package,szCreateShortcuts,row);
4487
4488         res = CoCreateInstance( &CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER,
4489                               &IID_IShellLinkW, (LPVOID *) &sl );
4490
4491         if (FAILED(res))
4492         {
4493             ERR("Is IID_IShellLink\n");
4494             msiobj_release(&row->hdr);
4495             continue;
4496         }
4497
4498         res = IShellLinkW_QueryInterface( sl, &IID_IPersistFile,(LPVOID*) &pf );
4499         if( FAILED( res ) )
4500         {
4501             ERR("Is IID_IPersistFile\n");
4502             msiobj_release(&row->hdr);
4503             continue;
4504         }
4505
4506         sz = 0x100;
4507         MSI_RecordGetStringW(row,2,buffer,&sz);
4508         target_folder = resolve_folder(package, buffer,FALSE,FALSE,NULL);
4509
4510         /* may be needed because of a bug somehwere else */
4511         create_full_pathW(target_folder);
4512
4513         sz = 0x100;
4514         MSI_RecordGetStringW(row,3,buffer,&sz);
4515         reduce_to_longfilename(buffer);
4516         if (!strchrW(buffer,'.') || strcmpiW(strchrW(buffer,'.'),szlnk))
4517             strcatW(buffer,szlnk);
4518         target_file = build_directory_name(2, target_folder, buffer);
4519         HeapFree(GetProcessHeap(),0,target_folder);
4520
4521         sz = 0x100;
4522         MSI_RecordGetStringW(row,5,buffer,&sz);
4523         if (strchrW(buffer,'['))
4524         {
4525             LPWSTR deformated;
4526             deformat_string(package,buffer,&deformated);
4527             IShellLinkW_SetPath(sl,deformated);
4528             HeapFree(GetProcessHeap(),0,deformated);
4529         }
4530         else
4531         {
4532             FIXME("UNHANDLED shortcut format, advertised shortcut\n");
4533             IPersistFile_Release( pf );
4534             IShellLinkW_Release( sl );
4535             msiobj_release(&row->hdr);
4536             continue;
4537         }
4538
4539         if (!MSI_RecordIsNull(row,6))
4540         {
4541             LPWSTR deformated;
4542             sz = 0x100;
4543             MSI_RecordGetStringW(row,6,buffer,&sz);
4544             deformat_string(package,buffer,&deformated);
4545             IShellLinkW_SetArguments(sl,deformated);
4546             HeapFree(GetProcessHeap(),0,deformated);
4547         }
4548
4549         if (!MSI_RecordIsNull(row,7))
4550         {
4551             LPWSTR deformated;
4552             deformated = load_dynamic_stringW(row,7);
4553             IShellLinkW_SetDescription(sl,deformated);
4554             HeapFree(GetProcessHeap(),0,deformated);
4555         }
4556
4557         if (!MSI_RecordIsNull(row,8))
4558             IShellLinkW_SetHotkey(sl,MSI_RecordGetInteger(row,8));
4559
4560         if (!MSI_RecordIsNull(row,9))
4561         {
4562             WCHAR *Path = NULL;
4563             INT index; 
4564
4565             sz = 0x100;
4566             MSI_RecordGetStringW(row,9,buffer,&sz);
4567
4568             build_icon_path(package,buffer,&Path);
4569             index = MSI_RecordGetInteger(row,10);
4570
4571             IShellLinkW_SetIconLocation(sl,Path,index);
4572             HeapFree(GetProcessHeap(),0,Path);
4573         }
4574
4575         if (!MSI_RecordIsNull(row,11))
4576             IShellLinkW_SetShowCmd(sl,MSI_RecordGetInteger(row,11));
4577
4578         if (!MSI_RecordIsNull(row,12))
4579         {
4580             LPWSTR Path;
4581             sz = 0x100;
4582             MSI_RecordGetStringW(row,12,buffer,&sz);
4583             Path = resolve_folder(package, buffer, FALSE, FALSE, NULL);
4584             IShellLinkW_SetWorkingDirectory(sl,Path);
4585             HeapFree(GetProcessHeap(), 0, Path);
4586         }
4587
4588         TRACE("Writing shortcut to %s\n",debugstr_w(target_file));
4589         IPersistFile_Save(pf,target_file,FALSE);
4590     
4591         HeapFree(GetProcessHeap(),0,target_file);    
4592
4593         IPersistFile_Release( pf );
4594         IShellLinkW_Release( sl );
4595
4596         msiobj_release(&row->hdr);
4597     }
4598     MSI_ViewClose(view);
4599     msiobj_release(&view->hdr);
4600
4601
4602     CoUninitialize();
4603
4604     return rc;
4605 }
4606
4607
4608 /*
4609  * 99% of the work done here is only done for 
4610  * advertised installs. However this is where the
4611  * Icon table is processed and written out
4612  * so that is what I am going to do here.
4613  */
4614 static UINT ACTION_PublishProduct(MSIPACKAGE *package)
4615 {
4616     UINT rc;
4617     MSIQUERY * view;
4618     MSIRECORD * row = 0;
4619     static const WCHAR Query[]={
4620         'S','E','L','E','C','T',' ','*',' ',
4621         'f','r','o','m',' ','I','c','o','n',0};
4622     DWORD sz;
4623     /* for registry stuff */
4624     LPWSTR productcode;
4625     HKEY hkey=0;
4626     HKEY hukey=0;
4627     static const WCHAR szProductCode[]=
4628          {'P','r','o','d','u','c','t','C','o','d','e',0};
4629     static const WCHAR szProductName[] = {
4630          'P','r','o','d','u','c','t','N','a','m','e',0};
4631     static const WCHAR szPackageCode[] = {
4632          'P','a','c','k','a','g','e','C','o','d','e',0};
4633     LPWSTR buffer;
4634     DWORD size;
4635     MSIHANDLE hDb, hSumInfo;
4636
4637     if (!package)
4638         return ERROR_INVALID_HANDLE;
4639
4640     rc = MSI_DatabaseOpenViewW(package->db, Query, &view);
4641     if (rc != ERROR_SUCCESS)
4642         goto next;
4643
4644     rc = MSI_ViewExecute(view, 0);
4645     if (rc != ERROR_SUCCESS)
4646     {
4647         MSI_ViewClose(view);
4648         msiobj_release(&view->hdr);
4649         goto next;
4650     }
4651
4652     while (1)
4653     {
4654         HANDLE the_file;
4655         WCHAR *FilePath=NULL;
4656         WCHAR *FileName=NULL;
4657         CHAR buffer[1024];
4658
4659         rc = MSI_ViewFetch(view,&row);
4660         if (rc != ERROR_SUCCESS)
4661         {
4662             rc = ERROR_SUCCESS;
4663             break;
4664         }
4665     
4666         FileName = load_dynamic_stringW(row,1);
4667         if (!FileName)
4668         {
4669             ERR("Unable to get FileName\n");
4670             msiobj_release(&row->hdr);
4671             continue;
4672         }
4673
4674         build_icon_path(package,FileName,&FilePath);
4675
4676         HeapFree(GetProcessHeap(),0,FileName);
4677
4678         TRACE("Creating icon file at %s\n",debugstr_w(FilePath));
4679         
4680         the_file = CreateFileW(FilePath, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS,
4681                            FILE_ATTRIBUTE_NORMAL, NULL);
4682
4683         if (the_file == INVALID_HANDLE_VALUE)
4684         {
4685             ERR("Unable to create file %s\n",debugstr_w(FilePath));
4686             msiobj_release(&row->hdr);
4687             HeapFree(GetProcessHeap(),0,FilePath);
4688             continue;
4689         }
4690
4691         do 
4692         {
4693             DWORD write;
4694             sz = 1024;
4695             rc = MSI_RecordReadStream(row,2,buffer,&sz);
4696             if (rc != ERROR_SUCCESS)
4697             {
4698                 ERR("Failed to get stream\n");
4699                 CloseHandle(the_file);  
4700                 DeleteFileW(FilePath);
4701                 break;
4702             }
4703             WriteFile(the_file,buffer,sz,&write,NULL);
4704         } while (sz == 1024);
4705
4706         HeapFree(GetProcessHeap(),0,FilePath);
4707
4708         CloseHandle(the_file);
4709         msiobj_release(&row->hdr);
4710     }
4711     MSI_ViewClose(view);
4712     msiobj_release(&view->hdr);
4713
4714 next:
4715     /* ok there is a lot more done here but i need to figure out what */
4716     productcode = load_dynamic_property(package,szProductCode,&rc);
4717     if (!productcode)
4718         return rc;
4719
4720     rc = MSIREG_OpenProductsKey(productcode,&hkey,TRUE);
4721     if (rc != ERROR_SUCCESS)
4722         goto end;
4723
4724     rc = MSIREG_OpenUserProductsKey(productcode,&hukey,TRUE);
4725     if (rc != ERROR_SUCCESS)
4726         goto end;
4727
4728
4729     buffer = load_dynamic_property(package,szProductName,NULL);
4730     size = strlenW(buffer)*sizeof(WCHAR);
4731     RegSetValueExW(hukey,szProductName,0,REG_SZ, (LPSTR)buffer,size);
4732     HeapFree(GetProcessHeap(),0,buffer);
4733     FIXME("Need to write more keys to the user registry\n");
4734   
4735     hDb= msiobj_findhandle( &package->db->hdr );
4736     rc = MsiGetSummaryInformationW(hDb, NULL, 0, &hSumInfo); 
4737     if (rc == ERROR_SUCCESS)
4738     {
4739         WCHAR guidbuffer[0x200];
4740         size = 0x200;
4741         rc = MsiSummaryInfoGetPropertyW(hSumInfo, 8, NULL, NULL, NULL,
4742                                         guidbuffer, &size);
4743         if (rc == ERROR_SUCCESS)
4744         {
4745             WCHAR squashed[GUID_SIZE];
4746             /* for now we only care about the first guid */
4747             LPWSTR ptr = strchrW(guidbuffer,';');
4748             if (ptr) *ptr = 0;
4749             squash_guid(guidbuffer,squashed);
4750             size = strlenW(guidbuffer)*sizeof(WCHAR);
4751             RegSetValueExW(hukey,szPackageCode,0,REG_SZ, (LPSTR)guidbuffer,
4752                            size);
4753             
4754         }
4755         else
4756         {
4757             ERR("Unable to query Revision_Number... \n");
4758             rc = ERROR_SUCCESS;
4759         }
4760         MsiCloseHandle(hSumInfo);
4761     }
4762     else
4763     {
4764         ERR("Unable to open Summary Information\n");
4765         rc = ERROR_SUCCESS;
4766     }
4767
4768 end:
4769
4770     HeapFree(GetProcessHeap(),0,productcode);    
4771     RegCloseKey(hkey);
4772     RegCloseKey(hukey);
4773
4774     return rc;
4775 }
4776
4777 static UINT ACTION_WriteIniValues(MSIPACKAGE *package)
4778 {
4779     UINT rc;
4780     MSIQUERY * view;
4781     MSIRECORD * row = 0;
4782     static const WCHAR ExecSeqQuery[] = {'S','e','l','e','c','t',' ','*',
4783         ' ','f','r','o','m',' ','I','n','i','F','i','l','e',0};
4784     static const WCHAR szWindowsFolder[] =
4785           {'W','i','n','d','o','w','s','F','o','l','d','e','r',0};
4786
4787     rc = MSI_DatabaseOpenViewW(package->db, ExecSeqQuery, &view);
4788     if (rc != ERROR_SUCCESS)
4789     {
4790         TRACE("no IniFile table\n");
4791         return ERROR_SUCCESS;
4792     }
4793
4794     rc = MSI_ViewExecute(view, 0);
4795     if (rc != ERROR_SUCCESS)
4796     {
4797         MSI_ViewClose(view);
4798         msiobj_release(&view->hdr);
4799         return rc;
4800     }
4801
4802     while (1)
4803     {
4804         LPWSTR component,filename,dirproperty,section,key,value,identifier;
4805         LPWSTR deformated_section, deformated_key, deformated_value;
4806         LPWSTR folder, fullname = NULL;
4807         MSIRECORD * uirow;
4808         INT component_index,action;
4809
4810         rc = MSI_ViewFetch(view,&row);
4811         if (rc != ERROR_SUCCESS)
4812         {
4813             rc = ERROR_SUCCESS;
4814             break;
4815         }
4816
4817         component = load_dynamic_stringW(row, 8);
4818         component_index = get_loaded_component(package,component);
4819         HeapFree(GetProcessHeap(),0,component);
4820
4821         if (package->components[component_index].ActionRequest != 
4822              INSTALLSTATE_LOCAL)
4823         {
4824             TRACE("Skipping ini file due to disabled component\n");
4825             msiobj_release(&row->hdr);
4826
4827             package->components[component_index].Action =
4828                 package->components[component_index].Installed;
4829
4830             continue;
4831         }
4832
4833         package->components[component_index].Action = INSTALLSTATE_LOCAL;
4834         package->components[component_index].Installed = INSTALLSTATE_LOCAL;
4835    
4836         identifier = load_dynamic_stringW(row,1); 
4837         filename = load_dynamic_stringW(row,2);
4838         dirproperty = load_dynamic_stringW(row,3);
4839         section = load_dynamic_stringW(row,4);
4840         key = load_dynamic_stringW(row,5);
4841         value = load_dynamic_stringW(row,6);
4842         action = MSI_RecordGetInteger(row,7);
4843
4844         deformat_string(package,section,&deformated_section);
4845         deformat_string(package,key,&deformated_key);
4846         deformat_string(package,value,&deformated_value);
4847
4848         if (dirproperty)
4849         {
4850             folder = resolve_folder(package, dirproperty, FALSE, FALSE, NULL);
4851             if (!folder)
4852                 folder = load_dynamic_property(package,dirproperty,NULL);
4853         }
4854         else
4855             folder = load_dynamic_property(package, szWindowsFolder, NULL);
4856
4857         if (!folder)
4858         {
4859             ERR("Unable to resolve folder! (%s)\n",debugstr_w(dirproperty));
4860             goto cleanup;
4861         }
4862
4863         fullname = build_directory_name(3, folder, filename, NULL);
4864
4865         if (action == 0)
4866         {
4867             TRACE("Adding value %s to section %s in %s\n",
4868                 debugstr_w(deformated_key), debugstr_w(deformated_section),
4869                 debugstr_w(fullname));
4870             WritePrivateProfileStringW(deformated_section, deformated_key,
4871                                        deformated_value, fullname);
4872         }
4873         else if (action == 1)
4874         {
4875             WCHAR returned[10];
4876             GetPrivateProfileStringW(deformated_section, deformated_key, NULL,
4877                                      returned, 10, fullname);
4878             if (returned[0] == 0)
4879             {
4880                 TRACE("Adding value %s to section %s in %s\n",
4881                     debugstr_w(deformated_key), debugstr_w(deformated_section),
4882                     debugstr_w(fullname));
4883
4884                 WritePrivateProfileStringW(deformated_section, deformated_key,
4885                                        deformated_value, fullname);
4886             }
4887         }
4888         else if (action == 3)
4889         {
4890             FIXME("Append to existing section not yet implemented\n");
4891         }
4892         
4893         uirow = MSI_CreateRecord(4);
4894         MSI_RecordSetStringW(uirow,1,identifier);
4895         MSI_RecordSetStringW(uirow,2,deformated_section);
4896         MSI_RecordSetStringW(uirow,3,deformated_key);
4897         MSI_RecordSetStringW(uirow,4,deformated_value);
4898         ui_actiondata(package,szWriteIniValues,uirow);
4899         msiobj_release( &uirow->hdr );
4900 cleanup:
4901         HeapFree(GetProcessHeap(),0,identifier);
4902         HeapFree(GetProcessHeap(),0,fullname);
4903         HeapFree(GetProcessHeap(),0,filename);
4904         HeapFree(GetProcessHeap(),0,key);
4905         HeapFree(GetProcessHeap(),0,value);
4906         HeapFree(GetProcessHeap(),0,section);
4907         HeapFree(GetProcessHeap(),0,dirproperty);
4908         HeapFree(GetProcessHeap(),0,folder);
4909         HeapFree(GetProcessHeap(),0,deformated_key);
4910         HeapFree(GetProcessHeap(),0,deformated_value);
4911         HeapFree(GetProcessHeap(),0,deformated_section);
4912         msiobj_release(&row->hdr);
4913     }
4914     MSI_ViewClose(view);
4915     msiobj_release(&view->hdr);
4916     return rc;
4917 }
4918
4919 static UINT ACTION_SelfRegModules(MSIPACKAGE *package)
4920 {
4921     UINT rc;
4922     MSIQUERY * view;
4923     MSIRECORD * row = 0;
4924     static const WCHAR ExecSeqQuery[] = {'S','e','l','e','c','t',' ','*',' ',
4925 'f','r','o','m',' ','S','e','l','f','R','e','g',0};
4926
4927     static const WCHAR ExeStr[] = {
4928 'r','e','g','s','v','r','3','2','.','e','x','e',' ','/','s',' ',0};
4929     STARTUPINFOW si;
4930     PROCESS_INFORMATION info;
4931     BOOL brc;
4932
4933     memset(&si,0,sizeof(STARTUPINFOW));
4934
4935     rc = MSI_DatabaseOpenViewW(package->db, ExecSeqQuery, &view);
4936     if (rc != ERROR_SUCCESS)
4937     {
4938         TRACE("no SelfReg table\n");
4939         return ERROR_SUCCESS;
4940     }
4941
4942     rc = MSI_ViewExecute(view, 0);
4943     if (rc != ERROR_SUCCESS)
4944     {
4945         MSI_ViewClose(view);
4946         msiobj_release(&view->hdr);
4947         return rc;
4948     }
4949
4950     while (1)
4951     {
4952         LPWSTR filename;
4953         INT index;
4954         DWORD len;
4955
4956         rc = MSI_ViewFetch(view,&row);
4957         if (rc != ERROR_SUCCESS)
4958         {
4959             rc = ERROR_SUCCESS;
4960             break;
4961         }
4962
4963         filename = load_dynamic_stringW(row,1);
4964         index = get_loaded_file(package,filename);
4965
4966         if (index < 0)
4967         {
4968             ERR("Unable to find file id %s\n",debugstr_w(filename));
4969             HeapFree(GetProcessHeap(),0,filename);
4970             msiobj_release(&row->hdr);
4971             continue;
4972         }
4973         HeapFree(GetProcessHeap(),0,filename);
4974
4975         len = strlenW(ExeStr);
4976         len += strlenW(package->files[index].TargetPath);
4977         len +=2;
4978
4979         filename = HeapAlloc(GetProcessHeap(),0,len*sizeof(WCHAR));
4980         strcpyW(filename,ExeStr);
4981         strcatW(filename,package->files[index].TargetPath);
4982
4983         TRACE("Registering %s\n",debugstr_w(filename));
4984         brc = CreateProcessW(NULL, filename, NULL, NULL, FALSE, 0, NULL,
4985                   c_collen, &si, &info);
4986
4987         if (brc)
4988             WaitForSingleObject(info.hProcess,INFINITE);
4989  
4990         HeapFree(GetProcessHeap(),0,filename);
4991         msiobj_release(&row->hdr);
4992     }
4993     MSI_ViewClose(view);
4994     msiobj_release(&view->hdr);
4995     return rc;
4996 }
4997
4998 static UINT ACTION_PublishFeatures(MSIPACKAGE *package)
4999 {
5000     LPWSTR productcode;
5001     UINT rc;
5002     DWORD i;
5003     HKEY hkey=0;
5004     HKEY hukey=0;
5005     static const WCHAR szProductCode[]=
5006          {'P','r','o','d','u','c','t','C','o','d','e',0};
5007     
5008     if (!package)
5009         return ERROR_INVALID_HANDLE;
5010
5011     productcode = load_dynamic_property(package,szProductCode,&rc);
5012     if (!productcode)
5013         return rc;
5014
5015     rc = MSIREG_OpenFeaturesKey(productcode,&hkey,TRUE);
5016     if (rc != ERROR_SUCCESS)
5017         goto end;
5018
5019     rc = MSIREG_OpenUserFeaturesKey(productcode,&hukey,TRUE);
5020     if (rc != ERROR_SUCCESS)
5021         goto end;
5022
5023     /* here the guids are base 85 encoded */
5024     for (i = 0; i < package->loaded_features; i++)
5025     {
5026         LPWSTR data = NULL;
5027         GUID clsid;
5028         int j;
5029         INT size;
5030
5031         size = package->features[i].ComponentCount*21;
5032         size +=1;
5033         if (package->features[i].Feature_Parent[0])
5034             size += strlenW(package->features[i].Feature_Parent)+2;
5035
5036         data = HeapAlloc(GetProcessHeap(), 0, size * sizeof(WCHAR));
5037
5038         data[0] = 0;
5039         for (j = 0; j < package->features[i].ComponentCount; j++)
5040         {
5041             WCHAR buf[21];
5042             memset(buf,0,sizeof(buf));
5043             TRACE("From %s\n",debugstr_w(package->components
5044                             [package->features[i].Components[j]].ComponentId));
5045             CLSIDFromString(package->components
5046                             [package->features[i].Components[j]].ComponentId,
5047                             &clsid);
5048             encode_base85_guid(&clsid,buf);
5049             TRACE("to %s\n",debugstr_w(buf));
5050             strcatW(data,buf);
5051         }
5052         if (package->features[i].Feature_Parent[0])
5053         {
5054             static const WCHAR sep[] = {'\2',0};
5055             strcatW(data,sep);
5056             strcatW(data,package->features[i].Feature_Parent);
5057         }
5058
5059         size = (strlenW(data)+1)*sizeof(WCHAR);
5060         RegSetValueExW(hkey,package->features[i].Feature,0,REG_SZ,
5061                        (LPSTR)data,size);
5062         HeapFree(GetProcessHeap(),0,data);
5063
5064         size = strlenW(package->features[i].Feature_Parent)*sizeof(WCHAR);
5065         RegSetValueExW(hukey,package->features[i].Feature,0,REG_SZ,
5066                        (LPSTR)package->features[i].Feature_Parent,size);
5067     }
5068
5069 end:
5070     RegCloseKey(hkey);
5071     RegCloseKey(hukey);
5072     HeapFree(GetProcessHeap(), 0, productcode);
5073     return rc;
5074 }
5075
5076 static UINT ACTION_RegisterProduct(MSIPACKAGE *package)
5077 {
5078     static const WCHAR szProductCode[]=
5079          {'P','r','o','d','u','c','t','C','o','d','e',0};
5080     HKEY hkey=0;
5081     LPWSTR buffer;
5082     LPWSTR productcode;
5083     UINT rc,i;
5084     DWORD size;
5085     static WCHAR szNONE[] = {0};
5086     static const WCHAR szWindowsInstaler[] = 
5087     {'W','i','n','d','o','w','s','I','n','s','t','a','l','l','e','r',0};
5088     static const WCHAR szPropKeys[][80] = 
5089     {
5090 {'A','R','P','A','U','T','H','O','R','I','Z','E','D','C','D','F','P','R','E','F','I','X',0},
5091 {'A','R','P','C','O','N','T','A','C','T'},
5092 {'A','R','P','C','O','M','M','E','N','T','S',0},
5093 {'P','r','o','d','u','c','t','N','a','m','e',0},
5094 {'P','r','o','d','u','c','t','V','e','r','s','i','o','n',0},
5095 {'A','R','P','H','E','L','P','L','I','N','K',0},
5096 {'A','R','P','H','E','L','P','T','E','L','E','P','H','O','N','E',0},
5097 {'A','R','P','I','N','S','T','A','L','L','L','O','C','A','T','I','O','N',0},
5098 {'S','O','U','R','C','E','D','I','R',0},
5099 {'M','a','n','u','f','a','c','t','u','r','e','r',0},
5100 {'A','R','P','R','E','A','D','M','E',0},
5101 {'A','R','P','S','I','Z','E',0},
5102 {'A','R','P','U','R','L','I','N','F','O','A','B','O','U','T',0},
5103 {'A','R','P','U','R','L','U','P','D','A','T','E','I','N','F','O',0},
5104 {'P','r','o','d','u','c','t','I','D',0},
5105 {0},
5106     };
5107
5108     static const WCHAR szRegKeys[][80] = 
5109     {
5110 {'A','u','t','h','o','r','i','z','e','d','C','D','F','P','r','e','f','i','x',0},
5111 {'C','o','n','t','a','c','t',0},
5112 {'C','o','m','m','e','n','t','s',0},
5113 {'D','i','s','p','l','a','y','N','a','m','e',0},
5114 {'D','i','s','p','l','a','y','V','e','r','s','i','o','n',0},
5115 {'H','e','l','p','L','i','n','k',0},
5116 {'H','e','l','p','T','e','l','e','p','h','o','n','e',0},
5117 {'I','n','s','t','a','l','l','L','o','c','a','t','i','o','n',0},
5118 {'I','n','s','t','a','l','l','S','o','u','r','c','e',0},
5119 {'P','u','b','l','i','s','h','e','r',0},
5120 {'R','e','a','d','m','e',0},
5121 {'S','i','z','e',0},
5122 {'U','R','L','I','n','f','o','A','b','o','u','t',0},
5123 {'U','R','L','U','p','d','a','t','e','I','n','f','o',0},
5124 {'P','r','o','d','u','c','t','I','D',0},
5125 {0},
5126     };
5127
5128     static const WCHAR path[] = {
5129     'C',':','\\','W','i','n','d','o','w','s','\\',
5130     'I','n','s','t','a','l','l','e','r','\\'};
5131     static const WCHAR fmt[] = {
5132     'C',':','\\','W','i','n','d','o','w','s','\\',
5133     'I','n','s','t','a','l','l','e','r','\\',
5134     '%','x','.','m','s','i',0};
5135     static const WCHAR szLocalPackage[]=
5136          {'L','o','c','a','l','P','a','c','k','a','g','e',0};
5137     WCHAR packagefile[MAX_PATH];
5138     INT num,start;
5139
5140     if (!package)
5141         return ERROR_INVALID_HANDLE;
5142
5143     productcode = load_dynamic_property(package,szProductCode,&rc);
5144     if (!productcode)
5145         return rc;
5146
5147     rc = MSIREG_OpenUninstallKey(productcode,&hkey,TRUE);
5148     if (rc != ERROR_SUCCESS)
5149         goto end;
5150
5151     /* dump all the info i can grab */
5152     FIXME("Flesh out more information \n");
5153
5154     i = 0;
5155     while (szPropKeys[i][0]!=0)
5156     {
5157         buffer = load_dynamic_property(package,szPropKeys[i],&rc);
5158         if (rc != ERROR_SUCCESS)
5159             buffer = szNONE;
5160         size = strlenW(buffer)*sizeof(WCHAR);
5161         RegSetValueExW(hkey,szRegKeys[i],0,REG_SZ,(LPSTR)buffer,size);
5162         i++;
5163     }
5164
5165     rc = 0x1;
5166     size = sizeof(rc);
5167     RegSetValueExW(hkey,szWindowsInstaler,0,REG_DWORD,(LPSTR)&rc,size);
5168     
5169     /* copy the package locally */
5170     num = GetTickCount() & 0xffff;
5171     if (!num) 
5172         num = 1;
5173     start = num;
5174     sprintfW(packagefile,fmt,num);
5175     do 
5176     {
5177         HANDLE handle = CreateFileW(packagefile,GENERIC_WRITE, 0, NULL,
5178                                   CREATE_NEW, FILE_ATTRIBUTE_NORMAL, 0 );
5179         if (handle != INVALID_HANDLE_VALUE)
5180         {
5181             CloseHandle(handle);
5182             break;
5183         }
5184         if (GetLastError() != ERROR_FILE_EXISTS &&
5185             GetLastError() != ERROR_SHARING_VIOLATION)
5186             break;
5187         if (!(++num & 0xffff)) num = 1;
5188         sprintfW(packagefile,fmt,num);
5189     } while (num != start);
5190
5191     create_full_pathW(path);
5192     TRACE("Copying to local package %s\n",debugstr_w(packagefile));
5193     CopyFileW(package->PackagePath,packagefile,FALSE);
5194     size = strlenW(packagefile)*sizeof(WCHAR);
5195     RegSetValueExW(hkey,szLocalPackage,0,REG_SZ,(LPSTR)packagefile,size);
5196     
5197 end:
5198     HeapFree(GetProcessHeap(),0,productcode);
5199     RegCloseKey(hkey);
5200
5201     return ERROR_SUCCESS;
5202 }
5203
5204 static UINT ACTION_InstallExecute(MSIPACKAGE *package)
5205 {
5206     int i;
5207     if (!package)
5208         return ERROR_INVALID_HANDLE;
5209
5210     for (i = 0; i < package->DeferredActionCount; i++)
5211     {
5212         LPWSTR action;
5213         action = package->DeferredAction[i];
5214         ui_actionstart(package, action);
5215         TRACE("Executing Action (%s)\n",debugstr_w(action));
5216         ACTION_CustomAction(package,action,TRUE);
5217         HeapFree(GetProcessHeap(),0,package->DeferredAction[i]);
5218     }
5219     HeapFree(GetProcessHeap(),0,package->DeferredAction);
5220
5221     package->DeferredActionCount = 0;
5222     package->DeferredAction = NULL;
5223
5224     return ERROR_SUCCESS;
5225 }
5226
5227 static UINT ACTION_InstallFinalize(MSIPACKAGE *package)
5228 {
5229     int i;
5230     if (!package)
5231         return ERROR_INVALID_HANDLE;
5232
5233     /* first do the same as an InstallExecute */
5234     ACTION_InstallExecute(package);
5235
5236     /* then handle Commit Actions */
5237     for (i = 0; i < package->CommitActionCount; i++)
5238     {
5239         LPWSTR action;
5240         action = package->CommitAction[i];
5241         ui_actionstart(package, action);
5242         TRACE("Executing Commit Action (%s)\n",debugstr_w(action));
5243         ACTION_CustomAction(package,action,TRUE);
5244         HeapFree(GetProcessHeap(),0,package->CommitAction[i]);
5245     }
5246     HeapFree(GetProcessHeap(),0,package->CommitAction);
5247
5248     package->CommitActionCount = 0;
5249     package->CommitAction = NULL;
5250
5251     return ERROR_SUCCESS;
5252 }
5253
5254 static UINT ACTION_ForceReboot(MSIPACKAGE *package)
5255 {
5256     static const WCHAR RunOnce[] = {
5257     'S','o','f','t','w','a','r','e','\\',
5258     'M','i','c','r','o','s','o','f','t','\\',
5259     'W','i','n','d','o','w','s','\\',
5260     'C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
5261     'R','u','n','O','n','c','e'};
5262     static const WCHAR InstallRunOnce[] = {
5263     'S','o','f','t','w','a','r','e','\\',
5264     'M','i','c','r','o','s','o','f','t','\\',
5265     'W','i','n','d','o','w','s','\\',
5266     'C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
5267     'I','n','s','t','a','l','l','e','r','\\',
5268     'R','u','n','O','n','c','e','E','n','t','r','i','e','s'};
5269
5270     static const WCHAR msiexec_fmt[] = {
5271     'C',':','\\','W','i','n','d','o','w','s','\\','S','y','s','t','e','m',
5272     '\\','M','s','i','E','x','e','c','.','e','x','e',' ','/','@',' ',
5273     '\"','%','s','\"',0};
5274     static const WCHAR install_fmt[] = {
5275     '/','I',' ','\"','%','s','\"',' ',
5276     'A','F','T','E','R','R','E','B','O','O','T','=','1',' ',
5277     'R','U','N','O','N','C','E','E','N','T','R','Y','=','\"','%','s','\"',0};
5278     WCHAR buffer[256];
5279     HKEY hkey,hukey;
5280     LPWSTR productcode;
5281     WCHAR  squished_pc[100];
5282     INT rc;
5283     DWORD size;
5284     static const WCHAR szProductCode[]=
5285          {'P','r','o','d','u','c','t','C','o','d','e',0};
5286     static const WCHAR szLUS[] = {
5287          'L','a','s','t','U','s','e','d','S','o','u','r','c','e',0};
5288     static const WCHAR szSourceList[] = {
5289          'S','o','u','r','c','e','L','i','s','t',0};
5290     static const WCHAR szPackageName[] = { 
5291         'P','a','c','k','a','g','e','N','a','m','e',0};
5292
5293     if (!package)
5294         return ERROR_INVALID_HANDLE;
5295
5296     productcode = load_dynamic_property(package,szProductCode,&rc);
5297     if (!productcode)
5298         return rc;
5299
5300     squash_guid(productcode,squished_pc);
5301
5302     RegCreateKeyW(HKEY_LOCAL_MACHINE,RunOnce,&hkey);
5303     sprintfW(buffer,msiexec_fmt,squished_pc);
5304
5305     size = strlenW(buffer)*sizeof(WCHAR);
5306     RegSetValueExW(hkey,squished_pc,0,REG_SZ,(LPSTR)buffer,size);
5307     RegCloseKey(hkey);
5308
5309     TRACE("Reboot command %s\n",debugstr_w(buffer));
5310
5311     RegCreateKeyW(HKEY_LOCAL_MACHINE,InstallRunOnce,&hkey);
5312     sprintfW(buffer,install_fmt,productcode,squished_pc);
5313     RegSetValueExW(hkey,squished_pc,0,REG_SZ,(LPSTR)buffer,size);
5314     RegCloseKey(hkey);
5315
5316     rc = MSIREG_OpenUserProductsKey(productcode,&hukey,TRUE);
5317     if (rc == ERROR_SUCCESS)
5318     {
5319         HKEY hukey2;
5320         LPWSTR buf;
5321         RegCreateKeyW(hukey, szSourceList, &hukey2);
5322         buf = load_dynamic_property(package,cszSourceDir,NULL);
5323         size = strlenW(buf)*sizeof(WCHAR);
5324         RegSetValueExW(hukey2,szLUS,0,REG_SZ,(LPSTR)buf,size);
5325         HeapFree(GetProcessHeap(),0,buf); 
5326
5327         buf = strrchrW(package->PackagePath,'\\');
5328         if (buf)
5329         {
5330             buf++;
5331             size = strlenW(buf)*sizeof(WCHAR);
5332             RegSetValueExW(hukey2,szPackageName,0,REG_SZ,(LPSTR)buf,size);
5333         }
5334
5335         RegCloseKey(hukey2);
5336     }
5337     HeapFree(GetProcessHeap(),0,productcode);
5338
5339     return ERROR_INSTALL_SUSPEND;
5340 }
5341
5342 UINT ACTION_ResolveSource(MSIPACKAGE* package)
5343 {
5344     /*
5345      * we are currently doing what should be done here in the top level Install
5346      * however for Adminastrative and uninstalls this step will be needed
5347      */
5348     return ERROR_SUCCESS;
5349 }
5350
5351 /* Msi functions that seem appropriate here */
5352 UINT WINAPI MsiDoActionA( MSIHANDLE hInstall, LPCSTR szAction )
5353 {
5354     LPWSTR szwAction;
5355     UINT rc;
5356
5357     TRACE(" exteral attempt at action %s\n",szAction);
5358
5359     if (!szAction)
5360         return ERROR_FUNCTION_FAILED;
5361     if (hInstall == 0)
5362         return ERROR_FUNCTION_FAILED;
5363
5364     szwAction = strdupAtoW(szAction);
5365
5366     if (!szwAction)
5367         return ERROR_FUNCTION_FAILED; 
5368
5369
5370     rc = MsiDoActionW(hInstall, szwAction);
5371     HeapFree(GetProcessHeap(),0,szwAction);
5372     return rc;
5373 }
5374
5375 UINT WINAPI MsiDoActionW( MSIHANDLE hInstall, LPCWSTR szAction )
5376 {
5377     MSIPACKAGE *package;
5378     UINT ret = ERROR_INVALID_HANDLE;
5379
5380     TRACE(" external attempt at action %s \n",debugstr_w(szAction));
5381
5382     package = msihandle2msiinfo(hInstall, MSIHANDLETYPE_PACKAGE);
5383     if( package )
5384     {
5385         ret = ACTION_PerformAction(package,szAction);
5386         msiobj_release( &package->hdr );
5387     }
5388     return ret;
5389 }
5390
5391 UINT WINAPI MsiGetTargetPathA( MSIHANDLE hInstall, LPCSTR szFolder, 
5392                                LPSTR szPathBuf, DWORD* pcchPathBuf) 
5393 {
5394     LPWSTR szwFolder;
5395     LPWSTR szwPathBuf;
5396     UINT rc;
5397
5398     TRACE("getting folder %s %p %li\n",szFolder,szPathBuf, *pcchPathBuf);
5399
5400     if (!szFolder)
5401         return ERROR_FUNCTION_FAILED;
5402     if (hInstall == 0)
5403         return ERROR_FUNCTION_FAILED;
5404
5405     szwFolder = strdupAtoW(szFolder);
5406
5407     if (!szwFolder)
5408         return ERROR_FUNCTION_FAILED; 
5409
5410     szwPathBuf = HeapAlloc( GetProcessHeap(), 0 , *pcchPathBuf * sizeof(WCHAR));
5411
5412     rc = MsiGetTargetPathW(hInstall, szwFolder, szwPathBuf,pcchPathBuf);
5413
5414     WideCharToMultiByte( CP_ACP, 0, szwPathBuf, *pcchPathBuf, szPathBuf,
5415                          *pcchPathBuf, NULL, NULL );
5416
5417     HeapFree(GetProcessHeap(),0,szwFolder);
5418     HeapFree(GetProcessHeap(),0,szwPathBuf);
5419
5420     return rc;
5421 }
5422
5423 UINT WINAPI MsiGetTargetPathW( MSIHANDLE hInstall, LPCWSTR szFolder, LPWSTR
5424                                 szPathBuf, DWORD* pcchPathBuf) 
5425 {
5426     LPWSTR path;
5427     UINT rc = ERROR_FUNCTION_FAILED;
5428     MSIPACKAGE *package;
5429
5430     TRACE("(%s %p %li)\n",debugstr_w(szFolder),szPathBuf,*pcchPathBuf);
5431
5432     package = msihandle2msiinfo(hInstall, MSIHANDLETYPE_PACKAGE);
5433     if (!package)
5434         return ERROR_INVALID_HANDLE;
5435     path = resolve_folder(package, szFolder, FALSE, FALSE, NULL);
5436     msiobj_release( &package->hdr );
5437
5438     if (path && (strlenW(path) > *pcchPathBuf))
5439     {
5440         *pcchPathBuf = strlenW(path)+1;
5441         rc = ERROR_MORE_DATA;
5442     }
5443     else if (path)
5444     {
5445         *pcchPathBuf = strlenW(path)+1;
5446         strcpyW(szPathBuf,path);
5447         TRACE("Returning Path %s\n",debugstr_w(path));
5448         rc = ERROR_SUCCESS;
5449     }
5450     HeapFree(GetProcessHeap(),0,path);
5451     
5452     return rc;
5453 }
5454
5455
5456 UINT WINAPI MsiGetSourcePathA( MSIHANDLE hInstall, LPCSTR szFolder, 
5457                                LPSTR szPathBuf, DWORD* pcchPathBuf) 
5458 {
5459     LPWSTR szwFolder;
5460     LPWSTR szwPathBuf;
5461     UINT rc;
5462
5463     TRACE("getting source %s %p %li\n",szFolder,szPathBuf, *pcchPathBuf);
5464
5465     if (!szFolder)
5466         return ERROR_FUNCTION_FAILED;
5467     if (hInstall == 0)
5468         return ERROR_FUNCTION_FAILED;
5469
5470     szwFolder = strdupAtoW(szFolder);
5471     if (!szwFolder)
5472         return ERROR_FUNCTION_FAILED; 
5473
5474     szwPathBuf = HeapAlloc( GetProcessHeap(), 0 , *pcchPathBuf * sizeof(WCHAR));
5475
5476     rc = MsiGetSourcePathW(hInstall, szwFolder, szwPathBuf,pcchPathBuf);
5477
5478     WideCharToMultiByte( CP_ACP, 0, szwPathBuf, *pcchPathBuf, szPathBuf,
5479                          *pcchPathBuf, NULL, NULL );
5480
5481     HeapFree(GetProcessHeap(),0,szwFolder);
5482     HeapFree(GetProcessHeap(),0,szwPathBuf);
5483
5484     return rc;
5485 }
5486
5487 UINT WINAPI MsiGetSourcePathW( MSIHANDLE hInstall, LPCWSTR szFolder, LPWSTR
5488                                 szPathBuf, DWORD* pcchPathBuf) 
5489 {
5490     LPWSTR path;
5491     UINT rc = ERROR_FUNCTION_FAILED;
5492     MSIPACKAGE *package;
5493
5494     TRACE("(%s %p %li)\n",debugstr_w(szFolder),szPathBuf,*pcchPathBuf);
5495
5496     package = msihandle2msiinfo(hInstall, MSIHANDLETYPE_PACKAGE);
5497     if( !package )
5498         return ERROR_INVALID_HANDLE;
5499     path = resolve_folder(package, szFolder, TRUE, FALSE, NULL);
5500     msiobj_release( &package->hdr );
5501
5502     if (path && strlenW(path) > *pcchPathBuf)
5503     {
5504         *pcchPathBuf = strlenW(path)+1;
5505         rc = ERROR_MORE_DATA;
5506     }
5507     else if (path)
5508     {
5509         *pcchPathBuf = strlenW(path)+1;
5510         strcpyW(szPathBuf,path);
5511         TRACE("Returning Path %s\n",debugstr_w(path));
5512         rc = ERROR_SUCCESS;
5513     }
5514     HeapFree(GetProcessHeap(),0,path);
5515     
5516     return rc;
5517 }
5518
5519
5520 UINT WINAPI MsiSetTargetPathA(MSIHANDLE hInstall, LPCSTR szFolder, 
5521                              LPCSTR szFolderPath)
5522 {
5523     LPWSTR szwFolder;
5524     LPWSTR szwFolderPath;
5525     UINT rc;
5526
5527     if (!szFolder)
5528         return ERROR_FUNCTION_FAILED;
5529     if (hInstall == 0)
5530         return ERROR_FUNCTION_FAILED;
5531
5532     szwFolder = strdupAtoW(szFolder);
5533     if (!szwFolder)
5534         return ERROR_FUNCTION_FAILED; 
5535
5536     szwFolderPath = strdupAtoW(szFolderPath);
5537     if (!szwFolderPath)
5538     {
5539         HeapFree(GetProcessHeap(),0,szwFolder);
5540         return ERROR_FUNCTION_FAILED; 
5541     }
5542
5543     rc = MsiSetTargetPathW(hInstall, szwFolder, szwFolderPath);
5544
5545     HeapFree(GetProcessHeap(),0,szwFolder);
5546     HeapFree(GetProcessHeap(),0,szwFolderPath);
5547
5548     return rc;
5549 }
5550
5551 UINT MSI_SetTargetPathW(MSIPACKAGE *package, LPCWSTR szFolder, 
5552                              LPCWSTR szFolderPath)
5553 {
5554     DWORD i;
5555     LPWSTR path = NULL;
5556     LPWSTR path2 = NULL;
5557     MSIFOLDER *folder;
5558
5559     TRACE("(%p %s %s)\n",package, debugstr_w(szFolder),debugstr_w(szFolderPath));
5560
5561     if (package==NULL)
5562         return ERROR_INVALID_HANDLE;
5563
5564     if (szFolderPath[0]==0)
5565         return ERROR_FUNCTION_FAILED;
5566
5567     if (GetFileAttributesW(szFolderPath) == INVALID_FILE_ATTRIBUTES)
5568         return ERROR_FUNCTION_FAILED;
5569
5570     path = resolve_folder(package,szFolder,FALSE,FALSE,&folder);
5571
5572     if (!path)
5573         return ERROR_INVALID_PARAMETER;
5574
5575     HeapFree(GetProcessHeap(),0,folder->Property);
5576     folder->Property = build_directory_name(2, szFolderPath, NULL);
5577
5578     if (strcmpiW(path, folder->Property) == 0)
5579     {
5580         /*
5581          *  Resolved Target has not really changed, so just 
5582          *  set this folder and do not recalculate everything.
5583          */
5584         HeapFree(GetProcessHeap(),0,folder->ResolvedTarget);
5585         folder->ResolvedTarget = NULL;
5586         path2 = resolve_folder(package,szFolder,FALSE,TRUE,NULL);
5587         HeapFree(GetProcessHeap(),0,path2);
5588     }
5589     else
5590     {
5591         for (i = 0; i < package->loaded_folders; i++)
5592         {
5593             HeapFree(GetProcessHeap(),0,package->folders[i].ResolvedTarget);
5594             package->folders[i].ResolvedTarget=NULL;
5595         }
5596
5597         for (i = 0; i < package->loaded_folders; i++)
5598         {
5599             path2=resolve_folder(package, package->folders[i].Directory, FALSE,
5600                        TRUE, NULL);
5601             HeapFree(GetProcessHeap(),0,path2);
5602         }
5603     }
5604     HeapFree(GetProcessHeap(),0,path);
5605
5606     return ERROR_SUCCESS;
5607 }
5608
5609 UINT WINAPI MsiSetTargetPathW(MSIHANDLE hInstall, LPCWSTR szFolder, 
5610                              LPCWSTR szFolderPath)
5611 {
5612     MSIPACKAGE *package;
5613     UINT ret;
5614
5615     TRACE("(%s %s)\n",debugstr_w(szFolder),debugstr_w(szFolderPath));
5616
5617     package = msihandle2msiinfo(hInstall, MSIHANDLETYPE_PACKAGE);
5618     ret = MSI_SetTargetPathW( package, szFolder, szFolderPath );
5619     msiobj_release( &package->hdr );
5620     return ret;
5621 }
5622
5623 /***********************************************************************
5624  *           MsiGetMode    (MSI.@)
5625  *
5626  * Returns an internal installer state (if it is running in a mode iRunMode)
5627  *
5628  * PARAMS
5629  *   hInstall    [I]  Handle to the installation
5630  *   hRunMode    [I]  Checking run mode
5631  *        MSIRUNMODE_ADMIN             Administrative mode
5632  *        MSIRUNMODE_ADVERTISE         Advertisement mode
5633  *        MSIRUNMODE_MAINTENANCE       Maintenance mode
5634  *        MSIRUNMODE_ROLLBACKENABLED   Rollback is enabled
5635  *        MSIRUNMODE_LOGENABLED        Log file is writing
5636  *        MSIRUNMODE_OPERATIONS        Operations in progress??
5637  *        MSIRUNMODE_REBOOTATEND       We need to reboot after installation completed
5638  *        MSIRUNMODE_REBOOTNOW         We need to reboot to continue the installation
5639  *        MSIRUNMODE_CABINET           Files from cabinet are installed
5640  *        MSIRUNMODE_SOURCESHORTNAMES  Long names in source files is suppressed
5641  *        MSIRUNMODE_TARGETSHORTNAMES  Long names in destination files is suppressed
5642  *        MSIRUNMODE_RESERVED11        Reserved
5643  *        MSIRUNMODE_WINDOWS9X         Running under Windows95/98
5644  *        MSIRUNMODE_ZAWENABLED        Demand installation is supported
5645  *        MSIRUNMODE_RESERVED14        Reserved
5646  *        MSIRUNMODE_RESERVED15        Reserved
5647  *        MSIRUNMODE_SCHEDULED         called from install script
5648  *        MSIRUNMODE_ROLLBACK          called from rollback script
5649  *        MSIRUNMODE_COMMIT            called from commit script
5650  *
5651  * RETURNS
5652  *    In the state: TRUE
5653  *    Not in the state: FALSE
5654  *
5655  */
5656
5657 BOOL WINAPI MsiGetMode(MSIHANDLE hInstall, MSIRUNMODE iRunMode)
5658 {
5659     FIXME("STUB (iRunMode=%i)\n",iRunMode);
5660     return TRUE;
5661 }
5662
5663 /*
5664  * According to the docs, when this is called it immediately recalculates
5665  * all the component states as well
5666  */
5667 UINT WINAPI MsiSetFeatureStateA(MSIHANDLE hInstall, LPCSTR szFeature,
5668                                 INSTALLSTATE iState)
5669 {
5670     LPWSTR szwFeature = NULL;
5671     UINT rc;
5672
5673     szwFeature = strdupAtoW(szFeature);
5674
5675     if (!szwFeature)
5676         return ERROR_FUNCTION_FAILED;
5677    
5678     rc = MsiSetFeatureStateW(hInstall,szwFeature, iState); 
5679
5680     HeapFree(GetProcessHeap(),0,szwFeature);
5681
5682     return rc;
5683 }
5684
5685 UINT WINAPI MsiSetFeatureStateW(MSIHANDLE hInstall, LPCWSTR szFeature,
5686                                 INSTALLSTATE iState)
5687 {
5688     MSIPACKAGE* package;
5689     INT index;
5690
5691     TRACE(" %s to %i\n",debugstr_w(szFeature), iState);
5692
5693     package = msihandle2msiinfo(hInstall, MSIHANDLETYPE_PACKAGE);
5694     if (!package)
5695         return ERROR_INVALID_HANDLE;
5696
5697     index = get_loaded_feature(package,szFeature);
5698     if (index < 0)
5699         return ERROR_UNKNOWN_FEATURE;
5700
5701     package->features[index].ActionRequest= iState;
5702     ACTION_UpdateComponentStates(package,szFeature);
5703
5704     return ERROR_SUCCESS;
5705 }
5706
5707 UINT WINAPI MsiGetFeatureStateA(MSIHANDLE hInstall, LPSTR szFeature,
5708                   INSTALLSTATE *piInstalled, INSTALLSTATE *piAction)
5709 {
5710     LPWSTR szwFeature = NULL;
5711     UINT rc;
5712     
5713     szwFeature = strdupAtoW(szFeature);
5714
5715     rc = MsiGetFeatureStateW(hInstall,szwFeature,piInstalled, piAction);
5716
5717     HeapFree( GetProcessHeap(), 0 , szwFeature);
5718
5719     return rc;
5720 }
5721
5722 UINT MSI_GetFeatureStateW(MSIPACKAGE *package, LPWSTR szFeature,
5723                   INSTALLSTATE *piInstalled, INSTALLSTATE *piAction)
5724 {
5725     INT index;
5726
5727     index = get_loaded_feature(package,szFeature);
5728     if (index < 0)
5729         return ERROR_UNKNOWN_FEATURE;
5730
5731     if (piInstalled)
5732         *piInstalled = package->features[index].Installed;
5733
5734     if (piAction)
5735         *piAction = package->features[index].Action;
5736
5737     TRACE("returning %i %i\n",*piInstalled,*piAction);
5738
5739     return ERROR_SUCCESS;
5740 }
5741
5742 UINT WINAPI MsiGetFeatureStateW(MSIHANDLE hInstall, LPWSTR szFeature,
5743                   INSTALLSTATE *piInstalled, INSTALLSTATE *piAction)
5744 {
5745     MSIPACKAGE* package;
5746     UINT ret;
5747
5748     TRACE("%ld %s %p %p\n", hInstall, debugstr_w(szFeature), piInstalled,
5749 piAction);
5750
5751     package = msihandle2msiinfo(hInstall, MSIHANDLETYPE_PACKAGE);
5752     if (!package)
5753         return ERROR_INVALID_HANDLE;
5754     ret = MSI_GetFeatureStateW(package, szFeature, piInstalled, piAction);
5755     msiobj_release( &package->hdr );
5756     return ret;
5757 }
5758
5759 UINT WINAPI MsiGetComponentStateA(MSIHANDLE hInstall, LPSTR szComponent,
5760                   INSTALLSTATE *piInstalled, INSTALLSTATE *piAction)
5761 {
5762     LPWSTR szwComponent= NULL;
5763     UINT rc;
5764     
5765     szwComponent= strdupAtoW(szComponent);
5766
5767     rc = MsiGetComponentStateW(hInstall,szwComponent,piInstalled, piAction);
5768
5769     HeapFree( GetProcessHeap(), 0 , szwComponent);
5770
5771     return rc;
5772 }
5773
5774 UINT MSI_GetComponentStateW(MSIPACKAGE *package, LPWSTR szComponent,
5775                   INSTALLSTATE *piInstalled, INSTALLSTATE *piAction)
5776 {
5777     INT index;
5778
5779     TRACE("%p %s %p %p\n", package, debugstr_w(szComponent), piInstalled,
5780 piAction);
5781
5782     index = get_loaded_component(package,szComponent);
5783     if (index < 0)
5784         return ERROR_UNKNOWN_COMPONENT;
5785
5786     if (piInstalled)
5787         *piInstalled = package->components[index].Installed;
5788
5789     if (piAction)
5790         *piAction = package->components[index].Action;
5791
5792     TRACE("states (%i, %i)\n",
5793 (piInstalled)?*piInstalled:-1,(piAction)?*piAction:-1);
5794
5795     return ERROR_SUCCESS;
5796 }
5797
5798 UINT WINAPI MsiGetComponentStateW(MSIHANDLE hInstall, LPWSTR szComponent,
5799                   INSTALLSTATE *piInstalled, INSTALLSTATE *piAction)
5800 {
5801     MSIPACKAGE* package;
5802     UINT ret;
5803
5804     TRACE("%ld %s %p %p\n", hInstall, debugstr_w(szComponent),
5805            piInstalled, piAction);
5806
5807     package = msihandle2msiinfo(hInstall, MSIHANDLETYPE_PACKAGE);
5808     if (!package)
5809         return ERROR_INVALID_HANDLE;
5810     ret = MSI_GetComponentStateW( package, szComponent, piInstalled, piAction);
5811     msiobj_release( &package->hdr );
5812     return ret;
5813 }
5814
5815 #if 0
5816 static UINT ACTION_Template(MSIPACKAGE *package)
5817 {
5818     UINT rc;
5819     MSIQUERY * view;
5820     MSIRECORD * row = 0;
5821     static const WCHAR ExecSeqQuery[] = {0};
5822
5823     rc = MSI_DatabaseOpenViewW(package->db, ExecSeqQuery, &view);
5824     if (rc != ERROR_SUCCESS)
5825         return rc;
5826
5827     rc = MSI_ViewExecute(view, 0);
5828     if (rc != ERROR_SUCCESS)
5829     {
5830         MSI_ViewClose(view);
5831         msiobj_release(&view->hdr);
5832         return rc;
5833     }
5834
5835     while (1)
5836     {
5837         rc = MSI_ViewFetch(view,&row);
5838         if (rc != ERROR_SUCCESS)
5839         {
5840             rc = ERROR_SUCCESS;
5841             break;
5842         }
5843
5844         msiobj_release(&row->hdr);
5845     }
5846     MSI_ViewClose(view);
5847     msiobj_release(&view->hdr);
5848     return rc;
5849 }
5850 #endif