vbscript: Compile tests with -D__WINESRC__.
[wine.git] / dlls / msi / media.c
blobd97f4bda09f1dfb154fabfb4ca5e37b9ca5f8071
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 HANDLE handle;
141 DWORD dwAccess = 0;
142 DWORD dwShareMode = 0;
143 DWORD dwCreateDisposition = OPEN_EXISTING;
145 switch (oflag & _O_ACCMODE)
147 case _O_RDONLY:
148 dwAccess = GENERIC_READ;
149 dwShareMode = FILE_SHARE_READ | FILE_SHARE_DELETE;
150 break;
151 case _O_WRONLY:
152 dwAccess = GENERIC_WRITE;
153 dwShareMode = FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE;
154 break;
155 case _O_RDWR:
156 dwAccess = GENERIC_READ | GENERIC_WRITE;
157 dwShareMode = FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE;
158 break;
161 if ((oflag & (_O_CREAT | _O_EXCL)) == (_O_CREAT | _O_EXCL))
162 dwCreateDisposition = CREATE_NEW;
163 else if (oflag & _O_CREAT)
164 dwCreateDisposition = CREATE_ALWAYS;
166 handle = CreateFileA(pszFile, dwAccess, dwShareMode, NULL,
167 dwCreateDisposition, 0, NULL);
168 if (handle == INVALID_HANDLE_VALUE)
169 return 0;
171 return (INT_PTR)handle;
174 static UINT CDECL cabinet_read(INT_PTR hf, void *pv, UINT cb)
176 HANDLE handle = (HANDLE)hf;
177 DWORD read;
179 if (ReadFile(handle, pv, cb, &read, NULL))
180 return read;
182 return 0;
185 static UINT CDECL cabinet_write(INT_PTR hf, void *pv, UINT cb)
187 HANDLE handle = (HANDLE)hf;
188 DWORD written;
190 if (WriteFile(handle, pv, cb, &written, NULL))
191 return written;
193 return 0;
196 static int CDECL cabinet_close(INT_PTR hf)
198 HANDLE handle = (HANDLE)hf;
199 return CloseHandle(handle) ? 0 : -1;
202 static LONG CDECL cabinet_seek(INT_PTR hf, LONG dist, int seektype)
204 HANDLE handle = (HANDLE)hf;
205 /* flags are compatible and so are passed straight through */
206 return SetFilePointer(handle, dist, NULL, seektype);
209 struct package_disk
211 MSIPACKAGE *package;
212 UINT id;
215 static struct package_disk package_disk;
217 static INT_PTR CDECL cabinet_open_stream( char *pszFile, int oflag, int pmode )
219 MSICABINETSTREAM *cab;
220 IStream *stream;
221 WCHAR *encoded;
222 HRESULT hr;
224 cab = msi_get_cabinet_stream( package_disk.package, package_disk.id );
225 if (!cab)
227 WARN("failed to get cabinet stream\n");
228 return 0;
230 if (!cab->stream[0] || !(encoded = encode_streamname( FALSE, cab->stream + 1 )))
232 WARN("failed to encode stream name\n");
233 return 0;
235 if (msi_clone_open_stream( package_disk.package->db, cab->storage, encoded, &stream ) != ERROR_SUCCESS)
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 0;
245 msi_free( encoded );
246 return (INT_PTR)stream;
249 static UINT CDECL cabinet_read_stream( INT_PTR hf, void *pv, UINT cb )
251 IStream *stm = (IStream *)hf;
252 DWORD read;
253 HRESULT hr;
255 hr = IStream_Read( stm, pv, cb, &read );
256 if (hr == S_OK || hr == S_FALSE)
257 return read;
259 return 0;
262 static int CDECL cabinet_close_stream( INT_PTR hf )
264 IStream *stm = (IStream *)hf;
265 IStream_Release( stm );
266 return 0;
269 static LONG CDECL cabinet_seek_stream( INT_PTR hf, LONG dist, int seektype )
271 IStream *stm = (IStream *)hf;
272 LARGE_INTEGER move;
273 ULARGE_INTEGER newpos;
274 HRESULT hr;
276 move.QuadPart = dist;
277 hr = IStream_Seek( stm, move, seektype, &newpos );
278 if (SUCCEEDED(hr))
280 if (newpos.QuadPart <= MAXLONG) return newpos.QuadPart;
281 ERR("Too big!\n");
283 return -1;
286 static UINT CDECL msi_media_get_disk_info(MSIPACKAGE *package, MSIMEDIAINFO *mi)
288 MSIRECORD *row;
290 static const WCHAR query[] = {
291 'S','E','L','E','C','T',' ','*',' ', 'F','R','O','M',' ',
292 '`','M','e','d','i','a','`',' ','W','H','E','R','E',' ',
293 '`','D','i','s','k','I','d','`',' ','=',' ','%','i',0};
295 row = MSI_QueryGetRecord(package->db, query, mi->disk_id);
296 if (!row)
298 TRACE("Unable to query row\n");
299 return ERROR_FUNCTION_FAILED;
302 mi->disk_prompt = strdupW(MSI_RecordGetString(row, 3));
303 mi->cabinet = strdupW(MSI_RecordGetString(row, 4));
304 mi->volume_label = strdupW(MSI_RecordGetString(row, 5));
306 if (!mi->first_volume)
307 mi->first_volume = strdupW(mi->volume_label);
309 msiobj_release(&row->hdr);
310 return ERROR_SUCCESS;
313 static INT_PTR cabinet_partial_file(FDINOTIFICATIONTYPE fdint,
314 PFDINOTIFICATION pfdin)
316 MSICABDATA *data = pfdin->pv;
317 data->mi->is_continuous = FALSE;
318 return 0;
321 static WCHAR *get_cabinet_filename(MSIMEDIAINFO *mi)
323 int len;
324 WCHAR *ret;
326 len = strlenW(mi->sourcedir) + strlenW(mi->cabinet) + 1;
327 if (!(ret = msi_alloc(len * sizeof(WCHAR)))) return NULL;
328 strcpyW(ret, mi->sourcedir);
329 strcatW(ret, mi->cabinet);
330 return ret;
333 static INT_PTR cabinet_next_cabinet(FDINOTIFICATIONTYPE fdint,
334 PFDINOTIFICATION pfdin)
336 MSICABDATA *data = pfdin->pv;
337 MSIMEDIAINFO *mi = data->mi;
338 LPWSTR cabinet_file = NULL, cab = strdupAtoW(pfdin->psz1);
339 INT_PTR res = -1;
340 UINT rc;
342 msi_free(mi->disk_prompt);
343 msi_free(mi->cabinet);
344 msi_free(mi->volume_label);
345 mi->disk_prompt = NULL;
346 mi->cabinet = NULL;
347 mi->volume_label = NULL;
349 mi->disk_id++;
350 mi->is_continuous = TRUE;
352 rc = msi_media_get_disk_info(data->package, mi);
353 if (rc != ERROR_SUCCESS)
355 ERR("Failed to get next cabinet information: %d\n", rc);
356 goto done;
359 if (strcmpiW( mi->cabinet, cab ))
361 char *next_cab;
362 ULONG length;
364 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));
366 /* Use cabinet name from the media table */
367 next_cab = strdupWtoA(mi->cabinet);
368 /* Modify path to cabinet file with full filename (psz3 points to a 256 bytes buffer that can be modified contrary to psz1 and psz2) */
369 length = strlen(pfdin->psz3) + 1 + strlen(next_cab) + 1;
370 if (length > 256)
372 WARN("Cannot update next cabinet filename with a string size %u > 256\n", length);
373 msi_free(next_cab);
374 goto done;
376 else
378 strcat(pfdin->psz3, "\\");
379 strcat(pfdin->psz3, next_cab);
381 /* Path psz3 and cabinet psz1 are concatenated by FDI so just reset psz1 */
382 *pfdin->psz1 = 0;
383 msi_free(next_cab);
386 if (!(cabinet_file = get_cabinet_filename(mi)))
387 goto done;
389 TRACE("Searching for %s\n", debugstr_w(cabinet_file));
391 res = 0;
392 if (GetFileAttributesW(cabinet_file) == INVALID_FILE_ATTRIBUTES)
394 if (msi_change_media(data->package, mi) != ERROR_SUCCESS)
395 res = -1;
398 done:
399 msi_free(cab);
400 msi_free(cabinet_file);
401 return res;
404 static INT_PTR cabinet_next_cabinet_stream( FDINOTIFICATIONTYPE fdint,
405 PFDINOTIFICATION pfdin )
407 MSICABDATA *data = pfdin->pv;
408 MSIMEDIAINFO *mi = data->mi;
409 UINT rc;
411 msi_free( mi->disk_prompt );
412 msi_free( mi->cabinet );
413 msi_free( mi->volume_label );
414 mi->disk_prompt = NULL;
415 mi->cabinet = NULL;
416 mi->volume_label = NULL;
418 mi->disk_id++;
419 mi->is_continuous = TRUE;
421 rc = msi_media_get_disk_info( data->package, mi );
422 if (rc != ERROR_SUCCESS)
424 ERR("Failed to get next cabinet information: %u\n", rc);
425 return -1;
427 package_disk.id = mi->disk_id;
429 TRACE("next cabinet is %s disk id %u\n", debugstr_w(mi->cabinet), mi->disk_id);
430 return 0;
433 static INT_PTR cabinet_copy_file(FDINOTIFICATIONTYPE fdint,
434 PFDINOTIFICATION pfdin)
436 MSICABDATA *data = pfdin->pv;
437 HANDLE handle = 0;
438 LPWSTR path = NULL;
439 DWORD attrs;
441 data->curfile = strdupAtoW(pfdin->psz1);
442 if (!data->cb(data->package, data->curfile, MSICABEXTRACT_BEGINEXTRACT, &path,
443 &attrs, data->user))
445 /* We're not extracting this file, so free the filename. */
446 msi_free(data->curfile);
447 data->curfile = NULL;
448 goto done;
451 TRACE("extracting %s -> %s\n", debugstr_w(data->curfile), debugstr_w(path));
453 attrs = attrs & (FILE_ATTRIBUTE_READONLY|FILE_ATTRIBUTE_HIDDEN|FILE_ATTRIBUTE_SYSTEM);
454 if (!attrs) attrs = FILE_ATTRIBUTE_NORMAL;
456 handle = CreateFileW(path, GENERIC_READ | GENERIC_WRITE, 0,
457 NULL, CREATE_ALWAYS, attrs, NULL);
458 if (handle == INVALID_HANDLE_VALUE)
460 DWORD err = GetLastError();
461 DWORD attrs2 = GetFileAttributesW(path);
463 if (attrs2 == INVALID_FILE_ATTRIBUTES)
465 ERR("failed to create %s (error %d)\n", debugstr_w(path), err);
466 goto done;
468 else if (err == ERROR_ACCESS_DENIED && (attrs2 & FILE_ATTRIBUTE_READONLY))
470 TRACE("removing read-only attribute on %s\n", debugstr_w(path));
471 SetFileAttributesW( path, attrs2 & ~FILE_ATTRIBUTE_READONLY );
472 handle = CreateFileW(path, GENERIC_READ | GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, attrs2, NULL);
474 if (handle != INVALID_HANDLE_VALUE) goto done;
475 err = GetLastError();
477 if (err == ERROR_SHARING_VIOLATION || err == ERROR_USER_MAPPED_FILE)
479 WCHAR *tmpfileW, *tmppathW, *p;
480 DWORD len;
482 TRACE("file in use, scheduling rename operation\n");
484 if (!(tmppathW = strdupW( path ))) return ERROR_OUTOFMEMORY;
485 if ((p = strrchrW(tmppathW, '\\'))) *p = 0;
486 len = strlenW( tmppathW ) + 16;
487 if (!(tmpfileW = msi_alloc(len * sizeof(WCHAR))))
489 msi_free( tmppathW );
490 return ERROR_OUTOFMEMORY;
492 if (!GetTempFileNameW(tmppathW, szMsi, 0, tmpfileW)) tmpfileW[0] = 0;
493 msi_free( tmppathW );
495 handle = CreateFileW(tmpfileW, GENERIC_READ | GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, attrs, NULL);
497 if (handle != INVALID_HANDLE_VALUE &&
498 MoveFileExW(path, NULL, MOVEFILE_DELAY_UNTIL_REBOOT) &&
499 MoveFileExW(tmpfileW, path, MOVEFILE_DELAY_UNTIL_REBOOT))
501 data->package->need_reboot_at_end = 1;
503 else
505 WARN("failed to schedule rename operation %s (error %d)\n", debugstr_w(path), GetLastError());
506 DeleteFileW( tmpfileW );
508 msi_free(tmpfileW);
510 else
511 WARN("failed to create %s (error %d)\n", debugstr_w(path), err);
514 done:
515 msi_free(path);
517 return (INT_PTR)handle;
520 static INT_PTR cabinet_close_file_info(FDINOTIFICATIONTYPE fdint,
521 PFDINOTIFICATION pfdin)
523 MSICABDATA *data = pfdin->pv;
524 FILETIME ft;
525 FILETIME ftLocal;
526 HANDLE handle = (HANDLE)pfdin->hf;
528 data->mi->is_continuous = FALSE;
530 if (!DosDateTimeToFileTime(pfdin->date, pfdin->time, &ft))
531 return -1;
532 if (!LocalFileTimeToFileTime(&ft, &ftLocal))
533 return -1;
534 if (!SetFileTime(handle, &ftLocal, 0, &ftLocal))
535 return -1;
537 CloseHandle(handle);
539 data->cb(data->package, data->curfile, MSICABEXTRACT_FILEEXTRACTED, NULL, NULL,
540 data->user);
542 msi_free(data->curfile);
543 data->curfile = NULL;
545 return 1;
548 static INT_PTR CDECL cabinet_notify(FDINOTIFICATIONTYPE fdint, PFDINOTIFICATION pfdin)
550 switch (fdint)
552 case fdintPARTIAL_FILE:
553 return cabinet_partial_file(fdint, pfdin);
555 case fdintNEXT_CABINET:
556 return cabinet_next_cabinet(fdint, pfdin);
558 case fdintCOPY_FILE:
559 return cabinet_copy_file(fdint, pfdin);
561 case fdintCLOSE_FILE_INFO:
562 return cabinet_close_file_info(fdint, pfdin);
564 default:
565 return 0;
569 static INT_PTR CDECL cabinet_notify_stream( FDINOTIFICATIONTYPE fdint, PFDINOTIFICATION pfdin )
571 switch (fdint)
573 case fdintPARTIAL_FILE:
574 return cabinet_partial_file( fdint, pfdin );
576 case fdintNEXT_CABINET:
577 return cabinet_next_cabinet_stream( fdint, pfdin );
579 case fdintCOPY_FILE:
580 return cabinet_copy_file( fdint, pfdin );
582 case fdintCLOSE_FILE_INFO:
583 return cabinet_close_file_info( fdint, pfdin );
585 case fdintCABINET_INFO:
586 return 0;
588 default:
589 ERR("Unexpected notification %d\n", fdint);
590 return 0;
594 static BOOL extract_cabinet( MSIPACKAGE* package, MSIMEDIAINFO *mi, LPVOID data )
596 LPSTR cabinet, cab_path = NULL;
597 HFDI hfdi;
598 ERF erf;
599 BOOL ret = FALSE;
601 TRACE("extracting %s disk id %u\n", debugstr_w(mi->cabinet), mi->disk_id);
603 hfdi = FDICreate( cabinet_alloc, cabinet_free, cabinet_open, cabinet_read,
604 cabinet_write, cabinet_close, cabinet_seek, 0, &erf );
605 if (!hfdi)
607 ERR("FDICreate failed\n");
608 return FALSE;
611 cabinet = strdupWtoA( mi->cabinet );
612 if (!cabinet)
613 goto done;
615 cab_path = strdupWtoA( mi->sourcedir );
616 if (!cab_path)
617 goto done;
619 ret = FDICopy( hfdi, cabinet, cab_path, 0, cabinet_notify, NULL, data );
620 if (!ret)
621 ERR("FDICopy failed\n");
623 done:
624 FDIDestroy( hfdi );
625 msi_free(cabinet );
626 msi_free( cab_path );
628 if (ret)
629 mi->is_extracted = TRUE;
631 return ret;
634 static BOOL extract_cabinet_stream( MSIPACKAGE *package, MSIMEDIAINFO *mi, LPVOID data )
636 static char filename[] = {'<','S','T','R','E','A','M','>',0};
637 HFDI hfdi;
638 ERF erf;
639 BOOL ret = FALSE;
641 TRACE("extracting %s disk id %u\n", debugstr_w(mi->cabinet), mi->disk_id);
643 hfdi = FDICreate( cabinet_alloc, cabinet_free, cabinet_open_stream, cabinet_read_stream,
644 cabinet_write, cabinet_close_stream, cabinet_seek_stream, 0, &erf );
645 if (!hfdi)
647 ERR("FDICreate failed\n");
648 return FALSE;
651 package_disk.package = package;
652 package_disk.id = mi->disk_id;
654 ret = FDICopy( hfdi, filename, NULL, 0, cabinet_notify_stream, NULL, data );
655 if (!ret) ERR("FDICopy failed\n");
657 FDIDestroy( hfdi );
658 if (ret) mi->is_extracted = TRUE;
659 return ret;
662 /***********************************************************************
663 * msi_cabextract
665 * Extract files from a cabinet file or stream.
667 BOOL msi_cabextract(MSIPACKAGE* package, MSIMEDIAINFO *mi, LPVOID data)
669 if (mi->cabinet[0] == '#')
671 return extract_cabinet_stream( package, mi, data );
673 return extract_cabinet( package, mi, data );
676 void msi_free_media_info(MSIMEDIAINFO *mi)
678 msi_free(mi->disk_prompt);
679 msi_free(mi->cabinet);
680 msi_free(mi->volume_label);
681 msi_free(mi->first_volume);
682 msi_free(mi);
685 static UINT get_drive_type(const WCHAR *path)
687 WCHAR root[MAX_PATH + 1];
689 strcpyW(root, path);
690 PathStripToRootW(root);
691 PathAddBackslashW(root);
693 return GetDriveTypeW(root);
696 UINT msi_load_media_info(MSIPACKAGE *package, UINT Sequence, MSIMEDIAINFO *mi)
698 static const WCHAR query[] = {
699 'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ','`','M','e','d','i','a','`',' ',
700 'W','H','E','R','E',' ','`','L','a','s','t','S','e','q','u','e','n','c','e','`',' ',
701 '>','=',' ','%','i',' ','O','R','D','E','R',' ','B','Y',' ','`','D','i','s','k','I','d','`',0};
702 MSIRECORD *row;
703 LPWSTR source_dir, source;
704 DWORD options;
706 if (Sequence <= mi->last_sequence) /* already loaded */
707 return ERROR_SUCCESS;
709 row = MSI_QueryGetRecord(package->db, query, Sequence);
710 if (!row)
712 TRACE("Unable to query row\n");
713 return ERROR_FUNCTION_FAILED;
716 mi->is_extracted = FALSE;
717 mi->disk_id = MSI_RecordGetInteger(row, 1);
718 mi->last_sequence = MSI_RecordGetInteger(row, 2);
719 msi_free(mi->disk_prompt);
720 mi->disk_prompt = strdupW(MSI_RecordGetString(row, 3));
721 msi_free(mi->cabinet);
722 mi->cabinet = strdupW(MSI_RecordGetString(row, 4));
723 msi_free(mi->volume_label);
724 mi->volume_label = strdupW(MSI_RecordGetString(row, 5));
725 msiobj_release(&row->hdr);
727 if (!mi->first_volume)
728 mi->first_volume = strdupW(mi->volume_label);
730 msi_set_sourcedir_props(package, FALSE);
731 source_dir = msi_dup_property(package->db, szSourceDir);
732 lstrcpyW(mi->sourcedir, source_dir);
733 PathAddBackslashW(mi->sourcedir);
734 mi->type = get_drive_type(source_dir);
736 options = MSICODE_PRODUCT;
737 if (mi->type == DRIVE_CDROM || mi->type == DRIVE_REMOVABLE)
739 source = source_dir;
740 options |= MSISOURCETYPE_MEDIA;
742 else if (package->BaseURL && UrlIsW(package->BaseURL, URLIS_URL))
744 source = package->BaseURL;
745 options |= MSISOURCETYPE_URL;
747 else
749 source = mi->sourcedir;
750 options |= MSISOURCETYPE_NETWORK;
753 msi_package_add_media_disk(package, package->Context,
754 MSICODE_PRODUCT, mi->disk_id,
755 mi->volume_label, mi->disk_prompt);
757 msi_package_add_info(package, package->Context,
758 options, INSTALLPROPERTY_LASTUSEDSOURCEW, source);
760 msi_free(source_dir);
761 TRACE("sequence %u -> cabinet %s disk id %u\n", Sequence, debugstr_w(mi->cabinet), mi->disk_id);
762 return ERROR_SUCCESS;
765 /* FIXME: search URL sources as well */
766 static UINT find_published_source(MSIPACKAGE *package, MSIMEDIAINFO *mi)
768 WCHAR source[MAX_PATH];
769 WCHAR volume[MAX_PATH];
770 WCHAR prompt[MAX_PATH];
771 DWORD volumesz, promptsz;
772 DWORD index, size, id;
773 WCHAR last_type[2];
774 UINT r;
776 size = 2;
777 r = MsiSourceListGetInfoW(package->ProductCode, NULL,
778 package->Context, MSICODE_PRODUCT,
779 INSTALLPROPERTY_LASTUSEDTYPEW, last_type, &size);
780 if (r != ERROR_SUCCESS)
781 return r;
783 size = MAX_PATH;
784 r = MsiSourceListGetInfoW(package->ProductCode, NULL,
785 package->Context, MSICODE_PRODUCT,
786 INSTALLPROPERTY_LASTUSEDSOURCEW, source, &size);
787 if (r != ERROR_SUCCESS)
788 return r;
790 if (last_type[0] == 'n')
792 WCHAR cabinet_file[MAX_PATH];
793 BOOL check_all = FALSE;
795 while(TRUE)
797 index = 0;
798 volumesz = MAX_PATH;
799 while (MsiSourceListEnumSourcesW(package->ProductCode, NULL,
800 package->Context,
801 MSISOURCETYPE_NETWORK, index++,
802 volume, &volumesz) == ERROR_SUCCESS)
804 if (check_all || !strncmpiW(source, volume, strlenW(source)))
806 lstrcpyW(cabinet_file, volume);
807 PathAddBackslashW(cabinet_file);
808 lstrcatW(cabinet_file, mi->cabinet);
810 if (GetFileAttributesW(cabinet_file) == INVALID_FILE_ATTRIBUTES)
812 volumesz = MAX_PATH;
813 if(!check_all)
814 break;
815 continue;
818 lstrcpyW(mi->sourcedir, volume);
819 PathAddBackslashW(mi->sourcedir);
820 TRACE("Found network source %s\n", debugstr_w(mi->sourcedir));
821 return ERROR_SUCCESS;
825 if (!check_all)
826 check_all = TRUE;
827 else
828 break;
832 index = 0;
833 volumesz = MAX_PATH;
834 promptsz = MAX_PATH;
835 while (MsiSourceListEnumMediaDisksW(package->ProductCode, NULL,
836 package->Context,
837 MSICODE_PRODUCT, index++, &id,
838 volume, &volumesz, prompt, &promptsz) == ERROR_SUCCESS)
840 mi->disk_id = id;
841 msi_free( mi->volume_label );
842 if (!(mi->volume_label = msi_alloc( ++volumesz * sizeof(WCHAR) ))) return ERROR_OUTOFMEMORY;
843 strcpyW( mi->volume_label, volume );
845 msi_free( mi->disk_prompt );
846 if (!(mi->disk_prompt = msi_alloc( ++promptsz * sizeof(WCHAR) ))) return ERROR_OUTOFMEMORY;
847 strcpyW( mi->disk_prompt, prompt );
849 if (source_matches_volume(mi, source))
851 /* FIXME: what about SourceDir */
852 lstrcpyW(mi->sourcedir, source);
853 PathAddBackslashW(mi->sourcedir);
854 TRACE("Found disk source %s\n", debugstr_w(mi->sourcedir));
855 return ERROR_SUCCESS;
859 return ERROR_FUNCTION_FAILED;
862 UINT ready_media( MSIPACKAGE *package, BOOL compressed, MSIMEDIAINFO *mi )
864 UINT rc;
865 WCHAR *cabinet_file = NULL;
867 /* media info for continuous cabinet is already loaded */
868 if (mi->is_continuous) return ERROR_SUCCESS;
870 if (mi->cabinet)
872 /* cabinet is internal, no checks needed */
873 if (mi->cabinet[0] == '#') return ERROR_SUCCESS;
875 if (!(cabinet_file = get_cabinet_filename( mi ))) return ERROR_OUTOFMEMORY;
877 /* package should be downloaded */
878 if (compressed && GetFileAttributesW( cabinet_file ) == INVALID_FILE_ATTRIBUTES &&
879 package->BaseURL && UrlIsW( package->BaseURL, URLIS_URL ))
881 WCHAR temppath[MAX_PATH], *p;
883 if ((rc = msi_download_file( cabinet_file, temppath )) != ERROR_SUCCESS)
885 ERR("failed to download %s (%u)\n", debugstr_w(cabinet_file), rc);
886 msi_free( cabinet_file );
887 return rc;
889 if ((p = strrchrW( temppath, '\\' ))) *p = 0;
890 strcpyW( mi->sourcedir, temppath );
891 PathAddBackslashW( mi->sourcedir );
892 msi_free( mi->cabinet );
893 mi->cabinet = strdupW( p + 1 );
894 msi_free( cabinet_file );
895 return ERROR_SUCCESS;
898 /* check volume matches, change media if not */
899 if (mi->volume_label && mi->disk_id > 1 && strcmpW( mi->first_volume, mi->volume_label ))
901 WCHAR *source = msi_dup_property( package->db, szSourceDir );
902 BOOL match = source_matches_volume( mi, source );
903 msi_free( source );
905 if (!match && (mi->type == DRIVE_CDROM || mi->type == DRIVE_REMOVABLE))
907 if ((rc = msi_change_media( package, mi )) != ERROR_SUCCESS)
909 msi_free( cabinet_file );
910 return rc;
914 if (mi->cabinet)
916 if (compressed && GetFileAttributesW( cabinet_file ) == INVALID_FILE_ATTRIBUTES)
918 if ((rc = find_published_source( package, mi )) != ERROR_SUCCESS)
920 ERR("cabinet not found: %s\n", debugstr_w(cabinet_file));
921 msi_free( cabinet_file );
922 return ERROR_INSTALL_FAILURE;
926 msi_free( cabinet_file );
927 return ERROR_SUCCESS;
930 UINT msi_add_cabinet_stream( MSIPACKAGE *package, UINT disk_id, IStorage *storage, const WCHAR *name )
932 MSICABINETSTREAM *cab, *item;
934 TRACE("%p, %u, %p, %s\n", package, disk_id, storage, debugstr_w(name));
936 LIST_FOR_EACH_ENTRY( item, &package->cabinet_streams, MSICABINETSTREAM, entry )
938 if (item->disk_id == disk_id)
940 TRACE("duplicate disk id %u\n", disk_id);
941 return ERROR_FUNCTION_FAILED;
944 if (!(cab = msi_alloc( sizeof(*cab) ))) return ERROR_OUTOFMEMORY;
945 if (!(cab->stream = msi_alloc( (strlenW( name ) + 1) * sizeof(WCHAR ) )))
947 msi_free( cab );
948 return ERROR_OUTOFMEMORY;
950 strcpyW( cab->stream, name );
951 cab->disk_id = disk_id;
952 cab->storage = storage;
953 IStorage_AddRef( storage );
954 list_add_tail( &package->cabinet_streams, &cab->entry );
956 return ERROR_SUCCESS;