user32: Fall back to downscaling the big icon for ICON_SMALL2.
[wine/multimedia.git] / dlls / msi / media.c
blob7c8c94f0668e94d6d39b20804ffe991186dc49b4
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;
223 WCHAR *encoded;
224 HRESULT hr;
226 cab = msi_get_cabinet_stream( package_disk.package, package_disk.id );
227 if (!cab)
229 WARN("failed to get cabinet stream\n");
230 return -1;
232 if (!cab->stream[0] || !(encoded = encode_streamname( FALSE, cab->stream + 1 )))
234 WARN("failed to encode stream name\n");
235 return -1;
237 hr = IStorage_OpenStream( cab->storage, encoded, NULL, STGM_READ|STGM_SHARE_EXCLUSIVE, 0, &stream );
238 if (FAILED(hr))
240 WARN("failed to open stream 0x%08x\n", hr);
241 msi_free( encoded );
242 return -1;
244 msi_free( encoded );
245 return (INT_PTR)stream;
248 static UINT CDECL cabinet_read_stream( INT_PTR hf, void *pv, UINT cb )
250 IStream *stm = (IStream *)hf;
251 DWORD read;
252 HRESULT hr;
254 hr = IStream_Read( stm, pv, cb, &read );
255 if (hr == S_OK || hr == S_FALSE)
256 return read;
258 return 0;
261 static int CDECL cabinet_close_stream( INT_PTR hf )
263 IStream *stm = (IStream *)hf;
264 IStream_Release( stm );
265 return 0;
268 static LONG CDECL cabinet_seek_stream( INT_PTR hf, LONG dist, int seektype )
270 IStream *stm = (IStream *)hf;
271 LARGE_INTEGER move;
272 ULARGE_INTEGER newpos;
273 HRESULT hr;
275 move.QuadPart = dist;
276 hr = IStream_Seek( stm, move, seektype, &newpos );
277 if (SUCCEEDED(hr))
279 if (newpos.QuadPart <= MAXLONG) return newpos.QuadPart;
280 ERR("Too big!\n");
282 return -1;
285 static UINT CDECL msi_media_get_disk_info(MSIPACKAGE *package, MSIMEDIAINFO *mi)
287 MSIRECORD *row;
289 static const WCHAR query[] = {
290 'S','E','L','E','C','T',' ','*',' ', 'F','R','O','M',' ',
291 '`','M','e','d','i','a','`',' ','W','H','E','R','E',' ',
292 '`','D','i','s','k','I','d','`',' ','=',' ','%','i',0};
294 row = MSI_QueryGetRecord(package->db, query, mi->disk_id);
295 if (!row)
297 TRACE("Unable to query row\n");
298 return ERROR_FUNCTION_FAILED;
301 mi->disk_prompt = strdupW(MSI_RecordGetString(row, 3));
302 mi->cabinet = strdupW(MSI_RecordGetString(row, 4));
303 mi->volume_label = strdupW(MSI_RecordGetString(row, 5));
305 msiobj_release(&row->hdr);
306 return ERROR_SUCCESS;
309 static INT_PTR cabinet_partial_file(FDINOTIFICATIONTYPE fdint,
310 PFDINOTIFICATION pfdin)
312 MSICABDATA *data = pfdin->pv;
313 data->mi->is_continuous = FALSE;
314 return 0;
317 static WCHAR *get_cabinet_filename(MSIMEDIAINFO *mi)
319 int len;
320 WCHAR *ret;
322 len = strlenW(mi->sourcedir) + strlenW(mi->cabinet) + 1;
323 if (!(ret = msi_alloc(len * sizeof(WCHAR)))) return NULL;
324 strcpyW(ret, mi->sourcedir);
325 strcatW(ret, mi->cabinet);
326 return ret;
329 static INT_PTR cabinet_next_cabinet(FDINOTIFICATIONTYPE fdint,
330 PFDINOTIFICATION pfdin)
332 MSICABDATA *data = pfdin->pv;
333 MSIMEDIAINFO *mi = data->mi;
334 LPWSTR cabinet_file = NULL, cab = strdupAtoW(pfdin->psz1);
335 INT_PTR res = -1;
336 UINT rc;
338 msi_free(mi->disk_prompt);
339 msi_free(mi->cabinet);
340 msi_free(mi->volume_label);
341 mi->disk_prompt = NULL;
342 mi->cabinet = NULL;
343 mi->volume_label = NULL;
345 mi->disk_id++;
346 mi->is_continuous = TRUE;
348 rc = msi_media_get_disk_info(data->package, mi);
349 if (rc != ERROR_SUCCESS)
351 ERR("Failed to get next cabinet information: %d\n", rc);
352 goto done;
355 if (strcmpiW( mi->cabinet, cab ))
357 char *next_cab;
358 ULONG length;
360 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));
362 /* Use cabinet name from the media table */
363 next_cab = strdupWtoA(mi->cabinet);
364 /* Modify path to cabinet file with full filename (psz3 points to a 256 bytes buffer that can be modified contrary to psz1 and psz2) */
365 length = strlen(pfdin->psz3) + 1 + strlen(next_cab) + 1;
366 if (length > 256)
368 WARN("Cannot update next cabinet filename with a string size %u > 256\n", length);
369 msi_free(next_cab);
370 goto done;
372 else
374 strcat(pfdin->psz3, "\\");
375 strcat(pfdin->psz3, next_cab);
377 /* Path psz3 and cabinet psz1 are concatenated by FDI so just reset psz1 */
378 *pfdin->psz1 = 0;
379 msi_free(next_cab);
382 if (!(cabinet_file = get_cabinet_filename(mi)))
383 goto done;
385 TRACE("Searching for %s\n", debugstr_w(cabinet_file));
387 res = 0;
388 if (GetFileAttributesW(cabinet_file) == INVALID_FILE_ATTRIBUTES)
390 if (msi_change_media(data->package, mi) != ERROR_SUCCESS)
391 res = -1;
394 done:
395 msi_free(cab);
396 msi_free(cabinet_file);
397 return res;
400 static INT_PTR cabinet_next_cabinet_stream( FDINOTIFICATIONTYPE fdint,
401 PFDINOTIFICATION pfdin )
403 MSICABDATA *data = pfdin->pv;
404 MSIMEDIAINFO *mi = data->mi;
405 UINT rc;
407 msi_free( mi->disk_prompt );
408 msi_free( mi->cabinet );
409 msi_free( mi->volume_label );
410 mi->disk_prompt = NULL;
411 mi->cabinet = NULL;
412 mi->volume_label = NULL;
414 mi->disk_id++;
415 mi->is_continuous = TRUE;
417 rc = msi_media_get_disk_info( data->package, mi );
418 if (rc != ERROR_SUCCESS)
420 ERR("Failed to get next cabinet information: %u\n", rc);
421 return -1;
423 package_disk.id = mi->disk_id;
425 TRACE("next cabinet is %s disk id %u\n", debugstr_w(mi->cabinet), mi->disk_id);
426 return 0;
429 static INT_PTR cabinet_copy_file(FDINOTIFICATIONTYPE fdint,
430 PFDINOTIFICATION pfdin)
432 MSICABDATA *data = pfdin->pv;
433 HANDLE handle = 0;
434 LPWSTR path = NULL;
435 DWORD attrs;
437 data->curfile = strdupAtoW(pfdin->psz1);
438 if (!data->cb(data->package, data->curfile, MSICABEXTRACT_BEGINEXTRACT, &path,
439 &attrs, data->user))
441 /* We're not extracting this file, so free the filename. */
442 msi_free(data->curfile);
443 data->curfile = NULL;
444 goto done;
447 TRACE("extracting %s -> %s\n", debugstr_w(data->curfile), debugstr_w(path));
449 attrs = attrs & (FILE_ATTRIBUTE_READONLY|FILE_ATTRIBUTE_HIDDEN|FILE_ATTRIBUTE_SYSTEM);
450 if (!attrs) attrs = FILE_ATTRIBUTE_NORMAL;
452 handle = CreateFileW(path, GENERIC_READ | GENERIC_WRITE, 0,
453 NULL, CREATE_ALWAYS, attrs, NULL);
454 if (handle == INVALID_HANDLE_VALUE)
456 DWORD err = GetLastError();
457 DWORD attrs2 = GetFileAttributesW(path);
459 if (attrs2 == INVALID_FILE_ATTRIBUTES)
461 ERR("failed to create %s (error %d)\n", debugstr_w(path), err);
462 goto done;
464 else if (err == ERROR_ACCESS_DENIED && (attrs2 & FILE_ATTRIBUTE_READONLY))
466 TRACE("removing read-only attribute on %s\n", debugstr_w(path));
467 SetFileAttributesW( path, attrs2 & ~FILE_ATTRIBUTE_READONLY );
468 handle = CreateFileW(path, GENERIC_READ | GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, attrs2, NULL);
470 if (handle != INVALID_HANDLE_VALUE) goto done;
471 err = GetLastError();
473 if (err == ERROR_SHARING_VIOLATION || err == ERROR_USER_MAPPED_FILE)
475 WCHAR *tmpfileW, *tmppathW, *p;
476 DWORD len;
478 TRACE("file in use, scheduling rename operation\n");
480 if (!(tmppathW = strdupW( path ))) return ERROR_OUTOFMEMORY;
481 if ((p = strrchrW(tmppathW, '\\'))) *p = 0;
482 len = strlenW( tmppathW ) + 16;
483 if (!(tmpfileW = msi_alloc(len * sizeof(WCHAR))))
485 msi_free( tmppathW );
486 return ERROR_OUTOFMEMORY;
488 if (!GetTempFileNameW(tmppathW, szMsi, 0, tmpfileW)) tmpfileW[0] = 0;
489 msi_free( tmppathW );
491 handle = CreateFileW(tmpfileW, GENERIC_READ | GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, attrs, NULL);
493 if (handle != INVALID_HANDLE_VALUE &&
494 MoveFileExW(path, NULL, MOVEFILE_DELAY_UNTIL_REBOOT) &&
495 MoveFileExW(tmpfileW, path, MOVEFILE_DELAY_UNTIL_REBOOT))
497 data->package->need_reboot_at_end = 1;
499 else
501 WARN("failed to schedule rename operation %s (error %d)\n", debugstr_w(path), GetLastError());
502 DeleteFileW( tmpfileW );
504 msi_free(tmpfileW);
506 else
507 WARN("failed to create %s (error %d)\n", debugstr_w(path), err);
510 done:
511 msi_free(path);
513 return (INT_PTR)handle;
516 static INT_PTR cabinet_close_file_info(FDINOTIFICATIONTYPE fdint,
517 PFDINOTIFICATION pfdin)
519 MSICABDATA *data = pfdin->pv;
520 FILETIME ft;
521 FILETIME ftLocal;
522 HANDLE handle = (HANDLE)pfdin->hf;
524 data->mi->is_continuous = FALSE;
526 if (!DosDateTimeToFileTime(pfdin->date, pfdin->time, &ft))
527 return -1;
528 if (!LocalFileTimeToFileTime(&ft, &ftLocal))
529 return -1;
530 if (!SetFileTime(handle, &ftLocal, 0, &ftLocal))
531 return -1;
533 CloseHandle(handle);
535 data->cb(data->package, data->curfile, MSICABEXTRACT_FILEEXTRACTED, NULL, NULL,
536 data->user);
538 msi_free(data->curfile);
539 data->curfile = NULL;
541 return 1;
544 static INT_PTR CDECL cabinet_notify(FDINOTIFICATIONTYPE fdint, PFDINOTIFICATION pfdin)
546 switch (fdint)
548 case fdintPARTIAL_FILE:
549 return cabinet_partial_file(fdint, pfdin);
551 case fdintNEXT_CABINET:
552 return cabinet_next_cabinet(fdint, pfdin);
554 case fdintCOPY_FILE:
555 return cabinet_copy_file(fdint, pfdin);
557 case fdintCLOSE_FILE_INFO:
558 return cabinet_close_file_info(fdint, pfdin);
560 default:
561 return 0;
565 static INT_PTR CDECL cabinet_notify_stream( FDINOTIFICATIONTYPE fdint, PFDINOTIFICATION pfdin )
567 switch (fdint)
569 case fdintPARTIAL_FILE:
570 return cabinet_partial_file( fdint, pfdin );
572 case fdintNEXT_CABINET:
573 return cabinet_next_cabinet_stream( fdint, pfdin );
575 case fdintCOPY_FILE:
576 return cabinet_copy_file( fdint, pfdin );
578 case fdintCLOSE_FILE_INFO:
579 return cabinet_close_file_info( fdint, pfdin );
581 case fdintCABINET_INFO:
582 return 0;
584 default:
585 ERR("Unexpected notification %d\n", fdint);
586 return 0;
590 static BOOL extract_cabinet( MSIPACKAGE* package, MSIMEDIAINFO *mi, LPVOID data )
592 LPSTR cabinet, cab_path = NULL;
593 HFDI hfdi;
594 ERF erf;
595 BOOL ret = FALSE;
597 TRACE("extracting %s disk id %u\n", debugstr_w(mi->cabinet), mi->disk_id);
599 hfdi = FDICreate( cabinet_alloc, cabinet_free, cabinet_open, cabinet_read,
600 cabinet_write, cabinet_close, cabinet_seek, 0, &erf );
601 if (!hfdi)
603 ERR("FDICreate failed\n");
604 return FALSE;
607 cabinet = strdupWtoA( mi->cabinet );
608 if (!cabinet)
609 goto done;
611 cab_path = strdupWtoA( mi->sourcedir );
612 if (!cab_path)
613 goto done;
615 ret = FDICopy( hfdi, cabinet, cab_path, 0, cabinet_notify, NULL, data );
616 if (!ret)
617 ERR("FDICopy failed\n");
619 done:
620 FDIDestroy( hfdi );
621 msi_free(cabinet );
622 msi_free( cab_path );
624 if (ret)
625 mi->is_extracted = TRUE;
627 return ret;
630 static BOOL extract_cabinet_stream( MSIPACKAGE *package, MSIMEDIAINFO *mi, LPVOID data )
632 static char filename[] = {'<','S','T','R','E','A','M','>',0};
633 HFDI hfdi;
634 ERF erf;
635 BOOL ret = FALSE;
637 TRACE("extracting %s disk id %u\n", debugstr_w(mi->cabinet), mi->disk_id);
639 hfdi = FDICreate( cabinet_alloc, cabinet_free, cabinet_open_stream, cabinet_read_stream,
640 cabinet_write, cabinet_close_stream, cabinet_seek_stream, 0, &erf );
641 if (!hfdi)
643 ERR("FDICreate failed\n");
644 return FALSE;
647 package_disk.package = package;
648 package_disk.id = mi->disk_id;
650 ret = FDICopy( hfdi, filename, NULL, 0, cabinet_notify_stream, NULL, data );
651 if (!ret) ERR("FDICopy failed\n");
653 FDIDestroy( hfdi );
654 if (ret) mi->is_extracted = TRUE;
655 return ret;
658 /***********************************************************************
659 * msi_cabextract
661 * Extract files from a cabinet file or stream.
663 BOOL msi_cabextract(MSIPACKAGE* package, MSIMEDIAINFO *mi, LPVOID data)
665 if (mi->cabinet[0] == '#')
667 return extract_cabinet_stream( package, mi, data );
669 return extract_cabinet( package, mi, data );
672 void msi_free_media_info(MSIMEDIAINFO *mi)
674 msi_free(mi->disk_prompt);
675 msi_free(mi->cabinet);
676 msi_free(mi->volume_label);
677 msi_free(mi);
680 static UINT get_drive_type(const WCHAR *path)
682 WCHAR root[MAX_PATH + 1];
684 strcpyW(root, path);
685 PathStripToRootW(root);
686 PathAddBackslashW(root);
688 return GetDriveTypeW(root);
691 UINT msi_load_media_info(MSIPACKAGE *package, UINT Sequence, MSIMEDIAINFO *mi)
693 static const WCHAR query[] = {
694 'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ','`','M','e','d','i','a','`',' ',
695 'W','H','E','R','E',' ','`','L','a','s','t','S','e','q','u','e','n','c','e','`',' ',
696 '>','=',' ','%','i',' ','O','R','D','E','R',' ','B','Y',' ','`','D','i','s','k','I','d','`',0};
697 MSIRECORD *row;
698 LPWSTR source_dir, source;
699 DWORD options;
701 if (Sequence <= mi->last_sequence) /* already loaded */
702 return ERROR_SUCCESS;
704 row = MSI_QueryGetRecord(package->db, query, Sequence);
705 if (!row)
707 TRACE("Unable to query row\n");
708 return ERROR_FUNCTION_FAILED;
711 mi->is_extracted = FALSE;
712 mi->disk_id = MSI_RecordGetInteger(row, 1);
713 mi->last_sequence = MSI_RecordGetInteger(row, 2);
714 msi_free(mi->disk_prompt);
715 mi->disk_prompt = strdupW(MSI_RecordGetString(row, 3));
716 msi_free(mi->cabinet);
717 mi->cabinet = strdupW(MSI_RecordGetString(row, 4));
718 msi_free(mi->volume_label);
719 mi->volume_label = strdupW(MSI_RecordGetString(row, 5));
720 msiobj_release(&row->hdr);
722 msi_set_sourcedir_props(package, FALSE);
723 source_dir = msi_dup_property(package->db, szSourceDir);
724 lstrcpyW(mi->sourcedir, source_dir);
725 PathAddBackslashW(mi->sourcedir);
726 mi->type = get_drive_type(source_dir);
728 options = MSICODE_PRODUCT;
729 if (mi->type == DRIVE_CDROM || mi->type == DRIVE_REMOVABLE)
731 source = source_dir;
732 options |= MSISOURCETYPE_MEDIA;
734 else if (package->BaseURL && UrlIsW(package->BaseURL, URLIS_URL))
736 source = package->BaseURL;
737 options |= MSISOURCETYPE_URL;
739 else
741 source = mi->sourcedir;
742 options |= MSISOURCETYPE_NETWORK;
745 msi_package_add_media_disk(package, package->Context,
746 MSICODE_PRODUCT, mi->disk_id,
747 mi->volume_label, mi->disk_prompt);
749 msi_package_add_info(package, package->Context,
750 options, INSTALLPROPERTY_LASTUSEDSOURCEW, source);
752 msi_free(source_dir);
753 TRACE("sequence %u -> cabinet %s disk id %u\n", Sequence, debugstr_w(mi->cabinet), mi->disk_id);
754 return ERROR_SUCCESS;
757 /* FIXME: search URL sources as well */
758 static UINT find_published_source(MSIPACKAGE *package, MSIMEDIAINFO *mi)
760 WCHAR source[MAX_PATH];
761 WCHAR volume[MAX_PATH];
762 WCHAR prompt[MAX_PATH];
763 DWORD volumesz, promptsz;
764 DWORD index, size, id;
765 WCHAR last_type[2];
766 UINT r;
768 size = 2;
769 r = MsiSourceListGetInfoW(package->ProductCode, NULL,
770 package->Context, MSICODE_PRODUCT,
771 INSTALLPROPERTY_LASTUSEDTYPEW, last_type, &size);
772 if (r != ERROR_SUCCESS)
773 return r;
775 size = MAX_PATH;
776 r = MsiSourceListGetInfoW(package->ProductCode, NULL,
777 package->Context, MSICODE_PRODUCT,
778 INSTALLPROPERTY_LASTUSEDSOURCEW, source, &size);
779 if (r != ERROR_SUCCESS)
780 return r;
782 if (last_type[0] == 'n')
784 WCHAR cabinet_file[MAX_PATH];
785 BOOL check_all = FALSE;
787 while(TRUE)
789 index = 0;
790 volumesz = MAX_PATH;
791 while (MsiSourceListEnumSourcesW(package->ProductCode, NULL,
792 package->Context,
793 MSISOURCETYPE_NETWORK, index++,
794 volume, &volumesz) == ERROR_SUCCESS)
796 if (check_all || !strncmpiW(source, volume, strlenW(source)))
798 lstrcpyW(cabinet_file, volume);
799 PathAddBackslashW(cabinet_file);
800 lstrcatW(cabinet_file, mi->cabinet);
802 if (GetFileAttributesW(cabinet_file) == INVALID_FILE_ATTRIBUTES)
804 volumesz = MAX_PATH;
805 if(!check_all)
806 break;
807 continue;
810 lstrcpyW(mi->sourcedir, volume);
811 PathAddBackslashW(mi->sourcedir);
812 TRACE("Found network source %s\n", debugstr_w(mi->sourcedir));
813 return ERROR_SUCCESS;
817 if (!check_all)
818 check_all = TRUE;
819 else
820 break;
824 index = 0;
825 volumesz = MAX_PATH;
826 promptsz = MAX_PATH;
827 while (MsiSourceListEnumMediaDisksW(package->ProductCode, NULL,
828 package->Context,
829 MSICODE_PRODUCT, index++, &id,
830 volume, &volumesz, prompt, &promptsz) == ERROR_SUCCESS)
832 mi->disk_id = id;
833 msi_free( mi->volume_label );
834 if (!(mi->volume_label = msi_alloc( ++volumesz * sizeof(WCHAR) ))) return ERROR_OUTOFMEMORY;
835 strcpyW( mi->volume_label, volume );
837 msi_free( mi->disk_prompt );
838 if (!(mi->disk_prompt = msi_alloc( ++promptsz * sizeof(WCHAR) ))) return ERROR_OUTOFMEMORY;
839 strcpyW( mi->disk_prompt, prompt );
841 if (source_matches_volume(mi, source))
843 /* FIXME: what about SourceDir */
844 lstrcpyW(mi->sourcedir, source);
845 PathAddBackslashW(mi->sourcedir);
846 TRACE("Found disk source %s\n", debugstr_w(mi->sourcedir));
847 return ERROR_SUCCESS;
851 return ERROR_FUNCTION_FAILED;
854 UINT ready_media( MSIPACKAGE *package, BOOL compressed, MSIMEDIAINFO *mi )
856 UINT rc;
857 WCHAR *cabinet_file = NULL;
859 /* media info for continuous cabinet is already loaded */
860 if (mi->is_continuous) return ERROR_SUCCESS;
862 if (mi->cabinet)
864 /* cabinet is internal, no checks needed */
865 if (mi->cabinet[0] == '#') return ERROR_SUCCESS;
867 if (!(cabinet_file = get_cabinet_filename( mi ))) return ERROR_OUTOFMEMORY;
869 /* package should be downloaded */
870 if (compressed && GetFileAttributesW( cabinet_file ) == INVALID_FILE_ATTRIBUTES &&
871 package->BaseURL && UrlIsW( package->BaseURL, URLIS_URL ))
873 WCHAR temppath[MAX_PATH], *p;
875 if ((rc = msi_download_file( cabinet_file, temppath )) != ERROR_SUCCESS)
877 ERR("failed to download %s (%u)\n", debugstr_w(cabinet_file), rc);
878 msi_free( cabinet_file );
879 return rc;
881 if ((p = strrchrW( temppath, '\\' ))) *p = 0;
882 strcpyW( mi->sourcedir, temppath );
883 PathAddBackslashW( mi->sourcedir );
884 msi_free( mi->cabinet );
885 mi->cabinet = strdupW( p + 1 );
886 msi_free( cabinet_file );
887 return ERROR_SUCCESS;
890 /* check volume matches, change media if not */
891 if (mi->volume_label && mi->disk_id > 1)
893 WCHAR *source = msi_dup_property( package->db, szSourceDir );
894 BOOL match = source_matches_volume( mi, source );
895 msi_free( source );
897 if (!match && (mi->type == DRIVE_CDROM || mi->type == DRIVE_REMOVABLE))
899 if ((rc = msi_change_media( package, mi )) != ERROR_SUCCESS)
901 msi_free( cabinet_file );
902 return rc;
906 if (mi->cabinet)
908 if (compressed && GetFileAttributesW( cabinet_file ) == INVALID_FILE_ATTRIBUTES)
910 if ((rc = find_published_source( package, mi )) != ERROR_SUCCESS)
912 ERR("cabinet not found: %s\n", debugstr_w(cabinet_file));
913 msi_free( cabinet_file );
914 return ERROR_INSTALL_FAILURE;
918 msi_free( cabinet_file );
919 return ERROR_SUCCESS;
922 UINT msi_add_cabinet_stream( MSIPACKAGE *package, UINT disk_id, IStorage *storage, const WCHAR *name )
924 MSICABINETSTREAM *cab, *item;
926 TRACE("%p, %u, %p, %s\n", package, disk_id, storage, debugstr_w(name));
928 LIST_FOR_EACH_ENTRY( item, &package->cabinet_streams, MSICABINETSTREAM, entry )
930 if (item->disk_id == disk_id)
932 TRACE("duplicate disk id %u\n", disk_id);
933 return ERROR_FUNCTION_FAILED;
936 if (!(cab = msi_alloc( sizeof(*cab) ))) return ERROR_OUTOFMEMORY;
937 if (!(cab->stream = msi_alloc( (strlenW( name ) + 1) * sizeof(WCHAR ) )))
939 msi_free( cab );
940 return ERROR_OUTOFMEMORY;
942 strcpyW( cab->stream, name );
943 cab->disk_id = disk_id;
944 cab->storage = storage;
945 IStorage_AddRef( storage );
946 list_add_tail( &package->cabinet_streams, &cab->entry );
948 return ERROR_SUCCESS;