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