push decde5eed3d79f9d889b4d757f73e86ce6ff9241
[wine/hacks.git] / dlls / msi / helpers.c
blobaba3bd94624ba4e2d60c33344e155d54881e38fc
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 "winbase.h"
30 #include "winerror.h"
31 #include "wine/debug.h"
32 #include "msipriv.h"
33 #include "winuser.h"
34 #include "winreg.h"
35 #include "shlwapi.h"
36 #include "wine/unicode.h"
37 #include "msidefs.h"
39 WINE_DEFAULT_DEBUG_CHANNEL(msi);
41 static const WCHAR cszTargetDir[] = {'T','A','R','G','E','T','D','I','R',0};
42 static const WCHAR cszDatabase[]={'D','A','T','A','B','A','S','E',0};
44 const WCHAR cszSourceDir[] = {'S','o','u','r','c','e','D','i','r',0};
45 const WCHAR cszSOURCEDIR[] = {'S','O','U','R','C','E','D','I','R',0};
46 const WCHAR cszRootDrive[] = {'R','O','O','T','D','R','I','V','E',0};
47 const WCHAR cszbs[]={'\\',0};
49 LPWSTR build_icon_path(MSIPACKAGE *package, LPCWSTR icon_name )
51 LPWSTR SystemFolder, dest, FilePath;
53 static const WCHAR szInstaller[] =
54 {'M','i','c','r','o','s','o','f','t','\\',
55 'I','n','s','t','a','l','l','e','r','\\',0};
56 static const WCHAR szFolder[] =
57 {'A','p','p','D','a','t','a','F','o','l','d','e','r',0};
59 SystemFolder = msi_dup_property( package, szFolder );
61 dest = build_directory_name(3, SystemFolder, szInstaller, package->ProductCode);
63 create_full_pathW(dest);
65 FilePath = build_directory_name(2, dest, icon_name);
67 msi_free(SystemFolder);
68 msi_free(dest);
69 return FilePath;
72 LPWSTR msi_dup_record_field( MSIRECORD *rec, INT field )
74 DWORD sz = 0;
75 LPWSTR str;
76 UINT r;
78 if (MSI_RecordIsNull( rec, field ))
79 return NULL;
81 r = MSI_RecordGetStringW( rec, field, NULL, &sz );
82 if (r != ERROR_SUCCESS)
83 return NULL;
85 sz ++;
86 str = msi_alloc( sz * sizeof (WCHAR) );
87 if (!str)
88 return str;
89 str[0] = 0;
90 r = MSI_RecordGetStringW( rec, field, str, &sz );
91 if (r != ERROR_SUCCESS)
93 ERR("failed to get string!\n");
94 msi_free( str );
95 return NULL;
97 return str;
100 MSICOMPONENT* get_loaded_component( MSIPACKAGE* package, LPCWSTR Component )
102 MSICOMPONENT *comp;
104 LIST_FOR_EACH_ENTRY( comp, &package->components, MSICOMPONENT, entry )
106 if (lstrcmpW(Component,comp->Component)==0)
107 return comp;
109 return NULL;
112 MSIFEATURE* get_loaded_feature(MSIPACKAGE* package, LPCWSTR Feature )
114 MSIFEATURE *feature;
116 LIST_FOR_EACH_ENTRY( feature, &package->features, MSIFEATURE, entry )
118 if (lstrcmpW( Feature, feature->Feature )==0)
119 return feature;
121 return NULL;
124 MSIFILE* get_loaded_file( MSIPACKAGE* package, LPCWSTR key )
126 MSIFILE *file;
128 LIST_FOR_EACH_ENTRY( file, &package->files, MSIFILE, entry )
130 if (lstrcmpW( key, file->File )==0)
131 return file;
133 return NULL;
136 int track_tempfile( MSIPACKAGE *package, LPCWSTR path )
138 MSITEMPFILE *temp;
140 TRACE("%s\n", debugstr_w(path));
142 LIST_FOR_EACH_ENTRY( temp, &package->tempfiles, MSITEMPFILE, entry )
143 if (!lstrcmpW( path, temp->Path ))
144 return 0;
146 temp = msi_alloc_zero( sizeof (MSITEMPFILE) );
147 if (!temp)
148 return -1;
150 list_add_head( &package->tempfiles, &temp->entry );
151 temp->Path = strdupW( path );
153 return 0;
156 MSIFOLDER *get_loaded_folder( MSIPACKAGE *package, LPCWSTR dir )
158 MSIFOLDER *folder;
160 LIST_FOR_EACH_ENTRY( folder, &package->folders, MSIFOLDER, entry )
162 if (lstrcmpW( dir, folder->Directory )==0)
163 return folder;
165 return NULL;
168 static LPWSTR get_source_root( MSIPACKAGE *package )
170 LPWSTR path, p;
172 path = msi_dup_property( package, cszSourceDir );
173 if (path)
174 return path;
176 path = msi_dup_property( package, cszDatabase );
177 if (path)
179 p = strrchrW(path,'\\');
180 if (p)
181 *(p+1) = 0;
183 return path;
187 * clean_spaces_from_path()
189 * removes spaces from the beginning and end of path segments
190 * removes multiple \\ characters
192 static void clean_spaces_from_path( LPWSTR p )
194 LPWSTR q = p;
195 int n, len = 0;
197 while (1)
199 /* copy until the end of the string or a space */
200 while (*p != ' ' && (*q = *p))
202 p++, len++;
203 /* reduce many backslashes to one */
204 if (*p != '\\' || *q != '\\')
205 q++;
208 /* quit at the end of the string */
209 if (!*p)
210 break;
212 /* count the number of spaces */
213 n = 0;
214 while (p[n] == ' ')
215 n++;
217 /* if it's leading or trailing space, skip it */
218 if ( len == 0 || p[-1] == '\\' || p[n] == '\\' )
219 p += n;
220 else /* copy n spaces */
221 while (n && (*q++ = *p++)) n--;
225 LPWSTR resolve_folder(MSIPACKAGE *package, LPCWSTR name, BOOL source,
226 BOOL set_prop, BOOL load_prop, MSIFOLDER **folder)
228 MSIFOLDER *f;
229 LPWSTR p, path = NULL, parent;
231 TRACE("Working to resolve %s\n",debugstr_w(name));
233 if (!name)
234 return NULL;
236 if (!lstrcmpW(name,cszSourceDir))
237 name = cszTargetDir;
239 f = get_loaded_folder( package, name );
240 if (!f)
241 return NULL;
243 /* special resolving for Target and Source root dir */
244 if (!strcmpW(name,cszTargetDir))
246 if (!f->ResolvedTarget && !f->Property)
248 LPWSTR check_path;
249 check_path = msi_dup_property( package, cszTargetDir );
250 if (!check_path)
252 check_path = msi_dup_property( package, cszRootDrive );
253 if (set_prop)
254 MSI_SetPropertyW(package,cszTargetDir,check_path);
257 /* correct misbuilt target dir */
258 path = build_directory_name(2, check_path, NULL);
259 clean_spaces_from_path( path );
260 if (strcmpiW(path,check_path)!=0)
261 MSI_SetPropertyW(package,cszTargetDir,path);
262 msi_free(check_path);
264 f->ResolvedTarget = path;
267 if (!f->ResolvedSource)
268 f->ResolvedSource = get_source_root( package );
271 if (folder)
272 *folder = f;
274 if (!source && f->ResolvedTarget)
276 path = strdupW( f->ResolvedTarget );
277 TRACE(" already resolved to %s\n",debugstr_w(path));
278 return path;
281 if (source && f->ResolvedSource)
283 path = strdupW( f->ResolvedSource );
284 TRACE(" (source)already resolved to %s\n",debugstr_w(path));
285 return path;
288 if (!source && f->Property)
290 path = build_directory_name( 2, f->Property, NULL );
292 TRACE(" internally set to %s\n",debugstr_w(path));
293 if (set_prop)
294 MSI_SetPropertyW( package, name, path );
295 return path;
298 if (!source && load_prop && (path = msi_dup_property( package, name )))
300 f->ResolvedTarget = strdupW( path );
301 TRACE(" property set to %s\n", debugstr_w(path));
302 return path;
305 if (!f->Parent)
306 return path;
308 parent = f->Parent;
310 TRACE(" ! Parent is %s\n", debugstr_w(parent));
312 p = resolve_folder(package, parent, source, set_prop, load_prop, NULL);
313 if (!source)
315 TRACE(" TargetDefault = %s\n", debugstr_w(f->TargetDefault));
317 path = build_directory_name( 3, p, f->TargetDefault, NULL );
318 clean_spaces_from_path( path );
319 f->ResolvedTarget = strdupW( path );
320 TRACE("target -> %s\n", debugstr_w(path));
321 if (set_prop)
322 MSI_SetPropertyW(package,name,path);
324 else
326 path = NULL;
328 if (package->WordCount & msidbSumInfoSourceTypeCompressed)
329 path = get_source_root( package );
330 else if (package->WordCount & msidbSumInfoSourceTypeSFN)
331 path = build_directory_name( 3, p, f->SourceShortPath, NULL );
332 else
333 path = build_directory_name( 3, p, f->SourceLongPath, NULL );
335 TRACE("source -> %s\n", debugstr_w(path));
336 f->ResolvedSource = strdupW( path );
338 msi_free(p);
340 return path;
343 /* wrapper to resist a need for a full rewrite right now */
344 DWORD deformat_string(MSIPACKAGE *package, LPCWSTR ptr, WCHAR** data )
346 if (ptr)
348 MSIRECORD *rec = MSI_CreateRecord(1);
349 DWORD size = 0;
351 MSI_RecordSetStringW(rec,0,ptr);
352 MSI_FormatRecordW(package,rec,NULL,&size);
354 size++;
355 *data = msi_alloc(size*sizeof(WCHAR));
356 if (size > 1)
357 MSI_FormatRecordW(package,rec,*data,&size);
358 else
359 *data[0] = 0;
361 msiobj_release( &rec->hdr );
362 return sizeof(WCHAR)*size;
365 *data = NULL;
366 return 0;
369 UINT schedule_action(MSIPACKAGE *package, UINT script, LPCWSTR action)
371 UINT count;
372 LPWSTR *newbuf = NULL;
373 if (script >= TOTAL_SCRIPTS)
375 FIXME("Unknown script requested %i\n",script);
376 return ERROR_FUNCTION_FAILED;
378 TRACE("Scheduling Action %s in script %i\n",debugstr_w(action), script);
380 count = package->script->ActionCount[script];
381 package->script->ActionCount[script]++;
382 if (count != 0)
383 newbuf = msi_realloc( package->script->Actions[script],
384 package->script->ActionCount[script]* sizeof(LPWSTR));
385 else
386 newbuf = msi_alloc( sizeof(LPWSTR));
388 newbuf[count] = strdupW(action);
389 package->script->Actions[script] = newbuf;
391 return ERROR_SUCCESS;
394 void msi_free_action_script(MSIPACKAGE *package, UINT script)
396 int i;
397 for (i = 0; i < package->script->ActionCount[script]; i++)
398 msi_free(package->script->Actions[script][i]);
400 msi_free(package->script->Actions[script]);
401 package->script->Actions[script] = NULL;
402 package->script->ActionCount[script] = 0;
405 static void remove_tracked_tempfiles(MSIPACKAGE* package)
407 struct list *item, *cursor;
409 LIST_FOR_EACH_SAFE( item, cursor, &package->tempfiles )
411 MSITEMPFILE *temp = LIST_ENTRY( item, MSITEMPFILE, entry );
413 list_remove( &temp->entry );
414 TRACE("deleting temp file %s\n", debugstr_w( temp->Path ));
415 if (!DeleteFileW( temp->Path ))
416 ERR("failed to delete %s\n", debugstr_w( temp->Path ));
417 msi_free( temp->Path );
418 msi_free( temp );
422 static void free_feature( MSIFEATURE *feature )
424 struct list *item, *cursor;
426 LIST_FOR_EACH_SAFE( item, cursor, &feature->Children )
428 FeatureList *fl = LIST_ENTRY( item, FeatureList, entry );
429 list_remove( &fl->entry );
430 msi_free( fl );
433 LIST_FOR_EACH_SAFE( item, cursor, &feature->Components )
435 ComponentList *cl = LIST_ENTRY( item, ComponentList, entry );
436 list_remove( &cl->entry );
437 msi_free( cl );
439 msi_free( feature->Feature );
440 msi_free( feature->Feature_Parent );
441 msi_free( feature->Directory );
442 msi_free( feature->Description );
443 msi_free( feature->Title );
444 msi_free( feature );
447 static void free_extension( MSIEXTENSION *ext )
449 struct list *item, *cursor;
451 LIST_FOR_EACH_SAFE( item, cursor, &ext->verbs )
453 MSIVERB *verb = LIST_ENTRY( item, MSIVERB, entry );
455 list_remove( &verb->entry );
456 msi_free( verb->Verb );
457 msi_free( verb->Command );
458 msi_free( verb->Argument );
459 msi_free( verb );
462 msi_free( ext->Extension );
463 msi_free( ext->ProgIDText );
464 msi_free( ext );
467 /* Called when the package is being closed */
468 void ACTION_free_package_structures( MSIPACKAGE* package)
470 INT i;
471 struct list *item, *cursor;
473 TRACE("Freeing package action data\n");
475 remove_tracked_tempfiles(package);
477 LIST_FOR_EACH_SAFE( item, cursor, &package->features )
479 MSIFEATURE *feature = LIST_ENTRY( item, MSIFEATURE, entry );
480 list_remove( &feature->entry );
481 free_feature( feature );
484 LIST_FOR_EACH_SAFE( item, cursor, &package->folders )
486 MSIFOLDER *folder = LIST_ENTRY( item, MSIFOLDER, entry );
488 list_remove( &folder->entry );
489 msi_free( folder->Parent );
490 msi_free( folder->Directory );
491 msi_free( folder->TargetDefault );
492 msi_free( folder->SourceLongPath );
493 msi_free( folder->SourceShortPath );
494 msi_free( folder->ResolvedTarget );
495 msi_free( folder->ResolvedSource );
496 msi_free( folder->Property );
497 msi_free( folder );
500 LIST_FOR_EACH_SAFE( item, cursor, &package->components )
502 MSICOMPONENT *comp = LIST_ENTRY( item, MSICOMPONENT, entry );
504 list_remove( &comp->entry );
505 msi_free( comp->Component );
506 msi_free( comp->ComponentId );
507 msi_free( comp->Directory );
508 msi_free( comp->Condition );
509 msi_free( comp->KeyPath );
510 msi_free( comp->FullKeypath );
511 msi_free( comp );
514 LIST_FOR_EACH_SAFE( item, cursor, &package->files )
516 MSIFILE *file = LIST_ENTRY( item, MSIFILE, entry );
518 list_remove( &file->entry );
519 msi_free( file->File );
520 msi_free( file->FileName );
521 msi_free( file->ShortName );
522 msi_free( file->LongName );
523 msi_free( file->Version );
524 msi_free( file->Language );
525 msi_free( file->SourcePath );
526 msi_free( file->TargetPath );
527 msi_free( file );
530 /* clean up extension, progid, class and verb structures */
531 LIST_FOR_EACH_SAFE( item, cursor, &package->classes )
533 MSICLASS *cls = LIST_ENTRY( item, MSICLASS, entry );
535 list_remove( &cls->entry );
536 msi_free( cls->clsid );
537 msi_free( cls->Context );
538 msi_free( cls->Description );
539 msi_free( cls->FileTypeMask );
540 msi_free( cls->IconPath );
541 msi_free( cls->DefInprocHandler );
542 msi_free( cls->DefInprocHandler32 );
543 msi_free( cls->Argument );
544 msi_free( cls->ProgIDText );
545 msi_free( cls );
548 LIST_FOR_EACH_SAFE( item, cursor, &package->extensions )
550 MSIEXTENSION *ext = LIST_ENTRY( item, MSIEXTENSION, entry );
552 list_remove( &ext->entry );
553 free_extension( ext );
556 LIST_FOR_EACH_SAFE( item, cursor, &package->progids )
558 MSIPROGID *progid = LIST_ENTRY( item, MSIPROGID, entry );
560 list_remove( &progid->entry );
561 msi_free( progid->ProgID );
562 msi_free( progid->Description );
563 msi_free( progid->IconPath );
564 msi_free( progid );
567 LIST_FOR_EACH_SAFE( item, cursor, &package->mimes )
569 MSIMIME *mt = LIST_ENTRY( item, MSIMIME, entry );
571 list_remove( &mt->entry );
572 msi_free( mt->clsid );
573 msi_free( mt->ContentType );
574 msi_free( mt );
577 LIST_FOR_EACH_SAFE( item, cursor, &package->appids )
579 MSIAPPID *appid = LIST_ENTRY( item, MSIAPPID, entry );
581 list_remove( &appid->entry );
582 msi_free( appid->AppID );
583 msi_free( appid->RemoteServerName );
584 msi_free( appid->LocalServer );
585 msi_free( appid->ServiceParameters );
586 msi_free( appid->DllSurrogate );
587 msi_free( appid );
590 LIST_FOR_EACH_SAFE( item, cursor, &package->sourcelist_info )
592 MSISOURCELISTINFO *info = LIST_ENTRY( item, MSISOURCELISTINFO, entry );
594 list_remove( &info->entry );
595 msi_free( info->value );
596 msi_free( info );
599 LIST_FOR_EACH_SAFE( item, cursor, &package->sourcelist_media )
601 MSIMEDIADISK *info = LIST_ENTRY( item, MSIMEDIADISK, entry );
603 list_remove( &info->entry );
604 msi_free( info->volume_label );
605 msi_free( info->disk_prompt );
606 msi_free( info );
609 if (package->script)
611 for (i = 0; i < TOTAL_SCRIPTS; i++)
612 msi_free_action_script(package, i);
614 for (i = 0; i < package->script->UniqueActionsCount; i++)
615 msi_free(package->script->UniqueActions[i]);
617 msi_free(package->script->UniqueActions);
618 msi_free(package->script);
621 msi_free(package->BaseURL);
622 msi_free(package->PackagePath);
623 msi_free(package->ProductCode);
624 msi_free(package->ActionFormat);
625 msi_free(package->LastAction);
627 /* cleanup control event subscriptions */
628 ControlEvent_CleanupSubscriptions(package);
632 * build_directory_name()
634 * This function is to save messing round with directory names
635 * It handles adding backslashes between path segments,
636 * and can add \ at the end of the directory name if told to.
638 * It takes a variable number of arguments.
639 * It always allocates a new string for the result, so make sure
640 * to free the return value when finished with it.
642 * The first arg is the number of path segments that follow.
643 * The arguments following count are a list of path segments.
644 * A path segment may be NULL.
646 * Path segments will be added with a \ separating them.
647 * A \ will not be added after the last segment, however if the
648 * last segment is NULL, then the last character will be a \
651 LPWSTR build_directory_name(DWORD count, ...)
653 DWORD sz = 1, i;
654 LPWSTR dir;
655 va_list va;
657 va_start(va,count);
658 for(i=0; i<count; i++)
660 LPCWSTR str = va_arg(va,LPCWSTR);
661 if (str)
662 sz += strlenW(str) + 1;
664 va_end(va);
666 dir = msi_alloc(sz*sizeof(WCHAR));
667 dir[0]=0;
669 va_start(va,count);
670 for(i=0; i<count; i++)
672 LPCWSTR str = va_arg(va,LPCWSTR);
673 if (!str)
674 continue;
675 strcatW(dir, str);
676 if( ((i+1)!=count) && dir[strlenW(dir)-1]!='\\')
677 strcatW(dir, cszbs);
679 return dir;
682 /***********************************************************************
683 * create_full_pathW
685 * Recursively create all directories in the path.
687 * shamelessly stolen from setupapi/queue.c
689 BOOL create_full_pathW(const WCHAR *path)
691 BOOL ret = TRUE;
692 int len;
693 WCHAR *new_path;
695 new_path = msi_alloc( (strlenW(path) + 1) * sizeof(WCHAR));
697 strcpyW(new_path, path);
699 while((len = strlenW(new_path)) && new_path[len - 1] == '\\')
700 new_path[len - 1] = 0;
702 while(!CreateDirectoryW(new_path, NULL))
704 WCHAR *slash;
705 DWORD last_error = GetLastError();
706 if(last_error == ERROR_ALREADY_EXISTS)
707 break;
709 if(last_error != ERROR_PATH_NOT_FOUND)
711 ret = FALSE;
712 break;
715 if(!(slash = strrchrW(new_path, '\\')))
717 ret = FALSE;
718 break;
721 len = slash - new_path;
722 new_path[len] = 0;
723 if(!create_full_pathW(new_path))
725 ret = FALSE;
726 break;
728 new_path[len] = '\\';
731 msi_free(new_path);
732 return ret;
735 void ui_progress(MSIPACKAGE *package, int a, int b, int c, int d )
737 MSIRECORD * row;
739 row = MSI_CreateRecord(4);
740 MSI_RecordSetInteger(row,1,a);
741 MSI_RecordSetInteger(row,2,b);
742 MSI_RecordSetInteger(row,3,c);
743 MSI_RecordSetInteger(row,4,d);
744 MSI_ProcessMessage(package, INSTALLMESSAGE_PROGRESS, row);
745 msiobj_release(&row->hdr);
747 msi_dialog_check_messages(NULL);
750 void ui_actiondata(MSIPACKAGE *package, LPCWSTR action, MSIRECORD * record)
752 static const WCHAR Query_t[] =
753 {'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
754 '`','A','c','t','i','o', 'n','T','e','x','t','`',' ',
755 'W','H','E','R','E',' ', '`','A','c','t','i','o','n','`',' ','=',
756 ' ','\'','%','s','\'',0};
757 WCHAR message[1024];
758 MSIRECORD * row = 0;
759 DWORD size;
761 if (!package->LastAction || strcmpW(package->LastAction,action))
763 row = MSI_QueryGetRecord(package->db, Query_t, action);
764 if (!row)
765 return;
767 if (MSI_RecordIsNull(row,3))
769 msiobj_release(&row->hdr);
770 return;
773 /* update the cached actionformat */
774 msi_free(package->ActionFormat);
775 package->ActionFormat = msi_dup_record_field(row,3);
777 msi_free(package->LastAction);
778 package->LastAction = strdupW(action);
780 msiobj_release(&row->hdr);
783 MSI_RecordSetStringW(record,0,package->ActionFormat);
784 size = 1024;
785 MSI_FormatRecordW(package,record,message,&size);
787 row = MSI_CreateRecord(1);
788 MSI_RecordSetStringW(row,1,message);
790 MSI_ProcessMessage(package, INSTALLMESSAGE_ACTIONDATA, row);
792 msiobj_release(&row->hdr);
795 BOOL ACTION_VerifyComponentForAction( const MSICOMPONENT* comp, INSTALLSTATE check )
797 if (!comp)
798 return FALSE;
800 if (comp->Installed == check)
801 return FALSE;
803 if (comp->ActionRequest == check)
804 return TRUE;
805 else
806 return FALSE;
809 BOOL ACTION_VerifyFeatureForAction( const MSIFEATURE* feature, INSTALLSTATE check )
811 if (!feature)
812 return FALSE;
814 if (feature->ActionRequest == check)
815 return TRUE;
816 else
817 return FALSE;
820 void reduce_to_longfilename(WCHAR* filename)
822 LPWSTR p = strchrW(filename,'|');
823 if (p)
824 memmove(filename, p+1, (strlenW(p+1)+1)*sizeof(WCHAR));
827 void reduce_to_shortfilename(WCHAR* filename)
829 LPWSTR p = strchrW(filename,'|');
830 if (p)
831 *p = 0;
834 LPWSTR create_component_advertise_string(MSIPACKAGE* package,
835 MSICOMPONENT* component, LPCWSTR feature)
837 static const WCHAR fmt[] = {'%','s','%','s','%','c','%','s',0};
838 WCHAR productid_85[21], component_85[21];
839 LPWSTR output = NULL;
840 DWORD sz = 0;
841 GUID clsid;
843 /* > is used if there is a component GUID and < if not. */
845 productid_85[0] = 0;
846 component_85[0] = 0;
848 CLSIDFromString(package->ProductCode, &clsid);
849 encode_base85_guid(&clsid, productid_85);
851 if (component)
853 CLSIDFromString(component->ComponentId, &clsid);
854 encode_base85_guid(&clsid, component_85);
857 TRACE("prod=%s feat=%s comp=%s\n", debugstr_w(productid_85),
858 debugstr_w(feature), debugstr_w(component_85));
860 sz = 20 + lstrlenW(feature) + 20 + 3;
862 output = msi_alloc_zero(sz*sizeof(WCHAR));
864 sprintfW(output, fmt, productid_85, feature,
865 component?'>':'<', component_85);
867 return output;
870 /* update component state based on a feature change */
871 void ACTION_UpdateComponentStates(MSIPACKAGE *package, LPCWSTR szFeature)
873 INSTALLSTATE newstate;
874 MSIFEATURE *feature;
875 ComponentList *cl;
877 feature = get_loaded_feature(package,szFeature);
878 if (!feature)
879 return;
881 newstate = feature->ActionRequest;
883 if (newstate == INSTALLSTATE_ABSENT)
884 newstate = INSTALLSTATE_UNKNOWN;
886 LIST_FOR_EACH_ENTRY( cl, &feature->Components, ComponentList, entry )
888 MSICOMPONENT* component = cl->component;
890 TRACE("MODIFYING(%i): Component %s (Installed %i, Action %i, Request %i)\n",
891 newstate, debugstr_w(component->Component), component->Installed,
892 component->Action, component->ActionRequest);
894 if (!component->Enabled)
895 continue;
897 if (newstate == INSTALLSTATE_LOCAL)
898 msi_component_set_state( component, INSTALLSTATE_LOCAL );
899 else
901 ComponentList *clist;
902 MSIFEATURE *f;
904 component->hasLocalFeature = FALSE;
906 msi_component_set_state( component, newstate );
908 /*if any other feature wants is local we need to set it local*/
909 LIST_FOR_EACH_ENTRY( f, &package->features, MSIFEATURE, entry )
911 if ( f->ActionRequest != INSTALLSTATE_LOCAL &&
912 f->ActionRequest != INSTALLSTATE_SOURCE )
914 continue;
917 LIST_FOR_EACH_ENTRY( clist, &f->Components, ComponentList, entry )
919 if ( clist->component == component &&
920 (f->ActionRequest == INSTALLSTATE_LOCAL ||
921 f->ActionRequest == INSTALLSTATE_SOURCE) )
923 TRACE("Saved by %s\n", debugstr_w(f->Feature));
924 component->hasLocalFeature = TRUE;
926 if (component->Attributes & msidbComponentAttributesOptional)
928 if (f->Attributes & msidbFeatureAttributesFavorSource)
929 msi_component_set_state( component, INSTALLSTATE_SOURCE );
930 else
931 msi_component_set_state( component, INSTALLSTATE_LOCAL );
933 else if (component->Attributes & msidbComponentAttributesSourceOnly)
934 msi_component_set_state( component, INSTALLSTATE_SOURCE );
935 else
936 msi_component_set_state( component, INSTALLSTATE_LOCAL );
941 TRACE("Result (%i): Component %s (Installed %i, Action %i, Request %i)\n",
942 newstate, debugstr_w(component->Component), component->Installed,
943 component->Action, component->ActionRequest);
947 UINT register_unique_action(MSIPACKAGE *package, LPCWSTR action)
949 UINT count;
950 LPWSTR *newbuf = NULL;
952 if (!package->script)
953 return FALSE;
955 TRACE("Registering Action %s as having fun\n",debugstr_w(action));
957 count = package->script->UniqueActionsCount;
958 package->script->UniqueActionsCount++;
959 if (count != 0)
960 newbuf = msi_realloc( package->script->UniqueActions,
961 package->script->UniqueActionsCount* sizeof(LPWSTR));
962 else
963 newbuf = msi_alloc( sizeof(LPWSTR));
965 newbuf[count] = strdupW(action);
966 package->script->UniqueActions = newbuf;
968 return ERROR_SUCCESS;
971 BOOL check_unique_action(const MSIPACKAGE *package, LPCWSTR action)
973 INT i;
975 if (!package->script)
976 return FALSE;
978 for (i = 0; i < package->script->UniqueActionsCount; i++)
979 if (!strcmpW(package->script->UniqueActions[i],action))
980 return TRUE;
982 return FALSE;
985 WCHAR* generate_error_string(MSIPACKAGE *package, UINT error, DWORD count, ... )
987 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};
989 MSIRECORD *rec;
990 MSIRECORD *row;
991 DWORD size = 0;
992 DWORD i;
993 va_list va;
994 LPCWSTR str;
995 LPWSTR data;
997 row = MSI_QueryGetRecord(package->db, query, error);
998 if (!row)
999 return 0;
1001 rec = MSI_CreateRecord(count+2);
1003 str = MSI_RecordGetString(row,1);
1004 MSI_RecordSetStringW(rec,0,str);
1005 msiobj_release( &row->hdr );
1006 MSI_RecordSetInteger(rec,1,error);
1008 va_start(va,count);
1009 for (i = 0; i < count; i++)
1011 str = va_arg(va,LPCWSTR);
1012 MSI_RecordSetStringW(rec,(i+2),str);
1014 va_end(va);
1016 MSI_FormatRecordW(package,rec,NULL,&size);
1018 size++;
1019 data = msi_alloc(size*sizeof(WCHAR));
1020 if (size > 1)
1021 MSI_FormatRecordW(package,rec,data,&size);
1022 else
1023 data[0] = 0;
1024 msiobj_release( &rec->hdr );
1025 return data;
1028 void msi_ui_error( DWORD msg_id, DWORD type )
1030 WCHAR text[2048];
1032 static const WCHAR title[] = {
1033 'W','i','n','d','o','w','s',' ','I','n','s','t','a','l','l','e','r',0
1036 if (!MsiLoadStringW( -1, msg_id, text, sizeof(text) / sizeof(text[0]),
1037 MAKELANGID(LANG_NEUTRAL, SUBLANG_NEUTRAL) ))
1038 return;
1040 MessageBoxW( NULL, text, title, type );
1043 typedef struct
1045 MSIPACKAGE *package;
1046 MSIMEDIAINFO *mi;
1047 MSIFILE *file;
1048 LPWSTR destination;
1049 } CabData;
1051 static INT_PTR cabinet_notify(FDINOTIFICATIONTYPE fdint, PFDINOTIFICATION pfdin)
1053 TRACE("(%d)\n", fdint);
1055 switch (fdint)
1057 case fdintNEXT_CABINET:
1059 ERR("continuous cabinets not handled\n");
1060 return 0;
1063 case fdintCOPY_FILE:
1065 CabData *data = (CabData*) pfdin->pv;
1066 LPWSTR file, path;
1067 DWORD attrs, size;
1068 HANDLE handle;
1069 MSIFILE *f;
1071 file = strdupAtoW(pfdin->psz1);
1072 f = get_loaded_file(data->package, file);
1073 msi_free(file);
1075 if (!f)
1077 WARN("unknown file in cabinet (%s)\n",debugstr_a(pfdin->psz1));
1078 return 0;
1081 if (lstrcmpW(f->File, data->file->File))
1082 return 0;
1084 size = lstrlenW(data->destination) + lstrlenW(data->file->FileName) + 2;
1085 path = msi_alloc(size * sizeof(WCHAR));
1086 lstrcpyW(path, data->destination);
1087 PathAddBackslashW(path);
1088 lstrcatW(path, data->file->FileName);
1090 TRACE("extracting %s\n", debugstr_w(path));
1092 attrs = f->Attributes & (FILE_ATTRIBUTE_READONLY|FILE_ATTRIBUTE_HIDDEN|FILE_ATTRIBUTE_SYSTEM);
1093 if (!attrs) attrs = FILE_ATTRIBUTE_NORMAL;
1095 handle = CreateFileW(path, GENERIC_READ | GENERIC_WRITE, 0,
1096 NULL, CREATE_ALWAYS, attrs, NULL);
1097 if (handle == INVALID_HANDLE_VALUE)
1099 if (GetFileAttributesW(path) == INVALID_FILE_ATTRIBUTES)
1100 ERR("failed to create %s (error %d)\n",
1101 debugstr_w(path), GetLastError());
1103 msi_free(path);
1104 return 0;
1107 msi_free(path);
1108 return (INT_PTR)handle;
1111 case fdintCLOSE_FILE_INFO:
1113 FILETIME ft;
1114 FILETIME ftLocal;
1115 HANDLE handle = (HANDLE)pfdin->hf;
1117 if (!DosDateTimeToFileTime(pfdin->date, pfdin->time, &ft))
1118 return -1;
1119 if (!LocalFileTimeToFileTime(&ft, &ftLocal))
1120 return -1;
1121 if (!SetFileTime(handle, &ftLocal, 0, &ftLocal))
1122 return -1;
1123 CloseHandle(handle);
1124 return 1;
1127 default:
1128 return 0;
1132 UINT msi_extract_file(MSIPACKAGE *package, MSIFILE *file, LPWSTR destdir)
1134 MSIMEDIAINFO *mi;
1135 CabData data;
1136 UINT r;
1138 mi = msi_alloc_zero(sizeof(MSIMEDIAINFO));
1139 if (!mi)
1140 return ERROR_OUTOFMEMORY;
1142 r = msi_load_media_info(package, file, mi);
1143 if (r != ERROR_SUCCESS)
1144 goto done;
1146 if (GetFileAttributesW(mi->source) == INVALID_FILE_ATTRIBUTES)
1148 r = find_published_source(package, mi);
1149 if (r != ERROR_SUCCESS)
1151 ERR("Cabinet not found: %s\n", debugstr_w(mi->source));
1152 return ERROR_INSTALL_FAILURE;
1156 data.package = package;
1157 data.mi = mi;
1158 data.file = file;
1159 data.destination = destdir;
1161 if (!msi_cabextract(package, mi, cabinet_notify, &data))
1163 ERR("Failed to extract cabinet file\n");
1164 r = ERROR_FUNCTION_FAILED;
1167 done:
1168 msi_free_media_info(mi);
1169 return r;