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
29 * RemoveDuplicateFiles(TODO)
38 #include "wine/debug.h"
42 #include "msvcrt/fcntl.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 static BOOL source_matches_volume(MSIMEDIAINFO *mi, LPWSTR source_root)
62 WCHAR volume_name[MAX_PATH + 1];
64 if (!GetVolumeInformationW(source_root, volume_name, MAX_PATH + 1,
65 NULL, NULL, NULL, NULL, 0))
67 ERR("Failed to get volume information\n");
71 return !lstrcmpW(mi->volume_label, volume_name);
74 static UINT msi_change_media( MSIPACKAGE *package, MSIMEDIAINFO *mi )
77 LPWSTR error, error_dialog;
79 UINT r = ERROR_SUCCESS;
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};
84 if ( (msi_get_property_int(package, szUILevel, 0) & INSTALLUILEVEL_MASK) == INSTALLUILEVEL_NONE && !gUIHandlerA )
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);
92 while ( r == ERROR_SUCCESS &&
93 !source_matches_volume(mi, source_dir) )
95 r = msi_spawn_error_dialog( package, error_dialog, error );
99 msg = strdupWtoA( error );
100 gUIHandlerA( gUIContext, MB_RETRYCANCEL | INSTALLMESSAGE_ERROR, msg );
106 msi_free( error_dialog );
107 msi_free( source_dir );
113 * This is a helper function for handling embedded cabinet media
115 static UINT writeout_cabinet_stream(MSIPACKAGE *package, LPCWSTR stream_name,
125 rc = read_raw_stream_data(package->db,stream_name,&data,&size);
126 if (rc != ERROR_SUCCESS)
130 if (MSI_GetPropertyW(package, cszTempFolder, tmp, &write))
131 GetTempPathW(MAX_PATH,tmp);
133 GetTempFileNameW(tmp,stream_name,0,source);
135 track_tempfile(package, source);
136 the_file = CreateFileW(source, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS,
137 FILE_ATTRIBUTE_NORMAL, NULL);
139 if (the_file == INVALID_HANDLE_VALUE)
141 ERR("Unable to create file %s\n",debugstr_w(source));
142 rc = ERROR_FUNCTION_FAILED;
146 WriteFile(the_file,data,size,&write,NULL);
147 CloseHandle(the_file);
148 TRACE("wrote %i bytes to %s\n",write,debugstr_w(source));
155 /* Support functions for FDI functions */
162 static void * cabinet_alloc(ULONG cb)
164 return msi_alloc(cb);
167 static void cabinet_free(void *pv)
172 static INT_PTR cabinet_open(char *pszFile, int oflag, int pmode)
176 DWORD dwShareMode = 0;
177 DWORD dwCreateDisposition = OPEN_EXISTING;
178 switch (oflag & _O_ACCMODE)
181 dwAccess = GENERIC_READ;
182 dwShareMode = FILE_SHARE_READ | FILE_SHARE_DELETE;
185 dwAccess = GENERIC_WRITE;
186 dwShareMode = FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE;
189 dwAccess = GENERIC_READ | GENERIC_WRITE;
190 dwShareMode = FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE;
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)
201 return (INT_PTR) handle;
204 static UINT cabinet_read(INT_PTR hf, void *pv, UINT cb)
206 HANDLE handle = (HANDLE) hf;
208 if (ReadFile(handle, pv, cb, &dwRead, NULL))
213 static UINT cabinet_write(INT_PTR hf, void *pv, UINT cb)
215 HANDLE handle = (HANDLE) hf;
217 if (WriteFile(handle, pv, cb, &dwWritten, NULL))
222 static int cabinet_close(INT_PTR hf)
224 HANDLE handle = (HANDLE) hf;
225 return CloseHandle(handle) ? 0 : -1;
228 static long cabinet_seek(INT_PTR hf, long dist, int seektype)
230 HANDLE handle = (HANDLE) hf;
231 /* flags are compatible and so are passed straight through */
232 return SetFilePointer(handle, dist, NULL, seektype);
235 static void msi_file_update_ui( MSIPACKAGE *package, MSIFILE *f, const WCHAR *action )
241 uirow = MSI_CreateRecord( 9 );
242 MSI_RecordSetStringW( uirow, 1, f->FileName );
243 uipath = strdupW( f->TargetPath );
244 p = strrchrW(uipath,'\\');
247 MSI_RecordSetStringW( uirow, 9, uipath);
248 MSI_RecordSetInteger( uirow, 6, f->FileSize );
249 ui_actiondata( package, action, uirow);
250 msiobj_release( &uirow->hdr );
252 ui_progress( package, 2, f->FileSize, 0, 0);
255 static UINT msi_media_get_disk_info( MSIPACKAGE *package, MSIMEDIAINFO *mi )
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};
265 row = MSI_QueryGetRecord(package->db, query, mi->disk_id);
268 TRACE("Unable to query row\n");
269 return ERROR_FUNCTION_FAILED;
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));
276 if (!mi->first_volume)
277 mi->first_volume = strdupW(mi->volume_label);
279 ptr = strrchrW(mi->source, '\\') + 1;
280 lstrcpyW(ptr, mi->cabinet);
281 msiobj_release(&row->hdr);
283 return ERROR_SUCCESS;
286 static INT_PTR cabinet_notify(FDINOTIFICATIONTYPE fdint, PFDINOTIFICATION pfdin)
288 TRACE("(%d)\n", fdint);
292 case fdintPARTIAL_FILE:
294 CabData *data = (CabData *)pfdin->pv;
295 data->mi->is_continuous = FALSE;
298 case fdintNEXT_CABINET:
300 CabData *data = (CabData *)pfdin->pv;
301 MSIMEDIAINFO *mi = data->mi;
302 LPWSTR cab = strdupAtoW(pfdin->psz1);
305 msi_free(mi->disk_prompt);
306 msi_free(mi->cabinet);
307 msi_free(mi->volume_label);
308 mi->disk_prompt = NULL;
310 mi->volume_label = NULL;
313 mi->is_continuous = TRUE;
315 rc = msi_media_get_disk_info(data->package, mi);
316 if (rc != ERROR_SUCCESS)
319 ERR("Failed to get next cabinet information: %d\n", rc);
323 if (lstrcmpiW(mi->cabinet, cab))
326 ERR("Continuous cabinet does not match the next cabinet in the Media table\n");
332 TRACE("Searching for %s\n", debugstr_w(mi->source));
334 if (GetFileAttributesW(mi->source) == INVALID_FILE_ATTRIBUTES)
335 rc = msi_change_media(data->package, mi);
337 if (rc != ERROR_SUCCESS)
344 CabData *data = (CabData*) pfdin->pv;
350 file = strdupAtoW(pfdin->psz1);
351 f = get_loaded_file(data->package, file);
356 WARN("unknown file in cabinet (%s)\n",debugstr_a(pfdin->psz1));
360 if (f->state != msifs_missing && f->state != msifs_overwrite)
362 TRACE("Skipping extraction of %s\n",debugstr_a(pfdin->psz1));
366 msi_file_update_ui( data->package, f, szInstallFiles );
368 TRACE("extracting %s\n", debugstr_w(f->TargetPath) );
370 attrs = f->Attributes & (FILE_ATTRIBUTE_READONLY|FILE_ATTRIBUTE_HIDDEN|FILE_ATTRIBUTE_SYSTEM);
371 if (!attrs) attrs = FILE_ATTRIBUTE_NORMAL;
373 handle = CreateFileW( f->TargetPath, GENERIC_READ | GENERIC_WRITE, 0,
374 NULL, CREATE_ALWAYS, attrs, NULL );
375 if ( handle == INVALID_HANDLE_VALUE )
377 if ( GetFileAttributesW( f->TargetPath ) != INVALID_FILE_ATTRIBUTES )
378 f->state = msifs_installed;
380 ERR("failed to create %s (error %d)\n",
381 debugstr_w( f->TargetPath ), GetLastError() );
386 f->state = msifs_installed;
387 return (INT_PTR) handle;
389 case fdintCLOSE_FILE_INFO:
391 CabData *data = (CabData*) pfdin->pv;
394 HANDLE handle = (HANDLE) pfdin->hf;
396 data->mi->is_continuous = FALSE;
398 if (!DosDateTimeToFileTime(pfdin->date, pfdin->time, &ft))
400 if (!LocalFileTimeToFileTime(&ft, &ftLocal))
402 if (!SetFileTime(handle, &ftLocal, 0, &ftLocal))
412 /***********************************************************************
415 * Extract files from a cab file.
417 BOOL msi_cabextract(MSIPACKAGE* package, MSIMEDIAINFO *mi,
418 PFNFDINOTIFY notify, LPVOID data)
420 LPSTR cabinet, cab_path = NULL;
426 TRACE("Extracting %s\n", debugstr_w(mi->source));
428 hfdi = FDICreate(cabinet_alloc, cabinet_free, cabinet_open, cabinet_read,
429 cabinet_write, cabinet_close, cabinet_seek, 0, &erf);
432 ERR("FDICreate failed\n");
436 ptr = strrchrW(mi->source, '\\') + 1;
437 cabinet = strdupWtoA(ptr);
441 cab_path = strdupWtoA(mi->source);
445 cab_path[ptr - mi->source] = '\0';
447 ret = FDICopy(hfdi, cabinet, cab_path, 0, notify, NULL, data);
449 ERR("FDICopy failed\n");
457 mi->is_extracted = TRUE;
462 void msi_free_media_info( MSIMEDIAINFO *mi )
464 msi_free( mi->disk_prompt );
465 msi_free( mi->cabinet );
466 msi_free( mi->volume_label );
467 msi_free( mi->first_volume );
471 UINT msi_load_media_info(MSIPACKAGE *package, MSIFILE *file, MSIMEDIAINFO *mi)
479 static const WCHAR query[] = {
480 'S','E','L','E','C','T',' ','*',' ', 'F','R','O','M',' ',
481 '`','M','e','d','i','a','`',' ','W','H','E','R','E',' ',
482 '`','L','a','s','t','S','e','q','u','e','n','c','e','`',' ','>','=',
483 ' ','%','i',' ','A','N','D',' ','`','D','i','s','k','I','d','`',' ','>','=',
484 ' ','%','i',' ','O','R','D','E','R',' ','B','Y',' ',
485 '`','D','i','s','k','I','d','`',0
488 row = MSI_QueryGetRecord(package->db, query, file->Sequence, mi->disk_id);
491 TRACE("Unable to query row\n");
492 return ERROR_FUNCTION_FAILED;
495 mi->is_extracted = FALSE;
496 mi->disk_id = MSI_RecordGetInteger(row, 1);
497 mi->last_sequence = MSI_RecordGetInteger(row, 2);
498 msi_free(mi->disk_prompt);
499 mi->disk_prompt = strdupW(MSI_RecordGetString(row, 3));
500 msi_free(mi->cabinet);
501 mi->cabinet = strdupW(MSI_RecordGetString(row, 4));
502 msi_free(mi->volume_label);
503 mi->volume_label = strdupW(MSI_RecordGetString(row, 5));
504 msiobj_release(&row->hdr);
506 if (!mi->first_volume)
507 mi->first_volume = strdupW(mi->volume_label);
509 source_dir = msi_dup_property(package, cszSourceDir);
510 lstrcpyW(mi->source, source_dir);
512 PathStripToRootW(source_dir);
513 mi->type = GetDriveTypeW(source_dir);
515 if (file->IsCompressed && mi->cabinet)
517 if (mi->cabinet[0] == '#')
519 r = writeout_cabinet_stream(package, &mi->cabinet[1], mi->source);
520 if (r != ERROR_SUCCESS)
522 ERR("Failed to extract cabinet stream\n");
523 return ERROR_FUNCTION_FAILED;
527 lstrcatW(mi->source, mi->cabinet);
530 options = MSICODE_PRODUCT;
531 if (mi->type == DRIVE_CDROM || mi->type == DRIVE_REMOVABLE)
534 options |= MSISOURCETYPE_MEDIA;
536 else if (package->BaseURL && UrlIsW(package->BaseURL, URLIS_URL))
538 source = package->BaseURL;
539 options |= MSISOURCETYPE_URL;
544 options |= MSISOURCETYPE_NETWORK;
547 if (mi->type == DRIVE_CDROM || mi->type == DRIVE_REMOVABLE)
548 msi_package_add_media_disk(package, package->Context,
549 MSICODE_PRODUCT, mi->disk_id,
550 mi->volume_label, mi->disk_prompt);
552 msi_package_add_info(package, package->Context,
553 options, INSTALLPROPERTY_LASTUSEDSOURCEW, source);
555 msi_free(source_dir);
556 return ERROR_SUCCESS;
559 /* FIXME: search NETWORK and URL sources as well */
560 UINT find_published_source(MSIPACKAGE *package, MSIMEDIAINFO *mi)
562 WCHAR source[MAX_PATH];
563 WCHAR volume[MAX_PATH];
564 WCHAR prompt[MAX_PATH];
565 DWORD volumesz, promptsz;
566 DWORD index, size, id;
570 r = MsiSourceListGetInfoW(package->ProductCode, NULL,
571 package->Context, MSICODE_PRODUCT,
572 INSTALLPROPERTY_LASTUSEDSOURCEW, source, &size);
573 if (r != ERROR_SUCCESS)
579 while (MsiSourceListEnumMediaDisksW(package->ProductCode, NULL,
581 MSICODE_PRODUCT, index++, &id,
582 volume, &volumesz, prompt, &promptsz) == ERROR_SUCCESS)
585 mi->volume_label = msi_realloc(mi->volume_label, ++volumesz * sizeof(WCHAR));
586 lstrcpyW(mi->volume_label, volume);
587 mi->disk_prompt = msi_realloc(mi->disk_prompt, ++promptsz * sizeof(WCHAR));
588 lstrcpyW(mi->disk_prompt, prompt);
590 if (source_matches_volume(mi, source))
592 /* FIXME: what about SourceDir */
593 lstrcpyW(mi->source, source);
594 lstrcatW(mi->source, mi->cabinet);
595 return ERROR_SUCCESS;
599 return ERROR_FUNCTION_FAILED;
602 static UINT ready_media(MSIPACKAGE *package, MSIFILE *file, MSIMEDIAINFO *mi)
604 UINT rc = ERROR_SUCCESS;
606 /* media info for continuous cabinet is already loaded */
607 if (mi->is_continuous)
608 return ERROR_SUCCESS;
610 rc = msi_load_media_info(package, file, mi);
611 if (rc != ERROR_SUCCESS)
613 ERR("Unable to load media info\n");
614 return ERROR_FUNCTION_FAILED;
617 /* cabinet is internal, no checks needed */
618 if (!mi->cabinet || mi->cabinet[0] == '#')
619 return ERROR_SUCCESS;
621 /* package should be downloaded */
622 if (file->IsCompressed &&
623 GetFileAttributesW(mi->source) == INVALID_FILE_ATTRIBUTES &&
624 package->BaseURL && UrlIsW(package->BaseURL, URLIS_URL))
626 WCHAR temppath[MAX_PATH];
628 msi_download_file(mi->source, temppath);
629 lstrcpyW(mi->source, temppath);
630 return ERROR_SUCCESS;
633 /* check volume matches, change media if not */
634 if (mi->volume_label && mi->disk_id > 1 &&
635 lstrcmpW(mi->first_volume, mi->volume_label))
637 LPWSTR source = msi_dup_property(package, cszSourceDir);
640 matches = source_matches_volume(mi, source);
643 if ((mi->type == DRIVE_CDROM || mi->type == DRIVE_REMOVABLE) && !matches)
645 rc = msi_change_media(package, mi);
646 if (rc != ERROR_SUCCESS)
651 if (file->IsCompressed &&
652 GetFileAttributesW(mi->source) == INVALID_FILE_ATTRIBUTES)
654 /* FIXME: this might be done earlier in the install process */
655 rc = find_published_source(package, mi);
656 if (rc != ERROR_SUCCESS)
658 ERR("Cabinet not found: %s\n", debugstr_w(mi->source));
659 return ERROR_INSTALL_FAILURE;
663 return ERROR_SUCCESS;
666 static UINT get_file_target(MSIPACKAGE *package, LPCWSTR file_key,
669 LIST_FOR_EACH_ENTRY( *file, &package->files, MSIFILE, entry )
671 if (lstrcmpW( file_key, (*file)->File )==0)
673 if ((*file)->state >= msifs_overwrite)
674 return ERROR_SUCCESS;
676 return ERROR_FILE_NOT_FOUND;
680 return ERROR_FUNCTION_FAILED;
683 static void schedule_install_files(MSIPACKAGE *package)
687 LIST_FOR_EACH_ENTRY(file, &package->files, MSIFILE, entry)
689 if (!ACTION_VerifyComponentForAction(file->Component, INSTALLSTATE_LOCAL))
691 TRACE("File %s is not scheduled for install\n", debugstr_w(file->File));
693 ui_progress(package,2,file->FileSize,0,0);
694 file->state = msifs_skipped;
699 static UINT copy_file(MSIFILE *file)
703 ret = CopyFileW(file->SourcePath, file->TargetPath, FALSE);
706 file->state = msifs_installed;
707 return ERROR_SUCCESS;
710 return GetLastError();
713 static UINT copy_install_file(MSIFILE *file)
717 TRACE("Copying %s to %s\n", debugstr_w(file->SourcePath),
718 debugstr_w(file->TargetPath));
720 gle = copy_file(file);
721 if (gle == ERROR_SUCCESS)
724 if (gle == ERROR_ALREADY_EXISTS && file->state == msifs_overwrite)
726 TRACE("overwriting existing file\n");
729 else if (gle == ERROR_FILE_NOT_FOUND)
731 /* FIXME: this needs to be tested, I'm pretty sure it fails */
732 TRACE("Source file not found\n");
735 else if (gle == ERROR_ACCESS_DENIED)
737 SetFileAttributesW(file->TargetPath, FILE_ATTRIBUTE_NORMAL);
739 gle = copy_file(file);
740 TRACE("Overwriting existing file: %d\n", gle);
742 else if (!(file->Attributes & msidbFileAttributesVital))
744 TRACE("Ignoring error for nonvital\n");
751 static BOOL check_dest_hash_matches(MSIFILE *file)
753 MSIFILEHASHINFO hash;
756 if (!file->hash.dwFileHashInfoSize)
759 hash.dwFileHashInfoSize = sizeof(MSIFILEHASHINFO);
760 r = MsiGetFileHashW(file->TargetPath, 0, &hash);
761 if (r != ERROR_SUCCESS)
764 return !memcmp(&hash, &file->hash, sizeof(MSIFILEHASHINFO));
768 * ACTION_InstallFiles()
770 * For efficiency, this is done in two passes:
771 * 1) Correct all the TargetPaths and determine what files are to be installed.
772 * 2) Extract Cabinets and copy files.
774 UINT ACTION_InstallFiles(MSIPACKAGE *package)
777 UINT rc = ERROR_SUCCESS;
780 /* increment progress bar each time action data is sent */
781 ui_progress(package,1,1,0,0);
783 schedule_install_files(package);
786 * Despite MSDN specifying that the CreateFolders action
787 * should be called before InstallFiles, some installers don't
788 * do that, and they seem to work correctly. We need to create
789 * directories here to make sure that the files can be copied.
791 msi_create_component_directories( package );
793 mi = msi_alloc_zero( sizeof(MSIMEDIAINFO) );
795 LIST_FOR_EACH_ENTRY( file, &package->files, MSIFILE, entry )
797 if (file->state != msifs_missing && !mi->is_continuous && file->state != msifs_overwrite)
800 if (check_dest_hash_matches(file))
802 TRACE("File hashes match, not overwriting\n");
806 if (file->Sequence > mi->last_sequence || mi->is_continuous ||
807 (file->IsCompressed && !mi->is_extracted))
811 rc = ready_media(package, file, mi);
812 if (rc != ERROR_SUCCESS)
814 ERR("Failed to ready media\n");
819 data.package = package;
821 if (file->IsCompressed &&
822 !msi_cabextract(package, mi, cabinet_notify, &data))
824 ERR("Failed to extract cabinet: %s\n", debugstr_w(mi->cabinet));
825 rc = ERROR_FUNCTION_FAILED;
830 if (!file->IsCompressed)
832 TRACE("file paths %s to %s\n", debugstr_w(file->SourcePath),
833 debugstr_w(file->TargetPath));
835 msi_file_update_ui(package, file, szInstallFiles);
836 rc = copy_install_file(file);
837 if (rc != ERROR_SUCCESS)
839 ERR("Failed to copy %s to %s (%d)\n", debugstr_w(file->SourcePath),
840 debugstr_w(file->TargetPath), rc);
841 rc = ERROR_INSTALL_FAILURE;
845 else if (file->state != msifs_installed)
847 ERR("compressed file wasn't extracted (%s)\n", debugstr_w(file->TargetPath));
848 rc = ERROR_INSTALL_FAILURE;
853 msi_free_media_info( mi );
857 static UINT ITERATE_DuplicateFiles(MSIRECORD *row, LPVOID param)
859 MSIPACKAGE *package = (MSIPACKAGE*)param;
860 WCHAR dest_name[0x100];
861 LPWSTR dest_path, dest;
862 LPCWSTR file_key, component;
868 component = MSI_RecordGetString(row,2);
869 comp = get_loaded_component(package,component);
871 if (!ACTION_VerifyComponentForAction( comp, INSTALLSTATE_LOCAL ))
873 TRACE("Skipping copy due to disabled component %s\n",
874 debugstr_w(component));
876 /* the action taken was the same as the current install state */
877 comp->Action = comp->Installed;
879 return ERROR_SUCCESS;
882 comp->Action = INSTALLSTATE_LOCAL;
884 file_key = MSI_RecordGetString(row,3);
887 ERR("Unable to get file key\n");
888 return ERROR_FUNCTION_FAILED;
891 rc = get_file_target(package,file_key,&file);
893 if (rc != ERROR_SUCCESS)
895 ERR("Original file unknown %s\n",debugstr_w(file_key));
896 return ERROR_SUCCESS;
899 if (MSI_RecordIsNull(row,4))
900 strcpyW(dest_name,strrchrW(file->TargetPath,'\\')+1);
904 MSI_RecordGetStringW(row,4,dest_name,&sz);
905 reduce_to_longfilename(dest_name);
908 if (MSI_RecordIsNull(row,5))
911 dest_path = strdupW(file->TargetPath);
912 p = strrchrW(dest_path,'\\');
919 destkey = MSI_RecordGetString(row,5);
920 dest_path = resolve_folder(package, destkey, FALSE, FALSE, TRUE, NULL);
924 dest_path = msi_dup_property( package, destkey );
927 FIXME("Unable to get destination folder, try AppSearch properties\n");
928 return ERROR_SUCCESS;
933 dest = build_directory_name(2, dest_path, dest_name);
934 create_full_pathW(dest_path);
936 TRACE("Duplicating file %s to %s\n",debugstr_w(file->TargetPath),
939 if (strcmpW(file->TargetPath,dest))
940 rc = !CopyFileW(file->TargetPath,dest,TRUE);
944 if (rc != ERROR_SUCCESS)
945 ERR("Failed to copy file %s -> %s, last error %d\n",
946 debugstr_w(file->TargetPath), debugstr_w(dest_path), GetLastError());
948 FIXME("We should track these duplicate files as well\n");
953 msi_file_update_ui(package, file, szDuplicateFiles);
955 return ERROR_SUCCESS;
958 UINT ACTION_DuplicateFiles(MSIPACKAGE *package)
962 static const WCHAR ExecSeqQuery[] =
963 {'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
964 '`','D','u','p','l','i','c','a','t','e','F','i','l','e','`',0};
966 rc = MSI_DatabaseOpenViewW(package->db, ExecSeqQuery, &view);
967 if (rc != ERROR_SUCCESS)
968 return ERROR_SUCCESS;
970 rc = MSI_IterateRecords(view, NULL, ITERATE_DuplicateFiles, package);
971 msiobj_release(&view->hdr);
976 /* compares the version of a file read from the filesystem and
977 * the version specified in the File table
979 static int msi_compare_file_version( MSIFILE *file )
981 WCHAR version[MAX_PATH];
987 r = MsiGetFileVersionW( file->TargetPath, version, &size, NULL, NULL );
988 if ( r != ERROR_SUCCESS )
991 return lstrcmpW( version, file->Version );
994 UINT ACTION_RemoveFiles( MSIPACKAGE *package )
998 LIST_FOR_EACH_ENTRY( file, &package->files, MSIFILE, entry )
1003 if ( !file->Component )
1005 if ( file->Component->Installed == INSTALLSTATE_LOCAL )
1008 if ( file->state == msifs_installed )
1009 ERR("removing installed file %s\n", debugstr_w(file->TargetPath));
1011 if ( file->state != msifs_present )
1014 /* only remove a file if the version to be installed
1015 * is strictly newer than the old file
1017 if ( msi_compare_file_version( file ) >= 0 )
1020 TRACE("removing %s\n", debugstr_w(file->File) );
1021 if ( !DeleteFileW( file->TargetPath ) )
1022 ERR("failed to delete %s\n", debugstr_w(file->TargetPath) );
1023 file->state = msifs_missing;
1026 uirow = MSI_CreateRecord( 9 );
1027 MSI_RecordSetStringW( uirow, 1, file->FileName );
1028 uipath = strdupW( file->TargetPath );
1029 p = strrchrW(uipath,'\\');
1032 MSI_RecordSetStringW( uirow, 9, uipath);
1033 ui_actiondata( package, szRemoveFiles, uirow);
1034 msiobj_release( &uirow->hdr );
1036 /* FIXME: call ui_progress here? */
1039 return ERROR_SUCCESS;