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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 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 szProductCode[]= {'P','r','o','d','u','c','t','C','o','d','e',0};
44 const WCHAR cszRootDrive[] = {'R','O','O','T','D','R','I','V','E',0};
45 const WCHAR cszbs[]={'\\',0};
47 DWORD build_version_dword(LPCWSTR version_string)
51 DWORD rc = 0x00000000;
54 ptr1 = version_string;
63 ptr1 = strchrW(ptr1,'.');
73 ptr1 = strchrW(ptr1,'.');
83 rc = MAKELONG(build,MAKEWORD(minor,major));
84 TRACE("%s -> 0x%lx\n",debugstr_w(version_string),rc);
88 UINT build_icon_path(MSIPACKAGE *package, LPCWSTR icon_name,
96 static const WCHAR szInstaller[] =
97 {'M','i','c','r','o','s','o','f','t','\\',
98 'I','n','s','t','a','l','l','e','r','\\',0};
99 static const WCHAR szFolder[] =
100 {'A','p','p','D','a','t','a','F','o','l','d','e','r',0};
102 ProductCode = load_dynamic_property(package,szProductCode,&rc);
106 SystemFolder = load_dynamic_property(package,szFolder,NULL);
108 dest = build_directory_name(3, SystemFolder, szInstaller, ProductCode);
110 create_full_pathW(dest);
112 *FilePath = build_directory_name(2, dest, icon_name);
114 HeapFree(GetProcessHeap(),0,SystemFolder);
115 HeapFree(GetProcessHeap(),0,ProductCode);
116 HeapFree(GetProcessHeap(),0,dest);
117 return ERROR_SUCCESS;
120 WCHAR *load_dynamic_stringW(MSIRECORD *row, INT index)
127 if (MSI_RecordIsNull(row,index))
130 rc = MSI_RecordGetStringW(row,index,NULL,&sz);
132 /* having an empty string is different than NULL */
135 ret = HeapAlloc(GetProcessHeap(),0,sizeof(WCHAR));
141 ret = HeapAlloc(GetProcessHeap(),0,sz * sizeof (WCHAR));
142 rc = MSI_RecordGetStringW(row,index,ret,&sz);
143 if (rc!=ERROR_SUCCESS)
145 ERR("Unable to load dynamic string\n");
146 HeapFree(GetProcessHeap(), 0, ret);
152 LPWSTR load_dynamic_property(MSIPACKAGE *package, LPCWSTR prop, UINT* rc)
158 r = MSI_GetPropertyW(package, prop, NULL, &sz);
159 if (r != ERROR_SUCCESS && r != ERROR_MORE_DATA)
166 str = HeapAlloc(GetProcessHeap(),0,sz*sizeof(WCHAR));
167 r = MSI_GetPropertyW(package, prop, str, &sz);
168 if (r != ERROR_SUCCESS)
170 HeapFree(GetProcessHeap(),0,str);
178 int get_loaded_component(MSIPACKAGE* package, LPCWSTR Component )
183 for (i = 0; i < package->loaded_components; i++)
185 if (strcmpW(Component,package->components[i].Component)==0)
194 int get_loaded_feature(MSIPACKAGE* package, LPCWSTR Feature )
199 for (i = 0; i < package->loaded_features; i++)
201 if (strcmpW(Feature,package->features[i].Feature)==0)
210 int get_loaded_file(MSIPACKAGE* package, LPCWSTR file)
215 for (i = 0; i < package->loaded_files; i++)
217 if (strcmpW(file,package->files[i].File)==0)
226 int track_tempfile(MSIPACKAGE *package, LPCWSTR name, LPCWSTR path)
234 for (i=0; i < package->loaded_files; i++)
235 if (strcmpW(package->files[i].File,name)==0)
238 index = package->loaded_files;
239 package->loaded_files++;
240 if (package->loaded_files== 1)
241 package->files = HeapAlloc(GetProcessHeap(),0,sizeof(MSIFILE));
243 package->files = HeapReAlloc(GetProcessHeap(),0,
244 package->files , package->loaded_files * sizeof(MSIFILE));
246 memset(&package->files[index],0,sizeof(MSIFILE));
248 package->files[index].File = strdupW(name);
249 package->files[index].TargetPath = strdupW(path);
250 package->files[index].Temporary = TRUE;
252 TRACE("Tracking tempfile (%s)\n",debugstr_w(package->files[index].File));
257 LPWSTR resolve_folder(MSIPACKAGE *package, LPCWSTR name, BOOL source,
258 BOOL set_prop, MSIFOLDER **folder)
261 LPWSTR p, path = NULL;
263 TRACE("Working to resolve %s\n",debugstr_w(name));
268 /* special resolving for Target and Source root dir */
269 if (strcmpW(name,cszTargetDir)==0 || strcmpW(name,cszSourceDir)==0)
273 path = load_dynamic_property(package,cszTargetDir,NULL);
276 path = load_dynamic_property(package,cszRootDrive,NULL);
278 MSI_SetPropertyW(package,cszTargetDir,path);
282 for (i = 0; i < package->loaded_folders; i++)
284 if (strcmpW(package->folders[i].Directory,name)==0)
287 *folder = &(package->folders[i]);
293 path = load_dynamic_property(package,cszSourceDir,NULL);
296 path = load_dynamic_property(package,cszDatabase,NULL);
299 p = strrchrW(path,'\\');
306 for (i = 0; i < package->loaded_folders; i++)
308 if (strcmpW(package->folders[i].Directory,name)==0)
311 *folder = &(package->folders[i]);
317 for (i = 0; i < package->loaded_folders; i++)
319 if (strcmpW(package->folders[i].Directory,name)==0)
323 if (i >= package->loaded_folders)
327 *folder = &(package->folders[i]);
329 if (!source && package->folders[i].ResolvedTarget)
331 path = strdupW(package->folders[i].ResolvedTarget);
332 TRACE(" already resolved to %s\n",debugstr_w(path));
335 else if (source && package->folders[i].ResolvedSource)
337 path = strdupW(package->folders[i].ResolvedSource);
338 TRACE(" (source)already resolved to %s\n",debugstr_w(path));
341 else if (!source && package->folders[i].Property)
343 path = build_directory_name(2, package->folders[i].Property, NULL);
345 TRACE(" internally set to %s\n",debugstr_w(path));
347 MSI_SetPropertyW(package,name,path);
351 if (package->folders[i].ParentIndex >= 0)
353 LPWSTR parent = package->folders[package->folders[i].ParentIndex].Directory;
355 TRACE(" ! Parent is %s\n", debugstr_w(parent));
357 p = resolve_folder(package, parent, source, set_prop, NULL);
360 TRACE(" TargetDefault = %s\n",
361 debugstr_w(package->folders[i].TargetDefault));
363 path = build_directory_name(3, p,
364 package->folders[i].TargetDefault, NULL);
365 package->folders[i].ResolvedTarget = strdupW(path);
366 TRACE(" resolved into %s\n",debugstr_w(path));
368 MSI_SetPropertyW(package,name,path);
372 if (package->folders[i].SourceDefault &&
373 package->folders[i].SourceDefault[0]!='.')
374 path = build_directory_name(3, p, package->folders[i].SourceDefault, NULL);
377 TRACE(" (source)resolved into %s\n",debugstr_w(path));
378 package->folders[i].ResolvedSource = strdupW(path);
380 HeapFree(GetProcessHeap(),0,p);
385 /* wrapper to resist a need for a full rewrite right now */
386 DWORD deformat_string(MSIPACKAGE *package, LPCWSTR ptr, WCHAR** data )
390 MSIRECORD *rec = MSI_CreateRecord(1);
393 MSI_RecordSetStringW(rec,0,ptr);
394 MSI_FormatRecordW(package,rec,NULL,&size);
398 *data = HeapAlloc(GetProcessHeap(),0,size*sizeof(WCHAR));
400 MSI_FormatRecordW(package,rec,*data,&size);
403 msiobj_release( &rec->hdr );
404 return sizeof(WCHAR)*size;
406 msiobj_release( &rec->hdr );
413 UINT schedule_action(MSIPACKAGE *package, UINT script, LPCWSTR action)
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]++;
427 newbuf = HeapReAlloc(GetProcessHeap(),0,
428 package->script->Actions[script],
429 package->script->ActionCount[script]* sizeof(LPWSTR));
431 newbuf = HeapAlloc(GetProcessHeap(),0, sizeof(LPWSTR));
433 newbuf[count] = strdupW(action);
434 package->script->Actions[script] = newbuf;
436 return ERROR_SUCCESS;
439 static void remove_tracked_tempfiles(MSIPACKAGE* package)
446 for (i = 0; i < package->loaded_files; i++)
448 if (package->files[i].Temporary)
450 TRACE("Cleaning up %s\n",debugstr_w(package->files[i].TargetPath));
451 DeleteFileW(package->files[i].TargetPath);
457 /* Called when the package is being closed */
458 void ACTION_free_package_structures( MSIPACKAGE* package)
462 TRACE("Freeing package action data\n");
464 remove_tracked_tempfiles(package);
466 /* No dynamic buffers in features */
467 if (package->features && package->loaded_features > 0)
468 HeapFree(GetProcessHeap(),0,package->features);
470 for (i = 0; i < package->loaded_folders; i++)
472 HeapFree(GetProcessHeap(),0,package->folders[i].Directory);
473 HeapFree(GetProcessHeap(),0,package->folders[i].TargetDefault);
474 HeapFree(GetProcessHeap(),0,package->folders[i].SourceDefault);
475 HeapFree(GetProcessHeap(),0,package->folders[i].ResolvedTarget);
476 HeapFree(GetProcessHeap(),0,package->folders[i].ResolvedSource);
477 HeapFree(GetProcessHeap(),0,package->folders[i].Property);
479 if (package->folders && package->loaded_folders > 0)
480 HeapFree(GetProcessHeap(),0,package->folders);
482 for (i = 0; i < package->loaded_components; i++)
483 HeapFree(GetProcessHeap(),0,package->components[i].FullKeypath);
485 if (package->components && package->loaded_components > 0)
486 HeapFree(GetProcessHeap(),0,package->components);
488 for (i = 0; i < package->loaded_files; i++)
490 HeapFree(GetProcessHeap(),0,package->files[i].File);
491 HeapFree(GetProcessHeap(),0,package->files[i].FileName);
492 HeapFree(GetProcessHeap(),0,package->files[i].ShortName);
493 HeapFree(GetProcessHeap(),0,package->files[i].Version);
494 HeapFree(GetProcessHeap(),0,package->files[i].Language);
495 HeapFree(GetProcessHeap(),0,package->files[i].SourcePath);
496 HeapFree(GetProcessHeap(),0,package->files[i].TargetPath);
499 if (package->files && package->loaded_files > 0)
500 HeapFree(GetProcessHeap(),0,package->files);
502 /* clean up extension, progid, class and verb structures */
503 for (i = 0; i < package->loaded_classes; i++)
505 HeapFree(GetProcessHeap(),0,package->classes[i].Description);
506 HeapFree(GetProcessHeap(),0,package->classes[i].FileTypeMask);
507 HeapFree(GetProcessHeap(),0,package->classes[i].IconPath);
508 HeapFree(GetProcessHeap(),0,package->classes[i].DefInprocHandler);
509 HeapFree(GetProcessHeap(),0,package->classes[i].DefInprocHandler32);
510 HeapFree(GetProcessHeap(),0,package->classes[i].Argument);
511 HeapFree(GetProcessHeap(),0,package->classes[i].ProgIDText);
514 if (package->classes && package->loaded_classes > 0)
515 HeapFree(GetProcessHeap(),0,package->classes);
517 for (i = 0; i < package->loaded_extensions; i++)
519 HeapFree(GetProcessHeap(),0,package->extensions[i].ProgIDText);
522 if (package->extensions && package->loaded_extensions > 0)
523 HeapFree(GetProcessHeap(),0,package->extensions);
525 for (i = 0; i < package->loaded_progids; i++)
527 HeapFree(GetProcessHeap(),0,package->progids[i].ProgID);
528 HeapFree(GetProcessHeap(),0,package->progids[i].Description);
529 HeapFree(GetProcessHeap(),0,package->progids[i].IconPath);
532 if (package->progids && package->loaded_progids > 0)
533 HeapFree(GetProcessHeap(),0,package->progids);
535 for (i = 0; i < package->loaded_verbs; i++)
537 HeapFree(GetProcessHeap(),0,package->verbs[i].Verb);
538 HeapFree(GetProcessHeap(),0,package->verbs[i].Command);
539 HeapFree(GetProcessHeap(),0,package->verbs[i].Argument);
542 if (package->verbs && package->loaded_verbs > 0)
543 HeapFree(GetProcessHeap(),0,package->verbs);
545 for (i = 0; i < package->loaded_mimes; i++)
546 HeapFree(GetProcessHeap(),0,package->mimes[i].ContentType);
548 if (package->mimes && package->loaded_mimes > 0)
549 HeapFree(GetProcessHeap(),0,package->mimes);
551 for (i = 0; i < package->loaded_appids; i++)
553 HeapFree(GetProcessHeap(),0,package->appids[i].RemoteServerName);
554 HeapFree(GetProcessHeap(),0,package->appids[i].LocalServer);
555 HeapFree(GetProcessHeap(),0,package->appids[i].ServiceParameters);
556 HeapFree(GetProcessHeap(),0,package->appids[i].DllSurrogate);
559 if (package->appids && package->loaded_appids > 0)
560 HeapFree(GetProcessHeap(),0,package->appids);
564 for (i = 0; i < TOTAL_SCRIPTS; i++)
567 for (j = 0; j < package->script->ActionCount[i]; j++)
568 HeapFree(GetProcessHeap(),0,package->script->Actions[i][j]);
570 HeapFree(GetProcessHeap(),0,package->script->Actions[i]);
572 HeapFree(GetProcessHeap(),0,package->script);
575 HeapFree(GetProcessHeap(),0,package->PackagePath);
577 /* cleanup control event subscriptions */
578 ControlEvent_CleanupSubscriptions(package);
582 * build_directory_name()
584 * This function is to save messing round with directory names
585 * It handles adding backslashes between path segments,
586 * and can add \ at the end of the directory name if told to.
588 * It takes a variable number of arguments.
589 * It always allocates a new string for the result, so make sure
590 * to free the return value when finished with it.
592 * The first arg is the number of path segments that follow.
593 * The arguments following count are a list of path segments.
594 * A path segment may be NULL.
596 * Path segments will be added with a \ separating them.
597 * A \ will not be added after the last segment, however if the
598 * last segment is NULL, then the last character will be a \
601 LPWSTR build_directory_name(DWORD count, ...)
608 for(i=0; i<count; i++)
610 LPCWSTR str = va_arg(va,LPCWSTR);
612 sz += strlenW(str) + 1;
616 dir = HeapAlloc(GetProcessHeap(), 0, sz*sizeof(WCHAR));
620 for(i=0; i<count; i++)
622 LPCWSTR str = va_arg(va,LPCWSTR);
626 if( ((i+1)!=count) && dir[strlenW(dir)-1]!='\\')
632 /***********************************************************************
635 * Recursively create all directories in the path.
637 * shamelessly stolen from setupapi/queue.c
639 BOOL create_full_pathW(const WCHAR *path)
645 new_path = HeapAlloc(GetProcessHeap(), 0, (strlenW(path) + 1) *
648 strcpyW(new_path, path);
650 while((len = strlenW(new_path)) && new_path[len - 1] == '\\')
651 new_path[len - 1] = 0;
653 while(!CreateDirectoryW(new_path, NULL))
656 DWORD last_error = GetLastError();
657 if(last_error == ERROR_ALREADY_EXISTS)
660 if(last_error != ERROR_PATH_NOT_FOUND)
666 if(!(slash = strrchrW(new_path, '\\')))
672 len = slash - new_path;
674 if(!create_full_pathW(new_path))
679 new_path[len] = '\\';
682 HeapFree(GetProcessHeap(), 0, new_path);
686 void ui_progress(MSIPACKAGE *package, int a, int b, int c, int d )
690 row = MSI_CreateRecord(4);
691 MSI_RecordSetInteger(row,1,a);
692 MSI_RecordSetInteger(row,2,b);
693 MSI_RecordSetInteger(row,3,c);
694 MSI_RecordSetInteger(row,4,d);
695 MSI_ProcessMessage(package, INSTALLMESSAGE_PROGRESS, row);
696 msiobj_release(&row->hdr);
698 msi_dialog_check_messages(NULL);
701 void ui_actiondata(MSIPACKAGE *package, LPCWSTR action, MSIRECORD * record)
703 static const WCHAR Query_t[] =
704 {'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
705 '`','A','c','t','i','o', 'n','T','e','x','t','`',' ',
706 'W','H','E','R','E',' ', '`','A','c','t','i','o','n','`',' ','=',
707 ' ','\'','%','s','\'',0};
711 static const WCHAR szActionData[] =
712 {'A','c','t','i','o','n','D','a','t','a',0};
714 if (!package->LastAction || strcmpW(package->LastAction,action))
716 row = MSI_QueryGetRecord(package->db, Query_t, action);
720 if (MSI_RecordIsNull(row,3))
722 msiobj_release(&row->hdr);
726 /* update the cached actionformat */
727 HeapFree(GetProcessHeap(),0,package->ActionFormat);
728 package->ActionFormat = load_dynamic_stringW(row,3);
730 HeapFree(GetProcessHeap(),0,package->LastAction);
731 package->LastAction = strdupW(action);
733 msiobj_release(&row->hdr);
736 MSI_RecordSetStringW(record,0,package->ActionFormat);
738 MSI_FormatRecordW(package,record,message,&size);
740 row = MSI_CreateRecord(1);
741 MSI_RecordSetStringW(row,1,message);
743 MSI_ProcessMessage(package, INSTALLMESSAGE_ACTIONDATA, row);
745 ControlEvent_FireSubscribedEvent(package,szActionData, row);
747 msiobj_release(&row->hdr);
750 BOOL ACTION_VerifyComponentForAction(MSIPACKAGE* package, INT index,
753 if (package->components[index].Installed == check)
756 if (package->components[index].ActionRequest == check)
762 BOOL ACTION_VerifyFeatureForAction(MSIPACKAGE* package, INT index,
765 if (package->features[index].Installed == check)
768 if (package->features[index].ActionRequest == check)
774 void reduce_to_longfilename(WCHAR* filename)
776 LPWSTR p = strchrW(filename,'|');
778 memmove(filename, p+1, (strlenW(p+1)+1)*sizeof(WCHAR));
781 void reduce_to_shortfilename(WCHAR* filename)
783 LPWSTR p = strchrW(filename,'|');
788 LPWSTR create_component_advertise_string(MSIPACKAGE* package,
789 MSICOMPONENT* component, LPCWSTR feature)
791 LPWSTR productid=NULL;
793 WCHAR productid_85[21];
794 WCHAR component_85[21];
796 * I have a fair bit of confusion as to when a < is used and when a > is
797 * used. I do not think i have it right...
799 * Ok it appears that the > is used if there is a guid for the compoenent
800 * and the < is used if not.
802 static WCHAR fmt1[] = {'%','s','%','s','<',0,0};
803 static WCHAR fmt2[] = {'%','s','%','s','>','%','s',0,0};
804 LPWSTR output = NULL;
807 memset(productid_85,0,sizeof(productid_85));
808 memset(component_85,0,sizeof(component_85));
810 productid = load_dynamic_property(package,szProductCode,NULL);
811 CLSIDFromString(productid, &clsid);
813 encode_base85_guid(&clsid,productid_85);
815 CLSIDFromString(component->ComponentId, &clsid);
816 encode_base85_guid(&clsid,component_85);
818 TRACE("Doing something with this... %s %s %s\n",
819 debugstr_w(productid_85), debugstr_w(feature),
820 debugstr_w(component_85));
822 sz = lstrlenW(productid_85) + lstrlenW(feature);
824 sz += lstrlenW(component_85);
829 output = HeapAlloc(GetProcessHeap(),0,sz);
833 sprintfW(output,fmt2,productid_85,feature,component_85);
835 sprintfW(output,fmt1,productid_85,feature);
837 HeapFree(GetProcessHeap(),0,productid);
842 /* update compoennt state based on a feature change */
843 void ACTION_UpdateComponentStates(MSIPACKAGE *package, LPCWSTR szFeature)
846 INSTALLSTATE newstate;
849 i = get_loaded_feature(package,szFeature);
853 feature = &package->features[i];
854 newstate = feature->ActionRequest;
856 for( i = 0; i < feature->ComponentCount; i++)
858 MSICOMPONENT* component = &package->components[feature->Components[i]];
860 TRACE("MODIFYING(%i): Component %s (Installed %i, Action %i, Request %i)\n",
861 newstate, debugstr_w(component->Component), component->Installed,
862 component->Action, component->ActionRequest);
864 if (!component->Enabled)
868 if (newstate == INSTALLSTATE_LOCAL)
870 component->ActionRequest = INSTALLSTATE_LOCAL;
871 component->Action = INSTALLSTATE_LOCAL;
877 component->ActionRequest = newstate;
878 component->Action = newstate;
880 /*if any other feature wants is local we need to set it local*/
882 j < package->loaded_features &&
883 component->ActionRequest != INSTALLSTATE_LOCAL;
886 for (k = 0; k < package->features[j].ComponentCount; k++)
887 if ( package->features[j].Components[k] ==
888 feature->Components[i] )
890 if (package->features[j].ActionRequest ==
893 TRACE("Saved by %s\n", debugstr_w(package->features[j].Feature));
894 component->ActionRequest = INSTALLSTATE_LOCAL;
895 component->Action = INSTALLSTATE_LOCAL;
902 TRACE("Result (%i): Component %s (Installed %i, Action %i, Request %i)\n",
903 newstate, debugstr_w(component->Component), component->Installed,
904 component->Action, component->ActionRequest);