push 014043c4937c940c54cd1214c96e33a3b3c8cf7d
[wine/hacks.git] / dlls / msi / files.c
blob33a3a5a8a108886d4a40f9e25e7409fb37b9b2c4
1 /*
2 * Implementation of the Microsoft Installer (msi.dll)
4 * Copyright 2005 Aric Stewart for CodeWeavers
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
23 * Actions dealing with files These are
25 * InstallFiles
26 * DuplicateFiles
27 * MoveFiles (TODO)
28 * PatchFiles (TODO)
29 * RemoveDuplicateFiles(TODO)
30 * RemoveFiles(TODO)
33 #include <stdarg.h>
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"
49 WINE_DEFAULT_DEBUG_CHANNEL(msi);
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[];
58 static const WCHAR cszTempFolder[]= {'T','e','m','p','F','o','l','d','e','r',0};
60 struct media_info {
61 UINT disk_id;
62 UINT type;
63 UINT last_sequence;
64 LPWSTR disk_prompt;
65 LPWSTR cabinet;
66 LPWSTR first_volume;
67 LPWSTR volume_label;
68 BOOL is_continuous;
69 BOOL is_extracted;
70 WCHAR source[MAX_PATH];
73 static BOOL source_matches_volume(struct media_info *mi, LPWSTR source_root)
75 WCHAR volume_name[MAX_PATH + 1];
77 if (!GetVolumeInformationW(source_root, volume_name, MAX_PATH + 1,
78 NULL, NULL, NULL, NULL, 0))
80 ERR("Failed to get volume information\n");
81 return FALSE;
84 return !lstrcmpW(mi->volume_label, volume_name);
87 static UINT msi_change_media( MSIPACKAGE *package, struct media_info *mi )
89 LPSTR msg;
90 LPWSTR error, error_dialog;
91 LPWSTR source_dir;
92 UINT r = ERROR_SUCCESS;
94 static const WCHAR szUILevel[] = {'U','I','L','e','v','e','l',0};
95 static const WCHAR error_prop[] = {'E','r','r','o','r','D','i','a','l','o','g',0};
97 if ( (msi_get_property_int(package, szUILevel, 0) & INSTALLUILEVEL_MASK) == INSTALLUILEVEL_NONE && !gUIHandlerA )
98 return ERROR_SUCCESS;
100 error = generate_error_string( package, 1302, 1, mi->disk_prompt );
101 error_dialog = msi_dup_property( package, error_prop );
102 source_dir = msi_dup_property( package, cszSourceDir );
103 PathStripToRootW(source_dir);
105 while ( r == ERROR_SUCCESS &&
106 !source_matches_volume(mi, source_dir) )
108 r = msi_spawn_error_dialog( package, error_dialog, error );
110 if (gUIHandlerA)
112 msg = strdupWtoA( error );
113 gUIHandlerA( gUIContext, MB_RETRYCANCEL | INSTALLMESSAGE_ERROR, msg );
114 msi_free(msg);
118 msi_free( error );
119 msi_free( error_dialog );
120 msi_free( source_dir );
122 return r;
126 * This is a helper function for handling embedded cabinet media
128 static UINT writeout_cabinet_stream(MSIPACKAGE *package, LPCWSTR stream_name,
129 WCHAR* source)
131 UINT rc;
132 USHORT* data;
133 UINT size;
134 DWORD write;
135 HANDLE the_file;
136 WCHAR tmp[MAX_PATH];
138 rc = read_raw_stream_data(package->db,stream_name,&data,&size);
139 if (rc != ERROR_SUCCESS)
140 return rc;
142 write = MAX_PATH;
143 if (MSI_GetPropertyW(package, cszTempFolder, tmp, &write))
144 GetTempPathW(MAX_PATH,tmp);
146 GetTempFileNameW(tmp,stream_name,0,source);
148 track_tempfile(package, source);
149 the_file = CreateFileW(source, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS,
150 FILE_ATTRIBUTE_NORMAL, NULL);
152 if (the_file == INVALID_HANDLE_VALUE)
154 ERR("Unable to create file %s\n",debugstr_w(source));
155 rc = ERROR_FUNCTION_FAILED;
156 goto end;
159 WriteFile(the_file,data,size,&write,NULL);
160 CloseHandle(the_file);
161 TRACE("wrote %i bytes to %s\n",write,debugstr_w(source));
162 end:
163 msi_free(data);
164 return rc;
168 /* Support functions for FDI functions */
169 typedef struct
171 MSIPACKAGE* package;
172 struct media_info *mi;
173 } CabData;
175 static void * cabinet_alloc(ULONG cb)
177 return msi_alloc(cb);
180 static void cabinet_free(void *pv)
182 msi_free(pv);
185 static INT_PTR cabinet_open(char *pszFile, int oflag, int pmode)
187 HANDLE handle;
188 DWORD dwAccess = 0;
189 DWORD dwShareMode = 0;
190 DWORD dwCreateDisposition = OPEN_EXISTING;
191 switch (oflag & _O_ACCMODE)
193 case _O_RDONLY:
194 dwAccess = GENERIC_READ;
195 dwShareMode = FILE_SHARE_READ | FILE_SHARE_DELETE;
196 break;
197 case _O_WRONLY:
198 dwAccess = GENERIC_WRITE;
199 dwShareMode = FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE;
200 break;
201 case _O_RDWR:
202 dwAccess = GENERIC_READ | GENERIC_WRITE;
203 dwShareMode = FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE;
204 break;
206 if ((oflag & (_O_CREAT | _O_EXCL)) == (_O_CREAT | _O_EXCL))
207 dwCreateDisposition = CREATE_NEW;
208 else if (oflag & _O_CREAT)
209 dwCreateDisposition = CREATE_ALWAYS;
210 handle = CreateFileA( pszFile, dwAccess, dwShareMode, NULL,
211 dwCreateDisposition, 0, NULL );
212 if (handle == INVALID_HANDLE_VALUE)
213 return 0;
214 return (INT_PTR) handle;
217 static UINT cabinet_read(INT_PTR hf, void *pv, UINT cb)
219 HANDLE handle = (HANDLE) hf;
220 DWORD dwRead;
221 if (ReadFile(handle, pv, cb, &dwRead, NULL))
222 return dwRead;
223 return 0;
226 static UINT cabinet_write(INT_PTR hf, void *pv, UINT cb)
228 HANDLE handle = (HANDLE) hf;
229 DWORD dwWritten;
230 if (WriteFile(handle, pv, cb, &dwWritten, NULL))
231 return dwWritten;
232 return 0;
235 static int cabinet_close(INT_PTR hf)
237 HANDLE handle = (HANDLE) hf;
238 return CloseHandle(handle) ? 0 : -1;
241 static long cabinet_seek(INT_PTR hf, long dist, int seektype)
243 HANDLE handle = (HANDLE) hf;
244 /* flags are compatible and so are passed straight through */
245 return SetFilePointer(handle, dist, NULL, seektype);
248 static void msi_file_update_ui( MSIPACKAGE *package, MSIFILE *f, const WCHAR *action )
250 MSIRECORD *uirow;
251 LPWSTR uipath, p;
253 /* the UI chunk */
254 uirow = MSI_CreateRecord( 9 );
255 MSI_RecordSetStringW( uirow, 1, f->FileName );
256 uipath = strdupW( f->TargetPath );
257 p = strrchrW(uipath,'\\');
258 if (p)
259 p[1]=0;
260 MSI_RecordSetStringW( uirow, 9, uipath);
261 MSI_RecordSetInteger( uirow, 6, f->FileSize );
262 ui_actiondata( package, action, uirow);
263 msiobj_release( &uirow->hdr );
264 msi_free( uipath );
265 ui_progress( package, 2, f->FileSize, 0, 0);
268 static UINT msi_media_get_disk_info( MSIPACKAGE *package, struct media_info *mi )
270 MSIRECORD *row;
271 LPWSTR ptr;
273 static const WCHAR query[] =
274 {'S','E','L','E','C','T',' ','*',' ', 'F','R','O','M',' ',
275 '`','M','e','d','i','a','`',' ','W','H','E','R','E',' ',
276 '`','D','i','s','k','I','d','`',' ','=',' ','%','i',0};
278 row = MSI_QueryGetRecord(package->db, query, mi->disk_id);
279 if (!row)
281 TRACE("Unable to query row\n");
282 return ERROR_FUNCTION_FAILED;
285 mi->disk_prompt = strdupW(MSI_RecordGetString(row, 3));
286 mi->cabinet = strdupW(MSI_RecordGetString(row, 4));
287 mi->volume_label = strdupW(MSI_RecordGetString(row, 5));
289 if (!mi->first_volume)
290 mi->first_volume = strdupW(mi->volume_label);
292 ptr = strrchrW(mi->source, '\\') + 1;
293 lstrcpyW(ptr, mi->cabinet);
294 msiobj_release(&row->hdr);
296 return ERROR_SUCCESS;
299 static INT_PTR cabinet_notify(FDINOTIFICATIONTYPE fdint, PFDINOTIFICATION pfdin)
301 TRACE("(%d)\n", fdint);
303 switch (fdint)
305 case fdintPARTIAL_FILE:
307 CabData *data = (CabData *)pfdin->pv;
308 data->mi->is_continuous = FALSE;
309 return 0;
311 case fdintNEXT_CABINET:
313 CabData *data = (CabData *)pfdin->pv;
314 struct media_info *mi = data->mi;
315 LPWSTR cab = strdupAtoW(pfdin->psz1);
316 UINT rc;
318 msi_free(mi->disk_prompt);
319 msi_free(mi->cabinet);
320 msi_free(mi->volume_label);
321 mi->disk_prompt = NULL;
322 mi->cabinet = NULL;
323 mi->volume_label = NULL;
325 mi->disk_id++;
326 mi->is_continuous = TRUE;
328 rc = msi_media_get_disk_info(data->package, mi);
329 if (rc != ERROR_SUCCESS)
331 msi_free(cab);
332 ERR("Failed to get next cabinet information: %d\n", rc);
333 return -1;
336 if (lstrcmpiW(mi->cabinet, cab))
338 msi_free(cab);
339 ERR("Continuous cabinet does not match the next cabinet in the Media table\n");
340 return -1;
343 msi_free(cab);
345 TRACE("Searching for %s\n", debugstr_w(mi->source));
347 if (GetFileAttributesW(mi->source) == INVALID_FILE_ATTRIBUTES)
348 rc = msi_change_media(data->package, mi);
350 if (rc != ERROR_SUCCESS)
351 return -1;
353 return 0;
355 case fdintCOPY_FILE:
357 CabData *data = (CabData*) pfdin->pv;
358 HANDLE handle;
359 LPWSTR file;
360 MSIFILE *f;
361 DWORD attrs;
363 file = strdupAtoW(pfdin->psz1);
364 f = get_loaded_file(data->package, file);
365 msi_free(file);
367 if (!f)
369 WARN("unknown file in cabinet (%s)\n",debugstr_a(pfdin->psz1));
370 return 0;
373 if (f->state != msifs_missing && f->state != msifs_overwrite)
375 TRACE("Skipping extraction of %s\n",debugstr_a(pfdin->psz1));
376 return 0;
379 msi_file_update_ui( data->package, f, szInstallFiles );
381 TRACE("extracting %s\n", debugstr_w(f->TargetPath) );
383 attrs = f->Attributes & (FILE_ATTRIBUTE_READONLY|FILE_ATTRIBUTE_HIDDEN|FILE_ATTRIBUTE_SYSTEM);
384 if (!attrs) attrs = FILE_ATTRIBUTE_NORMAL;
386 handle = CreateFileW( f->TargetPath, GENERIC_READ | GENERIC_WRITE, 0,
387 NULL, CREATE_ALWAYS, attrs, NULL );
388 if ( handle == INVALID_HANDLE_VALUE )
390 if ( GetFileAttributesW( f->TargetPath ) != INVALID_FILE_ATTRIBUTES )
391 f->state = msifs_installed;
392 else
393 ERR("failed to create %s (error %d)\n",
394 debugstr_w( f->TargetPath ), GetLastError() );
396 return 0;
399 f->state = msifs_installed;
400 return (INT_PTR) handle;
402 case fdintCLOSE_FILE_INFO:
404 FILETIME ft;
405 FILETIME ftLocal;
406 HANDLE handle = (HANDLE) pfdin->hf;
408 if (!DosDateTimeToFileTime(pfdin->date, pfdin->time, &ft))
409 return -1;
410 if (!LocalFileTimeToFileTime(&ft, &ftLocal))
411 return -1;
412 if (!SetFileTime(handle, &ftLocal, 0, &ftLocal))
413 return -1;
414 CloseHandle(handle);
415 return 1;
417 default:
418 return 0;
422 /***********************************************************************
423 * extract_cabinet_file
425 * Extract files from a cab file.
427 static BOOL extract_cabinet_file(MSIPACKAGE* package, struct media_info *mi)
429 LPSTR cabinet, cab_path = NULL;
430 LPWSTR ptr;
431 HFDI hfdi;
432 ERF erf;
433 BOOL ret = FALSE;
434 CabData data;
436 TRACE("Extracting %s\n", debugstr_w(mi->source));
438 hfdi = FDICreate(cabinet_alloc, cabinet_free, cabinet_open, cabinet_read,
439 cabinet_write, cabinet_close, cabinet_seek, 0, &erf);
440 if (!hfdi)
442 ERR("FDICreate failed\n");
443 return FALSE;
446 ptr = strrchrW(mi->source, '\\') + 1;
447 cabinet = strdupWtoA(ptr);
448 if (!cabinet)
449 goto done;
451 cab_path = strdupWtoA(mi->source);
452 if (!cab_path)
453 goto done;
455 cab_path[ptr - mi->source] = '\0';
457 data.package = package;
458 data.mi = mi;
460 ret = FDICopy(hfdi, cabinet, cab_path, 0, cabinet_notify, NULL, &data);
461 if (!ret)
462 ERR("FDICopy failed\n");
464 done:
465 FDIDestroy(hfdi);
466 msi_free(cabinet);
467 msi_free(cab_path);
469 if (ret)
470 mi->is_extracted = TRUE;
472 return ret;
475 static VOID set_file_source(MSIPACKAGE* package, MSIFILE* file, LPCWSTR path)
477 if (!file->IsCompressed)
479 LPWSTR p, path;
480 p = resolve_folder(package, file->Component->Directory, TRUE, FALSE, TRUE, NULL);
481 path = build_directory_name(2, p, file->ShortName);
482 if (file->LongName &&
483 INVALID_FILE_ATTRIBUTES == GetFileAttributesW( path ))
485 msi_free(path);
486 path = build_directory_name(2, p, file->LongName);
488 file->SourcePath = path;
489 msi_free(p);
491 else
492 file->SourcePath = build_directory_name(2, path, file->File);
495 static void free_media_info( struct media_info *mi )
497 msi_free( mi->disk_prompt );
498 msi_free( mi->cabinet );
499 msi_free( mi->volume_label );
500 msi_free( mi->first_volume );
501 msi_free( mi );
504 static UINT load_media_info(MSIPACKAGE *package, MSIFILE *file, struct media_info *mi)
506 MSIRECORD *row;
507 LPWSTR source_dir;
508 LPWSTR source;
509 DWORD options;
510 UINT r;
512 static const WCHAR query[] = {
513 'S','E','L','E','C','T',' ','*',' ', 'F','R','O','M',' ',
514 '`','M','e','d','i','a','`',' ','W','H','E','R','E',' ',
515 '`','L','a','s','t','S','e','q','u','e','n','c','e','`',' ','>','=',
516 ' ','%','i',' ','A','N','D',' ','`','D','i','s','k','I','d','`',' ','>','=',
517 ' ','%','i',' ','O','R','D','E','R',' ','B','Y',' ',
518 '`','D','i','s','k','I','d','`',0
521 row = MSI_QueryGetRecord(package->db, query, file->Sequence, mi->disk_id);
522 if (!row)
524 TRACE("Unable to query row\n");
525 return ERROR_FUNCTION_FAILED;
528 mi->is_extracted = FALSE;
529 mi->disk_id = MSI_RecordGetInteger(row, 1);
530 mi->last_sequence = MSI_RecordGetInteger(row, 2);
531 msi_free(mi->disk_prompt);
532 mi->disk_prompt = strdupW(MSI_RecordGetString(row, 3));
533 msi_free(mi->cabinet);
534 mi->cabinet = strdupW(MSI_RecordGetString(row, 4));
535 msi_free(mi->volume_label);
536 mi->volume_label = strdupW(MSI_RecordGetString(row, 5));
537 msiobj_release(&row->hdr);
539 if (!mi->first_volume)
540 mi->first_volume = strdupW(mi->volume_label);
542 source_dir = msi_dup_property(package, cszSourceDir);
543 lstrcpyW(mi->source, source_dir);
545 PathStripToRootW(source_dir);
546 mi->type = GetDriveTypeW(source_dir);
548 if (file->IsCompressed && mi->cabinet)
550 if (mi->cabinet[0] == '#')
552 r = writeout_cabinet_stream(package, &mi->cabinet[1], mi->source);
553 if (r != ERROR_SUCCESS)
555 ERR("Failed to extract cabinet stream\n");
556 return ERROR_FUNCTION_FAILED;
559 else
560 lstrcatW(mi->source, mi->cabinet);
563 options = MSICODE_PRODUCT;
564 if (mi->type == DRIVE_CDROM || mi->type == DRIVE_REMOVABLE)
566 source = source_dir;
567 options |= MSISOURCETYPE_MEDIA;
569 else if (package->BaseURL && UrlIsW(package->BaseURL, URLIS_URL))
571 source = package->BaseURL;
572 options |= MSISOURCETYPE_URL;
574 else
576 source = mi->source;
577 options |= MSISOURCETYPE_NETWORK;
580 if (mi->type == DRIVE_CDROM || mi->type == DRIVE_REMOVABLE)
581 msi_package_add_media_disk(package, MSIINSTALLCONTEXT_USERUNMANAGED,
582 MSICODE_PRODUCT, mi->disk_id,
583 mi->volume_label, mi->disk_prompt);
585 msi_package_add_info(package, MSIINSTALLCONTEXT_USERUNMANAGED,
586 options, INSTALLPROPERTY_LASTUSEDSOURCEW, source);
588 msi_free(source_dir);
589 return ERROR_SUCCESS;
592 /* FIXME: search NETWORK and URL sources as well */
593 static UINT find_published_source(MSIPACKAGE *package, struct media_info *mi)
595 WCHAR source[MAX_PATH];
596 WCHAR volume[MAX_PATH];
597 WCHAR prompt[MAX_PATH];
598 DWORD volumesz, promptsz;
599 DWORD index, size;
600 WORD id;
601 UINT r;
603 r = MsiSourceListGetInfoW(package->ProductCode, NULL,
604 MSIINSTALLCONTEXT_USERUNMANAGED, MSICODE_PRODUCT,
605 INSTALLPROPERTY_LASTUSEDSOURCEW, source, &size);
606 if (r != ERROR_SUCCESS)
607 return r;
609 index = 0;
610 volumesz = MAX_PATH;
611 promptsz = MAX_PATH;
612 while (MsiSourceListEnumMediaDisksW(package->ProductCode, NULL,
613 MSIINSTALLCONTEXT_USERUNMANAGED,
614 MSICODE_PRODUCT, index++, &id,
615 volume, &volumesz, prompt, &promptsz) == ERROR_SUCCESS)
617 mi->disk_id = id;
618 mi->volume_label = msi_realloc(mi->volume_label, ++volumesz * sizeof(WCHAR));
619 lstrcpyW(mi->volume_label, volume);
620 mi->disk_prompt = msi_realloc(mi->disk_prompt, ++promptsz * sizeof(WCHAR));
621 lstrcpyW(mi->disk_prompt, prompt);
623 if (source_matches_volume(mi, source))
625 /* FIXME: what about SourceDir */
626 lstrcpyW(mi->source, source);
627 lstrcatW(mi->source, mi->cabinet);
628 return ERROR_SUCCESS;
632 return ERROR_FUNCTION_FAILED;
635 static UINT ready_media(MSIPACKAGE *package, MSIFILE *file, struct media_info *mi)
637 UINT rc = ERROR_SUCCESS;
639 /* media info for continuous cabinet is already loaded */
640 if (mi->is_continuous)
641 return ERROR_SUCCESS;
643 rc = load_media_info(package, file, mi);
644 if (rc != ERROR_SUCCESS)
646 ERR("Unable to load media info\n");
647 return ERROR_FUNCTION_FAILED;
650 /* cabinet is internal, no checks needed */
651 if (!mi->cabinet || mi->cabinet[0] == '#')
652 return ERROR_SUCCESS;
654 /* package should be downloaded */
655 if (file->IsCompressed &&
656 GetFileAttributesW(mi->source) == INVALID_FILE_ATTRIBUTES &&
657 package->BaseURL && UrlIsW(package->BaseURL, URLIS_URL))
659 WCHAR temppath[MAX_PATH];
661 msi_download_file(mi->source, temppath);
662 lstrcpyW(mi->source, temppath);
663 return ERROR_SUCCESS;
666 /* check volume matches, change media if not */
667 if (mi->volume_label && mi->disk_id > 1 &&
668 lstrcmpW(mi->first_volume, mi->volume_label))
670 LPWSTR source = msi_dup_property(package, cszSourceDir);
671 BOOL matches;
673 matches = source_matches_volume(mi, source);
674 msi_free(source);
676 if ((mi->type == DRIVE_CDROM || mi->type == DRIVE_REMOVABLE) && !matches)
678 rc = msi_change_media(package, mi);
679 if (rc != ERROR_SUCCESS)
680 return rc;
684 if (file->IsCompressed &&
685 GetFileAttributesW(mi->source) == INVALID_FILE_ATTRIBUTES)
687 /* FIXME: this might be done earlier in the install process */
688 rc = find_published_source(package, mi);
689 if (rc != ERROR_SUCCESS)
691 ERR("Cabinet not found: %s\n", debugstr_w(mi->source));
692 return ERROR_INSTALL_FAILURE;
696 return ERROR_SUCCESS;
699 static UINT get_file_target(MSIPACKAGE *package, LPCWSTR file_key,
700 MSIFILE** file)
702 LIST_FOR_EACH_ENTRY( *file, &package->files, MSIFILE, entry )
704 if (lstrcmpW( file_key, (*file)->File )==0)
706 if ((*file)->state >= msifs_overwrite)
707 return ERROR_SUCCESS;
708 else
709 return ERROR_FILE_NOT_FOUND;
713 return ERROR_FUNCTION_FAILED;
716 static void schedule_install_files(MSIPACKAGE *package)
718 MSIFILE *file;
720 LIST_FOR_EACH_ENTRY(file, &package->files, MSIFILE, entry)
722 if (!ACTION_VerifyComponentForAction(file->Component, INSTALLSTATE_LOCAL))
724 TRACE("File %s is not scheduled for install\n", debugstr_w(file->File));
726 ui_progress(package,2,file->FileSize,0,0);
727 file->state = msifs_skipped;
732 static UINT copy_file(MSIFILE *file)
734 BOOL ret;
736 ret = CopyFileW(file->SourcePath, file->TargetPath, FALSE);
737 if (ret)
739 file->state = msifs_installed;
740 return ERROR_SUCCESS;
743 return GetLastError();
746 static UINT copy_install_file(MSIFILE *file)
748 UINT gle;
750 TRACE("Copying %s to %s\n", debugstr_w(file->SourcePath),
751 debugstr_w(file->TargetPath));
753 gle = copy_file(file);
754 if (gle == ERROR_SUCCESS)
755 return gle;
757 if (gle == ERROR_ALREADY_EXISTS && file->state == msifs_overwrite)
759 TRACE("overwriting existing file\n");
760 gle = ERROR_SUCCESS;
762 else if (gle == ERROR_FILE_NOT_FOUND)
764 /* FIXME: this needs to be tested, I'm pretty sure it fails */
765 TRACE("Source file not found\n");
766 gle = ERROR_SUCCESS;
768 else if (gle == ERROR_ACCESS_DENIED)
770 SetFileAttributesW(file->TargetPath, FILE_ATTRIBUTE_NORMAL);
772 gle = copy_file(file);
773 TRACE("Overwriting existing file: %d\n", gle);
775 else if (!(file->Attributes & msidbFileAttributesVital))
777 TRACE("Ignoring error for nonvital\n");
778 gle = ERROR_SUCCESS;
781 return gle;
784 static BOOL check_dest_hash_matches(MSIFILE *file)
786 MSIFILEHASHINFO hash;
787 UINT r;
789 if (!file->hash.dwFileHashInfoSize)
790 return FALSE;
792 hash.dwFileHashInfoSize = sizeof(MSIFILEHASHINFO);
793 r = MsiGetFileHashW(file->TargetPath, 0, &hash);
794 if (r != ERROR_SUCCESS)
795 return FALSE;
797 return !memcmp(&hash, &file->hash, sizeof(MSIFILEHASHINFO));
801 * ACTION_InstallFiles()
803 * For efficiency, this is done in two passes:
804 * 1) Correct all the TargetPaths and determine what files are to be installed.
805 * 2) Extract Cabinets and copy files.
807 UINT ACTION_InstallFiles(MSIPACKAGE *package)
809 struct media_info *mi;
810 UINT rc = ERROR_SUCCESS;
811 MSIFILE *file;
813 /* increment progress bar each time action data is sent */
814 ui_progress(package,1,1,0,0);
816 schedule_install_files(package);
819 * Despite MSDN specifying that the CreateFolders action
820 * should be called before InstallFiles, some installers don't
821 * do that, and they seem to work correctly. We need to create
822 * directories here to make sure that the files can be copied.
824 msi_create_component_directories( package );
826 mi = msi_alloc_zero( sizeof(struct media_info) );
828 LIST_FOR_EACH_ENTRY( file, &package->files, MSIFILE, entry )
830 if (file->state != msifs_missing && !mi->is_continuous && file->state != msifs_overwrite)
831 continue;
833 if (check_dest_hash_matches(file))
835 TRACE("File hashes match, not overwriting\n");
836 continue;
839 if (file->Sequence > mi->last_sequence || mi->is_continuous ||
840 (file->IsCompressed && !mi->is_extracted))
842 rc = ready_media(package, file, mi);
843 if (rc != ERROR_SUCCESS)
845 ERR("Failed to ready media\n");
846 break;
849 if (file->IsCompressed && !extract_cabinet_file(package, mi))
851 ERR("Failed to extract cabinet: %s\n", debugstr_w(mi->cabinet));
852 rc = ERROR_FUNCTION_FAILED;
853 break;
857 set_file_source(package, file, mi->source);
859 TRACE("file paths %s to %s\n",debugstr_w(file->SourcePath),
860 debugstr_w(file->TargetPath));
862 if (!file->IsCompressed)
864 msi_file_update_ui(package, file, szInstallFiles);
865 rc = copy_install_file(file);
866 if (rc != ERROR_SUCCESS)
868 ERR("Failed to copy %s to %s (%d)\n", debugstr_w(file->SourcePath),
869 debugstr_w(file->TargetPath), rc);
870 rc = ERROR_INSTALL_FAILURE;
871 break;
874 else if (file->state != msifs_installed)
876 ERR("compressed file wasn't extracted (%s)\n", debugstr_w(file->TargetPath));
877 rc = ERROR_INSTALL_FAILURE;
878 break;
882 free_media_info( mi );
883 return rc;
886 static UINT ITERATE_DuplicateFiles(MSIRECORD *row, LPVOID param)
888 MSIPACKAGE *package = (MSIPACKAGE*)param;
889 WCHAR dest_name[0x100];
890 LPWSTR dest_path, dest;
891 LPCWSTR file_key, component;
892 DWORD sz;
893 DWORD rc;
894 MSICOMPONENT *comp;
895 MSIFILE *file;
897 component = MSI_RecordGetString(row,2);
898 comp = get_loaded_component(package,component);
900 if (!ACTION_VerifyComponentForAction( comp, INSTALLSTATE_LOCAL ))
902 TRACE("Skipping copy due to disabled component %s\n",
903 debugstr_w(component));
905 /* the action taken was the same as the current install state */
906 comp->Action = comp->Installed;
908 return ERROR_SUCCESS;
911 comp->Action = INSTALLSTATE_LOCAL;
913 file_key = MSI_RecordGetString(row,3);
914 if (!file_key)
916 ERR("Unable to get file key\n");
917 return ERROR_FUNCTION_FAILED;
920 rc = get_file_target(package,file_key,&file);
922 if (rc != ERROR_SUCCESS)
924 ERR("Original file unknown %s\n",debugstr_w(file_key));
925 return ERROR_SUCCESS;
928 if (MSI_RecordIsNull(row,4))
929 strcpyW(dest_name,strrchrW(file->TargetPath,'\\')+1);
930 else
932 sz=0x100;
933 MSI_RecordGetStringW(row,4,dest_name,&sz);
934 reduce_to_longfilename(dest_name);
937 if (MSI_RecordIsNull(row,5))
939 LPWSTR p;
940 dest_path = strdupW(file->TargetPath);
941 p = strrchrW(dest_path,'\\');
942 if (p)
943 *p=0;
945 else
947 LPCWSTR destkey;
948 destkey = MSI_RecordGetString(row,5);
949 dest_path = resolve_folder(package, destkey, FALSE, FALSE, TRUE, NULL);
950 if (!dest_path)
952 /* try a Property */
953 dest_path = msi_dup_property( package, destkey );
954 if (!dest_path)
956 FIXME("Unable to get destination folder, try AppSearch properties\n");
957 return ERROR_SUCCESS;
962 dest = build_directory_name(2, dest_path, dest_name);
963 create_full_pathW(dest_path);
965 TRACE("Duplicating file %s to %s\n",debugstr_w(file->TargetPath),
966 debugstr_w(dest));
968 if (strcmpW(file->TargetPath,dest))
969 rc = !CopyFileW(file->TargetPath,dest,TRUE);
970 else
971 rc = ERROR_SUCCESS;
973 if (rc != ERROR_SUCCESS)
974 ERR("Failed to copy file %s -> %s, last error %d\n",
975 debugstr_w(file->TargetPath), debugstr_w(dest_path), GetLastError());
977 FIXME("We should track these duplicate files as well\n");
979 msi_free(dest_path);
980 msi_free(dest);
982 msi_file_update_ui(package, file, szDuplicateFiles);
984 return ERROR_SUCCESS;
987 UINT ACTION_DuplicateFiles(MSIPACKAGE *package)
989 UINT rc;
990 MSIQUERY * view;
991 static const WCHAR ExecSeqQuery[] =
992 {'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
993 '`','D','u','p','l','i','c','a','t','e','F','i','l','e','`',0};
995 rc = MSI_DatabaseOpenViewW(package->db, ExecSeqQuery, &view);
996 if (rc != ERROR_SUCCESS)
997 return ERROR_SUCCESS;
999 rc = MSI_IterateRecords(view, NULL, ITERATE_DuplicateFiles, package);
1000 msiobj_release(&view->hdr);
1002 return rc;
1005 /* compares the version of a file read from the filesystem and
1006 * the version specified in the File table
1008 static int msi_compare_file_version( MSIFILE *file )
1010 WCHAR version[MAX_PATH];
1011 DWORD size;
1012 UINT r;
1014 size = MAX_PATH;
1015 version[0] = '\0';
1016 r = MsiGetFileVersionW( file->TargetPath, version, &size, NULL, NULL );
1017 if ( r != ERROR_SUCCESS )
1018 return 0;
1020 return lstrcmpW( version, file->Version );
1023 UINT ACTION_RemoveFiles( MSIPACKAGE *package )
1025 MSIFILE *file;
1027 LIST_FOR_EACH_ENTRY( file, &package->files, MSIFILE, entry )
1029 MSIRECORD *uirow;
1030 LPWSTR uipath, p;
1032 if ( !file->Component )
1033 continue;
1034 if ( file->Component->Installed == INSTALLSTATE_LOCAL )
1035 continue;
1037 if ( file->state == msifs_installed )
1038 ERR("removing installed file %s\n", debugstr_w(file->TargetPath));
1040 if ( file->state != msifs_present )
1041 continue;
1043 /* only remove a file if the version to be installed
1044 * is strictly newer than the old file
1046 if ( msi_compare_file_version( file ) >= 0 )
1047 continue;
1049 TRACE("removing %s\n", debugstr_w(file->File) );
1050 if ( !DeleteFileW( file->TargetPath ) )
1051 ERR("failed to delete %s\n", debugstr_w(file->TargetPath) );
1052 file->state = msifs_missing;
1054 /* the UI chunk */
1055 uirow = MSI_CreateRecord( 9 );
1056 MSI_RecordSetStringW( uirow, 1, file->FileName );
1057 uipath = strdupW( file->TargetPath );
1058 p = strrchrW(uipath,'\\');
1059 if (p)
1060 p[1]=0;
1061 MSI_RecordSetStringW( uirow, 9, uipath);
1062 ui_actiondata( package, szRemoveFiles, uirow);
1063 msiobj_release( &uirow->hdr );
1064 msi_free( uipath );
1065 /* FIXME: call ui_progress here? */
1068 return ERROR_SUCCESS;