ntdll: Load .so builtin modules without using libwine.
[wine/zf.git] / dlls / msi / media.c
blob05a751092a317a40521f178552bb4c8af7229b14
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 "resource.h"
36 WINE_DEFAULT_DEBUG_CHANNEL(msi);
38 /* from msvcrt/fcntl.h */
39 #define _O_RDONLY 0
40 #define _O_WRONLY 1
41 #define _O_RDWR 2
42 #define _O_ACCMODE (_O_RDONLY|_O_WRONLY|_O_RDWR)
43 #define _O_APPEND 0x0008
44 #define _O_RANDOM 0x0010
45 #define _O_SEQUENTIAL 0x0020
46 #define _O_TEMPORARY 0x0040
47 #define _O_NOINHERIT 0x0080
48 #define _O_CREAT 0x0100
49 #define _O_TRUNC 0x0200
50 #define _O_EXCL 0x0400
51 #define _O_SHORT_LIVED 0x1000
52 #define _O_TEXT 0x4000
53 #define _O_BINARY 0x8000
55 static BOOL source_matches_volume(MSIMEDIAINFO *mi, LPCWSTR source_root)
57 WCHAR volume_name[MAX_PATH + 1], root[MAX_PATH + 1];
58 const WCHAR *p;
59 int len, len2;
61 lstrcpyW(root, source_root);
62 PathStripToRootW(root);
63 PathAddBackslashW(root);
65 if (!GetVolumeInformationW(root, volume_name, MAX_PATH + 1, NULL, NULL, NULL, NULL, 0))
67 WARN("failed to get volume information for %s (%u)\n", debugstr_w(root), GetLastError());
68 return FALSE;
71 len = lstrlenW( volume_name );
72 len2 = lstrlenW( mi->volume_label );
73 if (len2 > len) return FALSE;
74 p = volume_name + len - len2;
76 return !wcsicmp( mi->volume_label, p );
79 static UINT msi_change_media(MSIPACKAGE *package, MSIMEDIAINFO *mi)
81 MSIRECORD *record;
82 LPWSTR source_dir;
83 UINT r = IDRETRY;
85 source_dir = msi_dup_property(package->db, szSourceDir);
86 record = MSI_CreateRecord(2);
88 while (r == IDRETRY && !source_matches_volume(mi, source_dir))
90 MSI_RecordSetStringW(record, 0, NULL);
91 MSI_RecordSetInteger(record, 1, MSIERR_CABNOTFOUND);
92 MSI_RecordSetStringW(record, 2, mi->disk_prompt);
93 r = MSI_ProcessMessage(package, INSTALLMESSAGE_ERROR | MB_RETRYCANCEL, record);
96 msiobj_release(&record->hdr);
97 msi_free(source_dir);
99 return r == IDRETRY ? ERROR_SUCCESS : ERROR_INSTALL_SOURCE_ABSENT;
102 static MSICABINETSTREAM *msi_get_cabinet_stream( MSIPACKAGE *package, UINT disk_id )
104 MSICABINETSTREAM *cab;
106 LIST_FOR_EACH_ENTRY( cab, &package->cabinet_streams, MSICABINETSTREAM, entry )
108 if (cab->disk_id == disk_id) return cab;
110 return NULL;
113 static void * CDECL cabinet_alloc(ULONG cb)
115 return msi_alloc(cb);
118 static void CDECL cabinet_free(void *pv)
120 msi_free(pv);
123 static INT_PTR CDECL cabinet_open(char *pszFile, int oflag, int pmode)
125 DWORD dwAccess = 0;
126 DWORD dwShareMode = 0;
127 DWORD dwCreateDisposition = OPEN_EXISTING;
129 switch (oflag & _O_ACCMODE)
131 case _O_RDONLY:
132 dwAccess = GENERIC_READ;
133 dwShareMode = FILE_SHARE_READ | FILE_SHARE_DELETE;
134 break;
135 case _O_WRONLY:
136 dwAccess = GENERIC_WRITE;
137 dwShareMode = FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE;
138 break;
139 case _O_RDWR:
140 dwAccess = GENERIC_READ | GENERIC_WRITE;
141 dwShareMode = FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE;
142 break;
145 if ((oflag & (_O_CREAT | _O_EXCL)) == (_O_CREAT | _O_EXCL))
146 dwCreateDisposition = CREATE_NEW;
147 else if (oflag & _O_CREAT)
148 dwCreateDisposition = CREATE_ALWAYS;
150 return (INT_PTR)CreateFileA(pszFile, dwAccess, dwShareMode, NULL,
151 dwCreateDisposition, 0, NULL);
154 static UINT CDECL cabinet_read(INT_PTR hf, void *pv, UINT cb)
156 HANDLE handle = (HANDLE)hf;
157 DWORD read;
159 if (ReadFile(handle, pv, cb, &read, NULL))
160 return read;
162 return 0;
165 static UINT CDECL cabinet_write(INT_PTR hf, void *pv, UINT cb)
167 HANDLE handle = (HANDLE)hf;
168 DWORD written;
170 if (WriteFile(handle, pv, cb, &written, NULL))
171 return written;
173 return 0;
176 static int CDECL cabinet_close(INT_PTR hf)
178 HANDLE handle = (HANDLE)hf;
179 return CloseHandle(handle) ? 0 : -1;
182 static LONG CDECL cabinet_seek(INT_PTR hf, LONG dist, int seektype)
184 HANDLE handle = (HANDLE)hf;
185 /* flags are compatible and so are passed straight through */
186 return SetFilePointer(handle, dist, NULL, seektype);
189 struct package_disk
191 MSIPACKAGE *package;
192 UINT id;
195 static struct package_disk package_disk;
197 static INT_PTR CDECL cabinet_open_stream( char *pszFile, int oflag, int pmode )
199 MSICABINETSTREAM *cab;
200 IStream *stream;
202 if (!(cab = msi_get_cabinet_stream( package_disk.package, package_disk.id )))
204 WARN("failed to get cabinet stream\n");
205 return -1;
207 if (cab->storage == package_disk.package->db->storage)
209 UINT r = msi_get_stream( package_disk.package->db, cab->stream + 1, &stream );
210 if (r != ERROR_SUCCESS)
212 WARN("failed to get stream %u\n", r);
213 return -1;
216 else /* patch storage */
218 HRESULT hr;
219 WCHAR *encoded;
221 if (!(encoded = encode_streamname( FALSE, cab->stream + 1 )))
223 WARN("failed to encode stream name\n");
224 return -1;
226 hr = IStorage_OpenStream( cab->storage, encoded, NULL, STGM_READ|STGM_SHARE_EXCLUSIVE, 0, &stream );
227 msi_free( encoded );
228 if (FAILED(hr))
230 WARN("failed to open stream 0x%08x\n", hr);
231 return -1;
234 return (INT_PTR)stream;
237 static UINT CDECL cabinet_read_stream( INT_PTR hf, void *pv, UINT cb )
239 IStream *stm = (IStream *)hf;
240 DWORD read;
241 HRESULT hr;
243 hr = IStream_Read( stm, pv, cb, &read );
244 if (hr == S_OK || hr == S_FALSE)
245 return read;
247 return 0;
250 static int CDECL cabinet_close_stream( INT_PTR hf )
252 IStream *stm = (IStream *)hf;
253 IStream_Release( stm );
254 return 0;
257 static LONG CDECL cabinet_seek_stream( INT_PTR hf, LONG dist, int seektype )
259 IStream *stm = (IStream *)hf;
260 LARGE_INTEGER move;
261 ULARGE_INTEGER newpos;
262 HRESULT hr;
264 move.QuadPart = dist;
265 hr = IStream_Seek( stm, move, seektype, &newpos );
266 if (SUCCEEDED(hr))
268 if (newpos.QuadPart <= MAXLONG) return newpos.QuadPart;
269 ERR("Too big!\n");
271 return -1;
274 static UINT CDECL msi_media_get_disk_info(MSIPACKAGE *package, MSIMEDIAINFO *mi)
276 MSIRECORD *row;
278 static const WCHAR query[] = {
279 'S','E','L','E','C','T',' ','*',' ', 'F','R','O','M',' ',
280 '`','M','e','d','i','a','`',' ','W','H','E','R','E',' ',
281 '`','D','i','s','k','I','d','`',' ','=',' ','%','i',0};
283 row = MSI_QueryGetRecord(package->db, query, mi->disk_id);
284 if (!row)
286 TRACE("Unable to query row\n");
287 return ERROR_FUNCTION_FAILED;
290 mi->disk_prompt = strdupW(MSI_RecordGetString(row, 3));
291 mi->cabinet = strdupW(MSI_RecordGetString(row, 4));
292 mi->volume_label = strdupW(MSI_RecordGetString(row, 5));
294 msiobj_release(&row->hdr);
295 return ERROR_SUCCESS;
298 static INT_PTR cabinet_partial_file(FDINOTIFICATIONTYPE fdint,
299 PFDINOTIFICATION pfdin)
301 MSICABDATA *data = pfdin->pv;
302 data->mi->is_continuous = FALSE;
303 return 0;
306 static WCHAR *get_cabinet_filename(MSIMEDIAINFO *mi)
308 int len;
309 WCHAR *ret;
311 len = lstrlenW(mi->sourcedir) + lstrlenW(mi->cabinet) + 1;
312 if (!(ret = msi_alloc(len * sizeof(WCHAR)))) return NULL;
313 lstrcpyW(ret, mi->sourcedir);
314 lstrcatW(ret, mi->cabinet);
315 return ret;
318 static INT_PTR cabinet_next_cabinet(FDINOTIFICATIONTYPE fdint,
319 PFDINOTIFICATION pfdin)
321 MSICABDATA *data = pfdin->pv;
322 MSIMEDIAINFO *mi = data->mi;
323 LPWSTR cabinet_file = NULL, cab = strdupAtoW(pfdin->psz1);
324 INT_PTR res = -1;
325 UINT rc;
327 msi_free(mi->disk_prompt);
328 msi_free(mi->cabinet);
329 msi_free(mi->volume_label);
330 mi->disk_prompt = NULL;
331 mi->cabinet = NULL;
332 mi->volume_label = NULL;
334 mi->disk_id++;
335 mi->is_continuous = TRUE;
337 rc = msi_media_get_disk_info(data->package, mi);
338 if (rc != ERROR_SUCCESS)
340 ERR("Failed to get next cabinet information: %d\n", rc);
341 goto done;
344 if (wcsicmp( mi->cabinet, cab ))
346 char *next_cab;
347 ULONG length;
349 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));
351 /* Use cabinet name from the media table */
352 next_cab = strdupWtoA(mi->cabinet);
353 /* Modify path to cabinet file with full filename (psz3 points to a 256 bytes buffer that can be modified contrary to psz1 and psz2) */
354 length = strlen(pfdin->psz3) + 1 + strlen(next_cab) + 1;
355 if (length > 256)
357 WARN("Cannot update next cabinet filename with a string size %u > 256\n", length);
358 msi_free(next_cab);
359 goto done;
361 else
363 strcat(pfdin->psz3, "\\");
364 strcat(pfdin->psz3, next_cab);
366 /* Path psz3 and cabinet psz1 are concatenated by FDI so just reset psz1 */
367 *pfdin->psz1 = 0;
368 msi_free(next_cab);
371 if (!(cabinet_file = get_cabinet_filename(mi)))
372 goto done;
374 TRACE("Searching for %s\n", debugstr_w(cabinet_file));
376 res = 0;
377 if (GetFileAttributesW(cabinet_file) == INVALID_FILE_ATTRIBUTES)
379 if (msi_change_media(data->package, mi) != ERROR_SUCCESS)
380 res = -1;
383 done:
384 msi_free(cab);
385 msi_free(cabinet_file);
386 return res;
389 static INT_PTR cabinet_next_cabinet_stream( FDINOTIFICATIONTYPE fdint,
390 PFDINOTIFICATION pfdin )
392 MSICABDATA *data = pfdin->pv;
393 MSIMEDIAINFO *mi = data->mi;
394 UINT rc;
396 msi_free( mi->disk_prompt );
397 msi_free( mi->cabinet );
398 msi_free( mi->volume_label );
399 mi->disk_prompt = NULL;
400 mi->cabinet = NULL;
401 mi->volume_label = NULL;
403 mi->disk_id++;
404 mi->is_continuous = TRUE;
406 rc = msi_media_get_disk_info( data->package, mi );
407 if (rc != ERROR_SUCCESS)
409 ERR("Failed to get next cabinet information: %u\n", rc);
410 return -1;
412 package_disk.id = mi->disk_id;
414 TRACE("next cabinet is %s disk id %u\n", debugstr_w(mi->cabinet), mi->disk_id);
415 return 0;
418 static INT_PTR cabinet_copy_file(FDINOTIFICATIONTYPE fdint,
419 PFDINOTIFICATION pfdin)
421 MSICABDATA *data = pfdin->pv;
422 HANDLE handle = 0;
423 LPWSTR path = NULL;
424 DWORD attrs;
426 data->curfile = strdupAtoW(pfdin->psz1);
427 if (!data->cb(data->package, data->curfile, MSICABEXTRACT_BEGINEXTRACT, &path,
428 &attrs, data->user))
430 /* We're not extracting this file, so free the filename. */
431 msi_free(data->curfile);
432 data->curfile = NULL;
433 goto done;
436 TRACE("extracting %s -> %s\n", debugstr_w(data->curfile), debugstr_w(path));
438 attrs = attrs & (FILE_ATTRIBUTE_READONLY|FILE_ATTRIBUTE_HIDDEN|FILE_ATTRIBUTE_SYSTEM);
439 if (!attrs) attrs = FILE_ATTRIBUTE_NORMAL;
441 handle = msi_create_file( data->package, path, GENERIC_READ | GENERIC_WRITE, 0, CREATE_ALWAYS, attrs );
442 if (handle == INVALID_HANDLE_VALUE)
444 DWORD err = GetLastError();
445 DWORD attrs2 = msi_get_file_attributes( data->package, path );
447 if (attrs2 == INVALID_FILE_ATTRIBUTES)
449 ERR("failed to create %s (error %d)\n", debugstr_w(path), err);
450 goto done;
452 else if (err == ERROR_ACCESS_DENIED && (attrs2 & FILE_ATTRIBUTE_READONLY))
454 TRACE("removing read-only attribute on %s\n", debugstr_w(path));
455 msi_set_file_attributes( data->package, path, attrs2 & ~FILE_ATTRIBUTE_READONLY );
456 handle = msi_create_file( data->package, path, GENERIC_READ | GENERIC_WRITE, 0, CREATE_ALWAYS, attrs );
458 if (handle != INVALID_HANDLE_VALUE) goto done;
459 err = GetLastError();
461 if (err == ERROR_SHARING_VIOLATION || err == ERROR_USER_MAPPED_FILE)
463 WCHAR *tmpfileW, *tmppathW, *p;
464 DWORD len;
466 TRACE("file in use, scheduling rename operation\n");
468 if (!(tmppathW = strdupW( path ))) return ERROR_OUTOFMEMORY;
469 if ((p = wcsrchr(tmppathW, '\\'))) *p = 0;
470 len = lstrlenW( tmppathW ) + 16;
471 if (!(tmpfileW = msi_alloc(len * sizeof(WCHAR))))
473 msi_free( tmppathW );
474 return ERROR_OUTOFMEMORY;
476 if (!GetTempFileNameW(tmppathW, szMsi, 0, tmpfileW)) tmpfileW[0] = 0;
477 msi_free( tmppathW );
479 handle = CreateFileW(tmpfileW, GENERIC_READ | GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, attrs, NULL);
481 if (handle != INVALID_HANDLE_VALUE &&
482 msi_move_file( data->package, path, NULL, MOVEFILE_DELAY_UNTIL_REBOOT ) &&
483 msi_move_file( data->package, tmpfileW, path, MOVEFILE_DELAY_UNTIL_REBOOT ))
485 data->package->need_reboot_at_end = 1;
487 else
489 WARN("failed to schedule rename operation %s (error %d)\n", debugstr_w(path), GetLastError());
490 DeleteFileW( tmpfileW );
492 msi_free(tmpfileW);
494 else
495 WARN("failed to create %s (error %d)\n", debugstr_w(path), err);
498 done:
499 msi_free(path);
501 return (INT_PTR)handle;
504 static INT_PTR cabinet_close_file_info(FDINOTIFICATIONTYPE fdint,
505 PFDINOTIFICATION pfdin)
507 MSICABDATA *data = pfdin->pv;
508 FILETIME ft;
509 FILETIME ftLocal;
510 HANDLE handle = (HANDLE)pfdin->hf;
512 data->mi->is_continuous = FALSE;
514 if (!DosDateTimeToFileTime(pfdin->date, pfdin->time, &ft))
515 return -1;
516 if (!LocalFileTimeToFileTime(&ft, &ftLocal))
517 return -1;
518 if (!SetFileTime(handle, &ftLocal, 0, &ftLocal))
519 return -1;
521 CloseHandle(handle);
523 data->cb(data->package, data->curfile, MSICABEXTRACT_FILEEXTRACTED, NULL, NULL,
524 data->user);
526 msi_free(data->curfile);
527 data->curfile = NULL;
529 return 1;
532 static INT_PTR CDECL cabinet_notify(FDINOTIFICATIONTYPE fdint, PFDINOTIFICATION pfdin)
534 switch (fdint)
536 case fdintPARTIAL_FILE:
537 return cabinet_partial_file(fdint, pfdin);
539 case fdintNEXT_CABINET:
540 return cabinet_next_cabinet(fdint, pfdin);
542 case fdintCOPY_FILE:
543 return cabinet_copy_file(fdint, pfdin);
545 case fdintCLOSE_FILE_INFO:
546 return cabinet_close_file_info(fdint, pfdin);
548 default:
549 return 0;
553 static INT_PTR CDECL cabinet_notify_stream( FDINOTIFICATIONTYPE fdint, PFDINOTIFICATION pfdin )
555 switch (fdint)
557 case fdintPARTIAL_FILE:
558 return cabinet_partial_file( fdint, pfdin );
560 case fdintNEXT_CABINET:
561 return cabinet_next_cabinet_stream( fdint, pfdin );
563 case fdintCOPY_FILE:
564 return cabinet_copy_file( fdint, pfdin );
566 case fdintCLOSE_FILE_INFO:
567 return cabinet_close_file_info( fdint, pfdin );
569 case fdintCABINET_INFO:
570 return 0;
572 default:
573 ERR("Unexpected notification %d\n", fdint);
574 return 0;
578 static BOOL extract_cabinet( MSIPACKAGE* package, MSIMEDIAINFO *mi, LPVOID data )
580 LPSTR cabinet, cab_path = NULL;
581 HFDI hfdi;
582 ERF erf;
583 BOOL ret = FALSE;
585 TRACE("extracting %s disk id %u\n", debugstr_w(mi->cabinet), mi->disk_id);
587 hfdi = FDICreate( cabinet_alloc, cabinet_free, cabinet_open, cabinet_read,
588 cabinet_write, cabinet_close, cabinet_seek, 0, &erf );
589 if (!hfdi)
591 ERR("FDICreate failed\n");
592 return FALSE;
595 cabinet = strdupWtoA( mi->cabinet );
596 if (!cabinet)
597 goto done;
599 cab_path = strdupWtoA( mi->sourcedir );
600 if (!cab_path)
601 goto done;
603 ret = FDICopy( hfdi, cabinet, cab_path, 0, cabinet_notify, NULL, data );
604 if (!ret)
605 ERR("FDICopy failed\n");
607 done:
608 FDIDestroy( hfdi );
609 msi_free(cabinet );
610 msi_free( cab_path );
612 if (ret)
613 mi->is_extracted = TRUE;
615 return ret;
618 static BOOL extract_cabinet_stream( MSIPACKAGE *package, MSIMEDIAINFO *mi, LPVOID data )
620 static char filename[] = {'<','S','T','R','E','A','M','>',0};
621 HFDI hfdi;
622 ERF erf;
623 BOOL ret = FALSE;
625 TRACE("extracting %s disk id %u\n", debugstr_w(mi->cabinet), mi->disk_id);
627 hfdi = FDICreate( cabinet_alloc, cabinet_free, cabinet_open_stream, cabinet_read_stream,
628 cabinet_write, cabinet_close_stream, cabinet_seek_stream, 0, &erf );
629 if (!hfdi)
631 ERR("FDICreate failed\n");
632 return FALSE;
635 package_disk.package = package;
636 package_disk.id = mi->disk_id;
638 ret = FDICopy( hfdi, filename, NULL, 0, cabinet_notify_stream, NULL, data );
639 if (!ret) ERR("FDICopy failed\n");
641 FDIDestroy( hfdi );
642 if (ret) mi->is_extracted = TRUE;
643 return ret;
646 /***********************************************************************
647 * msi_cabextract
649 * Extract files from a cabinet file or stream.
651 BOOL msi_cabextract(MSIPACKAGE* package, MSIMEDIAINFO *mi, LPVOID data)
653 if (mi->cabinet[0] == '#')
655 return extract_cabinet_stream( package, mi, data );
657 return extract_cabinet( package, mi, data );
660 void msi_free_media_info(MSIMEDIAINFO *mi)
662 msi_free(mi->disk_prompt);
663 msi_free(mi->cabinet);
664 msi_free(mi->volume_label);
665 msi_free(mi->last_volume);
666 msi_free(mi);
669 static UINT get_drive_type(const WCHAR *path)
671 WCHAR root[MAX_PATH + 1];
673 lstrcpyW(root, path);
674 PathStripToRootW(root);
675 PathAddBackslashW(root);
677 return GetDriveTypeW(root);
680 static WCHAR *get_base_url( MSIDATABASE *db )
682 WCHAR *p, *ret = NULL, *orig_db = msi_dup_property( db, szOriginalDatabase );
683 if (UrlIsW( orig_db, URLIS_URL ) && (ret = strdupW( orig_db )) && (p = wcsrchr( ret, '/'))) p[1] = 0;
684 msi_free( orig_db );
685 return ret;
688 UINT msi_load_media_info(MSIPACKAGE *package, UINT Sequence, MSIMEDIAINFO *mi)
690 static const WCHAR query[] = {
691 'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ','`','M','e','d','i','a','`',' ',
692 'W','H','E','R','E',' ','`','L','a','s','t','S','e','q','u','e','n','c','e','`',' ',
693 '>','=',' ','%','i',' ','O','R','D','E','R',' ','B','Y',' ','`','D','i','s','k','I','d','`',0};
694 MSIRECORD *row;
695 WCHAR *source_dir, *source, *base_url = NULL;
696 DWORD options;
698 if (Sequence <= mi->last_sequence) /* already loaded */
699 return ERROR_SUCCESS;
701 row = MSI_QueryGetRecord(package->db, query, Sequence);
702 if (!row)
704 TRACE("Unable to query row\n");
705 return ERROR_FUNCTION_FAILED;
708 mi->is_extracted = FALSE;
709 mi->disk_id = MSI_RecordGetInteger(row, 1);
710 mi->last_sequence = MSI_RecordGetInteger(row, 2);
711 msi_free(mi->disk_prompt);
712 mi->disk_prompt = strdupW(MSI_RecordGetString(row, 3));
713 msi_free(mi->cabinet);
714 mi->cabinet = strdupW(MSI_RecordGetString(row, 4));
715 msi_free(mi->volume_label);
716 mi->volume_label = strdupW(MSI_RecordGetString(row, 5));
717 msiobj_release(&row->hdr);
719 msi_set_sourcedir_props(package, FALSE);
720 source_dir = msi_dup_property(package->db, szSourceDir);
721 lstrcpyW(mi->sourcedir, source_dir);
722 PathAddBackslashW(mi->sourcedir);
723 mi->type = get_drive_type(source_dir);
725 options = MSICODE_PRODUCT;
726 if (mi->type == DRIVE_CDROM || mi->type == DRIVE_REMOVABLE)
728 source = source_dir;
729 options |= MSISOURCETYPE_MEDIA;
731 else if ((base_url = get_base_url(package->db)))
733 source = base_url;
734 options |= MSISOURCETYPE_URL;
736 else
738 source = mi->sourcedir;
739 options |= MSISOURCETYPE_NETWORK;
742 msi_package_add_media_disk(package, package->Context,
743 MSICODE_PRODUCT, mi->disk_id,
744 mi->volume_label, mi->disk_prompt);
746 msi_package_add_info(package, package->Context,
747 options, INSTALLPROPERTY_LASTUSEDSOURCEW, source);
749 TRACE("sequence %u -> cabinet %s disk id %u\n", Sequence, debugstr_w(mi->cabinet), mi->disk_id);
751 msi_free(base_url);
752 msi_free(source_dir);
753 return ERROR_SUCCESS;
756 /* FIXME: search URL sources as well */
757 static UINT find_published_source(MSIPACKAGE *package, MSIMEDIAINFO *mi)
759 WCHAR source[MAX_PATH];
760 WCHAR volume[MAX_PATH];
761 WCHAR prompt[MAX_PATH];
762 DWORD volumesz, promptsz;
763 DWORD index, size, id;
764 WCHAR last_type[2];
765 UINT r;
767 size = 2;
768 r = MsiSourceListGetInfoW(package->ProductCode, NULL,
769 package->Context, MSICODE_PRODUCT,
770 INSTALLPROPERTY_LASTUSEDTYPEW, last_type, &size);
771 if (r != ERROR_SUCCESS)
772 return r;
774 size = MAX_PATH;
775 r = MsiSourceListGetInfoW(package->ProductCode, NULL,
776 package->Context, MSICODE_PRODUCT,
777 INSTALLPROPERTY_LASTUSEDSOURCEW, source, &size);
778 if (r != ERROR_SUCCESS)
779 return r;
781 if (last_type[0] == 'n')
783 WCHAR cabinet_file[MAX_PATH];
784 BOOL check_all = FALSE;
786 while(TRUE)
788 index = 0;
789 volumesz = MAX_PATH;
790 while (MsiSourceListEnumSourcesW(package->ProductCode, NULL,
791 package->Context,
792 MSISOURCETYPE_NETWORK, index++,
793 volume, &volumesz) == ERROR_SUCCESS)
795 if (check_all || !wcsnicmp(source, volume, lstrlenW(source)))
797 lstrcpyW(cabinet_file, volume);
798 PathAddBackslashW(cabinet_file);
799 lstrcatW(cabinet_file, mi->cabinet);
801 if (GetFileAttributesW(cabinet_file) == INVALID_FILE_ATTRIBUTES)
803 volumesz = MAX_PATH;
804 if(!check_all)
805 break;
806 continue;
809 lstrcpyW(mi->sourcedir, volume);
810 PathAddBackslashW(mi->sourcedir);
811 TRACE("Found network source %s\n", debugstr_w(mi->sourcedir));
812 return ERROR_SUCCESS;
816 if (!check_all)
817 check_all = TRUE;
818 else
819 break;
823 index = 0;
824 volumesz = MAX_PATH;
825 promptsz = MAX_PATH;
826 while (MsiSourceListEnumMediaDisksW(package->ProductCode, NULL,
827 package->Context,
828 MSICODE_PRODUCT, index++, &id,
829 volume, &volumesz, prompt, &promptsz) == ERROR_SUCCESS)
831 mi->disk_id = id;
832 msi_free( mi->volume_label );
833 if (!(mi->volume_label = msi_alloc( ++volumesz * sizeof(WCHAR) ))) return ERROR_OUTOFMEMORY;
834 lstrcpyW( mi->volume_label, volume );
836 msi_free( mi->disk_prompt );
837 if (!(mi->disk_prompt = msi_alloc( ++promptsz * sizeof(WCHAR) ))) return ERROR_OUTOFMEMORY;
838 lstrcpyW( mi->disk_prompt, prompt );
840 if (source_matches_volume(mi, source))
842 /* FIXME: what about SourceDir */
843 lstrcpyW(mi->sourcedir, source);
844 PathAddBackslashW(mi->sourcedir);
845 TRACE("Found disk source %s\n", debugstr_w(mi->sourcedir));
846 return ERROR_SUCCESS;
850 return ERROR_FUNCTION_FAILED;
853 UINT ready_media( MSIPACKAGE *package, BOOL compressed, MSIMEDIAINFO *mi )
855 UINT rc;
856 WCHAR *cabinet_file = NULL;
858 /* media info for continuous cabinet is already loaded */
859 if (mi->is_continuous) return ERROR_SUCCESS;
861 if (mi->cabinet)
863 WCHAR *base_url;
865 /* cabinet is internal, no checks needed */
866 if (mi->cabinet[0] == '#') return ERROR_SUCCESS;
868 if (!(cabinet_file = get_cabinet_filename( mi ))) return ERROR_OUTOFMEMORY;
870 /* package should be downloaded */
871 if (compressed && GetFileAttributesW( cabinet_file ) == INVALID_FILE_ATTRIBUTES &&
872 (base_url = get_base_url( package->db )))
874 WCHAR temppath[MAX_PATH], *p, *url;
876 msi_free( cabinet_file );
877 if (!(url = msi_alloc( (lstrlenW( base_url ) + lstrlenW( mi->cabinet ) + 1) * sizeof(WCHAR) )))
879 return ERROR_OUTOFMEMORY;
881 lstrcpyW( url, base_url );
882 lstrcatW( url, mi->cabinet );
883 if ((rc = msi_download_file( url, temppath )) != ERROR_SUCCESS)
885 ERR("failed to download %s (%u)\n", debugstr_w(url), rc);
886 msi_free( url );
887 return rc;
889 if ((p = wcsrchr( temppath, '\\' ))) *p = 0;
890 lstrcpyW( mi->sourcedir, temppath );
891 PathAddBackslashW( mi->sourcedir );
892 msi_free( mi->cabinet );
893 mi->cabinet = strdupW( p + 1 );
895 msi_free( url );
896 return ERROR_SUCCESS;
899 /* check volume matches, change media if not */
900 if (mi->volume_label)
902 /* assume first volume is in the drive */
903 if (mi->last_volume && wcsicmp( mi->last_volume, mi->volume_label ))
905 WCHAR *source = msi_dup_property( package->db, szSourceDir );
906 BOOL match = source_matches_volume( mi, source );
907 msi_free( source );
909 if (!match && (mi->type == DRIVE_CDROM || mi->type == DRIVE_REMOVABLE))
911 if ((rc = msi_change_media( package, mi )) != ERROR_SUCCESS)
913 msi_free( cabinet_file );
914 return rc;
919 msi_free(mi->last_volume);
920 mi->last_volume = strdupW(mi->volume_label);
922 if (mi->cabinet)
924 if (compressed && GetFileAttributesW( cabinet_file ) == INVALID_FILE_ATTRIBUTES)
926 if ((rc = find_published_source( package, mi )) != ERROR_SUCCESS)
928 ERR("cabinet not found: %s\n", debugstr_w(cabinet_file));
929 msi_free( cabinet_file );
930 return ERROR_INSTALL_FAILURE;
934 msi_free( cabinet_file );
935 return ERROR_SUCCESS;
938 UINT msi_add_cabinet_stream( MSIPACKAGE *package, UINT disk_id, IStorage *storage, const WCHAR *name )
940 MSICABINETSTREAM *cab, *item;
942 TRACE("%p, %u, %p, %s\n", package, disk_id, storage, debugstr_w(name));
944 LIST_FOR_EACH_ENTRY( item, &package->cabinet_streams, MSICABINETSTREAM, entry )
946 if (item->disk_id == disk_id)
948 TRACE("duplicate disk id %u\n", disk_id);
949 return ERROR_FUNCTION_FAILED;
952 if (!(cab = msi_alloc( sizeof(*cab) ))) return ERROR_OUTOFMEMORY;
953 if (!(cab->stream = msi_alloc( (lstrlenW( name ) + 1) * sizeof(WCHAR ) )))
955 msi_free( cab );
956 return ERROR_OUTOFMEMORY;
958 lstrcpyW( cab->stream, name );
959 cab->disk_id = disk_id;
960 cab->storage = storage;
961 IStorage_AddRef( storage );
962 list_add_tail( &package->cabinet_streams, &cab->entry );
964 return ERROR_SUCCESS;