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
25 #include "wine/debug.h"
31 #include "wine/unicode.h"
33 WINE_DEFAULT_DEBUG_CHANNEL(msi);
35 /* from msvcrt/fcntl.h */
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, LPCWSTR source_root)
54 WCHAR volume_name[MAX_PATH + 1];
55 WCHAR root[MAX_PATH + 1];
57 strcpyW(root, source_root);
58 PathStripToRootW(root);
59 PathAddBackslashW(root);
61 if (!GetVolumeInformationW(root, volume_name, MAX_PATH + 1,
62 NULL, NULL, NULL, NULL, 0))
64 ERR("Failed to get volume information\n");
68 return !lstrcmpW(mi->volume_label, volume_name);
71 static UINT msi_change_media(MSIPACKAGE *package, MSIMEDIAINFO *mi)
74 LPWSTR error, error_dialog;
76 UINT r = ERROR_SUCCESS;
78 static const WCHAR szUILevel[] = {'U','I','L','e','v','e','l',0};
79 static const WCHAR error_prop[] = {'E','r','r','o','r','D','i','a','l','o','g',0};
81 if ((msi_get_property_int(package, szUILevel, 0) & INSTALLUILEVEL_MASK) ==
82 INSTALLUILEVEL_NONE && !gUIHandlerA)
85 error = generate_error_string(package, 1302, 1, mi->disk_prompt);
86 error_dialog = msi_dup_property(package, error_prop);
87 source_dir = msi_dup_property(package, cszSourceDir);
89 while (r == ERROR_SUCCESS &&
90 !source_matches_volume(mi, source_dir))
92 r = msi_spawn_error_dialog(package, error_dialog, error);
96 msg = strdupWtoA(error);
97 gUIHandlerA(gUIContext, MB_RETRYCANCEL | INSTALLMESSAGE_ERROR, msg);
103 msi_free(error_dialog);
104 msi_free(source_dir);
109 static UINT writeout_cabinet_stream(MSIPACKAGE *package, LPCWSTR stream,
119 static const WCHAR cszTempFolder[]= {
120 'T','e','m','p','F','o','l','d','e','r',0};
122 rc = read_raw_stream_data(package->db, stream, &data, &size);
123 if (rc != ERROR_SUCCESS)
127 if (MSI_GetPropertyW(package, cszTempFolder, tmp, &write))
128 GetTempPathW(MAX_PATH, tmp);
130 GetTempFileNameW(tmp, stream, 0, source);
132 track_tempfile(package, source);
133 hfile = CreateFileW(source, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS,
134 FILE_ATTRIBUTE_NORMAL, NULL);
136 if (hfile == INVALID_HANDLE_VALUE)
138 ERR("Unable to create file %s\n", debugstr_w(source));
139 rc = ERROR_FUNCTION_FAILED;
143 WriteFile(hfile, data, size, &write, NULL);
145 TRACE("wrote %i bytes to %s\n", write, debugstr_w(source));
152 static void * CDECL cabinet_alloc(ULONG cb)
154 return msi_alloc(cb);
157 static void CDECL cabinet_free(void *pv)
162 static INT_PTR CDECL cabinet_open(char *pszFile, int oflag, int pmode)
166 DWORD dwShareMode = 0;
167 DWORD dwCreateDisposition = OPEN_EXISTING;
169 switch (oflag & _O_ACCMODE)
172 dwAccess = GENERIC_READ;
173 dwShareMode = FILE_SHARE_READ | FILE_SHARE_DELETE;
176 dwAccess = GENERIC_WRITE;
177 dwShareMode = FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE;
180 dwAccess = GENERIC_READ | GENERIC_WRITE;
181 dwShareMode = FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE;
185 if ((oflag & (_O_CREAT | _O_EXCL)) == (_O_CREAT | _O_EXCL))
186 dwCreateDisposition = CREATE_NEW;
187 else if (oflag & _O_CREAT)
188 dwCreateDisposition = CREATE_ALWAYS;
190 handle = CreateFileA(pszFile, dwAccess, dwShareMode, NULL,
191 dwCreateDisposition, 0, NULL);
192 if (handle == INVALID_HANDLE_VALUE)
195 return (INT_PTR)handle;
198 static UINT CDECL cabinet_read(INT_PTR hf, void *pv, UINT cb)
200 HANDLE handle = (HANDLE)hf;
203 if (ReadFile(handle, pv, cb, &read, NULL))
209 static UINT CDECL cabinet_write(INT_PTR hf, void *pv, UINT cb)
211 HANDLE handle = (HANDLE)hf;
214 if (WriteFile(handle, pv, cb, &written, NULL))
220 static int CDECL cabinet_close(INT_PTR hf)
222 HANDLE handle = (HANDLE)hf;
223 return CloseHandle(handle) ? 0 : -1;
226 static LONG CDECL cabinet_seek(INT_PTR hf, LONG dist, int seektype)
228 HANDLE handle = (HANDLE)hf;
229 /* flags are compatible and so are passed straight through */
230 return SetFilePointer(handle, dist, NULL, seektype);
233 static UINT CDECL msi_media_get_disk_info(MSIPACKAGE *package, MSIMEDIAINFO *mi)
238 static const WCHAR query[] = {
239 'S','E','L','E','C','T',' ','*',' ', 'F','R','O','M',' ',
240 '`','M','e','d','i','a','`',' ','W','H','E','R','E',' ',
241 '`','D','i','s','k','I','d','`',' ','=',' ','%','i',0};
243 row = MSI_QueryGetRecord(package->db, query, mi->disk_id);
246 TRACE("Unable to query row\n");
247 return ERROR_FUNCTION_FAILED;
250 mi->disk_prompt = strdupW(MSI_RecordGetString(row, 3));
251 mi->cabinet = strdupW(MSI_RecordGetString(row, 4));
252 mi->volume_label = strdupW(MSI_RecordGetString(row, 5));
254 if (!mi->first_volume)
255 mi->first_volume = strdupW(mi->volume_label);
257 ptr = strrchrW(mi->source, '\\') + 1;
258 lstrcpyW(ptr, mi->cabinet);
259 msiobj_release(&row->hdr);
261 return ERROR_SUCCESS;
264 static INT_PTR cabinet_partial_file(FDINOTIFICATIONTYPE fdint,
265 PFDINOTIFICATION pfdin)
267 MSICABDATA *data = pfdin->pv;
268 data->mi->is_continuous = FALSE;
272 static INT_PTR cabinet_next_cabinet(FDINOTIFICATIONTYPE fdint,
273 PFDINOTIFICATION pfdin)
275 MSICABDATA *data = pfdin->pv;
276 MSIMEDIAINFO *mi = data->mi;
277 LPWSTR cab = strdupAtoW(pfdin->psz1);
281 msi_free(mi->disk_prompt);
282 msi_free(mi->cabinet);
283 msi_free(mi->volume_label);
284 mi->disk_prompt = NULL;
286 mi->volume_label = NULL;
289 mi->is_continuous = TRUE;
291 rc = msi_media_get_disk_info(data->package, mi);
292 if (rc != ERROR_SUCCESS)
294 ERR("Failed to get next cabinet information: %d\n", rc);
298 if (lstrcmpiW(mi->cabinet, cab))
300 ERR("Continuous cabinet does not match the next cabinet in the Media table\n");
304 TRACE("Searching for %s\n", debugstr_w(mi->source));
307 if (GetFileAttributesW(mi->source) == INVALID_FILE_ATTRIBUTES)
309 if (msi_change_media(data->package, mi) != ERROR_SUCCESS)
318 static INT_PTR cabinet_copy_file(FDINOTIFICATIONTYPE fdint,
319 PFDINOTIFICATION pfdin)
321 MSICABDATA *data = pfdin->pv;
326 data->curfile = strdupAtoW(pfdin->psz1);
327 if (!data->cb(data->package, data->curfile, MSICABEXTRACT_BEGINEXTRACT, &path,
331 TRACE("extracting %s\n", debugstr_w(path));
333 attrs = attrs & (FILE_ATTRIBUTE_READONLY|FILE_ATTRIBUTE_HIDDEN|FILE_ATTRIBUTE_SYSTEM);
334 if (!attrs) attrs = FILE_ATTRIBUTE_NORMAL;
336 handle = CreateFileW(path, GENERIC_READ | GENERIC_WRITE, 0,
337 NULL, CREATE_ALWAYS, attrs, NULL);
338 if (handle == INVALID_HANDLE_VALUE)
340 DWORD err = GetLastError();
341 DWORD attrs = GetFileAttributesW(path);
343 if (attrs == INVALID_FILE_ATTRIBUTES)
344 ERR("failed to create %s (error %d)\n", debugstr_w(path), err);
345 else if (err == ERROR_ACCESS_DENIED && (attrs & FILE_ATTRIBUTE_READONLY))
347 TRACE("removing read-only attribute on %s\n", debugstr_w(path));
348 SetFileAttributesW( path, attrs & ~FILE_ATTRIBUTE_READONLY );
349 handle = CreateFileW(path, GENERIC_READ | GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, attrs, NULL);
352 WARN("failed to create %s (error %d)\n", debugstr_w(path), err);
358 return (INT_PTR)handle;
361 static INT_PTR cabinet_close_file_info(FDINOTIFICATIONTYPE fdint,
362 PFDINOTIFICATION pfdin)
364 MSICABDATA *data = pfdin->pv;
367 HANDLE handle = (HANDLE)pfdin->hf;
369 data->mi->is_continuous = FALSE;
371 if (!DosDateTimeToFileTime(pfdin->date, pfdin->time, &ft))
373 if (!LocalFileTimeToFileTime(&ft, &ftLocal))
375 if (!SetFileTime(handle, &ftLocal, 0, &ftLocal))
380 data->cb(data->package, data->curfile, MSICABEXTRACT_FILEEXTRACTED, NULL, NULL,
383 msi_free(data->curfile);
384 data->curfile = NULL;
389 static INT_PTR CDECL cabinet_notify(FDINOTIFICATIONTYPE fdint, PFDINOTIFICATION pfdin)
391 TRACE("(%d)\n", fdint);
395 case fdintPARTIAL_FILE:
396 return cabinet_partial_file(fdint, pfdin);
398 case fdintNEXT_CABINET:
399 return cabinet_next_cabinet(fdint, pfdin);
402 return cabinet_copy_file(fdint, pfdin);
404 case fdintCLOSE_FILE_INFO:
405 return cabinet_close_file_info(fdint, pfdin);
412 /***********************************************************************
415 * Extract files from a cab file.
417 BOOL msi_cabextract(MSIPACKAGE* package, MSIMEDIAINFO *mi, LPVOID data)
419 LPSTR cabinet, cab_path = NULL;
425 TRACE("Extracting %s\n", debugstr_w(mi->source));
427 hfdi = FDICreate(cabinet_alloc, cabinet_free, cabinet_open, cabinet_read,
428 cabinet_write, cabinet_close, cabinet_seek, 0, &erf);
431 ERR("FDICreate failed\n");
435 ptr = strrchrW(mi->source, '\\') + 1;
436 cabinet = strdupWtoA(ptr);
440 cab_path = strdupWtoA(mi->source);
444 cab_path[ptr - mi->source] = '\0';
446 ret = FDICopy(hfdi, cabinet, cab_path, 0, cabinet_notify, NULL, data);
448 ERR("FDICopy failed\n");
456 mi->is_extracted = TRUE;
461 void msi_free_media_info(MSIMEDIAINFO *mi)
463 msi_free(mi->disk_prompt);
464 msi_free(mi->cabinet);
465 msi_free(mi->volume_label);
466 msi_free(mi->first_volume);
470 static UINT get_drive_type(const WCHAR *path)
472 WCHAR root[MAX_PATH + 1];
475 PathStripToRootW(root);
476 PathAddBackslashW(root);
478 return GetDriveTypeW(root);
481 static UINT msi_load_media_info(MSIPACKAGE *package, MSIFILE *file, MSIMEDIAINFO *mi)
489 static const WCHAR query[] = {
490 'S','E','L','E','C','T',' ','*',' ', 'F','R','O','M',' ',
491 '`','M','e','d','i','a','`',' ','W','H','E','R','E',' ',
492 '`','L','a','s','t','S','e','q','u','e','n','c','e','`',' ','>','=',
493 ' ','%','i',' ','A','N','D',' ','`','D','i','s','k','I','d','`',' ','>','=',
494 ' ','%','i',' ','O','R','D','E','R',' ','B','Y',' ',
495 '`','D','i','s','k','I','d','`',0};
497 row = MSI_QueryGetRecord(package->db, query, file->Sequence, mi->disk_id);
500 TRACE("Unable to query row\n");
501 return ERROR_FUNCTION_FAILED;
504 mi->is_extracted = FALSE;
505 mi->disk_id = MSI_RecordGetInteger(row, 1);
506 mi->last_sequence = MSI_RecordGetInteger(row, 2);
507 msi_free(mi->disk_prompt);
508 mi->disk_prompt = strdupW(MSI_RecordGetString(row, 3));
509 msi_free(mi->cabinet);
510 mi->cabinet = strdupW(MSI_RecordGetString(row, 4));
511 msi_free(mi->volume_label);
512 mi->volume_label = strdupW(MSI_RecordGetString(row, 5));
513 msiobj_release(&row->hdr);
515 if (!mi->first_volume)
516 mi->first_volume = strdupW(mi->volume_label);
518 source_dir = msi_dup_property(package, cszSourceDir);
519 lstrcpyW(mi->source, source_dir);
520 mi->type = get_drive_type(source_dir);
522 if (file->IsCompressed && mi->cabinet)
524 if (mi->cabinet[0] == '#')
526 r = writeout_cabinet_stream(package, &mi->cabinet[1], mi->source);
527 if (r != ERROR_SUCCESS)
529 ERR("Failed to extract cabinet stream\n");
530 return ERROR_FUNCTION_FAILED;
534 lstrcatW(mi->source, mi->cabinet);
537 options = MSICODE_PRODUCT;
538 if (mi->type == DRIVE_CDROM || mi->type == DRIVE_REMOVABLE)
541 options |= MSISOURCETYPE_MEDIA;
543 else if (package->BaseURL && UrlIsW(package->BaseURL, URLIS_URL))
545 source = package->BaseURL;
546 options |= MSISOURCETYPE_URL;
551 options |= MSISOURCETYPE_NETWORK;
554 msi_package_add_media_disk(package, package->Context,
555 MSICODE_PRODUCT, mi->disk_id,
556 mi->volume_label, mi->disk_prompt);
558 msi_package_add_info(package, package->Context,
559 options, INSTALLPROPERTY_LASTUSEDSOURCEW, source);
561 msi_free(source_dir);
562 return ERROR_SUCCESS;
565 /* FIXME: search NETWORK and URL sources as well */
566 static UINT find_published_source(MSIPACKAGE *package, MSIMEDIAINFO *mi)
568 WCHAR source[MAX_PATH];
569 WCHAR volume[MAX_PATH];
570 WCHAR prompt[MAX_PATH];
571 DWORD volumesz, promptsz;
572 DWORD index, size, id;
576 r = MsiSourceListGetInfoW(package->ProductCode, NULL,
577 package->Context, MSICODE_PRODUCT,
578 INSTALLPROPERTY_LASTUSEDSOURCEW, source, &size);
579 if (r != ERROR_SUCCESS)
585 while (MsiSourceListEnumMediaDisksW(package->ProductCode, NULL,
587 MSICODE_PRODUCT, index++, &id,
588 volume, &volumesz, prompt, &promptsz) == ERROR_SUCCESS)
591 mi->volume_label = msi_realloc(mi->volume_label, ++volumesz * sizeof(WCHAR));
592 lstrcpyW(mi->volume_label, volume);
593 mi->disk_prompt = msi_realloc(mi->disk_prompt, ++promptsz * sizeof(WCHAR));
594 lstrcpyW(mi->disk_prompt, prompt);
596 if (source_matches_volume(mi, source))
598 /* FIXME: what about SourceDir */
599 lstrcpyW(mi->source, source);
600 return ERROR_SUCCESS;
604 return ERROR_FUNCTION_FAILED;
607 UINT ready_media(MSIPACKAGE *package, MSIFILE *file, MSIMEDIAINFO *mi)
609 UINT rc = ERROR_SUCCESS;
611 /* media info for continuous cabinet is already loaded */
612 if (mi->is_continuous)
613 return ERROR_SUCCESS;
615 rc = msi_load_media_info(package, file, mi);
616 if (rc != ERROR_SUCCESS)
618 ERR("Unable to load media info\n");
619 return ERROR_FUNCTION_FAILED;
622 /* cabinet is internal, no checks needed */
623 if (!mi->cabinet || mi->cabinet[0] == '#')
624 return ERROR_SUCCESS;
626 /* package should be downloaded */
627 if (file->IsCompressed &&
628 GetFileAttributesW(mi->source) == INVALID_FILE_ATTRIBUTES &&
629 package->BaseURL && UrlIsW(package->BaseURL, URLIS_URL))
631 WCHAR temppath[MAX_PATH];
633 msi_download_file(mi->source, temppath);
634 lstrcpyW(mi->source, temppath);
635 return ERROR_SUCCESS;
638 /* check volume matches, change media if not */
639 if (mi->volume_label && mi->disk_id > 1 &&
640 lstrcmpW(mi->first_volume, mi->volume_label))
642 LPWSTR source = msi_dup_property(package, cszSourceDir);
645 matches = source_matches_volume(mi, source);
648 if ((mi->type == DRIVE_CDROM || mi->type == DRIVE_REMOVABLE) && !matches)
650 rc = msi_change_media(package, mi);
651 if (rc != ERROR_SUCCESS)
656 if (file->IsCompressed &&
657 GetFileAttributesW(mi->source) == INVALID_FILE_ATTRIBUTES)
659 rc = find_published_source(package, mi);
660 if (rc != ERROR_SUCCESS)
662 ERR("Cabinet not found: %s\n", debugstr_w(mi->source));
663 return ERROR_INSTALL_FAILURE;
667 return ERROR_SUCCESS;