msi: Create temp files in the destination directory instead of the root.
[wine/multimedia.git] / dlls / msi / media.c
blob9f29ef730a5371ae20bc81445d49e1ada5e3bd13
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,
65 NULL, NULL, NULL, NULL, 0))
67 ERR("Failed to get volume information\n");
68 return FALSE;
71 return !strcmpW( mi->volume_label, volume_name );
74 static UINT msi_change_media(MSIPACKAGE *package, MSIMEDIAINFO *mi)
76 LPWSTR error, error_dialog;
77 LPWSTR source_dir;
78 UINT r = ERROR_SUCCESS;
80 static const WCHAR error_prop[] = {'E','r','r','o','r','D','i','a','l','o','g',0};
82 if ((msi_get_property_int(package->db, szUILevel, 0) & INSTALLUILEVEL_MASK) ==
83 INSTALLUILEVEL_NONE && !gUIHandlerA && !gUIHandlerW && !gUIHandlerRecord)
84 return ERROR_SUCCESS;
86 error = msi_build_error_string(package, 1302, 1, mi->disk_prompt);
87 error_dialog = msi_dup_property(package->db, error_prop);
88 source_dir = msi_dup_property(package->db, szSourceDir);
90 while (r == ERROR_SUCCESS && !source_matches_volume(mi, source_dir))
92 r = msi_spawn_error_dialog(package, error_dialog, error);
94 if (gUIHandlerW)
96 gUIHandlerW(gUIContext, MB_RETRYCANCEL | INSTALLMESSAGE_ERROR, error);
98 else if (gUIHandlerA)
100 char *msg = strdupWtoA(error);
101 gUIHandlerA(gUIContext, MB_RETRYCANCEL | INSTALLMESSAGE_ERROR, msg);
102 msi_free(msg);
104 else if (gUIHandlerRecord)
106 MSIHANDLE rec = MsiCreateRecord(1);
107 MsiRecordSetStringW(rec, 0, error);
108 gUIHandlerRecord(gUIContext, MB_RETRYCANCEL | INSTALLMESSAGE_ERROR, rec);
109 MsiCloseHandle(rec);
113 msi_free(error);
114 msi_free(error_dialog);
115 msi_free(source_dir);
117 return r;
120 static MSICABINETSTREAM *msi_get_cabinet_stream( MSIPACKAGE *package, UINT disk_id )
122 MSICABINETSTREAM *cab;
124 LIST_FOR_EACH_ENTRY( cab, &package->cabinet_streams, MSICABINETSTREAM, entry )
126 if (cab->disk_id == disk_id) return cab;
128 return NULL;
131 static void * CDECL cabinet_alloc(ULONG cb)
133 return msi_alloc(cb);
136 static void CDECL cabinet_free(void *pv)
138 msi_free(pv);
141 static INT_PTR CDECL cabinet_open(char *pszFile, int oflag, int pmode)
143 HANDLE handle;
144 DWORD dwAccess = 0;
145 DWORD dwShareMode = 0;
146 DWORD dwCreateDisposition = OPEN_EXISTING;
148 switch (oflag & _O_ACCMODE)
150 case _O_RDONLY:
151 dwAccess = GENERIC_READ;
152 dwShareMode = FILE_SHARE_READ | FILE_SHARE_DELETE;
153 break;
154 case _O_WRONLY:
155 dwAccess = GENERIC_WRITE;
156 dwShareMode = FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE;
157 break;
158 case _O_RDWR:
159 dwAccess = GENERIC_READ | GENERIC_WRITE;
160 dwShareMode = FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE;
161 break;
164 if ((oflag & (_O_CREAT | _O_EXCL)) == (_O_CREAT | _O_EXCL))
165 dwCreateDisposition = CREATE_NEW;
166 else if (oflag & _O_CREAT)
167 dwCreateDisposition = CREATE_ALWAYS;
169 handle = CreateFileA(pszFile, dwAccess, dwShareMode, NULL,
170 dwCreateDisposition, 0, NULL);
171 if (handle == INVALID_HANDLE_VALUE)
172 return 0;
174 return (INT_PTR)handle;
177 static UINT CDECL cabinet_read(INT_PTR hf, void *pv, UINT cb)
179 HANDLE handle = (HANDLE)hf;
180 DWORD read;
182 if (ReadFile(handle, pv, cb, &read, NULL))
183 return read;
185 return 0;
188 static UINT CDECL cabinet_write(INT_PTR hf, void *pv, UINT cb)
190 HANDLE handle = (HANDLE)hf;
191 DWORD written;
193 if (WriteFile(handle, pv, cb, &written, NULL))
194 return written;
196 return 0;
199 static int CDECL cabinet_close(INT_PTR hf)
201 HANDLE handle = (HANDLE)hf;
202 return CloseHandle(handle) ? 0 : -1;
205 static LONG CDECL cabinet_seek(INT_PTR hf, LONG dist, int seektype)
207 HANDLE handle = (HANDLE)hf;
208 /* flags are compatible and so are passed straight through */
209 return SetFilePointer(handle, dist, NULL, seektype);
212 struct package_disk
214 MSIPACKAGE *package;
215 UINT id;
218 static struct package_disk package_disk;
220 static INT_PTR CDECL cabinet_open_stream( char *pszFile, int oflag, int pmode )
222 MSICABINETSTREAM *cab;
223 IStream *stream;
224 WCHAR *encoded;
225 HRESULT hr;
227 cab = msi_get_cabinet_stream( package_disk.package, package_disk.id );
228 if (!cab)
230 WARN("failed to get cabinet stream\n");
231 return 0;
233 if (!cab->stream[0] || !(encoded = encode_streamname( FALSE, cab->stream + 1 )))
235 WARN("failed to encode stream name\n");
236 return 0;
238 if (msi_clone_open_stream( package_disk.package->db, cab->storage, encoded, &stream ) != ERROR_SUCCESS)
240 hr = IStorage_OpenStream( cab->storage, encoded, NULL, STGM_READ|STGM_SHARE_EXCLUSIVE, 0, &stream );
241 if (FAILED(hr))
243 WARN("failed to open stream 0x%08x\n", hr);
244 msi_free( encoded );
245 return 0;
248 msi_free( encoded );
249 return (INT_PTR)stream;
252 static UINT CDECL cabinet_read_stream( INT_PTR hf, void *pv, UINT cb )
254 IStream *stm = (IStream *)hf;
255 DWORD read;
256 HRESULT hr;
258 hr = IStream_Read( stm, pv, cb, &read );
259 if (hr == S_OK || hr == S_FALSE)
260 return read;
262 return 0;
265 static int CDECL cabinet_close_stream( INT_PTR hf )
267 IStream *stm = (IStream *)hf;
268 IStream_Release( stm );
269 return 0;
272 static LONG CDECL cabinet_seek_stream( INT_PTR hf, LONG dist, int seektype )
274 IStream *stm = (IStream *)hf;
275 LARGE_INTEGER move;
276 ULARGE_INTEGER newpos;
277 HRESULT hr;
279 move.QuadPart = dist;
280 hr = IStream_Seek( stm, move, seektype, &newpos );
281 if (SUCCEEDED(hr))
283 if (newpos.QuadPart <= MAXLONG) return newpos.QuadPart;
284 ERR("Too big!\n");
286 return -1;
289 static UINT CDECL msi_media_get_disk_info(MSIPACKAGE *package, MSIMEDIAINFO *mi)
291 MSIRECORD *row;
293 static const WCHAR query[] = {
294 'S','E','L','E','C','T',' ','*',' ', 'F','R','O','M',' ',
295 '`','M','e','d','i','a','`',' ','W','H','E','R','E',' ',
296 '`','D','i','s','k','I','d','`',' ','=',' ','%','i',0};
298 row = MSI_QueryGetRecord(package->db, query, mi->disk_id);
299 if (!row)
301 TRACE("Unable to query row\n");
302 return ERROR_FUNCTION_FAILED;
305 mi->disk_prompt = strdupW(MSI_RecordGetString(row, 3));
306 mi->cabinet = strdupW(MSI_RecordGetString(row, 4));
307 mi->volume_label = strdupW(MSI_RecordGetString(row, 5));
309 if (!mi->first_volume)
310 mi->first_volume = strdupW(mi->volume_label);
312 msiobj_release(&row->hdr);
313 return ERROR_SUCCESS;
316 static INT_PTR cabinet_partial_file(FDINOTIFICATIONTYPE fdint,
317 PFDINOTIFICATION pfdin)
319 MSICABDATA *data = pfdin->pv;
320 data->mi->is_continuous = FALSE;
321 return 0;
324 static WCHAR *get_cabinet_filename(MSIMEDIAINFO *mi)
326 int len;
327 WCHAR *ret;
329 len = strlenW(mi->sourcedir) + strlenW(mi->cabinet) + 1;
330 if (!(ret = msi_alloc(len * sizeof(WCHAR)))) return NULL;
331 strcpyW(ret, mi->sourcedir);
332 strcatW(ret, mi->cabinet);
333 return ret;
336 static INT_PTR cabinet_next_cabinet(FDINOTIFICATIONTYPE fdint,
337 PFDINOTIFICATION pfdin)
339 MSICABDATA *data = pfdin->pv;
340 MSIMEDIAINFO *mi = data->mi;
341 LPWSTR cabinet_file = NULL, cab = strdupAtoW(pfdin->psz1);
342 INT_PTR res = -1;
343 UINT rc;
345 msi_free(mi->disk_prompt);
346 msi_free(mi->cabinet);
347 msi_free(mi->volume_label);
348 mi->disk_prompt = NULL;
349 mi->cabinet = NULL;
350 mi->volume_label = NULL;
352 mi->disk_id++;
353 mi->is_continuous = TRUE;
355 rc = msi_media_get_disk_info(data->package, mi);
356 if (rc != ERROR_SUCCESS)
358 ERR("Failed to get next cabinet information: %d\n", rc);
359 goto done;
362 if (strcmpiW( mi->cabinet, cab ))
364 ERR("Continuous cabinet does not match the next cabinet in the Media table\n");
365 goto done;
368 if (!(cabinet_file = get_cabinet_filename(mi)))
369 goto done;
371 TRACE("Searching for %s\n", debugstr_w(cabinet_file));
373 res = 0;
374 if (GetFileAttributesW(cabinet_file) == INVALID_FILE_ATTRIBUTES)
376 if (msi_change_media(data->package, mi) != ERROR_SUCCESS)
377 res = -1;
380 done:
381 msi_free(cab);
382 msi_free(cabinet_file);
383 return res;
386 static INT_PTR cabinet_next_cabinet_stream( FDINOTIFICATIONTYPE fdint,
387 PFDINOTIFICATION pfdin )
389 MSICABDATA *data = pfdin->pv;
390 MSIMEDIAINFO *mi = data->mi;
391 UINT rc;
393 msi_free( mi->disk_prompt );
394 msi_free( mi->cabinet );
395 msi_free( mi->volume_label );
396 mi->disk_prompt = NULL;
397 mi->cabinet = NULL;
398 mi->volume_label = NULL;
400 mi->disk_id++;
401 mi->is_continuous = TRUE;
403 rc = msi_media_get_disk_info( data->package, mi );
404 if (rc != ERROR_SUCCESS)
406 ERR("Failed to get next cabinet information: %u\n", rc);
407 return -1;
409 package_disk.id = mi->disk_id;
411 TRACE("next cabinet is %s disk id %u\n", debugstr_w(mi->cabinet), mi->disk_id);
412 return 0;
415 static INT_PTR cabinet_copy_file(FDINOTIFICATIONTYPE fdint,
416 PFDINOTIFICATION pfdin)
418 MSICABDATA *data = pfdin->pv;
419 HANDLE handle = 0;
420 LPWSTR path = NULL;
421 DWORD attrs;
423 data->curfile = strdupAtoW(pfdin->psz1);
424 if (!data->cb(data->package, data->curfile, MSICABEXTRACT_BEGINEXTRACT, &path,
425 &attrs, data->user))
427 /* We're not extracting this file, so free the filename. */
428 msi_free(data->curfile);
429 data->curfile = NULL;
430 goto done;
433 TRACE("extracting %s\n", debugstr_w(path));
435 attrs = attrs & (FILE_ATTRIBUTE_READONLY|FILE_ATTRIBUTE_HIDDEN|FILE_ATTRIBUTE_SYSTEM);
436 if (!attrs) attrs = FILE_ATTRIBUTE_NORMAL;
438 handle = CreateFileW(path, GENERIC_READ | GENERIC_WRITE, 0,
439 NULL, CREATE_ALWAYS, attrs, NULL);
440 if (handle == INVALID_HANDLE_VALUE)
442 DWORD err = GetLastError();
443 DWORD attrs2 = GetFileAttributesW(path);
445 if (attrs2 == INVALID_FILE_ATTRIBUTES)
447 ERR("failed to create %s (error %d)\n", debugstr_w(path), err);
448 goto done;
450 else if (err == ERROR_ACCESS_DENIED && (attrs2 & FILE_ATTRIBUTE_READONLY))
452 TRACE("removing read-only attribute on %s\n", debugstr_w(path));
453 SetFileAttributesW( path, attrs2 & ~FILE_ATTRIBUTE_READONLY );
454 handle = CreateFileW(path, GENERIC_READ | GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, attrs2, NULL);
456 if (handle != INVALID_HANDLE_VALUE) goto done;
457 err = GetLastError();
459 if (err == ERROR_SHARING_VIOLATION || err == ERROR_USER_MAPPED_FILE)
461 WCHAR *tmpfileW, *tmppathW, *p;
462 DWORD len;
464 TRACE("file in use, scheduling rename operation\n");
466 if (!(tmppathW = strdupW( path ))) return ERROR_OUTOFMEMORY;
467 if ((p = strrchrW(tmppathW, '\\'))) *p = 0;
468 len = strlenW( tmppathW ) + 16;
469 if (!(tmpfileW = msi_alloc(len * sizeof(WCHAR))))
471 msi_free( tmppathW );
472 return ERROR_OUTOFMEMORY;
474 if (!GetTempFileNameW(tmppathW, szMsi, 0, tmpfileW)) tmpfileW[0] = 0;
475 msi_free( tmppathW );
477 handle = CreateFileW(tmpfileW, GENERIC_READ | GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, attrs, NULL);
479 if (handle != INVALID_HANDLE_VALUE &&
480 MoveFileExW(path, NULL, MOVEFILE_DELAY_UNTIL_REBOOT) &&
481 MoveFileExW(tmpfileW, path, MOVEFILE_DELAY_UNTIL_REBOOT))
483 data->package->need_reboot = 1;
485 else
487 WARN("failed to schedule rename operation %s (error %d)\n", debugstr_w(path), GetLastError());
488 DeleteFileW( tmpfileW );
490 msi_free(tmpfileW);
492 else
493 WARN("failed to create %s (error %d)\n", debugstr_w(path), err);
496 done:
497 msi_free(path);
499 return (INT_PTR)handle;
502 static INT_PTR cabinet_close_file_info(FDINOTIFICATIONTYPE fdint,
503 PFDINOTIFICATION pfdin)
505 MSICABDATA *data = pfdin->pv;
506 FILETIME ft;
507 FILETIME ftLocal;
508 HANDLE handle = (HANDLE)pfdin->hf;
510 data->mi->is_continuous = FALSE;
512 if (!DosDateTimeToFileTime(pfdin->date, pfdin->time, &ft))
513 return -1;
514 if (!LocalFileTimeToFileTime(&ft, &ftLocal))
515 return -1;
516 if (!SetFileTime(handle, &ftLocal, 0, &ftLocal))
517 return -1;
519 CloseHandle(handle);
521 data->cb(data->package, data->curfile, MSICABEXTRACT_FILEEXTRACTED, NULL, NULL,
522 data->user);
524 msi_free(data->curfile);
525 data->curfile = NULL;
527 return 1;
530 static INT_PTR CDECL cabinet_notify(FDINOTIFICATIONTYPE fdint, PFDINOTIFICATION pfdin)
532 switch (fdint)
534 case fdintPARTIAL_FILE:
535 return cabinet_partial_file(fdint, pfdin);
537 case fdintNEXT_CABINET:
538 return cabinet_next_cabinet(fdint, pfdin);
540 case fdintCOPY_FILE:
541 return cabinet_copy_file(fdint, pfdin);
543 case fdintCLOSE_FILE_INFO:
544 return cabinet_close_file_info(fdint, pfdin);
546 default:
547 return 0;
551 static INT_PTR CDECL cabinet_notify_stream( FDINOTIFICATIONTYPE fdint, PFDINOTIFICATION pfdin )
553 switch (fdint)
555 case fdintPARTIAL_FILE:
556 return cabinet_partial_file( fdint, pfdin );
558 case fdintNEXT_CABINET:
559 return cabinet_next_cabinet_stream( fdint, pfdin );
561 case fdintCOPY_FILE:
562 return cabinet_copy_file( fdint, pfdin );
564 case fdintCLOSE_FILE_INFO:
565 return cabinet_close_file_info( fdint, pfdin );
567 case fdintCABINET_INFO:
568 return 0;
570 default:
571 ERR("Unexpected notification %d\n", fdint);
572 return 0;
576 static BOOL extract_cabinet( MSIPACKAGE* package, MSIMEDIAINFO *mi, LPVOID data )
578 LPSTR cabinet, cab_path = NULL;
579 HFDI hfdi;
580 ERF erf;
581 BOOL ret = FALSE;
583 TRACE("extracting %s disk id %u\n", debugstr_w(mi->cabinet), mi->disk_id);
585 hfdi = FDICreate( cabinet_alloc, cabinet_free, cabinet_open, cabinet_read,
586 cabinet_write, cabinet_close, cabinet_seek, 0, &erf );
587 if (!hfdi)
589 ERR("FDICreate failed\n");
590 return FALSE;
593 cabinet = strdupWtoA( mi->cabinet );
594 if (!cabinet)
595 goto done;
597 cab_path = strdupWtoA( mi->sourcedir );
598 if (!cab_path)
599 goto done;
601 ret = FDICopy( hfdi, cabinet, cab_path, 0, cabinet_notify, NULL, data );
602 if (!ret)
603 ERR("FDICopy failed\n");
605 done:
606 FDIDestroy( hfdi );
607 msi_free(cabinet );
608 msi_free( cab_path );
610 if (ret)
611 mi->is_extracted = TRUE;
613 return ret;
616 static BOOL extract_cabinet_stream( MSIPACKAGE *package, MSIMEDIAINFO *mi, LPVOID data )
618 static char filename[] = {'<','S','T','R','E','A','M','>',0};
619 HFDI hfdi;
620 ERF erf;
621 BOOL ret = FALSE;
623 TRACE("extracting %s disk id %u\n", debugstr_w(mi->cabinet), mi->disk_id);
625 hfdi = FDICreate( cabinet_alloc, cabinet_free, cabinet_open_stream, cabinet_read_stream,
626 cabinet_write, cabinet_close_stream, cabinet_seek_stream, 0, &erf );
627 if (!hfdi)
629 ERR("FDICreate failed\n");
630 return FALSE;
633 package_disk.package = package;
634 package_disk.id = mi->disk_id;
636 ret = FDICopy( hfdi, filename, NULL, 0, cabinet_notify_stream, NULL, data );
637 if (!ret) ERR("FDICopy failed\n");
639 FDIDestroy( hfdi );
640 if (ret) mi->is_extracted = TRUE;
641 return ret;
644 /***********************************************************************
645 * msi_cabextract
647 * Extract files from a cabinet file or stream.
649 BOOL msi_cabextract(MSIPACKAGE* package, MSIMEDIAINFO *mi, LPVOID data)
651 if (mi->cabinet[0] == '#')
653 return extract_cabinet_stream( package, mi, data );
655 return extract_cabinet( package, mi, data );
658 void msi_free_media_info(MSIMEDIAINFO *mi)
660 msi_free(mi->disk_prompt);
661 msi_free(mi->cabinet);
662 msi_free(mi->volume_label);
663 msi_free(mi->first_volume);
664 msi_free(mi);
667 static UINT get_drive_type(const WCHAR *path)
669 WCHAR root[MAX_PATH + 1];
671 strcpyW(root, path);
672 PathStripToRootW(root);
673 PathAddBackslashW(root);
675 return GetDriveTypeW(root);
678 UINT msi_load_media_info(MSIPACKAGE *package, UINT Sequence, MSIMEDIAINFO *mi)
680 static const WCHAR query[] = {
681 'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ','`','M','e','d','i','a','`',' ',
682 'W','H','E','R','E',' ','`','L','a','s','t','S','e','q','u','e','n','c','e','`',' ',
683 '>','=',' ','%','i',' ','O','R','D','E','R',' ','B','Y',' ','`','D','i','s','k','I','d','`',0};
684 MSIRECORD *row;
685 LPWSTR source_dir, source;
686 DWORD options;
688 if (Sequence <= mi->last_sequence) /* already loaded */
689 return ERROR_SUCCESS;
691 row = MSI_QueryGetRecord(package->db, query, Sequence);
692 if (!row)
694 TRACE("Unable to query row\n");
695 return ERROR_FUNCTION_FAILED;
698 mi->is_extracted = FALSE;
699 mi->disk_id = MSI_RecordGetInteger(row, 1);
700 mi->last_sequence = MSI_RecordGetInteger(row, 2);
701 msi_free(mi->disk_prompt);
702 mi->disk_prompt = strdupW(MSI_RecordGetString(row, 3));
703 msi_free(mi->cabinet);
704 mi->cabinet = strdupW(MSI_RecordGetString(row, 4));
705 msi_free(mi->volume_label);
706 mi->volume_label = strdupW(MSI_RecordGetString(row, 5));
707 msiobj_release(&row->hdr);
709 if (!mi->first_volume)
710 mi->first_volume = strdupW(mi->volume_label);
712 msi_set_sourcedir_props(package, FALSE);
713 source_dir = msi_dup_property(package->db, szSourceDir);
714 lstrcpyW(mi->sourcedir, source_dir);
715 mi->type = get_drive_type(source_dir);
717 options = MSICODE_PRODUCT;
718 if (mi->type == DRIVE_CDROM || mi->type == DRIVE_REMOVABLE)
720 source = source_dir;
721 options |= MSISOURCETYPE_MEDIA;
723 else if (package->BaseURL && UrlIsW(package->BaseURL, URLIS_URL))
725 source = package->BaseURL;
726 options |= MSISOURCETYPE_URL;
728 else
730 source = mi->sourcedir;
731 options |= MSISOURCETYPE_NETWORK;
734 msi_package_add_media_disk(package, package->Context,
735 MSICODE_PRODUCT, mi->disk_id,
736 mi->volume_label, mi->disk_prompt);
738 msi_package_add_info(package, package->Context,
739 options, INSTALLPROPERTY_LASTUSEDSOURCEW, source);
741 msi_free(source_dir);
742 TRACE("sequence %u -> cabinet %s disk id %u\n", Sequence, debugstr_w(mi->cabinet), mi->disk_id);
743 return ERROR_SUCCESS;
746 /* FIXME: search URL sources as well */
747 static UINT find_published_source(MSIPACKAGE *package, MSIMEDIAINFO *mi)
749 WCHAR source[MAX_PATH];
750 WCHAR volume[MAX_PATH];
751 WCHAR prompt[MAX_PATH];
752 DWORD volumesz, promptsz;
753 DWORD index, size, id;
754 WCHAR last_type[2];
755 UINT r;
757 size = 2;
758 r = MsiSourceListGetInfoW(package->ProductCode, NULL,
759 package->Context, MSICODE_PRODUCT,
760 INSTALLPROPERTY_LASTUSEDTYPEW, last_type, &size);
761 if (r != ERROR_SUCCESS)
762 return r;
764 size = MAX_PATH;
765 r = MsiSourceListGetInfoW(package->ProductCode, NULL,
766 package->Context, MSICODE_PRODUCT,
767 INSTALLPROPERTY_LASTUSEDSOURCEW, source, &size);
768 if (r != ERROR_SUCCESS)
769 return r;
771 if (last_type[0] == 'n')
773 WCHAR cabinet_file[MAX_PATH];
774 BOOL check_all = FALSE;
776 while(TRUE)
778 index = 0;
779 volumesz = MAX_PATH;
780 while (MsiSourceListEnumSourcesW(package->ProductCode, NULL,
781 package->Context,
782 MSISOURCETYPE_NETWORK, index++,
783 volume, &volumesz) == ERROR_SUCCESS)
785 if (check_all || !strncmpiW(source, volume, strlenW(source)))
787 lstrcpyW(cabinet_file, volume);
788 PathAddBackslashW(cabinet_file);
789 lstrcatW(cabinet_file, mi->cabinet);
791 if (GetFileAttributesW(cabinet_file) == INVALID_FILE_ATTRIBUTES)
793 volumesz = MAX_PATH;
794 if(!check_all)
795 break;
796 continue;
799 lstrcpyW(mi->sourcedir, volume);
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 mi->volume_label = msi_realloc(mi->volume_label, ++volumesz * sizeof(WCHAR));
822 lstrcpyW(mi->volume_label, volume);
823 mi->disk_prompt = msi_realloc(mi->disk_prompt, ++promptsz * sizeof(WCHAR));
824 lstrcpyW(mi->disk_prompt, prompt);
826 if (source_matches_volume(mi, source))
828 /* FIXME: what about SourceDir */
829 lstrcpyW(mi->sourcedir, source);
830 TRACE("Found disk source %s\n", debugstr_w(mi->sourcedir));
831 return ERROR_SUCCESS;
835 return ERROR_FUNCTION_FAILED;
838 UINT ready_media(MSIPACKAGE *package, UINT Sequence, BOOL IsCompressed, MSIMEDIAINFO *mi)
840 UINT rc = ERROR_SUCCESS;
841 WCHAR *cabinet_file;
843 /* media info for continuous cabinet is already loaded */
844 if (mi->is_continuous)
845 return ERROR_SUCCESS;
847 /* cabinet is internal, no checks needed */
848 if (!mi->cabinet || mi->cabinet[0] == '#')
849 return ERROR_SUCCESS;
851 cabinet_file = get_cabinet_filename(mi);
853 /* package should be downloaded */
854 if (IsCompressed &&
855 GetFileAttributesW(cabinet_file) == INVALID_FILE_ATTRIBUTES &&
856 package->BaseURL && UrlIsW(package->BaseURL, URLIS_URL))
858 WCHAR temppath[MAX_PATH], *p;
860 rc = msi_download_file(cabinet_file, temppath);
861 if (rc != ERROR_SUCCESS)
863 ERR("Failed to download %s (%u)\n", debugstr_w(cabinet_file), rc);
864 msi_free(cabinet_file);
865 return rc;
867 if ((p = strrchrW(temppath, '\\'))) *p = 0;
868 strcpyW(mi->sourcedir, temppath);
869 msi_free(mi->cabinet);
870 mi->cabinet = strdupW(p + 1);
872 msi_free(cabinet_file);
873 return ERROR_SUCCESS;
876 /* check volume matches, change media if not */
877 if (mi->volume_label && mi->disk_id > 1 &&
878 strcmpW( mi->first_volume, mi->volume_label ))
880 LPWSTR source = msi_dup_property(package->db, szSourceDir);
881 BOOL matches;
883 matches = source_matches_volume(mi, source);
884 msi_free(source);
886 if ((mi->type == DRIVE_CDROM || mi->type == DRIVE_REMOVABLE) && !matches)
888 rc = msi_change_media(package, mi);
889 if (rc != ERROR_SUCCESS)
891 msi_free(cabinet_file);
892 return rc;
897 if (IsCompressed &&
898 GetFileAttributesW(cabinet_file) == INVALID_FILE_ATTRIBUTES)
900 rc = find_published_source(package, mi);
901 if (rc != ERROR_SUCCESS)
903 ERR("Cabinet not found: %s\n", debugstr_w(cabinet_file));
904 msi_free(cabinet_file);
905 return ERROR_INSTALL_FAILURE;
909 msi_free(cabinet_file);
910 return ERROR_SUCCESS;
913 UINT msi_add_cabinet_stream( MSIPACKAGE *package, UINT disk_id, IStorage *storage, const WCHAR *name )
915 MSICABINETSTREAM *cab, *item;
917 TRACE("%p, %u, %p, %s\n", package, disk_id, storage, debugstr_w(name));
919 LIST_FOR_EACH_ENTRY( item, &package->cabinet_streams, MSICABINETSTREAM, entry )
921 if (item->disk_id == disk_id)
923 TRACE("duplicate disk id %u\n", disk_id);
924 return ERROR_FUNCTION_FAILED;
927 if (!(cab = msi_alloc( sizeof(*cab) ))) return ERROR_OUTOFMEMORY;
928 if (!(cab->stream = msi_alloc( (strlenW( name ) + 1) * sizeof(WCHAR ) )))
930 msi_free( cab );
931 return ERROR_OUTOFMEMORY;
933 strcpyW( cab->stream, name );
934 cab->disk_id = disk_id;
935 cab->storage = storage;
936 IStorage_AddRef( storage );
937 list_add_tail( &package->cabinet_streams, &cab->entry );
939 return ERROR_SUCCESS;