788528e9b37aefc8f7ab5766972da3bf166ba858
[reactos.git] / reactos / dll / win32 / msi / media.c
1 /*
2 * Implementation of the Microsoft Installer (msi.dll)
3 *
4 * Copyright 2008 James Hawkins
5 *
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.
10 *
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.
15 *
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
19 */
20
21 #include <stdarg.h>
22
23 #define COBJMACROS
24
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
36 WINE_DEFAULT_DEBUG_CHANNEL(msi);
37
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
54
55 static BOOL source_matches_volume(MSIMEDIAINFO *mi, LPCWSTR source_root)
56 {
57 WCHAR volume_name[MAX_PATH + 1];
58 WCHAR root[MAX_PATH + 1];
59
60 strcpyW(root, source_root);
61 PathStripToRootW(root);
62 PathAddBackslashW(root);
63
64 if (!GetVolumeInformationW(root, volume_name, MAX_PATH + 1,
65 NULL, NULL, NULL, NULL, 0))
66 {
67 ERR("Failed to get volume information\n");
68 return FALSE;
69 }
70
71 return !lstrcmpW(mi->volume_label, volume_name);
72 }
73
74 static UINT msi_change_media(MSIPACKAGE *package, MSIMEDIAINFO *mi)
75 {
76 LPWSTR error, error_dialog;
77 LPWSTR source_dir;
78 UINT r = ERROR_SUCCESS;
79
80 static const WCHAR error_prop[] = {'E','r','r','o','r','D','i','a','l','o','g',0};
81
82 if ((msi_get_property_int(package->db, szUILevel, 0) & INSTALLUILEVEL_MASK) ==
83 INSTALLUILEVEL_NONE && !gUIHandlerA && !gUIHandlerW && !gUIHandlerRecord)
84 return ERROR_SUCCESS;
85
86 error = generate_error_string(package, 1302, 1, mi->disk_prompt);
87 error_dialog = msi_dup_property(package->db, error_prop);
88 source_dir = msi_dup_property(package->db, cszSourceDir);
89
90 while (r == ERROR_SUCCESS && !source_matches_volume(mi, source_dir))
91 {
92 r = msi_spawn_error_dialog(package, error_dialog, error);
93
94 if (gUIHandlerW)
95 {
96 gUIHandlerW(gUIContext, MB_RETRYCANCEL | INSTALLMESSAGE_ERROR, error);
97 }
98 else if (gUIHandlerA)
99 {
100 char *msg = strdupWtoA(error);
101 gUIHandlerA(gUIContext, MB_RETRYCANCEL | INSTALLMESSAGE_ERROR, msg);
102 msi_free(msg);
103 }
104 else if (gUIHandlerRecord)
105 {
106 MSIHANDLE rec = MsiCreateRecord(1);
107 MsiRecordSetStringW(rec, 0, error);
108 gUIHandlerRecord(gUIContext, MB_RETRYCANCEL | INSTALLMESSAGE_ERROR, rec);
109 MsiCloseHandle(rec);
110 }
111 }
112
113 msi_free(error);
114 msi_free(error_dialog);
115 msi_free(source_dir);
116
117 return r;
118 }
119
120 static void * CDECL cabinet_alloc(ULONG cb)
121 {
122 return msi_alloc(cb);
123 }
124
125 static void CDECL cabinet_free(void *pv)
126 {
127 msi_free(pv);
128 }
129
130 static INT_PTR CDECL cabinet_open(char *pszFile, int oflag, int pmode)
131 {
132 HANDLE handle;
133 DWORD dwAccess = 0;
134 DWORD dwShareMode = 0;
135 DWORD dwCreateDisposition = OPEN_EXISTING;
136
137 switch (oflag & _O_ACCMODE)
138 {
139 case _O_RDONLY:
140 dwAccess = GENERIC_READ;
141 dwShareMode = FILE_SHARE_READ | FILE_SHARE_DELETE;
142 break;
143 case _O_WRONLY:
144 dwAccess = GENERIC_WRITE;
145 dwShareMode = FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE;
146 break;
147 case _O_RDWR:
148 dwAccess = GENERIC_READ | GENERIC_WRITE;
149 dwShareMode = FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE;
150 break;
151 }
152
153 if ((oflag & (_O_CREAT | _O_EXCL)) == (_O_CREAT | _O_EXCL))
154 dwCreateDisposition = CREATE_NEW;
155 else if (oflag & _O_CREAT)
156 dwCreateDisposition = CREATE_ALWAYS;
157
158 handle = CreateFileA(pszFile, dwAccess, dwShareMode, NULL,
159 dwCreateDisposition, 0, NULL);
160 if (handle == INVALID_HANDLE_VALUE)
161 return 0;
162
163 return (INT_PTR)handle;
164 }
165
166 static UINT CDECL cabinet_read(INT_PTR hf, void *pv, UINT cb)
167 {
168 HANDLE handle = (HANDLE)hf;
169 DWORD read;
170
171 if (ReadFile(handle, pv, cb, &read, NULL))
172 return read;
173
174 return 0;
175 }
176
177 static UINT CDECL cabinet_write(INT_PTR hf, void *pv, UINT cb)
178 {
179 HANDLE handle = (HANDLE)hf;
180 DWORD written;
181
182 if (WriteFile(handle, pv, cb, &written, NULL))
183 return written;
184
185 return 0;
186 }
187
188 static int CDECL cabinet_close(INT_PTR hf)
189 {
190 HANDLE handle = (HANDLE)hf;
191 return CloseHandle(handle) ? 0 : -1;
192 }
193
194 static LONG CDECL cabinet_seek(INT_PTR hf, LONG dist, int seektype)
195 {
196 HANDLE handle = (HANDLE)hf;
197 /* flags are compatible and so are passed straight through */
198 return SetFilePointer(handle, dist, NULL, seektype);
199 }
200
201 struct cab_stream
202 {
203 MSIDATABASE *db;
204 WCHAR *name;
205 };
206
207 static struct cab_stream cab_stream;
208
209 static INT_PTR CDECL cabinet_open_stream( char *pszFile, int oflag, int pmode )
210 {
211 UINT r;
212 IStream *stm;
213
214 r = db_get_raw_stream( cab_stream.db, cab_stream.name, &stm );
215 if (r != ERROR_SUCCESS)
216 {
217 WARN("Failed to get cabinet stream %u\n", r);
218 return 0;
219 }
220
221 return (INT_PTR)stm;
222 }
223
224 static UINT CDECL cabinet_read_stream( INT_PTR hf, void *pv, UINT cb )
225 {
226 IStream *stm = (IStream *)hf;
227 DWORD read;
228 HRESULT hr;
229
230 hr = IStream_Read( stm, pv, cb, &read );
231 if (hr == S_OK || hr == S_FALSE)
232 return read;
233
234 return 0;
235 }
236
237 static int CDECL cabinet_close_stream( INT_PTR hf )
238 {
239 IStream *stm = (IStream *)hf;
240 IStream_Release( stm );
241 return 0;
242 }
243
244 static LONG CDECL cabinet_seek_stream( INT_PTR hf, LONG dist, int seektype )
245 {
246 IStream *stm = (IStream *)hf;
247 LARGE_INTEGER move;
248 ULARGE_INTEGER newpos;
249 HRESULT hr;
250
251 move.QuadPart = dist;
252 hr = IStream_Seek( stm, move, seektype, &newpos );
253 if (SUCCEEDED(hr))
254 {
255 if (newpos.QuadPart <= MAXLONG) return newpos.QuadPart;
256 ERR("Too big!\n");
257 }
258 return -1;
259 }
260
261 static UINT CDECL msi_media_get_disk_info(MSIPACKAGE *package, MSIMEDIAINFO *mi)
262 {
263 MSIRECORD *row;
264
265 static const WCHAR query[] = {
266 'S','E','L','E','C','T',' ','*',' ', 'F','R','O','M',' ',
267 '`','M','e','d','i','a','`',' ','W','H','E','R','E',' ',
268 '`','D','i','s','k','I','d','`',' ','=',' ','%','i',0};
269
270 row = MSI_QueryGetRecord(package->db, query, mi->disk_id);
271 if (!row)
272 {
273 TRACE("Unable to query row\n");
274 return ERROR_FUNCTION_FAILED;
275 }
276
277 mi->disk_prompt = strdupW(MSI_RecordGetString(row, 3));
278 mi->cabinet = strdupW(MSI_RecordGetString(row, 4));
279 mi->volume_label = strdupW(MSI_RecordGetString(row, 5));
280
281 if (!mi->first_volume)
282 mi->first_volume = strdupW(mi->volume_label);
283
284 msiobj_release(&row->hdr);
285 return ERROR_SUCCESS;
286 }
287
288 static INT_PTR cabinet_partial_file(FDINOTIFICATIONTYPE fdint,
289 PFDINOTIFICATION pfdin)
290 {
291 MSICABDATA *data = pfdin->pv;
292 data->mi->is_continuous = FALSE;
293 return 0;
294 }
295
296 static WCHAR *get_cabinet_filename(MSIMEDIAINFO *mi)
297 {
298 int len;
299 WCHAR *ret;
300
301 len = strlenW(mi->sourcedir) + strlenW(mi->cabinet) + 1;
302 if (!(ret = msi_alloc(len * sizeof(WCHAR)))) return NULL;
303 strcpyW(ret, mi->sourcedir);
304 strcatW(ret, mi->cabinet);
305 return ret;
306 }
307
308 static INT_PTR cabinet_next_cabinet(FDINOTIFICATIONTYPE fdint,
309 PFDINOTIFICATION pfdin)
310 {
311 MSICABDATA *data = pfdin->pv;
312 MSIMEDIAINFO *mi = data->mi;
313 LPWSTR cabinet_file = NULL, cab = strdupAtoW(pfdin->psz1);
314 INT_PTR res = -1;
315 UINT rc;
316
317 msi_free(mi->disk_prompt);
318 msi_free(mi->cabinet);
319 msi_free(mi->volume_label);
320 mi->disk_prompt = NULL;
321 mi->cabinet = NULL;
322 mi->volume_label = NULL;
323
324 mi->disk_id++;
325 mi->is_continuous = TRUE;
326
327 rc = msi_media_get_disk_info(data->package, mi);
328 if (rc != ERROR_SUCCESS)
329 {
330 ERR("Failed to get next cabinet information: %d\n", rc);
331 goto done;
332 }
333
334 if (lstrcmpiW(mi->cabinet, cab))
335 {
336 ERR("Continuous cabinet does not match the next cabinet in the Media table\n");
337 goto done;
338 }
339
340 if (!(cabinet_file = get_cabinet_filename(mi)))
341 goto done;
342
343 TRACE("Searching for %s\n", debugstr_w(cabinet_file));
344
345 res = 0;
346 if (GetFileAttributesW(cabinet_file) == INVALID_FILE_ATTRIBUTES)
347 {
348 if (msi_change_media(data->package, mi) != ERROR_SUCCESS)
349 res = -1;
350 }
351
352 done:
353 msi_free(cab);
354 msi_free(cabinet_file);
355 return res;
356 }
357
358 static INT_PTR cabinet_next_cabinet_stream( FDINOTIFICATIONTYPE fdint,
359 PFDINOTIFICATION pfdin )
360 {
361 MSICABDATA *data = pfdin->pv;
362 MSIMEDIAINFO *mi = data->mi;
363 UINT rc;
364
365 msi_free( mi->disk_prompt );
366 msi_free( mi->cabinet );
367 msi_free( mi->volume_label );
368 mi->disk_prompt = NULL;
369 mi->cabinet = NULL;
370 mi->volume_label = NULL;
371
372 mi->disk_id++;
373 mi->is_continuous = TRUE;
374
375 rc = msi_media_get_disk_info( data->package, mi );
376 if (rc != ERROR_SUCCESS)
377 {
378 ERR("Failed to get next cabinet information: %u\n", rc);
379 return -1;
380 }
381
382 msi_free( cab_stream.name );
383 cab_stream.name = encode_streamname( FALSE, mi->cabinet + 1 );
384 if (!cab_stream.name)
385 return -1;
386
387 TRACE("next cabinet is %s\n", debugstr_w(mi->cabinet));
388
389 return 0;
390 }
391
392 static INT_PTR cabinet_copy_file(FDINOTIFICATIONTYPE fdint,
393 PFDINOTIFICATION pfdin)
394 {
395 MSICABDATA *data = pfdin->pv;
396 HANDLE handle = 0;
397 LPWSTR path = NULL;
398 DWORD attrs;
399
400 data->curfile = strdupAtoW(pfdin->psz1);
401 if (!data->cb(data->package, data->curfile, MSICABEXTRACT_BEGINEXTRACT, &path,
402 &attrs, data->user))
403 {
404 /* We're not extracting this file, so free the filename. */
405 msi_free(data->curfile);
406 data->curfile = NULL;
407 goto done;
408 }
409
410 TRACE("extracting %s\n", debugstr_w(path));
411
412 attrs = attrs & (FILE_ATTRIBUTE_READONLY|FILE_ATTRIBUTE_HIDDEN|FILE_ATTRIBUTE_SYSTEM);
413 if (!attrs) attrs = FILE_ATTRIBUTE_NORMAL;
414
415 handle = CreateFileW(path, GENERIC_READ | GENERIC_WRITE, 0,
416 NULL, CREATE_ALWAYS, attrs, NULL);
417 if (handle == INVALID_HANDLE_VALUE)
418 {
419 DWORD err = GetLastError();
420 DWORD attrs2 = GetFileAttributesW(path);
421
422 if (attrs2 == INVALID_FILE_ATTRIBUTES)
423 {
424 ERR("failed to create %s (error %d)\n", debugstr_w(path), err);
425 goto done;
426 }
427 else if (err == ERROR_ACCESS_DENIED && (attrs2 & FILE_ATTRIBUTE_READONLY))
428 {
429 TRACE("removing read-only attribute on %s\n", debugstr_w(path));
430 SetFileAttributesW( path, attrs2 & ~FILE_ATTRIBUTE_READONLY );
431 handle = CreateFileW(path, GENERIC_READ | GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, attrs2, NULL);
432
433 if (handle != INVALID_HANDLE_VALUE) goto done;
434 err = GetLastError();
435 }
436 if (err == ERROR_SHARING_VIOLATION || err == ERROR_USER_MAPPED_FILE)
437 {
438 WCHAR tmpfileW[MAX_PATH], *tmppathW, *p;
439 DWORD len;
440
441 TRACE("file in use, scheduling rename operation\n");
442
443 GetTempFileNameW(szBackSlash, szMsi, 0, tmpfileW);
444 len = strlenW(path) + strlenW(tmpfileW) + 1;
445 if (!(tmppathW = msi_alloc(len * sizeof(WCHAR))))
446 return ERROR_OUTOFMEMORY;
447
448 strcpyW(tmppathW, path);
449 if ((p = strrchrW(tmppathW, '\\'))) *p = 0;
450 strcatW(tmppathW, tmpfileW);
451
452 handle = CreateFileW(tmppathW, GENERIC_READ | GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, attrs, NULL);
453
454 if (handle != INVALID_HANDLE_VALUE &&
455 MoveFileExW(path, NULL, MOVEFILE_DELAY_UNTIL_REBOOT) &&
456 MoveFileExW(tmppathW, path, MOVEFILE_DELAY_UNTIL_REBOOT))
457 {
458 data->package->need_reboot = 1;
459 }
460 else
461 WARN("failed to schedule rename operation %s (error %d)\n", debugstr_w(path), GetLastError());
462
463 msi_free(tmppathW);
464 }
465 else
466 WARN("failed to create %s (error %d)\n", debugstr_w(path), err);
467 }
468
469 done:
470 msi_free(path);
471
472 return (INT_PTR)handle;
473 }
474
475 static INT_PTR cabinet_close_file_info(FDINOTIFICATIONTYPE fdint,
476 PFDINOTIFICATION pfdin)
477 {
478 MSICABDATA *data = pfdin->pv;
479 FILETIME ft;
480 FILETIME ftLocal;
481 HANDLE handle = (HANDLE)pfdin->hf;
482
483 data->mi->is_continuous = FALSE;
484
485 if (!DosDateTimeToFileTime(pfdin->date, pfdin->time, &ft))
486 return -1;
487 if (!LocalFileTimeToFileTime(&ft, &ftLocal))
488 return -1;
489 if (!SetFileTime(handle, &ftLocal, 0, &ftLocal))
490 return -1;
491
492 CloseHandle(handle);
493
494 data->cb(data->package, data->curfile, MSICABEXTRACT_FILEEXTRACTED, NULL, NULL,
495 data->user);
496
497 msi_free(data->curfile);
498 data->curfile = NULL;
499
500 return 1;
501 }
502
503 static INT_PTR CDECL cabinet_notify(FDINOTIFICATIONTYPE fdint, PFDINOTIFICATION pfdin)
504 {
505 switch (fdint)
506 {
507 case fdintPARTIAL_FILE:
508 return cabinet_partial_file(fdint, pfdin);
509
510 case fdintNEXT_CABINET:
511 return cabinet_next_cabinet(fdint, pfdin);
512
513 case fdintCOPY_FILE:
514 return cabinet_copy_file(fdint, pfdin);
515
516 case fdintCLOSE_FILE_INFO:
517 return cabinet_close_file_info(fdint, pfdin);
518
519 default:
520 return 0;
521 }
522 }
523
524 static INT_PTR CDECL cabinet_notify_stream( FDINOTIFICATIONTYPE fdint, PFDINOTIFICATION pfdin )
525 {
526 switch (fdint)
527 {
528 case fdintPARTIAL_FILE:
529 return cabinet_partial_file( fdint, pfdin );
530
531 case fdintNEXT_CABINET:
532 return cabinet_next_cabinet_stream( fdint, pfdin );
533
534 case fdintCOPY_FILE:
535 return cabinet_copy_file( fdint, pfdin );
536
537 case fdintCLOSE_FILE_INFO:
538 return cabinet_close_file_info( fdint, pfdin );
539
540 case fdintCABINET_INFO:
541 return 0;
542
543 default:
544 ERR("Unexpected notification %d\n", fdint);
545 return 0;
546 }
547 }
548
549 static BOOL extract_cabinet( MSIPACKAGE* package, MSIMEDIAINFO *mi, LPVOID data )
550 {
551 LPSTR cabinet, cab_path = NULL;
552 HFDI hfdi;
553 ERF erf;
554 BOOL ret = FALSE;
555
556 TRACE("Extracting %s\n", debugstr_w(mi->cabinet));
557
558 hfdi = FDICreate( cabinet_alloc, cabinet_free, cabinet_open, cabinet_read,
559 cabinet_write, cabinet_close, cabinet_seek, 0, &erf );
560 if (!hfdi)
561 {
562 ERR("FDICreate failed\n");
563 return FALSE;
564 }
565
566 cabinet = strdupWtoA( mi->cabinet );
567 if (!cabinet)
568 goto done;
569
570 cab_path = strdupWtoA( mi->sourcedir );
571 if (!cab_path)
572 goto done;
573
574 ret = FDICopy( hfdi, cabinet, cab_path, 0, cabinet_notify, NULL, data );
575 if (!ret)
576 ERR("FDICopy failed\n");
577
578 done:
579 FDIDestroy( hfdi );
580 msi_free(cabinet );
581 msi_free( cab_path );
582
583 if (ret)
584 mi->is_extracted = TRUE;
585
586 return ret;
587 }
588
589 static BOOL extract_cabinet_stream( MSIPACKAGE *package, MSIMEDIAINFO *mi, LPVOID data )
590 {
591 static char filename[] = {'<','S','T','R','E','A','M','>',0};
592 HFDI hfdi;
593 ERF erf;
594 BOOL ret = FALSE;
595
596 TRACE("Extracting %s\n", debugstr_w(mi->cabinet));
597
598 hfdi = FDICreate( cabinet_alloc, cabinet_free, cabinet_open_stream, cabinet_read_stream,
599 cabinet_write, cabinet_close_stream, cabinet_seek_stream, 0, &erf );
600 if (!hfdi)
601 {
602 ERR("FDICreate failed\n");
603 return FALSE;
604 }
605
606 cab_stream.db = package->db;
607 cab_stream.name = encode_streamname( FALSE, mi->cabinet + 1 );
608 if (!cab_stream.name)
609 goto done;
610
611 ret = FDICopy( hfdi, filename, NULL, 0, cabinet_notify_stream, NULL, data );
612 if (!ret)
613 ERR("FDICopy failed\n");
614
615 done:
616 FDIDestroy( hfdi );
617 msi_free( cab_stream.name );
618
619 if (ret)
620 mi->is_extracted = TRUE;
621
622 return ret;
623 }
624
625 /***********************************************************************
626 * msi_cabextract
627 *
628 * Extract files from a cabinet file or stream.
629 */
630 BOOL msi_cabextract(MSIPACKAGE* package, MSIMEDIAINFO *mi, LPVOID data)
631 {
632 if (mi->cabinet[0] == '#')
633 {
634 return extract_cabinet_stream( package, mi, data );
635 }
636 return extract_cabinet( package, mi, data );
637 }
638
639 void msi_free_media_info(MSIMEDIAINFO *mi)
640 {
641 msi_free(mi->disk_prompt);
642 msi_free(mi->cabinet);
643 msi_free(mi->volume_label);
644 msi_free(mi->first_volume);
645 msi_free(mi);
646 }
647
648 static UINT get_drive_type(const WCHAR *path)
649 {
650 WCHAR root[MAX_PATH + 1];
651
652 strcpyW(root, path);
653 PathStripToRootW(root);
654 PathAddBackslashW(root);
655
656 return GetDriveTypeW(root);
657 }
658
659 static UINT msi_load_media_info(MSIPACKAGE *package, MSIFILE *file, MSIMEDIAINFO *mi)
660 {
661 MSIRECORD *row;
662 LPWSTR source_dir;
663 LPWSTR source;
664 DWORD options;
665
666 static const WCHAR query[] = {
667 'S','E','L','E','C','T',' ','*',' ', 'F','R','O','M',' ',
668 '`','M','e','d','i','a','`',' ','W','H','E','R','E',' ',
669 '`','L','a','s','t','S','e','q','u','e','n','c','e','`',' ','>','=',' ','%','i',
670 ' ','O','R','D','E','R',' ','B','Y',' ','`','D','i','s','k','I','d','`',0};
671
672 row = MSI_QueryGetRecord(package->db, query, file->Sequence);
673 if (!row)
674 {
675 TRACE("Unable to query row\n");
676 return ERROR_FUNCTION_FAILED;
677 }
678
679 mi->is_extracted = FALSE;
680 mi->disk_id = MSI_RecordGetInteger(row, 1);
681 mi->last_sequence = MSI_RecordGetInteger(row, 2);
682 msi_free(mi->disk_prompt);
683 mi->disk_prompt = strdupW(MSI_RecordGetString(row, 3));
684 msi_free(mi->cabinet);
685 mi->cabinet = strdupW(MSI_RecordGetString(row, 4));
686 msi_free(mi->volume_label);
687 mi->volume_label = strdupW(MSI_RecordGetString(row, 5));
688 msiobj_release(&row->hdr);
689
690 if (!mi->first_volume)
691 mi->first_volume = strdupW(mi->volume_label);
692
693 msi_set_sourcedir_props(package, FALSE);
694 source_dir = msi_dup_property(package->db, cszSourceDir);
695 lstrcpyW(mi->sourcedir, source_dir);
696 mi->type = get_drive_type(source_dir);
697
698 options = MSICODE_PRODUCT;
699 if (mi->type == DRIVE_CDROM || mi->type == DRIVE_REMOVABLE)
700 {
701 source = source_dir;
702 options |= MSISOURCETYPE_MEDIA;
703 }
704 else if (package->BaseURL && UrlIsW(package->BaseURL, URLIS_URL))
705 {
706 source = package->BaseURL;
707 options |= MSISOURCETYPE_URL;
708 }
709 else
710 {
711 source = mi->sourcedir;
712 options |= MSISOURCETYPE_NETWORK;
713 }
714
715 msi_package_add_media_disk(package, package->Context,
716 MSICODE_PRODUCT, mi->disk_id,
717 mi->volume_label, mi->disk_prompt);
718
719 msi_package_add_info(package, package->Context,
720 options, INSTALLPROPERTY_LASTUSEDSOURCEW, source);
721
722 msi_free(source_dir);
723 return ERROR_SUCCESS;
724 }
725
726 /* FIXME: search URL sources as well */
727 static UINT find_published_source(MSIPACKAGE *package, MSIMEDIAINFO *mi)
728 {
729 WCHAR source[MAX_PATH];
730 WCHAR volume[MAX_PATH];
731 WCHAR prompt[MAX_PATH];
732 DWORD volumesz, promptsz;
733 DWORD index, size, id;
734 WCHAR last_type[2];
735 UINT r;
736
737 size = 2;
738 r = MsiSourceListGetInfoW(package->ProductCode, NULL,
739 package->Context, MSICODE_PRODUCT,
740 INSTALLPROPERTY_LASTUSEDTYPEW, last_type, &size);
741 if (r != ERROR_SUCCESS)
742 return r;
743
744 size = MAX_PATH;
745 r = MsiSourceListGetInfoW(package->ProductCode, NULL,
746 package->Context, MSICODE_PRODUCT,
747 INSTALLPROPERTY_LASTUSEDSOURCEW, source, &size);
748 if (r != ERROR_SUCCESS)
749 return r;
750
751 index = 0;
752 volumesz = MAX_PATH;
753 promptsz = MAX_PATH;
754
755 if (last_type[0] == 'n')
756 {
757 while (MsiSourceListEnumSourcesW(package->ProductCode, NULL,
758 package->Context,
759 MSISOURCETYPE_NETWORK, index++,
760 volume, &volumesz) == ERROR_SUCCESS)
761 {
762 if (!strncmpiW(source, volume, strlenW(source)))
763 {
764 lstrcpyW(mi->sourcedir, source);
765 TRACE("Found network source %s\n", debugstr_w(mi->sourcedir));
766 return ERROR_SUCCESS;
767 }
768 }
769 }
770
771 index = 0;
772 volumesz = MAX_PATH;
773 promptsz = MAX_PATH;
774 while (MsiSourceListEnumMediaDisksW(package->ProductCode, NULL,
775 package->Context,
776 MSICODE_PRODUCT, index++, &id,
777 volume, &volumesz, prompt, &promptsz) == ERROR_SUCCESS)
778 {
779 mi->disk_id = id;
780 mi->volume_label = msi_realloc(mi->volume_label, ++volumesz * sizeof(WCHAR));
781 lstrcpyW(mi->volume_label, volume);
782 mi->disk_prompt = msi_realloc(mi->disk_prompt, ++promptsz * sizeof(WCHAR));
783 lstrcpyW(mi->disk_prompt, prompt);
784
785 if (source_matches_volume(mi, source))
786 {
787 /* FIXME: what about SourceDir */
788 lstrcpyW(mi->sourcedir, source);
789 TRACE("Found disk source %s\n", debugstr_w(mi->sourcedir));
790 return ERROR_SUCCESS;
791 }
792 }
793
794 return ERROR_FUNCTION_FAILED;
795 }
796
797 UINT ready_media(MSIPACKAGE *package, MSIFILE *file, MSIMEDIAINFO *mi)
798 {
799 UINT rc = ERROR_SUCCESS;
800 WCHAR *cabinet_file;
801
802 /* media info for continuous cabinet is already loaded */
803 if (mi->is_continuous)
804 return ERROR_SUCCESS;
805
806 rc = msi_load_media_info(package, file, mi);
807 if (rc != ERROR_SUCCESS)
808 {
809 ERR("Unable to load media info %u\n", rc);
810 return ERROR_FUNCTION_FAILED;
811 }
812
813 /* cabinet is internal, no checks needed */
814 if (!mi->cabinet || mi->cabinet[0] == '#')
815 return ERROR_SUCCESS;
816
817 cabinet_file = get_cabinet_filename(mi);
818
819 /* package should be downloaded */
820 if (file->IsCompressed &&
821 GetFileAttributesW(cabinet_file) == INVALID_FILE_ATTRIBUTES &&
822 package->BaseURL && UrlIsW(package->BaseURL, URLIS_URL))
823 {
824 WCHAR temppath[MAX_PATH], *p;
825
826 rc = msi_download_file(cabinet_file, temppath);
827 if (rc != ERROR_SUCCESS)
828 {
829 ERR("Failed to download %s (%u)\n", debugstr_w(cabinet_file), rc);
830 msi_free(cabinet_file);
831 return rc;
832 }
833 if ((p = strrchrW(temppath, '\\'))) *p = 0;
834 strcpyW(mi->sourcedir, temppath);
835 msi_free(mi->cabinet);
836 mi->cabinet = strdupW(p + 1);
837
838 msi_free(cabinet_file);
839 return ERROR_SUCCESS;
840 }
841
842 /* check volume matches, change media if not */
843 if (mi->volume_label && mi->disk_id > 1 &&
844 lstrcmpW(mi->first_volume, mi->volume_label))
845 {
846 LPWSTR source = msi_dup_property(package->db, cszSourceDir);
847 BOOL matches;
848
849 matches = source_matches_volume(mi, source);
850 msi_free(source);
851
852 if ((mi->type == DRIVE_CDROM || mi->type == DRIVE_REMOVABLE) && !matches)
853 {
854 rc = msi_change_media(package, mi);
855 if (rc != ERROR_SUCCESS)
856 {
857 msi_free(cabinet_file);
858 return rc;
859 }
860 }
861 }
862
863 if (file->IsCompressed &&
864 GetFileAttributesW(cabinet_file) == INVALID_FILE_ATTRIBUTES)
865 {
866 rc = find_published_source(package, mi);
867 if (rc != ERROR_SUCCESS)
868 {
869 ERR("Cabinet not found: %s\n", debugstr_w(cabinet_file));
870 msi_free(cabinet_file);
871 return ERROR_INSTALL_FAILURE;
872 }
873 }
874
875 msi_free(cabinet_file);
876 return ERROR_SUCCESS;
877 }