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
)
141 DWORD dwShareMode
= 0;
142 DWORD dwCreateDisposition
= OPEN_EXISTING
;
144 switch (oflag
& _O_ACCMODE
)
147 dwAccess
= GENERIC_READ
;
148 dwShareMode
= FILE_SHARE_READ
| FILE_SHARE_DELETE
;
151 dwAccess
= GENERIC_WRITE
;
152 dwShareMode
= FILE_SHARE_READ
| FILE_SHARE_WRITE
| FILE_SHARE_DELETE
;
155 dwAccess
= GENERIC_READ
| GENERIC_WRITE
;
156 dwShareMode
= FILE_SHARE_READ
| FILE_SHARE_WRITE
| FILE_SHARE_DELETE
;
160 if ((oflag
& (_O_CREAT
| _O_EXCL
)) == (_O_CREAT
| _O_EXCL
))
161 dwCreateDisposition
= CREATE_NEW
;
162 else if (oflag
& _O_CREAT
)
163 dwCreateDisposition
= CREATE_ALWAYS
;
165 return (INT_PTR
)CreateFileA(pszFile
, dwAccess
, dwShareMode
, NULL
,
166 dwCreateDisposition
, 0, NULL
);
169 static UINT CDECL
cabinet_read(INT_PTR hf
, void *pv
, UINT cb
)
171 HANDLE handle
= (HANDLE
)hf
;
174 if (ReadFile(handle
, pv
, cb
, &read
, NULL
))
180 static UINT CDECL
cabinet_write(INT_PTR hf
, void *pv
, UINT cb
)
182 HANDLE handle
= (HANDLE
)hf
;
185 if (WriteFile(handle
, pv
, cb
, &written
, NULL
))
191 static int CDECL
cabinet_close(INT_PTR hf
)
193 HANDLE handle
= (HANDLE
)hf
;
194 return CloseHandle(handle
) ? 0 : -1;
197 static LONG CDECL
cabinet_seek(INT_PTR hf
, LONG dist
, int seektype
)
199 HANDLE handle
= (HANDLE
)hf
;
200 /* flags are compatible and so are passed straight through */
201 return SetFilePointer(handle
, dist
, NULL
, seektype
);
210 static struct package_disk package_disk
;
212 static INT_PTR CDECL
cabinet_open_stream( char *pszFile
, int oflag
, int pmode
)
214 MSICABINETSTREAM
*cab
;
219 cab
= msi_get_cabinet_stream( package_disk
.package
, package_disk
.id
);
222 WARN("failed to get cabinet stream\n");
225 if (!cab
->stream
[0] || !(encoded
= encode_streamname( FALSE
, cab
->stream
+ 1 )))
227 WARN("failed to encode stream name\n");
230 if (msi_clone_open_stream( package_disk
.package
->db
, cab
->storage
, encoded
, &stream
) != ERROR_SUCCESS
)
232 hr
= IStorage_OpenStream( cab
->storage
, encoded
, NULL
, STGM_READ
|STGM_SHARE_EXCLUSIVE
, 0, &stream
);
235 WARN("failed to open stream 0x%08x\n", hr
);
241 return (INT_PTR
)stream
;
244 static UINT CDECL
cabinet_read_stream( INT_PTR hf
, void *pv
, UINT cb
)
246 IStream
*stm
= (IStream
*)hf
;
250 hr
= IStream_Read( stm
, pv
, cb
, &read
);
251 if (hr
== S_OK
|| hr
== S_FALSE
)
257 static int CDECL
cabinet_close_stream( INT_PTR hf
)
259 IStream
*stm
= (IStream
*)hf
;
260 IStream_Release( stm
);
264 static LONG CDECL
cabinet_seek_stream( INT_PTR hf
, LONG dist
, int seektype
)
266 IStream
*stm
= (IStream
*)hf
;
268 ULARGE_INTEGER newpos
;
271 move
.QuadPart
= dist
;
272 hr
= IStream_Seek( stm
, move
, seektype
, &newpos
);
275 if (newpos
.QuadPart
<= MAXLONG
) return newpos
.QuadPart
;
281 static UINT CDECL
msi_media_get_disk_info(MSIPACKAGE
*package
, MSIMEDIAINFO
*mi
)
285 static const WCHAR query
[] = {
286 'S','E','L','E','C','T',' ','*',' ', 'F','R','O','M',' ',
287 '`','M','e','d','i','a','`',' ','W','H','E','R','E',' ',
288 '`','D','i','s','k','I','d','`',' ','=',' ','%','i',0};
290 row
= MSI_QueryGetRecord(package
->db
, query
, mi
->disk_id
);
293 TRACE("Unable to query row\n");
294 return ERROR_FUNCTION_FAILED
;
297 mi
->disk_prompt
= strdupW(MSI_RecordGetString(row
, 3));
298 mi
->cabinet
= strdupW(MSI_RecordGetString(row
, 4));
299 mi
->volume_label
= strdupW(MSI_RecordGetString(row
, 5));
301 if (!mi
->first_volume
)
302 mi
->first_volume
= strdupW(mi
->volume_label
);
304 msiobj_release(&row
->hdr
);
305 return ERROR_SUCCESS
;
308 static INT_PTR
cabinet_partial_file(FDINOTIFICATIONTYPE fdint
,
309 PFDINOTIFICATION pfdin
)
311 MSICABDATA
*data
= pfdin
->pv
;
312 data
->mi
->is_continuous
= FALSE
;
316 static WCHAR
*get_cabinet_filename(MSIMEDIAINFO
*mi
)
321 len
= strlenW(mi
->sourcedir
) + strlenW(mi
->cabinet
) + 1;
322 if (!(ret
= msi_alloc(len
* sizeof(WCHAR
)))) return NULL
;
323 strcpyW(ret
, mi
->sourcedir
);
324 strcatW(ret
, mi
->cabinet
);
328 static INT_PTR
cabinet_next_cabinet(FDINOTIFICATIONTYPE fdint
,
329 PFDINOTIFICATION pfdin
)
331 MSICABDATA
*data
= pfdin
->pv
;
332 MSIMEDIAINFO
*mi
= data
->mi
;
333 LPWSTR cabinet_file
= NULL
, cab
= strdupAtoW(pfdin
->psz1
);
337 msi_free(mi
->disk_prompt
);
338 msi_free(mi
->cabinet
);
339 msi_free(mi
->volume_label
);
340 mi
->disk_prompt
= NULL
;
342 mi
->volume_label
= NULL
;
345 mi
->is_continuous
= TRUE
;
347 rc
= msi_media_get_disk_info(data
->package
, mi
);
348 if (rc
!= ERROR_SUCCESS
)
350 ERR("Failed to get next cabinet information: %d\n", rc
);
354 if (strcmpiW( mi
->cabinet
, cab
))
359 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
));
361 /* Use cabinet name from the media table */
362 next_cab
= strdupWtoA(mi
->cabinet
);
363 /* Modify path to cabinet file with full filename (psz3 points to a 256 bytes buffer that can be modified contrary to psz1 and psz2) */
364 length
= strlen(pfdin
->psz3
) + 1 + strlen(next_cab
) + 1;
367 WARN("Cannot update next cabinet filename with a string size %u > 256\n", length
);
373 strcat(pfdin
->psz3
, "\\");
374 strcat(pfdin
->psz3
, next_cab
);
376 /* Path psz3 and cabinet psz1 are concatenated by FDI so just reset psz1 */
381 if (!(cabinet_file
= get_cabinet_filename(mi
)))
384 TRACE("Searching for %s\n", debugstr_w(cabinet_file
));
387 if (GetFileAttributesW(cabinet_file
) == INVALID_FILE_ATTRIBUTES
)
389 if (msi_change_media(data
->package
, mi
) != ERROR_SUCCESS
)
395 msi_free(cabinet_file
);
399 static INT_PTR
cabinet_next_cabinet_stream( FDINOTIFICATIONTYPE fdint
,
400 PFDINOTIFICATION pfdin
)
402 MSICABDATA
*data
= pfdin
->pv
;
403 MSIMEDIAINFO
*mi
= data
->mi
;
406 msi_free( mi
->disk_prompt
);
407 msi_free( mi
->cabinet
);
408 msi_free( mi
->volume_label
);
409 mi
->disk_prompt
= NULL
;
411 mi
->volume_label
= NULL
;
414 mi
->is_continuous
= TRUE
;
416 rc
= msi_media_get_disk_info( data
->package
, mi
);
417 if (rc
!= ERROR_SUCCESS
)
419 ERR("Failed to get next cabinet information: %u\n", rc
);
422 package_disk
.id
= mi
->disk_id
;
424 TRACE("next cabinet is %s disk id %u\n", debugstr_w(mi
->cabinet
), mi
->disk_id
);
428 static INT_PTR
cabinet_copy_file(FDINOTIFICATIONTYPE fdint
,
429 PFDINOTIFICATION pfdin
)
431 MSICABDATA
*data
= pfdin
->pv
;
436 data
->curfile
= strdupAtoW(pfdin
->psz1
);
437 if (!data
->cb(data
->package
, data
->curfile
, MSICABEXTRACT_BEGINEXTRACT
, &path
,
440 /* We're not extracting this file, so free the filename. */
441 msi_free(data
->curfile
);
442 data
->curfile
= NULL
;
446 TRACE("extracting %s -> %s\n", debugstr_w(data
->curfile
), debugstr_w(path
));
448 attrs
= attrs
& (FILE_ATTRIBUTE_READONLY
|FILE_ATTRIBUTE_HIDDEN
|FILE_ATTRIBUTE_SYSTEM
);
449 if (!attrs
) attrs
= FILE_ATTRIBUTE_NORMAL
;
451 handle
= CreateFileW(path
, GENERIC_READ
| GENERIC_WRITE
, 0,
452 NULL
, CREATE_ALWAYS
, attrs
, NULL
);
453 if (handle
== INVALID_HANDLE_VALUE
)
455 DWORD err
= GetLastError();
456 DWORD attrs2
= GetFileAttributesW(path
);
458 if (attrs2
== INVALID_FILE_ATTRIBUTES
)
460 ERR("failed to create %s (error %d)\n", debugstr_w(path
), err
);
463 else if (err
== ERROR_ACCESS_DENIED
&& (attrs2
& FILE_ATTRIBUTE_READONLY
))
465 TRACE("removing read-only attribute on %s\n", debugstr_w(path
));
466 SetFileAttributesW( path
, attrs2
& ~FILE_ATTRIBUTE_READONLY
);
467 handle
= CreateFileW(path
, GENERIC_READ
| GENERIC_WRITE
, 0, NULL
, CREATE_ALWAYS
, attrs2
, NULL
);
469 if (handle
!= INVALID_HANDLE_VALUE
) goto done
;
470 err
= GetLastError();
472 if (err
== ERROR_SHARING_VIOLATION
|| err
== ERROR_USER_MAPPED_FILE
)
474 WCHAR
*tmpfileW
, *tmppathW
, *p
;
477 TRACE("file in use, scheduling rename operation\n");
479 if (!(tmppathW
= strdupW( path
))) return ERROR_OUTOFMEMORY
;
480 if ((p
= strrchrW(tmppathW
, '\\'))) *p
= 0;
481 len
= strlenW( tmppathW
) + 16;
482 if (!(tmpfileW
= msi_alloc(len
* sizeof(WCHAR
))))
484 msi_free( tmppathW
);
485 return ERROR_OUTOFMEMORY
;
487 if (!GetTempFileNameW(tmppathW
, szMsi
, 0, tmpfileW
)) tmpfileW
[0] = 0;
488 msi_free( tmppathW
);
490 handle
= CreateFileW(tmpfileW
, GENERIC_READ
| GENERIC_WRITE
, 0, NULL
, CREATE_ALWAYS
, attrs
, NULL
);
492 if (handle
!= INVALID_HANDLE_VALUE
&&
493 MoveFileExW(path
, NULL
, MOVEFILE_DELAY_UNTIL_REBOOT
) &&
494 MoveFileExW(tmpfileW
, path
, MOVEFILE_DELAY_UNTIL_REBOOT
))
496 data
->package
->need_reboot_at_end
= 1;
500 WARN("failed to schedule rename operation %s (error %d)\n", debugstr_w(path
), GetLastError());
501 DeleteFileW( tmpfileW
);
506 WARN("failed to create %s (error %d)\n", debugstr_w(path
), err
);
512 return (INT_PTR
)handle
;
515 static INT_PTR
cabinet_close_file_info(FDINOTIFICATIONTYPE fdint
,
516 PFDINOTIFICATION pfdin
)
518 MSICABDATA
*data
= pfdin
->pv
;
521 HANDLE handle
= (HANDLE
)pfdin
->hf
;
523 data
->mi
->is_continuous
= FALSE
;
525 if (!DosDateTimeToFileTime(pfdin
->date
, pfdin
->time
, &ft
))
527 if (!LocalFileTimeToFileTime(&ft
, &ftLocal
))
529 if (!SetFileTime(handle
, &ftLocal
, 0, &ftLocal
))
534 data
->cb(data
->package
, data
->curfile
, MSICABEXTRACT_FILEEXTRACTED
, NULL
, NULL
,
537 msi_free(data
->curfile
);
538 data
->curfile
= NULL
;
543 static INT_PTR CDECL
cabinet_notify(FDINOTIFICATIONTYPE fdint
, PFDINOTIFICATION pfdin
)
547 case fdintPARTIAL_FILE
:
548 return cabinet_partial_file(fdint
, pfdin
);
550 case fdintNEXT_CABINET
:
551 return cabinet_next_cabinet(fdint
, pfdin
);
554 return cabinet_copy_file(fdint
, pfdin
);
556 case fdintCLOSE_FILE_INFO
:
557 return cabinet_close_file_info(fdint
, pfdin
);
564 static INT_PTR CDECL
cabinet_notify_stream( FDINOTIFICATIONTYPE fdint
, PFDINOTIFICATION pfdin
)
568 case fdintPARTIAL_FILE
:
569 return cabinet_partial_file( fdint
, pfdin
);
571 case fdintNEXT_CABINET
:
572 return cabinet_next_cabinet_stream( fdint
, pfdin
);
575 return cabinet_copy_file( fdint
, pfdin
);
577 case fdintCLOSE_FILE_INFO
:
578 return cabinet_close_file_info( fdint
, pfdin
);
580 case fdintCABINET_INFO
:
584 ERR("Unexpected notification %d\n", fdint
);
589 static BOOL
extract_cabinet( MSIPACKAGE
* package
, MSIMEDIAINFO
*mi
, LPVOID data
)
591 LPSTR cabinet
, cab_path
= NULL
;
596 TRACE("extracting %s disk id %u\n", debugstr_w(mi
->cabinet
), mi
->disk_id
);
598 hfdi
= FDICreate( cabinet_alloc
, cabinet_free
, cabinet_open
, cabinet_read
,
599 cabinet_write
, cabinet_close
, cabinet_seek
, 0, &erf
);
602 ERR("FDICreate failed\n");
606 cabinet
= strdupWtoA( mi
->cabinet
);
610 cab_path
= strdupWtoA( mi
->sourcedir
);
614 ret
= FDICopy( hfdi
, cabinet
, cab_path
, 0, cabinet_notify
, NULL
, data
);
616 ERR("FDICopy failed\n");
621 msi_free( cab_path
);
624 mi
->is_extracted
= TRUE
;
629 static BOOL
extract_cabinet_stream( MSIPACKAGE
*package
, MSIMEDIAINFO
*mi
, LPVOID data
)
631 static char filename
[] = {'<','S','T','R','E','A','M','>',0};
636 TRACE("extracting %s disk id %u\n", debugstr_w(mi
->cabinet
), mi
->disk_id
);
638 hfdi
= FDICreate( cabinet_alloc
, cabinet_free
, cabinet_open_stream
, cabinet_read_stream
,
639 cabinet_write
, cabinet_close_stream
, cabinet_seek_stream
, 0, &erf
);
642 ERR("FDICreate failed\n");
646 package_disk
.package
= package
;
647 package_disk
.id
= mi
->disk_id
;
649 ret
= FDICopy( hfdi
, filename
, NULL
, 0, cabinet_notify_stream
, NULL
, data
);
650 if (!ret
) ERR("FDICopy failed\n");
653 if (ret
) mi
->is_extracted
= TRUE
;
657 /***********************************************************************
660 * Extract files from a cabinet file or stream.
662 BOOL
msi_cabextract(MSIPACKAGE
* package
, MSIMEDIAINFO
*mi
, LPVOID data
)
664 if (mi
->cabinet
[0] == '#')
666 return extract_cabinet_stream( package
, mi
, data
);
668 return extract_cabinet( package
, mi
, data
);
671 void msi_free_media_info(MSIMEDIAINFO
*mi
)
673 msi_free(mi
->disk_prompt
);
674 msi_free(mi
->cabinet
);
675 msi_free(mi
->volume_label
);
676 msi_free(mi
->first_volume
);
680 static UINT
get_drive_type(const WCHAR
*path
)
682 WCHAR root
[MAX_PATH
+ 1];
685 PathStripToRootW(root
);
686 PathAddBackslashW(root
);
688 return GetDriveTypeW(root
);
691 UINT
msi_load_media_info(MSIPACKAGE
*package
, UINT Sequence
, MSIMEDIAINFO
*mi
)
693 static const WCHAR query
[] = {
694 'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ','`','M','e','d','i','a','`',' ',
695 'W','H','E','R','E',' ','`','L','a','s','t','S','e','q','u','e','n','c','e','`',' ',
696 '>','=',' ','%','i',' ','O','R','D','E','R',' ','B','Y',' ','`','D','i','s','k','I','d','`',0};
698 LPWSTR source_dir
, source
;
701 if (Sequence
<= mi
->last_sequence
) /* already loaded */
702 return ERROR_SUCCESS
;
704 row
= MSI_QueryGetRecord(package
->db
, query
, Sequence
);
707 TRACE("Unable to query row\n");
708 return ERROR_FUNCTION_FAILED
;
711 mi
->is_extracted
= FALSE
;
712 mi
->disk_id
= MSI_RecordGetInteger(row
, 1);
713 mi
->last_sequence
= MSI_RecordGetInteger(row
, 2);
714 msi_free(mi
->disk_prompt
);
715 mi
->disk_prompt
= strdupW(MSI_RecordGetString(row
, 3));
716 msi_free(mi
->cabinet
);
717 mi
->cabinet
= strdupW(MSI_RecordGetString(row
, 4));
718 msi_free(mi
->volume_label
);
719 mi
->volume_label
= strdupW(MSI_RecordGetString(row
, 5));
720 msiobj_release(&row
->hdr
);
722 if (!mi
->first_volume
)
723 mi
->first_volume
= strdupW(mi
->volume_label
);
725 msi_set_sourcedir_props(package
, FALSE
);
726 source_dir
= msi_dup_property(package
->db
, szSourceDir
);
727 lstrcpyW(mi
->sourcedir
, source_dir
);
728 PathAddBackslashW(mi
->sourcedir
);
729 mi
->type
= get_drive_type(source_dir
);
731 options
= MSICODE_PRODUCT
;
732 if (mi
->type
== DRIVE_CDROM
|| mi
->type
== DRIVE_REMOVABLE
)
735 options
|= MSISOURCETYPE_MEDIA
;
737 else if (package
->BaseURL
&& UrlIsW(package
->BaseURL
, URLIS_URL
))
739 source
= package
->BaseURL
;
740 options
|= MSISOURCETYPE_URL
;
744 source
= mi
->sourcedir
;
745 options
|= MSISOURCETYPE_NETWORK
;
748 msi_package_add_media_disk(package
, package
->Context
,
749 MSICODE_PRODUCT
, mi
->disk_id
,
750 mi
->volume_label
, mi
->disk_prompt
);
752 msi_package_add_info(package
, package
->Context
,
753 options
, INSTALLPROPERTY_LASTUSEDSOURCEW
, source
);
755 msi_free(source_dir
);
756 TRACE("sequence %u -> cabinet %s disk id %u\n", Sequence
, debugstr_w(mi
->cabinet
), mi
->disk_id
);
757 return ERROR_SUCCESS
;
760 /* FIXME: search URL sources as well */
761 static UINT
find_published_source(MSIPACKAGE
*package
, MSIMEDIAINFO
*mi
)
763 WCHAR source
[MAX_PATH
];
764 WCHAR volume
[MAX_PATH
];
765 WCHAR prompt
[MAX_PATH
];
766 DWORD volumesz
, promptsz
;
767 DWORD index
, size
, id
;
772 r
= MsiSourceListGetInfoW(package
->ProductCode
, NULL
,
773 package
->Context
, MSICODE_PRODUCT
,
774 INSTALLPROPERTY_LASTUSEDTYPEW
, last_type
, &size
);
775 if (r
!= ERROR_SUCCESS
)
779 r
= MsiSourceListGetInfoW(package
->ProductCode
, NULL
,
780 package
->Context
, MSICODE_PRODUCT
,
781 INSTALLPROPERTY_LASTUSEDSOURCEW
, source
, &size
);
782 if (r
!= ERROR_SUCCESS
)
785 if (last_type
[0] == 'n')
787 WCHAR cabinet_file
[MAX_PATH
];
788 BOOL check_all
= FALSE
;
794 while (MsiSourceListEnumSourcesW(package
->ProductCode
, NULL
,
796 MSISOURCETYPE_NETWORK
, index
++,
797 volume
, &volumesz
) == ERROR_SUCCESS
)
799 if (check_all
|| !strncmpiW(source
, volume
, strlenW(source
)))
801 lstrcpyW(cabinet_file
, volume
);
802 PathAddBackslashW(cabinet_file
);
803 lstrcatW(cabinet_file
, mi
->cabinet
);
805 if (GetFileAttributesW(cabinet_file
) == INVALID_FILE_ATTRIBUTES
)
813 lstrcpyW(mi
->sourcedir
, volume
);
814 PathAddBackslashW(mi
->sourcedir
);
815 TRACE("Found network source %s\n", debugstr_w(mi
->sourcedir
));
816 return ERROR_SUCCESS
;
830 while (MsiSourceListEnumMediaDisksW(package
->ProductCode
, NULL
,
832 MSICODE_PRODUCT
, index
++, &id
,
833 volume
, &volumesz
, prompt
, &promptsz
) == ERROR_SUCCESS
)
836 msi_free( mi
->volume_label
);
837 if (!(mi
->volume_label
= msi_alloc( ++volumesz
* sizeof(WCHAR
) ))) return ERROR_OUTOFMEMORY
;
838 strcpyW( mi
->volume_label
, volume
);
840 msi_free( mi
->disk_prompt
);
841 if (!(mi
->disk_prompt
= msi_alloc( ++promptsz
* sizeof(WCHAR
) ))) return ERROR_OUTOFMEMORY
;
842 strcpyW( mi
->disk_prompt
, prompt
);
844 if (source_matches_volume(mi
, source
))
846 /* FIXME: what about SourceDir */
847 lstrcpyW(mi
->sourcedir
, source
);
848 PathAddBackslashW(mi
->sourcedir
);
849 TRACE("Found disk source %s\n", debugstr_w(mi
->sourcedir
));
850 return ERROR_SUCCESS
;
854 return ERROR_FUNCTION_FAILED
;
857 UINT
ready_media( MSIPACKAGE
*package
, BOOL compressed
, MSIMEDIAINFO
*mi
)
860 WCHAR
*cabinet_file
= NULL
;
862 /* media info for continuous cabinet is already loaded */
863 if (mi
->is_continuous
) return ERROR_SUCCESS
;
867 /* cabinet is internal, no checks needed */
868 if (mi
->cabinet
[0] == '#') return ERROR_SUCCESS
;
870 if (!(cabinet_file
= get_cabinet_filename( mi
))) return ERROR_OUTOFMEMORY
;
872 /* package should be downloaded */
873 if (compressed
&& GetFileAttributesW( cabinet_file
) == INVALID_FILE_ATTRIBUTES
&&
874 package
->BaseURL
&& UrlIsW( package
->BaseURL
, URLIS_URL
))
876 WCHAR temppath
[MAX_PATH
], *p
;
878 if ((rc
= msi_download_file( cabinet_file
, temppath
)) != ERROR_SUCCESS
)
880 ERR("failed to download %s (%u)\n", debugstr_w(cabinet_file
), rc
);
881 msi_free( cabinet_file
);
884 if ((p
= strrchrW( temppath
, '\\' ))) *p
= 0;
885 strcpyW( mi
->sourcedir
, temppath
);
886 PathAddBackslashW( mi
->sourcedir
);
887 msi_free( mi
->cabinet
);
888 mi
->cabinet
= strdupW( p
+ 1 );
889 msi_free( cabinet_file
);
890 return ERROR_SUCCESS
;
893 /* check volume matches, change media if not */
894 if (mi
->volume_label
&& mi
->disk_id
> 1 && strcmpW( mi
->first_volume
, mi
->volume_label
))
896 WCHAR
*source
= msi_dup_property( package
->db
, szSourceDir
);
897 BOOL match
= source_matches_volume( mi
, source
);
900 if (!match
&& (mi
->type
== DRIVE_CDROM
|| mi
->type
== DRIVE_REMOVABLE
))
902 if ((rc
= msi_change_media( package
, mi
)) != ERROR_SUCCESS
)
904 msi_free( cabinet_file
);
911 if (compressed
&& GetFileAttributesW( cabinet_file
) == INVALID_FILE_ATTRIBUTES
)
913 if ((rc
= find_published_source( package
, mi
)) != ERROR_SUCCESS
)
915 ERR("cabinet not found: %s\n", debugstr_w(cabinet_file
));
916 msi_free( cabinet_file
);
917 return ERROR_INSTALL_FAILURE
;
921 msi_free( cabinet_file
);
922 return ERROR_SUCCESS
;
925 UINT
msi_add_cabinet_stream( MSIPACKAGE
*package
, UINT disk_id
, IStorage
*storage
, const WCHAR
*name
)
927 MSICABINETSTREAM
*cab
, *item
;
929 TRACE("%p, %u, %p, %s\n", package
, disk_id
, storage
, debugstr_w(name
));
931 LIST_FOR_EACH_ENTRY( item
, &package
->cabinet_streams
, MSICABINETSTREAM
, entry
)
933 if (item
->disk_id
== disk_id
)
935 TRACE("duplicate disk id %u\n", disk_id
);
936 return ERROR_FUNCTION_FAILED
;
939 if (!(cab
= msi_alloc( sizeof(*cab
) ))) return ERROR_OUTOFMEMORY
;
940 if (!(cab
->stream
= msi_alloc( (strlenW( name
) + 1) * sizeof(WCHAR
) )))
943 return ERROR_OUTOFMEMORY
;
945 strcpyW( cab
->stream
, name
);
946 cab
->disk_id
= disk_id
;
947 cab
->storage
= storage
;
948 IStorage_AddRef( storage
);
949 list_add_tail( &package
->cabinet_streams
, &cab
->entry
);
951 return ERROR_SUCCESS
;