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