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 ((package->ui_level & INSTALLUILEVEL_MASK) == INSTALLUILEVEL_NONE &&
81 !gUIHandlerA && !gUIHandlerW && !gUIHandlerRecord) return ERROR_SUCCESS;
83 error = msi_build_error_string(package, 1302, 1, mi->disk_prompt);
84 error_dialog = msi_dup_property(package->db, error_prop);
85 source_dir = msi_dup_property(package->db, szSourceDir);
87 while (r == ERROR_SUCCESS && !source_matches_volume(mi, source_dir))
89 r = msi_spawn_error_dialog(package, error_dialog, error);
93 gUIHandlerW(gUIContext, MB_RETRYCANCEL | INSTALLMESSAGE_ERROR, error);
97 char *msg = strdupWtoA(error);
98 gUIHandlerA(gUIContext, MB_RETRYCANCEL | INSTALLMESSAGE_ERROR, msg);
101 else if (gUIHandlerRecord)
103 MSIHANDLE rec = MsiCreateRecord(1);
104 MsiRecordSetStringW(rec, 0, error);
105 gUIHandlerRecord(gUIContext, MB_RETRYCANCEL | INSTALLMESSAGE_ERROR, rec);
111 msi_free(error_dialog);
112 msi_free(source_dir);
117 static MSICABINETSTREAM *msi_get_cabinet_stream( MSIPACKAGE *package, UINT disk_id )
119 MSICABINETSTREAM *cab;
121 LIST_FOR_EACH_ENTRY( cab, &package->cabinet_streams, MSICABINETSTREAM, entry )
123 if (cab->disk_id == disk_id) return cab;
128 static void * CDECL cabinet_alloc(ULONG cb)
130 return msi_alloc(cb);
133 static void CDECL cabinet_free(void *pv)
138 static INT_PTR CDECL cabinet_open(char *pszFile, int oflag, int pmode)
142 DWORD dwShareMode = 0;
143 DWORD dwCreateDisposition = OPEN_EXISTING;
145 switch (oflag & _O_ACCMODE)
148 dwAccess = GENERIC_READ;
149 dwShareMode = FILE_SHARE_READ | FILE_SHARE_DELETE;
152 dwAccess = GENERIC_WRITE;
153 dwShareMode = FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE;
156 dwAccess = GENERIC_READ | GENERIC_WRITE;
157 dwShareMode = FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE;
161 if ((oflag & (_O_CREAT | _O_EXCL)) == (_O_CREAT | _O_EXCL))
162 dwCreateDisposition = CREATE_NEW;
163 else if (oflag & _O_CREAT)
164 dwCreateDisposition = CREATE_ALWAYS;
166 handle = CreateFileA(pszFile, dwAccess, dwShareMode, NULL,
167 dwCreateDisposition, 0, NULL);
168 if (handle == INVALID_HANDLE_VALUE)
171 return (INT_PTR)handle;
174 static UINT CDECL cabinet_read(INT_PTR hf, void *pv, UINT cb)
176 HANDLE handle = (HANDLE)hf;
179 if (ReadFile(handle, pv, cb, &read, NULL))
185 static UINT CDECL cabinet_write(INT_PTR hf, void *pv, UINT cb)
187 HANDLE handle = (HANDLE)hf;
190 if (WriteFile(handle, pv, cb, &written, NULL))
196 static int CDECL cabinet_close(INT_PTR hf)
198 HANDLE handle = (HANDLE)hf;
199 return CloseHandle(handle) ? 0 : -1;
202 static LONG CDECL cabinet_seek(INT_PTR hf, LONG dist, int seektype)
204 HANDLE handle = (HANDLE)hf;
205 /* flags are compatible and so are passed straight through */
206 return SetFilePointer(handle, dist, NULL, seektype);
215 static struct package_disk package_disk;
217 static INT_PTR CDECL cabinet_open_stream( char *pszFile, int oflag, int pmode )
219 MSICABINETSTREAM *cab;
224 cab = msi_get_cabinet_stream( package_disk.package, package_disk.id );
227 WARN("failed to get cabinet stream\n");
230 if (!cab->stream[0] || !(encoded = encode_streamname( FALSE, cab->stream + 1 )))
232 WARN("failed to encode stream name\n");
235 if (msi_clone_open_stream( package_disk.package->db, cab->storage, encoded, &stream ) != ERROR_SUCCESS)
237 hr = IStorage_OpenStream( cab->storage, encoded, NULL, STGM_READ|STGM_SHARE_EXCLUSIVE, 0, &stream );
240 WARN("failed to open stream 0x%08x\n", hr);
246 return (INT_PTR)stream;
249 static UINT CDECL cabinet_read_stream( INT_PTR hf, void *pv, UINT cb )
251 IStream *stm = (IStream *)hf;
255 hr = IStream_Read( stm, pv, cb, &read );
256 if (hr == S_OK || hr == S_FALSE)
262 static int CDECL cabinet_close_stream( INT_PTR hf )
264 IStream *stm = (IStream *)hf;
265 IStream_Release( stm );
269 static LONG CDECL cabinet_seek_stream( INT_PTR hf, LONG dist, int seektype )
271 IStream *stm = (IStream *)hf;
273 ULARGE_INTEGER newpos;
276 move.QuadPart = dist;
277 hr = IStream_Seek( stm, move, seektype, &newpos );
280 if (newpos.QuadPart <= MAXLONG) return newpos.QuadPart;
286 static UINT CDECL msi_media_get_disk_info(MSIPACKAGE *package, MSIMEDIAINFO *mi)
290 static const WCHAR query[] = {
291 'S','E','L','E','C','T',' ','*',' ', 'F','R','O','M',' ',
292 '`','M','e','d','i','a','`',' ','W','H','E','R','E',' ',
293 '`','D','i','s','k','I','d','`',' ','=',' ','%','i',0};
295 row = MSI_QueryGetRecord(package->db, query, mi->disk_id);
298 TRACE("Unable to query row\n");
299 return ERROR_FUNCTION_FAILED;
302 mi->disk_prompt = strdupW(MSI_RecordGetString(row, 3));
303 mi->cabinet = strdupW(MSI_RecordGetString(row, 4));
304 mi->volume_label = strdupW(MSI_RecordGetString(row, 5));
306 if (!mi->first_volume)
307 mi->first_volume = strdupW(mi->volume_label);
309 msiobj_release(&row->hdr);
310 return ERROR_SUCCESS;
313 static INT_PTR cabinet_partial_file(FDINOTIFICATIONTYPE fdint,
314 PFDINOTIFICATION pfdin)
316 MSICABDATA *data = pfdin->pv;
317 data->mi->is_continuous = FALSE;
321 static WCHAR *get_cabinet_filename(MSIMEDIAINFO *mi)
326 len = strlenW(mi->sourcedir) + strlenW(mi->cabinet) + 1;
327 if (!(ret = msi_alloc(len * sizeof(WCHAR)))) return NULL;
328 strcpyW(ret, mi->sourcedir);
329 strcatW(ret, mi->cabinet);
333 static INT_PTR cabinet_next_cabinet(FDINOTIFICATIONTYPE fdint,
334 PFDINOTIFICATION pfdin)
336 MSICABDATA *data = pfdin->pv;
337 MSIMEDIAINFO *mi = data->mi;
338 LPWSTR cabinet_file = NULL, cab = strdupAtoW(pfdin->psz1);
342 msi_free(mi->disk_prompt);
343 msi_free(mi->cabinet);
344 msi_free(mi->volume_label);
345 mi->disk_prompt = NULL;
347 mi->volume_label = NULL;
350 mi->is_continuous = TRUE;
352 rc = msi_media_get_disk_info(data->package, mi);
353 if (rc != ERROR_SUCCESS)
355 ERR("Failed to get next cabinet information: %d\n", rc);
359 if (strcmpiW( mi->cabinet, cab ))
361 ERR("Continuous cabinet does not match the next cabinet in the Media table\n");
365 if (!(cabinet_file = get_cabinet_filename(mi)))
368 TRACE("Searching for %s\n", debugstr_w(cabinet_file));
371 if (GetFileAttributesW(cabinet_file) == INVALID_FILE_ATTRIBUTES)
373 if (msi_change_media(data->package, mi) != ERROR_SUCCESS)
379 msi_free(cabinet_file);
383 static INT_PTR cabinet_next_cabinet_stream( FDINOTIFICATIONTYPE fdint,
384 PFDINOTIFICATION pfdin )
386 MSICABDATA *data = pfdin->pv;
387 MSIMEDIAINFO *mi = data->mi;
390 msi_free( mi->disk_prompt );
391 msi_free( mi->cabinet );
392 msi_free( mi->volume_label );
393 mi->disk_prompt = NULL;
395 mi->volume_label = NULL;
398 mi->is_continuous = TRUE;
400 rc = msi_media_get_disk_info( data->package, mi );
401 if (rc != ERROR_SUCCESS)
403 ERR("Failed to get next cabinet information: %u\n", rc);
406 package_disk.id = mi->disk_id;
408 TRACE("next cabinet is %s disk id %u\n", debugstr_w(mi->cabinet), mi->disk_id);
412 static INT_PTR cabinet_copy_file(FDINOTIFICATIONTYPE fdint,
413 PFDINOTIFICATION pfdin)
415 MSICABDATA *data = pfdin->pv;
420 data->curfile = strdupAtoW(pfdin->psz1);
421 if (!data->cb(data->package, data->curfile, MSICABEXTRACT_BEGINEXTRACT, &path,
424 /* We're not extracting this file, so free the filename. */
425 msi_free(data->curfile);
426 data->curfile = NULL;
430 TRACE("extracting %s -> %s\n", debugstr_w(data->curfile), debugstr_w(path));
432 attrs = attrs & (FILE_ATTRIBUTE_READONLY|FILE_ATTRIBUTE_HIDDEN|FILE_ATTRIBUTE_SYSTEM);
433 if (!attrs) attrs = FILE_ATTRIBUTE_NORMAL;
435 handle = CreateFileW(path, GENERIC_READ | GENERIC_WRITE, 0,
436 NULL, CREATE_ALWAYS, attrs, NULL);
437 if (handle == INVALID_HANDLE_VALUE)
439 DWORD err = GetLastError();
440 DWORD attrs2 = GetFileAttributesW(path);
442 if (attrs2 == INVALID_FILE_ATTRIBUTES)
444 ERR("failed to create %s (error %d)\n", debugstr_w(path), err);
447 else if (err == ERROR_ACCESS_DENIED && (attrs2 & FILE_ATTRIBUTE_READONLY))
449 TRACE("removing read-only attribute on %s\n", debugstr_w(path));
450 SetFileAttributesW( path, attrs2 & ~FILE_ATTRIBUTE_READONLY );
451 handle = CreateFileW(path, GENERIC_READ | GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, attrs2, NULL);
453 if (handle != INVALID_HANDLE_VALUE) goto done;
454 err = GetLastError();
456 if (err == ERROR_SHARING_VIOLATION || err == ERROR_USER_MAPPED_FILE)
458 WCHAR *tmpfileW, *tmppathW, *p;
461 TRACE("file in use, scheduling rename operation\n");
463 if (!(tmppathW = strdupW( path ))) return ERROR_OUTOFMEMORY;
464 if ((p = strrchrW(tmppathW, '\\'))) *p = 0;
465 len = strlenW( tmppathW ) + 16;
466 if (!(tmpfileW = msi_alloc(len * sizeof(WCHAR))))
468 msi_free( tmppathW );
469 return ERROR_OUTOFMEMORY;
471 if (!GetTempFileNameW(tmppathW, szMsi, 0, tmpfileW)) tmpfileW[0] = 0;
472 msi_free( tmppathW );
474 handle = CreateFileW(tmpfileW, GENERIC_READ | GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, attrs, NULL);
476 if (handle != INVALID_HANDLE_VALUE &&
477 MoveFileExW(path, NULL, MOVEFILE_DELAY_UNTIL_REBOOT) &&
478 MoveFileExW(tmpfileW, path, MOVEFILE_DELAY_UNTIL_REBOOT))
480 data->package->need_reboot_at_end = 1;
484 WARN("failed to schedule rename operation %s (error %d)\n", debugstr_w(path), GetLastError());
485 DeleteFileW( tmpfileW );
490 WARN("failed to create %s (error %d)\n", debugstr_w(path), err);
496 return (INT_PTR)handle;
499 static INT_PTR cabinet_close_file_info(FDINOTIFICATIONTYPE fdint,
500 PFDINOTIFICATION pfdin)
502 MSICABDATA *data = pfdin->pv;
505 HANDLE handle = (HANDLE)pfdin->hf;
507 data->mi->is_continuous = FALSE;
509 if (!DosDateTimeToFileTime(pfdin->date, pfdin->time, &ft))
511 if (!LocalFileTimeToFileTime(&ft, &ftLocal))
513 if (!SetFileTime(handle, &ftLocal, 0, &ftLocal))
518 data->cb(data->package, data->curfile, MSICABEXTRACT_FILEEXTRACTED, NULL, NULL,
521 msi_free(data->curfile);
522 data->curfile = NULL;
527 static INT_PTR CDECL cabinet_notify(FDINOTIFICATIONTYPE fdint, PFDINOTIFICATION pfdin)
531 case fdintPARTIAL_FILE:
532 return cabinet_partial_file(fdint, pfdin);
534 case fdintNEXT_CABINET:
535 return cabinet_next_cabinet(fdint, pfdin);
538 return cabinet_copy_file(fdint, pfdin);
540 case fdintCLOSE_FILE_INFO:
541 return cabinet_close_file_info(fdint, pfdin);
548 static INT_PTR CDECL cabinet_notify_stream( FDINOTIFICATIONTYPE fdint, PFDINOTIFICATION pfdin )
552 case fdintPARTIAL_FILE:
553 return cabinet_partial_file( fdint, pfdin );
555 case fdintNEXT_CABINET:
556 return cabinet_next_cabinet_stream( fdint, pfdin );
559 return cabinet_copy_file( fdint, pfdin );
561 case fdintCLOSE_FILE_INFO:
562 return cabinet_close_file_info( fdint, pfdin );
564 case fdintCABINET_INFO:
568 ERR("Unexpected notification %d\n", fdint);
573 static BOOL extract_cabinet( MSIPACKAGE* package, MSIMEDIAINFO *mi, LPVOID data )
575 LPSTR cabinet, cab_path = NULL;
580 TRACE("extracting %s disk id %u\n", debugstr_w(mi->cabinet), mi->disk_id);
582 hfdi = FDICreate( cabinet_alloc, cabinet_free, cabinet_open, cabinet_read,
583 cabinet_write, cabinet_close, cabinet_seek, 0, &erf );
586 ERR("FDICreate failed\n");
590 cabinet = strdupWtoA( mi->cabinet );
594 cab_path = strdupWtoA( mi->sourcedir );
598 ret = FDICopy( hfdi, cabinet, cab_path, 0, cabinet_notify, NULL, data );
600 ERR("FDICopy failed\n");
605 msi_free( cab_path );
608 mi->is_extracted = TRUE;
613 static BOOL extract_cabinet_stream( MSIPACKAGE *package, MSIMEDIAINFO *mi, LPVOID data )
615 static char filename[] = {'<','S','T','R','E','A','M','>',0};
620 TRACE("extracting %s disk id %u\n", debugstr_w(mi->cabinet), mi->disk_id);
622 hfdi = FDICreate( cabinet_alloc, cabinet_free, cabinet_open_stream, cabinet_read_stream,
623 cabinet_write, cabinet_close_stream, cabinet_seek_stream, 0, &erf );
626 ERR("FDICreate failed\n");
630 package_disk.package = package;
631 package_disk.id = mi->disk_id;
633 ret = FDICopy( hfdi, filename, NULL, 0, cabinet_notify_stream, NULL, data );
634 if (!ret) ERR("FDICopy failed\n");
637 if (ret) mi->is_extracted = TRUE;
641 /***********************************************************************
644 * Extract files from a cabinet file or stream.
646 BOOL msi_cabextract(MSIPACKAGE* package, MSIMEDIAINFO *mi, LPVOID data)
648 if (mi->cabinet[0] == '#')
650 return extract_cabinet_stream( package, mi, data );
652 return extract_cabinet( package, mi, data );
655 void msi_free_media_info(MSIMEDIAINFO *mi)
657 msi_free(mi->disk_prompt);
658 msi_free(mi->cabinet);
659 msi_free(mi->volume_label);
660 msi_free(mi->first_volume);
664 static UINT get_drive_type(const WCHAR *path)
666 WCHAR root[MAX_PATH + 1];
669 PathStripToRootW(root);
670 PathAddBackslashW(root);
672 return GetDriveTypeW(root);
675 UINT msi_load_media_info(MSIPACKAGE *package, UINT Sequence, MSIMEDIAINFO *mi)
677 static const WCHAR query[] = {
678 'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ','`','M','e','d','i','a','`',' ',
679 'W','H','E','R','E',' ','`','L','a','s','t','S','e','q','u','e','n','c','e','`',' ',
680 '>','=',' ','%','i',' ','O','R','D','E','R',' ','B','Y',' ','`','D','i','s','k','I','d','`',0};
682 LPWSTR source_dir, source;
685 if (Sequence <= mi->last_sequence) /* already loaded */
686 return ERROR_SUCCESS;
688 row = MSI_QueryGetRecord(package->db, query, Sequence);
691 TRACE("Unable to query row\n");
692 return ERROR_FUNCTION_FAILED;
695 mi->is_extracted = FALSE;
696 mi->disk_id = MSI_RecordGetInteger(row, 1);
697 mi->last_sequence = MSI_RecordGetInteger(row, 2);
698 msi_free(mi->disk_prompt);
699 mi->disk_prompt = strdupW(MSI_RecordGetString(row, 3));
700 msi_free(mi->cabinet);
701 mi->cabinet = strdupW(MSI_RecordGetString(row, 4));
702 msi_free(mi->volume_label);
703 mi->volume_label = strdupW(MSI_RecordGetString(row, 5));
704 msiobj_release(&row->hdr);
706 if (!mi->first_volume)
707 mi->first_volume = strdupW(mi->volume_label);
709 msi_set_sourcedir_props(package, FALSE);
710 source_dir = msi_dup_property(package->db, szSourceDir);
711 lstrcpyW(mi->sourcedir, source_dir);
712 PathAddBackslashW(mi->sourcedir);
713 mi->type = get_drive_type(source_dir);
715 options = MSICODE_PRODUCT;
716 if (mi->type == DRIVE_CDROM || mi->type == DRIVE_REMOVABLE)
719 options |= MSISOURCETYPE_MEDIA;
721 else if (package->BaseURL && UrlIsW(package->BaseURL, URLIS_URL))
723 source = package->BaseURL;
724 options |= MSISOURCETYPE_URL;
728 source = mi->sourcedir;
729 options |= MSISOURCETYPE_NETWORK;
732 msi_package_add_media_disk(package, package->Context,
733 MSICODE_PRODUCT, mi->disk_id,
734 mi->volume_label, mi->disk_prompt);
736 msi_package_add_info(package, package->Context,
737 options, INSTALLPROPERTY_LASTUSEDSOURCEW, source);
739 msi_free(source_dir);
740 TRACE("sequence %u -> cabinet %s disk id %u\n", Sequence, debugstr_w(mi->cabinet), mi->disk_id);
741 return ERROR_SUCCESS;
744 /* FIXME: search URL sources as well */
745 static UINT find_published_source(MSIPACKAGE *package, MSIMEDIAINFO *mi)
747 WCHAR source[MAX_PATH];
748 WCHAR volume[MAX_PATH];
749 WCHAR prompt[MAX_PATH];
750 DWORD volumesz, promptsz;
751 DWORD index, size, id;
756 r = MsiSourceListGetInfoW(package->ProductCode, NULL,
757 package->Context, MSICODE_PRODUCT,
758 INSTALLPROPERTY_LASTUSEDTYPEW, last_type, &size);
759 if (r != ERROR_SUCCESS)
763 r = MsiSourceListGetInfoW(package->ProductCode, NULL,
764 package->Context, MSICODE_PRODUCT,
765 INSTALLPROPERTY_LASTUSEDSOURCEW, source, &size);
766 if (r != ERROR_SUCCESS)
769 if (last_type[0] == 'n')
771 WCHAR cabinet_file[MAX_PATH];
772 BOOL check_all = FALSE;
778 while (MsiSourceListEnumSourcesW(package->ProductCode, NULL,
780 MSISOURCETYPE_NETWORK, index++,
781 volume, &volumesz) == ERROR_SUCCESS)
783 if (check_all || !strncmpiW(source, volume, strlenW(source)))
785 lstrcpyW(cabinet_file, volume);
786 PathAddBackslashW(cabinet_file);
787 lstrcatW(cabinet_file, mi->cabinet);
789 if (GetFileAttributesW(cabinet_file) == INVALID_FILE_ATTRIBUTES)
797 lstrcpyW(mi->sourcedir, volume);
798 PathAddBackslashW(mi->sourcedir);
799 TRACE("Found network source %s\n", debugstr_w(mi->sourcedir));
800 return ERROR_SUCCESS;
814 while (MsiSourceListEnumMediaDisksW(package->ProductCode, NULL,
816 MSICODE_PRODUCT, index++, &id,
817 volume, &volumesz, prompt, &promptsz) == ERROR_SUCCESS)
820 msi_free( mi->volume_label );
821 if (!(mi->volume_label = msi_alloc( ++volumesz * sizeof(WCHAR) ))) return ERROR_OUTOFMEMORY;
822 strcpyW( mi->volume_label, volume );
824 msi_free( mi->disk_prompt );
825 if (!(mi->disk_prompt = msi_alloc( ++promptsz * sizeof(WCHAR) ))) return ERROR_OUTOFMEMORY;
826 strcpyW( mi->disk_prompt, prompt );
828 if (source_matches_volume(mi, source))
830 /* FIXME: what about SourceDir */
831 lstrcpyW(mi->sourcedir, source);
832 PathAddBackslashW(mi->sourcedir);
833 TRACE("Found disk source %s\n", debugstr_w(mi->sourcedir));
834 return ERROR_SUCCESS;
838 return ERROR_FUNCTION_FAILED;
841 UINT ready_media( MSIPACKAGE *package, BOOL compressed, MSIMEDIAINFO *mi )
844 WCHAR *cabinet_file = NULL;
846 /* media info for continuous cabinet is already loaded */
847 if (mi->is_continuous) return ERROR_SUCCESS;
851 /* cabinet is internal, no checks needed */
852 if (mi->cabinet[0] == '#') return ERROR_SUCCESS;
854 if (!(cabinet_file = get_cabinet_filename( mi ))) return ERROR_OUTOFMEMORY;
856 /* package should be downloaded */
857 if (compressed && GetFileAttributesW( cabinet_file ) == INVALID_FILE_ATTRIBUTES &&
858 package->BaseURL && UrlIsW( package->BaseURL, URLIS_URL ))
860 WCHAR temppath[MAX_PATH], *p;
862 if ((rc = msi_download_file( cabinet_file, temppath )) != ERROR_SUCCESS)
864 ERR("failed to download %s (%u)\n", debugstr_w(cabinet_file), rc);
865 msi_free( cabinet_file );
868 if ((p = strrchrW( temppath, '\\' ))) *p = 0;
869 strcpyW( mi->sourcedir, temppath );
870 PathAddBackslashW( mi->sourcedir );
871 msi_free( mi->cabinet );
872 mi->cabinet = strdupW( p + 1 );
873 msi_free( cabinet_file );
874 return ERROR_SUCCESS;
877 /* check volume matches, change media if not */
878 if (mi->volume_label && mi->disk_id > 1 && strcmpW( mi->first_volume, mi->volume_label ))
880 WCHAR *source = msi_dup_property( package->db, szSourceDir );
881 BOOL match = source_matches_volume( mi, source );
884 if (!match && (mi->type == DRIVE_CDROM || mi->type == DRIVE_REMOVABLE))
886 if ((rc = msi_change_media( package, mi )) != ERROR_SUCCESS)
888 msi_free( cabinet_file );
895 if (compressed && GetFileAttributesW( cabinet_file ) == INVALID_FILE_ATTRIBUTES)
897 if ((rc = find_published_source( package, mi )) != ERROR_SUCCESS)
899 ERR("cabinet not found: %s\n", debugstr_w(cabinet_file));
900 msi_free( cabinet_file );
901 return ERROR_INSTALL_FAILURE;
905 msi_free( cabinet_file );
906 return ERROR_SUCCESS;
909 UINT msi_add_cabinet_stream( MSIPACKAGE *package, UINT disk_id, IStorage *storage, const WCHAR *name )
911 MSICABINETSTREAM *cab, *item;
913 TRACE("%p, %u, %p, %s\n", package, disk_id, storage, debugstr_w(name));
915 LIST_FOR_EACH_ENTRY( item, &package->cabinet_streams, MSICABINETSTREAM, entry )
917 if (item->disk_id == disk_id)
919 TRACE("duplicate disk id %u\n", disk_id);
920 return ERROR_FUNCTION_FAILED;
923 if (!(cab = msi_alloc( sizeof(*cab) ))) return ERROR_OUTOFMEMORY;
924 if (!(cab->stream = msi_alloc( (strlenW( name ) + 1) * sizeof(WCHAR ) )))
927 return ERROR_OUTOFMEMORY;
929 strcpyW( cab->stream, name );
930 cab->disk_id = disk_id;
931 cab->storage = storage;
932 IStorage_AddRef( storage );
933 list_add_tail( &package->cabinet_streams, &cab->entry );
935 return ERROR_SUCCESS;