taskkill: Implement graceful termination by process name.
[wine] / dlls / msi / package.c
1 /*
2  * Implementation of the Microsoft Installer (msi.dll)
3  *
4  * Copyright 2004 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 #define NONAMELESSUNION
22 #define NONAMELESSSTRUCT
23 #define COBJMACROS
24
25 #include <stdarg.h>
26 #include "windef.h"
27 #include "winbase.h"
28 #include "winreg.h"
29 #include "winnls.h"
30 #include "shlwapi.h"
31 #include "wingdi.h"
32 #include "wine/debug.h"
33 #include "msi.h"
34 #include "msiquery.h"
35 #include "objidl.h"
36 #include "wincrypt.h"
37 #include "winuser.h"
38 #include "wininet.h"
39 #include "winver.h"
40 #include "urlmon.h"
41 #include "shlobj.h"
42 #include "wine/unicode.h"
43 #include "objbase.h"
44 #include "msidefs.h"
45 #include "sddl.h"
46
47 #include "msipriv.h"
48 #include "msiserver.h"
49
50 WINE_DEFAULT_DEBUG_CHANNEL(msi);
51
52 static void remove_tracked_tempfiles( MSIPACKAGE *package )
53 {
54     struct list *item, *cursor;
55
56     LIST_FOR_EACH_SAFE( item, cursor, &package->tempfiles )
57     {
58         MSITEMPFILE *temp = LIST_ENTRY( item, MSITEMPFILE, entry );
59
60         list_remove( &temp->entry );
61         TRACE("deleting temp file %s\n", debugstr_w( temp->Path ));
62         if (!DeleteFileW( temp->Path ))
63             ERR("failed to delete %s\n", debugstr_w( temp->Path ));
64         msi_free( temp->Path );
65         msi_free( temp );
66     }
67 }
68
69 static void free_feature( MSIFEATURE *feature )
70 {
71     struct list *item, *cursor;
72
73     LIST_FOR_EACH_SAFE( item, cursor, &feature->Children )
74     {
75         FeatureList *fl = LIST_ENTRY( item, FeatureList, entry );
76         list_remove( &fl->entry );
77         msi_free( fl );
78     }
79
80     LIST_FOR_EACH_SAFE( item, cursor, &feature->Components )
81     {
82         ComponentList *cl = LIST_ENTRY( item, ComponentList, entry );
83         list_remove( &cl->entry );
84         msi_free( cl );
85     }
86     msi_free( feature->Feature );
87     msi_free( feature->Feature_Parent );
88     msi_free( feature->Directory );
89     msi_free( feature->Description );
90     msi_free( feature->Title );
91     msi_free( feature );
92 }
93
94 static void free_extension( MSIEXTENSION *ext )
95 {
96     struct list *item, *cursor;
97
98     LIST_FOR_EACH_SAFE( item, cursor, &ext->verbs )
99     {
100         MSIVERB *verb = LIST_ENTRY( item, MSIVERB, entry );
101
102         list_remove( &verb->entry );
103         msi_free( verb->Verb );
104         msi_free( verb->Command );
105         msi_free( verb->Argument );
106         msi_free( verb );
107     }
108
109     msi_free( ext->Extension );
110     msi_free( ext->ProgIDText );
111     msi_free( ext );
112 }
113
114 static void free_package_structures( MSIPACKAGE *package )
115 {
116     INT i;
117     struct list *item, *cursor;
118
119     TRACE("Freeing package action data\n");
120
121     remove_tracked_tempfiles(package);
122
123     LIST_FOR_EACH_SAFE( item, cursor, &package->features )
124     {
125         MSIFEATURE *feature = LIST_ENTRY( item, MSIFEATURE, entry );
126         list_remove( &feature->entry );
127         free_feature( feature );
128     }
129
130     LIST_FOR_EACH_SAFE( item, cursor, &package->folders )
131     {
132         MSIFOLDER *folder = LIST_ENTRY( item, MSIFOLDER, entry );
133
134         list_remove( &folder->entry );
135         msi_free( folder->Parent );
136         msi_free( folder->Directory );
137         msi_free( folder->TargetDefault );
138         msi_free( folder->SourceLongPath );
139         msi_free( folder->SourceShortPath );
140         msi_free( folder->ResolvedTarget );
141         msi_free( folder->ResolvedSource );
142         msi_free( folder->Property );
143         msi_free( folder );
144     }
145
146     LIST_FOR_EACH_SAFE( item, cursor, &package->components )
147     {
148         MSICOMPONENT *comp = LIST_ENTRY( item, MSICOMPONENT, entry );
149
150         list_remove( &comp->entry );
151         msi_free( comp->Component );
152         msi_free( comp->ComponentId );
153         msi_free( comp->Directory );
154         msi_free( comp->Condition );
155         msi_free( comp->KeyPath );
156         msi_free( comp->FullKeypath );
157         msi_free( comp );
158     }
159
160     LIST_FOR_EACH_SAFE( item, cursor, &package->files )
161     {
162         MSIFILE *file = LIST_ENTRY( item, MSIFILE, entry );
163
164         list_remove( &file->entry );
165         msi_free( file->File );
166         msi_free( file->FileName );
167         msi_free( file->ShortName );
168         msi_free( file->LongName );
169         msi_free( file->Version );
170         msi_free( file->Language );
171         msi_free( file->TargetPath );
172         msi_free( file );
173     }
174
175     /* clean up extension, progid, class and verb structures */
176     LIST_FOR_EACH_SAFE( item, cursor, &package->classes )
177     {
178         MSICLASS *cls = LIST_ENTRY( item, MSICLASS, entry );
179
180         list_remove( &cls->entry );
181         msi_free( cls->clsid );
182         msi_free( cls->Context );
183         msi_free( cls->Description );
184         msi_free( cls->FileTypeMask );
185         msi_free( cls->IconPath );
186         msi_free( cls->DefInprocHandler );
187         msi_free( cls->DefInprocHandler32 );
188         msi_free( cls->Argument );
189         msi_free( cls->ProgIDText );
190         msi_free( cls );
191     }
192
193     LIST_FOR_EACH_SAFE( item, cursor, &package->extensions )
194     {
195         MSIEXTENSION *ext = LIST_ENTRY( item, MSIEXTENSION, entry );
196
197         list_remove( &ext->entry );
198         free_extension( ext );
199     }
200
201     LIST_FOR_EACH_SAFE( item, cursor, &package->progids )
202     {
203         MSIPROGID *progid = LIST_ENTRY( item, MSIPROGID, entry );
204
205         list_remove( &progid->entry );
206         msi_free( progid->ProgID );
207         msi_free( progid->Description );
208         msi_free( progid->IconPath );
209         msi_free( progid );
210     }
211
212     LIST_FOR_EACH_SAFE( item, cursor, &package->mimes )
213     {
214         MSIMIME *mt = LIST_ENTRY( item, MSIMIME, entry );
215
216         list_remove( &mt->entry );
217         msi_free( mt->suffix );
218         msi_free( mt->clsid );
219         msi_free( mt->ContentType );
220         msi_free( mt );
221     }
222
223     LIST_FOR_EACH_SAFE( item, cursor, &package->appids )
224     {
225         MSIAPPID *appid = LIST_ENTRY( item, MSIAPPID, entry );
226
227         list_remove( &appid->entry );
228         msi_free( appid->AppID );
229         msi_free( appid->RemoteServerName );
230         msi_free( appid->LocalServer );
231         msi_free( appid->ServiceParameters );
232         msi_free( appid->DllSurrogate );
233         msi_free( appid );
234     }
235
236     LIST_FOR_EACH_SAFE( item, cursor, &package->sourcelist_info )
237     {
238         MSISOURCELISTINFO *info = LIST_ENTRY( item, MSISOURCELISTINFO, entry );
239
240         list_remove( &info->entry );
241         msi_free( info->value );
242         msi_free( info );
243     }
244
245     LIST_FOR_EACH_SAFE( item, cursor, &package->sourcelist_media )
246     {
247         MSIMEDIADISK *info = LIST_ENTRY( item, MSIMEDIADISK, entry );
248
249         list_remove( &info->entry );
250         msi_free( info->volume_label );
251         msi_free( info->disk_prompt );
252         msi_free( info );
253     }
254
255     if (package->script)
256     {
257         for (i = 0; i < TOTAL_SCRIPTS; i++)
258             msi_free_action_script( package, i );
259
260         for (i = 0; i < package->script->UniqueActionsCount; i++)
261             msi_free( package->script->UniqueActions[i] );
262
263         msi_free( package->script->UniqueActions );
264         msi_free( package->script );
265     }
266
267     LIST_FOR_EACH_SAFE( item, cursor, &package->patches )
268     {
269         MSIPATCHINFO *patch = LIST_ENTRY( item, MSIPATCHINFO, entry );
270
271         list_remove( &patch->entry );
272         msi_free( patch->patchcode );
273         msi_free( patch->transforms );
274         msi_free( patch->localfile );
275         msi_free( patch );
276     }
277
278     msi_free( package->BaseURL );
279     msi_free( package->PackagePath );
280     msi_free( package->ProductCode );
281     msi_free( package->ActionFormat );
282     msi_free( package->LastAction );
283     msi_free( package->langids );
284
285     /* cleanup control event subscriptions */
286     ControlEvent_CleanupSubscriptions( package );
287 }
288
289 static void MSI_FreePackage( MSIOBJECTHDR *arg)
290 {
291     MSIPACKAGE *package= (MSIPACKAGE*) arg;
292
293     if( package->dialog )
294         msi_dialog_destroy( package->dialog );
295
296     msiobj_release( &package->db->hdr );
297     free_package_structures(package);
298 }
299
300 static UINT create_temp_property_table(MSIPACKAGE *package)
301 {
302     MSIQUERY *view = NULL;
303     UINT rc;
304
305     static const WCHAR CreateSql[] = {
306        'C','R','E','A','T','E',' ','T','A','B','L','E',' ',
307        '`','_','P','r','o','p','e','r','t','y','`',' ','(',' ',
308        '`','_','P','r','o','p','e','r','t','y','`',' ',
309        'C','H','A','R','(','5','6',')',' ','N','O','T',' ','N','U','L','L',' ',
310        'T','E','M','P','O','R','A','R','Y',',',' ',
311        '`','V','a','l','u','e','`',' ','C','H','A','R','(','9','8',')',' ',
312        'N','O','T',' ','N','U','L','L',' ','T','E','M','P','O','R','A','R','Y',
313        ' ','P','R','I','M','A','R','Y',' ','K','E','Y',' ',
314        '`','_','P','r','o','p','e','r','t','y','`',')',' ','H','O','L','D',0};
315
316     rc = MSI_DatabaseOpenViewW(package->db, CreateSql, &view);
317     if (rc != ERROR_SUCCESS)
318         return rc;
319
320     rc = MSI_ViewExecute(view, 0);
321     MSI_ViewClose(view);
322     msiobj_release(&view->hdr);
323     return rc;
324 }
325
326 UINT msi_clone_properties(MSIPACKAGE *package)
327 {
328     MSIQUERY *view_select = NULL;
329     UINT rc;
330
331     static const WCHAR query_select[] = {
332        'S','E','L','E','C','T',' ','*',' ',
333        'F','R','O','M',' ','`','P','r','o','p','e','r','t','y','`',0};
334     static const WCHAR query_insert[] = {
335        'I','N','S','E','R','T',' ','i','n','t','o',' ',
336        '`','_','P','r','o','p','e','r','t','y','`',' ',
337        '(','`','_','P','r','o','p','e','r','t','y','`',',',
338        '`','V','a','l','u','e','`',')',' ',
339        'V','A','L','U','E','S',' ','(','?',',','?',')',0};
340     static const WCHAR query_update[] = {
341         'U','P','D','A','T','E',' ','`','_','P','r','o','p','e','r','t','y','`',' ',
342         'S','E','T',' ','`','V','a','l','u','e','`',' ','=',' ','?',' ',
343         'W','H','E','R','E',' ','`','_','P','r','o','p','e','r','t','y','`',' ','=',' ','?',0};
344
345     rc = MSI_DatabaseOpenViewW( package->db, query_select, &view_select );
346     if (rc != ERROR_SUCCESS)
347         return rc;
348
349     rc = MSI_ViewExecute( view_select, 0 );
350     if (rc != ERROR_SUCCESS)
351     {
352         MSI_ViewClose( view_select );
353         msiobj_release( &view_select->hdr );
354         return rc;
355     }
356
357     while (1)
358     {
359         MSIQUERY *view_insert, *view_update;
360         MSIRECORD *rec_select;
361
362         rc = MSI_ViewFetch( view_select, &rec_select );
363         if (rc != ERROR_SUCCESS)
364             break;
365
366         rc = MSI_DatabaseOpenViewW( package->db, query_insert, &view_insert );
367         if (rc != ERROR_SUCCESS)
368         {
369             msiobj_release( &rec_select->hdr );
370             continue;
371         }
372
373         rc = MSI_ViewExecute( view_insert, rec_select );
374         MSI_ViewClose( view_insert );
375         msiobj_release( &view_insert->hdr );
376         if (rc != ERROR_SUCCESS)
377         {
378             MSIRECORD *rec_update;
379
380             TRACE("insert failed, trying update\n");
381
382             rc = MSI_DatabaseOpenViewW( package->db, query_update, &view_update );
383             if (rc != ERROR_SUCCESS)
384             {
385                 WARN("open view failed %u\n", rc);
386                 msiobj_release( &rec_select->hdr );
387                 continue;
388             }
389
390             rec_update = MSI_CreateRecord( 2 );
391             MSI_RecordCopyField( rec_select, 1, rec_update, 2 );
392             MSI_RecordCopyField( rec_select, 2, rec_update, 1 );
393             rc = MSI_ViewExecute( view_update, rec_update );
394             if (rc != ERROR_SUCCESS)
395                 WARN("update failed %u\n", rc);
396
397             MSI_ViewClose( view_update );
398             msiobj_release( &view_update->hdr );
399             msiobj_release( &rec_update->hdr );
400         }
401
402         msiobj_release( &rec_select->hdr );
403     }
404
405     MSI_ViewClose( view_select );
406     msiobj_release( &view_select->hdr );
407     return rc;
408 }
409
410 /*
411  * set_installed_prop
412  *
413  * Sets the "Installed" property to indicate that
414  *  the product is installed for the current user.
415  */
416 static UINT set_installed_prop( MSIPACKAGE *package )
417 {
418     HKEY hkey = 0;
419     UINT r;
420
421     r = MSIREG_OpenUninstallKey( package->ProductCode, &hkey, FALSE );
422     if (r == ERROR_SUCCESS)
423     {
424         RegCloseKey( hkey );
425         msi_set_property( package->db, szInstalled, szOne );
426     }
427
428     return r;
429 }
430
431 static UINT set_user_sid_prop( MSIPACKAGE *package )
432 {
433     SID_NAME_USE use;
434     LPWSTR user_name;
435     LPWSTR sid_str = NULL, dom = NULL;
436     DWORD size, dom_size;
437     PSID psid = NULL;
438     UINT r = ERROR_FUNCTION_FAILED;
439
440     size = 0;
441     GetUserNameW( NULL, &size );
442
443     user_name = msi_alloc( (size + 1) * sizeof(WCHAR) );
444     if (!user_name)
445         return ERROR_OUTOFMEMORY;
446
447     if (!GetUserNameW( user_name, &size ))
448         goto done;
449
450     size = 0;
451     dom_size = 0;
452     LookupAccountNameW( NULL, user_name, NULL, &size, NULL, &dom_size, &use );
453
454     psid = msi_alloc( size );
455     dom = msi_alloc( dom_size*sizeof (WCHAR) );
456     if (!psid || !dom)
457     {
458         r = ERROR_OUTOFMEMORY;
459         goto done;
460     }
461
462     if (!LookupAccountNameW( NULL, user_name, psid, &size, dom, &dom_size, &use ))
463         goto done;
464
465     if (!ConvertSidToStringSidW( psid, &sid_str ))
466         goto done;
467
468     r = msi_set_property( package->db, szUserSID, sid_str );
469
470 done:
471     LocalFree( sid_str );
472     msi_free( dom );
473     msi_free( psid );
474     msi_free( user_name );
475
476     return r;
477 }
478
479 static LPWSTR get_fusion_filename(MSIPACKAGE *package)
480 {
481     HKEY netsetup;
482     LONG res;
483     LPWSTR file = NULL;
484     DWORD index = 0, size;
485     WCHAR ver[MAX_PATH];
486     WCHAR name[MAX_PATH];
487     WCHAR windir[MAX_PATH];
488
489     static const WCHAR fusion[] = {'f','u','s','i','o','n','.','d','l','l',0};
490     static const WCHAR sub[] = {
491         'S','o','f','t','w','a','r','e','\\',
492         'M','i','c','r','o','s','o','f','t','\\',
493         'N','E','T',' ','F','r','a','m','e','w','o','r','k',' ','S','e','t','u','p','\\',
494         'N','D','P',0
495     };
496     static const WCHAR subdir[] = {
497         'M','i','c','r','o','s','o','f','t','.','N','E','T','\\',
498         'F','r','a','m','e','w','o','r','k','\\',0
499     };
500
501     res = RegOpenKeyExW(HKEY_LOCAL_MACHINE, sub, 0, KEY_ENUMERATE_SUB_KEYS, &netsetup);
502     if (res != ERROR_SUCCESS)
503         return NULL;
504
505     GetWindowsDirectoryW(windir, MAX_PATH);
506
507     ver[0] = '\0';
508     size = MAX_PATH;
509     while (RegEnumKeyExW(netsetup, index, name, &size, NULL, NULL, NULL, NULL) == ERROR_SUCCESS)
510     {
511         index++;
512
513         /* verify existence of fusion.dll .Net 3.0 does not install a new one */
514         if (lstrcmpW(ver, name) < 0)
515         {
516             LPWSTR check;
517             size = lstrlenW(windir) + lstrlenW(subdir) + lstrlenW(name) +lstrlenW(fusion) + 3;
518             check = msi_alloc(size * sizeof(WCHAR));
519
520             if (!check)
521             {
522                 msi_free(file);
523                 return NULL;
524             }
525
526             lstrcpyW(check, windir);
527             lstrcatW(check, szBackSlash);
528             lstrcatW(check, subdir);
529             lstrcatW(check, name);
530             lstrcatW(check, szBackSlash);
531             lstrcatW(check, fusion);
532
533             if(GetFileAttributesW(check) != INVALID_FILE_ATTRIBUTES)
534             {
535                 msi_free(file);
536                 file = check;
537                 lstrcpyW(ver, name);
538             }
539             else
540                 msi_free(check);
541         }
542     }
543
544     RegCloseKey(netsetup);
545     return file;
546 }
547
548 typedef struct tagLANGANDCODEPAGE
549 {
550   WORD wLanguage;
551   WORD wCodePage;
552 } LANGANDCODEPAGE;
553
554 static void set_msi_assembly_prop(MSIPACKAGE *package)
555 {
556     UINT val_len;
557     DWORD size, handle;
558     LPVOID version = NULL;
559     WCHAR buf[MAX_PATH];
560     LPWSTR fusion, verstr;
561     LANGANDCODEPAGE *translate;
562
563     static const WCHAR netasm[] = {
564         'M','s','i','N','e','t','A','s','s','e','m','b','l','y','S','u','p','p','o','r','t',0
565     };
566     static const WCHAR translation[] = {
567         '\\','V','a','r','F','i','l','e','I','n','f','o',
568         '\\','T','r','a','n','s','l','a','t','i','o','n',0
569     };
570     static const WCHAR verfmt[] = {
571         '\\','S','t','r','i','n','g','F','i','l','e','I','n','f','o',
572         '\\','%','0','4','x','%','0','4','x',
573         '\\','P','r','o','d','u','c','t','V','e','r','s','i','o','n',0
574     };
575
576     fusion = get_fusion_filename(package);
577     if (!fusion)
578         return;
579
580     size = GetFileVersionInfoSizeW(fusion, &handle);
581     if (!size) return;
582
583     version = msi_alloc(size);
584     if (!version) return;
585
586     if (!GetFileVersionInfoW(fusion, handle, size, version))
587         goto done;
588
589     if (!VerQueryValueW(version, translation, (LPVOID *)&translate, &val_len))
590         goto done;
591
592     sprintfW(buf, verfmt, translate[0].wLanguage, translate[0].wCodePage);
593
594     if (!VerQueryValueW(version, buf, (LPVOID *)&verstr, &val_len))
595         goto done;
596
597     if (!val_len || !verstr)
598         goto done;
599
600     msi_set_property(package->db, netasm, verstr);
601
602 done:
603     msi_free(fusion);
604     msi_free(version);
605 }
606
607 static VOID set_installer_properties(MSIPACKAGE *package)
608 {
609     WCHAR pth[MAX_PATH];
610     WCHAR *ptr;
611     OSVERSIONINFOEXW OSVersion;
612     MEMORYSTATUSEX msex;
613     DWORD verval, len;
614     WCHAR verstr[10], bufstr[20];
615     HDC dc;
616     HKEY hkey;
617     LPWSTR username, companyname;
618     SYSTEM_INFO sys_info;
619     SYSTEMTIME systemtime;
620     LANGID langid;
621
622     static const WCHAR CFF[] = 
623 {'C','o','m','m','o','n','F','i','l','e','s','F','o','l','d','e','r',0};
624     static const WCHAR PFF[] = 
625 {'P','r','o','g','r','a','m','F','i','l','e','s','F','o','l','d','e','r',0};
626     static const WCHAR CADF[] = 
627 {'C','o','m','m','o','n','A','p','p','D','a','t','a','F','o','l','d','e','r',0};
628     static const WCHAR FaF[] = 
629 {'F','a','v','o','r','i','t','e','s','F','o','l','d','e','r',0};
630     static const WCHAR FoF[] = 
631 {'F','o','n','t','s','F','o','l','d','e','r',0};
632     static const WCHAR SendTF[] = 
633 {'S','e','n','d','T','o','F','o','l','d','e','r',0};
634     static const WCHAR SMF[] = 
635 {'S','t','a','r','t','M','e','n','u','F','o','l','d','e','r',0};
636     static const WCHAR StF[] = 
637 {'S','t','a','r','t','u','p','F','o','l','d','e','r',0};
638     static const WCHAR TemplF[] = 
639 {'T','e','m','p','l','a','t','e','F','o','l','d','e','r',0};
640     static const WCHAR DF[] = 
641 {'D','e','s','k','t','o','p','F','o','l','d','e','r',0};
642     static const WCHAR PMF[] = 
643 {'P','r','o','g','r','a','m','M','e','n','u','F','o','l','d','e','r',0};
644     static const WCHAR ATF[] = 
645 {'A','d','m','i','n','T','o','o','l','s','F','o','l','d','e','r',0};
646     static const WCHAR ADF[] = 
647 {'A','p','p','D','a','t','a','F','o','l','d','e','r',0};
648     static const WCHAR SF[] = 
649 {'S','y','s','t','e','m','F','o','l','d','e','r',0};
650     static const WCHAR SF16[] = 
651 {'S','y','s','t','e','m','1','6','F','o','l','d','e','r',0};
652     static const WCHAR LADF[] = 
653 {'L','o','c','a','l','A','p','p','D','a','t','a','F','o','l','d','e','r',0};
654     static const WCHAR MPF[] = 
655 {'M','y','P','i','c','t','u','r','e','s','F','o','l','d','e','r',0};
656     static const WCHAR PF[] = 
657 {'P','e','r','s','o','n','a','l','F','o','l','d','e','r',0};
658     static const WCHAR WF[] = 
659 {'W','i','n','d','o','w','s','F','o','l','d','e','r',0};
660     static const WCHAR WV[] = 
661 {'W','i','n','d','o','w','s','V','o','l','u','m','e',0};
662     static const WCHAR TF[]=
663 {'T','e','m','p','F','o','l','d','e','r',0};
664     static const WCHAR szPriv[] =
665 {'P','r','i','v','i','l','e','g','e','d',0};
666     static const WCHAR v9x[] = { 'V','e','r','s','i','o','n','9','X',0 };
667     static const WCHAR vNT[] = { 'V','e','r','s','i','o','n','N','T',0 };
668     static const WCHAR szMsiNTProductType[] = { 'M','s','i','N','T','P','r','o','d','u','c','t','T','y','p','e',0 };
669     static const WCHAR szFormat[] = {'%','l','i',0};
670     static const WCHAR szWinBuild[] =
671 {'W','i','n','d','o','w','s','B','u','i','l','d', 0 };
672     static const WCHAR szSPL[] = 
673 {'S','e','r','v','i','c','e','P','a','c','k','L','e','v','e','l',0 };
674     static const WCHAR szSix[] = {'6',0 };
675
676     static const WCHAR szVersionMsi[] = { 'V','e','r','s','i','o','n','M','s','i',0 };
677     static const WCHAR szVersionDatabase[] = { 'V','e','r','s','i','o','n','D','a','t','a','b','a','s','e',0 };
678     static const WCHAR szPhysicalMemory[] = { 'P','h','y','s','i','c','a','l','M','e','m','o','r','y',0 };
679     static const WCHAR szFormat2[] = {'%','l','i','.','%','l','i',0};
680 /* Screen properties */
681     static const WCHAR szScreenX[] = {'S','c','r','e','e','n','X',0};
682     static const WCHAR szScreenY[] = {'S','c','r','e','e','n','Y',0};
683     static const WCHAR szColorBits[] = {'C','o','l','o','r','B','i','t','s',0};
684     static const WCHAR szIntFormat[] = {'%','d',0};
685     static const WCHAR szMsiAMD64[] = { 'M','s','i','A','M','D','6','4',0 };
686     static const WCHAR szMsix64[] = { 'M','s','i','x','6','4',0 };
687     static const WCHAR szSystem64Folder[] = { 'S','y','s','t','e','m','6','4','F','o','l','d','e','r',0 };
688     static const WCHAR szUserInfo[] = {
689         'S','O','F','T','W','A','R','E','\\',
690         'M','i','c','r','o','s','o','f','t','\\',
691         'M','S',' ','S','e','t','u','p',' ','(','A','C','M','E',')','\\',
692         'U','s','e','r',' ','I','n','f','o',0
693     };
694     static const WCHAR szDefName[] = { 'D','e','f','N','a','m','e',0 };
695     static const WCHAR szDefCompany[] = { 'D','e','f','C','o','m','p','a','n','y',0 };
696     static const WCHAR szCurrentVersion[] = {
697         'S','O','F','T','W','A','R','E','\\',
698         'M','i','c','r','o','s','o','f','t','\\',
699         'W','i','n','d','o','w','s',' ','N','T','\\',
700         'C','u','r','r','e','n','t','V','e','r','s','i','o','n',0
701     };
702     static const WCHAR szRegisteredUser[] = {'R','e','g','i','s','t','e','r','e','d','O','w','n','e','r',0};
703     static const WCHAR szRegisteredOrg[] = {
704         'R','e','g','i','s','t','e','r','e','d','O','r','g','a','n','i','z','a','t','i','o','n',0
705     };
706     static const WCHAR szUSERNAME[] = {'U','S','E','R','N','A','M','E',0};
707     static const WCHAR szCOMPANYNAME[] = {'C','O','M','P','A','N','Y','N','A','M','E',0};
708     static const WCHAR szDate[] = {'D','a','t','e',0};
709     static const WCHAR szTime[] = {'T','i','m','e',0};
710     static const WCHAR szUserLangID[] = {'U','s','e','r','L','a','n','g','u','a','g','e','I','D',0};
711     static const WCHAR szSystemLangID[] = {'S','y','s','t','e','m','L','a','n','g','u','a','g','e','I','D',0};
712     static const WCHAR szProductState[] = {'P','r','o','d','u','c','t','S','t','a','t','e',0};
713     static const WCHAR szLogonUser[] = {'L','o','g','o','n','U','s','e','r',0};
714
715     /*
716      * Other things that probably should be set:
717      *
718      * ComputerName VirtualMemory
719      * ShellAdvSupport DefaultUIFont PackagecodeChanging
720      * CaptionHeight BorderTop BorderSide TextHeight
721      * RedirectedDllSupport
722      */
723
724     SHGetFolderPathW(NULL,CSIDL_PROGRAM_FILES_COMMON,NULL,0,pth);
725     strcatW(pth, szBackSlash);
726     msi_set_property(package->db, CFF, pth);
727
728     SHGetFolderPathW(NULL,CSIDL_PROGRAM_FILES,NULL,0,pth);
729     strcatW(pth, szBackSlash);
730     msi_set_property(package->db, PFF, pth);
731
732     SHGetFolderPathW(NULL,CSIDL_COMMON_APPDATA,NULL,0,pth);
733     strcatW(pth, szBackSlash);
734     msi_set_property(package->db, CADF, pth);
735
736     SHGetFolderPathW(NULL,CSIDL_FAVORITES,NULL,0,pth);
737     strcatW(pth, szBackSlash);
738     msi_set_property(package->db, FaF, pth);
739
740     SHGetFolderPathW(NULL,CSIDL_FONTS,NULL,0,pth);
741     strcatW(pth, szBackSlash);
742     msi_set_property(package->db, FoF, pth);
743
744     SHGetFolderPathW(NULL,CSIDL_SENDTO,NULL,0,pth);
745     strcatW(pth, szBackSlash);
746     msi_set_property(package->db, SendTF, pth);
747
748     SHGetFolderPathW(NULL,CSIDL_STARTMENU,NULL,0,pth);
749     strcatW(pth, szBackSlash);
750     msi_set_property(package->db, SMF, pth);
751
752     SHGetFolderPathW(NULL,CSIDL_STARTUP,NULL,0,pth);
753     strcatW(pth, szBackSlash);
754     msi_set_property(package->db, StF, pth);
755
756     SHGetFolderPathW(NULL,CSIDL_TEMPLATES,NULL,0,pth);
757     strcatW(pth, szBackSlash);
758     msi_set_property(package->db, TemplF, pth);
759
760     SHGetFolderPathW(NULL,CSIDL_DESKTOP,NULL,0,pth);
761     strcatW(pth, szBackSlash);
762     msi_set_property(package->db, DF, pth);
763
764     SHGetFolderPathW(NULL,CSIDL_PROGRAMS,NULL,0,pth);
765     strcatW(pth, szBackSlash);
766     msi_set_property(package->db, PMF, pth);
767
768     SHGetFolderPathW(NULL,CSIDL_ADMINTOOLS,NULL,0,pth);
769     strcatW(pth, szBackSlash);
770     msi_set_property(package->db, ATF, pth);
771
772     SHGetFolderPathW(NULL,CSIDL_APPDATA,NULL,0,pth);
773     strcatW(pth, szBackSlash);
774     msi_set_property(package->db, ADF, pth);
775
776     SHGetFolderPathW(NULL,CSIDL_SYSTEM,NULL,0,pth);
777     strcatW(pth, szBackSlash);
778     msi_set_property(package->db, SF, pth);
779     msi_set_property(package->db, SF16, pth);
780
781     SHGetFolderPathW(NULL,CSIDL_LOCAL_APPDATA,NULL,0,pth);
782     strcatW(pth, szBackSlash);
783     msi_set_property(package->db, LADF, pth);
784
785     SHGetFolderPathW(NULL,CSIDL_MYPICTURES,NULL,0,pth);
786     strcatW(pth, szBackSlash);
787     msi_set_property(package->db, MPF, pth);
788
789     SHGetFolderPathW(NULL,CSIDL_PERSONAL,NULL,0,pth);
790     strcatW(pth, szBackSlash);
791     msi_set_property(package->db, PF, pth);
792
793     SHGetFolderPathW(NULL,CSIDL_WINDOWS,NULL,0,pth);
794     strcatW(pth, szBackSlash);
795     msi_set_property(package->db, WF, pth);
796     
797     /* Physical Memory is specified in MB. Using total amount. */
798     msex.dwLength = sizeof(msex);
799     GlobalMemoryStatusEx( &msex );
800     sprintfW( bufstr, szIntFormat, (int)(msex.ullTotalPhys/1024/1024));
801     msi_set_property(package->db, szPhysicalMemory, bufstr);
802
803     SHGetFolderPathW(NULL,CSIDL_WINDOWS,NULL,0,pth);
804     ptr = strchrW(pth,'\\');
805     if (ptr)
806         *(ptr+1) = 0;
807     msi_set_property(package->db, WV, pth);
808     
809     GetTempPathW(MAX_PATH,pth);
810     msi_set_property(package->db, TF, pth);
811
812
813     /* in a wine environment the user is always admin and privileged */
814     msi_set_property(package->db, szAdminUser, szOne);
815     msi_set_property(package->db, szPriv, szOne);
816
817     /* set the os things */
818     OSVersion.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEXW);
819     GetVersionExW((OSVERSIONINFOW *)&OSVersion);
820     verval = OSVersion.dwMinorVersion+OSVersion.dwMajorVersion*100;
821     sprintfW(verstr,szFormat,verval);
822     switch (OSVersion.dwPlatformId)
823     {
824         case VER_PLATFORM_WIN32_WINDOWS:    
825             msi_set_property(package->db, v9x, verstr);
826             break;
827         case VER_PLATFORM_WIN32_NT:
828             msi_set_property(package->db, vNT, verstr);
829             sprintfW(verstr,szFormat,OSVersion.wProductType);
830             msi_set_property(package->db, szMsiNTProductType, verstr);
831             break;
832     }
833     sprintfW(verstr,szFormat,OSVersion.dwBuildNumber);
834     msi_set_property(package->db, szWinBuild, verstr);
835     /* just fudge this */
836     msi_set_property(package->db, szSPL, szSix);
837
838     sprintfW( bufstr, szFormat2, MSI_MAJORVERSION, MSI_MINORVERSION);
839     msi_set_property( package->db, szVersionMsi, bufstr );
840     sprintfW( bufstr, szFormat, MSI_MAJORVERSION * 100);
841     msi_set_property( package->db, szVersionDatabase, bufstr );
842
843     GetSystemInfo( &sys_info );
844     if (sys_info.u.s.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_INTEL)
845     {
846         sprintfW( bufstr, szIntFormat, sys_info.wProcessorLevel );
847         msi_set_property( package->db, szIntel, bufstr );
848     }
849     else if (sys_info.u.s.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_AMD64)
850     {
851         sprintfW( bufstr, szIntFormat, sys_info.wProcessorLevel );
852         msi_set_property( package->db, szMsiAMD64, bufstr );
853         msi_set_property( package->db, szMsix64, bufstr );
854
855         GetSystemDirectoryW( pth, MAX_PATH );
856         msi_set_property( package->db, szSystem64Folder, pth );
857     }
858
859     /* Screen properties. */
860     dc = GetDC(0);
861     sprintfW( bufstr, szIntFormat, GetDeviceCaps( dc, HORZRES ) );
862     msi_set_property( package->db, szScreenX, bufstr );
863     sprintfW( bufstr, szIntFormat, GetDeviceCaps( dc, VERTRES ));
864     msi_set_property( package->db, szScreenY, bufstr );
865     sprintfW( bufstr, szIntFormat, GetDeviceCaps( dc, BITSPIXEL ));
866     msi_set_property( package->db, szColorBits, bufstr );
867     ReleaseDC(0, dc);
868
869     /* USERNAME and COMPANYNAME */
870     username = msi_dup_property( package->db, szUSERNAME );
871     companyname = msi_dup_property( package->db, szCOMPANYNAME );
872
873     if ((!username || !companyname) &&
874         RegOpenKeyW( HKEY_CURRENT_USER, szUserInfo, &hkey ) == ERROR_SUCCESS)
875     {
876         if (!username &&
877             (username = msi_reg_get_val_str( hkey, szDefName )))
878             msi_set_property( package->db, szUSERNAME, username );
879         if (!companyname &&
880             (companyname = msi_reg_get_val_str( hkey, szDefCompany )))
881             msi_set_property( package->db, szCOMPANYNAME, companyname );
882         CloseHandle( hkey );
883     }
884     if ((!username || !companyname) &&
885         RegOpenKeyW( HKEY_LOCAL_MACHINE, szCurrentVersion, &hkey ) == ERROR_SUCCESS)
886     {
887         if (!username &&
888             (username = msi_reg_get_val_str( hkey, szRegisteredUser )))
889             msi_set_property( package->db, szUSERNAME, username );
890         if (!companyname &&
891             (companyname = msi_reg_get_val_str( hkey, szRegisteredOrg )))
892             msi_set_property( package->db, szCOMPANYNAME, companyname );
893         CloseHandle( hkey );
894     }
895     msi_free( username );
896     msi_free( companyname );
897
898     if ( set_user_sid_prop( package ) != ERROR_SUCCESS)
899         ERR("Failed to set the UserSID property\n");
900
901     /* Date and time properties */
902     GetSystemTime( &systemtime );
903     if (GetDateFormatW( LOCALE_USER_DEFAULT, DATE_SHORTDATE, &systemtime,
904                         NULL, bufstr, sizeof(bufstr)/sizeof(bufstr[0]) ))
905         msi_set_property( package->db, szDate, bufstr );
906     else
907         ERR("Couldn't set Date property: GetDateFormat failed with error %d\n", GetLastError());
908
909     if (GetTimeFormatW( LOCALE_USER_DEFAULT,
910                         TIME_FORCE24HOURFORMAT | TIME_NOTIMEMARKER,
911                         &systemtime, NULL, bufstr,
912                         sizeof(bufstr)/sizeof(bufstr[0]) ))
913         msi_set_property( package->db, szTime, bufstr );
914     else
915         ERR("Couldn't set Time property: GetTimeFormat failed with error %d\n", GetLastError());
916
917     set_msi_assembly_prop( package );
918
919     langid = GetUserDefaultLangID();
920     sprintfW(bufstr, szIntFormat, langid);
921     msi_set_property( package->db, szUserLangID, bufstr );
922
923     langid = GetSystemDefaultLangID();
924     sprintfW(bufstr, szIntFormat, langid);
925     msi_set_property( package->db, szSystemLangID, bufstr );
926
927     sprintfW(bufstr, szIntFormat, MsiQueryProductStateW(package->ProductCode));
928     msi_set_property( package->db, szProductState, bufstr );
929
930     len = 0;
931     if (!GetUserNameW( NULL, &len ) && GetLastError() == ERROR_MORE_DATA)
932     {
933         WCHAR *username;
934         if ((username = msi_alloc( len * sizeof(WCHAR) )))
935         {
936             if (GetUserNameW( username, &len ))
937                 msi_set_property( package->db, szLogonUser, username );
938             msi_free( username );
939         }
940     }
941 }
942
943 static UINT msi_load_summary_properties( MSIPACKAGE *package )
944 {
945     UINT rc;
946     MSIHANDLE suminfo;
947     MSIHANDLE hdb = alloc_msihandle( &package->db->hdr );
948     INT count;
949     DWORD len;
950     LPWSTR package_code;
951     static const WCHAR szPackageCode[] = {
952         'P','a','c','k','a','g','e','C','o','d','e',0};
953
954     if (!hdb) {
955         ERR("Unable to allocate handle\n");
956         return ERROR_OUTOFMEMORY;
957     }
958
959     rc = MsiGetSummaryInformationW( hdb, NULL, 0, &suminfo );
960     MsiCloseHandle(hdb);
961     if (rc != ERROR_SUCCESS)
962     {
963         ERR("Unable to open Summary Information\n");
964         return rc;
965     }
966
967     rc = MsiSummaryInfoGetPropertyW( suminfo, PID_PAGECOUNT, NULL,
968                                      &count, NULL, NULL, NULL );
969     if (rc != ERROR_SUCCESS)
970     {
971         WARN("Unable to query page count: %d\n", rc);
972         goto done;
973     }
974
975     /* load package code property */
976     len = 0;
977     rc = MsiSummaryInfoGetPropertyW( suminfo, PID_REVNUMBER, NULL,
978                                      NULL, NULL, NULL, &len );
979     if (rc != ERROR_MORE_DATA)
980     {
981         WARN("Unable to query revision number: %d\n", rc);
982         rc = ERROR_FUNCTION_FAILED;
983         goto done;
984     }
985
986     len++;
987     package_code = msi_alloc( len * sizeof(WCHAR) );
988     rc = MsiSummaryInfoGetPropertyW( suminfo, PID_REVNUMBER, NULL,
989                                      NULL, NULL, package_code, &len );
990     if (rc != ERROR_SUCCESS)
991     {
992         WARN("Unable to query rev number: %d\n", rc);
993         goto done;
994     }
995
996     msi_set_property( package->db, szPackageCode, package_code );
997     msi_free( package_code );
998
999     /* load package attributes */
1000     count = 0;
1001     MsiSummaryInfoGetPropertyW( suminfo, PID_WORDCOUNT, NULL,
1002                                 &count, NULL, NULL, NULL );
1003     package->WordCount = count;
1004
1005 done:
1006     MsiCloseHandle(suminfo);
1007     return rc;
1008 }
1009
1010 static MSIPACKAGE *msi_alloc_package( void )
1011 {
1012     MSIPACKAGE *package;
1013
1014     package = alloc_msiobject( MSIHANDLETYPE_PACKAGE, sizeof (MSIPACKAGE),
1015                                MSI_FreePackage );
1016     if( package )
1017     {
1018         list_init( &package->components );
1019         list_init( &package->features );
1020         list_init( &package->files );
1021         list_init( &package->tempfiles );
1022         list_init( &package->folders );
1023         list_init( &package->subscriptions );
1024         list_init( &package->appids );
1025         list_init( &package->classes );
1026         list_init( &package->mimes );
1027         list_init( &package->extensions );
1028         list_init( &package->progids );
1029         list_init( &package->RunningActions );
1030         list_init( &package->sourcelist_info );
1031         list_init( &package->sourcelist_media );
1032         list_init( &package->patches );
1033     }
1034
1035     return package;
1036 }
1037
1038 static UINT msi_load_admin_properties(MSIPACKAGE *package)
1039 {
1040     BYTE *data;
1041     UINT r, sz;
1042
1043     static const WCHAR stmname[] = {'A','d','m','i','n','P','r','o','p','e','r','t','i','e','s',0};
1044
1045     r = read_stream_data(package->db->storage, stmname, FALSE, &data, &sz);
1046     if (r != ERROR_SUCCESS)
1047         return r;
1048
1049     r = msi_parse_command_line(package, (WCHAR *)data, TRUE);
1050
1051     msi_free(data);
1052     return r;
1053 }
1054
1055 void msi_adjust_privilege_properties( MSIPACKAGE *package )
1056 {
1057     /* FIXME: this should depend on the user's privileges */
1058     if (msi_get_property_int( package->db, szAllUsers, 0 ) == 2)
1059     {
1060         TRACE("resetting ALLUSERS property from 2 to 1\n");
1061         msi_set_property( package->db, szAllUsers, szOne );
1062     }
1063     msi_set_property( package->db, szAdminUser, szOne );
1064 }
1065
1066 MSIPACKAGE *MSI_CreatePackage( MSIDATABASE *db, LPCWSTR base_url )
1067 {
1068     static const WCHAR szLevel[] = { 'U','I','L','e','v','e','l',0 };
1069     static const WCHAR szpi[] = {'%','i',0};
1070     MSIPACKAGE *package;
1071     WCHAR uilevel[10];
1072     UINT r;
1073
1074     TRACE("%p\n", db);
1075
1076     package = msi_alloc_package();
1077     if (package)
1078     {
1079         msiobj_addref( &db->hdr );
1080         package->db = db;
1081
1082         package->WordCount = 0;
1083         package->PackagePath = strdupW( db->path );
1084         package->BaseURL = strdupW( base_url );
1085
1086         create_temp_property_table( package );
1087         msi_clone_properties( package );
1088         msi_adjust_privilege_properties( package );
1089
1090         package->ProductCode = msi_dup_property( package->db, szProductCode );
1091         package->script = msi_alloc_zero( sizeof(MSISCRIPT) );
1092
1093         set_installed_prop( package );
1094         set_installer_properties( package );
1095
1096         sprintfW(uilevel,szpi,gUILevel);
1097         msi_set_property(package->db, szLevel, uilevel);
1098
1099         r = msi_load_summary_properties( package );
1100         if (r != ERROR_SUCCESS)
1101         {
1102             msiobj_release( &package->hdr );
1103             return NULL;
1104         }
1105
1106         if (package->WordCount & msidbSumInfoSourceTypeAdminImage)
1107             msi_load_admin_properties( package );
1108     }
1109
1110     return package;
1111 }
1112
1113 /*
1114  * copy_package_to_temp   [internal]
1115  *
1116  * copy the msi file to a temp file to prevent locking a CD
1117  * with a multi disc install 
1118  *
1119  * FIXME: I think this is wrong, and instead of copying the package,
1120  *        we should read all the tables to memory, then open the
1121  *        database to read binary streams on demand.
1122  */ 
1123 static UINT copy_package_to_temp( LPCWSTR szPackage, LPWSTR filename )
1124 {
1125     WCHAR path[MAX_PATH];
1126
1127     GetTempPathW( MAX_PATH, path );
1128     GetTempFileNameW( path, szMsi, 0, filename );
1129
1130     if( !CopyFileW( szPackage, filename, FALSE ) )
1131     {
1132         UINT error = GetLastError();
1133         if ( error == ERROR_FILE_NOT_FOUND )
1134             ERR("can't find %s\n", debugstr_w(szPackage));
1135         else
1136             ERR("failed to copy package %s to %s (%u)\n", debugstr_w(szPackage), debugstr_w(filename), error);
1137         DeleteFileW( filename );
1138         return error;
1139     }
1140
1141     return ERROR_SUCCESS;
1142 }
1143
1144 UINT msi_download_file( LPCWSTR szUrl, LPWSTR filename )
1145 {
1146     LPINTERNET_CACHE_ENTRY_INFOW cache_entry;
1147     DWORD size = 0;
1148     HRESULT hr;
1149
1150     /* call will always fail, becase size is 0,
1151      * but will return ERROR_FILE_NOT_FOUND first
1152      * if the file doesn't exist
1153      */
1154     GetUrlCacheEntryInfoW( szUrl, NULL, &size );
1155     if ( GetLastError() != ERROR_FILE_NOT_FOUND )
1156     {
1157         cache_entry = msi_alloc( size );
1158         if ( !GetUrlCacheEntryInfoW( szUrl, cache_entry, &size ) )
1159         {
1160             UINT error = GetLastError();
1161             msi_free( cache_entry );
1162             return error;
1163         }
1164
1165         lstrcpyW( filename, cache_entry->lpszLocalFileName );
1166         msi_free( cache_entry );
1167         return ERROR_SUCCESS;
1168     }
1169
1170     hr = URLDownloadToCacheFileW( NULL, szUrl, filename, MAX_PATH, 0, NULL );
1171     if ( FAILED(hr) )
1172     {
1173         WARN("failed to download %s to cache file\n", debugstr_w(szUrl));
1174         return ERROR_FUNCTION_FAILED;
1175     }
1176
1177     return ERROR_SUCCESS;
1178 }
1179
1180 UINT msi_get_local_package_name( LPWSTR path, LPCWSTR suffix )
1181 {
1182     static const WCHAR szInstaller[] = {
1183         '\\','I','n','s','t','a','l','l','e','r','\\',0};
1184     static const WCHAR fmt[] = {'%','x',0};
1185     DWORD time, len, i, offset;
1186     HANDLE handle;
1187
1188     time = GetTickCount();
1189     GetWindowsDirectoryW( path, MAX_PATH );
1190     strcatW( path, szInstaller );
1191     CreateDirectoryW( path, NULL );
1192
1193     len = strlenW(path);
1194     for (i = 0; i < 0x10000; i++)
1195     {
1196         offset = snprintfW( path + len, MAX_PATH - len, fmt, (time + i) & 0xffff );
1197         memcpy( path + len + offset, suffix, (strlenW( suffix ) + 1) * sizeof(WCHAR) );
1198         handle = CreateFileW( path, GENERIC_WRITE, 0, NULL,
1199                               CREATE_NEW, FILE_ATTRIBUTE_NORMAL, 0 );
1200         if (handle != INVALID_HANDLE_VALUE)
1201         {
1202             CloseHandle(handle);
1203             break;
1204         }
1205         if (GetLastError() != ERROR_FILE_EXISTS &&
1206             GetLastError() != ERROR_SHARING_VIOLATION)
1207             return ERROR_FUNCTION_FAILED;
1208     }
1209
1210     return ERROR_SUCCESS;
1211 }
1212
1213 static UINT apply_registered_patch( MSIPACKAGE *package, LPCWSTR patch_code )
1214 {
1215     UINT r;
1216     DWORD len;
1217     WCHAR patch_file[MAX_PATH];
1218     MSIDATABASE *patch_db;
1219     MSIPATCHINFO *patch_info;
1220     MSISUMMARYINFO *si;
1221
1222     len = sizeof(patch_file) / sizeof(WCHAR);
1223     r = MsiGetPatchInfoExW( patch_code, package->ProductCode, NULL, package->Context,
1224                             INSTALLPROPERTY_LOCALPACKAGEW, patch_file, &len );
1225     if (r != ERROR_SUCCESS)
1226     {
1227         ERR("failed to get patch filename %u\n", r);
1228         return r;
1229     }
1230
1231     r = MSI_OpenDatabaseW( patch_file, MSIDBOPEN_READONLY + MSIDBOPEN_PATCHFILE, &patch_db );
1232     if (r != ERROR_SUCCESS)
1233     {
1234         ERR("failed to open patch database %s\n", debugstr_w( patch_file ));
1235         return r;
1236     }
1237
1238     si = MSI_GetSummaryInformationW( patch_db->storage, 0 );
1239     if (!si)
1240     {
1241         msiobj_release( &patch_db->hdr );
1242         return ERROR_FUNCTION_FAILED;
1243     }
1244
1245     r = msi_parse_patch_summary( si, &patch_info );
1246     msiobj_release( &si->hdr );
1247     if (r != ERROR_SUCCESS)
1248     {
1249         ERR("failed to parse patch summary %u\n", r);
1250         msiobj_release( &patch_db->hdr );
1251         return r;
1252     }
1253
1254     patch_info->localfile = strdupW( patch_file );
1255     if (!patch_info->localfile)
1256     {
1257         msiobj_release( &patch_db->hdr );
1258         return ERROR_OUTOFMEMORY;
1259     }
1260
1261     r = msi_apply_patch_db( package, patch_db, patch_info );
1262     msiobj_release( &patch_db->hdr );
1263     if (r != ERROR_SUCCESS)
1264     {
1265         ERR("failed to apply patch %u\n", r);
1266         msi_free( patch_info->patchcode );
1267         msi_free( patch_info->transforms );
1268         msi_free( patch_info->localfile );
1269         msi_free( patch_info );
1270     }
1271     return r;
1272 }
1273
1274 static UINT msi_parse_summary( MSISUMMARYINFO *si, MSIPACKAGE *package )
1275 {
1276     WCHAR *template, *p, *q;
1277     DWORD i, count;
1278
1279     template = msi_suminfo_dup_string( si, PID_TEMPLATE );
1280     if (!template)
1281         return ERROR_SUCCESS; /* native accepts missing template property */
1282
1283     TRACE("template: %s\n", debugstr_w(template));
1284
1285     p = strchrW( template, ';' );
1286     if (!p)
1287     {
1288         WARN("invalid template string %s\n", debugstr_w(template));
1289         msi_free( template );
1290         return ERROR_PATCH_PACKAGE_INVALID;
1291     }
1292     *p = 0;
1293     if (!template[0] || !strcmpW( template, szIntel ))
1294         package->platform = PLATFORM_INTEL;
1295     else if (!strcmpW( template, szIntel64 ))
1296         package->platform = PLATFORM_INTEL64;
1297     else if (!strcmpW( template, szX64 ))
1298         package->platform = PLATFORM_X64;
1299     else
1300     {
1301         WARN("unknown platform %s\n", debugstr_w(template));
1302         msi_free( template );
1303         return ERROR_PATCH_PACKAGE_INVALID;
1304     }
1305
1306     count = 1;
1307     for (q = ++p; (q = strchrW( q, ',' )); q++) count++;
1308
1309     package->langids = msi_alloc( count * sizeof(LANGID) );
1310     if (!package->langids)
1311     {
1312         msi_free( template );
1313         return ERROR_OUTOFMEMORY;
1314     }
1315
1316     i = 0;
1317     while (*p)
1318     {
1319         q = strchrW( p, ',' );
1320         if (q) *q = 0;
1321         package->langids[i] = atoiW( p );
1322         if (!q) break;
1323         p = q + 1;
1324         i++;
1325     }
1326     package->num_langids = i + 1;
1327
1328     msi_free( template );
1329     return ERROR_SUCCESS;
1330 }
1331
1332 UINT MSI_OpenPackageW(LPCWSTR szPackage, MSIPACKAGE **pPackage)
1333 {
1334     static const WCHAR Database[] = {'D','A','T','A','B','A','S','E',0};
1335     static const WCHAR dotmsi[] = {'.','m','s','i',0};
1336     MSIDATABASE *db = NULL;
1337     MSIPACKAGE *package;
1338     MSIHANDLE handle;
1339     LPWSTR ptr, base_url = NULL;
1340     UINT r;
1341     WCHAR temppath[MAX_PATH], localfile[MAX_PATH], cachefile[MAX_PATH];
1342     LPCWSTR file = szPackage;
1343     DWORD index = 0;
1344     MSISUMMARYINFO *si;
1345
1346     TRACE("%s %p\n", debugstr_w(szPackage), pPackage);
1347
1348     if( szPackage[0] == '#' )
1349     {
1350         handle = atoiW(&szPackage[1]);
1351         db = msihandle2msiinfo( handle, MSIHANDLETYPE_DATABASE );
1352         if( !db )
1353         {
1354             IWineMsiRemoteDatabase *remote_database;
1355
1356             remote_database = (IWineMsiRemoteDatabase *)msi_get_remote( handle );
1357             if ( !remote_database )
1358                 return ERROR_INVALID_HANDLE;
1359
1360             IWineMsiRemoteDatabase_Release( remote_database );
1361             WARN("MsiOpenPackage not allowed during a custom action!\n");
1362
1363             return ERROR_FUNCTION_FAILED;
1364         }
1365     }
1366     else
1367     {
1368         if ( UrlIsW( szPackage, URLIS_URL ) )
1369         {
1370             r = msi_download_file( szPackage, cachefile );
1371             if ( r != ERROR_SUCCESS )
1372                 return r;
1373
1374             r = copy_package_to_temp( cachefile, temppath );
1375             if ( r != ERROR_SUCCESS )
1376                 return r;
1377
1378             file = temppath;
1379
1380             base_url = strdupW( szPackage );
1381             if ( !base_url )
1382                 return ERROR_OUTOFMEMORY;
1383
1384             ptr = strrchrW( base_url, '/' );
1385             if (ptr) *(ptr + 1) = '\0';
1386         }
1387         else
1388         {
1389             r = copy_package_to_temp( szPackage, temppath );
1390             if ( r != ERROR_SUCCESS )
1391                 return r;
1392
1393             file = temppath;
1394         }
1395
1396         r = msi_get_local_package_name( localfile, dotmsi );
1397         if (r != ERROR_SUCCESS)
1398             return r;
1399
1400         TRACE("Copying to local package %s\n", debugstr_w(localfile));
1401
1402         if (!CopyFileW( file, localfile, FALSE ))
1403         {
1404             ERR("Unable to copy package (%s -> %s) (error %u)\n",
1405                 debugstr_w(file), debugstr_w(localfile), GetLastError());
1406             return GetLastError();
1407         }
1408
1409         TRACE("Opening relocated package %s\n", debugstr_w( file ));
1410
1411         /* transforms that add binary streams require that we open the database
1412          * read/write, which is safe because we always create a copy that is thrown
1413          * away when we're done.
1414          */
1415         r = MSI_OpenDatabaseW( file, MSIDBOPEN_TRANSACT, &db );
1416         if( r != ERROR_SUCCESS )
1417         {
1418             if (file != szPackage)
1419                 DeleteFileW( file );
1420
1421             if (GetFileAttributesW(szPackage) == INVALID_FILE_ATTRIBUTES)
1422                 return ERROR_FILE_NOT_FOUND;
1423
1424             return r;
1425         }
1426
1427         db->localfile = strdupW( localfile );
1428     }
1429
1430     package = MSI_CreatePackage( db, base_url );
1431     msi_free( base_url );
1432     msiobj_release( &db->hdr );
1433     if( !package )
1434     {
1435         if (file != szPackage)
1436             DeleteFileW( file );
1437
1438         return ERROR_INSTALL_PACKAGE_INVALID;
1439     }
1440
1441     if( file != szPackage )
1442         track_tempfile( package, file );
1443
1444     si = MSI_GetSummaryInformationW( db->storage, 0 );
1445     if (!si)
1446     {
1447         WARN("failed to load summary info %u\n", r);
1448         msiobj_release( &package->hdr );
1449         return ERROR_INSTALL_PACKAGE_INVALID;
1450     }
1451
1452     r = msi_parse_summary( si, package );
1453     msiobj_release( &si->hdr );
1454     if (r != ERROR_SUCCESS)
1455     {
1456         WARN("failed to parse summary info %u\n", r);
1457         msiobj_release( &package->hdr );
1458         return r;
1459     }
1460
1461     msi_set_property( package->db, Database, db->path );
1462
1463     if( UrlIsW( szPackage, URLIS_URL ) )
1464         msi_set_property( package->db, szOriginalDatabase, szPackage );
1465     else if( szPackage[0] == '#' )
1466         msi_set_property( package->db, szOriginalDatabase, db->path );
1467     else
1468     {
1469         WCHAR fullpath[MAX_PATH];
1470
1471         GetFullPathNameW( szPackage, MAX_PATH, fullpath, NULL );
1472         msi_set_property( package->db, szOriginalDatabase, fullpath );
1473     }
1474
1475     msi_set_context( package );
1476
1477     while (1)
1478     {
1479         WCHAR patch_code[GUID_SIZE];
1480         r = MsiEnumPatchesExW( package->ProductCode, NULL, package->Context,
1481                                MSIPATCHSTATE_APPLIED, index, patch_code, NULL, NULL, NULL, NULL );
1482         if (r != ERROR_SUCCESS)
1483             break;
1484
1485         TRACE("found registered patch %s\n", debugstr_w(patch_code));
1486
1487         r = apply_registered_patch( package, patch_code );
1488         if (r != ERROR_SUCCESS)
1489         {
1490             ERR("registered patch failed to apply %u\n", r);
1491             msiobj_release( &package->hdr );
1492             return r;
1493         }
1494
1495         index++;
1496     }
1497
1498     if (index)
1499     {
1500         msi_clone_properties( package );
1501         msi_adjust_privilege_properties( package );
1502     }
1503
1504     *pPackage = package;
1505     return ERROR_SUCCESS;
1506 }
1507
1508 UINT WINAPI MsiOpenPackageExW(LPCWSTR szPackage, DWORD dwOptions, MSIHANDLE *phPackage)
1509 {
1510     MSIPACKAGE *package = NULL;
1511     UINT ret;
1512
1513     TRACE("%s %08x %p\n", debugstr_w(szPackage), dwOptions, phPackage );
1514
1515     if( !szPackage || !phPackage )
1516         return ERROR_INVALID_PARAMETER;
1517
1518     if ( !*szPackage )
1519     {
1520         FIXME("Should create an empty database and package\n");
1521         return ERROR_FUNCTION_FAILED;
1522     }
1523
1524     if( dwOptions )
1525         FIXME("dwOptions %08x not supported\n", dwOptions);
1526
1527     ret = MSI_OpenPackageW( szPackage, &package );
1528     if( ret == ERROR_SUCCESS )
1529     {
1530         *phPackage = alloc_msihandle( &package->hdr );
1531         if (! *phPackage)
1532             ret = ERROR_NOT_ENOUGH_MEMORY;
1533         msiobj_release( &package->hdr );
1534     }
1535
1536     return ret;
1537 }
1538
1539 UINT WINAPI MsiOpenPackageW(LPCWSTR szPackage, MSIHANDLE *phPackage)
1540 {
1541     return MsiOpenPackageExW( szPackage, 0, phPackage );
1542 }
1543
1544 UINT WINAPI MsiOpenPackageExA(LPCSTR szPackage, DWORD dwOptions, MSIHANDLE *phPackage)
1545 {
1546     LPWSTR szwPack = NULL;
1547     UINT ret;
1548
1549     if( szPackage )
1550     {
1551         szwPack = strdupAtoW( szPackage );
1552         if( !szwPack )
1553             return ERROR_OUTOFMEMORY;
1554     }
1555
1556     ret = MsiOpenPackageExW( szwPack, dwOptions, phPackage );
1557
1558     msi_free( szwPack );
1559
1560     return ret;
1561 }
1562
1563 UINT WINAPI MsiOpenPackageA(LPCSTR szPackage, MSIHANDLE *phPackage)
1564 {
1565     return MsiOpenPackageExA( szPackage, 0, phPackage );
1566 }
1567
1568 MSIHANDLE WINAPI MsiGetActiveDatabase(MSIHANDLE hInstall)
1569 {
1570     MSIPACKAGE *package;
1571     MSIHANDLE handle = 0;
1572     IUnknown *remote_unk;
1573     IWineMsiRemotePackage *remote_package;
1574
1575     TRACE("(%d)\n",hInstall);
1576
1577     package = msihandle2msiinfo( hInstall, MSIHANDLETYPE_PACKAGE);
1578     if( package)
1579     {
1580         handle = alloc_msihandle( &package->db->hdr );
1581         msiobj_release( &package->hdr );
1582     }
1583     else if ((remote_unk = msi_get_remote(hInstall)))
1584     {
1585         if (IUnknown_QueryInterface(remote_unk, &IID_IWineMsiRemotePackage,
1586                                         (LPVOID *)&remote_package) == S_OK)
1587         {
1588             IWineMsiRemotePackage_GetActiveDatabase(remote_package, &handle);
1589             IWineMsiRemotePackage_Release(remote_package);
1590         }
1591         else
1592         {
1593             WARN("remote handle %d is not a package\n", hInstall);
1594         }
1595         IUnknown_Release(remote_unk);
1596     }
1597
1598     return handle;
1599 }
1600
1601 INT MSI_ProcessMessage( MSIPACKAGE *package, INSTALLMESSAGE eMessageType,
1602                                MSIRECORD *record)
1603 {
1604     static const WCHAR szActionData[] =
1605         {'A','c','t','i','o','n','D','a','t','a',0};
1606     static const WCHAR szSetProgress[] =
1607         {'S','e','t','P','r','o','g','r','e','s','s',0};
1608     static const WCHAR szActionText[] =
1609         {'A','c','t','i','o','n','T','e','x','t',0};
1610     DWORD log_type = 0;
1611     LPWSTR message;
1612     DWORD sz;
1613     DWORD total_size = 0;
1614     INT i;
1615     INT rc;
1616     char *msg;
1617     int len;
1618
1619     TRACE("%x\n", eMessageType);
1620     rc = 0;
1621
1622     if ((eMessageType & 0xff000000) == INSTALLMESSAGE_ERROR)
1623         log_type |= INSTALLLOGMODE_ERROR;
1624     if ((eMessageType & 0xff000000) == INSTALLMESSAGE_WARNING)
1625         log_type |= INSTALLLOGMODE_WARNING;
1626     if ((eMessageType & 0xff000000) == INSTALLMESSAGE_USER)
1627         log_type |= INSTALLLOGMODE_USER;
1628     if ((eMessageType & 0xff000000) == INSTALLMESSAGE_INFO)
1629         log_type |= INSTALLLOGMODE_INFO;
1630     if ((eMessageType & 0xff000000) == INSTALLMESSAGE_COMMONDATA)
1631         log_type |= INSTALLLOGMODE_COMMONDATA;
1632     if ((eMessageType & 0xff000000) == INSTALLMESSAGE_ACTIONSTART)
1633         log_type |= INSTALLLOGMODE_ACTIONSTART;
1634     if ((eMessageType & 0xff000000) == INSTALLMESSAGE_ACTIONDATA)
1635         log_type |= INSTALLLOGMODE_ACTIONDATA;
1636     /* just a guess */
1637     if ((eMessageType & 0xff000000) == INSTALLMESSAGE_PROGRESS)
1638         log_type |= 0x800;
1639
1640     if ((eMessageType & 0xff000000) == INSTALLMESSAGE_ACTIONSTART)
1641     {
1642         static const WCHAR template_s[]=
1643             {'A','c','t','i','o','n',' ','%','s',':',' ','%','s','.',' ',0};
1644         static const WCHAR format[] = 
1645             {'H','H','\'',':','\'','m','m','\'',':','\'','s','s',0};
1646         WCHAR timet[0x100];
1647         LPCWSTR action_text, action;
1648         LPWSTR deformatted = NULL;
1649
1650         GetTimeFormatW(LOCALE_USER_DEFAULT, 0, NULL, format, timet, 0x100);
1651
1652         action = MSI_RecordGetString(record, 1);
1653         action_text = MSI_RecordGetString(record, 2);
1654
1655         if (!action || !action_text)
1656             return IDOK;
1657
1658         deformat_string(package, action_text, &deformatted);
1659
1660         len = strlenW(timet) + strlenW(action) + strlenW(template_s);
1661         if (deformatted)
1662             len += strlenW(deformatted);
1663         message = msi_alloc(len*sizeof(WCHAR));
1664         sprintfW(message, template_s, timet, action);
1665         if (deformatted)
1666             strcatW(message, deformatted);
1667         msi_free(deformatted);
1668     }
1669     else
1670     {
1671         INT msg_field=1;
1672         message = msi_alloc(1*sizeof (WCHAR));
1673         message[0]=0;
1674         msg_field = MSI_RecordGetFieldCount(record);
1675         for (i = 1; i <= msg_field; i++)
1676         {
1677             LPWSTR tmp;
1678             WCHAR number[3];
1679             static const WCHAR format[] = { '%','i',':',' ',0};
1680             sz = 0;
1681             MSI_RecordGetStringW(record,i,NULL,&sz);
1682             sz+=4;
1683             total_size+=sz*sizeof(WCHAR);
1684             tmp = msi_alloc(sz*sizeof(WCHAR));
1685             message = msi_realloc(message,total_size*sizeof (WCHAR));
1686
1687             MSI_RecordGetStringW(record,i,tmp,&sz);
1688
1689             if (msg_field > 1)
1690             {
1691                 sprintfW(number,format,i);
1692                 strcatW(message,number);
1693             }
1694             strcatW(message,tmp);
1695             if (msg_field > 1)
1696                 strcatW(message, szSpace);
1697
1698             msi_free(tmp);
1699         }
1700     }
1701
1702     TRACE("%p %p %p %x %x %s\n", gUIHandlerA, gUIHandlerW, gUIHandlerRecord,
1703           gUIFilter, log_type, debugstr_w(message));
1704
1705     /* convert it to ASCII */
1706     len = WideCharToMultiByte( CP_ACP, 0, message, -1, NULL, 0, NULL, NULL );
1707     msg = msi_alloc( len );
1708     WideCharToMultiByte( CP_ACP, 0, message, -1, msg, len, NULL, NULL );
1709
1710     if (gUIHandlerW && (gUIFilter & log_type))
1711     {
1712         rc = gUIHandlerW( gUIContext, eMessageType, message );
1713     }
1714     else if (gUIHandlerA && (gUIFilter & log_type))
1715     {
1716         rc = gUIHandlerA( gUIContext, eMessageType, msg );
1717     }
1718     else if (gUIHandlerRecord && (gUIFilter & log_type))
1719     {
1720         MSIHANDLE rec = MsiCreateRecord( 1 );
1721         MsiRecordSetStringW( rec, 0, message );
1722         rc = gUIHandlerRecord( gUIContext, eMessageType, rec );
1723         MsiCloseHandle( rec );
1724     }
1725
1726     if ((!rc) && (gszLogFile[0]) && !((eMessageType & 0xff000000) ==
1727                                       INSTALLMESSAGE_PROGRESS))
1728     {
1729         DWORD write;
1730         HANDLE log_file = CreateFileW(gszLogFile,GENERIC_WRITE, 0, NULL,
1731                                   OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
1732
1733         if (log_file != INVALID_HANDLE_VALUE)
1734         {
1735             SetFilePointer(log_file,0, NULL, FILE_END);
1736             WriteFile(log_file,msg,strlen(msg),&write,NULL);
1737             WriteFile(log_file,"\n",1,&write,NULL);
1738             CloseHandle(log_file);
1739         }
1740     }
1741     msi_free( msg );
1742     msi_free( message );
1743
1744     switch (eMessageType & 0xff000000)
1745     {
1746     case INSTALLMESSAGE_ACTIONDATA:
1747         /* FIXME: format record here instead of in ui_actiondata to get the
1748          * correct action data for external scripts */
1749         ControlEvent_FireSubscribedEvent(package, szActionData, record);
1750         break;
1751     case INSTALLMESSAGE_ACTIONSTART:
1752     {
1753         MSIRECORD *uirow;
1754         LPWSTR deformated;
1755         LPCWSTR action_text = MSI_RecordGetString(record, 2);
1756
1757         deformat_string(package, action_text, &deformated);
1758         uirow = MSI_CreateRecord(1);
1759         MSI_RecordSetStringW(uirow, 1, deformated);
1760         TRACE("INSTALLMESSAGE_ACTIONSTART: %s\n", debugstr_w(deformated));
1761         msi_free(deformated);
1762
1763         ControlEvent_FireSubscribedEvent(package, szActionText, uirow);
1764
1765         msiobj_release(&uirow->hdr);
1766         break;
1767     }
1768     case INSTALLMESSAGE_PROGRESS:
1769         ControlEvent_FireSubscribedEvent(package, szSetProgress, record);
1770         break;
1771     }
1772
1773     return ERROR_SUCCESS;
1774 }
1775
1776 INT WINAPI MsiProcessMessage( MSIHANDLE hInstall, INSTALLMESSAGE eMessageType,
1777                               MSIHANDLE hRecord)
1778 {
1779     UINT ret = ERROR_INVALID_HANDLE;
1780     MSIPACKAGE *package = NULL;
1781     MSIRECORD *record = NULL;
1782
1783     package = msihandle2msiinfo( hInstall, MSIHANDLETYPE_PACKAGE );
1784     if( !package )
1785     {
1786         HRESULT hr;
1787         IWineMsiRemotePackage *remote_package;
1788
1789         remote_package = (IWineMsiRemotePackage *)msi_get_remote( hInstall );
1790         if (!remote_package)
1791             return ERROR_INVALID_HANDLE;
1792
1793         hr = IWineMsiRemotePackage_ProcessMessage( remote_package, eMessageType, hRecord );
1794
1795         IWineMsiRemotePackage_Release( remote_package );
1796
1797         if (FAILED(hr))
1798         {
1799             if (HRESULT_FACILITY(hr) == FACILITY_WIN32)
1800                 return HRESULT_CODE(hr);
1801
1802             return ERROR_FUNCTION_FAILED;
1803         }
1804
1805         return ERROR_SUCCESS;
1806     }
1807
1808     record = msihandle2msiinfo( hRecord, MSIHANDLETYPE_RECORD );
1809     if( !record )
1810         goto out;
1811
1812     ret = MSI_ProcessMessage( package, eMessageType, record );
1813
1814 out:
1815     msiobj_release( &package->hdr );
1816     if( record )
1817         msiobj_release( &record->hdr );
1818
1819     return ret;
1820 }
1821
1822 /* property code */
1823
1824 UINT WINAPI MsiSetPropertyA( MSIHANDLE hInstall, LPCSTR szName, LPCSTR szValue )
1825 {
1826     LPWSTR szwName = NULL, szwValue = NULL;
1827     UINT r = ERROR_OUTOFMEMORY;
1828
1829     szwName = strdupAtoW( szName );
1830     if( szName && !szwName )
1831         goto end;
1832
1833     szwValue = strdupAtoW( szValue );
1834     if( szValue && !szwValue )
1835         goto end;
1836
1837     r = MsiSetPropertyW( hInstall, szwName, szwValue);
1838
1839 end:
1840     msi_free( szwName );
1841     msi_free( szwValue );
1842
1843     return r;
1844 }
1845
1846 void msi_reset_folders( MSIPACKAGE *package, BOOL source )
1847 {
1848     MSIFOLDER *folder;
1849
1850     LIST_FOR_EACH_ENTRY( folder, &package->folders, MSIFOLDER, entry )
1851     {
1852         if ( source )
1853         {
1854             msi_free( folder->ResolvedSource );
1855             folder->ResolvedSource = NULL;
1856         }
1857         else
1858         {
1859             msi_free( folder->ResolvedTarget );
1860             folder->ResolvedTarget = NULL;
1861         }
1862     }
1863 }
1864
1865 UINT msi_set_property( MSIDATABASE *db, LPCWSTR szName, LPCWSTR szValue )
1866 {
1867     MSIQUERY *view;
1868     MSIRECORD *row = NULL;
1869     UINT rc;
1870     DWORD sz = 0;
1871     WCHAR Query[1024];
1872
1873     static const WCHAR Insert[] = {
1874         'I','N','S','E','R','T',' ','i','n','t','o',' ',
1875         '`','_','P','r','o','p','e','r','t','y','`',' ','(',
1876         '`','_','P','r','o','p','e','r','t','y','`',',',
1877         '`','V','a','l','u','e','`',')',' ','V','A','L','U','E','S'
1878         ,' ','(','?',',','?',')',0};
1879     static const WCHAR Update[] = {
1880         'U','P','D','A','T','E',' ','`','_','P','r','o','p','e','r','t','y','`',
1881         ' ','s','e','t',' ','`','V','a','l','u','e','`',' ','=',' ','?',' ',
1882         'w','h','e','r','e',' ','`','_','P','r','o','p','e','r','t','y','`',
1883         ' ','=',' ','\'','%','s','\'',0};
1884     static const WCHAR Delete[] = {
1885         'D','E','L','E','T','E',' ','F','R','O','M',' ',
1886         '`','_','P','r','o','p','e','r','t','y','`',' ','W','H','E','R','E',' ',
1887         '`','_','P','r','o','p','e','r','t','y','`',' ','=',' ','\'','%','s','\'',0};
1888
1889     TRACE("%p %s %s\n", db, debugstr_w(szName), debugstr_w(szValue));
1890
1891     if (!szName)
1892         return ERROR_INVALID_PARAMETER;
1893
1894     /* this one is weird... */
1895     if (!szName[0])
1896         return szValue ? ERROR_FUNCTION_FAILED : ERROR_SUCCESS;
1897
1898     rc = msi_get_property(db, szName, 0, &sz);
1899     if (!szValue || !*szValue)
1900     {
1901         sprintfW(Query, Delete, szName);
1902     }
1903     else if (rc == ERROR_MORE_DATA || rc == ERROR_SUCCESS)
1904     {
1905         sprintfW(Query, Update, szName);
1906
1907         row = MSI_CreateRecord(1);
1908         MSI_RecordSetStringW(row, 1, szValue);
1909     }
1910     else
1911     {
1912         strcpyW(Query, Insert);
1913
1914         row = MSI_CreateRecord(2);
1915         MSI_RecordSetStringW(row, 1, szName);
1916         MSI_RecordSetStringW(row, 2, szValue);
1917     }
1918
1919     rc = MSI_DatabaseOpenViewW(db, Query, &view);
1920     if (rc == ERROR_SUCCESS)
1921     {
1922         rc = MSI_ViewExecute(view, row);
1923         MSI_ViewClose(view);
1924         msiobj_release(&view->hdr);
1925     }
1926
1927     if (row)
1928       msiobj_release(&row->hdr);
1929
1930     return rc;
1931 }
1932
1933 UINT WINAPI MsiSetPropertyW( MSIHANDLE hInstall, LPCWSTR szName, LPCWSTR szValue)
1934 {
1935     MSIPACKAGE *package;
1936     UINT ret;
1937
1938     package = msihandle2msiinfo( hInstall, MSIHANDLETYPE_PACKAGE);
1939     if( !package )
1940     {
1941         HRESULT hr;
1942         BSTR name = NULL, value = NULL;
1943         IWineMsiRemotePackage *remote_package;
1944
1945         remote_package = (IWineMsiRemotePackage *)msi_get_remote( hInstall );
1946         if (!remote_package)
1947             return ERROR_INVALID_HANDLE;
1948
1949         name = SysAllocString( szName );
1950         value = SysAllocString( szValue );
1951         if ((!name && szName) || (!value && szValue))
1952         {
1953             SysFreeString( name );
1954             SysFreeString( value );
1955             IWineMsiRemotePackage_Release( remote_package );
1956             return ERROR_OUTOFMEMORY;
1957         }
1958
1959         hr = IWineMsiRemotePackage_SetProperty( remote_package, name, value );
1960
1961         SysFreeString( name );
1962         SysFreeString( value );
1963         IWineMsiRemotePackage_Release( remote_package );
1964
1965         if (FAILED(hr))
1966         {
1967             if (HRESULT_FACILITY(hr) == FACILITY_WIN32)
1968                 return HRESULT_CODE(hr);
1969
1970             return ERROR_FUNCTION_FAILED;
1971         }
1972
1973         return ERROR_SUCCESS;
1974     }
1975
1976     ret = msi_set_property( package->db, szName, szValue );
1977     if (ret == ERROR_SUCCESS && !strcmpW( szName, cszSourceDir ))
1978         msi_reset_folders( package, TRUE );
1979
1980     msiobj_release( &package->hdr );
1981     return ret;
1982 }
1983
1984 static MSIRECORD *msi_get_property_row( MSIDATABASE *db, LPCWSTR name )
1985 {
1986     MSIQUERY *view;
1987     MSIRECORD *rec, *row = NULL;
1988     UINT r;
1989
1990     static const WCHAR query[]= {
1991         'S','E','L','E','C','T',' ','`','V','a','l','u','e','`',' ',
1992         'F','R','O','M',' ' ,'`','_','P','r','o','p','e','r','t','y','`',
1993         ' ','W','H','E','R','E',' ' ,'`','_','P','r','o','p','e','r','t','y','`',
1994         '=','?',0};
1995
1996     if (!name || !*name)
1997         return NULL;
1998
1999     rec = MSI_CreateRecord(1);
2000     if (!rec)
2001         return NULL;
2002
2003     MSI_RecordSetStringW(rec, 1, name);
2004
2005     r = MSI_DatabaseOpenViewW(db, query, &view);
2006     if (r == ERROR_SUCCESS)
2007     {
2008         MSI_ViewExecute(view, rec);
2009         MSI_ViewFetch(view, &row);
2010         MSI_ViewClose(view);
2011         msiobj_release(&view->hdr);
2012     }
2013
2014     msiobj_release(&rec->hdr);
2015     return row;
2016 }
2017
2018 /* internal function, not compatible with MsiGetPropertyW */
2019 UINT msi_get_property( MSIDATABASE *db, LPCWSTR szName,
2020                        LPWSTR szValueBuf, LPDWORD pchValueBuf )
2021 {
2022     MSIRECORD *row;
2023     UINT rc = ERROR_FUNCTION_FAILED;
2024
2025     row = msi_get_property_row( db, szName );
2026
2027     if (*pchValueBuf > 0)
2028         szValueBuf[0] = 0;
2029
2030     if (row)
2031     {
2032         rc = MSI_RecordGetStringW(row, 1, szValueBuf, pchValueBuf);
2033         msiobj_release(&row->hdr);
2034     }
2035
2036     if (rc == ERROR_SUCCESS)
2037         TRACE("returning %s for property %s\n", debugstr_w(szValueBuf),
2038             debugstr_w(szName));
2039     else if (rc == ERROR_MORE_DATA)
2040         TRACE("need %d sized buffer for %s\n", *pchValueBuf,
2041             debugstr_w(szName));
2042     else
2043     {
2044         *pchValueBuf = 0;
2045         TRACE("property %s not found\n", debugstr_w(szName));
2046     }
2047
2048     return rc;
2049 }
2050
2051 LPWSTR msi_dup_property(MSIDATABASE *db, LPCWSTR prop)
2052 {
2053     DWORD sz = 0;
2054     LPWSTR str;
2055     UINT r;
2056
2057     r = msi_get_property(db, prop, NULL, &sz);
2058     if (r != ERROR_SUCCESS && r != ERROR_MORE_DATA)
2059         return NULL;
2060
2061     sz++;
2062     str = msi_alloc(sz * sizeof(WCHAR));
2063     r = msi_get_property(db, prop, str, &sz);
2064     if (r != ERROR_SUCCESS)
2065     {
2066         msi_free(str);
2067         str = NULL;
2068     }
2069
2070     return str;
2071 }
2072
2073 int msi_get_property_int( MSIDATABASE *db, LPCWSTR prop, int def )
2074 {
2075     LPWSTR str = msi_dup_property( db, prop );
2076     int val = str ? atoiW(str) : def;
2077     msi_free(str);
2078     return val;
2079 }
2080
2081 static UINT MSI_GetProperty( MSIHANDLE handle, LPCWSTR name,
2082                              awstring *szValueBuf, LPDWORD pchValueBuf )
2083 {
2084     MSIPACKAGE *package;
2085     MSIRECORD *row = NULL;
2086     UINT r = ERROR_FUNCTION_FAILED;
2087     LPCWSTR val = NULL;
2088
2089     TRACE("%u %s %p %p\n", handle, debugstr_w(name),
2090           szValueBuf->str.w, pchValueBuf );
2091
2092     if (!name)
2093         return ERROR_INVALID_PARAMETER;
2094
2095     package = msihandle2msiinfo( handle, MSIHANDLETYPE_PACKAGE );
2096     if (!package)
2097     {
2098         HRESULT hr;
2099         IWineMsiRemotePackage *remote_package;
2100         LPWSTR value = NULL;
2101         BSTR bname;
2102         DWORD len;
2103
2104         remote_package = (IWineMsiRemotePackage *)msi_get_remote( handle );
2105         if (!remote_package)
2106             return ERROR_INVALID_HANDLE;
2107
2108         bname = SysAllocString( name );
2109         if (!bname)
2110         {
2111             IWineMsiRemotePackage_Release( remote_package );
2112             return ERROR_OUTOFMEMORY;
2113         }
2114
2115         len = 0;
2116         hr = IWineMsiRemotePackage_GetProperty( remote_package, bname, NULL, &len );
2117         if (FAILED(hr))
2118             goto done;
2119
2120         len++;
2121         value = msi_alloc(len * sizeof(WCHAR));
2122         if (!value)
2123         {
2124             r = ERROR_OUTOFMEMORY;
2125             goto done;
2126         }
2127
2128         hr = IWineMsiRemotePackage_GetProperty( remote_package, bname, (BSTR *)value, &len );
2129         if (FAILED(hr))
2130             goto done;
2131
2132         r = msi_strcpy_to_awstring( value, szValueBuf, pchValueBuf );
2133
2134         /* Bug required by Adobe installers */
2135         if (!szValueBuf->unicode && !szValueBuf->str.a)
2136             *pchValueBuf *= sizeof(WCHAR);
2137
2138 done:
2139         IWineMsiRemotePackage_Release(remote_package);
2140         SysFreeString(bname);
2141         msi_free(value);
2142
2143         if (FAILED(hr))
2144         {
2145             if (HRESULT_FACILITY(hr) == FACILITY_WIN32)
2146                 return HRESULT_CODE(hr);
2147
2148             return ERROR_FUNCTION_FAILED;
2149         }
2150
2151         return r;
2152     }
2153
2154     row = msi_get_property_row( package->db, name );
2155     if (row)
2156         val = MSI_RecordGetString( row, 1 );
2157
2158     if (!val)
2159         val = szEmpty;
2160
2161     r = msi_strcpy_to_awstring( val, szValueBuf, pchValueBuf );
2162
2163     if (row)
2164         msiobj_release( &row->hdr );
2165     msiobj_release( &package->hdr );
2166
2167     return r;
2168 }
2169
2170 UINT WINAPI MsiGetPropertyA( MSIHANDLE hInstall, LPCSTR szName,
2171                              LPSTR szValueBuf, LPDWORD pchValueBuf )
2172 {
2173     awstring val;
2174     LPWSTR name;
2175     UINT r;
2176
2177     val.unicode = FALSE;
2178     val.str.a = szValueBuf;
2179
2180     name = strdupAtoW( szName );
2181     if (szName && !name)
2182         return ERROR_OUTOFMEMORY;
2183
2184     r = MSI_GetProperty( hInstall, name, &val, pchValueBuf );
2185     msi_free( name );
2186     return r;
2187 }
2188
2189 UINT WINAPI MsiGetPropertyW( MSIHANDLE hInstall, LPCWSTR szName,
2190                              LPWSTR szValueBuf, LPDWORD pchValueBuf )
2191 {
2192     awstring val;
2193
2194     val.unicode = TRUE;
2195     val.str.w = szValueBuf;
2196
2197     return MSI_GetProperty( hInstall, szName, &val, pchValueBuf );
2198 }
2199
2200 typedef struct _msi_remote_package_impl {
2201     const IWineMsiRemotePackageVtbl *lpVtbl;
2202     MSIHANDLE package;
2203     LONG refs;
2204 } msi_remote_package_impl;
2205
2206 static inline msi_remote_package_impl* mrp_from_IWineMsiRemotePackage( IWineMsiRemotePackage* iface )
2207 {
2208     return (msi_remote_package_impl*) iface;
2209 }
2210
2211 static HRESULT WINAPI mrp_QueryInterface( IWineMsiRemotePackage *iface,
2212                 REFIID riid,LPVOID *ppobj)
2213 {
2214     if( IsEqualCLSID( riid, &IID_IUnknown ) ||
2215         IsEqualCLSID( riid, &IID_IWineMsiRemotePackage ) )
2216     {
2217         IUnknown_AddRef( iface );
2218         *ppobj = iface;
2219         return S_OK;
2220     }
2221
2222     return E_NOINTERFACE;
2223 }
2224
2225 static ULONG WINAPI mrp_AddRef( IWineMsiRemotePackage *iface )
2226 {
2227     msi_remote_package_impl* This = mrp_from_IWineMsiRemotePackage( iface );
2228
2229     return InterlockedIncrement( &This->refs );
2230 }
2231
2232 static ULONG WINAPI mrp_Release( IWineMsiRemotePackage *iface )
2233 {
2234     msi_remote_package_impl* This = mrp_from_IWineMsiRemotePackage( iface );
2235     ULONG r;
2236
2237     r = InterlockedDecrement( &This->refs );
2238     if (r == 0)
2239     {
2240         MsiCloseHandle( This->package );
2241         msi_free( This );
2242     }
2243     return r;
2244 }
2245
2246 static HRESULT WINAPI mrp_SetMsiHandle( IWineMsiRemotePackage *iface, MSIHANDLE handle )
2247 {
2248     msi_remote_package_impl* This = mrp_from_IWineMsiRemotePackage( iface );
2249     This->package = handle;
2250     return S_OK;
2251 }
2252
2253 static HRESULT WINAPI mrp_GetActiveDatabase( IWineMsiRemotePackage *iface, MSIHANDLE *handle )
2254 {
2255     msi_remote_package_impl* This = mrp_from_IWineMsiRemotePackage( iface );
2256     IWineMsiRemoteDatabase *rdb = NULL;
2257     HRESULT hr;
2258     MSIHANDLE hdb;
2259
2260     hr = create_msi_remote_database( NULL, (LPVOID *)&rdb );
2261     if (FAILED(hr) || !rdb)
2262     {
2263         ERR("Failed to create remote database\n");
2264         return hr;
2265     }
2266
2267     hdb = MsiGetActiveDatabase(This->package);
2268
2269     hr = IWineMsiRemoteDatabase_SetMsiHandle( rdb, hdb );
2270     if (FAILED(hr))
2271     {
2272         ERR("Failed to set the database handle\n");
2273         return hr;
2274     }
2275
2276     *handle = alloc_msi_remote_handle( (IUnknown *)rdb );
2277     return S_OK;
2278 }
2279
2280 static HRESULT WINAPI mrp_GetProperty( IWineMsiRemotePackage *iface, BSTR property, BSTR *value, DWORD *size )
2281 {
2282     msi_remote_package_impl* This = mrp_from_IWineMsiRemotePackage( iface );
2283     UINT r;
2284
2285     r = MsiGetPropertyW(This->package, (LPWSTR)property, (LPWSTR)value, size);
2286     if (r != ERROR_SUCCESS)
2287         return HRESULT_FROM_WIN32(r);
2288
2289     return S_OK;
2290 }
2291
2292 static HRESULT WINAPI mrp_SetProperty( IWineMsiRemotePackage *iface, BSTR property, BSTR value )
2293 {
2294     msi_remote_package_impl* This = mrp_from_IWineMsiRemotePackage( iface );
2295     UINT r = MsiSetPropertyW(This->package, property, value);
2296     return HRESULT_FROM_WIN32(r);
2297 }
2298
2299 static HRESULT WINAPI mrp_ProcessMessage( IWineMsiRemotePackage *iface, INSTALLMESSAGE message, MSIHANDLE record )
2300 {
2301     msi_remote_package_impl* This = mrp_from_IWineMsiRemotePackage( iface );
2302     UINT r = MsiProcessMessage(This->package, message, record);
2303     return HRESULT_FROM_WIN32(r);
2304 }
2305
2306 static HRESULT WINAPI mrp_DoAction( IWineMsiRemotePackage *iface, BSTR action )
2307 {
2308     msi_remote_package_impl* This = mrp_from_IWineMsiRemotePackage( iface );
2309     UINT r = MsiDoActionW(This->package, action);
2310     return HRESULT_FROM_WIN32(r);
2311 }
2312
2313 static HRESULT WINAPI mrp_Sequence( IWineMsiRemotePackage *iface, BSTR table, int sequence )
2314 {
2315     msi_remote_package_impl* This = mrp_from_IWineMsiRemotePackage( iface );
2316     UINT r = MsiSequenceW(This->package, table, sequence);
2317     return HRESULT_FROM_WIN32(r);
2318 }
2319
2320 static HRESULT WINAPI mrp_GetTargetPath( IWineMsiRemotePackage *iface, BSTR folder, BSTR *value, DWORD *size )
2321 {
2322     msi_remote_package_impl* This = mrp_from_IWineMsiRemotePackage( iface );
2323     UINT r = MsiGetTargetPathW(This->package, (LPWSTR)folder, (LPWSTR)value, size);
2324     return HRESULT_FROM_WIN32(r);
2325 }
2326
2327 static HRESULT WINAPI mrp_SetTargetPath( IWineMsiRemotePackage *iface, BSTR folder, BSTR value)
2328 {
2329     msi_remote_package_impl* This = mrp_from_IWineMsiRemotePackage( iface );
2330     UINT r = MsiSetTargetPathW(This->package, folder, value);
2331     return HRESULT_FROM_WIN32(r);
2332 }
2333
2334 static HRESULT WINAPI mrp_GetSourcePath( IWineMsiRemotePackage *iface, BSTR folder, BSTR *value, DWORD *size )
2335 {
2336     msi_remote_package_impl* This = mrp_from_IWineMsiRemotePackage( iface );
2337     UINT r = MsiGetSourcePathW(This->package, (LPWSTR)folder, (LPWSTR)value, size);
2338     return HRESULT_FROM_WIN32(r);
2339 }
2340
2341 static HRESULT WINAPI mrp_GetMode( IWineMsiRemotePackage *iface, MSIRUNMODE mode, BOOL *ret )
2342 {
2343     msi_remote_package_impl* This = mrp_from_IWineMsiRemotePackage( iface );
2344     *ret = MsiGetMode(This->package, mode);
2345     return S_OK;
2346 }
2347
2348 static HRESULT WINAPI mrp_SetMode( IWineMsiRemotePackage *iface, MSIRUNMODE mode, BOOL state )
2349 {
2350     msi_remote_package_impl* This = mrp_from_IWineMsiRemotePackage( iface );
2351     UINT r = MsiSetMode(This->package, mode, state);
2352     return HRESULT_FROM_WIN32(r);
2353 }
2354
2355 static HRESULT WINAPI mrp_GetFeatureState( IWineMsiRemotePackage *iface, BSTR feature,
2356                                     INSTALLSTATE *installed, INSTALLSTATE *action )
2357 {
2358     msi_remote_package_impl* This = mrp_from_IWineMsiRemotePackage( iface );
2359     UINT r = MsiGetFeatureStateW(This->package, feature, installed, action);
2360     return HRESULT_FROM_WIN32(r);
2361 }
2362
2363 static HRESULT WINAPI mrp_SetFeatureState( IWineMsiRemotePackage *iface, BSTR feature, INSTALLSTATE state )
2364 {
2365     msi_remote_package_impl* This = mrp_from_IWineMsiRemotePackage( iface );
2366     UINT r = MsiSetFeatureStateW(This->package, feature, state);
2367     return HRESULT_FROM_WIN32(r);
2368 }
2369
2370 static HRESULT WINAPI mrp_GetComponentState( IWineMsiRemotePackage *iface, BSTR component,
2371                                       INSTALLSTATE *installed, INSTALLSTATE *action )
2372 {
2373     msi_remote_package_impl* This = mrp_from_IWineMsiRemotePackage( iface );
2374     UINT r = MsiGetComponentStateW(This->package, component, installed, action);
2375     return HRESULT_FROM_WIN32(r);
2376 }
2377
2378 static HRESULT WINAPI mrp_SetComponentState( IWineMsiRemotePackage *iface, BSTR component, INSTALLSTATE state )
2379 {
2380     msi_remote_package_impl* This = mrp_from_IWineMsiRemotePackage( iface );
2381     UINT r = MsiSetComponentStateW(This->package, component, state);
2382     return HRESULT_FROM_WIN32(r);
2383 }
2384
2385 static HRESULT WINAPI mrp_GetLanguage( IWineMsiRemotePackage *iface, LANGID *language )
2386 {
2387     msi_remote_package_impl* This = mrp_from_IWineMsiRemotePackage( iface );
2388     *language = MsiGetLanguage(This->package);
2389     return S_OK;
2390 }
2391
2392 static HRESULT WINAPI mrp_SetInstallLevel( IWineMsiRemotePackage *iface, int level )
2393 {
2394     msi_remote_package_impl* This = mrp_from_IWineMsiRemotePackage( iface );
2395     UINT r = MsiSetInstallLevel(This->package, level);
2396     return HRESULT_FROM_WIN32(r);
2397 }
2398
2399 static HRESULT WINAPI mrp_FormatRecord( IWineMsiRemotePackage *iface, MSIHANDLE record,
2400                                         BSTR *value)
2401 {
2402     DWORD size = 0;
2403     msi_remote_package_impl* This = mrp_from_IWineMsiRemotePackage( iface );
2404     UINT r = MsiFormatRecordW(This->package, record, NULL, &size);
2405     if (r == ERROR_SUCCESS)
2406     {
2407         *value = SysAllocStringLen(NULL, size);
2408         if (!*value)
2409             return E_OUTOFMEMORY;
2410         size++;
2411         r = MsiFormatRecordW(This->package, record, *value, &size);
2412     }
2413     return HRESULT_FROM_WIN32(r);
2414 }
2415
2416 static HRESULT WINAPI mrp_EvaluateCondition( IWineMsiRemotePackage *iface, BSTR condition )
2417 {
2418     msi_remote_package_impl* This = mrp_from_IWineMsiRemotePackage( iface );
2419     UINT r = MsiEvaluateConditionW(This->package, condition);
2420     return HRESULT_FROM_WIN32(r);
2421 }
2422
2423 static HRESULT WINAPI mrp_GetFeatureCost( IWineMsiRemotePackage *iface, BSTR feature,
2424                                           INT cost_tree, INSTALLSTATE state, INT *cost )
2425 {
2426     msi_remote_package_impl* This = mrp_from_IWineMsiRemotePackage( iface );
2427     UINT r = MsiGetFeatureCostW(This->package, feature, cost_tree, state, cost);
2428     return HRESULT_FROM_WIN32(r);
2429 }
2430
2431 static const IWineMsiRemotePackageVtbl msi_remote_package_vtbl =
2432 {
2433     mrp_QueryInterface,
2434     mrp_AddRef,
2435     mrp_Release,
2436     mrp_SetMsiHandle,
2437     mrp_GetActiveDatabase,
2438     mrp_GetProperty,
2439     mrp_SetProperty,
2440     mrp_ProcessMessage,
2441     mrp_DoAction,
2442     mrp_Sequence,
2443     mrp_GetTargetPath,
2444     mrp_SetTargetPath,
2445     mrp_GetSourcePath,
2446     mrp_GetMode,
2447     mrp_SetMode,
2448     mrp_GetFeatureState,
2449     mrp_SetFeatureState,
2450     mrp_GetComponentState,
2451     mrp_SetComponentState,
2452     mrp_GetLanguage,
2453     mrp_SetInstallLevel,
2454     mrp_FormatRecord,
2455     mrp_EvaluateCondition,
2456     mrp_GetFeatureCost,
2457 };
2458
2459 HRESULT create_msi_remote_package( IUnknown *pOuter, LPVOID *ppObj )
2460 {
2461     msi_remote_package_impl* This;
2462
2463     This = msi_alloc( sizeof *This );
2464     if (!This)
2465         return E_OUTOFMEMORY;
2466
2467     This->lpVtbl = &msi_remote_package_vtbl;
2468     This->package = 0;
2469     This->refs = 1;
2470
2471     *ppObj = This;
2472
2473     return S_OK;
2474 }
2475
2476 UINT msi_package_add_info(MSIPACKAGE *package, DWORD context, DWORD options,
2477                           LPCWSTR property, LPWSTR value)
2478 {
2479     MSISOURCELISTINFO *info;
2480
2481     info = msi_alloc(sizeof(MSISOURCELISTINFO));
2482     if (!info)
2483         return ERROR_OUTOFMEMORY;
2484
2485     info->context = context;
2486     info->options = options;
2487     info->property = property;
2488     info->value = strdupW(value);
2489     list_add_head(&package->sourcelist_info, &info->entry);
2490
2491     return ERROR_SUCCESS;
2492 }
2493
2494 UINT msi_package_add_media_disk(MSIPACKAGE *package, DWORD context, DWORD options,
2495                                 DWORD disk_id, LPWSTR volume_label, LPWSTR disk_prompt)
2496 {
2497     MSIMEDIADISK *disk;
2498
2499     disk = msi_alloc(sizeof(MSIMEDIADISK));
2500     if (!disk)
2501         return ERROR_OUTOFMEMORY;
2502
2503     disk->context = context;
2504     disk->options = options;
2505     disk->disk_id = disk_id;
2506     disk->volume_label = strdupW(volume_label);
2507     disk->disk_prompt = strdupW(disk_prompt);
2508     list_add_head(&package->sourcelist_media, &disk->entry);
2509
2510     return ERROR_SUCCESS;
2511 }