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