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, NULL, NULL, NULL, NULL, 0))
66 WARN("failed to get volume information for %s (%u)\n", debugstr_w(root), GetLastError());
69 return !strcmpiW( mi->volume_label, volume_name );
72 static UINT msi_change_media(MSIPACKAGE *package, MSIMEDIAINFO *mi)
74 LPWSTR error, error_dialog;
76 UINT r = ERROR_SUCCESS;
78 static const WCHAR error_prop[] = {'E','r','r','o','r','D','i','a','l','o','g',0};
80 if ((msi_get_property_int(package->db, szUILevel, 0) & INSTALLUILEVEL_MASK) ==
81 INSTALLUILEVEL_NONE && !gUIHandlerA && !gUIHandlerW && !gUIHandlerRecord)
84 error = msi_build_error_string(package, 1302, 1, mi->disk_prompt);
85 error_dialog = msi_dup_property(package->db, error_prop);
86 source_dir = msi_dup_property(package->db, szSourceDir);
88 while (r == ERROR_SUCCESS && !source_matches_volume(mi, source_dir))
90 r = msi_spawn_error_dialog(package, error_dialog, error);
94 gUIHandlerW(gUIContext, MB_RETRYCANCEL | INSTALLMESSAGE_ERROR, error);
98 char *msg = strdupWtoA(error);
99 gUIHandlerA(gUIContext, MB_RETRYCANCEL | INSTALLMESSAGE_ERROR, msg);
102 else if (gUIHandlerRecord)
104 MSIHANDLE rec = MsiCreateRecord(1);
105 MsiRecordSetStringW(rec, 0, error);
106 gUIHandlerRecord(gUIContext, MB_RETRYCANCEL | INSTALLMESSAGE_ERROR, rec);
112 msi_free(error_dialog);
113 msi_free(source_dir);
118 static MSICABINETSTREAM *msi_get_cabinet_stream( MSIPACKAGE *package, UINT disk_id )
120 MSICABINETSTREAM *cab;
122 LIST_FOR_EACH_ENTRY( cab, &package->cabinet_streams, MSICABINETSTREAM, entry )
124 if (cab->disk_id == disk_id) return cab;
129 static void * CDECL cabinet_alloc(ULONG cb)
131 return msi_alloc(cb);
134 static void CDECL cabinet_free(void *pv)
139 static INT_PTR CDECL cabinet_open(char *pszFile, int oflag, int pmode)
143 DWORD dwShareMode = 0;
144 DWORD dwCreateDisposition = OPEN_EXISTING;
146 switch (oflag & _O_ACCMODE)
149 dwAccess = GENERIC_READ;
150 dwShareMode = FILE_SHARE_READ | FILE_SHARE_DELETE;
153 dwAccess = GENERIC_WRITE;
154 dwShareMode = FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE;
157 dwAccess = GENERIC_READ | GENERIC_WRITE;
158 dwShareMode = FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE;
162 if ((oflag & (_O_CREAT | _O_EXCL)) == (_O_CREAT | _O_EXCL))
163 dwCreateDisposition = CREATE_NEW;
164 else if (oflag & _O_CREAT)
165 dwCreateDisposition = CREATE_ALWAYS;
167 handle = CreateFileA(pszFile, dwAccess, dwShareMode, NULL,
168 dwCreateDisposition, 0, NULL);
169 if (handle == INVALID_HANDLE_VALUE)
172 return (INT_PTR)handle;
175 static UINT CDECL cabinet_read(INT_PTR hf, void *pv, UINT cb)
177 HANDLE handle = (HANDLE)hf;
180 if (ReadFile(handle, pv, cb, &read, NULL))
186 static UINT CDECL cabinet_write(INT_PTR hf, void *pv, UINT cb)
188 HANDLE handle = (HANDLE)hf;
191 if (WriteFile(handle, pv, cb, &written, NULL))
197 static int CDECL cabinet_close(INT_PTR hf)
199 HANDLE handle = (HANDLE)hf;
200 return CloseHandle(handle) ? 0 : -1;
203 static LONG CDECL cabinet_seek(INT_PTR hf, LONG dist, int seektype)
205 HANDLE handle = (HANDLE)hf;
206 /* flags are compatible and so are passed straight through */
207 return SetFilePointer(handle, dist, NULL, seektype);
216 static struct package_disk package_disk;
218 static INT_PTR CDECL cabinet_open_stream( char *pszFile, int oflag, int pmode )
220 MSICABINETSTREAM *cab;
225 cab = msi_get_cabinet_stream( package_disk.package, package_disk.id );
228 WARN("failed to get cabinet stream\n");
231 if (!cab->stream[0] || !(encoded = encode_streamname( FALSE, cab->stream + 1 )))
233 WARN("failed to encode stream name\n");
236 if (msi_clone_open_stream( package_disk.package->db, cab->storage, encoded, &stream ) != ERROR_SUCCESS)
238 hr = IStorage_OpenStream( cab->storage, encoded, NULL, STGM_READ|STGM_SHARE_EXCLUSIVE, 0, &stream );
241 WARN("failed to open stream 0x%08x\n", hr);
247 return (INT_PTR)stream;
250 static UINT CDECL cabinet_read_stream( INT_PTR hf, void *pv, UINT cb )
252 IStream *stm = (IStream *)hf;
256 hr = IStream_Read( stm, pv, cb, &read );
257 if (hr == S_OK || hr == S_FALSE)
263 static int CDECL cabinet_close_stream( INT_PTR hf )
265 IStream *stm = (IStream *)hf;
266 IStream_Release( stm );
270 static LONG CDECL cabinet_seek_stream( INT_PTR hf, LONG dist, int seektype )
272 IStream *stm = (IStream *)hf;
274 ULARGE_INTEGER newpos;
277 move.QuadPart = dist;
278 hr = IStream_Seek( stm, move, seektype, &newpos );
281 if (newpos.QuadPart <= MAXLONG) return newpos.QuadPart;
287 static UINT CDECL msi_media_get_disk_info(MSIPACKAGE *package, MSIMEDIAINFO *mi)
291 static const WCHAR query[] = {
292 'S','E','L','E','C','T',' ','*',' ', 'F','R','O','M',' ',
293 '`','M','e','d','i','a','`',' ','W','H','E','R','E',' ',
294 '`','D','i','s','k','I','d','`',' ','=',' ','%','i',0};
296 row = MSI_QueryGetRecord(package->db, query, mi->disk_id);
299 TRACE("Unable to query row\n");
300 return ERROR_FUNCTION_FAILED;
303 mi->disk_prompt = strdupW(MSI_RecordGetString(row, 3));
304 mi->cabinet = strdupW(MSI_RecordGetString(row, 4));
305 mi->volume_label = strdupW(MSI_RecordGetString(row, 5));
307 if (!mi->first_volume)
308 mi->first_volume = strdupW(mi->volume_label);
310 msiobj_release(&row->hdr);
311 return ERROR_SUCCESS;
314 static INT_PTR cabinet_partial_file(FDINOTIFICATIONTYPE fdint,
315 PFDINOTIFICATION pfdin)
317 MSICABDATA *data = pfdin->pv;
318 data->mi->is_continuous = FALSE;
322 static WCHAR *get_cabinet_filename(MSIMEDIAINFO *mi)
327 len = strlenW(mi->sourcedir) + strlenW(mi->cabinet) + 1;
328 if (!(ret = msi_alloc(len * sizeof(WCHAR)))) return NULL;
329 strcpyW(ret, mi->sourcedir);
330 strcatW(ret, mi->cabinet);
334 static INT_PTR cabinet_next_cabinet(FDINOTIFICATIONTYPE fdint,
335 PFDINOTIFICATION pfdin)
337 MSICABDATA *data = pfdin->pv;
338 MSIMEDIAINFO *mi = data->mi;
339 LPWSTR cabinet_file = NULL, cab = strdupAtoW(pfdin->psz1);
343 msi_free(mi->disk_prompt);
344 msi_free(mi->cabinet);
345 msi_free(mi->volume_label);
346 mi->disk_prompt = NULL;
348 mi->volume_label = NULL;
351 mi->is_continuous = TRUE;
353 rc = msi_media_get_disk_info(data->package, mi);
354 if (rc != ERROR_SUCCESS)
356 ERR("Failed to get next cabinet information: %d\n", rc);
360 if (strcmpiW( mi->cabinet, cab ))
362 ERR("Continuous cabinet does not match the next cabinet in the Media table\n");
366 if (!(cabinet_file = get_cabinet_filename(mi)))
369 TRACE("Searching for %s\n", debugstr_w(cabinet_file));
372 if (GetFileAttributesW(cabinet_file) == INVALID_FILE_ATTRIBUTES)
374 if (msi_change_media(data->package, mi) != ERROR_SUCCESS)
380 msi_free(cabinet_file);
384 static INT_PTR cabinet_next_cabinet_stream( FDINOTIFICATIONTYPE fdint,
385 PFDINOTIFICATION pfdin )
387 MSICABDATA *data = pfdin->pv;
388 MSIMEDIAINFO *mi = data->mi;
391 msi_free( mi->disk_prompt );
392 msi_free( mi->cabinet );
393 msi_free( mi->volume_label );
394 mi->disk_prompt = NULL;
396 mi->volume_label = NULL;
399 mi->is_continuous = TRUE;
401 rc = msi_media_get_disk_info( data->package, mi );
402 if (rc != ERROR_SUCCESS)
404 ERR("Failed to get next cabinet information: %u\n", rc);
407 package_disk.id = mi->disk_id;
409 TRACE("next cabinet is %s disk id %u\n", debugstr_w(mi->cabinet), mi->disk_id);
413 static INT_PTR cabinet_copy_file(FDINOTIFICATIONTYPE fdint,
414 PFDINOTIFICATION pfdin)
416 MSICABDATA *data = pfdin->pv;
421 data->curfile = strdupAtoW(pfdin->psz1);
422 if (!data->cb(data->package, data->curfile, MSICABEXTRACT_BEGINEXTRACT, &path,
425 /* We're not extracting this file, so free the filename. */
426 msi_free(data->curfile);
427 data->curfile = NULL;
431 TRACE("extracting %s\n", debugstr_w(path));
433 attrs = attrs & (FILE_ATTRIBUTE_READONLY|FILE_ATTRIBUTE_HIDDEN|FILE_ATTRIBUTE_SYSTEM);
434 if (!attrs) attrs = FILE_ATTRIBUTE_NORMAL;
436 handle = CreateFileW(path, GENERIC_READ | GENERIC_WRITE, 0,
437 NULL, CREATE_ALWAYS, attrs, NULL);
438 if (handle == INVALID_HANDLE_VALUE)
440 DWORD err = GetLastError();
441 DWORD attrs2 = GetFileAttributesW(path);
443 if (attrs2 == INVALID_FILE_ATTRIBUTES)
445 ERR("failed to create %s (error %d)\n", debugstr_w(path), err);
448 else if (err == ERROR_ACCESS_DENIED && (attrs2 & FILE_ATTRIBUTE_READONLY))
450 TRACE("removing read-only attribute on %s\n", debugstr_w(path));
451 SetFileAttributesW( path, attrs2 & ~FILE_ATTRIBUTE_READONLY );
452 handle = CreateFileW(path, GENERIC_READ | GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, attrs2, NULL);
454 if (handle != INVALID_HANDLE_VALUE) goto done;
455 err = GetLastError();
457 if (err == ERROR_SHARING_VIOLATION || err == ERROR_USER_MAPPED_FILE)
459 WCHAR *tmpfileW, *tmppathW, *p;
462 TRACE("file in use, scheduling rename operation\n");
464 if (!(tmppathW = strdupW( path ))) return ERROR_OUTOFMEMORY;
465 if ((p = strrchrW(tmppathW, '\\'))) *p = 0;
466 len = strlenW( tmppathW ) + 16;
467 if (!(tmpfileW = msi_alloc(len * sizeof(WCHAR))))
469 msi_free( tmppathW );
470 return ERROR_OUTOFMEMORY;
472 if (!GetTempFileNameW(tmppathW, szMsi, 0, tmpfileW)) tmpfileW[0] = 0;
473 msi_free( tmppathW );
475 handle = CreateFileW(tmpfileW, GENERIC_READ | GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, attrs, NULL);
477 if (handle != INVALID_HANDLE_VALUE &&
478 MoveFileExW(path, NULL, MOVEFILE_DELAY_UNTIL_REBOOT) &&
479 MoveFileExW(tmpfileW, path, MOVEFILE_DELAY_UNTIL_REBOOT))
481 data->package->need_reboot = 1;
485 WARN("failed to schedule rename operation %s (error %d)\n", debugstr_w(path), GetLastError());
486 DeleteFileW( tmpfileW );
491 WARN("failed to create %s (error %d)\n", debugstr_w(path), err);
497 return (INT_PTR)handle;
500 static INT_PTR cabinet_close_file_info(FDINOTIFICATIONTYPE fdint,
501 PFDINOTIFICATION pfdin)
503 MSICABDATA *data = pfdin->pv;
506 HANDLE handle = (HANDLE)pfdin->hf;
508 data->mi->is_continuous = FALSE;
510 if (!DosDateTimeToFileTime(pfdin->date, pfdin->time, &ft))
512 if (!LocalFileTimeToFileTime(&ft, &ftLocal))
514 if (!SetFileTime(handle, &ftLocal, 0, &ftLocal))
519 data->cb(data->package, data->curfile, MSICABEXTRACT_FILEEXTRACTED, NULL, NULL,
522 msi_free(data->curfile);
523 data->curfile = NULL;
528 static INT_PTR CDECL cabinet_notify(FDINOTIFICATIONTYPE fdint, PFDINOTIFICATION pfdin)
532 case fdintPARTIAL_FILE:
533 return cabinet_partial_file(fdint, pfdin);
535 case fdintNEXT_CABINET:
536 return cabinet_next_cabinet(fdint, pfdin);
539 return cabinet_copy_file(fdint, pfdin);
541 case fdintCLOSE_FILE_INFO:
542 return cabinet_close_file_info(fdint, pfdin);
549 static INT_PTR CDECL cabinet_notify_stream( FDINOTIFICATIONTYPE fdint, PFDINOTIFICATION pfdin )
553 case fdintPARTIAL_FILE:
554 return cabinet_partial_file( fdint, pfdin );
556 case fdintNEXT_CABINET:
557 return cabinet_next_cabinet_stream( fdint, pfdin );
560 return cabinet_copy_file( fdint, pfdin );
562 case fdintCLOSE_FILE_INFO:
563 return cabinet_close_file_info( fdint, pfdin );
565 case fdintCABINET_INFO:
569 ERR("Unexpected notification %d\n", fdint);
574 static BOOL extract_cabinet( MSIPACKAGE* package, MSIMEDIAINFO *mi, LPVOID data )
576 LPSTR cabinet, cab_path = NULL;
581 TRACE("extracting %s disk id %u\n", debugstr_w(mi->cabinet), mi->disk_id);
583 hfdi = FDICreate( cabinet_alloc, cabinet_free, cabinet_open, cabinet_read,
584 cabinet_write, cabinet_close, cabinet_seek, 0, &erf );
587 ERR("FDICreate failed\n");
591 cabinet = strdupWtoA( mi->cabinet );
595 cab_path = strdupWtoA( mi->sourcedir );
599 ret = FDICopy( hfdi, cabinet, cab_path, 0, cabinet_notify, NULL, data );
601 ERR("FDICopy failed\n");
606 msi_free( cab_path );
609 mi->is_extracted = TRUE;
614 static BOOL extract_cabinet_stream( MSIPACKAGE *package, MSIMEDIAINFO *mi, LPVOID data )
616 static char filename[] = {'<','S','T','R','E','A','M','>',0};
621 TRACE("extracting %s disk id %u\n", debugstr_w(mi->cabinet), mi->disk_id);
623 hfdi = FDICreate( cabinet_alloc, cabinet_free, cabinet_open_stream, cabinet_read_stream,
624 cabinet_write, cabinet_close_stream, cabinet_seek_stream, 0, &erf );
627 ERR("FDICreate failed\n");
631 package_disk.package = package;
632 package_disk.id = mi->disk_id;
634 ret = FDICopy( hfdi, filename, NULL, 0, cabinet_notify_stream, NULL, data );
635 if (!ret) ERR("FDICopy failed\n");
638 if (ret) mi->is_extracted = TRUE;
642 /***********************************************************************
645 * Extract files from a cabinet file or stream.
647 BOOL msi_cabextract(MSIPACKAGE* package, MSIMEDIAINFO *mi, LPVOID data)
649 if (mi->cabinet[0] == '#')
651 return extract_cabinet_stream( package, mi, data );
653 return extract_cabinet( package, mi, data );
656 void msi_free_media_info(MSIMEDIAINFO *mi)
658 msi_free(mi->disk_prompt);
659 msi_free(mi->cabinet);
660 msi_free(mi->volume_label);
661 msi_free(mi->first_volume);
665 static UINT get_drive_type(const WCHAR *path)
667 WCHAR root[MAX_PATH + 1];
670 PathStripToRootW(root);
671 PathAddBackslashW(root);
673 return GetDriveTypeW(root);
676 UINT msi_load_media_info(MSIPACKAGE *package, UINT Sequence, MSIMEDIAINFO *mi)
678 static const WCHAR query[] = {
679 'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ','`','M','e','d','i','a','`',' ',
680 'W','H','E','R','E',' ','`','L','a','s','t','S','e','q','u','e','n','c','e','`',' ',
681 '>','=',' ','%','i',' ','O','R','D','E','R',' ','B','Y',' ','`','D','i','s','k','I','d','`',0};
683 LPWSTR source_dir, source;
686 if (Sequence <= mi->last_sequence) /* already loaded */
687 return ERROR_SUCCESS;
689 row = MSI_QueryGetRecord(package->db, query, Sequence);
692 TRACE("Unable to query row\n");
693 return ERROR_FUNCTION_FAILED;
696 mi->is_extracted = FALSE;
697 mi->disk_id = MSI_RecordGetInteger(row, 1);
698 mi->last_sequence = MSI_RecordGetInteger(row, 2);
699 msi_free(mi->disk_prompt);
700 mi->disk_prompt = strdupW(MSI_RecordGetString(row, 3));
701 msi_free(mi->cabinet);
702 mi->cabinet = strdupW(MSI_RecordGetString(row, 4));
703 msi_free(mi->volume_label);
704 mi->volume_label = strdupW(MSI_RecordGetString(row, 5));
705 msiobj_release(&row->hdr);
707 if (!mi->first_volume)
708 mi->first_volume = strdupW(mi->volume_label);
710 msi_set_sourcedir_props(package, FALSE);
711 source_dir = msi_dup_property(package->db, szSourceDir);
712 lstrcpyW(mi->sourcedir, source_dir);
713 PathAddBackslashW(mi->sourcedir);
714 mi->type = get_drive_type(source_dir);
716 options = MSICODE_PRODUCT;
717 if (mi->type == DRIVE_CDROM || mi->type == DRIVE_REMOVABLE)
720 options |= MSISOURCETYPE_MEDIA;
722 else if (package->BaseURL && UrlIsW(package->BaseURL, URLIS_URL))
724 source = package->BaseURL;
725 options |= MSISOURCETYPE_URL;
729 source = mi->sourcedir;
730 options |= MSISOURCETYPE_NETWORK;
733 msi_package_add_media_disk(package, package->Context,
734 MSICODE_PRODUCT, mi->disk_id,
735 mi->volume_label, mi->disk_prompt);
737 msi_package_add_info(package, package->Context,
738 options, INSTALLPROPERTY_LASTUSEDSOURCEW, source);
740 msi_free(source_dir);
741 TRACE("sequence %u -> cabinet %s disk id %u\n", Sequence, debugstr_w(mi->cabinet), mi->disk_id);
742 return ERROR_SUCCESS;
745 /* FIXME: search URL sources as well */
746 static UINT find_published_source(MSIPACKAGE *package, MSIMEDIAINFO *mi)
748 WCHAR source[MAX_PATH];
749 WCHAR volume[MAX_PATH];
750 WCHAR prompt[MAX_PATH];
751 DWORD volumesz, promptsz;
752 DWORD index, size, id;
757 r = MsiSourceListGetInfoW(package->ProductCode, NULL,
758 package->Context, MSICODE_PRODUCT,
759 INSTALLPROPERTY_LASTUSEDTYPEW, last_type, &size);
760 if (r != ERROR_SUCCESS)
764 r = MsiSourceListGetInfoW(package->ProductCode, NULL,
765 package->Context, MSICODE_PRODUCT,
766 INSTALLPROPERTY_LASTUSEDSOURCEW, source, &size);
767 if (r != ERROR_SUCCESS)
770 if (last_type[0] == 'n')
772 WCHAR cabinet_file[MAX_PATH];
773 BOOL check_all = FALSE;
779 while (MsiSourceListEnumSourcesW(package->ProductCode, NULL,
781 MSISOURCETYPE_NETWORK, index++,
782 volume, &volumesz) == ERROR_SUCCESS)
784 if (check_all || !strncmpiW(source, volume, strlenW(source)))
786 lstrcpyW(cabinet_file, volume);
787 PathAddBackslashW(cabinet_file);
788 lstrcatW(cabinet_file, mi->cabinet);
790 if (GetFileAttributesW(cabinet_file) == INVALID_FILE_ATTRIBUTES)
798 lstrcpyW(mi->sourcedir, volume);
799 PathAddBackslashW(mi->sourcedir);
800 TRACE("Found network source %s\n", debugstr_w(mi->sourcedir));
801 return ERROR_SUCCESS;
815 while (MsiSourceListEnumMediaDisksW(package->ProductCode, NULL,
817 MSICODE_PRODUCT, index++, &id,
818 volume, &volumesz, prompt, &promptsz) == ERROR_SUCCESS)
821 msi_free( mi->volume_label );
822 if (!(mi->volume_label = msi_alloc( ++volumesz * sizeof(WCHAR) ))) return ERROR_OUTOFMEMORY;
823 strcpyW( mi->volume_label, volume );
825 msi_free( mi->disk_prompt );
826 if (!(mi->disk_prompt = msi_alloc( ++promptsz * sizeof(WCHAR) ))) return ERROR_OUTOFMEMORY;
827 strcpyW( mi->disk_prompt, prompt );
829 if (source_matches_volume(mi, source))
831 /* FIXME: what about SourceDir */
832 lstrcpyW(mi->sourcedir, source);
833 PathAddBackslashW(mi->sourcedir);
834 TRACE("Found disk source %s\n", debugstr_w(mi->sourcedir));
835 return ERROR_SUCCESS;
839 return ERROR_FUNCTION_FAILED;
842 UINT ready_media( MSIPACKAGE *package, BOOL compressed, MSIMEDIAINFO *mi )
845 WCHAR *cabinet_file = NULL;
847 /* media info for continuous cabinet is already loaded */
848 if (mi->is_continuous) return ERROR_SUCCESS;
852 /* cabinet is internal, no checks needed */
853 if (mi->cabinet[0] == '#') return ERROR_SUCCESS;
855 if (!(cabinet_file = get_cabinet_filename( mi ))) return ERROR_OUTOFMEMORY;
857 /* package should be downloaded */
858 if (compressed && GetFileAttributesW( cabinet_file ) == INVALID_FILE_ATTRIBUTES &&
859 package->BaseURL && UrlIsW( package->BaseURL, URLIS_URL ))
861 WCHAR temppath[MAX_PATH], *p;
863 if ((rc = msi_download_file( cabinet_file, temppath )) != ERROR_SUCCESS)
865 ERR("failed to download %s (%u)\n", debugstr_w(cabinet_file), rc);
866 msi_free( cabinet_file );
869 if ((p = strrchrW( temppath, '\\' ))) *p = 0;
870 strcpyW( mi->sourcedir, temppath );
871 PathAddBackslashW( mi->sourcedir );
872 msi_free( mi->cabinet );
873 mi->cabinet = strdupW( p + 1 );
874 msi_free( cabinet_file );
875 return ERROR_SUCCESS;
878 /* check volume matches, change media if not */
879 if (mi->volume_label && mi->disk_id > 1 && strcmpW( mi->first_volume, mi->volume_label ))
881 WCHAR *source = msi_dup_property( package->db, szSourceDir );
882 BOOL match = source_matches_volume( mi, source );
885 if (!match && (mi->type == DRIVE_CDROM || mi->type == DRIVE_REMOVABLE))
887 if ((rc = msi_change_media( package, mi )) != ERROR_SUCCESS)
889 msi_free( cabinet_file );
896 if (compressed && GetFileAttributesW( cabinet_file ) == INVALID_FILE_ATTRIBUTES)
898 if ((rc = find_published_source( package, mi )) != ERROR_SUCCESS)
900 ERR("cabinet not found: %s\n", debugstr_w(cabinet_file));
901 msi_free( cabinet_file );
902 return ERROR_INSTALL_FAILURE;
906 msi_free( cabinet_file );
907 return ERROR_SUCCESS;
910 UINT msi_add_cabinet_stream( MSIPACKAGE *package, UINT disk_id, IStorage *storage, const WCHAR *name )
912 MSICABINETSTREAM *cab, *item;
914 TRACE("%p, %u, %p, %s\n", package, disk_id, storage, debugstr_w(name));
916 LIST_FOR_EACH_ENTRY( item, &package->cabinet_streams, MSICABINETSTREAM, entry )
918 if (item->disk_id == disk_id)
920 TRACE("duplicate disk id %u\n", disk_id);
921 return ERROR_FUNCTION_FAILED;
924 if (!(cab = msi_alloc( sizeof(*cab) ))) return ERROR_OUTOFMEMORY;
925 if (!(cab->stream = msi_alloc( (strlenW( name ) + 1) * sizeof(WCHAR ) )))
928 return ERROR_OUTOFMEMORY;
930 strcpyW( cab->stream, name );
931 cab->disk_id = disk_id;
932 cab->storage = storage;
933 IStorage_AddRef( storage );
934 list_add_tail( &package->cabinet_streams, &cab->entry );
936 return ERROR_SUCCESS;