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 variaty of
23 * actions and functions.
31 #include "wine/debug.h"
34 #include "wine/unicode.h"
37 WINE_DEFAULT_DEBUG_CHANNEL(msi);
39 static const WCHAR cszTargetDir[] = {'T','A','R','G','E','T','D','I','R',0};
40 static const WCHAR cszDatabase[]={'D','A','T','A','B','A','S','E',0};
42 const WCHAR cszSourceDir[] = {'S','o','u','r','c','e','D','i','r',0};
43 const WCHAR cszRootDrive[] = {'R','O','O','T','D','R','I','V','E',0};
44 const WCHAR cszbs[]={'\\',0};
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);
69 LPWSTR msi_dup_record_field( MSIRECORD *row, INT index )
71 return strdupW( MSI_RecordGetString(row,index) );
74 MSICOMPONENT* get_loaded_component( MSIPACKAGE* package, LPCWSTR Component )
78 LIST_FOR_EACH_ENTRY( comp, &package->components, MSICOMPONENT, entry )
80 if (lstrcmpW(Component,comp->Component)==0)
86 MSIFEATURE* get_loaded_feature(MSIPACKAGE* package, LPCWSTR Feature )
90 LIST_FOR_EACH_ENTRY( feature, &package->features, MSIFEATURE, entry )
92 if (lstrcmpW( Feature, feature->Feature )==0)
98 MSIFILE* get_loaded_file( MSIPACKAGE* package, LPCWSTR key )
102 LIST_FOR_EACH_ENTRY( file, &package->files, MSIFILE, entry )
104 if (lstrcmpW( key, file->File )==0)
110 int track_tempfile( MSIPACKAGE *package, LPCWSTR name, LPCWSTR path )
114 LIST_FOR_EACH_ENTRY( temp, &package->tempfiles, MSITEMPFILE, entry )
116 if (lstrcmpW( name, temp->File )==0)
118 TRACE("tempfile %s already exists with path %s\n",
119 debugstr_w(temp->File), debugstr_w(temp->Path));
124 temp = msi_alloc_zero( sizeof (MSITEMPFILE) );
128 list_add_head( &package->tempfiles, &temp->entry );
130 temp->File = strdupW( name );
131 temp->Path = strdupW( path );
133 TRACE("adding tempfile %s with path %s\n",
134 debugstr_w(temp->File), debugstr_w(temp->Path));
139 MSIFOLDER *get_loaded_folder( MSIPACKAGE *package, LPCWSTR dir )
143 LIST_FOR_EACH_ENTRY( folder, &package->folders, MSIFOLDER, entry )
145 if (lstrcmpW( dir, folder->Directory )==0)
151 static LPWSTR get_source_root( MSIPACKAGE *package )
155 path = msi_dup_property( package, cszSourceDir );
159 path = msi_dup_property( package, cszDatabase );
162 p = strrchrW(path,'\\');
170 * clean_spaces_from_path()
172 * removes spaces from the beginning and end of path segments
173 * removes multiple \\ characters
175 static void clean_spaces_from_path( LPWSTR p )
182 /* copy until the end of the string or a space */
183 while (*p != ' ' && (*q = *p))
186 /* reduce many backslashes to one */
187 if (*p != '\\' || *q != '\\')
191 /* quit at the end of the string */
195 /* count the number of spaces */
200 /* if it's leading or trailing space, skip it */
201 if ( len == 0 || p[-1] == '\\' || p[n] == '\\' )
203 else /* copy n spaces */
204 while (n && (*q++ = *p++)) n--;
208 LPWSTR resolve_folder(MSIPACKAGE *package, LPCWSTR name, BOOL source,
209 BOOL set_prop, MSIFOLDER **folder)
212 LPWSTR p, path = NULL;
214 TRACE("Working to resolve %s\n",debugstr_w(name));
219 /* special resolving for Target and Source root dir */
220 if (strcmpW(name,cszTargetDir)==0 || strcmpW(name,cszSourceDir)==0)
225 check_path = msi_dup_property( package, cszTargetDir );
228 check_path = msi_dup_property( package, cszRootDrive );
230 MSI_SetPropertyW(package,cszTargetDir,check_path);
233 /* correct misbuilt target dir */
234 path = build_directory_name(2, check_path, NULL);
235 clean_spaces_from_path( path );
236 if (strcmpiW(path,check_path)!=0)
237 MSI_SetPropertyW(package,cszTargetDir,path);
238 msi_free(check_path);
241 path = get_source_root( package );
243 *folder = get_loaded_folder( package, name );
247 f = get_loaded_folder( package, name );
254 if (!source && f->ResolvedTarget)
256 path = strdupW( f->ResolvedTarget );
257 TRACE(" already resolved to %s\n",debugstr_w(path));
260 else if (source && f->ResolvedSource)
262 path = strdupW( f->ResolvedSource );
263 TRACE(" (source)already resolved to %s\n",debugstr_w(path));
266 else if (!source && f->Property)
268 path = build_directory_name( 2, f->Property, NULL );
270 TRACE(" internally set to %s\n",debugstr_w(path));
272 MSI_SetPropertyW( package, name, path );
278 LPWSTR parent = f->Parent->Directory;
280 TRACE(" ! Parent is %s\n", debugstr_w(parent));
282 p = resolve_folder(package, parent, source, set_prop, NULL);
285 TRACE(" TargetDefault = %s\n", debugstr_w(f->TargetDefault));
287 path = build_directory_name( 3, p, f->TargetDefault, NULL );
288 clean_spaces_from_path( path );
289 f->ResolvedTarget = strdupW( path );
290 TRACE("target -> %s\n", debugstr_w(path));
292 MSI_SetPropertyW(package,name,path);
296 /* source may be in a few different places ... check each of them */
299 /* try the long path directory */
300 if (f->SourceLongPath)
302 path = build_directory_name( 3, p, f->SourceLongPath, NULL );
303 if (INVALID_FILE_ATTRIBUTES == GetFileAttributesW( path ))
310 /* try the short path directory */
311 if (!path && f->SourceShortPath)
313 path = build_directory_name( 3, p, f->SourceShortPath, NULL );
314 if (INVALID_FILE_ATTRIBUTES == GetFileAttributesW( path ))
321 /* try the root of the install */
323 path = get_source_root( package );
325 TRACE("source -> %s\n", debugstr_w(path));
326 f->ResolvedSource = strdupW( path );
333 /* wrapper to resist a need for a full rewrite right now */
334 DWORD deformat_string(MSIPACKAGE *package, LPCWSTR ptr, WCHAR** data )
338 MSIRECORD *rec = MSI_CreateRecord(1);
341 MSI_RecordSetStringW(rec,0,ptr);
342 MSI_FormatRecordW(package,rec,NULL,&size);
346 *data = msi_alloc(size*sizeof(WCHAR));
348 MSI_FormatRecordW(package,rec,*data,&size);
351 msiobj_release( &rec->hdr );
352 return sizeof(WCHAR)*size;
354 msiobj_release( &rec->hdr );
361 UINT schedule_action(MSIPACKAGE *package, UINT script, LPCWSTR action)
364 LPWSTR *newbuf = NULL;
365 if (script >= TOTAL_SCRIPTS)
367 FIXME("Unknown script requested %i\n",script);
368 return ERROR_FUNCTION_FAILED;
370 TRACE("Scheduling Action %s in script %i\n",debugstr_w(action), script);
372 count = package->script->ActionCount[script];
373 package->script->ActionCount[script]++;
375 newbuf = msi_realloc( package->script->Actions[script],
376 package->script->ActionCount[script]* sizeof(LPWSTR));
378 newbuf = msi_alloc( sizeof(LPWSTR));
380 newbuf[count] = strdupW(action);
381 package->script->Actions[script] = newbuf;
383 return ERROR_SUCCESS;
386 static void remove_tracked_tempfiles(MSIPACKAGE* package)
388 struct list *item, *cursor;
390 LIST_FOR_EACH_SAFE( item, cursor, &package->tempfiles )
392 MSITEMPFILE *temp = LIST_ENTRY( item, MSITEMPFILE, entry );
394 list_remove( &temp->entry );
395 TRACE("deleting temp file %s\n", debugstr_w( temp->Path ));
396 DeleteFileW( temp->Path );
397 msi_free( temp->File );
398 msi_free( temp->Path );
403 static void free_feature( MSIFEATURE *feature )
405 struct list *item, *cursor;
407 LIST_FOR_EACH_SAFE( item, cursor, &feature->Components )
409 ComponentList *cl = LIST_ENTRY( item, ComponentList, entry );
410 list_remove( &cl->entry );
413 msi_free( feature->Feature );
414 msi_free( feature->Feature_Parent );
415 msi_free( feature->Directory );
416 msi_free( feature->Description );
417 msi_free( feature->Title );
421 void free_extension( MSIEXTENSION *ext )
423 struct list *item, *cursor;
425 LIST_FOR_EACH_SAFE( item, cursor, &ext->verbs )
427 MSIVERB *verb = LIST_ENTRY( item, MSIVERB, entry );
429 list_remove( &verb->entry );
430 msi_free( verb->Verb );
431 msi_free( verb->Command );
432 msi_free( verb->Argument );
436 msi_free( ext->Extension );
437 msi_free( ext->ProgIDText );
441 /* Called when the package is being closed */
442 void ACTION_free_package_structures( MSIPACKAGE* package)
445 struct list *item, *cursor;
447 TRACE("Freeing package action data\n");
449 remove_tracked_tempfiles(package);
451 LIST_FOR_EACH_SAFE( item, cursor, &package->features )
453 MSIFEATURE *feature = LIST_ENTRY( item, MSIFEATURE, entry );
454 list_remove( &feature->entry );
455 free_feature( feature );
458 LIST_FOR_EACH_SAFE( item, cursor, &package->folders )
460 MSIFOLDER *folder = LIST_ENTRY( item, MSIFOLDER, entry );
462 list_remove( &folder->entry );
463 msi_free( folder->Directory );
464 msi_free( folder->TargetDefault );
465 msi_free( folder->SourceLongPath );
466 msi_free( folder->SourceShortPath );
467 msi_free( folder->ResolvedTarget );
468 msi_free( folder->ResolvedSource );
469 msi_free( folder->Property );
473 LIST_FOR_EACH_SAFE( item, cursor, &package->components )
475 MSICOMPONENT *comp = LIST_ENTRY( item, MSICOMPONENT, entry );
477 list_remove( &comp->entry );
478 msi_free( comp->Component );
479 msi_free( comp->ComponentId );
480 msi_free( comp->Directory );
481 msi_free( comp->Condition );
482 msi_free( comp->KeyPath );
483 msi_free( comp->FullKeypath );
487 LIST_FOR_EACH_SAFE( item, cursor, &package->files )
489 MSIFILE *file = LIST_ENTRY( item, MSIFILE, entry );
491 list_remove( &file->entry );
492 msi_free( file->File );
493 msi_free( file->FileName );
494 msi_free( file->ShortName );
495 msi_free( file->LongName );
496 msi_free( file->Version );
497 msi_free( file->Language );
498 msi_free( file->SourcePath );
499 msi_free( file->TargetPath );
503 /* clean up extension, progid, class and verb structures */
504 LIST_FOR_EACH_SAFE( item, cursor, &package->classes )
506 MSICLASS *cls = LIST_ENTRY( item, MSICLASS, entry );
508 list_remove( &cls->entry );
509 msi_free( cls->clsid );
510 msi_free( cls->Context );
511 msi_free( cls->Description );
512 msi_free( cls->FileTypeMask );
513 msi_free( cls->IconPath );
514 msi_free( cls->DefInprocHandler );
515 msi_free( cls->DefInprocHandler32 );
516 msi_free( cls->Argument );
517 msi_free( cls->ProgIDText );
521 LIST_FOR_EACH_SAFE( item, cursor, &package->extensions )
523 MSIEXTENSION *ext = LIST_ENTRY( item, MSIEXTENSION, entry );
525 list_remove( &ext->entry );
526 free_extension( ext );
529 LIST_FOR_EACH_SAFE( item, cursor, &package->progids )
531 MSIPROGID *progid = LIST_ENTRY( item, MSIPROGID, entry );
533 list_remove( &progid->entry );
534 msi_free( progid->ProgID );
535 msi_free( progid->Description );
536 msi_free( progid->IconPath );
540 LIST_FOR_EACH_SAFE( item, cursor, &package->mimes )
542 MSIMIME *mt = LIST_ENTRY( item, MSIMIME, entry );
544 list_remove( &mt->entry );
545 msi_free( mt->clsid );
546 msi_free( mt->ContentType );
550 LIST_FOR_EACH_SAFE( item, cursor, &package->appids )
552 MSIAPPID *appid = LIST_ENTRY( item, MSIAPPID, entry );
554 list_remove( &appid->entry );
555 msi_free( appid->AppID );
556 msi_free( appid->RemoteServerName );
557 msi_free( appid->LocalServer );
558 msi_free( appid->ServiceParameters );
559 msi_free( appid->DllSurrogate );
565 for (i = 0; i < TOTAL_SCRIPTS; i++)
568 for (j = 0; j < package->script->ActionCount[i]; j++)
569 msi_free(package->script->Actions[i][j]);
571 msi_free(package->script->Actions[i]);
574 for (i = 0; i < package->script->UniqueActionsCount; i++)
575 msi_free(package->script->UniqueActions[i]);
577 msi_free(package->script->UniqueActions);
578 msi_free(package->script);
581 msi_free(package->PackagePath);
582 msi_free(package->ProductCode);
583 msi_free(package->ActionFormat);
584 msi_free(package->LastAction);
586 /* cleanup control event subscriptions */
587 ControlEvent_CleanupSubscriptions(package);
591 * build_directory_name()
593 * This function is to save messing round with directory names
594 * It handles adding backslashes between path segments,
595 * and can add \ at the end of the directory name if told to.
597 * It takes a variable number of arguments.
598 * It always allocates a new string for the result, so make sure
599 * to free the return value when finished with it.
601 * The first arg is the number of path segments that follow.
602 * The arguments following count are a list of path segments.
603 * A path segment may be NULL.
605 * Path segments will be added with a \ separating them.
606 * A \ will not be added after the last segment, however if the
607 * last segment is NULL, then the last character will be a \
610 LPWSTR build_directory_name(DWORD count, ...)
617 for(i=0; i<count; i++)
619 LPCWSTR str = va_arg(va,LPCWSTR);
621 sz += strlenW(str) + 1;
625 dir = msi_alloc(sz*sizeof(WCHAR));
629 for(i=0; i<count; i++)
631 LPCWSTR str = va_arg(va,LPCWSTR);
635 if( ((i+1)!=count) && dir[strlenW(dir)-1]!='\\')
641 /***********************************************************************
644 * Recursively create all directories in the path.
646 * shamelessly stolen from setupapi/queue.c
648 BOOL create_full_pathW(const WCHAR *path)
654 new_path = msi_alloc( (strlenW(path) + 1) * sizeof(WCHAR));
656 strcpyW(new_path, path);
658 while((len = strlenW(new_path)) && new_path[len - 1] == '\\')
659 new_path[len - 1] = 0;
661 while(!CreateDirectoryW(new_path, NULL))
664 DWORD last_error = GetLastError();
665 if(last_error == ERROR_ALREADY_EXISTS)
668 if(last_error != ERROR_PATH_NOT_FOUND)
674 if(!(slash = strrchrW(new_path, '\\')))
680 len = slash - new_path;
682 if(!create_full_pathW(new_path))
687 new_path[len] = '\\';
694 void ui_progress(MSIPACKAGE *package, int a, int b, int c, int d )
698 row = MSI_CreateRecord(4);
699 MSI_RecordSetInteger(row,1,a);
700 MSI_RecordSetInteger(row,2,b);
701 MSI_RecordSetInteger(row,3,c);
702 MSI_RecordSetInteger(row,4,d);
703 MSI_ProcessMessage(package, INSTALLMESSAGE_PROGRESS, row);
704 msiobj_release(&row->hdr);
706 msi_dialog_check_messages(NULL);
709 void ui_actiondata(MSIPACKAGE *package, LPCWSTR action, MSIRECORD * record)
711 static const WCHAR Query_t[] =
712 {'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
713 '`','A','c','t','i','o', 'n','T','e','x','t','`',' ',
714 'W','H','E','R','E',' ', '`','A','c','t','i','o','n','`',' ','=',
715 ' ','\'','%','s','\'',0};
720 if (!package->LastAction || strcmpW(package->LastAction,action))
722 row = MSI_QueryGetRecord(package->db, Query_t, action);
726 if (MSI_RecordIsNull(row,3))
728 msiobj_release(&row->hdr);
732 /* update the cached actionformat */
733 msi_free(package->ActionFormat);
734 package->ActionFormat = msi_dup_record_field(row,3);
736 msi_free(package->LastAction);
737 package->LastAction = strdupW(action);
739 msiobj_release(&row->hdr);
742 MSI_RecordSetStringW(record,0,package->ActionFormat);
744 MSI_FormatRecordW(package,record,message,&size);
746 row = MSI_CreateRecord(1);
747 MSI_RecordSetStringW(row,1,message);
749 MSI_ProcessMessage(package, INSTALLMESSAGE_ACTIONDATA, row);
751 msiobj_release(&row->hdr);
754 BOOL ACTION_VerifyComponentForAction( MSICOMPONENT* comp, INSTALLSTATE check )
759 if (comp->Installed == check)
762 if (comp->ActionRequest == check)
768 BOOL ACTION_VerifyFeatureForAction( MSIFEATURE* feature, INSTALLSTATE check )
773 if (feature->Installed == check)
776 if (feature->ActionRequest == check)
782 void reduce_to_longfilename(WCHAR* filename)
784 LPWSTR p = strchrW(filename,'|');
786 memmove(filename, p+1, (strlenW(p+1)+1)*sizeof(WCHAR));
789 void reduce_to_shortfilename(WCHAR* filename)
791 LPWSTR p = strchrW(filename,'|');
796 LPWSTR create_component_advertise_string(MSIPACKAGE* package,
797 MSICOMPONENT* component, LPCWSTR feature)
799 static const WCHAR fmt[] = {'%','s','%','s','%','c','%','s',0};
800 WCHAR productid_85[21], component_85[21];
801 LPWSTR output = NULL;
805 /* > is used if there is a component GUID and < if not. */
810 CLSIDFromString(package->ProductCode, &clsid);
811 encode_base85_guid(&clsid, productid_85);
815 CLSIDFromString(component->ComponentId, &clsid);
816 encode_base85_guid(&clsid, component_85);
819 TRACE("prod=%s feat=%s comp=%s\n", debugstr_w(productid_85),
820 debugstr_w(feature), debugstr_w(component_85));
822 sz = 20 + lstrlenW(feature) + 20 + 3;
824 output = msi_alloc_zero(sz*sizeof(WCHAR));
826 sprintfW(output, fmt, productid_85, feature,
827 component?'>':'<', component_85);
832 /* update compoennt state based on a feature change */
833 void ACTION_UpdateComponentStates(MSIPACKAGE *package, LPCWSTR szFeature)
835 INSTALLSTATE newstate;
839 feature = get_loaded_feature(package,szFeature);
843 newstate = feature->ActionRequest;
845 LIST_FOR_EACH_ENTRY( cl, &feature->Components, ComponentList, entry )
847 MSICOMPONENT* component = cl->component;
849 TRACE("MODIFYING(%i): Component %s (Installed %i, Action %i, Request %i)\n",
850 newstate, debugstr_w(component->Component), component->Installed,
851 component->Action, component->ActionRequest);
853 if (!component->Enabled)
856 if (newstate == INSTALLSTATE_LOCAL)
858 component->ActionRequest = INSTALLSTATE_LOCAL;
859 component->Action = INSTALLSTATE_LOCAL;
863 ComponentList *clist;
866 component->ActionRequest = newstate;
867 component->Action = newstate;
869 /*if any other feature wants is local we need to set it local*/
870 LIST_FOR_EACH_ENTRY( f, &package->features, MSIFEATURE, entry )
872 if ( f->ActionRequest != INSTALLSTATE_LOCAL &&
873 f->ActionRequest != INSTALLSTATE_SOURCE )
878 LIST_FOR_EACH_ENTRY( clist, &f->Components, ComponentList, entry )
880 if ( clist->component == component &&
881 (f->ActionRequest == INSTALLSTATE_LOCAL ||
882 f->ActionRequest == INSTALLSTATE_SOURCE) )
884 TRACE("Saved by %s\n", debugstr_w(f->Feature));
885 component->ActionRequest = f->ActionRequest;
886 component->Action = f->ActionRequest;
891 TRACE("Result (%i): Component %s (Installed %i, Action %i, Request %i)\n",
892 newstate, debugstr_w(component->Component), component->Installed,
893 component->Action, component->ActionRequest);
897 UINT register_unique_action(MSIPACKAGE *package, LPCWSTR action)
900 LPWSTR *newbuf = NULL;
902 if (!package->script)
905 TRACE("Registering Action %s as having fun\n",debugstr_w(action));
907 count = package->script->UniqueActionsCount;
908 package->script->UniqueActionsCount++;
910 newbuf = msi_realloc( package->script->UniqueActions,
911 package->script->UniqueActionsCount* sizeof(LPWSTR));
913 newbuf = msi_alloc( sizeof(LPWSTR));
915 newbuf[count] = strdupW(action);
916 package->script->UniqueActions = newbuf;
918 return ERROR_SUCCESS;
921 BOOL check_unique_action(MSIPACKAGE *package, LPCWSTR action)
925 if (!package->script)
928 for (i = 0; i < package->script->UniqueActionsCount; i++)
929 if (!strcmpW(package->script->UniqueActions[i],action))
935 WCHAR* generate_error_string(MSIPACKAGE *package, UINT error, DWORD count, ... )
937 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};
947 row = MSI_QueryGetRecord(package->db, query, error);
951 rec = MSI_CreateRecord(count+2);
953 str = MSI_RecordGetString(row,1);
954 MSI_RecordSetStringW(rec,0,str);
955 msiobj_release( &row->hdr );
956 MSI_RecordSetInteger(rec,1,error);
959 for (i = 0; i < count; i++)
961 str = va_arg(va,LPCWSTR);
962 MSI_RecordSetStringW(rec,(i+2),str);
966 MSI_FormatRecordW(package,rec,NULL,&size);
970 data = msi_alloc(size*sizeof(WCHAR));
972 MSI_FormatRecordW(package,rec,data,&size);
975 msiobj_release( &rec->hdr );
979 msiobj_release( &rec->hdr );
984 void msi_ui_error( DWORD msg_id, DWORD type )
988 static const WCHAR title[] = {
989 'W','i','n','d','o','w','s',' ','I','n','s','t','a','l','l','e','r',0
992 if (!MsiLoadStringW( -1, msg_id, text, sizeof(text) / sizeof(text[0]),
993 MAKELANGID(LANG_NEUTRAL, SUBLANG_NEUTRAL) ))
996 MessageBoxW( NULL, text, title, type );