push ac4b6ebdd3fd886d6a18959265755579ed195b88
[wine/hacks.git] / dlls / msi / helpers.c
blobe3009f7bce7948abd3091a24c22185e8821b5773
1 /*
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.
26 #include <stdarg.h>
28 #include "windef.h"
29 #include "wine/debug.h"
30 #include "msipriv.h"
31 #include "winuser.h"
32 #include "wine/unicode.h"
33 #include "msidefs.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 szInstaller[] =
45 {'M','i','c','r','o','s','o','f','t','\\',
46 'I','n','s','t','a','l','l','e','r','\\',0};
47 static const WCHAR szFolder[] =
48 {'A','p','p','D','a','t','a','F','o','l','d','e','r',0};
50 SystemFolder = msi_dup_property( package, szFolder );
52 dest = build_directory_name(3, SystemFolder, szInstaller, package->ProductCode);
54 create_full_pathW(dest);
56 FilePath = build_directory_name(2, dest, icon_name);
58 msi_free(SystemFolder);
59 msi_free(dest);
60 return FilePath;
63 LPWSTR msi_dup_record_field( MSIRECORD *rec, INT field )
65 DWORD sz = 0;
66 LPWSTR str;
67 UINT r;
69 if (MSI_RecordIsNull( rec, field ))
70 return NULL;
72 r = MSI_RecordGetStringW( rec, field, NULL, &sz );
73 if (r != ERROR_SUCCESS)
74 return NULL;
76 sz ++;
77 str = msi_alloc( sz * sizeof (WCHAR) );
78 if (!str)
79 return str;
80 str[0] = 0;
81 r = MSI_RecordGetStringW( rec, field, str, &sz );
82 if (r != ERROR_SUCCESS)
84 ERR("failed to get string!\n");
85 msi_free( str );
86 return NULL;
88 return str;
91 MSICOMPONENT* get_loaded_component( MSIPACKAGE* package, LPCWSTR Component )
93 MSICOMPONENT *comp;
95 LIST_FOR_EACH_ENTRY( comp, &package->components, MSICOMPONENT, entry )
97 if (lstrcmpW(Component,comp->Component)==0)
98 return comp;
100 return NULL;
103 MSIFEATURE* get_loaded_feature(MSIPACKAGE* package, LPCWSTR Feature )
105 MSIFEATURE *feature;
107 LIST_FOR_EACH_ENTRY( feature, &package->features, MSIFEATURE, entry )
109 if (lstrcmpW( Feature, feature->Feature )==0)
110 return feature;
112 return NULL;
115 MSIFILE* get_loaded_file( MSIPACKAGE* package, LPCWSTR key )
117 MSIFILE *file;
119 LIST_FOR_EACH_ENTRY( file, &package->files, MSIFILE, entry )
121 if (lstrcmpW( key, file->File )==0)
122 return file;
124 return NULL;
127 int track_tempfile( MSIPACKAGE *package, LPCWSTR path )
129 MSITEMPFILE *temp;
131 TRACE("%s\n", debugstr_w(path));
133 LIST_FOR_EACH_ENTRY( temp, &package->tempfiles, MSITEMPFILE, entry )
134 if (!lstrcmpW( path, temp->Path ))
135 return 0;
137 temp = msi_alloc_zero( sizeof (MSITEMPFILE) );
138 if (!temp)
139 return -1;
141 list_add_head( &package->tempfiles, &temp->entry );
142 temp->Path = strdupW( path );
144 return 0;
147 MSIFOLDER *get_loaded_folder( MSIPACKAGE *package, LPCWSTR dir )
149 MSIFOLDER *folder;
151 LIST_FOR_EACH_ENTRY( folder, &package->folders, MSIFOLDER, entry )
153 if (lstrcmpW( dir, folder->Directory )==0)
154 return folder;
156 return NULL;
159 void msi_reset_folders( MSIPACKAGE *package, BOOL source )
161 MSIFOLDER *folder;
163 LIST_FOR_EACH_ENTRY( folder, &package->folders, MSIFOLDER, entry )
165 if ( source )
167 msi_free( folder->ResolvedSource );
168 folder->ResolvedSource = NULL;
170 else
172 msi_free( folder->ResolvedTarget );
173 folder->ResolvedTarget = NULL;
178 static LPWSTR get_source_root( MSIPACKAGE *package )
180 LPWSTR path, p;
182 path = msi_dup_property( package, cszSourceDir );
183 if (path)
184 return path;
186 path = msi_dup_property( package, cszDatabase );
187 if (path)
189 p = strrchrW(path,'\\');
190 if (p)
191 *(p+1) = 0;
193 return path;
197 * clean_spaces_from_path()
199 * removes spaces from the beginning and end of path segments
200 * removes multiple \\ characters
202 static void clean_spaces_from_path( LPWSTR p )
204 LPWSTR q = p;
205 int n, len = 0;
207 while (1)
209 /* copy until the end of the string or a space */
210 while (*p != ' ' && (*q = *p))
212 p++, len++;
213 /* reduce many backslashes to one */
214 if (*p != '\\' || *q != '\\')
215 q++;
218 /* quit at the end of the string */
219 if (!*p)
220 break;
222 /* count the number of spaces */
223 n = 0;
224 while (p[n] == ' ')
225 n++;
227 /* if it's leading or trailing space, skip it */
228 if ( len == 0 || p[-1] == '\\' || p[n] == '\\' )
229 p += n;
230 else /* copy n spaces */
231 while (n && (*q++ = *p++)) n--;
235 LPWSTR resolve_file_source(MSIPACKAGE *package, MSIFILE *file)
237 LPWSTR p, path;
239 TRACE("Working to resolve source of file %s\n", debugstr_w(file->File));
241 if (file->IsCompressed)
242 return NULL;
244 p = resolve_folder(package, file->Component->Directory,
245 TRUE, FALSE, TRUE, NULL);
246 path = build_directory_name(2, p, file->ShortName);
248 if (file->LongName &&
249 GetFileAttributesW(path) == INVALID_FILE_ATTRIBUTES)
251 msi_free(path);
252 path = build_directory_name(2, p, file->LongName);
255 msi_free(p);
257 TRACE("file %s source resolves to %s\n", debugstr_w(file->File),
258 debugstr_w(path));
260 return path;
263 LPWSTR resolve_folder(MSIPACKAGE *package, LPCWSTR name, BOOL source,
264 BOOL set_prop, BOOL load_prop, MSIFOLDER **folder)
266 MSIFOLDER *f;
267 LPWSTR p, path = NULL, parent;
269 TRACE("Working to resolve %s\n",debugstr_w(name));
271 if (!name)
272 return NULL;
274 if (!lstrcmpW(name,cszSourceDir))
275 name = cszTargetDir;
277 f = get_loaded_folder( package, name );
278 if (!f)
279 return NULL;
281 /* special resolving for Target and Source root dir */
282 if (!strcmpW(name,cszTargetDir))
284 if (!f->ResolvedTarget && !f->Property)
286 LPWSTR check_path;
287 check_path = msi_dup_property( package, cszTargetDir );
288 if (!check_path)
290 check_path = msi_dup_property( package, cszRootDrive );
291 if (set_prop)
292 MSI_SetPropertyW(package,cszTargetDir,check_path);
295 /* correct misbuilt target dir */
296 path = build_directory_name(2, check_path, NULL);
297 clean_spaces_from_path( path );
298 if (strcmpiW(path,check_path)!=0)
299 MSI_SetPropertyW(package,cszTargetDir,path);
300 msi_free(check_path);
302 f->ResolvedTarget = path;
305 if (!f->ResolvedSource)
306 f->ResolvedSource = get_source_root( package );
309 if (folder)
310 *folder = f;
312 if (!source && f->ResolvedTarget)
314 path = strdupW( f->ResolvedTarget );
315 TRACE(" already resolved to %s\n",debugstr_w(path));
316 return path;
319 if (source && f->ResolvedSource)
321 path = strdupW( f->ResolvedSource );
322 TRACE(" (source)already resolved to %s\n",debugstr_w(path));
323 return path;
326 if (!source && f->Property)
328 path = build_directory_name( 2, f->Property, NULL );
330 TRACE(" internally set to %s\n",debugstr_w(path));
331 if (set_prop)
332 MSI_SetPropertyW( package, name, path );
333 return path;
336 if (!source && load_prop && (path = msi_dup_property( package, name )))
338 f->ResolvedTarget = strdupW( path );
339 TRACE(" property set to %s\n", debugstr_w(path));
340 return path;
343 if (!f->Parent)
344 return path;
346 parent = f->Parent;
348 TRACE(" ! Parent is %s\n", debugstr_w(parent));
350 p = resolve_folder(package, parent, source, set_prop, load_prop, NULL);
351 if (!source)
353 TRACE(" TargetDefault = %s\n", debugstr_w(f->TargetDefault));
355 path = build_directory_name( 3, p, f->TargetDefault, NULL );
356 clean_spaces_from_path( path );
357 f->ResolvedTarget = strdupW( path );
358 TRACE("target -> %s\n", debugstr_w(path));
359 if (set_prop)
360 MSI_SetPropertyW(package,name,path);
362 else
364 path = NULL;
366 if (package->WordCount & msidbSumInfoSourceTypeCompressed)
367 path = get_source_root( package );
368 else if (package->WordCount & msidbSumInfoSourceTypeSFN)
369 path = build_directory_name( 3, p, f->SourceShortPath, NULL );
370 else
371 path = build_directory_name( 3, p, f->SourceLongPath, NULL );
373 TRACE("source -> %s\n", debugstr_w(path));
374 f->ResolvedSource = strdupW( path );
376 msi_free(p);
378 return path;
381 /* wrapper to resist a need for a full rewrite right now */
382 DWORD deformat_string(MSIPACKAGE *package, LPCWSTR ptr, WCHAR** data )
384 if (ptr)
386 MSIRECORD *rec = MSI_CreateRecord(1);
387 DWORD size = 0;
389 MSI_RecordSetStringW(rec,0,ptr);
390 MSI_FormatRecordW(package,rec,NULL,&size);
392 size++;
393 *data = msi_alloc(size*sizeof(WCHAR));
394 if (size > 1)
395 MSI_FormatRecordW(package,rec,*data,&size);
396 else
397 *data[0] = 0;
399 msiobj_release( &rec->hdr );
400 return sizeof(WCHAR)*size;
403 *data = NULL;
404 return 0;
407 UINT schedule_action(MSIPACKAGE *package, UINT script, LPCWSTR action)
409 UINT count;
410 LPWSTR *newbuf = NULL;
411 if (script >= TOTAL_SCRIPTS)
413 FIXME("Unknown script requested %i\n",script);
414 return ERROR_FUNCTION_FAILED;
416 TRACE("Scheduling Action %s in script %i\n",debugstr_w(action), script);
418 count = package->script->ActionCount[script];
419 package->script->ActionCount[script]++;
420 if (count != 0)
421 newbuf = msi_realloc( package->script->Actions[script],
422 package->script->ActionCount[script]* sizeof(LPWSTR));
423 else
424 newbuf = msi_alloc( sizeof(LPWSTR));
426 newbuf[count] = strdupW(action);
427 package->script->Actions[script] = newbuf;
429 return ERROR_SUCCESS;
432 void msi_free_action_script(MSIPACKAGE *package, UINT script)
434 UINT i;
435 for (i = 0; i < package->script->ActionCount[script]; i++)
436 msi_free(package->script->Actions[script][i]);
438 msi_free(package->script->Actions[script]);
439 package->script->Actions[script] = NULL;
440 package->script->ActionCount[script] = 0;
444 * build_directory_name()
446 * This function is to save messing round with directory names
447 * It handles adding backslashes between path segments,
448 * and can add \ at the end of the directory name if told to.
450 * It takes a variable number of arguments.
451 * It always allocates a new string for the result, so make sure
452 * to free the return value when finished with it.
454 * The first arg is the number of path segments that follow.
455 * The arguments following count are a list of path segments.
456 * A path segment may be NULL.
458 * Path segments will be added with a \ separating them.
459 * A \ will not be added after the last segment, however if the
460 * last segment is NULL, then the last character will be a \
463 LPWSTR build_directory_name(DWORD count, ...)
465 DWORD sz = 1, i;
466 LPWSTR dir;
467 va_list va;
469 va_start(va,count);
470 for(i=0; i<count; i++)
472 LPCWSTR str = va_arg(va,LPCWSTR);
473 if (str)
474 sz += strlenW(str) + 1;
476 va_end(va);
478 dir = msi_alloc(sz*sizeof(WCHAR));
479 dir[0]=0;
481 va_start(va,count);
482 for(i=0; i<count; i++)
484 LPCWSTR str = va_arg(va,LPCWSTR);
485 if (!str)
486 continue;
487 strcatW(dir, str);
488 if( ((i+1)!=count) && dir[strlenW(dir)-1]!='\\')
489 strcatW(dir, szBackSlash);
491 return dir;
494 /***********************************************************************
495 * create_full_pathW
497 * Recursively create all directories in the path.
499 * shamelessly stolen from setupapi/queue.c
501 BOOL create_full_pathW(const WCHAR *path)
503 BOOL ret = TRUE;
504 int len;
505 WCHAR *new_path;
507 new_path = msi_alloc( (strlenW(path) + 1) * sizeof(WCHAR));
509 strcpyW(new_path, path);
511 while((len = strlenW(new_path)) && new_path[len - 1] == '\\')
512 new_path[len - 1] = 0;
514 while(!CreateDirectoryW(new_path, NULL))
516 WCHAR *slash;
517 DWORD last_error = GetLastError();
518 if(last_error == ERROR_ALREADY_EXISTS)
519 break;
521 if(last_error != ERROR_PATH_NOT_FOUND)
523 ret = FALSE;
524 break;
527 if(!(slash = strrchrW(new_path, '\\')))
529 ret = FALSE;
530 break;
533 len = slash - new_path;
534 new_path[len] = 0;
535 if(!create_full_pathW(new_path))
537 ret = FALSE;
538 break;
540 new_path[len] = '\\';
543 msi_free(new_path);
544 return ret;
547 void ui_progress(MSIPACKAGE *package, int a, int b, int c, int d )
549 MSIRECORD * row;
551 row = MSI_CreateRecord(4);
552 MSI_RecordSetInteger(row,1,a);
553 MSI_RecordSetInteger(row,2,b);
554 MSI_RecordSetInteger(row,3,c);
555 MSI_RecordSetInteger(row,4,d);
556 MSI_ProcessMessage(package, INSTALLMESSAGE_PROGRESS, row);
557 msiobj_release(&row->hdr);
559 msi_dialog_check_messages(NULL);
562 void ui_actiondata(MSIPACKAGE *package, LPCWSTR action, MSIRECORD * record)
564 static const WCHAR Query_t[] =
565 {'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
566 '`','A','c','t','i','o', 'n','T','e','x','t','`',' ',
567 'W','H','E','R','E',' ', '`','A','c','t','i','o','n','`',' ','=',
568 ' ','\'','%','s','\'',0};
569 WCHAR message[1024];
570 MSIRECORD * row = 0;
571 DWORD size;
573 if (!package->LastAction || strcmpW(package->LastAction,action))
575 row = MSI_QueryGetRecord(package->db, Query_t, action);
576 if (!row)
577 return;
579 if (MSI_RecordIsNull(row,3))
581 msiobj_release(&row->hdr);
582 return;
585 /* update the cached actionformat */
586 msi_free(package->ActionFormat);
587 package->ActionFormat = msi_dup_record_field(row,3);
589 msi_free(package->LastAction);
590 package->LastAction = strdupW(action);
592 msiobj_release(&row->hdr);
595 MSI_RecordSetStringW(record,0,package->ActionFormat);
596 size = 1024;
597 MSI_FormatRecordW(package,record,message,&size);
599 row = MSI_CreateRecord(1);
600 MSI_RecordSetStringW(row,1,message);
602 MSI_ProcessMessage(package, INSTALLMESSAGE_ACTIONDATA, row);
604 msiobj_release(&row->hdr);
607 BOOL ACTION_VerifyComponentForAction( const MSICOMPONENT* comp, INSTALLSTATE check )
609 if (!comp)
610 return FALSE;
612 if (comp->ActionRequest == check)
613 return TRUE;
614 else
615 return FALSE;
618 BOOL ACTION_VerifyFeatureForAction( const MSIFEATURE* feature, INSTALLSTATE check )
620 if (!feature)
621 return FALSE;
623 if (feature->ActionRequest == check)
624 return TRUE;
625 else
626 return FALSE;
629 void reduce_to_longfilename(WCHAR* filename)
631 LPWSTR p = strchrW(filename,'|');
632 if (p)
633 memmove(filename, p+1, (strlenW(p+1)+1)*sizeof(WCHAR));
636 LPWSTR create_component_advertise_string(MSIPACKAGE* package,
637 MSICOMPONENT* component, LPCWSTR feature)
639 static const WCHAR fmt[] = {'%','s','%','s','%','c','%','s',0};
640 WCHAR productid_85[21], component_85[21];
641 LPWSTR output = NULL;
642 DWORD sz = 0;
643 GUID clsid;
645 /* > is used if there is a component GUID and < if not. */
647 productid_85[0] = 0;
648 component_85[0] = 0;
650 CLSIDFromString(package->ProductCode, &clsid);
651 encode_base85_guid(&clsid, productid_85);
653 if (component)
655 CLSIDFromString(component->ComponentId, &clsid);
656 encode_base85_guid(&clsid, component_85);
659 TRACE("prod=%s feat=%s comp=%s\n", debugstr_w(productid_85),
660 debugstr_w(feature), debugstr_w(component_85));
662 sz = 20 + lstrlenW(feature) + 20 + 3;
664 output = msi_alloc_zero(sz*sizeof(WCHAR));
666 sprintfW(output, fmt, productid_85, feature,
667 component?'>':'<', component_85);
669 return output;
672 /* update component state based on a feature change */
673 void ACTION_UpdateComponentStates(MSIPACKAGE *package, LPCWSTR szFeature)
675 INSTALLSTATE newstate;
676 MSIFEATURE *feature;
677 ComponentList *cl;
679 feature = get_loaded_feature(package,szFeature);
680 if (!feature)
681 return;
683 newstate = feature->ActionRequest;
685 if (newstate == INSTALLSTATE_ABSENT)
686 newstate = INSTALLSTATE_UNKNOWN;
688 LIST_FOR_EACH_ENTRY( cl, &feature->Components, ComponentList, entry )
690 MSICOMPONENT* component = cl->component;
692 TRACE("MODIFYING(%i): Component %s (Installed %i, Action %i, Request %i)\n",
693 newstate, debugstr_w(component->Component), component->Installed,
694 component->Action, component->ActionRequest);
696 if (!component->Enabled)
697 continue;
699 if (newstate == INSTALLSTATE_LOCAL)
700 msi_component_set_state(package, component, INSTALLSTATE_LOCAL);
701 else
703 ComponentList *clist;
704 MSIFEATURE *f;
706 component->hasLocalFeature = FALSE;
708 msi_component_set_state(package, component, newstate);
710 /*if any other feature wants is local we need to set it local*/
711 LIST_FOR_EACH_ENTRY( f, &package->features, MSIFEATURE, entry )
713 if ( f->ActionRequest != INSTALLSTATE_LOCAL &&
714 f->ActionRequest != INSTALLSTATE_SOURCE )
716 continue;
719 LIST_FOR_EACH_ENTRY( clist, &f->Components, ComponentList, entry )
721 if ( clist->component == component &&
722 (f->ActionRequest == INSTALLSTATE_LOCAL ||
723 f->ActionRequest == INSTALLSTATE_SOURCE) )
725 TRACE("Saved by %s\n", debugstr_w(f->Feature));
726 component->hasLocalFeature = TRUE;
728 if (component->Attributes & msidbComponentAttributesOptional)
730 if (f->Attributes & msidbFeatureAttributesFavorSource)
731 msi_component_set_state(package, component, INSTALLSTATE_SOURCE);
732 else
733 msi_component_set_state(package, component, INSTALLSTATE_LOCAL);
735 else if (component->Attributes & msidbComponentAttributesSourceOnly)
736 msi_component_set_state(package, component, INSTALLSTATE_SOURCE);
737 else
738 msi_component_set_state(package, component, INSTALLSTATE_LOCAL);
743 TRACE("Result (%i): Component %s (Installed %i, Action %i, Request %i)\n",
744 newstate, debugstr_w(component->Component), component->Installed,
745 component->Action, component->ActionRequest);
749 UINT register_unique_action(MSIPACKAGE *package, LPCWSTR action)
751 UINT count;
752 LPWSTR *newbuf = NULL;
754 if (!package->script)
755 return FALSE;
757 TRACE("Registering Action %s as having fun\n",debugstr_w(action));
759 count = package->script->UniqueActionsCount;
760 package->script->UniqueActionsCount++;
761 if (count != 0)
762 newbuf = msi_realloc( package->script->UniqueActions,
763 package->script->UniqueActionsCount* sizeof(LPWSTR));
764 else
765 newbuf = msi_alloc( sizeof(LPWSTR));
767 newbuf[count] = strdupW(action);
768 package->script->UniqueActions = newbuf;
770 return ERROR_SUCCESS;
773 BOOL check_unique_action(const MSIPACKAGE *package, LPCWSTR action)
775 UINT i;
777 if (!package->script)
778 return FALSE;
780 for (i = 0; i < package->script->UniqueActionsCount; i++)
781 if (!strcmpW(package->script->UniqueActions[i],action))
782 return TRUE;
784 return FALSE;
787 WCHAR* generate_error_string(MSIPACKAGE *package, UINT error, DWORD count, ... )
789 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};
791 MSIRECORD *rec;
792 MSIRECORD *row;
793 DWORD size = 0;
794 DWORD i;
795 va_list va;
796 LPCWSTR str;
797 LPWSTR data;
799 row = MSI_QueryGetRecord(package->db, query, error);
800 if (!row)
801 return 0;
803 rec = MSI_CreateRecord(count+2);
805 str = MSI_RecordGetString(row,1);
806 MSI_RecordSetStringW(rec,0,str);
807 msiobj_release( &row->hdr );
808 MSI_RecordSetInteger(rec,1,error);
810 va_start(va,count);
811 for (i = 0; i < count; i++)
813 str = va_arg(va,LPCWSTR);
814 MSI_RecordSetStringW(rec,(i+2),str);
816 va_end(va);
818 MSI_FormatRecordW(package,rec,NULL,&size);
820 size++;
821 data = msi_alloc(size*sizeof(WCHAR));
822 if (size > 1)
823 MSI_FormatRecordW(package,rec,data,&size);
824 else
825 data[0] = 0;
826 msiobj_release( &rec->hdr );
827 return data;