riched32/tests: Remove the todo_wine logic where appropriate.
[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 "msvcrt/fcntl.h"
28 #include "msipriv.h"
29 #include "winuser.h"
30 #include "winreg.h"
31 #include "shlwapi.h"
32 #include "wine/unicode.h"
33
34 WINE_DEFAULT_DEBUG_CHANNEL(msi);
35
36 static BOOL source_matches_volume(MSIMEDIAINFO *mi, LPWSTR source_root)
37 {
38     WCHAR volume_name[MAX_PATH + 1];
39
40     if (!GetVolumeInformationW(source_root, volume_name, MAX_PATH + 1,
41                                NULL, NULL, NULL, NULL, 0))
42     {
43         ERR("Failed to get volume information\n");
44         return FALSE;
45     }
46
47     return !lstrcmpW(mi->volume_label, volume_name);
48 }
49
50 static UINT msi_change_media(MSIPACKAGE *package, MSIMEDIAINFO *mi)
51 {
52     LPSTR msg;
53     LPWSTR error, error_dialog;
54     LPWSTR source_dir;
55     UINT r = ERROR_SUCCESS;
56
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};
59
60     if ((msi_get_property_int(package, szUILevel, 0) & INSTALLUILEVEL_MASK) ==
61          INSTALLUILEVEL_NONE && !gUIHandlerA)
62         return ERROR_SUCCESS;
63
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);
68
69     while (r == ERROR_SUCCESS &&
70            !source_matches_volume(mi, source_dir))
71     {
72         r = msi_spawn_error_dialog(package, error_dialog, error);
73
74         if (gUIHandlerA)
75         {
76             msg = strdupWtoA(error);
77             gUIHandlerA(gUIContext, MB_RETRYCANCEL | INSTALLMESSAGE_ERROR, msg);
78             msi_free(msg);
79         }
80     }
81
82     msi_free(error);
83     msi_free(error_dialog);
84     msi_free(source_dir);
85
86     return r;
87 }
88
89 static UINT writeout_cabinet_stream(MSIPACKAGE *package, LPCWSTR stream,
90                                     WCHAR* source)
91 {
92     UINT rc;
93     USHORT* data;
94     UINT size;
95     DWORD write;
96     HANDLE hfile;
97     WCHAR tmp[MAX_PATH];
98
99     static const WCHAR cszTempFolder[]= {
100         'T','e','m','p','F','o','l','d','e','r',0};
101
102     rc = read_raw_stream_data(package->db, stream, &data, &size);
103     if (rc != ERROR_SUCCESS)
104         return rc;
105
106     write = MAX_PATH;
107     if (MSI_GetPropertyW(package, cszTempFolder, tmp, &write))
108         GetTempPathW(MAX_PATH, tmp);
109
110     GetTempFileNameW(tmp, stream, 0, source);
111
112     track_tempfile(package, source);
113     hfile = CreateFileW(source, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS,
114                         FILE_ATTRIBUTE_NORMAL, NULL);
115
116     if (hfile == INVALID_HANDLE_VALUE)
117     {
118         ERR("Unable to create file %s\n", debugstr_w(source));
119         rc = ERROR_FUNCTION_FAILED;
120         goto end;
121     }
122
123     WriteFile(hfile, data, size, &write, NULL);
124     CloseHandle(hfile);
125     TRACE("wrote %i bytes to %s\n", write, debugstr_w(source));
126
127 end:
128     msi_free(data);
129     return rc;
130 }
131
132 static void *cabinet_alloc(ULONG cb)
133 {
134     return msi_alloc(cb);
135 }
136
137 static void cabinet_free(void *pv)
138 {
139     msi_free(pv);
140 }
141
142 static INT_PTR cabinet_open(char *pszFile, int oflag, int pmode)
143 {
144     HANDLE handle;
145     DWORD dwAccess = 0;
146     DWORD dwShareMode = 0;
147     DWORD dwCreateDisposition = OPEN_EXISTING;
148
149     switch (oflag & _O_ACCMODE)
150     {
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;
163     }
164
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;
169
170     handle = CreateFileA(pszFile, dwAccess, dwShareMode, NULL,
171                          dwCreateDisposition, 0, NULL);
172     if (handle == INVALID_HANDLE_VALUE)
173         return 0;
174
175     return (INT_PTR)handle;
176 }
177
178 static UINT cabinet_read(INT_PTR hf, void *pv, UINT cb)
179 {
180     HANDLE handle = (HANDLE)hf;
181     DWORD read;
182
183     if (ReadFile(handle, pv, cb, &read, NULL))
184         return read;
185
186     return 0;
187 }
188
189 static UINT cabinet_write(INT_PTR hf, void *pv, UINT cb)
190 {
191     HANDLE handle = (HANDLE)hf;
192     DWORD written;
193
194     if (WriteFile(handle, pv, cb, &written, NULL))
195         return written;
196
197     return 0;
198 }
199
200 static int cabinet_close(INT_PTR hf)
201 {
202     HANDLE handle = (HANDLE)hf;
203     return CloseHandle(handle) ? 0 : -1;
204 }
205
206 static long cabinet_seek(INT_PTR hf, long dist, int seektype)
207 {
208     HANDLE handle = (HANDLE)hf;
209     /* flags are compatible and so are passed straight through */
210     return SetFilePointer(handle, dist, NULL, seektype);
211 }
212
213 static UINT msi_media_get_disk_info(MSIPACKAGE *package, MSIMEDIAINFO *mi)
214 {
215     MSIRECORD *row;
216     LPWSTR ptr;
217
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};
222
223     row = MSI_QueryGetRecord(package->db, query, mi->disk_id);
224     if (!row)
225     {
226         TRACE("Unable to query row\n");
227         return ERROR_FUNCTION_FAILED;
228     }
229
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));
233
234     if (!mi->first_volume)
235         mi->first_volume = strdupW(mi->volume_label);
236
237     ptr = strrchrW(mi->source, '\\') + 1;
238     lstrcpyW(ptr, mi->cabinet);
239     msiobj_release(&row->hdr);
240
241     return ERROR_SUCCESS;
242 }
243
244 static INT_PTR cabinet_partial_file(FDINOTIFICATIONTYPE fdint,
245                                     PFDINOTIFICATION pfdin)
246 {
247     MSICABDATA *data = (MSICABDATA *)pfdin->pv;
248     data->mi->is_continuous = FALSE;
249     return 0;
250 }
251
252 static INT_PTR cabinet_next_cabinet(FDINOTIFICATIONTYPE fdint,
253                                     PFDINOTIFICATION pfdin)
254 {
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;
260
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;
267
268     mi->disk_id++;
269     mi->is_continuous = TRUE;
270
271     rc = msi_media_get_disk_info(data->package, mi);
272     if (rc != ERROR_SUCCESS)
273     {
274         ERR("Failed to get next cabinet information: %d\n", rc);
275         goto done;
276     }
277
278     if (lstrcmpiW(mi->cabinet, cab))
279     {
280         ERR("Continuous cabinet does not match the next cabinet in the Media table\n");
281         goto done;
282     }
283
284     TRACE("Searching for %s\n", debugstr_w(mi->source));
285
286     res = 0;
287     if (GetFileAttributesW(mi->source) == INVALID_FILE_ATTRIBUTES)
288     {
289         if (msi_change_media(data->package, mi) != ERROR_SUCCESS)
290             res = -1;
291     }
292
293 done:
294     msi_free(cab);
295     return res;
296 }
297
298 static INT_PTR cabinet_copy_file(FDINOTIFICATIONTYPE fdint,
299                                  PFDINOTIFICATION pfdin)
300 {
301     MSICABDATA *data = (MSICABDATA*)pfdin->pv;
302     HANDLE handle = 0;
303     LPWSTR path = NULL;
304     DWORD attrs;
305
306     data->curfile = strdupAtoW(pfdin->psz1);
307     if (!data->cb(data->package, data->curfile, MSICABEXTRACT_BEGINEXTRACT, &path,
308                   &attrs, data->user))
309         goto done;
310
311     TRACE("extracting %s\n", debugstr_w(path));
312
313     attrs = attrs & (FILE_ATTRIBUTE_READONLY|FILE_ATTRIBUTE_HIDDEN|FILE_ATTRIBUTE_SYSTEM);
314     if (!attrs) attrs = FILE_ATTRIBUTE_NORMAL;
315
316     handle = CreateFileW(path, GENERIC_READ | GENERIC_WRITE, 0,
317                          NULL, CREATE_ALWAYS, attrs, NULL);
318     if (handle == INVALID_HANDLE_VALUE)
319     {
320         if (GetFileAttributesW(path) == INVALID_FILE_ATTRIBUTES)
321             ERR("failed to create %s (error %d)\n",
322                 debugstr_w(path), GetLastError());
323
324         goto done;
325     }
326
327 done:
328     msi_free(path);
329
330     return (INT_PTR)handle;
331 }
332
333 static INT_PTR cabinet_close_file_info(FDINOTIFICATIONTYPE fdint,
334                                        PFDINOTIFICATION pfdin)
335 {
336     MSICABDATA *data = (MSICABDATA*)pfdin->pv;
337     FILETIME ft;
338     FILETIME ftLocal;
339     HANDLE handle = (HANDLE)pfdin->hf;
340
341     data->mi->is_continuous = FALSE;
342
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;
349
350     CloseHandle(handle);
351
352     data->cb(data->package, data->curfile, MSICABEXTRACT_FILEEXTRACTED, NULL, NULL,
353              data->user);
354
355     msi_free(data->curfile);
356     data->curfile = NULL;
357
358     return 1;
359 }
360
361 static INT_PTR cabinet_notify(FDINOTIFICATIONTYPE fdint, PFDINOTIFICATION pfdin)
362 {
363     TRACE("(%d)\n", fdint);
364
365     switch (fdint)
366     {
367     case fdintPARTIAL_FILE:
368         return cabinet_partial_file(fdint, pfdin);
369
370     case fdintNEXT_CABINET:
371         return cabinet_next_cabinet(fdint, pfdin);
372
373     case fdintCOPY_FILE:
374         return cabinet_copy_file(fdint, pfdin);
375
376     case fdintCLOSE_FILE_INFO:
377         return cabinet_close_file_info(fdint, pfdin);
378
379     default:
380         return 0;
381     }
382 }
383
384 /***********************************************************************
385  *            msi_cabextract
386  *
387  * Extract files from a cab file.
388  */
389 BOOL msi_cabextract(MSIPACKAGE* package, MSIMEDIAINFO *mi, LPVOID data)
390 {
391     LPSTR cabinet, cab_path = NULL;
392     LPWSTR ptr;
393     HFDI hfdi;
394     ERF erf;
395     BOOL ret = FALSE;
396
397     TRACE("Extracting %s\n", debugstr_w(mi->source));
398
399     hfdi = FDICreate(cabinet_alloc, cabinet_free, cabinet_open, cabinet_read,
400                      cabinet_write, cabinet_close, cabinet_seek, 0, &erf);
401     if (!hfdi)
402     {
403         ERR("FDICreate failed\n");
404         return FALSE;
405     }
406
407     ptr = strrchrW(mi->source, '\\') + 1;
408     cabinet = strdupWtoA(ptr);
409     if (!cabinet)
410         goto done;
411
412     cab_path = strdupWtoA(mi->source);
413     if (!cab_path)
414         goto done;
415
416     cab_path[ptr - mi->source] = '\0';
417
418     ret = FDICopy(hfdi, cabinet, cab_path, 0, cabinet_notify, NULL, data);
419     if (!ret)
420         ERR("FDICopy failed\n");
421
422 done:
423     FDIDestroy(hfdi);
424     msi_free(cabinet);
425     msi_free(cab_path);
426
427     if (ret)
428         mi->is_extracted = TRUE;
429
430     return ret;
431 }
432
433 void msi_free_media_info(MSIMEDIAINFO *mi)
434 {
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);
440 }
441
442 UINT msi_load_media_info(MSIPACKAGE *package, MSIFILE *file, MSIMEDIAINFO *mi)
443 {
444     MSIRECORD *row;
445     LPWSTR source_dir;
446     LPWSTR source;
447     DWORD options;
448     UINT r;
449
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};
457
458     row = MSI_QueryGetRecord(package->db, query, file->Sequence, mi->disk_id);
459     if (!row)
460     {
461         TRACE("Unable to query row\n");
462         return ERROR_FUNCTION_FAILED;
463     }
464
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);
475
476     if (!mi->first_volume)
477         mi->first_volume = strdupW(mi->volume_label);
478
479     source_dir = msi_dup_property(package, cszSourceDir);
480     lstrcpyW(mi->source, source_dir);
481
482     PathStripToRootW(source_dir);
483     mi->type = GetDriveTypeW(source_dir);
484
485     if (file->IsCompressed && mi->cabinet)
486     {
487         if (mi->cabinet[0] == '#')
488         {
489             r = writeout_cabinet_stream(package, &mi->cabinet[1], mi->source);
490             if (r != ERROR_SUCCESS)
491             {
492                 ERR("Failed to extract cabinet stream\n");
493                 return ERROR_FUNCTION_FAILED;
494             }
495         }
496         else
497             lstrcatW(mi->source, mi->cabinet);
498     }
499
500     options = MSICODE_PRODUCT;
501     if (mi->type == DRIVE_CDROM || mi->type == DRIVE_REMOVABLE)
502     {
503         source = source_dir;
504         options |= MSISOURCETYPE_MEDIA;
505     }
506     else if (package->BaseURL && UrlIsW(package->BaseURL, URLIS_URL))
507     {
508         source = package->BaseURL;
509         options |= MSISOURCETYPE_URL;
510     }
511     else
512     {
513         source = mi->source;
514         options |= MSISOURCETYPE_NETWORK;
515     }
516
517     msi_package_add_media_disk(package, package->Context,
518                                MSICODE_PRODUCT, mi->disk_id,
519                                mi->volume_label, mi->disk_prompt);
520
521     msi_package_add_info(package, package->Context,
522                          options, INSTALLPROPERTY_LASTUSEDSOURCEW, source);
523
524     msi_free(source_dir);
525     return ERROR_SUCCESS;
526 }
527
528 /* FIXME: search NETWORK and URL sources as well */
529 UINT find_published_source(MSIPACKAGE *package, MSIMEDIAINFO *mi)
530 {
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;
537
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;
544
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)
552     {
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);
558
559         if (source_matches_volume(mi, source))
560         {
561             /* FIXME: what about SourceDir */
562             lstrcpyW(mi->source, source);
563             lstrcatW(mi->source, mi->cabinet);
564             return ERROR_SUCCESS;
565         }
566     }
567
568     return ERROR_FUNCTION_FAILED;
569 }
570
571 UINT ready_media(MSIPACKAGE *package, MSIFILE *file, MSIMEDIAINFO *mi)
572 {
573     UINT rc = ERROR_SUCCESS;
574
575     /* media info for continuous cabinet is already loaded */
576     if (mi->is_continuous)
577         return ERROR_SUCCESS;
578
579     rc = msi_load_media_info(package, file, mi);
580     if (rc != ERROR_SUCCESS)
581     {
582         ERR("Unable to load media info\n");
583         return ERROR_FUNCTION_FAILED;
584     }
585
586     /* cabinet is internal, no checks needed */
587     if (!mi->cabinet || mi->cabinet[0] == '#')
588         return ERROR_SUCCESS;
589
590     /* package should be downloaded */
591     if (file->IsCompressed &&
592         GetFileAttributesW(mi->source) == INVALID_FILE_ATTRIBUTES &&
593         package->BaseURL && UrlIsW(package->BaseURL, URLIS_URL))
594     {
595         WCHAR temppath[MAX_PATH];
596
597         msi_download_file(mi->source, temppath);
598         lstrcpyW(mi->source, temppath);
599         return ERROR_SUCCESS;
600     }
601
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))
605     {
606         LPWSTR source = msi_dup_property(package, cszSourceDir);
607         BOOL matches;
608
609         matches = source_matches_volume(mi, source);
610         msi_free(source);
611
612         if ((mi->type == DRIVE_CDROM || mi->type == DRIVE_REMOVABLE) && !matches)
613         {
614             rc = msi_change_media(package, mi);
615             if (rc != ERROR_SUCCESS)
616                 return rc;
617         }
618     }
619
620     if (file->IsCompressed &&
621         GetFileAttributesW(mi->source) == INVALID_FILE_ATTRIBUTES)
622     {
623         rc = find_published_source(package, mi);
624         if (rc != ERROR_SUCCESS)
625         {
626             ERR("Cabinet not found: %s\n", debugstr_w(mi->source));
627             return ERROR_INSTALL_FAILURE;
628         }
629     }
630
631     return ERROR_SUCCESS;
632 }