msxml3: Leading space chars are allowed in SelectionNamespaces value string.
[wine/multimedia.git] / dlls / msi / media.c
blob69bcb91de122ce3b145a4fd882555eece8e2b277
1 /*
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
21 #include <stdarg.h>
23 #define COBJMACROS
25 #include "windef.h"
26 #include "winerror.h"
27 #include "wine/debug.h"
28 #include "fdi.h"
29 #include "msipriv.h"
30 #include "winuser.h"
31 #include "winreg.h"
32 #include "shlwapi.h"
33 #include "objidl.h"
34 #include "wine/unicode.h"
36 WINE_DEFAULT_DEBUG_CHANNEL(msi);
38 /* from msvcrt/fcntl.h */
39 #define _O_RDONLY 0
40 #define _O_WRONLY 1
41 #define _O_RDWR 2
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());
67 return FALSE;
69 return !strcmpiW( mi->volume_label, volume_name );
72 static UINT msi_change_media(MSIPACKAGE *package, MSIMEDIAINFO *mi)
74 LPWSTR error, error_dialog;
75 LPWSTR source_dir;
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 ((msi_get_property_int(package->db, szUILevel, 0) & INSTALLUILEVEL_MASK) ==
81 INSTALLUILEVEL_NONE && !gUIHandlerA && !gUIHandlerW && !gUIHandlerRecord)
82 return ERROR_SUCCESS;
84 error = msi_build_error_string(package, 1302, 1, mi->disk_prompt);
85 error_dialog = msi_dup_property(package->db, error_prop);
86 source_dir = msi_dup_property(package->db, szSourceDir);
88 while (r == ERROR_SUCCESS && !source_matches_volume(mi, source_dir))
90 r = msi_spawn_error_dialog(package, error_dialog, error);
92 if (gUIHandlerW)
94 gUIHandlerW(gUIContext, MB_RETRYCANCEL | INSTALLMESSAGE_ERROR, error);
96 else if (gUIHandlerA)
98 char *msg = strdupWtoA(error);
99 gUIHandlerA(gUIContext, MB_RETRYCANCEL | INSTALLMESSAGE_ERROR, msg);
100 msi_free(msg);
102 else if (gUIHandlerRecord)
104 MSIHANDLE rec = MsiCreateRecord(1);
105 MsiRecordSetStringW(rec, 0, error);
106 gUIHandlerRecord(gUIContext, MB_RETRYCANCEL | INSTALLMESSAGE_ERROR, rec);
107 MsiCloseHandle(rec);
111 msi_free(error);
112 msi_free(error_dialog);
113 msi_free(source_dir);
115 return r;
118 static MSICABINETSTREAM *msi_get_cabinet_stream( MSIPACKAGE *package, UINT disk_id )
120 MSICABINETSTREAM *cab;
122 LIST_FOR_EACH_ENTRY( cab, &package->cabinet_streams, MSICABINETSTREAM, entry )
124 if (cab->disk_id == disk_id) return cab;
126 return NULL;
129 static void * CDECL cabinet_alloc(ULONG cb)
131 return msi_alloc(cb);
134 static void CDECL cabinet_free(void *pv)
136 msi_free(pv);
139 static INT_PTR CDECL cabinet_open(char *pszFile, int oflag, int pmode)
141 HANDLE handle;
142 DWORD dwAccess = 0;
143 DWORD dwShareMode = 0;
144 DWORD dwCreateDisposition = OPEN_EXISTING;
146 switch (oflag & _O_ACCMODE)
148 case _O_RDONLY:
149 dwAccess = GENERIC_READ;
150 dwShareMode = FILE_SHARE_READ | FILE_SHARE_DELETE;
151 break;
152 case _O_WRONLY:
153 dwAccess = GENERIC_WRITE;
154 dwShareMode = FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE;
155 break;
156 case _O_RDWR:
157 dwAccess = GENERIC_READ | GENERIC_WRITE;
158 dwShareMode = FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE;
159 break;
162 if ((oflag & (_O_CREAT | _O_EXCL)) == (_O_CREAT | _O_EXCL))
163 dwCreateDisposition = CREATE_NEW;
164 else if (oflag & _O_CREAT)
165 dwCreateDisposition = CREATE_ALWAYS;
167 handle = CreateFileA(pszFile, dwAccess, dwShareMode, NULL,
168 dwCreateDisposition, 0, NULL);
169 if (handle == INVALID_HANDLE_VALUE)
170 return 0;
172 return (INT_PTR)handle;
175 static UINT CDECL cabinet_read(INT_PTR hf, void *pv, UINT cb)
177 HANDLE handle = (HANDLE)hf;
178 DWORD read;
180 if (ReadFile(handle, pv, cb, &read, NULL))
181 return read;
183 return 0;
186 static UINT CDECL cabinet_write(INT_PTR hf, void *pv, UINT cb)
188 HANDLE handle = (HANDLE)hf;
189 DWORD written;
191 if (WriteFile(handle, pv, cb, &written, NULL))
192 return written;
194 return 0;
197 static int CDECL cabinet_close(INT_PTR hf)
199 HANDLE handle = (HANDLE)hf;
200 return CloseHandle(handle) ? 0 : -1;
203 static LONG CDECL cabinet_seek(INT_PTR hf, LONG dist, int seektype)
205 HANDLE handle = (HANDLE)hf;
206 /* flags are compatible and so are passed straight through */
207 return SetFilePointer(handle, dist, NULL, seektype);
210 struct package_disk
212 MSIPACKAGE *package;
213 UINT id;
216 static struct package_disk package_disk;
218 static INT_PTR CDECL cabinet_open_stream( char *pszFile, int oflag, int pmode )
220 MSICABINETSTREAM *cab;
221 IStream *stream;
222 WCHAR *encoded;
223 HRESULT hr;
225 cab = msi_get_cabinet_stream( package_disk.package, package_disk.id );
226 if (!cab)
228 WARN("failed to get cabinet stream\n");
229 return 0;
231 if (!cab->stream[0] || !(encoded = encode_streamname( FALSE, cab->stream + 1 )))
233 WARN("failed to encode stream name\n");
234 return 0;
236 if (msi_clone_open_stream( package_disk.package->db, cab->storage, encoded, &stream ) != ERROR_SUCCESS)
238 hr = IStorage_OpenStream( cab->storage, encoded, NULL, STGM_READ|STGM_SHARE_EXCLUSIVE, 0, &stream );
239 if (FAILED(hr))
241 WARN("failed to open stream 0x%08x\n", hr);
242 msi_free( encoded );
243 return 0;
246 msi_free( encoded );
247 return (INT_PTR)stream;
250 static UINT CDECL cabinet_read_stream( INT_PTR hf, void *pv, UINT cb )
252 IStream *stm = (IStream *)hf;
253 DWORD read;
254 HRESULT hr;
256 hr = IStream_Read( stm, pv, cb, &read );
257 if (hr == S_OK || hr == S_FALSE)
258 return read;
260 return 0;
263 static int CDECL cabinet_close_stream( INT_PTR hf )
265 IStream *stm = (IStream *)hf;
266 IStream_Release( stm );
267 return 0;
270 static LONG CDECL cabinet_seek_stream( INT_PTR hf, LONG dist, int seektype )
272 IStream *stm = (IStream *)hf;
273 LARGE_INTEGER move;
274 ULARGE_INTEGER newpos;
275 HRESULT hr;
277 move.QuadPart = dist;
278 hr = IStream_Seek( stm, move, seektype, &newpos );
279 if (SUCCEEDED(hr))
281 if (newpos.QuadPart <= MAXLONG) return newpos.QuadPart;
282 ERR("Too big!\n");
284 return -1;
287 static UINT CDECL msi_media_get_disk_info(MSIPACKAGE *package, MSIMEDIAINFO *mi)
289 MSIRECORD *row;
291 static const WCHAR query[] = {
292 'S','E','L','E','C','T',' ','*',' ', 'F','R','O','M',' ',
293 '`','M','e','d','i','a','`',' ','W','H','E','R','E',' ',
294 '`','D','i','s','k','I','d','`',' ','=',' ','%','i',0};
296 row = MSI_QueryGetRecord(package->db, query, mi->disk_id);
297 if (!row)
299 TRACE("Unable to query row\n");
300 return ERROR_FUNCTION_FAILED;
303 mi->disk_prompt = strdupW(MSI_RecordGetString(row, 3));
304 mi->cabinet = strdupW(MSI_RecordGetString(row, 4));
305 mi->volume_label = strdupW(MSI_RecordGetString(row, 5));
307 if (!mi->first_volume)
308 mi->first_volume = strdupW(mi->volume_label);
310 msiobj_release(&row->hdr);
311 return ERROR_SUCCESS;
314 static INT_PTR cabinet_partial_file(FDINOTIFICATIONTYPE fdint,
315 PFDINOTIFICATION pfdin)
317 MSICABDATA *data = pfdin->pv;
318 data->mi->is_continuous = FALSE;
319 return 0;
322 static WCHAR *get_cabinet_filename(MSIMEDIAINFO *mi)
324 int len;
325 WCHAR *ret;
327 len = strlenW(mi->sourcedir) + strlenW(mi->cabinet) + 1;
328 if (!(ret = msi_alloc(len * sizeof(WCHAR)))) return NULL;
329 strcpyW(ret, mi->sourcedir);
330 strcatW(ret, mi->cabinet);
331 return ret;
334 static INT_PTR cabinet_next_cabinet(FDINOTIFICATIONTYPE fdint,
335 PFDINOTIFICATION pfdin)
337 MSICABDATA *data = pfdin->pv;
338 MSIMEDIAINFO *mi = data->mi;
339 LPWSTR cabinet_file = NULL, cab = strdupAtoW(pfdin->psz1);
340 INT_PTR res = -1;
341 UINT rc;
343 msi_free(mi->disk_prompt);
344 msi_free(mi->cabinet);
345 msi_free(mi->volume_label);
346 mi->disk_prompt = NULL;
347 mi->cabinet = NULL;
348 mi->volume_label = NULL;
350 mi->disk_id++;
351 mi->is_continuous = TRUE;
353 rc = msi_media_get_disk_info(data->package, mi);
354 if (rc != ERROR_SUCCESS)
356 ERR("Failed to get next cabinet information: %d\n", rc);
357 goto done;
360 if (strcmpiW( mi->cabinet, cab ))
362 ERR("Continuous cabinet does not match the next cabinet in the Media table\n");
363 goto done;
366 if (!(cabinet_file = get_cabinet_filename(mi)))
367 goto done;
369 TRACE("Searching for %s\n", debugstr_w(cabinet_file));
371 res = 0;
372 if (GetFileAttributesW(cabinet_file) == INVALID_FILE_ATTRIBUTES)
374 if (msi_change_media(data->package, mi) != ERROR_SUCCESS)
375 res = -1;
378 done:
379 msi_free(cab);
380 msi_free(cabinet_file);
381 return res;
384 static INT_PTR cabinet_next_cabinet_stream( FDINOTIFICATIONTYPE fdint,
385 PFDINOTIFICATION pfdin )
387 MSICABDATA *data = pfdin->pv;
388 MSIMEDIAINFO *mi = data->mi;
389 UINT rc;
391 msi_free( mi->disk_prompt );
392 msi_free( mi->cabinet );
393 msi_free( mi->volume_label );
394 mi->disk_prompt = NULL;
395 mi->cabinet = NULL;
396 mi->volume_label = NULL;
398 mi->disk_id++;
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);
405 return -1;
407 package_disk.id = mi->disk_id;
409 TRACE("next cabinet is %s disk id %u\n", debugstr_w(mi->cabinet), mi->disk_id);
410 return 0;
413 static INT_PTR cabinet_copy_file(FDINOTIFICATIONTYPE fdint,
414 PFDINOTIFICATION pfdin)
416 MSICABDATA *data = pfdin->pv;
417 HANDLE handle = 0;
418 LPWSTR path = NULL;
419 DWORD attrs;
421 data->curfile = strdupAtoW(pfdin->psz1);
422 if (!data->cb(data->package, data->curfile, MSICABEXTRACT_BEGINEXTRACT, &path,
423 &attrs, data->user))
425 /* We're not extracting this file, so free the filename. */
426 msi_free(data->curfile);
427 data->curfile = NULL;
428 goto done;
431 TRACE("extracting %s\n", debugstr_w(path));
433 attrs = attrs & (FILE_ATTRIBUTE_READONLY|FILE_ATTRIBUTE_HIDDEN|FILE_ATTRIBUTE_SYSTEM);
434 if (!attrs) attrs = FILE_ATTRIBUTE_NORMAL;
436 handle = CreateFileW(path, GENERIC_READ | GENERIC_WRITE, 0,
437 NULL, CREATE_ALWAYS, attrs, NULL);
438 if (handle == INVALID_HANDLE_VALUE)
440 DWORD err = GetLastError();
441 DWORD attrs2 = GetFileAttributesW(path);
443 if (attrs2 == INVALID_FILE_ATTRIBUTES)
445 ERR("failed to create %s (error %d)\n", debugstr_w(path), err);
446 goto done;
448 else if (err == ERROR_ACCESS_DENIED && (attrs2 & FILE_ATTRIBUTE_READONLY))
450 TRACE("removing read-only attribute on %s\n", debugstr_w(path));
451 SetFileAttributesW( path, attrs2 & ~FILE_ATTRIBUTE_READONLY );
452 handle = CreateFileW(path, GENERIC_READ | GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, attrs2, NULL);
454 if (handle != INVALID_HANDLE_VALUE) goto done;
455 err = GetLastError();
457 if (err == ERROR_SHARING_VIOLATION || err == ERROR_USER_MAPPED_FILE)
459 WCHAR *tmpfileW, *tmppathW, *p;
460 DWORD len;
462 TRACE("file in use, scheduling rename operation\n");
464 if (!(tmppathW = strdupW( path ))) return ERROR_OUTOFMEMORY;
465 if ((p = strrchrW(tmppathW, '\\'))) *p = 0;
466 len = strlenW( tmppathW ) + 16;
467 if (!(tmpfileW = msi_alloc(len * sizeof(WCHAR))))
469 msi_free( tmppathW );
470 return ERROR_OUTOFMEMORY;
472 if (!GetTempFileNameW(tmppathW, szMsi, 0, tmpfileW)) tmpfileW[0] = 0;
473 msi_free( tmppathW );
475 handle = CreateFileW(tmpfileW, GENERIC_READ | GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, attrs, NULL);
477 if (handle != INVALID_HANDLE_VALUE &&
478 MoveFileExW(path, NULL, MOVEFILE_DELAY_UNTIL_REBOOT) &&
479 MoveFileExW(tmpfileW, path, MOVEFILE_DELAY_UNTIL_REBOOT))
481 data->package->need_reboot = 1;
483 else
485 WARN("failed to schedule rename operation %s (error %d)\n", debugstr_w(path), GetLastError());
486 DeleteFileW( tmpfileW );
488 msi_free(tmpfileW);
490 else
491 WARN("failed to create %s (error %d)\n", debugstr_w(path), err);
494 done:
495 msi_free(path);
497 return (INT_PTR)handle;
500 static INT_PTR cabinet_close_file_info(FDINOTIFICATIONTYPE fdint,
501 PFDINOTIFICATION pfdin)
503 MSICABDATA *data = pfdin->pv;
504 FILETIME ft;
505 FILETIME ftLocal;
506 HANDLE handle = (HANDLE)pfdin->hf;
508 data->mi->is_continuous = FALSE;
510 if (!DosDateTimeToFileTime(pfdin->date, pfdin->time, &ft))
511 return -1;
512 if (!LocalFileTimeToFileTime(&ft, &ftLocal))
513 return -1;
514 if (!SetFileTime(handle, &ftLocal, 0, &ftLocal))
515 return -1;
517 CloseHandle(handle);
519 data->cb(data->package, data->curfile, MSICABEXTRACT_FILEEXTRACTED, NULL, NULL,
520 data->user);
522 msi_free(data->curfile);
523 data->curfile = NULL;
525 return 1;
528 static INT_PTR CDECL cabinet_notify(FDINOTIFICATIONTYPE fdint, PFDINOTIFICATION pfdin)
530 switch (fdint)
532 case fdintPARTIAL_FILE:
533 return cabinet_partial_file(fdint, pfdin);
535 case fdintNEXT_CABINET:
536 return cabinet_next_cabinet(fdint, pfdin);
538 case fdintCOPY_FILE:
539 return cabinet_copy_file(fdint, pfdin);
541 case fdintCLOSE_FILE_INFO:
542 return cabinet_close_file_info(fdint, pfdin);
544 default:
545 return 0;
549 static INT_PTR CDECL cabinet_notify_stream( FDINOTIFICATIONTYPE fdint, PFDINOTIFICATION pfdin )
551 switch (fdint)
553 case fdintPARTIAL_FILE:
554 return cabinet_partial_file( fdint, pfdin );
556 case fdintNEXT_CABINET:
557 return cabinet_next_cabinet_stream( fdint, pfdin );
559 case fdintCOPY_FILE:
560 return cabinet_copy_file( fdint, pfdin );
562 case fdintCLOSE_FILE_INFO:
563 return cabinet_close_file_info( fdint, pfdin );
565 case fdintCABINET_INFO:
566 return 0;
568 default:
569 ERR("Unexpected notification %d\n", fdint);
570 return 0;
574 static BOOL extract_cabinet( MSIPACKAGE* package, MSIMEDIAINFO *mi, LPVOID data )
576 LPSTR cabinet, cab_path = NULL;
577 HFDI hfdi;
578 ERF erf;
579 BOOL ret = FALSE;
581 TRACE("extracting %s disk id %u\n", debugstr_w(mi->cabinet), mi->disk_id);
583 hfdi = FDICreate( cabinet_alloc, cabinet_free, cabinet_open, cabinet_read,
584 cabinet_write, cabinet_close, cabinet_seek, 0, &erf );
585 if (!hfdi)
587 ERR("FDICreate failed\n");
588 return FALSE;
591 cabinet = strdupWtoA( mi->cabinet );
592 if (!cabinet)
593 goto done;
595 cab_path = strdupWtoA( mi->sourcedir );
596 if (!cab_path)
597 goto done;
599 ret = FDICopy( hfdi, cabinet, cab_path, 0, cabinet_notify, NULL, data );
600 if (!ret)
601 ERR("FDICopy failed\n");
603 done:
604 FDIDestroy( hfdi );
605 msi_free(cabinet );
606 msi_free( cab_path );
608 if (ret)
609 mi->is_extracted = TRUE;
611 return ret;
614 static BOOL extract_cabinet_stream( MSIPACKAGE *package, MSIMEDIAINFO *mi, LPVOID data )
616 static char filename[] = {'<','S','T','R','E','A','M','>',0};
617 HFDI hfdi;
618 ERF erf;
619 BOOL ret = FALSE;
621 TRACE("extracting %s disk id %u\n", debugstr_w(mi->cabinet), mi->disk_id);
623 hfdi = FDICreate( cabinet_alloc, cabinet_free, cabinet_open_stream, cabinet_read_stream,
624 cabinet_write, cabinet_close_stream, cabinet_seek_stream, 0, &erf );
625 if (!hfdi)
627 ERR("FDICreate failed\n");
628 return FALSE;
631 package_disk.package = package;
632 package_disk.id = mi->disk_id;
634 ret = FDICopy( hfdi, filename, NULL, 0, cabinet_notify_stream, NULL, data );
635 if (!ret) ERR("FDICopy failed\n");
637 FDIDestroy( hfdi );
638 if (ret) mi->is_extracted = TRUE;
639 return ret;
642 /***********************************************************************
643 * msi_cabextract
645 * Extract files from a cabinet file or stream.
647 BOOL msi_cabextract(MSIPACKAGE* package, MSIMEDIAINFO *mi, LPVOID data)
649 if (mi->cabinet[0] == '#')
651 return extract_cabinet_stream( package, mi, data );
653 return extract_cabinet( package, mi, data );
656 void msi_free_media_info(MSIMEDIAINFO *mi)
658 msi_free(mi->disk_prompt);
659 msi_free(mi->cabinet);
660 msi_free(mi->volume_label);
661 msi_free(mi->first_volume);
662 msi_free(mi);
665 static UINT get_drive_type(const WCHAR *path)
667 WCHAR root[MAX_PATH + 1];
669 strcpyW(root, path);
670 PathStripToRootW(root);
671 PathAddBackslashW(root);
673 return GetDriveTypeW(root);
676 UINT msi_load_media_info(MSIPACKAGE *package, UINT Sequence, MSIMEDIAINFO *mi)
678 static const WCHAR query[] = {
679 'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ','`','M','e','d','i','a','`',' ',
680 'W','H','E','R','E',' ','`','L','a','s','t','S','e','q','u','e','n','c','e','`',' ',
681 '>','=',' ','%','i',' ','O','R','D','E','R',' ','B','Y',' ','`','D','i','s','k','I','d','`',0};
682 MSIRECORD *row;
683 LPWSTR source_dir, source;
684 DWORD options;
686 if (Sequence <= mi->last_sequence) /* already loaded */
687 return ERROR_SUCCESS;
689 row = MSI_QueryGetRecord(package->db, query, Sequence);
690 if (!row)
692 TRACE("Unable to query row\n");
693 return ERROR_FUNCTION_FAILED;
696 mi->is_extracted = FALSE;
697 mi->disk_id = MSI_RecordGetInteger(row, 1);
698 mi->last_sequence = MSI_RecordGetInteger(row, 2);
699 msi_free(mi->disk_prompt);
700 mi->disk_prompt = strdupW(MSI_RecordGetString(row, 3));
701 msi_free(mi->cabinet);
702 mi->cabinet = strdupW(MSI_RecordGetString(row, 4));
703 msi_free(mi->volume_label);
704 mi->volume_label = strdupW(MSI_RecordGetString(row, 5));
705 msiobj_release(&row->hdr);
707 if (!mi->first_volume)
708 mi->first_volume = strdupW(mi->volume_label);
710 msi_set_sourcedir_props(package, FALSE);
711 source_dir = msi_dup_property(package->db, szSourceDir);
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)
719 source = source_dir;
720 options |= MSISOURCETYPE_MEDIA;
722 else if (package->BaseURL && UrlIsW(package->BaseURL, URLIS_URL))
724 source = package->BaseURL;
725 options |= MSISOURCETYPE_URL;
727 else
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 msi_free(source_dir);
741 TRACE("sequence %u -> cabinet %s disk id %u\n", Sequence, debugstr_w(mi->cabinet), mi->disk_id);
742 return ERROR_SUCCESS;
745 /* FIXME: search URL sources as well */
746 static UINT find_published_source(MSIPACKAGE *package, MSIMEDIAINFO *mi)
748 WCHAR source[MAX_PATH];
749 WCHAR volume[MAX_PATH];
750 WCHAR prompt[MAX_PATH];
751 DWORD volumesz, promptsz;
752 DWORD index, size, id;
753 WCHAR last_type[2];
754 UINT r;
756 size = 2;
757 r = MsiSourceListGetInfoW(package->ProductCode, NULL,
758 package->Context, MSICODE_PRODUCT,
759 INSTALLPROPERTY_LASTUSEDTYPEW, last_type, &size);
760 if (r != ERROR_SUCCESS)
761 return r;
763 size = MAX_PATH;
764 r = MsiSourceListGetInfoW(package->ProductCode, NULL,
765 package->Context, MSICODE_PRODUCT,
766 INSTALLPROPERTY_LASTUSEDSOURCEW, source, &size);
767 if (r != ERROR_SUCCESS)
768 return r;
770 if (last_type[0] == 'n')
772 WCHAR cabinet_file[MAX_PATH];
773 BOOL check_all = FALSE;
775 while(TRUE)
777 index = 0;
778 volumesz = MAX_PATH;
779 while (MsiSourceListEnumSourcesW(package->ProductCode, NULL,
780 package->Context,
781 MSISOURCETYPE_NETWORK, index++,
782 volume, &volumesz) == ERROR_SUCCESS)
784 if (check_all || !strncmpiW(source, volume, strlenW(source)))
786 lstrcpyW(cabinet_file, volume);
787 PathAddBackslashW(cabinet_file);
788 lstrcatW(cabinet_file, mi->cabinet);
790 if (GetFileAttributesW(cabinet_file) == INVALID_FILE_ATTRIBUTES)
792 volumesz = MAX_PATH;
793 if(!check_all)
794 break;
795 continue;
798 lstrcpyW(mi->sourcedir, volume);
799 PathAddBackslashW(mi->sourcedir);
800 TRACE("Found network source %s\n", debugstr_w(mi->sourcedir));
801 return ERROR_SUCCESS;
805 if (!check_all)
806 check_all = TRUE;
807 else
808 break;
812 index = 0;
813 volumesz = MAX_PATH;
814 promptsz = MAX_PATH;
815 while (MsiSourceListEnumMediaDisksW(package->ProductCode, NULL,
816 package->Context,
817 MSICODE_PRODUCT, index++, &id,
818 volume, &volumesz, prompt, &promptsz) == ERROR_SUCCESS)
820 mi->disk_id = id;
821 msi_free( mi->volume_label );
822 if (!(mi->volume_label = msi_alloc( ++volumesz * sizeof(WCHAR) ))) return ERROR_OUTOFMEMORY;
823 strcpyW( mi->volume_label, volume );
825 msi_free( mi->disk_prompt );
826 if (!(mi->disk_prompt = msi_alloc( ++promptsz * sizeof(WCHAR) ))) return ERROR_OUTOFMEMORY;
827 strcpyW( mi->disk_prompt, prompt );
829 if (source_matches_volume(mi, source))
831 /* FIXME: what about SourceDir */
832 lstrcpyW(mi->sourcedir, source);
833 PathAddBackslashW(mi->sourcedir);
834 TRACE("Found disk source %s\n", debugstr_w(mi->sourcedir));
835 return ERROR_SUCCESS;
839 return ERROR_FUNCTION_FAILED;
842 UINT ready_media( MSIPACKAGE *package, BOOL compressed, MSIMEDIAINFO *mi )
844 UINT rc;
845 WCHAR *cabinet_file = NULL;
847 /* media info for continuous cabinet is already loaded */
848 if (mi->is_continuous) return ERROR_SUCCESS;
850 if (mi->cabinet)
852 /* cabinet is internal, no checks needed */
853 if (mi->cabinet[0] == '#') return ERROR_SUCCESS;
855 if (!(cabinet_file = get_cabinet_filename( mi ))) return ERROR_OUTOFMEMORY;
857 /* package should be downloaded */
858 if (compressed && GetFileAttributesW( cabinet_file ) == INVALID_FILE_ATTRIBUTES &&
859 package->BaseURL && UrlIsW( package->BaseURL, URLIS_URL ))
861 WCHAR temppath[MAX_PATH], *p;
863 if ((rc = msi_download_file( cabinet_file, temppath )) != ERROR_SUCCESS)
865 ERR("failed to download %s (%u)\n", debugstr_w(cabinet_file), rc);
866 msi_free( cabinet_file );
867 return rc;
869 if ((p = strrchrW( temppath, '\\' ))) *p = 0;
870 strcpyW( mi->sourcedir, temppath );
871 PathAddBackslashW( mi->sourcedir );
872 msi_free( mi->cabinet );
873 mi->cabinet = strdupW( p + 1 );
874 msi_free( cabinet_file );
875 return ERROR_SUCCESS;
878 /* check volume matches, change media if not */
879 if (mi->volume_label && mi->disk_id > 1 && strcmpW( mi->first_volume, mi->volume_label ))
881 WCHAR *source = msi_dup_property( package->db, szSourceDir );
882 BOOL match = source_matches_volume( mi, source );
883 msi_free( source );
885 if (!match && (mi->type == DRIVE_CDROM || mi->type == DRIVE_REMOVABLE))
887 if ((rc = msi_change_media( package, mi )) != ERROR_SUCCESS)
889 msi_free( cabinet_file );
890 return rc;
894 if (mi->cabinet)
896 if (compressed && GetFileAttributesW( cabinet_file ) == INVALID_FILE_ATTRIBUTES)
898 if ((rc = find_published_source( package, mi )) != ERROR_SUCCESS)
900 ERR("cabinet not found: %s\n", debugstr_w(cabinet_file));
901 msi_free( cabinet_file );
902 return ERROR_INSTALL_FAILURE;
906 msi_free( cabinet_file );
907 return ERROR_SUCCESS;
910 UINT msi_add_cabinet_stream( MSIPACKAGE *package, UINT disk_id, IStorage *storage, const WCHAR *name )
912 MSICABINETSTREAM *cab, *item;
914 TRACE("%p, %u, %p, %s\n", package, disk_id, storage, debugstr_w(name));
916 LIST_FOR_EACH_ENTRY( item, &package->cabinet_streams, MSICABINETSTREAM, entry )
918 if (item->disk_id == disk_id)
920 TRACE("duplicate disk id %u\n", disk_id);
921 return ERROR_FUNCTION_FAILED;
924 if (!(cab = msi_alloc( sizeof(*cab) ))) return ERROR_OUTOFMEMORY;
925 if (!(cab->stream = msi_alloc( (strlenW( name ) + 1) * sizeof(WCHAR ) )))
927 msi_free( cab );
928 return ERROR_OUTOFMEMORY;
930 strcpyW( cab->stream, name );
931 cab->disk_id = disk_id;
932 cab->storage = storage;
933 IStorage_AddRef( storage );
934 list_add_tail( &package->cabinet_streams, &cab->entry );
936 return ERROR_SUCCESS;