- Implement ProtocolResetComplete
[reactos.git] / dll / win32 / msi / files.c
1 /*
2 * Implementation of the Microsoft Installer (msi.dll)
3 *
4 * Copyright 2005 Aric Stewart for CodeWeavers
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
22 /*
23 * Actions dealing with files These are
24 *
25 * InstallFiles
26 * DuplicateFiles
27 * MoveFiles (TODO)
28 * PatchFiles (TODO)
29 * RemoveDuplicateFiles(TODO)
30 * RemoveFiles(TODO)
31 */
32
33 #include <stdarg.h>
34
35 #include "windef.h"
36 #include "winbase.h"
37 #include "winerror.h"
38 #include "wine/debug.h"
39 #include "fdi.h"
40 #include "msi.h"
41 #include "msidefs.h"
42 #include "msvcrt/fcntl.h"
43 #include "msipriv.h"
44 #include "winuser.h"
45 #include "winreg.h"
46 #include "shlwapi.h"
47 #include "wine/unicode.h"
48
49 WINE_DEFAULT_DEBUG_CHANNEL(msi);
50
51 extern const WCHAR szInstallFiles[];
52 extern const WCHAR szDuplicateFiles[];
53 extern const WCHAR szMoveFiles[];
54 extern const WCHAR szPatchFiles[];
55 extern const WCHAR szRemoveDuplicateFiles[];
56 extern const WCHAR szRemoveFiles[];
57
58 static const WCHAR cszTempFolder[]= {'T','e','m','p','F','o','l','d','e','r',0};
59
60 static BOOL source_matches_volume(MSIMEDIAINFO *mi, LPWSTR source_root)
61 {
62 WCHAR volume_name[MAX_PATH + 1];
63
64 if (!GetVolumeInformationW(source_root, volume_name, MAX_PATH + 1,
65 NULL, NULL, NULL, NULL, 0))
66 {
67 ERR("Failed to get volume information\n");
68 return FALSE;
69 }
70
71 return !lstrcmpW(mi->volume_label, volume_name);
72 }
73
74 static UINT msi_change_media( MSIPACKAGE *package, MSIMEDIAINFO *mi )
75 {
76 LPSTR msg;
77 LPWSTR error, error_dialog;
78 LPWSTR source_dir;
79 UINT r = ERROR_SUCCESS;
80
81 static const WCHAR szUILevel[] = {'U','I','L','e','v','e','l',0};
82 static const WCHAR error_prop[] = {'E','r','r','o','r','D','i','a','l','o','g',0};
83
84 if ( (msi_get_property_int(package, szUILevel, 0) & INSTALLUILEVEL_MASK) == INSTALLUILEVEL_NONE && !gUIHandlerA )
85 return ERROR_SUCCESS;
86
87 error = generate_error_string( package, 1302, 1, mi->disk_prompt );
88 error_dialog = msi_dup_property( package, error_prop );
89 source_dir = msi_dup_property( package, cszSourceDir );
90 PathStripToRootW(source_dir);
91
92 while ( r == ERROR_SUCCESS &&
93 !source_matches_volume(mi, source_dir) )
94 {
95 r = msi_spawn_error_dialog( package, error_dialog, error );
96
97 if (gUIHandlerA)
98 {
99 msg = strdupWtoA( error );
100 gUIHandlerA( gUIContext, MB_RETRYCANCEL | INSTALLMESSAGE_ERROR, msg );
101 msi_free(msg);
102 }
103 }
104
105 msi_free( error );
106 msi_free( error_dialog );
107 msi_free( source_dir );
108
109 return r;
110 }
111
112 /*
113 * This is a helper function for handling embedded cabinet media
114 */
115 static UINT writeout_cabinet_stream(MSIPACKAGE *package, LPCWSTR stream_name,
116 WCHAR* source)
117 {
118 UINT rc;
119 USHORT* data;
120 UINT size;
121 DWORD write;
122 HANDLE the_file;
123 WCHAR tmp[MAX_PATH];
124
125 rc = read_raw_stream_data(package->db,stream_name,&data,&size);
126 if (rc != ERROR_SUCCESS)
127 return rc;
128
129 write = MAX_PATH;
130 if (MSI_GetPropertyW(package, cszTempFolder, tmp, &write))
131 GetTempPathW(MAX_PATH,tmp);
132
133 GetTempFileNameW(tmp,stream_name,0,source);
134
135 track_tempfile(package, source);
136 the_file = CreateFileW(source, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS,
137 FILE_ATTRIBUTE_NORMAL, NULL);
138
139 if (the_file == INVALID_HANDLE_VALUE)
140 {
141 ERR("Unable to create file %s\n",debugstr_w(source));
142 rc = ERROR_FUNCTION_FAILED;
143 goto end;
144 }
145
146 WriteFile(the_file,data,size,&write,NULL);
147 CloseHandle(the_file);
148 TRACE("wrote %i bytes to %s\n",write,debugstr_w(source));
149 end:
150 msi_free(data);
151 return rc;
152 }
153
154
155 /* Support functions for FDI functions */
156 typedef struct
157 {
158 MSIPACKAGE* package;
159 MSIMEDIAINFO *mi;
160 } CabData;
161
162 static void * cabinet_alloc(ULONG cb)
163 {
164 return msi_alloc(cb);
165 }
166
167 static void cabinet_free(void *pv)
168 {
169 msi_free(pv);
170 }
171
172 static INT_PTR cabinet_open(char *pszFile, int oflag, int pmode)
173 {
174 HANDLE handle;
175 DWORD dwAccess = 0;
176 DWORD dwShareMode = 0;
177 DWORD dwCreateDisposition = OPEN_EXISTING;
178 switch (oflag & _O_ACCMODE)
179 {
180 case _O_RDONLY:
181 dwAccess = GENERIC_READ;
182 dwShareMode = FILE_SHARE_READ | FILE_SHARE_DELETE;
183 break;
184 case _O_WRONLY:
185 dwAccess = GENERIC_WRITE;
186 dwShareMode = FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE;
187 break;
188 case _O_RDWR:
189 dwAccess = GENERIC_READ | GENERIC_WRITE;
190 dwShareMode = FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE;
191 break;
192 }
193 if ((oflag & (_O_CREAT | _O_EXCL)) == (_O_CREAT | _O_EXCL))
194 dwCreateDisposition = CREATE_NEW;
195 else if (oflag & _O_CREAT)
196 dwCreateDisposition = CREATE_ALWAYS;
197 handle = CreateFileA( pszFile, dwAccess, dwShareMode, NULL,
198 dwCreateDisposition, 0, NULL );
199 if (handle == INVALID_HANDLE_VALUE)
200 return 0;
201 return (INT_PTR) handle;
202 }
203
204 static UINT cabinet_read(INT_PTR hf, void *pv, UINT cb)
205 {
206 HANDLE handle = (HANDLE) hf;
207 DWORD dwRead;
208 if (ReadFile(handle, pv, cb, &dwRead, NULL))
209 return dwRead;
210 return 0;
211 }
212
213 static UINT cabinet_write(INT_PTR hf, void *pv, UINT cb)
214 {
215 HANDLE handle = (HANDLE) hf;
216 DWORD dwWritten;
217 if (WriteFile(handle, pv, cb, &dwWritten, NULL))
218 return dwWritten;
219 return 0;
220 }
221
222 static int cabinet_close(INT_PTR hf)
223 {
224 HANDLE handle = (HANDLE) hf;
225 return CloseHandle(handle) ? 0 : -1;
226 }
227
228 static long cabinet_seek(INT_PTR hf, long dist, int seektype)
229 {
230 HANDLE handle = (HANDLE) hf;
231 /* flags are compatible and so are passed straight through */
232 return SetFilePointer(handle, dist, NULL, seektype);
233 }
234
235 static void msi_file_update_ui( MSIPACKAGE *package, MSIFILE *f, const WCHAR *action )
236 {
237 MSIRECORD *uirow;
238 LPWSTR uipath, p;
239
240 /* the UI chunk */
241 uirow = MSI_CreateRecord( 9 );
242 MSI_RecordSetStringW( uirow, 1, f->FileName );
243 uipath = strdupW( f->TargetPath );
244 p = strrchrW(uipath,'\\');
245 if (p)
246 p[1]=0;
247 MSI_RecordSetStringW( uirow, 9, uipath);
248 MSI_RecordSetInteger( uirow, 6, f->FileSize );
249 ui_actiondata( package, action, uirow);
250 msiobj_release( &uirow->hdr );
251 msi_free( uipath );
252 ui_progress( package, 2, f->FileSize, 0, 0);
253 }
254
255 static UINT msi_media_get_disk_info( MSIPACKAGE *package, MSIMEDIAINFO *mi )
256 {
257 MSIRECORD *row;
258 LPWSTR ptr;
259
260 static const WCHAR query[] =
261 {'S','E','L','E','C','T',' ','*',' ', 'F','R','O','M',' ',
262 '`','M','e','d','i','a','`',' ','W','H','E','R','E',' ',
263 '`','D','i','s','k','I','d','`',' ','=',' ','%','i',0};
264
265 row = MSI_QueryGetRecord(package->db, query, mi->disk_id);
266 if (!row)
267 {
268 TRACE("Unable to query row\n");
269 return ERROR_FUNCTION_FAILED;
270 }
271
272 mi->disk_prompt = strdupW(MSI_RecordGetString(row, 3));
273 mi->cabinet = strdupW(MSI_RecordGetString(row, 4));
274 mi->volume_label = strdupW(MSI_RecordGetString(row, 5));
275
276 if (!mi->first_volume)
277 mi->first_volume = strdupW(mi->volume_label);
278
279 ptr = strrchrW(mi->source, '\\') + 1;
280 lstrcpyW(ptr, mi->cabinet);
281 msiobj_release(&row->hdr);
282
283 return ERROR_SUCCESS;
284 }
285
286 static INT_PTR cabinet_notify(FDINOTIFICATIONTYPE fdint, PFDINOTIFICATION pfdin)
287 {
288 TRACE("(%d)\n", fdint);
289
290 switch (fdint)
291 {
292 case fdintPARTIAL_FILE:
293 {
294 CabData *data = (CabData *)pfdin->pv;
295 data->mi->is_continuous = FALSE;
296 return 0;
297 }
298 case fdintNEXT_CABINET:
299 {
300 CabData *data = (CabData *)pfdin->pv;
301 MSIMEDIAINFO *mi = data->mi;
302 LPWSTR cab = strdupAtoW(pfdin->psz1);
303 UINT rc;
304
305 msi_free(mi->disk_prompt);
306 msi_free(mi->cabinet);
307 msi_free(mi->volume_label);
308 mi->disk_prompt = NULL;
309 mi->cabinet = NULL;
310 mi->volume_label = NULL;
311
312 mi->disk_id++;
313 mi->is_continuous = TRUE;
314
315 rc = msi_media_get_disk_info(data->package, mi);
316 if (rc != ERROR_SUCCESS)
317 {
318 msi_free(cab);
319 ERR("Failed to get next cabinet information: %d\n", rc);
320 return -1;
321 }
322
323 if (lstrcmpiW(mi->cabinet, cab))
324 {
325 msi_free(cab);
326 ERR("Continuous cabinet does not match the next cabinet in the Media table\n");
327 return -1;
328 }
329
330 msi_free(cab);
331
332 TRACE("Searching for %s\n", debugstr_w(mi->source));
333
334 if (GetFileAttributesW(mi->source) == INVALID_FILE_ATTRIBUTES)
335 rc = msi_change_media(data->package, mi);
336
337 if (rc != ERROR_SUCCESS)
338 return -1;
339
340 return 0;
341 }
342 case fdintCOPY_FILE:
343 {
344 CabData *data = (CabData*) pfdin->pv;
345 HANDLE handle;
346 LPWSTR file;
347 MSIFILE *f;
348 DWORD attrs;
349
350 file = strdupAtoW(pfdin->psz1);
351 f = get_loaded_file(data->package, file);
352 msi_free(file);
353
354 if (!f)
355 {
356 WARN("unknown file in cabinet (%s)\n",debugstr_a(pfdin->psz1));
357 return 0;
358 }
359
360 if (f->state != msifs_missing && f->state != msifs_overwrite)
361 {
362 TRACE("Skipping extraction of %s\n",debugstr_a(pfdin->psz1));
363 return 0;
364 }
365
366 msi_file_update_ui( data->package, f, szInstallFiles );
367
368 TRACE("extracting %s\n", debugstr_w(f->TargetPath) );
369
370 attrs = f->Attributes & (FILE_ATTRIBUTE_READONLY|FILE_ATTRIBUTE_HIDDEN|FILE_ATTRIBUTE_SYSTEM);
371 if (!attrs) attrs = FILE_ATTRIBUTE_NORMAL;
372
373 handle = CreateFileW( f->TargetPath, GENERIC_READ | GENERIC_WRITE, 0,
374 NULL, CREATE_ALWAYS, attrs, NULL );
375 if ( handle == INVALID_HANDLE_VALUE )
376 {
377 if ( GetFileAttributesW( f->TargetPath ) != INVALID_FILE_ATTRIBUTES )
378 f->state = msifs_installed;
379 else
380 ERR("failed to create %s (error %d)\n",
381 debugstr_w( f->TargetPath ), GetLastError() );
382
383 return 0;
384 }
385
386 f->state = msifs_installed;
387 return (INT_PTR) handle;
388 }
389 case fdintCLOSE_FILE_INFO:
390 {
391 CabData *data = (CabData*) pfdin->pv;
392 FILETIME ft;
393 FILETIME ftLocal;
394 HANDLE handle = (HANDLE) pfdin->hf;
395
396 data->mi->is_continuous = FALSE;
397
398 if (!DosDateTimeToFileTime(pfdin->date, pfdin->time, &ft))
399 return -1;
400 if (!LocalFileTimeToFileTime(&ft, &ftLocal))
401 return -1;
402 if (!SetFileTime(handle, &ftLocal, 0, &ftLocal))
403 return -1;
404 CloseHandle(handle);
405 return 1;
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,
418 PFNFDINOTIFY notify, LPVOID data)
419 {
420 LPSTR cabinet, cab_path = NULL;
421 LPWSTR ptr;
422 HFDI hfdi;
423 ERF erf;
424 BOOL ret = FALSE;
425
426 TRACE("Extracting %s\n", debugstr_w(mi->source));
427
428 hfdi = FDICreate(cabinet_alloc, cabinet_free, cabinet_open, cabinet_read,
429 cabinet_write, cabinet_close, cabinet_seek, 0, &erf);
430 if (!hfdi)
431 {
432 ERR("FDICreate failed\n");
433 return FALSE;
434 }
435
436 ptr = strrchrW(mi->source, '\\') + 1;
437 cabinet = strdupWtoA(ptr);
438 if (!cabinet)
439 goto done;
440
441 cab_path = strdupWtoA(mi->source);
442 if (!cab_path)
443 goto done;
444
445 cab_path[ptr - mi->source] = '\0';
446
447 ret = FDICopy(hfdi, cabinet, cab_path, 0, notify, NULL, data);
448 if (!ret)
449 ERR("FDICopy failed\n");
450
451 done:
452 FDIDestroy(hfdi);
453 msi_free(cabinet);
454 msi_free(cab_path);
455
456 if (ret)
457 mi->is_extracted = TRUE;
458
459 return ret;
460 }
461
462 /* compares the version of a file read from the filesystem and
463 * the version specified in the File table
464 */
465 static int msi_compare_file_version(MSIFILE *file)
466 {
467 WCHAR version[MAX_PATH];
468 DWORD size;
469 UINT r;
470
471 size = MAX_PATH;
472 version[0] = '\0';
473 r = MsiGetFileVersionW(file->TargetPath, version, &size, NULL, NULL);
474 if (r != ERROR_SUCCESS)
475 return 0;
476
477 return lstrcmpW(version, file->Version);
478 }
479
480 void msi_free_media_info( MSIMEDIAINFO *mi )
481 {
482 msi_free( mi->disk_prompt );
483 msi_free( mi->cabinet );
484 msi_free( mi->volume_label );
485 msi_free( mi->first_volume );
486 msi_free( mi );
487 }
488
489 UINT msi_load_media_info(MSIPACKAGE *package, MSIFILE *file, MSIMEDIAINFO *mi)
490 {
491 MSIRECORD *row;
492 LPWSTR source_dir;
493 LPWSTR source;
494 DWORD options;
495 UINT r;
496
497 static const WCHAR query[] = {
498 'S','E','L','E','C','T',' ','*',' ', 'F','R','O','M',' ',
499 '`','M','e','d','i','a','`',' ','W','H','E','R','E',' ',
500 '`','L','a','s','t','S','e','q','u','e','n','c','e','`',' ','>','=',
501 ' ','%','i',' ','A','N','D',' ','`','D','i','s','k','I','d','`',' ','>','=',
502 ' ','%','i',' ','O','R','D','E','R',' ','B','Y',' ',
503 '`','D','i','s','k','I','d','`',0
504 };
505
506 row = MSI_QueryGetRecord(package->db, query, file->Sequence, mi->disk_id);
507 if (!row)
508 {
509 TRACE("Unable to query row\n");
510 return ERROR_FUNCTION_FAILED;
511 }
512
513 mi->is_extracted = FALSE;
514 mi->disk_id = MSI_RecordGetInteger(row, 1);
515 mi->last_sequence = MSI_RecordGetInteger(row, 2);
516 msi_free(mi->disk_prompt);
517 mi->disk_prompt = strdupW(MSI_RecordGetString(row, 3));
518 msi_free(mi->cabinet);
519 mi->cabinet = strdupW(MSI_RecordGetString(row, 4));
520 msi_free(mi->volume_label);
521 mi->volume_label = strdupW(MSI_RecordGetString(row, 5));
522 msiobj_release(&row->hdr);
523
524 if (!mi->first_volume)
525 mi->first_volume = strdupW(mi->volume_label);
526
527 source_dir = msi_dup_property(package, cszSourceDir);
528 lstrcpyW(mi->source, source_dir);
529
530 PathStripToRootW(source_dir);
531 mi->type = GetDriveTypeW(source_dir);
532
533 if (file->IsCompressed && mi->cabinet)
534 {
535 if (mi->cabinet[0] == '#')
536 {
537 r = writeout_cabinet_stream(package, &mi->cabinet[1], mi->source);
538 if (r != ERROR_SUCCESS)
539 {
540 ERR("Failed to extract cabinet stream\n");
541 return ERROR_FUNCTION_FAILED;
542 }
543 }
544 else
545 lstrcatW(mi->source, mi->cabinet);
546 }
547
548 options = MSICODE_PRODUCT;
549 if (mi->type == DRIVE_CDROM || mi->type == DRIVE_REMOVABLE)
550 {
551 source = source_dir;
552 options |= MSISOURCETYPE_MEDIA;
553 }
554 else if (package->BaseURL && UrlIsW(package->BaseURL, URLIS_URL))
555 {
556 source = package->BaseURL;
557 options |= MSISOURCETYPE_URL;
558 }
559 else
560 {
561 source = mi->source;
562 options |= MSISOURCETYPE_NETWORK;
563 }
564
565 msi_package_add_media_disk(package, package->Context,
566 MSICODE_PRODUCT, mi->disk_id,
567 mi->volume_label, mi->disk_prompt);
568
569 msi_package_add_info(package, package->Context,
570 options, INSTALLPROPERTY_LASTUSEDSOURCEW, source);
571
572 msi_free(source_dir);
573 return ERROR_SUCCESS;
574 }
575
576 /* FIXME: search NETWORK and URL sources as well */
577 UINT find_published_source(MSIPACKAGE *package, MSIMEDIAINFO *mi)
578 {
579 WCHAR source[MAX_PATH];
580 WCHAR volume[MAX_PATH];
581 WCHAR prompt[MAX_PATH];
582 DWORD volumesz, promptsz;
583 DWORD index, size, id;
584 UINT r;
585
586 size = MAX_PATH;
587 r = MsiSourceListGetInfoW(package->ProductCode, NULL,
588 package->Context, MSICODE_PRODUCT,
589 INSTALLPROPERTY_LASTUSEDSOURCEW, source, &size);
590 if (r != ERROR_SUCCESS)
591 return r;
592
593 index = 0;
594 volumesz = MAX_PATH;
595 promptsz = MAX_PATH;
596 while (MsiSourceListEnumMediaDisksW(package->ProductCode, NULL,
597 package->Context,
598 MSICODE_PRODUCT, index++, &id,
599 volume, &volumesz, prompt, &promptsz) == ERROR_SUCCESS)
600 {
601 mi->disk_id = id;
602 mi->volume_label = msi_realloc(mi->volume_label, ++volumesz * sizeof(WCHAR));
603 lstrcpyW(mi->volume_label, volume);
604 mi->disk_prompt = msi_realloc(mi->disk_prompt, ++promptsz * sizeof(WCHAR));
605 lstrcpyW(mi->disk_prompt, prompt);
606
607 if (source_matches_volume(mi, source))
608 {
609 /* FIXME: what about SourceDir */
610 lstrcpyW(mi->source, source);
611 lstrcatW(mi->source, mi->cabinet);
612 return ERROR_SUCCESS;
613 }
614 }
615
616 return ERROR_FUNCTION_FAILED;
617 }
618
619 static UINT ready_media(MSIPACKAGE *package, MSIFILE *file, MSIMEDIAINFO *mi)
620 {
621 UINT rc = ERROR_SUCCESS;
622
623 /* media info for continuous cabinet is already loaded */
624 if (mi->is_continuous)
625 return ERROR_SUCCESS;
626
627 rc = msi_load_media_info(package, file, mi);
628 if (rc != ERROR_SUCCESS)
629 {
630 ERR("Unable to load media info\n");
631 return ERROR_FUNCTION_FAILED;
632 }
633
634 /* cabinet is internal, no checks needed */
635 if (!mi->cabinet || mi->cabinet[0] == '#')
636 return ERROR_SUCCESS;
637
638 /* package should be downloaded */
639 if (file->IsCompressed &&
640 GetFileAttributesW(mi->source) == INVALID_FILE_ATTRIBUTES &&
641 package->BaseURL && UrlIsW(package->BaseURL, URLIS_URL))
642 {
643 WCHAR temppath[MAX_PATH];
644
645 msi_download_file(mi->source, temppath);
646 lstrcpyW(mi->source, temppath);
647 return ERROR_SUCCESS;
648 }
649
650 /* check volume matches, change media if not */
651 if (mi->volume_label && mi->disk_id > 1 &&
652 lstrcmpW(mi->first_volume, mi->volume_label))
653 {
654 LPWSTR source = msi_dup_property(package, cszSourceDir);
655 BOOL matches;
656
657 matches = source_matches_volume(mi, source);
658 msi_free(source);
659
660 if ((mi->type == DRIVE_CDROM || mi->type == DRIVE_REMOVABLE) && !matches)
661 {
662 rc = msi_change_media(package, mi);
663 if (rc != ERROR_SUCCESS)
664 return rc;
665 }
666 }
667
668 if (file->IsCompressed &&
669 GetFileAttributesW(mi->source) == INVALID_FILE_ATTRIBUTES)
670 {
671 /* FIXME: this might be done earlier in the install process */
672 rc = find_published_source(package, mi);
673 if (rc != ERROR_SUCCESS)
674 {
675 ERR("Cabinet not found: %s\n", debugstr_w(mi->source));
676 return ERROR_INSTALL_FAILURE;
677 }
678 }
679
680 return ERROR_SUCCESS;
681 }
682
683 static UINT get_file_target(MSIPACKAGE *package, LPCWSTR file_key,
684 MSIFILE** file)
685 {
686 LIST_FOR_EACH_ENTRY( *file, &package->files, MSIFILE, entry )
687 {
688 if (lstrcmpW( file_key, (*file)->File )==0)
689 {
690 if ((*file)->state >= msifs_overwrite)
691 return ERROR_SUCCESS;
692 else
693 return ERROR_FILE_NOT_FOUND;
694 }
695 }
696
697 return ERROR_FUNCTION_FAILED;
698 }
699
700 static void schedule_install_files(MSIPACKAGE *package)
701 {
702 MSIFILE *file;
703
704 LIST_FOR_EACH_ENTRY(file, &package->files, MSIFILE, entry)
705 {
706 if (!ACTION_VerifyComponentForAction(file->Component, INSTALLSTATE_LOCAL))
707 {
708 TRACE("File %s is not scheduled for install\n", debugstr_w(file->File));
709
710 ui_progress(package,2,file->FileSize,0,0);
711 file->state = msifs_skipped;
712 }
713 }
714 }
715
716 static UINT copy_file(MSIFILE *file)
717 {
718 BOOL ret;
719
720 ret = CopyFileW(file->SourcePath, file->TargetPath, FALSE);
721 if (ret)
722 {
723 file->state = msifs_installed;
724 return ERROR_SUCCESS;
725 }
726
727 return GetLastError();
728 }
729
730 static UINT copy_install_file(MSIFILE *file)
731 {
732 UINT gle;
733
734 TRACE("Copying %s to %s\n", debugstr_w(file->SourcePath),
735 debugstr_w(file->TargetPath));
736
737 gle = copy_file(file);
738 if (gle == ERROR_SUCCESS)
739 return gle;
740
741 if (gle == ERROR_ALREADY_EXISTS && file->state == msifs_overwrite)
742 {
743 TRACE("overwriting existing file\n");
744 gle = ERROR_SUCCESS;
745 }
746 else if (gle == ERROR_FILE_NOT_FOUND)
747 {
748 /* FIXME: this needs to be tested, I'm pretty sure it fails */
749 TRACE("Source file not found\n");
750 gle = ERROR_SUCCESS;
751 }
752 else if (gle == ERROR_ACCESS_DENIED)
753 {
754 SetFileAttributesW(file->TargetPath, FILE_ATTRIBUTE_NORMAL);
755
756 gle = copy_file(file);
757 TRACE("Overwriting existing file: %d\n", gle);
758 }
759 else if (!(file->Attributes & msidbFileAttributesVital))
760 {
761 TRACE("Ignoring error for nonvital\n");
762 gle = ERROR_SUCCESS;
763 }
764
765 return gle;
766 }
767
768 static BOOL check_dest_hash_matches(MSIFILE *file)
769 {
770 MSIFILEHASHINFO hash;
771 UINT r;
772
773 if (!file->hash.dwFileHashInfoSize)
774 return FALSE;
775
776 hash.dwFileHashInfoSize = sizeof(MSIFILEHASHINFO);
777 r = MsiGetFileHashW(file->TargetPath, 0, &hash);
778 if (r != ERROR_SUCCESS)
779 return FALSE;
780
781 return !memcmp(&hash, &file->hash, sizeof(MSIFILEHASHINFO));
782 }
783
784 /*
785 * ACTION_InstallFiles()
786 *
787 * For efficiency, this is done in two passes:
788 * 1) Correct all the TargetPaths and determine what files are to be installed.
789 * 2) Extract Cabinets and copy files.
790 */
791 UINT ACTION_InstallFiles(MSIPACKAGE *package)
792 {
793 MSIMEDIAINFO *mi;
794 UINT rc = ERROR_SUCCESS;
795 MSIFILE *file;
796
797 /* increment progress bar each time action data is sent */
798 ui_progress(package,1,1,0,0);
799
800 schedule_install_files(package);
801
802 /*
803 * Despite MSDN specifying that the CreateFolders action
804 * should be called before InstallFiles, some installers don't
805 * do that, and they seem to work correctly. We need to create
806 * directories here to make sure that the files can be copied.
807 */
808 msi_create_component_directories( package );
809
810 mi = msi_alloc_zero( sizeof(MSIMEDIAINFO) );
811
812 LIST_FOR_EACH_ENTRY( file, &package->files, MSIFILE, entry )
813 {
814 if (file->state != msifs_missing && !mi->is_continuous && file->state != msifs_overwrite)
815 continue;
816
817 if (check_dest_hash_matches(file))
818 {
819 TRACE("File hashes match, not overwriting\n");
820 continue;
821 }
822
823 if (MsiGetFileVersionW(file->TargetPath, NULL, NULL, NULL, NULL) == ERROR_SUCCESS &&
824 msi_compare_file_version(file) >= 0)
825 {
826 TRACE("Destination file version greater, not overwriting\n");
827 continue;
828 }
829
830 if (file->Sequence > mi->last_sequence || mi->is_continuous ||
831 (file->IsCompressed && !mi->is_extracted))
832 {
833 CabData data;
834
835 rc = ready_media(package, file, mi);
836 if (rc != ERROR_SUCCESS)
837 {
838 ERR("Failed to ready media\n");
839 break;
840 }
841
842 data.mi = mi;
843 data.package = package;
844
845 if (file->IsCompressed &&
846 !msi_cabextract(package, mi, cabinet_notify, &data))
847 {
848 ERR("Failed to extract cabinet: %s\n", debugstr_w(mi->cabinet));
849 rc = ERROR_FUNCTION_FAILED;
850 break;
851 }
852 }
853
854 if (!file->IsCompressed)
855 {
856 TRACE("file paths %s to %s\n", debugstr_w(file->SourcePath),
857 debugstr_w(file->TargetPath));
858
859 msi_file_update_ui(package, file, szInstallFiles);
860 rc = copy_install_file(file);
861 if (rc != ERROR_SUCCESS)
862 {
863 ERR("Failed to copy %s to %s (%d)\n", debugstr_w(file->SourcePath),
864 debugstr_w(file->TargetPath), rc);
865 rc = ERROR_INSTALL_FAILURE;
866 break;
867 }
868 }
869 else if (file->state != msifs_installed)
870 {
871 ERR("compressed file wasn't extracted (%s)\n", debugstr_w(file->TargetPath));
872 rc = ERROR_INSTALL_FAILURE;
873 break;
874 }
875 }
876
877 msi_free_media_info( mi );
878 return rc;
879 }
880
881 static UINT ITERATE_DuplicateFiles(MSIRECORD *row, LPVOID param)
882 {
883 MSIPACKAGE *package = (MSIPACKAGE*)param;
884 WCHAR dest_name[0x100];
885 LPWSTR dest_path, dest;
886 LPCWSTR file_key, component;
887 DWORD sz;
888 DWORD rc;
889 MSICOMPONENT *comp;
890 MSIFILE *file;
891
892 component = MSI_RecordGetString(row,2);
893 comp = get_loaded_component(package,component);
894
895 if (!ACTION_VerifyComponentForAction( comp, INSTALLSTATE_LOCAL ))
896 {
897 TRACE("Skipping copy due to disabled component %s\n",
898 debugstr_w(component));
899
900 /* the action taken was the same as the current install state */
901 comp->Action = comp->Installed;
902
903 return ERROR_SUCCESS;
904 }
905
906 comp->Action = INSTALLSTATE_LOCAL;
907
908 file_key = MSI_RecordGetString(row,3);
909 if (!file_key)
910 {
911 ERR("Unable to get file key\n");
912 return ERROR_FUNCTION_FAILED;
913 }
914
915 rc = get_file_target(package,file_key,&file);
916
917 if (rc != ERROR_SUCCESS)
918 {
919 ERR("Original file unknown %s\n",debugstr_w(file_key));
920 return ERROR_SUCCESS;
921 }
922
923 if (MSI_RecordIsNull(row,4))
924 strcpyW(dest_name,strrchrW(file->TargetPath,'\\')+1);
925 else
926 {
927 sz=0x100;
928 MSI_RecordGetStringW(row,4,dest_name,&sz);
929 reduce_to_longfilename(dest_name);
930 }
931
932 if (MSI_RecordIsNull(row,5))
933 {
934 LPWSTR p;
935 dest_path = strdupW(file->TargetPath);
936 p = strrchrW(dest_path,'\\');
937 if (p)
938 *p=0;
939 }
940 else
941 {
942 LPCWSTR destkey;
943 destkey = MSI_RecordGetString(row,5);
944 dest_path = resolve_folder(package, destkey, FALSE, FALSE, TRUE, NULL);
945 if (!dest_path)
946 {
947 /* try a Property */
948 dest_path = msi_dup_property( package, destkey );
949 if (!dest_path)
950 {
951 FIXME("Unable to get destination folder, try AppSearch properties\n");
952 return ERROR_SUCCESS;
953 }
954 }
955 }
956
957 dest = build_directory_name(2, dest_path, dest_name);
958 create_full_pathW(dest_path);
959
960 TRACE("Duplicating file %s to %s\n",debugstr_w(file->TargetPath),
961 debugstr_w(dest));
962
963 if (strcmpW(file->TargetPath,dest))
964 rc = !CopyFileW(file->TargetPath,dest,TRUE);
965 else
966 rc = ERROR_SUCCESS;
967
968 if (rc != ERROR_SUCCESS)
969 ERR("Failed to copy file %s -> %s, last error %d\n",
970 debugstr_w(file->TargetPath), debugstr_w(dest_path), GetLastError());
971
972 FIXME("We should track these duplicate files as well\n");
973
974 msi_free(dest_path);
975 msi_free(dest);
976
977 msi_file_update_ui(package, file, szDuplicateFiles);
978
979 return ERROR_SUCCESS;
980 }
981
982 UINT ACTION_DuplicateFiles(MSIPACKAGE *package)
983 {
984 UINT rc;
985 MSIQUERY * view;
986 static const WCHAR ExecSeqQuery[] =
987 {'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
988 '`','D','u','p','l','i','c','a','t','e','F','i','l','e','`',0};
989
990 rc = MSI_DatabaseOpenViewW(package->db, ExecSeqQuery, &view);
991 if (rc != ERROR_SUCCESS)
992 return ERROR_SUCCESS;
993
994 rc = MSI_IterateRecords(view, NULL, ITERATE_DuplicateFiles, package);
995 msiobj_release(&view->hdr);
996
997 return rc;
998 }
999
1000 UINT ACTION_RemoveFiles( MSIPACKAGE *package )
1001 {
1002 MSIFILE *file;
1003
1004 LIST_FOR_EACH_ENTRY( file, &package->files, MSIFILE, entry )
1005 {
1006 MSIRECORD *uirow;
1007 LPWSTR uipath, p;
1008
1009 if ( !file->Component )
1010 continue;
1011 if ( file->Component->Installed == INSTALLSTATE_LOCAL )
1012 continue;
1013
1014 if ( file->state == msifs_installed )
1015 ERR("removing installed file %s\n", debugstr_w(file->TargetPath));
1016
1017 if ( file->state != msifs_present )
1018 continue;
1019
1020 /* only remove a file if the version to be installed
1021 * is strictly newer than the old file
1022 */
1023 if ( msi_compare_file_version( file ) >= 0 )
1024 continue;
1025
1026 TRACE("removing %s\n", debugstr_w(file->File) );
1027 if ( !DeleteFileW( file->TargetPath ) )
1028 ERR("failed to delete %s\n", debugstr_w(file->TargetPath) );
1029 file->state = msifs_missing;
1030
1031 /* the UI chunk */
1032 uirow = MSI_CreateRecord( 9 );
1033 MSI_RecordSetStringW( uirow, 1, file->FileName );
1034 uipath = strdupW( file->TargetPath );
1035 p = strrchrW(uipath,'\\');
1036 if (p)
1037 p[1]=0;
1038 MSI_RecordSetStringW( uirow, 9, uipath);
1039 ui_actiondata( package, szRemoveFiles, uirow);
1040 msiobj_release( &uirow->hdr );
1041 msi_free( uipath );
1042 /* FIXME: call ui_progress here? */
1043 }
1044
1045 return ERROR_SUCCESS;
1046 }