user32: Use MAKEWPARAM instead of MAKELONG for menu messages.
[wine] / dlls / msi / media.c
1 /*
2  * Implementation of the Microsoft Installer (msi.dll)
3  *
4  * Copyright 2008 James Hawkins
5  *
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.
10  *
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.
15  *
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
19  */
20
21 #include <stdarg.h>
22
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"
32
33 WINE_DEFAULT_DEBUG_CHANNEL(msi);
34
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
51
52 static BOOL source_matches_volume(MSIMEDIAINFO *mi, LPWSTR source_root)
53 {
54     WCHAR volume_name[MAX_PATH + 1];
55
56     if (!GetVolumeInformationW(source_root, volume_name, MAX_PATH + 1,
57                                NULL, NULL, NULL, NULL, 0))
58     {
59         ERR("Failed to get volume information\n");
60         return FALSE;
61     }
62
63     return !lstrcmpW(mi->volume_label, volume_name);
64 }
65
66 static UINT msi_change_media(MSIPACKAGE *package, MSIMEDIAINFO *mi)
67 {
68     LPSTR msg;
69     LPWSTR error, error_dialog;
70     LPWSTR source_dir;
71     UINT r = ERROR_SUCCESS;
72
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};
75
76     if ((msi_get_property_int(package, szUILevel, 0) & INSTALLUILEVEL_MASK) ==
77          INSTALLUILEVEL_NONE && !gUIHandlerA)
78         return ERROR_SUCCESS;
79
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);
84
85     while (r == ERROR_SUCCESS &&
86            !source_matches_volume(mi, source_dir))
87     {
88         r = msi_spawn_error_dialog(package, error_dialog, error);
89
90         if (gUIHandlerA)
91         {
92             msg = strdupWtoA(error);
93             gUIHandlerA(gUIContext, MB_RETRYCANCEL | INSTALLMESSAGE_ERROR, msg);
94             msi_free(msg);
95         }
96     }
97
98     msi_free(error);
99     msi_free(error_dialog);
100     msi_free(source_dir);
101
102     return r;
103 }
104
105 static UINT writeout_cabinet_stream(MSIPACKAGE *package, LPCWSTR stream,
106                                     WCHAR* source)
107 {
108     UINT rc;
109     USHORT* data;
110     UINT size;
111     DWORD write;
112     HANDLE hfile;
113     WCHAR tmp[MAX_PATH];
114
115     static const WCHAR cszTempFolder[]= {
116         'T','e','m','p','F','o','l','d','e','r',0};
117
118     rc = read_raw_stream_data(package->db, stream, &data, &size);
119     if (rc != ERROR_SUCCESS)
120         return rc;
121
122     write = MAX_PATH;
123     if (MSI_GetPropertyW(package, cszTempFolder, tmp, &write))
124         GetTempPathW(MAX_PATH, tmp);
125
126     GetTempFileNameW(tmp, stream, 0, source);
127
128     track_tempfile(package, source);
129     hfile = CreateFileW(source, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS,
130                         FILE_ATTRIBUTE_NORMAL, NULL);
131
132     if (hfile == INVALID_HANDLE_VALUE)
133     {
134         ERR("Unable to create file %s\n", debugstr_w(source));
135         rc = ERROR_FUNCTION_FAILED;
136         goto end;
137     }
138
139     WriteFile(hfile, data, size, &write, NULL);
140     CloseHandle(hfile);
141     TRACE("wrote %i bytes to %s\n", write, debugstr_w(source));
142
143 end:
144     msi_free(data);
145     return rc;
146 }
147
148 static void * CDECL cabinet_alloc(ULONG cb)
149 {
150     return msi_alloc(cb);
151 }
152
153 static void CDECL cabinet_free(void *pv)
154 {
155     msi_free(pv);
156 }
157
158 static INT_PTR CDECL cabinet_open(char *pszFile, int oflag, int pmode)
159 {
160     HANDLE handle;
161     DWORD dwAccess = 0;
162     DWORD dwShareMode = 0;
163     DWORD dwCreateDisposition = OPEN_EXISTING;
164
165     switch (oflag & _O_ACCMODE)
166     {
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;
179     }
180
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;
185
186     handle = CreateFileA(pszFile, dwAccess, dwShareMode, NULL,
187                          dwCreateDisposition, 0, NULL);
188     if (handle == INVALID_HANDLE_VALUE)
189         return 0;
190
191     return (INT_PTR)handle;
192 }
193
194 static UINT CDECL cabinet_read(INT_PTR hf, void *pv, UINT cb)
195 {
196     HANDLE handle = (HANDLE)hf;
197     DWORD read;
198
199     if (ReadFile(handle, pv, cb, &read, NULL))
200         return read;
201
202     return 0;
203 }
204
205 static UINT CDECL cabinet_write(INT_PTR hf, void *pv, UINT cb)
206 {
207     HANDLE handle = (HANDLE)hf;
208     DWORD written;
209
210     if (WriteFile(handle, pv, cb, &written, NULL))
211         return written;
212
213     return 0;
214 }
215
216 static int CDECL cabinet_close(INT_PTR hf)
217 {
218     HANDLE handle = (HANDLE)hf;
219     return CloseHandle(handle) ? 0 : -1;
220 }
221
222 static LONG CDECL cabinet_seek(INT_PTR hf, LONG dist, int seektype)
223 {
224     HANDLE handle = (HANDLE)hf;
225     /* flags are compatible and so are passed straight through */
226     return SetFilePointer(handle, dist, NULL, seektype);
227 }
228
229 static UINT CDECL msi_media_get_disk_info(MSIPACKAGE *package, MSIMEDIAINFO *mi)
230 {
231     MSIRECORD *row;
232     LPWSTR ptr;
233
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};
238
239     row = MSI_QueryGetRecord(package->db, query, mi->disk_id);
240     if (!row)
241     {
242         TRACE("Unable to query row\n");
243         return ERROR_FUNCTION_FAILED;
244     }
245
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));
249
250     if (!mi->first_volume)
251         mi->first_volume = strdupW(mi->volume_label);
252
253     ptr = strrchrW(mi->source, '\\') + 1;
254     lstrcpyW(ptr, mi->cabinet);
255     msiobj_release(&row->hdr);
256
257     return ERROR_SUCCESS;
258 }
259
260 static INT_PTR cabinet_partial_file(FDINOTIFICATIONTYPE fdint,
261                                     PFDINOTIFICATION pfdin)
262 {
263     MSICABDATA *data = pfdin->pv;
264     data->mi->is_continuous = FALSE;
265     return 0;
266 }
267
268 static INT_PTR cabinet_next_cabinet(FDINOTIFICATIONTYPE fdint,
269                                     PFDINOTIFICATION pfdin)
270 {
271     MSICABDATA *data = pfdin->pv;
272     MSIMEDIAINFO *mi = data->mi;
273     LPWSTR cab = strdupAtoW(pfdin->psz1);
274     INT_PTR res = -1;
275     UINT rc;
276
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;
283
284     mi->disk_id++;
285     mi->is_continuous = TRUE;
286
287     rc = msi_media_get_disk_info(data->package, mi);
288     if (rc != ERROR_SUCCESS)
289     {
290         ERR("Failed to get next cabinet information: %d\n", rc);
291         goto done;
292     }
293
294     if (lstrcmpiW(mi->cabinet, cab))
295     {
296         ERR("Continuous cabinet does not match the next cabinet in the Media table\n");
297         goto done;
298     }
299
300     TRACE("Searching for %s\n", debugstr_w(mi->source));
301
302     res = 0;
303     if (GetFileAttributesW(mi->source) == INVALID_FILE_ATTRIBUTES)
304     {
305         if (msi_change_media(data->package, mi) != ERROR_SUCCESS)
306             res = -1;
307     }
308
309 done:
310     msi_free(cab);
311     return res;
312 }
313
314 static INT_PTR cabinet_copy_file(FDINOTIFICATIONTYPE fdint,
315                                  PFDINOTIFICATION pfdin)
316 {
317     MSICABDATA *data = pfdin->pv;
318     HANDLE handle = 0;
319     LPWSTR path = NULL;
320     DWORD attrs;
321
322     data->curfile = strdupAtoW(pfdin->psz1);
323     if (!data->cb(data->package, data->curfile, MSICABEXTRACT_BEGINEXTRACT, &path,
324                   &attrs, data->user))
325         goto done;
326
327     TRACE("extracting %s\n", debugstr_w(path));
328
329     attrs = attrs & (FILE_ATTRIBUTE_READONLY|FILE_ATTRIBUTE_HIDDEN|FILE_ATTRIBUTE_SYSTEM);
330     if (!attrs) attrs = FILE_ATTRIBUTE_NORMAL;
331
332     handle = CreateFileW(path, GENERIC_READ | GENERIC_WRITE, 0,
333                          NULL, CREATE_ALWAYS, attrs, NULL);
334     if (handle == INVALID_HANDLE_VALUE)
335     {
336         if (GetFileAttributesW(path) == INVALID_FILE_ATTRIBUTES)
337             ERR("failed to create %s (error %d)\n",
338                 debugstr_w(path), GetLastError());
339
340         goto done;
341     }
342
343 done:
344     msi_free(path);
345
346     return (INT_PTR)handle;
347 }
348
349 static INT_PTR cabinet_close_file_info(FDINOTIFICATIONTYPE fdint,
350                                        PFDINOTIFICATION pfdin)
351 {
352     MSICABDATA *data = pfdin->pv;
353     FILETIME ft;
354     FILETIME ftLocal;
355     HANDLE handle = (HANDLE)pfdin->hf;
356
357     data->mi->is_continuous = FALSE;
358
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;
365
366     CloseHandle(handle);
367
368     data->cb(data->package, data->curfile, MSICABEXTRACT_FILEEXTRACTED, NULL, NULL,
369              data->user);
370
371     msi_free(data->curfile);
372     data->curfile = NULL;
373
374     return 1;
375 }
376
377 static INT_PTR CDECL cabinet_notify(FDINOTIFICATIONTYPE fdint, PFDINOTIFICATION pfdin)
378 {
379     TRACE("(%d)\n", fdint);
380
381     switch (fdint)
382     {
383     case fdintPARTIAL_FILE:
384         return cabinet_partial_file(fdint, pfdin);
385
386     case fdintNEXT_CABINET:
387         return cabinet_next_cabinet(fdint, pfdin);
388
389     case fdintCOPY_FILE:
390         return cabinet_copy_file(fdint, pfdin);
391
392     case fdintCLOSE_FILE_INFO:
393         return cabinet_close_file_info(fdint, pfdin);
394
395     default:
396         return 0;
397     }
398 }
399
400 /***********************************************************************
401  *            msi_cabextract
402  *
403  * Extract files from a cab file.
404  */
405 BOOL msi_cabextract(MSIPACKAGE* package, MSIMEDIAINFO *mi, LPVOID data)
406 {
407     LPSTR cabinet, cab_path = NULL;
408     LPWSTR ptr;
409     HFDI hfdi;
410     ERF erf;
411     BOOL ret = FALSE;
412
413     TRACE("Extracting %s\n", debugstr_w(mi->source));
414
415     hfdi = FDICreate(cabinet_alloc, cabinet_free, cabinet_open, cabinet_read,
416                      cabinet_write, cabinet_close, cabinet_seek, 0, &erf);
417     if (!hfdi)
418     {
419         ERR("FDICreate failed\n");
420         return FALSE;
421     }
422
423     ptr = strrchrW(mi->source, '\\') + 1;
424     cabinet = strdupWtoA(ptr);
425     if (!cabinet)
426         goto done;
427
428     cab_path = strdupWtoA(mi->source);
429     if (!cab_path)
430         goto done;
431
432     cab_path[ptr - mi->source] = '\0';
433
434     ret = FDICopy(hfdi, cabinet, cab_path, 0, cabinet_notify, NULL, data);
435     if (!ret)
436         ERR("FDICopy failed\n");
437
438 done:
439     FDIDestroy(hfdi);
440     msi_free(cabinet);
441     msi_free(cab_path);
442
443     if (ret)
444         mi->is_extracted = TRUE;
445
446     return ret;
447 }
448
449 void msi_free_media_info(MSIMEDIAINFO *mi)
450 {
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);
456 }
457
458 static UINT msi_load_media_info(MSIPACKAGE *package, MSIFILE *file, MSIMEDIAINFO *mi)
459 {
460     MSIRECORD *row;
461     LPWSTR source_dir;
462     LPWSTR source;
463     DWORD options;
464     UINT r;
465
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};
473
474     row = MSI_QueryGetRecord(package->db, query, file->Sequence, mi->disk_id);
475     if (!row)
476     {
477         TRACE("Unable to query row\n");
478         return ERROR_FUNCTION_FAILED;
479     }
480
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);
491
492     if (!mi->first_volume)
493         mi->first_volume = strdupW(mi->volume_label);
494
495     source_dir = msi_dup_property(package, cszSourceDir);
496     lstrcpyW(mi->source, source_dir);
497
498     PathStripToRootW(source_dir);
499     mi->type = GetDriveTypeW(source_dir);
500
501     if (file->IsCompressed && mi->cabinet)
502     {
503         if (mi->cabinet[0] == '#')
504         {
505             r = writeout_cabinet_stream(package, &mi->cabinet[1], mi->source);
506             if (r != ERROR_SUCCESS)
507             {
508                 ERR("Failed to extract cabinet stream\n");
509                 return ERROR_FUNCTION_FAILED;
510             }
511         }
512         else
513             lstrcatW(mi->source, mi->cabinet);
514     }
515
516     options = MSICODE_PRODUCT;
517     if (mi->type == DRIVE_CDROM || mi->type == DRIVE_REMOVABLE)
518     {
519         source = source_dir;
520         options |= MSISOURCETYPE_MEDIA;
521     }
522     else if (package->BaseURL && UrlIsW(package->BaseURL, URLIS_URL))
523     {
524         source = package->BaseURL;
525         options |= MSISOURCETYPE_URL;
526     }
527     else
528     {
529         source = mi->source;
530         options |= MSISOURCETYPE_NETWORK;
531     }
532
533     msi_package_add_media_disk(package, package->Context,
534                                MSICODE_PRODUCT, mi->disk_id,
535                                mi->volume_label, mi->disk_prompt);
536
537     msi_package_add_info(package, package->Context,
538                          options, INSTALLPROPERTY_LASTUSEDSOURCEW, source);
539
540     msi_free(source_dir);
541     return ERROR_SUCCESS;
542 }
543
544 /* FIXME: search NETWORK and URL sources as well */
545 static UINT find_published_source(MSIPACKAGE *package, MSIMEDIAINFO *mi)
546 {
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;
553
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;
560
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)
568     {
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);
574
575         if (source_matches_volume(mi, source))
576         {
577             /* FIXME: what about SourceDir */
578             lstrcpyW(mi->source, source);
579             lstrcatW(mi->source, mi->cabinet);
580             return ERROR_SUCCESS;
581         }
582     }
583
584     return ERROR_FUNCTION_FAILED;
585 }
586
587 UINT ready_media(MSIPACKAGE *package, MSIFILE *file, MSIMEDIAINFO *mi)
588 {
589     UINT rc = ERROR_SUCCESS;
590
591     /* media info for continuous cabinet is already loaded */
592     if (mi->is_continuous)
593         return ERROR_SUCCESS;
594
595     rc = msi_load_media_info(package, file, mi);
596     if (rc != ERROR_SUCCESS)
597     {
598         ERR("Unable to load media info\n");
599         return ERROR_FUNCTION_FAILED;
600     }
601
602     /* cabinet is internal, no checks needed */
603     if (!mi->cabinet || mi->cabinet[0] == '#')
604         return ERROR_SUCCESS;
605
606     /* package should be downloaded */
607     if (file->IsCompressed &&
608         GetFileAttributesW(mi->source) == INVALID_FILE_ATTRIBUTES &&
609         package->BaseURL && UrlIsW(package->BaseURL, URLIS_URL))
610     {
611         WCHAR temppath[MAX_PATH];
612
613         msi_download_file(mi->source, temppath);
614         lstrcpyW(mi->source, temppath);
615         return ERROR_SUCCESS;
616     }
617
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))
621     {
622         LPWSTR source = msi_dup_property(package, cszSourceDir);
623         BOOL matches;
624
625         matches = source_matches_volume(mi, source);
626         msi_free(source);
627
628         if ((mi->type == DRIVE_CDROM || mi->type == DRIVE_REMOVABLE) && !matches)
629         {
630             rc = msi_change_media(package, mi);
631             if (rc != ERROR_SUCCESS)
632                 return rc;
633         }
634     }
635
636     if (file->IsCompressed &&
637         GetFileAttributesW(mi->source) == INVALID_FILE_ATTRIBUTES)
638     {
639         rc = find_published_source(package, mi);
640         if (rc != ERROR_SUCCESS)
641         {
642             ERR("Cabinet not found: %s\n", debugstr_w(mi->source));
643             return ERROR_INSTALL_FAILURE;
644         }
645     }
646
647     return ERROR_SUCCESS;
648 }