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