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.
29 #include "wine/debug.h"
32 #include "wine/unicode.h"
35 WINE_DEFAULT_DEBUG_CHANNEL(msi);
37 static const WCHAR cszTargetDir[] = {'T','A','R','G','E','T','D','I','R',0};
38 static const WCHAR cszDatabase[]={'D','A','T','A','B','A','S','E',0};
40 LPWSTR build_icon_path(MSIPACKAGE *package, LPCWSTR icon_name )
42 LPWSTR SystemFolder, dest, FilePath;
44 static const WCHAR szMicrosoft[] =
45 {'M','i','c','r','o','s','o','f','t','\\',0};
46 static const WCHAR szInstaller[] =
47 {'I','n','s','t','a','l','l','e','r','\\',0};
48 static const WCHAR szADFolder[] =
49 {'A','p','p','D','a','t','a','F','o','l','d','e','r',0};
50 static const WCHAR szWFolder[] =
51 {'W','i','n','d','o','w','s','F','o','l','d','e','r',0};
53 if(package->Context == MSIINSTALLCONTEXT_MACHINE)
54 SystemFolder = msi_dup_property( package->db, szWFolder );
57 LPWSTR ADTgt = msi_dup_property( package->db, szADFolder );
58 SystemFolder = build_directory_name(2, ADTgt, szMicrosoft);
62 dest = build_directory_name(3, SystemFolder, szInstaller, package->ProductCode);
64 create_full_pathW(dest);
66 FilePath = build_directory_name(2, dest, icon_name);
68 msi_free(SystemFolder);
73 LPWSTR msi_dup_record_field( MSIRECORD *rec, INT field )
79 if (MSI_RecordIsNull( rec, field ))
82 r = MSI_RecordGetStringW( rec, field, NULL, &sz );
83 if (r != ERROR_SUCCESS)
87 str = msi_alloc( sz * sizeof (WCHAR) );
91 r = MSI_RecordGetStringW( rec, field, str, &sz );
92 if (r != ERROR_SUCCESS)
94 ERR("failed to get string!\n");
101 MSICOMPONENT* get_loaded_component( MSIPACKAGE* package, LPCWSTR Component )
105 LIST_FOR_EACH_ENTRY( comp, &package->components, MSICOMPONENT, entry )
107 if (!strcmpW( Component, comp->Component ))
113 MSIFEATURE* get_loaded_feature(MSIPACKAGE* package, LPCWSTR Feature )
117 LIST_FOR_EACH_ENTRY( feature, &package->features, MSIFEATURE, entry )
119 if (!strcmpW( Feature, feature->Feature ))
125 MSIFILE* get_loaded_file( MSIPACKAGE* package, LPCWSTR key )
129 LIST_FOR_EACH_ENTRY( file, &package->files, MSIFILE, entry )
131 if (!strcmpW( key, file->File ))
137 int track_tempfile( MSIPACKAGE *package, LPCWSTR path )
141 TRACE("%s\n", debugstr_w(path));
143 LIST_FOR_EACH_ENTRY( temp, &package->tempfiles, MSITEMPFILE, entry )
144 if (!strcmpW( path, temp->Path ))
147 temp = msi_alloc_zero( sizeof (MSITEMPFILE) );
151 list_add_head( &package->tempfiles, &temp->entry );
152 temp->Path = strdupW( path );
157 MSIFOLDER *get_loaded_folder( MSIPACKAGE *package, LPCWSTR dir )
161 LIST_FOR_EACH_ENTRY( folder, &package->folders, MSIFOLDER, entry )
163 if (!strcmpW( dir, folder->Directory ))
169 static LPWSTR get_source_root( MSIPACKAGE *package )
173 path = msi_dup_property( package->db, cszSourceDir );
177 path = msi_dup_property( package->db, cszDatabase );
180 p = strrchrW(path,'\\');
188 * clean_spaces_from_path()
190 * removes spaces from the beginning and end of path segments
191 * removes multiple \\ characters
193 static void clean_spaces_from_path( LPWSTR p )
200 /* copy until the end of the string or a space */
201 while (*p != ' ' && (*q = *p))
204 /* reduce many backslashes to one */
205 if (*p != '\\' || *q != '\\')
209 /* quit at the end of the string */
213 /* count the number of spaces */
218 /* if it's leading or trailing space, skip it */
219 if ( len == 0 || p[-1] == '\\' || p[n] == '\\' )
221 else /* copy n spaces */
222 while (n && (*q++ = *p++)) n--;
226 LPWSTR resolve_file_source(MSIPACKAGE *package, MSIFILE *file)
230 TRACE("Working to resolve source of file %s\n", debugstr_w(file->File));
232 if (file->IsCompressed)
235 p = resolve_source_folder( package, file->Component->Directory, NULL );
236 path = build_directory_name(2, p, file->ShortName);
238 if (file->LongName &&
239 GetFileAttributesW(path) == INVALID_FILE_ATTRIBUTES)
242 path = build_directory_name(2, p, file->LongName);
247 TRACE("file %s source resolves to %s\n", debugstr_w(file->File), debugstr_w(path));
251 LPWSTR resolve_source_folder( MSIPACKAGE *package, LPCWSTR name, MSIFOLDER **folder )
254 LPWSTR p, path = NULL, parent;
256 TRACE("working to resolve %s\n", debugstr_w(name));
258 if (!strcmpW( name, cszSourceDir ))
261 f = get_loaded_folder( package, name );
265 /* special resolving for Target and Source root dir */
266 if (!strcmpW( name, cszTargetDir ))
268 if (!f->ResolvedSource)
269 f->ResolvedSource = get_source_root( package );
275 if (f->ResolvedSource)
277 path = strdupW( f->ResolvedSource );
278 TRACE(" already resolved to %s\n", debugstr_w(path));
286 TRACE(" ! parent is %s\n", debugstr_w(parent));
288 p = resolve_source_folder( package, parent, NULL );
290 if (package->WordCount & msidbSumInfoSourceTypeCompressed)
291 path = get_source_root( package );
292 else if (package->WordCount & msidbSumInfoSourceTypeSFN)
293 path = build_directory_name( 3, p, f->SourceShortPath, NULL );
295 path = build_directory_name( 3, p, f->SourceLongPath, NULL );
297 TRACE("-> %s\n", debugstr_w(path));
298 f->ResolvedSource = strdupW( path );
304 LPWSTR resolve_target_folder( MSIPACKAGE *package, LPCWSTR name, BOOL set_prop, BOOL load_prop,
308 LPWSTR p, path = NULL, parent;
310 TRACE("working to resolve %s\n", debugstr_w(name));
312 f = get_loaded_folder( package, name );
316 /* special resolving for Target and Source root dir */
317 if (!strcmpW( name, cszTargetDir ))
319 if (!f->ResolvedTarget && !f->Property)
322 check_path = msi_dup_property( package->db, cszTargetDir );
325 check_path = msi_dup_property( package->db, cszRootDrive );
327 msi_set_property( package->db, cszTargetDir, check_path );
330 /* correct misbuilt target dir */
331 path = build_directory_name(2, check_path, NULL);
332 clean_spaces_from_path( path );
333 if (strcmpiW( path, check_path ))
334 msi_set_property( package->db, cszTargetDir, path );
335 msi_free(check_path);
337 f->ResolvedTarget = path;
344 if (f->ResolvedTarget)
346 path = strdupW( f->ResolvedTarget );
347 TRACE(" already resolved to %s\n", debugstr_w(path));
353 path = build_directory_name( 2, f->Property, NULL );
354 TRACE(" internally set to %s\n", debugstr_w(path));
355 if (set_prop) msi_set_property( package->db, name, path );
359 if (load_prop && (path = msi_dup_property( package->db, name )))
361 f->ResolvedTarget = strdupW( path );
362 TRACE(" property set to %s\n", debugstr_w(path));
371 TRACE(" ! parent is %s\n", debugstr_w(parent));
373 p = resolve_target_folder( package, parent, set_prop, load_prop, NULL );
375 TRACE(" TargetDefault = %s\n", debugstr_w(f->TargetDefault));
376 path = build_directory_name( 3, p, f->TargetDefault, NULL );
377 clean_spaces_from_path( path );
378 f->ResolvedTarget = strdupW( path );
380 TRACE("-> %s\n", debugstr_w(path));
381 if (set_prop) msi_set_property( package->db, name, path );
387 /* wrapper to resist a need for a full rewrite right now */
388 DWORD deformat_string(MSIPACKAGE *package, LPCWSTR ptr, WCHAR** data )
392 MSIRECORD *rec = MSI_CreateRecord(1);
395 MSI_RecordSetStringW(rec,0,ptr);
396 MSI_FormatRecordW(package,rec,NULL,&size);
399 *data = msi_alloc(size*sizeof(WCHAR));
401 MSI_FormatRecordW(package,rec,*data,&size);
405 msiobj_release( &rec->hdr );
406 return sizeof(WCHAR)*size;
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 = msi_realloc( package->script->Actions[script],
428 package->script->ActionCount[script]* sizeof(LPWSTR));
430 newbuf = msi_alloc( sizeof(LPWSTR));
432 newbuf[count] = strdupW(action);
433 package->script->Actions[script] = newbuf;
435 return ERROR_SUCCESS;
438 void msi_free_action_script(MSIPACKAGE *package, UINT script)
441 for (i = 0; i < package->script->ActionCount[script]; i++)
442 msi_free(package->script->Actions[script][i]);
444 msi_free(package->script->Actions[script]);
445 package->script->Actions[script] = NULL;
446 package->script->ActionCount[script] = 0;
450 * build_directory_name()
452 * This function is to save messing round with directory names
453 * It handles adding backslashes between path segments,
454 * and can add \ at the end of the directory name if told to.
456 * It takes a variable number of arguments.
457 * It always allocates a new string for the result, so make sure
458 * to free the return value when finished with it.
460 * The first arg is the number of path segments that follow.
461 * The arguments following count are a list of path segments.
462 * A path segment may be NULL.
464 * Path segments will be added with a \ separating them.
465 * A \ will not be added after the last segment, however if the
466 * last segment is NULL, then the last character will be a \
469 LPWSTR build_directory_name(DWORD count, ...)
476 for(i=0; i<count; i++)
478 LPCWSTR str = va_arg(va,LPCWSTR);
480 sz += strlenW(str) + 1;
484 dir = msi_alloc(sz*sizeof(WCHAR));
488 for(i=0; i<count; i++)
490 LPCWSTR str = va_arg(va,LPCWSTR);
494 if( ((i+1)!=count) && dir[strlenW(dir)-1]!='\\')
495 strcatW(dir, szBackSlash);
502 /***********************************************************************
505 * Recursively create all directories in the path.
507 * shamelessly stolen from setupapi/queue.c
509 BOOL create_full_pathW(const WCHAR *path)
515 new_path = msi_alloc( (strlenW(path) + 1) * sizeof(WCHAR));
517 strcpyW(new_path, path);
519 while((len = strlenW(new_path)) && new_path[len - 1] == '\\')
520 new_path[len - 1] = 0;
522 while(!CreateDirectoryW(new_path, NULL))
525 DWORD last_error = GetLastError();
526 if(last_error == ERROR_ALREADY_EXISTS)
529 if(last_error != ERROR_PATH_NOT_FOUND)
535 if(!(slash = strrchrW(new_path, '\\')))
541 len = slash - new_path;
543 if(!create_full_pathW(new_path))
548 new_path[len] = '\\';
555 void ui_progress(MSIPACKAGE *package, int a, int b, int c, int d )
559 row = MSI_CreateRecord(4);
560 MSI_RecordSetInteger(row,1,a);
561 MSI_RecordSetInteger(row,2,b);
562 MSI_RecordSetInteger(row,3,c);
563 MSI_RecordSetInteger(row,4,d);
564 MSI_ProcessMessage(package, INSTALLMESSAGE_PROGRESS, row);
565 msiobj_release(&row->hdr);
567 msi_dialog_check_messages(NULL);
570 void ui_actiondata(MSIPACKAGE *package, LPCWSTR action, MSIRECORD * record)
572 static const WCHAR Query_t[] =
573 {'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
574 '`','A','c','t','i','o', 'n','T','e','x','t','`',' ',
575 'W','H','E','R','E',' ', '`','A','c','t','i','o','n','`',' ','=',
576 ' ','\'','%','s','\'',0};
581 if (!package->LastAction || strcmpW(package->LastAction, action))
583 row = MSI_QueryGetRecord(package->db, Query_t, action);
587 if (MSI_RecordIsNull(row,3))
589 msiobj_release(&row->hdr);
593 /* update the cached actionformat */
594 msi_free(package->ActionFormat);
595 package->ActionFormat = msi_dup_record_field(row,3);
597 msi_free(package->LastAction);
598 package->LastAction = strdupW(action);
600 msiobj_release(&row->hdr);
603 MSI_RecordSetStringW(record,0,package->ActionFormat);
605 MSI_FormatRecordW(package,record,message,&size);
607 row = MSI_CreateRecord(1);
608 MSI_RecordSetStringW(row,1,message);
610 MSI_ProcessMessage(package, INSTALLMESSAGE_ACTIONDATA, row);
612 msiobj_release(&row->hdr);
615 void reduce_to_longfilename(WCHAR* filename)
617 LPWSTR p = strchrW(filename,'|');
619 memmove(filename, p+1, (strlenW(p+1)+1)*sizeof(WCHAR));
622 LPWSTR create_component_advertise_string(MSIPACKAGE* package,
623 MSICOMPONENT* component, LPCWSTR feature)
625 static const WCHAR fmt[] = {'%','s','%','s','%','c','%','s',0};
626 WCHAR productid_85[21], component_85[21];
627 LPWSTR output = NULL;
631 /* > is used if there is a component GUID and < if not. */
636 CLSIDFromString(package->ProductCode, &clsid);
637 encode_base85_guid(&clsid, productid_85);
641 CLSIDFromString(component->ComponentId, &clsid);
642 encode_base85_guid(&clsid, component_85);
645 TRACE("prod=%s feat=%s comp=%s\n", debugstr_w(productid_85),
646 debugstr_w(feature), debugstr_w(component_85));
648 sz = 20 + lstrlenW(feature) + 20 + 3;
650 output = msi_alloc_zero(sz*sizeof(WCHAR));
652 sprintfW(output, fmt, productid_85, feature,
653 component?'>':'<', component_85);
658 /* update component state based on a feature change */
659 void ACTION_UpdateComponentStates( MSIPACKAGE *package, MSIFEATURE *feature )
661 INSTALLSTATE newstate;
664 newstate = feature->ActionRequest;
666 if (newstate == INSTALLSTATE_ABSENT)
667 newstate = INSTALLSTATE_UNKNOWN;
669 LIST_FOR_EACH_ENTRY( cl, &feature->Components, ComponentList, entry )
671 MSICOMPONENT* component = cl->component;
673 if (!component->Enabled) continue;
675 TRACE("MODIFYING(%i): Component %s (Installed %i, Action %i, Request %i)\n",
676 newstate, debugstr_w(component->Component), component->Installed,
677 component->Action, component->ActionRequest);
679 if (newstate == INSTALLSTATE_LOCAL)
681 component->Action = INSTALLSTATE_LOCAL;
682 component->ActionRequest = INSTALLSTATE_LOCAL;
686 ComponentList *clist;
689 component->hasLocalFeature = FALSE;
691 component->Action = newstate;
692 component->ActionRequest = newstate;
694 /* if any other feature wants it local we need to set it local */
695 LIST_FOR_EACH_ENTRY( f, &package->features, MSIFEATURE, entry )
697 if ( f->ActionRequest != INSTALLSTATE_LOCAL &&
698 f->ActionRequest != INSTALLSTATE_SOURCE )
703 LIST_FOR_EACH_ENTRY( clist, &f->Components, ComponentList, entry )
705 if ( clist->component == component &&
706 (f->ActionRequest == INSTALLSTATE_LOCAL ||
707 f->ActionRequest == INSTALLSTATE_SOURCE) )
709 TRACE("Saved by %s\n", debugstr_w(f->Feature));
710 component->hasLocalFeature = TRUE;
712 if (component->Attributes & msidbComponentAttributesOptional)
714 if (f->Attributes & msidbFeatureAttributesFavorSource)
716 component->Action = INSTALLSTATE_SOURCE;
717 component->ActionRequest = INSTALLSTATE_SOURCE;
721 component->Action = INSTALLSTATE_LOCAL;
722 component->ActionRequest = INSTALLSTATE_LOCAL;
725 else if (component->Attributes & msidbComponentAttributesSourceOnly)
727 component->Action = INSTALLSTATE_SOURCE;
728 component->ActionRequest = INSTALLSTATE_SOURCE;
732 component->Action = INSTALLSTATE_LOCAL;
733 component->ActionRequest = INSTALLSTATE_LOCAL;
739 TRACE("Result (%i): Component %s (Installed %i, Action %i, Request %i)\n",
740 newstate, debugstr_w(component->Component), component->Installed,
741 component->Action, component->ActionRequest);
745 UINT register_unique_action(MSIPACKAGE *package, LPCWSTR action)
748 LPWSTR *newbuf = NULL;
750 if (!package->script)
753 TRACE("Registering %s as unique action\n", debugstr_w(action));
755 count = package->script->UniqueActionsCount;
756 package->script->UniqueActionsCount++;
758 newbuf = msi_realloc( package->script->UniqueActions,
759 package->script->UniqueActionsCount* sizeof(LPWSTR));
761 newbuf = msi_alloc( sizeof(LPWSTR));
763 newbuf[count] = strdupW(action);
764 package->script->UniqueActions = newbuf;
766 return ERROR_SUCCESS;
769 BOOL check_unique_action(const MSIPACKAGE *package, LPCWSTR action)
773 if (!package->script)
776 for (i = 0; i < package->script->UniqueActionsCount; i++)
777 if (!strcmpW(package->script->UniqueActions[i], action))
783 WCHAR* generate_error_string(MSIPACKAGE *package, UINT error, DWORD count, ... )
785 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};
795 row = MSI_QueryGetRecord(package->db, query, error);
799 rec = MSI_CreateRecord(count+2);
801 str = MSI_RecordGetString(row,1);
802 MSI_RecordSetStringW(rec,0,str);
803 msiobj_release( &row->hdr );
804 MSI_RecordSetInteger(rec,1,error);
807 for (i = 0; i < count; i++)
809 str = va_arg(va,LPCWSTR);
810 MSI_RecordSetStringW(rec,(i+2),str);
814 MSI_FormatRecordW(package,rec,NULL,&size);
817 data = msi_alloc(size*sizeof(WCHAR));
819 MSI_FormatRecordW(package,rec,data,&size);
822 msiobj_release( &rec->hdr );