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