msi/tests: Remove win9x hacks.
[wine] / dlls / msi / helpers.c
1 /*
2  * Implementation of the Microsoft Installer (msi.dll)
3  *
4  * Copyright 2005 Aric Stewart for CodeWeavers
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19  */
20
21 /*
22  * Here are helper functions formally in action.c that are used by a variety of
23  * actions and functions.
24  */
25
26 #include <stdarg.h>
27
28 #include "windef.h"
29 #include "wine/debug.h"
30 #include "msipriv.h"
31 #include "winuser.h"
32 #include "wine/unicode.h"
33 #include "msidefs.h"
34
35 WINE_DEFAULT_DEBUG_CHANNEL(msi);
36
37 static const WCHAR cszTargetDir[] = {'T','A','R','G','E','T','D','I','R',0};
38 static const WCHAR cszDatabase[]={'D','A','T','A','B','A','S','E',0};
39
40 LPWSTR build_icon_path(MSIPACKAGE *package, LPCWSTR icon_name )
41 {
42     LPWSTR SystemFolder, dest, FilePath;
43
44     static const WCHAR szMicrosoft[] =
45         {'M','i','c','r','o','s','o','f','t','\\',0};
46     static const WCHAR szInstaller[] = 
47         {'I','n','s','t','a','l','l','e','r','\\',0};
48     static const WCHAR szADFolder[] =
49         {'A','p','p','D','a','t','a','F','o','l','d','e','r',0};
50     static const WCHAR szWFolder[] =
51         {'W','i','n','d','o','w','s','F','o','l','d','e','r',0};
52
53     if(package->Context == MSIINSTALLCONTEXT_MACHINE)
54         SystemFolder = msi_dup_property( package->db, szWFolder );
55     else
56     {
57         LPWSTR ADTgt = msi_dup_property( package->db, szADFolder );
58         SystemFolder = build_directory_name(2, ADTgt, szMicrosoft);
59         msi_free(ADTgt);
60     }
61
62     dest = build_directory_name(3, SystemFolder, szInstaller, package->ProductCode);
63
64     create_full_pathW(dest);
65
66     FilePath = build_directory_name(2, dest, icon_name);
67
68     msi_free(SystemFolder);
69     msi_free(dest);
70     return FilePath;
71 }
72
73 LPWSTR msi_dup_record_field( MSIRECORD *rec, INT field )
74 {
75     DWORD sz = 0;
76     LPWSTR str;
77     UINT r;
78
79     if (MSI_RecordIsNull( rec, field ))
80         return NULL;
81
82     r = MSI_RecordGetStringW( rec, field, NULL, &sz );
83     if (r != ERROR_SUCCESS)
84         return NULL;
85
86     sz ++;
87     str = msi_alloc( sz * sizeof (WCHAR) );
88     if (!str)
89         return str;
90     str[0] = 0;
91     r = MSI_RecordGetStringW( rec, field, str, &sz );
92     if (r != ERROR_SUCCESS)
93     {
94         ERR("failed to get string!\n");
95         msi_free( str );
96         return NULL;
97     }
98     return str;
99 }
100
101 MSICOMPONENT* get_loaded_component( MSIPACKAGE* package, LPCWSTR Component )
102 {
103     MSICOMPONENT *comp;
104
105     LIST_FOR_EACH_ENTRY( comp, &package->components, MSICOMPONENT, entry )
106     {
107         if (!strcmpW( Component, comp->Component ))
108             return comp;
109     }
110     return NULL;
111 }
112
113 MSIFEATURE* get_loaded_feature(MSIPACKAGE* package, LPCWSTR Feature )
114 {
115     MSIFEATURE *feature;
116
117     LIST_FOR_EACH_ENTRY( feature, &package->features, MSIFEATURE, entry )
118     {
119         if (!strcmpW( Feature, feature->Feature ))
120             return feature;
121     }
122     return NULL;
123 }
124
125 MSIFILE* get_loaded_file( MSIPACKAGE* package, LPCWSTR key )
126 {
127     MSIFILE *file;
128
129     LIST_FOR_EACH_ENTRY( file, &package->files, MSIFILE, entry )
130     {
131         if (!strcmpW( key, file->File ))
132             return file;
133     }
134     return NULL;
135 }
136
137 int track_tempfile( MSIPACKAGE *package, LPCWSTR path )
138 {
139     MSITEMPFILE *temp;
140
141     TRACE("%s\n", debugstr_w(path));
142
143     LIST_FOR_EACH_ENTRY( temp, &package->tempfiles, MSITEMPFILE, entry )
144         if (!strcmpW( path, temp->Path ))
145             return 0;
146
147     temp = msi_alloc_zero( sizeof (MSITEMPFILE) );
148     if (!temp)
149         return -1;
150
151     list_add_head( &package->tempfiles, &temp->entry );
152     temp->Path = strdupW( path );
153
154     return 0;
155 }
156
157 MSIFOLDER *get_loaded_folder( MSIPACKAGE *package, LPCWSTR dir )
158 {
159     MSIFOLDER *folder;
160
161     LIST_FOR_EACH_ENTRY( folder, &package->folders, MSIFOLDER, entry )
162     {
163         if (!strcmpW( dir, folder->Directory ))
164             return folder;
165     }
166     return NULL;
167 }
168
169 static LPWSTR get_source_root( MSIPACKAGE *package )
170 {
171     LPWSTR path, p;
172
173     path = msi_dup_property( package->db, cszSourceDir );
174     if (path)
175         return path;
176
177     path = msi_dup_property( package->db, cszDatabase );
178     if (path)
179     {
180         p = strrchrW(path,'\\');
181         if (p)
182             *(p+1) = 0;
183     }
184     return path;
185 }
186
187 /*
188  * clean_spaces_from_path()
189  *
190  * removes spaces from the beginning and end of path segments
191  * removes multiple \\ characters
192  */
193 static void clean_spaces_from_path( LPWSTR p )
194 {
195     LPWSTR q = p;
196     int n, len = 0;
197
198     while (1)
199     {
200         /* copy until the end of the string or a space */
201         while (*p != ' ' && (*q = *p))
202         {
203             p++, len++;
204             /* reduce many backslashes to one */
205             if (*p != '\\' || *q != '\\')
206                 q++;
207         }
208
209         /* quit at the end of the string */
210         if (!*p)
211             break;
212
213         /* count the number of spaces */
214         n = 0;
215         while (p[n] == ' ')
216             n++;
217
218         /* if it's leading or trailing space, skip it */
219         if ( len == 0 || p[-1] == '\\' || p[n] == '\\' )
220             p += n;
221         else  /* copy n spaces */
222             while (n && (*q++ = *p++)) n--;
223     }
224 }
225
226 LPWSTR resolve_file_source(MSIPACKAGE *package, MSIFILE *file)
227 {
228     LPWSTR p, path;
229
230     TRACE("Working to resolve source of file %s\n", debugstr_w(file->File));
231
232     if (file->IsCompressed)
233         return NULL;
234
235     p = resolve_source_folder( package, file->Component->Directory, NULL );
236     path = build_directory_name(2, p, file->ShortName);
237
238     if (file->LongName &&
239         GetFileAttributesW(path) == INVALID_FILE_ATTRIBUTES)
240     {
241         msi_free(path);
242         path = build_directory_name(2, p, file->LongName);
243     }
244
245     msi_free(p);
246
247     TRACE("file %s source resolves to %s\n", debugstr_w(file->File), debugstr_w(path));
248     return path;
249 }
250
251 LPWSTR resolve_source_folder( MSIPACKAGE *package, LPCWSTR name, MSIFOLDER **folder )
252 {
253     MSIFOLDER *f;
254     LPWSTR p, path = NULL, parent;
255
256     TRACE("working to resolve %s\n", debugstr_w(name));
257
258     if (!strcmpW( name, cszSourceDir ))
259         name = cszTargetDir;
260
261     f = get_loaded_folder( package, name );
262     if (!f)
263         return NULL;
264
265     /* special resolving for Target and Source root dir */
266     if (!strcmpW( name, cszTargetDir ))
267     {
268         if (!f->ResolvedSource)
269             f->ResolvedSource = get_source_root( package );
270     }
271
272     if (folder)
273         *folder = f;
274
275     if (f->ResolvedSource)
276     {
277         path = strdupW( f->ResolvedSource );
278         TRACE("   already resolved to %s\n", debugstr_w(path));
279         return path;
280     }
281
282     if (!f->Parent)
283         return path;
284
285     parent = f->Parent;
286     TRACE(" ! parent is %s\n", debugstr_w(parent));
287
288     p = resolve_source_folder( package, parent, NULL );
289
290     if (package->WordCount & msidbSumInfoSourceTypeCompressed)
291         path = get_source_root( package );
292     else if (package->WordCount & msidbSumInfoSourceTypeSFN)
293         path = build_directory_name( 3, p, f->SourceShortPath, NULL );
294     else
295         path = build_directory_name( 3, p, f->SourceLongPath, NULL );
296
297     TRACE("-> %s\n", debugstr_w(path));
298     f->ResolvedSource = strdupW( path );
299     msi_free( p );
300
301     return path;
302 }
303
304 LPWSTR resolve_target_folder( MSIPACKAGE *package, LPCWSTR name, BOOL set_prop, BOOL load_prop,
305                               MSIFOLDER **folder )
306 {
307     MSIFOLDER *f;
308     LPWSTR p, path = NULL, parent;
309
310     TRACE("working to resolve %s\n", debugstr_w(name));
311
312     f = get_loaded_folder( package, name );
313     if (!f)
314         return NULL;
315
316     /* special resolving for Target and Source root dir */
317     if (!strcmpW( name, cszTargetDir ))
318     {
319         if (!f->ResolvedTarget && !f->Property)
320         {
321             LPWSTR check_path;
322             check_path = msi_dup_property( package->db, cszTargetDir );
323             if (!check_path)
324             {
325                 check_path = msi_dup_property( package->db, cszRootDrive );
326                 if (set_prop)
327                     msi_set_property( package->db, cszTargetDir, check_path );
328             }
329
330             /* correct misbuilt target dir */
331             path = build_directory_name(2, check_path, NULL);
332             clean_spaces_from_path( path );
333             if (strcmpiW( path, check_path ))
334                 msi_set_property( package->db, cszTargetDir, path );
335             msi_free(check_path);
336
337             f->ResolvedTarget = path;
338         }
339     }
340
341     if (folder)
342         *folder = f;
343
344     if (f->ResolvedTarget)
345     {
346         path = strdupW( f->ResolvedTarget );
347         TRACE("   already resolved to %s\n", debugstr_w(path));
348         return path;
349     }
350
351     if (f->Property)
352     {
353         path = build_directory_name( 2, f->Property, NULL );
354         TRACE("   internally set to %s\n", debugstr_w(path));
355         if (set_prop) msi_set_property( package->db, name, path );
356         return path;
357     }
358
359     if (load_prop && (path = msi_dup_property( package->db, name )))
360     {
361         f->ResolvedTarget = strdupW( path );
362         TRACE("   property set to %s\n", debugstr_w(path));
363         return path;
364     }
365
366     if (!f->Parent)
367         return path;
368
369     parent = f->Parent;
370
371     TRACE(" ! parent is %s\n", debugstr_w(parent));
372
373     p = resolve_target_folder( package, parent, set_prop, load_prop, NULL );
374
375     TRACE("   TargetDefault = %s\n", debugstr_w(f->TargetDefault));
376     path = build_directory_name( 3, p, f->TargetDefault, NULL );
377     clean_spaces_from_path( path );
378     f->ResolvedTarget = strdupW( path );
379
380     TRACE("-> %s\n", debugstr_w(path));
381     if (set_prop) msi_set_property( package->db, name, path );
382     msi_free( p );
383
384     return path;
385 }
386
387 /* wrapper to resist a need for a full rewrite right now */
388 DWORD deformat_string(MSIPACKAGE *package, LPCWSTR ptr, WCHAR** data )
389 {
390     if (ptr)
391     {
392         MSIRECORD *rec = MSI_CreateRecord(1);
393         DWORD size = 0;
394
395         MSI_RecordSetStringW(rec,0,ptr);
396         MSI_FormatRecordW(package,rec,NULL,&size);
397
398         size++;
399         *data = msi_alloc(size*sizeof(WCHAR));
400         if (size > 1)
401             MSI_FormatRecordW(package,rec,*data,&size);
402         else
403             *data[0] = 0;
404
405         msiobj_release( &rec->hdr );
406         return sizeof(WCHAR)*size;
407     }
408
409     *data = NULL;
410     return 0;
411 }
412
413 UINT schedule_action(MSIPACKAGE *package, UINT script, LPCWSTR action)
414 {
415     UINT count;
416     LPWSTR *newbuf = NULL;
417     if (script >= TOTAL_SCRIPTS)
418     {
419         FIXME("Unknown script requested %i\n",script);
420         return ERROR_FUNCTION_FAILED;
421     }
422     TRACE("Scheduling Action %s in script %i\n",debugstr_w(action), script);
423     
424     count = package->script->ActionCount[script];
425     package->script->ActionCount[script]++;
426     if (count != 0)
427         newbuf = msi_realloc( package->script->Actions[script],
428                         package->script->ActionCount[script]* sizeof(LPWSTR));
429     else
430         newbuf = msi_alloc( sizeof(LPWSTR));
431
432     newbuf[count] = strdupW(action);
433     package->script->Actions[script] = newbuf;
434
435    return ERROR_SUCCESS;
436 }
437
438 void msi_free_action_script(MSIPACKAGE *package, UINT script)
439 {
440     UINT i;
441     for (i = 0; i < package->script->ActionCount[script]; i++)
442         msi_free(package->script->Actions[script][i]);
443
444     msi_free(package->script->Actions[script]);
445     package->script->Actions[script] = NULL;
446     package->script->ActionCount[script] = 0;
447 }
448
449 /*
450  *  build_directory_name()
451  *
452  *  This function is to save messing round with directory names
453  *  It handles adding backslashes between path segments, 
454  *   and can add \ at the end of the directory name if told to.
455  *
456  *  It takes a variable number of arguments.
457  *  It always allocates a new string for the result, so make sure
458  *   to free the return value when finished with it.
459  *
460  *  The first arg is the number of path segments that follow.
461  *  The arguments following count are a list of path segments.
462  *  A path segment may be NULL.
463  *
464  *  Path segments will be added with a \ separating them.
465  *  A \ will not be added after the last segment, however if the
466  *    last segment is NULL, then the last character will be a \
467  * 
468  */
469 LPWSTR build_directory_name(DWORD count, ...)
470 {
471     DWORD sz = 1, i;
472     LPWSTR dir;
473     va_list va;
474
475     va_start(va,count);
476     for(i=0; i<count; i++)
477     {
478         LPCWSTR str = va_arg(va,LPCWSTR);
479         if (str)
480             sz += strlenW(str) + 1;
481     }
482     va_end(va);
483
484     dir = msi_alloc(sz*sizeof(WCHAR));
485     dir[0]=0;
486
487     va_start(va,count);
488     for(i=0; i<count; i++)
489     {
490         LPCWSTR str = va_arg(va,LPCWSTR);
491         if (!str)
492             continue;
493         strcatW(dir, str);
494         if( ((i+1)!=count) && dir[strlenW(dir)-1]!='\\')
495             strcatW(dir, szBackSlash);
496     }
497     return dir;
498 }
499
500 /***********************************************************************
501  *            create_full_pathW
502  *
503  * Recursively create all directories in the path.
504  *
505  * shamelessly stolen from setupapi/queue.c
506  */
507 BOOL create_full_pathW(const WCHAR *path)
508 {
509     BOOL ret = TRUE;
510     int len;
511     WCHAR *new_path;
512
513     new_path = msi_alloc( (strlenW(path) + 1) * sizeof(WCHAR));
514
515     strcpyW(new_path, path);
516
517     while((len = strlenW(new_path)) && new_path[len - 1] == '\\')
518     new_path[len - 1] = 0;
519
520     while(!CreateDirectoryW(new_path, NULL))
521     {
522         WCHAR *slash;
523         DWORD last_error = GetLastError();
524         if(last_error == ERROR_ALREADY_EXISTS)
525             break;
526
527         if(last_error != ERROR_PATH_NOT_FOUND)
528         {
529             ret = FALSE;
530             break;
531         }
532
533         if(!(slash = strrchrW(new_path, '\\')))
534         {
535             ret = FALSE;
536             break;
537         }
538
539         len = slash - new_path;
540         new_path[len] = 0;
541         if(!create_full_pathW(new_path))
542         {
543             ret = FALSE;
544             break;
545         }
546         new_path[len] = '\\';
547     }
548
549     msi_free(new_path);
550     return ret;
551 }
552
553 void ui_progress(MSIPACKAGE *package, int a, int b, int c, int d )
554 {
555     MSIRECORD * row;
556
557     row = MSI_CreateRecord(4);
558     MSI_RecordSetInteger(row,1,a);
559     MSI_RecordSetInteger(row,2,b);
560     MSI_RecordSetInteger(row,3,c);
561     MSI_RecordSetInteger(row,4,d);
562     MSI_ProcessMessage(package, INSTALLMESSAGE_PROGRESS, row);
563     msiobj_release(&row->hdr);
564
565     msi_dialog_check_messages(NULL);
566 }
567
568 void ui_actiondata(MSIPACKAGE *package, LPCWSTR action, MSIRECORD * record)
569 {
570     static const WCHAR Query_t[] = 
571         {'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
572          '`','A','c','t','i','o', 'n','T','e','x','t','`',' ',
573          'W','H','E','R','E',' ', '`','A','c','t','i','o','n','`',' ','=', 
574          ' ','\'','%','s','\'',0};
575     WCHAR message[1024];
576     MSIRECORD * row = 0;
577     DWORD size;
578
579     if (!package->LastAction || strcmpW(package->LastAction, action))
580     {
581         row = MSI_QueryGetRecord(package->db, Query_t, action);
582         if (!row)
583             return;
584
585         if (MSI_RecordIsNull(row,3))
586         {
587             msiobj_release(&row->hdr);
588             return;
589         }
590
591         /* update the cached actionformat */
592         msi_free(package->ActionFormat);
593         package->ActionFormat = msi_dup_record_field(row,3);
594
595         msi_free(package->LastAction);
596         package->LastAction = strdupW(action);
597
598         msiobj_release(&row->hdr);
599     }
600
601     MSI_RecordSetStringW(record,0,package->ActionFormat);
602     size = 1024;
603     MSI_FormatRecordW(package,record,message,&size);
604
605     row = MSI_CreateRecord(1);
606     MSI_RecordSetStringW(row,1,message);
607  
608     MSI_ProcessMessage(package, INSTALLMESSAGE_ACTIONDATA, row);
609
610     msiobj_release(&row->hdr);
611 }
612
613 void reduce_to_longfilename(WCHAR* filename)
614 {
615     LPWSTR p = strchrW(filename,'|');
616     if (p)
617         memmove(filename, p+1, (strlenW(p+1)+1)*sizeof(WCHAR));
618 }
619
620 LPWSTR create_component_advertise_string(MSIPACKAGE* package, 
621                 MSICOMPONENT* component, LPCWSTR feature)
622 {
623     static const WCHAR fmt[] = {'%','s','%','s','%','c','%','s',0};
624     WCHAR productid_85[21], component_85[21];
625     LPWSTR output = NULL;
626     DWORD sz = 0;
627     GUID clsid;
628
629     /* > is used if there is a component GUID and < if not.  */
630
631     productid_85[0] = 0;
632     component_85[0] = 0;
633
634     CLSIDFromString(package->ProductCode, &clsid);
635     encode_base85_guid(&clsid, productid_85);
636
637     if (component)
638     {
639         CLSIDFromString(component->ComponentId, &clsid);
640         encode_base85_guid(&clsid, component_85);
641     }
642
643     TRACE("prod=%s feat=%s comp=%s\n", debugstr_w(productid_85),
644           debugstr_w(feature), debugstr_w(component_85));
645  
646     sz = 20 + lstrlenW(feature) + 20 + 3;
647
648     output = msi_alloc_zero(sz*sizeof(WCHAR));
649
650     sprintfW(output, fmt, productid_85, feature,
651              component?'>':'<', component_85);
652     
653     return output;
654 }
655
656 /* update component state based on a feature change */
657 void ACTION_UpdateComponentStates( MSIPACKAGE *package, MSIFEATURE *feature )
658 {
659     INSTALLSTATE newstate;
660     ComponentList *cl;
661
662     newstate = feature->ActionRequest;
663
664     if (newstate == INSTALLSTATE_ABSENT)
665         newstate = INSTALLSTATE_UNKNOWN;
666
667     LIST_FOR_EACH_ENTRY( cl, &feature->Components, ComponentList, entry )
668     {
669         MSICOMPONENT* component = cl->component;
670     
671         if (!component->Enabled) continue;
672
673         TRACE("MODIFYING(%i): Component %s (Installed %i, Action %i, Request %i)\n",
674             newstate, debugstr_w(component->Component), component->Installed, 
675             component->Action, component->ActionRequest);
676         
677         if (newstate == INSTALLSTATE_LOCAL)
678         {
679             component->Action = INSTALLSTATE_LOCAL;
680             component->ActionRequest = INSTALLSTATE_LOCAL;
681         }
682         else 
683         {
684             ComponentList *clist;
685             MSIFEATURE *f;
686
687             component->hasLocalFeature = FALSE;
688
689             component->Action = newstate;
690             component->ActionRequest = newstate;
691
692             /* if any other feature wants it local we need to set it local */
693             LIST_FOR_EACH_ENTRY( f, &package->features, MSIFEATURE, entry )
694             {
695                 if ( f->ActionRequest != INSTALLSTATE_LOCAL &&
696                      f->ActionRequest != INSTALLSTATE_SOURCE )
697                 {
698                     continue;
699                 }
700
701                 LIST_FOR_EACH_ENTRY( clist, &f->Components, ComponentList, entry )
702                 {
703                     if ( clist->component == component &&
704                          (f->ActionRequest == INSTALLSTATE_LOCAL ||
705                           f->ActionRequest == INSTALLSTATE_SOURCE) )
706                     {
707                         TRACE("Saved by %s\n", debugstr_w(f->Feature));
708                         component->hasLocalFeature = TRUE;
709
710                         if (component->Attributes & msidbComponentAttributesOptional)
711                         {
712                             if (f->Attributes & msidbFeatureAttributesFavorSource)
713                             {
714                                 component->Action = INSTALLSTATE_SOURCE;
715                                 component->ActionRequest = INSTALLSTATE_SOURCE;
716                             }
717                             else
718                             {
719                                 component->Action = INSTALLSTATE_LOCAL;
720                                 component->ActionRequest = INSTALLSTATE_LOCAL;
721                             }
722                         }
723                         else if (component->Attributes & msidbComponentAttributesSourceOnly)
724                         {
725                             component->Action = INSTALLSTATE_SOURCE;
726                             component->ActionRequest = INSTALLSTATE_SOURCE;
727                         }
728                         else
729                         {
730                             component->Action = INSTALLSTATE_LOCAL;
731                             component->ActionRequest = INSTALLSTATE_LOCAL;
732                         }
733                     }
734                 }
735             }
736         }
737         TRACE("Result (%i): Component %s (Installed %i, Action %i, Request %i)\n",
738             newstate, debugstr_w(component->Component), component->Installed, 
739             component->Action, component->ActionRequest);
740     } 
741 }
742
743 UINT register_unique_action(MSIPACKAGE *package, LPCWSTR action)
744 {
745     UINT count;
746     LPWSTR *newbuf = NULL;
747
748     if (!package->script)
749         return FALSE;
750
751     TRACE("Registering %s as unique action\n", debugstr_w(action));
752     
753     count = package->script->UniqueActionsCount;
754     package->script->UniqueActionsCount++;
755     if (count != 0)
756         newbuf = msi_realloc( package->script->UniqueActions,
757                         package->script->UniqueActionsCount* sizeof(LPWSTR));
758     else
759         newbuf = msi_alloc( sizeof(LPWSTR));
760
761     newbuf[count] = strdupW(action);
762     package->script->UniqueActions = newbuf;
763
764     return ERROR_SUCCESS;
765 }
766
767 BOOL check_unique_action(const MSIPACKAGE *package, LPCWSTR action)
768 {
769     UINT i;
770
771     if (!package->script)
772         return FALSE;
773
774     for (i = 0; i < package->script->UniqueActionsCount; i++)
775         if (!strcmpW(package->script->UniqueActions[i], action))
776             return TRUE;
777
778     return FALSE;
779 }
780
781 WCHAR* generate_error_string(MSIPACKAGE *package, UINT error, DWORD count, ... )
782 {
783     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};
784
785     MSIRECORD *rec;
786     MSIRECORD *row;
787     DWORD size = 0;
788     DWORD i;
789     va_list va;
790     LPCWSTR str;
791     LPWSTR data;
792
793     row = MSI_QueryGetRecord(package->db, query, error);
794     if (!row)
795         return 0;
796
797     rec = MSI_CreateRecord(count+2);
798
799     str = MSI_RecordGetString(row,1);
800     MSI_RecordSetStringW(rec,0,str);
801     msiobj_release( &row->hdr );
802     MSI_RecordSetInteger(rec,1,error);
803
804     va_start(va,count);
805     for (i = 0; i < count; i++)
806     {
807         str = va_arg(va,LPCWSTR);
808         MSI_RecordSetStringW(rec,(i+2),str);
809     }
810     va_end(va);
811
812     MSI_FormatRecordW(package,rec,NULL,&size);
813
814     size++;
815     data = msi_alloc(size*sizeof(WCHAR));
816     if (size > 1)
817         MSI_FormatRecordW(package,rec,data,&size);
818     else
819         data[0] = 0;
820     msiobj_release( &rec->hdr );
821     return data;
822 }