fc466ba7a206bf33cba604f59a79719de37e6106
[reactos.git] / reactos / lib / 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 */
20
21 /*
22 * Here are helper functions formally in action.c that are used by a variaty 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 "wine/unicode.h"
35 #include "action.h"
36
37 WINE_DEFAULT_DEBUG_CHANNEL(msi);
38
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};
41
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};
45
46 DWORD build_version_dword(LPCWSTR version_string)
47 {
48 SHORT major,minor;
49 WORD build;
50 DWORD rc = 0x00000000;
51 LPCWSTR ptr1;
52
53 ptr1 = version_string;
54
55 if (!ptr1)
56 return rc;
57 else
58 major = atoiW(ptr1);
59
60
61 if(ptr1)
62 ptr1 = strchrW(ptr1,'.');
63 if (ptr1)
64 {
65 ptr1++;
66 minor = atoiW(ptr1);
67 }
68 else
69 minor = 0;
70
71 if (ptr1)
72 ptr1 = strchrW(ptr1,'.');
73
74 if (ptr1)
75 {
76 ptr1++;
77 build = atoiW(ptr1);
78 }
79 else
80 build = 0;
81
82 rc = MAKELONG(build,MAKEWORD(minor,major));
83 TRACE("%s -> 0x%lx\n",debugstr_w(version_string),rc);
84 return rc;
85 }
86
87 LPWSTR build_icon_path(MSIPACKAGE *package, LPCWSTR icon_name )
88 {
89 LPWSTR SystemFolder, dest, FilePath;
90
91 static const WCHAR szInstaller[] =
92 {'M','i','c','r','o','s','o','f','t','\\',
93 'I','n','s','t','a','l','l','e','r','\\',0};
94 static const WCHAR szFolder[] =
95 {'A','p','p','D','a','t','a','F','o','l','d','e','r',0};
96
97 SystemFolder = msi_dup_property( package, szFolder );
98
99 dest = build_directory_name(3, SystemFolder, szInstaller, package->ProductCode);
100
101 create_full_pathW(dest);
102
103 FilePath = build_directory_name(2, dest, icon_name);
104
105 msi_free(SystemFolder);
106 msi_free(dest);
107 return FilePath;
108 }
109
110 WCHAR *load_dynamic_stringW(MSIRECORD *row, INT index)
111 {
112 UINT rc;
113 DWORD sz;
114 LPWSTR ret;
115
116 sz = 0;
117 if (MSI_RecordIsNull(row,index))
118 return NULL;
119
120 rc = MSI_RecordGetStringW(row,index,NULL,&sz);
121
122 /* having an empty string is different than NULL */
123 if (sz == 0)
124 {
125 ret = msi_alloc(sizeof(WCHAR));
126 ret[0] = 0;
127 return ret;
128 }
129
130 sz ++;
131 ret = msi_alloc(sz * sizeof (WCHAR));
132 rc = MSI_RecordGetStringW(row,index,ret,&sz);
133 if (rc!=ERROR_SUCCESS)
134 {
135 ERR("Unable to load dynamic string\n");
136 msi_free(ret);
137 ret = NULL;
138 }
139 return ret;
140 }
141
142 LPWSTR msi_dup_property(MSIPACKAGE *package, LPCWSTR prop)
143 {
144 DWORD sz = 0;
145 LPWSTR str;
146 UINT r;
147
148 r = MSI_GetPropertyW(package, prop, NULL, &sz);
149 if (r != ERROR_SUCCESS && r != ERROR_MORE_DATA)
150 return NULL;
151
152 sz++;
153 str = msi_alloc(sz*sizeof(WCHAR));
154 r = MSI_GetPropertyW(package, prop, str, &sz);
155 if (r != ERROR_SUCCESS)
156 {
157 msi_free(str);
158 str = NULL;
159 }
160 return str;
161 }
162
163 MSICOMPONENT* get_loaded_component( MSIPACKAGE* package, LPCWSTR Component )
164 {
165 MSICOMPONENT *comp;
166
167 LIST_FOR_EACH_ENTRY( comp, &package->components, MSICOMPONENT, entry )
168 {
169 if (lstrcmpW(Component,comp->Component)==0)
170 return comp;
171 }
172 return NULL;
173 }
174
175 MSIFEATURE* get_loaded_feature(MSIPACKAGE* package, LPCWSTR Feature )
176 {
177 MSIFEATURE *feature;
178
179 LIST_FOR_EACH_ENTRY( feature, &package->features, MSIFEATURE, entry )
180 {
181 if (lstrcmpW( Feature, feature->Feature )==0)
182 return feature;
183 }
184 return NULL;
185 }
186
187 MSIFILE* get_loaded_file( MSIPACKAGE* package, LPCWSTR key )
188 {
189 MSIFILE *file;
190
191 LIST_FOR_EACH_ENTRY( file, &package->files, MSIFILE, entry )
192 {
193 if (lstrcmpW( key, file->File )==0)
194 return file;
195 }
196 return NULL;
197 }
198
199 int track_tempfile( MSIPACKAGE *package, LPCWSTR name, LPCWSTR path )
200 {
201 MSITEMPFILE *temp;
202
203 if (!package)
204 return -1;
205
206 LIST_FOR_EACH_ENTRY( temp, &package->tempfiles, MSITEMPFILE, entry )
207 {
208 if (lstrcmpW( name, temp->File )==0)
209 {
210 TRACE("tempfile %s already exists with path %s\n",
211 debugstr_w(temp->File), debugstr_w(temp->Path));
212 return -1;
213 }
214 }
215
216 temp = msi_alloc_zero( sizeof (MSITEMPFILE) );
217 if (!temp)
218 return -1;
219
220 list_add_head( &package->tempfiles, &temp->entry );
221
222 temp->File = strdupW( name );
223 temp->Path = strdupW( path );
224
225 TRACE("adding tempfile %s with path %s\n",
226 debugstr_w(temp->File), debugstr_w(temp->Path));
227
228 return 0;
229 }
230
231 MSIFOLDER *get_loaded_folder( MSIPACKAGE *package, LPCWSTR dir )
232 {
233 MSIFOLDER *folder;
234
235 LIST_FOR_EACH_ENTRY( folder, &package->folders, MSIFOLDER, entry )
236 {
237 if (lstrcmpW( dir, folder->Directory )==0)
238 return folder;
239 }
240 return NULL;
241 }
242
243 LPWSTR resolve_folder(MSIPACKAGE *package, LPCWSTR name, BOOL source,
244 BOOL set_prop, MSIFOLDER **folder)
245 {
246 MSIFOLDER *f;
247 LPWSTR p, path = NULL;
248
249 TRACE("Working to resolve %s\n",debugstr_w(name));
250
251 if (!name)
252 return NULL;
253
254 /* special resolving for Target and Source root dir */
255 if (strcmpW(name,cszTargetDir)==0 || strcmpW(name,cszSourceDir)==0)
256 {
257 if (!source)
258 {
259 LPWSTR check_path;
260 check_path = msi_dup_property( package, cszTargetDir );
261 if (!check_path)
262 {
263 check_path = msi_dup_property( package, cszRootDrive );
264 if (set_prop)
265 MSI_SetPropertyW(package,cszTargetDir,check_path);
266 }
267
268 /* correct misbuilt target dir */
269 path = build_directory_name(2, check_path, NULL);
270 if (strcmpiW(path,check_path)!=0)
271 MSI_SetPropertyW(package,cszTargetDir,path);
272 msi_free(check_path);
273 }
274 else
275 {
276 path = msi_dup_property( package, cszSourceDir );
277 if (!path)
278 {
279 path = msi_dup_property( package, cszDatabase );
280 if (path)
281 {
282 p = strrchrW(path,'\\');
283 if (p)
284 *(p+1) = 0;
285 }
286 }
287 }
288 if (folder)
289 *folder = get_loaded_folder( package, name );
290 return path;
291 }
292
293 f = get_loaded_folder( package, name );
294 if (!f)
295 return NULL;
296
297 if (folder)
298 *folder = f;
299
300 if (!source && f->ResolvedTarget)
301 {
302 path = strdupW( f->ResolvedTarget );
303 TRACE(" already resolved to %s\n",debugstr_w(path));
304 return path;
305 }
306 else if (source && f->ResolvedSource)
307 {
308 path = strdupW( f->ResolvedSource );
309 TRACE(" (source)already resolved to %s\n",debugstr_w(path));
310 return path;
311 }
312 else if (!source && f->Property)
313 {
314 path = build_directory_name( 2, f->Property, NULL );
315
316 TRACE(" internally set to %s\n",debugstr_w(path));
317 if (set_prop)
318 MSI_SetPropertyW( package, name, path );
319 return path;
320 }
321
322 if (f->Parent)
323 {
324 LPWSTR parent = f->Parent->Directory;
325
326 TRACE(" ! Parent is %s\n", debugstr_w(parent));
327
328 p = resolve_folder(package, parent, source, set_prop, NULL);
329 if (!source)
330 {
331 TRACE(" TargetDefault = %s\n", debugstr_w(f->TargetDefault));
332
333 path = build_directory_name( 3, p, f->TargetDefault, NULL );
334 f->ResolvedTarget = strdupW( path );
335 TRACE(" resolved into %s\n",debugstr_w(path));
336 if (set_prop)
337 MSI_SetPropertyW(package,name,path);
338 }
339 else
340 {
341 if (f->SourceDefault && f->SourceDefault[0]!='.')
342 path = build_directory_name( 3, p, f->SourceDefault, NULL );
343 else
344 path = strdupW(p);
345 TRACE(" (source)resolved into %s\n",debugstr_w(path));
346 f->ResolvedSource = strdupW( path );
347 }
348 msi_free(p);
349 }
350 return path;
351 }
352
353 /* wrapper to resist a need for a full rewrite right now */
354 DWORD deformat_string(MSIPACKAGE *package, LPCWSTR ptr, WCHAR** data )
355 {
356 if (ptr)
357 {
358 MSIRECORD *rec = MSI_CreateRecord(1);
359 DWORD size = 0;
360
361 MSI_RecordSetStringW(rec,0,ptr);
362 MSI_FormatRecordW(package,rec,NULL,&size);
363 if (size >= 0)
364 {
365 size++;
366 *data = msi_alloc(size*sizeof(WCHAR));
367 if (size > 1)
368 MSI_FormatRecordW(package,rec,*data,&size);
369 else
370 *data[0] = 0;
371 msiobj_release( &rec->hdr );
372 return sizeof(WCHAR)*size;
373 }
374 msiobj_release( &rec->hdr );
375 }
376
377 *data = NULL;
378 return 0;
379 }
380
381 UINT schedule_action(MSIPACKAGE *package, UINT script, LPCWSTR action)
382 {
383 UINT count;
384 LPWSTR *newbuf = NULL;
385 if (script >= TOTAL_SCRIPTS)
386 {
387 FIXME("Unknown script requested %i\n",script);
388 return ERROR_FUNCTION_FAILED;
389 }
390 TRACE("Scheduling Action %s in script %i\n",debugstr_w(action), script);
391
392 count = package->script->ActionCount[script];
393 package->script->ActionCount[script]++;
394 if (count != 0)
395 newbuf = msi_realloc( package->script->Actions[script],
396 package->script->ActionCount[script]* sizeof(LPWSTR));
397 else
398 newbuf = msi_alloc( sizeof(LPWSTR));
399
400 newbuf[count] = strdupW(action);
401 package->script->Actions[script] = newbuf;
402
403 return ERROR_SUCCESS;
404 }
405
406 static void remove_tracked_tempfiles(MSIPACKAGE* package)
407 {
408 struct list *item, *cursor;
409
410 LIST_FOR_EACH_SAFE( item, cursor, &package->tempfiles )
411 {
412 MSITEMPFILE *temp = LIST_ENTRY( item, MSITEMPFILE, entry );
413
414 list_remove( &temp->entry );
415 TRACE("deleting temp file %s\n", debugstr_w( temp->Path ));
416 DeleteFileW( temp->Path );
417 msi_free( temp->File );
418 msi_free( temp->Path );
419 msi_free( temp );
420 }
421 }
422
423 static void free_feature( MSIFEATURE *feature )
424 {
425 struct list *item, *cursor;
426
427 LIST_FOR_EACH_SAFE( item, cursor, &feature->Components )
428 {
429 ComponentList *cl = LIST_ENTRY( item, ComponentList, entry );
430 list_remove( &cl->entry );
431 msi_free( cl );
432 }
433 msi_free( feature->Feature );
434 msi_free( feature->Feature_Parent );
435 msi_free( feature->Directory );
436 msi_free( feature->Description );
437 msi_free( feature->Title );
438 msi_free( feature );
439 }
440
441 void free_extension( MSIEXTENSION *ext )
442 {
443 struct list *item, *cursor;
444
445 LIST_FOR_EACH_SAFE( item, cursor, &ext->verbs )
446 {
447 MSIVERB *verb = LIST_ENTRY( item, MSIVERB, entry );
448
449 list_remove( &verb->entry );
450 msi_free( verb->Verb );
451 msi_free( verb->Command );
452 msi_free( verb->Argument );
453 msi_free( verb );
454 }
455
456 msi_free( ext->Extension );
457 msi_free( ext->ProgIDText );
458 msi_free( ext );
459 }
460
461 /* Called when the package is being closed */
462 void ACTION_free_package_structures( MSIPACKAGE* package)
463 {
464 INT i;
465 struct list *item, *cursor;
466
467 TRACE("Freeing package action data\n");
468
469 remove_tracked_tempfiles(package);
470
471 LIST_FOR_EACH_SAFE( item, cursor, &package->features )
472 {
473 MSIFEATURE *feature = LIST_ENTRY( item, MSIFEATURE, entry );
474 list_remove( &feature->entry );
475 free_feature( feature );
476 }
477
478 LIST_FOR_EACH_SAFE( item, cursor, &package->folders )
479 {
480 MSIFOLDER *folder = LIST_ENTRY( item, MSIFOLDER, entry );
481
482 list_remove( &folder->entry );
483 msi_free( folder->Directory );
484 msi_free( folder->TargetDefault );
485 msi_free( folder->SourceDefault );
486 msi_free( folder->ResolvedTarget );
487 msi_free( folder->ResolvedSource );
488 msi_free( folder->Property );
489 msi_free( folder );
490 }
491
492 LIST_FOR_EACH_SAFE( item, cursor, &package->components )
493 {
494 MSICOMPONENT *comp = LIST_ENTRY( item, MSICOMPONENT, entry );
495
496 list_remove( &comp->entry );
497 msi_free( comp->Component );
498 msi_free( comp->ComponentId );
499 msi_free( comp->Directory );
500 msi_free( comp->Condition );
501 msi_free( comp->KeyPath );
502 msi_free( comp->FullKeypath );
503 msi_free( comp );
504 }
505
506 LIST_FOR_EACH_SAFE( item, cursor, &package->files )
507 {
508 MSIFILE *file = LIST_ENTRY( item, MSIFILE, entry );
509
510 list_remove( &file->entry );
511 msi_free( file->File );
512 msi_free( file->FileName );
513 msi_free( file->ShortName );
514 msi_free( file->Version );
515 msi_free( file->Language );
516 msi_free( file->SourcePath );
517 msi_free( file->TargetPath );
518 msi_free( file );
519 }
520
521 /* clean up extension, progid, class and verb structures */
522 LIST_FOR_EACH_SAFE( item, cursor, &package->classes )
523 {
524 MSICLASS *cls = LIST_ENTRY( item, MSICLASS, entry );
525
526 list_remove( &cls->entry );
527 msi_free( cls->clsid );
528 msi_free( cls->Context );
529 msi_free( cls->Description );
530 msi_free( cls->FileTypeMask );
531 msi_free( cls->IconPath );
532 msi_free( cls->DefInprocHandler );
533 msi_free( cls->DefInprocHandler32 );
534 msi_free( cls->Argument );
535 msi_free( cls->ProgIDText );
536 msi_free( cls );
537 }
538
539 LIST_FOR_EACH_SAFE( item, cursor, &package->extensions )
540 {
541 MSIEXTENSION *ext = LIST_ENTRY( item, MSIEXTENSION, entry );
542
543 list_remove( &ext->entry );
544 free_extension( ext );
545 }
546
547 LIST_FOR_EACH_SAFE( item, cursor, &package->progids )
548 {
549 MSIPROGID *progid = LIST_ENTRY( item, MSIPROGID, entry );
550
551 list_remove( &progid->entry );
552 msi_free( progid->ProgID );
553 msi_free( progid->Description );
554 msi_free( progid->IconPath );
555 msi_free( progid );
556 }
557
558 LIST_FOR_EACH_SAFE( item, cursor, &package->mimes )
559 {
560 MSIMIME *mt = LIST_ENTRY( item, MSIMIME, entry );
561
562 list_remove( &mt->entry );
563 msi_free( mt->clsid );
564 msi_free( mt->ContentType );
565 msi_free( mt );
566 }
567
568 LIST_FOR_EACH_SAFE( item, cursor, &package->appids )
569 {
570 MSIAPPID *appid = LIST_ENTRY( item, MSIAPPID, entry );
571
572 list_remove( &appid->entry );
573 msi_free( appid->AppID );
574 msi_free( appid->RemoteServerName );
575 msi_free( appid->LocalServer );
576 msi_free( appid->ServiceParameters );
577 msi_free( appid->DllSurrogate );
578 msi_free( appid );
579 }
580
581 if (package->script)
582 {
583 for (i = 0; i < TOTAL_SCRIPTS; i++)
584 {
585 int j;
586 for (j = 0; j < package->script->ActionCount[i]; j++)
587 msi_free(package->script->Actions[i][j]);
588
589 msi_free(package->script->Actions[i]);
590 }
591
592 for (i = 0; i < package->script->UniqueActionsCount; i++)
593 msi_free(package->script->UniqueActions[i]);
594
595 msi_free(package->script->UniqueActions);
596 msi_free(package->script);
597 }
598
599 msi_free(package->PackagePath);
600 msi_free(package->ProductCode);
601 msi_free(package->ActionFormat);
602 msi_free(package->LastAction);
603
604 /* cleanup control event subscriptions */
605 ControlEvent_CleanupSubscriptions(package);
606 }
607
608 /*
609 * build_directory_name()
610 *
611 * This function is to save messing round with directory names
612 * It handles adding backslashes between path segments,
613 * and can add \ at the end of the directory name if told to.
614 *
615 * It takes a variable number of arguments.
616 * It always allocates a new string for the result, so make sure
617 * to free the return value when finished with it.
618 *
619 * The first arg is the number of path segments that follow.
620 * The arguments following count are a list of path segments.
621 * A path segment may be NULL.
622 *
623 * Path segments will be added with a \ separating them.
624 * A \ will not be added after the last segment, however if the
625 * last segment is NULL, then the last character will be a \
626 *
627 */
628 LPWSTR build_directory_name(DWORD count, ...)
629 {
630 DWORD sz = 1, i;
631 LPWSTR dir;
632 va_list va;
633
634 va_start(va,count);
635 for(i=0; i<count; i++)
636 {
637 LPCWSTR str = va_arg(va,LPCWSTR);
638 if (str)
639 sz += strlenW(str) + 1;
640 }
641 va_end(va);
642
643 dir = msi_alloc(sz*sizeof(WCHAR));
644 dir[0]=0;
645
646 va_start(va,count);
647 for(i=0; i<count; i++)
648 {
649 LPCWSTR str = va_arg(va,LPCWSTR);
650 if (!str)
651 continue;
652 strcatW(dir, str);
653 if( ((i+1)!=count) && dir[strlenW(dir)-1]!='\\')
654 strcatW(dir, cszbs);
655 }
656 return dir;
657 }
658
659 /***********************************************************************
660 * create_full_pathW
661 *
662 * Recursively create all directories in the path.
663 *
664 * shamelessly stolen from setupapi/queue.c
665 */
666 BOOL create_full_pathW(const WCHAR *path)
667 {
668 BOOL ret = TRUE;
669 int len;
670 WCHAR *new_path;
671
672 new_path = msi_alloc( (strlenW(path) + 1) * sizeof(WCHAR));
673
674 strcpyW(new_path, path);
675
676 while((len = strlenW(new_path)) && new_path[len - 1] == '\\')
677 new_path[len - 1] = 0;
678
679 while(!CreateDirectoryW(new_path, NULL))
680 {
681 WCHAR *slash;
682 DWORD last_error = GetLastError();
683 if(last_error == ERROR_ALREADY_EXISTS)
684 break;
685
686 if(last_error != ERROR_PATH_NOT_FOUND)
687 {
688 ret = FALSE;
689 break;
690 }
691
692 if(!(slash = strrchrW(new_path, '\\')))
693 {
694 ret = FALSE;
695 break;
696 }
697
698 len = slash - new_path;
699 new_path[len] = 0;
700 if(!create_full_pathW(new_path))
701 {
702 ret = FALSE;
703 break;
704 }
705 new_path[len] = '\\';
706 }
707
708 msi_free(new_path);
709 return ret;
710 }
711
712 void ui_progress(MSIPACKAGE *package, int a, int b, int c, int d )
713 {
714 MSIRECORD * row;
715
716 row = MSI_CreateRecord(4);
717 MSI_RecordSetInteger(row,1,a);
718 MSI_RecordSetInteger(row,2,b);
719 MSI_RecordSetInteger(row,3,c);
720 MSI_RecordSetInteger(row,4,d);
721 MSI_ProcessMessage(package, INSTALLMESSAGE_PROGRESS, row);
722 msiobj_release(&row->hdr);
723
724 msi_dialog_check_messages(NULL);
725 }
726
727 void ui_actiondata(MSIPACKAGE *package, LPCWSTR action, MSIRECORD * record)
728 {
729 static const WCHAR Query_t[] =
730 {'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
731 '`','A','c','t','i','o', 'n','T','e','x','t','`',' ',
732 'W','H','E','R','E',' ', '`','A','c','t','i','o','n','`',' ','=',
733 ' ','\'','%','s','\'',0};
734 WCHAR message[1024];
735 MSIRECORD * row = 0;
736 DWORD size;
737 static const WCHAR szActionData[] =
738 {'A','c','t','i','o','n','D','a','t','a',0};
739
740 if (!package->LastAction || strcmpW(package->LastAction,action))
741 {
742 row = MSI_QueryGetRecord(package->db, Query_t, action);
743 if (!row)
744 return;
745
746 if (MSI_RecordIsNull(row,3))
747 {
748 msiobj_release(&row->hdr);
749 return;
750 }
751
752 /* update the cached actionformat */
753 msi_free(package->ActionFormat);
754 package->ActionFormat = load_dynamic_stringW(row,3);
755
756 msi_free(package->LastAction);
757 package->LastAction = strdupW(action);
758
759 msiobj_release(&row->hdr);
760 }
761
762 MSI_RecordSetStringW(record,0,package->ActionFormat);
763 size = 1024;
764 MSI_FormatRecordW(package,record,message,&size);
765
766 row = MSI_CreateRecord(1);
767 MSI_RecordSetStringW(row,1,message);
768
769 MSI_ProcessMessage(package, INSTALLMESSAGE_ACTIONDATA, row);
770
771 ControlEvent_FireSubscribedEvent(package,szActionData, row);
772
773 msiobj_release(&row->hdr);
774 }
775
776 BOOL ACTION_VerifyComponentForAction(MSIPACKAGE* package, MSICOMPONENT* comp,
777 INSTALLSTATE check )
778 {
779 if (!comp)
780 return FALSE;
781
782 if (comp->Installed == check)
783 return FALSE;
784
785 if (comp->ActionRequest == check)
786 return TRUE;
787 else
788 return FALSE;
789 }
790
791 BOOL ACTION_VerifyFeatureForAction( MSIFEATURE* feature, INSTALLSTATE check )
792 {
793 if (!feature)
794 return FALSE;
795
796 if (feature->Installed == check)
797 return FALSE;
798
799 if (feature->ActionRequest == check)
800 return TRUE;
801 else
802 return FALSE;
803 }
804
805 void reduce_to_longfilename(WCHAR* filename)
806 {
807 LPWSTR p = strchrW(filename,'|');
808 if (p)
809 memmove(filename, p+1, (strlenW(p+1)+1)*sizeof(WCHAR));
810 }
811
812 void reduce_to_shortfilename(WCHAR* filename)
813 {
814 LPWSTR p = strchrW(filename,'|');
815 if (p)
816 *p = 0;
817 }
818
819 LPWSTR create_component_advertise_string(MSIPACKAGE* package,
820 MSICOMPONENT* component, LPCWSTR feature)
821 {
822 GUID clsid;
823 WCHAR productid_85[21];
824 WCHAR component_85[21];
825 /*
826 * I have a fair bit of confusion as to when a < is used and when a > is
827 * used. I do not think i have it right...
828 *
829 * Ok it appears that the > is used if there is a guid for the compoenent
830 * and the < is used if not.
831 */
832 static WCHAR fmt1[] = {'%','s','%','s','<',0,0};
833 static WCHAR fmt2[] = {'%','s','%','s','>','%','s',0,0};
834 LPWSTR output = NULL;
835 DWORD sz = 0;
836
837 memset(productid_85,0,sizeof(productid_85));
838 memset(component_85,0,sizeof(component_85));
839
840 CLSIDFromString(package->ProductCode, &clsid);
841
842 encode_base85_guid(&clsid,productid_85);
843
844 CLSIDFromString(component->ComponentId, &clsid);
845 encode_base85_guid(&clsid,component_85);
846
847 TRACE("Doing something with this... %s %s %s\n",
848 debugstr_w(productid_85), debugstr_w(feature),
849 debugstr_w(component_85));
850
851 sz = lstrlenW(productid_85) + lstrlenW(feature);
852 if (component)
853 sz += lstrlenW(component_85);
854
855 sz+=3;
856 sz *= sizeof(WCHAR);
857
858 output = msi_alloc(sz);
859 memset(output,0,sz);
860
861 if (component)
862 sprintfW(output,fmt2,productid_85,feature,component_85);
863 else
864 sprintfW(output,fmt1,productid_85,feature);
865
866 return output;
867 }
868
869 /* update compoennt state based on a feature change */
870 void ACTION_UpdateComponentStates(MSIPACKAGE *package, LPCWSTR szFeature)
871 {
872 INSTALLSTATE newstate;
873 MSIFEATURE *feature;
874 ComponentList *cl;
875
876 feature = get_loaded_feature(package,szFeature);
877 if (!feature)
878 return;
879
880 newstate = feature->ActionRequest;
881
882 LIST_FOR_EACH_ENTRY( cl, &feature->Components, ComponentList, entry )
883 {
884 MSICOMPONENT* component = cl->component;
885
886 TRACE("MODIFYING(%i): Component %s (Installed %i, Action %i, Request %i)\n",
887 newstate, debugstr_w(component->Component), component->Installed,
888 component->Action, component->ActionRequest);
889
890 if (!component->Enabled)
891 continue;
892
893 if (newstate == INSTALLSTATE_LOCAL)
894 {
895 component->ActionRequest = INSTALLSTATE_LOCAL;
896 component->Action = INSTALLSTATE_LOCAL;
897 }
898 else
899 {
900 ComponentList *clist;
901 MSIFEATURE *f;
902
903 component->ActionRequest = newstate;
904 component->Action = newstate;
905
906 /*if any other feature wants is local we need to set it local*/
907 LIST_FOR_EACH_ENTRY( f, &package->features, MSIFEATURE, entry )
908 {
909 if ( component->ActionRequest != INSTALLSTATE_LOCAL )
910 break;
911
912 LIST_FOR_EACH_ENTRY( clist, &f->Components, ComponentList, entry )
913 {
914 if ( clist->component == component )
915 {
916 if (f->ActionRequest == INSTALLSTATE_LOCAL)
917 {
918 TRACE("Saved by %s\n", debugstr_w(f->Feature));
919 component->ActionRequest = INSTALLSTATE_LOCAL;
920 component->Action = INSTALLSTATE_LOCAL;
921 }
922 break;
923 }
924 }
925 }
926 }
927 TRACE("Result (%i): Component %s (Installed %i, Action %i, Request %i)\n",
928 newstate, debugstr_w(component->Component), component->Installed,
929 component->Action, component->ActionRequest);
930 }
931 }
932
933 UINT register_unique_action(MSIPACKAGE *package, LPCWSTR action)
934 {
935 UINT count;
936 LPWSTR *newbuf = NULL;
937
938 if (!package || !package->script)
939 return FALSE;
940
941 TRACE("Registering Action %s as having fun\n",debugstr_w(action));
942
943 count = package->script->UniqueActionsCount;
944 package->script->UniqueActionsCount++;
945 if (count != 0)
946 newbuf = msi_realloc( package->script->UniqueActions,
947 package->script->UniqueActionsCount* sizeof(LPWSTR));
948 else
949 newbuf = msi_alloc( sizeof(LPWSTR));
950
951 newbuf[count] = strdupW(action);
952 package->script->UniqueActions = newbuf;
953
954 return ERROR_SUCCESS;
955 }
956
957 BOOL check_unique_action(MSIPACKAGE *package, LPCWSTR action)
958 {
959 INT i;
960
961 if (!package || !package->script)
962 return FALSE;
963
964 for (i = 0; i < package->script->UniqueActionsCount; i++)
965 if (!strcmpW(package->script->UniqueActions[i],action))
966 return TRUE;
967
968 return FALSE;
969 }
970
971 WCHAR* generate_error_string(MSIPACKAGE *package, UINT error, DWORD count, ... )
972 {
973 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};
974
975 MSIRECORD *rec;
976 MSIRECORD *row;
977 DWORD size = 0;
978 DWORD i;
979 va_list va;
980 LPCWSTR str;
981 LPWSTR data;
982
983 row = MSI_QueryGetRecord(package->db, query, error);
984 if (!row)
985 return 0;
986
987 rec = MSI_CreateRecord(count+2);
988
989 str = MSI_RecordGetString(row,1);
990 MSI_RecordSetStringW(rec,0,str);
991 msiobj_release( &row->hdr );
992 MSI_RecordSetInteger(rec,1,error);
993
994 va_start(va,count);
995 for (i = 0; i < count; i++)
996 {
997 str = va_arg(va,LPCWSTR);
998 MSI_RecordSetStringW(rec,(i+2),str);
999 }
1000 va_end(va);
1001
1002 MSI_FormatRecordW(package,rec,NULL,&size);
1003 if (size >= 0)
1004 {
1005 size++;
1006 data = msi_alloc(size*sizeof(WCHAR));
1007 if (size > 1)
1008 MSI_FormatRecordW(package,rec,data,&size);
1009 else
1010 data[0] = 0;
1011 msiobj_release( &rec->hdr );
1012 return data;
1013 }
1014
1015 msiobj_release( &rec->hdr );
1016 data = NULL;
1017 return data;
1018 }