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"
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 lstrcpyW(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
= lstrlenW( volume_name
);
72 len2
= lstrlenW( mi
->volume_label
);
73 if (len2
> len
) return FALSE
;
74 p
= volume_name
+ len
- len2
;
76 return !wcsicmp( mi
->volume_label
, p
);
79 static UINT
msi_change_media(MSIPACKAGE
*package
, MSIMEDIAINFO
*mi
)
85 source_dir
= msi_dup_property(package
->db
, L
"SourceDir");
86 record
= MSI_CreateRecord(2);
88 while (r
== IDRETRY
&& !source_matches_volume(mi
, source_dir
))
90 MSI_RecordSetStringW(record
, 0, NULL
);
91 MSI_RecordSetInteger(record
, 1, MSIERR_CABNOTFOUND
);
92 MSI_RecordSetStringW(record
, 2, mi
->disk_prompt
);
93 r
= MSI_ProcessMessage(package
, INSTALLMESSAGE_ERROR
| MB_RETRYCANCEL
, record
);
96 msiobj_release(&record
->hdr
);
99 return r
== IDRETRY
? ERROR_SUCCESS
: ERROR_INSTALL_SOURCE_ABSENT
;
102 static MSICABINETSTREAM
*msi_get_cabinet_stream( MSIPACKAGE
*package
, UINT disk_id
)
104 MSICABINETSTREAM
*cab
;
106 LIST_FOR_EACH_ENTRY( cab
, &package
->cabinet_streams
, MSICABINETSTREAM
, entry
)
108 if (cab
->disk_id
== disk_id
) return cab
;
113 static void * CDECL
cabinet_alloc(ULONG cb
)
115 return msi_alloc(cb
);
118 static void CDECL
cabinet_free(void *pv
)
123 static INT_PTR CDECL
cabinet_open(char *pszFile
, int oflag
, int pmode
)
126 DWORD dwShareMode
= 0;
127 DWORD dwCreateDisposition
= OPEN_EXISTING
;
129 switch (oflag
& _O_ACCMODE
)
132 dwAccess
= GENERIC_READ
;
133 dwShareMode
= FILE_SHARE_READ
| FILE_SHARE_DELETE
;
136 dwAccess
= GENERIC_WRITE
;
137 dwShareMode
= FILE_SHARE_READ
| FILE_SHARE_WRITE
| FILE_SHARE_DELETE
;
140 dwAccess
= GENERIC_READ
| GENERIC_WRITE
;
141 dwShareMode
= FILE_SHARE_READ
| FILE_SHARE_WRITE
| FILE_SHARE_DELETE
;
145 if ((oflag
& (_O_CREAT
| _O_EXCL
)) == (_O_CREAT
| _O_EXCL
))
146 dwCreateDisposition
= CREATE_NEW
;
147 else if (oflag
& _O_CREAT
)
148 dwCreateDisposition
= CREATE_ALWAYS
;
150 return (INT_PTR
)CreateFileA(pszFile
, dwAccess
, dwShareMode
, NULL
,
151 dwCreateDisposition
, 0, NULL
);
154 static UINT CDECL
cabinet_read(INT_PTR hf
, void *pv
, UINT cb
)
156 HANDLE handle
= (HANDLE
)hf
;
159 if (ReadFile(handle
, pv
, cb
, &read
, NULL
))
165 static UINT CDECL
cabinet_write(INT_PTR hf
, void *pv
, UINT cb
)
167 HANDLE handle
= (HANDLE
)hf
;
170 if (WriteFile(handle
, pv
, cb
, &written
, NULL
))
176 static int CDECL
cabinet_close(INT_PTR hf
)
178 HANDLE handle
= (HANDLE
)hf
;
179 return CloseHandle(handle
) ? 0 : -1;
182 static LONG CDECL
cabinet_seek(INT_PTR hf
, LONG dist
, int seektype
)
184 HANDLE handle
= (HANDLE
)hf
;
185 /* flags are compatible and so are passed straight through */
186 return SetFilePointer(handle
, dist
, NULL
, seektype
);
195 static struct package_disk package_disk
;
197 static INT_PTR CDECL
cabinet_open_stream( char *pszFile
, int oflag
, int pmode
)
199 MSICABINETSTREAM
*cab
;
202 if (!(cab
= msi_get_cabinet_stream( package_disk
.package
, package_disk
.id
)))
204 WARN("failed to get cabinet stream\n");
207 if (cab
->storage
== package_disk
.package
->db
->storage
)
209 UINT r
= msi_get_stream( package_disk
.package
->db
, cab
->stream
+ 1, &stream
);
210 if (r
!= ERROR_SUCCESS
)
212 WARN("failed to get stream %u\n", r
);
216 else /* patch storage */
221 if (!(encoded
= encode_streamname( FALSE
, cab
->stream
+ 1 )))
223 WARN("failed to encode stream name\n");
226 hr
= IStorage_OpenStream( cab
->storage
, encoded
, NULL
, STGM_READ
|STGM_SHARE_EXCLUSIVE
, 0, &stream
);
230 WARN("failed to open stream 0x%08x\n", hr
);
234 return (INT_PTR
)stream
;
237 static UINT CDECL
cabinet_read_stream( INT_PTR hf
, void *pv
, UINT cb
)
239 IStream
*stm
= (IStream
*)hf
;
243 hr
= IStream_Read( stm
, pv
, cb
, &read
);
244 if (hr
== S_OK
|| hr
== S_FALSE
)
250 static int CDECL
cabinet_close_stream( INT_PTR hf
)
252 IStream
*stm
= (IStream
*)hf
;
253 IStream_Release( stm
);
257 static LONG CDECL
cabinet_seek_stream( INT_PTR hf
, LONG dist
, int seektype
)
259 IStream
*stm
= (IStream
*)hf
;
261 ULARGE_INTEGER newpos
;
264 move
.QuadPart
= dist
;
265 hr
= IStream_Seek( stm
, move
, seektype
, &newpos
);
268 if (newpos
.QuadPart
<= MAXLONG
) return newpos
.QuadPart
;
274 static UINT CDECL
msi_media_get_disk_info(MSIPACKAGE
*package
, MSIMEDIAINFO
*mi
)
278 row
= MSI_QueryGetRecord(package
->db
, L
"SELECT * FROM `Media` WHERE `DiskId` = %d", mi
->disk_id
);
281 TRACE("Unable to query row\n");
282 return ERROR_FUNCTION_FAILED
;
285 mi
->disk_prompt
= strdupW(MSI_RecordGetString(row
, 3));
286 mi
->cabinet
= strdupW(MSI_RecordGetString(row
, 4));
287 mi
->volume_label
= strdupW(MSI_RecordGetString(row
, 5));
289 msiobj_release(&row
->hdr
);
290 return ERROR_SUCCESS
;
293 static INT_PTR
cabinet_partial_file(FDINOTIFICATIONTYPE fdint
,
294 PFDINOTIFICATION pfdin
)
296 MSICABDATA
*data
= pfdin
->pv
;
297 data
->mi
->is_continuous
= FALSE
;
301 static WCHAR
*get_cabinet_filename(MSIMEDIAINFO
*mi
)
306 len
= lstrlenW(mi
->sourcedir
) + lstrlenW(mi
->cabinet
) + 1;
307 if (!(ret
= msi_alloc(len
* sizeof(WCHAR
)))) return NULL
;
308 lstrcpyW(ret
, mi
->sourcedir
);
309 lstrcatW(ret
, mi
->cabinet
);
313 static INT_PTR
cabinet_next_cabinet(FDINOTIFICATIONTYPE fdint
,
314 PFDINOTIFICATION pfdin
)
316 MSICABDATA
*data
= pfdin
->pv
;
317 MSIMEDIAINFO
*mi
= data
->mi
;
318 LPWSTR cabinet_file
= NULL
, cab
= strdupAtoW(pfdin
->psz1
);
322 msi_free(mi
->disk_prompt
);
323 msi_free(mi
->cabinet
);
324 msi_free(mi
->volume_label
);
325 mi
->disk_prompt
= NULL
;
327 mi
->volume_label
= NULL
;
330 mi
->is_continuous
= TRUE
;
332 rc
= msi_media_get_disk_info(data
->package
, mi
);
333 if (rc
!= ERROR_SUCCESS
)
335 ERR("Failed to get next cabinet information: %d\n", rc
);
339 if (wcsicmp( mi
->cabinet
, cab
))
344 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
));
346 /* Use cabinet name from the media table */
347 next_cab
= strdupWtoA(mi
->cabinet
);
348 /* Modify path to cabinet file with full filename (psz3 points to a 256 bytes buffer that can be modified contrary to psz1 and psz2) */
349 length
= strlen(pfdin
->psz3
) + 1 + strlen(next_cab
) + 1;
352 WARN("Cannot update next cabinet filename with a string size %u > 256\n", length
);
358 strcat(pfdin
->psz3
, "\\");
359 strcat(pfdin
->psz3
, next_cab
);
361 /* Path psz3 and cabinet psz1 are concatenated by FDI so just reset psz1 */
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 -> %s\n", debugstr_w(data
->curfile
), debugstr_w(path
));
433 attrs
= attrs
& (FILE_ATTRIBUTE_READONLY
|FILE_ATTRIBUTE_HIDDEN
|FILE_ATTRIBUTE_SYSTEM
);
434 if (!attrs
) attrs
= FILE_ATTRIBUTE_NORMAL
;
436 handle
= msi_create_file( data
->package
, path
, GENERIC_READ
| GENERIC_WRITE
, 0, CREATE_ALWAYS
, attrs
);
437 if (handle
== INVALID_HANDLE_VALUE
)
439 DWORD err
= GetLastError();
440 DWORD attrs2
= msi_get_file_attributes( data
->package
, 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 msi_set_file_attributes( data
->package
, path
, attrs2
& ~FILE_ATTRIBUTE_READONLY
);
451 handle
= msi_create_file( data
->package
, path
, GENERIC_READ
| GENERIC_WRITE
, 0, CREATE_ALWAYS
, attrs
);
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
= wcsrchr(tmppathW
, '\\'))) *p
= 0;
465 len
= lstrlenW( tmppathW
) + 16;
466 if (!(tmpfileW
= msi_alloc(len
* sizeof(WCHAR
))))
468 msi_free( tmppathW
);
469 return ERROR_OUTOFMEMORY
;
471 if (!GetTempFileNameW(tmppathW
, L
"msi", 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 msi_move_file( data
->package
, path
, NULL
, MOVEFILE_DELAY_UNTIL_REBOOT
) &&
478 msi_move_file( data
->package
, 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
->last_volume
);
664 static UINT
get_drive_type(const WCHAR
*path
)
666 WCHAR root
[MAX_PATH
+ 1];
668 lstrcpyW(root
, path
);
669 PathStripToRootW(root
);
670 PathAddBackslashW(root
);
672 return GetDriveTypeW(root
);
675 static WCHAR
*get_base_url( MSIDATABASE
*db
)
677 WCHAR
*p
, *ret
= NULL
, *orig_db
= msi_dup_property( db
, L
"OriginalDatabase" );
678 if (UrlIsW( orig_db
, URLIS_URL
) && (ret
= strdupW( orig_db
)) && (p
= wcsrchr( ret
, '/'))) p
[1] = 0;
683 UINT
msi_load_media_info(MSIPACKAGE
*package
, UINT Sequence
, MSIMEDIAINFO
*mi
)
686 WCHAR
*source_dir
, *source
, *base_url
= NULL
;
689 if (Sequence
<= mi
->last_sequence
) /* already loaded */
690 return ERROR_SUCCESS
;
692 row
= MSI_QueryGetRecord(package
->db
, L
"SELECT * FROM `Media` WHERE `LastSequence` >= %d ORDER BY `DiskId`", Sequence
);
695 TRACE("Unable to query row\n");
696 return ERROR_FUNCTION_FAILED
;
699 mi
->is_extracted
= FALSE
;
700 mi
->disk_id
= MSI_RecordGetInteger(row
, 1);
701 mi
->last_sequence
= MSI_RecordGetInteger(row
, 2);
702 msi_free(mi
->disk_prompt
);
703 mi
->disk_prompt
= strdupW(MSI_RecordGetString(row
, 3));
704 msi_free(mi
->cabinet
);
705 mi
->cabinet
= strdupW(MSI_RecordGetString(row
, 4));
706 msi_free(mi
->volume_label
);
707 mi
->volume_label
= strdupW(MSI_RecordGetString(row
, 5));
708 msiobj_release(&row
->hdr
);
710 msi_set_sourcedir_props(package
, FALSE
);
711 source_dir
= msi_dup_property(package
->db
, L
"SourceDir");
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 ((base_url
= get_base_url(package
->db
)))
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 TRACE("sequence %u -> cabinet %s disk id %u\n", Sequence
, debugstr_w(mi
->cabinet
), mi
->disk_id
);
743 msi_free(source_dir
);
744 return ERROR_SUCCESS
;
747 /* FIXME: search URL sources as well */
748 static UINT
find_published_source(MSIPACKAGE
*package
, MSIMEDIAINFO
*mi
)
750 WCHAR source
[MAX_PATH
];
751 WCHAR volume
[MAX_PATH
];
752 WCHAR prompt
[MAX_PATH
];
753 DWORD volumesz
, promptsz
;
754 DWORD index
, size
, id
;
759 r
= MsiSourceListGetInfoW(package
->ProductCode
, NULL
,
760 package
->Context
, MSICODE_PRODUCT
,
761 INSTALLPROPERTY_LASTUSEDTYPEW
, last_type
, &size
);
762 if (r
!= ERROR_SUCCESS
)
766 r
= MsiSourceListGetInfoW(package
->ProductCode
, NULL
,
767 package
->Context
, MSICODE_PRODUCT
,
768 INSTALLPROPERTY_LASTUSEDSOURCEW
, source
, &size
);
769 if (r
!= ERROR_SUCCESS
)
772 if (last_type
[0] == 'n')
774 WCHAR cabinet_file
[MAX_PATH
];
775 BOOL check_all
= FALSE
;
781 while (MsiSourceListEnumSourcesW(package
->ProductCode
, NULL
,
783 MSISOURCETYPE_NETWORK
, index
++,
784 volume
, &volumesz
) == ERROR_SUCCESS
)
786 if (check_all
|| !wcsnicmp(source
, volume
, lstrlenW(source
)))
788 lstrcpyW(cabinet_file
, volume
);
789 PathAddBackslashW(cabinet_file
);
790 lstrcatW(cabinet_file
, mi
->cabinet
);
792 if (GetFileAttributesW(cabinet_file
) == INVALID_FILE_ATTRIBUTES
)
800 lstrcpyW(mi
->sourcedir
, volume
);
801 PathAddBackslashW(mi
->sourcedir
);
802 TRACE("Found network source %s\n", debugstr_w(mi
->sourcedir
));
803 return ERROR_SUCCESS
;
817 while (MsiSourceListEnumMediaDisksW(package
->ProductCode
, NULL
,
819 MSICODE_PRODUCT
, index
++, &id
,
820 volume
, &volumesz
, prompt
, &promptsz
) == ERROR_SUCCESS
)
823 msi_free( mi
->volume_label
);
824 if (!(mi
->volume_label
= msi_alloc( ++volumesz
* sizeof(WCHAR
) ))) return ERROR_OUTOFMEMORY
;
825 lstrcpyW( mi
->volume_label
, volume
);
827 msi_free( mi
->disk_prompt
);
828 if (!(mi
->disk_prompt
= msi_alloc( ++promptsz
* sizeof(WCHAR
) ))) return ERROR_OUTOFMEMORY
;
829 lstrcpyW( mi
->disk_prompt
, prompt
);
831 if (source_matches_volume(mi
, source
))
833 /* FIXME: what about SourceDir */
834 lstrcpyW(mi
->sourcedir
, source
);
835 PathAddBackslashW(mi
->sourcedir
);
836 TRACE("Found disk source %s\n", debugstr_w(mi
->sourcedir
));
837 return ERROR_SUCCESS
;
841 return ERROR_FUNCTION_FAILED
;
844 UINT
ready_media( MSIPACKAGE
*package
, BOOL compressed
, MSIMEDIAINFO
*mi
)
847 WCHAR
*cabinet_file
= NULL
;
849 /* media info for continuous cabinet is already loaded */
850 if (mi
->is_continuous
) return ERROR_SUCCESS
;
856 /* cabinet is internal, no checks needed */
857 if (mi
->cabinet
[0] == '#') return ERROR_SUCCESS
;
859 if (!(cabinet_file
= get_cabinet_filename( mi
))) return ERROR_OUTOFMEMORY
;
861 /* package should be downloaded */
862 if (compressed
&& GetFileAttributesW( cabinet_file
) == INVALID_FILE_ATTRIBUTES
&&
863 (base_url
= get_base_url( package
->db
)))
865 WCHAR temppath
[MAX_PATH
], *p
, *url
;
867 msi_free( cabinet_file
);
868 if (!(url
= msi_alloc( (lstrlenW( base_url
) + lstrlenW( mi
->cabinet
) + 1) * sizeof(WCHAR
) )))
870 return ERROR_OUTOFMEMORY
;
872 lstrcpyW( url
, base_url
);
873 lstrcatW( url
, mi
->cabinet
);
874 if ((rc
= msi_download_file( url
, temppath
)) != ERROR_SUCCESS
)
876 ERR("failed to download %s (%u)\n", debugstr_w(url
), rc
);
880 if ((p
= wcsrchr( temppath
, '\\' ))) *p
= 0;
881 lstrcpyW( mi
->sourcedir
, temppath
);
882 PathAddBackslashW( mi
->sourcedir
);
883 msi_free( mi
->cabinet
);
884 mi
->cabinet
= strdupW( p
+ 1 );
887 return ERROR_SUCCESS
;
890 /* check volume matches, change media if not */
891 if (mi
->volume_label
)
893 /* assume first volume is in the drive */
894 if (mi
->last_volume
&& wcsicmp( mi
->last_volume
, mi
->volume_label
))
896 WCHAR
*source
= msi_dup_property( package
->db
, L
"SourceDir" );
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
);
910 msi_free(mi
->last_volume
);
911 mi
->last_volume
= strdupW(mi
->volume_label
);
915 if (compressed
&& GetFileAttributesW( cabinet_file
) == INVALID_FILE_ATTRIBUTES
)
917 if ((rc
= find_published_source( package
, mi
)) != ERROR_SUCCESS
)
919 ERR("cabinet not found: %s\n", debugstr_w(cabinet_file
));
920 msi_free( cabinet_file
);
921 return ERROR_INSTALL_FAILURE
;
925 msi_free( cabinet_file
);
926 return ERROR_SUCCESS
;
929 UINT
msi_add_cabinet_stream( MSIPACKAGE
*package
, UINT disk_id
, IStorage
*storage
, const WCHAR
*name
)
931 MSICABINETSTREAM
*cab
, *item
;
933 TRACE("%p, %u, %p, %s\n", package
, disk_id
, storage
, debugstr_w(name
));
935 LIST_FOR_EACH_ENTRY( item
, &package
->cabinet_streams
, MSICABINETSTREAM
, entry
)
937 if (item
->disk_id
== disk_id
)
939 TRACE("duplicate disk id %u\n", disk_id
);
940 return ERROR_FUNCTION_FAILED
;
943 if (!(cab
= msi_alloc( sizeof(*cab
) ))) return ERROR_OUTOFMEMORY
;
944 if (!(cab
->stream
= msi_alloc( (lstrlenW( name
) + 1) * sizeof(WCHAR
) )))
947 return ERROR_OUTOFMEMORY
;
949 lstrcpyW( cab
->stream
, name
);
950 cab
->disk_id
= disk_id
;
951 cab
->storage
= storage
;
952 IStorage_AddRef( storage
);
953 list_add_tail( &package
->cabinet_streams
, &cab
->entry
);
955 return ERROR_SUCCESS
;