mstask: Implement ITask::DeleteTrigger().
[wine.git] / dlls / msi / media.c
blob3d4a8c09eb6e1e579dfaece543c2fdbd2329b6c1
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"
35 #include "resource.h"
37 WINE_DEFAULT_DEBUG_CHANNEL(msi);
39 /* from msvcrt/fcntl.h */
40 #define _O_RDONLY 0
41 #define _O_WRONLY 1
42 #define _O_RDWR 2
43 #define _O_ACCMODE (_O_RDONLY|_O_WRONLY|_O_RDWR)
44 #define _O_APPEND 0x0008
45 #define _O_RANDOM 0x0010
46 #define _O_SEQUENTIAL 0x0020
47 #define _O_TEMPORARY 0x0040
48 #define _O_NOINHERIT 0x0080
49 #define _O_CREAT 0x0100
50 #define _O_TRUNC 0x0200
51 #define _O_EXCL 0x0400
52 #define _O_SHORT_LIVED 0x1000
53 #define _O_TEXT 0x4000
54 #define _O_BINARY 0x8000
56 static BOOL source_matches_volume(MSIMEDIAINFO *mi, LPCWSTR source_root)
58 WCHAR volume_name[MAX_PATH + 1], root[MAX_PATH + 1];
59 const WCHAR *p;
60 int len, len2;
62 strcpyW(root, source_root);
63 PathStripToRootW(root);
64 PathAddBackslashW(root);
66 if (!GetVolumeInformationW(root, volume_name, MAX_PATH + 1, NULL, NULL, NULL, NULL, 0))
68 WARN("failed to get volume information for %s (%u)\n", debugstr_w(root), GetLastError());
69 return FALSE;
72 len = strlenW( volume_name );
73 len2 = strlenW( mi->volume_label );
74 if (len2 > len) return FALSE;
75 p = volume_name + len - len2;
77 return !strcmpiW( mi->volume_label, p );
80 static UINT msi_change_media(MSIPACKAGE *package, MSIMEDIAINFO *mi)
82 MSIRECORD *record;
83 LPWSTR source_dir;
84 UINT r = IDRETRY;
86 source_dir = msi_dup_property(package->db, szSourceDir);
87 record = MSI_CreateRecord(2);
89 while (r == IDRETRY && !source_matches_volume(mi, source_dir))
91 MSI_RecordSetStringW(record, 0, NULL);
92 MSI_RecordSetInteger(record, 1, MSIERR_CABNOTFOUND);
93 MSI_RecordSetStringW(record, 2, mi->disk_prompt);
94 r = MSI_ProcessMessage(package, INSTALLMESSAGE_ERROR | MB_RETRYCANCEL, record);
97 msiobj_release(&record->hdr);
98 msi_free(source_dir);
100 return r;
103 static MSICABINETSTREAM *msi_get_cabinet_stream( MSIPACKAGE *package, UINT disk_id )
105 MSICABINETSTREAM *cab;
107 LIST_FOR_EACH_ENTRY( cab, &package->cabinet_streams, MSICABINETSTREAM, entry )
109 if (cab->disk_id == disk_id) return cab;
111 return NULL;
114 static void * CDECL cabinet_alloc(ULONG cb)
116 return msi_alloc(cb);
119 static void CDECL cabinet_free(void *pv)
121 msi_free(pv);
124 static INT_PTR CDECL cabinet_open(char *pszFile, int oflag, int pmode)
126 DWORD dwAccess = 0;
127 DWORD dwShareMode = 0;
128 DWORD dwCreateDisposition = OPEN_EXISTING;
130 switch (oflag & _O_ACCMODE)
132 case _O_RDONLY:
133 dwAccess = GENERIC_READ;
134 dwShareMode = FILE_SHARE_READ | FILE_SHARE_DELETE;
135 break;
136 case _O_WRONLY:
137 dwAccess = GENERIC_WRITE;
138 dwShareMode = FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE;
139 break;
140 case _O_RDWR:
141 dwAccess = GENERIC_READ | GENERIC_WRITE;
142 dwShareMode = FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE;
143 break;
146 if ((oflag & (_O_CREAT | _O_EXCL)) == (_O_CREAT | _O_EXCL))
147 dwCreateDisposition = CREATE_NEW;
148 else if (oflag & _O_CREAT)
149 dwCreateDisposition = CREATE_ALWAYS;
151 return (INT_PTR)CreateFileA(pszFile, dwAccess, dwShareMode, NULL,
152 dwCreateDisposition, 0, NULL);
155 static UINT CDECL cabinet_read(INT_PTR hf, void *pv, UINT cb)
157 HANDLE handle = (HANDLE)hf;
158 DWORD read;
160 if (ReadFile(handle, pv, cb, &read, NULL))
161 return read;
163 return 0;
166 static UINT CDECL cabinet_write(INT_PTR hf, void *pv, UINT cb)
168 HANDLE handle = (HANDLE)hf;
169 DWORD written;
171 if (WriteFile(handle, pv, cb, &written, NULL))
172 return written;
174 return 0;
177 static int CDECL cabinet_close(INT_PTR hf)
179 HANDLE handle = (HANDLE)hf;
180 return CloseHandle(handle) ? 0 : -1;
183 static LONG CDECL cabinet_seek(INT_PTR hf, LONG dist, int seektype)
185 HANDLE handle = (HANDLE)hf;
186 /* flags are compatible and so are passed straight through */
187 return SetFilePointer(handle, dist, NULL, seektype);
190 struct package_disk
192 MSIPACKAGE *package;
193 UINT id;
196 static struct package_disk package_disk;
198 static INT_PTR CDECL cabinet_open_stream( char *pszFile, int oflag, int pmode )
200 MSICABINETSTREAM *cab;
201 IStream *stream;
203 if (!(cab = msi_get_cabinet_stream( package_disk.package, package_disk.id )))
205 WARN("failed to get cabinet stream\n");
206 return -1;
208 if (cab->storage == package_disk.package->db->storage)
210 UINT r = msi_get_stream( package_disk.package->db, cab->stream + 1, &stream );
211 if (r != ERROR_SUCCESS)
213 WARN("failed to get stream %u\n", r);
214 return -1;
217 else /* patch storage */
219 HRESULT hr;
220 WCHAR *encoded;
222 if (!(encoded = encode_streamname( FALSE, cab->stream + 1 )))
224 WARN("failed to encode stream name\n");
225 return -1;
227 hr = IStorage_OpenStream( cab->storage, encoded, NULL, STGM_READ|STGM_SHARE_EXCLUSIVE, 0, &stream );
228 msi_free( encoded );
229 if (FAILED(hr))
231 WARN("failed to open stream 0x%08x\n", hr);
232 return -1;
235 return (INT_PTR)stream;
238 static UINT CDECL cabinet_read_stream( INT_PTR hf, void *pv, UINT cb )
240 IStream *stm = (IStream *)hf;
241 DWORD read;
242 HRESULT hr;
244 hr = IStream_Read( stm, pv, cb, &read );
245 if (hr == S_OK || hr == S_FALSE)
246 return read;
248 return 0;
251 static int CDECL cabinet_close_stream( INT_PTR hf )
253 IStream *stm = (IStream *)hf;
254 IStream_Release( stm );
255 return 0;
258 static LONG CDECL cabinet_seek_stream( INT_PTR hf, LONG dist, int seektype )
260 IStream *stm = (IStream *)hf;
261 LARGE_INTEGER move;
262 ULARGE_INTEGER newpos;
263 HRESULT hr;
265 move.QuadPart = dist;
266 hr = IStream_Seek( stm, move, seektype, &newpos );
267 if (SUCCEEDED(hr))
269 if (newpos.QuadPart <= MAXLONG) return newpos.QuadPart;
270 ERR("Too big!\n");
272 return -1;
275 static UINT CDECL msi_media_get_disk_info(MSIPACKAGE *package, MSIMEDIAINFO *mi)
277 MSIRECORD *row;
279 static const WCHAR query[] = {
280 'S','E','L','E','C','T',' ','*',' ', 'F','R','O','M',' ',
281 '`','M','e','d','i','a','`',' ','W','H','E','R','E',' ',
282 '`','D','i','s','k','I','d','`',' ','=',' ','%','i',0};
284 row = MSI_QueryGetRecord(package->db, query, mi->disk_id);
285 if (!row)
287 TRACE("Unable to query row\n");
288 return ERROR_FUNCTION_FAILED;
291 mi->disk_prompt = strdupW(MSI_RecordGetString(row, 3));
292 mi->cabinet = strdupW(MSI_RecordGetString(row, 4));
293 mi->volume_label = strdupW(MSI_RecordGetString(row, 5));
295 msiobj_release(&row->hdr);
296 return ERROR_SUCCESS;
299 static INT_PTR cabinet_partial_file(FDINOTIFICATIONTYPE fdint,
300 PFDINOTIFICATION pfdin)
302 MSICABDATA *data = pfdin->pv;
303 data->mi->is_continuous = FALSE;
304 return 0;
307 static WCHAR *get_cabinet_filename(MSIMEDIAINFO *mi)
309 int len;
310 WCHAR *ret;
312 len = strlenW(mi->sourcedir) + strlenW(mi->cabinet) + 1;
313 if (!(ret = msi_alloc(len * sizeof(WCHAR)))) return NULL;
314 strcpyW(ret, mi->sourcedir);
315 strcatW(ret, mi->cabinet);
316 return ret;
319 static INT_PTR cabinet_next_cabinet(FDINOTIFICATIONTYPE fdint,
320 PFDINOTIFICATION pfdin)
322 MSICABDATA *data = pfdin->pv;
323 MSIMEDIAINFO *mi = data->mi;
324 LPWSTR cabinet_file = NULL, cab = strdupAtoW(pfdin->psz1);
325 INT_PTR res = -1;
326 UINT rc;
328 msi_free(mi->disk_prompt);
329 msi_free(mi->cabinet);
330 msi_free(mi->volume_label);
331 mi->disk_prompt = NULL;
332 mi->cabinet = NULL;
333 mi->volume_label = NULL;
335 mi->disk_id++;
336 mi->is_continuous = TRUE;
338 rc = msi_media_get_disk_info(data->package, mi);
339 if (rc != ERROR_SUCCESS)
341 ERR("Failed to get next cabinet information: %d\n", rc);
342 goto done;
345 if (strcmpiW( mi->cabinet, cab ))
347 char *next_cab;
348 ULONG length;
350 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));
352 /* Use cabinet name from the media table */
353 next_cab = strdupWtoA(mi->cabinet);
354 /* Modify path to cabinet file with full filename (psz3 points to a 256 bytes buffer that can be modified contrary to psz1 and psz2) */
355 length = strlen(pfdin->psz3) + 1 + strlen(next_cab) + 1;
356 if (length > 256)
358 WARN("Cannot update next cabinet filename with a string size %u > 256\n", length);
359 msi_free(next_cab);
360 goto done;
362 else
364 strcat(pfdin->psz3, "\\");
365 strcat(pfdin->psz3, next_cab);
367 /* Path psz3 and cabinet psz1 are concatenated by FDI so just reset psz1 */
368 *pfdin->psz1 = 0;
369 msi_free(next_cab);
372 if (!(cabinet_file = get_cabinet_filename(mi)))
373 goto done;
375 TRACE("Searching for %s\n", debugstr_w(cabinet_file));
377 res = 0;
378 if (GetFileAttributesW(cabinet_file) == INVALID_FILE_ATTRIBUTES)
380 if (msi_change_media(data->package, mi) != ERROR_SUCCESS)
381 res = -1;
384 done:
385 msi_free(cab);
386 msi_free(cabinet_file);
387 return res;
390 static INT_PTR cabinet_next_cabinet_stream( FDINOTIFICATIONTYPE fdint,
391 PFDINOTIFICATION pfdin )
393 MSICABDATA *data = pfdin->pv;
394 MSIMEDIAINFO *mi = data->mi;
395 UINT rc;
397 msi_free( mi->disk_prompt );
398 msi_free( mi->cabinet );
399 msi_free( mi->volume_label );
400 mi->disk_prompt = NULL;
401 mi->cabinet = NULL;
402 mi->volume_label = NULL;
404 mi->disk_id++;
405 mi->is_continuous = TRUE;
407 rc = msi_media_get_disk_info( data->package, mi );
408 if (rc != ERROR_SUCCESS)
410 ERR("Failed to get next cabinet information: %u\n", rc);
411 return -1;
413 package_disk.id = mi->disk_id;
415 TRACE("next cabinet is %s disk id %u\n", debugstr_w(mi->cabinet), mi->disk_id);
416 return 0;
419 static INT_PTR cabinet_copy_file(FDINOTIFICATIONTYPE fdint,
420 PFDINOTIFICATION pfdin)
422 MSICABDATA *data = pfdin->pv;
423 HANDLE handle = 0;
424 LPWSTR path = NULL;
425 DWORD attrs;
427 data->curfile = strdupAtoW(pfdin->psz1);
428 if (!data->cb(data->package, data->curfile, MSICABEXTRACT_BEGINEXTRACT, &path,
429 &attrs, data->user))
431 /* We're not extracting this file, so free the filename. */
432 msi_free(data->curfile);
433 data->curfile = NULL;
434 goto done;
437 TRACE("extracting %s -> %s\n", debugstr_w(data->curfile), debugstr_w(path));
439 attrs = attrs & (FILE_ATTRIBUTE_READONLY|FILE_ATTRIBUTE_HIDDEN|FILE_ATTRIBUTE_SYSTEM);
440 if (!attrs) attrs = FILE_ATTRIBUTE_NORMAL;
442 handle = CreateFileW(path, GENERIC_READ | GENERIC_WRITE, 0,
443 NULL, CREATE_ALWAYS, attrs, NULL);
444 if (handle == INVALID_HANDLE_VALUE)
446 DWORD err = GetLastError();
447 DWORD attrs2 = GetFileAttributesW(path);
449 if (attrs2 == INVALID_FILE_ATTRIBUTES)
451 ERR("failed to create %s (error %d)\n", debugstr_w(path), err);
452 goto done;
454 else if (err == ERROR_ACCESS_DENIED && (attrs2 & FILE_ATTRIBUTE_READONLY))
456 TRACE("removing read-only attribute on %s\n", debugstr_w(path));
457 SetFileAttributesW( path, attrs2 & ~FILE_ATTRIBUTE_READONLY );
458 handle = CreateFileW(path, GENERIC_READ | GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, attrs, NULL);
460 if (handle != INVALID_HANDLE_VALUE) goto done;
461 err = GetLastError();
463 if (err == ERROR_SHARING_VIOLATION || err == ERROR_USER_MAPPED_FILE)
465 WCHAR *tmpfileW, *tmppathW, *p;
466 DWORD len;
468 TRACE("file in use, scheduling rename operation\n");
470 if (!(tmppathW = strdupW( path ))) return ERROR_OUTOFMEMORY;
471 if ((p = strrchrW(tmppathW, '\\'))) *p = 0;
472 len = strlenW( tmppathW ) + 16;
473 if (!(tmpfileW = msi_alloc(len * sizeof(WCHAR))))
475 msi_free( tmppathW );
476 return ERROR_OUTOFMEMORY;
478 if (!GetTempFileNameW(tmppathW, szMsi, 0, tmpfileW)) tmpfileW[0] = 0;
479 msi_free( tmppathW );
481 handle = CreateFileW(tmpfileW, GENERIC_READ | GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, attrs, NULL);
483 if (handle != INVALID_HANDLE_VALUE &&
484 MoveFileExW(path, NULL, MOVEFILE_DELAY_UNTIL_REBOOT) &&
485 MoveFileExW(tmpfileW, path, MOVEFILE_DELAY_UNTIL_REBOOT))
487 data->package->need_reboot_at_end = 1;
489 else
491 WARN("failed to schedule rename operation %s (error %d)\n", debugstr_w(path), GetLastError());
492 DeleteFileW( tmpfileW );
494 msi_free(tmpfileW);
496 else
497 WARN("failed to create %s (error %d)\n", debugstr_w(path), err);
500 done:
501 msi_free(path);
503 return (INT_PTR)handle;
506 static INT_PTR cabinet_close_file_info(FDINOTIFICATIONTYPE fdint,
507 PFDINOTIFICATION pfdin)
509 MSICABDATA *data = pfdin->pv;
510 FILETIME ft;
511 FILETIME ftLocal;
512 HANDLE handle = (HANDLE)pfdin->hf;
514 data->mi->is_continuous = FALSE;
516 if (!DosDateTimeToFileTime(pfdin->date, pfdin->time, &ft))
517 return -1;
518 if (!LocalFileTimeToFileTime(&ft, &ftLocal))
519 return -1;
520 if (!SetFileTime(handle, &ftLocal, 0, &ftLocal))
521 return -1;
523 CloseHandle(handle);
525 data->cb(data->package, data->curfile, MSICABEXTRACT_FILEEXTRACTED, NULL, NULL,
526 data->user);
528 msi_free(data->curfile);
529 data->curfile = NULL;
531 return 1;
534 static INT_PTR CDECL cabinet_notify(FDINOTIFICATIONTYPE fdint, PFDINOTIFICATION pfdin)
536 switch (fdint)
538 case fdintPARTIAL_FILE:
539 return cabinet_partial_file(fdint, pfdin);
541 case fdintNEXT_CABINET:
542 return cabinet_next_cabinet(fdint, pfdin);
544 case fdintCOPY_FILE:
545 return cabinet_copy_file(fdint, pfdin);
547 case fdintCLOSE_FILE_INFO:
548 return cabinet_close_file_info(fdint, pfdin);
550 default:
551 return 0;
555 static INT_PTR CDECL cabinet_notify_stream( FDINOTIFICATIONTYPE fdint, PFDINOTIFICATION pfdin )
557 switch (fdint)
559 case fdintPARTIAL_FILE:
560 return cabinet_partial_file( fdint, pfdin );
562 case fdintNEXT_CABINET:
563 return cabinet_next_cabinet_stream( fdint, pfdin );
565 case fdintCOPY_FILE:
566 return cabinet_copy_file( fdint, pfdin );
568 case fdintCLOSE_FILE_INFO:
569 return cabinet_close_file_info( fdint, pfdin );
571 case fdintCABINET_INFO:
572 return 0;
574 default:
575 ERR("Unexpected notification %d\n", fdint);
576 return 0;
580 static BOOL extract_cabinet( MSIPACKAGE* package, MSIMEDIAINFO *mi, LPVOID data )
582 LPSTR cabinet, cab_path = NULL;
583 HFDI hfdi;
584 ERF erf;
585 BOOL ret = FALSE;
587 TRACE("extracting %s disk id %u\n", debugstr_w(mi->cabinet), mi->disk_id);
589 hfdi = FDICreate( cabinet_alloc, cabinet_free, cabinet_open, cabinet_read,
590 cabinet_write, cabinet_close, cabinet_seek, 0, &erf );
591 if (!hfdi)
593 ERR("FDICreate failed\n");
594 return FALSE;
597 cabinet = strdupWtoA( mi->cabinet );
598 if (!cabinet)
599 goto done;
601 cab_path = strdupWtoA( mi->sourcedir );
602 if (!cab_path)
603 goto done;
605 ret = FDICopy( hfdi, cabinet, cab_path, 0, cabinet_notify, NULL, data );
606 if (!ret)
607 ERR("FDICopy failed\n");
609 done:
610 FDIDestroy( hfdi );
611 msi_free(cabinet );
612 msi_free( cab_path );
614 if (ret)
615 mi->is_extracted = TRUE;
617 return ret;
620 static BOOL extract_cabinet_stream( MSIPACKAGE *package, MSIMEDIAINFO *mi, LPVOID data )
622 static char filename[] = {'<','S','T','R','E','A','M','>',0};
623 HFDI hfdi;
624 ERF erf;
625 BOOL ret = FALSE;
627 TRACE("extracting %s disk id %u\n", debugstr_w(mi->cabinet), mi->disk_id);
629 hfdi = FDICreate( cabinet_alloc, cabinet_free, cabinet_open_stream, cabinet_read_stream,
630 cabinet_write, cabinet_close_stream, cabinet_seek_stream, 0, &erf );
631 if (!hfdi)
633 ERR("FDICreate failed\n");
634 return FALSE;
637 package_disk.package = package;
638 package_disk.id = mi->disk_id;
640 ret = FDICopy( hfdi, filename, NULL, 0, cabinet_notify_stream, NULL, data );
641 if (!ret) ERR("FDICopy failed\n");
643 FDIDestroy( hfdi );
644 if (ret) mi->is_extracted = TRUE;
645 return ret;
648 /***********************************************************************
649 * msi_cabextract
651 * Extract files from a cabinet file or stream.
653 BOOL msi_cabextract(MSIPACKAGE* package, MSIMEDIAINFO *mi, LPVOID data)
655 if (mi->cabinet[0] == '#')
657 return extract_cabinet_stream( package, mi, data );
659 return extract_cabinet( package, mi, data );
662 void msi_free_media_info(MSIMEDIAINFO *mi)
664 msi_free(mi->disk_prompt);
665 msi_free(mi->cabinet);
666 msi_free(mi->volume_label);
667 msi_free(mi);
670 static UINT get_drive_type(const WCHAR *path)
672 WCHAR root[MAX_PATH + 1];
674 strcpyW(root, path);
675 PathStripToRootW(root);
676 PathAddBackslashW(root);
678 return GetDriveTypeW(root);
681 static WCHAR *get_base_url( MSIDATABASE *db )
683 WCHAR *p, *ret = NULL, *orig_db = msi_dup_property( db, szOriginalDatabase );
684 if (UrlIsW( orig_db, URLIS_URL ) && (ret = strdupW( orig_db )) && (p = strrchrW( ret, '/'))) p[1] = 0;
685 msi_free( orig_db );
686 return ret;
689 UINT msi_load_media_info(MSIPACKAGE *package, UINT Sequence, MSIMEDIAINFO *mi)
691 static const WCHAR query[] = {
692 'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ','`','M','e','d','i','a','`',' ',
693 'W','H','E','R','E',' ','`','L','a','s','t','S','e','q','u','e','n','c','e','`',' ',
694 '>','=',' ','%','i',' ','O','R','D','E','R',' ','B','Y',' ','`','D','i','s','k','I','d','`',0};
695 MSIRECORD *row;
696 WCHAR *source_dir, *source, *base_url = NULL;
697 DWORD options;
699 if (Sequence <= mi->last_sequence) /* already loaded */
700 return ERROR_SUCCESS;
702 row = MSI_QueryGetRecord(package->db, query, Sequence);
703 if (!row)
705 TRACE("Unable to query row\n");
706 return ERROR_FUNCTION_FAILED;
709 mi->is_extracted = FALSE;
710 mi->disk_id = MSI_RecordGetInteger(row, 1);
711 mi->last_sequence = MSI_RecordGetInteger(row, 2);
712 msi_free(mi->disk_prompt);
713 mi->disk_prompt = strdupW(MSI_RecordGetString(row, 3));
714 msi_free(mi->cabinet);
715 mi->cabinet = strdupW(MSI_RecordGetString(row, 4));
716 msi_free(mi->volume_label);
717 mi->volume_label = strdupW(MSI_RecordGetString(row, 5));
718 msiobj_release(&row->hdr);
720 msi_set_sourcedir_props(package, FALSE);
721 source_dir = msi_dup_property(package->db, szSourceDir);
722 lstrcpyW(mi->sourcedir, source_dir);
723 PathAddBackslashW(mi->sourcedir);
724 mi->type = get_drive_type(source_dir);
726 options = MSICODE_PRODUCT;
727 if (mi->type == DRIVE_CDROM || mi->type == DRIVE_REMOVABLE)
729 source = source_dir;
730 options |= MSISOURCETYPE_MEDIA;
732 else if ((base_url = get_base_url(package->db)))
734 source = base_url;
735 options |= MSISOURCETYPE_URL;
737 else
739 source = mi->sourcedir;
740 options |= MSISOURCETYPE_NETWORK;
743 msi_package_add_media_disk(package, package->Context,
744 MSICODE_PRODUCT, mi->disk_id,
745 mi->volume_label, mi->disk_prompt);
747 msi_package_add_info(package, package->Context,
748 options, INSTALLPROPERTY_LASTUSEDSOURCEW, source);
750 TRACE("sequence %u -> cabinet %s disk id %u\n", Sequence, debugstr_w(mi->cabinet), mi->disk_id);
752 msi_free(base_url);
753 msi_free(source_dir);
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 WCHAR *base_url;
866 /* cabinet is internal, no checks needed */
867 if (mi->cabinet[0] == '#') return ERROR_SUCCESS;
869 if (!(cabinet_file = get_cabinet_filename( mi ))) return ERROR_OUTOFMEMORY;
871 /* package should be downloaded */
872 if (compressed && GetFileAttributesW( cabinet_file ) == INVALID_FILE_ATTRIBUTES &&
873 (base_url = get_base_url( package->db )))
875 WCHAR temppath[MAX_PATH], *p, *url;
877 msi_free( cabinet_file );
878 if (!(url = msi_alloc( (strlenW( base_url ) + strlenW( mi->cabinet ) + 1) * sizeof(WCHAR) )))
880 return ERROR_OUTOFMEMORY;
882 strcpyW( url, base_url );
883 strcatW( url, mi->cabinet );
884 if ((rc = msi_download_file( url, temppath )) != ERROR_SUCCESS)
886 ERR("failed to download %s (%u)\n", debugstr_w(url), rc);
887 msi_free( url );
888 return rc;
890 if ((p = strrchrW( temppath, '\\' ))) *p = 0;
891 strcpyW( mi->sourcedir, temppath );
892 PathAddBackslashW( mi->sourcedir );
893 msi_free( mi->cabinet );
894 mi->cabinet = strdupW( p + 1 );
896 msi_free( url );
897 return ERROR_SUCCESS;
900 /* check volume matches, change media if not */
901 if (mi->volume_label && mi->disk_id > 1)
903 WCHAR *source = msi_dup_property( package->db, szSourceDir );
904 BOOL match = source_matches_volume( mi, source );
905 msi_free( source );
907 if (!match && (mi->type == DRIVE_CDROM || mi->type == DRIVE_REMOVABLE))
909 if ((rc = msi_change_media( package, mi )) != ERROR_SUCCESS)
911 msi_free( cabinet_file );
912 return rc;
916 if (mi->cabinet)
918 if (compressed && GetFileAttributesW( cabinet_file ) == INVALID_FILE_ATTRIBUTES)
920 if ((rc = find_published_source( package, mi )) != ERROR_SUCCESS)
922 ERR("cabinet not found: %s\n", debugstr_w(cabinet_file));
923 msi_free( cabinet_file );
924 return ERROR_INSTALL_FAILURE;
928 msi_free( cabinet_file );
929 return ERROR_SUCCESS;
932 UINT msi_add_cabinet_stream( MSIPACKAGE *package, UINT disk_id, IStorage *storage, const WCHAR *name )
934 MSICABINETSTREAM *cab, *item;
936 TRACE("%p, %u, %p, %s\n", package, disk_id, storage, debugstr_w(name));
938 LIST_FOR_EACH_ENTRY( item, &package->cabinet_streams, MSICABINETSTREAM, entry )
940 if (item->disk_id == disk_id)
942 TRACE("duplicate disk id %u\n", disk_id);
943 return ERROR_FUNCTION_FAILED;
946 if (!(cab = msi_alloc( sizeof(*cab) ))) return ERROR_OUTOFMEMORY;
947 if (!(cab->stream = msi_alloc( (strlenW( name ) + 1) * sizeof(WCHAR ) )))
949 msi_free( cab );
950 return ERROR_OUTOFMEMORY;
952 strcpyW( cab->stream, name );
953 cab->disk_id = disk_id;
954 cab->storage = storage;
955 IStorage_AddRef( storage );
956 list_add_tail( &package->cabinet_streams, &cab->entry );
958 return ERROR_SUCCESS;