dplayx: Adjust GetCaps behaviour to documentation
[wine/gsoc_dplay.git] / dlls / msi / media.c
blob2de3252d79dce6ada387b883353add487c673650
1 /*
2 * Implementation of the Microsoft Installer (msi.dll)
4 * Copyright 2008 James Hawkins
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.
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.
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
21 #include <stdarg.h>
23 #include "windef.h"
24 #include "winerror.h"
25 #include "wine/debug.h"
26 #include "fdi.h"
27 #include "msipriv.h"
28 #include "winuser.h"
29 #include "winreg.h"
30 #include "shlwapi.h"
31 #include "wine/unicode.h"
33 WINE_DEFAULT_DEBUG_CHANNEL(msi);
35 /* from msvcrt/fcntl.h */
36 #define _O_RDONLY 0
37 #define _O_WRONLY 1
38 #define _O_RDWR 2
39 #define _O_ACCMODE (_O_RDONLY|_O_WRONLY|_O_RDWR)
40 #define _O_APPEND 0x0008
41 #define _O_RANDOM 0x0010
42 #define _O_SEQUENTIAL 0x0020
43 #define _O_TEMPORARY 0x0040
44 #define _O_NOINHERIT 0x0080
45 #define _O_CREAT 0x0100
46 #define _O_TRUNC 0x0200
47 #define _O_EXCL 0x0400
48 #define _O_SHORT_LIVED 0x1000
49 #define _O_TEXT 0x4000
50 #define _O_BINARY 0x8000
52 static BOOL source_matches_volume(MSIMEDIAINFO *mi, LPWSTR source_root)
54 WCHAR volume_name[MAX_PATH + 1];
56 if (!GetVolumeInformationW(source_root, volume_name, MAX_PATH + 1,
57 NULL, NULL, NULL, NULL, 0))
59 ERR("Failed to get volume information\n");
60 return FALSE;
63 return !lstrcmpW(mi->volume_label, volume_name);
66 static UINT msi_change_media(MSIPACKAGE *package, MSIMEDIAINFO *mi)
68 LPSTR msg;
69 LPWSTR error, error_dialog;
70 LPWSTR source_dir;
71 UINT r = ERROR_SUCCESS;
73 static const WCHAR szUILevel[] = {'U','I','L','e','v','e','l',0};
74 static const WCHAR error_prop[] = {'E','r','r','o','r','D','i','a','l','o','g',0};
76 if ((msi_get_property_int(package, szUILevel, 0) & INSTALLUILEVEL_MASK) ==
77 INSTALLUILEVEL_NONE && !gUIHandlerA)
78 return ERROR_SUCCESS;
80 error = generate_error_string(package, 1302, 1, mi->disk_prompt);
81 error_dialog = msi_dup_property(package, error_prop);
82 source_dir = msi_dup_property(package, cszSourceDir);
83 PathStripToRootW(source_dir);
85 while (r == ERROR_SUCCESS &&
86 !source_matches_volume(mi, source_dir))
88 r = msi_spawn_error_dialog(package, error_dialog, error);
90 if (gUIHandlerA)
92 msg = strdupWtoA(error);
93 gUIHandlerA(gUIContext, MB_RETRYCANCEL | INSTALLMESSAGE_ERROR, msg);
94 msi_free(msg);
98 msi_free(error);
99 msi_free(error_dialog);
100 msi_free(source_dir);
102 return r;
105 static UINT writeout_cabinet_stream(MSIPACKAGE *package, LPCWSTR stream,
106 WCHAR* source)
108 UINT rc;
109 USHORT* data;
110 UINT size;
111 DWORD write;
112 HANDLE hfile;
113 WCHAR tmp[MAX_PATH];
115 static const WCHAR cszTempFolder[]= {
116 'T','e','m','p','F','o','l','d','e','r',0};
118 rc = read_raw_stream_data(package->db, stream, &data, &size);
119 if (rc != ERROR_SUCCESS)
120 return rc;
122 write = MAX_PATH;
123 if (MSI_GetPropertyW(package, cszTempFolder, tmp, &write))
124 GetTempPathW(MAX_PATH, tmp);
126 GetTempFileNameW(tmp, stream, 0, source);
128 track_tempfile(package, source);
129 hfile = CreateFileW(source, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS,
130 FILE_ATTRIBUTE_NORMAL, NULL);
132 if (hfile == INVALID_HANDLE_VALUE)
134 ERR("Unable to create file %s\n", debugstr_w(source));
135 rc = ERROR_FUNCTION_FAILED;
136 goto end;
139 WriteFile(hfile, data, size, &write, NULL);
140 CloseHandle(hfile);
141 TRACE("wrote %i bytes to %s\n", write, debugstr_w(source));
143 end:
144 msi_free(data);
145 return rc;
148 static void * CDECL cabinet_alloc(ULONG cb)
150 return msi_alloc(cb);
153 static void CDECL cabinet_free(void *pv)
155 msi_free(pv);
158 static INT_PTR CDECL cabinet_open(char *pszFile, int oflag, int pmode)
160 HANDLE handle;
161 DWORD dwAccess = 0;
162 DWORD dwShareMode = 0;
163 DWORD dwCreateDisposition = OPEN_EXISTING;
165 switch (oflag & _O_ACCMODE)
167 case _O_RDONLY:
168 dwAccess = GENERIC_READ;
169 dwShareMode = FILE_SHARE_READ | FILE_SHARE_DELETE;
170 break;
171 case _O_WRONLY:
172 dwAccess = GENERIC_WRITE;
173 dwShareMode = FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE;
174 break;
175 case _O_RDWR:
176 dwAccess = GENERIC_READ | GENERIC_WRITE;
177 dwShareMode = FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE;
178 break;
181 if ((oflag & (_O_CREAT | _O_EXCL)) == (_O_CREAT | _O_EXCL))
182 dwCreateDisposition = CREATE_NEW;
183 else if (oflag & _O_CREAT)
184 dwCreateDisposition = CREATE_ALWAYS;
186 handle = CreateFileA(pszFile, dwAccess, dwShareMode, NULL,
187 dwCreateDisposition, 0, NULL);
188 if (handle == INVALID_HANDLE_VALUE)
189 return 0;
191 return (INT_PTR)handle;
194 static UINT CDECL cabinet_read(INT_PTR hf, void *pv, UINT cb)
196 HANDLE handle = (HANDLE)hf;
197 DWORD read;
199 if (ReadFile(handle, pv, cb, &read, NULL))
200 return read;
202 return 0;
205 static UINT CDECL cabinet_write(INT_PTR hf, void *pv, UINT cb)
207 HANDLE handle = (HANDLE)hf;
208 DWORD written;
210 if (WriteFile(handle, pv, cb, &written, NULL))
211 return written;
213 return 0;
216 static int CDECL cabinet_close(INT_PTR hf)
218 HANDLE handle = (HANDLE)hf;
219 return CloseHandle(handle) ? 0 : -1;
222 static LONG CDECL cabinet_seek(INT_PTR hf, LONG dist, int seektype)
224 HANDLE handle = (HANDLE)hf;
225 /* flags are compatible and so are passed straight through */
226 return SetFilePointer(handle, dist, NULL, seektype);
229 static UINT CDECL msi_media_get_disk_info(MSIPACKAGE *package, MSIMEDIAINFO *mi)
231 MSIRECORD *row;
232 LPWSTR ptr;
234 static const WCHAR query[] = {
235 'S','E','L','E','C','T',' ','*',' ', 'F','R','O','M',' ',
236 '`','M','e','d','i','a','`',' ','W','H','E','R','E',' ',
237 '`','D','i','s','k','I','d','`',' ','=',' ','%','i',0};
239 row = MSI_QueryGetRecord(package->db, query, mi->disk_id);
240 if (!row)
242 TRACE("Unable to query row\n");
243 return ERROR_FUNCTION_FAILED;
246 mi->disk_prompt = strdupW(MSI_RecordGetString(row, 3));
247 mi->cabinet = strdupW(MSI_RecordGetString(row, 4));
248 mi->volume_label = strdupW(MSI_RecordGetString(row, 5));
250 if (!mi->first_volume)
251 mi->first_volume = strdupW(mi->volume_label);
253 ptr = strrchrW(mi->source, '\\') + 1;
254 lstrcpyW(ptr, mi->cabinet);
255 msiobj_release(&row->hdr);
257 return ERROR_SUCCESS;
260 static INT_PTR cabinet_partial_file(FDINOTIFICATIONTYPE fdint,
261 PFDINOTIFICATION pfdin)
263 MSICABDATA *data = pfdin->pv;
264 data->mi->is_continuous = FALSE;
265 return 0;
268 static INT_PTR cabinet_next_cabinet(FDINOTIFICATIONTYPE fdint,
269 PFDINOTIFICATION pfdin)
271 MSICABDATA *data = pfdin->pv;
272 MSIMEDIAINFO *mi = data->mi;
273 LPWSTR cab = strdupAtoW(pfdin->psz1);
274 INT_PTR res = -1;
275 UINT rc;
277 msi_free(mi->disk_prompt);
278 msi_free(mi->cabinet);
279 msi_free(mi->volume_label);
280 mi->disk_prompt = NULL;
281 mi->cabinet = NULL;
282 mi->volume_label = NULL;
284 mi->disk_id++;
285 mi->is_continuous = TRUE;
287 rc = msi_media_get_disk_info(data->package, mi);
288 if (rc != ERROR_SUCCESS)
290 ERR("Failed to get next cabinet information: %d\n", rc);
291 goto done;
294 if (lstrcmpiW(mi->cabinet, cab))
296 ERR("Continuous cabinet does not match the next cabinet in the Media table\n");
297 goto done;
300 TRACE("Searching for %s\n", debugstr_w(mi->source));
302 res = 0;
303 if (GetFileAttributesW(mi->source) == INVALID_FILE_ATTRIBUTES)
305 if (msi_change_media(data->package, mi) != ERROR_SUCCESS)
306 res = -1;
309 done:
310 msi_free(cab);
311 return res;
314 static INT_PTR cabinet_copy_file(FDINOTIFICATIONTYPE fdint,
315 PFDINOTIFICATION pfdin)
317 MSICABDATA *data = pfdin->pv;
318 HANDLE handle = 0;
319 LPWSTR path = NULL;
320 DWORD attrs;
322 data->curfile = strdupAtoW(pfdin->psz1);
323 if (!data->cb(data->package, data->curfile, MSICABEXTRACT_BEGINEXTRACT, &path,
324 &attrs, data->user))
325 goto done;
327 TRACE("extracting %s\n", debugstr_w(path));
329 attrs = attrs & (FILE_ATTRIBUTE_READONLY|FILE_ATTRIBUTE_HIDDEN|FILE_ATTRIBUTE_SYSTEM);
330 if (!attrs) attrs = FILE_ATTRIBUTE_NORMAL;
332 handle = CreateFileW(path, GENERIC_READ | GENERIC_WRITE, 0,
333 NULL, CREATE_ALWAYS, attrs, NULL);
334 if (handle == INVALID_HANDLE_VALUE)
336 if (GetFileAttributesW(path) == INVALID_FILE_ATTRIBUTES)
337 ERR("failed to create %s (error %d)\n",
338 debugstr_w(path), GetLastError());
340 goto done;
343 done:
344 msi_free(path);
346 return (INT_PTR)handle;
349 static INT_PTR cabinet_close_file_info(FDINOTIFICATIONTYPE fdint,
350 PFDINOTIFICATION pfdin)
352 MSICABDATA *data = pfdin->pv;
353 FILETIME ft;
354 FILETIME ftLocal;
355 HANDLE handle = (HANDLE)pfdin->hf;
357 data->mi->is_continuous = FALSE;
359 if (!DosDateTimeToFileTime(pfdin->date, pfdin->time, &ft))
360 return -1;
361 if (!LocalFileTimeToFileTime(&ft, &ftLocal))
362 return -1;
363 if (!SetFileTime(handle, &ftLocal, 0, &ftLocal))
364 return -1;
366 CloseHandle(handle);
368 data->cb(data->package, data->curfile, MSICABEXTRACT_FILEEXTRACTED, NULL, NULL,
369 data->user);
371 msi_free(data->curfile);
372 data->curfile = NULL;
374 return 1;
377 static INT_PTR CDECL cabinet_notify(FDINOTIFICATIONTYPE fdint, PFDINOTIFICATION pfdin)
379 TRACE("(%d)\n", fdint);
381 switch (fdint)
383 case fdintPARTIAL_FILE:
384 return cabinet_partial_file(fdint, pfdin);
386 case fdintNEXT_CABINET:
387 return cabinet_next_cabinet(fdint, pfdin);
389 case fdintCOPY_FILE:
390 return cabinet_copy_file(fdint, pfdin);
392 case fdintCLOSE_FILE_INFO:
393 return cabinet_close_file_info(fdint, pfdin);
395 default:
396 return 0;
400 /***********************************************************************
401 * msi_cabextract
403 * Extract files from a cab file.
405 BOOL msi_cabextract(MSIPACKAGE* package, MSIMEDIAINFO *mi, LPVOID data)
407 LPSTR cabinet, cab_path = NULL;
408 LPWSTR ptr;
409 HFDI hfdi;
410 ERF erf;
411 BOOL ret = FALSE;
413 TRACE("Extracting %s\n", debugstr_w(mi->source));
415 hfdi = FDICreate(cabinet_alloc, cabinet_free, cabinet_open, cabinet_read,
416 cabinet_write, cabinet_close, cabinet_seek, 0, &erf);
417 if (!hfdi)
419 ERR("FDICreate failed\n");
420 return FALSE;
423 ptr = strrchrW(mi->source, '\\') + 1;
424 cabinet = strdupWtoA(ptr);
425 if (!cabinet)
426 goto done;
428 cab_path = strdupWtoA(mi->source);
429 if (!cab_path)
430 goto done;
432 cab_path[ptr - mi->source] = '\0';
434 ret = FDICopy(hfdi, cabinet, cab_path, 0, cabinet_notify, NULL, data);
435 if (!ret)
436 ERR("FDICopy failed\n");
438 done:
439 FDIDestroy(hfdi);
440 msi_free(cabinet);
441 msi_free(cab_path);
443 if (ret)
444 mi->is_extracted = TRUE;
446 return ret;
449 void msi_free_media_info(MSIMEDIAINFO *mi)
451 msi_free(mi->disk_prompt);
452 msi_free(mi->cabinet);
453 msi_free(mi->volume_label);
454 msi_free(mi->first_volume);
455 msi_free(mi);
458 static UINT msi_load_media_info(MSIPACKAGE *package, MSIFILE *file, MSIMEDIAINFO *mi)
460 MSIRECORD *row;
461 LPWSTR source_dir;
462 LPWSTR source;
463 DWORD options;
464 UINT r;
466 static const WCHAR query[] = {
467 'S','E','L','E','C','T',' ','*',' ', 'F','R','O','M',' ',
468 '`','M','e','d','i','a','`',' ','W','H','E','R','E',' ',
469 '`','L','a','s','t','S','e','q','u','e','n','c','e','`',' ','>','=',
470 ' ','%','i',' ','A','N','D',' ','`','D','i','s','k','I','d','`',' ','>','=',
471 ' ','%','i',' ','O','R','D','E','R',' ','B','Y',' ',
472 '`','D','i','s','k','I','d','`',0};
474 row = MSI_QueryGetRecord(package->db, query, file->Sequence, mi->disk_id);
475 if (!row)
477 TRACE("Unable to query row\n");
478 return ERROR_FUNCTION_FAILED;
481 mi->is_extracted = FALSE;
482 mi->disk_id = MSI_RecordGetInteger(row, 1);
483 mi->last_sequence = MSI_RecordGetInteger(row, 2);
484 msi_free(mi->disk_prompt);
485 mi->disk_prompt = strdupW(MSI_RecordGetString(row, 3));
486 msi_free(mi->cabinet);
487 mi->cabinet = strdupW(MSI_RecordGetString(row, 4));
488 msi_free(mi->volume_label);
489 mi->volume_label = strdupW(MSI_RecordGetString(row, 5));
490 msiobj_release(&row->hdr);
492 if (!mi->first_volume)
493 mi->first_volume = strdupW(mi->volume_label);
495 source_dir = msi_dup_property(package, cszSourceDir);
496 lstrcpyW(mi->source, source_dir);
498 PathStripToRootW(source_dir);
499 mi->type = GetDriveTypeW(source_dir);
501 if (file->IsCompressed && mi->cabinet)
503 if (mi->cabinet[0] == '#')
505 r = writeout_cabinet_stream(package, &mi->cabinet[1], mi->source);
506 if (r != ERROR_SUCCESS)
508 ERR("Failed to extract cabinet stream\n");
509 return ERROR_FUNCTION_FAILED;
512 else
513 lstrcatW(mi->source, mi->cabinet);
516 options = MSICODE_PRODUCT;
517 if (mi->type == DRIVE_CDROM || mi->type == DRIVE_REMOVABLE)
519 source = source_dir;
520 options |= MSISOURCETYPE_MEDIA;
522 else if (package->BaseURL && UrlIsW(package->BaseURL, URLIS_URL))
524 source = package->BaseURL;
525 options |= MSISOURCETYPE_URL;
527 else
529 source = mi->source;
530 options |= MSISOURCETYPE_NETWORK;
533 msi_package_add_media_disk(package, package->Context,
534 MSICODE_PRODUCT, mi->disk_id,
535 mi->volume_label, mi->disk_prompt);
537 msi_package_add_info(package, package->Context,
538 options, INSTALLPROPERTY_LASTUSEDSOURCEW, source);
540 msi_free(source_dir);
541 return ERROR_SUCCESS;
544 /* FIXME: search NETWORK and URL sources as well */
545 static UINT find_published_source(MSIPACKAGE *package, MSIMEDIAINFO *mi)
547 WCHAR source[MAX_PATH];
548 WCHAR volume[MAX_PATH];
549 WCHAR prompt[MAX_PATH];
550 DWORD volumesz, promptsz;
551 DWORD index, size, id;
552 UINT r;
554 size = MAX_PATH;
555 r = MsiSourceListGetInfoW(package->ProductCode, NULL,
556 package->Context, MSICODE_PRODUCT,
557 INSTALLPROPERTY_LASTUSEDSOURCEW, source, &size);
558 if (r != ERROR_SUCCESS)
559 return r;
561 index = 0;
562 volumesz = MAX_PATH;
563 promptsz = MAX_PATH;
564 while (MsiSourceListEnumMediaDisksW(package->ProductCode, NULL,
565 package->Context,
566 MSICODE_PRODUCT, index++, &id,
567 volume, &volumesz, prompt, &promptsz) == ERROR_SUCCESS)
569 mi->disk_id = id;
570 mi->volume_label = msi_realloc(mi->volume_label, ++volumesz * sizeof(WCHAR));
571 lstrcpyW(mi->volume_label, volume);
572 mi->disk_prompt = msi_realloc(mi->disk_prompt, ++promptsz * sizeof(WCHAR));
573 lstrcpyW(mi->disk_prompt, prompt);
575 if (source_matches_volume(mi, source))
577 /* FIXME: what about SourceDir */
578 lstrcpyW(mi->source, source);
579 lstrcatW(mi->source, mi->cabinet);
580 return ERROR_SUCCESS;
584 return ERROR_FUNCTION_FAILED;
587 UINT ready_media(MSIPACKAGE *package, MSIFILE *file, MSIMEDIAINFO *mi)
589 UINT rc = ERROR_SUCCESS;
591 /* media info for continuous cabinet is already loaded */
592 if (mi->is_continuous)
593 return ERROR_SUCCESS;
595 rc = msi_load_media_info(package, file, mi);
596 if (rc != ERROR_SUCCESS)
598 ERR("Unable to load media info\n");
599 return ERROR_FUNCTION_FAILED;
602 /* cabinet is internal, no checks needed */
603 if (!mi->cabinet || mi->cabinet[0] == '#')
604 return ERROR_SUCCESS;
606 /* package should be downloaded */
607 if (file->IsCompressed &&
608 GetFileAttributesW(mi->source) == INVALID_FILE_ATTRIBUTES &&
609 package->BaseURL && UrlIsW(package->BaseURL, URLIS_URL))
611 WCHAR temppath[MAX_PATH];
613 msi_download_file(mi->source, temppath);
614 lstrcpyW(mi->source, temppath);
615 return ERROR_SUCCESS;
618 /* check volume matches, change media if not */
619 if (mi->volume_label && mi->disk_id > 1 &&
620 lstrcmpW(mi->first_volume, mi->volume_label))
622 LPWSTR source = msi_dup_property(package, cszSourceDir);
623 BOOL matches;
625 matches = source_matches_volume(mi, source);
626 msi_free(source);
628 if ((mi->type == DRIVE_CDROM || mi->type == DRIVE_REMOVABLE) && !matches)
630 rc = msi_change_media(package, mi);
631 if (rc != ERROR_SUCCESS)
632 return rc;
636 if (file->IsCompressed &&
637 GetFileAttributesW(mi->source) == INVALID_FILE_ATTRIBUTES)
639 rc = find_published_source(package, mi);
640 if (rc != ERROR_SUCCESS)
642 ERR("Cabinet not found: %s\n", debugstr_w(mi->source));
643 return ERROR_INSTALL_FAILURE;
647 return ERROR_SUCCESS;