2 * Implementation of the Microsoft Installer (msi.dll)
4 * Copyright 2008 James Hawkins
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.
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.
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
27 #include "wine/debug.h"
34 #include "wine/unicode.h"
36 WINE_DEFAULT_DEBUG_CHANNEL(msi);
38 /* from msvcrt/fcntl.h */
42 #define _O_ACCMODE (_O_RDONLY|_O_WRONLY|_O_RDWR)
43 #define _O_APPEND 0x0008
44 #define _O_RANDOM 0x0010
45 #define _O_SEQUENTIAL 0x0020
46 #define _O_TEMPORARY 0x0040
47 #define _O_NOINHERIT 0x0080
48 #define _O_CREAT 0x0100
49 #define _O_TRUNC 0x0200
50 #define _O_EXCL 0x0400
51 #define _O_SHORT_LIVED 0x1000
52 #define _O_TEXT 0x4000
53 #define _O_BINARY 0x8000
55 static BOOL source_matches_volume(MSIMEDIAINFO *mi, LPCWSTR source_root)
57 WCHAR volume_name[MAX_PATH + 1];
58 WCHAR root[MAX_PATH + 1];
60 strcpyW(root, source_root);
61 PathStripToRootW(root);
62 PathAddBackslashW(root);
64 if (!GetVolumeInformationW(root, volume_name, MAX_PATH + 1,
65 NULL, NULL, NULL, NULL, 0))
67 ERR("Failed to get volume information\n");
71 return !strcmpiW( mi->volume_label, volume_name );
74 static UINT msi_change_media(MSIPACKAGE *package, MSIMEDIAINFO *mi)
76 LPWSTR error, error_dialog;
78 UINT r = ERROR_SUCCESS;
80 static const WCHAR error_prop[] = {'E','r','r','o','r','D','i','a','l','o','g',0};
82 if ((msi_get_property_int(package->db, szUILevel, 0) & INSTALLUILEVEL_MASK) ==
83 INSTALLUILEVEL_NONE && !gUIHandlerA && !gUIHandlerW && !gUIHandlerRecord)
86 error = msi_build_error_string(package, 1302, 1, mi->disk_prompt);
87 error_dialog = msi_dup_property(package->db, error_prop);
88 source_dir = msi_dup_property(package->db, szSourceDir);
90 while (r == ERROR_SUCCESS && !source_matches_volume(mi, source_dir))
92 r = msi_spawn_error_dialog(package, error_dialog, error);
96 gUIHandlerW(gUIContext, MB_RETRYCANCEL | INSTALLMESSAGE_ERROR, error);
100 char *msg = strdupWtoA(error);
101 gUIHandlerA(gUIContext, MB_RETRYCANCEL | INSTALLMESSAGE_ERROR, msg);
104 else if (gUIHandlerRecord)
106 MSIHANDLE rec = MsiCreateRecord(1);
107 MsiRecordSetStringW(rec, 0, error);
108 gUIHandlerRecord(gUIContext, MB_RETRYCANCEL | INSTALLMESSAGE_ERROR, rec);
114 msi_free(error_dialog);
115 msi_free(source_dir);
120 static MSICABINETSTREAM *msi_get_cabinet_stream( MSIPACKAGE *package, UINT disk_id )
122 MSICABINETSTREAM *cab;
124 LIST_FOR_EACH_ENTRY( cab, &package->cabinet_streams, MSICABINETSTREAM, entry )
126 if (cab->disk_id == disk_id) return cab;
131 static void * CDECL cabinet_alloc(ULONG cb)
133 return msi_alloc(cb);
136 static void CDECL cabinet_free(void *pv)
141 static INT_PTR CDECL cabinet_open(char *pszFile, int oflag, int pmode)
145 DWORD dwShareMode = 0;
146 DWORD dwCreateDisposition = OPEN_EXISTING;
148 switch (oflag & _O_ACCMODE)
151 dwAccess = GENERIC_READ;
152 dwShareMode = FILE_SHARE_READ | FILE_SHARE_DELETE;
155 dwAccess = GENERIC_WRITE;
156 dwShareMode = FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE;
159 dwAccess = GENERIC_READ | GENERIC_WRITE;
160 dwShareMode = FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE;
164 if ((oflag & (_O_CREAT | _O_EXCL)) == (_O_CREAT | _O_EXCL))
165 dwCreateDisposition = CREATE_NEW;
166 else if (oflag & _O_CREAT)
167 dwCreateDisposition = CREATE_ALWAYS;
169 handle = CreateFileA(pszFile, dwAccess, dwShareMode, NULL,
170 dwCreateDisposition, 0, NULL);
171 if (handle == INVALID_HANDLE_VALUE)
174 return (INT_PTR)handle;
177 static UINT CDECL cabinet_read(INT_PTR hf, void *pv, UINT cb)
179 HANDLE handle = (HANDLE)hf;
182 if (ReadFile(handle, pv, cb, &read, NULL))
188 static UINT CDECL cabinet_write(INT_PTR hf, void *pv, UINT cb)
190 HANDLE handle = (HANDLE)hf;
193 if (WriteFile(handle, pv, cb, &written, NULL))
199 static int CDECL cabinet_close(INT_PTR hf)
201 HANDLE handle = (HANDLE)hf;
202 return CloseHandle(handle) ? 0 : -1;
205 static LONG CDECL cabinet_seek(INT_PTR hf, LONG dist, int seektype)
207 HANDLE handle = (HANDLE)hf;
208 /* flags are compatible and so are passed straight through */
209 return SetFilePointer(handle, dist, NULL, seektype);
218 static struct package_disk package_disk;
220 static INT_PTR CDECL cabinet_open_stream( char *pszFile, int oflag, int pmode )
222 MSICABINETSTREAM *cab;
227 cab = msi_get_cabinet_stream( package_disk.package, package_disk.id );
230 WARN("failed to get cabinet stream\n");
233 if (!cab->stream[0] || !(encoded = encode_streamname( FALSE, cab->stream + 1 )))
235 WARN("failed to encode stream name\n");
238 if (msi_clone_open_stream( package_disk.package->db, cab->storage, encoded, &stream ) != ERROR_SUCCESS)
240 hr = IStorage_OpenStream( cab->storage, encoded, NULL, STGM_READ|STGM_SHARE_EXCLUSIVE, 0, &stream );
243 WARN("failed to open stream 0x%08x\n", hr);
249 return (INT_PTR)stream;
252 static UINT CDECL cabinet_read_stream( INT_PTR hf, void *pv, UINT cb )
254 IStream *stm = (IStream *)hf;
258 hr = IStream_Read( stm, pv, cb, &read );
259 if (hr == S_OK || hr == S_FALSE)
265 static int CDECL cabinet_close_stream( INT_PTR hf )
267 IStream *stm = (IStream *)hf;
268 IStream_Release( stm );
272 static LONG CDECL cabinet_seek_stream( INT_PTR hf, LONG dist, int seektype )
274 IStream *stm = (IStream *)hf;
276 ULARGE_INTEGER newpos;
279 move.QuadPart = dist;
280 hr = IStream_Seek( stm, move, seektype, &newpos );
283 if (newpos.QuadPart <= MAXLONG) return newpos.QuadPart;
289 static UINT CDECL msi_media_get_disk_info(MSIPACKAGE *package, MSIMEDIAINFO *mi)
293 static const WCHAR query[] = {
294 'S','E','L','E','C','T',' ','*',' ', 'F','R','O','M',' ',
295 '`','M','e','d','i','a','`',' ','W','H','E','R','E',' ',
296 '`','D','i','s','k','I','d','`',' ','=',' ','%','i',0};
298 row = MSI_QueryGetRecord(package->db, query, mi->disk_id);
301 TRACE("Unable to query row\n");
302 return ERROR_FUNCTION_FAILED;
305 mi->disk_prompt = strdupW(MSI_RecordGetString(row, 3));
306 mi->cabinet = strdupW(MSI_RecordGetString(row, 4));
307 mi->volume_label = strdupW(MSI_RecordGetString(row, 5));
309 if (!mi->first_volume)
310 mi->first_volume = strdupW(mi->volume_label);
312 msiobj_release(&row->hdr);
313 return ERROR_SUCCESS;
316 static INT_PTR cabinet_partial_file(FDINOTIFICATIONTYPE fdint,
317 PFDINOTIFICATION pfdin)
319 MSICABDATA *data = pfdin->pv;
320 data->mi->is_continuous = FALSE;
324 static WCHAR *get_cabinet_filename(MSIMEDIAINFO *mi)
329 len = strlenW(mi->sourcedir) + strlenW(mi->cabinet) + 1;
330 if (!(ret = msi_alloc(len * sizeof(WCHAR)))) return NULL;
331 strcpyW(ret, mi->sourcedir);
332 strcatW(ret, mi->cabinet);
336 static INT_PTR cabinet_next_cabinet(FDINOTIFICATIONTYPE fdint,
337 PFDINOTIFICATION pfdin)
339 MSICABDATA *data = pfdin->pv;
340 MSIMEDIAINFO *mi = data->mi;
341 LPWSTR cabinet_file = NULL, cab = strdupAtoW(pfdin->psz1);
345 msi_free(mi->disk_prompt);
346 msi_free(mi->cabinet);
347 msi_free(mi->volume_label);
348 mi->disk_prompt = NULL;
350 mi->volume_label = NULL;
353 mi->is_continuous = TRUE;
355 rc = msi_media_get_disk_info(data->package, mi);
356 if (rc != ERROR_SUCCESS)
358 ERR("Failed to get next cabinet information: %d\n", rc);
362 if (strcmpiW( mi->cabinet, cab ))
364 ERR("Continuous cabinet does not match the next cabinet in the Media table\n");
368 if (!(cabinet_file = get_cabinet_filename(mi)))
371 TRACE("Searching for %s\n", debugstr_w(cabinet_file));
374 if (GetFileAttributesW(cabinet_file) == INVALID_FILE_ATTRIBUTES)
376 if (msi_change_media(data->package, mi) != ERROR_SUCCESS)
382 msi_free(cabinet_file);
386 static INT_PTR cabinet_next_cabinet_stream( FDINOTIFICATIONTYPE fdint,
387 PFDINOTIFICATION pfdin )
389 MSICABDATA *data = pfdin->pv;
390 MSIMEDIAINFO *mi = data->mi;
393 msi_free( mi->disk_prompt );
394 msi_free( mi->cabinet );
395 msi_free( mi->volume_label );
396 mi->disk_prompt = NULL;
398 mi->volume_label = NULL;
401 mi->is_continuous = TRUE;
403 rc = msi_media_get_disk_info( data->package, mi );
404 if (rc != ERROR_SUCCESS)
406 ERR("Failed to get next cabinet information: %u\n", rc);
409 package_disk.id = mi->disk_id;
411 TRACE("next cabinet is %s disk id %u\n", debugstr_w(mi->cabinet), mi->disk_id);
415 static INT_PTR cabinet_copy_file(FDINOTIFICATIONTYPE fdint,
416 PFDINOTIFICATION pfdin)
418 MSICABDATA *data = pfdin->pv;
423 data->curfile = strdupAtoW(pfdin->psz1);
424 if (!data->cb(data->package, data->curfile, MSICABEXTRACT_BEGINEXTRACT, &path,
427 /* We're not extracting this file, so free the filename. */
428 msi_free(data->curfile);
429 data->curfile = NULL;
433 TRACE("extracting %s\n", debugstr_w(path));
435 attrs = attrs & (FILE_ATTRIBUTE_READONLY|FILE_ATTRIBUTE_HIDDEN|FILE_ATTRIBUTE_SYSTEM);
436 if (!attrs) attrs = FILE_ATTRIBUTE_NORMAL;
438 handle = CreateFileW(path, GENERIC_READ | GENERIC_WRITE, 0,
439 NULL, CREATE_ALWAYS, attrs, NULL);
440 if (handle == INVALID_HANDLE_VALUE)
442 DWORD err = GetLastError();
443 DWORD attrs2 = GetFileAttributesW(path);
445 if (attrs2 == INVALID_FILE_ATTRIBUTES)
447 ERR("failed to create %s (error %d)\n", debugstr_w(path), err);
450 else if (err == ERROR_ACCESS_DENIED && (attrs2 & FILE_ATTRIBUTE_READONLY))
452 TRACE("removing read-only attribute on %s\n", debugstr_w(path));
453 SetFileAttributesW( path, attrs2 & ~FILE_ATTRIBUTE_READONLY );
454 handle = CreateFileW(path, GENERIC_READ | GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, attrs2, NULL);
456 if (handle != INVALID_HANDLE_VALUE) goto done;
457 err = GetLastError();
459 if (err == ERROR_SHARING_VIOLATION || err == ERROR_USER_MAPPED_FILE)
461 WCHAR *tmpfileW, *tmppathW, *p;
464 TRACE("file in use, scheduling rename operation\n");
466 if (!(tmppathW = strdupW( path ))) return ERROR_OUTOFMEMORY;
467 if ((p = strrchrW(tmppathW, '\\'))) *p = 0;
468 len = strlenW( tmppathW ) + 16;
469 if (!(tmpfileW = msi_alloc(len * sizeof(WCHAR))))
471 msi_free( tmppathW );
472 return ERROR_OUTOFMEMORY;
474 if (!GetTempFileNameW(tmppathW, szMsi, 0, tmpfileW)) tmpfileW[0] = 0;
475 msi_free( tmppathW );
477 handle = CreateFileW(tmpfileW, GENERIC_READ | GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, attrs, NULL);
479 if (handle != INVALID_HANDLE_VALUE &&
480 MoveFileExW(path, NULL, MOVEFILE_DELAY_UNTIL_REBOOT) &&
481 MoveFileExW(tmpfileW, path, MOVEFILE_DELAY_UNTIL_REBOOT))
483 data->package->need_reboot = 1;
487 WARN("failed to schedule rename operation %s (error %d)\n", debugstr_w(path), GetLastError());
488 DeleteFileW( tmpfileW );
493 WARN("failed to create %s (error %d)\n", debugstr_w(path), err);
499 return (INT_PTR)handle;
502 static INT_PTR cabinet_close_file_info(FDINOTIFICATIONTYPE fdint,
503 PFDINOTIFICATION pfdin)
505 MSICABDATA *data = pfdin->pv;
508 HANDLE handle = (HANDLE)pfdin->hf;
510 data->mi->is_continuous = FALSE;
512 if (!DosDateTimeToFileTime(pfdin->date, pfdin->time, &ft))
514 if (!LocalFileTimeToFileTime(&ft, &ftLocal))
516 if (!SetFileTime(handle, &ftLocal, 0, &ftLocal))
521 data->cb(data->package, data->curfile, MSICABEXTRACT_FILEEXTRACTED, NULL, NULL,
524 msi_free(data->curfile);
525 data->curfile = NULL;
530 static INT_PTR CDECL cabinet_notify(FDINOTIFICATIONTYPE fdint, PFDINOTIFICATION pfdin)
534 case fdintPARTIAL_FILE:
535 return cabinet_partial_file(fdint, pfdin);
537 case fdintNEXT_CABINET:
538 return cabinet_next_cabinet(fdint, pfdin);
541 return cabinet_copy_file(fdint, pfdin);
543 case fdintCLOSE_FILE_INFO:
544 return cabinet_close_file_info(fdint, pfdin);
551 static INT_PTR CDECL cabinet_notify_stream( FDINOTIFICATIONTYPE fdint, PFDINOTIFICATION pfdin )
555 case fdintPARTIAL_FILE:
556 return cabinet_partial_file( fdint, pfdin );
558 case fdintNEXT_CABINET:
559 return cabinet_next_cabinet_stream( fdint, pfdin );
562 return cabinet_copy_file( fdint, pfdin );
564 case fdintCLOSE_FILE_INFO:
565 return cabinet_close_file_info( fdint, pfdin );
567 case fdintCABINET_INFO:
571 ERR("Unexpected notification %d\n", fdint);
576 static BOOL extract_cabinet( MSIPACKAGE* package, MSIMEDIAINFO *mi, LPVOID data )
578 LPSTR cabinet, cab_path = NULL;
583 TRACE("extracting %s disk id %u\n", debugstr_w(mi->cabinet), mi->disk_id);
585 hfdi = FDICreate( cabinet_alloc, cabinet_free, cabinet_open, cabinet_read,
586 cabinet_write, cabinet_close, cabinet_seek, 0, &erf );
589 ERR("FDICreate failed\n");
593 cabinet = strdupWtoA( mi->cabinet );
597 cab_path = strdupWtoA( mi->sourcedir );
601 ret = FDICopy( hfdi, cabinet, cab_path, 0, cabinet_notify, NULL, data );
603 ERR("FDICopy failed\n");
608 msi_free( cab_path );
611 mi->is_extracted = TRUE;
616 static BOOL extract_cabinet_stream( MSIPACKAGE *package, MSIMEDIAINFO *mi, LPVOID data )
618 static char filename[] = {'<','S','T','R','E','A','M','>',0};
623 TRACE("extracting %s disk id %u\n", debugstr_w(mi->cabinet), mi->disk_id);
625 hfdi = FDICreate( cabinet_alloc, cabinet_free, cabinet_open_stream, cabinet_read_stream,
626 cabinet_write, cabinet_close_stream, cabinet_seek_stream, 0, &erf );
629 ERR("FDICreate failed\n");
633 package_disk.package = package;
634 package_disk.id = mi->disk_id;
636 ret = FDICopy( hfdi, filename, NULL, 0, cabinet_notify_stream, NULL, data );
637 if (!ret) ERR("FDICopy failed\n");
640 if (ret) mi->is_extracted = TRUE;
644 /***********************************************************************
647 * Extract files from a cabinet file or stream.
649 BOOL msi_cabextract(MSIPACKAGE* package, MSIMEDIAINFO *mi, LPVOID data)
651 if (mi->cabinet[0] == '#')
653 return extract_cabinet_stream( package, mi, data );
655 return extract_cabinet( package, mi, data );
658 void msi_free_media_info(MSIMEDIAINFO *mi)
660 msi_free(mi->disk_prompt);
661 msi_free(mi->cabinet);
662 msi_free(mi->volume_label);
663 msi_free(mi->first_volume);
667 static UINT get_drive_type(const WCHAR *path)
669 WCHAR root[MAX_PATH + 1];
672 PathStripToRootW(root);
673 PathAddBackslashW(root);
675 return GetDriveTypeW(root);
678 UINT msi_load_media_info(MSIPACKAGE *package, UINT Sequence, MSIMEDIAINFO *mi)
680 static const WCHAR query[] = {
681 'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ','`','M','e','d','i','a','`',' ',
682 'W','H','E','R','E',' ','`','L','a','s','t','S','e','q','u','e','n','c','e','`',' ',
683 '>','=',' ','%','i',' ','O','R','D','E','R',' ','B','Y',' ','`','D','i','s','k','I','d','`',0};
685 LPWSTR source_dir, source;
688 if (Sequence <= mi->last_sequence) /* already loaded */
689 return ERROR_SUCCESS;
691 row = MSI_QueryGetRecord(package->db, query, Sequence);
694 TRACE("Unable to query row\n");
695 return ERROR_FUNCTION_FAILED;
698 mi->is_extracted = FALSE;
699 mi->disk_id = MSI_RecordGetInteger(row, 1);
700 mi->last_sequence = MSI_RecordGetInteger(row, 2);
701 msi_free(mi->disk_prompt);
702 mi->disk_prompt = strdupW(MSI_RecordGetString(row, 3));
703 msi_free(mi->cabinet);
704 mi->cabinet = strdupW(MSI_RecordGetString(row, 4));
705 msi_free(mi->volume_label);
706 mi->volume_label = strdupW(MSI_RecordGetString(row, 5));
707 msiobj_release(&row->hdr);
709 if (!mi->first_volume)
710 mi->first_volume = strdupW(mi->volume_label);
712 msi_set_sourcedir_props(package, FALSE);
713 source_dir = msi_dup_property(package->db, szSourceDir);
714 lstrcpyW(mi->sourcedir, source_dir);
715 PathAddBackslashW(mi->sourcedir);
716 mi->type = get_drive_type(source_dir);
718 options = MSICODE_PRODUCT;
719 if (mi->type == DRIVE_CDROM || mi->type == DRIVE_REMOVABLE)
722 options |= MSISOURCETYPE_MEDIA;
724 else if (package->BaseURL && UrlIsW(package->BaseURL, URLIS_URL))
726 source = package->BaseURL;
727 options |= MSISOURCETYPE_URL;
731 source = mi->sourcedir;
732 options |= MSISOURCETYPE_NETWORK;
735 msi_package_add_media_disk(package, package->Context,
736 MSICODE_PRODUCT, mi->disk_id,
737 mi->volume_label, mi->disk_prompt);
739 msi_package_add_info(package, package->Context,
740 options, INSTALLPROPERTY_LASTUSEDSOURCEW, source);
742 msi_free(source_dir);
743 TRACE("sequence %u -> cabinet %s disk id %u\n", Sequence, debugstr_w(mi->cabinet), mi->disk_id);
744 return ERROR_SUCCESS;
747 /* FIXME: search URL sources as well */
748 static UINT find_published_source(MSIPACKAGE *package, MSIMEDIAINFO *mi)
750 WCHAR source[MAX_PATH];
751 WCHAR volume[MAX_PATH];
752 WCHAR prompt[MAX_PATH];
753 DWORD volumesz, promptsz;
754 DWORD index, size, id;
759 r = MsiSourceListGetInfoW(package->ProductCode, NULL,
760 package->Context, MSICODE_PRODUCT,
761 INSTALLPROPERTY_LASTUSEDTYPEW, last_type, &size);
762 if (r != ERROR_SUCCESS)
766 r = MsiSourceListGetInfoW(package->ProductCode, NULL,
767 package->Context, MSICODE_PRODUCT,
768 INSTALLPROPERTY_LASTUSEDSOURCEW, source, &size);
769 if (r != ERROR_SUCCESS)
772 if (last_type[0] == 'n')
774 WCHAR cabinet_file[MAX_PATH];
775 BOOL check_all = FALSE;
781 while (MsiSourceListEnumSourcesW(package->ProductCode, NULL,
783 MSISOURCETYPE_NETWORK, index++,
784 volume, &volumesz) == ERROR_SUCCESS)
786 if (check_all || !strncmpiW(source, volume, strlenW(source)))
788 lstrcpyW(cabinet_file, volume);
789 PathAddBackslashW(cabinet_file);
790 lstrcatW(cabinet_file, mi->cabinet);
792 if (GetFileAttributesW(cabinet_file) == INVALID_FILE_ATTRIBUTES)
800 lstrcpyW(mi->sourcedir, volume);
801 PathAddBackslashW(mi->sourcedir);
802 TRACE("Found network source %s\n", debugstr_w(mi->sourcedir));
803 return ERROR_SUCCESS;
817 while (MsiSourceListEnumMediaDisksW(package->ProductCode, NULL,
819 MSICODE_PRODUCT, index++, &id,
820 volume, &volumesz, prompt, &promptsz) == ERROR_SUCCESS)
823 msi_free( mi->volume_label );
824 if (!(mi->volume_label = msi_alloc( ++volumesz * sizeof(WCHAR) ))) return ERROR_OUTOFMEMORY;
825 strcpyW( mi->volume_label, volume );
827 msi_free( mi->disk_prompt );
828 if (!(mi->disk_prompt = msi_alloc( ++promptsz * sizeof(WCHAR) ))) return ERROR_OUTOFMEMORY;
829 strcpyW( mi->disk_prompt, prompt );
831 if (source_matches_volume(mi, source))
833 /* FIXME: what about SourceDir */
834 lstrcpyW(mi->sourcedir, source);
835 PathAddBackslashW(mi->sourcedir);
836 TRACE("Found disk source %s\n", debugstr_w(mi->sourcedir));
837 return ERROR_SUCCESS;
841 return ERROR_FUNCTION_FAILED;
844 UINT ready_media( MSIPACKAGE *package, BOOL compressed, MSIMEDIAINFO *mi )
847 WCHAR *cabinet_file = NULL;
849 /* media info for continuous cabinet is already loaded */
850 if (mi->is_continuous) return ERROR_SUCCESS;
854 /* cabinet is internal, no checks needed */
855 if (mi->cabinet[0] == '#') return ERROR_SUCCESS;
857 if (!(cabinet_file = get_cabinet_filename( mi ))) return ERROR_OUTOFMEMORY;
859 /* package should be downloaded */
860 if (compressed && GetFileAttributesW( cabinet_file ) == INVALID_FILE_ATTRIBUTES &&
861 package->BaseURL && UrlIsW( package->BaseURL, URLIS_URL ))
863 WCHAR temppath[MAX_PATH], *p;
865 if ((rc = msi_download_file( cabinet_file, temppath )) != ERROR_SUCCESS)
867 ERR("failed to download %s (%u)\n", debugstr_w(cabinet_file), rc);
868 msi_free( cabinet_file );
871 if ((p = strrchrW( temppath, '\\' ))) *p = 0;
872 strcpyW( mi->sourcedir, temppath );
873 PathAddBackslashW( mi->sourcedir );
874 msi_free( mi->cabinet );
875 mi->cabinet = strdupW( p + 1 );
876 msi_free( cabinet_file );
877 return ERROR_SUCCESS;
880 /* check volume matches, change media if not */
881 if (mi->volume_label && mi->disk_id > 1 && strcmpW( mi->first_volume, mi->volume_label ))
883 WCHAR *source = msi_dup_property( package->db, szSourceDir );
884 BOOL match = source_matches_volume( mi, source );
887 if (!match && (mi->type == DRIVE_CDROM || mi->type == DRIVE_REMOVABLE))
889 if ((rc = msi_change_media( package, mi )) != ERROR_SUCCESS)
891 msi_free( cabinet_file );
898 if (compressed && GetFileAttributesW( cabinet_file ) == INVALID_FILE_ATTRIBUTES)
900 if ((rc = find_published_source( package, mi )) != ERROR_SUCCESS)
902 ERR("cabinet not found: %s\n", debugstr_w(cabinet_file));
903 msi_free( cabinet_file );
904 return ERROR_INSTALL_FAILURE;
908 msi_free( cabinet_file );
909 return ERROR_SUCCESS;
912 UINT msi_add_cabinet_stream( MSIPACKAGE *package, UINT disk_id, IStorage *storage, const WCHAR *name )
914 MSICABINETSTREAM *cab, *item;
916 TRACE("%p, %u, %p, %s\n", package, disk_id, storage, debugstr_w(name));
918 LIST_FOR_EACH_ENTRY( item, &package->cabinet_streams, MSICABINETSTREAM, entry )
920 if (item->disk_id == disk_id)
922 TRACE("duplicate disk id %u\n", disk_id);
923 return ERROR_FUNCTION_FAILED;
926 if (!(cab = msi_alloc( sizeof(*cab) ))) return ERROR_OUTOFMEMORY;
927 if (!(cab->stream = msi_alloc( (strlenW( name ) + 1) * sizeof(WCHAR ) )))
930 return ERROR_OUTOFMEMORY;
932 strcpyW( cab->stream, name );
933 cab->disk_id = disk_id;
934 cab->storage = storage;
935 IStorage_AddRef( storage );
936 list_add_tail( &package->cabinet_streams, &cab->entry );
938 return ERROR_SUCCESS;