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