gdiplus: Implemented GdipIsEmptyClip with tests.
[wine.git] / dlls / msi / media.c
blobbac801d320fa9f45997f3b8b9a2f0d4009ce8f5a
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 "msvcrt/fcntl.h"
28 #include "msipriv.h"
29 #include "winuser.h"
30 #include "winreg.h"
31 #include "shlwapi.h"
32 #include "wine/unicode.h"
34 WINE_DEFAULT_DEBUG_CHANNEL(msi);
36 static BOOL source_matches_volume(MSIMEDIAINFO *mi, LPWSTR source_root)
38 WCHAR volume_name[MAX_PATH + 1];
40 if (!GetVolumeInformationW(source_root, volume_name, MAX_PATH + 1,
41 NULL, NULL, NULL, NULL, 0))
43 ERR("Failed to get volume information\n");
44 return FALSE;
47 return !lstrcmpW(mi->volume_label, volume_name);
50 static UINT msi_change_media(MSIPACKAGE *package, MSIMEDIAINFO *mi)
52 LPSTR msg;
53 LPWSTR error, error_dialog;
54 LPWSTR source_dir;
55 UINT r = ERROR_SUCCESS;
57 static const WCHAR szUILevel[] = {'U','I','L','e','v','e','l',0};
58 static const WCHAR error_prop[] = {'E','r','r','o','r','D','i','a','l','o','g',0};
60 if ((msi_get_property_int(package, szUILevel, 0) & INSTALLUILEVEL_MASK) ==
61 INSTALLUILEVEL_NONE && !gUIHandlerA)
62 return ERROR_SUCCESS;
64 error = generate_error_string(package, 1302, 1, mi->disk_prompt);
65 error_dialog = msi_dup_property(package, error_prop);
66 source_dir = msi_dup_property(package, cszSourceDir);
67 PathStripToRootW(source_dir);
69 while (r == ERROR_SUCCESS &&
70 !source_matches_volume(mi, source_dir))
72 r = msi_spawn_error_dialog(package, error_dialog, error);
74 if (gUIHandlerA)
76 msg = strdupWtoA(error);
77 gUIHandlerA(gUIContext, MB_RETRYCANCEL | INSTALLMESSAGE_ERROR, msg);
78 msi_free(msg);
82 msi_free(error);
83 msi_free(error_dialog);
84 msi_free(source_dir);
86 return r;
89 static UINT writeout_cabinet_stream(MSIPACKAGE *package, LPCWSTR stream,
90 WCHAR* source)
92 UINT rc;
93 USHORT* data;
94 UINT size;
95 DWORD write;
96 HANDLE hfile;
97 WCHAR tmp[MAX_PATH];
99 static const WCHAR cszTempFolder[]= {
100 'T','e','m','p','F','o','l','d','e','r',0};
102 rc = read_raw_stream_data(package->db, stream, &data, &size);
103 if (rc != ERROR_SUCCESS)
104 return rc;
106 write = MAX_PATH;
107 if (MSI_GetPropertyW(package, cszTempFolder, tmp, &write))
108 GetTempPathW(MAX_PATH, tmp);
110 GetTempFileNameW(tmp, stream, 0, source);
112 track_tempfile(package, source);
113 hfile = CreateFileW(source, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS,
114 FILE_ATTRIBUTE_NORMAL, NULL);
116 if (hfile == INVALID_HANDLE_VALUE)
118 ERR("Unable to create file %s\n", debugstr_w(source));
119 rc = ERROR_FUNCTION_FAILED;
120 goto end;
123 WriteFile(hfile, data, size, &write, NULL);
124 CloseHandle(hfile);
125 TRACE("wrote %i bytes to %s\n", write, debugstr_w(source));
127 end:
128 msi_free(data);
129 return rc;
132 static void *cabinet_alloc(ULONG cb)
134 return msi_alloc(cb);
137 static void cabinet_free(void *pv)
139 msi_free(pv);
142 static INT_PTR cabinet_open(char *pszFile, int oflag, int pmode)
144 HANDLE handle;
145 DWORD dwAccess = 0;
146 DWORD dwShareMode = 0;
147 DWORD dwCreateDisposition = OPEN_EXISTING;
149 switch (oflag & _O_ACCMODE)
151 case _O_RDONLY:
152 dwAccess = GENERIC_READ;
153 dwShareMode = FILE_SHARE_READ | FILE_SHARE_DELETE;
154 break;
155 case _O_WRONLY:
156 dwAccess = GENERIC_WRITE;
157 dwShareMode = FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE;
158 break;
159 case _O_RDWR:
160 dwAccess = GENERIC_READ | GENERIC_WRITE;
161 dwShareMode = FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE;
162 break;
165 if ((oflag & (_O_CREAT | _O_EXCL)) == (_O_CREAT | _O_EXCL))
166 dwCreateDisposition = CREATE_NEW;
167 else if (oflag & _O_CREAT)
168 dwCreateDisposition = CREATE_ALWAYS;
170 handle = CreateFileA(pszFile, dwAccess, dwShareMode, NULL,
171 dwCreateDisposition, 0, NULL);
172 if (handle == INVALID_HANDLE_VALUE)
173 return 0;
175 return (INT_PTR)handle;
178 static UINT cabinet_read(INT_PTR hf, void *pv, UINT cb)
180 HANDLE handle = (HANDLE)hf;
181 DWORD read;
183 if (ReadFile(handle, pv, cb, &read, NULL))
184 return read;
186 return 0;
189 static UINT cabinet_write(INT_PTR hf, void *pv, UINT cb)
191 HANDLE handle = (HANDLE)hf;
192 DWORD written;
194 if (WriteFile(handle, pv, cb, &written, NULL))
195 return written;
197 return 0;
200 static int cabinet_close(INT_PTR hf)
202 HANDLE handle = (HANDLE)hf;
203 return CloseHandle(handle) ? 0 : -1;
206 static long cabinet_seek(INT_PTR hf, long dist, int seektype)
208 HANDLE handle = (HANDLE)hf;
209 /* flags are compatible and so are passed straight through */
210 return SetFilePointer(handle, dist, NULL, seektype);
213 static UINT msi_media_get_disk_info(MSIPACKAGE *package, MSIMEDIAINFO *mi)
215 MSIRECORD *row;
216 LPWSTR ptr;
218 static const WCHAR query[] = {
219 'S','E','L','E','C','T',' ','*',' ', 'F','R','O','M',' ',
220 '`','M','e','d','i','a','`',' ','W','H','E','R','E',' ',
221 '`','D','i','s','k','I','d','`',' ','=',' ','%','i',0};
223 row = MSI_QueryGetRecord(package->db, query, mi->disk_id);
224 if (!row)
226 TRACE("Unable to query row\n");
227 return ERROR_FUNCTION_FAILED;
230 mi->disk_prompt = strdupW(MSI_RecordGetString(row, 3));
231 mi->cabinet = strdupW(MSI_RecordGetString(row, 4));
232 mi->volume_label = strdupW(MSI_RecordGetString(row, 5));
234 if (!mi->first_volume)
235 mi->first_volume = strdupW(mi->volume_label);
237 ptr = strrchrW(mi->source, '\\') + 1;
238 lstrcpyW(ptr, mi->cabinet);
239 msiobj_release(&row->hdr);
241 return ERROR_SUCCESS;
244 static INT_PTR cabinet_partial_file(FDINOTIFICATIONTYPE fdint,
245 PFDINOTIFICATION pfdin)
247 MSICABDATA *data = (MSICABDATA *)pfdin->pv;
248 data->mi->is_continuous = FALSE;
249 return 0;
252 static INT_PTR cabinet_next_cabinet(FDINOTIFICATIONTYPE fdint,
253 PFDINOTIFICATION pfdin)
255 MSICABDATA *data = (MSICABDATA *)pfdin->pv;
256 MSIMEDIAINFO *mi = data->mi;
257 LPWSTR cab = strdupAtoW(pfdin->psz1);
258 INT_PTR res = -1;
259 UINT rc;
261 msi_free(mi->disk_prompt);
262 msi_free(mi->cabinet);
263 msi_free(mi->volume_label);
264 mi->disk_prompt = NULL;
265 mi->cabinet = NULL;
266 mi->volume_label = NULL;
268 mi->disk_id++;
269 mi->is_continuous = TRUE;
271 rc = msi_media_get_disk_info(data->package, mi);
272 if (rc != ERROR_SUCCESS)
274 ERR("Failed to get next cabinet information: %d\n", rc);
275 goto done;
278 if (lstrcmpiW(mi->cabinet, cab))
280 ERR("Continuous cabinet does not match the next cabinet in the Media table\n");
281 goto done;
284 TRACE("Searching for %s\n", debugstr_w(mi->source));
286 res = 0;
287 if (GetFileAttributesW(mi->source) == INVALID_FILE_ATTRIBUTES)
289 if (msi_change_media(data->package, mi) != ERROR_SUCCESS)
290 res = -1;
293 done:
294 msi_free(cab);
295 return res;
298 static INT_PTR cabinet_copy_file(FDINOTIFICATIONTYPE fdint,
299 PFDINOTIFICATION pfdin)
301 MSICABDATA *data = (MSICABDATA*)pfdin->pv;
302 HANDLE handle = 0;
303 LPWSTR path = NULL;
304 DWORD attrs;
306 data->curfile = strdupAtoW(pfdin->psz1);
307 if (!data->cb(data->package, data->curfile, MSICABEXTRACT_BEGINEXTRACT, &path,
308 &attrs, data->user))
309 goto done;
311 TRACE("extracting %s\n", debugstr_w(path));
313 attrs = attrs & (FILE_ATTRIBUTE_READONLY|FILE_ATTRIBUTE_HIDDEN|FILE_ATTRIBUTE_SYSTEM);
314 if (!attrs) attrs = FILE_ATTRIBUTE_NORMAL;
316 handle = CreateFileW(path, GENERIC_READ | GENERIC_WRITE, 0,
317 NULL, CREATE_ALWAYS, attrs, NULL);
318 if (handle == INVALID_HANDLE_VALUE)
320 if (GetFileAttributesW(path) == INVALID_FILE_ATTRIBUTES)
321 ERR("failed to create %s (error %d)\n",
322 debugstr_w(path), GetLastError());
324 goto done;
327 done:
328 msi_free(path);
330 return (INT_PTR)handle;
333 static INT_PTR cabinet_close_file_info(FDINOTIFICATIONTYPE fdint,
334 PFDINOTIFICATION pfdin)
336 MSICABDATA *data = (MSICABDATA*)pfdin->pv;
337 FILETIME ft;
338 FILETIME ftLocal;
339 HANDLE handle = (HANDLE)pfdin->hf;
341 data->mi->is_continuous = FALSE;
343 if (!DosDateTimeToFileTime(pfdin->date, pfdin->time, &ft))
344 return -1;
345 if (!LocalFileTimeToFileTime(&ft, &ftLocal))
346 return -1;
347 if (!SetFileTime(handle, &ftLocal, 0, &ftLocal))
348 return -1;
350 CloseHandle(handle);
352 data->cb(data->package, data->curfile, MSICABEXTRACT_FILEEXTRACTED, NULL, NULL,
353 data->user);
355 msi_free(data->curfile);
356 data->curfile = NULL;
358 return 1;
361 static INT_PTR cabinet_notify(FDINOTIFICATIONTYPE fdint, PFDINOTIFICATION pfdin)
363 TRACE("(%d)\n", fdint);
365 switch (fdint)
367 case fdintPARTIAL_FILE:
368 return cabinet_partial_file(fdint, pfdin);
370 case fdintNEXT_CABINET:
371 return cabinet_next_cabinet(fdint, pfdin);
373 case fdintCOPY_FILE:
374 return cabinet_copy_file(fdint, pfdin);
376 case fdintCLOSE_FILE_INFO:
377 return cabinet_close_file_info(fdint, pfdin);
379 default:
380 return 0;
384 /***********************************************************************
385 * msi_cabextract
387 * Extract files from a cab file.
389 BOOL msi_cabextract(MSIPACKAGE* package, MSIMEDIAINFO *mi, LPVOID data)
391 LPSTR cabinet, cab_path = NULL;
392 LPWSTR ptr;
393 HFDI hfdi;
394 ERF erf;
395 BOOL ret = FALSE;
397 TRACE("Extracting %s\n", debugstr_w(mi->source));
399 hfdi = FDICreate(cabinet_alloc, cabinet_free, cabinet_open, cabinet_read,
400 cabinet_write, cabinet_close, cabinet_seek, 0, &erf);
401 if (!hfdi)
403 ERR("FDICreate failed\n");
404 return FALSE;
407 ptr = strrchrW(mi->source, '\\') + 1;
408 cabinet = strdupWtoA(ptr);
409 if (!cabinet)
410 goto done;
412 cab_path = strdupWtoA(mi->source);
413 if (!cab_path)
414 goto done;
416 cab_path[ptr - mi->source] = '\0';
418 ret = FDICopy(hfdi, cabinet, cab_path, 0, cabinet_notify, NULL, data);
419 if (!ret)
420 ERR("FDICopy failed\n");
422 done:
423 FDIDestroy(hfdi);
424 msi_free(cabinet);
425 msi_free(cab_path);
427 if (ret)
428 mi->is_extracted = TRUE;
430 return ret;
433 void msi_free_media_info(MSIMEDIAINFO *mi)
435 msi_free(mi->disk_prompt);
436 msi_free(mi->cabinet);
437 msi_free(mi->volume_label);
438 msi_free(mi->first_volume);
439 msi_free(mi);
442 UINT msi_load_media_info(MSIPACKAGE *package, MSIFILE *file, MSIMEDIAINFO *mi)
444 MSIRECORD *row;
445 LPWSTR source_dir;
446 LPWSTR source;
447 DWORD options;
448 UINT r;
450 static const WCHAR query[] = {
451 'S','E','L','E','C','T',' ','*',' ', 'F','R','O','M',' ',
452 '`','M','e','d','i','a','`',' ','W','H','E','R','E',' ',
453 '`','L','a','s','t','S','e','q','u','e','n','c','e','`',' ','>','=',
454 ' ','%','i',' ','A','N','D',' ','`','D','i','s','k','I','d','`',' ','>','=',
455 ' ','%','i',' ','O','R','D','E','R',' ','B','Y',' ',
456 '`','D','i','s','k','I','d','`',0};
458 row = MSI_QueryGetRecord(package->db, query, file->Sequence, mi->disk_id);
459 if (!row)
461 TRACE("Unable to query row\n");
462 return ERROR_FUNCTION_FAILED;
465 mi->is_extracted = FALSE;
466 mi->disk_id = MSI_RecordGetInteger(row, 1);
467 mi->last_sequence = MSI_RecordGetInteger(row, 2);
468 msi_free(mi->disk_prompt);
469 mi->disk_prompt = strdupW(MSI_RecordGetString(row, 3));
470 msi_free(mi->cabinet);
471 mi->cabinet = strdupW(MSI_RecordGetString(row, 4));
472 msi_free(mi->volume_label);
473 mi->volume_label = strdupW(MSI_RecordGetString(row, 5));
474 msiobj_release(&row->hdr);
476 if (!mi->first_volume)
477 mi->first_volume = strdupW(mi->volume_label);
479 source_dir = msi_dup_property(package, cszSourceDir);
480 lstrcpyW(mi->source, source_dir);
482 PathStripToRootW(source_dir);
483 mi->type = GetDriveTypeW(source_dir);
485 if (file->IsCompressed && mi->cabinet)
487 if (mi->cabinet[0] == '#')
489 r = writeout_cabinet_stream(package, &mi->cabinet[1], mi->source);
490 if (r != ERROR_SUCCESS)
492 ERR("Failed to extract cabinet stream\n");
493 return ERROR_FUNCTION_FAILED;
496 else
497 lstrcatW(mi->source, mi->cabinet);
500 options = MSICODE_PRODUCT;
501 if (mi->type == DRIVE_CDROM || mi->type == DRIVE_REMOVABLE)
503 source = source_dir;
504 options |= MSISOURCETYPE_MEDIA;
506 else if (package->BaseURL && UrlIsW(package->BaseURL, URLIS_URL))
508 source = package->BaseURL;
509 options |= MSISOURCETYPE_URL;
511 else
513 source = mi->source;
514 options |= MSISOURCETYPE_NETWORK;
517 msi_package_add_media_disk(package, package->Context,
518 MSICODE_PRODUCT, mi->disk_id,
519 mi->volume_label, mi->disk_prompt);
521 msi_package_add_info(package, package->Context,
522 options, INSTALLPROPERTY_LASTUSEDSOURCEW, source);
524 msi_free(source_dir);
525 return ERROR_SUCCESS;
528 /* FIXME: search NETWORK and URL sources as well */
529 UINT find_published_source(MSIPACKAGE *package, MSIMEDIAINFO *mi)
531 WCHAR source[MAX_PATH];
532 WCHAR volume[MAX_PATH];
533 WCHAR prompt[MAX_PATH];
534 DWORD volumesz, promptsz;
535 DWORD index, size, id;
536 UINT r;
538 size = MAX_PATH;
539 r = MsiSourceListGetInfoW(package->ProductCode, NULL,
540 package->Context, MSICODE_PRODUCT,
541 INSTALLPROPERTY_LASTUSEDSOURCEW, source, &size);
542 if (r != ERROR_SUCCESS)
543 return r;
545 index = 0;
546 volumesz = MAX_PATH;
547 promptsz = MAX_PATH;
548 while (MsiSourceListEnumMediaDisksW(package->ProductCode, NULL,
549 package->Context,
550 MSICODE_PRODUCT, index++, &id,
551 volume, &volumesz, prompt, &promptsz) == ERROR_SUCCESS)
553 mi->disk_id = id;
554 mi->volume_label = msi_realloc(mi->volume_label, ++volumesz * sizeof(WCHAR));
555 lstrcpyW(mi->volume_label, volume);
556 mi->disk_prompt = msi_realloc(mi->disk_prompt, ++promptsz * sizeof(WCHAR));
557 lstrcpyW(mi->disk_prompt, prompt);
559 if (source_matches_volume(mi, source))
561 /* FIXME: what about SourceDir */
562 lstrcpyW(mi->source, source);
563 lstrcatW(mi->source, mi->cabinet);
564 return ERROR_SUCCESS;
568 return ERROR_FUNCTION_FAILED;
571 UINT ready_media(MSIPACKAGE *package, MSIFILE *file, MSIMEDIAINFO *mi)
573 UINT rc = ERROR_SUCCESS;
575 /* media info for continuous cabinet is already loaded */
576 if (mi->is_continuous)
577 return ERROR_SUCCESS;
579 rc = msi_load_media_info(package, file, mi);
580 if (rc != ERROR_SUCCESS)
582 ERR("Unable to load media info\n");
583 return ERROR_FUNCTION_FAILED;
586 /* cabinet is internal, no checks needed */
587 if (!mi->cabinet || mi->cabinet[0] == '#')
588 return ERROR_SUCCESS;
590 /* package should be downloaded */
591 if (file->IsCompressed &&
592 GetFileAttributesW(mi->source) == INVALID_FILE_ATTRIBUTES &&
593 package->BaseURL && UrlIsW(package->BaseURL, URLIS_URL))
595 WCHAR temppath[MAX_PATH];
597 msi_download_file(mi->source, temppath);
598 lstrcpyW(mi->source, temppath);
599 return ERROR_SUCCESS;
602 /* check volume matches, change media if not */
603 if (mi->volume_label && mi->disk_id > 1 &&
604 lstrcmpW(mi->first_volume, mi->volume_label))
606 LPWSTR source = msi_dup_property(package, cszSourceDir);
607 BOOL matches;
609 matches = source_matches_volume(mi, source);
610 msi_free(source);
612 if ((mi->type == DRIVE_CDROM || mi->type == DRIVE_REMOVABLE) && !matches)
614 rc = msi_change_media(package, mi);
615 if (rc != ERROR_SUCCESS)
616 return rc;
620 if (file->IsCompressed &&
621 GetFileAttributesW(mi->source) == INVALID_FILE_ATTRIBUTES)
623 rc = find_published_source(package, mi);
624 if (rc != ERROR_SUCCESS)
626 ERR("Cabinet not found: %s\n", debugstr_w(mi->source));
627 return ERROR_INSTALL_FAILURE;
631 return ERROR_SUCCESS;