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
))
364 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
));
366 /* Use cabinet name from the media table */
367 next_cab
= strdupWtoA(mi
->cabinet
);
368 /* Modify path to cabinet file with full filename (psz3 points to a 256 bytes buffer that can be modified contrary to psz1 and psz2) */
369 length
= strlen(pfdin
->psz3
) + 1 + strlen(next_cab
) + 1;
372 WARN("Cannot update next cabinet filename with a string size %u > 256\n", length
);
378 strcat(pfdin
->psz3
, "\\");
379 strcat(pfdin
->psz3
, next_cab
);
381 /* Path psz3 and cabinet psz1 are concatenated by FDI so just reset psz1 */
386 if (!(cabinet_file
= get_cabinet_filename(mi
)))
389 TRACE("Searching for %s\n", debugstr_w(cabinet_file
));
392 if (GetFileAttributesW(cabinet_file
) == INVALID_FILE_ATTRIBUTES
)
394 if (msi_change_media(data
->package
, mi
) != ERROR_SUCCESS
)
400 msi_free(cabinet_file
);
404 static INT_PTR
cabinet_next_cabinet_stream( FDINOTIFICATIONTYPE fdint
,
405 PFDINOTIFICATION pfdin
)
407 MSICABDATA
*data
= pfdin
->pv
;
408 MSIMEDIAINFO
*mi
= data
->mi
;
411 msi_free( mi
->disk_prompt
);
412 msi_free( mi
->cabinet
);
413 msi_free( mi
->volume_label
);
414 mi
->disk_prompt
= NULL
;
416 mi
->volume_label
= NULL
;
419 mi
->is_continuous
= TRUE
;
421 rc
= msi_media_get_disk_info( data
->package
, mi
);
422 if (rc
!= ERROR_SUCCESS
)
424 ERR("Failed to get next cabinet information: %u\n", rc
);
427 package_disk
.id
= mi
->disk_id
;
429 TRACE("next cabinet is %s disk id %u\n", debugstr_w(mi
->cabinet
), mi
->disk_id
);
433 static INT_PTR
cabinet_copy_file(FDINOTIFICATIONTYPE fdint
,
434 PFDINOTIFICATION pfdin
)
436 MSICABDATA
*data
= pfdin
->pv
;
441 data
->curfile
= strdupAtoW(pfdin
->psz1
);
442 if (!data
->cb(data
->package
, data
->curfile
, MSICABEXTRACT_BEGINEXTRACT
, &path
,
445 /* We're not extracting this file, so free the filename. */
446 msi_free(data
->curfile
);
447 data
->curfile
= NULL
;
451 TRACE("extracting %s -> %s\n", debugstr_w(data
->curfile
), debugstr_w(path
));
453 attrs
= attrs
& (FILE_ATTRIBUTE_READONLY
|FILE_ATTRIBUTE_HIDDEN
|FILE_ATTRIBUTE_SYSTEM
);
454 if (!attrs
) attrs
= FILE_ATTRIBUTE_NORMAL
;
456 handle
= CreateFileW(path
, GENERIC_READ
| GENERIC_WRITE
, 0,
457 NULL
, CREATE_ALWAYS
, attrs
, NULL
);
458 if (handle
== INVALID_HANDLE_VALUE
)
460 DWORD err
= GetLastError();
461 DWORD attrs2
= GetFileAttributesW(path
);
463 if (attrs2
== INVALID_FILE_ATTRIBUTES
)
465 ERR("failed to create %s (error %d)\n", debugstr_w(path
), err
);
468 else if (err
== ERROR_ACCESS_DENIED
&& (attrs2
& FILE_ATTRIBUTE_READONLY
))
470 TRACE("removing read-only attribute on %s\n", debugstr_w(path
));
471 SetFileAttributesW( path
, attrs2
& ~FILE_ATTRIBUTE_READONLY
);
472 handle
= CreateFileW(path
, GENERIC_READ
| GENERIC_WRITE
, 0, NULL
, CREATE_ALWAYS
, attrs2
, NULL
);
474 if (handle
!= INVALID_HANDLE_VALUE
) goto done
;
475 err
= GetLastError();
477 if (err
== ERROR_SHARING_VIOLATION
|| err
== ERROR_USER_MAPPED_FILE
)
479 WCHAR
*tmpfileW
, *tmppathW
, *p
;
482 TRACE("file in use, scheduling rename operation\n");
484 if (!(tmppathW
= strdupW( path
))) return ERROR_OUTOFMEMORY
;
485 if ((p
= strrchrW(tmppathW
, '\\'))) *p
= 0;
486 len
= strlenW( tmppathW
) + 16;
487 if (!(tmpfileW
= msi_alloc(len
* sizeof(WCHAR
))))
489 msi_free( tmppathW
);
490 return ERROR_OUTOFMEMORY
;
492 if (!GetTempFileNameW(tmppathW
, szMsi
, 0, tmpfileW
)) tmpfileW
[0] = 0;
493 msi_free( tmppathW
);
495 handle
= CreateFileW(tmpfileW
, GENERIC_READ
| GENERIC_WRITE
, 0, NULL
, CREATE_ALWAYS
, attrs
, NULL
);
497 if (handle
!= INVALID_HANDLE_VALUE
&&
498 MoveFileExW(path
, NULL
, MOVEFILE_DELAY_UNTIL_REBOOT
) &&
499 MoveFileExW(tmpfileW
, path
, MOVEFILE_DELAY_UNTIL_REBOOT
))
501 data
->package
->need_reboot_at_end
= 1;
505 WARN("failed to schedule rename operation %s (error %d)\n", debugstr_w(path
), GetLastError());
506 DeleteFileW( tmpfileW
);
511 WARN("failed to create %s (error %d)\n", debugstr_w(path
), err
);
517 return (INT_PTR
)handle
;
520 static INT_PTR
cabinet_close_file_info(FDINOTIFICATIONTYPE fdint
,
521 PFDINOTIFICATION pfdin
)
523 MSICABDATA
*data
= pfdin
->pv
;
526 HANDLE handle
= (HANDLE
)pfdin
->hf
;
528 data
->mi
->is_continuous
= FALSE
;
530 if (!DosDateTimeToFileTime(pfdin
->date
, pfdin
->time
, &ft
))
532 if (!LocalFileTimeToFileTime(&ft
, &ftLocal
))
534 if (!SetFileTime(handle
, &ftLocal
, 0, &ftLocal
))
539 data
->cb(data
->package
, data
->curfile
, MSICABEXTRACT_FILEEXTRACTED
, NULL
, NULL
,
542 msi_free(data
->curfile
);
543 data
->curfile
= NULL
;
548 static INT_PTR CDECL
cabinet_notify(FDINOTIFICATIONTYPE fdint
, PFDINOTIFICATION pfdin
)
552 case fdintPARTIAL_FILE
:
553 return cabinet_partial_file(fdint
, pfdin
);
555 case fdintNEXT_CABINET
:
556 return cabinet_next_cabinet(fdint
, pfdin
);
559 return cabinet_copy_file(fdint
, pfdin
);
561 case fdintCLOSE_FILE_INFO
:
562 return cabinet_close_file_info(fdint
, pfdin
);
569 static INT_PTR CDECL
cabinet_notify_stream( FDINOTIFICATIONTYPE fdint
, PFDINOTIFICATION pfdin
)
573 case fdintPARTIAL_FILE
:
574 return cabinet_partial_file( fdint
, pfdin
);
576 case fdintNEXT_CABINET
:
577 return cabinet_next_cabinet_stream( fdint
, pfdin
);
580 return cabinet_copy_file( fdint
, pfdin
);
582 case fdintCLOSE_FILE_INFO
:
583 return cabinet_close_file_info( fdint
, pfdin
);
585 case fdintCABINET_INFO
:
589 ERR("Unexpected notification %d\n", fdint
);
594 static BOOL
extract_cabinet( MSIPACKAGE
* package
, MSIMEDIAINFO
*mi
, LPVOID data
)
596 LPSTR cabinet
, cab_path
= NULL
;
601 TRACE("extracting %s disk id %u\n", debugstr_w(mi
->cabinet
), mi
->disk_id
);
603 hfdi
= FDICreate( cabinet_alloc
, cabinet_free
, cabinet_open
, cabinet_read
,
604 cabinet_write
, cabinet_close
, cabinet_seek
, 0, &erf
);
607 ERR("FDICreate failed\n");
611 cabinet
= strdupWtoA( mi
->cabinet
);
615 cab_path
= strdupWtoA( mi
->sourcedir
);
619 ret
= FDICopy( hfdi
, cabinet
, cab_path
, 0, cabinet_notify
, NULL
, data
);
621 ERR("FDICopy failed\n");
626 msi_free( cab_path
);
629 mi
->is_extracted
= TRUE
;
634 static BOOL
extract_cabinet_stream( MSIPACKAGE
*package
, MSIMEDIAINFO
*mi
, LPVOID data
)
636 static char filename
[] = {'<','S','T','R','E','A','M','>',0};
641 TRACE("extracting %s disk id %u\n", debugstr_w(mi
->cabinet
), mi
->disk_id
);
643 hfdi
= FDICreate( cabinet_alloc
, cabinet_free
, cabinet_open_stream
, cabinet_read_stream
,
644 cabinet_write
, cabinet_close_stream
, cabinet_seek_stream
, 0, &erf
);
647 ERR("FDICreate failed\n");
651 package_disk
.package
= package
;
652 package_disk
.id
= mi
->disk_id
;
654 ret
= FDICopy( hfdi
, filename
, NULL
, 0, cabinet_notify_stream
, NULL
, data
);
655 if (!ret
) ERR("FDICopy failed\n");
658 if (ret
) mi
->is_extracted
= TRUE
;
662 /***********************************************************************
665 * Extract files from a cabinet file or stream.
667 BOOL
msi_cabextract(MSIPACKAGE
* package
, MSIMEDIAINFO
*mi
, LPVOID data
)
669 if (mi
->cabinet
[0] == '#')
671 return extract_cabinet_stream( package
, mi
, data
);
673 return extract_cabinet( package
, mi
, data
);
676 void msi_free_media_info(MSIMEDIAINFO
*mi
)
678 msi_free(mi
->disk_prompt
);
679 msi_free(mi
->cabinet
);
680 msi_free(mi
->volume_label
);
681 msi_free(mi
->first_volume
);
685 static UINT
get_drive_type(const WCHAR
*path
)
687 WCHAR root
[MAX_PATH
+ 1];
690 PathStripToRootW(root
);
691 PathAddBackslashW(root
);
693 return GetDriveTypeW(root
);
696 UINT
msi_load_media_info(MSIPACKAGE
*package
, UINT Sequence
, MSIMEDIAINFO
*mi
)
698 static const WCHAR query
[] = {
699 'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ','`','M','e','d','i','a','`',' ',
700 'W','H','E','R','E',' ','`','L','a','s','t','S','e','q','u','e','n','c','e','`',' ',
701 '>','=',' ','%','i',' ','O','R','D','E','R',' ','B','Y',' ','`','D','i','s','k','I','d','`',0};
703 LPWSTR source_dir
, source
;
706 if (Sequence
<= mi
->last_sequence
) /* already loaded */
707 return ERROR_SUCCESS
;
709 row
= MSI_QueryGetRecord(package
->db
, query
, Sequence
);
712 TRACE("Unable to query row\n");
713 return ERROR_FUNCTION_FAILED
;
716 mi
->is_extracted
= FALSE
;
717 mi
->disk_id
= MSI_RecordGetInteger(row
, 1);
718 mi
->last_sequence
= MSI_RecordGetInteger(row
, 2);
719 msi_free(mi
->disk_prompt
);
720 mi
->disk_prompt
= strdupW(MSI_RecordGetString(row
, 3));
721 msi_free(mi
->cabinet
);
722 mi
->cabinet
= strdupW(MSI_RecordGetString(row
, 4));
723 msi_free(mi
->volume_label
);
724 mi
->volume_label
= strdupW(MSI_RecordGetString(row
, 5));
725 msiobj_release(&row
->hdr
);
727 if (!mi
->first_volume
)
728 mi
->first_volume
= strdupW(mi
->volume_label
);
730 msi_set_sourcedir_props(package
, FALSE
);
731 source_dir
= msi_dup_property(package
->db
, szSourceDir
);
732 lstrcpyW(mi
->sourcedir
, source_dir
);
733 PathAddBackslashW(mi
->sourcedir
);
734 mi
->type
= get_drive_type(source_dir
);
736 options
= MSICODE_PRODUCT
;
737 if (mi
->type
== DRIVE_CDROM
|| mi
->type
== DRIVE_REMOVABLE
)
740 options
|= MSISOURCETYPE_MEDIA
;
742 else if (package
->BaseURL
&& UrlIsW(package
->BaseURL
, URLIS_URL
))
744 source
= package
->BaseURL
;
745 options
|= MSISOURCETYPE_URL
;
749 source
= mi
->sourcedir
;
750 options
|= MSISOURCETYPE_NETWORK
;
753 msi_package_add_media_disk(package
, package
->Context
,
754 MSICODE_PRODUCT
, mi
->disk_id
,
755 mi
->volume_label
, mi
->disk_prompt
);
757 msi_package_add_info(package
, package
->Context
,
758 options
, INSTALLPROPERTY_LASTUSEDSOURCEW
, source
);
760 msi_free(source_dir
);
761 TRACE("sequence %u -> cabinet %s disk id %u\n", Sequence
, debugstr_w(mi
->cabinet
), mi
->disk_id
);
762 return ERROR_SUCCESS
;
765 /* FIXME: search URL sources as well */
766 static UINT
find_published_source(MSIPACKAGE
*package
, MSIMEDIAINFO
*mi
)
768 WCHAR source
[MAX_PATH
];
769 WCHAR volume
[MAX_PATH
];
770 WCHAR prompt
[MAX_PATH
];
771 DWORD volumesz
, promptsz
;
772 DWORD index
, size
, id
;
777 r
= MsiSourceListGetInfoW(package
->ProductCode
, NULL
,
778 package
->Context
, MSICODE_PRODUCT
,
779 INSTALLPROPERTY_LASTUSEDTYPEW
, last_type
, &size
);
780 if (r
!= ERROR_SUCCESS
)
784 r
= MsiSourceListGetInfoW(package
->ProductCode
, NULL
,
785 package
->Context
, MSICODE_PRODUCT
,
786 INSTALLPROPERTY_LASTUSEDSOURCEW
, source
, &size
);
787 if (r
!= ERROR_SUCCESS
)
790 if (last_type
[0] == 'n')
792 WCHAR cabinet_file
[MAX_PATH
];
793 BOOL check_all
= FALSE
;
799 while (MsiSourceListEnumSourcesW(package
->ProductCode
, NULL
,
801 MSISOURCETYPE_NETWORK
, index
++,
802 volume
, &volumesz
) == ERROR_SUCCESS
)
804 if (check_all
|| !strncmpiW(source
, volume
, strlenW(source
)))
806 lstrcpyW(cabinet_file
, volume
);
807 PathAddBackslashW(cabinet_file
);
808 lstrcatW(cabinet_file
, mi
->cabinet
);
810 if (GetFileAttributesW(cabinet_file
) == INVALID_FILE_ATTRIBUTES
)
818 lstrcpyW(mi
->sourcedir
, volume
);
819 PathAddBackslashW(mi
->sourcedir
);
820 TRACE("Found network source %s\n", debugstr_w(mi
->sourcedir
));
821 return ERROR_SUCCESS
;
835 while (MsiSourceListEnumMediaDisksW(package
->ProductCode
, NULL
,
837 MSICODE_PRODUCT
, index
++, &id
,
838 volume
, &volumesz
, prompt
, &promptsz
) == ERROR_SUCCESS
)
841 msi_free( mi
->volume_label
);
842 if (!(mi
->volume_label
= msi_alloc( ++volumesz
* sizeof(WCHAR
) ))) return ERROR_OUTOFMEMORY
;
843 strcpyW( mi
->volume_label
, volume
);
845 msi_free( mi
->disk_prompt
);
846 if (!(mi
->disk_prompt
= msi_alloc( ++promptsz
* sizeof(WCHAR
) ))) return ERROR_OUTOFMEMORY
;
847 strcpyW( mi
->disk_prompt
, prompt
);
849 if (source_matches_volume(mi
, source
))
851 /* FIXME: what about SourceDir */
852 lstrcpyW(mi
->sourcedir
, source
);
853 PathAddBackslashW(mi
->sourcedir
);
854 TRACE("Found disk source %s\n", debugstr_w(mi
->sourcedir
));
855 return ERROR_SUCCESS
;
859 return ERROR_FUNCTION_FAILED
;
862 UINT
ready_media( MSIPACKAGE
*package
, BOOL compressed
, MSIMEDIAINFO
*mi
)
865 WCHAR
*cabinet_file
= NULL
;
867 /* media info for continuous cabinet is already loaded */
868 if (mi
->is_continuous
) return ERROR_SUCCESS
;
872 /* cabinet is internal, no checks needed */
873 if (mi
->cabinet
[0] == '#') return ERROR_SUCCESS
;
875 if (!(cabinet_file
= get_cabinet_filename( mi
))) return ERROR_OUTOFMEMORY
;
877 /* package should be downloaded */
878 if (compressed
&& GetFileAttributesW( cabinet_file
) == INVALID_FILE_ATTRIBUTES
&&
879 package
->BaseURL
&& UrlIsW( package
->BaseURL
, URLIS_URL
))
881 WCHAR temppath
[MAX_PATH
], *p
;
883 if ((rc
= msi_download_file( cabinet_file
, temppath
)) != ERROR_SUCCESS
)
885 ERR("failed to download %s (%u)\n", debugstr_w(cabinet_file
), rc
);
886 msi_free( cabinet_file
);
889 if ((p
= strrchrW( temppath
, '\\' ))) *p
= 0;
890 strcpyW( mi
->sourcedir
, temppath
);
891 PathAddBackslashW( mi
->sourcedir
);
892 msi_free( mi
->cabinet
);
893 mi
->cabinet
= strdupW( p
+ 1 );
894 msi_free( cabinet_file
);
895 return ERROR_SUCCESS
;
898 /* check volume matches, change media if not */
899 if (mi
->volume_label
&& mi
->disk_id
> 1 && strcmpW( mi
->first_volume
, mi
->volume_label
))
901 WCHAR
*source
= msi_dup_property( package
->db
, szSourceDir
);
902 BOOL match
= source_matches_volume( mi
, source
);
905 if (!match
&& (mi
->type
== DRIVE_CDROM
|| mi
->type
== DRIVE_REMOVABLE
))
907 if ((rc
= msi_change_media( package
, mi
)) != ERROR_SUCCESS
)
909 msi_free( cabinet_file
);
916 if (compressed
&& GetFileAttributesW( cabinet_file
) == INVALID_FILE_ATTRIBUTES
)
918 if ((rc
= find_published_source( package
, mi
)) != ERROR_SUCCESS
)
920 ERR("cabinet not found: %s\n", debugstr_w(cabinet_file
));
921 msi_free( cabinet_file
);
922 return ERROR_INSTALL_FAILURE
;
926 msi_free( cabinet_file
);
927 return ERROR_SUCCESS
;
930 UINT
msi_add_cabinet_stream( MSIPACKAGE
*package
, UINT disk_id
, IStorage
*storage
, const WCHAR
*name
)
932 MSICABINETSTREAM
*cab
, *item
;
934 TRACE("%p, %u, %p, %s\n", package
, disk_id
, storage
, debugstr_w(name
));
936 LIST_FOR_EACH_ENTRY( item
, &package
->cabinet_streams
, MSICABINETSTREAM
, entry
)
938 if (item
->disk_id
== disk_id
)
940 TRACE("duplicate disk id %u\n", disk_id
);
941 return ERROR_FUNCTION_FAILED
;
944 if (!(cab
= msi_alloc( sizeof(*cab
) ))) return ERROR_OUTOFMEMORY
;
945 if (!(cab
->stream
= msi_alloc( (strlenW( name
) + 1) * sizeof(WCHAR
) )))
948 return ERROR_OUTOFMEMORY
;
950 strcpyW( cab
->stream
, name
);
951 cab
->disk_id
= disk_id
;
952 cab
->storage
= storage
;
953 IStorage_AddRef( storage
);
954 list_add_tail( &package
->cabinet_streams
, &cab
->entry
);
956 return ERROR_SUCCESS
;