crypt32: Implement CertFindCRLInStore for find type CRL_FIND_ISSUED_FOR.
[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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19  */
20
21 /*
22  * Here are helper functions formally in action.c that are used by a variety of
23  * actions and functions.
24  */
25
26 #include <stdarg.h>
27
28 #include "windef.h"
29 #include "wine/debug.h"
30 #include "msipriv.h"
31 #include "winuser.h"
32 #include "wine/unicode.h"
33 #include "msidefs.h"
34
35 WINE_DEFAULT_DEBUG_CHANNEL(msi);
36
37 static const WCHAR cszTargetDir[] = {'T','A','R','G','E','T','D','I','R',0};
38 static const WCHAR cszDatabase[]={'D','A','T','A','B','A','S','E',0};
39
40 LPWSTR build_icon_path(MSIPACKAGE *package, LPCWSTR icon_name )
41 {
42     LPWSTR SystemFolder, dest, FilePath;
43
44     static const WCHAR szInstaller[] = 
45         {'M','i','c','r','o','s','o','f','t','\\',
46          'I','n','s','t','a','l','l','e','r','\\',0};
47     static const WCHAR szFolder[] =
48         {'A','p','p','D','a','t','a','F','o','l','d','e','r',0};
49
50     SystemFolder = msi_dup_property( package, szFolder );
51
52     dest = build_directory_name(3, SystemFolder, szInstaller, package->ProductCode);
53
54     create_full_pathW(dest);
55
56     FilePath = build_directory_name(2, dest, icon_name);
57
58     msi_free(SystemFolder);
59     msi_free(dest);
60     return FilePath;
61 }
62
63 LPWSTR msi_dup_record_field( MSIRECORD *rec, INT field )
64 {
65     DWORD sz = 0;
66     LPWSTR str;
67     UINT r;
68
69     if (MSI_RecordIsNull( rec, field ))
70         return NULL;
71
72     r = MSI_RecordGetStringW( rec, field, NULL, &sz );
73     if (r != ERROR_SUCCESS)
74         return NULL;
75
76     sz ++;
77     str = msi_alloc( sz * sizeof (WCHAR) );
78     if (!str)
79         return str;
80     str[0] = 0;
81     r = MSI_RecordGetStringW( rec, field, str, &sz );
82     if (r != ERROR_SUCCESS)
83     {
84         ERR("failed to get string!\n");
85         msi_free( str );
86         return NULL;
87     }
88     return str;
89 }
90
91 MSICOMPONENT* get_loaded_component( MSIPACKAGE* package, LPCWSTR Component )
92 {
93     MSICOMPONENT *comp;
94
95     LIST_FOR_EACH_ENTRY( comp, &package->components, MSICOMPONENT, entry )
96     {
97         if (lstrcmpW(Component,comp->Component)==0)
98             return comp;
99     }
100     return NULL;
101 }
102
103 MSIFEATURE* get_loaded_feature(MSIPACKAGE* package, LPCWSTR Feature )
104 {
105     MSIFEATURE *feature;
106
107     LIST_FOR_EACH_ENTRY( feature, &package->features, MSIFEATURE, entry )
108     {
109         if (lstrcmpW( Feature, feature->Feature )==0)
110             return feature;
111     }
112     return NULL;
113 }
114
115 MSIFILE* get_loaded_file( MSIPACKAGE* package, LPCWSTR key )
116 {
117     MSIFILE *file;
118
119     LIST_FOR_EACH_ENTRY( file, &package->files, MSIFILE, entry )
120     {
121         if (lstrcmpW( key, file->File )==0)
122             return file;
123     }
124     return NULL;
125 }
126
127 int track_tempfile( MSIPACKAGE *package, LPCWSTR path )
128 {
129     MSITEMPFILE *temp;
130
131     TRACE("%s\n", debugstr_w(path));
132
133     LIST_FOR_EACH_ENTRY( temp, &package->tempfiles, MSITEMPFILE, entry )
134         if (!lstrcmpW( path, temp->Path ))
135             return 0;
136
137     temp = msi_alloc_zero( sizeof (MSITEMPFILE) );
138     if (!temp)
139         return -1;
140
141     list_add_head( &package->tempfiles, &temp->entry );
142     temp->Path = strdupW( path );
143
144     return 0;
145 }
146
147 MSIFOLDER *get_loaded_folder( MSIPACKAGE *package, LPCWSTR dir )
148 {
149     MSIFOLDER *folder;
150
151     LIST_FOR_EACH_ENTRY( folder, &package->folders, MSIFOLDER, entry )
152     {
153         if (lstrcmpW( dir, folder->Directory )==0)
154             return folder;
155     }
156     return NULL;
157 }
158
159 void msi_reset_folders( MSIPACKAGE *package, BOOL source )
160 {
161     MSIFOLDER *folder;
162
163     LIST_FOR_EACH_ENTRY( folder, &package->folders, MSIFOLDER, entry )
164     {
165         if ( source )
166         {
167             msi_free( folder->ResolvedSource );
168             folder->ResolvedSource = NULL;
169         }
170         else
171         {
172             msi_free( folder->ResolvedTarget );
173             folder->ResolvedTarget = NULL;
174         }
175     }
176 }
177
178 static LPWSTR get_source_root( MSIPACKAGE *package )
179 {
180     LPWSTR path, p;
181
182     path = msi_dup_property( package, cszSourceDir );
183     if (path)
184         return path;
185
186     path = msi_dup_property( package, cszDatabase );
187     if (path)
188     {
189         p = strrchrW(path,'\\');
190         if (p)
191             *(p+1) = 0;
192     }
193     return path;
194 }
195
196 /*
197  * clean_spaces_from_path()
198  *
199  * removes spaces from the beginning and end of path segments
200  * removes multiple \\ characters
201  */
202 static void clean_spaces_from_path( LPWSTR p )
203 {
204     LPWSTR q = p;
205     int n, len = 0;
206
207     while (1)
208     {
209         /* copy until the end of the string or a space */
210         while (*p != ' ' && (*q = *p))
211         {
212             p++, len++;
213             /* reduce many backslashes to one */
214             if (*p != '\\' || *q != '\\')
215                 q++;
216         }
217
218         /* quit at the end of the string */
219         if (!*p)
220             break;
221
222         /* count the number of spaces */
223         n = 0;
224         while (p[n] == ' ')
225             n++;
226
227         /* if it's leading or trailing space, skip it */
228         if ( len == 0 || p[-1] == '\\' || p[n] == '\\' )
229             p += n;
230         else  /* copy n spaces */
231             while (n && (*q++ = *p++)) n--;
232     }
233 }
234
235 LPWSTR resolve_file_source(MSIPACKAGE *package, MSIFILE *file)
236 {
237     LPWSTR p, path;
238
239     TRACE("Working to resolve source of file %s\n", debugstr_w(file->File));
240
241     if (file->IsCompressed)
242         return NULL;
243
244     p = resolve_folder(package, file->Component->Directory,
245                        TRUE, FALSE, TRUE, NULL);
246     path = build_directory_name(2, p, file->ShortName);
247
248     if (file->LongName &&
249         GetFileAttributesW(path) == INVALID_FILE_ATTRIBUTES)
250     {
251         msi_free(path);
252         path = build_directory_name(2, p, file->LongName);
253     }
254
255     msi_free(p);
256
257     TRACE("file %s source resolves to %s\n", debugstr_w(file->File),
258           debugstr_w(path));
259
260     return path;
261 }
262
263 LPWSTR resolve_folder(MSIPACKAGE *package, LPCWSTR name, BOOL source, 
264                       BOOL set_prop, BOOL load_prop, MSIFOLDER **folder)
265 {
266     MSIFOLDER *f;
267     LPWSTR p, path = NULL, parent;
268
269     TRACE("Working to resolve %s\n",debugstr_w(name));
270
271     if (!name)
272         return NULL;
273
274     if (!lstrcmpW(name,cszSourceDir))
275         name = cszTargetDir;
276
277     f = get_loaded_folder( package, name );
278     if (!f)
279         return NULL;
280
281     /* special resolving for Target and Source root dir */
282     if (!strcmpW(name,cszTargetDir))
283     {
284         if (!f->ResolvedTarget && !f->Property)
285         {
286             LPWSTR check_path;
287             check_path = msi_dup_property( package, cszTargetDir );
288             if (!check_path)
289             {
290                 check_path = msi_dup_property( package, cszRootDrive );
291                 if (set_prop)
292                     MSI_SetPropertyW(package,cszTargetDir,check_path);
293             }
294
295             /* correct misbuilt target dir */
296             path = build_directory_name(2, check_path, NULL);
297             clean_spaces_from_path( path );
298             if (strcmpiW(path,check_path)!=0)
299                 MSI_SetPropertyW(package,cszTargetDir,path);
300             msi_free(check_path);
301
302             f->ResolvedTarget = path;
303         }
304
305         if (!f->ResolvedSource)
306             f->ResolvedSource = get_source_root( package );
307     }
308
309     if (folder)
310         *folder = f;
311
312     if (!source && f->ResolvedTarget)
313     {
314         path = strdupW( f->ResolvedTarget );
315         TRACE("   already resolved to %s\n",debugstr_w(path));
316         return path;
317     }
318
319     if (source && f->ResolvedSource)
320     {
321         path = strdupW( f->ResolvedSource );
322         TRACE("   (source)already resolved to %s\n",debugstr_w(path));
323         return path;
324     }
325
326     if (!source && f->Property)
327     {
328         path = build_directory_name( 2, f->Property, NULL );
329
330         TRACE("   internally set to %s\n",debugstr_w(path));
331         if (set_prop)
332             MSI_SetPropertyW( package, name, path );
333         return path;
334     }
335
336     if (!source && load_prop && (path = msi_dup_property( package, name )))
337     {
338         f->ResolvedTarget = strdupW( path );
339         TRACE("   property set to %s\n", debugstr_w(path));
340         return path;
341     }
342
343     if (!f->Parent)
344         return path;
345
346     parent = f->Parent;
347
348     TRACE(" ! Parent is %s\n", debugstr_w(parent));
349
350     p = resolve_folder(package, parent, source, set_prop, load_prop, NULL);
351     if (!source)
352     {
353         TRACE("   TargetDefault = %s\n", debugstr_w(f->TargetDefault));
354
355         path = build_directory_name( 3, p, f->TargetDefault, NULL );
356         clean_spaces_from_path( path );
357         f->ResolvedTarget = strdupW( path );
358         TRACE("target -> %s\n", debugstr_w(path));
359         if (set_prop)
360             MSI_SetPropertyW(package,name,path);
361     }
362     else
363     {
364         path = NULL;
365
366         if (package->WordCount & msidbSumInfoSourceTypeCompressed)
367             path = get_source_root( package );
368         else if (package->WordCount & msidbSumInfoSourceTypeSFN)
369             path = build_directory_name( 3, p, f->SourceShortPath, NULL );
370         else
371             path = build_directory_name( 3, p, f->SourceLongPath, NULL );
372
373         TRACE("source -> %s\n", debugstr_w(path));
374         f->ResolvedSource = strdupW( path );
375     }
376     msi_free(p);
377
378     return path;
379 }
380
381 /* wrapper to resist a need for a full rewrite right now */
382 DWORD deformat_string(MSIPACKAGE *package, LPCWSTR ptr, WCHAR** data )
383 {
384     if (ptr)
385     {
386         MSIRECORD *rec = MSI_CreateRecord(1);
387         DWORD size = 0;
388
389         MSI_RecordSetStringW(rec,0,ptr);
390         MSI_FormatRecordW(package,rec,NULL,&size);
391
392         size++;
393         *data = msi_alloc(size*sizeof(WCHAR));
394         if (size > 1)
395             MSI_FormatRecordW(package,rec,*data,&size);
396         else
397             *data[0] = 0;
398
399         msiobj_release( &rec->hdr );
400         return sizeof(WCHAR)*size;
401     }
402
403     *data = NULL;
404     return 0;
405 }
406
407 UINT schedule_action(MSIPACKAGE *package, UINT script, LPCWSTR action)
408 {
409     UINT count;
410     LPWSTR *newbuf = NULL;
411     if (script >= TOTAL_SCRIPTS)
412     {
413         FIXME("Unknown script requested %i\n",script);
414         return ERROR_FUNCTION_FAILED;
415     }
416     TRACE("Scheduling Action %s in script %i\n",debugstr_w(action), script);
417     
418     count = package->script->ActionCount[script];
419     package->script->ActionCount[script]++;
420     if (count != 0)
421         newbuf = msi_realloc( package->script->Actions[script],
422                         package->script->ActionCount[script]* sizeof(LPWSTR));
423     else
424         newbuf = msi_alloc( sizeof(LPWSTR));
425
426     newbuf[count] = strdupW(action);
427     package->script->Actions[script] = newbuf;
428
429    return ERROR_SUCCESS;
430 }
431
432 void msi_free_action_script(MSIPACKAGE *package, UINT script)
433 {
434     UINT i;
435     for (i = 0; i < package->script->ActionCount[script]; i++)
436         msi_free(package->script->Actions[script][i]);
437
438     msi_free(package->script->Actions[script]);
439     package->script->Actions[script] = NULL;
440     package->script->ActionCount[script] = 0;
441 }
442
443 static void remove_tracked_tempfiles(MSIPACKAGE* package)
444 {
445     struct list *item, *cursor;
446
447     LIST_FOR_EACH_SAFE( item, cursor, &package->tempfiles )
448     {
449         MSITEMPFILE *temp = LIST_ENTRY( item, MSITEMPFILE, entry );
450
451         list_remove( &temp->entry );
452         TRACE("deleting temp file %s\n", debugstr_w( temp->Path ));
453         if (!DeleteFileW( temp->Path ))
454             ERR("failed to delete %s\n", debugstr_w( temp->Path ));
455         msi_free( temp->Path );
456         msi_free( temp );
457     }
458 }
459
460 static void free_feature( MSIFEATURE *feature )
461 {
462     struct list *item, *cursor;
463
464     LIST_FOR_EACH_SAFE( item, cursor, &feature->Children )
465     {
466         FeatureList *fl = LIST_ENTRY( item, FeatureList, entry );
467         list_remove( &fl->entry );
468         msi_free( fl );
469     }
470
471     LIST_FOR_EACH_SAFE( item, cursor, &feature->Components )
472     {
473         ComponentList *cl = LIST_ENTRY( item, ComponentList, entry );
474         list_remove( &cl->entry );
475         msi_free( cl );
476     }
477     msi_free( feature->Feature );
478     msi_free( feature->Feature_Parent );
479     msi_free( feature->Directory );
480     msi_free( feature->Description );
481     msi_free( feature->Title );
482     msi_free( feature );
483 }
484
485 static void free_extension( MSIEXTENSION *ext )
486 {
487     struct list *item, *cursor;
488
489     LIST_FOR_EACH_SAFE( item, cursor, &ext->verbs )
490     {
491         MSIVERB *verb = LIST_ENTRY( item, MSIVERB, entry );
492
493         list_remove( &verb->entry );
494         msi_free( verb->Verb );
495         msi_free( verb->Command );
496         msi_free( verb->Argument );
497         msi_free( verb );
498     }
499
500     msi_free( ext->Extension );
501     msi_free( ext->ProgIDText );
502     msi_free( ext );
503 }
504
505 /* Called when the package is being closed */
506 void ACTION_free_package_structures( MSIPACKAGE* package)
507 {
508     INT i;
509     struct list *item, *cursor;
510
511     TRACE("Freeing package action data\n");
512
513     remove_tracked_tempfiles(package);
514
515     LIST_FOR_EACH_SAFE( item, cursor, &package->features )
516     {
517         MSIFEATURE *feature = LIST_ENTRY( item, MSIFEATURE, entry );
518         list_remove( &feature->entry );
519         free_feature( feature );
520     }
521
522     LIST_FOR_EACH_SAFE( item, cursor, &package->folders )
523     {
524         MSIFOLDER *folder = LIST_ENTRY( item, MSIFOLDER, entry );
525
526         list_remove( &folder->entry );
527         msi_free( folder->Parent );
528         msi_free( folder->Directory );
529         msi_free( folder->TargetDefault );
530         msi_free( folder->SourceLongPath );
531         msi_free( folder->SourceShortPath );
532         msi_free( folder->ResolvedTarget );
533         msi_free( folder->ResolvedSource );
534         msi_free( folder->Property );
535         msi_free( folder );
536     }
537
538     LIST_FOR_EACH_SAFE( item, cursor, &package->components )
539     {
540         MSICOMPONENT *comp = LIST_ENTRY( item, MSICOMPONENT, entry );
541
542         list_remove( &comp->entry );
543         msi_free( comp->Component );
544         msi_free( comp->ComponentId );
545         msi_free( comp->Directory );
546         msi_free( comp->Condition );
547         msi_free( comp->KeyPath );
548         msi_free( comp->FullKeypath );
549         msi_free( comp );
550     }
551
552     LIST_FOR_EACH_SAFE( item, cursor, &package->files )
553     {
554         MSIFILE *file = LIST_ENTRY( item, MSIFILE, entry );
555
556         list_remove( &file->entry );
557         msi_free( file->File );
558         msi_free( file->FileName );
559         msi_free( file->ShortName );
560         msi_free( file->LongName );
561         msi_free( file->Version );
562         msi_free( file->Language );
563         msi_free( file->TargetPath );
564         msi_free( file );
565     }
566
567     /* clean up extension, progid, class and verb structures */
568     LIST_FOR_EACH_SAFE( item, cursor, &package->classes )
569     {
570         MSICLASS *cls = LIST_ENTRY( item, MSICLASS, entry );
571
572         list_remove( &cls->entry );
573         msi_free( cls->clsid );
574         msi_free( cls->Context );
575         msi_free( cls->Description );
576         msi_free( cls->FileTypeMask );
577         msi_free( cls->IconPath );
578         msi_free( cls->DefInprocHandler );
579         msi_free( cls->DefInprocHandler32 );
580         msi_free( cls->Argument );
581         msi_free( cls->ProgIDText );
582         msi_free( cls );
583     }
584
585     LIST_FOR_EACH_SAFE( item, cursor, &package->extensions )
586     {
587         MSIEXTENSION *ext = LIST_ENTRY( item, MSIEXTENSION, entry );
588
589         list_remove( &ext->entry );
590         free_extension( ext );
591     }
592
593     LIST_FOR_EACH_SAFE( item, cursor, &package->progids )
594     {
595         MSIPROGID *progid = LIST_ENTRY( item, MSIPROGID, entry );
596
597         list_remove( &progid->entry );
598         msi_free( progid->ProgID );
599         msi_free( progid->Description );
600         msi_free( progid->IconPath );
601         msi_free( progid );
602     }
603
604     LIST_FOR_EACH_SAFE( item, cursor, &package->mimes )
605     {
606         MSIMIME *mt = LIST_ENTRY( item, MSIMIME, entry );
607
608         list_remove( &mt->entry );
609         msi_free( mt->clsid );
610         msi_free( mt->ContentType );
611         msi_free( mt );
612     }
613
614     LIST_FOR_EACH_SAFE( item, cursor, &package->appids )
615     {
616         MSIAPPID *appid = LIST_ENTRY( item, MSIAPPID, entry );
617
618         list_remove( &appid->entry );
619         msi_free( appid->AppID );
620         msi_free( appid->RemoteServerName );
621         msi_free( appid->LocalServer );
622         msi_free( appid->ServiceParameters );
623         msi_free( appid->DllSurrogate );
624         msi_free( appid );
625     }
626
627     LIST_FOR_EACH_SAFE( item, cursor, &package->sourcelist_info )
628     {
629         MSISOURCELISTINFO *info = LIST_ENTRY( item, MSISOURCELISTINFO, entry );
630
631         list_remove( &info->entry );
632         msi_free( info->value );
633         msi_free( info );
634     }
635
636     LIST_FOR_EACH_SAFE( item, cursor, &package->sourcelist_media )
637     {
638         MSIMEDIADISK *info = LIST_ENTRY( item, MSIMEDIADISK, entry );
639
640         list_remove( &info->entry );
641         msi_free( info->volume_label );
642         msi_free( info->disk_prompt );
643         msi_free( info );
644     }
645
646     if (package->script)
647     {
648         for (i = 0; i < TOTAL_SCRIPTS; i++)
649             msi_free_action_script(package, i);
650
651         for (i = 0; i < package->script->UniqueActionsCount; i++)
652             msi_free(package->script->UniqueActions[i]);
653
654         msi_free(package->script->UniqueActions);
655         msi_free(package->script);
656     }
657
658     if (package->patch)
659     {
660         msi_free(package->patch->patchcode);
661         msi_free(package->patch->transforms);
662         msi_free(package->patch);
663     }
664
665     msi_free(package->BaseURL);
666     msi_free(package->PackagePath);
667     msi_free(package->ProductCode);
668     msi_free(package->ActionFormat);
669     msi_free(package->LastAction);
670
671     /* cleanup control event subscriptions */
672     ControlEvent_CleanupSubscriptions(package);
673 }
674
675 /*
676  *  build_directory_name()
677  *
678  *  This function is to save messing round with directory names
679  *  It handles adding backslashes between path segments, 
680  *   and can add \ at the end of the directory name if told to.
681  *
682  *  It takes a variable number of arguments.
683  *  It always allocates a new string for the result, so make sure
684  *   to free the return value when finished with it.
685  *
686  *  The first arg is the number of path segments that follow.
687  *  The arguments following count are a list of path segments.
688  *  A path segment may be NULL.
689  *
690  *  Path segments will be added with a \ separating them.
691  *  A \ will not be added after the last segment, however if the
692  *    last segment is NULL, then the last character will be a \
693  * 
694  */
695 LPWSTR build_directory_name(DWORD count, ...)
696 {
697     DWORD sz = 1, i;
698     LPWSTR dir;
699     va_list va;
700
701     va_start(va,count);
702     for(i=0; i<count; i++)
703     {
704         LPCWSTR str = va_arg(va,LPCWSTR);
705         if (str)
706             sz += strlenW(str) + 1;
707     }
708     va_end(va);
709
710     dir = msi_alloc(sz*sizeof(WCHAR));
711     dir[0]=0;
712
713     va_start(va,count);
714     for(i=0; i<count; i++)
715     {
716         LPCWSTR str = va_arg(va,LPCWSTR);
717         if (!str)
718             continue;
719         strcatW(dir, str);
720         if( ((i+1)!=count) && dir[strlenW(dir)-1]!='\\')
721             strcatW(dir, szBackSlash);
722     }
723     return dir;
724 }
725
726 /***********************************************************************
727  *            create_full_pathW
728  *
729  * Recursively create all directories in the path.
730  *
731  * shamelessly stolen from setupapi/queue.c
732  */
733 BOOL create_full_pathW(const WCHAR *path)
734 {
735     BOOL ret = TRUE;
736     int len;
737     WCHAR *new_path;
738
739     new_path = msi_alloc( (strlenW(path) + 1) * sizeof(WCHAR));
740
741     strcpyW(new_path, path);
742
743     while((len = strlenW(new_path)) && new_path[len - 1] == '\\')
744     new_path[len - 1] = 0;
745
746     while(!CreateDirectoryW(new_path, NULL))
747     {
748         WCHAR *slash;
749         DWORD last_error = GetLastError();
750         if(last_error == ERROR_ALREADY_EXISTS)
751             break;
752
753         if(last_error != ERROR_PATH_NOT_FOUND)
754         {
755             ret = FALSE;
756             break;
757         }
758
759         if(!(slash = strrchrW(new_path, '\\')))
760         {
761             ret = FALSE;
762             break;
763         }
764
765         len = slash - new_path;
766         new_path[len] = 0;
767         if(!create_full_pathW(new_path))
768         {
769             ret = FALSE;
770             break;
771         }
772         new_path[len] = '\\';
773     }
774
775     msi_free(new_path);
776     return ret;
777 }
778
779 void ui_progress(MSIPACKAGE *package, int a, int b, int c, int d )
780 {
781     MSIRECORD * row;
782
783     row = MSI_CreateRecord(4);
784     MSI_RecordSetInteger(row,1,a);
785     MSI_RecordSetInteger(row,2,b);
786     MSI_RecordSetInteger(row,3,c);
787     MSI_RecordSetInteger(row,4,d);
788     MSI_ProcessMessage(package, INSTALLMESSAGE_PROGRESS, row);
789     msiobj_release(&row->hdr);
790
791     msi_dialog_check_messages(NULL);
792 }
793
794 void ui_actiondata(MSIPACKAGE *package, LPCWSTR action, MSIRECORD * record)
795 {
796     static const WCHAR Query_t[] = 
797         {'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
798          '`','A','c','t','i','o', 'n','T','e','x','t','`',' ',
799          'W','H','E','R','E',' ', '`','A','c','t','i','o','n','`',' ','=', 
800          ' ','\'','%','s','\'',0};
801     WCHAR message[1024];
802     MSIRECORD * row = 0;
803     DWORD size;
804
805     if (!package->LastAction || strcmpW(package->LastAction,action))
806     {
807         row = MSI_QueryGetRecord(package->db, Query_t, action);
808         if (!row)
809             return;
810
811         if (MSI_RecordIsNull(row,3))
812         {
813             msiobj_release(&row->hdr);
814             return;
815         }
816
817         /* update the cached actionformat */
818         msi_free(package->ActionFormat);
819         package->ActionFormat = msi_dup_record_field(row,3);
820
821         msi_free(package->LastAction);
822         package->LastAction = strdupW(action);
823
824         msiobj_release(&row->hdr);
825     }
826
827     MSI_RecordSetStringW(record,0,package->ActionFormat);
828     size = 1024;
829     MSI_FormatRecordW(package,record,message,&size);
830
831     row = MSI_CreateRecord(1);
832     MSI_RecordSetStringW(row,1,message);
833  
834     MSI_ProcessMessage(package, INSTALLMESSAGE_ACTIONDATA, row);
835
836     msiobj_release(&row->hdr);
837 }
838
839 BOOL ACTION_VerifyComponentForAction( const MSICOMPONENT* comp, INSTALLSTATE check )
840 {
841     if (!comp)
842         return FALSE;
843
844     if (comp->ActionRequest == check)
845         return TRUE;
846     else
847         return FALSE;
848 }
849
850 BOOL ACTION_VerifyFeatureForAction( const MSIFEATURE* feature, INSTALLSTATE check )
851 {
852     if (!feature)
853         return FALSE;
854
855     if (feature->ActionRequest == check)
856         return TRUE;
857     else
858         return FALSE;
859 }
860
861 void reduce_to_longfilename(WCHAR* filename)
862 {
863     LPWSTR p = strchrW(filename,'|');
864     if (p)
865         memmove(filename, p+1, (strlenW(p+1)+1)*sizeof(WCHAR));
866 }
867
868 LPWSTR create_component_advertise_string(MSIPACKAGE* package, 
869                 MSICOMPONENT* component, LPCWSTR feature)
870 {
871     static const WCHAR fmt[] = {'%','s','%','s','%','c','%','s',0};
872     WCHAR productid_85[21], component_85[21];
873     LPWSTR output = NULL;
874     DWORD sz = 0;
875     GUID clsid;
876
877     /* > is used if there is a component GUID and < if not.  */
878
879     productid_85[0] = 0;
880     component_85[0] = 0;
881
882     CLSIDFromString(package->ProductCode, &clsid);
883     encode_base85_guid(&clsid, productid_85);
884
885     if (component)
886     {
887         CLSIDFromString(component->ComponentId, &clsid);
888         encode_base85_guid(&clsid, component_85);
889     }
890
891     TRACE("prod=%s feat=%s comp=%s\n", debugstr_w(productid_85),
892           debugstr_w(feature), debugstr_w(component_85));
893  
894     sz = 20 + lstrlenW(feature) + 20 + 3;
895
896     output = msi_alloc_zero(sz*sizeof(WCHAR));
897
898     sprintfW(output, fmt, productid_85, feature,
899              component?'>':'<', component_85);
900     
901     return output;
902 }
903
904 /* update component state based on a feature change */
905 void ACTION_UpdateComponentStates(MSIPACKAGE *package, LPCWSTR szFeature)
906 {
907     INSTALLSTATE newstate;
908     MSIFEATURE *feature;
909     ComponentList *cl;
910
911     feature = get_loaded_feature(package,szFeature);
912     if (!feature)
913         return;
914
915     newstate = feature->ActionRequest;
916
917     if (newstate == INSTALLSTATE_ABSENT)
918         newstate = INSTALLSTATE_UNKNOWN;
919
920     LIST_FOR_EACH_ENTRY( cl, &feature->Components, ComponentList, entry )
921     {
922         MSICOMPONENT* component = cl->component;
923     
924         TRACE("MODIFYING(%i): Component %s (Installed %i, Action %i, Request %i)\n",
925             newstate, debugstr_w(component->Component), component->Installed, 
926             component->Action, component->ActionRequest);
927         
928         if (!component->Enabled)
929             continue;
930  
931         if (newstate == INSTALLSTATE_LOCAL)
932             msi_component_set_state(package, component, INSTALLSTATE_LOCAL);
933         else 
934         {
935             ComponentList *clist;
936             MSIFEATURE *f;
937
938             component->hasLocalFeature = FALSE;
939
940             msi_component_set_state(package, component, newstate);
941
942             /*if any other feature wants is local we need to set it local*/
943             LIST_FOR_EACH_ENTRY( f, &package->features, MSIFEATURE, entry )
944             {
945                 if ( f->ActionRequest != INSTALLSTATE_LOCAL &&
946                      f->ActionRequest != INSTALLSTATE_SOURCE )
947                 {
948                     continue;
949                 }
950
951                 LIST_FOR_EACH_ENTRY( clist, &f->Components, ComponentList, entry )
952                 {
953                     if ( clist->component == component &&
954                          (f->ActionRequest == INSTALLSTATE_LOCAL ||
955                           f->ActionRequest == INSTALLSTATE_SOURCE) )
956                     {
957                         TRACE("Saved by %s\n", debugstr_w(f->Feature));
958                         component->hasLocalFeature = TRUE;
959
960                         if (component->Attributes & msidbComponentAttributesOptional)
961                         {
962                             if (f->Attributes & msidbFeatureAttributesFavorSource)
963                                 msi_component_set_state(package, component, INSTALLSTATE_SOURCE);
964                             else
965                                 msi_component_set_state(package, component, INSTALLSTATE_LOCAL);
966                         }
967                         else if (component->Attributes & msidbComponentAttributesSourceOnly)
968                             msi_component_set_state(package, component, INSTALLSTATE_SOURCE);
969                         else
970                             msi_component_set_state(package, component, INSTALLSTATE_LOCAL);
971                     }
972                 }
973             }
974         }
975         TRACE("Result (%i): Component %s (Installed %i, Action %i, Request %i)\n",
976             newstate, debugstr_w(component->Component), component->Installed, 
977             component->Action, component->ActionRequest);
978     } 
979 }
980
981 UINT register_unique_action(MSIPACKAGE *package, LPCWSTR action)
982 {
983     UINT count;
984     LPWSTR *newbuf = NULL;
985
986     if (!package->script)
987         return FALSE;
988
989     TRACE("Registering Action %s as having fun\n",debugstr_w(action));
990     
991     count = package->script->UniqueActionsCount;
992     package->script->UniqueActionsCount++;
993     if (count != 0)
994         newbuf = msi_realloc( package->script->UniqueActions,
995                         package->script->UniqueActionsCount* sizeof(LPWSTR));
996     else
997         newbuf = msi_alloc( sizeof(LPWSTR));
998
999     newbuf[count] = strdupW(action);
1000     package->script->UniqueActions = newbuf;
1001
1002     return ERROR_SUCCESS;
1003 }
1004
1005 BOOL check_unique_action(const MSIPACKAGE *package, LPCWSTR action)
1006 {
1007     UINT i;
1008
1009     if (!package->script)
1010         return FALSE;
1011
1012     for (i = 0; i < package->script->UniqueActionsCount; i++)
1013         if (!strcmpW(package->script->UniqueActions[i],action))
1014             return TRUE;
1015
1016     return FALSE;
1017 }
1018
1019 WCHAR* generate_error_string(MSIPACKAGE *package, UINT error, DWORD count, ... )
1020 {
1021     static const WCHAR query[] = {'S','E','L','E','C','T',' ','`','M','e','s','s','a','g','e','`',' ','F','R','O','M',' ','`','E','r','r','o','r','`',' ','W','H','E','R','E',' ','`','E','r','r','o','r','`',' ','=',' ','%','i',0};
1022
1023     MSIRECORD *rec;
1024     MSIRECORD *row;
1025     DWORD size = 0;
1026     DWORD i;
1027     va_list va;
1028     LPCWSTR str;
1029     LPWSTR data;
1030
1031     row = MSI_QueryGetRecord(package->db, query, error);
1032     if (!row)
1033         return 0;
1034
1035     rec = MSI_CreateRecord(count+2);
1036
1037     str = MSI_RecordGetString(row,1);
1038     MSI_RecordSetStringW(rec,0,str);
1039     msiobj_release( &row->hdr );
1040     MSI_RecordSetInteger(rec,1,error);
1041
1042     va_start(va,count);
1043     for (i = 0; i < count; i++)
1044     {
1045         str = va_arg(va,LPCWSTR);
1046         MSI_RecordSetStringW(rec,(i+2),str);
1047     }
1048     va_end(va);
1049
1050     MSI_FormatRecordW(package,rec,NULL,&size);
1051
1052     size++;
1053     data = msi_alloc(size*sizeof(WCHAR));
1054     if (size > 1)
1055         MSI_FormatRecordW(package,rec,data,&size);
1056     else
1057         data[0] = 0;
1058     msiobj_release( &rec->hdr );
1059     return data;
1060 }