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, 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");
63 return !lstrcmpW(mi->volume_label, volume_name);
66 static UINT msi_change_media(MSIPACKAGE *package, MSIMEDIAINFO *mi)
69 LPWSTR error, error_dialog;
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)
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);
92 msg = strdupWtoA(error);
93 gUIHandlerA(gUIContext, MB_RETRYCANCEL | INSTALLMESSAGE_ERROR, msg);
99 msi_free(error_dialog);
100 msi_free(source_dir);
105 static UINT writeout_cabinet_stream(MSIPACKAGE *package, LPCWSTR stream,
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)
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;
139 WriteFile(hfile, data, size, &write, NULL);
141 TRACE("wrote %i bytes to %s\n", write, debugstr_w(source));
148 static void CDECL *cabinet_alloc(ULONG cb)
150 return msi_alloc(cb);
153 static void CDECL cabinet_free(void *pv)
158 static INT_PTR CDECL cabinet_open(char *pszFile, int oflag, int pmode)
162 DWORD dwShareMode = 0;
163 DWORD dwCreateDisposition = OPEN_EXISTING;
165 switch (oflag & _O_ACCMODE)
168 dwAccess = GENERIC_READ;
169 dwShareMode = FILE_SHARE_READ | FILE_SHARE_DELETE;
172 dwAccess = GENERIC_WRITE;
173 dwShareMode = FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE;
176 dwAccess = GENERIC_READ | GENERIC_WRITE;
177 dwShareMode = FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE;
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)
191 return (INT_PTR)handle;
194 static UINT CDECL cabinet_read(INT_PTR hf, void *pv, UINT cb)
196 HANDLE handle = (HANDLE)hf;
199 if (ReadFile(handle, pv, cb, &read, NULL))
205 static UINT CDECL cabinet_write(INT_PTR hf, void *pv, UINT cb)
207 HANDLE handle = (HANDLE)hf;
210 if (WriteFile(handle, pv, cb, &written, NULL))
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)
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);
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;
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);
277 msi_free(mi->disk_prompt);
278 msi_free(mi->cabinet);
279 msi_free(mi->volume_label);
280 mi->disk_prompt = NULL;
282 mi->volume_label = NULL;
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);
294 if (lstrcmpiW(mi->cabinet, cab))
296 ERR("Continuous cabinet does not match the next cabinet in the Media table\n");
300 TRACE("Searching for %s\n", debugstr_w(mi->source));
303 if (GetFileAttributesW(mi->source) == INVALID_FILE_ATTRIBUTES)
305 if (msi_change_media(data->package, mi) != ERROR_SUCCESS)
314 static INT_PTR cabinet_copy_file(FDINOTIFICATIONTYPE fdint,
315 PFDINOTIFICATION pfdin)
317 MSICABDATA *data = pfdin->pv;
322 data->curfile = strdupAtoW(pfdin->psz1);
323 if (!data->cb(data->package, data->curfile, MSICABEXTRACT_BEGINEXTRACT, &path,
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());
346 return (INT_PTR)handle;
349 static INT_PTR cabinet_close_file_info(FDINOTIFICATIONTYPE fdint,
350 PFDINOTIFICATION pfdin)
352 MSICABDATA *data = pfdin->pv;
355 HANDLE handle = (HANDLE)pfdin->hf;
357 data->mi->is_continuous = FALSE;
359 if (!DosDateTimeToFileTime(pfdin->date, pfdin->time, &ft))
361 if (!LocalFileTimeToFileTime(&ft, &ftLocal))
363 if (!SetFileTime(handle, &ftLocal, 0, &ftLocal))
368 data->cb(data->package, data->curfile, MSICABEXTRACT_FILEEXTRACTED, NULL, NULL,
371 msi_free(data->curfile);
372 data->curfile = NULL;
377 static INT_PTR CDECL cabinet_notify(FDINOTIFICATIONTYPE fdint, PFDINOTIFICATION pfdin)
379 TRACE("(%d)\n", fdint);
383 case fdintPARTIAL_FILE:
384 return cabinet_partial_file(fdint, pfdin);
386 case fdintNEXT_CABINET:
387 return cabinet_next_cabinet(fdint, pfdin);
390 return cabinet_copy_file(fdint, pfdin);
392 case fdintCLOSE_FILE_INFO:
393 return cabinet_close_file_info(fdint, pfdin);
400 /***********************************************************************
403 * Extract files from a cab file.
405 BOOL msi_cabextract(MSIPACKAGE* package, MSIMEDIAINFO *mi, LPVOID data)
407 LPSTR cabinet, cab_path = NULL;
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);
419 ERR("FDICreate failed\n");
423 ptr = strrchrW(mi->source, '\\') + 1;
424 cabinet = strdupWtoA(ptr);
428 cab_path = strdupWtoA(mi->source);
432 cab_path[ptr - mi->source] = '\0';
434 ret = FDICopy(hfdi, cabinet, cab_path, 0, cabinet_notify, NULL, data);
436 ERR("FDICopy failed\n");
444 mi->is_extracted = TRUE;
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);
458 static UINT msi_load_media_info(MSIPACKAGE *package, MSIFILE *file, MSIMEDIAINFO *mi)
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);
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;
513 lstrcatW(mi->source, mi->cabinet);
516 options = MSICODE_PRODUCT;
517 if (mi->type == DRIVE_CDROM || mi->type == DRIVE_REMOVABLE)
520 options |= MSISOURCETYPE_MEDIA;
522 else if (package->BaseURL && UrlIsW(package->BaseURL, URLIS_URL))
524 source = package->BaseURL;
525 options |= MSISOURCETYPE_URL;
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;
555 r = MsiSourceListGetInfoW(package->ProductCode, NULL,
556 package->Context, MSICODE_PRODUCT,
557 INSTALLPROPERTY_LASTUSEDSOURCEW, source, &size);
558 if (r != ERROR_SUCCESS)
564 while (MsiSourceListEnumMediaDisksW(package->ProductCode, NULL,
566 MSICODE_PRODUCT, index++, &id,
567 volume, &volumesz, prompt, &promptsz) == ERROR_SUCCESS)
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);
625 matches = source_matches_volume(mi, source);
628 if ((mi->type == DRIVE_CDROM || mi->type == DRIVE_REMOVABLE) && !matches)
630 rc = msi_change_media(package, mi);
631 if (rc != ERROR_SUCCESS)
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;