winemac: Simplify format_for_type() and callers since there's at most one format...
[wine.git] / dlls / msi / media.c
blob1357a648121559df45451636b24f898a7fd4e3ec
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], root[MAX_PATH + 1];
58 const WCHAR *p;
59 int len, len2;
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());
68 return FALSE;
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;
82 LPWSTR source_dir;
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);
98 if (gUIHandlerW)
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);
106 msi_free(msg);
108 else if (gUIHandlerRecord)
110 MSIHANDLE rec = MsiCreateRecord(1);
111 MsiRecordSetStringW(rec, 0, error);
112 gUIHandlerRecord(gUIContext, MB_RETRYCANCEL | INSTALLMESSAGE_ERROR, rec);
113 MsiCloseHandle(rec);
117 msi_free(error);
118 msi_free(error_dialog);
119 msi_free(source_dir);
121 return r;
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;
132 return NULL;
135 static void * CDECL cabinet_alloc(ULONG cb)
137 return msi_alloc(cb);
140 static void CDECL cabinet_free(void *pv)
142 msi_free(pv);
145 static INT_PTR CDECL cabinet_open(char *pszFile, int oflag, int pmode)
147 DWORD dwAccess = 0;
148 DWORD dwShareMode = 0;
149 DWORD dwCreateDisposition = OPEN_EXISTING;
151 switch (oflag & _O_ACCMODE)
153 case _O_RDONLY:
154 dwAccess = GENERIC_READ;
155 dwShareMode = FILE_SHARE_READ | FILE_SHARE_DELETE;
156 break;
157 case _O_WRONLY:
158 dwAccess = GENERIC_WRITE;
159 dwShareMode = FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE;
160 break;
161 case _O_RDWR:
162 dwAccess = GENERIC_READ | GENERIC_WRITE;
163 dwShareMode = FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE;
164 break;
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;
179 DWORD read;
181 if (ReadFile(handle, pv, cb, &read, NULL))
182 return read;
184 return 0;
187 static UINT CDECL cabinet_write(INT_PTR hf, void *pv, UINT cb)
189 HANDLE handle = (HANDLE)hf;
190 DWORD written;
192 if (WriteFile(handle, pv, cb, &written, NULL))
193 return written;
195 return 0;
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);
211 struct package_disk
213 MSIPACKAGE *package;
214 UINT id;
217 static struct package_disk package_disk;
219 static INT_PTR CDECL cabinet_open_stream( char *pszFile, int oflag, int pmode )
221 MSICABINETSTREAM *cab;
222 IStream *stream;
224 if (!(cab = msi_get_cabinet_stream( package_disk.package, package_disk.id )))
226 WARN("failed to get cabinet stream\n");
227 return -1;
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);
235 return -1;
238 else /* patch storage */
240 HRESULT hr;
241 WCHAR *encoded;
243 if (!(encoded = encode_streamname( FALSE, cab->stream + 1 )))
245 WARN("failed to encode stream name\n");
246 return -1;
248 hr = IStorage_OpenStream( cab->storage, encoded, NULL, STGM_READ|STGM_SHARE_EXCLUSIVE, 0, &stream );
249 msi_free( encoded );
250 if (FAILED(hr))
252 WARN("failed to open stream 0x%08x\n", hr);
253 return -1;
256 return (INT_PTR)stream;
259 static UINT CDECL cabinet_read_stream( INT_PTR hf, void *pv, UINT cb )
261 IStream *stm = (IStream *)hf;
262 DWORD read;
263 HRESULT hr;
265 hr = IStream_Read( stm, pv, cb, &read );
266 if (hr == S_OK || hr == S_FALSE)
267 return read;
269 return 0;
272 static int CDECL cabinet_close_stream( INT_PTR hf )
274 IStream *stm = (IStream *)hf;
275 IStream_Release( stm );
276 return 0;
279 static LONG CDECL cabinet_seek_stream( INT_PTR hf, LONG dist, int seektype )
281 IStream *stm = (IStream *)hf;
282 LARGE_INTEGER move;
283 ULARGE_INTEGER newpos;
284 HRESULT hr;
286 move.QuadPart = dist;
287 hr = IStream_Seek( stm, move, seektype, &newpos );
288 if (SUCCEEDED(hr))
290 if (newpos.QuadPart <= MAXLONG) return newpos.QuadPart;
291 ERR("Too big!\n");
293 return -1;
296 static UINT CDECL msi_media_get_disk_info(MSIPACKAGE *package, MSIMEDIAINFO *mi)
298 MSIRECORD *row;
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);
306 if (!row)
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;
325 return 0;
328 static WCHAR *get_cabinet_filename(MSIMEDIAINFO *mi)
330 int len;
331 WCHAR *ret;
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);
337 return ret;
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);
346 INT_PTR res = -1;
347 UINT rc;
349 msi_free(mi->disk_prompt);
350 msi_free(mi->cabinet);
351 msi_free(mi->volume_label);
352 mi->disk_prompt = NULL;
353 mi->cabinet = NULL;
354 mi->volume_label = NULL;
356 mi->disk_id++;
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);
363 goto done;
366 if (strcmpiW( mi->cabinet, cab ))
368 char *next_cab;
369 ULONG length;
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;
377 if (length > 256)
379 WARN("Cannot update next cabinet filename with a string size %u > 256\n", length);
380 msi_free(next_cab);
381 goto done;
383 else
385 strcat(pfdin->psz3, "\\");
386 strcat(pfdin->psz3, next_cab);
388 /* Path psz3 and cabinet psz1 are concatenated by FDI so just reset psz1 */
389 *pfdin->psz1 = 0;
390 msi_free(next_cab);
393 if (!(cabinet_file = get_cabinet_filename(mi)))
394 goto done;
396 TRACE("Searching for %s\n", debugstr_w(cabinet_file));
398 res = 0;
399 if (GetFileAttributesW(cabinet_file) == INVALID_FILE_ATTRIBUTES)
401 if (msi_change_media(data->package, mi) != ERROR_SUCCESS)
402 res = -1;
405 done:
406 msi_free(cab);
407 msi_free(cabinet_file);
408 return res;
411 static INT_PTR cabinet_next_cabinet_stream( FDINOTIFICATIONTYPE fdint,
412 PFDINOTIFICATION pfdin )
414 MSICABDATA *data = pfdin->pv;
415 MSIMEDIAINFO *mi = data->mi;
416 UINT rc;
418 msi_free( mi->disk_prompt );
419 msi_free( mi->cabinet );
420 msi_free( mi->volume_label );
421 mi->disk_prompt = NULL;
422 mi->cabinet = NULL;
423 mi->volume_label = NULL;
425 mi->disk_id++;
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);
432 return -1;
434 package_disk.id = mi->disk_id;
436 TRACE("next cabinet is %s disk id %u\n", debugstr_w(mi->cabinet), mi->disk_id);
437 return 0;
440 static INT_PTR cabinet_copy_file(FDINOTIFICATIONTYPE fdint,
441 PFDINOTIFICATION pfdin)
443 MSICABDATA *data = pfdin->pv;
444 HANDLE handle = 0;
445 LPWSTR path = NULL;
446 DWORD attrs;
448 data->curfile = strdupAtoW(pfdin->psz1);
449 if (!data->cb(data->package, data->curfile, MSICABEXTRACT_BEGINEXTRACT, &path,
450 &attrs, data->user))
452 /* We're not extracting this file, so free the filename. */
453 msi_free(data->curfile);
454 data->curfile = NULL;
455 goto done;
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);
473 goto done;
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;
487 DWORD len;
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;
510 else
512 WARN("failed to schedule rename operation %s (error %d)\n", debugstr_w(path), GetLastError());
513 DeleteFileW( tmpfileW );
515 msi_free(tmpfileW);
517 else
518 WARN("failed to create %s (error %d)\n", debugstr_w(path), err);
521 done:
522 msi_free(path);
524 return (INT_PTR)handle;
527 static INT_PTR cabinet_close_file_info(FDINOTIFICATIONTYPE fdint,
528 PFDINOTIFICATION pfdin)
530 MSICABDATA *data = pfdin->pv;
531 FILETIME ft;
532 FILETIME ftLocal;
533 HANDLE handle = (HANDLE)pfdin->hf;
535 data->mi->is_continuous = FALSE;
537 if (!DosDateTimeToFileTime(pfdin->date, pfdin->time, &ft))
538 return -1;
539 if (!LocalFileTimeToFileTime(&ft, &ftLocal))
540 return -1;
541 if (!SetFileTime(handle, &ftLocal, 0, &ftLocal))
542 return -1;
544 CloseHandle(handle);
546 data->cb(data->package, data->curfile, MSICABEXTRACT_FILEEXTRACTED, NULL, NULL,
547 data->user);
549 msi_free(data->curfile);
550 data->curfile = NULL;
552 return 1;
555 static INT_PTR CDECL cabinet_notify(FDINOTIFICATIONTYPE fdint, PFDINOTIFICATION pfdin)
557 switch (fdint)
559 case fdintPARTIAL_FILE:
560 return cabinet_partial_file(fdint, pfdin);
562 case fdintNEXT_CABINET:
563 return cabinet_next_cabinet(fdint, pfdin);
565 case fdintCOPY_FILE:
566 return cabinet_copy_file(fdint, pfdin);
568 case fdintCLOSE_FILE_INFO:
569 return cabinet_close_file_info(fdint, pfdin);
571 default:
572 return 0;
576 static INT_PTR CDECL cabinet_notify_stream( FDINOTIFICATIONTYPE fdint, PFDINOTIFICATION pfdin )
578 switch (fdint)
580 case fdintPARTIAL_FILE:
581 return cabinet_partial_file( fdint, pfdin );
583 case fdintNEXT_CABINET:
584 return cabinet_next_cabinet_stream( fdint, pfdin );
586 case fdintCOPY_FILE:
587 return cabinet_copy_file( fdint, pfdin );
589 case fdintCLOSE_FILE_INFO:
590 return cabinet_close_file_info( fdint, pfdin );
592 case fdintCABINET_INFO:
593 return 0;
595 default:
596 ERR("Unexpected notification %d\n", fdint);
597 return 0;
601 static BOOL extract_cabinet( MSIPACKAGE* package, MSIMEDIAINFO *mi, LPVOID data )
603 LPSTR cabinet, cab_path = NULL;
604 HFDI hfdi;
605 ERF erf;
606 BOOL ret = FALSE;
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 );
612 if (!hfdi)
614 ERR("FDICreate failed\n");
615 return FALSE;
618 cabinet = strdupWtoA( mi->cabinet );
619 if (!cabinet)
620 goto done;
622 cab_path = strdupWtoA( mi->sourcedir );
623 if (!cab_path)
624 goto done;
626 ret = FDICopy( hfdi, cabinet, cab_path, 0, cabinet_notify, NULL, data );
627 if (!ret)
628 ERR("FDICopy failed\n");
630 done:
631 FDIDestroy( hfdi );
632 msi_free(cabinet );
633 msi_free( cab_path );
635 if (ret)
636 mi->is_extracted = TRUE;
638 return ret;
641 static BOOL extract_cabinet_stream( MSIPACKAGE *package, MSIMEDIAINFO *mi, LPVOID data )
643 static char filename[] = {'<','S','T','R','E','A','M','>',0};
644 HFDI hfdi;
645 ERF erf;
646 BOOL ret = FALSE;
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 );
652 if (!hfdi)
654 ERR("FDICreate failed\n");
655 return FALSE;
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");
664 FDIDestroy( hfdi );
665 if (ret) mi->is_extracted = TRUE;
666 return ret;
669 /***********************************************************************
670 * msi_cabextract
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);
688 msi_free(mi);
691 static UINT get_drive_type(const WCHAR *path)
693 WCHAR root[MAX_PATH + 1];
695 strcpyW(root, path);
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};
708 MSIRECORD *row;
709 LPWSTR source_dir, source;
710 DWORD options;
712 if (Sequence <= mi->last_sequence) /* already loaded */
713 return ERROR_SUCCESS;
715 row = MSI_QueryGetRecord(package->db, query, Sequence);
716 if (!row)
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)
742 source = source_dir;
743 options |= MSISOURCETYPE_MEDIA;
745 else if (package->BaseURL && UrlIsW(package->BaseURL, URLIS_URL))
747 source = package->BaseURL;
748 options |= MSISOURCETYPE_URL;
750 else
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;
776 WCHAR last_type[2];
777 UINT r;
779 size = 2;
780 r = MsiSourceListGetInfoW(package->ProductCode, NULL,
781 package->Context, MSICODE_PRODUCT,
782 INSTALLPROPERTY_LASTUSEDTYPEW, last_type, &size);
783 if (r != ERROR_SUCCESS)
784 return r;
786 size = MAX_PATH;
787 r = MsiSourceListGetInfoW(package->ProductCode, NULL,
788 package->Context, MSICODE_PRODUCT,
789 INSTALLPROPERTY_LASTUSEDSOURCEW, source, &size);
790 if (r != ERROR_SUCCESS)
791 return r;
793 if (last_type[0] == 'n')
795 WCHAR cabinet_file[MAX_PATH];
796 BOOL check_all = FALSE;
798 while(TRUE)
800 index = 0;
801 volumesz = MAX_PATH;
802 while (MsiSourceListEnumSourcesW(package->ProductCode, NULL,
803 package->Context,
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)
815 volumesz = MAX_PATH;
816 if(!check_all)
817 break;
818 continue;
821 lstrcpyW(mi->sourcedir, volume);
822 PathAddBackslashW(mi->sourcedir);
823 TRACE("Found network source %s\n", debugstr_w(mi->sourcedir));
824 return ERROR_SUCCESS;
828 if (!check_all)
829 check_all = TRUE;
830 else
831 break;
835 index = 0;
836 volumesz = MAX_PATH;
837 promptsz = MAX_PATH;
838 while (MsiSourceListEnumMediaDisksW(package->ProductCode, NULL,
839 package->Context,
840 MSICODE_PRODUCT, index++, &id,
841 volume, &volumesz, prompt, &promptsz) == ERROR_SUCCESS)
843 mi->disk_id = id;
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 )
867 UINT rc;
868 WCHAR *cabinet_file = NULL;
870 /* media info for continuous cabinet is already loaded */
871 if (mi->is_continuous) return ERROR_SUCCESS;
873 if (mi->cabinet)
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 );
890 return rc;
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 );
906 msi_free( 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 );
913 return rc;
917 if (mi->cabinet)
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 ) )))
950 msi_free( cab );
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;