Fix situations where TARGETDIR is set to a non \ terminated
[wine] / dlls / msi / helpers.c
1 /*
2  * Implementation of the Microsoft Installer (msi.dll)
3  *
4  * Copyright 2005 Aric Stewart for CodeWeavers
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
20
21 /*
22  * Here are helper functions formally in action.c that are used by a variaty of
23  * actions and functions.
24  */
25
26 #include <stdarg.h>
27
28 #include "windef.h"
29 #include "winbase.h"
30 #include "winerror.h"
31 #include "wine/debug.h"
32 #include "msipriv.h"
33 #include "winuser.h"
34 #include "wine/unicode.h"
35 #include "action.h"
36
37 WINE_DEFAULT_DEBUG_CHANNEL(msi);
38
39 static const WCHAR cszTargetDir[] = {'T','A','R','G','E','T','D','I','R',0};
40 static const WCHAR cszDatabase[]={'D','A','T','A','B','A','S','E',0};
41
42 const WCHAR cszSourceDir[] = {'S','o','u','r','c','e','D','i','r',0};
43 const WCHAR szProductCode[]= {'P','r','o','d','u','c','t','C','o','d','e',0};
44 const WCHAR cszRootDrive[] = {'R','O','O','T','D','R','I','V','E',0};
45 const WCHAR cszbs[]={'\\',0};
46
47 DWORD build_version_dword(LPCWSTR version_string)
48 {
49     SHORT major,minor;
50     WORD build;
51     DWORD rc = 0x00000000;
52     LPCWSTR ptr1;
53
54     ptr1 = version_string;
55
56     if (!ptr1)
57         return rc;
58     else
59         major = atoiW(ptr1);
60
61
62     if(ptr1)
63         ptr1 = strchrW(ptr1,'.');
64     if (ptr1)
65     {
66         ptr1++;
67         minor = atoiW(ptr1);
68     }
69     else
70         minor = 0;
71
72     if (ptr1)
73         ptr1 = strchrW(ptr1,'.');
74
75     if (ptr1)
76     {
77         ptr1++;
78         build = atoiW(ptr1);
79     }
80     else
81         build = 0;
82
83     rc = MAKELONG(build,MAKEWORD(minor,major));
84     TRACE("%s -> 0x%lx\n",debugstr_w(version_string),rc);
85     return rc;
86 }
87
88 UINT build_icon_path(MSIPACKAGE *package, LPCWSTR icon_name, 
89                             LPWSTR *FilePath)
90 {
91     LPWSTR ProductCode;
92     LPWSTR SystemFolder;
93     LPWSTR dest;
94     UINT rc;
95
96     static const WCHAR szInstaller[] = 
97         {'M','i','c','r','o','s','o','f','t','\\',
98          'I','n','s','t','a','l','l','e','r','\\',0};
99     static const WCHAR szFolder[] =
100         {'A','p','p','D','a','t','a','F','o','l','d','e','r',0};
101
102     ProductCode = load_dynamic_property(package,szProductCode,&rc);
103     if (!ProductCode)
104         return rc;
105
106     SystemFolder = load_dynamic_property(package,szFolder,NULL);
107
108     dest = build_directory_name(3, SystemFolder, szInstaller, ProductCode);
109
110     create_full_pathW(dest);
111
112     *FilePath = build_directory_name(2, dest, icon_name);
113
114     HeapFree(GetProcessHeap(),0,SystemFolder);
115     HeapFree(GetProcessHeap(),0,ProductCode);
116     HeapFree(GetProcessHeap(),0,dest);
117     return ERROR_SUCCESS;
118 }
119
120 WCHAR *load_dynamic_stringW(MSIRECORD *row, INT index)
121 {
122     UINT rc;
123     DWORD sz;
124     LPWSTR ret;
125    
126     sz = 0; 
127     if (MSI_RecordIsNull(row,index))
128         return NULL;
129
130     rc = MSI_RecordGetStringW(row,index,NULL,&sz);
131
132     /* having an empty string is different than NULL */
133     if (sz == 0)
134     {
135         ret = HeapAlloc(GetProcessHeap(),0,sizeof(WCHAR));
136         ret[0] = 0;
137         return ret;
138     }
139
140     sz ++;
141     ret = HeapAlloc(GetProcessHeap(),0,sz * sizeof (WCHAR));
142     rc = MSI_RecordGetStringW(row,index,ret,&sz);
143     if (rc!=ERROR_SUCCESS)
144     {
145         ERR("Unable to load dynamic string\n");
146         HeapFree(GetProcessHeap(), 0, ret);
147         ret = NULL;
148     }
149     return ret;
150 }
151
152 LPWSTR load_dynamic_property(MSIPACKAGE *package, LPCWSTR prop, UINT* rc)
153 {
154     DWORD sz = 0;
155     LPWSTR str;
156     UINT r;
157
158     r = MSI_GetPropertyW(package, prop, NULL, &sz);
159     if (r != ERROR_SUCCESS && r != ERROR_MORE_DATA)
160     {
161         if (rc)
162             *rc = r;
163         return NULL;
164     }
165     sz++;
166     str = HeapAlloc(GetProcessHeap(),0,sz*sizeof(WCHAR));
167     r = MSI_GetPropertyW(package, prop, str, &sz);
168     if (r != ERROR_SUCCESS)
169     {
170         HeapFree(GetProcessHeap(),0,str);
171         str = NULL;
172     }
173     if (rc)
174         *rc = r;
175     return str;
176 }
177
178 int get_loaded_component(MSIPACKAGE* package, LPCWSTR Component )
179 {
180     int rc = -1;
181     DWORD i;
182
183     for (i = 0; i < package->loaded_components; i++)
184     {
185         if (strcmpW(Component,package->components[i].Component)==0)
186         {
187             rc = i;
188             break;
189         }
190     }
191     return rc;
192 }
193
194 int get_loaded_feature(MSIPACKAGE* package, LPCWSTR Feature )
195 {
196     int rc = -1;
197     DWORD i;
198
199     for (i = 0; i < package->loaded_features; i++)
200     {
201         if (strcmpW(Feature,package->features[i].Feature)==0)
202         {
203             rc = i;
204             break;
205         }
206     }
207     return rc;
208 }
209
210 int get_loaded_file(MSIPACKAGE* package, LPCWSTR file)
211 {
212     int rc = -1;
213     DWORD i;
214
215     for (i = 0; i < package->loaded_files; i++)
216     {
217         if (strcmpW(file,package->files[i].File)==0)
218         {
219             rc = i;
220             break;
221         }
222     }
223     return rc;
224 }
225
226 int track_tempfile(MSIPACKAGE *package, LPCWSTR name, LPCWSTR path)
227 {
228     DWORD i;
229     DWORD index;
230
231     if (!package)
232         return -2;
233
234     for (i=0; i < package->loaded_files; i++)
235         if (strcmpW(package->files[i].File,name)==0)
236             return -1;
237
238     index = package->loaded_files;
239     package->loaded_files++;
240     if (package->loaded_files== 1)
241         package->files = HeapAlloc(GetProcessHeap(),0,sizeof(MSIFILE));
242     else
243         package->files = HeapReAlloc(GetProcessHeap(),0,
244             package->files , package->loaded_files * sizeof(MSIFILE));
245
246     memset(&package->files[index],0,sizeof(MSIFILE));
247
248     package->files[index].File = strdupW(name);
249     package->files[index].TargetPath = strdupW(path);
250     package->files[index].Temporary = TRUE;
251
252     TRACE("Tracking tempfile (%s)\n",debugstr_w(package->files[index].File));  
253
254     return 0;
255 }
256
257 LPWSTR resolve_folder(MSIPACKAGE *package, LPCWSTR name, BOOL source, 
258                       BOOL set_prop, MSIFOLDER **folder)
259 {
260     DWORD i;
261     LPWSTR p, path = NULL;
262
263     TRACE("Working to resolve %s\n",debugstr_w(name));
264
265     if (!name)
266         return NULL;
267
268     /* special resolving for Target and Source root dir */
269     if (strcmpW(name,cszTargetDir)==0 || strcmpW(name,cszSourceDir)==0)
270     {
271         if (!source)
272         {
273             LPWSTR check_path;
274             check_path = load_dynamic_property(package,cszTargetDir,NULL);
275             if (!check_path)
276             {
277                 check_path = load_dynamic_property(package,cszRootDrive,NULL);
278                 if (set_prop)
279                     MSI_SetPropertyW(package,cszTargetDir,check_path);
280             }
281
282             /* correct misbuilt target dir */
283             path = build_directory_name(2, check_path, NULL);
284             if (strcmpiW(path,check_path)!=0)
285                 MSI_SetPropertyW(package,cszTargetDir,path);
286
287             if (folder)
288             {
289                 for (i = 0; i < package->loaded_folders; i++)
290                 {
291                     if (strcmpW(package->folders[i].Directory,name)==0)
292                         break;
293                 }
294                 *folder = &(package->folders[i]);
295             }
296             return path;
297         }
298         else
299         {
300             path = load_dynamic_property(package,cszSourceDir,NULL);
301             if (!path)
302             {
303                 path = load_dynamic_property(package,cszDatabase,NULL);
304                 if (path)
305                 {
306                     p = strrchrW(path,'\\');
307                     if (p)
308                         *(p+1) = 0;
309                 }
310             }
311             if (folder)
312             {
313                 for (i = 0; i < package->loaded_folders; i++)
314                 {
315                     if (strcmpW(package->folders[i].Directory,name)==0)
316                         break;
317                 }
318                 *folder = &(package->folders[i]);
319             }
320             return path;
321         }
322     }
323
324     for (i = 0; i < package->loaded_folders; i++)
325     {
326         if (strcmpW(package->folders[i].Directory,name)==0)
327             break;
328     }
329
330     if (i >= package->loaded_folders)
331         return NULL;
332
333     if (folder)
334         *folder = &(package->folders[i]);
335
336     if (!source && package->folders[i].ResolvedTarget)
337     {
338         path = strdupW(package->folders[i].ResolvedTarget);
339         TRACE("   already resolved to %s\n",debugstr_w(path));
340         return path;
341     }
342     else if (source && package->folders[i].ResolvedSource)
343     {
344         path = strdupW(package->folders[i].ResolvedSource);
345         TRACE("   (source)already resolved to %s\n",debugstr_w(path));
346         return path;
347     }
348     else if (!source && package->folders[i].Property)
349     {
350         path = build_directory_name(2, package->folders[i].Property, NULL);
351                     
352         TRACE("   internally set to %s\n",debugstr_w(path));
353         if (set_prop)
354             MSI_SetPropertyW(package,name,path);
355         return path;
356     }
357
358     if (package->folders[i].ParentIndex >= 0)
359     {
360         LPWSTR parent = package->folders[package->folders[i].ParentIndex].Directory;
361
362         TRACE(" ! Parent is %s\n", debugstr_w(parent));
363
364         p = resolve_folder(package, parent, source, set_prop, NULL);
365         if (!source)
366         {
367             TRACE("   TargetDefault = %s\n",
368                     debugstr_w(package->folders[i].TargetDefault));
369
370             path = build_directory_name(3, p, 
371                             package->folders[i].TargetDefault, NULL);
372             package->folders[i].ResolvedTarget = strdupW(path);
373             TRACE("   resolved into %s\n",debugstr_w(path));
374             if (set_prop)
375                 MSI_SetPropertyW(package,name,path);
376         }
377         else 
378         {
379             if (package->folders[i].SourceDefault && 
380                 package->folders[i].SourceDefault[0]!='.')
381                 path = build_directory_name(3, p, package->folders[i].SourceDefault, NULL);
382             else
383                 path = strdupW(p);
384             TRACE("   (source)resolved into %s\n",debugstr_w(path));
385             package->folders[i].ResolvedSource = strdupW(path);
386         }
387         HeapFree(GetProcessHeap(),0,p);
388     }
389     return path;
390 }
391
392 /* wrapper to resist a need for a full rewrite right now */
393 DWORD deformat_string(MSIPACKAGE *package, LPCWSTR ptr, WCHAR** data )
394 {
395     if (ptr)
396     {
397         MSIRECORD *rec = MSI_CreateRecord(1);
398         DWORD size = 0;
399
400         MSI_RecordSetStringW(rec,0,ptr);
401         MSI_FormatRecordW(package,rec,NULL,&size);
402         if (size >= 0)
403         {
404             size++;
405             *data = HeapAlloc(GetProcessHeap(),0,size*sizeof(WCHAR));
406             if (size > 1)
407                 MSI_FormatRecordW(package,rec,*data,&size);
408             else
409                 *data[0] = 0;
410             msiobj_release( &rec->hdr );
411             return sizeof(WCHAR)*size;
412         }
413         msiobj_release( &rec->hdr );
414     }
415
416     *data = NULL;
417     return 0;
418 }
419
420 UINT schedule_action(MSIPACKAGE *package, UINT script, LPCWSTR action)
421 {
422     UINT count;
423     LPWSTR *newbuf = NULL;
424     if (script >= TOTAL_SCRIPTS)
425     {
426         FIXME("Unknown script requested %i\n",script);
427         return ERROR_FUNCTION_FAILED;
428     }
429     TRACE("Scheduling Action %s in script %i\n",debugstr_w(action), script);
430     
431     count = package->script->ActionCount[script];
432     package->script->ActionCount[script]++;
433     if (count != 0)
434         newbuf = HeapReAlloc(GetProcessHeap(),0,
435                         package->script->Actions[script],
436                         package->script->ActionCount[script]* sizeof(LPWSTR));
437     else
438         newbuf = HeapAlloc(GetProcessHeap(),0, sizeof(LPWSTR));
439
440     newbuf[count] = strdupW(action);
441     package->script->Actions[script] = newbuf;
442
443    return ERROR_SUCCESS;
444 }
445
446 static void remove_tracked_tempfiles(MSIPACKAGE* package)
447 {
448     DWORD i;
449
450     if (!package)
451         return;
452
453     for (i = 0; i < package->loaded_files; i++)
454     {
455         if (package->files[i].Temporary)
456         {
457             TRACE("Cleaning up %s\n",debugstr_w(package->files[i].TargetPath));
458             DeleteFileW(package->files[i].TargetPath);
459         }
460
461     }
462 }
463
464 /* Called when the package is being closed */
465 void ACTION_free_package_structures( MSIPACKAGE* package)
466 {
467     INT i;
468     
469     TRACE("Freeing package action data\n");
470
471     remove_tracked_tempfiles(package);
472
473     /* No dynamic buffers in features */
474     if (package->features && package->loaded_features > 0)
475         HeapFree(GetProcessHeap(),0,package->features);
476
477     for (i = 0; i < package->loaded_folders; i++)
478     {
479         HeapFree(GetProcessHeap(),0,package->folders[i].Directory);
480         HeapFree(GetProcessHeap(),0,package->folders[i].TargetDefault);
481         HeapFree(GetProcessHeap(),0,package->folders[i].SourceDefault);
482         HeapFree(GetProcessHeap(),0,package->folders[i].ResolvedTarget);
483         HeapFree(GetProcessHeap(),0,package->folders[i].ResolvedSource);
484         HeapFree(GetProcessHeap(),0,package->folders[i].Property);
485     }
486     if (package->folders && package->loaded_folders > 0)
487         HeapFree(GetProcessHeap(),0,package->folders);
488
489     for (i = 0; i < package->loaded_components; i++)
490         HeapFree(GetProcessHeap(),0,package->components[i].FullKeypath);
491
492     if (package->components && package->loaded_components > 0)
493         HeapFree(GetProcessHeap(),0,package->components);
494
495     for (i = 0; i < package->loaded_files; i++)
496     {
497         HeapFree(GetProcessHeap(),0,package->files[i].File);
498         HeapFree(GetProcessHeap(),0,package->files[i].FileName);
499         HeapFree(GetProcessHeap(),0,package->files[i].ShortName);
500         HeapFree(GetProcessHeap(),0,package->files[i].Version);
501         HeapFree(GetProcessHeap(),0,package->files[i].Language);
502         HeapFree(GetProcessHeap(),0,package->files[i].SourcePath);
503         HeapFree(GetProcessHeap(),0,package->files[i].TargetPath);
504     }
505
506     if (package->files && package->loaded_files > 0)
507         HeapFree(GetProcessHeap(),0,package->files);
508
509     /* clean up extension, progid, class and verb structures */
510     for (i = 0; i < package->loaded_classes; i++)
511     {
512         HeapFree(GetProcessHeap(),0,package->classes[i].Description);
513         HeapFree(GetProcessHeap(),0,package->classes[i].FileTypeMask);
514         HeapFree(GetProcessHeap(),0,package->classes[i].IconPath);
515         HeapFree(GetProcessHeap(),0,package->classes[i].DefInprocHandler);
516         HeapFree(GetProcessHeap(),0,package->classes[i].DefInprocHandler32);
517         HeapFree(GetProcessHeap(),0,package->classes[i].Argument);
518         HeapFree(GetProcessHeap(),0,package->classes[i].ProgIDText);
519     }
520
521     if (package->classes && package->loaded_classes > 0)
522         HeapFree(GetProcessHeap(),0,package->classes);
523
524     for (i = 0; i < package->loaded_extensions; i++)
525     {
526         HeapFree(GetProcessHeap(),0,package->extensions[i].ProgIDText);
527     }
528
529     if (package->extensions && package->loaded_extensions > 0)
530         HeapFree(GetProcessHeap(),0,package->extensions);
531
532     for (i = 0; i < package->loaded_progids; i++)
533     {
534         HeapFree(GetProcessHeap(),0,package->progids[i].ProgID);
535         HeapFree(GetProcessHeap(),0,package->progids[i].Description);
536         HeapFree(GetProcessHeap(),0,package->progids[i].IconPath);
537     }
538
539     if (package->progids && package->loaded_progids > 0)
540         HeapFree(GetProcessHeap(),0,package->progids);
541
542     for (i = 0; i < package->loaded_verbs; i++)
543     {
544         HeapFree(GetProcessHeap(),0,package->verbs[i].Verb);
545         HeapFree(GetProcessHeap(),0,package->verbs[i].Command);
546         HeapFree(GetProcessHeap(),0,package->verbs[i].Argument);
547     }
548
549     if (package->verbs && package->loaded_verbs > 0)
550         HeapFree(GetProcessHeap(),0,package->verbs);
551
552     for (i = 0; i < package->loaded_mimes; i++)
553         HeapFree(GetProcessHeap(),0,package->mimes[i].ContentType);
554
555     if (package->mimes && package->loaded_mimes > 0)
556         HeapFree(GetProcessHeap(),0,package->mimes);
557
558     for (i = 0; i < package->loaded_appids; i++)
559     {
560         HeapFree(GetProcessHeap(),0,package->appids[i].RemoteServerName);
561         HeapFree(GetProcessHeap(),0,package->appids[i].LocalServer);
562         HeapFree(GetProcessHeap(),0,package->appids[i].ServiceParameters);
563         HeapFree(GetProcessHeap(),0,package->appids[i].DllSurrogate);
564     }
565
566     if (package->appids && package->loaded_appids > 0)
567         HeapFree(GetProcessHeap(),0,package->appids);
568
569     if (package->script)
570     {
571         for (i = 0; i < TOTAL_SCRIPTS; i++)
572         {
573             int j;
574             for (j = 0; j < package->script->ActionCount[i]; j++)
575                 HeapFree(GetProcessHeap(),0,package->script->Actions[i][j]);
576         
577             HeapFree(GetProcessHeap(),0,package->script->Actions[i]);
578         }
579         HeapFree(GetProcessHeap(),0,package->script);
580     }
581
582     HeapFree(GetProcessHeap(),0,package->PackagePath);
583
584     /* cleanup control event subscriptions */
585     ControlEvent_CleanupSubscriptions(package);
586 }
587
588 /*
589  *  build_directory_name()
590  *
591  *  This function is to save messing round with directory names
592  *  It handles adding backslashes between path segments, 
593  *   and can add \ at the end of the directory name if told to.
594  *
595  *  It takes a variable number of arguments.
596  *  It always allocates a new string for the result, so make sure
597  *   to free the return value when finished with it.
598  *
599  *  The first arg is the number of path segments that follow.
600  *  The arguments following count are a list of path segments.
601  *  A path segment may be NULL.
602  *
603  *  Path segments will be added with a \ separating them.
604  *  A \ will not be added after the last segment, however if the
605  *    last segment is NULL, then the last character will be a \
606  * 
607  */
608 LPWSTR build_directory_name(DWORD count, ...)
609 {
610     DWORD sz = 1, i;
611     LPWSTR dir;
612     va_list va;
613
614     va_start(va,count);
615     for(i=0; i<count; i++)
616     {
617         LPCWSTR str = va_arg(va,LPCWSTR);
618         if (str)
619             sz += strlenW(str) + 1;
620     }
621     va_end(va);
622
623     dir = HeapAlloc(GetProcessHeap(), 0, sz*sizeof(WCHAR));
624     dir[0]=0;
625
626     va_start(va,count);
627     for(i=0; i<count; i++)
628     {
629         LPCWSTR str = va_arg(va,LPCWSTR);
630         if (!str)
631             continue;
632         strcatW(dir, str);
633         if( ((i+1)!=count) && dir[strlenW(dir)-1]!='\\')
634             strcatW(dir, cszbs);
635     }
636     return dir;
637 }
638
639 /***********************************************************************
640  *            create_full_pathW
641  *
642  * Recursively create all directories in the path.
643  *
644  * shamelessly stolen from setupapi/queue.c
645  */
646 BOOL create_full_pathW(const WCHAR *path)
647 {
648     BOOL ret = TRUE;
649     int len;
650     WCHAR *new_path;
651
652     new_path = HeapAlloc(GetProcessHeap(), 0, (strlenW(path) + 1) *
653                                               sizeof(WCHAR));
654
655     strcpyW(new_path, path);
656
657     while((len = strlenW(new_path)) && new_path[len - 1] == '\\')
658     new_path[len - 1] = 0;
659
660     while(!CreateDirectoryW(new_path, NULL))
661     {
662         WCHAR *slash;
663         DWORD last_error = GetLastError();
664         if(last_error == ERROR_ALREADY_EXISTS)
665             break;
666
667         if(last_error != ERROR_PATH_NOT_FOUND)
668         {
669             ret = FALSE;
670             break;
671         }
672
673         if(!(slash = strrchrW(new_path, '\\')))
674         {
675             ret = FALSE;
676             break;
677         }
678
679         len = slash - new_path;
680         new_path[len] = 0;
681         if(!create_full_pathW(new_path))
682         {
683             ret = FALSE;
684             break;
685         }
686         new_path[len] = '\\';
687     }
688
689     HeapFree(GetProcessHeap(), 0, new_path);
690     return ret;
691 }
692
693 void ui_progress(MSIPACKAGE *package, int a, int b, int c, int d )
694 {
695     MSIRECORD * row;
696
697     row = MSI_CreateRecord(4);
698     MSI_RecordSetInteger(row,1,a);
699     MSI_RecordSetInteger(row,2,b);
700     MSI_RecordSetInteger(row,3,c);
701     MSI_RecordSetInteger(row,4,d);
702     MSI_ProcessMessage(package, INSTALLMESSAGE_PROGRESS, row);
703     msiobj_release(&row->hdr);
704
705     msi_dialog_check_messages(NULL);
706 }
707
708 void ui_actiondata(MSIPACKAGE *package, LPCWSTR action, MSIRECORD * record)
709 {
710     static const WCHAR Query_t[] = 
711         {'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
712          '`','A','c','t','i','o', 'n','T','e','x','t','`',' ',
713          'W','H','E','R','E',' ', '`','A','c','t','i','o','n','`',' ','=', 
714          ' ','\'','%','s','\'',0};
715     WCHAR message[1024];
716     MSIRECORD * row = 0;
717     DWORD size;
718     static const WCHAR szActionData[] = 
719         {'A','c','t','i','o','n','D','a','t','a',0};
720
721     if (!package->LastAction || strcmpW(package->LastAction,action))
722     {
723         row = MSI_QueryGetRecord(package->db, Query_t, action);
724         if (!row)
725             return;
726
727         if (MSI_RecordIsNull(row,3))
728         {
729             msiobj_release(&row->hdr);
730             return;
731         }
732
733         /* update the cached actionformat */
734         HeapFree(GetProcessHeap(),0,package->ActionFormat);
735         package->ActionFormat = load_dynamic_stringW(row,3);
736
737         HeapFree(GetProcessHeap(),0,package->LastAction);
738         package->LastAction = strdupW(action);
739
740         msiobj_release(&row->hdr);
741     }
742
743     MSI_RecordSetStringW(record,0,package->ActionFormat);
744     size = 1024;
745     MSI_FormatRecordW(package,record,message,&size);
746
747     row = MSI_CreateRecord(1);
748     MSI_RecordSetStringW(row,1,message);
749  
750     MSI_ProcessMessage(package, INSTALLMESSAGE_ACTIONDATA, row);
751
752     ControlEvent_FireSubscribedEvent(package,szActionData, row);
753
754     msiobj_release(&row->hdr);
755 }
756
757 BOOL ACTION_VerifyComponentForAction(MSIPACKAGE* package, INT index, 
758                                             INSTALLSTATE check )
759 {
760     if (package->components[index].Installed == check)
761         return FALSE;
762
763     if (package->components[index].ActionRequest == check)
764         return TRUE;
765     else
766         return FALSE;
767 }
768
769 BOOL ACTION_VerifyFeatureForAction(MSIPACKAGE* package, INT index, 
770                                             INSTALLSTATE check )
771 {
772     if (package->features[index].Installed == check)
773         return FALSE;
774
775     if (package->features[index].ActionRequest == check)
776         return TRUE;
777     else
778         return FALSE;
779 }
780
781 void reduce_to_longfilename(WCHAR* filename)
782 {
783     LPWSTR p = strchrW(filename,'|');
784     if (p)
785         memmove(filename, p+1, (strlenW(p+1)+1)*sizeof(WCHAR));
786 }
787
788 void reduce_to_shortfilename(WCHAR* filename)
789 {
790     LPWSTR p = strchrW(filename,'|');
791     if (p)
792         *p = 0;
793 }
794
795 LPWSTR create_component_advertise_string(MSIPACKAGE* package, 
796                 MSICOMPONENT* component, LPCWSTR feature)
797 {
798     LPWSTR productid=NULL;
799     GUID clsid;
800     WCHAR productid_85[21];
801     WCHAR component_85[21];
802     /*
803      * I have a fair bit of confusion as to when a < is used and when a > is
804      * used. I do not think i have it right...
805      *
806      * Ok it appears that the > is used if there is a guid for the compoenent
807      * and the < is used if not.
808      */
809     static WCHAR fmt1[] = {'%','s','%','s','<',0,0};
810     static WCHAR fmt2[] = {'%','s','%','s','>','%','s',0,0};
811     LPWSTR output = NULL;
812     DWORD sz = 0;
813
814     memset(productid_85,0,sizeof(productid_85));
815     memset(component_85,0,sizeof(component_85));
816
817     productid = load_dynamic_property(package,szProductCode,NULL);
818     CLSIDFromString(productid, &clsid);
819     
820     encode_base85_guid(&clsid,productid_85);
821
822     CLSIDFromString(component->ComponentId, &clsid);
823     encode_base85_guid(&clsid,component_85);
824
825     TRACE("Doing something with this... %s %s %s\n", 
826             debugstr_w(productid_85), debugstr_w(feature),
827             debugstr_w(component_85));
828  
829     sz = lstrlenW(productid_85) + lstrlenW(feature);
830     if (component)
831         sz += lstrlenW(component_85);
832
833     sz+=3;
834     sz *= sizeof(WCHAR);
835            
836     output = HeapAlloc(GetProcessHeap(),0,sz);
837     memset(output,0,sz);
838
839     if (component)
840         sprintfW(output,fmt2,productid_85,feature,component_85);
841     else
842         sprintfW(output,fmt1,productid_85,feature);
843
844     HeapFree(GetProcessHeap(),0,productid);
845     
846     return output;
847 }
848
849 /* update compoennt state based on a feature change */
850 void ACTION_UpdateComponentStates(MSIPACKAGE *package, LPCWSTR szFeature)
851 {
852     int i;
853     INSTALLSTATE newstate;
854     MSIFEATURE *feature;
855
856     i = get_loaded_feature(package,szFeature);
857     if (i < 0)
858         return;
859
860     feature = &package->features[i];
861     newstate = feature->ActionRequest;
862
863     for( i = 0; i < feature->ComponentCount; i++)
864     {
865         MSICOMPONENT* component = &package->components[feature->Components[i]];
866
867         TRACE("MODIFYING(%i): Component %s (Installed %i, Action %i, Request %i)\n",
868             newstate, debugstr_w(component->Component), component->Installed, 
869             component->Action, component->ActionRequest);
870         
871         if (!component->Enabled)
872             continue;
873         else
874         {
875             if (newstate == INSTALLSTATE_LOCAL)
876             {
877                 component->ActionRequest = INSTALLSTATE_LOCAL;
878                 component->Action = INSTALLSTATE_LOCAL;
879             }
880             else 
881             {
882                 int j,k;
883
884                 component->ActionRequest = newstate;
885                 component->Action = newstate;
886
887                 /*if any other feature wants is local we need to set it local*/
888                 for (j = 0; 
889                      j < package->loaded_features &&
890                      component->ActionRequest != INSTALLSTATE_LOCAL; 
891                      j++)
892                 {
893                     for (k = 0; k < package->features[j].ComponentCount; k++)
894                         if ( package->features[j].Components[k] ==
895                              feature->Components[i] )
896                         {
897                             if (package->features[j].ActionRequest == 
898                                 INSTALLSTATE_LOCAL)
899                             {
900                                 TRACE("Saved by %s\n", debugstr_w(package->features[j].Feature));
901                                 component->ActionRequest = INSTALLSTATE_LOCAL;
902                                 component->Action = INSTALLSTATE_LOCAL;
903                             }
904                             break;
905                         }
906                 }
907             }
908         }
909         TRACE("Result (%i): Component %s (Installed %i, Action %i, Request %i)\n",
910             newstate, debugstr_w(component->Component), component->Installed, 
911             component->Action, component->ActionRequest);
912     } 
913 }