rpcrt4: Clean up handling of enum16 types.
[wine] / dlls / msi / media.c
1 /*
2  * Implementation of the Microsoft Installer (msi.dll)
3  *
4  * Copyright 2008 James Hawkins
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 #include <stdarg.h>
22
23 #define COBJMACROS
24
25 #include "windef.h"
26 #include "winerror.h"
27 #include "wine/debug.h"
28 #include "fdi.h"
29 #include "msipriv.h"
30 #include "winuser.h"
31 #include "winreg.h"
32 #include "shlwapi.h"
33 #include "objidl.h"
34 #include "wine/unicode.h"
35
36 WINE_DEFAULT_DEBUG_CHANNEL(msi);
37
38 /* from msvcrt/fcntl.h */
39 #define _O_RDONLY      0
40 #define _O_WRONLY      1
41 #define _O_RDWR        2
42 #define _O_ACCMODE     (_O_RDONLY|_O_WRONLY|_O_RDWR)
43 #define _O_APPEND      0x0008
44 #define _O_RANDOM      0x0010
45 #define _O_SEQUENTIAL  0x0020
46 #define _O_TEMPORARY   0x0040
47 #define _O_NOINHERIT   0x0080
48 #define _O_CREAT       0x0100
49 #define _O_TRUNC       0x0200
50 #define _O_EXCL        0x0400
51 #define _O_SHORT_LIVED 0x1000
52 #define _O_TEXT        0x4000
53 #define _O_BINARY      0x8000
54
55 static BOOL source_matches_volume(MSIMEDIAINFO *mi, LPCWSTR source_root)
56 {
57     WCHAR volume_name[MAX_PATH + 1];
58     WCHAR root[MAX_PATH + 1];
59
60     strcpyW(root, source_root);
61     PathStripToRootW(root);
62     PathAddBackslashW(root);
63
64     if (!GetVolumeInformationW(root, volume_name, MAX_PATH + 1,
65                                NULL, NULL, NULL, NULL, 0))
66     {
67         ERR("Failed to get volume information\n");
68         return FALSE;
69     }
70
71     return !lstrcmpW(mi->volume_label, volume_name);
72 }
73
74 static UINT msi_change_media(MSIPACKAGE *package, MSIMEDIAINFO *mi)
75 {
76     LPWSTR error, error_dialog;
77     LPWSTR source_dir;
78     UINT r = ERROR_SUCCESS;
79
80     static const WCHAR error_prop[] = {'E','r','r','o','r','D','i','a','l','o','g',0};
81
82     if ((msi_get_property_int(package, szUILevel, 0) & INSTALLUILEVEL_MASK) ==
83          INSTALLUILEVEL_NONE && !gUIHandlerA && !gUIHandlerW && !gUIHandlerRecord)
84         return ERROR_SUCCESS;
85
86     error = generate_error_string(package, 1302, 1, mi->disk_prompt);
87     error_dialog = msi_dup_property(package, error_prop);
88     source_dir = msi_dup_property(package, cszSourceDir);
89
90     while (r == ERROR_SUCCESS && !source_matches_volume(mi, source_dir))
91     {
92         r = msi_spawn_error_dialog(package, error_dialog, error);
93
94         if (gUIHandlerW)
95         {
96             gUIHandlerW(gUIContext, MB_RETRYCANCEL | INSTALLMESSAGE_ERROR, error);
97         }
98         else if (gUIHandlerA)
99         {
100             char *msg = strdupWtoA(error);
101             gUIHandlerA(gUIContext, MB_RETRYCANCEL | INSTALLMESSAGE_ERROR, msg);
102             msi_free(msg);
103         }
104         else if (gUIHandlerRecord)
105         {
106             MSIHANDLE rec = MsiCreateRecord(1);
107             MsiRecordSetStringW(rec, 0, error);
108             gUIHandlerRecord(gUIContext, MB_RETRYCANCEL | INSTALLMESSAGE_ERROR, rec);
109             MsiCloseHandle(rec);
110         }
111     }
112
113     msi_free(error);
114     msi_free(error_dialog);
115     msi_free(source_dir);
116
117     return r;
118 }
119
120 static void * CDECL cabinet_alloc(ULONG cb)
121 {
122     return msi_alloc(cb);
123 }
124
125 static void CDECL cabinet_free(void *pv)
126 {
127     msi_free(pv);
128 }
129
130 static INT_PTR CDECL cabinet_open(char *pszFile, int oflag, int pmode)
131 {
132     HANDLE handle;
133     DWORD dwAccess = 0;
134     DWORD dwShareMode = 0;
135     DWORD dwCreateDisposition = OPEN_EXISTING;
136
137     switch (oflag & _O_ACCMODE)
138     {
139     case _O_RDONLY:
140         dwAccess = GENERIC_READ;
141         dwShareMode = FILE_SHARE_READ | FILE_SHARE_DELETE;
142         break;
143     case _O_WRONLY:
144         dwAccess = GENERIC_WRITE;
145         dwShareMode = FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE;
146         break;
147     case _O_RDWR:
148         dwAccess = GENERIC_READ | GENERIC_WRITE;
149         dwShareMode = FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE;
150         break;
151     }
152
153     if ((oflag & (_O_CREAT | _O_EXCL)) == (_O_CREAT | _O_EXCL))
154         dwCreateDisposition = CREATE_NEW;
155     else if (oflag & _O_CREAT)
156         dwCreateDisposition = CREATE_ALWAYS;
157
158     handle = CreateFileA(pszFile, dwAccess, dwShareMode, NULL,
159                          dwCreateDisposition, 0, NULL);
160     if (handle == INVALID_HANDLE_VALUE)
161         return 0;
162
163     return (INT_PTR)handle;
164 }
165
166 static UINT CDECL cabinet_read(INT_PTR hf, void *pv, UINT cb)
167 {
168     HANDLE handle = (HANDLE)hf;
169     DWORD read;
170
171     if (ReadFile(handle, pv, cb, &read, NULL))
172         return read;
173
174     return 0;
175 }
176
177 static UINT CDECL cabinet_write(INT_PTR hf, void *pv, UINT cb)
178 {
179     HANDLE handle = (HANDLE)hf;
180     DWORD written;
181
182     if (WriteFile(handle, pv, cb, &written, NULL))
183         return written;
184
185     return 0;
186 }
187
188 static int CDECL cabinet_close(INT_PTR hf)
189 {
190     HANDLE handle = (HANDLE)hf;
191     return CloseHandle(handle) ? 0 : -1;
192 }
193
194 static LONG CDECL cabinet_seek(INT_PTR hf, LONG dist, int seektype)
195 {
196     HANDLE handle = (HANDLE)hf;
197     /* flags are compatible and so are passed straight through */
198     return SetFilePointer(handle, dist, NULL, seektype);
199 }
200
201 struct cab_stream
202 {
203     MSIDATABASE *db;
204     WCHAR       *name;
205 };
206
207 static struct cab_stream cab_stream;
208
209 static INT_PTR CDECL cabinet_open_stream( char *pszFile, int oflag, int pmode )
210 {
211     UINT r;
212     IStream *stm;
213
214     if (oflag)
215         WARN("ignoring open flags 0x%08x\n", oflag);
216
217     r = db_get_raw_stream( cab_stream.db, cab_stream.name, &stm );
218     if (r != ERROR_SUCCESS)
219     {
220         WARN("Failed to get cabinet stream %u\n", r);
221         return 0;
222     }
223
224     return (INT_PTR)stm;
225 }
226
227 static UINT CDECL cabinet_read_stream( INT_PTR hf, void *pv, UINT cb )
228 {
229     IStream *stm = (IStream *)hf;
230     DWORD read;
231     HRESULT hr;
232
233     hr = IStream_Read( stm, pv, cb, &read );
234     if (hr == S_OK || hr == S_FALSE)
235         return read;
236
237     return 0;
238 }
239
240 static int CDECL cabinet_close_stream( INT_PTR hf )
241 {
242     IStream *stm = (IStream *)hf;
243     IStream_Release( stm );
244     return 0;
245 }
246
247 static LONG CDECL cabinet_seek_stream( INT_PTR hf, LONG dist, int seektype )
248 {
249     IStream *stm = (IStream *)hf;
250     LARGE_INTEGER move;
251     ULARGE_INTEGER newpos;
252     HRESULT hr;
253
254     move.QuadPart = dist;
255     hr = IStream_Seek( stm, move, seektype, &newpos );
256     if (SUCCEEDED(hr))
257     {
258         if (newpos.QuadPart <= MAXLONG) return newpos.QuadPart;
259         ERR("Too big!\n");
260     }
261     return -1;
262 }
263
264 static UINT CDECL msi_media_get_disk_info(MSIPACKAGE *package, MSIMEDIAINFO *mi)
265 {
266     MSIRECORD *row;
267
268     static const WCHAR query[] = {
269         'S','E','L','E','C','T',' ','*',' ', 'F','R','O','M',' ',
270         '`','M','e','d','i','a','`',' ','W','H','E','R','E',' ',
271         '`','D','i','s','k','I','d','`',' ','=',' ','%','i',0};
272
273     row = MSI_QueryGetRecord(package->db, query, mi->disk_id);
274     if (!row)
275     {
276         TRACE("Unable to query row\n");
277         return ERROR_FUNCTION_FAILED;
278     }
279
280     mi->disk_prompt = strdupW(MSI_RecordGetString(row, 3));
281     mi->cabinet = strdupW(MSI_RecordGetString(row, 4));
282     mi->volume_label = strdupW(MSI_RecordGetString(row, 5));
283
284     if (!mi->first_volume)
285         mi->first_volume = strdupW(mi->volume_label);
286
287     msiobj_release(&row->hdr);
288     return ERROR_SUCCESS;
289 }
290
291 static INT_PTR cabinet_partial_file(FDINOTIFICATIONTYPE fdint,
292                                     PFDINOTIFICATION pfdin)
293 {
294     MSICABDATA *data = pfdin->pv;
295     data->mi->is_continuous = FALSE;
296     return 0;
297 }
298
299 static WCHAR *get_cabinet_filename(MSIMEDIAINFO *mi)
300 {
301     int len;
302     WCHAR *ret;
303
304     len = strlenW(mi->sourcedir) + strlenW(mi->cabinet) + 1;
305     if (!(ret = msi_alloc(len * sizeof(WCHAR)))) return NULL;
306     strcpyW(ret, mi->sourcedir);
307     strcatW(ret, mi->cabinet);
308     return ret;
309 }
310
311 static INT_PTR cabinet_next_cabinet(FDINOTIFICATIONTYPE fdint,
312                                     PFDINOTIFICATION pfdin)
313 {
314     MSICABDATA *data = pfdin->pv;
315     MSIMEDIAINFO *mi = data->mi;
316     LPWSTR cabinet_file = NULL, cab = strdupAtoW(pfdin->psz1);
317     INT_PTR res = -1;
318     UINT rc;
319
320     msi_free(mi->disk_prompt);
321     msi_free(mi->cabinet);
322     msi_free(mi->volume_label);
323     mi->disk_prompt = NULL;
324     mi->cabinet = NULL;
325     mi->volume_label = NULL;
326
327     mi->disk_id++;
328     mi->is_continuous = TRUE;
329
330     rc = msi_media_get_disk_info(data->package, mi);
331     if (rc != ERROR_SUCCESS)
332     {
333         ERR("Failed to get next cabinet information: %d\n", rc);
334         goto done;
335     }
336
337     if (lstrcmpiW(mi->cabinet, cab))
338     {
339         ERR("Continuous cabinet does not match the next cabinet in the Media table\n");
340         goto done;
341     }
342
343     if (!(cabinet_file = get_cabinet_filename(mi)))
344         goto done;
345
346     TRACE("Searching for %s\n", debugstr_w(cabinet_file));
347
348     res = 0;
349     if (GetFileAttributesW(cabinet_file) == INVALID_FILE_ATTRIBUTES)
350     {
351         if (msi_change_media(data->package, mi) != ERROR_SUCCESS)
352             res = -1;
353     }
354
355 done:
356     msi_free(cab);
357     msi_free(cabinet_file);
358     return res;
359 }
360
361 static INT_PTR cabinet_copy_file(FDINOTIFICATIONTYPE fdint,
362                                  PFDINOTIFICATION pfdin)
363 {
364     MSICABDATA *data = pfdin->pv;
365     HANDLE handle = 0;
366     LPWSTR path = NULL;
367     DWORD attrs;
368
369     data->curfile = strdupAtoW(pfdin->psz1);
370     if (!data->cb(data->package, data->curfile, MSICABEXTRACT_BEGINEXTRACT, &path,
371                   &attrs, data->user))
372     {
373         /* We're not extracting this file, so free the filename. */
374         msi_free(data->curfile);
375         data->curfile = NULL;
376         goto done;
377     }
378
379     TRACE("extracting %s\n", debugstr_w(path));
380
381     attrs = attrs & (FILE_ATTRIBUTE_READONLY|FILE_ATTRIBUTE_HIDDEN|FILE_ATTRIBUTE_SYSTEM);
382     if (!attrs) attrs = FILE_ATTRIBUTE_NORMAL;
383
384     handle = CreateFileW(path, GENERIC_READ | GENERIC_WRITE, 0,
385                          NULL, CREATE_ALWAYS, attrs, NULL);
386     if (handle == INVALID_HANDLE_VALUE)
387     {
388         DWORD err = GetLastError();
389         DWORD attrs2 = GetFileAttributesW(path);
390
391         if (attrs2 == INVALID_FILE_ATTRIBUTES)
392         {
393             ERR("failed to create %s (error %d)\n", debugstr_w(path), err);
394             goto done;
395         }
396         else if (err == ERROR_ACCESS_DENIED && (attrs2 & FILE_ATTRIBUTE_READONLY))
397         {
398             TRACE("removing read-only attribute on %s\n", debugstr_w(path));
399             SetFileAttributesW( path, attrs2 & ~FILE_ATTRIBUTE_READONLY );
400             handle = CreateFileW(path, GENERIC_READ | GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, attrs2, NULL);
401
402             if (handle != INVALID_HANDLE_VALUE) goto done;
403             err = GetLastError();
404         }
405         if (err == ERROR_SHARING_VIOLATION || err == ERROR_USER_MAPPED_FILE)
406         {
407             WCHAR tmpfileW[MAX_PATH], *tmppathW, *p;
408             DWORD len;
409
410             TRACE("file in use, scheduling rename operation\n");
411
412             GetTempFileNameW(szBackSlash, szMsi, 0, tmpfileW);
413             len = strlenW(path) + strlenW(tmpfileW) + 1;
414             if (!(tmppathW = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR))))
415                 return ERROR_OUTOFMEMORY;
416
417             strcpyW(tmppathW, path);
418             if ((p = strrchrW(tmppathW, '\\'))) *p = 0;
419             strcatW(tmppathW, tmpfileW);
420
421             handle = CreateFileW(tmppathW, GENERIC_READ | GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, attrs, NULL);
422
423             if (handle != INVALID_HANDLE_VALUE &&
424                 MoveFileExW(path, NULL, MOVEFILE_DELAY_UNTIL_REBOOT) &&
425                 MoveFileExW(tmppathW, path, MOVEFILE_DELAY_UNTIL_REBOOT))
426             {
427                 data->package->need_reboot = 1;
428             }
429             else
430                 WARN("failed to schedule rename operation %s (error %d)\n", debugstr_w(path), GetLastError());
431
432             HeapFree(GetProcessHeap(), 0, tmppathW);
433         }
434         else
435             WARN("failed to create %s (error %d)\n", debugstr_w(path), err);
436     }
437
438 done:
439     msi_free(path);
440
441     return (INT_PTR)handle;
442 }
443
444 static INT_PTR cabinet_close_file_info(FDINOTIFICATIONTYPE fdint,
445                                        PFDINOTIFICATION pfdin)
446 {
447     MSICABDATA *data = pfdin->pv;
448     FILETIME ft;
449     FILETIME ftLocal;
450     HANDLE handle = (HANDLE)pfdin->hf;
451
452     data->mi->is_continuous = FALSE;
453
454     if (!DosDateTimeToFileTime(pfdin->date, pfdin->time, &ft))
455         return -1;
456     if (!LocalFileTimeToFileTime(&ft, &ftLocal))
457         return -1;
458     if (!SetFileTime(handle, &ftLocal, 0, &ftLocal))
459         return -1;
460
461     CloseHandle(handle);
462
463     data->cb(data->package, data->curfile, MSICABEXTRACT_FILEEXTRACTED, NULL, NULL,
464              data->user);
465
466     msi_free(data->curfile);
467     data->curfile = NULL;
468
469     return 1;
470 }
471
472 static INT_PTR CDECL cabinet_notify(FDINOTIFICATIONTYPE fdint, PFDINOTIFICATION pfdin)
473 {
474     TRACE("(%d)\n", fdint);
475
476     switch (fdint)
477     {
478     case fdintPARTIAL_FILE:
479         return cabinet_partial_file(fdint, pfdin);
480
481     case fdintNEXT_CABINET:
482         return cabinet_next_cabinet(fdint, pfdin);
483
484     case fdintCOPY_FILE:
485         return cabinet_copy_file(fdint, pfdin);
486
487     case fdintCLOSE_FILE_INFO:
488         return cabinet_close_file_info(fdint, pfdin);
489
490     default:
491         return 0;
492     }
493 }
494
495 static INT_PTR CDECL cabinet_notify_stream( FDINOTIFICATIONTYPE fdint, PFDINOTIFICATION pfdin )
496 {
497     TRACE("(%d)\n", fdint);
498
499     switch (fdint)
500     {
501     case fdintCOPY_FILE:
502         return cabinet_copy_file( fdint, pfdin );
503
504     case fdintCLOSE_FILE_INFO:
505         return cabinet_close_file_info( fdint, pfdin );
506
507     case fdintCABINET_INFO:
508         return 0;
509
510     default:
511         ERR("Unexpected notification %d\n", fdint);
512         return 0;
513     }
514 }
515
516 static BOOL extract_cabinet( MSIPACKAGE* package, MSIMEDIAINFO *mi, LPVOID data )
517 {
518     LPSTR cabinet, cab_path = NULL;
519     HFDI hfdi;
520     ERF erf;
521     BOOL ret = FALSE;
522
523     TRACE("Extracting %s\n", debugstr_w(mi->cabinet));
524
525     hfdi = FDICreate( cabinet_alloc, cabinet_free, cabinet_open, cabinet_read,
526                       cabinet_write, cabinet_close, cabinet_seek, 0, &erf );
527     if (!hfdi)
528     {
529         ERR("FDICreate failed\n");
530         return FALSE;
531     }
532
533     cabinet = strdupWtoA( mi->cabinet );
534     if (!cabinet)
535         goto done;
536
537     cab_path = strdupWtoA( mi->sourcedir );
538     if (!cab_path)
539         goto done;
540
541     ret = FDICopy( hfdi, cabinet, cab_path, 0, cabinet_notify, NULL, data );
542     if (!ret)
543         ERR("FDICopy failed\n");
544
545 done:
546     FDIDestroy( hfdi );
547     msi_free(cabinet );
548     msi_free( cab_path );
549
550     if (ret)
551         mi->is_extracted = TRUE;
552
553     return ret;
554 }
555
556 static BOOL extract_cabinet_stream( MSIPACKAGE *package, MSIMEDIAINFO *mi, LPVOID data )
557 {
558     static char filename[] = {'<','S','T','R','E','A','M','>',0};
559     HFDI hfdi;
560     ERF erf;
561     BOOL ret = FALSE;
562
563     TRACE("Extracting %s\n", debugstr_w(mi->cabinet));
564
565     hfdi = FDICreate( cabinet_alloc, cabinet_free, cabinet_open_stream, cabinet_read_stream,
566                       cabinet_write, cabinet_close_stream, cabinet_seek_stream, 0, &erf );
567     if (!hfdi)
568     {
569         ERR("FDICreate failed\n");
570         return FALSE;
571     }
572
573     cab_stream.db = package->db;
574     cab_stream.name = encode_streamname( FALSE, mi->cabinet + 1 );
575     if (!cab_stream.name)
576         goto done;
577
578     ret = FDICopy( hfdi, filename, NULL, 0, cabinet_notify_stream, NULL, data );
579     if (!ret)
580         ERR("FDICopy failed\n");
581
582 done:
583     FDIDestroy( hfdi );
584     msi_free( cab_stream.name );
585
586     if (ret)
587         mi->is_extracted = TRUE;
588
589     return ret;
590 }
591
592 /***********************************************************************
593  *            msi_cabextract
594  *
595  * Extract files from a cabinet file or stream.
596  */
597 BOOL msi_cabextract(MSIPACKAGE* package, MSIMEDIAINFO *mi, LPVOID data)
598 {
599     if (mi->cabinet[0] == '#')
600     {
601         return extract_cabinet_stream( package, mi, data );
602     }
603     return extract_cabinet( package, mi, data );
604 }
605
606 void msi_free_media_info(MSIMEDIAINFO *mi)
607 {
608     msi_free(mi->disk_prompt);
609     msi_free(mi->cabinet);
610     msi_free(mi->volume_label);
611     msi_free(mi->first_volume);
612     msi_free(mi);
613 }
614
615 static UINT get_drive_type(const WCHAR *path)
616 {
617     WCHAR root[MAX_PATH + 1];
618
619     strcpyW(root, path);
620     PathStripToRootW(root);
621     PathAddBackslashW(root);
622
623     return GetDriveTypeW(root);
624 }
625
626 static UINT msi_load_media_info(MSIPACKAGE *package, MSIFILE *file, MSIMEDIAINFO *mi)
627 {
628     MSIRECORD *row;
629     LPWSTR source_dir;
630     LPWSTR source;
631     DWORD options;
632
633     static const WCHAR query[] = {
634         'S','E','L','E','C','T',' ','*',' ', 'F','R','O','M',' ',
635         '`','M','e','d','i','a','`',' ','W','H','E','R','E',' ',
636         '`','L','a','s','t','S','e','q','u','e','n','c','e','`',' ','>','=',
637         ' ','%','i',' ','A','N','D',' ','`','D','i','s','k','I','d','`',' ','>','=',
638         ' ','%','i',' ','O','R','D','E','R',' ','B','Y',' ',
639         '`','D','i','s','k','I','d','`',0};
640
641     row = MSI_QueryGetRecord(package->db, query, file->Sequence, mi->disk_id);
642     if (!row)
643     {
644         TRACE("Unable to query row\n");
645         return ERROR_FUNCTION_FAILED;
646     }
647
648     mi->is_extracted = FALSE;
649     mi->disk_id = MSI_RecordGetInteger(row, 1);
650     mi->last_sequence = MSI_RecordGetInteger(row, 2);
651     msi_free(mi->disk_prompt);
652     mi->disk_prompt = strdupW(MSI_RecordGetString(row, 3));
653     msi_free(mi->cabinet);
654     mi->cabinet = strdupW(MSI_RecordGetString(row, 4));
655     msi_free(mi->volume_label);
656     mi->volume_label = strdupW(MSI_RecordGetString(row, 5));
657     msiobj_release(&row->hdr);
658
659     if (!mi->first_volume)
660         mi->first_volume = strdupW(mi->volume_label);
661
662     source_dir = msi_dup_property(package, cszSourceDir);
663     lstrcpyW(mi->sourcedir, source_dir);
664     mi->type = get_drive_type(source_dir);
665
666     options = MSICODE_PRODUCT;
667     if (mi->type == DRIVE_CDROM || mi->type == DRIVE_REMOVABLE)
668     {
669         source = source_dir;
670         options |= MSISOURCETYPE_MEDIA;
671     }
672     else if (package->BaseURL && UrlIsW(package->BaseURL, URLIS_URL))
673     {
674         source = package->BaseURL;
675         options |= MSISOURCETYPE_URL;
676     }
677     else
678     {
679         source = mi->sourcedir;
680         options |= MSISOURCETYPE_NETWORK;
681     }
682
683     msi_package_add_media_disk(package, package->Context,
684                                MSICODE_PRODUCT, mi->disk_id,
685                                mi->volume_label, mi->disk_prompt);
686
687     msi_package_add_info(package, package->Context,
688                          options, INSTALLPROPERTY_LASTUSEDSOURCEW, source);
689
690     msi_free(source_dir);
691     return ERROR_SUCCESS;
692 }
693
694 /* FIXME: search URL sources as well */
695 static UINT find_published_source(MSIPACKAGE *package, MSIMEDIAINFO *mi)
696 {
697     WCHAR source[MAX_PATH];
698     WCHAR volume[MAX_PATH];
699     WCHAR prompt[MAX_PATH];
700     DWORD volumesz, promptsz;
701     DWORD index, size, id;
702     WCHAR last_type[2];
703     UINT r;
704
705     size = 2;
706     r = MsiSourceListGetInfoW(package->ProductCode, NULL,
707                               package->Context, MSICODE_PRODUCT,
708                               INSTALLPROPERTY_LASTUSEDTYPEW, last_type, &size);
709     if (r != ERROR_SUCCESS)
710         return r;
711
712     size = MAX_PATH;
713     r = MsiSourceListGetInfoW(package->ProductCode, NULL,
714                               package->Context, MSICODE_PRODUCT,
715                               INSTALLPROPERTY_LASTUSEDSOURCEW, source, &size);
716     if (r != ERROR_SUCCESS)
717         return r;
718
719     index = 0;
720     volumesz = MAX_PATH;
721     promptsz = MAX_PATH;
722
723     if (last_type[0] == 'n')
724     {
725         while (MsiSourceListEnumSourcesW(package->ProductCode, NULL,
726                                         package->Context,
727                                         MSISOURCETYPE_NETWORK, index++,
728                                         volume, &volumesz) == ERROR_SUCCESS)
729         {
730             if (!strncmpiW(source, volume, strlenW(source)))
731             {
732                 lstrcpyW(mi->sourcedir, source);
733                 TRACE("Found network source %s\n", debugstr_w(mi->sourcedir));
734                 return ERROR_SUCCESS;
735             }
736         }
737     }
738
739     index = 0;
740     volumesz = MAX_PATH;
741     promptsz = MAX_PATH;
742     while (MsiSourceListEnumMediaDisksW(package->ProductCode, NULL,
743                                         package->Context,
744                                         MSICODE_PRODUCT, index++, &id,
745                                         volume, &volumesz, prompt, &promptsz) == ERROR_SUCCESS)
746     {
747         mi->disk_id = id;
748         mi->volume_label = msi_realloc(mi->volume_label, ++volumesz * sizeof(WCHAR));
749         lstrcpyW(mi->volume_label, volume);
750         mi->disk_prompt = msi_realloc(mi->disk_prompt, ++promptsz * sizeof(WCHAR));
751         lstrcpyW(mi->disk_prompt, prompt);
752
753         if (source_matches_volume(mi, source))
754         {
755             /* FIXME: what about SourceDir */
756             lstrcpyW(mi->sourcedir, source);
757             TRACE("Found disk source %s\n", debugstr_w(mi->sourcedir));
758             return ERROR_SUCCESS;
759         }
760     }
761
762     return ERROR_FUNCTION_FAILED;
763 }
764
765 UINT ready_media(MSIPACKAGE *package, MSIFILE *file, MSIMEDIAINFO *mi)
766 {
767     UINT rc = ERROR_SUCCESS;
768     WCHAR *cabinet_file;
769
770     /* media info for continuous cabinet is already loaded */
771     if (mi->is_continuous)
772         return ERROR_SUCCESS;
773
774     rc = msi_load_media_info(package, file, mi);
775     if (rc != ERROR_SUCCESS)
776     {
777         ERR("Unable to load media info\n");
778         return ERROR_FUNCTION_FAILED;
779     }
780
781     /* cabinet is internal, no checks needed */
782     if (!mi->cabinet || mi->cabinet[0] == '#')
783         return ERROR_SUCCESS;
784
785     cabinet_file = get_cabinet_filename(mi);
786
787     /* package should be downloaded */
788     if (file->IsCompressed &&
789         GetFileAttributesW(cabinet_file) == INVALID_FILE_ATTRIBUTES &&
790         package->BaseURL && UrlIsW(package->BaseURL, URLIS_URL))
791     {
792         WCHAR temppath[MAX_PATH], *p;
793
794         msi_download_file(cabinet_file, temppath);
795         if ((p = strrchrW(temppath, '\\'))) *p = 0;
796
797         msi_free(mi->sourcedir);
798         strcpyW(mi->sourcedir, temppath);
799         msi_free(mi->cabinet);
800         strcpyW(mi->cabinet, p + 1);
801
802         msi_free(cabinet_file);
803         return ERROR_SUCCESS;
804     }
805
806     /* check volume matches, change media if not */
807     if (mi->volume_label && mi->disk_id > 1 &&
808         lstrcmpW(mi->first_volume, mi->volume_label))
809     {
810         LPWSTR source = msi_dup_property(package, cszSourceDir);
811         BOOL matches;
812
813         matches = source_matches_volume(mi, source);
814         msi_free(source);
815
816         if ((mi->type == DRIVE_CDROM || mi->type == DRIVE_REMOVABLE) && !matches)
817         {
818             rc = msi_change_media(package, mi);
819             if (rc != ERROR_SUCCESS)
820             {
821                 msi_free(cabinet_file);
822                 return rc;
823             }
824         }
825     }
826
827     if (file->IsCompressed &&
828         GetFileAttributesW(cabinet_file) == INVALID_FILE_ATTRIBUTES)
829     {
830         rc = find_published_source(package, mi);
831         if (rc != ERROR_SUCCESS)
832         {
833             ERR("Cabinet not found: %s\n", debugstr_w(cabinet_file));
834             msi_free(cabinet_file);
835             return ERROR_INSTALL_FAILURE;
836         }
837     }
838
839     msi_free(cabinet_file);
840     return ERROR_SUCCESS;
841 }