Generate stub entries on the fly for missing entry points instead of
[wine] / dlls / msi / action.c
1 /*
2  * Implementation of the Microsoft Installer (msi.dll)
3  *
4  * Copyright 2004 Aric Stewart for CodeWeavers
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
20
21 /*
22  * Pages I need
23  *
24 http://msdn.microsoft.com/library/default.asp?url=/library/en-us/msi/setup/installexecutesequence_table.asp
25
26 http://msdn.microsoft.com/library/default.asp?url=/library/en-us/msi/setup/standard_actions_reference.asp
27  */
28
29 #include <stdarg.h>
30 #include <stdio.h>
31
32 #define COBJMACROS
33
34 #include "windef.h"
35 #include "winbase.h"
36 #include "winerror.h"
37 #include "winreg.h"
38 #include "wine/debug.h"
39 #include "fdi.h"
40 #include "msi.h"
41 #include "msiquery.h"
42 #include "msvcrt/fcntl.h"
43 #include "objbase.h"
44 #include "objidl.h"
45 #include "msipriv.h"
46 #include "winnls.h"
47 #include "winuser.h"
48 #include "shlobj.h"
49 #include "wine/unicode.h"
50 #include "ver.h"
51
52 #define CUSTOM_ACTION_TYPE_MASK 0x3F
53
54 WINE_DEFAULT_DEBUG_CHANNEL(msi);
55
56 typedef struct tagMSIFEATURE
57 {
58     WCHAR Feature[96];
59     WCHAR Feature_Parent[96];
60     WCHAR Title[0x100];
61     WCHAR Description[0x100];
62     INT Display;
63     INT Level;
64     WCHAR Directory[96];
65     INT Attributes;
66     
67     INSTALLSTATE State;
68     BOOL Enabled;
69     INT ComponentCount;
70     INT Components[1024]; /* yes hardcoded limit.... I am bad */
71     INT Cost;
72 } MSIFEATURE;
73
74 typedef struct tagMSICOMPONENT
75 {
76     WCHAR Component[96];
77     WCHAR ComponentId[96];
78     WCHAR Directory[96];
79     INT Attributes;
80     WCHAR Condition[0x100];
81     WCHAR KeyPath[96];
82
83     INSTALLSTATE State;
84     BOOL FeatureState;
85     BOOL Enabled;
86     INT  Cost;
87 } MSICOMPONENT;
88
89 typedef struct tagMSIFOLDER
90 {
91     WCHAR Directory[96];
92     WCHAR TargetDefault[96];
93     WCHAR SourceDefault[96];
94
95     WCHAR ResolvedTarget[MAX_PATH];
96     WCHAR ResolvedSource[MAX_PATH];
97     WCHAR Property[MAX_PATH];   /* initially set property */
98     INT   ParentIndex;
99     INT   State;
100         /* 0 = uninitialized */
101         /* 1 = existing */
102         /* 2 = created remove if empty */
103         /* 3 = created persist if empty */
104     INT   Cost;
105     INT   Space;
106 }MSIFOLDER;
107
108 typedef struct tagMSIFILE
109 {
110     WCHAR File[72];
111     INT ComponentIndex;
112     WCHAR FileName[MAX_PATH];
113     INT FileSize;
114     WCHAR Version[72];
115     WCHAR Language[20];
116     INT Attributes;
117     INT Sequence;   
118
119     INT State;
120        /* 0 = uninitialize */
121        /* 1 = not present */
122        /* 2 = present but replace */
123        /* 3 = present do not replace */
124        /* 4 = Installed */
125     WCHAR   SourcePath[MAX_PATH];
126     WCHAR   TargetPath[MAX_PATH];
127     BOOL    Temporary; 
128 }MSIFILE;
129
130 /*
131  * Prototypes
132  */
133 static UINT ACTION_ProcessExecSequence(MSIPACKAGE *package, BOOL UIran);
134 static UINT ACTION_ProcessUISequence(MSIPACKAGE *package);
135
136 UINT ACTION_PerformAction(MSIPACKAGE *package, const WCHAR *action);
137
138 static UINT ACTION_LaunchConditions(MSIPACKAGE *package);
139 static UINT ACTION_CostInitialize(MSIPACKAGE *package);
140 static UINT ACTION_CreateFolders(MSIPACKAGE *package);
141 static UINT ACTION_CostFinalize(MSIPACKAGE *package);
142 static UINT ACTION_FileCost(MSIPACKAGE *package);
143 static UINT ACTION_InstallFiles(MSIPACKAGE *package);
144 static UINT ACTION_DuplicateFiles(MSIPACKAGE *package);
145 static UINT ACTION_WriteRegistryValues(MSIPACKAGE *package);
146 static UINT ACTION_CustomAction(MSIPACKAGE *package,const WCHAR *action);
147 static UINT ACTION_InstallInitialize(MSIPACKAGE *package);
148 static UINT ACTION_InstallValidate(MSIPACKAGE *package);
149 static UINT ACTION_ProcessComponents(MSIPACKAGE *package);
150 static UINT ACTION_RegisterTypeLibraries(MSIPACKAGE *package);
151 static UINT ACTION_RegisterClassInfo(MSIPACKAGE *package);
152 static UINT ACTION_RegisterProgIdInfo(MSIPACKAGE *package);
153 static UINT ACTION_CreateShortcuts(MSIPACKAGE *package);
154 static UINT ACTION_PublishProduct(MSIPACKAGE *package);
155
156 static UINT HANDLE_CustomType1(MSIPACKAGE *package, const LPWSTR source, 
157                                 const LPWSTR target, const INT type);
158 static UINT HANDLE_CustomType2(MSIPACKAGE *package, const LPWSTR source, 
159                                 const LPWSTR target, const INT type);
160 static UINT HANDLE_CustomType18(MSIPACKAGE *package, const LPWSTR source, 
161                                 const LPWSTR target, const INT type);
162 static UINT HANDLE_CustomType50(MSIPACKAGE *package, const LPWSTR source, 
163                                 const LPWSTR target, const INT type);
164 static UINT HANDLE_CustomType34(MSIPACKAGE *package, const LPWSTR source, 
165                                 const LPWSTR target, const INT type);
166
167 static DWORD deformat_string(MSIPACKAGE *package, WCHAR* ptr,WCHAR** data);
168 static UINT resolve_folder(MSIPACKAGE *package, LPCWSTR name, LPWSTR path, 
169                            BOOL source, BOOL set_prop, MSIFOLDER **folder);
170
171 static int track_tempfile(MSIPACKAGE *package, LPCWSTR name, LPCWSTR path);
172  
173 /*
174  * consts and values used
175  */
176 static const WCHAR cszSourceDir[] = {'S','o','u','r','c','e','D','i','r',0};
177 static const WCHAR cszRootDrive[] = {'R','O','O','T','D','R','I','V','E',0};
178 static const WCHAR cszTargetDir[] = {'T','A','R','G','E','T','D','I','R',0};
179 static const WCHAR cszTempFolder[]= {'T','e','m','p','F','o','l','d','e','r',0};
180 static const WCHAR cszDatabase[]={'D','A','T','A','B','A','S','E',0};
181 static const WCHAR c_collen[] = {'C',':','\\',0};
182  
183 static const WCHAR cszlsb[]={'[',0};
184 static const WCHAR cszrsb[]={']',0};
185 static const WCHAR cszbs[]={'\\',0};
186
187 const static WCHAR szCreateFolders[] =
188     {'C','r','e','a','t','e','F','o','l','d','e','r','s',0};
189 const static WCHAR szCostFinalize[] =
190     {'C','o','s','t','F','i','n','a','l','i','z','e',0};
191 const static WCHAR szInstallFiles[] =
192     {'I','n','s','t','a','l','l','F','i','l','e','s',0};
193 const static WCHAR szDuplicateFiles[] =
194     {'D','u','p','l','i','c','a','t','e','F','i','l','e','s',0};
195 const static WCHAR szWriteRegistryValues[] =
196 {'W','r','i','t','e','R','e','g','i','s','t','r','y','V','a','l','u','e','s',0};
197 const static WCHAR szCostInitialize[] =
198     {'C','o','s','t','I','n','i','t','i','a','l','i','z','e',0};
199 const static WCHAR szFileCost[] = {'F','i','l','e','C','o','s','t',0};
200 const static WCHAR szInstallInitialize[] = 
201     {'I','n','s','t','a','l','l','I','n','i','t','i','a','l','i','z','e',0};
202 const static WCHAR szInstallValidate[] = 
203     {'I','n','s','t','a','l','l','V','a','l','i','d','a','t','e',0};
204 const static WCHAR szLaunchConditions[] = 
205     {'L','a','u','n','c','h','C','o','n','d','i','t','i','o','n','s',0};
206 const static WCHAR szProcessComponents[] = 
207     {'P','r','o','c','e','s','s','C','o','m','p','o','n','e','n','t','s',0};
208 const static WCHAR szRegisterTypeLibraries[] = 
209 {'R','e','g','i','s','t','e','r','T','y','p','e','L','i','b','r','a','r',
210 'i','e','s',0};
211 const static WCHAR szRegisterClassInfo[] = 
212 {'R','e','g','i','s','t','e','r','C','l','a','s','s','I','n','f','o',0};
213 const static WCHAR szRegisterProgIdInfo[] = 
214 {'R','e','g','i','s','t','e','r','P','r','o','g','I','d','I','n','f','o',0};
215 const static WCHAR szCreateShortcuts[] = 
216 {'C','r','e','a','t','e','S','h','o','r','t','c','u','t','s',0};
217 const static WCHAR szPublishProduct[] = 
218 {'P','u','b','l','i','s','h','P','r','o','d','u','c','t',0};
219
220 /******************************************************** 
221  * helper functions to get around current HACKS and such
222  ********************************************************/
223 inline static void reduce_to_longfilename(WCHAR* filename)
224 {
225     if (strchrW(filename,'|'))
226     {
227         WCHAR newname[MAX_PATH];
228         strcpyW(newname,strchrW(filename,'|')+1);
229         strcpyW(filename,newname);
230     }
231 }
232
233 inline static char *strdupWtoA( const WCHAR *str )
234 {
235     char *ret = NULL;
236     if (str)
237     {
238         DWORD len = WideCharToMultiByte( CP_ACP, 0, str, -1, NULL, 0, NULL, NULL
239 );
240         if ((ret = HeapAlloc( GetProcessHeap(), 0, len )))
241             WideCharToMultiByte( CP_ACP, 0, str, -1, ret, len, NULL, NULL );
242     }
243     return ret;
244 }
245
246 inline static WCHAR *strdupAtoW( const char *str )
247 {
248     WCHAR *ret = NULL;
249     if (str)
250     {
251         DWORD len = MultiByteToWideChar( CP_ACP, 0, str, -1, NULL, 0 );
252         if ((ret = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) )))
253             MultiByteToWideChar( CP_ACP, 0, str, -1, ret, len );
254     }
255     return ret;
256 }
257
258 inline static WCHAR *load_dynamic_stringW(MSIRECORD *row, INT index)
259 {
260     UINT rc;
261     DWORD sz;
262     LPWSTR ret;
263    
264     sz = 0; 
265     rc = MSI_RecordGetStringW(row,index,NULL,&sz);
266     if (sz <= 0)
267         return NULL;
268
269     sz ++;
270     ret = HeapAlloc(GetProcessHeap(),0,sz * sizeof (WCHAR));
271     rc = MSI_RecordGetStringW(row,index,ret,&sz);
272     return ret;
273 }
274
275 inline static int get_loaded_component(MSIPACKAGE* package, LPCWSTR Component )
276 {
277     int rc = -1;
278     DWORD i;
279
280     for (i = 0; i < package->loaded_components; i++)
281     {
282         if (strcmpW(Component,package->components[i].Component)==0)
283         {
284             rc = i;
285             break;
286         }
287     }
288     return rc;
289 }
290
291 inline static int get_loaded_feature(MSIPACKAGE* package, LPCWSTR Feature )
292 {
293     int rc = -1;
294     DWORD i;
295
296     for (i = 0; i < package->loaded_features; i++)
297     {
298         if (strcmpW(Feature,package->features[i].Feature)==0)
299         {
300             rc = i;
301             break;
302         }
303     }
304     return rc;
305 }
306
307 inline static int get_loaded_file(MSIPACKAGE* package, LPCWSTR file)
308 {
309     int rc = -1;
310     DWORD i;
311
312     for (i = 0; i < package->loaded_files; i++)
313     {
314         if (strcmpW(file,package->files[i].File)==0)
315         {
316             rc = i;
317             break;
318         }
319     }
320     return rc;
321 }
322
323 static int track_tempfile(MSIPACKAGE *package, LPCWSTR name, LPCWSTR path)
324 {
325     DWORD i;
326     DWORD index;
327
328     if (!package)
329         return -2;
330
331     for (i=0; i < package->loaded_files; i++)
332         if (strcmpW(package->files[i].File,name)==0)
333             return -1;
334
335     index = package->loaded_files;
336     package->loaded_files++;
337     if (package->loaded_files== 1)
338         package->files = HeapAlloc(GetProcessHeap(),0,sizeof(MSIFILE));
339     else
340         package->files = HeapReAlloc(GetProcessHeap(),0,
341             package->files , package->loaded_files * sizeof(MSIFILE));
342
343     memset(&package->files[index],0,sizeof(MSIFILE));
344
345     strcpyW(package->files[index].File,name);
346     strcpyW(package->files[index].TargetPath,path);
347     package->files[index].Temporary = TRUE;
348
349     TRACE("Tracking tempfile (%s)\n",debugstr_w(package->files[index].File));  
350
351     return 0;
352 }
353
354 void ACTION_remove_tracked_tempfiles(MSIPACKAGE* package)
355 {
356     DWORD i;
357
358     if (!package)
359         return;
360
361     for (i = 0; i < package->loaded_files; i++)
362     {
363         if (package->files[i].Temporary)
364             DeleteFileW(package->files[i].TargetPath);
365
366     }
367 }
368
369 static void ui_progress(MSIPACKAGE *package, int a, int b, int c, int d )
370 {
371     MSIRECORD * row;
372
373     row = MSI_CreateRecord(4);
374     MSI_RecordSetInteger(row,1,a);
375     MSI_RecordSetInteger(row,2,b);
376     MSI_RecordSetInteger(row,3,c);
377     MSI_RecordSetInteger(row,4,d);
378     MSI_ProcessMessage(package, INSTALLMESSAGE_PROGRESS, row);
379     msiobj_release(&row->hdr);
380 }
381
382 static void ui_actiondata(MSIPACKAGE *package, LPCWSTR action, MSIRECORD * record)
383 {
384     static const WCHAR Query_t[] = 
385 {'S','E','L','E','C','T',' ','*',' ','f','r','o','m',' ','A','c','t','i','o',
386 'n','T','e','x','t',' ','w','h','e','r','e',' ','A','c','t','i','o','n',' ','=',
387 ' ','\'','%','s','\'',0};
388     WCHAR message[1024];
389     UINT rc;
390     MSIQUERY * view;
391     MSIRECORD * row = 0;
392     static WCHAR *ActionFormat=NULL;
393     static WCHAR LastAction[0x100] = {0};
394     WCHAR Query[1024];
395     LPWSTR ptr;
396
397     if (strcmpW(LastAction,action)!=0)
398     {
399         sprintfW(Query,Query_t,action);
400         rc = MSI_DatabaseOpenViewW(package->db, Query, &view);
401         if (rc != ERROR_SUCCESS)
402             return;
403         rc = MSI_ViewExecute(view, 0);
404         if (rc != ERROR_SUCCESS)
405         {
406             MSI_ViewClose(view);
407             return;
408         }
409         rc = MSI_ViewFetch(view,&row);
410         if (rc != ERROR_SUCCESS)
411         {
412             MSI_ViewClose(view);
413             return;
414         }
415
416         if (MSI_RecordIsNull(row,3))
417         {
418             msiobj_release(&row->hdr);
419             MSI_ViewClose(view);
420             msiobj_release(&view->hdr);
421             return;
422         }
423
424         if (ActionFormat)
425             HeapFree(GetProcessHeap(),0,ActionFormat);
426
427         ActionFormat = load_dynamic_stringW(row,3);
428         strcpyW(LastAction,action);
429         msiobj_release(&row->hdr);
430         MSI_ViewClose(view);
431         msiobj_release(&view->hdr);
432     }
433
434     message[0]=0;
435     ptr = ActionFormat;
436     while (*ptr)
437     {
438         LPWSTR ptr2;
439         LPWSTR data=NULL;
440         WCHAR tmp[1023];
441         INT field;
442
443         ptr2 = strchrW(ptr,'[');
444         if (ptr2)
445         {
446             strncpyW(tmp,ptr,ptr2-ptr);
447             tmp[ptr2-ptr]=0;
448             strcatW(message,tmp);
449             ptr2++;
450             field = atoiW(ptr2);
451             data = load_dynamic_stringW(record,field);
452             if (data)
453             {
454                 strcatW(message,data);
455                 HeapFree(GetProcessHeap(),0,data);
456             }
457             ptr=strchrW(ptr2,']');
458             ptr++;
459         }
460         else
461         {
462             strcatW(message,ptr);
463             break;
464         }
465     }
466
467     row = MSI_CreateRecord(1);
468     MSI_RecordSetStringW(row,1,message);
469  
470     MSI_ProcessMessage(package, INSTALLMESSAGE_ACTIONDATA, row);
471     msiobj_release(&row->hdr);
472 }
473
474
475 static void ui_actionstart(MSIPACKAGE *package, LPCWSTR action)
476 {
477     static const WCHAR template_s[]=
478 {'A','c','t','i','o','n',' ','%','s',':',' ','%','s','.',' ','%','s','.',0};
479     static const WCHAR format[] = 
480 {'H','H','\'',':','\'','m','m','\'',':','\'','s','s',0};
481     static const WCHAR Query_t[] = 
482 {'S','E','L','E','C','T',' ','*',' ','f','r','o','m',' ','A','c','t','i','o',
483 'n','T','e','x','t',' ','w','h','e','r','e',' ','A','c','t','i','o','n',' ','=',
484 ' ','\'','%','s','\'',0};
485     WCHAR message[1024];
486     WCHAR timet[0x100];
487     UINT rc;
488     MSIQUERY * view;
489     MSIRECORD * row = 0;
490     WCHAR *ActionText=NULL;
491     WCHAR Query[1024];
492
493     GetTimeFormatW(LOCALE_USER_DEFAULT, 0, NULL, format, timet, 0x100);
494
495     sprintfW(Query,Query_t,action);
496     rc = MSI_DatabaseOpenViewW(package->db, Query, &view);
497     if (rc != ERROR_SUCCESS)
498         return;
499     rc = MSI_ViewExecute(view, 0);
500     if (rc != ERROR_SUCCESS)
501     {
502         MSI_ViewClose(view);
503         msiobj_release(&view->hdr);
504         return;
505     }
506     rc = MSI_ViewFetch(view,&row);
507     if (rc != ERROR_SUCCESS)
508     {
509         MSI_ViewClose(view);
510         msiobj_release(&view->hdr);
511         return;
512     }
513
514     ActionText = load_dynamic_stringW(row,2);
515     msiobj_release(&row->hdr);
516     MSI_ViewClose(view);
517     msiobj_release(&view->hdr);
518
519     sprintfW(message,template_s,timet,action,ActionText);
520
521     row = MSI_CreateRecord(1);
522     MSI_RecordSetStringW(row,1,message);
523  
524     MSI_ProcessMessage(package, INSTALLMESSAGE_ACTIONSTART, row);
525     msiobj_release(&row->hdr);
526     HeapFree(GetProcessHeap(),0,ActionText);
527 }
528
529 static void ui_actioninfo(MSIPACKAGE *package, LPCWSTR action, BOOL start, 
530                           UINT rc)
531 {
532     MSIRECORD * row;
533     static const WCHAR template_s[]=
534 {'A','c','t','i','o','n',' ','s','t','a','r','t',' ','%','s',':',' ','%','s',
535 '.',0};
536     static const WCHAR template_e[]=
537 {'A','c','t','i','o','n',' ','e','n','d','e','d',' ','%','s',':',' ','%','s',
538 '.',' ','R','e','t','u','r','n',' ','v','a','l','u','e',' ','%','i','.',0};
539     static const WCHAR format[] = 
540 {'H','H','\'',':','\'','m','m','\'',':','\'','s','s',0};
541     WCHAR message[1024];
542     WCHAR timet[0x100];
543
544     GetTimeFormatW(LOCALE_USER_DEFAULT, 0, NULL, format, timet, 0x100);
545     if (start)
546         sprintfW(message,template_s,timet,action);
547     else
548         sprintfW(message,template_e,timet,action,rc);
549     
550     row = MSI_CreateRecord(1);
551     MSI_RecordSetStringW(row,1,message);
552  
553     MSI_ProcessMessage(package, INSTALLMESSAGE_INFO, row);
554     msiobj_release(&row->hdr);
555 }
556
557 /****************************************************
558  * TOP level entry points 
559  *****************************************************/
560
561 UINT ACTION_DoTopLevelINSTALL(MSIPACKAGE *package, LPCWSTR szPackagePath,
562                               LPCWSTR szCommandLine)
563 {
564     DWORD sz;
565     WCHAR buffer[10];
566     UINT rc;
567     static const WCHAR szUILevel[] = {'U','I','L','e','v','e','l',0};
568
569     if (szPackagePath)   
570     {
571         LPWSTR p;
572         WCHAR check[MAX_PATH];
573         WCHAR pth[MAX_PATH];
574         DWORD size;
575  
576         strcpyW(pth,szPackagePath);
577         p = strrchrW(pth,'\\');    
578         if (p)
579         {
580             p++;
581             *p=0;
582         }
583
584         size = MAX_PATH;
585         if (MSI_GetPropertyW(package,cszSourceDir,check,&size) 
586             != ERROR_SUCCESS )
587             MSI_SetPropertyW(package, cszSourceDir, pth);
588     }
589
590     if (szCommandLine)
591     {
592         LPWSTR ptr,ptr2;
593         ptr = (LPWSTR)szCommandLine;
594        
595         while (*ptr)
596         {
597             WCHAR prop[0x100];
598             WCHAR val[0x100];
599
600             TRACE("Looking at %s\n",debugstr_w(ptr));
601
602             ptr2 = strchrW(ptr,'=');
603             if (ptr2)
604             {
605                 BOOL quote=FALSE;
606                 DWORD len = 0;
607
608                 while (*ptr == ' ') ptr++;
609                 strncpyW(prop,ptr,ptr2-ptr);
610                 prop[ptr2-ptr]=0;
611                 ptr2++;
612             
613                 ptr = ptr2; 
614                 while (*ptr && (quote || (!quote && *ptr!=' ')))
615                 {
616                     if (*ptr == '"')
617                         quote = !quote;
618                     ptr++;
619                     len++;
620                 }
621                
622                 if (*ptr2=='"')
623                 {
624                     ptr2++;
625                     len -= 2;
626                 }
627                 strncpyW(val,ptr2,len);
628                 val[len]=0;
629
630                 if (strlenW(prop) > 0)
631                 {
632                     TRACE("Found commandline property (%s) = (%s)\n", debugstr_w(prop), debugstr_w(val));
633                     MSI_SetPropertyW(package,prop,val);
634                 }
635             }
636             ptr++;
637         }
638     }
639   
640     sz = 10; 
641     if (MSI_GetPropertyW(package,szUILevel,buffer,&sz) == ERROR_SUCCESS)
642     {
643         if (atoiW(buffer) >= INSTALLUILEVEL_REDUCED)
644         {
645             rc = ACTION_ProcessUISequence(package);
646             if (rc == ERROR_SUCCESS)
647                 rc = ACTION_ProcessExecSequence(package,TRUE);
648         }
649         else
650             rc = ACTION_ProcessExecSequence(package,FALSE);
651     }
652     else
653         rc = ACTION_ProcessExecSequence(package,FALSE);
654
655     return rc;
656 }
657
658
659 static UINT ACTION_ProcessExecSequence(MSIPACKAGE *package, BOOL UIran)
660 {
661     MSIQUERY * view;
662     UINT rc;
663     static const WCHAR ExecSeqQuery[] =  {
664        's','e','l','e','c','t',' ','*',' ',
665        'f','r','o','m',' ',
666            'I','n','s','t','a','l','l','E','x','e','c','u','t','e',
667            'S','e','q','u','e','n','c','e',' ',
668        'w','h','e','r','e',' ','S','e','q','u','e','n','c','e',' ',
669            '>',' ','%','i',' ','o','r','d','e','r',' ',
670        'b','y',' ','S','e','q','u','e','n','c','e',0 };
671     WCHAR Query[1024];
672     MSIRECORD * row = 0;
673     static const WCHAR IVQuery[] = {
674        's','e','l','e','c','t',' ','S','e','q','u','e','n','c','e',' ',
675        'f','r','o','m',' ','I','n','s','t','a','l','l',
676            'E','x','e','c','u','t','e','S','e','q','u','e','n','c','e',' ',
677        'w','h','e','r','e',' ','A','c','t','i','o','n',' ','=',' ',
678            '`','I','n','s','t','a','l','l','V','a','l','i','d','a','t','e','`',
679        0};
680
681     if (UIran)
682     {
683         INT seq = 0;
684         
685         rc = MSI_DatabaseOpenViewW(package->db, IVQuery, &view);
686         if (rc != ERROR_SUCCESS)
687             return rc;
688         rc = MSI_ViewExecute(view, 0);
689         if (rc != ERROR_SUCCESS)
690         {
691             MSI_ViewClose(view);
692             msiobj_release(&view->hdr);
693             return rc;
694         }
695         rc = MSI_ViewFetch(view,&row);
696         if (rc != ERROR_SUCCESS)
697         {
698             MSI_ViewClose(view);
699             msiobj_release(&view->hdr);
700             return rc;
701         }
702         seq = MSI_RecordGetInteger(row,1);
703         msiobj_release(&row->hdr);
704         MSI_ViewClose(view);
705         msiobj_release(&view->hdr);
706         sprintfW(Query,ExecSeqQuery,seq);
707     }
708     else
709         sprintfW(Query,ExecSeqQuery,0);
710     
711     rc = MSI_DatabaseOpenViewW(package->db, Query, &view);
712     if (rc == ERROR_SUCCESS)
713     {
714         rc = MSI_ViewExecute(view, 0);
715
716         if (rc != ERROR_SUCCESS)
717         {
718             MSI_ViewClose(view);
719             msiobj_release(&view->hdr);
720             goto end;
721         }
722        
723         TRACE("Running the actions \n"); 
724
725         while (1)
726         {
727             WCHAR buffer[0x100];
728             DWORD sz = 0x100;
729
730             rc = MSI_ViewFetch(view,&row);
731             if (rc != ERROR_SUCCESS)
732             {
733                 rc = ERROR_SUCCESS;
734                 break;
735             }
736
737             /* check conditions */
738             if (!MSI_RecordIsNull(row,2))
739             {
740                 LPWSTR cond = NULL;
741                 cond = load_dynamic_stringW(row,2);
742
743                 if (cond)
744                 {
745                     /* this is a hack to skip errors in the condition code */
746                     if (MSI_EvaluateConditionW(package, cond) ==
747                             MSICONDITION_FALSE)
748                     {
749                         HeapFree(GetProcessHeap(),0,cond);
750                         msiobj_release(&row->hdr);
751                         continue; 
752                     }
753                     else
754                         HeapFree(GetProcessHeap(),0,cond);
755                 }
756             }
757
758             sz=0x100;
759             rc =  MSI_RecordGetStringW(row,1,buffer,&sz);
760             if (rc != ERROR_SUCCESS)
761             {
762                 ERR("Error is %x\n",rc);
763                 msiobj_release(&row->hdr);
764                 break;
765             }
766
767             rc = ACTION_PerformAction(package,buffer);
768
769             if (rc == ERROR_FUNCTION_NOT_CALLED)
770                 rc = ERROR_SUCCESS;
771
772             if (rc != ERROR_SUCCESS)
773             {
774                 ERR("Execution halted due to error (%i)\n",rc);
775                 msiobj_release(&row->hdr);
776                 break;
777             }
778
779             msiobj_release(&row->hdr);
780         }
781
782         MSI_ViewClose(view);
783         msiobj_release(&view->hdr);
784     }
785
786 end:
787     return rc;
788 }
789
790
791 static UINT ACTION_ProcessUISequence(MSIPACKAGE *package)
792 {
793     MSIQUERY * view;
794     UINT rc;
795     static const WCHAR ExecSeqQuery [] = {
796       's','e','l','e','c','t',' ','*',' ',
797       'f','r','o','m',' ','I','n','s','t','a','l','l',
798             'U','I','S','e','q','u','e','n','c','e',' ',
799       'w','h','e','r','e',' ','S','e','q','u','e','n','c','e',' ', '>',' ','0',' ',
800       'o','r','d','e','r',' ','b','y',' ','S','e','q','u','e','n','c','e',0};
801     
802     rc = MSI_DatabaseOpenViewW(package->db, ExecSeqQuery, &view);
803     
804     if (rc == ERROR_SUCCESS)
805     {
806         rc = MSI_ViewExecute(view, 0);
807
808         if (rc != ERROR_SUCCESS)
809         {
810             MSI_ViewClose(view);
811             msiobj_release(&view->hdr);
812             goto end;
813         }
814        
815         TRACE("Running the actions \n"); 
816
817         while (1)
818         {
819             WCHAR buffer[0x100];
820             DWORD sz = 0x100;
821             MSIRECORD * row = 0;
822
823             rc = MSI_ViewFetch(view,&row);
824             if (rc != ERROR_SUCCESS)
825             {
826                 rc = ERROR_SUCCESS;
827                 break;
828             }
829
830             /* check conditions */
831             if (!MSI_RecordIsNull(row,2))
832             {
833                 LPWSTR cond = NULL;
834                 cond = load_dynamic_stringW(row,2);
835
836                 if (cond)
837                 {
838                     /* this is a hack to skip errors in the condition code */
839                     if (MSI_EvaluateConditionW(package, cond) ==
840                             MSICONDITION_FALSE)
841                     {
842                         HeapFree(GetProcessHeap(),0,cond);
843                         msiobj_release(&row->hdr);
844                         continue; 
845                     }
846                     else
847                         HeapFree(GetProcessHeap(),0,cond);
848                 }
849             }
850
851             sz=0x100;
852             rc =  MSI_RecordGetStringW(row,1,buffer,&sz);
853             if (rc != ERROR_SUCCESS)
854             {
855                 ERR("Error is %x\n",rc);
856                 msiobj_release(&row->hdr);
857                 break;
858             }
859
860             rc = ACTION_PerformAction(package,buffer);
861
862             if (rc == ERROR_FUNCTION_NOT_CALLED)
863                 rc = ERROR_SUCCESS;
864
865             if (rc != ERROR_SUCCESS)
866             {
867                 ERR("Execution halted due to error (%i)\n",rc);
868                 msiobj_release(&row->hdr);
869                 break;
870             }
871
872             msiobj_release(&row->hdr);
873         }
874
875         MSI_ViewClose(view);
876         msiobj_release(&view->hdr);
877     }
878
879 end:
880     return rc;
881 }
882
883 /********************************************************
884  * ACTION helper functions and functions that perform the actions
885  *******************************************************/
886
887 /* 
888  * Alot of actions are really important even if they don't do anything
889  * explicit.. Lots of properties are set at the beginning of the installation
890  * CostFinalize does a bunch of work to translated the directories and such
891  * 
892  * But until I get write access to the database that is hard, so I am going to
893  * hack it to see if I can get something to run.
894  */
895 UINT ACTION_PerformAction(MSIPACKAGE *package, const WCHAR *action)
896 {
897     UINT rc = ERROR_SUCCESS; 
898
899     TRACE("Performing action (%s)\n",debugstr_w(action));
900     ui_actioninfo(package, action, TRUE, 0);
901     ui_actionstart(package, action);
902     ui_progress(package,2,1,0,0);
903
904     /* pre install, setup and configuration block */
905     if (strcmpW(action,szLaunchConditions)==0)
906         rc = ACTION_LaunchConditions(package);
907     else if (strcmpW(action,szCostInitialize)==0)
908         rc = ACTION_CostInitialize(package);
909     else if (strcmpW(action,szFileCost)==0)
910         rc = ACTION_FileCost(package);
911     else if (strcmpW(action,szCostFinalize)==0)
912         rc = ACTION_CostFinalize(package);
913     else if (strcmpW(action,szInstallValidate)==0)
914         rc = ACTION_InstallValidate(package);
915
916     /* install block */
917     else if (strcmpW(action,szProcessComponents)==0)
918         rc = ACTION_ProcessComponents(package);
919     else if (strcmpW(action,szInstallInitialize)==0)
920         rc = ACTION_InstallInitialize(package);
921     else if (strcmpW(action,szCreateFolders)==0)
922         rc = ACTION_CreateFolders(package);
923     else if (strcmpW(action,szInstallFiles)==0)
924         rc = ACTION_InstallFiles(package);
925     else if (strcmpW(action,szDuplicateFiles)==0)
926         rc = ACTION_DuplicateFiles(package);
927     else if (strcmpW(action,szWriteRegistryValues)==0)
928         rc = ACTION_WriteRegistryValues(package);
929      else if (strcmpW(action,szRegisterTypeLibraries)==0)
930         rc = ACTION_RegisterTypeLibraries(package);
931      else if (strcmpW(action,szRegisterClassInfo)==0)
932         rc = ACTION_RegisterClassInfo(package);
933      else if (strcmpW(action,szRegisterProgIdInfo)==0)
934         rc = ACTION_RegisterProgIdInfo(package);
935      else if (strcmpW(action,szCreateShortcuts)==0)
936         rc = ACTION_CreateShortcuts(package);
937     else if (strcmpW(action,szPublishProduct)==0)
938         rc = ACTION_PublishProduct(package);
939
940     /*
941      Called during iTunes but unimplemented and seem important
942
943      ResolveSource  (sets SourceDir)
944      RegisterProduct
945      InstallFinalize
946      */
947      else if ((rc = ACTION_CustomAction(package,action)) != ERROR_SUCCESS)
948      {
949         FIXME("UNHANDLED MSI ACTION %s\n",debugstr_w(action));
950         rc = ERROR_FUNCTION_NOT_CALLED;
951      }
952
953     ui_actioninfo(package, action, FALSE, rc);
954     return rc;
955 }
956
957
958 static UINT ACTION_CustomAction(MSIPACKAGE *package,const WCHAR *action)
959 {
960     UINT rc = ERROR_SUCCESS;
961     MSIQUERY * view;
962     MSIRECORD * row = 0;
963     WCHAR ExecSeqQuery[1024] = 
964     {'s','e','l','e','c','t',' ','*',' ','f','r','o','m',' ','C','u','s','t','o'
965 ,'m','A','c','t','i','o','n',' ','w','h','e','r','e',' ','`','A','c','t','i'
966 ,'o','n','`',' ','=',' ','`',0};
967     static const WCHAR end[]={'`',0};
968     UINT type;
969     LPWSTR source;
970     LPWSTR target;
971     WCHAR *deformated=NULL;
972
973     strcatW(ExecSeqQuery,action);
974     strcatW(ExecSeqQuery,end);
975
976     rc = MSI_DatabaseOpenViewW(package->db, ExecSeqQuery, &view);
977
978     if (rc != ERROR_SUCCESS)
979         return rc;
980
981     rc = MSI_ViewExecute(view, 0);
982     if (rc != ERROR_SUCCESS)
983     {
984         MSI_ViewClose(view);
985         msiobj_release(&view->hdr);
986         return rc;
987     }
988
989     rc = MSI_ViewFetch(view,&row);
990     if (rc != ERROR_SUCCESS)
991     {
992         MSI_ViewClose(view);
993         msiobj_release(&view->hdr);
994         return rc;
995     }
996
997     type = MSI_RecordGetInteger(row,2);
998
999     source = load_dynamic_stringW(row,3);
1000     target = load_dynamic_stringW(row,4);
1001
1002     TRACE("Handling custom action %s (%x %s %s)\n",debugstr_w(action),type,
1003           debugstr_w(source), debugstr_w(target));
1004
1005     /* we are ignoring ALOT of flags and important synchronization stuff */
1006     switch (type & CUSTOM_ACTION_TYPE_MASK)
1007     {
1008         case 1: /* DLL file stored in a Binary table stream */
1009             rc = HANDLE_CustomType1(package,source,target,type);
1010             break;
1011         case 2: /* EXE file stored in a Binary table strem */
1012             rc = HANDLE_CustomType2(package,source,target,type);
1013             break;
1014         case 18: /*EXE file installed with package */
1015             rc = HANDLE_CustomType18(package,source,target,type);
1016             break;
1017         case 50: /*EXE file specified by a property value */
1018             rc = HANDLE_CustomType50(package,source,target,type);
1019             break;
1020         case 34: /*EXE to be run in specified directory */
1021             rc = HANDLE_CustomType34(package,source,target,type);
1022             break;
1023         case 35: /* Directory set with formatted text. */
1024         case 51: /* Property set with formatted text. */
1025             deformat_string(package,target,&deformated);
1026             rc = MSI_SetPropertyW(package,source,deformated);
1027             HeapFree(GetProcessHeap(),0,deformated);
1028             break;
1029         default:
1030             FIXME("UNHANDLED ACTION TYPE %i (%s %s)\n",
1031              type & CUSTOM_ACTION_TYPE_MASK, debugstr_w(source),
1032              debugstr_w(target));
1033     }
1034
1035     HeapFree(GetProcessHeap(),0,source);
1036     HeapFree(GetProcessHeap(),0,target);
1037     msiobj_release(&row->hdr);
1038     MSI_ViewClose(view);
1039     msiobj_release(&view->hdr);
1040     return rc;
1041 }
1042
1043 static UINT store_binary_to_temp(MSIPACKAGE *package, const LPWSTR source, 
1044                                 LPWSTR tmp_file)
1045 {
1046     DWORD sz=MAX_PATH;
1047
1048     if (MSI_GetPropertyW(package, cszTempFolder, tmp_file, &sz) 
1049         != ERROR_SUCCESS)
1050         GetTempPathW(MAX_PATH,tmp_file);
1051
1052     strcatW(tmp_file,source);
1053
1054     if (GetFileAttributesW(tmp_file) != INVALID_FILE_ATTRIBUTES)
1055     {
1056         TRACE("File already exists\n");
1057         return ERROR_SUCCESS;
1058     }
1059     else
1060     {
1061         /* write out the file */
1062         UINT rc;
1063         MSIQUERY * view;
1064         MSIRECORD * row = 0;
1065         WCHAR Query[1024] =
1066         {'s','e','l','e','c','t',' ','*',' ','f','r','o','m',' ','B','i'
1067 ,'n','a','r','y',' ','w','h','e','r','e',' ','N','a','m','e','=','`',0};
1068         static const WCHAR end[]={'`',0};
1069         HANDLE the_file;
1070         CHAR buffer[1024];
1071
1072         if (track_tempfile(package, source, tmp_file)!=0)
1073             FIXME("File Name in temp tracking collision\n");
1074
1075         the_file = CreateFileW(tmp_file, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS,
1076                            FILE_ATTRIBUTE_NORMAL, NULL);
1077     
1078         if (the_file == INVALID_HANDLE_VALUE)
1079             return ERROR_FUNCTION_FAILED;
1080
1081         strcatW(Query,source);
1082         strcatW(Query,end);
1083
1084         rc = MSI_DatabaseOpenViewW( package->db, Query, &view);
1085         if (rc != ERROR_SUCCESS)
1086             return rc;
1087
1088         rc = MSI_ViewExecute(view, 0);
1089         if (rc != ERROR_SUCCESS)
1090         {
1091             MSI_ViewClose(view);
1092             msiobj_release(&view->hdr);
1093             return rc;
1094         }
1095
1096         rc = MSI_ViewFetch(view,&row);
1097         if (rc != ERROR_SUCCESS)
1098         {
1099             MSI_ViewClose(view);
1100             msiobj_release(&view->hdr);
1101             return rc;
1102         }
1103
1104         do 
1105         {
1106             DWORD write;
1107             sz = 1024;
1108             rc = MSI_RecordReadStream(row,2,buffer,&sz);
1109             if (rc != ERROR_SUCCESS)
1110             {
1111                 ERR("Failed to get stream\n");
1112                 CloseHandle(the_file);  
1113                 DeleteFileW(tmp_file);
1114                 break;
1115             }
1116             WriteFile(the_file,buffer,sz,&write,NULL);
1117         } while (sz == 1024);
1118
1119         CloseHandle(the_file);
1120
1121         msiobj_release(&row->hdr);
1122         MSI_ViewClose(view);
1123         msiobj_release(&view->hdr);
1124     }
1125
1126     return ERROR_SUCCESS;
1127 }
1128
1129
1130 typedef UINT __stdcall CustomEntry(MSIHANDLE);
1131 typedef struct 
1132 {
1133         MSIPACKAGE *package;
1134         WCHAR target[MAX_PATH];
1135         WCHAR source[MAX_PATH];
1136 } thread_struct;
1137
1138 #if 0
1139 static DWORD WINAPI DllThread(LPVOID info)
1140 {
1141     HANDLE DLL;
1142     LPSTR proc;
1143     thread_struct *stuff;
1144     CustomEntry *fn;
1145      
1146     stuff = (thread_struct*)info;
1147
1148     TRACE("Asynchronous start (%s, %s) \n", debugstr_w(stuff->source),
1149           debugstr_w(stuff->target));
1150
1151     DLL = LoadLibraryW(stuff->source);
1152     if (DLL)
1153     {
1154         proc = strdupWtoA( stuff->target );
1155         fn = (CustomEntry*)GetProcAddress(DLL,proc);
1156         if (fn)
1157         {
1158             MSIHANDLE hPackage;
1159             MSIPACKAGE *package = stuff->package;
1160
1161             TRACE("Calling function\n");
1162             hPackage = msiobj_findhandle( &package->hdr );
1163             if( !hPackage )
1164                 ERR("Handle for object %p not found\n", package );
1165             fn(hPackage);
1166             msiobj_release( &package->hdr );
1167         }
1168         else
1169             ERR("Cannot load functon\n");
1170
1171         HeapFree(GetProcessHeap(),0,proc);
1172         FreeLibrary(DLL);
1173     }
1174     else
1175         ERR("Unable to load library\n");
1176     msiobj_release( &stuff->package->hdr );
1177     HeapFree( GetProcessHeap(), 0, info );
1178     return 0;
1179 }
1180 #endif
1181
1182 static UINT HANDLE_CustomType1(MSIPACKAGE *package, const LPWSTR source, 
1183                                 const LPWSTR target, const INT type)
1184 {
1185     WCHAR tmp_file[MAX_PATH];
1186     CustomEntry *fn;
1187     HANDLE DLL;
1188     LPSTR proc;
1189
1190     store_binary_to_temp(package, source, tmp_file);
1191
1192     TRACE("Calling function %s from %s\n",debugstr_w(target),
1193           debugstr_w(tmp_file));
1194
1195     if (!strchrW(tmp_file,'.'))
1196     {
1197         static const WCHAR dot[]={'.',0};
1198         strcatW(tmp_file,dot);
1199     } 
1200
1201     if (type & 0xc0)
1202     {
1203         /* DWORD ThreadId; */
1204         thread_struct *info = HeapAlloc( GetProcessHeap(), 0, sizeof(*info) );
1205
1206         /* msiobj_addref( &package->hdr ); */
1207         info->package = package;
1208         strcpyW(info->target,target);
1209         strcpyW(info->source,tmp_file);
1210         TRACE("Start Asynchronous execution\n");
1211         FIXME("DATABASE NOT THREADSAFE... not starting\n");
1212         /* CreateThread(NULL,0,DllThread,(LPVOID)&info,0,&ThreadId); */
1213         /* FIXME: release the package if the CreateThread fails */
1214         HeapFree( GetProcessHeap(), 0, info );
1215         return ERROR_SUCCESS;
1216     }
1217  
1218     DLL = LoadLibraryW(tmp_file);
1219     if (DLL)
1220     {
1221         proc = strdupWtoA( target );
1222         fn = (CustomEntry*)GetProcAddress(DLL,proc);
1223         if (fn)
1224         {
1225             MSIHANDLE hPackage;
1226
1227             TRACE("Calling function\n");
1228             hPackage = msiobj_findhandle( &package->hdr );
1229             if( !hPackage )
1230                 ERR("Handle for object %p not found\n", package );
1231             fn(hPackage);
1232             msiobj_release( &package->hdr );
1233         }
1234         else
1235             ERR("Cannot load functon\n");
1236
1237         HeapFree(GetProcessHeap(),0,proc);
1238         FreeLibrary(DLL);
1239     }
1240     else
1241         ERR("Unable to load library\n");
1242
1243     return ERROR_SUCCESS;
1244 }
1245
1246 static UINT HANDLE_CustomType2(MSIPACKAGE *package, const LPWSTR source, 
1247                                 const LPWSTR target, const INT type)
1248 {
1249     WCHAR tmp_file[MAX_PATH*2];
1250     STARTUPINFOW si;
1251     PROCESS_INFORMATION info;
1252     BOOL rc;
1253     WCHAR *deformated;
1254     static const WCHAR spc[] = {' ',0};
1255
1256     memset(&si,0,sizeof(STARTUPINFOW));
1257
1258     store_binary_to_temp(package, source, tmp_file);
1259
1260     strcatW(tmp_file,spc);
1261     deformat_string(package,target,&deformated);
1262     strcatW(tmp_file,deformated);
1263
1264     HeapFree(GetProcessHeap(),0,deformated);
1265
1266     TRACE("executing exe %s \n",debugstr_w(tmp_file));
1267
1268     rc = CreateProcessW(NULL, tmp_file, NULL, NULL, FALSE, 0, NULL,
1269                   c_collen, &si, &info);
1270
1271     if ( !rc )
1272     {
1273         ERR("Unable to execute command\n");
1274         return ERROR_SUCCESS;
1275     }
1276
1277     if (!(type & 0xc0))
1278         WaitForSingleObject(info.hProcess,INFINITE);
1279
1280     CloseHandle( info.hProcess );
1281     CloseHandle( info.hThread );
1282     return ERROR_SUCCESS;
1283 }
1284
1285 static UINT HANDLE_CustomType18(MSIPACKAGE *package, const LPWSTR source, 
1286                                 const LPWSTR target, const INT type)
1287 {
1288     WCHAR filename[MAX_PATH*2];
1289     STARTUPINFOW si;
1290     PROCESS_INFORMATION info;
1291     BOOL rc;
1292     WCHAR *deformated;
1293     static const WCHAR spc[] = {' ',0};
1294     int index;
1295
1296     memset(&si,0,sizeof(STARTUPINFOW));
1297
1298     index = get_loaded_file(package,source);
1299     strcpyW(filename,package->files[index].TargetPath);
1300
1301     strcatW(filename,spc);
1302     deformat_string(package,target,&deformated);
1303     strcatW(filename,deformated);
1304
1305     HeapFree(GetProcessHeap(),0,deformated);
1306
1307     TRACE("executing exe %s \n",debugstr_w(filename));
1308
1309     rc = CreateProcessW(NULL, filename, NULL, NULL, FALSE, 0, NULL,
1310                   c_collen, &si, &info);
1311
1312     if ( !rc )
1313     {
1314         ERR("Unable to execute command\n");
1315         return ERROR_SUCCESS;
1316     }
1317
1318     if (!(type & 0xc0))
1319         WaitForSingleObject(info.hProcess,INFINITE);
1320
1321     CloseHandle( info.hProcess );
1322     CloseHandle( info.hThread );
1323     return ERROR_SUCCESS;
1324 }
1325
1326 static UINT HANDLE_CustomType50(MSIPACKAGE *package, const LPWSTR source, 
1327                                 const LPWSTR target, const INT type)
1328 {
1329     WCHAR filename[MAX_PATH*2];
1330     STARTUPINFOW si;
1331     PROCESS_INFORMATION info;
1332     BOOL rc;
1333     WCHAR *deformated;
1334     static const WCHAR spc[] = {' ',0};
1335     DWORD sz;
1336
1337     memset(&si,0,sizeof(STARTUPINFOW));
1338
1339     sz = MAX_PATH*2;
1340     if (MSI_GetPropertyW(package,source,filename,&sz) != ERROR_SUCCESS)
1341         return ERROR_FUNCTION_FAILED;
1342
1343     strcatW(filename,spc);
1344     deformat_string(package,target,&deformated);
1345     strcatW(filename,deformated);
1346
1347     HeapFree(GetProcessHeap(),0,deformated);
1348
1349     TRACE("executing exe %s \n",debugstr_w(filename));
1350
1351     rc = CreateProcessW(NULL, filename, NULL, NULL, FALSE, 0, NULL,
1352                   c_collen, &si, &info);
1353
1354     if ( !rc )
1355     {
1356         ERR("Unable to execute command\n");
1357         return ERROR_SUCCESS;
1358     }
1359
1360     if (!(type & 0xc0))
1361         WaitForSingleObject(info.hProcess,INFINITE);
1362
1363     CloseHandle( info.hProcess );
1364     CloseHandle( info.hThread );
1365     return ERROR_SUCCESS;
1366 }
1367
1368 static UINT HANDLE_CustomType34(MSIPACKAGE *package, const LPWSTR source, 
1369                                 const LPWSTR target, const INT type)
1370 {
1371     WCHAR filename[MAX_PATH*2];
1372     STARTUPINFOW si;
1373     PROCESS_INFORMATION info;
1374     BOOL rc;
1375     WCHAR *deformated;
1376
1377     memset(&si,0,sizeof(STARTUPINFOW));
1378
1379     rc = resolve_folder(package, source, filename, FALSE, FALSE, NULL);
1380     if (rc != ERROR_SUCCESS)
1381         return rc;
1382
1383     SetCurrentDirectoryW(filename);
1384
1385     deformat_string(package,target,&deformated);
1386     strcpyW(filename,deformated);
1387
1388     HeapFree(GetProcessHeap(),0,deformated);
1389
1390     TRACE("executing exe %s \n",debugstr_w(filename));
1391
1392     rc = CreateProcessW(NULL, filename, NULL, NULL, FALSE, 0, NULL,
1393                   c_collen, &si, &info);
1394
1395     if ( !rc )
1396     {
1397         ERR("Unable to execute command\n");
1398         return ERROR_SUCCESS;
1399     }
1400
1401     if (!(type & 0xc0))
1402         WaitForSingleObject(info.hProcess,INFINITE);
1403
1404     CloseHandle( info.hProcess );
1405     CloseHandle( info.hThread );
1406     return ERROR_SUCCESS;
1407 }
1408
1409 /***********************************************************************
1410  *            create_full_pathW
1411  *
1412  * Recursively create all directories in the path.
1413  *
1414  * shamelessly stolen from setupapi/queue.c
1415  */
1416 static BOOL create_full_pathW(const WCHAR *path)
1417 {
1418     BOOL ret = TRUE;
1419     int len;
1420     WCHAR *new_path;
1421
1422     new_path = HeapAlloc(GetProcessHeap(), 0, (strlenW(path) + 1) *
1423 sizeof(WCHAR));
1424     strcpyW(new_path, path);
1425
1426     while((len = strlenW(new_path)) && new_path[len - 1] == '\\')
1427     new_path[len - 1] = 0;
1428
1429     while(!CreateDirectoryW(new_path, NULL))
1430     {
1431     WCHAR *slash;
1432     DWORD last_error = GetLastError();
1433     if(last_error == ERROR_ALREADY_EXISTS)
1434         break;
1435
1436     if(last_error != ERROR_PATH_NOT_FOUND)
1437     {
1438         ret = FALSE;
1439         break;
1440     }
1441
1442     if(!(slash = strrchrW(new_path, '\\')))
1443     {
1444         ret = FALSE;
1445         break;
1446     }
1447
1448     len = slash - new_path;
1449     new_path[len] = 0;
1450     if(!create_full_pathW(new_path))
1451     {
1452         ret = FALSE;
1453         break;
1454     }
1455     new_path[len] = '\\';
1456     }
1457
1458     HeapFree(GetProcessHeap(), 0, new_path);
1459     return ret;
1460 }
1461
1462 /*
1463  * Also we cannot enable/disable components either, so for now I am just going 
1464  * to do all the directories for all the components.
1465  */
1466 static UINT ACTION_CreateFolders(MSIPACKAGE *package)
1467 {
1468     static const WCHAR ExecSeqQuery[] = {
1469         's','e','l','e','c','t',' ','D','i','r','e','c','t','o','r','y','_',' ',
1470         'f','r','o','m',' ','C','r','e','a','t','e','F','o','l','d','e','r',0 };
1471     UINT rc;
1472     MSIQUERY *view;
1473     MSIFOLDER *folder;
1474
1475     rc = MSI_DatabaseOpenViewW(package->db, ExecSeqQuery, &view );
1476     if (rc != ERROR_SUCCESS)
1477         return ERROR_SUCCESS;
1478
1479     rc = MSI_ViewExecute(view, 0);
1480     if (rc != ERROR_SUCCESS)
1481     {
1482         MSI_ViewClose(view);
1483         msiobj_release(&view->hdr);
1484         return rc;
1485     }
1486     
1487     while (1)
1488     {
1489         WCHAR dir[0x100];
1490         WCHAR full_path[MAX_PATH];
1491         DWORD sz;
1492         MSIRECORD *row = NULL, *uirow;
1493
1494         rc = MSI_ViewFetch(view,&row);
1495         if (rc != ERROR_SUCCESS)
1496         {
1497             rc = ERROR_SUCCESS;
1498             break;
1499         }
1500
1501         sz=0x100;
1502         rc = MSI_RecordGetStringW(row,1,dir,&sz);
1503
1504         if (rc!= ERROR_SUCCESS)
1505         {
1506             ERR("Unable to get folder id \n");
1507             msiobj_release(&row->hdr);
1508             continue;
1509         }
1510
1511         sz = MAX_PATH;
1512         rc = resolve_folder(package,dir,full_path,FALSE,FALSE,&folder);
1513
1514         if (rc != ERROR_SUCCESS)
1515         {
1516             ERR("Unable to resolve folder id %s\n",debugstr_w(dir));
1517             msiobj_release(&row->hdr);
1518             continue;
1519         }
1520
1521         TRACE("Folder is %s\n",debugstr_w(full_path));
1522
1523         /* UI stuff */
1524         uirow = MSI_CreateRecord(1);
1525         MSI_RecordSetStringW(uirow,1,full_path);
1526         ui_actiondata(package,szCreateFolders,uirow);
1527         msiobj_release( &uirow->hdr );
1528
1529         if (folder->State == 0)
1530             create_full_pathW(full_path);
1531
1532         folder->State = 3;
1533
1534         msiobj_release(&row->hdr);
1535     }
1536     MSI_ViewClose(view);
1537     msiobj_release(&view->hdr);
1538    
1539     return rc;
1540 }
1541
1542 static int load_component(MSIPACKAGE* package, MSIRECORD * row)
1543 {
1544     int index = package->loaded_components;
1545     DWORD sz;
1546
1547     /* fill in the data */
1548
1549     package->loaded_components++;
1550     if (package->loaded_components == 1)
1551         package->components = HeapAlloc(GetProcessHeap(),0,
1552                                         sizeof(MSICOMPONENT));
1553     else
1554         package->components = HeapReAlloc(GetProcessHeap(),0,
1555             package->components, package->loaded_components * 
1556             sizeof(MSICOMPONENT));
1557
1558     memset(&package->components[index],0,sizeof(MSICOMPONENT));
1559
1560     sz = 96;       
1561     MSI_RecordGetStringW(row,1,package->components[index].Component,&sz);
1562
1563     TRACE("Loading Component %s\n",
1564            debugstr_w(package->components[index].Component));
1565
1566     sz = 0x100;
1567     if (!MSI_RecordIsNull(row,2))
1568         MSI_RecordGetStringW(row,2,package->components[index].ComponentId,&sz);
1569             
1570     sz = 96;       
1571     MSI_RecordGetStringW(row,3,package->components[index].Directory,&sz);
1572
1573     package->components[index].Attributes = MSI_RecordGetInteger(row,4);
1574
1575     sz = 0x100;       
1576     MSI_RecordGetStringW(row,5,package->components[index].Condition,&sz);
1577
1578     sz = 96;       
1579     MSI_RecordGetStringW(row,6,package->components[index].KeyPath,&sz);
1580
1581     package->components[index].State = INSTALLSTATE_UNKNOWN;
1582     package->components[index].Enabled = TRUE;
1583     package->components[index].FeatureState= FALSE;
1584
1585     return index;
1586 }
1587
1588 static void load_feature(MSIPACKAGE* package, MSIRECORD * row)
1589 {
1590     int index = package->loaded_features;
1591     DWORD sz;
1592     static const WCHAR Query1[] = {'S','E','L','E','C','T',' ','C','o','m','p',
1593 'o','n','e','n','t','_',' ','F','R','O','M',' ','F','e','a','t','u','r','e',
1594 'C','o','m','p','o','n','e','n','t','s',' ','W','H','E','R','E',' ','F','e',
1595 'a','t','u','r','e','_','=','\'','%','s','\'',0};
1596     static const WCHAR Query2[] = {'S','E','L','E','C','T',' ','*',' ','F','R',
1597 'O','M',' ','C','o','m','p','o','n','e','n','t',' ','W','H','E','R','E',' ','C',
1598 'o','m','p','o','n','e','n','t','=','\'','%','s','\'',0};
1599     WCHAR Query[1024];
1600     MSIQUERY * view;
1601     MSIQUERY * view2;
1602     MSIRECORD * row2;
1603     MSIRECORD * row3;
1604     UINT    rc;
1605
1606     /* fill in the data */
1607
1608     package->loaded_features ++;
1609     if (package->loaded_features == 1)
1610         package->features = HeapAlloc(GetProcessHeap(),0,sizeof(MSIFEATURE));
1611     else
1612         package->features = HeapReAlloc(GetProcessHeap(),0,package->features,
1613                                 package->loaded_features * sizeof(MSIFEATURE));
1614
1615     memset(&package->features[index],0,sizeof(MSIFEATURE));
1616     
1617     sz = 96;       
1618     MSI_RecordGetStringW(row,1,package->features[index].Feature,&sz);
1619
1620     TRACE("Loading feature %s\n",debugstr_w(package->features[index].Feature));
1621
1622     sz = 96;
1623     if (!MSI_RecordIsNull(row,2))
1624         MSI_RecordGetStringW(row,2,package->features[index].Feature_Parent,&sz);
1625
1626     sz = 0x100;
1627      if (!MSI_RecordIsNull(row,3))
1628         MSI_RecordGetStringW(row,3,package->features[index].Title,&sz);
1629
1630      sz = 0x100;
1631      if (!MSI_RecordIsNull(row,4))
1632         MSI_RecordGetStringW(row,4,package->features[index].Description,&sz);
1633
1634     if (!MSI_RecordIsNull(row,5))
1635         package->features[index].Display = MSI_RecordGetInteger(row,5);
1636   
1637     package->features[index].Level= MSI_RecordGetInteger(row,6);
1638
1639      sz = 96;
1640      if (!MSI_RecordIsNull(row,7))
1641         MSI_RecordGetStringW(row,7,package->features[index].Directory,&sz);
1642
1643     package->features[index].Attributes= MSI_RecordGetInteger(row,8);
1644     package->features[index].State = INSTALLSTATE_UNKNOWN;
1645
1646     /* load feature components */
1647
1648     sprintfW(Query,Query1,package->features[index].Feature);
1649     rc = MSI_DatabaseOpenViewW(package->db,Query,&view);
1650     if (rc != ERROR_SUCCESS)
1651         return;
1652     rc = MSI_ViewExecute(view,0);
1653     if (rc != ERROR_SUCCESS)
1654     {
1655         MSI_ViewClose(view);
1656         msiobj_release(&view->hdr);
1657         return;
1658     }
1659     while (1)
1660     {
1661         DWORD sz = 0x100;
1662         WCHAR buffer[0x100];
1663         DWORD rc;
1664         INT c_indx;
1665         INT cnt = package->features[index].ComponentCount;
1666
1667         rc = MSI_ViewFetch(view,&row2);
1668         if (rc != ERROR_SUCCESS)
1669             break;
1670
1671         sz = 0x100;
1672         MSI_RecordGetStringW(row2,1,buffer,&sz);
1673
1674         /* check to see if the component is already loaded */
1675         c_indx = get_loaded_component(package,buffer);
1676         if (c_indx != -1)
1677         {
1678             TRACE("Component %s already loaded at %i\n", debugstr_w(buffer),
1679                   c_indx);
1680             package->features[index].Components[cnt] = c_indx;
1681             package->features[index].ComponentCount ++;
1682         }
1683
1684         sprintfW(Query,Query2,buffer);
1685    
1686         rc = MSI_DatabaseOpenViewW(package->db,Query,&view2);
1687         if (rc != ERROR_SUCCESS)
1688         {
1689             msiobj_release( &row2->hdr );
1690             continue;
1691         }
1692         rc = MSI_ViewExecute(view2,0);
1693         if (rc != ERROR_SUCCESS)
1694         {
1695             msiobj_release( &row2->hdr );
1696             MSI_ViewClose(view2);
1697             msiobj_release( &view2->hdr );  
1698             continue;
1699         }
1700         while (1)
1701         {
1702             DWORD rc;
1703
1704             rc = MSI_ViewFetch(view2,&row3);
1705             if (rc != ERROR_SUCCESS)
1706                 break;
1707             c_indx = load_component(package,row3);
1708             msiobj_release( &row3->hdr );
1709
1710             package->features[index].Components[cnt] = c_indx;
1711             package->features[index].ComponentCount ++;
1712         }
1713         MSI_ViewClose(view2);
1714         msiobj_release( &view2->hdr );
1715         msiobj_release( &row2->hdr );
1716     }
1717     MSI_ViewClose(view);
1718     msiobj_release(&view->hdr);
1719 }
1720
1721 /*
1722  * I am not doing any of the costing functionality yet. 
1723  * Mostly looking at doing the Component and Feature loading
1724  *
1725  * The native MSI does ALOT of modification to tables here. Mostly adding alot
1726  * of temporary columns to the Feature and Component tables. 
1727  *
1728  *    note: native msi also tracks the short filename. but I am only going to
1729  *          track the long ones.  Also looking at this directory table
1730  *          it appears that the directory table does not get the parents
1731  *          resolved base on property only based on their entrys in the 
1732  *          directory table.
1733  */
1734 static UINT ACTION_CostInitialize(MSIPACKAGE *package)
1735 {
1736     MSIQUERY * view;
1737     MSIRECORD * row;
1738     DWORD sz;
1739     UINT rc;
1740     static const WCHAR Query_all[] = {
1741        'S','E','L','E','C','T',' ','*',' ',
1742        'F','R','O','M',' ','F','e','a','t','u','r','e',0};
1743     static const WCHAR szCosting[] = {
1744        'C','o','s','t','i','n','g','C','o','m','p','l','e','t','e',0 };
1745     static const WCHAR szZero[] = { '0', 0 };
1746
1747     MSI_SetPropertyW(package, szCosting, szZero);
1748     MSI_SetPropertyW(package, cszRootDrive , c_collen);
1749
1750     sz = 0x100;
1751     rc = MSI_DatabaseOpenViewW(package->db,Query_all,&view);
1752     if (rc != ERROR_SUCCESS)
1753         return rc;
1754     rc = MSI_ViewExecute(view,0);
1755     if (rc != ERROR_SUCCESS)
1756     {
1757         MSI_ViewClose(view);
1758         msiobj_release(&view->hdr);
1759         return rc;
1760     }
1761     while (1)
1762     {
1763         DWORD rc;
1764
1765         rc = MSI_ViewFetch(view,&row);
1766         if (rc != ERROR_SUCCESS)
1767             break;
1768        
1769         load_feature(package,row); 
1770         msiobj_release(&row->hdr);
1771     }
1772     MSI_ViewClose(view);
1773     msiobj_release(&view->hdr);
1774
1775     return ERROR_SUCCESS;
1776 }
1777
1778 static UINT load_file(MSIPACKAGE* package, MSIRECORD * row)
1779 {
1780     DWORD index = package->loaded_files;
1781     DWORD i;
1782     WCHAR buffer[0x100];
1783     DWORD sz;
1784
1785     /* fill in the data */
1786
1787     package->loaded_files++;
1788     if (package->loaded_files== 1)
1789         package->files = HeapAlloc(GetProcessHeap(),0,sizeof(MSIFILE));
1790     else
1791         package->files = HeapReAlloc(GetProcessHeap(),0,
1792             package->files , package->loaded_files * sizeof(MSIFILE));
1793
1794     memset(&package->files[index],0,sizeof(MSIFILE));
1795
1796     sz = 72;       
1797     MSI_RecordGetStringW(row,1,package->files[index].File,&sz);
1798
1799     sz = 0x100;       
1800     MSI_RecordGetStringW(row,2,buffer,&sz);
1801
1802     package->files[index].ComponentIndex = -1;
1803     for (i = 0; i < package->loaded_components; i++)
1804         if (strcmpW(package->components[i].Component,buffer)==0)
1805         {
1806             package->files[index].ComponentIndex = i;
1807             break;
1808         }
1809     if (package->files[index].ComponentIndex == -1)
1810         ERR("Unfound Component %s\n",debugstr_w(buffer));
1811
1812     sz = MAX_PATH;       
1813     MSI_RecordGetStringW(row,3,package->files[index].FileName,&sz);
1814
1815     reduce_to_longfilename(package->files[index].FileName);
1816     
1817     package->files[index].FileSize = MSI_RecordGetInteger(row,4);
1818
1819     sz = 72;       
1820     if (!MSI_RecordIsNull(row,5))
1821         MSI_RecordGetStringW(row,5,package->files[index].Version,&sz);
1822
1823     sz = 20;       
1824     if (!MSI_RecordIsNull(row,6))
1825         MSI_RecordGetStringW(row,6,package->files[index].Language,&sz);
1826
1827     if (!MSI_RecordIsNull(row,7))
1828         package->files[index].Attributes= MSI_RecordGetInteger(row,7);
1829
1830     package->files[index].Sequence= MSI_RecordGetInteger(row,8);
1831
1832     package->files[index].Temporary = FALSE;
1833     package->files[index].State = 0;
1834
1835     TRACE("File Loaded (%s)\n",debugstr_w(package->files[index].File));  
1836  
1837     return ERROR_SUCCESS;
1838 }
1839
1840 static UINT ACTION_FileCost(MSIPACKAGE *package)
1841 {
1842     MSIQUERY * view;
1843     MSIRECORD * row;
1844     UINT rc;
1845     static const WCHAR Query[] = {
1846         'S','E','L','E','C','T',' ','*',' ',
1847         'F','R','O','M',' ','F','i','l','e',' ',
1848         'O','r','d','e','r',' ','b','y',' ','S','e','q','u','e','n','c','e', 0};
1849
1850     if (!package)
1851         return ERROR_INVALID_HANDLE;
1852
1853     rc = MSI_DatabaseOpenViewW(package->db, Query, &view);
1854     if (rc != ERROR_SUCCESS)
1855         return ERROR_SUCCESS;
1856    
1857     rc = MSI_ViewExecute(view, 0);
1858     if (rc != ERROR_SUCCESS)
1859     {
1860         MSI_ViewClose(view);
1861         msiobj_release(&view->hdr);
1862         return ERROR_SUCCESS;
1863     }
1864
1865     while (1)
1866     {
1867         rc = MSI_ViewFetch(view,&row);
1868         if (rc != ERROR_SUCCESS)
1869         {
1870             rc = ERROR_SUCCESS;
1871             break;
1872         }
1873         load_file(package,row);
1874         msiobj_release(&row->hdr);
1875     }
1876     MSI_ViewClose(view);
1877     msiobj_release(&view->hdr);
1878
1879     return ERROR_SUCCESS;
1880 }
1881
1882 static INT load_folder(MSIPACKAGE *package, const WCHAR* dir)
1883
1884 {
1885     WCHAR Query[1024] = 
1886 {'s','e','l','e','c','t',' ','*',' ','f','r','o','m',' ','D','i','r','e','c',
1887 't','o','r','y',' ','w','h','e','r','e',' ','`','D','i','r','e','c','t',
1888 'o','r','y','`',' ','=',' ','`',0};
1889     static const WCHAR end[]={'`',0};
1890     UINT rc;
1891     MSIQUERY * view;
1892     WCHAR targetbuffer[0x100];
1893     WCHAR *srcdir = NULL;
1894     WCHAR *targetdir = NULL;
1895     WCHAR parent[0x100];
1896     DWORD sz=0x100;
1897     MSIRECORD * row = 0;
1898     INT index = -1;
1899     DWORD i;
1900
1901     TRACE("Looking for dir %s\n",debugstr_w(dir));
1902
1903     for (i = 0; i < package->loaded_folders; i++)
1904     {
1905         if (strcmpW(package->folders[i].Directory,dir)==0)
1906         {
1907             TRACE(" %s retuning on index %lu\n",debugstr_w(dir),i);
1908             return i;
1909         }
1910     }
1911
1912     TRACE("Working to load %s\n",debugstr_w(dir));
1913
1914     index = package->loaded_folders; 
1915
1916     package->loaded_folders++;
1917     if (package->loaded_folders== 1)
1918         package->folders = HeapAlloc(GetProcessHeap(),0,
1919                                         sizeof(MSIFOLDER));
1920     else
1921         package->folders= HeapReAlloc(GetProcessHeap(),0,
1922             package->folders, package->loaded_folders* 
1923             sizeof(MSIFOLDER));
1924
1925     memset(&package->folders[index],0,sizeof(MSIFOLDER));
1926
1927     strcpyW(package->folders[index].Directory,dir);
1928
1929     strcatW(Query,dir);
1930     strcatW(Query,end);
1931
1932     rc = MSI_DatabaseOpenViewW(package->db, Query, &view);
1933
1934     if (rc != ERROR_SUCCESS)
1935         return -1;
1936
1937     rc = MSI_ViewExecute(view, 0);
1938     if (rc != ERROR_SUCCESS)
1939     {
1940         MSI_ViewClose(view);
1941         msiobj_release(&view->hdr);
1942         return -1;
1943     }
1944
1945     rc = MSI_ViewFetch(view,&row);
1946     if (rc != ERROR_SUCCESS)
1947     {
1948         MSI_ViewClose(view);
1949         msiobj_release(&view->hdr);
1950         return -1;
1951     }
1952
1953     sz=0x100;
1954     MSI_RecordGetStringW(row,3,targetbuffer,&sz);
1955     targetdir=targetbuffer;
1956
1957     /* split src and target dir */
1958     if (strchrW(targetdir,':'))
1959     {
1960         srcdir=strchrW(targetdir,':');
1961         *srcdir=0;
1962         srcdir ++;
1963     }
1964     else
1965         srcdir=NULL;
1966
1967     /* for now only pick long filename versions */
1968     if (strchrW(targetdir,'|'))
1969     {
1970         targetdir = strchrW(targetdir,'|'); 
1971         *targetdir = 0;
1972         targetdir ++;
1973     }
1974     if (srcdir && strchrW(srcdir,'|'))
1975     {
1976         srcdir= strchrW(srcdir,'|'); 
1977         *srcdir= 0;
1978         srcdir ++;
1979     }
1980
1981     /* now check for root dirs */
1982     if (targetdir[0] == '.' && targetdir[1] == 0)
1983         targetdir = NULL;
1984         
1985     if (srcdir && srcdir[0] == '.' && srcdir[1] == 0)
1986         srcdir = NULL;
1987
1988      if (targetdir)
1989         strcpyW(package->folders[index].TargetDefault,targetdir);
1990
1991      if (srcdir)
1992         strcpyW(package->folders[index].SourceDefault,srcdir);
1993      else if (targetdir)
1994         strcpyW(package->folders[index].SourceDefault,targetdir);
1995
1996     if (MSI_RecordIsNull(row,2))
1997         parent[0]=0;
1998     else
1999     {
2000             sz=0x100;
2001             MSI_RecordGetStringW(row,2,parent,&sz);
2002     }
2003
2004     if (parent[0]) 
2005     {
2006         i = load_folder(package,parent);
2007         package->folders[index].ParentIndex = i;
2008         TRACE("Parent is index %i... %s %s\n",
2009                     package->folders[index].ParentIndex,
2010     debugstr_w(package->folders[package->folders[index].ParentIndex].Directory),
2011                     debugstr_w(parent));
2012     }
2013     else
2014         package->folders[index].ParentIndex = -2;
2015
2016     sz = MAX_PATH;
2017     rc = MSI_GetPropertyW(package, dir, package->folders[index].Property, &sz);
2018     if (rc != ERROR_SUCCESS)
2019         package->folders[index].Property[0]=0;
2020
2021     msiobj_release(&row->hdr);
2022     MSI_ViewClose(view);
2023     msiobj_release(&view->hdr);
2024     TRACE(" %s retuning on index %i\n",debugstr_w(dir),index);
2025     return index;
2026 }
2027
2028 static UINT resolve_folder(MSIPACKAGE *package, LPCWSTR name, LPWSTR path, 
2029                            BOOL source, BOOL set_prop, MSIFOLDER **folder)
2030 {
2031     DWORD i;
2032     UINT rc = ERROR_SUCCESS;
2033     DWORD sz;
2034
2035     TRACE("Working to resolve %s\n",debugstr_w(name));
2036
2037     if (!path)
2038         return rc;
2039
2040     /* special resolving for Target and Source root dir */
2041     if (strcmpW(name,cszTargetDir)==0 || strcmpW(name,cszSourceDir)==0)
2042     {
2043         if (!source)
2044         {
2045             sz = MAX_PATH;
2046             rc = MSI_GetPropertyW(package,cszTargetDir,path,&sz);
2047             if (rc != ERROR_SUCCESS)
2048             {
2049                 sz = MAX_PATH;
2050                 rc = MSI_GetPropertyW(package,cszRootDrive,path,&sz);
2051                 if (set_prop)
2052                     MSI_SetPropertyW(package,cszTargetDir,path);
2053             }
2054             if (folder)
2055                 *folder = &(package->folders[0]);
2056             return rc;
2057         }
2058         else
2059         {
2060             sz = MAX_PATH;
2061             rc = MSI_GetPropertyW(package,cszSourceDir,path,&sz);
2062             if (rc != ERROR_SUCCESS)
2063             {
2064                 sz = MAX_PATH;
2065                 rc = MSI_GetPropertyW(package,cszDatabase,path,&sz);
2066                 if (rc == ERROR_SUCCESS)
2067                 {
2068                     LPWSTR ptr = strrchrW(path,'\\');
2069                     if (ptr)
2070                     {
2071                         ptr++;
2072                         *ptr = 0;
2073                     }
2074                 }
2075             }
2076             if (folder)
2077                 *folder = &(package->folders[0]);
2078             return rc;
2079         }
2080     }
2081
2082     for (i = 0; i < package->loaded_folders; i++)
2083     {
2084         if (strcmpW(package->folders[i].Directory,name)==0)
2085             break;
2086     }
2087
2088     if (i >= package->loaded_folders)
2089         return ERROR_FUNCTION_FAILED;
2090
2091     if (folder)
2092         *folder = &(package->folders[i]);
2093
2094     if (!source && package->folders[i].ResolvedTarget[0])
2095     {
2096         strcpyW(path,package->folders[i].ResolvedTarget);
2097         TRACE("   already resolved to %s\n",debugstr_w(path));
2098         return ERROR_SUCCESS;
2099     }
2100     else if (source && package->folders[i].ResolvedSource[0])
2101     {
2102         strcpyW(path,package->folders[i].ResolvedSource);
2103         return ERROR_SUCCESS;
2104     }
2105     else if (!source && package->folders[i].Property[0])
2106     {
2107         strcpyW(path,package->folders[i].Property);
2108         TRACE("   internally set to %s\n",debugstr_w(path));
2109         if (set_prop)
2110             MSI_SetPropertyW(package,name,path);
2111         return ERROR_SUCCESS;
2112     }
2113
2114     if (package->folders[i].ParentIndex >= 0)
2115     {
2116         int len;
2117         TRACE(" ! Parent is %s\n", debugstr_w(package->folders[
2118                    package->folders[i].ParentIndex].Directory));
2119         resolve_folder(package, package->folders[
2120                        package->folders[i].ParentIndex].Directory, path,source,
2121                        set_prop, NULL);
2122
2123         len = strlenW(path);
2124         if (len && path[len-1] != '\\')
2125             strcatW(path, cszbs);
2126
2127         if (!source)
2128         {
2129             if (package->folders[i].TargetDefault[0])
2130             {
2131                 strcatW(path,package->folders[i].TargetDefault);
2132                 strcatW(path,cszbs);
2133             }
2134             strcpyW(package->folders[i].ResolvedTarget,path);
2135             TRACE("   resolved into %s\n",debugstr_w(path));
2136             if (set_prop)
2137                 MSI_SetPropertyW(package,name,path);
2138         }
2139         else 
2140         {
2141             if (package->folders[i].SourceDefault[0])
2142             {
2143                 strcatW(path,package->folders[i].SourceDefault);
2144                 strcatW(path,cszbs);
2145             }
2146             strcpyW(package->folders[i].ResolvedSource,path);
2147         }
2148     }
2149     return rc;
2150 }
2151
2152 /* 
2153  * Alot is done in this function aside from just the costing.
2154  * The costing needs to be implemented at some point but for now I am going
2155  * to focus on the directory building
2156  *
2157  */
2158 static UINT ACTION_CostFinalize(MSIPACKAGE *package)
2159 {
2160     static const WCHAR ExecSeqQuery[] = {
2161         's','e','l','e','c','t',' ','*',' ','f','r','o','m',' ',
2162         'D','i','r','e','c','t','o','r','y',0};
2163     static const WCHAR ConditionQuery[] = {
2164         's','e','l','e','c','t',' ','*',' ','f','r','o','m',' ',
2165         'C','o','n','d','i','t','i','o','n',0};
2166     static const WCHAR szCosting[] = {
2167        'C','o','s','t','i','n','g','C','o','m','p','l','e','t','e',0 };
2168     static const WCHAR szOne[] = { '1', 0 };
2169     UINT rc;
2170     MSIQUERY * view;
2171     DWORD i;
2172
2173     TRACE("Building Directory properties\n");
2174
2175     rc = MSI_DatabaseOpenViewW(package->db, ExecSeqQuery, &view);
2176     if (rc == ERROR_SUCCESS)
2177     {
2178         rc = MSI_ViewExecute(view, 0);
2179         if (rc != ERROR_SUCCESS)
2180         {
2181             MSI_ViewClose(view);
2182             msiobj_release(&view->hdr);
2183             return rc;
2184         }
2185
2186         while (1)
2187         {
2188             WCHAR name[0x100];
2189             WCHAR path[MAX_PATH];
2190             MSIRECORD * row = 0;
2191             DWORD sz;
2192
2193             rc = MSI_ViewFetch(view,&row);
2194             if (rc != ERROR_SUCCESS)
2195             {
2196                 rc = ERROR_SUCCESS;
2197                 break;
2198             }
2199
2200             sz=0x100;
2201             MSI_RecordGetStringW(row,1,name,&sz);
2202
2203             /* This helper function now does ALL the work */
2204             TRACE("Dir %s ...\n",debugstr_w(name));
2205             load_folder(package,name);
2206             resolve_folder(package,name,path,FALSE,TRUE,NULL);
2207             TRACE("resolves to %s\n",debugstr_w(path));
2208
2209             msiobj_release(&row->hdr);
2210         }
2211         MSI_ViewClose(view);
2212         msiobj_release(&view->hdr);
2213     }
2214
2215     TRACE("File calculations %i files\n",package->loaded_files);
2216
2217     for (i = 0; i < package->loaded_files; i++)
2218     {
2219         MSICOMPONENT* comp = NULL;
2220         MSIFILE* file= NULL;
2221
2222         file = &package->files[i];
2223         if (file->ComponentIndex >= 0)
2224             comp = &package->components[file->ComponentIndex];
2225
2226         if (comp)
2227         {
2228             int len;
2229             /* calculate target */
2230             resolve_folder(package, comp->Directory, file->TargetPath, FALSE,
2231                        FALSE, NULL);
2232             /* make sure that the path ends in a \ */
2233             len = strlenW(file->TargetPath);
2234             if (len && file->TargetPath[len-1] != '\\')
2235                 strcatW(file->TargetPath, cszbs);
2236             strcatW(file->TargetPath,file->FileName);
2237
2238             TRACE("file %s resolves to %s\n",
2239                    debugstr_w(file->File),debugstr_w(file->TargetPath));       
2240  
2241             if (GetFileAttributesW(file->TargetPath) == INVALID_FILE_ATTRIBUTES)
2242             {
2243                 file->State = 1;
2244                 comp->Cost += file->FileSize;
2245             }
2246             else
2247             {
2248                 if (file->Version[0])
2249                 {
2250                     DWORD handle;
2251                     DWORD versize;
2252                     UINT sz;
2253                     LPVOID version;
2254                     static const WCHAR name[] = 
2255                     {'\\',0};
2256                     static const WCHAR name_fmt[] = 
2257                     {'%','u','.','%','u','.','%','u','.','%','u',0};
2258                     WCHAR filever[0x100];
2259                     VS_FIXEDFILEINFO *lpVer;
2260
2261                     FIXME("Version comparison.. \n");
2262                     versize = GetFileVersionInfoSizeW(file->TargetPath,&handle);
2263                     version = HeapAlloc(GetProcessHeap(),0,versize);
2264                     GetFileVersionInfoW(file->TargetPath, 0, versize, version);
2265
2266                     VerQueryValueW(version, name, (LPVOID*)&lpVer, &sz);
2267
2268                     sprintfW(filever,name_fmt,
2269                         HIWORD(lpVer->dwFileVersionMS),
2270                         LOWORD(lpVer->dwFileVersionMS),
2271                         HIWORD(lpVer->dwFileVersionLS),
2272                         LOWORD(lpVer->dwFileVersionLS));
2273
2274                     TRACE("new %s old %s\n", debugstr_w(file->Version),
2275                           debugstr_w(filever));
2276                     if (strcmpiW(filever,file->Version)<0)
2277                     {
2278                         file->State = 2;
2279                         FIXME("cost should be diff in size\n");
2280                         comp->Cost += file->FileSize;
2281                     }
2282                     else
2283                         file->State = 3;
2284                     HeapFree(GetProcessHeap(),0,version);
2285                 }
2286                 else
2287                     file->State = 3;
2288             }
2289         } 
2290     }
2291
2292     TRACE("Evaluating Condition Table\n");
2293
2294     rc = MSI_DatabaseOpenViewW(package->db, ConditionQuery, &view);
2295     if (rc == ERROR_SUCCESS)
2296     {
2297     rc = MSI_ViewExecute(view, 0);
2298     if (rc != ERROR_SUCCESS)
2299     {
2300         MSI_ViewClose(view);
2301         msiobj_release(&view->hdr);
2302         return rc;
2303     }
2304     
2305     while (1)
2306     {
2307         WCHAR Feature[0x100];
2308         MSIRECORD * row = 0;
2309         DWORD sz;
2310         int feature_index;
2311
2312         rc = MSI_ViewFetch(view,&row);
2313
2314         if (rc != ERROR_SUCCESS)
2315         {
2316             rc = ERROR_SUCCESS;
2317             break;
2318         }
2319
2320         sz = 0x100;
2321         MSI_RecordGetStringW(row,1,Feature,&sz);
2322
2323         feature_index = get_loaded_feature(package,Feature);
2324         if (feature_index < 0)
2325             ERR("FAILED to find loaded feature %s\n",debugstr_w(Feature));
2326         else
2327         {
2328             LPWSTR Condition;
2329             Condition = load_dynamic_stringW(row,3);
2330
2331                 if (MSI_EvaluateConditionW(package,Condition) == 
2332                     MSICONDITION_TRUE)
2333             {
2334                 int level = MSI_RecordGetInteger(row,2);
2335                     TRACE("Reseting feature %s to level %i\n",
2336                            debugstr_w(Feature), level);
2337                 package->features[feature_index].Level = level;
2338             }
2339             HeapFree(GetProcessHeap(),0,Condition);
2340         }
2341
2342         msiobj_release(&row->hdr);
2343     }
2344     MSI_ViewClose(view);
2345     msiobj_release(&view->hdr);
2346     }
2347
2348     TRACE("Enabling or Disabling Components\n");
2349     for (i = 0; i < package->loaded_components; i++)
2350     {
2351         if (package->components[i].Condition[0])
2352         {
2353             if (MSI_EvaluateConditionW(package,
2354                 package->components[i].Condition) == MSICONDITION_FALSE)
2355             {
2356                 TRACE("Disabling component %s\n",
2357                       debugstr_w(package->components[i].Component));
2358                 package->components[i].Enabled = FALSE;
2359             }
2360         }
2361     }
2362
2363     MSI_SetPropertyW(package,szCosting,szOne);
2364     return ERROR_SUCCESS;
2365 }
2366
2367 /*
2368  * This is a helper function for handling embedded cabinet media
2369  */
2370 static UINT writeout_cabinet_stream(MSIPACKAGE *package, WCHAR* stream_name,
2371                                     WCHAR* source)
2372 {
2373     UINT rc;
2374     USHORT* data;
2375     UINT    size;
2376     DWORD   write;
2377     HANDLE  the_file;
2378     WCHAR tmp[MAX_PATH];
2379
2380     rc = read_raw_stream_data(package->db,stream_name,&data,&size); 
2381     if (rc != ERROR_SUCCESS)
2382         return rc;
2383
2384     write = MAX_PATH;
2385     if (MSI_GetPropertyW(package, cszTempFolder, tmp, &write))
2386         GetTempPathW(MAX_PATH,tmp);
2387
2388     GetTempFileNameW(tmp,stream_name,0,source);
2389
2390     track_tempfile(package,strrchrW(source,'\\'), source);
2391     the_file = CreateFileW(source, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS,
2392                            FILE_ATTRIBUTE_NORMAL, NULL);
2393
2394     if (the_file == INVALID_HANDLE_VALUE)
2395     {
2396         rc = ERROR_FUNCTION_FAILED;
2397         goto end;
2398     }
2399
2400     WriteFile(the_file,data,size,&write,NULL);
2401     CloseHandle(the_file);
2402     TRACE("wrote %li bytes to %s\n",write,debugstr_w(source));
2403 end:
2404     HeapFree(GetProcessHeap(),0,data);
2405     return rc;
2406 }
2407
2408
2409 /* Support functions for FDI functions */
2410
2411 static void * cabinet_alloc(ULONG cb)
2412 {
2413     return HeapAlloc(GetProcessHeap(), 0, cb);
2414 }
2415
2416 static void cabinet_free(void *pv)
2417 {
2418     HeapFree(GetProcessHeap(), 0, pv);
2419 }
2420
2421 static INT_PTR cabinet_open(char *pszFile, int oflag, int pmode)
2422 {
2423     DWORD dwAccess = 0;
2424     DWORD dwShareMode = 0;
2425     DWORD dwCreateDisposition = OPEN_EXISTING;
2426     switch (oflag & _O_ACCMODE)
2427     {
2428     case _O_RDONLY:
2429         dwAccess = GENERIC_READ;
2430         dwShareMode = FILE_SHARE_READ | FILE_SHARE_DELETE;
2431         break;
2432     case _O_WRONLY:
2433         dwAccess = GENERIC_WRITE;
2434         dwShareMode = FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE;
2435         break;
2436     case _O_RDWR:
2437         dwAccess = GENERIC_READ | GENERIC_WRITE;
2438         dwShareMode = FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE;
2439         break;
2440     }
2441     if ((oflag & (_O_CREAT | _O_EXCL)) == (_O_CREAT | _O_EXCL))
2442         dwCreateDisposition = CREATE_NEW;
2443     else if (oflag & _O_CREAT)
2444         dwCreateDisposition = CREATE_ALWAYS;
2445     return (INT_PTR)CreateFileA(pszFile, dwAccess, dwShareMode, NULL, dwCreateDisposition, 0, NULL);
2446 }
2447
2448 static UINT cabinet_read(INT_PTR hf, void *pv, UINT cb)
2449 {
2450     DWORD dwRead;
2451     if (ReadFile((HANDLE)hf, pv, cb, &dwRead, NULL))
2452         return dwRead;
2453     return 0;
2454 }
2455
2456 static UINT cabinet_write(INT_PTR hf, void *pv, UINT cb)
2457 {
2458     DWORD dwWritten;
2459     if (WriteFile((HANDLE)hf, pv, cb, &dwWritten, NULL))
2460         return dwWritten;
2461     return 0;
2462 }
2463
2464 static int cabinet_close(INT_PTR hf)
2465 {
2466     return CloseHandle((HANDLE)hf) ? 0 : -1;
2467 }
2468
2469 static long cabinet_seek(INT_PTR hf, long dist, int seektype)
2470 {
2471     /* flags are compatible and so are passed straight through */
2472     return SetFilePointer((HANDLE)hf, dist, NULL, seektype);
2473 }
2474
2475 static INT_PTR cabinet_notify(FDINOTIFICATIONTYPE fdint, PFDINOTIFICATION pfdin)
2476 {
2477     /* FIXME: try to do more processing in this function */
2478     switch (fdint)
2479     {
2480     case fdintCOPY_FILE:
2481     {
2482         ULONG len = strlen((char*)pfdin->pv) + strlen(pfdin->psz1);
2483         char *file = cabinet_alloc((len+1)*sizeof(char));
2484
2485         strcpy(file, (char*)pfdin->pv);
2486         strcat(file, pfdin->psz1);
2487
2488         TRACE("file: %s\n", debugstr_a(file));
2489
2490         return cabinet_open(file, _O_WRONLY | _O_CREAT, 0);
2491     }
2492     case fdintCLOSE_FILE_INFO:
2493     {
2494         FILETIME ft;
2495             FILETIME ftLocal;
2496         if (!DosDateTimeToFileTime(pfdin->date, pfdin->time, &ft))
2497             return -1;
2498         if (!LocalFileTimeToFileTime(&ft, &ftLocal))
2499             return -1;
2500         if (!SetFileTime((HANDLE)pfdin->hf, &ftLocal, 0, &ftLocal))
2501             return -1;
2502
2503         cabinet_close(pfdin->hf);
2504         return 1;
2505     }
2506     default:
2507         return 0;
2508     }
2509 }
2510
2511 /***********************************************************************
2512  *            extract_cabinet_file
2513  *
2514  * Extract files from a cab file.
2515  */
2516 static BOOL extract_cabinet_file(const WCHAR* source, const WCHAR* path)
2517 {
2518     HFDI hfdi;
2519     ERF erf;
2520     BOOL ret;
2521     char *cabinet;
2522     char *cab_path;
2523
2524     TRACE("Extracting %s to %s\n",debugstr_w(source), debugstr_w(path));
2525
2526     hfdi = FDICreate(cabinet_alloc,
2527                      cabinet_free,
2528                      cabinet_open,
2529                      cabinet_read,
2530                      cabinet_write,
2531                      cabinet_close,
2532                      cabinet_seek,
2533                      0,
2534                      &erf);
2535     if (!hfdi)
2536     {
2537         ERR("FDICreate failed\n");
2538         return FALSE;
2539     }
2540
2541     if (!(cabinet = strdupWtoA( source )))
2542     {
2543         FDIDestroy(hfdi);
2544         return FALSE;
2545     }
2546     if (!(cab_path = strdupWtoA( path )))
2547     {
2548         FDIDestroy(hfdi);
2549         HeapFree(GetProcessHeap(), 0, cabinet);
2550         return FALSE;
2551     }
2552
2553     ret = FDICopy(hfdi, cabinet, "", 0, cabinet_notify, NULL, cab_path);
2554
2555     if (!ret)
2556         ERR("FDICopy failed\n");
2557
2558     FDIDestroy(hfdi);
2559
2560     HeapFree(GetProcessHeap(), 0, cabinet);
2561     HeapFree(GetProcessHeap(), 0, cab_path);
2562
2563     return ret;
2564 }
2565
2566 static UINT ready_media_for_file(MSIPACKAGE *package, UINT sequence, 
2567                                  WCHAR* path)
2568 {
2569     UINT rc;
2570     MSIQUERY * view;
2571     MSIRECORD * row = 0;
2572     WCHAR source[MAX_PATH];
2573     static const WCHAR ExecSeqQuery[] = {
2574         's','e','l','e','c','t',' ','*',' ',
2575         'f','r','o','m',' ','M','e','d','i','a',' ',
2576         'w','h','e','r','e',' ','L','a','s','t','S','e','q','u','e','n','c','e',' ','>','=',' ','%','i',' ',
2577         'o','r','d','e','r',' ','b','y',' ','L','a','s','t','S','e','q','u','e','n','c','e',0};
2578     WCHAR Query[1024];
2579     WCHAR cab[0x100];
2580     DWORD sz=0x100;
2581     INT seq;
2582     static UINT last_sequence = 0; 
2583
2584     if (sequence <= last_sequence)
2585     {
2586         TRACE("Media already ready (%u, %u)\n",sequence,last_sequence);
2587         return ERROR_SUCCESS;
2588     }
2589
2590     sprintfW(Query,ExecSeqQuery,sequence);
2591
2592     rc = MSI_DatabaseOpenViewW(package->db, Query, &view);
2593     if (rc != ERROR_SUCCESS)
2594         return rc;
2595
2596     rc = MSI_ViewExecute(view, 0);
2597     if (rc != ERROR_SUCCESS)
2598     {
2599         MSI_ViewClose(view);
2600         msiobj_release(&view->hdr);
2601         return rc;
2602     }
2603
2604     rc = MSI_ViewFetch(view,&row);
2605     if (rc != ERROR_SUCCESS)
2606     {
2607         MSI_ViewClose(view);
2608         msiobj_release(&view->hdr);
2609         return rc;
2610     }
2611     seq = MSI_RecordGetInteger(row,2);
2612     last_sequence = seq;
2613
2614     if (!MSI_RecordIsNull(row,4))
2615     {
2616         sz=0x100;
2617         MSI_RecordGetStringW(row,4,cab,&sz);
2618         TRACE("Source is CAB %s\n",debugstr_w(cab));
2619         /* the stream does not contain the # character */
2620         if (cab[0]=='#')
2621         {
2622             writeout_cabinet_stream(package,&cab[1],source);
2623             strcpyW(path,source);
2624             *(strrchrW(path,'\\')+1)=0;
2625         }
2626         else
2627         {
2628             sz = MAX_PATH;
2629             if (MSI_GetPropertyW(package, cszSourceDir, source, &sz))
2630             {
2631                 ERR("No Source dir defined \n");
2632                 rc = ERROR_FUNCTION_FAILED;
2633             }
2634             else
2635             {
2636                 strcpyW(path,source);
2637                 strcatW(source,cab);
2638                 /* extract the cab file into a folder in the temp folder */
2639                 sz = MAX_PATH;
2640                 if (MSI_GetPropertyW(package, cszTempFolder,path, &sz) 
2641                                     != ERROR_SUCCESS)
2642                     GetTempPathW(MAX_PATH,path);
2643             }
2644         }
2645         rc = !extract_cabinet_file(source,path);
2646     }
2647     msiobj_release(&row->hdr);
2648     MSI_ViewClose(view);
2649     msiobj_release(&view->hdr);
2650     return rc;
2651 }
2652
2653 inline static UINT create_component_directory ( MSIPACKAGE* package, INT component)
2654 {
2655     UINT rc;
2656     MSIFOLDER *folder;
2657     WCHAR install_path[MAX_PATH];
2658
2659     rc = resolve_folder(package, package->components[component].Directory,
2660                         install_path, FALSE, FALSE, &folder);
2661
2662     if (rc != ERROR_SUCCESS)
2663         return rc; 
2664
2665     /* create the path */
2666     if (folder->State == 0)
2667     {
2668         create_full_pathW(install_path);
2669         folder->State = 2;
2670     }
2671
2672     return rc;
2673 }
2674
2675 static UINT ACTION_InstallFiles(MSIPACKAGE *package)
2676 {
2677     UINT rc = ERROR_SUCCESS;
2678     DWORD index;
2679     MSIRECORD * uirow;
2680     WCHAR uipath[MAX_PATH];
2681
2682     if (!package)
2683         return ERROR_INVALID_HANDLE;
2684
2685     /* increment progress bar each time action data is sent */
2686     ui_progress(package,1,1,1,0);
2687
2688     for (index = 0; index < package->loaded_files; index++)
2689     {
2690         WCHAR path_to_source[MAX_PATH];
2691         MSIFILE *file;
2692         
2693         file = &package->files[index];
2694
2695         if (file->Temporary)
2696             continue;
2697
2698         if (!package->components[file->ComponentIndex].Enabled ||
2699             !package->components[file->ComponentIndex].FeatureState)
2700         {
2701             TRACE("File %s is not scheduled for install\n",
2702                    debugstr_w(file->File));
2703             continue;
2704         }
2705
2706         if ((file->State == 1) || (file->State == 2))
2707         {
2708             TRACE("Installing %s\n",debugstr_w(file->File));
2709             rc = ready_media_for_file(package,file->Sequence,path_to_source);
2710             /* 
2711              * WARNING!
2712              * our file table could change here because a new temp file
2713              * may have been created
2714              */
2715             file = &package->files[index];
2716             if (rc != ERROR_SUCCESS)
2717             {
2718                 ERR("Unable to ready media\n");
2719                 rc = ERROR_FUNCTION_FAILED;
2720                 break;
2721             }
2722
2723             create_component_directory( package, file->ComponentIndex);
2724
2725             strcpyW(file->SourcePath, path_to_source);
2726             strcatW(file->SourcePath, file->File);
2727
2728             TRACE("file paths %s to %s\n",debugstr_w(file->SourcePath),
2729                   debugstr_w(file->TargetPath));
2730
2731             /* the UI chunk */
2732             uirow=MSI_CreateRecord(9);
2733             MSI_RecordSetStringW(uirow,1,file->File);
2734             strcpyW(uipath,file->TargetPath);
2735             *(strrchrW(uipath,'\\')+1)=0;
2736             MSI_RecordSetStringW(uirow,9,uipath);
2737             MSI_RecordSetInteger(uirow,6,file->FileSize);
2738             ui_actiondata(package,szInstallFiles,uirow);
2739             msiobj_release( &uirow->hdr );
2740
2741             if (!MoveFileW(file->SourcePath,file->TargetPath))
2742             {
2743                 rc = GetLastError();
2744                 ERR("Unable to move file (%s -> %s) (error %d)\n",
2745                      debugstr_w(file->SourcePath), debugstr_w(file->TargetPath),
2746                       rc);
2747                 if (rc == ERROR_ALREADY_EXISTS && file->State == 2)
2748                 {
2749                     CopyFileW(file->SourcePath,file->TargetPath,FALSE);
2750                     DeleteFileW(file->SourcePath);
2751                     rc = 0;
2752                 }
2753                 else
2754                     break;
2755             }
2756             else
2757                 file->State = 4;
2758
2759             ui_progress(package,2,0,0,0);
2760         }
2761     }
2762
2763     return rc;
2764 }
2765
2766 inline static UINT get_file_target(MSIPACKAGE *package, LPCWSTR file_key, 
2767                                    LPWSTR file_source)
2768 {
2769     DWORD index;
2770
2771     if (!package)
2772         return ERROR_INVALID_HANDLE;
2773
2774     for (index = 0; index < package->loaded_files; index ++)
2775     {
2776         if (strcmpW(file_key,package->files[index].File)==0)
2777         {
2778             if (package->files[index].State >= 3)
2779             {
2780                 strcpyW(file_source,package->files[index].TargetPath);
2781                 return ERROR_SUCCESS;
2782             }
2783             else
2784                 return ERROR_FILE_NOT_FOUND;
2785         }
2786     }
2787
2788     return ERROR_FUNCTION_FAILED;
2789 }
2790
2791 static UINT ACTION_DuplicateFiles(MSIPACKAGE *package)
2792 {
2793     UINT rc;
2794     MSIQUERY * view;
2795     MSIRECORD * row = 0;
2796     static const WCHAR ExecSeqQuery[] = {
2797         's','e','l','e','c','t',' ','*',' ','f','r','o','m',' ',
2798         'D','u','p','l','i','c','a','t','e','F','i','l','e',0};
2799
2800     if (!package)
2801         return ERROR_INVALID_HANDLE;
2802
2803     rc = MSI_DatabaseOpenViewW(package->db, ExecSeqQuery, &view);
2804     if (rc != ERROR_SUCCESS)
2805         return ERROR_SUCCESS;
2806
2807     rc = MSI_ViewExecute(view, 0);
2808     if (rc != ERROR_SUCCESS)
2809     {
2810         MSI_ViewClose(view);
2811         msiobj_release(&view->hdr);
2812         return rc;
2813     }
2814
2815     while (1)
2816     {
2817         WCHAR file_key[0x100];
2818         WCHAR file_source[MAX_PATH];
2819         WCHAR dest_name[0x100];
2820         WCHAR dest_path[MAX_PATH];
2821         WCHAR component[0x100];
2822         INT component_index;
2823
2824         DWORD sz=0x100;
2825
2826         rc = MSI_ViewFetch(view,&row);
2827         if (rc != ERROR_SUCCESS)
2828         {
2829             rc = ERROR_SUCCESS;
2830             break;
2831         }
2832
2833         sz=0x100;
2834         rc = MSI_RecordGetStringW(row,2,component,&sz);
2835         if (rc != ERROR_SUCCESS)
2836         {
2837             ERR("Unable to get component\n");
2838             msiobj_release(&row->hdr);
2839             break;
2840         }
2841
2842         component_index = get_loaded_component(package,component);
2843         if (!package->components[component_index].Enabled ||
2844             !package->components[component_index].FeatureState)
2845         {
2846             TRACE("Skipping copy due to disabled component\n");
2847             msiobj_release(&row->hdr);
2848             continue;
2849         }
2850
2851         sz=0x100;
2852         rc = MSI_RecordGetStringW(row,3,file_key,&sz);
2853         if (rc != ERROR_SUCCESS)
2854         {
2855             ERR("Unable to get file key\n");
2856             msiobj_release(&row->hdr);
2857             break;
2858         }
2859
2860         rc = get_file_target(package,file_key,file_source);
2861
2862         if (rc != ERROR_SUCCESS)
2863         {
2864             ERR("Original file unknown %s\n",debugstr_w(file_key));
2865             msiobj_release(&row->hdr);
2866             break;
2867         }
2868
2869         if (MSI_RecordIsNull(row,4))
2870         {
2871             strcpyW(dest_name,strrchrW(file_source,'\\')+1);
2872         }
2873         else
2874         {
2875             sz=0x100;
2876             MSI_RecordGetStringW(row,4,dest_name,&sz);
2877             reduce_to_longfilename(dest_name);
2878          }
2879
2880         if (MSI_RecordIsNull(row,5))
2881         {
2882             strcpyW(dest_path,file_source);
2883             *strrchrW(dest_path,'\\')=0;
2884         }
2885         else
2886         {
2887             WCHAR destkey[0x100];
2888             sz=0x100;
2889             MSI_RecordGetStringW(row,5,destkey,&sz);
2890             sz = 0x100;
2891             rc = resolve_folder(package, destkey, dest_path,FALSE,FALSE,NULL);
2892             if (rc != ERROR_SUCCESS)
2893             {
2894                 ERR("Unable to get destination folder\n");
2895                 msiobj_release(&row->hdr);
2896                 break;
2897             }
2898         }
2899
2900         strcatW(dest_path,dest_name);
2901            
2902         TRACE("Duplicating file %s to %s\n",debugstr_w(file_source),
2903               debugstr_w(dest_path)); 
2904         
2905         if (strcmpW(file_source,dest_path))
2906             rc = !CopyFileW(file_source,dest_path,TRUE);
2907         else
2908             rc = ERROR_SUCCESS;
2909         
2910         if (rc != ERROR_SUCCESS)
2911             ERR("Failed to copy file\n");
2912
2913         FIXME("We should track these duplicate files as well\n");   
2914  
2915         msiobj_release(&row->hdr);
2916     }
2917     MSI_ViewClose(view);
2918     msiobj_release(&view->hdr);
2919     return rc;
2920
2921 }
2922
2923
2924 /* OK this value is "interpretted" and then formatted based on the 
2925    first few characters */
2926 static LPSTR parse_value(MSIPACKAGE *package, WCHAR *value, DWORD *type, 
2927                          DWORD *size)
2928 {
2929     LPSTR data = NULL;
2930     if (value[0]=='#' && value[1]!='#' && value[1]!='%')
2931     {
2932         if (value[1]=='x')
2933         {
2934             LPWSTR ptr;
2935             CHAR byte[5];
2936             LPWSTR deformated;
2937             int count;
2938
2939             deformat_string(package, &value[2], &deformated);
2940
2941             /* binary value type */
2942             ptr = deformated; 
2943             *type=REG_BINARY;
2944             *size = strlenW(ptr)/2;
2945             data = HeapAlloc(GetProcessHeap(),0,*size);
2946           
2947             byte[0] = '0'; 
2948             byte[1] = 'x'; 
2949             byte[4] = 0; 
2950             count = 0;
2951             while (*ptr)
2952             {
2953                 byte[2]= *ptr;
2954                 ptr++;
2955                 byte[3]= *ptr;
2956                 ptr++;
2957                 data[count] = (BYTE)strtol(byte,NULL,0);
2958                 count ++;
2959             }
2960             HeapFree(GetProcessHeap(),0,deformated);
2961
2962             TRACE("Data %li bytes(%i)\n",*size,count);
2963         }
2964         else
2965         {
2966             LPWSTR deformated;
2967             deformat_string(package, &value[1], &deformated);
2968
2969             *type=REG_DWORD; 
2970             *size = sizeof(DWORD);
2971             data = HeapAlloc(GetProcessHeap(),0,*size);
2972             *(LPDWORD)data = atoiW(deformated); 
2973             TRACE("DWORD %i\n",*data);
2974
2975             HeapFree(GetProcessHeap(),0,deformated);
2976         }
2977     }
2978     else
2979     {
2980         WCHAR *ptr;
2981         *type=REG_SZ;
2982
2983         if (value[0]=='#')
2984         {
2985             if (value[1]=='%')
2986             {
2987                 ptr = &value[2];
2988                 *type=REG_EXPAND_SZ;
2989             }
2990             else
2991                 ptr = &value[1];
2992          }
2993          else
2994             ptr=value;
2995
2996         *size = deformat_string(package, ptr,(LPWSTR*)&data);
2997     }
2998     return data;
2999 }
3000
3001 static UINT ACTION_WriteRegistryValues(MSIPACKAGE *package)
3002 {
3003     UINT rc;
3004     MSIQUERY * view;
3005     MSIRECORD * row = 0;
3006     static const WCHAR ExecSeqQuery[] = {
3007         's','e','l','e','c','t',' ','*',' ',
3008         'f','r','o','m',' ','R','e','g','i','s','t','r','y',0 };
3009
3010     if (!package)
3011         return ERROR_INVALID_HANDLE;
3012
3013     rc = MSI_DatabaseOpenViewW(package->db, ExecSeqQuery, &view);
3014     if (rc != ERROR_SUCCESS)
3015         return ERROR_SUCCESS;
3016
3017     rc = MSI_ViewExecute(view, 0);
3018     if (rc != ERROR_SUCCESS)
3019     {
3020         MSI_ViewClose(view);
3021         msiobj_release(&view->hdr);
3022         return rc;
3023     }
3024
3025     /* increment progress bar each time action data is sent */
3026     ui_progress(package,1,1,1,0);
3027
3028     while (1)
3029     {
3030         static const WCHAR szHCR[] = 
3031 {'H','K','E','Y','_','C','L','A','S','S','E','S','_','R','O','O','T','\\',0};
3032         static const WCHAR szHCU[] =
3033 {'H','K','E','Y','_','C','U','R','R','E','N','T','_','U','S','E','R','\\',0};
3034         static const WCHAR szHLM[] =
3035 {'H','K','E','Y','_','L','O','C','A','L','_','M','A','C','H','I','N','E',
3036 '\\',0};
3037         static const WCHAR szHU[] =
3038 {'H','K','E','Y','_','U','S','E','R','S','\\',0};
3039
3040         WCHAR key[0x100];
3041         WCHAR name[0x100];
3042         LPWSTR value;
3043         LPSTR value_data = NULL;
3044         HKEY  root_key, hkey;
3045         DWORD type,size;
3046         WCHAR component[0x100];
3047         INT component_index;
3048         MSIRECORD * uirow;
3049         WCHAR uikey[0x110];
3050
3051         INT   root;
3052         DWORD sz=0x100;
3053
3054         rc = MSI_ViewFetch(view,&row);
3055         if (rc != ERROR_SUCCESS)
3056         {
3057             rc = ERROR_SUCCESS;
3058             break;
3059         }
3060
3061         sz= 0x100;
3062         MSI_RecordGetStringW(row,6,component,&sz);
3063         component_index = get_loaded_component(package,component);
3064
3065         if (!package->components[component_index].Enabled ||
3066             !package->components[component_index].FeatureState)
3067         {
3068             TRACE("Skipping write due to disabled component\n");
3069             msiobj_release(&row->hdr);
3070             continue;
3071         }
3072
3073         /* null values have special meanings during uninstalls and such */
3074         
3075         if(MSI_RecordIsNull(row,5))
3076         {
3077             msiobj_release(&row->hdr);
3078             continue;
3079         }
3080
3081         root = MSI_RecordGetInteger(row,2);
3082         sz = 0x100;
3083         MSI_RecordGetStringW(row,3,key,&sz);
3084       
3085         sz = 0x100; 
3086         if (MSI_RecordIsNull(row,4))
3087             name[0]=0;
3088         else
3089             MSI_RecordGetStringW(row,4,name,&sz);
3090    
3091         /* get the root key */
3092         switch (root)
3093         {
3094             case 0:  root_key = HKEY_CLASSES_ROOT; 
3095                      strcpyW(uikey,szHCR); break;
3096             case 1:  root_key = HKEY_CURRENT_USER;
3097                      strcpyW(uikey,szHCU); break;
3098             case 2:  root_key = HKEY_LOCAL_MACHINE;
3099                      strcpyW(uikey,szHLM); break;
3100             case 3:  root_key = HKEY_USERS; 
3101                      strcpyW(uikey,szHU); break;
3102             default:
3103                  ERR("Unknown root %i\n",root);
3104                  root_key=NULL;
3105                  break;
3106         }
3107         if (!root_key)
3108         {
3109             msiobj_release(&row->hdr);
3110             continue;
3111         }
3112
3113         strcatW(uikey,key);
3114         if (RegCreateKeyW( root_key, key, &hkey))
3115         {
3116             ERR("Could not create key %s\n",debugstr_w(key));
3117             msiobj_release(&row->hdr);
3118             continue;
3119         }
3120
3121         value = load_dynamic_stringW(row,5);
3122         value_data = parse_value(package, value, &type, &size); 
3123
3124         if (value_data)
3125         {
3126             TRACE("Setting value %s\n",debugstr_w(name));
3127             RegSetValueExW(hkey, name, 0, type, value_data, size);
3128
3129             uirow = MSI_CreateRecord(3);
3130             MSI_RecordSetStringW(uirow,2,name);
3131             MSI_RecordSetStringW(uirow,1,uikey);
3132
3133             if (type == REG_SZ)
3134                 MSI_RecordSetStringW(uirow,3,(LPWSTR)value_data);
3135             else
3136                 MSI_RecordSetStringW(uirow,3,value);
3137
3138             ui_actiondata(package,szWriteRegistryValues,uirow);
3139             ui_progress(package,2,0,0,0);
3140             msiobj_release( &uirow->hdr );
3141
3142             HeapFree(GetProcessHeap(),0,value_data);
3143         }
3144         HeapFree(GetProcessHeap(),0,value);
3145
3146         msiobj_release(&row->hdr);
3147         RegCloseKey(hkey);
3148     }
3149     MSI_ViewClose(view);
3150     msiobj_release(&view->hdr);
3151     return rc;
3152 }
3153
3154 /*
3155  * This helper function should probably go alot of places
3156  *
3157  * Thinking about this, maybe this should become yet another Bison file
3158  */
3159 static DWORD deformat_string(MSIPACKAGE *package, WCHAR* ptr,WCHAR** data)
3160 {
3161     WCHAR* mark=NULL;
3162     DWORD size=0;
3163     DWORD chunk=0;
3164     WCHAR key[0x100];
3165     LPWSTR value;
3166     DWORD sz;
3167     UINT rc;
3168
3169     /* scan for special characters */
3170     if (!strchrW(ptr,'[') || (strchrW(ptr,'[') && !strchrW(ptr,']')))
3171     {
3172         /* not formatted */
3173         size = (strlenW(ptr)+1) * sizeof(WCHAR);
3174         *data = HeapAlloc(GetProcessHeap(),0,size);
3175         strcpyW(*data,ptr);
3176         return size;
3177     }
3178    
3179     /* formatted string located */ 
3180     mark = strchrW(ptr,'[');
3181     if (mark != ptr)
3182     {
3183         INT cnt = (mark - ptr);
3184         TRACE("%i  (%i) characters before marker\n",cnt,(mark-ptr));
3185         size = cnt * sizeof(WCHAR);
3186         size += sizeof(WCHAR);
3187         *data = HeapAlloc(GetProcessHeap(),0,size);
3188         strncpyW(*data,ptr,cnt);
3189         (*data)[cnt]=0;
3190     }
3191     else
3192     {
3193         size = sizeof(WCHAR);
3194         *data = HeapAlloc(GetProcessHeap(),0,size);
3195         (*data)[0]=0;
3196     }
3197     mark++;
3198     strcpyW(key,mark);
3199     *strchrW(key,']')=0;
3200     mark = strchrW(mark,']');
3201     mark++;
3202     TRACE("Current %s .. %s\n",debugstr_w(*data),debugstr_w(mark));
3203     sz = 0;
3204     rc = MSI_GetPropertyW(package, key, NULL, &sz);
3205     if ((rc == ERROR_SUCCESS) || (rc == ERROR_MORE_DATA))
3206     {
3207         LPWSTR newdata;
3208
3209         sz++;
3210         value = HeapAlloc(GetProcessHeap(),0,sz * sizeof(WCHAR));
3211         MSI_GetPropertyW(package, key, value, &sz);
3212
3213         chunk = (strlenW(value)+1) * sizeof(WCHAR);
3214         size+=chunk;   
3215         newdata = HeapReAlloc(GetProcessHeap(),0,*data,size);
3216         *data = newdata;
3217         strcatW(*data,value);
3218     }
3219     TRACE("Current %s .. %s\n",debugstr_w(*data),debugstr_w(mark));
3220     if (*mark!=0)
3221     {
3222         LPWSTR newdata;
3223         chunk = (strlenW(mark)+1) * sizeof(WCHAR);
3224         size+=chunk;
3225         newdata = HeapReAlloc(GetProcessHeap(),0,*data,size);
3226         *data = newdata;
3227         strcatW(*data,mark);
3228     }
3229     (*data)[strlenW(*data)]=0;
3230     TRACE("Current %s .. %s\n",debugstr_w(*data),debugstr_w(mark));
3231
3232     /* recursively do this to clean up */
3233     mark = HeapAlloc(GetProcessHeap(),0,size);
3234     strcpyW(mark,*data);
3235     TRACE("String at this point %s\n",debugstr_w(mark));
3236     size = deformat_string(package,mark,data);
3237     HeapFree(GetProcessHeap(),0,mark);
3238     return size;
3239 }
3240
3241 static UINT ACTION_InstallInitialize(MSIPACKAGE *package)
3242 {
3243     WCHAR level[10000];
3244     INT install_level;
3245     DWORD sz;
3246     DWORD i;
3247     INT j;
3248     DWORD rc;
3249     LPWSTR override = NULL;
3250     static const WCHAR addlocal[]={'A','D','D','L','O','C','A','L',0};
3251     static const WCHAR all[]={'A','L','L',0};
3252     static const WCHAR szlevel[] = {
3253         'I','N','S','T','A','L','L','L','E','V','E','L',0};
3254     static const WCHAR szAddLocal[] = {
3255         'A','D','D','L','O','C','A','L',0};
3256
3257     /* I do not know if this is where it should happen.. but */
3258
3259     TRACE("Checking Install Level\n");
3260
3261     sz = 10000;
3262     if (MSI_GetPropertyW(package,szlevel,level,&sz)==ERROR_SUCCESS)
3263         install_level = atoiW(level);
3264     else
3265         install_level = 1;
3266
3267     sz = 0;
3268     rc = MSI_GetPropertyW(package,szAddLocal,NULL,&sz);
3269     if (rc == ERROR_SUCCESS || rc == ERROR_MORE_DATA)
3270     {
3271         sz++;
3272         override = HeapAlloc(GetProcessHeap(),0,sz*sizeof(WCHAR));
3273         MSI_GetPropertyW(package, addlocal,override,&sz);
3274     }
3275    
3276     /*
3277      * Components FeatureState defaults to FALSE. The idea is we want to 
3278      * enable the component is ANY feature that uses it is enabled to install
3279      */
3280     for(i = 0; i < package->loaded_features; i++)
3281     {
3282         BOOL feature_state= ((package->features[i].Level > 0) &&
3283                              (package->features[i].Level <= install_level));
3284
3285         if (override && (strcmpiW(override,all)==0 || 
3286                          strstrW(override,package->features[i].Feature)))
3287         {
3288             TRACE("Override of install level found\n");
3289             feature_state = TRUE;
3290             package->features[i].Enabled = feature_state;
3291         }
3292
3293         TRACE("Feature %s has a state of %i\n",
3294                debugstr_w(package->features[i].Feature), feature_state);
3295         for( j = 0; j < package->features[i].ComponentCount; j++)
3296         {
3297             package->components[package->features[i].Components[j]].FeatureState
3298             |= feature_state;
3299         }
3300     } 
3301     if (override != NULL)
3302         HeapFree(GetProcessHeap(),0,override);
3303     /* 
3304      * So basically we ONLY want to install a component if its Enabled AND
3305      * FeatureState are both TRUE 
3306      */
3307     return ERROR_SUCCESS;
3308 }
3309
3310 static UINT ACTION_InstallValidate(MSIPACKAGE *package)
3311 {
3312     DWORD progress = 0;
3313     static const WCHAR q1[]={
3314         'S','E','L','E','C','T',' ','*',' ',
3315         'F','R','O','M',' ','R','e','g','i','s','t','r','y',0};
3316     UINT rc;
3317     MSIQUERY * view;
3318     MSIRECORD * row = 0;
3319
3320     TRACE(" InstallValidate \n");
3321
3322     rc = MSI_DatabaseOpenViewW(package->db, q1, &view);
3323     if (rc != ERROR_SUCCESS)
3324         return ERROR_SUCCESS;
3325
3326     rc = MSI_ViewExecute(view, 0);
3327     if (rc != ERROR_SUCCESS)
3328     {
3329         MSI_ViewClose(view);
3330         msiobj_release(&view->hdr);
3331         return rc;
3332     }
3333     while (1)
3334     {
3335         rc = MSI_ViewFetch(view,&row);
3336         if (rc != ERROR_SUCCESS)
3337         {
3338             rc = ERROR_SUCCESS;
3339             break;
3340         }
3341         progress +=1;
3342
3343         msiobj_release(&row->hdr);
3344     }
3345     MSI_ViewClose(view);
3346     msiobj_release(&view->hdr);
3347
3348     ui_progress(package,0,progress+package->loaded_files,0,0);
3349
3350     return ERROR_SUCCESS;
3351 }
3352
3353 static UINT ACTION_LaunchConditions(MSIPACKAGE *package)
3354 {
3355     UINT rc;
3356     MSIQUERY * view = NULL;
3357     MSIRECORD * row = 0;
3358     static const WCHAR ExecSeqQuery[] = {
3359         'S','E','L','E','C','T',' ','*',' ',
3360         'f','r','o','m',' ','L','a','u','n','c','h','C','o','n','d','i','t','i','o','n',0};
3361     static const WCHAR title[]=
3362             {'I','n','s','t','a','l','l',' ','F','a', 'i','l','e','d',0};
3363
3364     TRACE("Checking launch conditions\n");
3365
3366     rc = MSI_DatabaseOpenViewW(package->db, ExecSeqQuery, &view);
3367     if (rc != ERROR_SUCCESS)
3368         return ERROR_SUCCESS;
3369
3370     rc = MSI_ViewExecute(view, 0);
3371     if (rc != ERROR_SUCCESS)
3372     {
3373         MSI_ViewClose(view);
3374         msiobj_release(&view->hdr);
3375         return rc;
3376     }
3377
3378     rc = ERROR_SUCCESS;
3379     while (rc == ERROR_SUCCESS)
3380     {
3381         LPWSTR cond = NULL; 
3382         LPWSTR message = NULL;
3383
3384         rc = MSI_ViewFetch(view,&row);
3385         if (rc != ERROR_SUCCESS)
3386         {
3387             rc = ERROR_SUCCESS;
3388             break;
3389         }
3390
3391         cond = load_dynamic_stringW(row,1);
3392
3393         if (MSI_EvaluateConditionW(package,cond) != MSICONDITION_TRUE)
3394         {
3395             message = load_dynamic_stringW(row,2);
3396             MessageBoxW(NULL,message,title,MB_OK);
3397             HeapFree(GetProcessHeap(),0,message);
3398             rc = ERROR_FUNCTION_FAILED;
3399         }
3400         HeapFree(GetProcessHeap(),0,cond);
3401         msiobj_release(&row->hdr);
3402     }
3403     MSI_ViewClose(view);
3404     msiobj_release(&view->hdr);
3405     return rc;
3406 }
3407
3408 static void resolve_keypath( MSIPACKAGE* package, INT
3409                             component_index, WCHAR *keypath)
3410 {
3411     MSICOMPONENT* cmp = &package->components[component_index];
3412
3413     if (cmp->KeyPath[0]==0)
3414     {
3415         resolve_folder(package,cmp->Directory,keypath,FALSE,FALSE,NULL);
3416         return;
3417     }
3418     if ((cmp->Attributes & 0x4) || (cmp->Attributes & 0x20))
3419     {
3420         FIXME("UNIMPLEMENTED keypath as Registry or ODBC Source\n");
3421         keypath[0]=0;
3422     }
3423     else
3424     {
3425         int j;
3426         j = get_loaded_file(package,cmp->KeyPath);
3427
3428         if (j>=0)
3429             strcpyW(keypath,package->files[j].TargetPath);
3430     }
3431 }
3432
3433 /*
3434  * Ok further analysis makes me think that this work is
3435  * actually done in the PublishComponents and PublishFeatures
3436  * step, and not here.  It appears like the keypath and all that is
3437  * resolved in this step, however actually written in the Publish steps.
3438  * But we will leave it here for now because it is unclear
3439  */
3440 static UINT ACTION_ProcessComponents(MSIPACKAGE *package)
3441 {
3442     WCHAR productcode[0x100];
3443     WCHAR squished_pc[0x100];
3444     WCHAR squished_cc[0x100];
3445     DWORD sz;
3446     UINT rc;
3447     DWORD i;
3448     HKEY hkey=0,hkey2=0,hkey3=0;
3449     static const WCHAR szProductCode[]=
3450 {'P','r','o','d','u','c','t','C','o','d','e',0};
3451     static const WCHAR szInstaller[] = {
3452 'S','o','f','t','w','a','r','e','\\',
3453 'M','i','c','r','o','s','o','f','t','\\',
3454 'W','i','n','d','o','w','s','\\',
3455 'C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
3456 'I','n','s','t','a','l','l','e','r',0 };
3457     static const WCHAR szFeatures[] = {
3458 'F','e','a','t','u','r','e','s',0 };
3459     static const WCHAR szComponents[] = {
3460 'C','o','m','p','o','n','e','n','t','s',0 };
3461
3462     if (!package)
3463         return ERROR_INVALID_HANDLE;
3464
3465     /* writes the Component and Features values to the registry */
3466     sz = 0x100;
3467     rc = MSI_GetPropertyW(package,szProductCode,productcode,&sz);
3468     if (rc != ERROR_SUCCESS)
3469         return ERROR_SUCCESS;
3470
3471     squash_guid(productcode,squished_pc);
3472     rc = RegCreateKeyW(HKEY_LOCAL_MACHINE,szInstaller,&hkey);
3473     if (rc != ERROR_SUCCESS)
3474         goto end;
3475
3476     rc = RegCreateKeyW(hkey,szFeatures,&hkey2);
3477     if (rc != ERROR_SUCCESS)
3478         goto end;
3479
3480     rc = RegCreateKeyW(hkey2,squished_pc,&hkey3);
3481     if (rc != ERROR_SUCCESS)
3482         goto end;
3483
3484     /* here the guids are base 85 encoded */
3485     for (i = 0; i < package->loaded_features; i++)
3486     {
3487         LPWSTR data = NULL;
3488         GUID clsid;
3489         int j;
3490         INT size;
3491
3492         size = package->features[i].ComponentCount*21*sizeof(WCHAR);
3493         data = HeapAlloc(GetProcessHeap(), 0, size);
3494
3495         data[0] = 0;
3496         for (j = 0; j < package->features[i].ComponentCount; j++)
3497         {
3498             WCHAR buf[21];
3499             TRACE("From %s\n",debugstr_w(package->components
3500                             [package->features[i].Components[j]].ComponentId));
3501             CLSIDFromString(package->components
3502                             [package->features[i].Components[j]].ComponentId,
3503                             &clsid);
3504             encode_base85_guid(&clsid,buf);
3505             TRACE("to %s\n",debugstr_w(buf));
3506             strcatW(data,buf);
3507         }
3508
3509         size = strlenW(data)*sizeof(WCHAR);
3510         RegSetValueExW(hkey3,package->features[i].Feature,0,REG_SZ,
3511                        (LPSTR)data,size);
3512         HeapFree(GetProcessHeap(),0,data);
3513     }
3514
3515     RegCloseKey(hkey3);
3516     RegCloseKey(hkey2);
3517
3518     rc = RegCreateKeyW(hkey,szComponents,&hkey2);
3519     if (rc != ERROR_SUCCESS)
3520         goto end;
3521   
3522     for (i = 0; i < package->loaded_components; i++)
3523     {
3524         if (package->components[i].ComponentId[0]!=0)
3525         {
3526             WCHAR keypath[0x1000];
3527             MSIRECORD * uirow;
3528
3529             squash_guid(package->components[i].ComponentId,squished_cc);
3530             rc = RegCreateKeyW(hkey2,squished_cc,&hkey3);
3531             if (rc != ERROR_SUCCESS)
3532                 continue;
3533            
3534             resolve_keypath(package,i,keypath);
3535
3536             RegSetValueExW(hkey3,squished_pc,0,REG_SZ,(LPVOID)keypath,
3537                             (strlenW(keypath)+1)*sizeof(WCHAR));
3538             RegCloseKey(hkey3);
3539         
3540             /* UI stuff */
3541             uirow = MSI_CreateRecord(3);
3542             MSI_RecordSetStringW(uirow,1,productcode);
3543             MSI_RecordSetStringW(uirow,2,package->components[i].ComponentId);
3544             MSI_RecordSetStringW(uirow,3,keypath);
3545             ui_actiondata(package,szProcessComponents,uirow);
3546             msiobj_release( &uirow->hdr );
3547         }
3548     } 
3549 end:
3550     RegCloseKey(hkey2);
3551     RegCloseKey(hkey);
3552     return rc;
3553 }
3554
3555 static UINT ACTION_RegisterTypeLibraries(MSIPACKAGE *package)
3556 {
3557     /* 
3558      * OK this is a bit confusing.. I am given a _Component key and I believe
3559      * that the file that is being registered as a type library is the "key file
3560      * of that component" which I interpret to mean "The file in the KeyPath of
3561      * that component".
3562      */
3563     UINT rc;
3564     MSIQUERY * view;
3565     MSIRECORD * row = 0;
3566     static const WCHAR Query[] = {
3567         'S','E','L','E','C','T',' ','*',' ',
3568         'f','r','o','m',' ','T','y','p','e','L','i','b',0};
3569     ITypeLib *ptLib;
3570     HRESULT res;
3571
3572     if (!package)
3573         return ERROR_INVALID_HANDLE;
3574
3575     rc = MSI_DatabaseOpenViewW(package->db, Query, &view);
3576     if (rc != ERROR_SUCCESS)
3577         return ERROR_SUCCESS;
3578
3579     rc = MSI_ViewExecute(view, 0);
3580     if (rc != ERROR_SUCCESS)
3581     {
3582         MSI_ViewClose(view);
3583         msiobj_release(&view->hdr);
3584         return rc;
3585     }
3586
3587     while (1)
3588     {   
3589         WCHAR component[0x100];
3590         DWORD sz;
3591         INT index;
3592
3593         rc = MSI_ViewFetch(view,&row);
3594         if (rc != ERROR_SUCCESS)
3595         {
3596             rc = ERROR_SUCCESS;
3597             break;
3598         }
3599
3600         sz = 0x100;
3601         MSI_RecordGetStringW(row,3,component,&sz);
3602
3603         index = get_loaded_component(package,component);
3604         if (index < 0)
3605         {
3606             msiobj_release(&row->hdr);
3607             continue;
3608         }
3609
3610         if (!package->components[index].Enabled ||
3611             !package->components[index].FeatureState)
3612         {
3613             TRACE("Skipping typelib reg due to disabled component\n");
3614             msiobj_release(&row->hdr);
3615             continue;
3616         }
3617
3618         index = get_loaded_file(package,package->components[index].KeyPath); 
3619    
3620         if (index < 0)
3621         {
3622             msiobj_release(&row->hdr);
3623             continue;
3624         }
3625
3626         res = LoadTypeLib(package->files[index].TargetPath,&ptLib);
3627         if (SUCCEEDED(res))
3628         {
3629             WCHAR help[MAX_PATH];
3630             WCHAR helpid[0x100];
3631
3632             sz = 0x100;
3633             MSI_RecordGetStringW(row,6,helpid,&sz);
3634
3635             resolve_folder(package,helpid,help,FALSE,FALSE,NULL);
3636
3637             res = RegisterTypeLib(ptLib,package->files[index].TargetPath,help);
3638             if (!SUCCEEDED(res))
3639                 ERR("Failed to register type library %s\n",
3640                      debugstr_w(package->files[index].TargetPath));
3641             else
3642             {
3643                 /* Yes the row has more fields than I need, but #1 is 
3644                    correct and the only one I need. Why make a new row? */
3645
3646                 ui_actiondata(package,szRegisterTypeLibraries,row);
3647                 
3648                 TRACE("Registered %s\n",
3649                        debugstr_w(package->files[index].TargetPath));
3650             }
3651
3652             if (ptLib)
3653                 ITypeLib_Release(ptLib);
3654         }
3655         else
3656             ERR("Failed to load type library %s\n",
3657                 debugstr_w(package->files[index].TargetPath));
3658         
3659         msiobj_release(&row->hdr);
3660     }
3661     MSI_ViewClose(view);
3662     msiobj_release(&view->hdr);
3663     return rc;
3664    
3665 }
3666
3667 static UINT register_appid(MSIPACKAGE *package, LPCWSTR clsid, LPCWSTR app )
3668 {
3669     static const WCHAR szAppID[] = { 'A','p','p','I','D',0 };
3670     UINT rc;
3671     MSIQUERY * view;
3672     MSIRECORD * row = 0;
3673     static const WCHAR ExecSeqQuery[] = 
3674 {'S','E','L','E','C','T',' ','*',' ','f','r','o','m',' ','A','p','p','I'
3675 ,'d',' ','w','h','e','r','e',' ','A','p','p','I','d','=','`','%','s','`',0};
3676     WCHAR Query[0x1000];
3677     HKEY hkey2,hkey3;
3678     LPWSTR buffer=0;
3679
3680     if (!package)
3681         return ERROR_INVALID_HANDLE;
3682
3683     sprintfW(Query,ExecSeqQuery,clsid);
3684
3685     rc = MSI_DatabaseOpenViewW(package->db, Query, &view);
3686     if (rc != ERROR_SUCCESS)
3687         return rc;
3688
3689     rc = MSI_ViewExecute(view, 0);
3690     if (rc != ERROR_SUCCESS)
3691     {
3692         MSI_ViewClose(view);
3693         msiobj_release(&view->hdr);
3694         return rc;
3695     }
3696
3697     RegCreateKeyW(HKEY_CLASSES_ROOT,szAppID,&hkey2);
3698     RegCreateKeyW(hkey2,clsid,&hkey3);
3699     RegSetValueExW(hkey3,NULL,0,REG_SZ,(LPVOID)app,
3700                    (strlenW(app)+1)*sizeof(WCHAR));
3701
3702     rc = MSI_ViewFetch(view,&row);
3703     if (rc != ERROR_SUCCESS)
3704     {
3705         MSI_ViewClose(view);
3706         msiobj_release(&view->hdr);
3707         return rc;
3708     }
3709
3710     if (!MSI_RecordIsNull(row,2)) 
3711     {
3712         LPWSTR deformated=0;
3713         UINT size; 
3714         static const WCHAR szRemoteServerName[] =
3715 {'R','e','m','o','t','e','S','e','r','v','e','r','N','a','m','e',0};
3716         buffer = load_dynamic_stringW(row,2);
3717         size = deformat_string(package,buffer,&deformated);
3718         RegSetValueExW(hkey3,szRemoteServerName,0,REG_SZ,(LPVOID)deformated,
3719                        size);
3720         HeapFree(GetProcessHeap(),0,deformated);
3721         HeapFree(GetProcessHeap(),0,buffer);
3722     }
3723
3724     if (!MSI_RecordIsNull(row,3)) 
3725     {
3726         static const WCHAR szLocalService[] =
3727 {'L','o','c','a','l','S','e','r','v','i','c','e',0};
3728         UINT size;
3729         buffer = load_dynamic_stringW(row,3);
3730         size = (strlenW(buffer)+1) * sizeof(WCHAR);
3731         RegSetValueExW(hkey3,szLocalService,0,REG_SZ,(LPVOID)buffer,size);
3732         HeapFree(GetProcessHeap(),0,buffer);
3733     }
3734
3735     if (!MSI_RecordIsNull(row,4)) 
3736     {
3737         static const WCHAR szService[] =
3738 {'S','e','r','v','i','c','e','P','a','r','a','m','e','t','e','r','s',0};
3739         UINT size;
3740         buffer = load_dynamic_stringW(row,4);
3741         size = (strlenW(buffer)+1) * sizeof(WCHAR);
3742         RegSetValueExW(hkey3,szService,0,REG_SZ,(LPVOID)buffer,size);
3743         HeapFree(GetProcessHeap(),0,buffer);
3744     }
3745
3746     if (!MSI_RecordIsNull(row,5)) 
3747     {
3748         static const WCHAR szDLL[] =
3749 {'D','l','l','S','u','r','r','o','g','a','t','e',0};
3750         UINT size;
3751         buffer = load_dynamic_stringW(row,5);
3752         size = (strlenW(buffer)+1) * sizeof(WCHAR);
3753         RegSetValueExW(hkey3,szDLL,0,REG_SZ,(LPVOID)buffer,size);
3754         HeapFree(GetProcessHeap(),0,buffer);
3755     }
3756
3757     if (!MSI_RecordIsNull(row,6)) 
3758     {
3759         static const WCHAR szActivate[] =
3760 {'A','c','t','i','v','a','t','e','A','s','S','t','o','r','a','g','e',0};
3761         static const WCHAR szY[] = {'Y',0};
3762
3763         if (MSI_RecordGetInteger(row,6))
3764             RegSetValueExW(hkey3,szActivate,0,REG_SZ,(LPVOID)szY,4);
3765     }
3766
3767     if (!MSI_RecordIsNull(row,7)) 
3768     {
3769         static const WCHAR szRunAs[] = {'R','u','n','A','s',0};
3770         static const WCHAR szUser[] = 
3771 {'I','n','t','e','r','a','c','t','i','v','e',' ','U','s','e','r',0};
3772
3773         if (MSI_RecordGetInteger(row,7))
3774             RegSetValueExW(hkey3,szRunAs,0,REG_SZ,(LPVOID)szUser,34);
3775     }
3776
3777     msiobj_release(&row->hdr);
3778     MSI_ViewClose(view);
3779     msiobj_release(&view->hdr);
3780     RegCloseKey(hkey3);
3781     RegCloseKey(hkey2);
3782     return rc;
3783 }
3784
3785 static UINT ACTION_RegisterClassInfo(MSIPACKAGE *package)
3786 {
3787     /* 
3788      * Again I am assuming the words, "Whose key file represents" when referring
3789      * to a Component as to meaning that Components KeyPath file
3790      *
3791      * Also there is a very strong connection between ClassInfo and ProgID
3792      * that I am mostly glossing over.  
3793      * What would be more propper is to load the ClassInfo and the ProgID info
3794      * into memory data structures and then be able to enable and disable them
3795      * based on component. 
3796      */
3797     
3798     UINT rc;
3799     MSIQUERY * view;
3800     MSIRECORD * row = 0;
3801     static const WCHAR ExecSeqQuery[] = {
3802         'S','E','L','E','C','T',' ','*',' ',
3803         'f','r','o','m',' ','C','l','a','s','s',0};
3804     static const WCHAR szCLSID[] = { 'C','L','S','I','D',0 };
3805     static const WCHAR szProgID[] = { 'P','r','o','g','I','D',0 };
3806     static const WCHAR szAppID[] = { 'A','p','p','I','D',0 };
3807     HKEY hkey,hkey2,hkey3;
3808
3809     if (!package)
3810         return ERROR_INVALID_HANDLE;
3811
3812     rc = RegCreateKeyW(HKEY_CLASSES_ROOT,szCLSID,&hkey);
3813     if (rc != ERROR_SUCCESS)
3814         return ERROR_FUNCTION_FAILED;
3815
3816     rc = MSI_DatabaseOpenViewW(package->db, ExecSeqQuery, &view);
3817     if (rc != ERROR_SUCCESS)
3818     {
3819         rc = ERROR_SUCCESS;
3820         goto end;
3821     }
3822
3823     rc = MSI_ViewExecute(view, 0);
3824     if (rc != ERROR_SUCCESS)
3825     {
3826         MSI_ViewClose(view);
3827         msiobj_release(&view->hdr);
3828         goto end;
3829     }
3830
3831     while (1)
3832     {
3833         WCHAR clsid[0x100];
3834         WCHAR buffer[0x100];
3835         WCHAR desc[0x100];
3836         DWORD sz;
3837         INT index;
3838      
3839         rc = MSI_ViewFetch(view,&row);
3840         if (rc != ERROR_SUCCESS)
3841         {
3842             rc = ERROR_SUCCESS;
3843             break;
3844         }
3845
3846         sz=0x100;
3847         MSI_RecordGetStringW(row,3,buffer,&sz);
3848
3849         index = get_loaded_component(package,buffer);
3850
3851         if (index < 0)
3852         {
3853             msiobj_release(&row->hdr);
3854             continue;
3855         }
3856
3857         if (!package->components[index].Enabled ||
3858             !package->components[index].FeatureState)
3859         {
3860             TRACE("Skipping class reg due to disabled component\n");
3861             msiobj_release(&row->hdr);
3862             continue;
3863         }
3864
3865         sz=0x100;
3866         MSI_RecordGetStringW(row,1,clsid,&sz);
3867         RegCreateKeyW(hkey,clsid,&hkey2);
3868
3869         if (!MSI_RecordIsNull(row,5))
3870         {
3871             sz=0x100;
3872             MSI_RecordGetStringW(row,5,desc,&sz);
3873
3874             RegSetValueExW(hkey2,NULL,0,REG_SZ,(LPVOID)desc,
3875                            (strlenW(desc)+1)*sizeof(WCHAR));
3876         }
3877         else
3878             desc[0]=0;
3879
3880         sz=0x100;
3881         MSI_RecordGetStringW(row,2,buffer,&sz);
3882
3883         RegCreateKeyW(hkey2,buffer,&hkey3);
3884
3885         index = get_loaded_file(package,package->components[index].KeyPath);
3886         RegSetValueExW(hkey3,NULL,0,REG_SZ,
3887                        (LPVOID)package->files[index].TargetPath,
3888                        (strlenW(package->files[index].TargetPath)+1)
3889                         *sizeof(WCHAR));
3890
3891         RegCloseKey(hkey3);
3892
3893         if (!MSI_RecordIsNull(row,4))
3894         {
3895             sz=0x100;
3896             MSI_RecordGetStringW(row,4,buffer,&sz);
3897
3898             RegCreateKeyW(hkey2,szProgID,&hkey3);
3899     
3900             RegSetValueExW(hkey3,NULL,0,REG_SZ,(LPVOID)buffer,
3901                        (strlenW(buffer)+1)*sizeof(WCHAR));
3902
3903             RegCloseKey(hkey3);
3904         }
3905
3906         if (!MSI_RecordIsNull(row,6))
3907         { 
3908             sz=0x100;
3909             MSI_RecordGetStringW(row,6,buffer,&sz);
3910
3911             RegSetValueExW(hkey2,szAppID,0,REG_SZ,(LPVOID)buffer,
3912                        (strlenW(buffer)+1)*sizeof(WCHAR));
3913
3914             register_appid(package,buffer,desc);
3915         }
3916
3917         RegCloseKey(hkey2);
3918
3919         FIXME("Process the rest of the fields >7\n");
3920
3921         ui_actiondata(package,szRegisterClassInfo,row);
3922
3923         msiobj_release(&row->hdr);
3924     }
3925     MSI_ViewClose(view);
3926     msiobj_release(&view->hdr);
3927
3928 end:
3929     RegCloseKey(hkey);
3930     return rc;
3931 }
3932
3933 static UINT register_progid_base(MSIRECORD * row, LPWSTR clsid)
3934 {
3935     static const WCHAR szCLSID[] = { 'C','L','S','I','D',0 };
3936     HKEY hkey,hkey2;
3937     WCHAR buffer[0x100];
3938     DWORD sz;
3939
3940
3941     sz = 0x100;
3942     MSI_RecordGetStringW(row,1,buffer,&sz);
3943     RegCreateKeyW(HKEY_CLASSES_ROOT,buffer,&hkey);
3944
3945     if (!MSI_RecordIsNull(row,4))
3946     {
3947         sz = 0x100;
3948         MSI_RecordGetStringW(row,4,buffer,&sz);
3949         RegSetValueExW(hkey,NULL,0,REG_SZ,(LPVOID)buffer, (strlenW(buffer)+1) *
3950                        sizeof(WCHAR));
3951     }
3952
3953     if (!MSI_RecordIsNull(row,3))
3954     {   
3955         sz = 0x100;
3956     
3957         MSI_RecordGetStringW(row,3,buffer,&sz);
3958         RegCreateKeyW(hkey,szCLSID,&hkey2);
3959         RegSetValueExW(hkey2,NULL,0,REG_SZ,(LPVOID)buffer, (strlenW(buffer)+1) *
3960                        sizeof(WCHAR));
3961
3962         if (clsid)
3963             strcpyW(clsid,buffer);
3964
3965         RegCloseKey(hkey2);
3966     }
3967     else
3968     {
3969         FIXME("UNHANDLED case, Parent progid but classid is NULL\n");
3970         return ERROR_FUNCTION_FAILED;
3971     }
3972     if (!MSI_RecordIsNull(row,5))
3973         FIXME ("UNHANDLED icon in Progid\n");
3974     return ERROR_SUCCESS;
3975 }
3976
3977 static UINT register_progid(MSIPACKAGE *package, MSIRECORD * row, LPWSTR clsid);
3978
3979 static UINT register_parent_progid(MSIPACKAGE *package, LPCWSTR parent, 
3980                                    LPWSTR clsid)
3981 {
3982     UINT rc;
3983     MSIQUERY * view;
3984     MSIRECORD * row = 0;
3985     static const WCHAR Query_t[] = 
3986 {'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ','P','r','o','g'
3987 ,'I','d',' ','w','h','e','r','e',' ','P','r','o','g','I','d',' ','=',' ','`'
3988 ,'%','s','`',0};
3989     WCHAR Query[0x1000];
3990
3991     if (!package)
3992         return ERROR_INVALID_HANDLE;
3993
3994     sprintfW(Query,Query_t,parent);
3995
3996     rc = MSI_DatabaseOpenViewW(package->db, Query, &view);
3997     if (rc != ERROR_SUCCESS)
3998         return rc;
3999
4000     rc = MSI_ViewExecute(view, 0);
4001     if (rc != ERROR_SUCCESS)
4002     {
4003         MSI_ViewClose(view);
4004         msiobj_release(&view->hdr);
4005         return rc;
4006     }
4007
4008     rc = MSI_ViewFetch(view,&row);
4009     if (rc != ERROR_SUCCESS)
4010     {
4011         MSI_ViewClose(view);
4012         msiobj_release(&view->hdr);
4013         return rc;
4014     }
4015
4016     register_progid(package,row,clsid);
4017
4018     msiobj_release(&row->hdr);
4019     MSI_ViewClose(view);
4020     msiobj_release(&view->hdr);
4021     return rc;
4022 }
4023
4024 static UINT register_progid(MSIPACKAGE *package, MSIRECORD * row, LPWSTR clsid)
4025 {
4026     UINT rc = ERROR_SUCCESS; 
4027
4028     if (MSI_RecordIsNull(row,2))
4029         rc = register_progid_base(row,clsid);
4030     else
4031     {
4032         WCHAR buffer[0x1000];
4033         DWORD sz, disp;
4034         HKEY hkey,hkey2;
4035         static const WCHAR szCLSID[] = { 'C','L','S','I','D',0 };
4036
4037         /* check if already registered */
4038         sz = 0x100;
4039         MSI_RecordGetStringW(row,1,buffer,&sz);
4040         RegCreateKeyExW(HKEY_CLASSES_ROOT, buffer, 0, NULL, 0,
4041                         KEY_ALL_ACCESS, NULL, &hkey, &disp );
4042         if (disp == REG_OPENED_EXISTING_KEY)
4043         {
4044             TRACE("Key already registered\n");
4045             RegCloseKey(hkey);
4046             return rc;
4047         }
4048         /* clsid is same as parent */
4049         RegCreateKeyW(hkey,szCLSID,&hkey2);
4050         RegSetValueExW(hkey2,NULL,0,REG_SZ,(LPVOID)clsid, (strlenW(clsid)+1) *
4051                        sizeof(WCHAR));
4052
4053         RegCloseKey(hkey2);
4054
4055         sz = 0x100;
4056         MSI_RecordGetStringW(row,2,buffer,&sz);
4057         rc = register_parent_progid(package,buffer,clsid);
4058
4059         if (!MSI_RecordIsNull(row,4))
4060         {
4061             sz = 0x100;
4062             MSI_RecordGetStringW(row,4,buffer,&sz);
4063             RegSetValueExW(hkey,NULL,0,REG_SZ,(LPVOID)buffer,
4064                            (strlenW(buffer)+1) * sizeof(WCHAR));
4065         }
4066
4067         if (!MSI_RecordIsNull(row,5))
4068             FIXME ("UNHANDLED icon in Progid\n");
4069
4070         RegCloseKey(hkey);
4071     }
4072     return rc;
4073 }
4074
4075 static UINT ACTION_RegisterProgIdInfo(MSIPACKAGE *package)
4076 {
4077     /* 
4078      * Sigh, here I am just brute force registering all progids
4079      * this needs to be linked to the Classes that have been registered
4080      * but the easiest way to do that is to load all these stuff into
4081      * memory for easy checking.
4082      *
4083      * Gives me something to continue to work toward.
4084      */
4085     UINT rc;
4086     MSIQUERY * view;
4087     MSIRECORD * row = 0;
4088     static const WCHAR Query[] = {
4089         'S','E','L','E','C','T',' ','*',' ',
4090         'F','R','O','M',' ','P','r','o','g','I','d',0};
4091
4092     if (!package)
4093         return ERROR_INVALID_HANDLE;
4094
4095     rc = MSI_DatabaseOpenViewW(package->db, Query, &view);
4096     if (rc != ERROR_SUCCESS)
4097         return ERROR_SUCCESS;
4098
4099     rc = MSI_ViewExecute(view, 0);
4100     if (rc != ERROR_SUCCESS)
4101     {
4102         MSI_ViewClose(view);
4103         msiobj_release(&view->hdr);
4104         return rc;
4105     }
4106
4107     while (1)
4108     {
4109         WCHAR clsid[0x1000];
4110
4111         rc = MSI_ViewFetch(view,&row);
4112         if (rc != ERROR_SUCCESS)
4113         {
4114             rc = ERROR_SUCCESS;
4115             break;
4116         }
4117         
4118         register_progid(package,row,clsid);
4119         ui_actiondata(package,szRegisterProgIdInfo,row);
4120
4121         msiobj_release(&row->hdr);
4122     }
4123     MSI_ViewClose(view);
4124     msiobj_release(&view->hdr);
4125     return rc;
4126 }
4127
4128 static UINT build_icon_path(MSIPACKAGE *package, LPCWSTR icon_name, 
4129                             LPWSTR FilePath)
4130 {
4131     WCHAR ProductCode[0x100];
4132     WCHAR SystemFolder[MAX_PATH];
4133     DWORD sz;
4134
4135     static const WCHAR szInstaller[] = 
4136 {'I','n','s','t','a','l','l','e','r','\\',0};
4137     static const WCHAR szProductCode[] =
4138 {'P','r','o','d','u','c','t','C','o','d','e',0};
4139     static const WCHAR szFolder[] =
4140 {'W','i','n','d','o','w','s','F','o','l','d','e','r',0};
4141
4142     sz = 0x100;
4143     MSI_GetPropertyW(package,szProductCode,ProductCode,&sz);
4144     if (strlenW(ProductCode)==0)
4145         return ERROR_FUNCTION_FAILED;
4146
4147     sz = MAX_PATH;
4148     MSI_GetPropertyW(package,szFolder,SystemFolder,&sz);
4149     strcatW(SystemFolder,szInstaller); 
4150     strcatW(SystemFolder,ProductCode);
4151     create_full_pathW(SystemFolder);
4152
4153     strcpyW(FilePath,SystemFolder);
4154     strcatW(FilePath,cszbs);
4155     strcatW(FilePath,icon_name);
4156     return ERROR_SUCCESS;
4157 }
4158
4159 static UINT ACTION_CreateShortcuts(MSIPACKAGE *package)
4160 {
4161     UINT rc;
4162     MSIQUERY * view;
4163     MSIRECORD * row = 0;
4164     static const WCHAR Query[] = {
4165        'S','E','L','E','C','T',' ','*',' ','f','r','o','m',' ',
4166        'S','h','o','r','t','c','u','t',0};
4167     IShellLinkW *sl;
4168     IPersistFile *pf;
4169     HRESULT res;
4170
4171     if (!package)
4172         return ERROR_INVALID_HANDLE;
4173
4174     res = CoInitialize( NULL );
4175     if (FAILED (res))
4176     {
4177         ERR("CoInitialize failed\n");
4178         return ERROR_FUNCTION_FAILED;
4179     }
4180
4181     rc = MSI_DatabaseOpenViewW(package->db, Query, &view);
4182     if (rc != ERROR_SUCCESS)
4183         return ERROR_SUCCESS;
4184
4185     rc = MSI_ViewExecute(view, 0);
4186     if (rc != ERROR_SUCCESS)
4187     {
4188         MSI_ViewClose(view);
4189         msiobj_release(&view->hdr);
4190         return rc;
4191     }
4192
4193     while (1)
4194     {
4195         WCHAR target_file[MAX_PATH];
4196         WCHAR buffer[0x100];
4197         DWORD sz;
4198         DWORD index;
4199         static const WCHAR szlnk[]={'.','l','n','k',0};
4200
4201         rc = MSI_ViewFetch(view,&row);
4202         if (rc != ERROR_SUCCESS)
4203         {
4204             rc = ERROR_SUCCESS;
4205             break;
4206         }
4207         
4208         sz = 0x100;
4209         MSI_RecordGetStringW(row,4,buffer,&sz);
4210
4211         index = get_loaded_component(package,buffer);
4212
4213         if (index < 0)
4214         {
4215             msiobj_release(&row->hdr);
4216             continue;
4217         }
4218
4219         if (!package->components[index].Enabled ||
4220             !package->components[index].FeatureState)
4221         {
4222             TRACE("Skipping shortcut creation due to disabled component\n");
4223             msiobj_release(&row->hdr);
4224             continue;
4225         }
4226
4227         ui_actiondata(package,szCreateShortcuts,row);
4228
4229         res = CoCreateInstance( &CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER,
4230                               &IID_IShellLinkW, (LPVOID *) &sl );
4231
4232         if (FAILED(res))
4233         {
4234             ERR("Is IID_IShellLink\n");
4235             msiobj_release(&row->hdr);
4236             continue;
4237         }
4238
4239         res = IShellLinkW_QueryInterface( sl, &IID_IPersistFile,(LPVOID*) &pf );
4240         if( FAILED( res ) )
4241         {
4242             ERR("Is IID_IPersistFile\n");
4243             msiobj_release(&row->hdr);
4244             continue;
4245         }
4246
4247         sz = 0x100;
4248         MSI_RecordGetStringW(row,2,buffer,&sz);
4249         resolve_folder(package, buffer,target_file,FALSE,FALSE,NULL);
4250
4251         sz = 0x100;
4252         MSI_RecordGetStringW(row,3,buffer,&sz);
4253         reduce_to_longfilename(buffer);
4254         strcatW(target_file,buffer);
4255         if (!strchrW(target_file,'.'))
4256             strcatW(target_file,szlnk);
4257
4258         sz = 0x100;
4259         MSI_RecordGetStringW(row,5,buffer,&sz);
4260         if (strchrW(buffer,'['))
4261         {
4262             LPWSTR deformated;
4263             deformat_string(package,buffer,&deformated);
4264             IShellLinkW_SetPath(sl,deformated);
4265             HeapFree(GetProcessHeap(),0,deformated);
4266         }
4267         else
4268         {
4269             FIXME("UNHANDLED shortcut format, advertised shortcut\n");
4270             IPersistFile_Release( pf );
4271             IShellLinkW_Release( sl );
4272             msiobj_release(&row->hdr);
4273             continue;
4274         }
4275
4276         if (!MSI_RecordIsNull(row,6))
4277         {
4278             LPWSTR deformated;
4279             sz = 0x100;
4280             MSI_RecordGetStringW(row,6,buffer,&sz);
4281             deformat_string(package,buffer,&deformated);
4282             IShellLinkW_SetArguments(sl,deformated);
4283             HeapFree(GetProcessHeap(),0,deformated);
4284         }
4285
4286         if (!MSI_RecordIsNull(row,7))
4287         {
4288             LPWSTR deformated;
4289             deformated = load_dynamic_stringW(row,7);
4290             IShellLinkW_SetDescription(sl,deformated);
4291             HeapFree(GetProcessHeap(),0,deformated);
4292         }
4293
4294         if (!MSI_RecordIsNull(row,8))
4295             IShellLinkW_SetHotkey(sl,MSI_RecordGetInteger(row,8));
4296
4297         if (!MSI_RecordIsNull(row,9))
4298         {
4299             WCHAR Path[MAX_PATH];
4300             INT index; 
4301
4302             sz = 0x100;
4303             MSI_RecordGetStringW(row,9,buffer,&sz);
4304
4305             build_icon_path(package,buffer,Path);
4306             index = MSI_RecordGetInteger(row,10);
4307
4308             IShellLinkW_SetIconLocation(sl,Path,index);
4309         }
4310
4311         if (!MSI_RecordIsNull(row,11))
4312             IShellLinkW_SetShowCmd(sl,MSI_RecordGetInteger(row,11));
4313
4314         if (!MSI_RecordIsNull(row,12))
4315         {
4316             WCHAR Path[MAX_PATH];
4317
4318             sz = 0x100;
4319             MSI_RecordGetStringW(row,12,buffer,&sz);
4320             resolve_folder(package, buffer, Path, FALSE, FALSE, NULL);
4321             IShellLinkW_SetWorkingDirectory(sl,Path);
4322         }
4323
4324         TRACE("Writing shortcut to %s\n",debugstr_w(target_file));
4325         IPersistFile_Save(pf,target_file,FALSE);
4326         
4327         IPersistFile_Release( pf );
4328         IShellLinkW_Release( sl );
4329
4330         msiobj_release(&row->hdr);
4331     }
4332     MSI_ViewClose(view);
4333     msiobj_release(&view->hdr);
4334
4335
4336     CoUninitialize();
4337
4338     return rc;
4339 }
4340
4341
4342 /*
4343  * 99% of the work done here is only done for 
4344  * advertised installs. However this is where the
4345  * Icon table is processed and written out
4346  * so that is what I am going to do here.
4347  */
4348 static UINT ACTION_PublishProduct(MSIPACKAGE *package)
4349 {
4350     UINT rc;
4351     MSIQUERY * view;
4352     MSIRECORD * row = 0;
4353     static const WCHAR Query[]={
4354         'S','E','L','E','C','T',' ','*',' ',
4355         'f','r','o','m',' ','I','c','o','n',0};
4356     DWORD sz;
4357
4358     if (!package)
4359         return ERROR_INVALID_HANDLE;
4360
4361     rc = MSI_DatabaseOpenViewW(package->db, Query, &view);
4362     if (rc != ERROR_SUCCESS)
4363         return ERROR_SUCCESS;
4364
4365     rc = MSI_ViewExecute(view, 0);
4366     if (rc != ERROR_SUCCESS)
4367     {
4368         MSI_ViewClose(view);
4369         msiobj_release(&view->hdr);
4370         return rc;
4371     }
4372
4373     while (1)
4374     {
4375         HANDLE the_file;
4376         WCHAR FilePath[MAX_PATH];
4377         WCHAR FileName[MAX_PATH];
4378         CHAR buffer[1024];
4379
4380         rc = MSI_ViewFetch(view,&row);
4381         if (rc != ERROR_SUCCESS)
4382         {
4383             rc = ERROR_SUCCESS;
4384             break;
4385         }
4386     
4387         sz = MAX_PATH;
4388         MSI_RecordGetStringW(row,1,FileName,&sz);
4389         if (sz == 0)
4390         {
4391             ERR("Unable to get FileName\n");
4392             msiobj_release(&row->hdr);
4393             continue;
4394         }
4395
4396         build_icon_path(package,FileName,FilePath);
4397
4398         TRACE("Creating icon file at %s\n",debugstr_w(FilePath));
4399         
4400         the_file = CreateFileW(FilePath, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS,
4401                            FILE_ATTRIBUTE_NORMAL, NULL);
4402
4403         if (the_file == INVALID_HANDLE_VALUE)
4404         {
4405             ERR("Unable to create file %s\n",debugstr_w(FilePath));
4406             msiobj_release(&row->hdr);
4407             continue;
4408         }
4409
4410         do 
4411         {
4412             DWORD write;
4413             sz = 1024;
4414             rc = MSI_RecordReadStream(row,2,buffer,&sz);
4415             if (rc != ERROR_SUCCESS)
4416             {
4417                 ERR("Failed to get stream\n");
4418                 CloseHandle(the_file);  
4419                 DeleteFileW(FilePath);
4420                 break;
4421             }
4422             WriteFile(the_file,buffer,sz,&write,NULL);
4423         } while (sz == 1024);
4424
4425         CloseHandle(the_file);
4426         msiobj_release(&row->hdr);
4427     }
4428     MSI_ViewClose(view);
4429     msiobj_release(&view->hdr);
4430     return rc;
4431
4432 }
4433
4434 /* Msi functions that seem appropriate here */
4435 UINT WINAPI MsiDoActionA( MSIHANDLE hInstall, LPCSTR szAction )
4436 {
4437     LPWSTR szwAction;
4438     UINT rc;
4439
4440     TRACE(" exteral attempt at action %s\n",szAction);
4441
4442     if (!szAction)
4443         return ERROR_FUNCTION_FAILED;
4444     if (hInstall == 0)
4445         return ERROR_FUNCTION_FAILED;
4446
4447     szwAction = strdupAtoW(szAction);
4448
4449     if (!szwAction)
4450         return ERROR_FUNCTION_FAILED; 
4451
4452
4453     rc = MsiDoActionW(hInstall, szwAction);
4454     HeapFree(GetProcessHeap(),0,szwAction);
4455     return rc;
4456 }
4457
4458 UINT WINAPI MsiDoActionW( MSIHANDLE hInstall, LPCWSTR szAction )
4459 {
4460     MSIPACKAGE *package;
4461     UINT ret = ERROR_INVALID_HANDLE;
4462
4463     TRACE(" external attempt at action %s \n",debugstr_w(szAction));
4464
4465     package = msihandle2msiinfo(hInstall, MSIHANDLETYPE_PACKAGE);
4466     if( package )
4467     {
4468         ret = ACTION_PerformAction(package,szAction);
4469         msiobj_release( &package->hdr );
4470     }
4471     return ret;
4472 }
4473
4474 UINT WINAPI MsiGetTargetPathA( MSIHANDLE hInstall, LPCSTR szFolder, 
4475                                LPSTR szPathBuf, DWORD* pcchPathBuf) 
4476 {
4477     LPWSTR szwFolder;
4478     LPWSTR szwPathBuf;
4479     UINT rc;
4480
4481     TRACE("getting folder %s %p %li\n",szFolder,szPathBuf, *pcchPathBuf);
4482
4483     if (!szFolder)
4484         return ERROR_FUNCTION_FAILED;
4485     if (hInstall == 0)
4486         return ERROR_FUNCTION_FAILED;
4487
4488     szwFolder = strdupAtoW(szFolder);
4489
4490     if (!szwFolder)
4491         return ERROR_FUNCTION_FAILED; 
4492
4493     szwPathBuf = HeapAlloc( GetProcessHeap(), 0 , *pcchPathBuf * sizeof(WCHAR));
4494
4495     rc = MsiGetTargetPathW(hInstall, szwFolder, szwPathBuf,pcchPathBuf);
4496
4497     WideCharToMultiByte( CP_ACP, 0, szwPathBuf, *pcchPathBuf, szPathBuf,
4498                          *pcchPathBuf, NULL, NULL );
4499
4500     HeapFree(GetProcessHeap(),0,szwFolder);
4501     HeapFree(GetProcessHeap(),0,szwPathBuf);
4502
4503     return rc;
4504 }
4505
4506 UINT WINAPI MsiGetTargetPathW( MSIHANDLE hInstall, LPCWSTR szFolder, LPWSTR
4507                                 szPathBuf, DWORD* pcchPathBuf) 
4508 {
4509     WCHAR path[MAX_PATH];
4510     UINT rc;
4511     MSIPACKAGE *package;
4512
4513     TRACE("(%s %p %li)\n",debugstr_w(szFolder),szPathBuf,*pcchPathBuf);
4514
4515     package = msihandle2msiinfo(hInstall, MSIHANDLETYPE_PACKAGE);
4516     if( !package )
4517         return ERROR_INVALID_HANDLE;
4518     rc = resolve_folder(package, szFolder, path, FALSE, FALSE, NULL);
4519     msiobj_release( &package->hdr );
4520
4521     if (rc == ERROR_SUCCESS && strlenW(path) > *pcchPathBuf)
4522     {
4523         *pcchPathBuf = strlenW(path)+1;
4524         return ERROR_MORE_DATA;
4525     }
4526     else if (rc == ERROR_SUCCESS)
4527     {
4528         *pcchPathBuf = strlenW(path)+1;
4529         strcpyW(szPathBuf,path);
4530         TRACE("Returning Path %s\n",debugstr_w(path));
4531     }
4532     
4533     return rc;
4534 }
4535
4536
4537 UINT WINAPI MsiGetSourcePathA( MSIHANDLE hInstall, LPCSTR szFolder, 
4538                                LPSTR szPathBuf, DWORD* pcchPathBuf) 
4539 {
4540     LPWSTR szwFolder;
4541     LPWSTR szwPathBuf;
4542     UINT rc;
4543
4544     TRACE("getting source %s %p %li\n",szFolder,szPathBuf, *pcchPathBuf);
4545
4546     if (!szFolder)
4547         return ERROR_FUNCTION_FAILED;
4548     if (hInstall == 0)
4549         return ERROR_FUNCTION_FAILED;
4550
4551     szwFolder = strdupAtoW(szFolder);
4552     if (!szwFolder)
4553         return ERROR_FUNCTION_FAILED; 
4554
4555     szwPathBuf = HeapAlloc( GetProcessHeap(), 0 , *pcchPathBuf * sizeof(WCHAR));
4556
4557     rc = MsiGetSourcePathW(hInstall, szwFolder, szwPathBuf,pcchPathBuf);
4558
4559     WideCharToMultiByte( CP_ACP, 0, szwPathBuf, *pcchPathBuf, szPathBuf,
4560                          *pcchPathBuf, NULL, NULL );
4561
4562     HeapFree(GetProcessHeap(),0,szwFolder);
4563     HeapFree(GetProcessHeap(),0,szwPathBuf);
4564
4565     return rc;
4566 }
4567
4568 UINT WINAPI MsiGetSourcePathW( MSIHANDLE hInstall, LPCWSTR szFolder, LPWSTR
4569                                 szPathBuf, DWORD* pcchPathBuf) 
4570 {
4571     WCHAR path[MAX_PATH];
4572     UINT rc;
4573     MSIPACKAGE *package;
4574
4575     TRACE("(%s %p %li)\n",debugstr_w(szFolder),szPathBuf,*pcchPathBuf);
4576
4577     package = msihandle2msiinfo(hInstall, MSIHANDLETYPE_PACKAGE);
4578     if( !package )
4579         return ERROR_INVALID_HANDLE;
4580     rc = resolve_folder(package, szFolder, path, TRUE, FALSE, NULL);
4581     msiobj_release( &package->hdr );
4582
4583     if (rc == ERROR_SUCCESS && strlenW(path) > *pcchPathBuf)
4584     {
4585         *pcchPathBuf = strlenW(path)+1;
4586         return ERROR_MORE_DATA;
4587     }
4588     else if (rc == ERROR_SUCCESS)
4589     {
4590         *pcchPathBuf = strlenW(path)+1;
4591         strcpyW(szPathBuf,path);
4592         TRACE("Returning Path %s\n",debugstr_w(path));
4593     }
4594     
4595     return rc;
4596 }
4597
4598
4599 UINT WINAPI MsiSetTargetPathA(MSIHANDLE hInstall, LPCSTR szFolder, 
4600                              LPCSTR szFolderPath)
4601 {
4602     LPWSTR szwFolder;
4603     LPWSTR szwFolderPath;
4604     UINT rc;
4605
4606     if (!szFolder)
4607         return ERROR_FUNCTION_FAILED;
4608     if (hInstall == 0)
4609         return ERROR_FUNCTION_FAILED;
4610
4611     szwFolder = strdupAtoW(szFolder);
4612     if (!szwFolder)
4613         return ERROR_FUNCTION_FAILED; 
4614
4615     szwFolderPath = strdupAtoW(szFolderPath);
4616     if (!szwFolderPath)
4617     {
4618         HeapFree(GetProcessHeap(),0,szwFolder);
4619         return ERROR_FUNCTION_FAILED; 
4620     }
4621
4622     rc = MsiSetTargetPathW(hInstall, szwFolder, szwFolderPath);
4623
4624     HeapFree(GetProcessHeap(),0,szwFolder);
4625     HeapFree(GetProcessHeap(),0,szwFolderPath);
4626
4627     return rc;
4628 }
4629
4630 UINT MSI_SetTargetPathW(MSIPACKAGE *package, LPCWSTR szFolder, 
4631                              LPCWSTR szFolderPath)
4632 {
4633     DWORD i;
4634     WCHAR path[MAX_PATH];
4635     MSIFOLDER *folder;
4636
4637     TRACE("(%p %s %s)\n",package, debugstr_w(szFolder),debugstr_w(szFolderPath));
4638
4639     if (package==NULL)
4640         return ERROR_INVALID_HANDLE;
4641
4642     if (szFolderPath[0]==0)
4643         return ERROR_FUNCTION_FAILED;
4644
4645     if (GetFileAttributesW(szFolderPath) == INVALID_FILE_ATTRIBUTES)
4646         return ERROR_FUNCTION_FAILED;
4647
4648     resolve_folder(package,szFolder,path,FALSE,FALSE,&folder);
4649
4650     if (!folder)
4651         return ERROR_INVALID_PARAMETER;
4652
4653     strcpyW(folder->Property,szFolderPath);
4654
4655     for (i = 0; i < package->loaded_folders; i++)
4656         package->folders[i].ResolvedTarget[0]=0;
4657
4658     for (i = 0; i < package->loaded_folders; i++)
4659         resolve_folder(package, package->folders[i].Directory, path, FALSE,
4660                        TRUE, NULL);
4661
4662     return ERROR_SUCCESS;
4663 }
4664
4665 UINT WINAPI MsiSetTargetPathW(MSIHANDLE hInstall, LPCWSTR szFolder, 
4666                              LPCWSTR szFolderPath)
4667 {
4668     MSIPACKAGE *package;
4669     UINT ret;
4670
4671     TRACE("(%s %s)\n",debugstr_w(szFolder),debugstr_w(szFolderPath));
4672
4673     package = msihandle2msiinfo(hInstall, MSIHANDLETYPE_PACKAGE);
4674     ret = MSI_SetTargetPathW( package, szFolder, szFolderPath );
4675     msiobj_release( &package->hdr );
4676     return ret;
4677 }
4678
4679 BOOL WINAPI MsiGetMode(MSIHANDLE hInstall, DWORD iRunMode)
4680 {
4681     FIXME("STUB (%li)\n",iRunMode);
4682     return FALSE;
4683 }
4684
4685 /*
4686  * According to the docs, when this is called it immediately recalculates
4687  * all the component states as well
4688  */
4689 UINT WINAPI MsiSetFeatureStateA(MSIHANDLE hInstall, LPCSTR szFeature,
4690                                 INSTALLSTATE iState)
4691 {
4692     LPWSTR szwFeature = NULL;
4693     UINT rc;
4694
4695     szwFeature = strdupAtoW(szFeature);
4696
4697     if (!szwFeature)
4698         return ERROR_FUNCTION_FAILED;
4699    
4700     rc = MsiSetFeatureStateW(hInstall,szwFeature, iState); 
4701
4702     HeapFree(GetProcessHeap(),0,szwFeature);
4703
4704     return rc;
4705 }
4706
4707 UINT WINAPI MsiSetFeatureStateW(MSIHANDLE hInstall, LPCWSTR szFeature,
4708                                 INSTALLSTATE iState)
4709 {
4710     MSIPACKAGE* package;
4711     INT index;
4712
4713     TRACE(" %s to %i\n",debugstr_w(szFeature), iState);
4714
4715     package = msihandle2msiinfo(hInstall, MSIHANDLETYPE_PACKAGE);
4716     if (!package)
4717         return ERROR_INVALID_HANDLE;
4718
4719     index = get_loaded_feature(package,szFeature);
4720     if (index < 0)
4721         return ERROR_UNKNOWN_FEATURE;
4722
4723     package->features[index].State = iState;
4724
4725     return ERROR_SUCCESS;
4726 }
4727
4728 UINT WINAPI MsiGetFeatureStateA(MSIHANDLE hInstall, LPSTR szFeature,
4729                   INSTALLSTATE *piInstalled, INSTALLSTATE *piAction)
4730 {
4731     LPWSTR szwFeature = NULL;
4732     UINT rc;
4733     
4734     szwFeature = strdupAtoW(szFeature);
4735
4736     rc = MsiGetFeatureStateW(hInstall,szwFeature,piInstalled, piAction);
4737
4738     HeapFree( GetProcessHeap(), 0 , szwFeature);
4739
4740     return rc;
4741 }
4742
4743 UINT MSI_GetFeatureStateW(MSIPACKAGE *package, LPWSTR szFeature,
4744                   INSTALLSTATE *piInstalled, INSTALLSTATE *piAction)
4745 {
4746     INT index;
4747
4748     index = get_loaded_feature(package,szFeature);
4749     if (index < 0)
4750         return ERROR_UNKNOWN_FEATURE;
4751
4752     if (piInstalled)
4753         *piInstalled = package->features[index].State;
4754
4755     if (piAction)
4756     {
4757         if (package->features[index].Enabled)
4758             *piAction = INSTALLSTATE_LOCAL;
4759         else
4760             *piAction = INSTALLSTATE_UNKNOWN;
4761     }
4762
4763     return ERROR_SUCCESS;
4764 }
4765
4766 UINT WINAPI MsiGetFeatureStateW(MSIHANDLE hInstall, LPWSTR szFeature,
4767                   INSTALLSTATE *piInstalled, INSTALLSTATE *piAction)
4768 {
4769     MSIPACKAGE* package;
4770     UINT ret;
4771
4772     TRACE("%ld %s %p %p\n", hInstall, debugstr_w(szFeature), piInstalled,
4773 piAction);
4774
4775     package = msihandle2msiinfo(hInstall, MSIHANDLETYPE_PACKAGE);
4776     if (!package)
4777         return ERROR_INVALID_HANDLE;
4778     ret = MSI_GetFeatureStateW(package, szFeature, piInstalled, piAction);
4779     msiobj_release( &package->hdr );
4780     return ret;
4781 }
4782
4783 UINT WINAPI MsiGetComponentStateA(MSIHANDLE hInstall, LPSTR szComponent,
4784                   INSTALLSTATE *piInstalled, INSTALLSTATE *piAction)
4785 {
4786     LPWSTR szwComponent= NULL;
4787     UINT rc;
4788     
4789     szwComponent= strdupAtoW(szComponent);
4790
4791     rc = MsiGetComponentStateW(hInstall,szwComponent,piInstalled, piAction);
4792
4793     HeapFree( GetProcessHeap(), 0 , szwComponent);
4794
4795     return rc;
4796 }
4797
4798 UINT MSI_GetComponentStateW(MSIPACKAGE *package, LPWSTR szComponent,
4799                   INSTALLSTATE *piInstalled, INSTALLSTATE *piAction)
4800 {
4801     INT index;
4802
4803     TRACE("%p %s %p %p\n", package, debugstr_w(szComponent), piInstalled,
4804 piAction);
4805
4806     index = get_loaded_component(package,szComponent);
4807     if (index < 0)
4808         return ERROR_UNKNOWN_COMPONENT;
4809
4810     if (piInstalled)
4811         *piInstalled = package->components[index].State;
4812
4813     if (piAction)
4814     {
4815         if (package->components[index].Enabled &&
4816             package->components[index].FeatureState)
4817             *piAction = INSTALLSTATE_LOCAL;
4818         else
4819             *piAction = INSTALLSTATE_UNKNOWN;
4820     }
4821
4822     return ERROR_SUCCESS;
4823 }
4824
4825 UINT WINAPI MsiGetComponentStateW(MSIHANDLE hInstall, LPWSTR szComponent,
4826                   INSTALLSTATE *piInstalled, INSTALLSTATE *piAction)
4827 {
4828     MSIPACKAGE* package;
4829     UINT ret;
4830
4831     TRACE("%ld %s %p %p\n", hInstall, debugstr_w(szComponent),
4832            piInstalled, piAction);
4833
4834     package = msihandle2msiinfo(hInstall, MSIHANDLETYPE_PACKAGE);
4835     if (!package)
4836         return ERROR_INVALID_HANDLE;
4837     ret = MSI_GetComponentStateW( package, szComponent, piInstalled, piAction);
4838     msiobj_release( &package->hdr );
4839     return ret;
4840 }
4841
4842 #if 0
4843 static UINT ACTION_Template(MSIPACKAGE *package)
4844 {
4845     UINT rc;
4846     MSIQUERY * view;
4847     MSIRECORD * row = 0;
4848     static const WCHAR ExecSeqQuery[] = {0};
4849
4850     rc = MsiDatabaseOpenViewW(package->db, ExecSeqQuery, &view);
4851     if (rc != ERROR_SUCCESS)
4852         return rc;
4853
4854     rc = MsiViewExecute(view, 0);
4855     if (rc != ERROR_SUCCESS)
4856     {
4857         MsiViewClose(view);
4858         msiobj_release(&view->hdr);
4859         return rc;
4860     }
4861
4862     while (1)
4863     {
4864         rc = MsiViewFetch(view,&row);
4865         if (rc != ERROR_SUCCESS)
4866         {
4867             rc = ERROR_SUCCESS;
4868             break;
4869         }
4870
4871         msiobj_release(&row->hdr);
4872     }
4873     MsiViewClose(view);
4874     msiobj_release(&view->hdr);
4875     return rc;
4876 }
4877 #endif