includes: Fix alignment for 64-bits
[wine/wine64.git] / dlls / msi / helpers.c
blobea36b3d5f9c62de5b1962595496f45d4e1df2fa5
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};
44 const WCHAR szLocalSid[] = {'S','-','1','-','5','-','1','8',0};
46 LPWSTR build_icon_path(MSIPACKAGE *package, LPCWSTR icon_name )
48 LPWSTR SystemFolder, dest, FilePath;
50 static const WCHAR szInstaller[] =
51 {'M','i','c','r','o','s','o','f','t','\\',
52 'I','n','s','t','a','l','l','e','r','\\',0};
53 static const WCHAR szFolder[] =
54 {'A','p','p','D','a','t','a','F','o','l','d','e','r',0};
56 SystemFolder = msi_dup_property( package, szFolder );
58 dest = build_directory_name(3, SystemFolder, szInstaller, package->ProductCode);
60 create_full_pathW(dest);
62 FilePath = build_directory_name(2, dest, icon_name);
64 msi_free(SystemFolder);
65 msi_free(dest);
66 return FilePath;
69 LPWSTR msi_dup_record_field( MSIRECORD *rec, INT field )
71 DWORD sz = 0;
72 LPWSTR str;
73 UINT r;
75 if (MSI_RecordIsNull( rec, field ))
76 return NULL;
78 r = MSI_RecordGetStringW( rec, field, NULL, &sz );
79 if (r != ERROR_SUCCESS)
80 return NULL;
82 sz ++;
83 str = msi_alloc( sz * sizeof (WCHAR) );
84 if (!str)
85 return str;
86 str[0] = 0;
87 r = MSI_RecordGetStringW( rec, field, str, &sz );
88 if (r != ERROR_SUCCESS)
90 ERR("failed to get string!\n");
91 msi_free( str );
92 return NULL;
94 return str;
97 MSICOMPONENT* get_loaded_component( MSIPACKAGE* package, LPCWSTR Component )
99 MSICOMPONENT *comp;
101 LIST_FOR_EACH_ENTRY( comp, &package->components, MSICOMPONENT, entry )
103 if (lstrcmpW(Component,comp->Component)==0)
104 return comp;
106 return NULL;
109 MSIFEATURE* get_loaded_feature(MSIPACKAGE* package, LPCWSTR Feature )
111 MSIFEATURE *feature;
113 LIST_FOR_EACH_ENTRY( feature, &package->features, MSIFEATURE, entry )
115 if (lstrcmpW( Feature, feature->Feature )==0)
116 return feature;
118 return NULL;
121 MSIFILE* get_loaded_file( MSIPACKAGE* package, LPCWSTR key )
123 MSIFILE *file;
125 LIST_FOR_EACH_ENTRY( file, &package->files, MSIFILE, entry )
127 if (lstrcmpW( key, file->File )==0)
128 return file;
130 return NULL;
133 int track_tempfile( MSIPACKAGE *package, LPCWSTR path )
135 MSITEMPFILE *temp;
137 TRACE("%s\n", debugstr_w(path));
139 LIST_FOR_EACH_ENTRY( temp, &package->tempfiles, MSITEMPFILE, entry )
140 if (!lstrcmpW( path, temp->Path ))
141 return 0;
143 temp = msi_alloc_zero( sizeof (MSITEMPFILE) );
144 if (!temp)
145 return -1;
147 list_add_head( &package->tempfiles, &temp->entry );
148 temp->Path = strdupW( path );
150 return 0;
153 MSIFOLDER *get_loaded_folder( MSIPACKAGE *package, LPCWSTR dir )
155 MSIFOLDER *folder;
157 LIST_FOR_EACH_ENTRY( folder, &package->folders, MSIFOLDER, entry )
159 if (lstrcmpW( dir, folder->Directory )==0)
160 return folder;
162 return NULL;
165 void msi_reset_folders( MSIPACKAGE *package, BOOL source )
167 MSIFOLDER *folder;
169 LIST_FOR_EACH_ENTRY( folder, &package->folders, MSIFOLDER, entry )
171 if ( source )
173 msi_free( folder->ResolvedSource );
174 folder->ResolvedSource = NULL;
176 else
178 msi_free( folder->ResolvedTarget );
179 folder->ResolvedTarget = NULL;
184 static LPWSTR get_source_root( MSIPACKAGE *package )
186 LPWSTR path, p;
188 path = msi_dup_property( package, cszSourceDir );
189 if (path)
190 return path;
192 path = msi_dup_property( package, cszDatabase );
193 if (path)
195 p = strrchrW(path,'\\');
196 if (p)
197 *(p+1) = 0;
199 return path;
203 * clean_spaces_from_path()
205 * removes spaces from the beginning and end of path segments
206 * removes multiple \\ characters
208 static void clean_spaces_from_path( LPWSTR p )
210 LPWSTR q = p;
211 int n, len = 0;
213 while (1)
215 /* copy until the end of the string or a space */
216 while (*p != ' ' && (*q = *p))
218 p++, len++;
219 /* reduce many backslashes to one */
220 if (*p != '\\' || *q != '\\')
221 q++;
224 /* quit at the end of the string */
225 if (!*p)
226 break;
228 /* count the number of spaces */
229 n = 0;
230 while (p[n] == ' ')
231 n++;
233 /* if it's leading or trailing space, skip it */
234 if ( len == 0 || p[-1] == '\\' || p[n] == '\\' )
235 p += n;
236 else /* copy n spaces */
237 while (n && (*q++ = *p++)) n--;
241 LPWSTR resolve_file_source(MSIPACKAGE *package, MSIFILE *file)
243 LPWSTR p, path;
245 TRACE("Working to resolve source of file %s\n", debugstr_w(file->File));
247 if (file->IsCompressed)
248 return NULL;
250 p = resolve_folder(package, file->Component->Directory,
251 TRUE, FALSE, TRUE, NULL);
252 path = build_directory_name(2, p, file->ShortName);
254 if (file->LongName &&
255 GetFileAttributesW(path) == INVALID_FILE_ATTRIBUTES)
257 msi_free(path);
258 path = build_directory_name(2, p, file->LongName);
261 msi_free(p);
263 TRACE("file %s source resolves to %s\n", debugstr_w(file->File),
264 debugstr_w(path));
266 return path;
269 LPWSTR resolve_folder(MSIPACKAGE *package, LPCWSTR name, BOOL source,
270 BOOL set_prop, BOOL load_prop, MSIFOLDER **folder)
272 MSIFOLDER *f;
273 LPWSTR p, path = NULL, parent;
275 TRACE("Working to resolve %s\n",debugstr_w(name));
277 if (!name)
278 return NULL;
280 if (!lstrcmpW(name,cszSourceDir))
281 name = cszTargetDir;
283 f = get_loaded_folder( package, name );
284 if (!f)
285 return NULL;
287 /* special resolving for Target and Source root dir */
288 if (!strcmpW(name,cszTargetDir))
290 if (!f->ResolvedTarget && !f->Property)
292 LPWSTR check_path;
293 check_path = msi_dup_property( package, cszTargetDir );
294 if (!check_path)
296 check_path = msi_dup_property( package, cszRootDrive );
297 if (set_prop)
298 MSI_SetPropertyW(package,cszTargetDir,check_path);
301 /* correct misbuilt target dir */
302 path = build_directory_name(2, check_path, NULL);
303 clean_spaces_from_path( path );
304 if (strcmpiW(path,check_path)!=0)
305 MSI_SetPropertyW(package,cszTargetDir,path);
306 msi_free(check_path);
308 f->ResolvedTarget = path;
311 if (!f->ResolvedSource)
312 f->ResolvedSource = get_source_root( package );
315 if (folder)
316 *folder = f;
318 if (!source && f->ResolvedTarget)
320 path = strdupW( f->ResolvedTarget );
321 TRACE(" already resolved to %s\n",debugstr_w(path));
322 return path;
325 if (source && f->ResolvedSource)
327 path = strdupW( f->ResolvedSource );
328 TRACE(" (source)already resolved to %s\n",debugstr_w(path));
329 return path;
332 if (!source && f->Property)
334 path = build_directory_name( 2, f->Property, NULL );
336 TRACE(" internally set to %s\n",debugstr_w(path));
337 if (set_prop)
338 MSI_SetPropertyW( package, name, path );
339 return path;
342 if (!source && load_prop && (path = msi_dup_property( package, name )))
344 f->ResolvedTarget = strdupW( path );
345 TRACE(" property set to %s\n", debugstr_w(path));
346 return path;
349 if (!f->Parent)
350 return path;
352 parent = f->Parent;
354 TRACE(" ! Parent is %s\n", debugstr_w(parent));
356 p = resolve_folder(package, parent, source, set_prop, load_prop, NULL);
357 if (!source)
359 TRACE(" TargetDefault = %s\n", debugstr_w(f->TargetDefault));
361 path = build_directory_name( 3, p, f->TargetDefault, NULL );
362 clean_spaces_from_path( path );
363 f->ResolvedTarget = strdupW( path );
364 TRACE("target -> %s\n", debugstr_w(path));
365 if (set_prop)
366 MSI_SetPropertyW(package,name,path);
368 else
370 path = NULL;
372 if (package->WordCount & msidbSumInfoSourceTypeCompressed)
373 path = get_source_root( package );
374 else if (package->WordCount & msidbSumInfoSourceTypeSFN)
375 path = build_directory_name( 3, p, f->SourceShortPath, NULL );
376 else
377 path = build_directory_name( 3, p, f->SourceLongPath, NULL );
379 TRACE("source -> %s\n", debugstr_w(path));
380 f->ResolvedSource = strdupW( path );
382 msi_free(p);
384 return path;
387 /* wrapper to resist a need for a full rewrite right now */
388 DWORD deformat_string(MSIPACKAGE *package, LPCWSTR ptr, WCHAR** data )
390 if (ptr)
392 MSIRECORD *rec = MSI_CreateRecord(1);
393 DWORD size = 0;
395 MSI_RecordSetStringW(rec,0,ptr);
396 MSI_FormatRecordW(package,rec,NULL,&size);
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;
405 msiobj_release( &rec->hdr );
406 return sizeof(WCHAR)*size;
409 *data = NULL;
410 return 0;
413 UINT schedule_action(MSIPACKAGE *package, UINT script, LPCWSTR action)
415 UINT count;
416 LPWSTR *newbuf = NULL;
417 if (script >= TOTAL_SCRIPTS)
419 FIXME("Unknown script requested %i\n",script);
420 return ERROR_FUNCTION_FAILED;
422 TRACE("Scheduling Action %s in script %i\n",debugstr_w(action), script);
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));
432 newbuf[count] = strdupW(action);
433 package->script->Actions[script] = newbuf;
435 return ERROR_SUCCESS;
438 void msi_free_action_script(MSIPACKAGE *package, UINT script)
440 UINT i;
441 for (i = 0; i < package->script->ActionCount[script]; i++)
442 msi_free(package->script->Actions[script][i]);
444 msi_free(package->script->Actions[script]);
445 package->script->Actions[script] = NULL;
446 package->script->ActionCount[script] = 0;
449 static void remove_tracked_tempfiles(MSIPACKAGE* package)
451 struct list *item, *cursor;
453 LIST_FOR_EACH_SAFE( item, cursor, &package->tempfiles )
455 MSITEMPFILE *temp = LIST_ENTRY( item, MSITEMPFILE, entry );
457 list_remove( &temp->entry );
458 TRACE("deleting temp file %s\n", debugstr_w( temp->Path ));
459 if (!DeleteFileW( temp->Path ))
460 ERR("failed to delete %s\n", debugstr_w( temp->Path ));
461 msi_free( temp->Path );
462 msi_free( temp );
466 static void free_feature( MSIFEATURE *feature )
468 struct list *item, *cursor;
470 LIST_FOR_EACH_SAFE( item, cursor, &feature->Children )
472 FeatureList *fl = LIST_ENTRY( item, FeatureList, entry );
473 list_remove( &fl->entry );
474 msi_free( fl );
477 LIST_FOR_EACH_SAFE( item, cursor, &feature->Components )
479 ComponentList *cl = LIST_ENTRY( item, ComponentList, entry );
480 list_remove( &cl->entry );
481 msi_free( cl );
483 msi_free( feature->Feature );
484 msi_free( feature->Feature_Parent );
485 msi_free( feature->Directory );
486 msi_free( feature->Description );
487 msi_free( feature->Title );
488 msi_free( feature );
491 static void free_extension( MSIEXTENSION *ext )
493 struct list *item, *cursor;
495 LIST_FOR_EACH_SAFE( item, cursor, &ext->verbs )
497 MSIVERB *verb = LIST_ENTRY( item, MSIVERB, entry );
499 list_remove( &verb->entry );
500 msi_free( verb->Verb );
501 msi_free( verb->Command );
502 msi_free( verb->Argument );
503 msi_free( verb );
506 msi_free( ext->Extension );
507 msi_free( ext->ProgIDText );
508 msi_free( ext );
511 /* Called when the package is being closed */
512 void ACTION_free_package_structures( MSIPACKAGE* package)
514 INT i;
515 struct list *item, *cursor;
517 TRACE("Freeing package action data\n");
519 remove_tracked_tempfiles(package);
521 LIST_FOR_EACH_SAFE( item, cursor, &package->features )
523 MSIFEATURE *feature = LIST_ENTRY( item, MSIFEATURE, entry );
524 list_remove( &feature->entry );
525 free_feature( feature );
528 LIST_FOR_EACH_SAFE( item, cursor, &package->folders )
530 MSIFOLDER *folder = LIST_ENTRY( item, MSIFOLDER, entry );
532 list_remove( &folder->entry );
533 msi_free( folder->Parent );
534 msi_free( folder->Directory );
535 msi_free( folder->TargetDefault );
536 msi_free( folder->SourceLongPath );
537 msi_free( folder->SourceShortPath );
538 msi_free( folder->ResolvedTarget );
539 msi_free( folder->ResolvedSource );
540 msi_free( folder->Property );
541 msi_free( folder );
544 LIST_FOR_EACH_SAFE( item, cursor, &package->components )
546 MSICOMPONENT *comp = LIST_ENTRY( item, MSICOMPONENT, entry );
548 list_remove( &comp->entry );
549 msi_free( comp->Component );
550 msi_free( comp->ComponentId );
551 msi_free( comp->Directory );
552 msi_free( comp->Condition );
553 msi_free( comp->KeyPath );
554 msi_free( comp->FullKeypath );
555 msi_free( comp );
558 LIST_FOR_EACH_SAFE( item, cursor, &package->files )
560 MSIFILE *file = LIST_ENTRY( item, MSIFILE, entry );
562 list_remove( &file->entry );
563 msi_free( file->File );
564 msi_free( file->FileName );
565 msi_free( file->ShortName );
566 msi_free( file->LongName );
567 msi_free( file->Version );
568 msi_free( file->Language );
569 msi_free( file->TargetPath );
570 msi_free( file );
573 /* clean up extension, progid, class and verb structures */
574 LIST_FOR_EACH_SAFE( item, cursor, &package->classes )
576 MSICLASS *cls = LIST_ENTRY( item, MSICLASS, entry );
578 list_remove( &cls->entry );
579 msi_free( cls->clsid );
580 msi_free( cls->Context );
581 msi_free( cls->Description );
582 msi_free( cls->FileTypeMask );
583 msi_free( cls->IconPath );
584 msi_free( cls->DefInprocHandler );
585 msi_free( cls->DefInprocHandler32 );
586 msi_free( cls->Argument );
587 msi_free( cls->ProgIDText );
588 msi_free( cls );
591 LIST_FOR_EACH_SAFE( item, cursor, &package->extensions )
593 MSIEXTENSION *ext = LIST_ENTRY( item, MSIEXTENSION, entry );
595 list_remove( &ext->entry );
596 free_extension( ext );
599 LIST_FOR_EACH_SAFE( item, cursor, &package->progids )
601 MSIPROGID *progid = LIST_ENTRY( item, MSIPROGID, entry );
603 list_remove( &progid->entry );
604 msi_free( progid->ProgID );
605 msi_free( progid->Description );
606 msi_free( progid->IconPath );
607 msi_free( progid );
610 LIST_FOR_EACH_SAFE( item, cursor, &package->mimes )
612 MSIMIME *mt = LIST_ENTRY( item, MSIMIME, entry );
614 list_remove( &mt->entry );
615 msi_free( mt->clsid );
616 msi_free( mt->ContentType );
617 msi_free( mt );
620 LIST_FOR_EACH_SAFE( item, cursor, &package->appids )
622 MSIAPPID *appid = LIST_ENTRY( item, MSIAPPID, entry );
624 list_remove( &appid->entry );
625 msi_free( appid->AppID );
626 msi_free( appid->RemoteServerName );
627 msi_free( appid->LocalServer );
628 msi_free( appid->ServiceParameters );
629 msi_free( appid->DllSurrogate );
630 msi_free( appid );
633 LIST_FOR_EACH_SAFE( item, cursor, &package->sourcelist_info )
635 MSISOURCELISTINFO *info = LIST_ENTRY( item, MSISOURCELISTINFO, entry );
637 list_remove( &info->entry );
638 msi_free( info->value );
639 msi_free( info );
642 LIST_FOR_EACH_SAFE( item, cursor, &package->sourcelist_media )
644 MSIMEDIADISK *info = LIST_ENTRY( item, MSIMEDIADISK, entry );
646 list_remove( &info->entry );
647 msi_free( info->volume_label );
648 msi_free( info->disk_prompt );
649 msi_free( info );
652 if (package->script)
654 for (i = 0; i < TOTAL_SCRIPTS; i++)
655 msi_free_action_script(package, i);
657 for (i = 0; i < package->script->UniqueActionsCount; i++)
658 msi_free(package->script->UniqueActions[i]);
660 msi_free(package->script->UniqueActions);
661 msi_free(package->script);
664 if (package->patch)
666 msi_free(package->patch->patchcode);
667 msi_free(package->patch->transforms);
668 msi_free(package->patch);
671 msi_free(package->BaseURL);
672 msi_free(package->PackagePath);
673 msi_free(package->ProductCode);
674 msi_free(package->ActionFormat);
675 msi_free(package->LastAction);
677 /* cleanup control event subscriptions */
678 ControlEvent_CleanupSubscriptions(package);
682 * build_directory_name()
684 * This function is to save messing round with directory names
685 * It handles adding backslashes between path segments,
686 * and can add \ at the end of the directory name if told to.
688 * It takes a variable number of arguments.
689 * It always allocates a new string for the result, so make sure
690 * to free the return value when finished with it.
692 * The first arg is the number of path segments that follow.
693 * The arguments following count are a list of path segments.
694 * A path segment may be NULL.
696 * Path segments will be added with a \ separating them.
697 * A \ will not be added after the last segment, however if the
698 * last segment is NULL, then the last character will be a \
701 LPWSTR build_directory_name(DWORD count, ...)
703 DWORD sz = 1, i;
704 LPWSTR dir;
705 va_list va;
707 va_start(va,count);
708 for(i=0; i<count; i++)
710 LPCWSTR str = va_arg(va,LPCWSTR);
711 if (str)
712 sz += strlenW(str) + 1;
714 va_end(va);
716 dir = msi_alloc(sz*sizeof(WCHAR));
717 dir[0]=0;
719 va_start(va,count);
720 for(i=0; i<count; i++)
722 LPCWSTR str = va_arg(va,LPCWSTR);
723 if (!str)
724 continue;
725 strcatW(dir, str);
726 if( ((i+1)!=count) && dir[strlenW(dir)-1]!='\\')
727 strcatW(dir, cszbs);
729 return dir;
732 /***********************************************************************
733 * create_full_pathW
735 * Recursively create all directories in the path.
737 * shamelessly stolen from setupapi/queue.c
739 BOOL create_full_pathW(const WCHAR *path)
741 BOOL ret = TRUE;
742 int len;
743 WCHAR *new_path;
745 new_path = msi_alloc( (strlenW(path) + 1) * sizeof(WCHAR));
747 strcpyW(new_path, path);
749 while((len = strlenW(new_path)) && new_path[len - 1] == '\\')
750 new_path[len - 1] = 0;
752 while(!CreateDirectoryW(new_path, NULL))
754 WCHAR *slash;
755 DWORD last_error = GetLastError();
756 if(last_error == ERROR_ALREADY_EXISTS)
757 break;
759 if(last_error != ERROR_PATH_NOT_FOUND)
761 ret = FALSE;
762 break;
765 if(!(slash = strrchrW(new_path, '\\')))
767 ret = FALSE;
768 break;
771 len = slash - new_path;
772 new_path[len] = 0;
773 if(!create_full_pathW(new_path))
775 ret = FALSE;
776 break;
778 new_path[len] = '\\';
781 msi_free(new_path);
782 return ret;
785 void ui_progress(MSIPACKAGE *package, int a, int b, int c, int d )
787 MSIRECORD * row;
789 row = MSI_CreateRecord(4);
790 MSI_RecordSetInteger(row,1,a);
791 MSI_RecordSetInteger(row,2,b);
792 MSI_RecordSetInteger(row,3,c);
793 MSI_RecordSetInteger(row,4,d);
794 MSI_ProcessMessage(package, INSTALLMESSAGE_PROGRESS, row);
795 msiobj_release(&row->hdr);
797 msi_dialog_check_messages(NULL);
800 void ui_actiondata(MSIPACKAGE *package, LPCWSTR action, MSIRECORD * record)
802 static const WCHAR Query_t[] =
803 {'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
804 '`','A','c','t','i','o', 'n','T','e','x','t','`',' ',
805 'W','H','E','R','E',' ', '`','A','c','t','i','o','n','`',' ','=',
806 ' ','\'','%','s','\'',0};
807 WCHAR message[1024];
808 MSIRECORD * row = 0;
809 DWORD size;
811 if (!package->LastAction || strcmpW(package->LastAction,action))
813 row = MSI_QueryGetRecord(package->db, Query_t, action);
814 if (!row)
815 return;
817 if (MSI_RecordIsNull(row,3))
819 msiobj_release(&row->hdr);
820 return;
823 /* update the cached actionformat */
824 msi_free(package->ActionFormat);
825 package->ActionFormat = msi_dup_record_field(row,3);
827 msi_free(package->LastAction);
828 package->LastAction = strdupW(action);
830 msiobj_release(&row->hdr);
833 MSI_RecordSetStringW(record,0,package->ActionFormat);
834 size = 1024;
835 MSI_FormatRecordW(package,record,message,&size);
837 row = MSI_CreateRecord(1);
838 MSI_RecordSetStringW(row,1,message);
840 MSI_ProcessMessage(package, INSTALLMESSAGE_ACTIONDATA, row);
842 msiobj_release(&row->hdr);
845 BOOL ACTION_VerifyComponentForAction( const MSICOMPONENT* comp, INSTALLSTATE check )
847 if (!comp)
848 return FALSE;
850 if (comp->ActionRequest == check)
851 return TRUE;
852 else
853 return FALSE;
856 BOOL ACTION_VerifyFeatureForAction( const MSIFEATURE* feature, INSTALLSTATE check )
858 if (!feature)
859 return FALSE;
861 if (feature->ActionRequest == check)
862 return TRUE;
863 else
864 return FALSE;
867 void reduce_to_longfilename(WCHAR* filename)
869 LPWSTR p = strchrW(filename,'|');
870 if (p)
871 memmove(filename, p+1, (strlenW(p+1)+1)*sizeof(WCHAR));
874 void reduce_to_shortfilename(WCHAR* filename)
876 LPWSTR p = strchrW(filename,'|');
877 if (p)
878 *p = 0;
881 LPWSTR create_component_advertise_string(MSIPACKAGE* package,
882 MSICOMPONENT* component, LPCWSTR feature)
884 static const WCHAR fmt[] = {'%','s','%','s','%','c','%','s',0};
885 WCHAR productid_85[21], component_85[21];
886 LPWSTR output = NULL;
887 DWORD sz = 0;
888 GUID clsid;
890 /* > is used if there is a component GUID and < if not. */
892 productid_85[0] = 0;
893 component_85[0] = 0;
895 CLSIDFromString(package->ProductCode, &clsid);
896 encode_base85_guid(&clsid, productid_85);
898 if (component)
900 CLSIDFromString(component->ComponentId, &clsid);
901 encode_base85_guid(&clsid, component_85);
904 TRACE("prod=%s feat=%s comp=%s\n", debugstr_w(productid_85),
905 debugstr_w(feature), debugstr_w(component_85));
907 sz = 20 + lstrlenW(feature) + 20 + 3;
909 output = msi_alloc_zero(sz*sizeof(WCHAR));
911 sprintfW(output, fmt, productid_85, feature,
912 component?'>':'<', component_85);
914 return output;
917 /* update component state based on a feature change */
918 void ACTION_UpdateComponentStates(MSIPACKAGE *package, LPCWSTR szFeature)
920 INSTALLSTATE newstate;
921 MSIFEATURE *feature;
922 ComponentList *cl;
924 feature = get_loaded_feature(package,szFeature);
925 if (!feature)
926 return;
928 newstate = feature->ActionRequest;
930 if (newstate == INSTALLSTATE_ABSENT)
931 newstate = INSTALLSTATE_UNKNOWN;
933 LIST_FOR_EACH_ENTRY( cl, &feature->Components, ComponentList, entry )
935 MSICOMPONENT* component = cl->component;
937 TRACE("MODIFYING(%i): Component %s (Installed %i, Action %i, Request %i)\n",
938 newstate, debugstr_w(component->Component), component->Installed,
939 component->Action, component->ActionRequest);
941 if (!component->Enabled)
942 continue;
944 if (newstate == INSTALLSTATE_LOCAL)
945 msi_component_set_state(package, component, INSTALLSTATE_LOCAL);
946 else
948 ComponentList *clist;
949 MSIFEATURE *f;
951 component->hasLocalFeature = FALSE;
953 msi_component_set_state(package, component, newstate);
955 /*if any other feature wants is local we need to set it local*/
956 LIST_FOR_EACH_ENTRY( f, &package->features, MSIFEATURE, entry )
958 if ( f->ActionRequest != INSTALLSTATE_LOCAL &&
959 f->ActionRequest != INSTALLSTATE_SOURCE )
961 continue;
964 LIST_FOR_EACH_ENTRY( clist, &f->Components, ComponentList, entry )
966 if ( clist->component == component &&
967 (f->ActionRequest == INSTALLSTATE_LOCAL ||
968 f->ActionRequest == INSTALLSTATE_SOURCE) )
970 TRACE("Saved by %s\n", debugstr_w(f->Feature));
971 component->hasLocalFeature = TRUE;
973 if (component->Attributes & msidbComponentAttributesOptional)
975 if (f->Attributes & msidbFeatureAttributesFavorSource)
976 msi_component_set_state(package, component, INSTALLSTATE_SOURCE);
977 else
978 msi_component_set_state(package, component, INSTALLSTATE_LOCAL);
980 else if (component->Attributes & msidbComponentAttributesSourceOnly)
981 msi_component_set_state(package, component, INSTALLSTATE_SOURCE);
982 else
983 msi_component_set_state(package, component, INSTALLSTATE_LOCAL);
988 TRACE("Result (%i): Component %s (Installed %i, Action %i, Request %i)\n",
989 newstate, debugstr_w(component->Component), component->Installed,
990 component->Action, component->ActionRequest);
994 UINT register_unique_action(MSIPACKAGE *package, LPCWSTR action)
996 UINT count;
997 LPWSTR *newbuf = NULL;
999 if (!package->script)
1000 return FALSE;
1002 TRACE("Registering Action %s as having fun\n",debugstr_w(action));
1004 count = package->script->UniqueActionsCount;
1005 package->script->UniqueActionsCount++;
1006 if (count != 0)
1007 newbuf = msi_realloc( package->script->UniqueActions,
1008 package->script->UniqueActionsCount* sizeof(LPWSTR));
1009 else
1010 newbuf = msi_alloc( sizeof(LPWSTR));
1012 newbuf[count] = strdupW(action);
1013 package->script->UniqueActions = newbuf;
1015 return ERROR_SUCCESS;
1018 BOOL check_unique_action(const MSIPACKAGE *package, LPCWSTR action)
1020 UINT i;
1022 if (!package->script)
1023 return FALSE;
1025 for (i = 0; i < package->script->UniqueActionsCount; i++)
1026 if (!strcmpW(package->script->UniqueActions[i],action))
1027 return TRUE;
1029 return FALSE;
1032 WCHAR* generate_error_string(MSIPACKAGE *package, UINT error, DWORD count, ... )
1034 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};
1036 MSIRECORD *rec;
1037 MSIRECORD *row;
1038 DWORD size = 0;
1039 DWORD i;
1040 va_list va;
1041 LPCWSTR str;
1042 LPWSTR data;
1044 row = MSI_QueryGetRecord(package->db, query, error);
1045 if (!row)
1046 return 0;
1048 rec = MSI_CreateRecord(count+2);
1050 str = MSI_RecordGetString(row,1);
1051 MSI_RecordSetStringW(rec,0,str);
1052 msiobj_release( &row->hdr );
1053 MSI_RecordSetInteger(rec,1,error);
1055 va_start(va,count);
1056 for (i = 0; i < count; i++)
1058 str = va_arg(va,LPCWSTR);
1059 MSI_RecordSetStringW(rec,(i+2),str);
1061 va_end(va);
1063 MSI_FormatRecordW(package,rec,NULL,&size);
1065 size++;
1066 data = msi_alloc(size*sizeof(WCHAR));
1067 if (size > 1)
1068 MSI_FormatRecordW(package,rec,data,&size);
1069 else
1070 data[0] = 0;
1071 msiobj_release( &rec->hdr );
1072 return data;
1075 void msi_ui_error( DWORD msg_id, DWORD type )
1077 WCHAR text[2048];
1079 static const WCHAR title[] = {
1080 'W','i','n','d','o','w','s',' ','I','n','s','t','a','l','l','e','r',0
1083 if (!MsiLoadStringW( -1, msg_id, text, sizeof(text) / sizeof(text[0]),
1084 MAKELANGID(LANG_NEUTRAL, SUBLANG_NEUTRAL) ))
1085 return;
1087 MessageBoxW( NULL, text, title, type );