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