urlmon/tests: Use BOOL type where appropriate.
[wine/multimedia.git] / dlls / msi / media.c
blob8646069636093743d0d2a30b8a5055c94b6d7d4a
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 if (!mi->first_volume)
302 mi->first_volume = strdupW(mi->volume_label);
304 msiobj_release(&row->hdr);
305 return ERROR_SUCCESS;
308 static INT_PTR cabinet_partial_file(FDINOTIFICATIONTYPE fdint,
309 PFDINOTIFICATION pfdin)
311 MSICABDATA *data = pfdin->pv;
312 data->mi->is_continuous = FALSE;
313 return 0;
316 static WCHAR *get_cabinet_filename(MSIMEDIAINFO *mi)
318 int len;
319 WCHAR *ret;
321 len = strlenW(mi->sourcedir) + strlenW(mi->cabinet) + 1;
322 if (!(ret = msi_alloc(len * sizeof(WCHAR)))) return NULL;
323 strcpyW(ret, mi->sourcedir);
324 strcatW(ret, mi->cabinet);
325 return ret;
328 static INT_PTR cabinet_next_cabinet(FDINOTIFICATIONTYPE fdint,
329 PFDINOTIFICATION pfdin)
331 MSICABDATA *data = pfdin->pv;
332 MSIMEDIAINFO *mi = data->mi;
333 LPWSTR cabinet_file = NULL, cab = strdupAtoW(pfdin->psz1);
334 INT_PTR res = -1;
335 UINT rc;
337 msi_free(mi->disk_prompt);
338 msi_free(mi->cabinet);
339 msi_free(mi->volume_label);
340 mi->disk_prompt = NULL;
341 mi->cabinet = NULL;
342 mi->volume_label = NULL;
344 mi->disk_id++;
345 mi->is_continuous = TRUE;
347 rc = msi_media_get_disk_info(data->package, mi);
348 if (rc != ERROR_SUCCESS)
350 ERR("Failed to get next cabinet information: %d\n", rc);
351 goto done;
354 if (strcmpiW( mi->cabinet, cab ))
356 char *next_cab;
357 ULONG length;
359 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));
361 /* Use cabinet name from the media table */
362 next_cab = strdupWtoA(mi->cabinet);
363 /* Modify path to cabinet file with full filename (psz3 points to a 256 bytes buffer that can be modified contrary to psz1 and psz2) */
364 length = strlen(pfdin->psz3) + 1 + strlen(next_cab) + 1;
365 if (length > 256)
367 WARN("Cannot update next cabinet filename with a string size %u > 256\n", length);
368 msi_free(next_cab);
369 goto done;
371 else
373 strcat(pfdin->psz3, "\\");
374 strcat(pfdin->psz3, next_cab);
376 /* Path psz3 and cabinet psz1 are concatenated by FDI so just reset psz1 */
377 *pfdin->psz1 = 0;
378 msi_free(next_cab);
381 if (!(cabinet_file = get_cabinet_filename(mi)))
382 goto done;
384 TRACE("Searching for %s\n", debugstr_w(cabinet_file));
386 res = 0;
387 if (GetFileAttributesW(cabinet_file) == INVALID_FILE_ATTRIBUTES)
389 if (msi_change_media(data->package, mi) != ERROR_SUCCESS)
390 res = -1;
393 done:
394 msi_free(cab);
395 msi_free(cabinet_file);
396 return res;
399 static INT_PTR cabinet_next_cabinet_stream( FDINOTIFICATIONTYPE fdint,
400 PFDINOTIFICATION pfdin )
402 MSICABDATA *data = pfdin->pv;
403 MSIMEDIAINFO *mi = data->mi;
404 UINT rc;
406 msi_free( mi->disk_prompt );
407 msi_free( mi->cabinet );
408 msi_free( mi->volume_label );
409 mi->disk_prompt = NULL;
410 mi->cabinet = NULL;
411 mi->volume_label = NULL;
413 mi->disk_id++;
414 mi->is_continuous = TRUE;
416 rc = msi_media_get_disk_info( data->package, mi );
417 if (rc != ERROR_SUCCESS)
419 ERR("Failed to get next cabinet information: %u\n", rc);
420 return -1;
422 package_disk.id = mi->disk_id;
424 TRACE("next cabinet is %s disk id %u\n", debugstr_w(mi->cabinet), mi->disk_id);
425 return 0;
428 static INT_PTR cabinet_copy_file(FDINOTIFICATIONTYPE fdint,
429 PFDINOTIFICATION pfdin)
431 MSICABDATA *data = pfdin->pv;
432 HANDLE handle = 0;
433 LPWSTR path = NULL;
434 DWORD attrs;
436 data->curfile = strdupAtoW(pfdin->psz1);
437 if (!data->cb(data->package, data->curfile, MSICABEXTRACT_BEGINEXTRACT, &path,
438 &attrs, data->user))
440 /* We're not extracting this file, so free the filename. */
441 msi_free(data->curfile);
442 data->curfile = NULL;
443 goto done;
446 TRACE("extracting %s -> %s\n", debugstr_w(data->curfile), debugstr_w(path));
448 attrs = attrs & (FILE_ATTRIBUTE_READONLY|FILE_ATTRIBUTE_HIDDEN|FILE_ATTRIBUTE_SYSTEM);
449 if (!attrs) attrs = FILE_ATTRIBUTE_NORMAL;
451 handle = CreateFileW(path, GENERIC_READ | GENERIC_WRITE, 0,
452 NULL, CREATE_ALWAYS, attrs, NULL);
453 if (handle == INVALID_HANDLE_VALUE)
455 DWORD err = GetLastError();
456 DWORD attrs2 = GetFileAttributesW(path);
458 if (attrs2 == INVALID_FILE_ATTRIBUTES)
460 ERR("failed to create %s (error %d)\n", debugstr_w(path), err);
461 goto done;
463 else if (err == ERROR_ACCESS_DENIED && (attrs2 & FILE_ATTRIBUTE_READONLY))
465 TRACE("removing read-only attribute on %s\n", debugstr_w(path));
466 SetFileAttributesW( path, attrs2 & ~FILE_ATTRIBUTE_READONLY );
467 handle = CreateFileW(path, GENERIC_READ | GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, attrs2, NULL);
469 if (handle != INVALID_HANDLE_VALUE) goto done;
470 err = GetLastError();
472 if (err == ERROR_SHARING_VIOLATION || err == ERROR_USER_MAPPED_FILE)
474 WCHAR *tmpfileW, *tmppathW, *p;
475 DWORD len;
477 TRACE("file in use, scheduling rename operation\n");
479 if (!(tmppathW = strdupW( path ))) return ERROR_OUTOFMEMORY;
480 if ((p = strrchrW(tmppathW, '\\'))) *p = 0;
481 len = strlenW( tmppathW ) + 16;
482 if (!(tmpfileW = msi_alloc(len * sizeof(WCHAR))))
484 msi_free( tmppathW );
485 return ERROR_OUTOFMEMORY;
487 if (!GetTempFileNameW(tmppathW, szMsi, 0, tmpfileW)) tmpfileW[0] = 0;
488 msi_free( tmppathW );
490 handle = CreateFileW(tmpfileW, GENERIC_READ | GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, attrs, NULL);
492 if (handle != INVALID_HANDLE_VALUE &&
493 MoveFileExW(path, NULL, MOVEFILE_DELAY_UNTIL_REBOOT) &&
494 MoveFileExW(tmpfileW, path, MOVEFILE_DELAY_UNTIL_REBOOT))
496 data->package->need_reboot_at_end = 1;
498 else
500 WARN("failed to schedule rename operation %s (error %d)\n", debugstr_w(path), GetLastError());
501 DeleteFileW( tmpfileW );
503 msi_free(tmpfileW);
505 else
506 WARN("failed to create %s (error %d)\n", debugstr_w(path), err);
509 done:
510 msi_free(path);
512 return (INT_PTR)handle;
515 static INT_PTR cabinet_close_file_info(FDINOTIFICATIONTYPE fdint,
516 PFDINOTIFICATION pfdin)
518 MSICABDATA *data = pfdin->pv;
519 FILETIME ft;
520 FILETIME ftLocal;
521 HANDLE handle = (HANDLE)pfdin->hf;
523 data->mi->is_continuous = FALSE;
525 if (!DosDateTimeToFileTime(pfdin->date, pfdin->time, &ft))
526 return -1;
527 if (!LocalFileTimeToFileTime(&ft, &ftLocal))
528 return -1;
529 if (!SetFileTime(handle, &ftLocal, 0, &ftLocal))
530 return -1;
532 CloseHandle(handle);
534 data->cb(data->package, data->curfile, MSICABEXTRACT_FILEEXTRACTED, NULL, NULL,
535 data->user);
537 msi_free(data->curfile);
538 data->curfile = NULL;
540 return 1;
543 static INT_PTR CDECL cabinet_notify(FDINOTIFICATIONTYPE fdint, PFDINOTIFICATION pfdin)
545 switch (fdint)
547 case fdintPARTIAL_FILE:
548 return cabinet_partial_file(fdint, pfdin);
550 case fdintNEXT_CABINET:
551 return cabinet_next_cabinet(fdint, pfdin);
553 case fdintCOPY_FILE:
554 return cabinet_copy_file(fdint, pfdin);
556 case fdintCLOSE_FILE_INFO:
557 return cabinet_close_file_info(fdint, pfdin);
559 default:
560 return 0;
564 static INT_PTR CDECL cabinet_notify_stream( FDINOTIFICATIONTYPE fdint, PFDINOTIFICATION pfdin )
566 switch (fdint)
568 case fdintPARTIAL_FILE:
569 return cabinet_partial_file( fdint, pfdin );
571 case fdintNEXT_CABINET:
572 return cabinet_next_cabinet_stream( fdint, pfdin );
574 case fdintCOPY_FILE:
575 return cabinet_copy_file( fdint, pfdin );
577 case fdintCLOSE_FILE_INFO:
578 return cabinet_close_file_info( fdint, pfdin );
580 case fdintCABINET_INFO:
581 return 0;
583 default:
584 ERR("Unexpected notification %d\n", fdint);
585 return 0;
589 static BOOL extract_cabinet( MSIPACKAGE* package, MSIMEDIAINFO *mi, LPVOID data )
591 LPSTR cabinet, cab_path = NULL;
592 HFDI hfdi;
593 ERF erf;
594 BOOL ret = FALSE;
596 TRACE("extracting %s disk id %u\n", debugstr_w(mi->cabinet), mi->disk_id);
598 hfdi = FDICreate( cabinet_alloc, cabinet_free, cabinet_open, cabinet_read,
599 cabinet_write, cabinet_close, cabinet_seek, 0, &erf );
600 if (!hfdi)
602 ERR("FDICreate failed\n");
603 return FALSE;
606 cabinet = strdupWtoA( mi->cabinet );
607 if (!cabinet)
608 goto done;
610 cab_path = strdupWtoA( mi->sourcedir );
611 if (!cab_path)
612 goto done;
614 ret = FDICopy( hfdi, cabinet, cab_path, 0, cabinet_notify, NULL, data );
615 if (!ret)
616 ERR("FDICopy failed\n");
618 done:
619 FDIDestroy( hfdi );
620 msi_free(cabinet );
621 msi_free( cab_path );
623 if (ret)
624 mi->is_extracted = TRUE;
626 return ret;
629 static BOOL extract_cabinet_stream( MSIPACKAGE *package, MSIMEDIAINFO *mi, LPVOID data )
631 static char filename[] = {'<','S','T','R','E','A','M','>',0};
632 HFDI hfdi;
633 ERF erf;
634 BOOL ret = FALSE;
636 TRACE("extracting %s disk id %u\n", debugstr_w(mi->cabinet), mi->disk_id);
638 hfdi = FDICreate( cabinet_alloc, cabinet_free, cabinet_open_stream, cabinet_read_stream,
639 cabinet_write, cabinet_close_stream, cabinet_seek_stream, 0, &erf );
640 if (!hfdi)
642 ERR("FDICreate failed\n");
643 return FALSE;
646 package_disk.package = package;
647 package_disk.id = mi->disk_id;
649 ret = FDICopy( hfdi, filename, NULL, 0, cabinet_notify_stream, NULL, data );
650 if (!ret) ERR("FDICopy failed\n");
652 FDIDestroy( hfdi );
653 if (ret) mi->is_extracted = TRUE;
654 return ret;
657 /***********************************************************************
658 * msi_cabextract
660 * Extract files from a cabinet file or stream.
662 BOOL msi_cabextract(MSIPACKAGE* package, MSIMEDIAINFO *mi, LPVOID data)
664 if (mi->cabinet[0] == '#')
666 return extract_cabinet_stream( package, mi, data );
668 return extract_cabinet( package, mi, data );
671 void msi_free_media_info(MSIMEDIAINFO *mi)
673 msi_free(mi->disk_prompt);
674 msi_free(mi->cabinet);
675 msi_free(mi->volume_label);
676 msi_free(mi->first_volume);
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 if (!mi->first_volume)
723 mi->first_volume = strdupW(mi->volume_label);
725 msi_set_sourcedir_props(package, FALSE);
726 source_dir = msi_dup_property(package->db, szSourceDir);
727 lstrcpyW(mi->sourcedir, source_dir);
728 PathAddBackslashW(mi->sourcedir);
729 mi->type = get_drive_type(source_dir);
731 options = MSICODE_PRODUCT;
732 if (mi->type == DRIVE_CDROM || mi->type == DRIVE_REMOVABLE)
734 source = source_dir;
735 options |= MSISOURCETYPE_MEDIA;
737 else if (package->BaseURL && UrlIsW(package->BaseURL, URLIS_URL))
739 source = package->BaseURL;
740 options |= MSISOURCETYPE_URL;
742 else
744 source = mi->sourcedir;
745 options |= MSISOURCETYPE_NETWORK;
748 msi_package_add_media_disk(package, package->Context,
749 MSICODE_PRODUCT, mi->disk_id,
750 mi->volume_label, mi->disk_prompt);
752 msi_package_add_info(package, package->Context,
753 options, INSTALLPROPERTY_LASTUSEDSOURCEW, source);
755 msi_free(source_dir);
756 TRACE("sequence %u -> cabinet %s disk id %u\n", Sequence, debugstr_w(mi->cabinet), mi->disk_id);
757 return ERROR_SUCCESS;
760 /* FIXME: search URL sources as well */
761 static UINT find_published_source(MSIPACKAGE *package, MSIMEDIAINFO *mi)
763 WCHAR source[MAX_PATH];
764 WCHAR volume[MAX_PATH];
765 WCHAR prompt[MAX_PATH];
766 DWORD volumesz, promptsz;
767 DWORD index, size, id;
768 WCHAR last_type[2];
769 UINT r;
771 size = 2;
772 r = MsiSourceListGetInfoW(package->ProductCode, NULL,
773 package->Context, MSICODE_PRODUCT,
774 INSTALLPROPERTY_LASTUSEDTYPEW, last_type, &size);
775 if (r != ERROR_SUCCESS)
776 return r;
778 size = MAX_PATH;
779 r = MsiSourceListGetInfoW(package->ProductCode, NULL,
780 package->Context, MSICODE_PRODUCT,
781 INSTALLPROPERTY_LASTUSEDSOURCEW, source, &size);
782 if (r != ERROR_SUCCESS)
783 return r;
785 if (last_type[0] == 'n')
787 WCHAR cabinet_file[MAX_PATH];
788 BOOL check_all = FALSE;
790 while(TRUE)
792 index = 0;
793 volumesz = MAX_PATH;
794 while (MsiSourceListEnumSourcesW(package->ProductCode, NULL,
795 package->Context,
796 MSISOURCETYPE_NETWORK, index++,
797 volume, &volumesz) == ERROR_SUCCESS)
799 if (check_all || !strncmpiW(source, volume, strlenW(source)))
801 lstrcpyW(cabinet_file, volume);
802 PathAddBackslashW(cabinet_file);
803 lstrcatW(cabinet_file, mi->cabinet);
805 if (GetFileAttributesW(cabinet_file) == INVALID_FILE_ATTRIBUTES)
807 volumesz = MAX_PATH;
808 if(!check_all)
809 break;
810 continue;
813 lstrcpyW(mi->sourcedir, volume);
814 PathAddBackslashW(mi->sourcedir);
815 TRACE("Found network source %s\n", debugstr_w(mi->sourcedir));
816 return ERROR_SUCCESS;
820 if (!check_all)
821 check_all = TRUE;
822 else
823 break;
827 index = 0;
828 volumesz = MAX_PATH;
829 promptsz = MAX_PATH;
830 while (MsiSourceListEnumMediaDisksW(package->ProductCode, NULL,
831 package->Context,
832 MSICODE_PRODUCT, index++, &id,
833 volume, &volumesz, prompt, &promptsz) == ERROR_SUCCESS)
835 mi->disk_id = id;
836 msi_free( mi->volume_label );
837 if (!(mi->volume_label = msi_alloc( ++volumesz * sizeof(WCHAR) ))) return ERROR_OUTOFMEMORY;
838 strcpyW( mi->volume_label, volume );
840 msi_free( mi->disk_prompt );
841 if (!(mi->disk_prompt = msi_alloc( ++promptsz * sizeof(WCHAR) ))) return ERROR_OUTOFMEMORY;
842 strcpyW( mi->disk_prompt, prompt );
844 if (source_matches_volume(mi, source))
846 /* FIXME: what about SourceDir */
847 lstrcpyW(mi->sourcedir, source);
848 PathAddBackslashW(mi->sourcedir);
849 TRACE("Found disk source %s\n", debugstr_w(mi->sourcedir));
850 return ERROR_SUCCESS;
854 return ERROR_FUNCTION_FAILED;
857 UINT ready_media( MSIPACKAGE *package, BOOL compressed, MSIMEDIAINFO *mi )
859 UINT rc;
860 WCHAR *cabinet_file = NULL;
862 /* media info for continuous cabinet is already loaded */
863 if (mi->is_continuous) return ERROR_SUCCESS;
865 if (mi->cabinet)
867 /* cabinet is internal, no checks needed */
868 if (mi->cabinet[0] == '#') return ERROR_SUCCESS;
870 if (!(cabinet_file = get_cabinet_filename( mi ))) return ERROR_OUTOFMEMORY;
872 /* package should be downloaded */
873 if (compressed && GetFileAttributesW( cabinet_file ) == INVALID_FILE_ATTRIBUTES &&
874 package->BaseURL && UrlIsW( package->BaseURL, URLIS_URL ))
876 WCHAR temppath[MAX_PATH], *p;
878 if ((rc = msi_download_file( cabinet_file, temppath )) != ERROR_SUCCESS)
880 ERR("failed to download %s (%u)\n", debugstr_w(cabinet_file), rc);
881 msi_free( cabinet_file );
882 return rc;
884 if ((p = strrchrW( temppath, '\\' ))) *p = 0;
885 strcpyW( mi->sourcedir, temppath );
886 PathAddBackslashW( mi->sourcedir );
887 msi_free( mi->cabinet );
888 mi->cabinet = strdupW( p + 1 );
889 msi_free( cabinet_file );
890 return ERROR_SUCCESS;
893 /* check volume matches, change media if not */
894 if (mi->volume_label && mi->disk_id > 1 && strcmpW( mi->first_volume, mi->volume_label ))
896 WCHAR *source = msi_dup_property( package->db, szSourceDir );
897 BOOL match = source_matches_volume( mi, source );
898 msi_free( source );
900 if (!match && (mi->type == DRIVE_CDROM || mi->type == DRIVE_REMOVABLE))
902 if ((rc = msi_change_media( package, mi )) != ERROR_SUCCESS)
904 msi_free( cabinet_file );
905 return rc;
909 if (mi->cabinet)
911 if (compressed && GetFileAttributesW( cabinet_file ) == INVALID_FILE_ATTRIBUTES)
913 if ((rc = find_published_source( package, mi )) != ERROR_SUCCESS)
915 ERR("cabinet not found: %s\n", debugstr_w(cabinet_file));
916 msi_free( cabinet_file );
917 return ERROR_INSTALL_FAILURE;
921 msi_free( cabinet_file );
922 return ERROR_SUCCESS;
925 UINT msi_add_cabinet_stream( MSIPACKAGE *package, UINT disk_id, IStorage *storage, const WCHAR *name )
927 MSICABINETSTREAM *cab, *item;
929 TRACE("%p, %u, %p, %s\n", package, disk_id, storage, debugstr_w(name));
931 LIST_FOR_EACH_ENTRY( item, &package->cabinet_streams, MSICABINETSTREAM, entry )
933 if (item->disk_id == disk_id)
935 TRACE("duplicate disk id %u\n", disk_id);
936 return ERROR_FUNCTION_FAILED;
939 if (!(cab = msi_alloc( sizeof(*cab) ))) return ERROR_OUTOFMEMORY;
940 if (!(cab->stream = msi_alloc( (strlenW( name ) + 1) * sizeof(WCHAR ) )))
942 msi_free( cab );
943 return ERROR_OUTOFMEMORY;
945 strcpyW( cab->stream, name );
946 cab->disk_id = disk_id;
947 cab->storage = storage;
948 IStorage_AddRef( storage );
949 list_add_tail( &package->cabinet_streams, &cab->entry );
951 return ERROR_SUCCESS;