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