msi: Add support for the Unicode version of the global UI handler.
[wine/hacks.git] / dlls / msi / media.c
blobfe0192e53924e161789b0a65da0688375adc11bc
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, 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");
65 return FALSE;
68 return !lstrcmpW(mi->volume_label, volume_name);
71 static UINT msi_change_media(MSIPACKAGE *package, MSIMEDIAINFO *mi)
73 LPWSTR error, error_dialog;
74 LPWSTR source_dir;
75 UINT r = ERROR_SUCCESS;
77 static const WCHAR error_prop[] = {'E','r','r','o','r','D','i','a','l','o','g',0};
79 if ((msi_get_property_int(package, szUILevel, 0) & INSTALLUILEVEL_MASK) ==
80 INSTALLUILEVEL_NONE && !gUIHandlerA && !gUIHandlerW)
81 return ERROR_SUCCESS;
83 error = generate_error_string(package, 1302, 1, mi->disk_prompt);
84 error_dialog = msi_dup_property(package, error_prop);
85 source_dir = msi_dup_property(package, cszSourceDir);
87 while (r == ERROR_SUCCESS && !source_matches_volume(mi, source_dir))
89 r = msi_spawn_error_dialog(package, error_dialog, error);
91 if (gUIHandlerW)
93 gUIHandlerW(gUIContext, MB_RETRYCANCEL | INSTALLMESSAGE_ERROR, error);
95 else if (gUIHandlerA)
97 char *msg = strdupWtoA(error);
98 gUIHandlerA(gUIContext, MB_RETRYCANCEL | INSTALLMESSAGE_ERROR, msg);
99 msi_free(msg);
103 msi_free(error);
104 msi_free(error_dialog);
105 msi_free(source_dir);
107 return r;
110 static UINT writeout_cabinet_stream(MSIPACKAGE *package, LPCWSTR stream,
111 WCHAR* source)
113 UINT rc;
114 USHORT* data;
115 UINT size;
116 DWORD write;
117 HANDLE hfile;
118 WCHAR tmp[MAX_PATH];
120 static const WCHAR cszTempFolder[]= {
121 'T','e','m','p','F','o','l','d','e','r',0};
123 rc = read_raw_stream_data(package->db, stream, &data, &size);
124 if (rc != ERROR_SUCCESS)
125 return rc;
127 write = MAX_PATH;
128 if (MSI_GetPropertyW(package, cszTempFolder, tmp, &write))
129 GetTempPathW(MAX_PATH, tmp);
131 GetTempFileNameW(tmp, stream, 0, source);
133 track_tempfile(package, source);
134 hfile = CreateFileW(source, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS,
135 FILE_ATTRIBUTE_NORMAL, NULL);
137 if (hfile == INVALID_HANDLE_VALUE)
139 ERR("Unable to create file %s\n", debugstr_w(source));
140 rc = ERROR_FUNCTION_FAILED;
141 goto end;
144 WriteFile(hfile, data, size, &write, NULL);
145 CloseHandle(hfile);
146 TRACE("wrote %i bytes to %s\n", write, debugstr_w(source));
148 end:
149 msi_free(data);
150 return rc;
153 static void * CDECL cabinet_alloc(ULONG cb)
155 return msi_alloc(cb);
158 static void CDECL cabinet_free(void *pv)
160 msi_free(pv);
163 static INT_PTR CDECL cabinet_open(char *pszFile, int oflag, int pmode)
165 HANDLE handle;
166 DWORD dwAccess = 0;
167 DWORD dwShareMode = 0;
168 DWORD dwCreateDisposition = OPEN_EXISTING;
170 switch (oflag & _O_ACCMODE)
172 case _O_RDONLY:
173 dwAccess = GENERIC_READ;
174 dwShareMode = FILE_SHARE_READ | FILE_SHARE_DELETE;
175 break;
176 case _O_WRONLY:
177 dwAccess = GENERIC_WRITE;
178 dwShareMode = FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE;
179 break;
180 case _O_RDWR:
181 dwAccess = GENERIC_READ | GENERIC_WRITE;
182 dwShareMode = FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE;
183 break;
186 if ((oflag & (_O_CREAT | _O_EXCL)) == (_O_CREAT | _O_EXCL))
187 dwCreateDisposition = CREATE_NEW;
188 else if (oflag & _O_CREAT)
189 dwCreateDisposition = CREATE_ALWAYS;
191 handle = CreateFileA(pszFile, dwAccess, dwShareMode, NULL,
192 dwCreateDisposition, 0, NULL);
193 if (handle == INVALID_HANDLE_VALUE)
194 return 0;
196 return (INT_PTR)handle;
199 static UINT CDECL cabinet_read(INT_PTR hf, void *pv, UINT cb)
201 HANDLE handle = (HANDLE)hf;
202 DWORD read;
204 if (ReadFile(handle, pv, cb, &read, NULL))
205 return read;
207 return 0;
210 static UINT CDECL cabinet_write(INT_PTR hf, void *pv, UINT cb)
212 HANDLE handle = (HANDLE)hf;
213 DWORD written;
215 if (WriteFile(handle, pv, cb, &written, NULL))
216 return written;
218 return 0;
221 static int CDECL cabinet_close(INT_PTR hf)
223 HANDLE handle = (HANDLE)hf;
224 return CloseHandle(handle) ? 0 : -1;
227 static LONG CDECL cabinet_seek(INT_PTR hf, LONG dist, int seektype)
229 HANDLE handle = (HANDLE)hf;
230 /* flags are compatible and so are passed straight through */
231 return SetFilePointer(handle, dist, NULL, seektype);
234 static UINT CDECL msi_media_get_disk_info(MSIPACKAGE *package, MSIMEDIAINFO *mi)
236 MSIRECORD *row;
237 LPWSTR ptr;
239 static const WCHAR query[] = {
240 'S','E','L','E','C','T',' ','*',' ', 'F','R','O','M',' ',
241 '`','M','e','d','i','a','`',' ','W','H','E','R','E',' ',
242 '`','D','i','s','k','I','d','`',' ','=',' ','%','i',0};
244 row = MSI_QueryGetRecord(package->db, query, mi->disk_id);
245 if (!row)
247 TRACE("Unable to query row\n");
248 return ERROR_FUNCTION_FAILED;
251 mi->disk_prompt = strdupW(MSI_RecordGetString(row, 3));
252 mi->cabinet = strdupW(MSI_RecordGetString(row, 4));
253 mi->volume_label = strdupW(MSI_RecordGetString(row, 5));
255 if (!mi->first_volume)
256 mi->first_volume = strdupW(mi->volume_label);
258 ptr = strrchrW(mi->source, '\\') + 1;
259 lstrcpyW(ptr, mi->cabinet);
260 msiobj_release(&row->hdr);
262 return ERROR_SUCCESS;
265 static INT_PTR cabinet_partial_file(FDINOTIFICATIONTYPE fdint,
266 PFDINOTIFICATION pfdin)
268 MSICABDATA *data = pfdin->pv;
269 data->mi->is_continuous = FALSE;
270 return 0;
273 static INT_PTR cabinet_next_cabinet(FDINOTIFICATIONTYPE fdint,
274 PFDINOTIFICATION pfdin)
276 MSICABDATA *data = pfdin->pv;
277 MSIMEDIAINFO *mi = data->mi;
278 LPWSTR cab = strdupAtoW(pfdin->psz1);
279 INT_PTR res = -1;
280 UINT rc;
282 msi_free(mi->disk_prompt);
283 msi_free(mi->cabinet);
284 msi_free(mi->volume_label);
285 mi->disk_prompt = NULL;
286 mi->cabinet = NULL;
287 mi->volume_label = NULL;
289 mi->disk_id++;
290 mi->is_continuous = TRUE;
292 rc = msi_media_get_disk_info(data->package, mi);
293 if (rc != ERROR_SUCCESS)
295 ERR("Failed to get next cabinet information: %d\n", rc);
296 goto done;
299 if (lstrcmpiW(mi->cabinet, cab))
301 ERR("Continuous cabinet does not match the next cabinet in the Media table\n");
302 goto done;
305 TRACE("Searching for %s\n", debugstr_w(mi->source));
307 res = 0;
308 if (GetFileAttributesW(mi->source) == INVALID_FILE_ATTRIBUTES)
310 if (msi_change_media(data->package, mi) != ERROR_SUCCESS)
311 res = -1;
314 done:
315 msi_free(cab);
316 return res;
319 static INT_PTR cabinet_copy_file(FDINOTIFICATIONTYPE fdint,
320 PFDINOTIFICATION pfdin)
322 MSICABDATA *data = pfdin->pv;
323 HANDLE handle = 0;
324 LPWSTR path = NULL;
325 DWORD attrs;
327 data->curfile = strdupAtoW(pfdin->psz1);
328 if (!data->cb(data->package, data->curfile, MSICABEXTRACT_BEGINEXTRACT, &path,
329 &attrs, data->user))
330 goto done;
332 TRACE("extracting %s\n", debugstr_w(path));
334 attrs = attrs & (FILE_ATTRIBUTE_READONLY|FILE_ATTRIBUTE_HIDDEN|FILE_ATTRIBUTE_SYSTEM);
335 if (!attrs) attrs = FILE_ATTRIBUTE_NORMAL;
337 handle = CreateFileW(path, GENERIC_READ | GENERIC_WRITE, 0,
338 NULL, CREATE_ALWAYS, attrs, NULL);
339 if (handle == INVALID_HANDLE_VALUE)
341 DWORD err = GetLastError();
342 DWORD attrs2 = GetFileAttributesW(path);
344 if (attrs2 == INVALID_FILE_ATTRIBUTES)
346 ERR("failed to create %s (error %d)\n", debugstr_w(path), err);
347 goto done;
349 else if (err == ERROR_ACCESS_DENIED && (attrs2 & FILE_ATTRIBUTE_READONLY))
351 TRACE("removing read-only attribute on %s\n", debugstr_w(path));
352 SetFileAttributesW( path, attrs2 & ~FILE_ATTRIBUTE_READONLY );
353 handle = CreateFileW(path, GENERIC_READ | GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, attrs2, NULL);
355 if (handle != INVALID_HANDLE_VALUE) goto done;
356 err = GetLastError();
358 if (err == ERROR_SHARING_VIOLATION)
360 WCHAR tmpfileW[MAX_PATH], *tmppathW, *p;
361 DWORD len;
363 TRACE("file in use, scheduling rename operation\n");
365 GetTempFileNameW(szBackSlash, szMsi, 0, tmpfileW);
366 len = strlenW(path) + strlenW(tmpfileW) + 1;
367 if (!(tmppathW = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR))))
368 return ERROR_OUTOFMEMORY;
370 strcpyW(tmppathW, path);
371 if ((p = strrchrW(tmppathW, '\\'))) *p = 0;
372 strcatW(tmppathW, tmpfileW);
374 handle = CreateFileW(tmppathW, GENERIC_READ | GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, attrs, NULL);
376 if (handle != INVALID_HANDLE_VALUE &&
377 MoveFileExW(path, NULL, MOVEFILE_DELAY_UNTIL_REBOOT) &&
378 MoveFileExW(tmppathW, path, MOVEFILE_DELAY_UNTIL_REBOOT))
380 data->package->need_reboot = 1;
382 else
383 WARN("failed to schedule rename operation %s (error %d)\n", debugstr_w(path), GetLastError());
385 HeapFree(GetProcessHeap(), 0, tmppathW);
387 else
388 WARN("failed to create %s (error %d)\n", debugstr_w(path), err);
391 done:
392 msi_free(path);
394 return (INT_PTR)handle;
397 static INT_PTR cabinet_close_file_info(FDINOTIFICATIONTYPE fdint,
398 PFDINOTIFICATION pfdin)
400 MSICABDATA *data = pfdin->pv;
401 FILETIME ft;
402 FILETIME ftLocal;
403 HANDLE handle = (HANDLE)pfdin->hf;
405 data->mi->is_continuous = FALSE;
407 if (!DosDateTimeToFileTime(pfdin->date, pfdin->time, &ft))
408 return -1;
409 if (!LocalFileTimeToFileTime(&ft, &ftLocal))
410 return -1;
411 if (!SetFileTime(handle, &ftLocal, 0, &ftLocal))
412 return -1;
414 CloseHandle(handle);
416 data->cb(data->package, data->curfile, MSICABEXTRACT_FILEEXTRACTED, NULL, NULL,
417 data->user);
419 msi_free(data->curfile);
420 data->curfile = NULL;
422 return 1;
425 static INT_PTR CDECL cabinet_notify(FDINOTIFICATIONTYPE fdint, PFDINOTIFICATION pfdin)
427 TRACE("(%d)\n", fdint);
429 switch (fdint)
431 case fdintPARTIAL_FILE:
432 return cabinet_partial_file(fdint, pfdin);
434 case fdintNEXT_CABINET:
435 return cabinet_next_cabinet(fdint, pfdin);
437 case fdintCOPY_FILE:
438 return cabinet_copy_file(fdint, pfdin);
440 case fdintCLOSE_FILE_INFO:
441 return cabinet_close_file_info(fdint, pfdin);
443 default:
444 return 0;
448 /***********************************************************************
449 * msi_cabextract
451 * Extract files from a cab file.
453 BOOL msi_cabextract(MSIPACKAGE* package, MSIMEDIAINFO *mi, LPVOID data)
455 LPSTR cabinet, cab_path = NULL;
456 LPWSTR ptr;
457 HFDI hfdi;
458 ERF erf;
459 BOOL ret = FALSE;
461 TRACE("Extracting %s\n", debugstr_w(mi->source));
463 hfdi = FDICreate(cabinet_alloc, cabinet_free, cabinet_open, cabinet_read,
464 cabinet_write, cabinet_close, cabinet_seek, 0, &erf);
465 if (!hfdi)
467 ERR("FDICreate failed\n");
468 return FALSE;
471 ptr = strrchrW(mi->source, '\\') + 1;
472 cabinet = strdupWtoA(ptr);
473 if (!cabinet)
474 goto done;
476 cab_path = strdupWtoA(mi->source);
477 if (!cab_path)
478 goto done;
480 cab_path[ptr - mi->source] = '\0';
482 ret = FDICopy(hfdi, cabinet, cab_path, 0, cabinet_notify, NULL, data);
483 if (!ret)
484 ERR("FDICopy failed\n");
486 done:
487 FDIDestroy(hfdi);
488 msi_free(cabinet);
489 msi_free(cab_path);
491 if (ret)
492 mi->is_extracted = TRUE;
494 return ret;
497 void msi_free_media_info(MSIMEDIAINFO *mi)
499 msi_free(mi->disk_prompt);
500 msi_free(mi->cabinet);
501 msi_free(mi->volume_label);
502 msi_free(mi->first_volume);
503 msi_free(mi);
506 static UINT get_drive_type(const WCHAR *path)
508 WCHAR root[MAX_PATH + 1];
510 strcpyW(root, path);
511 PathStripToRootW(root);
512 PathAddBackslashW(root);
514 return GetDriveTypeW(root);
517 static UINT msi_load_media_info(MSIPACKAGE *package, MSIFILE *file, MSIMEDIAINFO *mi)
519 MSIRECORD *row;
520 LPWSTR source_dir;
521 LPWSTR source;
522 DWORD options;
523 UINT r;
525 static const WCHAR query[] = {
526 'S','E','L','E','C','T',' ','*',' ', 'F','R','O','M',' ',
527 '`','M','e','d','i','a','`',' ','W','H','E','R','E',' ',
528 '`','L','a','s','t','S','e','q','u','e','n','c','e','`',' ','>','=',
529 ' ','%','i',' ','A','N','D',' ','`','D','i','s','k','I','d','`',' ','>','=',
530 ' ','%','i',' ','O','R','D','E','R',' ','B','Y',' ',
531 '`','D','i','s','k','I','d','`',0};
533 row = MSI_QueryGetRecord(package->db, query, file->Sequence, mi->disk_id);
534 if (!row)
536 TRACE("Unable to query row\n");
537 return ERROR_FUNCTION_FAILED;
540 mi->is_extracted = FALSE;
541 mi->disk_id = MSI_RecordGetInteger(row, 1);
542 mi->last_sequence = MSI_RecordGetInteger(row, 2);
543 msi_free(mi->disk_prompt);
544 mi->disk_prompt = strdupW(MSI_RecordGetString(row, 3));
545 msi_free(mi->cabinet);
546 mi->cabinet = strdupW(MSI_RecordGetString(row, 4));
547 msi_free(mi->volume_label);
548 mi->volume_label = strdupW(MSI_RecordGetString(row, 5));
549 msiobj_release(&row->hdr);
551 if (!mi->first_volume)
552 mi->first_volume = strdupW(mi->volume_label);
554 source_dir = msi_dup_property(package, cszSourceDir);
555 lstrcpyW(mi->source, source_dir);
556 mi->type = get_drive_type(source_dir);
558 if (file->IsCompressed && mi->cabinet)
560 if (mi->cabinet[0] == '#')
562 r = writeout_cabinet_stream(package, &mi->cabinet[1], mi->source);
563 if (r != ERROR_SUCCESS)
565 ERR("Failed to extract cabinet stream\n");
566 return ERROR_FUNCTION_FAILED;
569 else
570 lstrcatW(mi->source, mi->cabinet);
573 options = MSICODE_PRODUCT;
574 if (mi->type == DRIVE_CDROM || mi->type == DRIVE_REMOVABLE)
576 source = source_dir;
577 options |= MSISOURCETYPE_MEDIA;
579 else if (package->BaseURL && UrlIsW(package->BaseURL, URLIS_URL))
581 source = package->BaseURL;
582 options |= MSISOURCETYPE_URL;
584 else
586 source = mi->source;
587 options |= MSISOURCETYPE_NETWORK;
590 msi_package_add_media_disk(package, package->Context,
591 MSICODE_PRODUCT, mi->disk_id,
592 mi->volume_label, mi->disk_prompt);
594 msi_package_add_info(package, package->Context,
595 options, INSTALLPROPERTY_LASTUSEDSOURCEW, source);
597 msi_free(source_dir);
598 return ERROR_SUCCESS;
601 /* FIXME: search NETWORK and URL sources as well */
602 static UINT find_published_source(MSIPACKAGE *package, MSIMEDIAINFO *mi)
604 WCHAR source[MAX_PATH];
605 WCHAR volume[MAX_PATH];
606 WCHAR prompt[MAX_PATH];
607 DWORD volumesz, promptsz;
608 DWORD index, size, id;
609 UINT r;
611 size = MAX_PATH;
612 r = MsiSourceListGetInfoW(package->ProductCode, NULL,
613 package->Context, MSICODE_PRODUCT,
614 INSTALLPROPERTY_LASTUSEDSOURCEW, source, &size);
615 if (r != ERROR_SUCCESS)
616 return r;
618 index = 0;
619 volumesz = MAX_PATH;
620 promptsz = MAX_PATH;
621 while (MsiSourceListEnumMediaDisksW(package->ProductCode, NULL,
622 package->Context,
623 MSICODE_PRODUCT, index++, &id,
624 volume, &volumesz, prompt, &promptsz) == ERROR_SUCCESS)
626 mi->disk_id = id;
627 mi->volume_label = msi_realloc(mi->volume_label, ++volumesz * sizeof(WCHAR));
628 lstrcpyW(mi->volume_label, volume);
629 mi->disk_prompt = msi_realloc(mi->disk_prompt, ++promptsz * sizeof(WCHAR));
630 lstrcpyW(mi->disk_prompt, prompt);
632 if (source_matches_volume(mi, source))
634 /* FIXME: what about SourceDir */
635 lstrcpyW(mi->source, source);
636 return ERROR_SUCCESS;
640 return ERROR_FUNCTION_FAILED;
643 UINT ready_media(MSIPACKAGE *package, MSIFILE *file, MSIMEDIAINFO *mi)
645 UINT rc = ERROR_SUCCESS;
647 /* media info for continuous cabinet is already loaded */
648 if (mi->is_continuous)
649 return ERROR_SUCCESS;
651 rc = msi_load_media_info(package, file, mi);
652 if (rc != ERROR_SUCCESS)
654 ERR("Unable to load media info\n");
655 return ERROR_FUNCTION_FAILED;
658 /* cabinet is internal, no checks needed */
659 if (!mi->cabinet || mi->cabinet[0] == '#')
660 return ERROR_SUCCESS;
662 /* package should be downloaded */
663 if (file->IsCompressed &&
664 GetFileAttributesW(mi->source) == INVALID_FILE_ATTRIBUTES &&
665 package->BaseURL && UrlIsW(package->BaseURL, URLIS_URL))
667 WCHAR temppath[MAX_PATH];
669 msi_download_file(mi->source, temppath);
670 lstrcpyW(mi->source, temppath);
671 return ERROR_SUCCESS;
674 /* check volume matches, change media if not */
675 if (mi->volume_label && mi->disk_id > 1 &&
676 lstrcmpW(mi->first_volume, mi->volume_label))
678 LPWSTR source = msi_dup_property(package, cszSourceDir);
679 BOOL matches;
681 matches = source_matches_volume(mi, source);
682 msi_free(source);
684 if ((mi->type == DRIVE_CDROM || mi->type == DRIVE_REMOVABLE) && !matches)
686 rc = msi_change_media(package, mi);
687 if (rc != ERROR_SUCCESS)
688 return rc;
692 if (file->IsCompressed &&
693 GetFileAttributesW(mi->source) == INVALID_FILE_ATTRIBUTES)
695 rc = find_published_source(package, mi);
696 if (rc != ERROR_SUCCESS)
698 ERR("Cabinet not found: %s\n", debugstr_w(mi->source));
699 return ERROR_INSTALL_FAILURE;
703 return ERROR_SUCCESS;