223cacd5ad262aeb35c1bedca22605b5b6f5ecbc
[reactos.git] / reactos / dll / win32 / msi / helpers.c
1 /*
2 * Implementation of the Microsoft Installer (msi.dll)
3 *
4 * Copyright 2005 Aric Stewart for CodeWeavers
5 *
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.
10 *
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.
15 *
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
19 */
20
21 /*
22 * Here are helper functions formally in action.c that are used by a variety of
23 * actions and functions.
24 */
25
26 #include <stdarg.h>
27
28 #include "windef.h"
29 #include "winbase.h"
30 #include "winerror.h"
31 #include "wine/debug.h"
32 #include "msipriv.h"
33 #include "winuser.h"
34 #include "winreg.h"
35 #include "shlwapi.h"
36 #include "wine/unicode.h"
37 #include "msidefs.h"
38
39 WINE_DEFAULT_DEBUG_CHANNEL(msi);
40
41 static const WCHAR cszTargetDir[] = {'T','A','R','G','E','T','D','I','R',0};
42 static const WCHAR cszDatabase[]={'D','A','T','A','B','A','S','E',0};
43
44 const WCHAR cszSourceDir[] = {'S','o','u','r','c','e','D','i','r',0};
45 const WCHAR cszSOURCEDIR[] = {'S','O','U','R','C','E','D','I','R',0};
46 const WCHAR cszRootDrive[] = {'R','O','O','T','D','R','I','V','E',0};
47 const WCHAR cszbs[]={'\\',0};
48
49 LPWSTR build_icon_path(MSIPACKAGE *package, LPCWSTR icon_name )
50 {
51 LPWSTR SystemFolder, dest, FilePath;
52
53 static const WCHAR szInstaller[] =
54 {'M','i','c','r','o','s','o','f','t','\\',
55 'I','n','s','t','a','l','l','e','r','\\',0};
56 static const WCHAR szFolder[] =
57 {'A','p','p','D','a','t','a','F','o','l','d','e','r',0};
58
59 SystemFolder = msi_dup_property( package, szFolder );
60
61 dest = build_directory_name(3, SystemFolder, szInstaller, package->ProductCode);
62
63 create_full_pathW(dest);
64
65 FilePath = build_directory_name(2, dest, icon_name);
66
67 msi_free(SystemFolder);
68 msi_free(dest);
69 return FilePath;
70 }
71
72 LPWSTR msi_dup_record_field( MSIRECORD *rec, INT field )
73 {
74 DWORD sz = 0;
75 LPWSTR str;
76 UINT r;
77
78 if (MSI_RecordIsNull( rec, field ))
79 return NULL;
80
81 r = MSI_RecordGetStringW( rec, field, NULL, &sz );
82 if (r != ERROR_SUCCESS)
83 return NULL;
84
85 sz ++;
86 str = msi_alloc( sz * sizeof (WCHAR) );
87 if (!str)
88 return str;
89 str[0] = 0;
90 r = MSI_RecordGetStringW( rec, field, str, &sz );
91 if (r != ERROR_SUCCESS)
92 {
93 ERR("failed to get string!\n");
94 msi_free( str );
95 return NULL;
96 }
97 return str;
98 }
99
100 MSICOMPONENT* get_loaded_component( MSIPACKAGE* package, LPCWSTR Component )
101 {
102 MSICOMPONENT *comp;
103
104 LIST_FOR_EACH_ENTRY( comp, &package->components, MSICOMPONENT, entry )
105 {
106 if (lstrcmpW(Component,comp->Component)==0)
107 return comp;
108 }
109 return NULL;
110 }
111
112 MSIFEATURE* get_loaded_feature(MSIPACKAGE* package, LPCWSTR Feature )
113 {
114 MSIFEATURE *feature;
115
116 LIST_FOR_EACH_ENTRY( feature, &package->features, MSIFEATURE, entry )
117 {
118 if (lstrcmpW( Feature, feature->Feature )==0)
119 return feature;
120 }
121 return NULL;
122 }
123
124 MSIFILE* get_loaded_file( MSIPACKAGE* package, LPCWSTR key )
125 {
126 MSIFILE *file;
127
128 LIST_FOR_EACH_ENTRY( file, &package->files, MSIFILE, entry )
129 {
130 if (lstrcmpW( key, file->File )==0)
131 return file;
132 }
133 return NULL;
134 }
135
136 int track_tempfile( MSIPACKAGE *package, LPCWSTR path )
137 {
138 MSITEMPFILE *temp;
139
140 TRACE("%s\n", debugstr_w(path));
141
142 LIST_FOR_EACH_ENTRY( temp, &package->tempfiles, MSITEMPFILE, entry )
143 if (!lstrcmpW( path, temp->Path ))
144 return 0;
145
146 temp = msi_alloc_zero( sizeof (MSITEMPFILE) );
147 if (!temp)
148 return -1;
149
150 list_add_head( &package->tempfiles, &temp->entry );
151 temp->Path = strdupW( path );
152
153 return 0;
154 }
155
156 MSIFOLDER *get_loaded_folder( MSIPACKAGE *package, LPCWSTR dir )
157 {
158 MSIFOLDER *folder;
159
160 LIST_FOR_EACH_ENTRY( folder, &package->folders, MSIFOLDER, entry )
161 {
162 if (lstrcmpW( dir, folder->Directory )==0)
163 return folder;
164 }
165 return NULL;
166 }
167
168 static LPWSTR get_source_root( MSIPACKAGE *package )
169 {
170 LPWSTR path, p;
171
172 path = msi_dup_property( package, cszSourceDir );
173 if (path)
174 return path;
175
176 path = msi_dup_property( package, cszDatabase );
177 if (path)
178 {
179 p = strrchrW(path,'\\');
180 if (p)
181 *(p+1) = 0;
182 }
183 return path;
184 }
185
186 /*
187 * clean_spaces_from_path()
188 *
189 * removes spaces from the beginning and end of path segments
190 * removes multiple \\ characters
191 */
192 static void clean_spaces_from_path( LPWSTR p )
193 {
194 LPWSTR q = p;
195 int n, len = 0;
196
197 while (1)
198 {
199 /* copy until the end of the string or a space */
200 while (*p != ' ' && (*q = *p))
201 {
202 p++, len++;
203 /* reduce many backslashes to one */
204 if (*p != '\\' || *q != '\\')
205 q++;
206 }
207
208 /* quit at the end of the string */
209 if (!*p)
210 break;
211
212 /* count the number of spaces */
213 n = 0;
214 while (p[n] == ' ')
215 n++;
216
217 /* if it's leading or trailing space, skip it */
218 if ( len == 0 || p[-1] == '\\' || p[n] == '\\' )
219 p += n;
220 else /* copy n spaces */
221 while (n && (*q++ = *p++)) n--;
222 }
223 }
224
225 LPWSTR resolve_folder(MSIPACKAGE *package, LPCWSTR name, BOOL source,
226 BOOL set_prop, BOOL load_prop, MSIFOLDER **folder)
227 {
228 MSIFOLDER *f;
229 LPWSTR p, path = NULL, parent;
230
231 TRACE("Working to resolve %s\n",debugstr_w(name));
232
233 if (!name)
234 return NULL;
235
236 if (!lstrcmpW(name,cszSourceDir))
237 name = cszTargetDir;
238
239 f = get_loaded_folder( package, name );
240 if (!f)
241 return NULL;
242
243 /* special resolving for Target and Source root dir */
244 if (!strcmpW(name,cszTargetDir))
245 {
246 if (!f->ResolvedTarget && !f->Property)
247 {
248 LPWSTR check_path;
249 check_path = msi_dup_property( package, cszTargetDir );
250 if (!check_path)
251 {
252 check_path = msi_dup_property( package, cszRootDrive );
253 if (set_prop)
254 MSI_SetPropertyW(package,cszTargetDir,check_path);
255 }
256
257 /* correct misbuilt target dir */
258 path = build_directory_name(2, check_path, NULL);
259 clean_spaces_from_path( path );
260 if (strcmpiW(path,check_path)!=0)
261 MSI_SetPropertyW(package,cszTargetDir,path);
262 msi_free(check_path);
263
264 f->ResolvedTarget = path;
265 }
266
267 if (!f->ResolvedSource)
268 f->ResolvedSource = get_source_root( package );
269 }
270
271 if (folder)
272 *folder = f;
273
274 if (!source && f->ResolvedTarget)
275 {
276 path = strdupW( f->ResolvedTarget );
277 TRACE(" already resolved to %s\n",debugstr_w(path));
278 return path;
279 }
280
281 if (source && f->ResolvedSource)
282 {
283 path = strdupW( f->ResolvedSource );
284 TRACE(" (source)already resolved to %s\n",debugstr_w(path));
285 return path;
286 }
287
288 if (!source && f->Property)
289 {
290 path = build_directory_name( 2, f->Property, NULL );
291
292 TRACE(" internally set to %s\n",debugstr_w(path));
293 if (set_prop)
294 MSI_SetPropertyW( package, name, path );
295 return path;
296 }
297
298 if (!source && load_prop && (path = msi_dup_property( package, name )))
299 {
300 f->ResolvedTarget = strdupW( path );
301 TRACE(" property set to %s\n", debugstr_w(path));
302 return path;
303 }
304
305 if (!f->Parent)
306 return path;
307
308 parent = f->Parent;
309
310 TRACE(" ! Parent is %s\n", debugstr_w(parent));
311
312 p = resolve_folder(package, parent, source, set_prop, load_prop, NULL);
313 if (!source)
314 {
315 TRACE(" TargetDefault = %s\n", debugstr_w(f->TargetDefault));
316
317 path = build_directory_name( 3, p, f->TargetDefault, NULL );
318 clean_spaces_from_path( path );
319 f->ResolvedTarget = strdupW( path );
320 TRACE("target -> %s\n", debugstr_w(path));
321 if (set_prop)
322 MSI_SetPropertyW(package,name,path);
323 }
324 else
325 {
326 /* source may be in a few different places ... check each of them */
327 path = NULL;
328
329 /* try the long path directory */
330 if (f->SourceLongPath)
331 {
332 path = build_directory_name( 3, p, f->SourceLongPath, NULL );
333 if (INVALID_FILE_ATTRIBUTES == GetFileAttributesW( path ))
334 {
335 msi_free( path );
336 path = NULL;
337 }
338 }
339
340 /* try the short path directory */
341 if (!path && f->SourceShortPath)
342 {
343 path = build_directory_name( 3, p, f->SourceShortPath, NULL );
344 if (INVALID_FILE_ATTRIBUTES == GetFileAttributesW( path ))
345 {
346 msi_free( path );
347 path = NULL;
348 }
349 }
350
351 /* try the root of the install */
352 if (!path)
353 path = get_source_root( package );
354
355 TRACE("source -> %s\n", debugstr_w(path));
356 f->ResolvedSource = strdupW( path );
357 }
358 msi_free(p);
359
360 return path;
361 }
362
363 /* wrapper to resist a need for a full rewrite right now */
364 DWORD deformat_string(MSIPACKAGE *package, LPCWSTR ptr, WCHAR** data )
365 {
366 if (ptr)
367 {
368 MSIRECORD *rec = MSI_CreateRecord(1);
369 DWORD size = 0;
370
371 MSI_RecordSetStringW(rec,0,ptr);
372 MSI_FormatRecordW(package,rec,NULL,&size);
373
374 size++;
375 *data = msi_alloc(size*sizeof(WCHAR));
376 if (size > 1)
377 MSI_FormatRecordW(package,rec,*data,&size);
378 else
379 *data[0] = 0;
380
381 msiobj_release( &rec->hdr );
382 return sizeof(WCHAR)*size;
383 }
384
385 *data = NULL;
386 return 0;
387 }
388
389 UINT schedule_action(MSIPACKAGE *package, UINT script, LPCWSTR action)
390 {
391 UINT count;
392 LPWSTR *newbuf = NULL;
393 if (script >= TOTAL_SCRIPTS)
394 {
395 FIXME("Unknown script requested %i\n",script);
396 return ERROR_FUNCTION_FAILED;
397 }
398 TRACE("Scheduling Action %s in script %i\n",debugstr_w(action), script);
399
400 count = package->script->ActionCount[script];
401 package->script->ActionCount[script]++;
402 if (count != 0)
403 newbuf = msi_realloc( package->script->Actions[script],
404 package->script->ActionCount[script]* sizeof(LPWSTR));
405 else
406 newbuf = msi_alloc( sizeof(LPWSTR));
407
408 newbuf[count] = strdupW(action);
409 package->script->Actions[script] = newbuf;
410
411 return ERROR_SUCCESS;
412 }
413
414 void msi_free_action_script(MSIPACKAGE *package, UINT script)
415 {
416 int i;
417 for (i = 0; i < package->script->ActionCount[script]; i++)
418 msi_free(package->script->Actions[script][i]);
419
420 msi_free(package->script->Actions[script]);
421 package->script->Actions[script] = NULL;
422 package->script->ActionCount[script] = 0;
423 }
424
425 static void remove_tracked_tempfiles(MSIPACKAGE* package)
426 {
427 struct list *item, *cursor;
428
429 LIST_FOR_EACH_SAFE( item, cursor, &package->tempfiles )
430 {
431 MSITEMPFILE *temp = LIST_ENTRY( item, MSITEMPFILE, entry );
432
433 list_remove( &temp->entry );
434 TRACE("deleting temp file %s\n", debugstr_w( temp->Path ));
435 if (!DeleteFileW( temp->Path ))
436 ERR("failed to delete %s\n", debugstr_w( temp->Path ));
437 msi_free( temp->Path );
438 msi_free( temp );
439 }
440 }
441
442 static void free_feature( MSIFEATURE *feature )
443 {
444 struct list *item, *cursor;
445
446 LIST_FOR_EACH_SAFE( item, cursor, &feature->Children )
447 {
448 FeatureList *fl = LIST_ENTRY( item, FeatureList, entry );
449 list_remove( &fl->entry );
450 msi_free( fl );
451 }
452
453 LIST_FOR_EACH_SAFE( item, cursor, &feature->Components )
454 {
455 ComponentList *cl = LIST_ENTRY( item, ComponentList, entry );
456 list_remove( &cl->entry );
457 msi_free( cl );
458 }
459 msi_free( feature->Feature );
460 msi_free( feature->Feature_Parent );
461 msi_free( feature->Directory );
462 msi_free( feature->Description );
463 msi_free( feature->Title );
464 msi_free( feature );
465 }
466
467 static void free_extension( MSIEXTENSION *ext )
468 {
469 struct list *item, *cursor;
470
471 LIST_FOR_EACH_SAFE( item, cursor, &ext->verbs )
472 {
473 MSIVERB *verb = LIST_ENTRY( item, MSIVERB, entry );
474
475 list_remove( &verb->entry );
476 msi_free( verb->Verb );
477 msi_free( verb->Command );
478 msi_free( verb->Argument );
479 msi_free( verb );
480 }
481
482 msi_free( ext->Extension );
483 msi_free( ext->ProgIDText );
484 msi_free( ext );
485 }
486
487 /* Called when the package is being closed */
488 void ACTION_free_package_structures( MSIPACKAGE* package)
489 {
490 INT i;
491 struct list *item, *cursor;
492
493 TRACE("Freeing package action data\n");
494
495 remove_tracked_tempfiles(package);
496
497 LIST_FOR_EACH_SAFE( item, cursor, &package->features )
498 {
499 MSIFEATURE *feature = LIST_ENTRY( item, MSIFEATURE, entry );
500 list_remove( &feature->entry );
501 free_feature( feature );
502 }
503
504 LIST_FOR_EACH_SAFE( item, cursor, &package->folders )
505 {
506 MSIFOLDER *folder = LIST_ENTRY( item, MSIFOLDER, entry );
507
508 list_remove( &folder->entry );
509 msi_free( folder->Parent );
510 msi_free( folder->Directory );
511 msi_free( folder->TargetDefault );
512 msi_free( folder->SourceLongPath );
513 msi_free( folder->SourceShortPath );
514 msi_free( folder->ResolvedTarget );
515 msi_free( folder->ResolvedSource );
516 msi_free( folder->Property );
517 msi_free( folder );
518 }
519
520 LIST_FOR_EACH_SAFE( item, cursor, &package->components )
521 {
522 MSICOMPONENT *comp = LIST_ENTRY( item, MSICOMPONENT, entry );
523
524 list_remove( &comp->entry );
525 msi_free( comp->Component );
526 msi_free( comp->ComponentId );
527 msi_free( comp->Directory );
528 msi_free( comp->Condition );
529 msi_free( comp->KeyPath );
530 msi_free( comp->FullKeypath );
531 msi_free( comp );
532 }
533
534 LIST_FOR_EACH_SAFE( item, cursor, &package->files )
535 {
536 MSIFILE *file = LIST_ENTRY( item, MSIFILE, entry );
537
538 list_remove( &file->entry );
539 msi_free( file->File );
540 msi_free( file->FileName );
541 msi_free( file->ShortName );
542 msi_free( file->LongName );
543 msi_free( file->Version );
544 msi_free( file->Language );
545 msi_free( file->SourcePath );
546 msi_free( file->TargetPath );
547 msi_free( file );
548 }
549
550 /* clean up extension, progid, class and verb structures */
551 LIST_FOR_EACH_SAFE( item, cursor, &package->classes )
552 {
553 MSICLASS *cls = LIST_ENTRY( item, MSICLASS, entry );
554
555 list_remove( &cls->entry );
556 msi_free( cls->clsid );
557 msi_free( cls->Context );
558 msi_free( cls->Description );
559 msi_free( cls->FileTypeMask );
560 msi_free( cls->IconPath );
561 msi_free( cls->DefInprocHandler );
562 msi_free( cls->DefInprocHandler32 );
563 msi_free( cls->Argument );
564 msi_free( cls->ProgIDText );
565 msi_free( cls );
566 }
567
568 LIST_FOR_EACH_SAFE( item, cursor, &package->extensions )
569 {
570 MSIEXTENSION *ext = LIST_ENTRY( item, MSIEXTENSION, entry );
571
572 list_remove( &ext->entry );
573 free_extension( ext );
574 }
575
576 LIST_FOR_EACH_SAFE( item, cursor, &package->progids )
577 {
578 MSIPROGID *progid = LIST_ENTRY( item, MSIPROGID, entry );
579
580 list_remove( &progid->entry );
581 msi_free( progid->ProgID );
582 msi_free( progid->Description );
583 msi_free( progid->IconPath );
584 msi_free( progid );
585 }
586
587 LIST_FOR_EACH_SAFE( item, cursor, &package->mimes )
588 {
589 MSIMIME *mt = LIST_ENTRY( item, MSIMIME, entry );
590
591 list_remove( &mt->entry );
592 msi_free( mt->clsid );
593 msi_free( mt->ContentType );
594 msi_free( mt );
595 }
596
597 LIST_FOR_EACH_SAFE( item, cursor, &package->appids )
598 {
599 MSIAPPID *appid = LIST_ENTRY( item, MSIAPPID, entry );
600
601 list_remove( &appid->entry );
602 msi_free( appid->AppID );
603 msi_free( appid->RemoteServerName );
604 msi_free( appid->LocalServer );
605 msi_free( appid->ServiceParameters );
606 msi_free( appid->DllSurrogate );
607 msi_free( appid );
608 }
609
610 LIST_FOR_EACH_SAFE( item, cursor, &package->sourcelist_info )
611 {
612 MSISOURCELISTINFO *info = LIST_ENTRY( item, MSISOURCELISTINFO, entry );
613
614 list_remove( &info->entry );
615 msi_free( info->value );
616 msi_free( info );
617 }
618
619 LIST_FOR_EACH_SAFE( item, cursor, &package->sourcelist_media )
620 {
621 MSIMEDIADISK *info = LIST_ENTRY( item, MSIMEDIADISK, entry );
622
623 list_remove( &info->entry );
624 msi_free( info->volume_label );
625 msi_free( info->disk_prompt );
626 msi_free( info );
627 }
628
629 if (package->script)
630 {
631 for (i = 0; i < TOTAL_SCRIPTS; i++)
632 msi_free_action_script(package, i);
633
634 for (i = 0; i < package->script->UniqueActionsCount; i++)
635 msi_free(package->script->UniqueActions[i]);
636
637 msi_free(package->script->UniqueActions);
638 msi_free(package->script);
639 }
640
641 msi_free(package->BaseURL);
642 msi_free(package->PackagePath);
643 msi_free(package->ProductCode);
644 msi_free(package->ActionFormat);
645 msi_free(package->LastAction);
646
647 /* cleanup control event subscriptions */
648 ControlEvent_CleanupSubscriptions(package);
649 }
650
651 /*
652 * build_directory_name()
653 *
654 * This function is to save messing round with directory names
655 * It handles adding backslashes between path segments,
656 * and can add \ at the end of the directory name if told to.
657 *
658 * It takes a variable number of arguments.
659 * It always allocates a new string for the result, so make sure
660 * to free the return value when finished with it.
661 *
662 * The first arg is the number of path segments that follow.
663 * The arguments following count are a list of path segments.
664 * A path segment may be NULL.
665 *
666 * Path segments will be added with a \ separating them.
667 * A \ will not be added after the last segment, however if the
668 * last segment is NULL, then the last character will be a \
669 *
670 */
671 LPWSTR build_directory_name(DWORD count, ...)
672 {
673 DWORD sz = 1, i;
674 LPWSTR dir;
675 va_list va;
676
677 va_start(va,count);
678 for(i=0; i<count; i++)
679 {
680 LPCWSTR str = va_arg(va,LPCWSTR);
681 if (str)
682 sz += strlenW(str) + 1;
683 }
684 va_end(va);
685
686 dir = msi_alloc(sz*sizeof(WCHAR));
687 dir[0]=0;
688
689 va_start(va,count);
690 for(i=0; i<count; i++)
691 {
692 LPCWSTR str = va_arg(va,LPCWSTR);
693 if (!str)
694 continue;
695 strcatW(dir, str);
696 if( ((i+1)!=count) && dir[strlenW(dir)-1]!='\\')
697 strcatW(dir, cszbs);
698 }
699 return dir;
700 }
701
702 /***********************************************************************
703 * create_full_pathW
704 *
705 * Recursively create all directories in the path.
706 *
707 * shamelessly stolen from setupapi/queue.c
708 */
709 BOOL create_full_pathW(const WCHAR *path)
710 {
711 BOOL ret = TRUE;
712 int len;
713 WCHAR *new_path;
714
715 new_path = msi_alloc( (strlenW(path) + 1) * sizeof(WCHAR));
716
717 strcpyW(new_path, path);
718
719 while((len = strlenW(new_path)) && new_path[len - 1] == '\\')
720 new_path[len - 1] = 0;
721
722 while(!CreateDirectoryW(new_path, NULL))
723 {
724 WCHAR *slash;
725 DWORD last_error = GetLastError();
726 if(last_error == ERROR_ALREADY_EXISTS)
727 break;
728
729 if(last_error != ERROR_PATH_NOT_FOUND)
730 {
731 ret = FALSE;
732 break;
733 }
734
735 if(!(slash = strrchrW(new_path, '\\')))
736 {
737 ret = FALSE;
738 break;
739 }
740
741 len = slash - new_path;
742 new_path[len] = 0;
743 if(!create_full_pathW(new_path))
744 {
745 ret = FALSE;
746 break;
747 }
748 new_path[len] = '\\';
749 }
750
751 msi_free(new_path);
752 return ret;
753 }
754
755 void ui_progress(MSIPACKAGE *package, int a, int b, int c, int d )
756 {
757 MSIRECORD * row;
758
759 row = MSI_CreateRecord(4);
760 MSI_RecordSetInteger(row,1,a);
761 MSI_RecordSetInteger(row,2,b);
762 MSI_RecordSetInteger(row,3,c);
763 MSI_RecordSetInteger(row,4,d);
764 MSI_ProcessMessage(package, INSTALLMESSAGE_PROGRESS, row);
765 msiobj_release(&row->hdr);
766
767 msi_dialog_check_messages(NULL);
768 }
769
770 void ui_actiondata(MSIPACKAGE *package, LPCWSTR action, MSIRECORD * record)
771 {
772 static const WCHAR Query_t[] =
773 {'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
774 '`','A','c','t','i','o', 'n','T','e','x','t','`',' ',
775 'W','H','E','R','E',' ', '`','A','c','t','i','o','n','`',' ','=',
776 ' ','\'','%','s','\'',0};
777 WCHAR message[1024];
778 MSIRECORD * row = 0;
779 DWORD size;
780
781 if (!package->LastAction || strcmpW(package->LastAction,action))
782 {
783 row = MSI_QueryGetRecord(package->db, Query_t, action);
784 if (!row)
785 return;
786
787 if (MSI_RecordIsNull(row,3))
788 {
789 msiobj_release(&row->hdr);
790 return;
791 }
792
793 /* update the cached actionformat */
794 msi_free(package->ActionFormat);
795 package->ActionFormat = msi_dup_record_field(row,3);
796
797 msi_free(package->LastAction);
798 package->LastAction = strdupW(action);
799
800 msiobj_release(&row->hdr);
801 }
802
803 MSI_RecordSetStringW(record,0,package->ActionFormat);
804 size = 1024;
805 MSI_FormatRecordW(package,record,message,&size);
806
807 row = MSI_CreateRecord(1);
808 MSI_RecordSetStringW(row,1,message);
809
810 MSI_ProcessMessage(package, INSTALLMESSAGE_ACTIONDATA, row);
811
812 msiobj_release(&row->hdr);
813 }
814
815 BOOL ACTION_VerifyComponentForAction( const MSICOMPONENT* comp, INSTALLSTATE check )
816 {
817 if (!comp)
818 return FALSE;
819
820 if (comp->Installed == check)
821 return FALSE;
822
823 if (comp->ActionRequest == check)
824 return TRUE;
825 else
826 return FALSE;
827 }
828
829 BOOL ACTION_VerifyFeatureForAction( const MSIFEATURE* feature, INSTALLSTATE check )
830 {
831 if (!feature)
832 return FALSE;
833
834 if (feature->ActionRequest == check)
835 return TRUE;
836 else
837 return FALSE;
838 }
839
840 void reduce_to_longfilename(WCHAR* filename)
841 {
842 LPWSTR p = strchrW(filename,'|');
843 if (p)
844 memmove(filename, p+1, (strlenW(p+1)+1)*sizeof(WCHAR));
845 }
846
847 void reduce_to_shortfilename(WCHAR* filename)
848 {
849 LPWSTR p = strchrW(filename,'|');
850 if (p)
851 *p = 0;
852 }
853
854 LPWSTR create_component_advertise_string(MSIPACKAGE* package,
855 MSICOMPONENT* component, LPCWSTR feature)
856 {
857 static const WCHAR fmt[] = {'%','s','%','s','%','c','%','s',0};
858 WCHAR productid_85[21], component_85[21];
859 LPWSTR output = NULL;
860 DWORD sz = 0;
861 GUID clsid;
862
863 /* > is used if there is a component GUID and < if not. */
864
865 productid_85[0] = 0;
866 component_85[0] = 0;
867
868 CLSIDFromString(package->ProductCode, &clsid);
869 encode_base85_guid(&clsid, productid_85);
870
871 if (component)
872 {
873 CLSIDFromString(component->ComponentId, &clsid);
874 encode_base85_guid(&clsid, component_85);
875 }
876
877 TRACE("prod=%s feat=%s comp=%s\n", debugstr_w(productid_85),
878 debugstr_w(feature), debugstr_w(component_85));
879
880 sz = 20 + lstrlenW(feature) + 20 + 3;
881
882 output = msi_alloc_zero(sz*sizeof(WCHAR));
883
884 sprintfW(output, fmt, productid_85, feature,
885 component?'>':'<', component_85);
886
887 return output;
888 }
889
890 /* update component state based on a feature change */
891 void ACTION_UpdateComponentStates(MSIPACKAGE *package, LPCWSTR szFeature)
892 {
893 INSTALLSTATE newstate;
894 MSIFEATURE *feature;
895 ComponentList *cl;
896
897 feature = get_loaded_feature(package,szFeature);
898 if (!feature)
899 return;
900
901 newstate = feature->ActionRequest;
902
903 if (newstate == INSTALLSTATE_ABSENT)
904 newstate = INSTALLSTATE_UNKNOWN;
905
906 LIST_FOR_EACH_ENTRY( cl, &feature->Components, ComponentList, entry )
907 {
908 MSICOMPONENT* component = cl->component;
909
910 TRACE("MODIFYING(%i): Component %s (Installed %i, Action %i, Request %i)\n",
911 newstate, debugstr_w(component->Component), component->Installed,
912 component->Action, component->ActionRequest);
913
914 if (!component->Enabled)
915 continue;
916
917 if (newstate == INSTALLSTATE_LOCAL)
918 msi_component_set_state( component, INSTALLSTATE_LOCAL );
919 else
920 {
921 ComponentList *clist;
922 MSIFEATURE *f;
923
924 msi_component_set_state( component, newstate );
925
926 /*if any other feature wants is local we need to set it local*/
927 LIST_FOR_EACH_ENTRY( f, &package->features, MSIFEATURE, entry )
928 {
929 if ( f->ActionRequest != INSTALLSTATE_LOCAL &&
930 f->ActionRequest != INSTALLSTATE_SOURCE )
931 {
932 continue;
933 }
934
935 LIST_FOR_EACH_ENTRY( clist, &f->Components, ComponentList, entry )
936 {
937 if ( clist->component == component &&
938 (f->ActionRequest == INSTALLSTATE_LOCAL ||
939 f->ActionRequest == INSTALLSTATE_SOURCE) )
940 {
941 TRACE("Saved by %s\n", debugstr_w(f->Feature));
942
943 if (component->Attributes & msidbComponentAttributesOptional)
944 {
945 if (f->Attributes & msidbFeatureAttributesFavorSource)
946 msi_component_set_state( component, INSTALLSTATE_SOURCE );
947 else
948 msi_component_set_state( component, INSTALLSTATE_LOCAL );
949 }
950 else if (component->Attributes & msidbComponentAttributesSourceOnly)
951 msi_component_set_state( component, INSTALLSTATE_SOURCE );
952 else
953 msi_component_set_state( component, INSTALLSTATE_LOCAL );
954 }
955 }
956 }
957 }
958 TRACE("Result (%i): Component %s (Installed %i, Action %i, Request %i)\n",
959 newstate, debugstr_w(component->Component), component->Installed,
960 component->Action, component->ActionRequest);
961 }
962 }
963
964 UINT register_unique_action(MSIPACKAGE *package, LPCWSTR action)
965 {
966 UINT count;
967 LPWSTR *newbuf = NULL;
968
969 if (!package->script)
970 return FALSE;
971
972 TRACE("Registering Action %s as having fun\n",debugstr_w(action));
973
974 count = package->script->UniqueActionsCount;
975 package->script->UniqueActionsCount++;
976 if (count != 0)
977 newbuf = msi_realloc( package->script->UniqueActions,
978 package->script->UniqueActionsCount* sizeof(LPWSTR));
979 else
980 newbuf = msi_alloc( sizeof(LPWSTR));
981
982 newbuf[count] = strdupW(action);
983 package->script->UniqueActions = newbuf;
984
985 return ERROR_SUCCESS;
986 }
987
988 BOOL check_unique_action(const MSIPACKAGE *package, LPCWSTR action)
989 {
990 INT i;
991
992 if (!package->script)
993 return FALSE;
994
995 for (i = 0; i < package->script->UniqueActionsCount; i++)
996 if (!strcmpW(package->script->UniqueActions[i],action))
997 return TRUE;
998
999 return FALSE;
1000 }
1001
1002 WCHAR* generate_error_string(MSIPACKAGE *package, UINT error, DWORD count, ... )
1003 {
1004 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};
1005
1006 MSIRECORD *rec;
1007 MSIRECORD *row;
1008 DWORD size = 0;
1009 DWORD i;
1010 va_list va;
1011 LPCWSTR str;
1012 LPWSTR data;
1013
1014 row = MSI_QueryGetRecord(package->db, query, error);
1015 if (!row)
1016 return 0;
1017
1018 rec = MSI_CreateRecord(count+2);
1019
1020 str = MSI_RecordGetString(row,1);
1021 MSI_RecordSetStringW(rec,0,str);
1022 msiobj_release( &row->hdr );
1023 MSI_RecordSetInteger(rec,1,error);
1024
1025 va_start(va,count);
1026 for (i = 0; i < count; i++)
1027 {
1028 str = va_arg(va,LPCWSTR);
1029 MSI_RecordSetStringW(rec,(i+2),str);
1030 }
1031 va_end(va);
1032
1033 MSI_FormatRecordW(package,rec,NULL,&size);
1034
1035 size++;
1036 data = msi_alloc(size*sizeof(WCHAR));
1037 if (size > 1)
1038 MSI_FormatRecordW(package,rec,data,&size);
1039 else
1040 data[0] = 0;
1041 msiobj_release( &rec->hdr );
1042 return data;
1043 }
1044
1045 void msi_ui_error( DWORD msg_id, DWORD type )
1046 {
1047 WCHAR text[2048];
1048
1049 static const WCHAR title[] = {
1050 'W','i','n','d','o','w','s',' ','I','n','s','t','a','l','l','e','r',0
1051 };
1052
1053 if (!MsiLoadStringW( -1, msg_id, text, sizeof(text) / sizeof(text[0]),
1054 MAKELANGID(LANG_NEUTRAL, SUBLANG_NEUTRAL) ))
1055 return;
1056
1057 MessageBoxW( NULL, text, title, type );
1058 }
1059
1060 typedef struct
1061 {
1062 MSIPACKAGE *package;
1063 MSIMEDIAINFO *mi;
1064 MSIFILE *file;
1065 LPWSTR destination;
1066 } CabData;
1067
1068 static INT_PTR cabinet_notify(FDINOTIFICATIONTYPE fdint, PFDINOTIFICATION pfdin)
1069 {
1070 TRACE("(%d)\n", fdint);
1071
1072 switch (fdint)
1073 {
1074 case fdintNEXT_CABINET:
1075 {
1076 ERR("continuous cabinets not handled\n");
1077 return 0;
1078 }
1079
1080 case fdintCOPY_FILE:
1081 {
1082 CabData *data = (CabData*) pfdin->pv;
1083 LPWSTR file, path;
1084 DWORD attrs, size;
1085 HANDLE handle;
1086 MSIFILE *f;
1087
1088 file = strdupAtoW(pfdin->psz1);
1089 f = get_loaded_file(data->package, file);
1090 msi_free(file);
1091
1092 if (!f)
1093 {
1094 WARN("unknown file in cabinet (%s)\n",debugstr_a(pfdin->psz1));
1095 return 0;
1096 }
1097
1098 if (lstrcmpW(f->File, data->file->File))
1099 return 0;
1100
1101 size = lstrlenW(data->destination) + lstrlenW(data->file->FileName) + 2;
1102 path = msi_alloc(size * sizeof(WCHAR));
1103 lstrcpyW(path, data->destination);
1104 PathAddBackslashW(path);
1105 lstrcatW(path, data->file->FileName);
1106
1107 TRACE("extracting %s\n", debugstr_w(path));
1108
1109 attrs = f->Attributes & (FILE_ATTRIBUTE_READONLY|FILE_ATTRIBUTE_HIDDEN|FILE_ATTRIBUTE_SYSTEM);
1110 if (!attrs) attrs = FILE_ATTRIBUTE_NORMAL;
1111
1112 handle = CreateFileW(path, GENERIC_READ | GENERIC_WRITE, 0,
1113 NULL, CREATE_ALWAYS, attrs, NULL);
1114 if (handle == INVALID_HANDLE_VALUE)
1115 {
1116 if (GetFileAttributesW(path) == INVALID_FILE_ATTRIBUTES)
1117 ERR("failed to create %s (error %d)\n",
1118 debugstr_w(path), GetLastError());
1119
1120 msi_free(path);
1121 return 0;
1122 }
1123
1124 msi_free(path);
1125 return (INT_PTR)handle;
1126 }
1127
1128 case fdintCLOSE_FILE_INFO:
1129 {
1130 FILETIME ft;
1131 FILETIME ftLocal;
1132 HANDLE handle = (HANDLE)pfdin->hf;
1133
1134 if (!DosDateTimeToFileTime(pfdin->date, pfdin->time, &ft))
1135 return -1;
1136 if (!LocalFileTimeToFileTime(&ft, &ftLocal))
1137 return -1;
1138 if (!SetFileTime(handle, &ftLocal, 0, &ftLocal))
1139 return -1;
1140 CloseHandle(handle);
1141 return 1;
1142 }
1143
1144 default:
1145 return 0;
1146 }
1147 }
1148
1149 UINT msi_extract_file(MSIPACKAGE *package, MSIFILE *file, LPWSTR destdir)
1150 {
1151 MSIMEDIAINFO *mi;
1152 CabData data;
1153 UINT r;
1154
1155 mi = msi_alloc_zero(sizeof(MSIMEDIAINFO));
1156 if (!mi)
1157 return ERROR_OUTOFMEMORY;
1158
1159 r = msi_load_media_info(package, file, mi);
1160 if (r != ERROR_SUCCESS)
1161 goto done;
1162
1163 data.package = package;
1164 data.mi = mi;
1165 data.file = file;
1166 data.destination = destdir;
1167
1168 if (!msi_cabextract(package, mi, cabinet_notify, &data))
1169 {
1170 ERR("Failed to extract cabinet file\n");
1171 r = ERROR_FUNCTION_FAILED;
1172 }
1173
1174 done:
1175 msi_free_media_info(mi);
1176 return r;
1177 }