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