msi: Fix memory leak in ready_media (scan-build).
[wine.git] / dlls / msi / media.c
blob50673f99cae4fef1fa85d2bd07bd5a10c2de784b
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 <fcntl.h>
22 #include <stdarg.h>
24 #define COBJMACROS
26 #include "windef.h"
27 #include "winerror.h"
28 #include "wine/debug.h"
29 #include "fdi.h"
30 #include "msipriv.h"
31 #include "winuser.h"
32 #include "winreg.h"
33 #include "shlwapi.h"
34 #include "objidl.h"
35 #include "resource.h"
37 WINE_DEFAULT_DEBUG_CHANNEL(msi);
39 static BOOL source_matches_volume(MSIMEDIAINFO *mi, LPCWSTR source_root)
41 WCHAR volume_name[MAX_PATH + 1], root[MAX_PATH + 1];
42 const WCHAR *p;
43 int len, len2;
45 lstrcpyW(root, source_root);
46 PathStripToRootW(root);
47 PathAddBackslashW(root);
49 if (!GetVolumeInformationW(root, volume_name, MAX_PATH + 1, NULL, NULL, NULL, NULL, 0))
51 WARN( "failed to get volume information for %s (%lu)\n", debugstr_w(root), GetLastError() );
52 return FALSE;
55 len = lstrlenW( volume_name );
56 len2 = lstrlenW( mi->volume_label );
57 if (len2 > len) return FALSE;
58 p = volume_name + len - len2;
60 return !wcsicmp( mi->volume_label, p );
63 static UINT change_media(MSIPACKAGE *package, MSIMEDIAINFO *mi)
65 MSIRECORD *record;
66 LPWSTR source_dir;
67 UINT r = IDRETRY;
69 source_dir = msi_dup_property(package->db, L"SourceDir");
70 record = MSI_CreateRecord(2);
72 while (r == IDRETRY && !source_matches_volume(mi, source_dir))
74 MSI_RecordSetStringW(record, 0, NULL);
75 MSI_RecordSetInteger(record, 1, MSIERR_CABNOTFOUND);
76 MSI_RecordSetStringW(record, 2, mi->disk_prompt);
77 r = MSI_ProcessMessage(package, INSTALLMESSAGE_ERROR | MB_RETRYCANCEL, record);
80 msiobj_release(&record->hdr);
81 free(source_dir);
83 return r == IDRETRY ? ERROR_SUCCESS : ERROR_INSTALL_SOURCE_ABSENT;
86 static MSICABINETSTREAM *get_cabinet_stream( MSIPACKAGE *package, UINT disk_id )
88 MSICABINETSTREAM *cab;
90 LIST_FOR_EACH_ENTRY( cab, &package->cabinet_streams, MSICABINETSTREAM, entry )
92 if (cab->disk_id == disk_id) return cab;
94 return NULL;
97 static void * CDECL cabinet_alloc(ULONG cb)
99 return malloc(cb);
102 static void CDECL cabinet_free(void *pv)
104 free(pv);
107 static INT_PTR CDECL cabinet_open(char *pszFile, int oflag, int pmode)
109 DWORD dwAccess = 0;
110 DWORD dwShareMode = 0;
111 DWORD dwCreateDisposition = OPEN_EXISTING;
113 switch (oflag & _O_ACCMODE)
115 case _O_RDONLY:
116 dwAccess = GENERIC_READ;
117 dwShareMode = FILE_SHARE_READ | FILE_SHARE_DELETE;
118 break;
119 case _O_WRONLY:
120 dwAccess = GENERIC_WRITE;
121 dwShareMode = FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE;
122 break;
123 case _O_RDWR:
124 dwAccess = GENERIC_READ | GENERIC_WRITE;
125 dwShareMode = FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE;
126 break;
129 if ((oflag & (_O_CREAT | _O_EXCL)) == (_O_CREAT | _O_EXCL))
130 dwCreateDisposition = CREATE_NEW;
131 else if (oflag & _O_CREAT)
132 dwCreateDisposition = CREATE_ALWAYS;
134 return (INT_PTR)CreateFileA(pszFile, dwAccess, dwShareMode, NULL,
135 dwCreateDisposition, 0, NULL);
138 static UINT CDECL cabinet_read(INT_PTR hf, void *pv, UINT cb)
140 HANDLE handle = (HANDLE)hf;
141 DWORD read;
143 if (ReadFile(handle, pv, cb, &read, NULL))
144 return read;
146 return 0;
149 static UINT CDECL cabinet_write(INT_PTR hf, void *pv, UINT cb)
151 HANDLE handle = (HANDLE)hf;
152 DWORD written;
154 if (WriteFile(handle, pv, cb, &written, NULL))
155 return written;
157 return 0;
160 static int CDECL cabinet_close(INT_PTR hf)
162 HANDLE handle = (HANDLE)hf;
163 return CloseHandle(handle) ? 0 : -1;
166 static LONG CDECL cabinet_seek(INT_PTR hf, LONG dist, int seektype)
168 HANDLE handle = (HANDLE)hf;
169 /* flags are compatible and so are passed straight through */
170 return SetFilePointer(handle, dist, NULL, seektype);
173 struct package_disk
175 MSIPACKAGE *package;
176 UINT id;
179 static struct package_disk package_disk;
181 static INT_PTR CDECL cabinet_open_stream( char *pszFile, int oflag, int pmode )
183 MSICABINETSTREAM *cab;
184 IStream *stream;
186 if (!(cab = get_cabinet_stream( package_disk.package, package_disk.id )))
188 WARN("failed to get cabinet stream\n");
189 return -1;
191 if (cab->storage == package_disk.package->db->storage)
193 UINT r = msi_get_stream( package_disk.package->db, cab->stream + 1, &stream );
194 if (r != ERROR_SUCCESS)
196 WARN("failed to get stream %u\n", r);
197 return -1;
200 else /* patch storage */
202 HRESULT hr;
203 WCHAR *encoded;
205 if (!(encoded = encode_streamname( FALSE, cab->stream + 1 )))
207 WARN("failed to encode stream name\n");
208 return -1;
210 hr = IStorage_OpenStream( cab->storage, encoded, NULL, STGM_READ|STGM_SHARE_EXCLUSIVE, 0, &stream );
211 free( encoded );
212 if (FAILED(hr))
214 WARN( "failed to open stream %#lx\n", hr );
215 return -1;
218 return (INT_PTR)stream;
221 static UINT CDECL cabinet_read_stream( INT_PTR hf, void *pv, UINT cb )
223 IStream *stm = (IStream *)hf;
224 DWORD read;
225 HRESULT hr;
227 hr = IStream_Read( stm, pv, cb, &read );
228 if (hr == S_OK || hr == S_FALSE)
229 return read;
231 return 0;
234 static int CDECL cabinet_close_stream( INT_PTR hf )
236 IStream *stm = (IStream *)hf;
237 IStream_Release( stm );
238 return 0;
241 static LONG CDECL cabinet_seek_stream( INT_PTR hf, LONG dist, int seektype )
243 IStream *stm = (IStream *)hf;
244 LARGE_INTEGER move;
245 ULARGE_INTEGER newpos;
246 HRESULT hr;
248 move.QuadPart = dist;
249 hr = IStream_Seek( stm, move, seektype, &newpos );
250 if (SUCCEEDED(hr))
252 if (newpos.QuadPart <= MAXLONG) return newpos.QuadPart;
253 ERR("Too big!\n");
255 return -1;
258 static UINT media_get_disk_info(MSIPACKAGE *package, MSIMEDIAINFO *mi)
260 MSIRECORD *row;
262 row = MSI_QueryGetRecord(package->db, L"SELECT * FROM `Media` WHERE `DiskId` = %d", mi->disk_id);
263 if (!row)
265 TRACE("Unable to query row\n");
266 return ERROR_FUNCTION_FAILED;
269 mi->disk_prompt = wcsdup(MSI_RecordGetString(row, 3));
270 mi->cabinet = wcsdup(MSI_RecordGetString(row, 4));
271 mi->volume_label = wcsdup(MSI_RecordGetString(row, 5));
273 msiobj_release(&row->hdr);
274 return ERROR_SUCCESS;
277 static INT_PTR cabinet_partial_file(FDINOTIFICATIONTYPE fdint,
278 PFDINOTIFICATION pfdin)
280 MSICABDATA *data = pfdin->pv;
281 data->mi->is_continuous = FALSE;
282 return 0;
285 static WCHAR *get_cabinet_filename(MSIMEDIAINFO *mi)
287 int len;
288 WCHAR *ret;
290 len = lstrlenW(mi->sourcedir) + lstrlenW(mi->cabinet) + 1;
291 if (!(ret = malloc(len * sizeof(WCHAR)))) return NULL;
292 lstrcpyW(ret, mi->sourcedir);
293 lstrcatW(ret, mi->cabinet);
294 return ret;
297 static INT_PTR cabinet_next_cabinet(FDINOTIFICATIONTYPE fdint,
298 PFDINOTIFICATION pfdin)
300 MSICABDATA *data = pfdin->pv;
301 MSIMEDIAINFO *mi = data->mi;
302 LPWSTR cabinet_file = NULL, cab = strdupAtoW(pfdin->psz1);
303 INT_PTR res = -1;
304 UINT rc;
306 free(mi->disk_prompt);
307 free(mi->cabinet);
308 free(mi->volume_label);
309 mi->disk_prompt = NULL;
310 mi->cabinet = NULL;
311 mi->volume_label = NULL;
313 mi->disk_id++;
314 mi->is_continuous = TRUE;
316 rc = media_get_disk_info(data->package, mi);
317 if (rc != ERROR_SUCCESS)
319 ERR("Failed to get next cabinet information: %d\n", rc);
320 goto done;
323 if (wcsicmp( mi->cabinet, cab ))
325 char *next_cab;
326 ULONG length;
328 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));
330 /* Use cabinet name from the media table */
331 next_cab = strdupWtoA(mi->cabinet);
332 /* Modify path to cabinet file with full filename (psz3 points to a 256 bytes buffer that can be modified contrary to psz1 and psz2) */
333 length = strlen(pfdin->psz3) + 1 + strlen(next_cab) + 1;
334 if (length > 256)
336 WARN( "cannot update next cabinet filename with a string size %lu > 256\n", length );
337 free(next_cab);
338 goto done;
340 else
342 strcat(pfdin->psz3, "\\");
343 strcat(pfdin->psz3, next_cab);
345 /* Path psz3 and cabinet psz1 are concatenated by FDI so just reset psz1 */
346 *pfdin->psz1 = 0;
347 free(next_cab);
350 if (!(cabinet_file = get_cabinet_filename(mi)))
351 goto done;
353 TRACE("Searching for %s\n", debugstr_w(cabinet_file));
355 res = 0;
356 if (GetFileAttributesW(cabinet_file) == INVALID_FILE_ATTRIBUTES)
358 if (change_media(data->package, mi) != ERROR_SUCCESS)
359 res = -1;
362 done:
363 free(cab);
364 free(cabinet_file);
365 return res;
368 static INT_PTR cabinet_next_cabinet_stream( FDINOTIFICATIONTYPE fdint,
369 PFDINOTIFICATION pfdin )
371 MSICABDATA *data = pfdin->pv;
372 MSIMEDIAINFO *mi = data->mi;
373 UINT rc;
375 free( mi->disk_prompt );
376 free( mi->cabinet );
377 free( mi->volume_label );
378 mi->disk_prompt = NULL;
379 mi->cabinet = NULL;
380 mi->volume_label = NULL;
382 mi->disk_id++;
383 mi->is_continuous = TRUE;
385 rc = media_get_disk_info( data->package, mi );
386 if (rc != ERROR_SUCCESS)
388 ERR("Failed to get next cabinet information: %u\n", rc);
389 return -1;
391 package_disk.id = mi->disk_id;
393 TRACE("next cabinet is %s disk id %u\n", debugstr_w(mi->cabinet), mi->disk_id);
394 return 0;
397 static INT_PTR cabinet_copy_file(FDINOTIFICATIONTYPE fdint,
398 PFDINOTIFICATION pfdin)
400 MSICABDATA *data = pfdin->pv;
401 HANDLE handle = 0;
402 LPWSTR path = NULL;
403 DWORD attrs;
405 data->curfile = strdupAtoW(pfdin->psz1);
406 if (!data->cb(data->package, data->curfile, MSICABEXTRACT_BEGINEXTRACT, &path,
407 &attrs, data->user))
409 /* We're not extracting this file, so free the filename. */
410 free(data->curfile);
411 data->curfile = NULL;
412 goto done;
415 TRACE("extracting %s -> %s\n", debugstr_w(data->curfile), debugstr_w(path));
417 attrs = attrs & (FILE_ATTRIBUTE_READONLY|FILE_ATTRIBUTE_HIDDEN|FILE_ATTRIBUTE_SYSTEM);
418 if (!attrs) attrs = FILE_ATTRIBUTE_NORMAL;
420 handle = msi_create_file( data->package, path, GENERIC_READ | GENERIC_WRITE, 0, CREATE_ALWAYS, attrs );
421 if (handle == INVALID_HANDLE_VALUE)
423 DWORD err = GetLastError();
424 DWORD attrs2 = msi_get_file_attributes( data->package, path );
426 if (attrs2 == INVALID_FILE_ATTRIBUTES)
428 ERR( "failed to create %s (error %lu)\n", debugstr_w(path), err );
429 goto done;
431 else if (err == ERROR_ACCESS_DENIED && (attrs2 & FILE_ATTRIBUTE_READONLY))
433 TRACE("removing read-only attribute on %s\n", debugstr_w(path));
434 msi_set_file_attributes( data->package, path, attrs2 & ~FILE_ATTRIBUTE_READONLY );
435 handle = msi_create_file( data->package, path, GENERIC_READ | GENERIC_WRITE, 0, CREATE_ALWAYS, attrs );
437 if (handle != INVALID_HANDLE_VALUE) goto done;
438 err = GetLastError();
440 if (err == ERROR_SHARING_VIOLATION || err == ERROR_USER_MAPPED_FILE)
442 WCHAR *tmpfileW, *tmppathW, *p;
443 DWORD len;
445 TRACE("file in use, scheduling rename operation\n");
447 if (!(tmppathW = wcsdup(path))) return ERROR_OUTOFMEMORY;
448 if ((p = wcsrchr(tmppathW, '\\'))) *p = 0;
449 len = lstrlenW( tmppathW ) + 16;
450 if (!(tmpfileW = malloc(len * sizeof(WCHAR))))
452 free( tmppathW );
453 return ERROR_OUTOFMEMORY;
455 if (!msi_get_temp_file_name( data->package, tmppathW, L"msi", tmpfileW )) tmpfileW[0] = 0;
456 free( tmppathW );
458 handle = msi_create_file( data->package, tmpfileW, GENERIC_READ | GENERIC_WRITE, 0, CREATE_ALWAYS, attrs );
460 if (handle != INVALID_HANDLE_VALUE &&
461 msi_move_file( data->package, path, NULL, MOVEFILE_DELAY_UNTIL_REBOOT ) &&
462 msi_move_file( data->package, tmpfileW, path, MOVEFILE_DELAY_UNTIL_REBOOT ))
464 data->package->need_reboot_at_end = 1;
466 else
468 WARN( "failed to schedule rename operation %s (error %lu)\n", debugstr_w(path), GetLastError() );
469 msi_delete_file( data->package, tmpfileW );
471 free(tmpfileW);
473 else WARN( "failed to create %s (error %lu)\n", debugstr_w(path), err );
476 done:
477 free(path);
479 return (INT_PTR)handle;
482 static INT_PTR cabinet_close_file_info(FDINOTIFICATIONTYPE fdint,
483 PFDINOTIFICATION pfdin)
485 MSICABDATA *data = pfdin->pv;
486 FILETIME ft;
487 FILETIME ftLocal;
488 HANDLE handle = (HANDLE)pfdin->hf;
490 data->mi->is_continuous = FALSE;
492 if (!DosDateTimeToFileTime(pfdin->date, pfdin->time, &ft))
494 CloseHandle(handle);
495 return -1;
497 if (!LocalFileTimeToFileTime(&ft, &ftLocal))
499 CloseHandle(handle);
500 return -1;
502 if (!SetFileTime(handle, &ftLocal, 0, &ftLocal))
504 CloseHandle(handle);
505 return -1;
508 CloseHandle(handle);
509 data->cb(data->package, data->curfile, MSICABEXTRACT_FILEEXTRACTED, NULL, NULL, data->user);
511 free(data->curfile);
512 data->curfile = NULL;
514 return 1;
517 static INT_PTR CDECL cabinet_notify(FDINOTIFICATIONTYPE fdint, PFDINOTIFICATION pfdin)
519 switch (fdint)
521 case fdintPARTIAL_FILE:
522 return cabinet_partial_file(fdint, pfdin);
524 case fdintNEXT_CABINET:
525 return cabinet_next_cabinet(fdint, pfdin);
527 case fdintCOPY_FILE:
528 return cabinet_copy_file(fdint, pfdin);
530 case fdintCLOSE_FILE_INFO:
531 return cabinet_close_file_info(fdint, pfdin);
533 default:
534 return 0;
538 static INT_PTR CDECL cabinet_notify_stream( FDINOTIFICATIONTYPE fdint, PFDINOTIFICATION pfdin )
540 switch (fdint)
542 case fdintPARTIAL_FILE:
543 return cabinet_partial_file( fdint, pfdin );
545 case fdintNEXT_CABINET:
546 return cabinet_next_cabinet_stream( fdint, pfdin );
548 case fdintCOPY_FILE:
549 return cabinet_copy_file( fdint, pfdin );
551 case fdintCLOSE_FILE_INFO:
552 return cabinet_close_file_info( fdint, pfdin );
554 case fdintCABINET_INFO:
555 return 0;
557 default:
558 ERR("Unexpected notification %d\n", fdint);
559 return 0;
563 static BOOL extract_cabinet( MSIPACKAGE* package, MSIMEDIAINFO *mi, LPVOID data )
565 LPSTR cabinet, cab_path = NULL;
566 HFDI hfdi;
567 ERF erf;
568 BOOL ret = FALSE;
570 TRACE("extracting %s disk id %u\n", debugstr_w(mi->cabinet), mi->disk_id);
572 hfdi = FDICreate( cabinet_alloc, cabinet_free, cabinet_open, cabinet_read,
573 cabinet_write, cabinet_close, cabinet_seek, 0, &erf );
574 if (!hfdi)
576 ERR("FDICreate failed\n");
577 return FALSE;
580 cabinet = strdupWtoA( mi->cabinet );
581 if (!cabinet)
582 goto done;
584 cab_path = strdupWtoA( mi->sourcedir );
585 if (!cab_path)
586 goto done;
588 ret = FDICopy( hfdi, cabinet, cab_path, 0, cabinet_notify, NULL, data );
589 if (!ret)
590 ERR("FDICopy failed\n");
592 done:
593 FDIDestroy( hfdi );
594 free( cabinet );
595 free( cab_path );
597 if (ret)
598 mi->is_extracted = TRUE;
600 return ret;
603 static BOOL extract_cabinet_stream( MSIPACKAGE *package, MSIMEDIAINFO *mi, LPVOID data )
605 static char filename[] = {'<','S','T','R','E','A','M','>',0};
606 HFDI hfdi;
607 ERF erf;
608 BOOL ret = FALSE;
610 TRACE("extracting %s disk id %u\n", debugstr_w(mi->cabinet), mi->disk_id);
612 hfdi = FDICreate( cabinet_alloc, cabinet_free, cabinet_open_stream, cabinet_read_stream,
613 cabinet_write, cabinet_close_stream, cabinet_seek_stream, 0, &erf );
614 if (!hfdi)
616 ERR("FDICreate failed\n");
617 return FALSE;
620 package_disk.package = package;
621 package_disk.id = mi->disk_id;
623 ret = FDICopy( hfdi, filename, NULL, 0, cabinet_notify_stream, NULL, data );
624 if (!ret) ERR("FDICopy failed\n");
626 FDIDestroy( hfdi );
627 if (ret) mi->is_extracted = TRUE;
628 return ret;
631 /***********************************************************************
632 * msi_cabextract
634 * Extract files from a cabinet file or stream.
636 BOOL msi_cabextract(MSIPACKAGE* package, MSIMEDIAINFO *mi, LPVOID data)
638 if (mi->cabinet[0] == '#')
640 return extract_cabinet_stream( package, mi, data );
642 return extract_cabinet( package, mi, data );
645 void msi_free_media_info(MSIMEDIAINFO *mi)
647 free(mi->disk_prompt);
648 free(mi->cabinet);
649 free(mi->volume_label);
650 free(mi->last_volume);
651 free(mi);
654 static UINT get_drive_type(const WCHAR *path)
656 WCHAR root[MAX_PATH + 1];
658 lstrcpyW(root, path);
659 PathStripToRootW(root);
660 PathAddBackslashW(root);
662 return GetDriveTypeW(root);
665 static WCHAR *get_base_url( MSIDATABASE *db )
667 WCHAR *p, *ret = NULL, *orig_db = msi_dup_property( db, L"OriginalDatabase" );
668 if (UrlIsW( orig_db, URLIS_URL ) && (ret = wcsdup( orig_db )) && (p = wcsrchr( ret, '/' ))) p[1] = 0;
669 free( orig_db );
670 return ret;
673 UINT msi_load_media_info(MSIPACKAGE *package, UINT Sequence, MSIMEDIAINFO *mi)
675 MSIRECORD *row;
676 WCHAR *source_dir, *source, *base_url = NULL;
677 DWORD options;
679 if (Sequence <= mi->last_sequence) /* already loaded */
680 return ERROR_SUCCESS;
682 row = MSI_QueryGetRecord(package->db, L"SELECT * FROM `Media` WHERE `LastSequence` >= %d ORDER BY `DiskId`", Sequence);
683 if (!row)
685 TRACE("Unable to query row\n");
686 return ERROR_FUNCTION_FAILED;
689 mi->is_extracted = FALSE;
690 mi->disk_id = MSI_RecordGetInteger(row, 1);
691 mi->last_sequence = MSI_RecordGetInteger(row, 2);
692 free(mi->disk_prompt);
693 mi->disk_prompt = wcsdup(MSI_RecordGetString(row, 3));
694 free(mi->cabinet);
695 mi->cabinet = wcsdup(MSI_RecordGetString(row, 4));
696 free(mi->volume_label);
697 mi->volume_label = wcsdup(MSI_RecordGetString(row, 5));
698 msiobj_release(&row->hdr);
700 msi_set_sourcedir_props(package, FALSE);
701 source_dir = msi_dup_property(package->db, L"SourceDir");
702 lstrcpyW(mi->sourcedir, source_dir);
703 PathAddBackslashW(mi->sourcedir);
704 mi->type = get_drive_type(source_dir);
706 options = MSICODE_PRODUCT;
707 if (mi->type == DRIVE_CDROM || mi->type == DRIVE_REMOVABLE)
709 source = source_dir;
710 options |= MSISOURCETYPE_MEDIA;
712 else if ((base_url = get_base_url(package->db)))
714 source = base_url;
715 options |= MSISOURCETYPE_URL;
717 else
719 source = mi->sourcedir;
720 options |= MSISOURCETYPE_NETWORK;
723 msi_package_add_media_disk(package, package->Context,
724 MSICODE_PRODUCT, mi->disk_id,
725 mi->volume_label, mi->disk_prompt);
727 msi_package_add_info(package, package->Context,
728 options, INSTALLPROPERTY_LASTUSEDSOURCEW, source);
730 TRACE("sequence %u -> cabinet %s disk id %u\n", Sequence, debugstr_w(mi->cabinet), mi->disk_id);
732 free(base_url);
733 free(source_dir);
734 return ERROR_SUCCESS;
737 /* FIXME: search URL sources as well */
738 static UINT find_published_source(MSIPACKAGE *package, MSIMEDIAINFO *mi)
740 WCHAR source[MAX_PATH];
741 WCHAR volume[MAX_PATH];
742 WCHAR prompt[MAX_PATH];
743 DWORD volumesz, promptsz;
744 DWORD index, size, id;
745 WCHAR last_type[2];
746 UINT r;
748 size = 2;
749 r = MsiSourceListGetInfoW(package->ProductCode, NULL,
750 package->Context, MSICODE_PRODUCT,
751 INSTALLPROPERTY_LASTUSEDTYPEW, last_type, &size);
752 if (r != ERROR_SUCCESS)
753 return r;
755 size = MAX_PATH;
756 r = MsiSourceListGetInfoW(package->ProductCode, NULL,
757 package->Context, MSICODE_PRODUCT,
758 INSTALLPROPERTY_LASTUSEDSOURCEW, source, &size);
759 if (r != ERROR_SUCCESS)
760 return r;
762 if (last_type[0] == 'n')
764 WCHAR cabinet_file[MAX_PATH];
765 BOOL check_all = FALSE;
767 while(TRUE)
769 index = 0;
770 volumesz = MAX_PATH;
771 while (MsiSourceListEnumSourcesW(package->ProductCode, NULL,
772 package->Context,
773 MSISOURCETYPE_NETWORK, index++,
774 volume, &volumesz) == ERROR_SUCCESS)
776 if (check_all || !wcsnicmp(source, volume, lstrlenW(source)))
778 lstrcpyW(cabinet_file, volume);
779 PathAddBackslashW(cabinet_file);
780 lstrcatW(cabinet_file, mi->cabinet);
782 if (GetFileAttributesW(cabinet_file) == INVALID_FILE_ATTRIBUTES)
784 volumesz = MAX_PATH;
785 if(!check_all)
786 break;
787 continue;
790 lstrcpyW(mi->sourcedir, volume);
791 PathAddBackslashW(mi->sourcedir);
792 TRACE("Found network source %s\n", debugstr_w(mi->sourcedir));
793 return ERROR_SUCCESS;
797 if (!check_all)
798 check_all = TRUE;
799 else
800 break;
804 index = 0;
805 volumesz = MAX_PATH;
806 promptsz = MAX_PATH;
807 while (MsiSourceListEnumMediaDisksW(package->ProductCode, NULL,
808 package->Context,
809 MSICODE_PRODUCT, index++, &id,
810 volume, &volumesz, prompt, &promptsz) == ERROR_SUCCESS)
812 mi->disk_id = id;
813 free( mi->volume_label );
814 if (!(mi->volume_label = malloc( ++volumesz * sizeof(WCHAR) ))) return ERROR_OUTOFMEMORY;
815 lstrcpyW( mi->volume_label, volume );
817 free( mi->disk_prompt );
818 if (!(mi->disk_prompt = malloc( ++promptsz * sizeof(WCHAR) ))) return ERROR_OUTOFMEMORY;
819 lstrcpyW( mi->disk_prompt, prompt );
821 if (source_matches_volume(mi, source))
823 /* FIXME: what about SourceDir */
824 lstrcpyW(mi->sourcedir, source);
825 PathAddBackslashW(mi->sourcedir);
826 TRACE("Found disk source %s\n", debugstr_w(mi->sourcedir));
827 return ERROR_SUCCESS;
831 return ERROR_FUNCTION_FAILED;
834 UINT ready_media( MSIPACKAGE *package, BOOL compressed, MSIMEDIAINFO *mi )
836 UINT rc;
837 WCHAR *cabinet_file = NULL;
839 /* media info for continuous cabinet is already loaded */
840 if (mi->is_continuous) return ERROR_SUCCESS;
842 if (mi->cabinet)
844 WCHAR *base_url;
846 /* cabinet is internal, no checks needed */
847 if (mi->cabinet[0] == '#') return ERROR_SUCCESS;
849 if (!(cabinet_file = get_cabinet_filename( mi ))) return ERROR_OUTOFMEMORY;
851 /* package should be downloaded */
852 if (compressed && GetFileAttributesW( cabinet_file ) == INVALID_FILE_ATTRIBUTES &&
853 (base_url = get_base_url( package->db )))
855 WCHAR temppath[MAX_PATH], *p, *url;
857 free( cabinet_file );
858 if (!(url = realloc( base_url, (wcslen( base_url ) + wcslen( mi->cabinet ) + 1) * sizeof(WCHAR) )))
860 free( base_url );
861 return ERROR_OUTOFMEMORY;
863 lstrcatW( url, mi->cabinet );
864 if ((rc = msi_download_file( url, temppath )) != ERROR_SUCCESS)
866 ERR("failed to download %s (%u)\n", debugstr_w(url), rc);
867 free( url );
868 return rc;
870 if ((p = wcsrchr( temppath, '\\' ))) *p = 0;
871 lstrcpyW( mi->sourcedir, temppath );
872 PathAddBackslashW( mi->sourcedir );
873 free( mi->cabinet );
874 mi->cabinet = wcsdup( p + 1 );
876 free( url );
877 return ERROR_SUCCESS;
880 /* check volume matches, change media if not */
881 if (mi->volume_label)
883 /* assume first volume is in the drive */
884 if (mi->last_volume && wcsicmp( mi->last_volume, mi->volume_label ))
886 WCHAR *source = msi_dup_property( package->db, L"SourceDir" );
887 BOOL match = source_matches_volume( mi, source );
888 free( source );
890 if (!match && (mi->type == DRIVE_CDROM || mi->type == DRIVE_REMOVABLE))
892 if ((rc = change_media( package, mi )) != ERROR_SUCCESS)
894 free( cabinet_file );
895 return rc;
900 free(mi->last_volume);
901 mi->last_volume = wcsdup(mi->volume_label);
903 if (mi->cabinet)
905 if (compressed && GetFileAttributesW( cabinet_file ) == INVALID_FILE_ATTRIBUTES)
907 if ((rc = find_published_source( package, mi )) != ERROR_SUCCESS)
909 ERR("cabinet not found: %s\n", debugstr_w(cabinet_file));
910 free( cabinet_file );
911 return ERROR_INSTALL_FAILURE;
915 free( cabinet_file );
916 return ERROR_SUCCESS;
919 UINT msi_add_cabinet_stream( MSIPACKAGE *package, UINT disk_id, IStorage *storage, const WCHAR *name )
921 MSICABINETSTREAM *cab, *item;
923 TRACE("%p, %u, %p, %s\n", package, disk_id, storage, debugstr_w(name));
925 LIST_FOR_EACH_ENTRY( item, &package->cabinet_streams, MSICABINETSTREAM, entry )
927 if (item->disk_id == disk_id)
929 TRACE("duplicate disk id %u\n", disk_id);
930 return ERROR_FUNCTION_FAILED;
933 if (!(cab = malloc( sizeof(*cab) ))) return ERROR_OUTOFMEMORY;
934 if (!(cab->stream = malloc( (wcslen( name ) + 1) * sizeof(WCHAR) )))
936 free( cab );
937 return ERROR_OUTOFMEMORY;
939 lstrcpyW( cab->stream, name );
940 cab->disk_id = disk_id;
941 cab->storage = storage;
942 IStorage_AddRef( storage );
943 list_add_tail( &package->cabinet_streams, &cab->entry );
945 return ERROR_SUCCESS;