Convert MsiEnumComponentQualifiers to use msi_strcpy_to_awstring.
[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 LPWSTR resolve_folder(MSIPACKAGE *package, LPCWSTR name, BOOL source, 
214                       BOOL set_prop, MSIFOLDER **folder)
215 {
216     MSIFOLDER *f;
217     LPWSTR p, path = NULL;
218
219     TRACE("Working to resolve %s\n",debugstr_w(name));
220
221     if (!name)
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 (!source)
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             if (strcmpiW(path,check_path)!=0)
241                 MSI_SetPropertyW(package,cszTargetDir,path);
242             msi_free(check_path);
243         }
244         else
245         {
246             path = msi_dup_property( package, cszSourceDir );
247             if (!path)
248             {
249                 path = msi_dup_property( package, cszDatabase );
250                 if (path)
251                 {
252                     p = strrchrW(path,'\\');
253                     if (p)
254                         *(p+1) = 0;
255                 }
256             }
257         }
258         if (folder)
259             *folder = get_loaded_folder( package, name );
260         return path;
261     }
262
263     f = get_loaded_folder( package, name );
264     if (!f)
265         return NULL;
266
267     if (folder)
268         *folder = f;
269
270     if (!source && f->ResolvedTarget)
271     {
272         path = strdupW( f->ResolvedTarget );
273         TRACE("   already resolved to %s\n",debugstr_w(path));
274         return path;
275     }
276     else if (source && f->ResolvedSource)
277     {
278         path = strdupW( f->ResolvedSource );
279         TRACE("   (source)already resolved to %s\n",debugstr_w(path));
280         return path;
281     }
282     else if (!source && f->Property)
283     {
284         path = build_directory_name( 2, f->Property, NULL );
285                     
286         TRACE("   internally set to %s\n",debugstr_w(path));
287         if (set_prop)
288             MSI_SetPropertyW( package, name, path );
289         return path;
290     }
291
292     if (f->Parent)
293     {
294         LPWSTR parent = f->Parent->Directory;
295
296         TRACE(" ! Parent is %s\n", debugstr_w(parent));
297
298         p = resolve_folder(package, parent, source, set_prop, NULL);
299         if (!source)
300         {
301             TRACE("   TargetDefault = %s\n", debugstr_w(f->TargetDefault));
302
303             path = build_directory_name( 3, p, f->TargetDefault, NULL );
304             f->ResolvedTarget = strdupW( path );
305             TRACE("   resolved into %s\n",debugstr_w(path));
306             if (set_prop)
307                 MSI_SetPropertyW(package,name,path);
308         }
309         else 
310         {
311             if (f->SourceDefault && f->SourceDefault[0]!='.')
312                 path = build_directory_name( 3, p, f->SourceDefault, NULL );
313             else
314                 path = strdupW(p);
315             TRACE("   (source)resolved into %s\n",debugstr_w(path));
316             f->ResolvedSource = strdupW( path );
317         }
318         msi_free(p);
319     }
320     return path;
321 }
322
323 /* wrapper to resist a need for a full rewrite right now */
324 DWORD deformat_string(MSIPACKAGE *package, LPCWSTR ptr, WCHAR** data )
325 {
326     if (ptr)
327     {
328         MSIRECORD *rec = MSI_CreateRecord(1);
329         DWORD size = 0;
330
331         MSI_RecordSetStringW(rec,0,ptr);
332         MSI_FormatRecordW(package,rec,NULL,&size);
333         if (size >= 0)
334         {
335             size++;
336             *data = msi_alloc(size*sizeof(WCHAR));
337             if (size > 1)
338                 MSI_FormatRecordW(package,rec,*data,&size);
339             else
340                 *data[0] = 0;
341             msiobj_release( &rec->hdr );
342             return sizeof(WCHAR)*size;
343         }
344         msiobj_release( &rec->hdr );
345     }
346
347     *data = NULL;
348     return 0;
349 }
350
351 UINT schedule_action(MSIPACKAGE *package, UINT script, LPCWSTR action)
352 {
353     UINT count;
354     LPWSTR *newbuf = NULL;
355     if (script >= TOTAL_SCRIPTS)
356     {
357         FIXME("Unknown script requested %i\n",script);
358         return ERROR_FUNCTION_FAILED;
359     }
360     TRACE("Scheduling Action %s in script %i\n",debugstr_w(action), script);
361     
362     count = package->script->ActionCount[script];
363     package->script->ActionCount[script]++;
364     if (count != 0)
365         newbuf = msi_realloc( package->script->Actions[script],
366                         package->script->ActionCount[script]* sizeof(LPWSTR));
367     else
368         newbuf = msi_alloc( sizeof(LPWSTR));
369
370     newbuf[count] = strdupW(action);
371     package->script->Actions[script] = newbuf;
372
373    return ERROR_SUCCESS;
374 }
375
376 static void remove_tracked_tempfiles(MSIPACKAGE* package)
377 {
378     struct list *item, *cursor;
379
380     LIST_FOR_EACH_SAFE( item, cursor, &package->tempfiles )
381     {
382         MSITEMPFILE *temp = LIST_ENTRY( item, MSITEMPFILE, entry );
383
384         list_remove( &temp->entry );
385         TRACE("deleting temp file %s\n", debugstr_w( temp->Path ));
386         DeleteFileW( temp->Path );
387         msi_free( temp->File );
388         msi_free( temp->Path );
389         msi_free( temp );
390     }
391 }
392
393 static void free_feature( MSIFEATURE *feature )
394 {
395     struct list *item, *cursor;
396
397     LIST_FOR_EACH_SAFE( item, cursor, &feature->Components )
398     {
399         ComponentList *cl = LIST_ENTRY( item, ComponentList, entry );
400         list_remove( &cl->entry );
401         msi_free( cl );
402     }
403     msi_free( feature->Feature );
404     msi_free( feature->Feature_Parent );
405     msi_free( feature->Directory );
406     msi_free( feature->Description );
407     msi_free( feature->Title );
408     msi_free( feature );
409 }
410
411 void free_extension( MSIEXTENSION *ext )
412 {
413     struct list *item, *cursor;
414
415     LIST_FOR_EACH_SAFE( item, cursor, &ext->verbs )
416     {
417         MSIVERB *verb = LIST_ENTRY( item, MSIVERB, entry );
418
419         list_remove( &verb->entry );
420         msi_free( verb->Verb );
421         msi_free( verb->Command );
422         msi_free( verb->Argument );
423         msi_free( verb );
424     }
425
426     msi_free( ext->Extension );
427     msi_free( ext->ProgIDText );
428     msi_free( ext );
429 }
430
431 /* Called when the package is being closed */
432 void ACTION_free_package_structures( MSIPACKAGE* package)
433 {
434     INT i;
435     struct list *item, *cursor;
436     
437     TRACE("Freeing package action data\n");
438
439     remove_tracked_tempfiles(package);
440
441     LIST_FOR_EACH_SAFE( item, cursor, &package->features )
442     {
443         MSIFEATURE *feature = LIST_ENTRY( item, MSIFEATURE, entry );
444         list_remove( &feature->entry );
445         free_feature( feature );
446     }
447
448     LIST_FOR_EACH_SAFE( item, cursor, &package->folders )
449     {
450         MSIFOLDER *folder = LIST_ENTRY( item, MSIFOLDER, entry );
451
452         list_remove( &folder->entry );
453         msi_free( folder->Directory );
454         msi_free( folder->TargetDefault );
455         msi_free( folder->SourceDefault );
456         msi_free( folder->ResolvedTarget );
457         msi_free( folder->ResolvedSource );
458         msi_free( folder->Property );
459         msi_free( folder );
460     }
461
462     LIST_FOR_EACH_SAFE( item, cursor, &package->components )
463     {
464         MSICOMPONENT *comp = LIST_ENTRY( item, MSICOMPONENT, entry );
465         
466         list_remove( &comp->entry );
467         msi_free( comp->Component );
468         msi_free( comp->ComponentId );
469         msi_free( comp->Directory );
470         msi_free( comp->Condition );
471         msi_free( comp->KeyPath );
472         msi_free( comp->FullKeypath );
473         msi_free( comp );
474     }
475
476     LIST_FOR_EACH_SAFE( item, cursor, &package->files )
477     {
478         MSIFILE *file = LIST_ENTRY( item, MSIFILE, entry );
479
480         list_remove( &file->entry );
481         msi_free( file->File );
482         msi_free( file->FileName );
483         msi_free( file->ShortName );
484         msi_free( file->Version );
485         msi_free( file->Language );
486         msi_free( file->SourcePath );
487         msi_free( file->TargetPath );
488         msi_free( file );
489     }
490
491     /* clean up extension, progid, class and verb structures */
492     LIST_FOR_EACH_SAFE( item, cursor, &package->classes )
493     {
494         MSICLASS *cls = LIST_ENTRY( item, MSICLASS, entry );
495
496         list_remove( &cls->entry );
497         msi_free( cls->clsid );
498         msi_free( cls->Context );
499         msi_free( cls->Description );
500         msi_free( cls->FileTypeMask );
501         msi_free( cls->IconPath );
502         msi_free( cls->DefInprocHandler );
503         msi_free( cls->DefInprocHandler32 );
504         msi_free( cls->Argument );
505         msi_free( cls->ProgIDText );
506         msi_free( cls );
507     }
508
509     LIST_FOR_EACH_SAFE( item, cursor, &package->extensions )
510     {
511         MSIEXTENSION *ext = LIST_ENTRY( item, MSIEXTENSION, entry );
512
513         list_remove( &ext->entry );
514         free_extension( ext );
515     }
516
517     LIST_FOR_EACH_SAFE( item, cursor, &package->progids )
518     {
519         MSIPROGID *progid = LIST_ENTRY( item, MSIPROGID, entry );
520
521         list_remove( &progid->entry );
522         msi_free( progid->ProgID );
523         msi_free( progid->Description );
524         msi_free( progid->IconPath );
525         msi_free( progid );
526     }
527
528     LIST_FOR_EACH_SAFE( item, cursor, &package->mimes )
529     {
530         MSIMIME *mt = LIST_ENTRY( item, MSIMIME, entry );
531
532         list_remove( &mt->entry );
533         msi_free( mt->clsid );
534         msi_free( mt->ContentType );
535         msi_free( mt );
536     }
537
538     LIST_FOR_EACH_SAFE( item, cursor, &package->appids )
539     {
540         MSIAPPID *appid = LIST_ENTRY( item, MSIAPPID, entry );
541
542         list_remove( &appid->entry );
543         msi_free( appid->AppID );
544         msi_free( appid->RemoteServerName );
545         msi_free( appid->LocalServer );
546         msi_free( appid->ServiceParameters );
547         msi_free( appid->DllSurrogate );
548         msi_free( appid );
549     }
550
551     if (package->script)
552     {
553         for (i = 0; i < TOTAL_SCRIPTS; i++)
554         {
555             int j;
556             for (j = 0; j < package->script->ActionCount[i]; j++)
557                 msi_free(package->script->Actions[i][j]);
558         
559             msi_free(package->script->Actions[i]);
560         }
561
562         for (i = 0; i < package->script->UniqueActionsCount; i++)
563             msi_free(package->script->UniqueActions[i]);
564
565         msi_free(package->script->UniqueActions);
566         msi_free(package->script);
567     }
568
569     msi_free(package->PackagePath);
570     msi_free(package->ProductCode);
571     msi_free(package->ActionFormat);
572     msi_free(package->LastAction);
573
574     /* cleanup control event subscriptions */
575     ControlEvent_CleanupSubscriptions(package);
576 }
577
578 /*
579  *  build_directory_name()
580  *
581  *  This function is to save messing round with directory names
582  *  It handles adding backslashes between path segments, 
583  *   and can add \ at the end of the directory name if told to.
584  *
585  *  It takes a variable number of arguments.
586  *  It always allocates a new string for the result, so make sure
587  *   to free the return value when finished with it.
588  *
589  *  The first arg is the number of path segments that follow.
590  *  The arguments following count are a list of path segments.
591  *  A path segment may be NULL.
592  *
593  *  Path segments will be added with a \ separating them.
594  *  A \ will not be added after the last segment, however if the
595  *    last segment is NULL, then the last character will be a \
596  * 
597  */
598 LPWSTR build_directory_name(DWORD count, ...)
599 {
600     DWORD sz = 1, i;
601     LPWSTR dir;
602     va_list va;
603
604     va_start(va,count);
605     for(i=0; i<count; i++)
606     {
607         LPCWSTR str = va_arg(va,LPCWSTR);
608         if (str)
609             sz += strlenW(str) + 1;
610     }
611     va_end(va);
612
613     dir = msi_alloc(sz*sizeof(WCHAR));
614     dir[0]=0;
615
616     va_start(va,count);
617     for(i=0; i<count; i++)
618     {
619         LPCWSTR str = va_arg(va,LPCWSTR);
620         if (!str)
621             continue;
622         strcatW(dir, str);
623         if( ((i+1)!=count) && dir[strlenW(dir)-1]!='\\')
624             strcatW(dir, cszbs);
625     }
626     return dir;
627 }
628
629 /***********************************************************************
630  *            create_full_pathW
631  *
632  * Recursively create all directories in the path.
633  *
634  * shamelessly stolen from setupapi/queue.c
635  */
636 BOOL create_full_pathW(const WCHAR *path)
637 {
638     BOOL ret = TRUE;
639     int len;
640     WCHAR *new_path;
641
642     new_path = msi_alloc( (strlenW(path) + 1) * sizeof(WCHAR));
643
644     strcpyW(new_path, path);
645
646     while((len = strlenW(new_path)) && new_path[len - 1] == '\\')
647     new_path[len - 1] = 0;
648
649     while(!CreateDirectoryW(new_path, NULL))
650     {
651         WCHAR *slash;
652         DWORD last_error = GetLastError();
653         if(last_error == ERROR_ALREADY_EXISTS)
654             break;
655
656         if(last_error != ERROR_PATH_NOT_FOUND)
657         {
658             ret = FALSE;
659             break;
660         }
661
662         if(!(slash = strrchrW(new_path, '\\')))
663         {
664             ret = FALSE;
665             break;
666         }
667
668         len = slash - new_path;
669         new_path[len] = 0;
670         if(!create_full_pathW(new_path))
671         {
672             ret = FALSE;
673             break;
674         }
675         new_path[len] = '\\';
676     }
677
678     msi_free(new_path);
679     return ret;
680 }
681
682 void ui_progress(MSIPACKAGE *package, int a, int b, int c, int d )
683 {
684     MSIRECORD * row;
685
686     row = MSI_CreateRecord(4);
687     MSI_RecordSetInteger(row,1,a);
688     MSI_RecordSetInteger(row,2,b);
689     MSI_RecordSetInteger(row,3,c);
690     MSI_RecordSetInteger(row,4,d);
691     MSI_ProcessMessage(package, INSTALLMESSAGE_PROGRESS, row);
692     msiobj_release(&row->hdr);
693
694     msi_dialog_check_messages(NULL);
695 }
696
697 void ui_actiondata(MSIPACKAGE *package, LPCWSTR action, MSIRECORD * record)
698 {
699     static const WCHAR Query_t[] = 
700         {'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
701          '`','A','c','t','i','o', 'n','T','e','x','t','`',' ',
702          'W','H','E','R','E',' ', '`','A','c','t','i','o','n','`',' ','=', 
703          ' ','\'','%','s','\'',0};
704     WCHAR message[1024];
705     MSIRECORD * row = 0;
706     DWORD size;
707     static const WCHAR szActionData[] = 
708         {'A','c','t','i','o','n','D','a','t','a',0};
709
710     if (!package->LastAction || strcmpW(package->LastAction,action))
711     {
712         row = MSI_QueryGetRecord(package->db, Query_t, action);
713         if (!row)
714             return;
715
716         if (MSI_RecordIsNull(row,3))
717         {
718             msiobj_release(&row->hdr);
719             return;
720         }
721
722         /* update the cached actionformat */
723         msi_free(package->ActionFormat);
724         package->ActionFormat = msi_dup_record_field(row,3);
725
726         msi_free(package->LastAction);
727         package->LastAction = strdupW(action);
728
729         msiobj_release(&row->hdr);
730     }
731
732     MSI_RecordSetStringW(record,0,package->ActionFormat);
733     size = 1024;
734     MSI_FormatRecordW(package,record,message,&size);
735
736     row = MSI_CreateRecord(1);
737     MSI_RecordSetStringW(row,1,message);
738  
739     MSI_ProcessMessage(package, INSTALLMESSAGE_ACTIONDATA, row);
740
741     ControlEvent_FireSubscribedEvent(package,szActionData, row);
742
743     msiobj_release(&row->hdr);
744 }
745
746 BOOL ACTION_VerifyComponentForAction( MSICOMPONENT* comp, INSTALLSTATE check )
747 {
748     if (!comp)
749         return FALSE;
750
751     if (comp->Installed == check)
752         return FALSE;
753
754     if (comp->ActionRequest == check)
755         return TRUE;
756     else
757         return FALSE;
758 }
759
760 BOOL ACTION_VerifyFeatureForAction( MSIFEATURE* feature, INSTALLSTATE check )
761 {
762     if (!feature)
763         return FALSE;
764
765     if (feature->Installed == check)
766         return FALSE;
767
768     if (feature->ActionRequest == check)
769         return TRUE;
770     else
771         return FALSE;
772 }
773
774 void reduce_to_longfilename(WCHAR* filename)
775 {
776     LPWSTR p = strchrW(filename,'|');
777     if (p)
778         memmove(filename, p+1, (strlenW(p+1)+1)*sizeof(WCHAR));
779 }
780
781 void reduce_to_shortfilename(WCHAR* filename)
782 {
783     LPWSTR p = strchrW(filename,'|');
784     if (p)
785         *p = 0;
786 }
787
788 LPWSTR create_component_advertise_string(MSIPACKAGE* package, 
789                 MSICOMPONENT* component, LPCWSTR feature)
790 {
791     GUID clsid;
792     WCHAR productid_85[21];
793     WCHAR component_85[21];
794     /*
795      * I have a fair bit of confusion as to when a < is used and when a > is
796      * used. I do not think i have it right...
797      *
798      * Ok it appears that the > is used if there is a guid for the compoenent
799      * and the < is used if not.
800      */
801     static WCHAR fmt1[] = {'%','s','%','s','<',0,0};
802     static WCHAR fmt2[] = {'%','s','%','s','>','%','s',0,0};
803     LPWSTR output = NULL;
804     DWORD sz = 0;
805
806     memset(productid_85,0,sizeof(productid_85));
807     memset(component_85,0,sizeof(component_85));
808
809     CLSIDFromString(package->ProductCode, &clsid);
810     
811     encode_base85_guid(&clsid,productid_85);
812
813     CLSIDFromString(component->ComponentId, &clsid);
814     encode_base85_guid(&clsid,component_85);
815
816     TRACE("Doing something with this... %s %s %s\n", 
817             debugstr_w(productid_85), debugstr_w(feature),
818             debugstr_w(component_85));
819  
820     sz = lstrlenW(productid_85) + lstrlenW(feature);
821     if (component)
822         sz += lstrlenW(component_85);
823
824     sz+=3;
825     sz *= sizeof(WCHAR);
826            
827     output = msi_alloc(sz);
828     memset(output,0,sz);
829
830     if (component)
831         sprintfW(output,fmt2,productid_85,feature,component_85);
832     else
833         sprintfW(output,fmt1,productid_85,feature);
834     
835     return output;
836 }
837
838 /* update compoennt state based on a feature change */
839 void ACTION_UpdateComponentStates(MSIPACKAGE *package, LPCWSTR szFeature)
840 {
841     INSTALLSTATE newstate;
842     MSIFEATURE *feature;
843     ComponentList *cl;
844
845     feature = get_loaded_feature(package,szFeature);
846     if (!feature)
847         return;
848
849     newstate = feature->ActionRequest;
850
851     LIST_FOR_EACH_ENTRY( cl, &feature->Components, ComponentList, entry )
852     {
853         MSICOMPONENT* component = cl->component;
854     
855         TRACE("MODIFYING(%i): Component %s (Installed %i, Action %i, Request %i)\n",
856             newstate, debugstr_w(component->Component), component->Installed, 
857             component->Action, component->ActionRequest);
858         
859         if (!component->Enabled)
860             continue;
861  
862         if (newstate == INSTALLSTATE_LOCAL)
863         {
864             component->ActionRequest = INSTALLSTATE_LOCAL;
865             component->Action = INSTALLSTATE_LOCAL;
866         }
867         else 
868         {
869             ComponentList *clist;
870             MSIFEATURE *f;
871
872             component->ActionRequest = newstate;
873             component->Action = newstate;
874
875             /*if any other feature wants is local we need to set it local*/
876             LIST_FOR_EACH_ENTRY( f, &package->features, MSIFEATURE, entry )
877             {
878                 if ( component->ActionRequest != INSTALLSTATE_LOCAL )
879                     break;
880
881                 LIST_FOR_EACH_ENTRY( clist, &f->Components, ComponentList, entry )
882                 {
883                     if ( clist->component == component )
884                     {
885                         if (f->ActionRequest == INSTALLSTATE_LOCAL)
886                         {
887                             TRACE("Saved by %s\n", debugstr_w(f->Feature));
888                             component->ActionRequest = INSTALLSTATE_LOCAL;
889                             component->Action = INSTALLSTATE_LOCAL;
890                         }
891                         break;
892                     }
893                 }
894             }
895         }
896         TRACE("Result (%i): Component %s (Installed %i, Action %i, Request %i)\n",
897             newstate, debugstr_w(component->Component), component->Installed, 
898             component->Action, component->ActionRequest);
899     } 
900 }
901
902 UINT register_unique_action(MSIPACKAGE *package, LPCWSTR action)
903 {
904     UINT count;
905     LPWSTR *newbuf = NULL;
906
907     if (!package->script)
908         return FALSE;
909
910     TRACE("Registering Action %s as having fun\n",debugstr_w(action));
911     
912     count = package->script->UniqueActionsCount;
913     package->script->UniqueActionsCount++;
914     if (count != 0)
915         newbuf = msi_realloc( package->script->UniqueActions,
916                         package->script->UniqueActionsCount* sizeof(LPWSTR));
917     else
918         newbuf = msi_alloc( sizeof(LPWSTR));
919
920     newbuf[count] = strdupW(action);
921     package->script->UniqueActions = newbuf;
922
923     return ERROR_SUCCESS;
924 }
925
926 BOOL check_unique_action(MSIPACKAGE *package, LPCWSTR action)
927 {
928     INT i;
929
930     if (!package->script)
931         return FALSE;
932
933     for (i = 0; i < package->script->UniqueActionsCount; i++)
934         if (!strcmpW(package->script->UniqueActions[i],action))
935             return TRUE;
936
937     return FALSE;
938 }
939
940 WCHAR* generate_error_string(MSIPACKAGE *package, UINT error, DWORD count, ... )
941 {
942     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};
943
944     MSIRECORD *rec;
945     MSIRECORD *row;
946     DWORD size = 0;
947     DWORD i;
948     va_list va;
949     LPCWSTR str;
950     LPWSTR data;
951
952     row = MSI_QueryGetRecord(package->db, query, error);
953     if (!row)
954         return 0;
955
956     rec = MSI_CreateRecord(count+2);
957
958     str = MSI_RecordGetString(row,1);
959     MSI_RecordSetStringW(rec,0,str);
960     msiobj_release( &row->hdr );
961     MSI_RecordSetInteger(rec,1,error);
962
963     va_start(va,count);
964     for (i = 0; i < count; i++)
965     {
966         str = va_arg(va,LPCWSTR);
967         MSI_RecordSetStringW(rec,(i+2),str);
968     }
969     va_end(va);
970
971     MSI_FormatRecordW(package,rec,NULL,&size);
972     if (size >= 0)
973     {
974         size++;
975         data = msi_alloc(size*sizeof(WCHAR));
976         if (size > 1)
977             MSI_FormatRecordW(package,rec,data,&size);
978         else
979             data[0] = 0;
980         msiobj_release( &rec->hdr );
981         return data;
982     }
983
984     msiobj_release( &rec->hdr );
985     data = NULL;
986     return data;
987 }