fltlib: Add a stub dll.
[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, LPCWSTR source_root)
53 {
54     WCHAR volume_name[MAX_PATH + 1];
55     WCHAR root[MAX_PATH + 1];
56
57     strcpyW(root, source_root);
58     PathStripToRootW(root);
59     PathAddBackslashW(root);
60
61     if (!GetVolumeInformationW(root, volume_name, MAX_PATH + 1,
62                                NULL, NULL, NULL, NULL, 0))
63     {
64         ERR("Failed to get volume information\n");
65         return FALSE;
66     }
67
68     return !lstrcmpW(mi->volume_label, volume_name);
69 }
70
71 static UINT msi_change_media(MSIPACKAGE *package, MSIMEDIAINFO *mi)
72 {
73     LPSTR msg;
74     LPWSTR error, error_dialog;
75     LPWSTR source_dir;
76     UINT r = ERROR_SUCCESS;
77
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};
80
81     if ((msi_get_property_int(package, szUILevel, 0) & INSTALLUILEVEL_MASK) ==
82          INSTALLUILEVEL_NONE && !gUIHandlerA)
83         return ERROR_SUCCESS;
84
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);
88
89     while (r == ERROR_SUCCESS &&
90            !source_matches_volume(mi, source_dir))
91     {
92         r = msi_spawn_error_dialog(package, error_dialog, error);
93
94         if (gUIHandlerA)
95         {
96             msg = strdupWtoA(error);
97             gUIHandlerA(gUIContext, MB_RETRYCANCEL | INSTALLMESSAGE_ERROR, msg);
98             msi_free(msg);
99         }
100     }
101
102     msi_free(error);
103     msi_free(error_dialog);
104     msi_free(source_dir);
105
106     return r;
107 }
108
109 static UINT writeout_cabinet_stream(MSIPACKAGE *package, LPCWSTR stream,
110                                     WCHAR* source)
111 {
112     UINT rc;
113     USHORT* data;
114     UINT size;
115     DWORD write;
116     HANDLE hfile;
117     WCHAR tmp[MAX_PATH];
118
119     static const WCHAR cszTempFolder[]= {
120         'T','e','m','p','F','o','l','d','e','r',0};
121
122     rc = read_raw_stream_data(package->db, stream, &data, &size);
123     if (rc != ERROR_SUCCESS)
124         return rc;
125
126     write = MAX_PATH;
127     if (MSI_GetPropertyW(package, cszTempFolder, tmp, &write))
128         GetTempPathW(MAX_PATH, tmp);
129
130     GetTempFileNameW(tmp, stream, 0, source);
131
132     track_tempfile(package, source);
133     hfile = CreateFileW(source, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS,
134                         FILE_ATTRIBUTE_NORMAL, NULL);
135
136     if (hfile == INVALID_HANDLE_VALUE)
137     {
138         ERR("Unable to create file %s\n", debugstr_w(source));
139         rc = ERROR_FUNCTION_FAILED;
140         goto end;
141     }
142
143     WriteFile(hfile, data, size, &write, NULL);
144     CloseHandle(hfile);
145     TRACE("wrote %i bytes to %s\n", write, debugstr_w(source));
146
147 end:
148     msi_free(data);
149     return rc;
150 }
151
152 static void * CDECL cabinet_alloc(ULONG cb)
153 {
154     return msi_alloc(cb);
155 }
156
157 static void CDECL cabinet_free(void *pv)
158 {
159     msi_free(pv);
160 }
161
162 static INT_PTR CDECL cabinet_open(char *pszFile, int oflag, int pmode)
163 {
164     HANDLE handle;
165     DWORD dwAccess = 0;
166     DWORD dwShareMode = 0;
167     DWORD dwCreateDisposition = OPEN_EXISTING;
168
169     switch (oflag & _O_ACCMODE)
170     {
171     case _O_RDONLY:
172         dwAccess = GENERIC_READ;
173         dwShareMode = FILE_SHARE_READ | FILE_SHARE_DELETE;
174         break;
175     case _O_WRONLY:
176         dwAccess = GENERIC_WRITE;
177         dwShareMode = FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE;
178         break;
179     case _O_RDWR:
180         dwAccess = GENERIC_READ | GENERIC_WRITE;
181         dwShareMode = FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE;
182         break;
183     }
184
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;
189
190     handle = CreateFileA(pszFile, dwAccess, dwShareMode, NULL,
191                          dwCreateDisposition, 0, NULL);
192     if (handle == INVALID_HANDLE_VALUE)
193         return 0;
194
195     return (INT_PTR)handle;
196 }
197
198 static UINT CDECL cabinet_read(INT_PTR hf, void *pv, UINT cb)
199 {
200     HANDLE handle = (HANDLE)hf;
201     DWORD read;
202
203     if (ReadFile(handle, pv, cb, &read, NULL))
204         return read;
205
206     return 0;
207 }
208
209 static UINT CDECL cabinet_write(INT_PTR hf, void *pv, UINT cb)
210 {
211     HANDLE handle = (HANDLE)hf;
212     DWORD written;
213
214     if (WriteFile(handle, pv, cb, &written, NULL))
215         return written;
216
217     return 0;
218 }
219
220 static int CDECL cabinet_close(INT_PTR hf)
221 {
222     HANDLE handle = (HANDLE)hf;
223     return CloseHandle(handle) ? 0 : -1;
224 }
225
226 static LONG CDECL cabinet_seek(INT_PTR hf, LONG dist, int seektype)
227 {
228     HANDLE handle = (HANDLE)hf;
229     /* flags are compatible and so are passed straight through */
230     return SetFilePointer(handle, dist, NULL, seektype);
231 }
232
233 static UINT CDECL msi_media_get_disk_info(MSIPACKAGE *package, MSIMEDIAINFO *mi)
234 {
235     MSIRECORD *row;
236     LPWSTR ptr;
237
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};
242
243     row = MSI_QueryGetRecord(package->db, query, mi->disk_id);
244     if (!row)
245     {
246         TRACE("Unable to query row\n");
247         return ERROR_FUNCTION_FAILED;
248     }
249
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));
253
254     if (!mi->first_volume)
255         mi->first_volume = strdupW(mi->volume_label);
256
257     ptr = strrchrW(mi->source, '\\') + 1;
258     lstrcpyW(ptr, mi->cabinet);
259     msiobj_release(&row->hdr);
260
261     return ERROR_SUCCESS;
262 }
263
264 static INT_PTR cabinet_partial_file(FDINOTIFICATIONTYPE fdint,
265                                     PFDINOTIFICATION pfdin)
266 {
267     MSICABDATA *data = pfdin->pv;
268     data->mi->is_continuous = FALSE;
269     return 0;
270 }
271
272 static INT_PTR cabinet_next_cabinet(FDINOTIFICATIONTYPE fdint,
273                                     PFDINOTIFICATION pfdin)
274 {
275     MSICABDATA *data = pfdin->pv;
276     MSIMEDIAINFO *mi = data->mi;
277     LPWSTR cab = strdupAtoW(pfdin->psz1);
278     INT_PTR res = -1;
279     UINT rc;
280
281     msi_free(mi->disk_prompt);
282     msi_free(mi->cabinet);
283     msi_free(mi->volume_label);
284     mi->disk_prompt = NULL;
285     mi->cabinet = NULL;
286     mi->volume_label = NULL;
287
288     mi->disk_id++;
289     mi->is_continuous = TRUE;
290
291     rc = msi_media_get_disk_info(data->package, mi);
292     if (rc != ERROR_SUCCESS)
293     {
294         ERR("Failed to get next cabinet information: %d\n", rc);
295         goto done;
296     }
297
298     if (lstrcmpiW(mi->cabinet, cab))
299     {
300         ERR("Continuous cabinet does not match the next cabinet in the Media table\n");
301         goto done;
302     }
303
304     TRACE("Searching for %s\n", debugstr_w(mi->source));
305
306     res = 0;
307     if (GetFileAttributesW(mi->source) == INVALID_FILE_ATTRIBUTES)
308     {
309         if (msi_change_media(data->package, mi) != ERROR_SUCCESS)
310             res = -1;
311     }
312
313 done:
314     msi_free(cab);
315     return res;
316 }
317
318 static INT_PTR cabinet_copy_file(FDINOTIFICATIONTYPE fdint,
319                                  PFDINOTIFICATION pfdin)
320 {
321     MSICABDATA *data = pfdin->pv;
322     HANDLE handle = 0;
323     LPWSTR path = NULL;
324     DWORD attrs;
325
326     data->curfile = strdupAtoW(pfdin->psz1);
327     if (!data->cb(data->package, data->curfile, MSICABEXTRACT_BEGINEXTRACT, &path,
328                   &attrs, data->user))
329         goto done;
330
331     TRACE("extracting %s\n", debugstr_w(path));
332
333     attrs = attrs & (FILE_ATTRIBUTE_READONLY|FILE_ATTRIBUTE_HIDDEN|FILE_ATTRIBUTE_SYSTEM);
334     if (!attrs) attrs = FILE_ATTRIBUTE_NORMAL;
335
336     handle = CreateFileW(path, GENERIC_READ | GENERIC_WRITE, 0,
337                          NULL, CREATE_ALWAYS, attrs, NULL);
338     if (handle == INVALID_HANDLE_VALUE)
339     {
340         DWORD err = GetLastError();
341         DWORD attrs = GetFileAttributesW(path);
342
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))
346         {
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);
350         }
351         else
352             WARN("failed to create %s (error %d)\n", debugstr_w(path), err);
353     }
354
355 done:
356     msi_free(path);
357
358     return (INT_PTR)handle;
359 }
360
361 static INT_PTR cabinet_close_file_info(FDINOTIFICATIONTYPE fdint,
362                                        PFDINOTIFICATION pfdin)
363 {
364     MSICABDATA *data = pfdin->pv;
365     FILETIME ft;
366     FILETIME ftLocal;
367     HANDLE handle = (HANDLE)pfdin->hf;
368
369     data->mi->is_continuous = FALSE;
370
371     if (!DosDateTimeToFileTime(pfdin->date, pfdin->time, &ft))
372         return -1;
373     if (!LocalFileTimeToFileTime(&ft, &ftLocal))
374         return -1;
375     if (!SetFileTime(handle, &ftLocal, 0, &ftLocal))
376         return -1;
377
378     CloseHandle(handle);
379
380     data->cb(data->package, data->curfile, MSICABEXTRACT_FILEEXTRACTED, NULL, NULL,
381              data->user);
382
383     msi_free(data->curfile);
384     data->curfile = NULL;
385
386     return 1;
387 }
388
389 static INT_PTR CDECL cabinet_notify(FDINOTIFICATIONTYPE fdint, PFDINOTIFICATION pfdin)
390 {
391     TRACE("(%d)\n", fdint);
392
393     switch (fdint)
394     {
395     case fdintPARTIAL_FILE:
396         return cabinet_partial_file(fdint, pfdin);
397
398     case fdintNEXT_CABINET:
399         return cabinet_next_cabinet(fdint, pfdin);
400
401     case fdintCOPY_FILE:
402         return cabinet_copy_file(fdint, pfdin);
403
404     case fdintCLOSE_FILE_INFO:
405         return cabinet_close_file_info(fdint, pfdin);
406
407     default:
408         return 0;
409     }
410 }
411
412 /***********************************************************************
413  *            msi_cabextract
414  *
415  * Extract files from a cab file.
416  */
417 BOOL msi_cabextract(MSIPACKAGE* package, MSIMEDIAINFO *mi, LPVOID data)
418 {
419     LPSTR cabinet, cab_path = NULL;
420     LPWSTR ptr;
421     HFDI hfdi;
422     ERF erf;
423     BOOL ret = FALSE;
424
425     TRACE("Extracting %s\n", debugstr_w(mi->source));
426
427     hfdi = FDICreate(cabinet_alloc, cabinet_free, cabinet_open, cabinet_read,
428                      cabinet_write, cabinet_close, cabinet_seek, 0, &erf);
429     if (!hfdi)
430     {
431         ERR("FDICreate failed\n");
432         return FALSE;
433     }
434
435     ptr = strrchrW(mi->source, '\\') + 1;
436     cabinet = strdupWtoA(ptr);
437     if (!cabinet)
438         goto done;
439
440     cab_path = strdupWtoA(mi->source);
441     if (!cab_path)
442         goto done;
443
444     cab_path[ptr - mi->source] = '\0';
445
446     ret = FDICopy(hfdi, cabinet, cab_path, 0, cabinet_notify, NULL, data);
447     if (!ret)
448         ERR("FDICopy failed\n");
449
450 done:
451     FDIDestroy(hfdi);
452     msi_free(cabinet);
453     msi_free(cab_path);
454
455     if (ret)
456         mi->is_extracted = TRUE;
457
458     return ret;
459 }
460
461 void msi_free_media_info(MSIMEDIAINFO *mi)
462 {
463     msi_free(mi->disk_prompt);
464     msi_free(mi->cabinet);
465     msi_free(mi->volume_label);
466     msi_free(mi->first_volume);
467     msi_free(mi);
468 }
469
470 static UINT get_drive_type(const WCHAR *path)
471 {
472     WCHAR root[MAX_PATH + 1];
473
474     strcpyW(root, path);
475     PathStripToRootW(root);
476     PathAddBackslashW(root);
477
478     return GetDriveTypeW(root);
479 }
480
481 static UINT msi_load_media_info(MSIPACKAGE *package, MSIFILE *file, MSIMEDIAINFO *mi)
482 {
483     MSIRECORD *row;
484     LPWSTR source_dir;
485     LPWSTR source;
486     DWORD options;
487     UINT r;
488
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};
496
497     row = MSI_QueryGetRecord(package->db, query, file->Sequence, mi->disk_id);
498     if (!row)
499     {
500         TRACE("Unable to query row\n");
501         return ERROR_FUNCTION_FAILED;
502     }
503
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);
514
515     if (!mi->first_volume)
516         mi->first_volume = strdupW(mi->volume_label);
517
518     source_dir = msi_dup_property(package, cszSourceDir);
519     lstrcpyW(mi->source, source_dir);
520     mi->type = get_drive_type(source_dir);
521
522     if (file->IsCompressed && mi->cabinet)
523     {
524         if (mi->cabinet[0] == '#')
525         {
526             r = writeout_cabinet_stream(package, &mi->cabinet[1], mi->source);
527             if (r != ERROR_SUCCESS)
528             {
529                 ERR("Failed to extract cabinet stream\n");
530                 return ERROR_FUNCTION_FAILED;
531             }
532         }
533         else
534             lstrcatW(mi->source, mi->cabinet);
535     }
536
537     options = MSICODE_PRODUCT;
538     if (mi->type == DRIVE_CDROM || mi->type == DRIVE_REMOVABLE)
539     {
540         source = source_dir;
541         options |= MSISOURCETYPE_MEDIA;
542     }
543     else if (package->BaseURL && UrlIsW(package->BaseURL, URLIS_URL))
544     {
545         source = package->BaseURL;
546         options |= MSISOURCETYPE_URL;
547     }
548     else
549     {
550         source = mi->source;
551         options |= MSISOURCETYPE_NETWORK;
552     }
553
554     msi_package_add_media_disk(package, package->Context,
555                                MSICODE_PRODUCT, mi->disk_id,
556                                mi->volume_label, mi->disk_prompt);
557
558     msi_package_add_info(package, package->Context,
559                          options, INSTALLPROPERTY_LASTUSEDSOURCEW, source);
560
561     msi_free(source_dir);
562     return ERROR_SUCCESS;
563 }
564
565 /* FIXME: search NETWORK and URL sources as well */
566 static UINT find_published_source(MSIPACKAGE *package, MSIMEDIAINFO *mi)
567 {
568     WCHAR source[MAX_PATH];
569     WCHAR volume[MAX_PATH];
570     WCHAR prompt[MAX_PATH];
571     DWORD volumesz, promptsz;
572     DWORD index, size, id;
573     UINT r;
574
575     size = MAX_PATH;
576     r = MsiSourceListGetInfoW(package->ProductCode, NULL,
577                               package->Context, MSICODE_PRODUCT,
578                               INSTALLPROPERTY_LASTUSEDSOURCEW, source, &size);
579     if (r != ERROR_SUCCESS)
580         return r;
581
582     index = 0;
583     volumesz = MAX_PATH;
584     promptsz = MAX_PATH;
585     while (MsiSourceListEnumMediaDisksW(package->ProductCode, NULL,
586                                         package->Context,
587                                         MSICODE_PRODUCT, index++, &id,
588                                         volume, &volumesz, prompt, &promptsz) == ERROR_SUCCESS)
589     {
590         mi->disk_id = id;
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);
595
596         if (source_matches_volume(mi, source))
597         {
598             /* FIXME: what about SourceDir */
599             lstrcpyW(mi->source, source);
600             return ERROR_SUCCESS;
601         }
602     }
603
604     return ERROR_FUNCTION_FAILED;
605 }
606
607 UINT ready_media(MSIPACKAGE *package, MSIFILE *file, MSIMEDIAINFO *mi)
608 {
609     UINT rc = ERROR_SUCCESS;
610
611     /* media info for continuous cabinet is already loaded */
612     if (mi->is_continuous)
613         return ERROR_SUCCESS;
614
615     rc = msi_load_media_info(package, file, mi);
616     if (rc != ERROR_SUCCESS)
617     {
618         ERR("Unable to load media info\n");
619         return ERROR_FUNCTION_FAILED;
620     }
621
622     /* cabinet is internal, no checks needed */
623     if (!mi->cabinet || mi->cabinet[0] == '#')
624         return ERROR_SUCCESS;
625
626     /* package should be downloaded */
627     if (file->IsCompressed &&
628         GetFileAttributesW(mi->source) == INVALID_FILE_ATTRIBUTES &&
629         package->BaseURL && UrlIsW(package->BaseURL, URLIS_URL))
630     {
631         WCHAR temppath[MAX_PATH];
632
633         msi_download_file(mi->source, temppath);
634         lstrcpyW(mi->source, temppath);
635         return ERROR_SUCCESS;
636     }
637
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))
641     {
642         LPWSTR source = msi_dup_property(package, cszSourceDir);
643         BOOL matches;
644
645         matches = source_matches_volume(mi, source);
646         msi_free(source);
647
648         if ((mi->type == DRIVE_CDROM || mi->type == DRIVE_REMOVABLE) && !matches)
649         {
650             rc = msi_change_media(package, mi);
651             if (rc != ERROR_SUCCESS)
652                 return rc;
653         }
654     }
655
656     if (file->IsCompressed &&
657         GetFileAttributesW(mi->source) == INVALID_FILE_ATTRIBUTES)
658     {
659         rc = find_published_source(package, mi);
660         if (rc != ERROR_SUCCESS)
661         {
662             ERR("Cabinet not found: %s\n", debugstr_w(mi->source));
663             return ERROR_INSTALL_FAILURE;
664         }
665     }
666
667     return ERROR_SUCCESS;
668 }