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], root
[MAX_PATH
+ 1];
61 strcpyW(root
, source_root
);
62 PathStripToRootW(root
);
63 PathAddBackslashW(root
);
65 if (!GetVolumeInformationW(root
, volume_name
, MAX_PATH
+ 1, NULL
, NULL
, NULL
, NULL
, 0))
67 WARN("failed to get volume information for %s (%u)\n", debugstr_w(root
), GetLastError());
71 len
= strlenW( volume_name
);
72 len2
= strlenW( mi
->volume_label
);
73 if (len2
> len
) return FALSE
;
74 p
= volume_name
+ len
- len2
;
76 return !strcmpiW( mi
->volume_label
, p
);
79 static UINT
msi_change_media(MSIPACKAGE
*package
, MSIMEDIAINFO
*mi
)
81 LPWSTR error
, error_dialog
;
83 UINT r
= ERROR_SUCCESS
;
85 static const WCHAR error_prop
[] = {'E','r','r','o','r','D','i','a','l','o','g',0};
87 if ((package
->ui_level
& INSTALLUILEVEL_MASK
) == INSTALLUILEVEL_NONE
&&
88 !gUIHandlerA
&& !gUIHandlerW
&& !gUIHandlerRecord
) return ERROR_SUCCESS
;
90 error
= msi_build_error_string(package
, 1302, 1, mi
->disk_prompt
);
91 error_dialog
= msi_dup_property(package
->db
, error_prop
);
92 source_dir
= msi_dup_property(package
->db
, szSourceDir
);
94 while (r
== ERROR_SUCCESS
&& !source_matches_volume(mi
, source_dir
))
96 r
= msi_spawn_error_dialog(package
, error_dialog
, error
);
100 gUIHandlerW(gUIContext
, MB_RETRYCANCEL
| INSTALLMESSAGE_ERROR
, error
);
102 else if (gUIHandlerA
)
104 char *msg
= strdupWtoA(error
);
105 gUIHandlerA(gUIContext
, MB_RETRYCANCEL
| INSTALLMESSAGE_ERROR
, msg
);
108 else if (gUIHandlerRecord
)
110 MSIHANDLE rec
= MsiCreateRecord(1);
111 MsiRecordSetStringW(rec
, 0, error
);
112 gUIHandlerRecord(gUIContext
, MB_RETRYCANCEL
| INSTALLMESSAGE_ERROR
, rec
);
118 msi_free(error_dialog
);
119 msi_free(source_dir
);
124 static MSICABINETSTREAM
*msi_get_cabinet_stream( MSIPACKAGE
*package
, UINT disk_id
)
126 MSICABINETSTREAM
*cab
;
128 LIST_FOR_EACH_ENTRY( cab
, &package
->cabinet_streams
, MSICABINETSTREAM
, entry
)
130 if (cab
->disk_id
== disk_id
) return cab
;
135 static void * CDECL
cabinet_alloc(ULONG cb
)
137 return msi_alloc(cb
);
140 static void CDECL
cabinet_free(void *pv
)
145 static INT_PTR CDECL
cabinet_open(char *pszFile
, int oflag
, int pmode
)
148 DWORD dwShareMode
= 0;
149 DWORD dwCreateDisposition
= OPEN_EXISTING
;
151 switch (oflag
& _O_ACCMODE
)
154 dwAccess
= GENERIC_READ
;
155 dwShareMode
= FILE_SHARE_READ
| FILE_SHARE_DELETE
;
158 dwAccess
= GENERIC_WRITE
;
159 dwShareMode
= FILE_SHARE_READ
| FILE_SHARE_WRITE
| FILE_SHARE_DELETE
;
162 dwAccess
= GENERIC_READ
| GENERIC_WRITE
;
163 dwShareMode
= FILE_SHARE_READ
| FILE_SHARE_WRITE
| FILE_SHARE_DELETE
;
167 if ((oflag
& (_O_CREAT
| _O_EXCL
)) == (_O_CREAT
| _O_EXCL
))
168 dwCreateDisposition
= CREATE_NEW
;
169 else if (oflag
& _O_CREAT
)
170 dwCreateDisposition
= CREATE_ALWAYS
;
172 return (INT_PTR
)CreateFileA(pszFile
, dwAccess
, dwShareMode
, NULL
,
173 dwCreateDisposition
, 0, NULL
);
176 static UINT CDECL
cabinet_read(INT_PTR hf
, void *pv
, UINT cb
)
178 HANDLE handle
= (HANDLE
)hf
;
181 if (ReadFile(handle
, pv
, cb
, &read
, NULL
))
187 static UINT CDECL
cabinet_write(INT_PTR hf
, void *pv
, UINT cb
)
189 HANDLE handle
= (HANDLE
)hf
;
192 if (WriteFile(handle
, pv
, cb
, &written
, NULL
))
198 static int CDECL
cabinet_close(INT_PTR hf
)
200 HANDLE handle
= (HANDLE
)hf
;
201 return CloseHandle(handle
) ? 0 : -1;
204 static LONG CDECL
cabinet_seek(INT_PTR hf
, LONG dist
, int seektype
)
206 HANDLE handle
= (HANDLE
)hf
;
207 /* flags are compatible and so are passed straight through */
208 return SetFilePointer(handle
, dist
, NULL
, seektype
);
217 static struct package_disk package_disk
;
219 static INT_PTR CDECL
cabinet_open_stream( char *pszFile
, int oflag
, int pmode
)
221 MSICABINETSTREAM
*cab
;
224 if (!(cab
= msi_get_cabinet_stream( package_disk
.package
, package_disk
.id
)))
226 WARN("failed to get cabinet stream\n");
229 if (cab
->storage
== package_disk
.package
->db
->storage
)
231 UINT r
= msi_get_stream( package_disk
.package
->db
, cab
->stream
+ 1, &stream
);
232 if (r
!= ERROR_SUCCESS
)
234 WARN("failed to get stream %u\n", r
);
238 else /* patch storage */
243 if (!(encoded
= encode_streamname( FALSE
, cab
->stream
+ 1 )))
245 WARN("failed to encode stream name\n");
248 hr
= IStorage_OpenStream( cab
->storage
, encoded
, NULL
, STGM_READ
|STGM_SHARE_EXCLUSIVE
, 0, &stream
);
252 WARN("failed to open stream 0x%08x\n", hr
);
256 return (INT_PTR
)stream
;
259 static UINT CDECL
cabinet_read_stream( INT_PTR hf
, void *pv
, UINT cb
)
261 IStream
*stm
= (IStream
*)hf
;
265 hr
= IStream_Read( stm
, pv
, cb
, &read
);
266 if (hr
== S_OK
|| hr
== S_FALSE
)
272 static int CDECL
cabinet_close_stream( INT_PTR hf
)
274 IStream
*stm
= (IStream
*)hf
;
275 IStream_Release( stm
);
279 static LONG CDECL
cabinet_seek_stream( INT_PTR hf
, LONG dist
, int seektype
)
281 IStream
*stm
= (IStream
*)hf
;
283 ULARGE_INTEGER newpos
;
286 move
.QuadPart
= dist
;
287 hr
= IStream_Seek( stm
, move
, seektype
, &newpos
);
290 if (newpos
.QuadPart
<= MAXLONG
) return newpos
.QuadPart
;
296 static UINT CDECL
msi_media_get_disk_info(MSIPACKAGE
*package
, MSIMEDIAINFO
*mi
)
300 static const WCHAR query
[] = {
301 'S','E','L','E','C','T',' ','*',' ', 'F','R','O','M',' ',
302 '`','M','e','d','i','a','`',' ','W','H','E','R','E',' ',
303 '`','D','i','s','k','I','d','`',' ','=',' ','%','i',0};
305 row
= MSI_QueryGetRecord(package
->db
, query
, mi
->disk_id
);
308 TRACE("Unable to query row\n");
309 return ERROR_FUNCTION_FAILED
;
312 mi
->disk_prompt
= strdupW(MSI_RecordGetString(row
, 3));
313 mi
->cabinet
= strdupW(MSI_RecordGetString(row
, 4));
314 mi
->volume_label
= strdupW(MSI_RecordGetString(row
, 5));
316 msiobj_release(&row
->hdr
);
317 return ERROR_SUCCESS
;
320 static INT_PTR
cabinet_partial_file(FDINOTIFICATIONTYPE fdint
,
321 PFDINOTIFICATION pfdin
)
323 MSICABDATA
*data
= pfdin
->pv
;
324 data
->mi
->is_continuous
= FALSE
;
328 static WCHAR
*get_cabinet_filename(MSIMEDIAINFO
*mi
)
333 len
= strlenW(mi
->sourcedir
) + strlenW(mi
->cabinet
) + 1;
334 if (!(ret
= msi_alloc(len
* sizeof(WCHAR
)))) return NULL
;
335 strcpyW(ret
, mi
->sourcedir
);
336 strcatW(ret
, mi
->cabinet
);
340 static INT_PTR
cabinet_next_cabinet(FDINOTIFICATIONTYPE fdint
,
341 PFDINOTIFICATION pfdin
)
343 MSICABDATA
*data
= pfdin
->pv
;
344 MSIMEDIAINFO
*mi
= data
->mi
;
345 LPWSTR cabinet_file
= NULL
, cab
= strdupAtoW(pfdin
->psz1
);
349 msi_free(mi
->disk_prompt
);
350 msi_free(mi
->cabinet
);
351 msi_free(mi
->volume_label
);
352 mi
->disk_prompt
= NULL
;
354 mi
->volume_label
= NULL
;
357 mi
->is_continuous
= TRUE
;
359 rc
= msi_media_get_disk_info(data
->package
, mi
);
360 if (rc
!= ERROR_SUCCESS
)
362 ERR("Failed to get next cabinet information: %d\n", rc
);
366 if (strcmpiW( mi
->cabinet
, cab
))
371 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
));
373 /* Use cabinet name from the media table */
374 next_cab
= strdupWtoA(mi
->cabinet
);
375 /* Modify path to cabinet file with full filename (psz3 points to a 256 bytes buffer that can be modified contrary to psz1 and psz2) */
376 length
= strlen(pfdin
->psz3
) + 1 + strlen(next_cab
) + 1;
379 WARN("Cannot update next cabinet filename with a string size %u > 256\n", length
);
385 strcat(pfdin
->psz3
, "\\");
386 strcat(pfdin
->psz3
, next_cab
);
388 /* Path psz3 and cabinet psz1 are concatenated by FDI so just reset psz1 */
393 if (!(cabinet_file
= get_cabinet_filename(mi
)))
396 TRACE("Searching for %s\n", debugstr_w(cabinet_file
));
399 if (GetFileAttributesW(cabinet_file
) == INVALID_FILE_ATTRIBUTES
)
401 if (msi_change_media(data
->package
, mi
) != ERROR_SUCCESS
)
407 msi_free(cabinet_file
);
411 static INT_PTR
cabinet_next_cabinet_stream( FDINOTIFICATIONTYPE fdint
,
412 PFDINOTIFICATION pfdin
)
414 MSICABDATA
*data
= pfdin
->pv
;
415 MSIMEDIAINFO
*mi
= data
->mi
;
418 msi_free( mi
->disk_prompt
);
419 msi_free( mi
->cabinet
);
420 msi_free( mi
->volume_label
);
421 mi
->disk_prompt
= NULL
;
423 mi
->volume_label
= NULL
;
426 mi
->is_continuous
= TRUE
;
428 rc
= msi_media_get_disk_info( data
->package
, mi
);
429 if (rc
!= ERROR_SUCCESS
)
431 ERR("Failed to get next cabinet information: %u\n", rc
);
434 package_disk
.id
= mi
->disk_id
;
436 TRACE("next cabinet is %s disk id %u\n", debugstr_w(mi
->cabinet
), mi
->disk_id
);
440 static INT_PTR
cabinet_copy_file(FDINOTIFICATIONTYPE fdint
,
441 PFDINOTIFICATION pfdin
)
443 MSICABDATA
*data
= pfdin
->pv
;
448 data
->curfile
= strdupAtoW(pfdin
->psz1
);
449 if (!data
->cb(data
->package
, data
->curfile
, MSICABEXTRACT_BEGINEXTRACT
, &path
,
452 /* We're not extracting this file, so free the filename. */
453 msi_free(data
->curfile
);
454 data
->curfile
= NULL
;
458 TRACE("extracting %s -> %s\n", debugstr_w(data
->curfile
), debugstr_w(path
));
460 attrs
= attrs
& (FILE_ATTRIBUTE_READONLY
|FILE_ATTRIBUTE_HIDDEN
|FILE_ATTRIBUTE_SYSTEM
);
461 if (!attrs
) attrs
= FILE_ATTRIBUTE_NORMAL
;
463 handle
= CreateFileW(path
, GENERIC_READ
| GENERIC_WRITE
, 0,
464 NULL
, CREATE_ALWAYS
, attrs
, NULL
);
465 if (handle
== INVALID_HANDLE_VALUE
)
467 DWORD err
= GetLastError();
468 DWORD attrs2
= GetFileAttributesW(path
);
470 if (attrs2
== INVALID_FILE_ATTRIBUTES
)
472 ERR("failed to create %s (error %d)\n", debugstr_w(path
), err
);
475 else if (err
== ERROR_ACCESS_DENIED
&& (attrs2
& FILE_ATTRIBUTE_READONLY
))
477 TRACE("removing read-only attribute on %s\n", debugstr_w(path
));
478 SetFileAttributesW( path
, attrs2
& ~FILE_ATTRIBUTE_READONLY
);
479 handle
= CreateFileW(path
, GENERIC_READ
| GENERIC_WRITE
, 0, NULL
, CREATE_ALWAYS
, attrs
, NULL
);
481 if (handle
!= INVALID_HANDLE_VALUE
) goto done
;
482 err
= GetLastError();
484 if (err
== ERROR_SHARING_VIOLATION
|| err
== ERROR_USER_MAPPED_FILE
)
486 WCHAR
*tmpfileW
, *tmppathW
, *p
;
489 TRACE("file in use, scheduling rename operation\n");
491 if (!(tmppathW
= strdupW( path
))) return ERROR_OUTOFMEMORY
;
492 if ((p
= strrchrW(tmppathW
, '\\'))) *p
= 0;
493 len
= strlenW( tmppathW
) + 16;
494 if (!(tmpfileW
= msi_alloc(len
* sizeof(WCHAR
))))
496 msi_free( tmppathW
);
497 return ERROR_OUTOFMEMORY
;
499 if (!GetTempFileNameW(tmppathW
, szMsi
, 0, tmpfileW
)) tmpfileW
[0] = 0;
500 msi_free( tmppathW
);
502 handle
= CreateFileW(tmpfileW
, GENERIC_READ
| GENERIC_WRITE
, 0, NULL
, CREATE_ALWAYS
, attrs
, NULL
);
504 if (handle
!= INVALID_HANDLE_VALUE
&&
505 MoveFileExW(path
, NULL
, MOVEFILE_DELAY_UNTIL_REBOOT
) &&
506 MoveFileExW(tmpfileW
, path
, MOVEFILE_DELAY_UNTIL_REBOOT
))
508 data
->package
->need_reboot_at_end
= 1;
512 WARN("failed to schedule rename operation %s (error %d)\n", debugstr_w(path
), GetLastError());
513 DeleteFileW( tmpfileW
);
518 WARN("failed to create %s (error %d)\n", debugstr_w(path
), err
);
524 return (INT_PTR
)handle
;
527 static INT_PTR
cabinet_close_file_info(FDINOTIFICATIONTYPE fdint
,
528 PFDINOTIFICATION pfdin
)
530 MSICABDATA
*data
= pfdin
->pv
;
533 HANDLE handle
= (HANDLE
)pfdin
->hf
;
535 data
->mi
->is_continuous
= FALSE
;
537 if (!DosDateTimeToFileTime(pfdin
->date
, pfdin
->time
, &ft
))
539 if (!LocalFileTimeToFileTime(&ft
, &ftLocal
))
541 if (!SetFileTime(handle
, &ftLocal
, 0, &ftLocal
))
546 data
->cb(data
->package
, data
->curfile
, MSICABEXTRACT_FILEEXTRACTED
, NULL
, NULL
,
549 msi_free(data
->curfile
);
550 data
->curfile
= NULL
;
555 static INT_PTR CDECL
cabinet_notify(FDINOTIFICATIONTYPE fdint
, PFDINOTIFICATION pfdin
)
559 case fdintPARTIAL_FILE
:
560 return cabinet_partial_file(fdint
, pfdin
);
562 case fdintNEXT_CABINET
:
563 return cabinet_next_cabinet(fdint
, pfdin
);
566 return cabinet_copy_file(fdint
, pfdin
);
568 case fdintCLOSE_FILE_INFO
:
569 return cabinet_close_file_info(fdint
, pfdin
);
576 static INT_PTR CDECL
cabinet_notify_stream( FDINOTIFICATIONTYPE fdint
, PFDINOTIFICATION pfdin
)
580 case fdintPARTIAL_FILE
:
581 return cabinet_partial_file( fdint
, pfdin
);
583 case fdintNEXT_CABINET
:
584 return cabinet_next_cabinet_stream( fdint
, pfdin
);
587 return cabinet_copy_file( fdint
, pfdin
);
589 case fdintCLOSE_FILE_INFO
:
590 return cabinet_close_file_info( fdint
, pfdin
);
592 case fdintCABINET_INFO
:
596 ERR("Unexpected notification %d\n", fdint
);
601 static BOOL
extract_cabinet( MSIPACKAGE
* package
, MSIMEDIAINFO
*mi
, LPVOID data
)
603 LPSTR cabinet
, cab_path
= NULL
;
608 TRACE("extracting %s disk id %u\n", debugstr_w(mi
->cabinet
), mi
->disk_id
);
610 hfdi
= FDICreate( cabinet_alloc
, cabinet_free
, cabinet_open
, cabinet_read
,
611 cabinet_write
, cabinet_close
, cabinet_seek
, 0, &erf
);
614 ERR("FDICreate failed\n");
618 cabinet
= strdupWtoA( mi
->cabinet
);
622 cab_path
= strdupWtoA( mi
->sourcedir
);
626 ret
= FDICopy( hfdi
, cabinet
, cab_path
, 0, cabinet_notify
, NULL
, data
);
628 ERR("FDICopy failed\n");
633 msi_free( cab_path
);
636 mi
->is_extracted
= TRUE
;
641 static BOOL
extract_cabinet_stream( MSIPACKAGE
*package
, MSIMEDIAINFO
*mi
, LPVOID data
)
643 static char filename
[] = {'<','S','T','R','E','A','M','>',0};
648 TRACE("extracting %s disk id %u\n", debugstr_w(mi
->cabinet
), mi
->disk_id
);
650 hfdi
= FDICreate( cabinet_alloc
, cabinet_free
, cabinet_open_stream
, cabinet_read_stream
,
651 cabinet_write
, cabinet_close_stream
, cabinet_seek_stream
, 0, &erf
);
654 ERR("FDICreate failed\n");
658 package_disk
.package
= package
;
659 package_disk
.id
= mi
->disk_id
;
661 ret
= FDICopy( hfdi
, filename
, NULL
, 0, cabinet_notify_stream
, NULL
, data
);
662 if (!ret
) ERR("FDICopy failed\n");
665 if (ret
) mi
->is_extracted
= TRUE
;
669 /***********************************************************************
672 * Extract files from a cabinet file or stream.
674 BOOL
msi_cabextract(MSIPACKAGE
* package
, MSIMEDIAINFO
*mi
, LPVOID data
)
676 if (mi
->cabinet
[0] == '#')
678 return extract_cabinet_stream( package
, mi
, data
);
680 return extract_cabinet( package
, mi
, data
);
683 void msi_free_media_info(MSIMEDIAINFO
*mi
)
685 msi_free(mi
->disk_prompt
);
686 msi_free(mi
->cabinet
);
687 msi_free(mi
->volume_label
);
691 static UINT
get_drive_type(const WCHAR
*path
)
693 WCHAR root
[MAX_PATH
+ 1];
696 PathStripToRootW(root
);
697 PathAddBackslashW(root
);
699 return GetDriveTypeW(root
);
702 UINT
msi_load_media_info(MSIPACKAGE
*package
, UINT Sequence
, MSIMEDIAINFO
*mi
)
704 static const WCHAR query
[] = {
705 'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ','`','M','e','d','i','a','`',' ',
706 'W','H','E','R','E',' ','`','L','a','s','t','S','e','q','u','e','n','c','e','`',' ',
707 '>','=',' ','%','i',' ','O','R','D','E','R',' ','B','Y',' ','`','D','i','s','k','I','d','`',0};
709 LPWSTR source_dir
, source
;
712 if (Sequence
<= mi
->last_sequence
) /* already loaded */
713 return ERROR_SUCCESS
;
715 row
= MSI_QueryGetRecord(package
->db
, query
, Sequence
);
718 TRACE("Unable to query row\n");
719 return ERROR_FUNCTION_FAILED
;
722 mi
->is_extracted
= FALSE
;
723 mi
->disk_id
= MSI_RecordGetInteger(row
, 1);
724 mi
->last_sequence
= MSI_RecordGetInteger(row
, 2);
725 msi_free(mi
->disk_prompt
);
726 mi
->disk_prompt
= strdupW(MSI_RecordGetString(row
, 3));
727 msi_free(mi
->cabinet
);
728 mi
->cabinet
= strdupW(MSI_RecordGetString(row
, 4));
729 msi_free(mi
->volume_label
);
730 mi
->volume_label
= strdupW(MSI_RecordGetString(row
, 5));
731 msiobj_release(&row
->hdr
);
733 msi_set_sourcedir_props(package
, FALSE
);
734 source_dir
= msi_dup_property(package
->db
, szSourceDir
);
735 lstrcpyW(mi
->sourcedir
, source_dir
);
736 PathAddBackslashW(mi
->sourcedir
);
737 mi
->type
= get_drive_type(source_dir
);
739 options
= MSICODE_PRODUCT
;
740 if (mi
->type
== DRIVE_CDROM
|| mi
->type
== DRIVE_REMOVABLE
)
743 options
|= MSISOURCETYPE_MEDIA
;
745 else if (package
->BaseURL
&& UrlIsW(package
->BaseURL
, URLIS_URL
))
747 source
= package
->BaseURL
;
748 options
|= MSISOURCETYPE_URL
;
752 source
= mi
->sourcedir
;
753 options
|= MSISOURCETYPE_NETWORK
;
756 msi_package_add_media_disk(package
, package
->Context
,
757 MSICODE_PRODUCT
, mi
->disk_id
,
758 mi
->volume_label
, mi
->disk_prompt
);
760 msi_package_add_info(package
, package
->Context
,
761 options
, INSTALLPROPERTY_LASTUSEDSOURCEW
, source
);
763 msi_free(source_dir
);
764 TRACE("sequence %u -> cabinet %s disk id %u\n", Sequence
, debugstr_w(mi
->cabinet
), mi
->disk_id
);
765 return ERROR_SUCCESS
;
768 /* FIXME: search URL sources as well */
769 static UINT
find_published_source(MSIPACKAGE
*package
, MSIMEDIAINFO
*mi
)
771 WCHAR source
[MAX_PATH
];
772 WCHAR volume
[MAX_PATH
];
773 WCHAR prompt
[MAX_PATH
];
774 DWORD volumesz
, promptsz
;
775 DWORD index
, size
, id
;
780 r
= MsiSourceListGetInfoW(package
->ProductCode
, NULL
,
781 package
->Context
, MSICODE_PRODUCT
,
782 INSTALLPROPERTY_LASTUSEDTYPEW
, last_type
, &size
);
783 if (r
!= ERROR_SUCCESS
)
787 r
= MsiSourceListGetInfoW(package
->ProductCode
, NULL
,
788 package
->Context
, MSICODE_PRODUCT
,
789 INSTALLPROPERTY_LASTUSEDSOURCEW
, source
, &size
);
790 if (r
!= ERROR_SUCCESS
)
793 if (last_type
[0] == 'n')
795 WCHAR cabinet_file
[MAX_PATH
];
796 BOOL check_all
= FALSE
;
802 while (MsiSourceListEnumSourcesW(package
->ProductCode
, NULL
,
804 MSISOURCETYPE_NETWORK
, index
++,
805 volume
, &volumesz
) == ERROR_SUCCESS
)
807 if (check_all
|| !strncmpiW(source
, volume
, strlenW(source
)))
809 lstrcpyW(cabinet_file
, volume
);
810 PathAddBackslashW(cabinet_file
);
811 lstrcatW(cabinet_file
, mi
->cabinet
);
813 if (GetFileAttributesW(cabinet_file
) == INVALID_FILE_ATTRIBUTES
)
821 lstrcpyW(mi
->sourcedir
, volume
);
822 PathAddBackslashW(mi
->sourcedir
);
823 TRACE("Found network source %s\n", debugstr_w(mi
->sourcedir
));
824 return ERROR_SUCCESS
;
838 while (MsiSourceListEnumMediaDisksW(package
->ProductCode
, NULL
,
840 MSICODE_PRODUCT
, index
++, &id
,
841 volume
, &volumesz
, prompt
, &promptsz
) == ERROR_SUCCESS
)
844 msi_free( mi
->volume_label
);
845 if (!(mi
->volume_label
= msi_alloc( ++volumesz
* sizeof(WCHAR
) ))) return ERROR_OUTOFMEMORY
;
846 strcpyW( mi
->volume_label
, volume
);
848 msi_free( mi
->disk_prompt
);
849 if (!(mi
->disk_prompt
= msi_alloc( ++promptsz
* sizeof(WCHAR
) ))) return ERROR_OUTOFMEMORY
;
850 strcpyW( mi
->disk_prompt
, prompt
);
852 if (source_matches_volume(mi
, source
))
854 /* FIXME: what about SourceDir */
855 lstrcpyW(mi
->sourcedir
, source
);
856 PathAddBackslashW(mi
->sourcedir
);
857 TRACE("Found disk source %s\n", debugstr_w(mi
->sourcedir
));
858 return ERROR_SUCCESS
;
862 return ERROR_FUNCTION_FAILED
;
865 UINT
ready_media( MSIPACKAGE
*package
, BOOL compressed
, MSIMEDIAINFO
*mi
)
868 WCHAR
*cabinet_file
= NULL
;
870 /* media info for continuous cabinet is already loaded */
871 if (mi
->is_continuous
) return ERROR_SUCCESS
;
875 /* cabinet is internal, no checks needed */
876 if (mi
->cabinet
[0] == '#') return ERROR_SUCCESS
;
878 if (!(cabinet_file
= get_cabinet_filename( mi
))) return ERROR_OUTOFMEMORY
;
880 /* package should be downloaded */
881 if (compressed
&& GetFileAttributesW( cabinet_file
) == INVALID_FILE_ATTRIBUTES
&&
882 package
->BaseURL
&& UrlIsW( package
->BaseURL
, URLIS_URL
))
884 WCHAR temppath
[MAX_PATH
], *p
;
886 if ((rc
= msi_download_file( cabinet_file
, temppath
)) != ERROR_SUCCESS
)
888 ERR("failed to download %s (%u)\n", debugstr_w(cabinet_file
), rc
);
889 msi_free( cabinet_file
);
892 if ((p
= strrchrW( temppath
, '\\' ))) *p
= 0;
893 strcpyW( mi
->sourcedir
, temppath
);
894 PathAddBackslashW( mi
->sourcedir
);
895 msi_free( mi
->cabinet
);
896 mi
->cabinet
= strdupW( p
+ 1 );
897 msi_free( cabinet_file
);
898 return ERROR_SUCCESS
;
901 /* check volume matches, change media if not */
902 if (mi
->volume_label
&& mi
->disk_id
> 1)
904 WCHAR
*source
= msi_dup_property( package
->db
, szSourceDir
);
905 BOOL match
= source_matches_volume( mi
, source
);
908 if (!match
&& (mi
->type
== DRIVE_CDROM
|| mi
->type
== DRIVE_REMOVABLE
))
910 if ((rc
= msi_change_media( package
, mi
)) != ERROR_SUCCESS
)
912 msi_free( cabinet_file
);
919 if (compressed
&& GetFileAttributesW( cabinet_file
) == INVALID_FILE_ATTRIBUTES
)
921 if ((rc
= find_published_source( package
, mi
)) != ERROR_SUCCESS
)
923 ERR("cabinet not found: %s\n", debugstr_w(cabinet_file
));
924 msi_free( cabinet_file
);
925 return ERROR_INSTALL_FAILURE
;
929 msi_free( cabinet_file
);
930 return ERROR_SUCCESS
;
933 UINT
msi_add_cabinet_stream( MSIPACKAGE
*package
, UINT disk_id
, IStorage
*storage
, const WCHAR
*name
)
935 MSICABINETSTREAM
*cab
, *item
;
937 TRACE("%p, %u, %p, %s\n", package
, disk_id
, storage
, debugstr_w(name
));
939 LIST_FOR_EACH_ENTRY( item
, &package
->cabinet_streams
, MSICABINETSTREAM
, entry
)
941 if (item
->disk_id
== disk_id
)
943 TRACE("duplicate disk id %u\n", disk_id
);
944 return ERROR_FUNCTION_FAILED
;
947 if (!(cab
= msi_alloc( sizeof(*cab
) ))) return ERROR_OUTOFMEMORY
;
948 if (!(cab
->stream
= msi_alloc( (strlenW( name
) + 1) * sizeof(WCHAR
) )))
951 return ERROR_OUTOFMEMORY
;
953 strcpyW( cab
->stream
, name
);
954 cab
->disk_id
= disk_id
;
955 cab
->storage
= storage
;
956 IStorage_AddRef( storage
);
957 list_add_tail( &package
->cabinet_streams
, &cab
->entry
);
959 return ERROR_SUCCESS
;