kernel32/tests: Add a few more tests, fix some failures on Windows.
[wine/wine-kai.git] / dlls / msi / helpers.c
blobd15d79a2e5ffbf53e2e74e81a4579dd805cbb076
1 /*
2 * Implementation of the Microsoft Installer (msi.dll)
4 * Copyright 2005 Aric Stewart for CodeWeavers
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.
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.
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
22 * Here are helper functions formally in action.c that are used by a variety of
23 * actions and functions.
26 #include <stdarg.h>
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"
35 WINE_DEFAULT_DEBUG_CHANNEL(msi);
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};
40 const WCHAR cszSourceDir[] = {'S','o','u','r','c','e','D','i','r',0};
41 const WCHAR cszSOURCEDIR[] = {'S','O','U','R','C','E','D','I','R',0};
42 const WCHAR cszRootDrive[] = {'R','O','O','T','D','R','I','V','E',0};
43 const WCHAR cszbs[]={'\\',0};
45 LPWSTR build_icon_path(MSIPACKAGE *package, LPCWSTR icon_name )
47 LPWSTR SystemFolder, dest, FilePath;
49 static const WCHAR szInstaller[] =
50 {'M','i','c','r','o','s','o','f','t','\\',
51 'I','n','s','t','a','l','l','e','r','\\',0};
52 static const WCHAR szFolder[] =
53 {'A','p','p','D','a','t','a','F','o','l','d','e','r',0};
55 SystemFolder = msi_dup_property( package, szFolder );
57 dest = build_directory_name(3, SystemFolder, szInstaller, package->ProductCode);
59 create_full_pathW(dest);
61 FilePath = build_directory_name(2, dest, icon_name);
63 msi_free(SystemFolder);
64 msi_free(dest);
65 return FilePath;
68 LPWSTR msi_dup_record_field( MSIRECORD *rec, INT field )
70 DWORD sz = 0;
71 LPWSTR str;
72 UINT r;
74 if (MSI_RecordIsNull( rec, field ))
75 return NULL;
77 r = MSI_RecordGetStringW( rec, field, NULL, &sz );
78 if (r != ERROR_SUCCESS)
79 return NULL;
81 sz ++;
82 str = msi_alloc( sz * sizeof (WCHAR) );
83 if (!str)
84 return str;
85 str[0] = 0;
86 r = MSI_RecordGetStringW( rec, field, str, &sz );
87 if (r != ERROR_SUCCESS)
89 ERR("failed to get string!\n");
90 msi_free( str );
91 return NULL;
93 return str;
96 MSICOMPONENT* get_loaded_component( MSIPACKAGE* package, LPCWSTR Component )
98 MSICOMPONENT *comp;
100 LIST_FOR_EACH_ENTRY( comp, &package->components, MSICOMPONENT, entry )
102 if (lstrcmpW(Component,comp->Component)==0)
103 return comp;
105 return NULL;
108 MSIFEATURE* get_loaded_feature(MSIPACKAGE* package, LPCWSTR Feature )
110 MSIFEATURE *feature;
112 LIST_FOR_EACH_ENTRY( feature, &package->features, MSIFEATURE, entry )
114 if (lstrcmpW( Feature, feature->Feature )==0)
115 return feature;
117 return NULL;
120 MSIFILE* get_loaded_file( MSIPACKAGE* package, LPCWSTR key )
122 MSIFILE *file;
124 LIST_FOR_EACH_ENTRY( file, &package->files, MSIFILE, entry )
126 if (lstrcmpW( key, file->File )==0)
127 return file;
129 return NULL;
132 int track_tempfile( MSIPACKAGE *package, LPCWSTR path )
134 MSITEMPFILE *temp;
136 TRACE("%s\n", debugstr_w(path));
138 LIST_FOR_EACH_ENTRY( temp, &package->tempfiles, MSITEMPFILE, entry )
139 if (!lstrcmpW( path, temp->Path ))
140 return 0;
142 temp = msi_alloc_zero( sizeof (MSITEMPFILE) );
143 if (!temp)
144 return -1;
146 list_add_head( &package->tempfiles, &temp->entry );
147 temp->Path = strdupW( path );
149 return 0;
152 MSIFOLDER *get_loaded_folder( MSIPACKAGE *package, LPCWSTR dir )
154 MSIFOLDER *folder;
156 LIST_FOR_EACH_ENTRY( folder, &package->folders, MSIFOLDER, entry )
158 if (lstrcmpW( dir, folder->Directory )==0)
159 return folder;
161 return NULL;
164 void msi_reset_folders( MSIPACKAGE *package, BOOL source )
166 MSIFOLDER *folder;
168 LIST_FOR_EACH_ENTRY( folder, &package->folders, MSIFOLDER, entry )
170 if ( source )
172 msi_free( folder->ResolvedSource );
173 folder->ResolvedSource = NULL;
175 else
177 msi_free( folder->ResolvedTarget );
178 folder->ResolvedTarget = NULL;
183 static LPWSTR get_source_root( MSIPACKAGE *package )
185 LPWSTR path, p;
187 path = msi_dup_property( package, cszSourceDir );
188 if (path)
189 return path;
191 path = msi_dup_property( package, cszDatabase );
192 if (path)
194 p = strrchrW(path,'\\');
195 if (p)
196 *(p+1) = 0;
198 return path;
202 * clean_spaces_from_path()
204 * removes spaces from the beginning and end of path segments
205 * removes multiple \\ characters
207 static void clean_spaces_from_path( LPWSTR p )
209 LPWSTR q = p;
210 int n, len = 0;
212 while (1)
214 /* copy until the end of the string or a space */
215 while (*p != ' ' && (*q = *p))
217 p++, len++;
218 /* reduce many backslashes to one */
219 if (*p != '\\' || *q != '\\')
220 q++;
223 /* quit at the end of the string */
224 if (!*p)
225 break;
227 /* count the number of spaces */
228 n = 0;
229 while (p[n] == ' ')
230 n++;
232 /* if it's leading or trailing space, skip it */
233 if ( len == 0 || p[-1] == '\\' || p[n] == '\\' )
234 p += n;
235 else /* copy n spaces */
236 while (n && (*q++ = *p++)) n--;
240 LPWSTR resolve_file_source(MSIPACKAGE *package, MSIFILE *file)
242 LPWSTR p, path;
244 TRACE("Working to resolve source of file %s\n", debugstr_w(file->File));
246 if (file->IsCompressed)
247 return NULL;
249 p = resolve_folder(package, file->Component->Directory,
250 TRUE, FALSE, TRUE, NULL);
251 path = build_directory_name(2, p, file->ShortName);
253 if (file->LongName &&
254 GetFileAttributesW(path) == INVALID_FILE_ATTRIBUTES)
256 msi_free(path);
257 path = build_directory_name(2, p, file->LongName);
260 msi_free(p);
262 TRACE("file %s source resolves to %s\n", debugstr_w(file->File),
263 debugstr_w(path));
265 return path;
268 LPWSTR resolve_folder(MSIPACKAGE *package, LPCWSTR name, BOOL source,
269 BOOL set_prop, BOOL load_prop, MSIFOLDER **folder)
271 MSIFOLDER *f;
272 LPWSTR p, path = NULL, parent;
274 TRACE("Working to resolve %s\n",debugstr_w(name));
276 if (!name)
277 return NULL;
279 if (!lstrcmpW(name,cszSourceDir))
280 name = cszTargetDir;
282 f = get_loaded_folder( package, name );
283 if (!f)
284 return NULL;
286 /* special resolving for Target and Source root dir */
287 if (!strcmpW(name,cszTargetDir))
289 if (!f->ResolvedTarget && !f->Property)
291 LPWSTR check_path;
292 check_path = msi_dup_property( package, cszTargetDir );
293 if (!check_path)
295 check_path = msi_dup_property( package, cszRootDrive );
296 if (set_prop)
297 MSI_SetPropertyW(package,cszTargetDir,check_path);
300 /* correct misbuilt target dir */
301 path = build_directory_name(2, check_path, NULL);
302 clean_spaces_from_path( path );
303 if (strcmpiW(path,check_path)!=0)
304 MSI_SetPropertyW(package,cszTargetDir,path);
305 msi_free(check_path);
307 f->ResolvedTarget = path;
310 if (!f->ResolvedSource)
311 f->ResolvedSource = get_source_root( package );
314 if (folder)
315 *folder = f;
317 if (!source && f->ResolvedTarget)
319 path = strdupW( f->ResolvedTarget );
320 TRACE(" already resolved to %s\n",debugstr_w(path));
321 return path;
324 if (source && f->ResolvedSource)
326 path = strdupW( f->ResolvedSource );
327 TRACE(" (source)already resolved to %s\n",debugstr_w(path));
328 return path;
331 if (!source && f->Property)
333 path = build_directory_name( 2, f->Property, NULL );
335 TRACE(" internally set to %s\n",debugstr_w(path));
336 if (set_prop)
337 MSI_SetPropertyW( package, name, path );
338 return path;
341 if (!source && load_prop && (path = msi_dup_property( package, name )))
343 f->ResolvedTarget = strdupW( path );
344 TRACE(" property set to %s\n", debugstr_w(path));
345 return path;
348 if (!f->Parent)
349 return path;
351 parent = f->Parent;
353 TRACE(" ! Parent is %s\n", debugstr_w(parent));
355 p = resolve_folder(package, parent, source, set_prop, load_prop, NULL);
356 if (!source)
358 TRACE(" TargetDefault = %s\n", debugstr_w(f->TargetDefault));
360 path = build_directory_name( 3, p, f->TargetDefault, NULL );
361 clean_spaces_from_path( path );
362 f->ResolvedTarget = strdupW( path );
363 TRACE("target -> %s\n", debugstr_w(path));
364 if (set_prop)
365 MSI_SetPropertyW(package,name,path);
367 else
369 path = NULL;
371 if (package->WordCount & msidbSumInfoSourceTypeCompressed)
372 path = get_source_root( package );
373 else if (package->WordCount & msidbSumInfoSourceTypeSFN)
374 path = build_directory_name( 3, p, f->SourceShortPath, NULL );
375 else
376 path = build_directory_name( 3, p, f->SourceLongPath, NULL );
378 TRACE("source -> %s\n", debugstr_w(path));
379 f->ResolvedSource = strdupW( path );
381 msi_free(p);
383 return path;
386 /* wrapper to resist a need for a full rewrite right now */
387 DWORD deformat_string(MSIPACKAGE *package, LPCWSTR ptr, WCHAR** data )
389 if (ptr)
391 MSIRECORD *rec = MSI_CreateRecord(1);
392 DWORD size = 0;
394 MSI_RecordSetStringW(rec,0,ptr);
395 MSI_FormatRecordW(package,rec,NULL,&size);
397 size++;
398 *data = msi_alloc(size*sizeof(WCHAR));
399 if (size > 1)
400 MSI_FormatRecordW(package,rec,*data,&size);
401 else
402 *data[0] = 0;
404 msiobj_release( &rec->hdr );
405 return sizeof(WCHAR)*size;
408 *data = NULL;
409 return 0;
412 UINT schedule_action(MSIPACKAGE *package, UINT script, LPCWSTR action)
414 UINT count;
415 LPWSTR *newbuf = NULL;
416 if (script >= TOTAL_SCRIPTS)
418 FIXME("Unknown script requested %i\n",script);
419 return ERROR_FUNCTION_FAILED;
421 TRACE("Scheduling Action %s in script %i\n",debugstr_w(action), script);
423 count = package->script->ActionCount[script];
424 package->script->ActionCount[script]++;
425 if (count != 0)
426 newbuf = msi_realloc( package->script->Actions[script],
427 package->script->ActionCount[script]* sizeof(LPWSTR));
428 else
429 newbuf = msi_alloc( sizeof(LPWSTR));
431 newbuf[count] = strdupW(action);
432 package->script->Actions[script] = newbuf;
434 return ERROR_SUCCESS;
437 void msi_free_action_script(MSIPACKAGE *package, UINT script)
439 UINT i;
440 for (i = 0; i < package->script->ActionCount[script]; i++)
441 msi_free(package->script->Actions[script][i]);
443 msi_free(package->script->Actions[script]);
444 package->script->Actions[script] = NULL;
445 package->script->ActionCount[script] = 0;
448 static void remove_tracked_tempfiles(MSIPACKAGE* package)
450 struct list *item, *cursor;
452 LIST_FOR_EACH_SAFE( item, cursor, &package->tempfiles )
454 MSITEMPFILE *temp = LIST_ENTRY( item, MSITEMPFILE, entry );
456 list_remove( &temp->entry );
457 TRACE("deleting temp file %s\n", debugstr_w( temp->Path ));
458 if (!DeleteFileW( temp->Path ))
459 ERR("failed to delete %s\n", debugstr_w( temp->Path ));
460 msi_free( temp->Path );
461 msi_free( temp );
465 static void free_feature( MSIFEATURE *feature )
467 struct list *item, *cursor;
469 LIST_FOR_EACH_SAFE( item, cursor, &feature->Children )
471 FeatureList *fl = LIST_ENTRY( item, FeatureList, entry );
472 list_remove( &fl->entry );
473 msi_free( fl );
476 LIST_FOR_EACH_SAFE( item, cursor, &feature->Components )
478 ComponentList *cl = LIST_ENTRY( item, ComponentList, entry );
479 list_remove( &cl->entry );
480 msi_free( cl );
482 msi_free( feature->Feature );
483 msi_free( feature->Feature_Parent );
484 msi_free( feature->Directory );
485 msi_free( feature->Description );
486 msi_free( feature->Title );
487 msi_free( feature );
490 static void free_extension( MSIEXTENSION *ext )
492 struct list *item, *cursor;
494 LIST_FOR_EACH_SAFE( item, cursor, &ext->verbs )
496 MSIVERB *verb = LIST_ENTRY( item, MSIVERB, entry );
498 list_remove( &verb->entry );
499 msi_free( verb->Verb );
500 msi_free( verb->Command );
501 msi_free( verb->Argument );
502 msi_free( verb );
505 msi_free( ext->Extension );
506 msi_free( ext->ProgIDText );
507 msi_free( ext );
510 /* Called when the package is being closed */
511 void ACTION_free_package_structures( MSIPACKAGE* package)
513 INT i;
514 struct list *item, *cursor;
516 TRACE("Freeing package action data\n");
518 remove_tracked_tempfiles(package);
520 LIST_FOR_EACH_SAFE( item, cursor, &package->features )
522 MSIFEATURE *feature = LIST_ENTRY( item, MSIFEATURE, entry );
523 list_remove( &feature->entry );
524 free_feature( feature );
527 LIST_FOR_EACH_SAFE( item, cursor, &package->folders )
529 MSIFOLDER *folder = LIST_ENTRY( item, MSIFOLDER, entry );
531 list_remove( &folder->entry );
532 msi_free( folder->Parent );
533 msi_free( folder->Directory );
534 msi_free( folder->TargetDefault );
535 msi_free( folder->SourceLongPath );
536 msi_free( folder->SourceShortPath );
537 msi_free( folder->ResolvedTarget );
538 msi_free( folder->ResolvedSource );
539 msi_free( folder->Property );
540 msi_free( folder );
543 LIST_FOR_EACH_SAFE( item, cursor, &package->components )
545 MSICOMPONENT *comp = LIST_ENTRY( item, MSICOMPONENT, entry );
547 list_remove( &comp->entry );
548 msi_free( comp->Component );
549 msi_free( comp->ComponentId );
550 msi_free( comp->Directory );
551 msi_free( comp->Condition );
552 msi_free( comp->KeyPath );
553 msi_free( comp->FullKeypath );
554 msi_free( comp );
557 LIST_FOR_EACH_SAFE( item, cursor, &package->files )
559 MSIFILE *file = LIST_ENTRY( item, MSIFILE, entry );
561 list_remove( &file->entry );
562 msi_free( file->File );
563 msi_free( file->FileName );
564 msi_free( file->ShortName );
565 msi_free( file->LongName );
566 msi_free( file->Version );
567 msi_free( file->Language );
568 msi_free( file->TargetPath );
569 msi_free( file );
572 /* clean up extension, progid, class and verb structures */
573 LIST_FOR_EACH_SAFE( item, cursor, &package->classes )
575 MSICLASS *cls = LIST_ENTRY( item, MSICLASS, entry );
577 list_remove( &cls->entry );
578 msi_free( cls->clsid );
579 msi_free( cls->Context );
580 msi_free( cls->Description );
581 msi_free( cls->FileTypeMask );
582 msi_free( cls->IconPath );
583 msi_free( cls->DefInprocHandler );
584 msi_free( cls->DefInprocHandler32 );
585 msi_free( cls->Argument );
586 msi_free( cls->ProgIDText );
587 msi_free( cls );
590 LIST_FOR_EACH_SAFE( item, cursor, &package->extensions )
592 MSIEXTENSION *ext = LIST_ENTRY( item, MSIEXTENSION, entry );
594 list_remove( &ext->entry );
595 free_extension( ext );
598 LIST_FOR_EACH_SAFE( item, cursor, &package->progids )
600 MSIPROGID *progid = LIST_ENTRY( item, MSIPROGID, entry );
602 list_remove( &progid->entry );
603 msi_free( progid->ProgID );
604 msi_free( progid->Description );
605 msi_free( progid->IconPath );
606 msi_free( progid );
609 LIST_FOR_EACH_SAFE( item, cursor, &package->mimes )
611 MSIMIME *mt = LIST_ENTRY( item, MSIMIME, entry );
613 list_remove( &mt->entry );
614 msi_free( mt->clsid );
615 msi_free( mt->ContentType );
616 msi_free( mt );
619 LIST_FOR_EACH_SAFE( item, cursor, &package->appids )
621 MSIAPPID *appid = LIST_ENTRY( item, MSIAPPID, entry );
623 list_remove( &appid->entry );
624 msi_free( appid->AppID );
625 msi_free( appid->RemoteServerName );
626 msi_free( appid->LocalServer );
627 msi_free( appid->ServiceParameters );
628 msi_free( appid->DllSurrogate );
629 msi_free( appid );
632 LIST_FOR_EACH_SAFE( item, cursor, &package->sourcelist_info )
634 MSISOURCELISTINFO *info = LIST_ENTRY( item, MSISOURCELISTINFO, entry );
636 list_remove( &info->entry );
637 msi_free( info->value );
638 msi_free( info );
641 LIST_FOR_EACH_SAFE( item, cursor, &package->sourcelist_media )
643 MSIMEDIADISK *info = LIST_ENTRY( item, MSIMEDIADISK, entry );
645 list_remove( &info->entry );
646 msi_free( info->volume_label );
647 msi_free( info->disk_prompt );
648 msi_free( info );
651 if (package->script)
653 for (i = 0; i < TOTAL_SCRIPTS; i++)
654 msi_free_action_script(package, i);
656 for (i = 0; i < package->script->UniqueActionsCount; i++)
657 msi_free(package->script->UniqueActions[i]);
659 msi_free(package->script->UniqueActions);
660 msi_free(package->script);
663 msi_free(package->BaseURL);
664 msi_free(package->PackagePath);
665 msi_free(package->ProductCode);
666 msi_free(package->ActionFormat);
667 msi_free(package->LastAction);
669 /* cleanup control event subscriptions */
670 ControlEvent_CleanupSubscriptions(package);
674 * build_directory_name()
676 * This function is to save messing round with directory names
677 * It handles adding backslashes between path segments,
678 * and can add \ at the end of the directory name if told to.
680 * It takes a variable number of arguments.
681 * It always allocates a new string for the result, so make sure
682 * to free the return value when finished with it.
684 * The first arg is the number of path segments that follow.
685 * The arguments following count are a list of path segments.
686 * A path segment may be NULL.
688 * Path segments will be added with a \ separating them.
689 * A \ will not be added after the last segment, however if the
690 * last segment is NULL, then the last character will be a \
693 LPWSTR build_directory_name(DWORD count, ...)
695 DWORD sz = 1, i;
696 LPWSTR dir;
697 va_list va;
699 va_start(va,count);
700 for(i=0; i<count; i++)
702 LPCWSTR str = va_arg(va,LPCWSTR);
703 if (str)
704 sz += strlenW(str) + 1;
706 va_end(va);
708 dir = msi_alloc(sz*sizeof(WCHAR));
709 dir[0]=0;
711 va_start(va,count);
712 for(i=0; i<count; i++)
714 LPCWSTR str = va_arg(va,LPCWSTR);
715 if (!str)
716 continue;
717 strcatW(dir, str);
718 if( ((i+1)!=count) && dir[strlenW(dir)-1]!='\\')
719 strcatW(dir, cszbs);
721 return dir;
724 /***********************************************************************
725 * create_full_pathW
727 * Recursively create all directories in the path.
729 * shamelessly stolen from setupapi/queue.c
731 BOOL create_full_pathW(const WCHAR *path)
733 BOOL ret = TRUE;
734 int len;
735 WCHAR *new_path;
737 new_path = msi_alloc( (strlenW(path) + 1) * sizeof(WCHAR));
739 strcpyW(new_path, path);
741 while((len = strlenW(new_path)) && new_path[len - 1] == '\\')
742 new_path[len - 1] = 0;
744 while(!CreateDirectoryW(new_path, NULL))
746 WCHAR *slash;
747 DWORD last_error = GetLastError();
748 if(last_error == ERROR_ALREADY_EXISTS)
749 break;
751 if(last_error != ERROR_PATH_NOT_FOUND)
753 ret = FALSE;
754 break;
757 if(!(slash = strrchrW(new_path, '\\')))
759 ret = FALSE;
760 break;
763 len = slash - new_path;
764 new_path[len] = 0;
765 if(!create_full_pathW(new_path))
767 ret = FALSE;
768 break;
770 new_path[len] = '\\';
773 msi_free(new_path);
774 return ret;
777 void ui_progress(MSIPACKAGE *package, int a, int b, int c, int d )
779 MSIRECORD * row;
781 row = MSI_CreateRecord(4);
782 MSI_RecordSetInteger(row,1,a);
783 MSI_RecordSetInteger(row,2,b);
784 MSI_RecordSetInteger(row,3,c);
785 MSI_RecordSetInteger(row,4,d);
786 MSI_ProcessMessage(package, INSTALLMESSAGE_PROGRESS, row);
787 msiobj_release(&row->hdr);
789 msi_dialog_check_messages(NULL);
792 void ui_actiondata(MSIPACKAGE *package, LPCWSTR action, MSIRECORD * record)
794 static const WCHAR Query_t[] =
795 {'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
796 '`','A','c','t','i','o', 'n','T','e','x','t','`',' ',
797 'W','H','E','R','E',' ', '`','A','c','t','i','o','n','`',' ','=',
798 ' ','\'','%','s','\'',0};
799 WCHAR message[1024];
800 MSIRECORD * row = 0;
801 DWORD size;
803 if (!package->LastAction || strcmpW(package->LastAction,action))
805 row = MSI_QueryGetRecord(package->db, Query_t, action);
806 if (!row)
807 return;
809 if (MSI_RecordIsNull(row,3))
811 msiobj_release(&row->hdr);
812 return;
815 /* update the cached actionformat */
816 msi_free(package->ActionFormat);
817 package->ActionFormat = msi_dup_record_field(row,3);
819 msi_free(package->LastAction);
820 package->LastAction = strdupW(action);
822 msiobj_release(&row->hdr);
825 MSI_RecordSetStringW(record,0,package->ActionFormat);
826 size = 1024;
827 MSI_FormatRecordW(package,record,message,&size);
829 row = MSI_CreateRecord(1);
830 MSI_RecordSetStringW(row,1,message);
832 MSI_ProcessMessage(package, INSTALLMESSAGE_ACTIONDATA, row);
834 msiobj_release(&row->hdr);
837 BOOL ACTION_VerifyComponentForAction( const MSICOMPONENT* comp, INSTALLSTATE check )
839 if (!comp)
840 return FALSE;
842 if (comp->ActionRequest == check)
843 return TRUE;
844 else
845 return FALSE;
848 BOOL ACTION_VerifyFeatureForAction( const MSIFEATURE* feature, INSTALLSTATE check )
850 if (!feature)
851 return FALSE;
853 if (feature->ActionRequest == check)
854 return TRUE;
855 else
856 return FALSE;
859 void reduce_to_longfilename(WCHAR* filename)
861 LPWSTR p = strchrW(filename,'|');
862 if (p)
863 memmove(filename, p+1, (strlenW(p+1)+1)*sizeof(WCHAR));
866 void reduce_to_shortfilename(WCHAR* filename)
868 LPWSTR p = strchrW(filename,'|');
869 if (p)
870 *p = 0;
873 LPWSTR create_component_advertise_string(MSIPACKAGE* package,
874 MSICOMPONENT* component, LPCWSTR feature)
876 static const WCHAR fmt[] = {'%','s','%','s','%','c','%','s',0};
877 WCHAR productid_85[21], component_85[21];
878 LPWSTR output = NULL;
879 DWORD sz = 0;
880 GUID clsid;
882 /* > is used if there is a component GUID and < if not. */
884 productid_85[0] = 0;
885 component_85[0] = 0;
887 CLSIDFromString(package->ProductCode, &clsid);
888 encode_base85_guid(&clsid, productid_85);
890 if (component)
892 CLSIDFromString(component->ComponentId, &clsid);
893 encode_base85_guid(&clsid, component_85);
896 TRACE("prod=%s feat=%s comp=%s\n", debugstr_w(productid_85),
897 debugstr_w(feature), debugstr_w(component_85));
899 sz = 20 + lstrlenW(feature) + 20 + 3;
901 output = msi_alloc_zero(sz*sizeof(WCHAR));
903 sprintfW(output, fmt, productid_85, feature,
904 component?'>':'<', component_85);
906 return output;
909 /* update component state based on a feature change */
910 void ACTION_UpdateComponentStates(MSIPACKAGE *package, LPCWSTR szFeature)
912 INSTALLSTATE newstate;
913 MSIFEATURE *feature;
914 ComponentList *cl;
916 feature = get_loaded_feature(package,szFeature);
917 if (!feature)
918 return;
920 newstate = feature->ActionRequest;
922 if (newstate == INSTALLSTATE_ABSENT)
923 newstate = INSTALLSTATE_UNKNOWN;
925 LIST_FOR_EACH_ENTRY( cl, &feature->Components, ComponentList, entry )
927 MSICOMPONENT* component = cl->component;
929 TRACE("MODIFYING(%i): Component %s (Installed %i, Action %i, Request %i)\n",
930 newstate, debugstr_w(component->Component), component->Installed,
931 component->Action, component->ActionRequest);
933 if (!component->Enabled)
934 continue;
936 if (newstate == INSTALLSTATE_LOCAL)
937 msi_component_set_state(package, component, INSTALLSTATE_LOCAL);
938 else
940 ComponentList *clist;
941 MSIFEATURE *f;
943 component->hasLocalFeature = FALSE;
945 msi_component_set_state(package, component, newstate);
947 /*if any other feature wants is local we need to set it local*/
948 LIST_FOR_EACH_ENTRY( f, &package->features, MSIFEATURE, entry )
950 if ( f->ActionRequest != INSTALLSTATE_LOCAL &&
951 f->ActionRequest != INSTALLSTATE_SOURCE )
953 continue;
956 LIST_FOR_EACH_ENTRY( clist, &f->Components, ComponentList, entry )
958 if ( clist->component == component &&
959 (f->ActionRequest == INSTALLSTATE_LOCAL ||
960 f->ActionRequest == INSTALLSTATE_SOURCE) )
962 TRACE("Saved by %s\n", debugstr_w(f->Feature));
963 component->hasLocalFeature = TRUE;
965 if (component->Attributes & msidbComponentAttributesOptional)
967 if (f->Attributes & msidbFeatureAttributesFavorSource)
968 msi_component_set_state(package, component, INSTALLSTATE_SOURCE);
969 else
970 msi_component_set_state(package, component, INSTALLSTATE_LOCAL);
972 else if (component->Attributes & msidbComponentAttributesSourceOnly)
973 msi_component_set_state(package, component, INSTALLSTATE_SOURCE);
974 else
975 msi_component_set_state(package, component, INSTALLSTATE_LOCAL);
980 TRACE("Result (%i): Component %s (Installed %i, Action %i, Request %i)\n",
981 newstate, debugstr_w(component->Component), component->Installed,
982 component->Action, component->ActionRequest);
986 UINT register_unique_action(MSIPACKAGE *package, LPCWSTR action)
988 UINT count;
989 LPWSTR *newbuf = NULL;
991 if (!package->script)
992 return FALSE;
994 TRACE("Registering Action %s as having fun\n",debugstr_w(action));
996 count = package->script->UniqueActionsCount;
997 package->script->UniqueActionsCount++;
998 if (count != 0)
999 newbuf = msi_realloc( package->script->UniqueActions,
1000 package->script->UniqueActionsCount* sizeof(LPWSTR));
1001 else
1002 newbuf = msi_alloc( sizeof(LPWSTR));
1004 newbuf[count] = strdupW(action);
1005 package->script->UniqueActions = newbuf;
1007 return ERROR_SUCCESS;
1010 BOOL check_unique_action(const MSIPACKAGE *package, LPCWSTR action)
1012 UINT i;
1014 if (!package->script)
1015 return FALSE;
1017 for (i = 0; i < package->script->UniqueActionsCount; i++)
1018 if (!strcmpW(package->script->UniqueActions[i],action))
1019 return TRUE;
1021 return FALSE;
1024 WCHAR* generate_error_string(MSIPACKAGE *package, UINT error, DWORD count, ... )
1026 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};
1028 MSIRECORD *rec;
1029 MSIRECORD *row;
1030 DWORD size = 0;
1031 DWORD i;
1032 va_list va;
1033 LPCWSTR str;
1034 LPWSTR data;
1036 row = MSI_QueryGetRecord(package->db, query, error);
1037 if (!row)
1038 return 0;
1040 rec = MSI_CreateRecord(count+2);
1042 str = MSI_RecordGetString(row,1);
1043 MSI_RecordSetStringW(rec,0,str);
1044 msiobj_release( &row->hdr );
1045 MSI_RecordSetInteger(rec,1,error);
1047 va_start(va,count);
1048 for (i = 0; i < count; i++)
1050 str = va_arg(va,LPCWSTR);
1051 MSI_RecordSetStringW(rec,(i+2),str);
1053 va_end(va);
1055 MSI_FormatRecordW(package,rec,NULL,&size);
1057 size++;
1058 data = msi_alloc(size*sizeof(WCHAR));
1059 if (size > 1)
1060 MSI_FormatRecordW(package,rec,data,&size);
1061 else
1062 data[0] = 0;
1063 msiobj_release( &rec->hdr );
1064 return data;
1067 void msi_ui_error( DWORD msg_id, DWORD type )
1069 WCHAR text[2048];
1071 static const WCHAR title[] = {
1072 'W','i','n','d','o','w','s',' ','I','n','s','t','a','l','l','e','r',0
1075 if (!MsiLoadStringW( -1, msg_id, text, sizeof(text) / sizeof(text[0]),
1076 MAKELANGID(LANG_NEUTRAL, SUBLANG_NEUTRAL) ))
1077 return;
1079 MessageBoxW( NULL, text, title, type );