dwrite: Implement CreateFontFaceFromHdc().
[wine.git] / dlls / msi / media.c
blobc26ac4114d285ad1b1face8946c19737aea2cfe9
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 ((package->ui_level & INSTALLUILEVEL_MASK) == INSTALLUILEVEL_NONE &&
81 !gUIHandlerA && !gUIHandlerW && !gUIHandlerRecord) return ERROR_SUCCESS;
83 error = msi_build_error_string(package, 1302, 1, mi->disk_prompt);
84 error_dialog = msi_dup_property(package->db, error_prop);
85 source_dir = msi_dup_property(package->db, szSourceDir);
87 while (r == ERROR_SUCCESS && !source_matches_volume(mi, source_dir))
89 r = msi_spawn_error_dialog(package, error_dialog, error);
91 if (gUIHandlerW)
93 gUIHandlerW(gUIContext, MB_RETRYCANCEL | INSTALLMESSAGE_ERROR, error);
95 else if (gUIHandlerA)
97 char *msg = strdupWtoA(error);
98 gUIHandlerA(gUIContext, MB_RETRYCANCEL | INSTALLMESSAGE_ERROR, msg);
99 msi_free(msg);
101 else if (gUIHandlerRecord)
103 MSIHANDLE rec = MsiCreateRecord(1);
104 MsiRecordSetStringW(rec, 0, error);
105 gUIHandlerRecord(gUIContext, MB_RETRYCANCEL | INSTALLMESSAGE_ERROR, rec);
106 MsiCloseHandle(rec);
110 msi_free(error);
111 msi_free(error_dialog);
112 msi_free(source_dir);
114 return r;
117 static MSICABINETSTREAM *msi_get_cabinet_stream( MSIPACKAGE *package, UINT disk_id )
119 MSICABINETSTREAM *cab;
121 LIST_FOR_EACH_ENTRY( cab, &package->cabinet_streams, MSICABINETSTREAM, entry )
123 if (cab->disk_id == disk_id) return cab;
125 return NULL;
128 static void * CDECL cabinet_alloc(ULONG cb)
130 return msi_alloc(cb);
133 static void CDECL cabinet_free(void *pv)
135 msi_free(pv);
138 static INT_PTR CDECL cabinet_open(char *pszFile, int oflag, int pmode)
140 DWORD dwAccess = 0;
141 DWORD dwShareMode = 0;
142 DWORD dwCreateDisposition = OPEN_EXISTING;
144 switch (oflag & _O_ACCMODE)
146 case _O_RDONLY:
147 dwAccess = GENERIC_READ;
148 dwShareMode = FILE_SHARE_READ | FILE_SHARE_DELETE;
149 break;
150 case _O_WRONLY:
151 dwAccess = GENERIC_WRITE;
152 dwShareMode = FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE;
153 break;
154 case _O_RDWR:
155 dwAccess = GENERIC_READ | GENERIC_WRITE;
156 dwShareMode = FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE;
157 break;
160 if ((oflag & (_O_CREAT | _O_EXCL)) == (_O_CREAT | _O_EXCL))
161 dwCreateDisposition = CREATE_NEW;
162 else if (oflag & _O_CREAT)
163 dwCreateDisposition = CREATE_ALWAYS;
165 return (INT_PTR)CreateFileA(pszFile, dwAccess, dwShareMode, NULL,
166 dwCreateDisposition, 0, NULL);
169 static UINT CDECL cabinet_read(INT_PTR hf, void *pv, UINT cb)
171 HANDLE handle = (HANDLE)hf;
172 DWORD read;
174 if (ReadFile(handle, pv, cb, &read, NULL))
175 return read;
177 return 0;
180 static UINT CDECL cabinet_write(INT_PTR hf, void *pv, UINT cb)
182 HANDLE handle = (HANDLE)hf;
183 DWORD written;
185 if (WriteFile(handle, pv, cb, &written, NULL))
186 return written;
188 return 0;
191 static int CDECL cabinet_close(INT_PTR hf)
193 HANDLE handle = (HANDLE)hf;
194 return CloseHandle(handle) ? 0 : -1;
197 static LONG CDECL cabinet_seek(INT_PTR hf, LONG dist, int seektype)
199 HANDLE handle = (HANDLE)hf;
200 /* flags are compatible and so are passed straight through */
201 return SetFilePointer(handle, dist, NULL, seektype);
204 struct package_disk
206 MSIPACKAGE *package;
207 UINT id;
210 static struct package_disk package_disk;
212 static INT_PTR CDECL cabinet_open_stream( char *pszFile, int oflag, int pmode )
214 MSICABINETSTREAM *cab;
215 IStream *stream;
216 WCHAR *encoded;
217 HRESULT hr;
219 cab = msi_get_cabinet_stream( package_disk.package, package_disk.id );
220 if (!cab)
222 WARN("failed to get cabinet stream\n");
223 return -1;
225 if (!cab->stream[0] || !(encoded = encode_streamname( FALSE, cab->stream + 1 )))
227 WARN("failed to encode stream name\n");
228 return -1;
230 if (msi_clone_open_stream( package_disk.package->db, cab->storage, encoded, &stream ) != ERROR_SUCCESS)
232 hr = IStorage_OpenStream( cab->storage, encoded, NULL, STGM_READ|STGM_SHARE_EXCLUSIVE, 0, &stream );
233 if (FAILED(hr))
235 WARN("failed to open stream 0x%08x\n", hr);
236 msi_free( encoded );
237 return -1;
240 msi_free( encoded );
241 return (INT_PTR)stream;
244 static UINT CDECL cabinet_read_stream( INT_PTR hf, void *pv, UINT cb )
246 IStream *stm = (IStream *)hf;
247 DWORD read;
248 HRESULT hr;
250 hr = IStream_Read( stm, pv, cb, &read );
251 if (hr == S_OK || hr == S_FALSE)
252 return read;
254 return 0;
257 static int CDECL cabinet_close_stream( INT_PTR hf )
259 IStream *stm = (IStream *)hf;
260 IStream_Release( stm );
261 return 0;
264 static LONG CDECL cabinet_seek_stream( INT_PTR hf, LONG dist, int seektype )
266 IStream *stm = (IStream *)hf;
267 LARGE_INTEGER move;
268 ULARGE_INTEGER newpos;
269 HRESULT hr;
271 move.QuadPart = dist;
272 hr = IStream_Seek( stm, move, seektype, &newpos );
273 if (SUCCEEDED(hr))
275 if (newpos.QuadPart <= MAXLONG) return newpos.QuadPart;
276 ERR("Too big!\n");
278 return -1;
281 static UINT CDECL msi_media_get_disk_info(MSIPACKAGE *package, MSIMEDIAINFO *mi)
283 MSIRECORD *row;
285 static const WCHAR query[] = {
286 'S','E','L','E','C','T',' ','*',' ', 'F','R','O','M',' ',
287 '`','M','e','d','i','a','`',' ','W','H','E','R','E',' ',
288 '`','D','i','s','k','I','d','`',' ','=',' ','%','i',0};
290 row = MSI_QueryGetRecord(package->db, query, mi->disk_id);
291 if (!row)
293 TRACE("Unable to query row\n");
294 return ERROR_FUNCTION_FAILED;
297 mi->disk_prompt = strdupW(MSI_RecordGetString(row, 3));
298 mi->cabinet = strdupW(MSI_RecordGetString(row, 4));
299 mi->volume_label = strdupW(MSI_RecordGetString(row, 5));
301 msiobj_release(&row->hdr);
302 return ERROR_SUCCESS;
305 static INT_PTR cabinet_partial_file(FDINOTIFICATIONTYPE fdint,
306 PFDINOTIFICATION pfdin)
308 MSICABDATA *data = pfdin->pv;
309 data->mi->is_continuous = FALSE;
310 return 0;
313 static WCHAR *get_cabinet_filename(MSIMEDIAINFO *mi)
315 int len;
316 WCHAR *ret;
318 len = strlenW(mi->sourcedir) + strlenW(mi->cabinet) + 1;
319 if (!(ret = msi_alloc(len * sizeof(WCHAR)))) return NULL;
320 strcpyW(ret, mi->sourcedir);
321 strcatW(ret, mi->cabinet);
322 return ret;
325 static INT_PTR cabinet_next_cabinet(FDINOTIFICATIONTYPE fdint,
326 PFDINOTIFICATION pfdin)
328 MSICABDATA *data = pfdin->pv;
329 MSIMEDIAINFO *mi = data->mi;
330 LPWSTR cabinet_file = NULL, cab = strdupAtoW(pfdin->psz1);
331 INT_PTR res = -1;
332 UINT rc;
334 msi_free(mi->disk_prompt);
335 msi_free(mi->cabinet);
336 msi_free(mi->volume_label);
337 mi->disk_prompt = NULL;
338 mi->cabinet = NULL;
339 mi->volume_label = NULL;
341 mi->disk_id++;
342 mi->is_continuous = TRUE;
344 rc = msi_media_get_disk_info(data->package, mi);
345 if (rc != ERROR_SUCCESS)
347 ERR("Failed to get next cabinet information: %d\n", rc);
348 goto done;
351 if (strcmpiW( mi->cabinet, cab ))
353 char *next_cab;
354 ULONG length;
356 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));
358 /* Use cabinet name from the media table */
359 next_cab = strdupWtoA(mi->cabinet);
360 /* Modify path to cabinet file with full filename (psz3 points to a 256 bytes buffer that can be modified contrary to psz1 and psz2) */
361 length = strlen(pfdin->psz3) + 1 + strlen(next_cab) + 1;
362 if (length > 256)
364 WARN("Cannot update next cabinet filename with a string size %u > 256\n", length);
365 msi_free(next_cab);
366 goto done;
368 else
370 strcat(pfdin->psz3, "\\");
371 strcat(pfdin->psz3, next_cab);
373 /* Path psz3 and cabinet psz1 are concatenated by FDI so just reset psz1 */
374 *pfdin->psz1 = 0;
375 msi_free(next_cab);
378 if (!(cabinet_file = get_cabinet_filename(mi)))
379 goto done;
381 TRACE("Searching for %s\n", debugstr_w(cabinet_file));
383 res = 0;
384 if (GetFileAttributesW(cabinet_file) == INVALID_FILE_ATTRIBUTES)
386 if (msi_change_media(data->package, mi) != ERROR_SUCCESS)
387 res = -1;
390 done:
391 msi_free(cab);
392 msi_free(cabinet_file);
393 return res;
396 static INT_PTR cabinet_next_cabinet_stream( FDINOTIFICATIONTYPE fdint,
397 PFDINOTIFICATION pfdin )
399 MSICABDATA *data = pfdin->pv;
400 MSIMEDIAINFO *mi = data->mi;
401 UINT rc;
403 msi_free( mi->disk_prompt );
404 msi_free( mi->cabinet );
405 msi_free( mi->volume_label );
406 mi->disk_prompt = NULL;
407 mi->cabinet = NULL;
408 mi->volume_label = NULL;
410 mi->disk_id++;
411 mi->is_continuous = TRUE;
413 rc = msi_media_get_disk_info( data->package, mi );
414 if (rc != ERROR_SUCCESS)
416 ERR("Failed to get next cabinet information: %u\n", rc);
417 return -1;
419 package_disk.id = mi->disk_id;
421 TRACE("next cabinet is %s disk id %u\n", debugstr_w(mi->cabinet), mi->disk_id);
422 return 0;
425 static INT_PTR cabinet_copy_file(FDINOTIFICATIONTYPE fdint,
426 PFDINOTIFICATION pfdin)
428 MSICABDATA *data = pfdin->pv;
429 HANDLE handle = 0;
430 LPWSTR path = NULL;
431 DWORD attrs;
433 data->curfile = strdupAtoW(pfdin->psz1);
434 if (!data->cb(data->package, data->curfile, MSICABEXTRACT_BEGINEXTRACT, &path,
435 &attrs, data->user))
437 /* We're not extracting this file, so free the filename. */
438 msi_free(data->curfile);
439 data->curfile = NULL;
440 goto done;
443 TRACE("extracting %s -> %s\n", debugstr_w(data->curfile), debugstr_w(path));
445 attrs = attrs & (FILE_ATTRIBUTE_READONLY|FILE_ATTRIBUTE_HIDDEN|FILE_ATTRIBUTE_SYSTEM);
446 if (!attrs) attrs = FILE_ATTRIBUTE_NORMAL;
448 handle = CreateFileW(path, GENERIC_READ | GENERIC_WRITE, 0,
449 NULL, CREATE_ALWAYS, attrs, NULL);
450 if (handle == INVALID_HANDLE_VALUE)
452 DWORD err = GetLastError();
453 DWORD attrs2 = GetFileAttributesW(path);
455 if (attrs2 == INVALID_FILE_ATTRIBUTES)
457 ERR("failed to create %s (error %d)\n", debugstr_w(path), err);
458 goto done;
460 else if (err == ERROR_ACCESS_DENIED && (attrs2 & FILE_ATTRIBUTE_READONLY))
462 TRACE("removing read-only attribute on %s\n", debugstr_w(path));
463 SetFileAttributesW( path, attrs2 & ~FILE_ATTRIBUTE_READONLY );
464 handle = CreateFileW(path, GENERIC_READ | GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, attrs2, NULL);
466 if (handle != INVALID_HANDLE_VALUE) goto done;
467 err = GetLastError();
469 if (err == ERROR_SHARING_VIOLATION || err == ERROR_USER_MAPPED_FILE)
471 WCHAR *tmpfileW, *tmppathW, *p;
472 DWORD len;
474 TRACE("file in use, scheduling rename operation\n");
476 if (!(tmppathW = strdupW( path ))) return ERROR_OUTOFMEMORY;
477 if ((p = strrchrW(tmppathW, '\\'))) *p = 0;
478 len = strlenW( tmppathW ) + 16;
479 if (!(tmpfileW = msi_alloc(len * sizeof(WCHAR))))
481 msi_free( tmppathW );
482 return ERROR_OUTOFMEMORY;
484 if (!GetTempFileNameW(tmppathW, szMsi, 0, tmpfileW)) tmpfileW[0] = 0;
485 msi_free( tmppathW );
487 handle = CreateFileW(tmpfileW, GENERIC_READ | GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, attrs, NULL);
489 if (handle != INVALID_HANDLE_VALUE &&
490 MoveFileExW(path, NULL, MOVEFILE_DELAY_UNTIL_REBOOT) &&
491 MoveFileExW(tmpfileW, path, MOVEFILE_DELAY_UNTIL_REBOOT))
493 data->package->need_reboot_at_end = 1;
495 else
497 WARN("failed to schedule rename operation %s (error %d)\n", debugstr_w(path), GetLastError());
498 DeleteFileW( tmpfileW );
500 msi_free(tmpfileW);
502 else
503 WARN("failed to create %s (error %d)\n", debugstr_w(path), err);
506 done:
507 msi_free(path);
509 return (INT_PTR)handle;
512 static INT_PTR cabinet_close_file_info(FDINOTIFICATIONTYPE fdint,
513 PFDINOTIFICATION pfdin)
515 MSICABDATA *data = pfdin->pv;
516 FILETIME ft;
517 FILETIME ftLocal;
518 HANDLE handle = (HANDLE)pfdin->hf;
520 data->mi->is_continuous = FALSE;
522 if (!DosDateTimeToFileTime(pfdin->date, pfdin->time, &ft))
523 return -1;
524 if (!LocalFileTimeToFileTime(&ft, &ftLocal))
525 return -1;
526 if (!SetFileTime(handle, &ftLocal, 0, &ftLocal))
527 return -1;
529 CloseHandle(handle);
531 data->cb(data->package, data->curfile, MSICABEXTRACT_FILEEXTRACTED, NULL, NULL,
532 data->user);
534 msi_free(data->curfile);
535 data->curfile = NULL;
537 return 1;
540 static INT_PTR CDECL cabinet_notify(FDINOTIFICATIONTYPE fdint, PFDINOTIFICATION pfdin)
542 switch (fdint)
544 case fdintPARTIAL_FILE:
545 return cabinet_partial_file(fdint, pfdin);
547 case fdintNEXT_CABINET:
548 return cabinet_next_cabinet(fdint, pfdin);
550 case fdintCOPY_FILE:
551 return cabinet_copy_file(fdint, pfdin);
553 case fdintCLOSE_FILE_INFO:
554 return cabinet_close_file_info(fdint, pfdin);
556 default:
557 return 0;
561 static INT_PTR CDECL cabinet_notify_stream( FDINOTIFICATIONTYPE fdint, PFDINOTIFICATION pfdin )
563 switch (fdint)
565 case fdintPARTIAL_FILE:
566 return cabinet_partial_file( fdint, pfdin );
568 case fdintNEXT_CABINET:
569 return cabinet_next_cabinet_stream( fdint, pfdin );
571 case fdintCOPY_FILE:
572 return cabinet_copy_file( fdint, pfdin );
574 case fdintCLOSE_FILE_INFO:
575 return cabinet_close_file_info( fdint, pfdin );
577 case fdintCABINET_INFO:
578 return 0;
580 default:
581 ERR("Unexpected notification %d\n", fdint);
582 return 0;
586 static BOOL extract_cabinet( MSIPACKAGE* package, MSIMEDIAINFO *mi, LPVOID data )
588 LPSTR cabinet, cab_path = NULL;
589 HFDI hfdi;
590 ERF erf;
591 BOOL ret = FALSE;
593 TRACE("extracting %s disk id %u\n", debugstr_w(mi->cabinet), mi->disk_id);
595 hfdi = FDICreate( cabinet_alloc, cabinet_free, cabinet_open, cabinet_read,
596 cabinet_write, cabinet_close, cabinet_seek, 0, &erf );
597 if (!hfdi)
599 ERR("FDICreate failed\n");
600 return FALSE;
603 cabinet = strdupWtoA( mi->cabinet );
604 if (!cabinet)
605 goto done;
607 cab_path = strdupWtoA( mi->sourcedir );
608 if (!cab_path)
609 goto done;
611 ret = FDICopy( hfdi, cabinet, cab_path, 0, cabinet_notify, NULL, data );
612 if (!ret)
613 ERR("FDICopy failed\n");
615 done:
616 FDIDestroy( hfdi );
617 msi_free(cabinet );
618 msi_free( cab_path );
620 if (ret)
621 mi->is_extracted = TRUE;
623 return ret;
626 static BOOL extract_cabinet_stream( MSIPACKAGE *package, MSIMEDIAINFO *mi, LPVOID data )
628 static char filename[] = {'<','S','T','R','E','A','M','>',0};
629 HFDI hfdi;
630 ERF erf;
631 BOOL ret = FALSE;
633 TRACE("extracting %s disk id %u\n", debugstr_w(mi->cabinet), mi->disk_id);
635 hfdi = FDICreate( cabinet_alloc, cabinet_free, cabinet_open_stream, cabinet_read_stream,
636 cabinet_write, cabinet_close_stream, cabinet_seek_stream, 0, &erf );
637 if (!hfdi)
639 ERR("FDICreate failed\n");
640 return FALSE;
643 package_disk.package = package;
644 package_disk.id = mi->disk_id;
646 ret = FDICopy( hfdi, filename, NULL, 0, cabinet_notify_stream, NULL, data );
647 if (!ret) ERR("FDICopy failed\n");
649 FDIDestroy( hfdi );
650 if (ret) mi->is_extracted = TRUE;
651 return ret;
654 /***********************************************************************
655 * msi_cabextract
657 * Extract files from a cabinet file or stream.
659 BOOL msi_cabextract(MSIPACKAGE* package, MSIMEDIAINFO *mi, LPVOID data)
661 if (mi->cabinet[0] == '#')
663 return extract_cabinet_stream( package, mi, data );
665 return extract_cabinet( package, mi, data );
668 void msi_free_media_info(MSIMEDIAINFO *mi)
670 msi_free(mi->disk_prompt);
671 msi_free(mi->cabinet);
672 msi_free(mi->volume_label);
673 msi_free(mi);
676 static UINT get_drive_type(const WCHAR *path)
678 WCHAR root[MAX_PATH + 1];
680 strcpyW(root, path);
681 PathStripToRootW(root);
682 PathAddBackslashW(root);
684 return GetDriveTypeW(root);
687 UINT msi_load_media_info(MSIPACKAGE *package, UINT Sequence, MSIMEDIAINFO *mi)
689 static const WCHAR query[] = {
690 'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ','`','M','e','d','i','a','`',' ',
691 'W','H','E','R','E',' ','`','L','a','s','t','S','e','q','u','e','n','c','e','`',' ',
692 '>','=',' ','%','i',' ','O','R','D','E','R',' ','B','Y',' ','`','D','i','s','k','I','d','`',0};
693 MSIRECORD *row;
694 LPWSTR source_dir, source;
695 DWORD options;
697 if (Sequence <= mi->last_sequence) /* already loaded */
698 return ERROR_SUCCESS;
700 row = MSI_QueryGetRecord(package->db, query, Sequence);
701 if (!row)
703 TRACE("Unable to query row\n");
704 return ERROR_FUNCTION_FAILED;
707 mi->is_extracted = FALSE;
708 mi->disk_id = MSI_RecordGetInteger(row, 1);
709 mi->last_sequence = MSI_RecordGetInteger(row, 2);
710 msi_free(mi->disk_prompt);
711 mi->disk_prompt = strdupW(MSI_RecordGetString(row, 3));
712 msi_free(mi->cabinet);
713 mi->cabinet = strdupW(MSI_RecordGetString(row, 4));
714 msi_free(mi->volume_label);
715 mi->volume_label = strdupW(MSI_RecordGetString(row, 5));
716 msiobj_release(&row->hdr);
718 msi_set_sourcedir_props(package, FALSE);
719 source_dir = msi_dup_property(package->db, szSourceDir);
720 lstrcpyW(mi->sourcedir, source_dir);
721 PathAddBackslashW(mi->sourcedir);
722 mi->type = get_drive_type(source_dir);
724 options = MSICODE_PRODUCT;
725 if (mi->type == DRIVE_CDROM || mi->type == DRIVE_REMOVABLE)
727 source = source_dir;
728 options |= MSISOURCETYPE_MEDIA;
730 else if (package->BaseURL && UrlIsW(package->BaseURL, URLIS_URL))
732 source = package->BaseURL;
733 options |= MSISOURCETYPE_URL;
735 else
737 source = mi->sourcedir;
738 options |= MSISOURCETYPE_NETWORK;
741 msi_package_add_media_disk(package, package->Context,
742 MSICODE_PRODUCT, mi->disk_id,
743 mi->volume_label, mi->disk_prompt);
745 msi_package_add_info(package, package->Context,
746 options, INSTALLPROPERTY_LASTUSEDSOURCEW, source);
748 msi_free(source_dir);
749 TRACE("sequence %u -> cabinet %s disk id %u\n", Sequence, debugstr_w(mi->cabinet), mi->disk_id);
750 return ERROR_SUCCESS;
753 /* FIXME: search URL sources as well */
754 static UINT find_published_source(MSIPACKAGE *package, MSIMEDIAINFO *mi)
756 WCHAR source[MAX_PATH];
757 WCHAR volume[MAX_PATH];
758 WCHAR prompt[MAX_PATH];
759 DWORD volumesz, promptsz;
760 DWORD index, size, id;
761 WCHAR last_type[2];
762 UINT r;
764 size = 2;
765 r = MsiSourceListGetInfoW(package->ProductCode, NULL,
766 package->Context, MSICODE_PRODUCT,
767 INSTALLPROPERTY_LASTUSEDTYPEW, last_type, &size);
768 if (r != ERROR_SUCCESS)
769 return r;
771 size = MAX_PATH;
772 r = MsiSourceListGetInfoW(package->ProductCode, NULL,
773 package->Context, MSICODE_PRODUCT,
774 INSTALLPROPERTY_LASTUSEDSOURCEW, source, &size);
775 if (r != ERROR_SUCCESS)
776 return r;
778 if (last_type[0] == 'n')
780 WCHAR cabinet_file[MAX_PATH];
781 BOOL check_all = FALSE;
783 while(TRUE)
785 index = 0;
786 volumesz = MAX_PATH;
787 while (MsiSourceListEnumSourcesW(package->ProductCode, NULL,
788 package->Context,
789 MSISOURCETYPE_NETWORK, index++,
790 volume, &volumesz) == ERROR_SUCCESS)
792 if (check_all || !strncmpiW(source, volume, strlenW(source)))
794 lstrcpyW(cabinet_file, volume);
795 PathAddBackslashW(cabinet_file);
796 lstrcatW(cabinet_file, mi->cabinet);
798 if (GetFileAttributesW(cabinet_file) == INVALID_FILE_ATTRIBUTES)
800 volumesz = MAX_PATH;
801 if(!check_all)
802 break;
803 continue;
806 lstrcpyW(mi->sourcedir, volume);
807 PathAddBackslashW(mi->sourcedir);
808 TRACE("Found network source %s\n", debugstr_w(mi->sourcedir));
809 return ERROR_SUCCESS;
813 if (!check_all)
814 check_all = TRUE;
815 else
816 break;
820 index = 0;
821 volumesz = MAX_PATH;
822 promptsz = MAX_PATH;
823 while (MsiSourceListEnumMediaDisksW(package->ProductCode, NULL,
824 package->Context,
825 MSICODE_PRODUCT, index++, &id,
826 volume, &volumesz, prompt, &promptsz) == ERROR_SUCCESS)
828 mi->disk_id = id;
829 msi_free( mi->volume_label );
830 if (!(mi->volume_label = msi_alloc( ++volumesz * sizeof(WCHAR) ))) return ERROR_OUTOFMEMORY;
831 strcpyW( mi->volume_label, volume );
833 msi_free( mi->disk_prompt );
834 if (!(mi->disk_prompt = msi_alloc( ++promptsz * sizeof(WCHAR) ))) return ERROR_OUTOFMEMORY;
835 strcpyW( mi->disk_prompt, prompt );
837 if (source_matches_volume(mi, source))
839 /* FIXME: what about SourceDir */
840 lstrcpyW(mi->sourcedir, source);
841 PathAddBackslashW(mi->sourcedir);
842 TRACE("Found disk source %s\n", debugstr_w(mi->sourcedir));
843 return ERROR_SUCCESS;
847 return ERROR_FUNCTION_FAILED;
850 UINT ready_media( MSIPACKAGE *package, BOOL compressed, MSIMEDIAINFO *mi )
852 UINT rc;
853 WCHAR *cabinet_file = NULL;
855 /* media info for continuous cabinet is already loaded */
856 if (mi->is_continuous) return ERROR_SUCCESS;
858 if (mi->cabinet)
860 /* cabinet is internal, no checks needed */
861 if (mi->cabinet[0] == '#') return ERROR_SUCCESS;
863 if (!(cabinet_file = get_cabinet_filename( mi ))) return ERROR_OUTOFMEMORY;
865 /* package should be downloaded */
866 if (compressed && GetFileAttributesW( cabinet_file ) == INVALID_FILE_ATTRIBUTES &&
867 package->BaseURL && UrlIsW( package->BaseURL, URLIS_URL ))
869 WCHAR temppath[MAX_PATH], *p;
871 if ((rc = msi_download_file( cabinet_file, temppath )) != ERROR_SUCCESS)
873 ERR("failed to download %s (%u)\n", debugstr_w(cabinet_file), rc);
874 msi_free( cabinet_file );
875 return rc;
877 if ((p = strrchrW( temppath, '\\' ))) *p = 0;
878 strcpyW( mi->sourcedir, temppath );
879 PathAddBackslashW( mi->sourcedir );
880 msi_free( mi->cabinet );
881 mi->cabinet = strdupW( p + 1 );
882 msi_free( cabinet_file );
883 return ERROR_SUCCESS;
886 /* check volume matches, change media if not */
887 if (mi->volume_label && mi->disk_id > 1)
889 WCHAR *source = msi_dup_property( package->db, szSourceDir );
890 BOOL match = source_matches_volume( mi, source );
891 msi_free( source );
893 if (!match && (mi->type == DRIVE_CDROM || mi->type == DRIVE_REMOVABLE))
895 if ((rc = msi_change_media( package, mi )) != ERROR_SUCCESS)
897 msi_free( cabinet_file );
898 return rc;
902 if (mi->cabinet)
904 if (compressed && GetFileAttributesW( cabinet_file ) == INVALID_FILE_ATTRIBUTES)
906 if ((rc = find_published_source( package, mi )) != ERROR_SUCCESS)
908 ERR("cabinet not found: %s\n", debugstr_w(cabinet_file));
909 msi_free( cabinet_file );
910 return ERROR_INSTALL_FAILURE;
914 msi_free( cabinet_file );
915 return ERROR_SUCCESS;
918 UINT msi_add_cabinet_stream( MSIPACKAGE *package, UINT disk_id, IStorage *storage, const WCHAR *name )
920 MSICABINETSTREAM *cab, *item;
922 TRACE("%p, %u, %p, %s\n", package, disk_id, storage, debugstr_w(name));
924 LIST_FOR_EACH_ENTRY( item, &package->cabinet_streams, MSICABINETSTREAM, entry )
926 if (item->disk_id == disk_id)
928 TRACE("duplicate disk id %u\n", disk_id);
929 return ERROR_FUNCTION_FAILED;
932 if (!(cab = msi_alloc( sizeof(*cab) ))) return ERROR_OUTOFMEMORY;
933 if (!(cab->stream = msi_alloc( (strlenW( name ) + 1) * sizeof(WCHAR ) )))
935 msi_free( cab );
936 return ERROR_OUTOFMEMORY;
938 strcpyW( cab->stream, name );
939 cab->disk_id = disk_id;
940 cab->storage = storage;
941 IStorage_AddRef( storage );
942 list_add_tail( &package->cabinet_streams, &cab->entry );
944 return ERROR_SUCCESS;