wined3d: Use a shader for unconverted blits.
[wine.git] / dlls / msi / media.c
blob3dbd1b7374415efd59ebbf07f57eb7841d9f3db6
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 hr = IStorage_OpenStream( cab->storage, encoded, NULL, STGM_READ|STGM_SHARE_EXCLUSIVE, 0, &stream );
231 if (FAILED(hr))
233 WARN("failed to open stream 0x%08x\n", hr);
234 msi_free( encoded );
235 return -1;
237 msi_free( encoded );
238 return (INT_PTR)stream;
241 static UINT CDECL cabinet_read_stream( INT_PTR hf, void *pv, UINT cb )
243 IStream *stm = (IStream *)hf;
244 DWORD read;
245 HRESULT hr;
247 hr = IStream_Read( stm, pv, cb, &read );
248 if (hr == S_OK || hr == S_FALSE)
249 return read;
251 return 0;
254 static int CDECL cabinet_close_stream( INT_PTR hf )
256 IStream *stm = (IStream *)hf;
257 IStream_Release( stm );
258 return 0;
261 static LONG CDECL cabinet_seek_stream( INT_PTR hf, LONG dist, int seektype )
263 IStream *stm = (IStream *)hf;
264 LARGE_INTEGER move;
265 ULARGE_INTEGER newpos;
266 HRESULT hr;
268 move.QuadPart = dist;
269 hr = IStream_Seek( stm, move, seektype, &newpos );
270 if (SUCCEEDED(hr))
272 if (newpos.QuadPart <= MAXLONG) return newpos.QuadPart;
273 ERR("Too big!\n");
275 return -1;
278 static UINT CDECL msi_media_get_disk_info(MSIPACKAGE *package, MSIMEDIAINFO *mi)
280 MSIRECORD *row;
282 static const WCHAR query[] = {
283 'S','E','L','E','C','T',' ','*',' ', 'F','R','O','M',' ',
284 '`','M','e','d','i','a','`',' ','W','H','E','R','E',' ',
285 '`','D','i','s','k','I','d','`',' ','=',' ','%','i',0};
287 row = MSI_QueryGetRecord(package->db, query, mi->disk_id);
288 if (!row)
290 TRACE("Unable to query row\n");
291 return ERROR_FUNCTION_FAILED;
294 mi->disk_prompt = strdupW(MSI_RecordGetString(row, 3));
295 mi->cabinet = strdupW(MSI_RecordGetString(row, 4));
296 mi->volume_label = strdupW(MSI_RecordGetString(row, 5));
298 msiobj_release(&row->hdr);
299 return ERROR_SUCCESS;
302 static INT_PTR cabinet_partial_file(FDINOTIFICATIONTYPE fdint,
303 PFDINOTIFICATION pfdin)
305 MSICABDATA *data = pfdin->pv;
306 data->mi->is_continuous = FALSE;
307 return 0;
310 static WCHAR *get_cabinet_filename(MSIMEDIAINFO *mi)
312 int len;
313 WCHAR *ret;
315 len = strlenW(mi->sourcedir) + strlenW(mi->cabinet) + 1;
316 if (!(ret = msi_alloc(len * sizeof(WCHAR)))) return NULL;
317 strcpyW(ret, mi->sourcedir);
318 strcatW(ret, mi->cabinet);
319 return ret;
322 static INT_PTR cabinet_next_cabinet(FDINOTIFICATIONTYPE fdint,
323 PFDINOTIFICATION pfdin)
325 MSICABDATA *data = pfdin->pv;
326 MSIMEDIAINFO *mi = data->mi;
327 LPWSTR cabinet_file = NULL, cab = strdupAtoW(pfdin->psz1);
328 INT_PTR res = -1;
329 UINT rc;
331 msi_free(mi->disk_prompt);
332 msi_free(mi->cabinet);
333 msi_free(mi->volume_label);
334 mi->disk_prompt = NULL;
335 mi->cabinet = NULL;
336 mi->volume_label = NULL;
338 mi->disk_id++;
339 mi->is_continuous = TRUE;
341 rc = msi_media_get_disk_info(data->package, mi);
342 if (rc != ERROR_SUCCESS)
344 ERR("Failed to get next cabinet information: %d\n", rc);
345 goto done;
348 if (strcmpiW( mi->cabinet, cab ))
350 char *next_cab;
351 ULONG length;
353 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));
355 /* Use cabinet name from the media table */
356 next_cab = strdupWtoA(mi->cabinet);
357 /* Modify path to cabinet file with full filename (psz3 points to a 256 bytes buffer that can be modified contrary to psz1 and psz2) */
358 length = strlen(pfdin->psz3) + 1 + strlen(next_cab) + 1;
359 if (length > 256)
361 WARN("Cannot update next cabinet filename with a string size %u > 256\n", length);
362 msi_free(next_cab);
363 goto done;
365 else
367 strcat(pfdin->psz3, "\\");
368 strcat(pfdin->psz3, next_cab);
370 /* Path psz3 and cabinet psz1 are concatenated by FDI so just reset psz1 */
371 *pfdin->psz1 = 0;
372 msi_free(next_cab);
375 if (!(cabinet_file = get_cabinet_filename(mi)))
376 goto done;
378 TRACE("Searching for %s\n", debugstr_w(cabinet_file));
380 res = 0;
381 if (GetFileAttributesW(cabinet_file) == INVALID_FILE_ATTRIBUTES)
383 if (msi_change_media(data->package, mi) != ERROR_SUCCESS)
384 res = -1;
387 done:
388 msi_free(cab);
389 msi_free(cabinet_file);
390 return res;
393 static INT_PTR cabinet_next_cabinet_stream( FDINOTIFICATIONTYPE fdint,
394 PFDINOTIFICATION pfdin )
396 MSICABDATA *data = pfdin->pv;
397 MSIMEDIAINFO *mi = data->mi;
398 UINT rc;
400 msi_free( mi->disk_prompt );
401 msi_free( mi->cabinet );
402 msi_free( mi->volume_label );
403 mi->disk_prompt = NULL;
404 mi->cabinet = NULL;
405 mi->volume_label = NULL;
407 mi->disk_id++;
408 mi->is_continuous = TRUE;
410 rc = msi_media_get_disk_info( data->package, mi );
411 if (rc != ERROR_SUCCESS)
413 ERR("Failed to get next cabinet information: %u\n", rc);
414 return -1;
416 package_disk.id = mi->disk_id;
418 TRACE("next cabinet is %s disk id %u\n", debugstr_w(mi->cabinet), mi->disk_id);
419 return 0;
422 static INT_PTR cabinet_copy_file(FDINOTIFICATIONTYPE fdint,
423 PFDINOTIFICATION pfdin)
425 MSICABDATA *data = pfdin->pv;
426 HANDLE handle = 0;
427 LPWSTR path = NULL;
428 DWORD attrs;
430 data->curfile = strdupAtoW(pfdin->psz1);
431 if (!data->cb(data->package, data->curfile, MSICABEXTRACT_BEGINEXTRACT, &path,
432 &attrs, data->user))
434 /* We're not extracting this file, so free the filename. */
435 msi_free(data->curfile);
436 data->curfile = NULL;
437 goto done;
440 TRACE("extracting %s -> %s\n", debugstr_w(data->curfile), debugstr_w(path));
442 attrs = attrs & (FILE_ATTRIBUTE_READONLY|FILE_ATTRIBUTE_HIDDEN|FILE_ATTRIBUTE_SYSTEM);
443 if (!attrs) attrs = FILE_ATTRIBUTE_NORMAL;
445 handle = CreateFileW(path, GENERIC_READ | GENERIC_WRITE, 0,
446 NULL, CREATE_ALWAYS, attrs, NULL);
447 if (handle == INVALID_HANDLE_VALUE)
449 DWORD err = GetLastError();
450 DWORD attrs2 = GetFileAttributesW(path);
452 if (attrs2 == INVALID_FILE_ATTRIBUTES)
454 ERR("failed to create %s (error %d)\n", debugstr_w(path), err);
455 goto done;
457 else if (err == ERROR_ACCESS_DENIED && (attrs2 & FILE_ATTRIBUTE_READONLY))
459 TRACE("removing read-only attribute on %s\n", debugstr_w(path));
460 SetFileAttributesW( path, attrs2 & ~FILE_ATTRIBUTE_READONLY );
461 handle = CreateFileW(path, GENERIC_READ | GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, attrs2, NULL);
463 if (handle != INVALID_HANDLE_VALUE) goto done;
464 err = GetLastError();
466 if (err == ERROR_SHARING_VIOLATION || err == ERROR_USER_MAPPED_FILE)
468 WCHAR *tmpfileW, *tmppathW, *p;
469 DWORD len;
471 TRACE("file in use, scheduling rename operation\n");
473 if (!(tmppathW = strdupW( path ))) return ERROR_OUTOFMEMORY;
474 if ((p = strrchrW(tmppathW, '\\'))) *p = 0;
475 len = strlenW( tmppathW ) + 16;
476 if (!(tmpfileW = msi_alloc(len * sizeof(WCHAR))))
478 msi_free( tmppathW );
479 return ERROR_OUTOFMEMORY;
481 if (!GetTempFileNameW(tmppathW, szMsi, 0, tmpfileW)) tmpfileW[0] = 0;
482 msi_free( tmppathW );
484 handle = CreateFileW(tmpfileW, GENERIC_READ | GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, attrs, NULL);
486 if (handle != INVALID_HANDLE_VALUE &&
487 MoveFileExW(path, NULL, MOVEFILE_DELAY_UNTIL_REBOOT) &&
488 MoveFileExW(tmpfileW, path, MOVEFILE_DELAY_UNTIL_REBOOT))
490 data->package->need_reboot_at_end = 1;
492 else
494 WARN("failed to schedule rename operation %s (error %d)\n", debugstr_w(path), GetLastError());
495 DeleteFileW( tmpfileW );
497 msi_free(tmpfileW);
499 else
500 WARN("failed to create %s (error %d)\n", debugstr_w(path), err);
503 done:
504 msi_free(path);
506 return (INT_PTR)handle;
509 static INT_PTR cabinet_close_file_info(FDINOTIFICATIONTYPE fdint,
510 PFDINOTIFICATION pfdin)
512 MSICABDATA *data = pfdin->pv;
513 FILETIME ft;
514 FILETIME ftLocal;
515 HANDLE handle = (HANDLE)pfdin->hf;
517 data->mi->is_continuous = FALSE;
519 if (!DosDateTimeToFileTime(pfdin->date, pfdin->time, &ft))
520 return -1;
521 if (!LocalFileTimeToFileTime(&ft, &ftLocal))
522 return -1;
523 if (!SetFileTime(handle, &ftLocal, 0, &ftLocal))
524 return -1;
526 CloseHandle(handle);
528 data->cb(data->package, data->curfile, MSICABEXTRACT_FILEEXTRACTED, NULL, NULL,
529 data->user);
531 msi_free(data->curfile);
532 data->curfile = NULL;
534 return 1;
537 static INT_PTR CDECL cabinet_notify(FDINOTIFICATIONTYPE fdint, PFDINOTIFICATION pfdin)
539 switch (fdint)
541 case fdintPARTIAL_FILE:
542 return cabinet_partial_file(fdint, pfdin);
544 case fdintNEXT_CABINET:
545 return cabinet_next_cabinet(fdint, pfdin);
547 case fdintCOPY_FILE:
548 return cabinet_copy_file(fdint, pfdin);
550 case fdintCLOSE_FILE_INFO:
551 return cabinet_close_file_info(fdint, pfdin);
553 default:
554 return 0;
558 static INT_PTR CDECL cabinet_notify_stream( FDINOTIFICATIONTYPE fdint, PFDINOTIFICATION pfdin )
560 switch (fdint)
562 case fdintPARTIAL_FILE:
563 return cabinet_partial_file( fdint, pfdin );
565 case fdintNEXT_CABINET:
566 return cabinet_next_cabinet_stream( fdint, pfdin );
568 case fdintCOPY_FILE:
569 return cabinet_copy_file( fdint, pfdin );
571 case fdintCLOSE_FILE_INFO:
572 return cabinet_close_file_info( fdint, pfdin );
574 case fdintCABINET_INFO:
575 return 0;
577 default:
578 ERR("Unexpected notification %d\n", fdint);
579 return 0;
583 static BOOL extract_cabinet( MSIPACKAGE* package, MSIMEDIAINFO *mi, LPVOID data )
585 LPSTR cabinet, cab_path = NULL;
586 HFDI hfdi;
587 ERF erf;
588 BOOL ret = FALSE;
590 TRACE("extracting %s disk id %u\n", debugstr_w(mi->cabinet), mi->disk_id);
592 hfdi = FDICreate( cabinet_alloc, cabinet_free, cabinet_open, cabinet_read,
593 cabinet_write, cabinet_close, cabinet_seek, 0, &erf );
594 if (!hfdi)
596 ERR("FDICreate failed\n");
597 return FALSE;
600 cabinet = strdupWtoA( mi->cabinet );
601 if (!cabinet)
602 goto done;
604 cab_path = strdupWtoA( mi->sourcedir );
605 if (!cab_path)
606 goto done;
608 ret = FDICopy( hfdi, cabinet, cab_path, 0, cabinet_notify, NULL, data );
609 if (!ret)
610 ERR("FDICopy failed\n");
612 done:
613 FDIDestroy( hfdi );
614 msi_free(cabinet );
615 msi_free( cab_path );
617 if (ret)
618 mi->is_extracted = TRUE;
620 return ret;
623 static BOOL extract_cabinet_stream( MSIPACKAGE *package, MSIMEDIAINFO *mi, LPVOID data )
625 static char filename[] = {'<','S','T','R','E','A','M','>',0};
626 HFDI hfdi;
627 ERF erf;
628 BOOL ret = FALSE;
630 TRACE("extracting %s disk id %u\n", debugstr_w(mi->cabinet), mi->disk_id);
632 hfdi = FDICreate( cabinet_alloc, cabinet_free, cabinet_open_stream, cabinet_read_stream,
633 cabinet_write, cabinet_close_stream, cabinet_seek_stream, 0, &erf );
634 if (!hfdi)
636 ERR("FDICreate failed\n");
637 return FALSE;
640 package_disk.package = package;
641 package_disk.id = mi->disk_id;
643 ret = FDICopy( hfdi, filename, NULL, 0, cabinet_notify_stream, NULL, data );
644 if (!ret) ERR("FDICopy failed\n");
646 FDIDestroy( hfdi );
647 if (ret) mi->is_extracted = TRUE;
648 return ret;
651 /***********************************************************************
652 * msi_cabextract
654 * Extract files from a cabinet file or stream.
656 BOOL msi_cabextract(MSIPACKAGE* package, MSIMEDIAINFO *mi, LPVOID data)
658 if (mi->cabinet[0] == '#')
660 return extract_cabinet_stream( package, mi, data );
662 return extract_cabinet( package, mi, data );
665 void msi_free_media_info(MSIMEDIAINFO *mi)
667 msi_free(mi->disk_prompt);
668 msi_free(mi->cabinet);
669 msi_free(mi->volume_label);
670 msi_free(mi);
673 static UINT get_drive_type(const WCHAR *path)
675 WCHAR root[MAX_PATH + 1];
677 strcpyW(root, path);
678 PathStripToRootW(root);
679 PathAddBackslashW(root);
681 return GetDriveTypeW(root);
684 UINT msi_load_media_info(MSIPACKAGE *package, UINT Sequence, MSIMEDIAINFO *mi)
686 static const WCHAR query[] = {
687 'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ','`','M','e','d','i','a','`',' ',
688 'W','H','E','R','E',' ','`','L','a','s','t','S','e','q','u','e','n','c','e','`',' ',
689 '>','=',' ','%','i',' ','O','R','D','E','R',' ','B','Y',' ','`','D','i','s','k','I','d','`',0};
690 MSIRECORD *row;
691 LPWSTR source_dir, source;
692 DWORD options;
694 if (Sequence <= mi->last_sequence) /* already loaded */
695 return ERROR_SUCCESS;
697 row = MSI_QueryGetRecord(package->db, query, Sequence);
698 if (!row)
700 TRACE("Unable to query row\n");
701 return ERROR_FUNCTION_FAILED;
704 mi->is_extracted = FALSE;
705 mi->disk_id = MSI_RecordGetInteger(row, 1);
706 mi->last_sequence = MSI_RecordGetInteger(row, 2);
707 msi_free(mi->disk_prompt);
708 mi->disk_prompt = strdupW(MSI_RecordGetString(row, 3));
709 msi_free(mi->cabinet);
710 mi->cabinet = strdupW(MSI_RecordGetString(row, 4));
711 msi_free(mi->volume_label);
712 mi->volume_label = strdupW(MSI_RecordGetString(row, 5));
713 msiobj_release(&row->hdr);
715 msi_set_sourcedir_props(package, FALSE);
716 source_dir = msi_dup_property(package->db, szSourceDir);
717 lstrcpyW(mi->sourcedir, source_dir);
718 PathAddBackslashW(mi->sourcedir);
719 mi->type = get_drive_type(source_dir);
721 options = MSICODE_PRODUCT;
722 if (mi->type == DRIVE_CDROM || mi->type == DRIVE_REMOVABLE)
724 source = source_dir;
725 options |= MSISOURCETYPE_MEDIA;
727 else if (package->BaseURL && UrlIsW(package->BaseURL, URLIS_URL))
729 source = package->BaseURL;
730 options |= MSISOURCETYPE_URL;
732 else
734 source = mi->sourcedir;
735 options |= MSISOURCETYPE_NETWORK;
738 msi_package_add_media_disk(package, package->Context,
739 MSICODE_PRODUCT, mi->disk_id,
740 mi->volume_label, mi->disk_prompt);
742 msi_package_add_info(package, package->Context,
743 options, INSTALLPROPERTY_LASTUSEDSOURCEW, source);
745 msi_free(source_dir);
746 TRACE("sequence %u -> cabinet %s disk id %u\n", Sequence, debugstr_w(mi->cabinet), mi->disk_id);
747 return ERROR_SUCCESS;
750 /* FIXME: search URL sources as well */
751 static UINT find_published_source(MSIPACKAGE *package, MSIMEDIAINFO *mi)
753 WCHAR source[MAX_PATH];
754 WCHAR volume[MAX_PATH];
755 WCHAR prompt[MAX_PATH];
756 DWORD volumesz, promptsz;
757 DWORD index, size, id;
758 WCHAR last_type[2];
759 UINT r;
761 size = 2;
762 r = MsiSourceListGetInfoW(package->ProductCode, NULL,
763 package->Context, MSICODE_PRODUCT,
764 INSTALLPROPERTY_LASTUSEDTYPEW, last_type, &size);
765 if (r != ERROR_SUCCESS)
766 return r;
768 size = MAX_PATH;
769 r = MsiSourceListGetInfoW(package->ProductCode, NULL,
770 package->Context, MSICODE_PRODUCT,
771 INSTALLPROPERTY_LASTUSEDSOURCEW, source, &size);
772 if (r != ERROR_SUCCESS)
773 return r;
775 if (last_type[0] == 'n')
777 WCHAR cabinet_file[MAX_PATH];
778 BOOL check_all = FALSE;
780 while(TRUE)
782 index = 0;
783 volumesz = MAX_PATH;
784 while (MsiSourceListEnumSourcesW(package->ProductCode, NULL,
785 package->Context,
786 MSISOURCETYPE_NETWORK, index++,
787 volume, &volumesz) == ERROR_SUCCESS)
789 if (check_all || !strncmpiW(source, volume, strlenW(source)))
791 lstrcpyW(cabinet_file, volume);
792 PathAddBackslashW(cabinet_file);
793 lstrcatW(cabinet_file, mi->cabinet);
795 if (GetFileAttributesW(cabinet_file) == INVALID_FILE_ATTRIBUTES)
797 volumesz = MAX_PATH;
798 if(!check_all)
799 break;
800 continue;
803 lstrcpyW(mi->sourcedir, volume);
804 PathAddBackslashW(mi->sourcedir);
805 TRACE("Found network source %s\n", debugstr_w(mi->sourcedir));
806 return ERROR_SUCCESS;
810 if (!check_all)
811 check_all = TRUE;
812 else
813 break;
817 index = 0;
818 volumesz = MAX_PATH;
819 promptsz = MAX_PATH;
820 while (MsiSourceListEnumMediaDisksW(package->ProductCode, NULL,
821 package->Context,
822 MSICODE_PRODUCT, index++, &id,
823 volume, &volumesz, prompt, &promptsz) == ERROR_SUCCESS)
825 mi->disk_id = id;
826 msi_free( mi->volume_label );
827 if (!(mi->volume_label = msi_alloc( ++volumesz * sizeof(WCHAR) ))) return ERROR_OUTOFMEMORY;
828 strcpyW( mi->volume_label, volume );
830 msi_free( mi->disk_prompt );
831 if (!(mi->disk_prompt = msi_alloc( ++promptsz * sizeof(WCHAR) ))) return ERROR_OUTOFMEMORY;
832 strcpyW( mi->disk_prompt, prompt );
834 if (source_matches_volume(mi, source))
836 /* FIXME: what about SourceDir */
837 lstrcpyW(mi->sourcedir, source);
838 PathAddBackslashW(mi->sourcedir);
839 TRACE("Found disk source %s\n", debugstr_w(mi->sourcedir));
840 return ERROR_SUCCESS;
844 return ERROR_FUNCTION_FAILED;
847 UINT ready_media( MSIPACKAGE *package, BOOL compressed, MSIMEDIAINFO *mi )
849 UINT rc;
850 WCHAR *cabinet_file = NULL;
852 /* media info for continuous cabinet is already loaded */
853 if (mi->is_continuous) return ERROR_SUCCESS;
855 if (mi->cabinet)
857 /* cabinet is internal, no checks needed */
858 if (mi->cabinet[0] == '#') return ERROR_SUCCESS;
860 if (!(cabinet_file = get_cabinet_filename( mi ))) return ERROR_OUTOFMEMORY;
862 /* package should be downloaded */
863 if (compressed && GetFileAttributesW( cabinet_file ) == INVALID_FILE_ATTRIBUTES &&
864 package->BaseURL && UrlIsW( package->BaseURL, URLIS_URL ))
866 WCHAR temppath[MAX_PATH], *p;
868 if ((rc = msi_download_file( cabinet_file, temppath )) != ERROR_SUCCESS)
870 ERR("failed to download %s (%u)\n", debugstr_w(cabinet_file), rc);
871 msi_free( cabinet_file );
872 return rc;
874 if ((p = strrchrW( temppath, '\\' ))) *p = 0;
875 strcpyW( mi->sourcedir, temppath );
876 PathAddBackslashW( mi->sourcedir );
877 msi_free( mi->cabinet );
878 mi->cabinet = strdupW( p + 1 );
879 msi_free( cabinet_file );
880 return ERROR_SUCCESS;
883 /* check volume matches, change media if not */
884 if (mi->volume_label && mi->disk_id > 1)
886 WCHAR *source = msi_dup_property( package->db, szSourceDir );
887 BOOL match = source_matches_volume( mi, source );
888 msi_free( source );
890 if (!match && (mi->type == DRIVE_CDROM || mi->type == DRIVE_REMOVABLE))
892 if ((rc = msi_change_media( package, mi )) != ERROR_SUCCESS)
894 msi_free( cabinet_file );
895 return rc;
899 if (mi->cabinet)
901 if (compressed && GetFileAttributesW( cabinet_file ) == INVALID_FILE_ATTRIBUTES)
903 if ((rc = find_published_source( package, mi )) != ERROR_SUCCESS)
905 ERR("cabinet not found: %s\n", debugstr_w(cabinet_file));
906 msi_free( cabinet_file );
907 return ERROR_INSTALL_FAILURE;
911 msi_free( cabinet_file );
912 return ERROR_SUCCESS;
915 UINT msi_add_cabinet_stream( MSIPACKAGE *package, UINT disk_id, IStorage *storage, const WCHAR *name )
917 MSICABINETSTREAM *cab, *item;
919 TRACE("%p, %u, %p, %s\n", package, disk_id, storage, debugstr_w(name));
921 LIST_FOR_EACH_ENTRY( item, &package->cabinet_streams, MSICABINETSTREAM, entry )
923 if (item->disk_id == disk_id)
925 TRACE("duplicate disk id %u\n", disk_id);
926 return ERROR_FUNCTION_FAILED;
929 if (!(cab = msi_alloc( sizeof(*cab) ))) return ERROR_OUTOFMEMORY;
930 if (!(cab->stream = msi_alloc( (strlenW( name ) + 1) * sizeof(WCHAR ) )))
932 msi_free( cab );
933 return ERROR_OUTOFMEMORY;
935 strcpyW( cab->stream, name );
936 cab->disk_id = disk_id;
937 cab->storage = storage;
938 IStorage_AddRef( storage );
939 list_add_tail( &package->cabinet_streams, &cab->entry );
941 return ERROR_SUCCESS;