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