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