Fix gcc 4.0 -Wpointer-sign warnings.
[wine] / dlls / msi / action.c
1 /*
2  * Implementation of the Microsoft Installer (msi.dll)
3  *
4  * Copyright 2004,2005 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
31 #define COBJMACROS
32
33 #include "windef.h"
34 #include "winbase.h"
35 #include "winerror.h"
36 #include "winreg.h"
37 #include "wine/debug.h"
38 #include "msidefs.h"
39 #include "msipriv.h"
40 #include "winuser.h"
41 #include "shlobj.h"
42 #include "wine/unicode.h"
43 #include "winver.h"
44 #include "action.h"
45
46 #define REG_PROGRESS_VALUE 13200
47 #define COMPONENT_PROGRESS_VALUE 24000
48
49 WINE_DEFAULT_DEBUG_CHANNEL(msi);
50
51 /*
52  * Prototypes
53  */
54 static UINT ACTION_ProcessExecSequence(MSIPACKAGE *package, BOOL UIran);
55 static UINT ACTION_ProcessUISequence(MSIPACKAGE *package);
56 static UINT ACTION_PerformActionSequence(MSIPACKAGE *package, UINT seq, BOOL UI);
57
58 /* 
59  * action handlers
60  */
61 typedef UINT (*STANDARDACTIONHANDLER)(MSIPACKAGE*);
62
63 static UINT ACTION_LaunchConditions(MSIPACKAGE *package);
64 static UINT ACTION_CostInitialize(MSIPACKAGE *package);
65 static UINT ACTION_CreateFolders(MSIPACKAGE *package);
66 static UINT ACTION_CostFinalize(MSIPACKAGE *package);
67 static UINT ACTION_FileCost(MSIPACKAGE *package);
68 static UINT ACTION_WriteRegistryValues(MSIPACKAGE *package);
69 static UINT ACTION_InstallInitialize(MSIPACKAGE *package);
70 static UINT ACTION_InstallValidate(MSIPACKAGE *package);
71 static UINT ACTION_ProcessComponents(MSIPACKAGE *package);
72 static UINT ACTION_RegisterTypeLibraries(MSIPACKAGE *package);
73 static UINT ACTION_RegisterUser(MSIPACKAGE *package);
74 static UINT ACTION_CreateShortcuts(MSIPACKAGE *package);
75 static UINT ACTION_PublishProduct(MSIPACKAGE *package);
76 static UINT ACTION_WriteIniValues(MSIPACKAGE *package);
77 static UINT ACTION_SelfRegModules(MSIPACKAGE *package);
78 static UINT ACTION_PublishFeatures(MSIPACKAGE *package);
79 static UINT ACTION_RegisterProduct(MSIPACKAGE *package);
80 static UINT ACTION_InstallExecute(MSIPACKAGE *package);
81 static UINT ACTION_InstallFinalize(MSIPACKAGE *package);
82 static UINT ACTION_ForceReboot(MSIPACKAGE *package);
83 static UINT ACTION_ResolveSource(MSIPACKAGE *package);
84 static UINT ACTION_ExecuteAction(MSIPACKAGE *package);
85 static UINT ACTION_RegisterFonts(MSIPACKAGE *package);
86 static UINT ACTION_PublishComponents(MSIPACKAGE *package);
87
88 /*
89  * consts and values used
90  */
91 static const WCHAR c_colon[] = {'C',':','\\',0};
92
93 const static WCHAR szCreateFolders[] =
94     {'C','r','e','a','t','e','F','o','l','d','e','r','s',0};
95 const static WCHAR szCostFinalize[] =
96     {'C','o','s','t','F','i','n','a','l','i','z','e',0};
97 const WCHAR szInstallFiles[] =
98     {'I','n','s','t','a','l','l','F','i','l','e','s',0};
99 const WCHAR szDuplicateFiles[] =
100     {'D','u','p','l','i','c','a','t','e','F','i','l','e','s',0};
101 const static WCHAR szWriteRegistryValues[] =
102     {'W','r','i','t','e','R','e','g','i','s','t','r','y',
103             'V','a','l','u','e','s',0};
104 const static WCHAR szCostInitialize[] =
105     {'C','o','s','t','I','n','i','t','i','a','l','i','z','e',0};
106 const static WCHAR szFileCost[] = 
107     {'F','i','l','e','C','o','s','t',0};
108 const static WCHAR szInstallInitialize[] = 
109     {'I','n','s','t','a','l','l','I','n','i','t','i','a','l','i','z','e',0};
110 const static WCHAR szInstallValidate[] = 
111     {'I','n','s','t','a','l','l','V','a','l','i','d','a','t','e',0};
112 const static WCHAR szLaunchConditions[] = 
113     {'L','a','u','n','c','h','C','o','n','d','i','t','i','o','n','s',0};
114 const static WCHAR szProcessComponents[] = 
115     {'P','r','o','c','e','s','s','C','o','m','p','o','n','e','n','t','s',0};
116 const static WCHAR szRegisterTypeLibraries[] = 
117     {'R','e','g','i','s','t','e','r','T','y','p','e',
118             'L','i','b','r','a','r','i','e','s',0};
119 const WCHAR szRegisterClassInfo[] = 
120     {'R','e','g','i','s','t','e','r','C','l','a','s','s','I','n','f','o',0};
121 const WCHAR szRegisterProgIdInfo[] = 
122     {'R','e','g','i','s','t','e','r','P','r','o','g','I','d','I','n','f','o',0};
123 const static WCHAR szCreateShortcuts[] = 
124     {'C','r','e','a','t','e','S','h','o','r','t','c','u','t','s',0};
125 const static WCHAR szPublishProduct[] = 
126     {'P','u','b','l','i','s','h','P','r','o','d','u','c','t',0};
127 const static WCHAR szWriteIniValues[] = 
128     {'W','r','i','t','e','I','n','i','V','a','l','u','e','s',0};
129 const static WCHAR szSelfRegModules[] = 
130     {'S','e','l','f','R','e','g','M','o','d','u','l','e','s',0};
131 const static WCHAR szPublishFeatures[] = 
132     {'P','u','b','l','i','s','h','F','e','a','t','u','r','e','s',0};
133 const static WCHAR szRegisterProduct[] = 
134     {'R','e','g','i','s','t','e','r','P','r','o','d','u','c','t',0};
135 const static WCHAR szInstallExecute[] = 
136     {'I','n','s','t','a','l','l','E','x','e','c','u','t','e',0};
137 const static WCHAR szInstallExecuteAgain[] = 
138     {'I','n','s','t','a','l','l','E','x','e','c','u','t','e',
139             'A','g','a','i','n',0};
140 const static WCHAR szInstallFinalize[] = 
141     {'I','n','s','t','a','l','l','F','i','n','a','l','i','z','e',0};
142 const static WCHAR szForceReboot[] = 
143     {'F','o','r','c','e','R','e','b','o','o','t',0};
144 const static WCHAR szResolveSource[] =
145     {'R','e','s','o','l','v','e','S','o','u','r','c','e',0};
146 const WCHAR szAppSearch[] = 
147     {'A','p','p','S','e','a','r','c','h',0};
148 const static WCHAR szAllocateRegistrySpace[] = 
149     {'A','l','l','o','c','a','t','e','R','e','g','i','s','t','r','y',
150             'S','p','a','c','e',0};
151 const static WCHAR szBindImage[] = 
152     {'B','i','n','d','I','m','a','g','e',0};
153 const static WCHAR szCCPSearch[] = 
154     {'C','C','P','S','e','a','r','c','h',0};
155 const static WCHAR szDeleteServices[] = 
156     {'D','e','l','e','t','e','S','e','r','v','i','c','e','s',0};
157 const static WCHAR szDisableRollback[] = 
158     {'D','i','s','a','b','l','e','R','o','l','l','b','a','c','k',0};
159 const static WCHAR szExecuteAction[] = 
160     {'E','x','e','c','u','t','e','A','c','t','i','o','n',0};
161 const WCHAR szFindRelatedProducts[] = 
162     {'F','i','n','d','R','e','l','a','t','e','d',
163             'P','r','o','d','u','c','t','s',0};
164 const static WCHAR szInstallAdminPackage[] = 
165     {'I','n','s','t','a','l','l','A','d','m','i','n',
166             'P','a','c','k','a','g','e',0};
167 const static WCHAR szInstallSFPCatalogFile[] = 
168     {'I','n','s','t','a','l','l','S','F','P','C','a','t','a','l','o','g',
169             'F','i','l','e',0};
170 const static WCHAR szIsolateComponents[] = 
171     {'I','s','o','l','a','t','e','C','o','m','p','o','n','e','n','t','s',0};
172 const WCHAR szMigrateFeatureStates[] = 
173     {'M','i','g','r','a','t','e','F','e','a','t','u','r','e',
174             'S','t','a','t','e','s',0};
175 const WCHAR szMoveFiles[] = 
176     {'M','o','v','e','F','i','l','e','s',0};
177 const static WCHAR szMsiPublishAssemblies[] = 
178     {'M','s','i','P','u','b','l','i','s','h',
179             'A','s','s','e','m','b','l','i','e','s',0};
180 const static WCHAR szMsiUnpublishAssemblies[] = 
181     {'M','s','i','U','n','p','u','b','l','i','s','h',
182             'A','s','s','e','m','b','l','i','e','s',0};
183 const static WCHAR szInstallODBC[] = 
184     {'I','n','s','t','a','l','l','O','D','B','C',0};
185 const static WCHAR szInstallServices[] = 
186     {'I','n','s','t','a','l','l','S','e','r','v','i','c','e','s',0};
187 const WCHAR szPatchFiles[] = 
188     {'P','a','t','c','h','F','i','l','e','s',0};
189 const static WCHAR szPublishComponents[] = 
190     {'P','u','b','l','i','s','h','C','o','m','p','o','n','e','n','t','s',0};
191 const static WCHAR szRegisterComPlus[] =
192     {'R','e','g','i','s','t','e','r','C','o','m','P','l','u','s',0};
193 const WCHAR szRegisterExtensionInfo[] =
194     {'R','e','g','i','s','t','e','r','E','x','t','e','n','s','i','o','n',
195             'I','n','f','o',0};
196 const static WCHAR szRegisterFonts[] =
197     {'R','e','g','i','s','t','e','r','F','o','n','t','s',0};
198 const WCHAR szRegisterMIMEInfo[] =
199     {'R','e','g','i','s','t','e','r','M','I','M','E','I','n','f','o',0};
200 const static WCHAR szRegisterUser[] =
201     {'R','e','g','i','s','t','e','r','U','s','e','r',0};
202 const WCHAR szRemoveDuplicateFiles[] =
203     {'R','e','m','o','v','e','D','u','p','l','i','c','a','t','e',
204             'F','i','l','e','s',0};
205 const static WCHAR szRemoveEnvironmentStrings[] =
206     {'R','e','m','o','v','e','E','n','v','i','r','o','n','m','e','n','t',
207             'S','t','r','i','n','g','s',0};
208 const WCHAR szRemoveExistingProducts[] =
209     {'R','e','m','o','v','e','E','x','i','s','t','i','n','g',
210             'P','r','o','d','u','c','t','s',0};
211 const WCHAR szRemoveFiles[] =
212     {'R','e','m','o','v','e','F','i','l','e','s',0};
213 const static WCHAR szRemoveFolders[] =
214     {'R','e','m','o','v','e','F','o','l','d','e','r','s',0};
215 const static WCHAR szRemoveIniValues[] =
216     {'R','e','m','o','v','e','I','n','i','V','a','l','u','e','s',0};
217 const static WCHAR szRemoveODBC[] =
218     {'R','e','m','o','v','e','O','D','B','C',0};
219 const static WCHAR szRemoveRegistryValues[] =
220     {'R','e','m','o','v','e','R','e','g','i','s','t','r','y',
221             'V','a','l','u','e','s',0};
222 const static WCHAR szRemoveShortcuts[] =
223     {'R','e','m','o','v','e','S','h','o','r','t','c','u','t','s',0};
224 const static WCHAR szRMCCPSearch[] =
225     {'R','M','C','C','P','S','e','a','r','c','h',0};
226 const static WCHAR szScheduleReboot[] =
227     {'S','c','h','e','d','u','l','e','R','e','b','o','o','t',0};
228 const static WCHAR szSelfUnregModules[] =
229     {'S','e','l','f','U','n','r','e','g','M','o','d','u','l','e','s',0};
230 const static WCHAR szSetODBCFolders[] =
231     {'S','e','t','O','D','B','C','F','o','l','d','e','r','s',0};
232 const static WCHAR szStartServices[] =
233     {'S','t','a','r','t','S','e','r','v','i','c','e','s',0};
234 const static WCHAR szStopServices[] =
235     {'S','t','o','p','S','e','r','v','i','c','e','s',0};
236 const static WCHAR szUnpublishComponents[] =
237     {'U','n','p','u','b','l','i','s','h',
238             'C','o','m','p','o','n','e','n','t','s',0};
239 const static WCHAR szUnpublishFeatures[] =
240     {'U','n','p','u','b','l','i','s','h','F','e','a','t','u','r','e','s',0};
241 const WCHAR szUnregisterClassInfo[] =
242     {'U','n','r','e','g','i','s','t','e','r','C','l','a','s','s',
243             'I','n','f','o',0};
244 const static WCHAR szUnregisterComPlus[] =
245     {'U','n','r','e','g','i','s','t','e','r','C','o','m','P','l','u','s',0};
246 const WCHAR szUnregisterExtensionInfo[] =
247     {'U','n','r','e','g','i','s','t','e','r',
248             '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 WCHAR szUnregisterMIMEInfo[] =
252     {'U','n','r','e','g','i','s','t','e','r','M','I','M','E','I','n','f','o',0};
253 const WCHAR szUnregisterProgIdInfo[] =
254     {'U','n','r','e','g','i','s','t','e','r','P','r','o','g','I','d',
255             'I','n','f','o',0};
256 const static WCHAR szUnregisterTypeLibraries[] =
257     {'U','n','r','e','g','i','s','t','e','r','T','y','p','e',
258             'L','i','b','r','a','r','i','e','s',0};
259 const static WCHAR szValidateProductID[] =
260     {'V','a','l','i','d','a','t','e','P','r','o','d','u','c','t','I','D',0};
261 const static WCHAR szWriteEnvironmentStrings[] =
262     {'W','r','i','t','e','E','n','v','i','r','o','n','m','e','n','t',
263             'S','t','r','i','n','g','s',0};
264
265 struct _actions {
266     LPCWSTR action;
267     STANDARDACTIONHANDLER handler;
268 };
269
270 static struct _actions StandardActions[] = {
271     { szAllocateRegistrySpace, NULL},
272     { szAppSearch, ACTION_AppSearch },
273     { szBindImage, NULL},
274     { szCCPSearch, NULL},
275     { szCostFinalize, ACTION_CostFinalize },
276     { szCostInitialize, ACTION_CostInitialize },
277     { szCreateFolders, ACTION_CreateFolders },
278     { szCreateShortcuts, ACTION_CreateShortcuts },
279     { szDeleteServices, NULL},
280     { szDisableRollback, NULL},
281     { szDuplicateFiles, ACTION_DuplicateFiles },
282     { szExecuteAction, ACTION_ExecuteAction },
283     { szFileCost, ACTION_FileCost },
284     { szFindRelatedProducts, ACTION_FindRelatedProducts },
285     { szForceReboot, ACTION_ForceReboot },
286     { szInstallAdminPackage, NULL},
287     { szInstallExecute, ACTION_InstallExecute },
288     { szInstallExecuteAgain, ACTION_InstallExecute },
289     { szInstallFiles, ACTION_InstallFiles},
290     { szInstallFinalize, ACTION_InstallFinalize },
291     { szInstallInitialize, ACTION_InstallInitialize },
292     { szInstallSFPCatalogFile, NULL},
293     { szInstallValidate, ACTION_InstallValidate },
294     { szIsolateComponents, NULL},
295     { szLaunchConditions, ACTION_LaunchConditions },
296     { szMigrateFeatureStates, NULL},
297     { szMoveFiles, NULL},
298     { szMsiPublishAssemblies, NULL},
299     { szMsiUnpublishAssemblies, NULL},
300     { szInstallODBC, NULL},
301     { szInstallServices, NULL},
302     { szPatchFiles, NULL},
303     { szProcessComponents, ACTION_ProcessComponents },
304     { szPublishComponents, ACTION_PublishComponents },
305     { szPublishFeatures, ACTION_PublishFeatures },
306     { szPublishProduct, ACTION_PublishProduct },
307     { szRegisterClassInfo, ACTION_RegisterClassInfo },
308     { szRegisterComPlus, NULL},
309     { szRegisterExtensionInfo, ACTION_RegisterExtensionInfo },
310     { szRegisterFonts, ACTION_RegisterFonts },
311     { szRegisterMIMEInfo, ACTION_RegisterMIMEInfo },
312     { szRegisterProduct, ACTION_RegisterProduct },
313     { szRegisterProgIdInfo, ACTION_RegisterProgIdInfo },
314     { szRegisterTypeLibraries, ACTION_RegisterTypeLibraries },
315     { szRegisterUser, ACTION_RegisterUser},
316     { szRemoveDuplicateFiles, NULL},
317     { szRemoveEnvironmentStrings, NULL},
318     { szRemoveExistingProducts, NULL},
319     { szRemoveFiles, NULL},
320     { szRemoveFolders, NULL},
321     { szRemoveIniValues, NULL},
322     { szRemoveODBC, NULL},
323     { szRemoveRegistryValues, NULL},
324     { szRemoveShortcuts, NULL},
325     { szResolveSource, ACTION_ResolveSource},
326     { szRMCCPSearch, NULL},
327     { szScheduleReboot, NULL},
328     { szSelfRegModules, ACTION_SelfRegModules },
329     { szSelfUnregModules, NULL},
330     { szSetODBCFolders, NULL},
331     { szStartServices, NULL},
332     { szStopServices, NULL},
333     { szUnpublishComponents, NULL},
334     { szUnpublishFeatures, NULL},
335     { szUnregisterClassInfo, NULL},
336     { szUnregisterComPlus, NULL},
337     { szUnregisterExtensionInfo, NULL},
338     { szUnregisterFonts, NULL},
339     { szUnregisterMIMEInfo, NULL},
340     { szUnregisterProgIdInfo, NULL},
341     { szUnregisterTypeLibraries, NULL},
342     { szValidateProductID, NULL},
343     { szWriteEnvironmentStrings, NULL},
344     { szWriteIniValues, ACTION_WriteIniValues },
345     { szWriteRegistryValues, ACTION_WriteRegistryValues},
346     { NULL, NULL},
347 };
348
349
350 /********************************************************
351  * helper functions
352  ********************************************************/
353
354 static void ce_actiontext(MSIPACKAGE* package, LPCWSTR action)
355 {
356     static const WCHAR szActionText[] = 
357         {'A','c','t','i','o','n','T','e','x','t',0};
358     MSIRECORD *row;
359
360     row = MSI_CreateRecord(1);
361     MSI_RecordSetStringW(row,1,action);
362     ControlEvent_FireSubscribedEvent(package,szActionText, row);
363     msiobj_release(&row->hdr);
364 }
365
366 static void ui_actionstart(MSIPACKAGE *package, LPCWSTR action)
367 {
368     static const WCHAR template_s[]=
369         {'A','c','t','i','o','n',' ','%','s',':',' ','%','s','.',' ', '%','s',
370          '.',0};
371     static const WCHAR format[] = 
372         {'H','H','\'',':','\'','m','m','\'',':','\'','s','s',0};
373     static const WCHAR Query_t[] = 
374         {'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
375          '`','A','c','t','i','o', 'n','T','e','x','t','`',' ',
376          'W','H','E','R','E', ' ','`','A','c','t','i','o','n','`',' ','=', 
377          ' ','\'','%','s','\'',0};
378     WCHAR message[1024];
379     WCHAR timet[0x100];
380     MSIRECORD * row = 0;
381     LPCWSTR ActionText;
382
383     GetTimeFormatW(LOCALE_USER_DEFAULT, 0, NULL, format, timet, 0x100);
384
385     row = MSI_QueryGetRecord( package->db, Query_t, action );
386     if (!row)
387         return;
388
389     ActionText = MSI_RecordGetString(row,2);
390
391     sprintfW(message,template_s,timet,action,ActionText);
392     msiobj_release(&row->hdr);
393
394     row = MSI_CreateRecord(1);
395     MSI_RecordSetStringW(row,1,message);
396  
397     MSI_ProcessMessage(package, INSTALLMESSAGE_ACTIONSTART, row);
398     msiobj_release(&row->hdr);
399 }
400
401 static void ui_actioninfo(MSIPACKAGE *package, LPCWSTR action, BOOL start, 
402                           UINT rc)
403 {
404     MSIRECORD * row;
405     static const WCHAR template_s[]=
406         {'A','c','t','i','o','n',' ','s','t','a','r','t',' ','%','s',':',' ',
407          '%','s', '.',0};
408     static const WCHAR template_e[]=
409         {'A','c','t','i','o','n',' ','e','n','d','e','d',' ','%','s',':',' ',
410          '%','s', '.',' ','R','e','t','u','r','n',' ','v','a','l','u','e',' ',
411          '%','i','.',0};
412     static const WCHAR format[] = 
413         {'H','H','\'',':','\'','m','m','\'',':','\'','s','s',0};
414     WCHAR message[1024];
415     WCHAR timet[0x100];
416
417     GetTimeFormatW(LOCALE_USER_DEFAULT, 0, NULL, format, timet, 0x100);
418     if (start)
419         sprintfW(message,template_s,timet,action);
420     else
421         sprintfW(message,template_e,timet,action,rc);
422     
423     row = MSI_CreateRecord(1);
424     MSI_RecordSetStringW(row,1,message);
425  
426     MSI_ProcessMessage(package, INSTALLMESSAGE_INFO, row);
427     msiobj_release(&row->hdr);
428 }
429
430 /****************************************************
431  * TOP level entry points 
432  *****************************************************/
433
434 UINT ACTION_DoTopLevelINSTALL(MSIPACKAGE *package, LPCWSTR szPackagePath,
435                               LPCWSTR szCommandLine, LPCWSTR msiFilePath)
436 {
437     DWORD sz;
438     WCHAR buffer[10];
439     UINT rc;
440     BOOL ui = FALSE;
441     static const WCHAR szUILevel[] = {'U','I','L','e','v','e','l',0};
442     static const WCHAR szAction[] = {'A','C','T','I','O','N',0};
443     static const WCHAR szInstall[] = {'I','N','S','T','A','L','L',0};
444
445     MSI_SetPropertyW(package, szAction, szInstall);
446
447     package->script = HeapAlloc(GetProcessHeap(),0,sizeof(MSISCRIPT));
448     memset(package->script,0,sizeof(MSISCRIPT));
449
450     package->script->InWhatSequence = SEQUENCE_INSTALL;
451
452     package->msiFilePath= strdupW(msiFilePath);
453
454     if (szPackagePath)   
455     {
456         LPWSTR p, check, path;
457  
458         package->PackagePath = strdupW(szPackagePath);
459         path = strdupW(szPackagePath);
460         p = strrchrW(path,'\\');    
461         if (p)
462         {
463             p++;
464             *p=0;
465         }
466         else
467         {
468             HeapFree(GetProcessHeap(),0,path);
469             path = HeapAlloc(GetProcessHeap(),0,MAX_PATH*sizeof(WCHAR));
470             GetCurrentDirectoryW(MAX_PATH,path);
471             strcatW(path,cszbs);
472         }
473
474         check = load_dynamic_property(package, cszSourceDir,NULL);
475         if (!check)
476             MSI_SetPropertyW(package, cszSourceDir, path);
477         else
478             HeapFree(GetProcessHeap(), 0, check);
479
480         HeapFree(GetProcessHeap(), 0, path);
481     }
482
483     if (szCommandLine)
484     {
485         LPWSTR ptr,ptr2;
486         ptr = (LPWSTR)szCommandLine;
487        
488         while (*ptr)
489         {
490             WCHAR *prop = NULL;
491             WCHAR *val = NULL;
492
493             TRACE("Looking at %s\n",debugstr_w(ptr));
494
495             ptr2 = strchrW(ptr,'=');
496             if (ptr2)
497             {
498                 BOOL quote=FALSE;
499                 DWORD len = 0;
500
501                 while (*ptr == ' ') ptr++;
502                 len = ptr2-ptr;
503                 prop = HeapAlloc(GetProcessHeap(),0,(len+1)*sizeof(WCHAR));
504                 memcpy(prop,ptr,len*sizeof(WCHAR));
505                 prop[len]=0;
506                 ptr2++;
507            
508                 len = 0; 
509                 ptr = ptr2; 
510                 while (*ptr && (quote || (!quote && *ptr!=' ')))
511                 {
512                     if (*ptr == '"')
513                         quote = !quote;
514                     ptr++;
515                     len++;
516                 }
517                
518                 if (*ptr2=='"')
519                 {
520                     ptr2++;
521                     len -= 2;
522                 }
523                 val = HeapAlloc(GetProcessHeap(),0,(len+1)*sizeof(WCHAR));
524                 memcpy(val,ptr2,len*sizeof(WCHAR));
525                 val[len] = 0;
526
527                 if (strlenW(prop) > 0)
528                 {
529                     TRACE("Found commandline property (%s) = (%s)\n", 
530                                        debugstr_w(prop), debugstr_w(val));
531                     MSI_SetPropertyW(package,prop,val);
532                 }
533                 HeapFree(GetProcessHeap(),0,val);
534                 HeapFree(GetProcessHeap(),0,prop);
535             }
536             ptr++;
537         }
538     }
539   
540     sz = 10; 
541     if (MSI_GetPropertyW(package,szUILevel,buffer,&sz) == ERROR_SUCCESS)
542     {
543         if (atoiW(buffer) >= INSTALLUILEVEL_REDUCED)
544         {
545             package->script->InWhatSequence |= SEQUENCE_UI;
546             rc = ACTION_ProcessUISequence(package);
547             ui = TRUE;
548             if (rc == ERROR_SUCCESS)
549             {
550                 package->script->InWhatSequence |= SEQUENCE_EXEC;
551                 rc = ACTION_ProcessExecSequence(package,TRUE);
552             }
553         }
554         else
555             rc = ACTION_ProcessExecSequence(package,FALSE);
556     }
557     else
558         rc = ACTION_ProcessExecSequence(package,FALSE);
559     
560     if (rc == -1)
561     {
562         /* install was halted but should be considered a success */
563         rc = ERROR_SUCCESS;
564     }
565
566     package->script->CurrentlyScripting= FALSE;
567
568     /* process the ending type action */
569     if (rc == ERROR_SUCCESS)
570         ACTION_PerformActionSequence(package,-1,ui);
571     else if (rc == ERROR_INSTALL_USEREXIT) 
572         ACTION_PerformActionSequence(package,-2,ui);
573     else if (rc == ERROR_INSTALL_SUSPEND) 
574         ACTION_PerformActionSequence(package,-4,ui);
575     else  /* failed */
576         ACTION_PerformActionSequence(package,-3,ui);
577
578     /* finish up running custom actions */
579     ACTION_FinishCustomActions(package);
580     
581     return rc;
582 }
583
584 static UINT ACTION_PerformActionSequence(MSIPACKAGE *package, UINT seq, BOOL UI)
585 {
586     UINT rc = ERROR_SUCCESS;
587     MSIRECORD * row = 0;
588     static const WCHAR ExecSeqQuery[] =
589         {'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
590          '`','I','n','s','t','a','l','l','E','x','e','c','u','t','e',
591          'S','e','q','u','e','n','c','e','`',' ', 'W','H','E','R','E',' ',
592          '`','S','e','q','u','e','n','c','e','`',' ', '=',' ','%','i',0};
593
594     static const WCHAR UISeqQuery[] =
595         {'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
596      '`','I','n','s','t','a','l','l','U','I','S','e','q','u','e','n','c','e',
597      '`', ' ', 'W','H','E','R','E',' ','`','S','e','q','u','e','n','c','e','`',
598          ' ', '=',' ','%','i',0};
599
600     if (UI)
601         row = MSI_QueryGetRecord(package->db, UISeqQuery, seq);
602     else
603         row = MSI_QueryGetRecord(package->db, ExecSeqQuery, seq);
604
605     if (row)
606     {
607         LPCWSTR action, cond;
608
609         TRACE("Running the actions\n"); 
610
611         /* check conditions */
612         cond = MSI_RecordGetString(row,2);
613         if (cond)
614         {
615             /* this is a hack to skip errors in the condition code */
616             if (MSI_EvaluateConditionW(package, cond) == MSICONDITION_FALSE)
617                 goto end;
618         }
619
620         action = MSI_RecordGetString(row,1);
621         if (!action)
622         {
623             ERR("failed to fetch action\n");
624             rc = ERROR_FUNCTION_FAILED;
625             goto end;
626         }
627
628         if (UI)
629             rc = ACTION_PerformUIAction(package,action);
630         else
631             rc = ACTION_PerformAction(package,action,FALSE);
632 end:
633         msiobj_release(&row->hdr);
634     }
635     else
636         rc = ERROR_SUCCESS;
637
638     return rc;
639 }
640
641 typedef struct {
642     MSIPACKAGE* package;
643     BOOL UI;
644 } iterate_action_param;
645
646 static UINT ITERATE_Actions(MSIRECORD *row, LPVOID param)
647 {
648     iterate_action_param *iap= (iterate_action_param*)param;
649     UINT rc;
650     LPCWSTR cond, action;
651
652     action = MSI_RecordGetString(row,1);
653     if (!action)
654     {
655         ERR("Error is retrieving action name\n");
656         return  ERROR_FUNCTION_FAILED;
657     }
658
659     /* check conditions */
660     cond = MSI_RecordGetString(row,2);
661     if (cond)
662     {
663         /* this is a hack to skip errors in the condition code */
664         if (MSI_EvaluateConditionW(iap->package, cond) == MSICONDITION_FALSE)
665         {
666             TRACE("Skipping action: %s (condition is false)\n",
667                             debugstr_w(action));
668             return ERROR_SUCCESS;
669         }
670     }
671
672     if (iap->UI)
673         rc = ACTION_PerformUIAction(iap->package,action);
674     else
675         rc = ACTION_PerformAction(iap->package,action,FALSE);
676
677     if (rc == ERROR_FUNCTION_NOT_CALLED)
678         rc = ERROR_SUCCESS;
679
680     if (rc != ERROR_SUCCESS)
681         ERR("Execution halted due to error (%i)\n",rc);
682
683     return rc;
684 }
685
686 static UINT ACTION_ProcessExecSequence(MSIPACKAGE *package, BOOL UIran)
687 {
688     MSIQUERY * view;
689     UINT rc;
690     static const WCHAR ExecSeqQuery[] =
691         {'S','E','L','E','C','T',' ','*',' ', 'F','R','O','M',' ',
692          '`','I','n','s','t','a','l','l','E','x','e','c','u','t','e',
693          'S','e','q','u','e','n','c','e','`',' ', 'W','H','E','R','E',' ',
694          '`','S','e','q','u','e','n','c','e','`',' ', '>',' ','%','i',' ',
695          'O','R','D','E','R',' ', 'B','Y',' ',
696          '`','S','e','q','u','e','n','c','e','`',0 };
697     MSIRECORD * row = 0;
698     static const WCHAR IVQuery[] =
699         {'S','E','L','E','C','T',' ','`','S','e','q','u','e','n','c','e','`',
700          ' ', 'F','R','O','M',' ','`','I','n','s','t','a','l','l',
701          'E','x','e','c','u','t','e','S','e','q','u','e','n','c','e','`',' ',
702          'W','H','E','R','E',' ','`','A','c','t','i','o','n','`',' ','=',
703          ' ','\'', 'I','n','s','t','a','l','l',
704          'V','a','l','i','d','a','t','e','\'', 0};
705     INT seq = 0;
706     iterate_action_param iap;
707
708     iap.package = package;
709     iap.UI = FALSE;
710
711     if (package->script->ExecuteSequenceRun)
712     {
713         TRACE("Execute Sequence already Run\n");
714         return ERROR_SUCCESS;
715     }
716
717     package->script->ExecuteSequenceRun = TRUE;
718
719     /* get the sequence number */
720     if (UIran)
721     {
722         row = MSI_QueryGetRecord(package->db, IVQuery);
723         if( !row )
724             return ERROR_FUNCTION_FAILED;
725         seq = MSI_RecordGetInteger(row,1);
726         msiobj_release(&row->hdr);
727     }
728
729     rc = MSI_OpenQuery(package->db, &view, ExecSeqQuery, seq);
730     if (rc == ERROR_SUCCESS)
731     {
732         TRACE("Running the actions\n");
733
734         rc = MSI_IterateRecords(view, NULL, ITERATE_Actions, &iap);
735         msiobj_release(&view->hdr);
736     }
737
738     return rc;
739 }
740
741 static UINT ACTION_ProcessUISequence(MSIPACKAGE *package)
742 {
743     MSIQUERY * view;
744     UINT rc;
745     static const WCHAR ExecSeqQuery [] =
746         {'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
747          '`','I','n','s','t','a','l','l',
748          'U','I','S','e','q','u','e','n','c','e','`',
749          ' ','W','H','E','R','E',' ', 
750          '`','S','e','q','u','e','n','c','e','`',' ',
751          '>',' ','0',' ','O','R','D','E','R',' ','B','Y',' ',
752          '`','S','e','q','u','e','n','c','e','`',0};
753     iterate_action_param iap;
754
755     iap.package = package;
756     iap.UI = TRUE;
757
758     rc = MSI_DatabaseOpenViewW(package->db, ExecSeqQuery, &view);
759     
760     if (rc == ERROR_SUCCESS)
761     {
762         TRACE("Running the actions \n"); 
763
764         rc = MSI_IterateRecords(view, NULL, ITERATE_Actions, &iap);
765         msiobj_release(&view->hdr);
766     }
767
768     return rc;
769 }
770
771 /********************************************************
772  * ACTION helper functions and functions that perform the actions
773  *******************************************************/
774 static BOOL ACTION_HandleStandardAction(MSIPACKAGE *package, LPCWSTR action, 
775                                         UINT* rc, BOOL force )
776 {
777     BOOL ret = FALSE; 
778     BOOL run = force;
779     int i;
780
781     if (!run && !package->script->CurrentlyScripting)
782         run = TRUE;
783    
784     if (!run)
785     {
786         if (strcmpW(action,szInstallFinalize) == 0 ||
787             strcmpW(action,szInstallExecute) == 0 ||
788             strcmpW(action,szInstallExecuteAgain) == 0) 
789                 run = TRUE;
790     }
791     
792     i = 0;
793     while (StandardActions[i].action != NULL)
794     {
795         if (strcmpW(StandardActions[i].action, action)==0)
796         {
797             ce_actiontext(package, action);
798             if (!run)
799             {
800                 ui_actioninfo(package, action, TRUE, 0);
801                 *rc = schedule_action(package,INSTALL_SCRIPT,action);
802                 ui_actioninfo(package, action, FALSE, *rc);
803             }
804             else
805             {
806                 ui_actionstart(package, action);
807                 if (StandardActions[i].handler)
808                 {
809                     *rc = StandardActions[i].handler(package);
810                 }
811                 else
812                 {
813                     FIXME("UNHANDLED Standard Action %s\n",debugstr_w(action));
814                     *rc = ERROR_SUCCESS;
815                 }
816             }
817             ret = TRUE;
818             break;
819         }
820         i++;
821     }
822     return ret;
823 }
824
825 static BOOL ACTION_HandleDialogBox( MSIPACKAGE *package, LPCWSTR dialog, UINT* rc )
826 {
827     BOOL ret = FALSE;
828
829     if (ACTION_DialogBox(package,dialog) == ERROR_SUCCESS)
830     {
831         *rc = package->CurrentInstallState;
832         ret = TRUE;
833     }
834     return ret;
835 }
836
837 static BOOL ACTION_HandleCustomAction( MSIPACKAGE* package, LPCWSTR action,
838                                        UINT* rc, BOOL force )
839 {
840     BOOL ret=FALSE;
841     UINT arc;
842
843     arc = ACTION_CustomAction(package,action, force);
844
845     if (arc != ERROR_CALL_NOT_IMPLEMENTED)
846     {
847         *rc = arc;
848         ret = TRUE;
849     }
850     return ret;
851 }
852
853 /* 
854  * A lot of actions are really important even if they don't do anything
855  * explicit... Lots of properties are set at the beginning of the installation
856  * CostFinalize does a bunch of work to translate the directories and such
857  * 
858  * But until I get write access to the database that is hard, so I am going to
859  * hack it to see if I can get something to run.
860  */
861 UINT ACTION_PerformAction(MSIPACKAGE *package, const WCHAR *action, BOOL force)
862 {
863     UINT rc = ERROR_SUCCESS; 
864     BOOL handled;
865
866     TRACE("Performing action (%s)\n",debugstr_w(action));
867
868     handled = ACTION_HandleStandardAction(package, action, &rc, force);
869
870     if (!handled)
871         handled = ACTION_HandleCustomAction(package, action, &rc, force);
872
873     if (!handled)
874     {
875         FIXME("UNHANDLED MSI ACTION %s\n",debugstr_w(action));
876         rc = ERROR_FUNCTION_NOT_CALLED;
877     }
878
879     package->CurrentInstallState = rc;
880     return rc;
881 }
882
883 UINT ACTION_PerformUIAction(MSIPACKAGE *package, const WCHAR *action)
884 {
885     UINT rc = ERROR_SUCCESS;
886     BOOL handled = FALSE;
887
888     TRACE("Performing action (%s)\n",debugstr_w(action));
889
890     handled = ACTION_HandleStandardAction(package, action, &rc,TRUE);
891
892     if (!handled)
893         handled = ACTION_HandleCustomAction(package, action, &rc, FALSE);
894
895     if (!handled)
896         handled = ACTION_HandleDialogBox(package, action, &rc);
897
898     msi_dialog_check_messages( NULL );
899
900     if (!handled)
901     {
902         FIXME("UNHANDLED MSI ACTION %s\n",debugstr_w(action));
903         rc = ERROR_FUNCTION_NOT_CALLED;
904     }
905
906     package->CurrentInstallState = rc;
907     return rc;
908 }
909
910
911 /*
912  * Actual Action Handlers
913  */
914
915 static UINT ITERATE_CreateFolders(MSIRECORD *row, LPVOID param)
916 {
917     MSIPACKAGE *package = (MSIPACKAGE*)param;
918     LPCWSTR dir;
919     LPWSTR full_path;
920     MSIRECORD *uirow;
921     MSIFOLDER *folder;
922
923     dir = MSI_RecordGetString(row,1);
924     if (!dir)
925     {
926         ERR("Unable to get folder id \n");
927         return ERROR_SUCCESS;
928     }
929
930     full_path = resolve_folder(package,dir,FALSE,FALSE,&folder);
931     if (!full_path)
932     {
933         ERR("Unable to resolve folder id %s\n",debugstr_w(dir));
934         return ERROR_SUCCESS;
935     }
936
937     TRACE("Folder is %s\n",debugstr_w(full_path));
938
939     /* UI stuff */
940     uirow = MSI_CreateRecord(1);
941     MSI_RecordSetStringW(uirow,1,full_path);
942     ui_actiondata(package,szCreateFolders,uirow);
943     msiobj_release( &uirow->hdr );
944
945     if (folder->State == 0)
946         create_full_pathW(full_path);
947
948     folder->State = 3;
949
950     HeapFree(GetProcessHeap(),0,full_path);
951     return ERROR_SUCCESS;
952 }
953
954
955 /*
956  * Also we cannot enable/disable components either, so for now I am just going 
957  * to do all the directories for all the components.
958  */
959 static UINT ACTION_CreateFolders(MSIPACKAGE *package)
960 {
961     static const WCHAR ExecSeqQuery[] =
962         {'S','E','L','E','C','T',' ',
963          '`','D','i','r','e','c','t','o','r','y','_','`',
964          ' ','F','R','O','M',' ',
965          '`','C','r','e','a','t','e','F','o','l','d','e','r','`',0 };
966     UINT rc;
967     MSIQUERY *view;
968
969     rc = MSI_DatabaseOpenViewW(package->db, ExecSeqQuery, &view );
970     if (rc != ERROR_SUCCESS)
971         return ERROR_SUCCESS;
972
973     rc = MSI_IterateRecords(view, NULL, ITERATE_CreateFolders, package);
974     msiobj_release(&view->hdr);
975    
976     return rc;
977 }
978
979 static int load_component(MSIPACKAGE* package, MSIRECORD * row)
980 {
981     int index = package->loaded_components;
982     DWORD sz;
983
984     /* fill in the data */
985
986     package->loaded_components++;
987     if (package->loaded_components == 1)
988         package->components = HeapAlloc(GetProcessHeap(),0,
989                                         sizeof(MSICOMPONENT));
990     else
991         package->components = HeapReAlloc(GetProcessHeap(),0,
992             package->components, package->loaded_components * 
993             sizeof(MSICOMPONENT));
994
995     memset(&package->components[index],0,sizeof(MSICOMPONENT));
996
997     sz = IDENTIFIER_SIZE;       
998     MSI_RecordGetStringW(row,1,package->components[index].Component,&sz);
999
1000     TRACE("Loading Component %s\n",
1001            debugstr_w(package->components[index].Component));
1002
1003     sz = 0x100;
1004     if (!MSI_RecordIsNull(row,2))
1005         MSI_RecordGetStringW(row,2,package->components[index].ComponentId,&sz);
1006             
1007     sz = IDENTIFIER_SIZE;       
1008     MSI_RecordGetStringW(row,3,package->components[index].Directory,&sz);
1009
1010     package->components[index].Attributes = MSI_RecordGetInteger(row,4);
1011
1012     sz = 0x100;       
1013     MSI_RecordGetStringW(row,5,package->components[index].Condition,&sz);
1014
1015     sz = IDENTIFIER_SIZE;       
1016     MSI_RecordGetStringW(row,6,package->components[index].KeyPath,&sz);
1017
1018     package->components[index].Installed = INSTALLSTATE_ABSENT;
1019     package->components[index].Action = INSTALLSTATE_UNKNOWN;
1020     package->components[index].ActionRequest = INSTALLSTATE_UNKNOWN;
1021
1022     package->components[index].Enabled = TRUE;
1023
1024     return index;
1025 }
1026
1027 typedef struct {
1028     MSIPACKAGE *package;
1029     INT index;
1030     INT cnt;
1031 } _ilfs;
1032
1033 static UINT iterate_component_check(MSIRECORD *row, LPVOID param)
1034 {
1035     _ilfs* ilfs= (_ilfs*)param;
1036     INT c_indx;
1037
1038     c_indx = load_component(ilfs->package,row);
1039
1040     ilfs->package->features[ilfs->index].Components[ilfs->cnt] = c_indx;
1041     ilfs->package->features[ilfs->index].ComponentCount ++;
1042     TRACE("Loaded new component to index %i\n",c_indx);
1043
1044     return ERROR_SUCCESS;
1045 }
1046
1047 static UINT iterate_load_featurecomponents(MSIRECORD *row, LPVOID param)
1048 {
1049     _ilfs* ilfs= (_ilfs*)param;
1050     LPCWSTR component;
1051     DWORD rc;
1052     INT c_indx;
1053     INT cnt = ilfs->package->features[ilfs->index].ComponentCount;
1054     MSIQUERY * view;
1055     static const WCHAR Query[] = 
1056         {'S','E','L','E','C','T',' ','*',' ','F','R', 'O','M',' ', 
1057          '`','C','o','m','p','o','n','e','n','t','`',' ',
1058          'W','H','E','R','E',' ', 
1059          '`','C','o','m','p','o','n','e','n','t','`',' ',
1060          '=','\'','%','s','\'',0};
1061
1062     component = MSI_RecordGetString(row,1);
1063
1064     /* check to see if the component is already loaded */
1065     c_indx = get_loaded_component(ilfs->package,component);
1066     if (c_indx != -1)
1067     {
1068         TRACE("Component %s already loaded at %i\n", debugstr_w(component),
1069                         c_indx);
1070         ilfs->package->features[ilfs->index].Components[cnt] = c_indx;
1071         ilfs->package->features[ilfs->index].ComponentCount ++;
1072         return ERROR_SUCCESS;
1073     }
1074
1075     rc = MSI_OpenQuery(ilfs->package->db, &view, Query, component);
1076     if (rc != ERROR_SUCCESS)
1077         return ERROR_SUCCESS;
1078
1079     ilfs->cnt = cnt;
1080     rc = MSI_IterateRecords(view, NULL, iterate_component_check, ilfs);
1081     msiobj_release( &view->hdr );
1082
1083     return ERROR_SUCCESS;
1084 }
1085
1086 static UINT load_feature(MSIRECORD * row, LPVOID param)
1087 {
1088     MSIPACKAGE* package = (MSIPACKAGE*)param;
1089     int index = package->loaded_features;
1090     DWORD sz;
1091     static const WCHAR Query1[] = 
1092         {'S','E','L','E','C','T',' ',
1093          '`','C','o','m','p','o','n','e','n','t','_','`',
1094          ' ','F','R','O','M',' ','`','F','e','a','t','u','r','e',
1095          'C','o','m','p','o','n','e','n','t','s','`',' ',
1096          'W','H','E','R','E',' ',
1097          '`','F','e', 'a','t','u','r','e','_','`',' ','=','\'','%','s','\'',0};
1098     MSIQUERY * view;
1099     UINT    rc;
1100     _ilfs ilfs;
1101
1102     ilfs.package = package;
1103     ilfs.index = index;
1104
1105     /* fill in the data */
1106
1107     package->loaded_features ++;
1108     if (package->loaded_features == 1)
1109         package->features = HeapAlloc(GetProcessHeap(),0,sizeof(MSIFEATURE));
1110     else
1111         package->features = HeapReAlloc(GetProcessHeap(),0,package->features,
1112                                 package->loaded_features * sizeof(MSIFEATURE));
1113
1114     memset(&package->features[index],0,sizeof(MSIFEATURE));
1115     
1116     sz = IDENTIFIER_SIZE;       
1117     MSI_RecordGetStringW(row,1,package->features[index].Feature,&sz);
1118
1119     TRACE("Loading feature %s\n",debugstr_w(package->features[index].Feature));
1120
1121     sz = IDENTIFIER_SIZE;
1122     if (!MSI_RecordIsNull(row,2))
1123         MSI_RecordGetStringW(row,2,package->features[index].Feature_Parent,&sz);
1124
1125     sz = 0x100;
1126      if (!MSI_RecordIsNull(row,3))
1127         MSI_RecordGetStringW(row,3,package->features[index].Title,&sz);
1128
1129      sz = 0x100;
1130      if (!MSI_RecordIsNull(row,4))
1131         MSI_RecordGetStringW(row,4,package->features[index].Description,&sz);
1132
1133     if (!MSI_RecordIsNull(row,5))
1134         package->features[index].Display = MSI_RecordGetInteger(row,5);
1135   
1136     package->features[index].Level= MSI_RecordGetInteger(row,6);
1137
1138      sz = IDENTIFIER_SIZE;
1139      if (!MSI_RecordIsNull(row,7))
1140         MSI_RecordGetStringW(row,7,package->features[index].Directory,&sz);
1141
1142     package->features[index].Attributes= MSI_RecordGetInteger(row,8);
1143
1144     package->features[index].Installed = INSTALLSTATE_ABSENT;
1145     package->features[index].Action = INSTALLSTATE_UNKNOWN;
1146     package->features[index].ActionRequest = INSTALLSTATE_UNKNOWN;
1147
1148     /* load feature components */
1149
1150     rc = MSI_OpenQuery(package->db, &view, Query1, 
1151                     package->features[index].Feature);
1152     if (rc != ERROR_SUCCESS)
1153         return ERROR_SUCCESS;
1154
1155     MSI_IterateRecords(view, NULL, iterate_load_featurecomponents , &ilfs);
1156     msiobj_release(&view->hdr);
1157
1158     return ERROR_SUCCESS;
1159 }
1160
1161 static UINT load_file(MSIRECORD *row, LPVOID param)
1162 {
1163     MSIPACKAGE* package = (MSIPACKAGE*)param;
1164     DWORD index = package->loaded_files;
1165     LPCWSTR component;
1166
1167     /* fill in the data */
1168
1169     package->loaded_files++;
1170     if (package->loaded_files== 1)
1171         package->files = HeapAlloc(GetProcessHeap(),0,sizeof(MSIFILE));
1172     else
1173         package->files = HeapReAlloc(GetProcessHeap(),0,
1174             package->files , package->loaded_files * sizeof(MSIFILE));
1175
1176     memset(&package->files[index],0,sizeof(MSIFILE));
1177  
1178     package->files[index].File = load_dynamic_stringW(row, 1);
1179
1180     component = MSI_RecordGetString(row, 2);
1181     package->files[index].ComponentIndex = get_loaded_component(package,
1182                     component);
1183
1184     if (package->files[index].ComponentIndex == -1)
1185         ERR("Unfound Component %s\n",debugstr_w(component));
1186
1187     package->files[index].FileName = load_dynamic_stringW(row,3);
1188     reduce_to_longfilename(package->files[index].FileName);
1189
1190     package->files[index].ShortName = load_dynamic_stringW(row,3);
1191     reduce_to_shortfilename(package->files[index].ShortName);
1192     
1193     package->files[index].FileSize = MSI_RecordGetInteger(row,4);
1194     package->files[index].Version = load_dynamic_stringW(row, 5);
1195     package->files[index].Language = load_dynamic_stringW(row, 6);
1196     package->files[index].Attributes= MSI_RecordGetInteger(row,7);
1197     package->files[index].Sequence= MSI_RecordGetInteger(row,8);
1198
1199     package->files[index].Temporary = FALSE;
1200     package->files[index].State = 0;
1201
1202     TRACE("File Loaded (%s)\n",debugstr_w(package->files[index].File));  
1203  
1204     return ERROR_SUCCESS;
1205 }
1206
1207 static UINT load_all_files(MSIPACKAGE *package)
1208 {
1209     MSIQUERY * view;
1210     UINT rc;
1211     static const WCHAR Query[] =
1212         {'S','E','L','E','C','T',' ','*',' ', 'F','R','O','M',' ',
1213          '`','F','i','l','e','`',' ', 'O','R','D','E','R',' ','B','Y',' ',
1214          '`','S','e','q','u','e','n','c','e','`', 0};
1215
1216     if (!package)
1217         return ERROR_INVALID_HANDLE;
1218
1219     rc = MSI_DatabaseOpenViewW(package->db, Query, &view);
1220     if (rc != ERROR_SUCCESS)
1221         return ERROR_SUCCESS;
1222
1223     rc = MSI_IterateRecords(view, NULL, load_file, package);
1224     msiobj_release(&view->hdr);
1225
1226     return ERROR_SUCCESS;
1227 }
1228
1229
1230 /*
1231  * I am not doing any of the costing functionality yet. 
1232  * Mostly looking at doing the Component and Feature loading
1233  *
1234  * The native MSI does A LOT of modification to tables here. Mostly adding
1235  * a lot of temporary columns to the Feature and Component tables. 
1236  *
1237  *    note: Native msi also tracks the short filename. But I am only going to
1238  *          track the long ones.  Also looking at this directory table
1239  *          it appears that the directory table does not get the parents
1240  *          resolved base on property only based on their entries in the 
1241  *          directory table.
1242  */
1243 static UINT ACTION_CostInitialize(MSIPACKAGE *package)
1244 {
1245     MSIQUERY * view;
1246     UINT rc;
1247     static const WCHAR Query_all[] =
1248         {'S','E','L','E','C','T',' ','*',' ', 'F','R','O','M',' ',
1249          '`','F','e','a','t','u','r','e','`',0};
1250     static const WCHAR szCosting[] =
1251         {'C','o','s','t','i','n','g','C','o','m','p','l','e','t','e',0 };
1252     static const WCHAR szZero[] = { '0', 0 };
1253     WCHAR buffer[3];
1254     DWORD sz = 3;
1255
1256     MSI_GetPropertyW(package, szCosting, buffer, &sz);
1257     if (buffer[0]=='1')
1258         return ERROR_SUCCESS;
1259     
1260     MSI_SetPropertyW(package, szCosting, szZero);
1261     MSI_SetPropertyW(package, cszRootDrive , c_colon);
1262
1263     rc = MSI_DatabaseOpenViewW(package->db,Query_all,&view);
1264     if (rc != ERROR_SUCCESS)
1265         return rc;
1266
1267     rc = MSI_IterateRecords(view, NULL, load_feature, package);
1268     msiobj_release(&view->hdr);
1269
1270     load_all_files(package);
1271
1272     return ERROR_SUCCESS;
1273 }
1274
1275 static UINT execute_script(MSIPACKAGE *package, UINT script )
1276 {
1277     int i;
1278     UINT rc = ERROR_SUCCESS;
1279
1280     TRACE("Executing Script %i\n",script);
1281
1282     for (i = 0; i < package->script->ActionCount[script]; i++)
1283     {
1284         LPWSTR action;
1285         action = package->script->Actions[script][i];
1286         ui_actionstart(package, action);
1287         TRACE("Executing Action (%s)\n",debugstr_w(action));
1288         rc = ACTION_PerformAction(package, action, TRUE);
1289         HeapFree(GetProcessHeap(),0,package->script->Actions[script][i]);
1290         if (rc != ERROR_SUCCESS)
1291             break;
1292     }
1293     HeapFree(GetProcessHeap(),0,package->script->Actions[script]);
1294
1295     package->script->ActionCount[script] = 0;
1296     package->script->Actions[script] = NULL;
1297     return rc;
1298 }
1299
1300 static UINT ACTION_FileCost(MSIPACKAGE *package)
1301 {
1302     return ERROR_SUCCESS;
1303 }
1304
1305
1306 static INT load_folder(MSIPACKAGE *package, const WCHAR* dir)
1307 {
1308     static const WCHAR Query[] =
1309         {'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
1310          '`','D','i','r','e','c', 't','o','r','y','`',' ',
1311          'W','H','E','R','E',' ', '`', 'D','i','r','e','c','t', 'o','r','y','`',
1312          ' ','=',' ','\'','%','s','\'',
1313          0};
1314     LPWSTR ptargetdir, targetdir, srcdir;
1315     LPCWSTR parent;
1316     LPWSTR shortname = NULL;
1317     MSIRECORD * row = 0;
1318     INT index = -1;
1319     DWORD i;
1320
1321     TRACE("Looking for dir %s\n",debugstr_w(dir));
1322
1323     for (i = 0; i < package->loaded_folders; i++)
1324     {
1325         if (strcmpW(package->folders[i].Directory,dir)==0)
1326         {
1327             TRACE(" %s retuning on index %lu\n",debugstr_w(dir),i);
1328             return i;
1329         }
1330     }
1331
1332     TRACE("Working to load %s\n",debugstr_w(dir));
1333
1334     index = package->loaded_folders++;
1335     if (package->loaded_folders==1)
1336         package->folders = HeapAlloc(GetProcessHeap(),0,
1337                                         sizeof(MSIFOLDER));
1338     else
1339         package->folders= HeapReAlloc(GetProcessHeap(),0,
1340             package->folders, package->loaded_folders* 
1341             sizeof(MSIFOLDER));
1342
1343     memset(&package->folders[index],0,sizeof(MSIFOLDER));
1344
1345     package->folders[index].Directory = strdupW(dir);
1346
1347     row = MSI_QueryGetRecord(package->db, Query, dir);
1348     if (!row)
1349         return -1;
1350
1351     ptargetdir = targetdir = load_dynamic_stringW(row,3);
1352
1353     /* split src and target dir */
1354     if (strchrW(targetdir,':'))
1355     {
1356         srcdir=strchrW(targetdir,':');
1357         *srcdir=0;
1358         srcdir ++;
1359     }
1360     else
1361         srcdir=NULL;
1362
1363     /* for now only pick long filename versions */
1364     if (strchrW(targetdir,'|'))
1365     {
1366         shortname = targetdir;
1367         targetdir = strchrW(targetdir,'|'); 
1368         *targetdir = 0;
1369         targetdir ++;
1370     }
1371     /* for the sourcedir pick the short filename */
1372     if (srcdir && strchrW(srcdir,'|'))
1373     {
1374         LPWSTR p = strchrW(srcdir,'|'); 
1375         *p = 0;
1376     }
1377
1378     /* now check for root dirs */
1379     if (targetdir[0] == '.' && targetdir[1] == 0)
1380         targetdir = NULL;
1381         
1382     if (targetdir)
1383     {
1384         TRACE("   TargetDefault = %s\n",debugstr_w(targetdir));
1385         HeapFree(GetProcessHeap(),0, package->folders[index].TargetDefault);
1386         package->folders[index].TargetDefault = strdupW(targetdir);
1387     }
1388
1389     if (srcdir)
1390         package->folders[index].SourceDefault = strdupW(srcdir);
1391     else if (shortname)
1392         package->folders[index].SourceDefault = strdupW(shortname);
1393     else if (targetdir)
1394         package->folders[index].SourceDefault = strdupW(targetdir);
1395     HeapFree(GetProcessHeap(), 0, ptargetdir);
1396         TRACE("   SourceDefault = %s\n",debugstr_w(package->folders[index].SourceDefault));
1397
1398     parent = MSI_RecordGetString(row,2);
1399     if (parent) 
1400     {
1401         i = load_folder(package,parent);
1402         package->folders[index].ParentIndex = i;
1403         TRACE("Parent is index %i... %s %s\n",
1404                     package->folders[index].ParentIndex,
1405         debugstr_w(package->folders[package->folders[index].ParentIndex].Directory),
1406                     debugstr_w(parent));
1407     }
1408     else
1409         package->folders[index].ParentIndex = -2;
1410
1411     package->folders[index].Property = load_dynamic_property(package, dir,NULL);
1412
1413     msiobj_release(&row->hdr);
1414     TRACE(" %s retuning on index %i\n",debugstr_w(dir),index);
1415     return index;
1416 }
1417
1418 /* scan for and update current install states */
1419 static void ACTION_UpdateInstallStates(MSIPACKAGE *package)
1420 {
1421     int i;
1422     LPWSTR productcode;
1423
1424     productcode = load_dynamic_property(package,szProductCode,NULL);
1425
1426     for (i = 0; i < package->loaded_components; i++)
1427     {
1428         INSTALLSTATE res;
1429         res = MsiGetComponentPathW(productcode, 
1430                         package->components[i].ComponentId , NULL, NULL);
1431         if (res < 0)
1432             res = INSTALLSTATE_ABSENT;
1433         package->components[i].Installed = res;
1434     }
1435
1436     for (i = 0; i < package->loaded_features; i++)
1437     {
1438         INSTALLSTATE res = -10;
1439         int j;
1440         for (j = 0; j < package->features[i].ComponentCount; j++)
1441         {
1442             MSICOMPONENT* component = &package->components[package->features[i].
1443                                                            Components[j]];
1444             if (res == -10)
1445                 res = component->Installed;
1446             else
1447             {
1448                 if (res == component->Installed)
1449                     continue;
1450
1451                 if (res != component->Installed)
1452                         res = INSTALLSTATE_INCOMPLETE;
1453             }
1454         }
1455     }
1456 }
1457
1458 static BOOL process_state_property (MSIPACKAGE* package, LPCWSTR property, 
1459                                     INSTALLSTATE state)
1460 {
1461     static const WCHAR all[]={'A','L','L',0};
1462     LPWSTR override = NULL;
1463     INT i;
1464     BOOL rc = FALSE;
1465
1466     override = load_dynamic_property(package, property, NULL);
1467     if (override)
1468     {
1469         rc = TRUE;
1470         for(i = 0; i < package->loaded_features; i++)
1471         {
1472             if (strcmpiW(override,all)==0)
1473             {
1474                 package->features[i].ActionRequest= state;
1475                 package->features[i].Action = state;
1476             }
1477             else
1478             {
1479                 LPWSTR ptr = override;
1480                 LPWSTR ptr2 = strchrW(override,',');
1481
1482                 while (ptr)
1483                 {
1484                     if ((ptr2 && 
1485                         strncmpW(ptr,package->features[i].Feature, ptr2-ptr)==0)
1486                         || (!ptr2 &&
1487                         strcmpW(ptr,package->features[i].Feature)==0))
1488                     {
1489                         package->features[i].ActionRequest= state;
1490                         package->features[i].Action = state;
1491                         break;
1492                     }
1493                     if (ptr2)
1494                     {
1495                         ptr=ptr2+1;
1496                         ptr2 = strchrW(ptr,',');
1497                     }
1498                     else
1499                         break;
1500                 }
1501             }
1502         }
1503         HeapFree(GetProcessHeap(),0,override);
1504     } 
1505
1506     return rc;
1507 }
1508
1509 static UINT SetFeatureStates(MSIPACKAGE *package)
1510 {
1511     LPWSTR level;
1512     INT install_level;
1513     DWORD i;
1514     INT j;
1515     static const WCHAR szlevel[] =
1516         {'I','N','S','T','A','L','L','L','E','V','E','L',0};
1517     static const WCHAR szAddLocal[] =
1518         {'A','D','D','L','O','C','A','L',0};
1519     static const WCHAR szRemove[] =
1520         {'R','E','M','O','V','E',0};
1521     BOOL override = FALSE;
1522
1523     /* I do not know if this is where it should happen.. but */
1524
1525     TRACE("Checking Install Level\n");
1526
1527     level = load_dynamic_property(package,szlevel,NULL);
1528     if (level)
1529     {
1530         install_level = atoiW(level);
1531         HeapFree(GetProcessHeap(), 0, level);
1532     }
1533     else
1534         install_level = 1;
1535
1536     /* ok hereis the _real_ rub
1537      * all these activation/deactivation things happen in order and things
1538      * later on the list override things earlier on the list.
1539      * 1) INSTALLLEVEL processing
1540      * 2) ADDLOCAL
1541      * 3) REMOVE
1542      * 4) ADDSOURCE
1543      * 5) ADDDEFAULT
1544      * 6) REINSTALL
1545      * 7) COMPADDLOCAL
1546      * 8) COMPADDSOURCE
1547      * 9) FILEADDLOCAL
1548      * 10) FILEADDSOURCE
1549      * 11) FILEADDDEFAULT
1550      * I have confirmed that if ADDLOCAL is stated then the INSTALLLEVEL is
1551      * ignored for all the features. seems strange, especially since it is not
1552      * documented anywhere, but it is how it works. 
1553      *
1554      * I am still ignoring a lot of these. But that is ok for now, ADDLOCAL and
1555      * REMOVE are the big ones, since we don't handle administrative installs
1556      * yet anyway.
1557      */
1558     override |= process_state_property(package,szAddLocal,INSTALLSTATE_LOCAL);
1559     override |= process_state_property(package,szRemove,INSTALLSTATE_ABSENT);
1560
1561     if (!override)
1562     {
1563         for(i = 0; i < package->loaded_features; i++)
1564         {
1565             BOOL feature_state = ((package->features[i].Level > 0) &&
1566                              (package->features[i].Level <= install_level));
1567
1568             if ((feature_state) && 
1569                (package->features[i].Action == INSTALLSTATE_UNKNOWN))
1570             {
1571                 if (package->features[i].Attributes & 
1572                                 msidbFeatureAttributesFavorSource)
1573                 {
1574                     package->features[i].ActionRequest = INSTALLSTATE_SOURCE;
1575                     package->features[i].Action = INSTALLSTATE_SOURCE;
1576                 }
1577                 else if (package->features[i].Attributes &
1578                                 msidbFeatureAttributesFavorAdvertise)
1579                 {
1580                     package->features[i].ActionRequest =INSTALLSTATE_ADVERTISED;
1581                     package->features[i].Action =INSTALLSTATE_ADVERTISED;
1582                 }
1583                 else
1584                 {
1585                     package->features[i].ActionRequest = INSTALLSTATE_LOCAL;
1586                     package->features[i].Action = INSTALLSTATE_LOCAL;
1587                 }
1588             }
1589         }
1590     }
1591     else
1592     {
1593         /* set the Preselected Property */
1594         static const WCHAR szPreselected[] = {'P','r','e','s','e','l','e','c','t','e','d',0};
1595         static const WCHAR szOne[] = { '1', 0 };
1596
1597         MSI_SetPropertyW(package,szPreselected,szOne);
1598     }
1599
1600     /*
1601      * now we want to enable or disable components base on feature 
1602     */
1603
1604     for(i = 0; i < package->loaded_features; i++)
1605     {
1606         MSIFEATURE* feature = &package->features[i];
1607         TRACE("Examining Feature %s (Installed %i, Action %i, Request %i)\n",
1608             debugstr_w(feature->Feature), feature->Installed, feature->Action,
1609             feature->ActionRequest);
1610
1611         for( j = 0; j < feature->ComponentCount; j++)
1612         {
1613             MSICOMPONENT* component = &package->components[
1614                                                     feature->Components[j]];
1615
1616             if (!component->Enabled)
1617             {
1618                 component->Action = INSTALLSTATE_UNKNOWN;
1619                 component->ActionRequest = INSTALLSTATE_UNKNOWN;
1620             }
1621             else
1622             {
1623                 if (feature->Action == INSTALLSTATE_LOCAL)
1624                 {
1625                     component->Action = INSTALLSTATE_LOCAL;
1626                     component->ActionRequest = INSTALLSTATE_LOCAL;
1627                 }
1628                 else if (feature->ActionRequest == INSTALLSTATE_SOURCE)
1629                 {
1630                     if ((component->Action == INSTALLSTATE_UNKNOWN) ||
1631                         (component->Action == INSTALLSTATE_ABSENT) ||
1632                         (component->Action == INSTALLSTATE_ADVERTISED))
1633                            
1634                     {
1635                         component->Action = INSTALLSTATE_SOURCE;
1636                         component->ActionRequest = INSTALLSTATE_SOURCE;
1637                     }
1638                 }
1639                 else if (feature->ActionRequest == INSTALLSTATE_ADVERTISED)
1640                 {
1641                     if ((component->Action == INSTALLSTATE_UNKNOWN) ||
1642                         (component->Action == INSTALLSTATE_ABSENT))
1643                            
1644                     {
1645                         component->Action = INSTALLSTATE_ADVERTISED;
1646                         component->ActionRequest = INSTALLSTATE_ADVERTISED;
1647                     }
1648                 }
1649                 else if (feature->ActionRequest == INSTALLSTATE_ABSENT)
1650                 {
1651                     if (component->Action == INSTALLSTATE_UNKNOWN)
1652                     {
1653                         component->Action = INSTALLSTATE_ABSENT;
1654                         component->ActionRequest = INSTALLSTATE_ABSENT;
1655                     }
1656                 }
1657             }
1658         }
1659     } 
1660
1661     for(i = 0; i < package->loaded_components; i++)
1662     {
1663         MSICOMPONENT* component= &package->components[i];
1664
1665         TRACE("Result: Component %s (Installed %i, Action %i, Request %i)\n",
1666             debugstr_w(component->Component), component->Installed, 
1667             component->Action, component->ActionRequest);
1668     }
1669
1670
1671     return ERROR_SUCCESS;
1672 }
1673
1674 static UINT ITERATE_CostFinalizeDirectories(MSIRECORD *row, LPVOID param)
1675 {
1676     MSIPACKAGE *package = (MSIPACKAGE*)param;
1677     LPCWSTR name;
1678     LPWSTR path;
1679
1680     name = MSI_RecordGetString(row,1);
1681
1682     /* This helper function now does ALL the work */
1683     TRACE("Dir %s ...\n",debugstr_w(name));
1684     load_folder(package,name);
1685     path = resolve_folder(package,name,FALSE,TRUE,NULL);
1686     TRACE("resolves to %s\n",debugstr_w(path));
1687     HeapFree( GetProcessHeap(), 0, path);
1688
1689     return ERROR_SUCCESS;
1690 }
1691
1692 static UINT ITERATE_CostFinalizeConditions(MSIRECORD *row, LPVOID param)
1693 {
1694     MSIPACKAGE *package = (MSIPACKAGE*)param;
1695     LPCWSTR Feature;
1696     int feature_index;
1697
1698     Feature = MSI_RecordGetString(row,1);
1699
1700     feature_index = get_loaded_feature(package,Feature);
1701     if (feature_index < 0)
1702         ERR("FAILED to find loaded feature %s\n",debugstr_w(Feature));
1703     else
1704     {
1705         LPCWSTR Condition;
1706         Condition = MSI_RecordGetString(row,3);
1707
1708         if (MSI_EvaluateConditionW(package,Condition) == MSICONDITION_TRUE)
1709         {
1710             int level = MSI_RecordGetInteger(row,2);
1711             TRACE("Reseting feature %s to level %i\n", debugstr_w(Feature),
1712                             level);
1713             package->features[feature_index].Level = level;
1714         }
1715     }
1716     return ERROR_SUCCESS;
1717 }
1718
1719
1720 /* 
1721  * A lot is done in this function aside from just the costing.
1722  * The costing needs to be implemented at some point but for now I am going
1723  * to focus on the directory building
1724  *
1725  */
1726 static UINT ACTION_CostFinalize(MSIPACKAGE *package)
1727 {
1728     static const WCHAR ExecSeqQuery[] =
1729         {'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
1730          '`','D','i','r','e','c','t','o','r','y','`',0};
1731     static const WCHAR ConditionQuery[] =
1732         {'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
1733          '`','C','o','n','d','i','t','i','o','n','`',0};
1734     static const WCHAR szCosting[] =
1735         {'C','o','s','t','i','n','g','C','o','m','p','l','e','t','e',0 };
1736     static const WCHAR szlevel[] =
1737         {'I','N','S','T','A','L','L','L','E','V','E','L',0};
1738     static const WCHAR szOne[] = { '1', 0 };
1739     UINT rc;
1740     MSIQUERY * view;
1741     DWORD i;
1742     LPWSTR level;
1743     DWORD sz = 3;
1744     WCHAR buffer[3];
1745
1746     MSI_GetPropertyW(package, szCosting, buffer, &sz);
1747     if (buffer[0]=='1')
1748         return ERROR_SUCCESS;
1749
1750     TRACE("Building Directory properties\n");
1751
1752     rc = MSI_DatabaseOpenViewW(package->db, ExecSeqQuery, &view);
1753     if (rc == ERROR_SUCCESS)
1754     {
1755         rc = MSI_IterateRecords(view, NULL, ITERATE_CostFinalizeDirectories,
1756                         package);
1757         msiobj_release(&view->hdr);
1758     }
1759
1760     TRACE("File calculations %i files\n",package->loaded_files);
1761
1762     for (i = 0; i < package->loaded_files; i++)
1763     {
1764         MSICOMPONENT* comp = NULL;
1765         MSIFILE* file= NULL;
1766
1767         file = &package->files[i];
1768         if (file->ComponentIndex >= 0)
1769             comp = &package->components[file->ComponentIndex];
1770
1771         if (file->Temporary == TRUE)
1772             continue;
1773
1774         if (comp)
1775         {
1776             LPWSTR p;
1777
1778             /* calculate target */
1779             p = resolve_folder(package, comp->Directory, FALSE, FALSE, NULL);
1780
1781             HeapFree(GetProcessHeap(),0,file->TargetPath);
1782
1783             TRACE("file %s is named %s\n",
1784                    debugstr_w(file->File),debugstr_w(file->FileName));       
1785
1786             file->TargetPath = build_directory_name(2, p, file->FileName);
1787
1788             HeapFree(GetProcessHeap(),0,p);
1789
1790             TRACE("file %s resolves to %s\n",
1791                    debugstr_w(file->File),debugstr_w(file->TargetPath));       
1792
1793             if (GetFileAttributesW(file->TargetPath) == INVALID_FILE_ATTRIBUTES)
1794             {
1795                 file->State = 1;
1796                 comp->Cost += file->FileSize;
1797             }
1798             else
1799             {
1800                 if (file->Version)
1801                 {
1802                     DWORD handle;
1803                     DWORD versize;
1804                     UINT sz;
1805                     LPVOID version;
1806                     static const WCHAR name[] = 
1807                         {'\\',0};
1808                     static const WCHAR name_fmt[] = 
1809                         {'%','u','.','%','u','.','%','u','.','%','u',0};
1810                     WCHAR filever[0x100];
1811                     VS_FIXEDFILEINFO *lpVer;
1812
1813                     TRACE("Version comparison.. \n");
1814                     versize = GetFileVersionInfoSizeW(file->TargetPath,&handle);
1815                     version = HeapAlloc(GetProcessHeap(),0,versize);
1816                     GetFileVersionInfoW(file->TargetPath, 0, versize, version);
1817
1818                     VerQueryValueW(version, name, (LPVOID*)&lpVer, &sz);
1819
1820                     sprintfW(filever,name_fmt,
1821                         HIWORD(lpVer->dwFileVersionMS),
1822                         LOWORD(lpVer->dwFileVersionMS),
1823                         HIWORD(lpVer->dwFileVersionLS),
1824                         LOWORD(lpVer->dwFileVersionLS));
1825
1826                     TRACE("new %s old %s\n", debugstr_w(file->Version),
1827                           debugstr_w(filever));
1828                     if (strcmpiW(filever,file->Version)<0)
1829                     {
1830                         file->State = 2;
1831                         FIXME("cost should be diff in size\n");
1832                         comp->Cost += file->FileSize;
1833                     }
1834                     else
1835                         file->State = 3;
1836                     HeapFree(GetProcessHeap(),0,version);
1837                 }
1838                 else
1839                     file->State = 3;
1840             }
1841         } 
1842     }
1843
1844     TRACE("Evaluating Condition Table\n");
1845
1846     rc = MSI_DatabaseOpenViewW(package->db, ConditionQuery, &view);
1847     if (rc == ERROR_SUCCESS)
1848     {
1849         rc = MSI_IterateRecords(view, NULL, ITERATE_CostFinalizeConditions,
1850                     package);
1851         msiobj_release(&view->hdr);
1852     }
1853
1854     TRACE("Enabling or Disabling Components\n");
1855     for (i = 0; i < package->loaded_components; i++)
1856     {
1857         if (package->components[i].Condition[0])
1858         {
1859             if (MSI_EvaluateConditionW(package,
1860                 package->components[i].Condition) == MSICONDITION_FALSE)
1861             {
1862                 TRACE("Disabling component %s\n",
1863                       debugstr_w(package->components[i].Component));
1864                 package->components[i].Enabled = FALSE;
1865             }
1866         }
1867     }
1868
1869     MSI_SetPropertyW(package,szCosting,szOne);
1870     /* set default run level if not set */
1871     level = load_dynamic_property(package,szlevel,NULL);
1872     if (!level)
1873         MSI_SetPropertyW(package,szlevel, szOne);
1874     else
1875         HeapFree(GetProcessHeap(),0,level);
1876
1877     ACTION_UpdateInstallStates(package);
1878
1879     return SetFeatureStates(package);
1880 }
1881
1882 /* OK this value is "interpreted" and then formatted based on the 
1883    first few characters */
1884 static LPSTR parse_value(MSIPACKAGE *package, LPCWSTR value, DWORD *type, 
1885                          DWORD *size)
1886 {
1887     LPSTR data = NULL;
1888     if (value[0]=='#' && value[1]!='#' && value[1]!='%')
1889     {
1890         if (value[1]=='x')
1891         {
1892             LPWSTR ptr;
1893             CHAR byte[5];
1894             LPWSTR deformated = NULL;
1895             int count;
1896
1897             deformat_string(package, &value[2], &deformated);
1898
1899             /* binary value type */
1900             ptr = deformated;
1901             *type = REG_BINARY;
1902             if (strlenW(ptr)%2)
1903                 *size = (strlenW(ptr)/2)+1;
1904             else
1905                 *size = strlenW(ptr)/2;
1906
1907             data = HeapAlloc(GetProcessHeap(),0,*size);
1908
1909             byte[0] = '0'; 
1910             byte[1] = 'x'; 
1911             byte[4] = 0; 
1912             count = 0;
1913             /* if uneven pad with a zero in front */
1914             if (strlenW(ptr)%2)
1915             {
1916                 byte[2]= '0';
1917                 byte[3]= *ptr;
1918                 ptr++;
1919                 data[count] = (BYTE)strtol(byte,NULL,0);
1920                 count ++;
1921                 TRACE("Uneven byte count\n");
1922             }
1923             while (*ptr)
1924             {
1925                 byte[2]= *ptr;
1926                 ptr++;
1927                 byte[3]= *ptr;
1928                 ptr++;
1929                 data[count] = (BYTE)strtol(byte,NULL,0);
1930                 count ++;
1931             }
1932             HeapFree(GetProcessHeap(),0,deformated);
1933
1934             TRACE("Data %li bytes(%i)\n",*size,count);
1935         }
1936         else
1937         {
1938             LPWSTR deformated;
1939             LPWSTR p;
1940             DWORD d = 0;
1941             deformat_string(package, &value[1], &deformated);
1942
1943             *type=REG_DWORD; 
1944             *size = sizeof(DWORD);
1945             data = HeapAlloc(GetProcessHeap(),0,*size);
1946             p = deformated;
1947             if (*p == '-')
1948                 p++;
1949             while (*p)
1950             {
1951                 if ( (*p < '0') || (*p > '9') )
1952                     break;
1953                 d *= 10;
1954                 d += (*p - '0');
1955                 p++;
1956             }
1957             if (deformated[0] == '-')
1958                 d = -d;
1959             *(LPDWORD)data = d;
1960             TRACE("DWORD %li\n",*(LPDWORD)data);
1961
1962             HeapFree(GetProcessHeap(),0,deformated);
1963         }
1964     }
1965     else
1966     {
1967         static const WCHAR szMulti[] = {'[','~',']',0};
1968         LPCWSTR ptr;
1969         *type=REG_SZ;
1970
1971         if (value[0]=='#')
1972         {
1973             if (value[1]=='%')
1974             {
1975                 ptr = &value[2];
1976                 *type=REG_EXPAND_SZ;
1977             }
1978             else
1979                 ptr = &value[1];
1980          }
1981          else
1982             ptr=value;
1983
1984         if (strstrW(value,szMulti))
1985             *type = REG_MULTI_SZ;
1986
1987         *size = deformat_string(package, ptr,(LPWSTR*)&data);
1988     }
1989     return data;
1990 }
1991
1992 static UINT ITERATE_WriteRegistryValues(MSIRECORD *row, LPVOID param)
1993 {
1994     MSIPACKAGE *package = (MSIPACKAGE*)param;
1995     static const WCHAR szHCR[] = 
1996         {'H','K','E','Y','_','C','L','A','S','S','E','S','_',
1997          'R','O','O','T','\\',0};
1998     static const WCHAR szHCU[] =
1999         {'H','K','E','Y','_','C','U','R','R','E','N','T','_',
2000          'U','S','E','R','\\',0};
2001     static const WCHAR szHLM[] =
2002         {'H','K','E','Y','_','L','O','C','A','L','_',
2003          'M','A','C','H','I','N','E','\\',0};
2004     static const WCHAR szHU[] =
2005         {'H','K','E','Y','_','U','S','E','R','S','\\',0};
2006
2007     LPSTR value_data = NULL;
2008     HKEY  root_key, hkey;
2009     DWORD type,size;
2010     LPWSTR  deformated;
2011     LPCWSTR szRoot, component, name, key, value;
2012     INT component_index;
2013     MSIRECORD * uirow;
2014     LPWSTR uikey;
2015     INT   root;
2016     BOOL check_first = FALSE;
2017     UINT rc;
2018
2019     ui_progress(package,2,0,0,0);
2020
2021     value = NULL;
2022     key = NULL;
2023     uikey = NULL;
2024     name = NULL;
2025
2026     component = MSI_RecordGetString(row, 6);
2027     component_index = get_loaded_component(package,component);
2028
2029     if (!ACTION_VerifyComponentForAction(package, component_index,
2030                             INSTALLSTATE_LOCAL))
2031     {
2032         TRACE("Skipping write due to disabled component %s\n",
2033                         debugstr_w(component));
2034
2035         package->components[component_index].Action =
2036                 package->components[component_index].Installed;
2037
2038         return ERROR_SUCCESS;
2039     }
2040
2041     package->components[component_index].Action = INSTALLSTATE_LOCAL;
2042
2043     name = MSI_RecordGetString(row, 4);
2044     if( MSI_RecordIsNull(row,5) && name )
2045     {
2046         /* null values can have special meanings */
2047         if (name[0]=='-' && name[1] == 0)
2048                 return ERROR_SUCCESS;
2049         else if ((name[0]=='+' && name[1] == 0) || 
2050                  (name[0] == '*' && name[1] == 0))
2051                 name = NULL;
2052         check_first = TRUE;
2053     }
2054
2055     root = MSI_RecordGetInteger(row,2);
2056     key = MSI_RecordGetString(row, 3);
2057
2058     /* get the root key */
2059     switch (root)
2060     {
2061         case -1: 
2062             {
2063                 static const WCHAR szALLUSER[] = {'A','L','L','U','S','E','R','S',0};
2064                 LPWSTR all_users = load_dynamic_property(package, szALLUSER, NULL);
2065                 if (all_users && all_users[0] == '1')
2066                 {
2067                     root_key = HKEY_LOCAL_MACHINE;
2068                     szRoot = szHLM;
2069                 }
2070                 else
2071                 {
2072                     root_key = HKEY_CURRENT_USER;
2073                     szRoot = szHCU;
2074                 }
2075                 HeapFree(GetProcessHeap(),0,all_users);
2076             }
2077                  break;
2078         case 0:  root_key = HKEY_CLASSES_ROOT; 
2079                  szRoot = szHCR;
2080                  break;
2081         case 1:  root_key = HKEY_CURRENT_USER;
2082                  szRoot = szHCU;
2083                  break;
2084         case 2:  root_key = HKEY_LOCAL_MACHINE;
2085                  szRoot = szHLM;
2086                  break;
2087         case 3:  root_key = HKEY_USERS; 
2088                  szRoot = szHU;
2089                  break;
2090         default:
2091                  ERR("Unknown root %i\n",root);
2092                  root_key=NULL;
2093                  szRoot = NULL;
2094                  break;
2095     }
2096     if (!root_key)
2097         return ERROR_SUCCESS;
2098
2099     deformat_string(package, key , &deformated);
2100     size = strlenW(deformated) + strlenW(szRoot) + 1;
2101     uikey = HeapAlloc(GetProcessHeap(), 0, size*sizeof(WCHAR));
2102     strcpyW(uikey,szRoot);
2103     strcatW(uikey,deformated);
2104
2105     if (RegCreateKeyW( root_key, deformated, &hkey))
2106     {
2107         ERR("Could not create key %s\n",debugstr_w(deformated));
2108         HeapFree(GetProcessHeap(),0,deformated);
2109         HeapFree(GetProcessHeap(),0,uikey);
2110         return ERROR_SUCCESS;
2111     }
2112     HeapFree(GetProcessHeap(),0,deformated);
2113
2114     value = MSI_RecordGetString(row,5);
2115     if (value)
2116         value_data = parse_value(package, value, &type, &size); 
2117     else
2118     {
2119         static const WCHAR szEmpty[] = {0};
2120         value_data = (LPSTR)strdupW(szEmpty);
2121         size = 0;
2122         type = REG_SZ;
2123     }
2124
2125     deformat_string(package, name, &deformated);
2126
2127     /* get the double nulls to terminate SZ_MULTI */
2128     if (type == REG_MULTI_SZ)
2129         size +=sizeof(WCHAR);
2130
2131     if (!check_first)
2132     {
2133         TRACE("Setting value %s of %s\n",debugstr_w(deformated),
2134                         debugstr_w(uikey));
2135         RegSetValueExW(hkey, deformated, 0, type, value_data, size);
2136     }
2137     else
2138     {
2139         DWORD sz = 0;
2140         rc = RegQueryValueExW(hkey, deformated, NULL, NULL, NULL, &sz);
2141         if (rc == ERROR_SUCCESS || rc == ERROR_MORE_DATA)
2142         {
2143             TRACE("value %s of %s checked already exists\n",
2144                             debugstr_w(deformated), debugstr_w(uikey));
2145         }
2146         else
2147         {
2148             TRACE("Checked and setting value %s of %s\n",
2149                             debugstr_w(deformated), debugstr_w(uikey));
2150             if (deformated || size)
2151                 RegSetValueExW(hkey, deformated, 0, type, value_data, size);
2152         }
2153     }
2154     RegCloseKey(hkey);
2155
2156     uirow = MSI_CreateRecord(3);
2157     MSI_RecordSetStringW(uirow,2,deformated);
2158     MSI_RecordSetStringW(uirow,1,uikey);
2159
2160     if (type == REG_SZ)
2161         MSI_RecordSetStringW(uirow,3,(LPWSTR)value_data);
2162     else
2163         MSI_RecordSetStringW(uirow,3,value);
2164
2165     ui_actiondata(package,szWriteRegistryValues,uirow);
2166     msiobj_release( &uirow->hdr );
2167
2168     HeapFree(GetProcessHeap(),0,value_data);
2169     HeapFree(GetProcessHeap(),0,deformated);
2170     HeapFree(GetProcessHeap(),0,uikey);
2171
2172     return ERROR_SUCCESS;
2173 }
2174
2175 static UINT ACTION_WriteRegistryValues(MSIPACKAGE *package)
2176 {
2177     UINT rc;
2178     MSIQUERY * view;
2179     static const WCHAR ExecSeqQuery[] =
2180         {'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
2181          '`','R','e','g','i','s','t','r','y','`',0 };
2182
2183     if (!package)
2184         return ERROR_INVALID_HANDLE;
2185
2186     rc = MSI_DatabaseOpenViewW(package->db, ExecSeqQuery, &view);
2187     if (rc != ERROR_SUCCESS)
2188         return ERROR_SUCCESS;
2189
2190     /* increment progress bar each time action data is sent */
2191     ui_progress(package,1,REG_PROGRESS_VALUE,1,0);
2192
2193     rc = MSI_IterateRecords(view, NULL, ITERATE_WriteRegistryValues, package);
2194
2195     msiobj_release(&view->hdr);
2196     return rc;
2197 }
2198
2199 static UINT ACTION_InstallInitialize(MSIPACKAGE *package)
2200 {
2201     package->script->CurrentlyScripting = TRUE;
2202
2203     return ERROR_SUCCESS;
2204 }
2205
2206
2207 static UINT ACTION_InstallValidate(MSIPACKAGE *package)
2208 {
2209     DWORD progress = 0;
2210     DWORD total = 0;
2211     static const WCHAR q1[]=
2212         {'S','E','L','E','C','T',' ','*',' ', 'F','R','O','M',' ',
2213          '`','R','e','g','i','s','t','r','y','`',0};
2214     UINT rc;
2215     MSIQUERY * view;
2216     MSIRECORD * row = 0;
2217     int i;
2218
2219     TRACE(" InstallValidate \n");
2220
2221     rc = MSI_DatabaseOpenViewW(package->db, q1, &view);
2222     if (rc != ERROR_SUCCESS)
2223         return ERROR_SUCCESS;
2224
2225     rc = MSI_ViewExecute(view, 0);
2226     if (rc != ERROR_SUCCESS)
2227     {
2228         MSI_ViewClose(view);
2229         msiobj_release(&view->hdr);
2230         return rc;
2231     }
2232     while (1)
2233     {
2234         rc = MSI_ViewFetch(view,&row);
2235         if (rc != ERROR_SUCCESS)
2236         {
2237             rc = ERROR_SUCCESS;
2238             break;
2239         }
2240         progress +=1;
2241
2242         msiobj_release(&row->hdr);
2243     }
2244     MSI_ViewClose(view);
2245     msiobj_release(&view->hdr);
2246
2247     total = total + progress * REG_PROGRESS_VALUE;
2248     total = total + package->loaded_components * COMPONENT_PROGRESS_VALUE;
2249     for (i=0; i < package->loaded_files; i++)
2250         total += package->files[i].FileSize;
2251     ui_progress(package,0,total,0,0);
2252
2253     for(i = 0; i < package->loaded_features; i++)
2254     {
2255         MSIFEATURE* feature = &package->features[i];
2256         TRACE("Feature: %s; Installed: %i; Action %i; Request %i\n",
2257             debugstr_w(feature->Feature), feature->Installed, feature->Action,
2258             feature->ActionRequest);
2259     }
2260     
2261     return ERROR_SUCCESS;
2262 }
2263
2264 static UINT ITERATE_LaunchConditions(MSIRECORD *row, LPVOID param)
2265 {
2266     MSIPACKAGE* package = (MSIPACKAGE*)param;
2267     LPCWSTR cond = NULL; 
2268     LPCWSTR message = NULL;
2269     static const WCHAR title[]=
2270         {'I','n','s','t','a','l','l',' ','F','a', 'i','l','e','d',0};
2271
2272     cond = MSI_RecordGetString(row,1);
2273
2274     if (MSI_EvaluateConditionW(package,cond) != MSICONDITION_TRUE)
2275     {
2276         LPWSTR deformated;
2277         message = MSI_RecordGetString(row,2);
2278         deformat_string(package,message,&deformated); 
2279         MessageBoxW(NULL,deformated,title,MB_OK);
2280         HeapFree(GetProcessHeap(),0,deformated);
2281         return ERROR_FUNCTION_FAILED;
2282     }
2283
2284     return ERROR_SUCCESS;
2285 }
2286
2287 static UINT ACTION_LaunchConditions(MSIPACKAGE *package)
2288 {
2289     UINT rc;
2290     MSIQUERY * view = NULL;
2291     static const WCHAR ExecSeqQuery[] =
2292         {'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
2293          '`','L','a','u','n','c','h','C','o','n','d','i','t','i','o','n','`',0};
2294
2295     TRACE("Checking launch conditions\n");
2296
2297     rc = MSI_DatabaseOpenViewW(package->db, ExecSeqQuery, &view);
2298     if (rc != ERROR_SUCCESS)
2299         return ERROR_SUCCESS;
2300
2301     rc = MSI_IterateRecords(view, NULL, ITERATE_LaunchConditions, package);
2302     msiobj_release(&view->hdr);
2303
2304     return rc;
2305 }
2306
2307 static LPWSTR resolve_keypath( MSIPACKAGE* package, INT
2308                             component_index)
2309 {
2310     MSICOMPONENT* cmp = &package->components[component_index];
2311
2312     if (cmp->KeyPath[0]==0)
2313     {
2314         LPWSTR p = resolve_folder(package,cmp->Directory,FALSE,FALSE,NULL);
2315         return p;
2316     }
2317     if (cmp->Attributes & msidbComponentAttributesRegistryKeyPath)
2318     {
2319         MSIRECORD * row = 0;
2320         UINT root,len;
2321         LPWSTR deformated,buffer,deformated_name;
2322         LPCWSTR key,name;
2323         static const WCHAR ExecSeqQuery[] =
2324             {'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
2325              '`','R','e','g','i','s','t','r','y','`',' ',
2326              'W','H','E','R','E',' ', '`','R','e','g','i','s','t','r','y','`',
2327              ' ','=',' ' ,'\'','%','s','\'',0 };
2328         static const WCHAR fmt[]={'%','0','2','i',':','\\','%','s','\\',0};
2329         static const WCHAR fmt2[]=
2330             {'%','0','2','i',':','\\','%','s','\\','%','s',0};
2331
2332         row = MSI_QueryGetRecord(package->db, ExecSeqQuery,cmp->KeyPath);
2333         if (!row)
2334             return NULL;
2335
2336         root = MSI_RecordGetInteger(row,2);
2337         key = MSI_RecordGetString(row, 3);
2338         name = MSI_RecordGetString(row, 4);
2339         deformat_string(package, key , &deformated);
2340         deformat_string(package, name, &deformated_name);
2341
2342         len = strlenW(deformated) + 6;
2343         if (deformated_name)
2344             len+=strlenW(deformated_name);
2345
2346         buffer = HeapAlloc(GetProcessHeap(),0, len *sizeof(WCHAR));
2347
2348         if (deformated_name)
2349             sprintfW(buffer,fmt2,root,deformated,deformated_name);
2350         else
2351             sprintfW(buffer,fmt,root,deformated);
2352
2353         HeapFree(GetProcessHeap(),0,deformated);
2354         HeapFree(GetProcessHeap(),0,deformated_name);
2355         msiobj_release(&row->hdr);
2356
2357         return buffer;
2358     }
2359     else if (cmp->Attributes & msidbComponentAttributesODBCDataSource)
2360     {
2361         FIXME("UNIMPLEMENTED keypath as ODBC Source\n");
2362         return NULL;
2363     }
2364     else
2365     {
2366         int j;
2367         j = get_loaded_file(package,cmp->KeyPath);
2368
2369         if (j>=0)
2370         {
2371             LPWSTR p = strdupW(package->files[j].TargetPath);
2372             return p;
2373         }
2374     }
2375     return NULL;
2376 }
2377
2378 static HKEY openSharedDLLsKey(void)
2379 {
2380     HKEY hkey=0;
2381     static const WCHAR path[] =
2382         {'S','o','f','t','w','a','r','e','\\',
2383          'M','i','c','r','o','s','o','f','t','\\',
2384          'W','i','n','d','o','w','s','\\',
2385          'C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
2386          'S','h','a','r','e','d','D','L','L','s',0};
2387
2388     RegCreateKeyW(HKEY_LOCAL_MACHINE,path,&hkey);
2389     return hkey;
2390 }
2391
2392 static UINT ACTION_GetSharedDLLsCount(LPCWSTR dll)
2393 {
2394     HKEY hkey;
2395     DWORD count=0;
2396     DWORD type;
2397     DWORD sz = sizeof(count);
2398     DWORD rc;
2399     
2400     hkey = openSharedDLLsKey();
2401     rc = RegQueryValueExW(hkey, dll, NULL, &type, (LPBYTE)&count, &sz);
2402     if (rc != ERROR_SUCCESS)
2403         count = 0;
2404     RegCloseKey(hkey);
2405     return count;
2406 }
2407
2408 static UINT ACTION_WriteSharedDLLsCount(LPCWSTR path, UINT count)
2409 {
2410     HKEY hkey;
2411
2412     hkey = openSharedDLLsKey();
2413     if (count > 0)
2414         RegSetValueExW(hkey,path,0,REG_DWORD,
2415                     (LPBYTE)&count,sizeof(count));
2416     else
2417         RegDeleteValueW(hkey,path);
2418     RegCloseKey(hkey);
2419     return count;
2420 }
2421
2422 /*
2423  * Return TRUE if the count should be written out and FALSE if not
2424  */
2425 static void ACTION_RefCountComponent( MSIPACKAGE* package, UINT index)
2426 {
2427     INT count = 0;
2428     BOOL write = FALSE;
2429     INT j;
2430
2431     /* only refcount DLLs */
2432     if (package->components[index].KeyPath[0]==0 || 
2433         package->components[index].Attributes & 
2434             msidbComponentAttributesRegistryKeyPath || 
2435         package->components[index].Attributes & 
2436             msidbComponentAttributesODBCDataSource)
2437         write = FALSE;
2438     else
2439     {
2440         count = ACTION_GetSharedDLLsCount(package->components[index].
2441                         FullKeypath);
2442         write = (count > 0);
2443
2444         if (package->components[index].Attributes & 
2445                     msidbComponentAttributesSharedDllRefCount)
2446             write = TRUE;
2447     }
2448
2449     /* increment counts */
2450     for (j = 0; j < package->loaded_features; j++)
2451     {
2452         int i;
2453
2454         if (!ACTION_VerifyFeatureForAction(package,j,INSTALLSTATE_LOCAL))
2455             continue;
2456
2457         for (i = 0; i < package->features[j].ComponentCount; i++)
2458         {
2459             if (package->features[j].Components[i] == index)
2460                 count++;
2461         }
2462     }
2463     /* decrement counts */
2464     for (j = 0; j < package->loaded_features; j++)
2465     {
2466         int i;
2467         if (!ACTION_VerifyFeatureForAction(package,j,INSTALLSTATE_ABSENT))
2468             continue;
2469
2470         for (i = 0; i < package->features[j].ComponentCount; i++)
2471         {
2472             if (package->features[j].Components[i] == index)
2473                 count--;
2474         }
2475     }
2476
2477     /* ref count all the files in the component */
2478     if (write)
2479         for (j = 0; j < package->loaded_files; j++)
2480         {
2481             if (package->files[j].Temporary)
2482                 continue;
2483             if (package->files[j].ComponentIndex == index)
2484                 ACTION_WriteSharedDLLsCount(package->files[j].TargetPath,count);
2485         }
2486     
2487     /* add a count for permenent */
2488     if (package->components[index].Attributes &
2489                                 msidbComponentAttributesPermanent)
2490         count ++;
2491     
2492     package->components[index].RefCount = count;
2493
2494     if (write)
2495         ACTION_WriteSharedDLLsCount(package->components[index].FullKeypath,
2496             package->components[index].RefCount);
2497 }
2498
2499 /*
2500  * Ok further analysis makes me think that this work is
2501  * actually done in the PublishComponents and PublishFeatures
2502  * step, and not here.  It appears like the keypath and all that is
2503  * resolved in this step, however actually written in the Publish steps.
2504  * But we will leave it here for now because it is unclear
2505  */
2506 static UINT ACTION_ProcessComponents(MSIPACKAGE *package)
2507 {
2508     LPWSTR productcode;
2509     WCHAR squished_pc[GUID_SIZE];
2510     WCHAR squished_cc[GUID_SIZE];
2511     UINT rc;
2512     DWORD i;
2513     HKEY hkey=0,hkey2=0;
2514
2515     if (!package)
2516         return ERROR_INVALID_HANDLE;
2517
2518     /* writes the Component and Features values to the registry */
2519     productcode = load_dynamic_property(package,szProductCode,&rc);
2520     if (!productcode)
2521         return rc;
2522
2523     rc = MSIREG_OpenComponents(&hkey);
2524     if (rc != ERROR_SUCCESS)
2525         goto end;
2526       
2527     squash_guid(productcode,squished_pc);
2528     ui_progress(package,1,COMPONENT_PROGRESS_VALUE,1,0);
2529     for (i = 0; i < package->loaded_components; i++)
2530     {
2531         ui_progress(package,2,0,0,0);
2532         if (package->components[i].ComponentId[0]!=0)
2533         {
2534             WCHAR *keypath = NULL;
2535             MSIRECORD * uirow;
2536
2537             squash_guid(package->components[i].ComponentId,squished_cc);
2538            
2539             keypath = resolve_keypath(package,i);
2540             package->components[i].FullKeypath = keypath;
2541
2542             /* do the refcounting */
2543             ACTION_RefCountComponent( package, i);
2544
2545             TRACE("Component %s (%s), Keypath=%s, RefCount=%i\n", 
2546                             debugstr_w(package->components[i].Component),
2547                             debugstr_w(squished_cc),
2548                             debugstr_w(package->components[i].FullKeypath), 
2549                             package->components[i].RefCount);
2550             /*
2551             * Write the keypath out if the component is to be registered
2552             * and delete the key if the component is to be deregistered
2553             */
2554             if (ACTION_VerifyComponentForAction(package, i,
2555                                     INSTALLSTATE_LOCAL))
2556             {
2557                 rc = RegCreateKeyW(hkey,squished_cc,&hkey2);
2558                 if (rc != ERROR_SUCCESS)
2559                     continue;
2560
2561                 if (keypath)
2562                 {
2563                     RegSetValueExW(hkey2,squished_pc,0,REG_SZ,(LPVOID)keypath,
2564                                 (strlenW(keypath)+1)*sizeof(WCHAR));
2565
2566                     if (package->components[i].Attributes & 
2567                                 msidbComponentAttributesPermanent)
2568                     {
2569                         static const WCHAR szPermKey[] =
2570                             { '0','0','0','0','0','0','0','0','0','0','0','0',
2571                               '0','0','0','0','0','0','0', '0','0','0','0','0',
2572                               '0','0','0','0','0','0','0','0',0};
2573
2574                         RegSetValueExW(hkey2,szPermKey,0,REG_SZ,
2575                                         (LPVOID)keypath,
2576                                         (strlenW(keypath)+1)*sizeof(WCHAR));
2577                     }
2578                     
2579                     RegCloseKey(hkey2);
2580         
2581                     /* UI stuff */
2582                     uirow = MSI_CreateRecord(3);
2583                     MSI_RecordSetStringW(uirow,1,productcode);
2584                     MSI_RecordSetStringW(uirow,2,package->components[i].
2585                                                             ComponentId);
2586                     MSI_RecordSetStringW(uirow,3,keypath);
2587                     ui_actiondata(package,szProcessComponents,uirow);
2588                     msiobj_release( &uirow->hdr );
2589                }
2590             }
2591             else if (ACTION_VerifyComponentForAction(package, i,
2592                                     INSTALLSTATE_ABSENT))
2593             {
2594                 DWORD res;
2595
2596                 rc = RegOpenKeyW(hkey,squished_cc,&hkey2);
2597                 if (rc != ERROR_SUCCESS)
2598                     continue;
2599
2600                 RegDeleteValueW(hkey2,squished_pc);
2601
2602                 /* if the key is empty delete it */
2603                 res = RegEnumKeyExW(hkey2,0,NULL,0,0,NULL,0,NULL);
2604                 RegCloseKey(hkey2);
2605                 if (res == ERROR_NO_MORE_ITEMS)
2606                     RegDeleteKeyW(hkey,squished_cc);
2607         
2608                 /* UI stuff */
2609                 uirow = MSI_CreateRecord(2);
2610                 MSI_RecordSetStringW(uirow,1,productcode);
2611                 MSI_RecordSetStringW(uirow,2,package->components[i].
2612                                 ComponentId);
2613                 ui_actiondata(package,szProcessComponents,uirow);
2614                 msiobj_release( &uirow->hdr );
2615             }
2616         }
2617     } 
2618 end:
2619     HeapFree(GetProcessHeap(), 0, productcode);
2620     RegCloseKey(hkey);
2621     return rc;
2622 }
2623
2624 typedef struct {
2625     CLSID       clsid;
2626     LPWSTR      source;
2627
2628     LPWSTR      path;
2629     ITypeLib    *ptLib;
2630 } typelib_struct;
2631
2632 static BOOL CALLBACK Typelib_EnumResNameProc( HMODULE hModule, LPCWSTR lpszType, 
2633                                        LPWSTR lpszName, LONG_PTR lParam)
2634 {
2635     TLIBATTR *attr;
2636     typelib_struct *tl_struct = (typelib_struct*) lParam;
2637     static const WCHAR fmt[] = {'%','s','\\','%','i',0};
2638     int sz; 
2639     HRESULT res;
2640
2641     if (!IS_INTRESOURCE(lpszName))
2642     {
2643         ERR("Not Int Resource Name %s\n",debugstr_w(lpszName));
2644         return TRUE;
2645     }
2646
2647     sz = strlenW(tl_struct->source)+4;
2648     sz *= sizeof(WCHAR);
2649
2650     if ((INT)lpszName == 1)
2651         tl_struct->path = strdupW(tl_struct->source);
2652     else
2653     {
2654         tl_struct->path = HeapAlloc(GetProcessHeap(),0,sz);
2655         sprintfW(tl_struct->path,fmt,tl_struct->source, lpszName);
2656     }
2657
2658     TRACE("trying %s\n", debugstr_w(tl_struct->path));
2659     res = LoadTypeLib(tl_struct->path,&tl_struct->ptLib);
2660     if (!SUCCEEDED(res))
2661     {
2662         HeapFree(GetProcessHeap(),0,tl_struct->path);
2663         tl_struct->path = NULL;
2664
2665         return TRUE;
2666     }
2667
2668     ITypeLib_GetLibAttr(tl_struct->ptLib, &attr);
2669     if (IsEqualGUID(&(tl_struct->clsid),&(attr->guid)))
2670     {
2671         ITypeLib_ReleaseTLibAttr(tl_struct->ptLib, attr);
2672         return FALSE;
2673     }
2674
2675     HeapFree(GetProcessHeap(),0,tl_struct->path);
2676     tl_struct->path = NULL;
2677
2678     ITypeLib_ReleaseTLibAttr(tl_struct->ptLib, attr);
2679     ITypeLib_Release(tl_struct->ptLib);
2680
2681     return TRUE;
2682 }
2683
2684 static UINT ITERATE_RegisterTypeLibraries(MSIRECORD *row, LPVOID param)
2685 {
2686     MSIPACKAGE* package = (MSIPACKAGE*)param;
2687     LPCWSTR component;
2688     INT index;
2689     typelib_struct tl_struct;
2690     HMODULE module;
2691     static const WCHAR szTYPELIB[] = {'T','Y','P','E','L','I','B',0};
2692
2693     component = MSI_RecordGetString(row,3);
2694     index = get_loaded_component(package,component);
2695     if (index < 0)
2696         return ERROR_SUCCESS;
2697
2698     if (!ACTION_VerifyComponentForAction(package, index, INSTALLSTATE_LOCAL))
2699     {
2700         TRACE("Skipping typelib reg due to disabled component\n");
2701
2702         package->components[index].Action =
2703             package->components[index].Installed;
2704
2705         return ERROR_SUCCESS;
2706     }
2707
2708     package->components[index].Action = INSTALLSTATE_LOCAL;
2709
2710     index = get_loaded_file(package,package->components[index].KeyPath); 
2711
2712     if (index < 0)
2713         return ERROR_SUCCESS;
2714
2715     module = LoadLibraryExW(package->files[index].TargetPath, NULL,
2716                     LOAD_LIBRARY_AS_DATAFILE);
2717     if (module != NULL)
2718     {
2719         LPWSTR guid;
2720         guid = load_dynamic_stringW(row,1);
2721         CLSIDFromString(guid, &tl_struct.clsid);
2722         HeapFree(GetProcessHeap(),0,guid);
2723         tl_struct.source = strdupW(package->files[index].TargetPath);
2724         tl_struct.path = NULL;
2725
2726         EnumResourceNamesW(module, szTYPELIB, Typelib_EnumResNameProc,
2727                         (LONG_PTR)&tl_struct);
2728
2729         if (tl_struct.path != NULL)
2730         {
2731             LPWSTR help = NULL;
2732             LPCWSTR helpid;
2733             HRESULT res;
2734
2735             helpid = MSI_RecordGetString(row,6);
2736
2737             if (helpid)
2738                 help = resolve_folder(package,helpid,FALSE,FALSE,NULL);
2739             res = RegisterTypeLib(tl_struct.ptLib,tl_struct.path,help);
2740             HeapFree(GetProcessHeap(),0,help);
2741
2742             if (!SUCCEEDED(res))
2743                 ERR("Failed to register type library %s\n",
2744                         debugstr_w(tl_struct.path));
2745             else
2746             {
2747                 ui_actiondata(package,szRegisterTypeLibraries,row);
2748
2749                 TRACE("Registered %s\n", debugstr_w(tl_struct.path));
2750             }
2751
2752             ITypeLib_Release(tl_struct.ptLib);
2753             HeapFree(GetProcessHeap(),0,tl_struct.path);
2754         }
2755         else
2756             ERR("Failed to load type library %s\n",
2757                     debugstr_w(tl_struct.source));
2758
2759         FreeLibrary(module);
2760         HeapFree(GetProcessHeap(),0,tl_struct.source);
2761     }
2762     else
2763         ERR("Could not load file! %s\n",
2764                 debugstr_w(package->files[index].TargetPath));
2765
2766     return ERROR_SUCCESS;
2767 }
2768
2769 static UINT ACTION_RegisterTypeLibraries(MSIPACKAGE *package)
2770 {
2771     /* 
2772      * OK this is a bit confusing.. I am given a _Component key and I believe
2773      * that the file that is being registered as a type library is the "key file
2774      * of that component" which I interpret to mean "The file in the KeyPath of
2775      * that component".
2776      */
2777     UINT rc;
2778     MSIQUERY * view;
2779     static const WCHAR Query[] =
2780         {'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
2781          '`','T','y','p','e','L','i','b','`',0};
2782
2783     if (!package)
2784         return ERROR_INVALID_HANDLE;
2785
2786     rc = MSI_DatabaseOpenViewW(package->db, Query, &view);
2787     if (rc != ERROR_SUCCESS)
2788         return ERROR_SUCCESS;
2789
2790     rc = MSI_IterateRecords(view, NULL, ITERATE_RegisterTypeLibraries, package);
2791     msiobj_release(&view->hdr);
2792     return rc;
2793 }
2794
2795 static UINT ITERATE_CreateShortcuts(MSIRECORD *row, LPVOID param)
2796 {
2797     MSIPACKAGE *package = (MSIPACKAGE*)param;
2798     LPWSTR target_file, target_folder;
2799     LPCWSTR buffer;
2800     WCHAR filename[0x100];
2801     DWORD sz;
2802     DWORD index;
2803     static const WCHAR szlnk[]={'.','l','n','k',0};
2804     IShellLinkW *sl;
2805     IPersistFile *pf;
2806     HRESULT res;
2807
2808     buffer = MSI_RecordGetString(row,4);
2809     index = get_loaded_component(package,buffer);
2810
2811     if (index < 0)
2812         return ERROR_SUCCESS;
2813
2814     if (!ACTION_VerifyComponentForAction(package, index, INSTALLSTATE_LOCAL))
2815     {
2816         TRACE("Skipping shortcut creation due to disabled component\n");
2817
2818         package->components[index].Action =
2819                 package->components[index].Installed;
2820
2821         return ERROR_SUCCESS;
2822     }
2823
2824     package->components[index].Action = INSTALLSTATE_LOCAL;
2825
2826     ui_actiondata(package,szCreateShortcuts,row);
2827
2828     res = CoCreateInstance( &CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER,
2829                     &IID_IShellLinkW, (LPVOID *) &sl );
2830
2831     if (FAILED(res))
2832     {
2833         ERR("Is IID_IShellLink\n");
2834         return ERROR_SUCCESS;
2835     }
2836
2837     res = IShellLinkW_QueryInterface( sl, &IID_IPersistFile,(LPVOID*) &pf );
2838     if( FAILED( res ) )
2839     {
2840         ERR("Is IID_IPersistFile\n");
2841         return ERROR_SUCCESS;
2842     }
2843
2844     buffer = MSI_RecordGetString(row,2);
2845     target_folder = resolve_folder(package, buffer,FALSE,FALSE,NULL);
2846
2847     /* may be needed because of a bug somehwere else */
2848     create_full_pathW(target_folder);
2849
2850     sz = 0x100;
2851     MSI_RecordGetStringW(row,3,filename,&sz);
2852     reduce_to_longfilename(filename);
2853     if (!strchrW(filename,'.') || strcmpiW(strchrW(filename,'.'),szlnk))
2854         strcatW(filename,szlnk);
2855     target_file = build_directory_name(2, target_folder, filename);
2856     HeapFree(GetProcessHeap(),0,target_folder);
2857
2858     buffer = MSI_RecordGetString(row,5);
2859     if (strchrW(buffer,'['))
2860     {
2861         LPWSTR deformated;
2862         deformat_string(package,buffer,&deformated);
2863         IShellLinkW_SetPath(sl,deformated);
2864         HeapFree(GetProcessHeap(),0,deformated);
2865     }
2866     else
2867     {
2868         LPWSTR keypath;
2869         FIXME("poorly handled shortcut format, advertised shortcut\n");
2870         keypath = strdupW(package->components[index].FullKeypath);
2871         IShellLinkW_SetPath(sl,keypath);
2872         HeapFree(GetProcessHeap(),0,keypath);
2873     }
2874
2875     if (!MSI_RecordIsNull(row,6))
2876     {
2877         LPWSTR deformated;
2878         buffer = MSI_RecordGetString(row,6);
2879         deformat_string(package,buffer,&deformated);
2880         IShellLinkW_SetArguments(sl,deformated);
2881         HeapFree(GetProcessHeap(),0,deformated);
2882     }
2883
2884     if (!MSI_RecordIsNull(row,7))
2885     {
2886         buffer = MSI_RecordGetString(row,7);
2887         IShellLinkW_SetDescription(sl,buffer);
2888     }
2889
2890     if (!MSI_RecordIsNull(row,8))
2891         IShellLinkW_SetHotkey(sl,MSI_RecordGetInteger(row,8));
2892
2893     if (!MSI_RecordIsNull(row,9))
2894     {
2895         WCHAR *Path = NULL;
2896         INT index; 
2897
2898         buffer = MSI_RecordGetString(row,9);
2899
2900         build_icon_path(package,buffer,&Path);
2901         index = MSI_RecordGetInteger(row,10);
2902
2903         IShellLinkW_SetIconLocation(sl,Path,index);
2904         HeapFree(GetProcessHeap(),0,Path);
2905     }
2906
2907     if (!MSI_RecordIsNull(row,11))
2908         IShellLinkW_SetShowCmd(sl,MSI_RecordGetInteger(row,11));
2909
2910     if (!MSI_RecordIsNull(row,12))
2911     {
2912         LPWSTR Path;
2913         buffer = MSI_RecordGetString(row,12);
2914         Path = resolve_folder(package, buffer, FALSE, FALSE, NULL);
2915         IShellLinkW_SetWorkingDirectory(sl,Path);
2916         HeapFree(GetProcessHeap(), 0, Path);
2917     }
2918
2919     TRACE("Writing shortcut to %s\n",debugstr_w(target_file));
2920     IPersistFile_Save(pf,target_file,FALSE);
2921
2922     HeapFree(GetProcessHeap(),0,target_file);    
2923
2924     IPersistFile_Release( pf );
2925     IShellLinkW_Release( sl );
2926
2927     return ERROR_SUCCESS;
2928 }
2929
2930 static UINT ACTION_CreateShortcuts(MSIPACKAGE *package)
2931 {
2932     UINT rc;
2933     HRESULT res;
2934     MSIQUERY * view;
2935     static const WCHAR Query[] =
2936         {'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
2937          '`','S','h','o','r','t','c','u','t','`',0};
2938
2939     if (!package)
2940         return ERROR_INVALID_HANDLE;
2941
2942     rc = MSI_DatabaseOpenViewW(package->db, Query, &view);
2943     if (rc != ERROR_SUCCESS)
2944         return ERROR_SUCCESS;
2945
2946     res = CoInitialize( NULL );
2947     if (FAILED (res))
2948     {
2949         ERR("CoInitialize failed\n");
2950         return ERROR_FUNCTION_FAILED;
2951     }
2952
2953     rc = MSI_IterateRecords(view, NULL, ITERATE_CreateShortcuts, package);
2954     msiobj_release(&view->hdr);
2955
2956     CoUninitialize();
2957
2958     return rc;
2959 }
2960
2961 static UINT ITERATE_PublishProduct(MSIRECORD *row, LPVOID param)
2962 {
2963     MSIPACKAGE* package = (MSIPACKAGE*)param;
2964     HANDLE the_file;
2965     LPWSTR FilePath=NULL;
2966     LPCWSTR FileName=NULL;
2967     CHAR buffer[1024];
2968     DWORD sz;
2969     UINT rc;
2970
2971     FileName = MSI_RecordGetString(row,1);
2972     if (!FileName)
2973     {
2974         ERR("Unable to get FileName\n");
2975         return ERROR_SUCCESS;
2976     }
2977
2978     build_icon_path(package,FileName,&FilePath);
2979
2980     TRACE("Creating icon file at %s\n",debugstr_w(FilePath));
2981
2982     the_file = CreateFileW(FilePath, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS,
2983                         FILE_ATTRIBUTE_NORMAL, NULL);
2984
2985     if (the_file == INVALID_HANDLE_VALUE)
2986     {
2987         ERR("Unable to create file %s\n",debugstr_w(FilePath));
2988         HeapFree(GetProcessHeap(),0,FilePath);
2989         return ERROR_SUCCESS;
2990     }
2991
2992     do 
2993     {
2994         DWORD write;
2995         sz = 1024;
2996         rc = MSI_RecordReadStream(row,2,buffer,&sz);
2997         if (rc != ERROR_SUCCESS)
2998         {
2999             ERR("Failed to get stream\n");
3000             CloseHandle(the_file);  
3001             DeleteFileW(FilePath);
3002             break;
3003         }
3004         WriteFile(the_file,buffer,sz,&write,NULL);
3005     } while (sz == 1024);
3006
3007     HeapFree(GetProcessHeap(),0,FilePath);
3008
3009     CloseHandle(the_file);
3010     return ERROR_SUCCESS;
3011 }
3012
3013 /*
3014  * 99% of the work done here is only done for 
3015  * advertised installs. However this is where the
3016  * Icon table is processed and written out
3017  * so that is what I am going to do here.
3018  */
3019 static UINT ACTION_PublishProduct(MSIPACKAGE *package)
3020 {
3021     UINT rc;
3022     MSIQUERY * view;
3023     static const WCHAR Query[]=
3024         {'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
3025          '`','I','c','o','n','`',0};
3026     /* for registry stuff */
3027     LPWSTR productcode;
3028     HKEY hkey=0;
3029     HKEY hukey=0;
3030     static const WCHAR szProductName[] =
3031         {'P','r','o','d','u','c','t','N','a','m','e',0};
3032     static const WCHAR szPackageCode[] =
3033         {'P','a','c','k','a','g','e','C','o','d','e',0};
3034     static const WCHAR szLanguage[] =
3035         {'L','a','n','g','u','a','g','e',0};
3036     static const WCHAR szProductLanguage[] =
3037         {'P','r','o','d','u','c','t','L','a','n','g','u','a','g','e',0};
3038     static const WCHAR szProductIcon[] =
3039         {'P','r','o','d','u','c','t','I','c','o','n',0};
3040     static const WCHAR szARPProductIcon[] =
3041         {'A','R','P','P','R','O','D','U','C','T','I','C','O','N',0};
3042     static const WCHAR szProductVersion[] =
3043         {'P','r','o','d','u','c','t','V','e','r','s','i','o','n',0};
3044     static const WCHAR szVersion[] =
3045         {'V','e','r','s','i','o','n',0};
3046     DWORD langid;
3047     LPWSTR buffer;
3048     DWORD size;
3049     MSIHANDLE hDb, hSumInfo;
3050
3051     if (!package)
3052         return ERROR_INVALID_HANDLE;
3053
3054     /* write out icon files */
3055
3056     rc = MSI_DatabaseOpenViewW(package->db, Query, &view);
3057     if (rc == ERROR_SUCCESS)
3058     {
3059         MSI_IterateRecords(view, NULL, ITERATE_PublishProduct, package);
3060         msiobj_release(&view->hdr);
3061     }
3062
3063     /* ok there is a lot more done here but i need to figure out what */
3064
3065     productcode = load_dynamic_property(package,szProductCode,&rc);
3066     if (!productcode)
3067         return rc;
3068
3069     rc = MSIREG_OpenProductsKey(productcode,&hkey,TRUE);
3070     if (rc != ERROR_SUCCESS)
3071         goto end;
3072
3073     rc = MSIREG_OpenUserProductsKey(productcode,&hukey,TRUE);
3074     if (rc != ERROR_SUCCESS)
3075         goto end;
3076
3077
3078     buffer = load_dynamic_property(package,szProductName,NULL);
3079     size = strlenW(buffer)*sizeof(WCHAR);
3080     RegSetValueExW(hukey,szProductName,0,REG_SZ, (BYTE *)buffer,size);
3081     HeapFree(GetProcessHeap(),0,buffer);
3082
3083     buffer = load_dynamic_property(package,szProductLanguage,NULL);
3084     size = sizeof(DWORD);
3085     langid = atoiW(buffer);
3086     RegSetValueExW(hukey,szLanguage,0,REG_DWORD, (BYTE *)&langid,size);
3087     HeapFree(GetProcessHeap(),0,buffer);
3088
3089     buffer = load_dynamic_property(package,szARPProductIcon,NULL);
3090     if (buffer)
3091     {
3092         LPWSTR path;
3093         build_icon_path(package,buffer,&path);
3094         size = strlenW(path) * sizeof(WCHAR);
3095         RegSetValueExW(hukey,szProductIcon,0,REG_SZ, (BYTE *)path,size);
3096     }
3097     HeapFree(GetProcessHeap(),0,buffer);
3098
3099     buffer = load_dynamic_property(package,szProductVersion,NULL);
3100     if (buffer)
3101     {
3102         DWORD verdword = build_version_dword(buffer);
3103         size = sizeof(DWORD);
3104         RegSetValueExW(hukey,szVersion,0,REG_DWORD, (BYTE *)&verdword,size);
3105     }
3106     HeapFree(GetProcessHeap(),0,buffer);
3107     
3108     FIXME("Need to write more keys to the user registry\n");
3109   
3110     hDb= alloc_msihandle( &package->db->hdr );
3111     rc = MsiGetSummaryInformationW(hDb, NULL, 0, &hSumInfo); 
3112     MsiCloseHandle(hDb);
3113     if (rc == ERROR_SUCCESS)
3114     {
3115         WCHAR guidbuffer[0x200];
3116         size = 0x200;
3117         rc = MsiSummaryInfoGetPropertyW(hSumInfo, 9, NULL, NULL, NULL,
3118                                         guidbuffer, &size);
3119         if (rc == ERROR_SUCCESS)
3120         {
3121             WCHAR squashed[GUID_SIZE];
3122             /* for now we only care about the first guid */
3123             LPWSTR ptr = strchrW(guidbuffer,';');
3124             if (ptr) *ptr = 0;
3125             squash_guid(guidbuffer,squashed);
3126             size = strlenW(squashed)*sizeof(WCHAR);
3127             RegSetValueExW(hukey,szPackageCode,0,REG_SZ, (LPSTR)squashed,
3128                            size);
3129         }
3130         else
3131         {
3132             ERR("Unable to query Revision_Number... \n");
3133             rc = ERROR_SUCCESS;
3134         }
3135         MsiCloseHandle(hSumInfo);
3136     }
3137     else
3138     {
3139         ERR("Unable to open Summary Information\n");
3140         rc = ERROR_SUCCESS;
3141     }
3142
3143 end:
3144
3145     HeapFree(GetProcessHeap(),0,productcode);    
3146     RegCloseKey(hkey);
3147     RegCloseKey(hukey);
3148
3149     return rc;
3150 }
3151
3152 static UINT ITERATE_WriteIniValues(MSIRECORD *row, LPVOID param)
3153 {
3154     MSIPACKAGE *package = (MSIPACKAGE*)param;
3155     LPCWSTR component,section,key,value,identifier,filename,dirproperty;
3156     LPWSTR deformated_section, deformated_key, deformated_value;
3157     LPWSTR folder, fullname = NULL;
3158     MSIRECORD * uirow;
3159     INT component_index,action;
3160     static const WCHAR szWindowsFolder[] =
3161           {'W','i','n','d','o','w','s','F','o','l','d','e','r',0};
3162
3163     component = MSI_RecordGetString(row, 8);
3164     component_index = get_loaded_component(package,component);
3165
3166     if (!ACTION_VerifyComponentForAction(package, component_index,
3167                             INSTALLSTATE_LOCAL))
3168     {
3169         TRACE("Skipping ini file due to disabled component %s\n",
3170                         debugstr_w(component));
3171
3172         package->components[component_index].Action =
3173             package->components[component_index].Installed;
3174
3175         return ERROR_SUCCESS;
3176     }
3177
3178     package->components[component_index].Action = INSTALLSTATE_LOCAL;
3179
3180     identifier = MSI_RecordGetString(row,1); 
3181     filename = MSI_RecordGetString(row,2);
3182     dirproperty = MSI_RecordGetString(row,3);
3183     section = MSI_RecordGetString(row,4);
3184     key = MSI_RecordGetString(row,5);
3185     value = MSI_RecordGetString(row,6);
3186     action = MSI_RecordGetInteger(row,7);
3187
3188     deformat_string(package,section,&deformated_section);
3189     deformat_string(package,key,&deformated_key);
3190     deformat_string(package,value,&deformated_value);
3191
3192     if (dirproperty)
3193     {
3194         folder = resolve_folder(package, dirproperty, FALSE, FALSE, NULL);
3195         if (!folder)
3196             folder = load_dynamic_property(package,dirproperty,NULL);
3197     }
3198     else
3199         folder = load_dynamic_property(package, szWindowsFolder, NULL);
3200
3201     if (!folder)
3202     {
3203         ERR("Unable to resolve folder! (%s)\n",debugstr_w(dirproperty));
3204         goto cleanup;
3205     }
3206
3207     fullname = build_directory_name(3, folder, filename, NULL);
3208
3209     if (action == 0)
3210     {
3211         TRACE("Adding value %s to section %s in %s\n",
3212                 debugstr_w(deformated_key), debugstr_w(deformated_section),
3213                 debugstr_w(fullname));
3214         WritePrivateProfileStringW(deformated_section, deformated_key,
3215                                    deformated_value, fullname);
3216     }
3217     else if (action == 1)
3218     {
3219         WCHAR returned[10];
3220         GetPrivateProfileStringW(deformated_section, deformated_key, NULL,
3221                                  returned, 10, fullname);
3222         if (returned[0] == 0)
3223         {
3224             TRACE("Adding value %s to section %s in %s\n",
3225                     debugstr_w(deformated_key), debugstr_w(deformated_section),
3226                     debugstr_w(fullname));
3227
3228             WritePrivateProfileStringW(deformated_section, deformated_key,
3229                                        deformated_value, fullname);
3230         }
3231     }
3232     else if (action == 3)
3233         FIXME("Append to existing section not yet implemented\n");
3234
3235     uirow = MSI_CreateRecord(4);
3236     MSI_RecordSetStringW(uirow,1,identifier);
3237     MSI_RecordSetStringW(uirow,2,deformated_section);
3238     MSI_RecordSetStringW(uirow,3,deformated_key);
3239     MSI_RecordSetStringW(uirow,4,deformated_value);
3240     ui_actiondata(package,szWriteIniValues,uirow);
3241     msiobj_release( &uirow->hdr );
3242 cleanup:
3243     HeapFree(GetProcessHeap(),0,fullname);
3244     HeapFree(GetProcessHeap(),0,folder);
3245     HeapFree(GetProcessHeap(),0,deformated_key);
3246     HeapFree(GetProcessHeap(),0,deformated_value);
3247     HeapFree(GetProcessHeap(),0,deformated_section);
3248     return ERROR_SUCCESS;
3249 }
3250
3251 static UINT ACTION_WriteIniValues(MSIPACKAGE *package)
3252 {
3253     UINT rc;
3254     MSIQUERY * view;
3255     static const WCHAR ExecSeqQuery[] = 
3256         {'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
3257          '`','I','n','i','F','i','l','e','`',0};
3258
3259     rc = MSI_DatabaseOpenViewW(package->db, ExecSeqQuery, &view);
3260     if (rc != ERROR_SUCCESS)
3261     {
3262         TRACE("no IniFile table\n");
3263         return ERROR_SUCCESS;
3264     }
3265
3266     rc = MSI_IterateRecords(view, NULL, ITERATE_WriteIniValues, package);
3267     msiobj_release(&view->hdr);
3268     return rc;
3269 }
3270
3271 static UINT ITERATE_SelfRegModules(MSIRECORD *row, LPVOID param)
3272 {
3273     MSIPACKAGE *package = (MSIPACKAGE*)param;
3274     LPCWSTR filename;
3275     LPWSTR FullName;
3276     INT index;
3277     DWORD len;
3278     static const WCHAR ExeStr[] =
3279         {'r','e','g','s','v','r','3','2','.','e','x','e',' ','\"',0};
3280     static const WCHAR close[] =  {'\"',0};
3281     STARTUPINFOW si;
3282     PROCESS_INFORMATION info;
3283     BOOL brc;
3284
3285     memset(&si,0,sizeof(STARTUPINFOW));
3286
3287     filename = MSI_RecordGetString(row,1);
3288     index = get_loaded_file(package,filename);
3289
3290     if (index < 0)
3291     {
3292         ERR("Unable to find file id %s\n",debugstr_w(filename));
3293         return ERROR_SUCCESS;
3294     }
3295
3296     len = strlenW(ExeStr);
3297     len += strlenW(package->files[index].TargetPath);
3298     len +=2;
3299
3300     FullName = HeapAlloc(GetProcessHeap(),0,len*sizeof(WCHAR));
3301     strcpyW(FullName,ExeStr);
3302     strcatW(FullName,package->files[index].TargetPath);
3303     strcatW(FullName,close);
3304
3305     TRACE("Registering %s\n",debugstr_w(FullName));
3306     brc = CreateProcessW(NULL, FullName, NULL, NULL, FALSE, 0, NULL, c_colon,
3307                     &si, &info);
3308
3309     if (brc)
3310         msi_dialog_check_messages(info.hProcess);
3311
3312     HeapFree(GetProcessHeap(),0,FullName);
3313     return ERROR_SUCCESS;
3314 }
3315
3316 static UINT ACTION_SelfRegModules(MSIPACKAGE *package)
3317 {
3318     UINT rc;
3319     MSIQUERY * view;
3320     static const WCHAR ExecSeqQuery[] = 
3321         {'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
3322          '`','S','e','l','f','R','e','g','`',0};
3323
3324     rc = MSI_DatabaseOpenViewW(package->db, ExecSeqQuery, &view);
3325     if (rc != ERROR_SUCCESS)
3326     {
3327         TRACE("no SelfReg table\n");
3328         return ERROR_SUCCESS;
3329     }
3330
3331     MSI_IterateRecords(view, NULL, ITERATE_SelfRegModules, package);
3332     msiobj_release(&view->hdr);
3333
3334     return ERROR_SUCCESS;
3335 }
3336
3337 static UINT ACTION_PublishFeatures(MSIPACKAGE *package)
3338 {
3339     LPWSTR productcode;
3340     UINT rc;
3341     DWORD i;
3342     HKEY hkey=0;
3343     HKEY hukey=0;
3344     
3345     if (!package)
3346         return ERROR_INVALID_HANDLE;
3347
3348     productcode = load_dynamic_property(package,szProductCode,&rc);
3349     if (!productcode)
3350         return rc;
3351
3352     rc = MSIREG_OpenFeaturesKey(productcode,&hkey,TRUE);
3353     if (rc != ERROR_SUCCESS)
3354         goto end;
3355
3356     rc = MSIREG_OpenUserFeaturesKey(productcode,&hukey,TRUE);
3357     if (rc != ERROR_SUCCESS)
3358         goto end;
3359
3360     /* here the guids are base 85 encoded */
3361     for (i = 0; i < package->loaded_features; i++)
3362     {
3363         LPWSTR data = NULL;
3364         GUID clsid;
3365         int j;
3366         INT size;
3367         BOOL absent = FALSE;
3368
3369         if (!ACTION_VerifyFeatureForAction(package,i,INSTALLSTATE_LOCAL) &&
3370             !ACTION_VerifyFeatureForAction(package,i,INSTALLSTATE_SOURCE) &&
3371             !ACTION_VerifyFeatureForAction(package,i,INSTALLSTATE_ADVERTISED))
3372             absent = TRUE;
3373
3374         size = package->features[i].ComponentCount*21;
3375         size +=1;
3376         if (package->features[i].Feature_Parent[0])
3377             size += strlenW(package->features[i].Feature_Parent)+2;
3378
3379         data = HeapAlloc(GetProcessHeap(), 0, size * sizeof(WCHAR));
3380
3381         data[0] = 0;
3382         for (j = 0; j < package->features[i].ComponentCount; j++)
3383         {
3384             WCHAR buf[21];
3385             memset(buf,0,sizeof(buf));
3386             if (package->components
3387                 [package->features[i].Components[j]].ComponentId[0]!=0)
3388             {
3389                 TRACE("From %s\n",debugstr_w(package->components
3390                             [package->features[i].Components[j]].ComponentId));
3391                 CLSIDFromString(package->components
3392                             [package->features[i].Components[j]].ComponentId,
3393                             &clsid);
3394                 encode_base85_guid(&clsid,buf);
3395                 TRACE("to %s\n",debugstr_w(buf));
3396                 strcatW(data,buf);
3397             }
3398         }
3399         if (package->features[i].Feature_Parent[0])
3400         {
3401             static const WCHAR sep[] = {'\2',0};
3402             strcatW(data,sep);
3403             strcatW(data,package->features[i].Feature_Parent);
3404         }
3405
3406         size = (strlenW(data)+1)*sizeof(WCHAR);
3407         RegSetValueExW(hkey,package->features[i].Feature,0,REG_SZ,
3408                        (LPSTR)data,size);
3409         HeapFree(GetProcessHeap(),0,data);
3410
3411         if (!absent)
3412         {
3413             size = strlenW(package->features[i].Feature_Parent)*sizeof(WCHAR);
3414             RegSetValueExW(hukey,package->features[i].Feature,0,REG_SZ,
3415                        (LPSTR)package->features[i].Feature_Parent,size);
3416         }
3417         else
3418         {
3419             size = (strlenW(package->features[i].Feature_Parent)+2)*
3420                     sizeof(WCHAR);
3421             data = HeapAlloc(GetProcessHeap(),0,size);
3422             data[0] = 0x6;
3423             strcpyW(&data[1],package->features[i].Feature_Parent);
3424             RegSetValueExW(hukey,package->features[i].Feature,0,REG_SZ,
3425                        (LPSTR)data,size);
3426             HeapFree(GetProcessHeap(),0,data);
3427         }
3428     }
3429
3430 end:
3431     RegCloseKey(hkey);
3432     RegCloseKey(hukey);
3433     HeapFree(GetProcessHeap(), 0, productcode);
3434     return rc;
3435 }
3436
3437 static UINT ACTION_RegisterProduct(MSIPACKAGE *package)
3438 {
3439     HKEY hkey=0;
3440     LPWSTR buffer = NULL;
3441     LPWSTR productcode;
3442     UINT rc,i;
3443     DWORD size;
3444     static WCHAR szNONE[] = {0};
3445     static const WCHAR szWindowsInstaler[] = 
3446     {'W','i','n','d','o','w','s','I','n','s','t','a','l','l','e','r',0};
3447     static const WCHAR szPropKeys[][80] = 
3448     {
3449 {'A','R','P','A','U','T','H','O','R','I','Z','E','D','C','D','F','P','R','E','F','I','X',0},
3450 {'A','R','P','C','O','N','T','A','C','T',0},
3451 {'A','R','P','C','O','M','M','E','N','T','S',0},
3452 {'P','r','o','d','u','c','t','N','a','m','e',0},
3453 {'P','r','o','d','u','c','t','V','e','r','s','i','o','n',0},
3454 {'A','R','P','H','E','L','P','L','I','N','K',0},
3455 {'A','R','P','H','E','L','P','T','E','L','E','P','H','O','N','E',0},
3456 {'A','R','P','I','N','S','T','A','L','L','L','O','C','A','T','I','O','N',0},
3457 {'S','o','u','r','c','e','D','i','r',0},
3458 {'M','a','n','u','f','a','c','t','u','r','e','r',0},
3459 {'A','R','P','R','E','A','D','M','E',0},
3460 {'A','R','P','S','I','Z','E',0},
3461 {'A','R','P','U','R','L','I','N','F','O','A','B','O','U','T',0},
3462 {'A','R','P','U','R','L','U','P','D','A','T','E','I','N','F','O',0},
3463 {0},
3464     };
3465
3466     static const WCHAR szRegKeys[][80] = 
3467     {
3468 {'A','u','t','h','o','r','i','z','e','d','C','D','F','P','r','e','f','i','x',0},
3469 {'C','o','n','t','a','c','t',0},
3470 {'C','o','m','m','e','n','t','s',0},
3471 {'D','i','s','p','l','a','y','N','a','m','e',0},
3472 {'D','i','s','p','l','a','y','V','e','r','s','i','o','n',0},
3473 {'H','e','l','p','L','i','n','k',0},
3474 {'H','e','l','p','T','e','l','e','p','h','o','n','e',0},
3475 {'I','n','s','t','a','l','l','L','o','c','a','t','i','o','n',0},
3476 {'I','n','s','t','a','l','l','S','o','u','r','c','e',0},
3477 {'P','u','b','l','i','s','h','e','r',0},
3478 {'R','e','a','d','m','e',0},
3479 {'S','i','z','e',0},
3480 {'U','R','L','I','n','f','o','A','b','o','u','t',0},
3481 {'U','R','L','U','p','d','a','t','e','I','n','f','o',0},
3482 {0},
3483     };
3484
3485     static const WCHAR installerPathFmt[] = {
3486     '%','s','\\',
3487     'I','n','s','t','a','l','l','e','r','\\',0};
3488     static const WCHAR fmt[] = {
3489     '%','s','\\',
3490     'I','n','s','t','a','l','l','e','r','\\',
3491     '%','x','.','m','s','i',0};
3492     static const WCHAR szLocalPackage[]=
3493          {'L','o','c','a','l','P','a','c','k','a','g','e',0};
3494     static const WCHAR szUpgradeCode[] = 
3495         {'U','p','g','r','a','d','e','C','o','d','e',0};
3496     static const WCHAR modpath_fmt[] = 
3497         {'M','s','i','E','x','e','c','.','e','x','e',' ','/','I','[','P','r','o','d','u','c','t','C','o','d','e',']',0};
3498     static const WCHAR szModifyPath[] = 
3499         {'M','o','d','i','f','y','P','a','t','h',0};
3500     static const WCHAR szUninstallString[] = 
3501         {'U','n','i','n','s','t','a','l','l','S','t','r','i','n','g',0};
3502     static const WCHAR szEstimatedSize[] = 
3503         {'E','s','t','i','m','a','t','e','d','S','i','z','e',0};
3504     static const WCHAR szInstallDate[] = 
3505         {'I','n','s','t','a','l','l','D','a','t','e',0};
3506     static const WCHAR szLanguage[] =
3507         {'L','a','n','g','u','a','g','e',0};
3508     static const WCHAR szProductLanguage[] =
3509         {'P','r','o','d','u','c','t','L','a','n','g','u','a','g','e',0};
3510     static const WCHAR szProductVersion[] =
3511         {'P','r','o','d','u','c','t','V','e','r','s','i','o','n',0};
3512     static const WCHAR szVersion[] =
3513         {'V','e','r','s','i','o','n',0};
3514     static const WCHAR szVersionMajor[] =
3515         {'V','e','r','s','i','o','n','M','a','j','o','r',0};
3516     static const WCHAR szVersionMinor[] =
3517         {'V','e','r','s','i','o','n','M','i','n','o','r',0};
3518
3519     SYSTEMTIME systime;
3520     static const WCHAR date_fmt[] = {'%','i','%','i','%','i',0};
3521     LPWSTR upgrade_code;
3522     WCHAR windir[MAX_PATH], path[MAX_PATH], packagefile[MAX_PATH];
3523     INT num,start;
3524
3525     if (!package)
3526         return ERROR_INVALID_HANDLE;
3527
3528     productcode = load_dynamic_property(package,szProductCode,&rc);
3529     if (!productcode)
3530         return rc;
3531
3532     rc = MSIREG_OpenUninstallKey(productcode,&hkey,TRUE);
3533     if (rc != ERROR_SUCCESS)
3534         goto end;
3535
3536     /* dump all the info i can grab */
3537     FIXME("Flesh out more information \n");
3538
3539     i = 0;
3540     while (szPropKeys[i][0]!=0)
3541     {
3542         buffer = load_dynamic_property(package,szPropKeys[i],&rc);
3543         if (rc != ERROR_SUCCESS)
3544             buffer = szNONE;
3545         size = strlenW(buffer)*sizeof(WCHAR);
3546         RegSetValueExW(hkey,szRegKeys[i],0,REG_SZ,(LPSTR)buffer,size);
3547         HeapFree(GetProcessHeap(),0,buffer);
3548         i++;
3549     }
3550
3551     rc = 0x1;
3552     size = sizeof(rc);
3553     RegSetValueExW(hkey,szWindowsInstaler,0,REG_DWORD,(LPSTR)&rc,size);
3554     
3555     /* copy the package locally */
3556     num = GetTickCount() & 0xffff;
3557     if (!num) 
3558         num = 1;
3559     start = num;
3560     GetWindowsDirectoryW(windir, sizeof(windir) / sizeof(windir[0]));
3561     snprintfW(packagefile,sizeof(packagefile)/sizeof(packagefile[0]),fmt,
3562      windir,num);
3563     do 
3564     {
3565         HANDLE handle = CreateFileW(packagefile,GENERIC_WRITE, 0, NULL,
3566                                   CREATE_NEW, FILE_ATTRIBUTE_NORMAL, 0 );
3567         if (handle != INVALID_HANDLE_VALUE)
3568         {
3569             CloseHandle(handle);
3570             break;
3571         }
3572         if (GetLastError() != ERROR_FILE_EXISTS &&
3573             GetLastError() != ERROR_SHARING_VIOLATION)
3574             break;
3575         if (!(++num & 0xffff)) num = 1;
3576         sprintfW(packagefile,fmt,num);
3577     } while (num != start);
3578
3579     snprintfW(path,sizeof(path)/sizeof(path[0]),installerPathFmt,windir);
3580     create_full_pathW(path);
3581     TRACE("Copying to local package %s\n",debugstr_w(packagefile));
3582     if (!CopyFileW(package->msiFilePath,packagefile,FALSE))
3583         ERR("Unable to copy package (%s -> %s) (error %ld)\n",
3584             debugstr_w(package->msiFilePath), debugstr_w(packagefile),
3585             GetLastError());
3586     size = strlenW(packagefile)*sizeof(WCHAR);
3587     RegSetValueExW(hkey,szLocalPackage,0,REG_SZ,(LPSTR)packagefile,size);
3588
3589     /* do ModifyPath and UninstallString */
3590     size = deformat_string(package,modpath_fmt,&buffer);
3591     RegSetValueExW(hkey,szModifyPath,0,REG_EXPAND_SZ,(LPSTR)buffer,size);
3592     RegSetValueExW(hkey,szUninstallString,0,REG_EXPAND_SZ,(LPSTR)buffer,size);
3593     HeapFree(GetProcessHeap(),0,buffer);
3594
3595     FIXME("Write real Estimated Size when we have it\n");
3596     size = 0;
3597     RegSetValueExW(hkey,szEstimatedSize,0,REG_DWORD,(LPSTR)&size,sizeof(DWORD));
3598    
3599     GetLocalTime(&systime);
3600     size = 9*sizeof(WCHAR);
3601     buffer= HeapAlloc(GetProcessHeap(),0,size);
3602     sprintfW(buffer,date_fmt,systime.wYear,systime.wMonth,systime.wDay);
3603     size = strlenW(buffer)*sizeof(WCHAR);
3604     RegSetValueExW(hkey,szInstallDate,0,REG_SZ,(LPSTR)buffer,size);
3605     HeapFree(GetProcessHeap(),0,buffer);
3606    
3607     buffer = load_dynamic_property(package,szProductLanguage,NULL);
3608     size = atoiW(buffer);
3609     RegSetValueExW(hkey,szLanguage,0,REG_DWORD, (LPSTR)&size,sizeof(DWORD));
3610     HeapFree(GetProcessHeap(),1,buffer);
3611
3612     buffer = load_dynamic_property(package,szProductVersion,NULL);
3613     if (buffer)
3614     {
3615         DWORD verdword = build_version_dword(buffer);
3616         DWORD vermajor = verdword>>24;
3617         DWORD verminor = (verdword>>16)&0x00FF;
3618         size = sizeof(DWORD);
3619         RegSetValueExW(hkey,szVersion,0,REG_DWORD,(LPSTR)&verdword,size);
3620         RegSetValueExW(hkey,szVersionMajor,0,REG_DWORD,(LPSTR)&vermajor,size);
3621         RegSetValueExW(hkey,szVersionMinor,0,REG_DWORD,(LPSTR)&verminor,size);
3622     }
3623     HeapFree(GetProcessHeap(),0,buffer);
3624     
3625     /* Handle Upgrade Codes */
3626     upgrade_code = load_dynamic_property(package,szUpgradeCode, NULL);
3627     if (upgrade_code)
3628     {
3629         HKEY hkey2;
3630         WCHAR squashed[33];
3631         MSIREG_OpenUpgradeCodesKey(upgrade_code, &hkey2, TRUE);
3632         squash_guid(productcode,squashed);
3633         RegSetValueExW(hkey2, squashed, 0,REG_SZ,NULL,0);
3634         RegCloseKey(hkey2);
3635         MSIREG_OpenUserUpgradeCodesKey(upgrade_code, &hkey2, TRUE);
3636         squash_guid(productcode,squashed);
3637         RegSetValueExW(hkey2, squashed, 0,REG_SZ,NULL,0);
3638         RegCloseKey(hkey2);
3639
3640         HeapFree(GetProcessHeap(),0,upgrade_code);
3641     }
3642     
3643 end:
3644     HeapFree(GetProcessHeap(),0,productcode);
3645     RegCloseKey(hkey);
3646
3647     return ERROR_SUCCESS;
3648 }
3649
3650 static UINT ACTION_InstallExecute(MSIPACKAGE *package)
3651 {
3652     UINT rc;
3653
3654     if (!package)
3655         return ERROR_INVALID_HANDLE;
3656
3657     rc = execute_script(package,INSTALL_SCRIPT);
3658
3659     return rc;
3660 }
3661
3662 static UINT ACTION_InstallFinalize(MSIPACKAGE *package)
3663 {
3664     UINT rc;
3665
3666     if (!package)
3667         return ERROR_INVALID_HANDLE;
3668
3669     /* turn off scheduleing */
3670     package->script->CurrentlyScripting= FALSE;
3671
3672     /* first do the same as an InstallExecute */
3673     rc = ACTION_InstallExecute(package);
3674     if (rc != ERROR_SUCCESS)
3675         return rc;
3676
3677     /* then handle Commit Actions */
3678     rc = execute_script(package,COMMIT_SCRIPT);
3679
3680     return rc;
3681 }
3682
3683 static UINT ACTION_ForceReboot(MSIPACKAGE *package)
3684 {
3685     static const WCHAR RunOnce[] = {
3686     'S','o','f','t','w','a','r','e','\\',
3687     'M','i','c','r','o','s','o','f','t','\\',
3688     'W','i','n','d','o','w','s','\\',
3689     'C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
3690     'R','u','n','O','n','c','e',0};
3691     static const WCHAR InstallRunOnce[] = {
3692     'S','o','f','t','w','a','r','e','\\',
3693     'M','i','c','r','o','s','o','f','t','\\',
3694     'W','i','n','d','o','w','s','\\',
3695     'C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
3696     'I','n','s','t','a','l','l','e','r','\\',
3697     'R','u','n','O','n','c','e','E','n','t','r','i','e','s',0};
3698
3699     static const WCHAR msiexec_fmt[] = {
3700     '%','s',
3701     '\\','M','s','i','E','x','e','c','.','e','x','e',' ','/','@',' ',
3702     '\"','%','s','\"',0};
3703     static const WCHAR install_fmt[] = {
3704     '/','I',' ','\"','%','s','\"',' ',
3705     'A','F','T','E','R','R','E','B','O','O','T','=','1',' ',
3706     'R','U','N','O','N','C','E','E','N','T','R','Y','=','\"','%','s','\"',0};
3707     WCHAR buffer[256], sysdir[MAX_PATH];
3708     HKEY hkey,hukey;
3709     LPWSTR productcode;
3710     WCHAR  squished_pc[100];
3711     INT rc;
3712     DWORD size;
3713     static const WCHAR szLUS[] = {
3714          'L','a','s','t','U','s','e','d','S','o','u','r','c','e',0};
3715     static const WCHAR szSourceList[] = {
3716          'S','o','u','r','c','e','L','i','s','t',0};
3717     static const WCHAR szPackageName[] = { 
3718         'P','a','c','k','a','g','e','N','a','m','e',0};
3719
3720     if (!package)
3721         return ERROR_INVALID_HANDLE;
3722
3723     productcode = load_dynamic_property(package,szProductCode,&rc);
3724     if (!productcode)
3725         return rc;
3726
3727     squash_guid(productcode,squished_pc);
3728
3729     GetSystemDirectoryW(sysdir, sizeof(sysdir)/sizeof(sysdir[0]));
3730     RegCreateKeyW(HKEY_LOCAL_MACHINE,RunOnce,&hkey);
3731     snprintfW(buffer,sizeof(buffer)/sizeof(buffer[0]),msiexec_fmt,sysdir,
3732      squished_pc);
3733
3734     size = strlenW(buffer)*sizeof(WCHAR);
3735     RegSetValueExW(hkey,squished_pc,0,REG_SZ,(LPSTR)buffer,size);
3736     RegCloseKey(hkey);
3737
3738     TRACE("Reboot command %s\n",debugstr_w(buffer));
3739
3740     RegCreateKeyW(HKEY_LOCAL_MACHINE,InstallRunOnce,&hkey);
3741     sprintfW(buffer,install_fmt,productcode,squished_pc);
3742
3743     size = strlenW(buffer)*sizeof(WCHAR);
3744     RegSetValueExW(hkey,squished_pc,0,REG_SZ,(LPSTR)buffer,size);
3745     RegCloseKey(hkey);
3746
3747     rc = MSIREG_OpenUserProductsKey(productcode,&hukey,TRUE);
3748     if (rc == ERROR_SUCCESS)
3749     {
3750         HKEY hukey2;
3751         LPWSTR buf;
3752         RegCreateKeyW(hukey, szSourceList, &hukey2);
3753         buf = load_dynamic_property(package,cszSourceDir,NULL);
3754         size = strlenW(buf)*sizeof(WCHAR);
3755         RegSetValueExW(hukey2,szLUS,0,REG_SZ,(LPSTR)buf,size);
3756         HeapFree(GetProcessHeap(),0,buf); 
3757
3758         buf = strrchrW(package->PackagePath,'\\');
3759         if (buf)
3760         {
3761             buf++;
3762             size = strlenW(buf)*sizeof(WCHAR);
3763             RegSetValueExW(hukey2,szPackageName,0,REG_SZ,(LPSTR)buf,size);
3764         }
3765
3766         RegCloseKey(hukey2);
3767     }
3768     HeapFree(GetProcessHeap(),0,productcode);
3769
3770     return ERROR_INSTALL_SUSPEND;
3771 }
3772
3773 UINT ACTION_ResolveSource(MSIPACKAGE* package)
3774 {
3775     /*
3776      * we are currently doing what should be done here in the top level Install
3777      * however for Adminastrative and uninstalls this step will be needed
3778      */
3779     return ERROR_SUCCESS;
3780 }
3781
3782 static UINT ACTION_RegisterUser(MSIPACKAGE *package)
3783 {
3784     static const WCHAR szProductID[]=
3785          {'P','r','o','d','u','c','t','I','D',0};
3786     HKEY hkey=0;
3787     LPWSTR buffer;
3788     LPWSTR productcode;
3789     LPWSTR productid;
3790     UINT rc,i;
3791     DWORD size;
3792
3793     static const WCHAR szPropKeys[][80] = 
3794     {
3795         {'P','r','o','d','u','c','t','I','D',0},
3796         {'U','S','E','R','N','A','M','E',0},
3797         {'C','O','M','P','A','N','Y','N','A','M','E',0},
3798         {0},
3799     };
3800
3801     static const WCHAR szRegKeys[][80] = 
3802     {
3803         {'P','r','o','d','u','c','t','I','D',0},
3804         {'R','e','g','O','w','n','e','r',0},
3805         {'R','e','g','C','o','m','p','a','n','y',0},
3806         {0},
3807     };
3808
3809     if (!package)
3810         return ERROR_INVALID_HANDLE;
3811
3812     productid = load_dynamic_property(package,szProductID,&rc);
3813     if (!productid)
3814         return ERROR_SUCCESS;
3815
3816     productcode = load_dynamic_property(package,szProductCode,&rc);
3817     if (!productcode)
3818         return rc;
3819
3820     rc = MSIREG_OpenUninstallKey(productcode,&hkey,TRUE);
3821     if (rc != ERROR_SUCCESS)
3822         goto end;
3823
3824     i = 0;
3825     while (szPropKeys[i][0]!=0)
3826     {
3827         buffer = load_dynamic_property(package,szPropKeys[i],&rc);
3828         if (rc == ERROR_SUCCESS)
3829         {
3830             size = strlenW(buffer)*sizeof(WCHAR);
3831             RegSetValueExW(hkey,szRegKeys[i],0,REG_SZ,(LPSTR)buffer,size);
3832         }
3833         else
3834             RegSetValueExW(hkey,szRegKeys[i],0,REG_SZ,NULL,0);
3835         i++;
3836     }
3837
3838 end:
3839     HeapFree(GetProcessHeap(),0,productcode);
3840     HeapFree(GetProcessHeap(),0,productid);
3841     RegCloseKey(hkey);
3842
3843     return ERROR_SUCCESS;
3844 }
3845
3846
3847 static UINT ACTION_ExecuteAction(MSIPACKAGE *package)
3848 {
3849     static const WCHAR szUILevel[] = {'U','I','L','e','v','e','l',0};
3850     static const WCHAR szTwo[] = {'2',0};
3851     UINT rc;
3852     LPWSTR level;
3853     level = load_dynamic_property(package,szUILevel,NULL);
3854
3855     MSI_SetPropertyW(package,szUILevel,szTwo);
3856     package->script->InWhatSequence |= SEQUENCE_EXEC;
3857     rc = ACTION_ProcessExecSequence(package,FALSE);
3858     MSI_SetPropertyW(package,szUILevel,level);
3859     HeapFree(GetProcessHeap(),0,level);
3860     return rc;
3861 }
3862
3863
3864 /*
3865  * Code based off of code located here
3866  * http://www.codeproject.com/gdi/fontnamefromfile.asp
3867  *
3868  * Using string index 4 (full font name) instead of 1 (family name)
3869  */
3870 static LPWSTR load_ttfname_from(LPCWSTR filename)
3871 {
3872     HANDLE handle;
3873     LPWSTR ret = NULL;
3874     int i;
3875
3876     typedef struct _tagTT_OFFSET_TABLE{
3877         USHORT uMajorVersion;
3878         USHORT uMinorVersion;
3879         USHORT uNumOfTables;
3880         USHORT uSearchRange;
3881         USHORT uEntrySelector;
3882         USHORT uRangeShift;
3883     }TT_OFFSET_TABLE;
3884
3885     typedef struct _tagTT_TABLE_DIRECTORY{
3886         char szTag[4]; /* table name */
3887         ULONG uCheckSum; /* Check sum */
3888         ULONG uOffset; /* Offset from beginning of file */
3889         ULONG uLength; /* length of the table in bytes */
3890     }TT_TABLE_DIRECTORY;
3891
3892     typedef struct _tagTT_NAME_TABLE_HEADER{
3893     USHORT uFSelector; /* format selector. Always 0 */
3894     USHORT uNRCount; /* Name Records count */
3895     USHORT uStorageOffset; /* Offset for strings storage, 
3896                             * from start of the table */
3897     }TT_NAME_TABLE_HEADER;
3898    
3899     typedef struct _tagTT_NAME_RECORD{
3900         USHORT uPlatformID;
3901         USHORT uEncodingID;
3902         USHORT uLanguageID;
3903         USHORT uNameID;
3904         USHORT uStringLength;
3905         USHORT uStringOffset; /* from start of storage area */
3906     }TT_NAME_RECORD;
3907
3908 #define SWAPWORD(x) MAKEWORD(HIBYTE(x), LOBYTE(x))
3909 #define SWAPLONG(x) MAKELONG(SWAPWORD(HIWORD(x)), SWAPWORD(LOWORD(x)))
3910
3911     handle = CreateFileW(filename ,GENERIC_READ, 0, NULL, OPEN_EXISTING,
3912                     FILE_ATTRIBUTE_NORMAL, 0 );
3913     if (handle != INVALID_HANDLE_VALUE)
3914     {
3915         TT_TABLE_DIRECTORY tblDir;
3916         BOOL bFound = FALSE;
3917         TT_OFFSET_TABLE ttOffsetTable;
3918
3919         ReadFile(handle,&ttOffsetTable, sizeof(TT_OFFSET_TABLE),NULL,NULL);
3920         ttOffsetTable.uNumOfTables = SWAPWORD(ttOffsetTable.uNumOfTables);
3921         ttOffsetTable.uMajorVersion = SWAPWORD(ttOffsetTable.uMajorVersion);
3922         ttOffsetTable.uMinorVersion = SWAPWORD(ttOffsetTable.uMinorVersion);
3923         
3924         if (ttOffsetTable.uMajorVersion != 1 || 
3925                         ttOffsetTable.uMinorVersion != 0)
3926             return NULL;
3927
3928         for (i=0; i< ttOffsetTable.uNumOfTables; i++)
3929         {
3930             ReadFile(handle,&tblDir, sizeof(TT_TABLE_DIRECTORY),NULL,NULL);
3931             if (strncmp(tblDir.szTag,"name",4)==0)
3932             {
3933                 bFound = TRUE;
3934                 tblDir.uLength = SWAPLONG(tblDir.uLength);
3935                 tblDir.uOffset = SWAPLONG(tblDir.uOffset);
3936                 break;
3937             }
3938         }
3939
3940         if (bFound)
3941         {
3942             TT_NAME_TABLE_HEADER ttNTHeader;
3943             TT_NAME_RECORD ttRecord;
3944
3945             SetFilePointer(handle, tblDir.uOffset, NULL, FILE_BEGIN);
3946             ReadFile(handle,&ttNTHeader, sizeof(TT_NAME_TABLE_HEADER),
3947                             NULL,NULL);
3948
3949             ttNTHeader.uNRCount = SWAPWORD(ttNTHeader.uNRCount);
3950             ttNTHeader.uStorageOffset = SWAPWORD(ttNTHeader.uStorageOffset);
3951             bFound = FALSE;
3952             for(i=0; i<ttNTHeader.uNRCount; i++)
3953             {
3954                 ReadFile(handle,&ttRecord, sizeof(TT_NAME_RECORD),NULL,NULL);
3955                 ttRecord.uNameID = SWAPWORD(ttRecord.uNameID);
3956                 /* 4 is the Full Font Name */
3957                 if(ttRecord.uNameID == 4)
3958                 {
3959                     int nPos;
3960                     LPSTR buf;
3961                     static LPCSTR tt = " (TrueType)";
3962
3963                     ttRecord.uStringLength = SWAPWORD(ttRecord.uStringLength);
3964                     ttRecord.uStringOffset = SWAPWORD(ttRecord.uStringOffset);
3965                     nPos = SetFilePointer(handle, 0, NULL, FILE_CURRENT);
3966                     SetFilePointer(handle, tblDir.uOffset + 
3967                                     ttRecord.uStringOffset + 
3968                                     ttNTHeader.uStorageOffset,
3969                                     NULL, FILE_BEGIN);
3970                     buf = HeapAlloc(GetProcessHeap(), 0, 
3971                                     ttRecord.uStringLength + 1 + strlen(tt));
3972                     memset(buf, 0, ttRecord.uStringLength + 1 + strlen(tt));
3973                     ReadFile(handle, buf, ttRecord.uStringLength, NULL, NULL);
3974                     if (strlen(buf) > 0)
3975                     {
3976                         strcat(buf,tt);
3977                         ret = strdupAtoW(buf);
3978                         HeapFree(GetProcessHeap(),0,buf);
3979                         break;
3980                     }
3981
3982                     HeapFree(GetProcessHeap(),0,buf);
3983                     SetFilePointer(handle,nPos, NULL, FILE_BEGIN);
3984                 }
3985             }
3986         }
3987         CloseHandle(handle);
3988     }
3989     else
3990         ERR("Unable to open font file %s\n", debugstr_w(filename));
3991
3992     TRACE("Returning fontname %s\n",debugstr_w(ret));
3993     return ret;
3994 }
3995
3996 static UINT ITERATE_RegisterFonts(MSIRECORD *row, LPVOID param)
3997 {
3998     MSIPACKAGE *package = (MSIPACKAGE*)param;
3999     LPWSTR name;
4000     LPCWSTR file;
4001     UINT index;
4002     DWORD size;
4003     static const WCHAR regfont1[] =
4004         {'S','o','f','t','w','a','r','e','\\',
4005          'M','i','c','r','o','s','o','f','t','\\',
4006          'W','i','n','d','o','w','s',' ','N','T','\\',
4007          'C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
4008          'F','o','n','t','s',0};
4009     static const WCHAR regfont2[] =
4010         {'S','o','f','t','w','a','r','e','\\',
4011          'M','i','c','r','o','s','o','f','t','\\',
4012          'W','i','n','d','o','w','s','\\',
4013          'C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
4014          'F','o','n','t','s',0};
4015     HKEY hkey1;
4016     HKEY hkey2;
4017
4018     file = MSI_RecordGetString(row,1);
4019     index = get_loaded_file(package,file);
4020     if (index < 0)
4021     {
4022         ERR("Unable to load file\n");
4023         return ERROR_SUCCESS;
4024     }
4025
4026     /* check to make sure that component is installed */
4027     if (!ACTION_VerifyComponentForAction(package, 
4028                 package->files[index].ComponentIndex, INSTALLSTATE_LOCAL))
4029     {
4030         TRACE("Skipping: Component not scheduled for install\n");
4031         return ERROR_SUCCESS;
4032     }
4033
4034     RegCreateKeyW(HKEY_LOCAL_MACHINE,regfont1,&hkey1);
4035     RegCreateKeyW(HKEY_LOCAL_MACHINE,regfont2,&hkey2);
4036
4037     if (MSI_RecordIsNull(row,2))
4038         name = load_ttfname_from(package->files[index].TargetPath);
4039     else
4040         name = load_dynamic_stringW(row,2);
4041
4042     if (name)
4043     {
4044         size = strlenW(package->files[index].FileName) * sizeof(WCHAR);
4045         RegSetValueExW(hkey1,name,0,REG_SZ,
4046                     (LPBYTE)package->files[index].FileName,size);
4047         RegSetValueExW(hkey2,name,0,REG_SZ,
4048                     (LPBYTE)package->files[index].FileName,size);
4049     }
4050
4051     HeapFree(GetProcessHeap(),0,name);
4052     RegCloseKey(hkey1);
4053     RegCloseKey(hkey2);
4054     return ERROR_SUCCESS;
4055 }
4056
4057 static UINT ACTION_RegisterFonts(MSIPACKAGE *package)
4058 {
4059     UINT rc;
4060     MSIQUERY * view;
4061     static const WCHAR ExecSeqQuery[] =
4062         {'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
4063          '`','F','o','n','t','`',0};
4064
4065     rc = MSI_DatabaseOpenViewW(package->db, ExecSeqQuery, &view);
4066     if (rc != ERROR_SUCCESS)
4067     {
4068         TRACE("MSI_DatabaseOpenViewW failed: %d\n", rc);
4069         return ERROR_SUCCESS;
4070     }
4071
4072     MSI_IterateRecords(view, NULL, ITERATE_RegisterFonts, package);
4073     msiobj_release(&view->hdr);
4074
4075     return ERROR_SUCCESS;
4076 }
4077
4078 static UINT ITERATE_PublishComponent(MSIRECORD *rec, LPVOID param)
4079 {
4080     MSIPACKAGE *package = (MSIPACKAGE*)param;
4081     LPCWSTR compgroupid=NULL;
4082     LPCWSTR feature=NULL;
4083     LPCWSTR text = NULL;
4084     LPCWSTR qualifier = NULL;
4085     LPCWSTR component = NULL;
4086     LPWSTR advertise = NULL;
4087     LPWSTR output = NULL;
4088     HKEY hkey;
4089     UINT rc = ERROR_SUCCESS;
4090     UINT index;
4091     DWORD sz = 0;
4092
4093     component = MSI_RecordGetString(rec,3);
4094     index = get_loaded_component(package,component);
4095
4096     if (!ACTION_VerifyComponentForAction(package, index,
4097                             INSTALLSTATE_LOCAL) && 
4098        !ACTION_VerifyComponentForAction(package, index,
4099                             INSTALLSTATE_SOURCE) &&
4100        !ACTION_VerifyComponentForAction(package, index,
4101                             INSTALLSTATE_ADVERTISED))
4102     {
4103         TRACE("Skipping: Component %s not scheduled for install\n",
4104                         debugstr_w(component));
4105
4106         return ERROR_SUCCESS;
4107     }
4108
4109     compgroupid = MSI_RecordGetString(rec,1);
4110
4111     rc = MSIREG_OpenUserComponentsKey(compgroupid, &hkey, TRUE);
4112     if (rc != ERROR_SUCCESS)
4113         goto end;
4114     
4115     text = MSI_RecordGetString(rec,4);
4116     qualifier = MSI_RecordGetString(rec,2);
4117     feature = MSI_RecordGetString(rec,5);
4118   
4119     advertise = create_component_advertise_string(package, 
4120                     &package->components[index], feature);
4121
4122     sz = strlenW(advertise);
4123
4124     if (text)
4125         sz += lstrlenW(text);
4126
4127     sz+=3;
4128     sz *= sizeof(WCHAR);
4129            
4130     output = HeapAlloc(GetProcessHeap(),0,sz);
4131     memset(output,0,sz);
4132     strcpyW(output,advertise);
4133
4134     if (text)
4135         strcatW(output,text);
4136
4137     sz = (lstrlenW(output)+2) * sizeof(WCHAR);
4138     RegSetValueExW(hkey, qualifier,0,REG_MULTI_SZ, (LPBYTE)output, sz);
4139     
4140 end:
4141     RegCloseKey(hkey);
4142     HeapFree(GetProcessHeap(),0,output);
4143     
4144     return rc;
4145 }
4146
4147 /*
4148  * At present I am ignorning the advertised components part of this and only
4149  * focusing on the qualified component sets
4150  */
4151 static UINT ACTION_PublishComponents(MSIPACKAGE *package)
4152 {
4153     UINT rc;
4154     MSIQUERY * view;
4155     static const WCHAR ExecSeqQuery[] =
4156         {'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
4157          '`','P','u','b','l','i','s','h',
4158          'C','o','m','p','o','n','e','n','t','`',0};
4159     
4160     rc = MSI_DatabaseOpenViewW(package->db, ExecSeqQuery, &view);
4161     if (rc != ERROR_SUCCESS)
4162         return ERROR_SUCCESS;
4163
4164     rc = MSI_IterateRecords(view, NULL, ITERATE_PublishComponent, package);
4165     msiobj_release(&view->hdr);
4166
4167     return rc;
4168 }