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 LPWSTR msi_dup_property(MSIPACKAGE *package, LPCWSTR prop)
80 r = MSI_GetPropertyW(package, prop, NULL, &sz);
81 if (r != ERROR_SUCCESS && r != ERROR_MORE_DATA)
85 str = msi_alloc(sz*sizeof(WCHAR));
86 r = MSI_GetPropertyW(package, prop, str, &sz);
87 if (r != ERROR_SUCCESS)
95 MSICOMPONENT* get_loaded_component( MSIPACKAGE* package, LPCWSTR Component )
99 LIST_FOR_EACH_ENTRY( comp, &package->components, MSICOMPONENT, entry )
101 if (lstrcmpW(Component,comp->Component)==0)
107 MSIFEATURE* get_loaded_feature(MSIPACKAGE* package, LPCWSTR Feature )
111 LIST_FOR_EACH_ENTRY( feature, &package->features, MSIFEATURE, entry )
113 if (lstrcmpW( Feature, feature->Feature )==0)
119 MSIFILE* get_loaded_file( MSIPACKAGE* package, LPCWSTR key )
123 LIST_FOR_EACH_ENTRY( file, &package->files, MSIFILE, entry )
125 if (lstrcmpW( key, file->File )==0)
131 int track_tempfile( MSIPACKAGE *package, LPCWSTR name, LPCWSTR path )
135 LIST_FOR_EACH_ENTRY( temp, &package->tempfiles, MSITEMPFILE, entry )
137 if (lstrcmpW( name, temp->File )==0)
139 TRACE("tempfile %s already exists with path %s\n",
140 debugstr_w(temp->File), debugstr_w(temp->Path));
145 temp = msi_alloc_zero( sizeof (MSITEMPFILE) );
149 list_add_head( &package->tempfiles, &temp->entry );
151 temp->File = strdupW( name );
152 temp->Path = strdupW( path );
154 TRACE("adding tempfile %s with path %s\n",
155 debugstr_w(temp->File), debugstr_w(temp->Path));
160 MSIFOLDER *get_loaded_folder( MSIPACKAGE *package, LPCWSTR dir )
164 LIST_FOR_EACH_ENTRY( folder, &package->folders, MSIFOLDER, entry )
166 if (lstrcmpW( dir, folder->Directory )==0)
172 static LPWSTR get_source_root( MSIPACKAGE *package )
176 path = msi_dup_property( package, cszSourceDir );
180 path = msi_dup_property( package, cszDatabase );
183 p = strrchrW(path,'\\');
191 * clean_spaces_from_path()
193 * removes spaces from the beginning and end of path segments
194 * removes multiple \\ characters
196 static void clean_spaces_from_path( LPWSTR p )
203 /* copy until the end of the string or a space */
204 while (*p != ' ' && (*q = *p))
207 /* reduce many backslashes to one */
208 if (*p != '\\' || *q != '\\')
212 /* quit at the end of the string */
216 /* count the number of spaces */
221 /* if it's leading or trailing space, skip it */
222 if ( len == 0 || p[-1] == '\\' || p[n] == '\\' )
224 else /* copy n spaces */
225 while (n && (*q++ = *p++)) n--;
229 LPWSTR resolve_folder(MSIPACKAGE *package, LPCWSTR name, BOOL source,
230 BOOL set_prop, MSIFOLDER **folder)
233 LPWSTR p, path = NULL;
235 TRACE("Working to resolve %s\n",debugstr_w(name));
240 /* special resolving for Target and Source root dir */
241 if (strcmpW(name,cszTargetDir)==0 || strcmpW(name,cszSourceDir)==0)
246 check_path = msi_dup_property( package, cszTargetDir );
249 check_path = msi_dup_property( package, cszRootDrive );
251 MSI_SetPropertyW(package,cszTargetDir,check_path);
254 /* correct misbuilt target dir */
255 path = build_directory_name(2, check_path, NULL);
256 clean_spaces_from_path( path );
257 if (strcmpiW(path,check_path)!=0)
258 MSI_SetPropertyW(package,cszTargetDir,path);
259 msi_free(check_path);
262 path = get_source_root( package );
264 *folder = get_loaded_folder( package, name );
268 f = get_loaded_folder( package, name );
275 if (!source && f->ResolvedTarget)
277 path = strdupW( f->ResolvedTarget );
278 TRACE(" already resolved to %s\n",debugstr_w(path));
281 else if (source && f->ResolvedSource)
283 path = strdupW( f->ResolvedSource );
284 TRACE(" (source)already resolved to %s\n",debugstr_w(path));
287 else if (!source && f->Property)
289 path = build_directory_name( 2, f->Property, NULL );
291 TRACE(" internally set to %s\n",debugstr_w(path));
293 MSI_SetPropertyW( package, name, path );
299 LPWSTR parent = f->Parent->Directory;
301 TRACE(" ! Parent is %s\n", debugstr_w(parent));
303 p = resolve_folder(package, parent, source, set_prop, NULL);
306 TRACE(" TargetDefault = %s\n", debugstr_w(f->TargetDefault));
308 path = build_directory_name( 3, p, f->TargetDefault, NULL );
309 clean_spaces_from_path( path );
310 f->ResolvedTarget = strdupW( path );
311 TRACE("target -> %s\n", debugstr_w(path));
313 MSI_SetPropertyW(package,name,path);
317 /* source may be in a few different places ... check each of them */
320 /* try the long path directory */
321 if (f->SourceLongPath)
323 path = build_directory_name( 3, p, f->SourceLongPath, NULL );
324 if (INVALID_FILE_ATTRIBUTES == GetFileAttributesW( path ))
331 /* try the short path directory */
332 if (!path && f->SourceShortPath)
334 path = build_directory_name( 3, p, f->SourceShortPath, NULL );
335 if (INVALID_FILE_ATTRIBUTES == GetFileAttributesW( path ))
342 /* try the root of the install */
344 path = get_source_root( package );
346 TRACE("source -> %s\n", debugstr_w(path));
347 f->ResolvedSource = strdupW( path );
354 /* wrapper to resist a need for a full rewrite right now */
355 DWORD deformat_string(MSIPACKAGE *package, LPCWSTR ptr, WCHAR** data )
359 MSIRECORD *rec = MSI_CreateRecord(1);
362 MSI_RecordSetStringW(rec,0,ptr);
363 MSI_FormatRecordW(package,rec,NULL,&size);
367 *data = msi_alloc(size*sizeof(WCHAR));
369 MSI_FormatRecordW(package,rec,*data,&size);
372 msiobj_release( &rec->hdr );
373 return sizeof(WCHAR)*size;
375 msiobj_release( &rec->hdr );
382 UINT schedule_action(MSIPACKAGE *package, UINT script, LPCWSTR action)
385 LPWSTR *newbuf = NULL;
386 if (script >= TOTAL_SCRIPTS)
388 FIXME("Unknown script requested %i\n",script);
389 return ERROR_FUNCTION_FAILED;
391 TRACE("Scheduling Action %s in script %i\n",debugstr_w(action), script);
393 count = package->script->ActionCount[script];
394 package->script->ActionCount[script]++;
396 newbuf = msi_realloc( package->script->Actions[script],
397 package->script->ActionCount[script]* sizeof(LPWSTR));
399 newbuf = msi_alloc( sizeof(LPWSTR));
401 newbuf[count] = strdupW(action);
402 package->script->Actions[script] = newbuf;
404 return ERROR_SUCCESS;
407 static void remove_tracked_tempfiles(MSIPACKAGE* package)
409 struct list *item, *cursor;
411 LIST_FOR_EACH_SAFE( item, cursor, &package->tempfiles )
413 MSITEMPFILE *temp = LIST_ENTRY( item, MSITEMPFILE, entry );
415 list_remove( &temp->entry );
416 TRACE("deleting temp file %s\n", debugstr_w( temp->Path ));
417 DeleteFileW( temp->Path );
418 msi_free( temp->File );
419 msi_free( temp->Path );
424 static void free_feature( MSIFEATURE *feature )
426 struct list *item, *cursor;
428 LIST_FOR_EACH_SAFE( item, cursor, &feature->Components )
430 ComponentList *cl = LIST_ENTRY( item, ComponentList, entry );
431 list_remove( &cl->entry );
434 msi_free( feature->Feature );
435 msi_free( feature->Feature_Parent );
436 msi_free( feature->Directory );
437 msi_free( feature->Description );
438 msi_free( feature->Title );
442 void free_extension( MSIEXTENSION *ext )
444 struct list *item, *cursor;
446 LIST_FOR_EACH_SAFE( item, cursor, &ext->verbs )
448 MSIVERB *verb = LIST_ENTRY( item, MSIVERB, entry );
450 list_remove( &verb->entry );
451 msi_free( verb->Verb );
452 msi_free( verb->Command );
453 msi_free( verb->Argument );
457 msi_free( ext->Extension );
458 msi_free( ext->ProgIDText );
462 /* Called when the package is being closed */
463 void ACTION_free_package_structures( MSIPACKAGE* package)
466 struct list *item, *cursor;
468 TRACE("Freeing package action data\n");
470 remove_tracked_tempfiles(package);
472 LIST_FOR_EACH_SAFE( item, cursor, &package->features )
474 MSIFEATURE *feature = LIST_ENTRY( item, MSIFEATURE, entry );
475 list_remove( &feature->entry );
476 free_feature( feature );
479 LIST_FOR_EACH_SAFE( item, cursor, &package->folders )
481 MSIFOLDER *folder = LIST_ENTRY( item, MSIFOLDER, entry );
483 list_remove( &folder->entry );
484 msi_free( folder->Directory );
485 msi_free( folder->TargetDefault );
486 msi_free( folder->SourceLongPath );
487 msi_free( folder->SourceShortPath );
488 msi_free( folder->ResolvedTarget );
489 msi_free( folder->ResolvedSource );
490 msi_free( folder->Property );
494 LIST_FOR_EACH_SAFE( item, cursor, &package->components )
496 MSICOMPONENT *comp = LIST_ENTRY( item, MSICOMPONENT, entry );
498 list_remove( &comp->entry );
499 msi_free( comp->Component );
500 msi_free( comp->ComponentId );
501 msi_free( comp->Directory );
502 msi_free( comp->Condition );
503 msi_free( comp->KeyPath );
504 msi_free( comp->FullKeypath );
508 LIST_FOR_EACH_SAFE( item, cursor, &package->files )
510 MSIFILE *file = LIST_ENTRY( item, MSIFILE, entry );
512 list_remove( &file->entry );
513 msi_free( file->File );
514 msi_free( file->FileName );
515 msi_free( file->ShortName );
516 msi_free( file->LongName );
517 msi_free( file->Version );
518 msi_free( file->Language );
519 msi_free( file->SourcePath );
520 msi_free( file->TargetPath );
524 /* clean up extension, progid, class and verb structures */
525 LIST_FOR_EACH_SAFE( item, cursor, &package->classes )
527 MSICLASS *cls = LIST_ENTRY( item, MSICLASS, entry );
529 list_remove( &cls->entry );
530 msi_free( cls->clsid );
531 msi_free( cls->Context );
532 msi_free( cls->Description );
533 msi_free( cls->FileTypeMask );
534 msi_free( cls->IconPath );
535 msi_free( cls->DefInprocHandler );
536 msi_free( cls->DefInprocHandler32 );
537 msi_free( cls->Argument );
538 msi_free( cls->ProgIDText );
542 LIST_FOR_EACH_SAFE( item, cursor, &package->extensions )
544 MSIEXTENSION *ext = LIST_ENTRY( item, MSIEXTENSION, entry );
546 list_remove( &ext->entry );
547 free_extension( ext );
550 LIST_FOR_EACH_SAFE( item, cursor, &package->progids )
552 MSIPROGID *progid = LIST_ENTRY( item, MSIPROGID, entry );
554 list_remove( &progid->entry );
555 msi_free( progid->ProgID );
556 msi_free( progid->Description );
557 msi_free( progid->IconPath );
561 LIST_FOR_EACH_SAFE( item, cursor, &package->mimes )
563 MSIMIME *mt = LIST_ENTRY( item, MSIMIME, entry );
565 list_remove( &mt->entry );
566 msi_free( mt->clsid );
567 msi_free( mt->ContentType );
571 LIST_FOR_EACH_SAFE( item, cursor, &package->appids )
573 MSIAPPID *appid = LIST_ENTRY( item, MSIAPPID, entry );
575 list_remove( &appid->entry );
576 msi_free( appid->AppID );
577 msi_free( appid->RemoteServerName );
578 msi_free( appid->LocalServer );
579 msi_free( appid->ServiceParameters );
580 msi_free( appid->DllSurrogate );
586 for (i = 0; i < TOTAL_SCRIPTS; i++)
589 for (j = 0; j < package->script->ActionCount[i]; j++)
590 msi_free(package->script->Actions[i][j]);
592 msi_free(package->script->Actions[i]);
595 for (i = 0; i < package->script->UniqueActionsCount; i++)
596 msi_free(package->script->UniqueActions[i]);
598 msi_free(package->script->UniqueActions);
599 msi_free(package->script);
602 msi_free(package->PackagePath);
603 msi_free(package->ProductCode);
604 msi_free(package->ActionFormat);
605 msi_free(package->LastAction);
607 /* cleanup control event subscriptions */
608 ControlEvent_CleanupSubscriptions(package);
612 * build_directory_name()
614 * This function is to save messing round with directory names
615 * It handles adding backslashes between path segments,
616 * and can add \ at the end of the directory name if told to.
618 * It takes a variable number of arguments.
619 * It always allocates a new string for the result, so make sure
620 * to free the return value when finished with it.
622 * The first arg is the number of path segments that follow.
623 * The arguments following count are a list of path segments.
624 * A path segment may be NULL.
626 * Path segments will be added with a \ separating them.
627 * A \ will not be added after the last segment, however if the
628 * last segment is NULL, then the last character will be a \
631 LPWSTR build_directory_name(DWORD count, ...)
638 for(i=0; i<count; i++)
640 LPCWSTR str = va_arg(va,LPCWSTR);
642 sz += strlenW(str) + 1;
646 dir = msi_alloc(sz*sizeof(WCHAR));
650 for(i=0; i<count; i++)
652 LPCWSTR str = va_arg(va,LPCWSTR);
656 if( ((i+1)!=count) && dir[strlenW(dir)-1]!='\\')
662 /***********************************************************************
665 * Recursively create all directories in the path.
667 * shamelessly stolen from setupapi/queue.c
669 BOOL create_full_pathW(const WCHAR *path)
675 new_path = msi_alloc( (strlenW(path) + 1) * sizeof(WCHAR));
677 strcpyW(new_path, path);
679 while((len = strlenW(new_path)) && new_path[len - 1] == '\\')
680 new_path[len - 1] = 0;
682 while(!CreateDirectoryW(new_path, NULL))
685 DWORD last_error = GetLastError();
686 if(last_error == ERROR_ALREADY_EXISTS)
689 if(last_error != ERROR_PATH_NOT_FOUND)
695 if(!(slash = strrchrW(new_path, '\\')))
701 len = slash - new_path;
703 if(!create_full_pathW(new_path))
708 new_path[len] = '\\';
715 void ui_progress(MSIPACKAGE *package, int a, int b, int c, int d )
719 row = MSI_CreateRecord(4);
720 MSI_RecordSetInteger(row,1,a);
721 MSI_RecordSetInteger(row,2,b);
722 MSI_RecordSetInteger(row,3,c);
723 MSI_RecordSetInteger(row,4,d);
724 MSI_ProcessMessage(package, INSTALLMESSAGE_PROGRESS, row);
725 msiobj_release(&row->hdr);
727 msi_dialog_check_messages(NULL);
730 void ui_actiondata(MSIPACKAGE *package, LPCWSTR action, MSIRECORD * record)
732 static const WCHAR Query_t[] =
733 {'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
734 '`','A','c','t','i','o', 'n','T','e','x','t','`',' ',
735 'W','H','E','R','E',' ', '`','A','c','t','i','o','n','`',' ','=',
736 ' ','\'','%','s','\'',0};
741 if (!package->LastAction || strcmpW(package->LastAction,action))
743 row = MSI_QueryGetRecord(package->db, Query_t, action);
747 if (MSI_RecordIsNull(row,3))
749 msiobj_release(&row->hdr);
753 /* update the cached actionformat */
754 msi_free(package->ActionFormat);
755 package->ActionFormat = msi_dup_record_field(row,3);
757 msi_free(package->LastAction);
758 package->LastAction = strdupW(action);
760 msiobj_release(&row->hdr);
763 MSI_RecordSetStringW(record,0,package->ActionFormat);
765 MSI_FormatRecordW(package,record,message,&size);
767 row = MSI_CreateRecord(1);
768 MSI_RecordSetStringW(row,1,message);
770 MSI_ProcessMessage(package, INSTALLMESSAGE_ACTIONDATA, row);
772 msiobj_release(&row->hdr);
775 BOOL ACTION_VerifyComponentForAction( MSICOMPONENT* comp, INSTALLSTATE check )
780 if (comp->Installed == check)
783 if (comp->ActionRequest == check)
789 BOOL ACTION_VerifyFeatureForAction( MSIFEATURE* feature, INSTALLSTATE check )
794 if (feature->Installed == check)
797 if (feature->ActionRequest == check)
803 void reduce_to_longfilename(WCHAR* filename)
805 LPWSTR p = strchrW(filename,'|');
807 memmove(filename, p+1, (strlenW(p+1)+1)*sizeof(WCHAR));
810 void reduce_to_shortfilename(WCHAR* filename)
812 LPWSTR p = strchrW(filename,'|');
817 LPWSTR create_component_advertise_string(MSIPACKAGE* package,
818 MSICOMPONENT* component, LPCWSTR feature)
820 static const WCHAR fmt[] = {'%','s','%','s','%','c','%','s',0};
821 WCHAR productid_85[21], component_85[21];
822 LPWSTR output = NULL;
826 /* > is used if there is a component GUID and < if not. */
831 CLSIDFromString(package->ProductCode, &clsid);
832 encode_base85_guid(&clsid, productid_85);
836 CLSIDFromString(component->ComponentId, &clsid);
837 encode_base85_guid(&clsid, component_85);
840 TRACE("prod=%s feat=%s comp=%s\n", debugstr_w(productid_85),
841 debugstr_w(feature), debugstr_w(component_85));
843 sz = 20 + lstrlenW(feature) + 20 + 3;
845 output = msi_alloc_zero(sz*sizeof(WCHAR));
847 sprintfW(output, fmt, productid_85, feature,
848 component?'>':'<', component_85);
853 /* update compoennt state based on a feature change */
854 void ACTION_UpdateComponentStates(MSIPACKAGE *package, LPCWSTR szFeature)
856 INSTALLSTATE newstate;
860 feature = get_loaded_feature(package,szFeature);
864 newstate = feature->ActionRequest;
866 LIST_FOR_EACH_ENTRY( cl, &feature->Components, ComponentList, entry )
868 MSICOMPONENT* component = cl->component;
870 TRACE("MODIFYING(%i): Component %s (Installed %i, Action %i, Request %i)\n",
871 newstate, debugstr_w(component->Component), component->Installed,
872 component->Action, component->ActionRequest);
874 if (!component->Enabled)
877 if (newstate == INSTALLSTATE_LOCAL)
879 component->ActionRequest = INSTALLSTATE_LOCAL;
880 component->Action = INSTALLSTATE_LOCAL;
884 ComponentList *clist;
887 component->ActionRequest = newstate;
888 component->Action = newstate;
890 /*if any other feature wants is local we need to set it local*/
891 LIST_FOR_EACH_ENTRY( f, &package->features, MSIFEATURE, entry )
893 if ( component->ActionRequest != INSTALLSTATE_LOCAL )
896 LIST_FOR_EACH_ENTRY( clist, &f->Components, ComponentList, entry )
898 if ( clist->component == component )
900 if (f->ActionRequest == INSTALLSTATE_LOCAL)
902 TRACE("Saved by %s\n", debugstr_w(f->Feature));
903 component->ActionRequest = INSTALLSTATE_LOCAL;
904 component->Action = INSTALLSTATE_LOCAL;
911 TRACE("Result (%i): Component %s (Installed %i, Action %i, Request %i)\n",
912 newstate, debugstr_w(component->Component), component->Installed,
913 component->Action, component->ActionRequest);
917 UINT register_unique_action(MSIPACKAGE *package, LPCWSTR action)
920 LPWSTR *newbuf = NULL;
922 if (!package->script)
925 TRACE("Registering Action %s as having fun\n",debugstr_w(action));
927 count = package->script->UniqueActionsCount;
928 package->script->UniqueActionsCount++;
930 newbuf = msi_realloc( package->script->UniqueActions,
931 package->script->UniqueActionsCount* sizeof(LPWSTR));
933 newbuf = msi_alloc( sizeof(LPWSTR));
935 newbuf[count] = strdupW(action);
936 package->script->UniqueActions = newbuf;
938 return ERROR_SUCCESS;
941 BOOL check_unique_action(MSIPACKAGE *package, LPCWSTR action)
945 if (!package->script)
948 for (i = 0; i < package->script->UniqueActionsCount; i++)
949 if (!strcmpW(package->script->UniqueActions[i],action))
955 WCHAR* generate_error_string(MSIPACKAGE *package, UINT error, DWORD count, ... )
957 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};
967 row = MSI_QueryGetRecord(package->db, query, error);
971 rec = MSI_CreateRecord(count+2);
973 str = MSI_RecordGetString(row,1);
974 MSI_RecordSetStringW(rec,0,str);
975 msiobj_release( &row->hdr );
976 MSI_RecordSetInteger(rec,1,error);
979 for (i = 0; i < count; i++)
981 str = va_arg(va,LPCWSTR);
982 MSI_RecordSetStringW(rec,(i+2),str);
986 MSI_FormatRecordW(package,rec,NULL,&size);
990 data = msi_alloc(size*sizeof(WCHAR));
992 MSI_FormatRecordW(package,rec,data,&size);
995 msiobj_release( &rec->hdr );
999 msiobj_release( &rec->hdr );