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 int msi_get_property_int( MSIPACKAGE *package, LPCWSTR prop, int def )
97 LPWSTR str = msi_dup_property( package, prop );
98 int val = str ? atoiW( str ) : def;
103 MSICOMPONENT* get_loaded_component( MSIPACKAGE* package, LPCWSTR Component )
107 LIST_FOR_EACH_ENTRY( comp, &package->components, MSICOMPONENT, entry )
109 if (lstrcmpW(Component,comp->Component)==0)
115 MSIFEATURE* get_loaded_feature(MSIPACKAGE* package, LPCWSTR Feature )
119 LIST_FOR_EACH_ENTRY( feature, &package->features, MSIFEATURE, entry )
121 if (lstrcmpW( Feature, feature->Feature )==0)
127 MSIFILE* get_loaded_file( MSIPACKAGE* package, LPCWSTR key )
131 LIST_FOR_EACH_ENTRY( file, &package->files, MSIFILE, entry )
133 if (lstrcmpW( key, file->File )==0)
139 int track_tempfile( MSIPACKAGE *package, LPCWSTR name, LPCWSTR path )
143 LIST_FOR_EACH_ENTRY( temp, &package->tempfiles, MSITEMPFILE, entry )
145 if (lstrcmpW( name, temp->File )==0)
147 TRACE("tempfile %s already exists with path %s\n",
148 debugstr_w(temp->File), debugstr_w(temp->Path));
153 temp = msi_alloc_zero( sizeof (MSITEMPFILE) );
157 list_add_head( &package->tempfiles, &temp->entry );
159 temp->File = strdupW( name );
160 temp->Path = strdupW( path );
162 TRACE("adding tempfile %s with path %s\n",
163 debugstr_w(temp->File), debugstr_w(temp->Path));
168 MSIFOLDER *get_loaded_folder( MSIPACKAGE *package, LPCWSTR dir )
172 LIST_FOR_EACH_ENTRY( folder, &package->folders, MSIFOLDER, entry )
174 if (lstrcmpW( dir, folder->Directory )==0)
180 static LPWSTR get_source_root( MSIPACKAGE *package )
184 path = msi_dup_property( package, cszSourceDir );
188 path = msi_dup_property( package, cszDatabase );
191 p = strrchrW(path,'\\');
199 * clean_spaces_from_path()
201 * removes spaces from the beginning and end of path segments
202 * removes multiple \\ characters
204 static void clean_spaces_from_path( LPWSTR p )
211 /* copy until the end of the string or a space */
212 while (*p != ' ' && (*q = *p))
215 /* reduce many backslashes to one */
216 if (*p != '\\' || *q != '\\')
220 /* quit at the end of the string */
224 /* count the number of spaces */
229 /* if it's leading or trailing space, skip it */
230 if ( len == 0 || p[-1] == '\\' || p[n] == '\\' )
232 else /* copy n spaces */
233 while (n && (*q++ = *p++)) n--;
237 LPWSTR resolve_folder(MSIPACKAGE *package, LPCWSTR name, BOOL source,
238 BOOL set_prop, MSIFOLDER **folder)
241 LPWSTR p, path = NULL;
243 TRACE("Working to resolve %s\n",debugstr_w(name));
248 /* special resolving for Target and Source root dir */
249 if (strcmpW(name,cszTargetDir)==0 || strcmpW(name,cszSourceDir)==0)
254 check_path = msi_dup_property( package, cszTargetDir );
257 check_path = msi_dup_property( package, cszRootDrive );
259 MSI_SetPropertyW(package,cszTargetDir,check_path);
262 /* correct misbuilt target dir */
263 path = build_directory_name(2, check_path, NULL);
264 clean_spaces_from_path( path );
265 if (strcmpiW(path,check_path)!=0)
266 MSI_SetPropertyW(package,cszTargetDir,path);
267 msi_free(check_path);
270 path = get_source_root( package );
272 *folder = get_loaded_folder( package, name );
276 f = get_loaded_folder( package, name );
283 if (!source && f->ResolvedTarget)
285 path = strdupW( f->ResolvedTarget );
286 TRACE(" already resolved to %s\n",debugstr_w(path));
289 else if (source && f->ResolvedSource)
291 path = strdupW( f->ResolvedSource );
292 TRACE(" (source)already resolved to %s\n",debugstr_w(path));
295 else if (!source && f->Property)
297 path = build_directory_name( 2, f->Property, NULL );
299 TRACE(" internally set to %s\n",debugstr_w(path));
301 MSI_SetPropertyW( package, name, path );
307 LPWSTR parent = f->Parent->Directory;
309 TRACE(" ! Parent is %s\n", debugstr_w(parent));
311 p = resolve_folder(package, parent, source, set_prop, NULL);
314 TRACE(" TargetDefault = %s\n", debugstr_w(f->TargetDefault));
316 path = build_directory_name( 3, p, f->TargetDefault, NULL );
317 clean_spaces_from_path( path );
318 f->ResolvedTarget = strdupW( path );
319 TRACE("target -> %s\n", debugstr_w(path));
321 MSI_SetPropertyW(package,name,path);
325 /* source may be in a few different places ... check each of them */
328 /* try the long path directory */
329 if (f->SourceLongPath)
331 path = build_directory_name( 3, p, f->SourceLongPath, NULL );
332 if (INVALID_FILE_ATTRIBUTES == GetFileAttributesW( path ))
339 /* try the short path directory */
340 if (!path && f->SourceShortPath)
342 path = build_directory_name( 3, p, f->SourceShortPath, NULL );
343 if (INVALID_FILE_ATTRIBUTES == GetFileAttributesW( path ))
350 /* try the root of the install */
352 path = get_source_root( package );
354 TRACE("source -> %s\n", debugstr_w(path));
355 f->ResolvedSource = strdupW( path );
362 /* wrapper to resist a need for a full rewrite right now */
363 DWORD deformat_string(MSIPACKAGE *package, LPCWSTR ptr, WCHAR** data )
367 MSIRECORD *rec = MSI_CreateRecord(1);
370 MSI_RecordSetStringW(rec,0,ptr);
371 MSI_FormatRecordW(package,rec,NULL,&size);
375 *data = msi_alloc(size*sizeof(WCHAR));
377 MSI_FormatRecordW(package,rec,*data,&size);
380 msiobj_release( &rec->hdr );
381 return sizeof(WCHAR)*size;
383 msiobj_release( &rec->hdr );
390 UINT schedule_action(MSIPACKAGE *package, UINT script, LPCWSTR action)
393 LPWSTR *newbuf = NULL;
394 if (script >= TOTAL_SCRIPTS)
396 FIXME("Unknown script requested %i\n",script);
397 return ERROR_FUNCTION_FAILED;
399 TRACE("Scheduling Action %s in script %i\n",debugstr_w(action), script);
401 count = package->script->ActionCount[script];
402 package->script->ActionCount[script]++;
404 newbuf = msi_realloc( package->script->Actions[script],
405 package->script->ActionCount[script]* sizeof(LPWSTR));
407 newbuf = msi_alloc( sizeof(LPWSTR));
409 newbuf[count] = strdupW(action);
410 package->script->Actions[script] = newbuf;
412 return ERROR_SUCCESS;
415 static void remove_tracked_tempfiles(MSIPACKAGE* package)
417 struct list *item, *cursor;
419 LIST_FOR_EACH_SAFE( item, cursor, &package->tempfiles )
421 MSITEMPFILE *temp = LIST_ENTRY( item, MSITEMPFILE, entry );
423 list_remove( &temp->entry );
424 TRACE("deleting temp file %s\n", debugstr_w( temp->Path ));
425 DeleteFileW( temp->Path );
426 msi_free( temp->File );
427 msi_free( temp->Path );
432 static void free_feature( MSIFEATURE *feature )
434 struct list *item, *cursor;
436 LIST_FOR_EACH_SAFE( item, cursor, &feature->Components )
438 ComponentList *cl = LIST_ENTRY( item, ComponentList, entry );
439 list_remove( &cl->entry );
442 msi_free( feature->Feature );
443 msi_free( feature->Feature_Parent );
444 msi_free( feature->Directory );
445 msi_free( feature->Description );
446 msi_free( feature->Title );
450 void free_extension( MSIEXTENSION *ext )
452 struct list *item, *cursor;
454 LIST_FOR_EACH_SAFE( item, cursor, &ext->verbs )
456 MSIVERB *verb = LIST_ENTRY( item, MSIVERB, entry );
458 list_remove( &verb->entry );
459 msi_free( verb->Verb );
460 msi_free( verb->Command );
461 msi_free( verb->Argument );
465 msi_free( ext->Extension );
466 msi_free( ext->ProgIDText );
470 /* Called when the package is being closed */
471 void ACTION_free_package_structures( MSIPACKAGE* package)
474 struct list *item, *cursor;
476 TRACE("Freeing package action data\n");
478 remove_tracked_tempfiles(package);
480 LIST_FOR_EACH_SAFE( item, cursor, &package->features )
482 MSIFEATURE *feature = LIST_ENTRY( item, MSIFEATURE, entry );
483 list_remove( &feature->entry );
484 free_feature( feature );
487 LIST_FOR_EACH_SAFE( item, cursor, &package->folders )
489 MSIFOLDER *folder = LIST_ENTRY( item, MSIFOLDER, entry );
491 list_remove( &folder->entry );
492 msi_free( folder->Directory );
493 msi_free( folder->TargetDefault );
494 msi_free( folder->SourceLongPath );
495 msi_free( folder->SourceShortPath );
496 msi_free( folder->ResolvedTarget );
497 msi_free( folder->ResolvedSource );
498 msi_free( folder->Property );
502 LIST_FOR_EACH_SAFE( item, cursor, &package->components )
504 MSICOMPONENT *comp = LIST_ENTRY( item, MSICOMPONENT, entry );
506 list_remove( &comp->entry );
507 msi_free( comp->Component );
508 msi_free( comp->ComponentId );
509 msi_free( comp->Directory );
510 msi_free( comp->Condition );
511 msi_free( comp->KeyPath );
512 msi_free( comp->FullKeypath );
516 LIST_FOR_EACH_SAFE( item, cursor, &package->files )
518 MSIFILE *file = LIST_ENTRY( item, MSIFILE, entry );
520 list_remove( &file->entry );
521 msi_free( file->File );
522 msi_free( file->FileName );
523 msi_free( file->ShortName );
524 msi_free( file->LongName );
525 msi_free( file->Version );
526 msi_free( file->Language );
527 msi_free( file->SourcePath );
528 msi_free( file->TargetPath );
532 /* clean up extension, progid, class and verb structures */
533 LIST_FOR_EACH_SAFE( item, cursor, &package->classes )
535 MSICLASS *cls = LIST_ENTRY( item, MSICLASS, entry );
537 list_remove( &cls->entry );
538 msi_free( cls->clsid );
539 msi_free( cls->Context );
540 msi_free( cls->Description );
541 msi_free( cls->FileTypeMask );
542 msi_free( cls->IconPath );
543 msi_free( cls->DefInprocHandler );
544 msi_free( cls->DefInprocHandler32 );
545 msi_free( cls->Argument );
546 msi_free( cls->ProgIDText );
550 LIST_FOR_EACH_SAFE( item, cursor, &package->extensions )
552 MSIEXTENSION *ext = LIST_ENTRY( item, MSIEXTENSION, entry );
554 list_remove( &ext->entry );
555 free_extension( ext );
558 LIST_FOR_EACH_SAFE( item, cursor, &package->progids )
560 MSIPROGID *progid = LIST_ENTRY( item, MSIPROGID, entry );
562 list_remove( &progid->entry );
563 msi_free( progid->ProgID );
564 msi_free( progid->Description );
565 msi_free( progid->IconPath );
569 LIST_FOR_EACH_SAFE( item, cursor, &package->mimes )
571 MSIMIME *mt = LIST_ENTRY( item, MSIMIME, entry );
573 list_remove( &mt->entry );
574 msi_free( mt->clsid );
575 msi_free( mt->ContentType );
579 LIST_FOR_EACH_SAFE( item, cursor, &package->appids )
581 MSIAPPID *appid = LIST_ENTRY( item, MSIAPPID, entry );
583 list_remove( &appid->entry );
584 msi_free( appid->AppID );
585 msi_free( appid->RemoteServerName );
586 msi_free( appid->LocalServer );
587 msi_free( appid->ServiceParameters );
588 msi_free( appid->DllSurrogate );
594 for (i = 0; i < TOTAL_SCRIPTS; i++)
597 for (j = 0; j < package->script->ActionCount[i]; j++)
598 msi_free(package->script->Actions[i][j]);
600 msi_free(package->script->Actions[i]);
603 for (i = 0; i < package->script->UniqueActionsCount; i++)
604 msi_free(package->script->UniqueActions[i]);
606 msi_free(package->script->UniqueActions);
607 msi_free(package->script);
610 msi_free(package->PackagePath);
611 msi_free(package->ProductCode);
612 msi_free(package->ActionFormat);
613 msi_free(package->LastAction);
615 /* cleanup control event subscriptions */
616 ControlEvent_CleanupSubscriptions(package);
620 * build_directory_name()
622 * This function is to save messing round with directory names
623 * It handles adding backslashes between path segments,
624 * and can add \ at the end of the directory name if told to.
626 * It takes a variable number of arguments.
627 * It always allocates a new string for the result, so make sure
628 * to free the return value when finished with it.
630 * The first arg is the number of path segments that follow.
631 * The arguments following count are a list of path segments.
632 * A path segment may be NULL.
634 * Path segments will be added with a \ separating them.
635 * A \ will not be added after the last segment, however if the
636 * last segment is NULL, then the last character will be a \
639 LPWSTR build_directory_name(DWORD count, ...)
646 for(i=0; i<count; i++)
648 LPCWSTR str = va_arg(va,LPCWSTR);
650 sz += strlenW(str) + 1;
654 dir = msi_alloc(sz*sizeof(WCHAR));
658 for(i=0; i<count; i++)
660 LPCWSTR str = va_arg(va,LPCWSTR);
664 if( ((i+1)!=count) && dir[strlenW(dir)-1]!='\\')
670 /***********************************************************************
673 * Recursively create all directories in the path.
675 * shamelessly stolen from setupapi/queue.c
677 BOOL create_full_pathW(const WCHAR *path)
683 new_path = msi_alloc( (strlenW(path) + 1) * sizeof(WCHAR));
685 strcpyW(new_path, path);
687 while((len = strlenW(new_path)) && new_path[len - 1] == '\\')
688 new_path[len - 1] = 0;
690 while(!CreateDirectoryW(new_path, NULL))
693 DWORD last_error = GetLastError();
694 if(last_error == ERROR_ALREADY_EXISTS)
697 if(last_error != ERROR_PATH_NOT_FOUND)
703 if(!(slash = strrchrW(new_path, '\\')))
709 len = slash - new_path;
711 if(!create_full_pathW(new_path))
716 new_path[len] = '\\';
723 void ui_progress(MSIPACKAGE *package, int a, int b, int c, int d )
727 row = MSI_CreateRecord(4);
728 MSI_RecordSetInteger(row,1,a);
729 MSI_RecordSetInteger(row,2,b);
730 MSI_RecordSetInteger(row,3,c);
731 MSI_RecordSetInteger(row,4,d);
732 MSI_ProcessMessage(package, INSTALLMESSAGE_PROGRESS, row);
733 msiobj_release(&row->hdr);
735 msi_dialog_check_messages(NULL);
738 void ui_actiondata(MSIPACKAGE *package, LPCWSTR action, MSIRECORD * record)
740 static const WCHAR Query_t[] =
741 {'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
742 '`','A','c','t','i','o', 'n','T','e','x','t','`',' ',
743 'W','H','E','R','E',' ', '`','A','c','t','i','o','n','`',' ','=',
744 ' ','\'','%','s','\'',0};
749 if (!package->LastAction || strcmpW(package->LastAction,action))
751 row = MSI_QueryGetRecord(package->db, Query_t, action);
755 if (MSI_RecordIsNull(row,3))
757 msiobj_release(&row->hdr);
761 /* update the cached actionformat */
762 msi_free(package->ActionFormat);
763 package->ActionFormat = msi_dup_record_field(row,3);
765 msi_free(package->LastAction);
766 package->LastAction = strdupW(action);
768 msiobj_release(&row->hdr);
771 MSI_RecordSetStringW(record,0,package->ActionFormat);
773 MSI_FormatRecordW(package,record,message,&size);
775 row = MSI_CreateRecord(1);
776 MSI_RecordSetStringW(row,1,message);
778 MSI_ProcessMessage(package, INSTALLMESSAGE_ACTIONDATA, row);
780 msiobj_release(&row->hdr);
783 BOOL ACTION_VerifyComponentForAction( MSICOMPONENT* comp, INSTALLSTATE check )
788 if (comp->Installed == check)
791 if (comp->ActionRequest == check)
797 BOOL ACTION_VerifyFeatureForAction( MSIFEATURE* feature, INSTALLSTATE check )
802 if (feature->Installed == check)
805 if (feature->ActionRequest == check)
811 void reduce_to_longfilename(WCHAR* filename)
813 LPWSTR p = strchrW(filename,'|');
815 memmove(filename, p+1, (strlenW(p+1)+1)*sizeof(WCHAR));
818 void reduce_to_shortfilename(WCHAR* filename)
820 LPWSTR p = strchrW(filename,'|');
825 LPWSTR create_component_advertise_string(MSIPACKAGE* package,
826 MSICOMPONENT* component, LPCWSTR feature)
828 static const WCHAR fmt[] = {'%','s','%','s','%','c','%','s',0};
829 WCHAR productid_85[21], component_85[21];
830 LPWSTR output = NULL;
834 /* > is used if there is a component GUID and < if not. */
839 CLSIDFromString(package->ProductCode, &clsid);
840 encode_base85_guid(&clsid, productid_85);
844 CLSIDFromString(component->ComponentId, &clsid);
845 encode_base85_guid(&clsid, component_85);
848 TRACE("prod=%s feat=%s comp=%s\n", debugstr_w(productid_85),
849 debugstr_w(feature), debugstr_w(component_85));
851 sz = 20 + lstrlenW(feature) + 20 + 3;
853 output = msi_alloc_zero(sz*sizeof(WCHAR));
855 sprintfW(output, fmt, productid_85, feature,
856 component?'>':'<', component_85);
861 /* update compoennt state based on a feature change */
862 void ACTION_UpdateComponentStates(MSIPACKAGE *package, LPCWSTR szFeature)
864 INSTALLSTATE newstate;
868 feature = get_loaded_feature(package,szFeature);
872 newstate = feature->ActionRequest;
874 LIST_FOR_EACH_ENTRY( cl, &feature->Components, ComponentList, entry )
876 MSICOMPONENT* component = cl->component;
878 TRACE("MODIFYING(%i): Component %s (Installed %i, Action %i, Request %i)\n",
879 newstate, debugstr_w(component->Component), component->Installed,
880 component->Action, component->ActionRequest);
882 if (!component->Enabled)
885 if (newstate == INSTALLSTATE_LOCAL)
887 component->ActionRequest = INSTALLSTATE_LOCAL;
888 component->Action = INSTALLSTATE_LOCAL;
892 ComponentList *clist;
895 component->ActionRequest = newstate;
896 component->Action = newstate;
898 /*if any other feature wants is local we need to set it local*/
899 LIST_FOR_EACH_ENTRY( f, &package->features, MSIFEATURE, entry )
901 if ( component->ActionRequest != INSTALLSTATE_LOCAL )
904 LIST_FOR_EACH_ENTRY( clist, &f->Components, ComponentList, entry )
906 if ( clist->component == component )
908 if (f->ActionRequest == INSTALLSTATE_LOCAL)
910 TRACE("Saved by %s\n", debugstr_w(f->Feature));
911 component->ActionRequest = INSTALLSTATE_LOCAL;
912 component->Action = INSTALLSTATE_LOCAL;
919 TRACE("Result (%i): Component %s (Installed %i, Action %i, Request %i)\n",
920 newstate, debugstr_w(component->Component), component->Installed,
921 component->Action, component->ActionRequest);
925 UINT register_unique_action(MSIPACKAGE *package, LPCWSTR action)
928 LPWSTR *newbuf = NULL;
930 if (!package->script)
933 TRACE("Registering Action %s as having fun\n",debugstr_w(action));
935 count = package->script->UniqueActionsCount;
936 package->script->UniqueActionsCount++;
938 newbuf = msi_realloc( package->script->UniqueActions,
939 package->script->UniqueActionsCount* sizeof(LPWSTR));
941 newbuf = msi_alloc( sizeof(LPWSTR));
943 newbuf[count] = strdupW(action);
944 package->script->UniqueActions = newbuf;
946 return ERROR_SUCCESS;
949 BOOL check_unique_action(MSIPACKAGE *package, LPCWSTR action)
953 if (!package->script)
956 for (i = 0; i < package->script->UniqueActionsCount; i++)
957 if (!strcmpW(package->script->UniqueActions[i],action))
963 WCHAR* generate_error_string(MSIPACKAGE *package, UINT error, DWORD count, ... )
965 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};
975 row = MSI_QueryGetRecord(package->db, query, error);
979 rec = MSI_CreateRecord(count+2);
981 str = MSI_RecordGetString(row,1);
982 MSI_RecordSetStringW(rec,0,str);
983 msiobj_release( &row->hdr );
984 MSI_RecordSetInteger(rec,1,error);
987 for (i = 0; i < count; i++)
989 str = va_arg(va,LPCWSTR);
990 MSI_RecordSetStringW(rec,(i+2),str);
994 MSI_FormatRecordW(package,rec,NULL,&size);
998 data = msi_alloc(size*sizeof(WCHAR));
1000 MSI_FormatRecordW(package,rec,data,&size);
1003 msiobj_release( &rec->hdr );
1007 msiobj_release( &rec->hdr );