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
38 #include "wine/debug.h"
46 #include "wine/unicode.h"
48 WINE_DEFAULT_DEBUG_CHANNEL(msi
);
50 static void msi_file_update_ui( MSIPACKAGE
*package
, MSIFILE
*f
, const WCHAR
*action
)
56 uirow
= MSI_CreateRecord( 9 );
57 MSI_RecordSetStringW( uirow
, 1, f
->FileName
);
58 uipath
= strdupW( f
->TargetPath
);
59 p
= strrchrW(uipath
,'\\');
62 MSI_RecordSetStringW( uirow
, 9, uipath
);
63 MSI_RecordSetInteger( uirow
, 6, f
->FileSize
);
64 ui_actiondata( package
, action
, uirow
);
65 msiobj_release( &uirow
->hdr
);
67 ui_progress( package
, 2, f
->FileSize
, 0, 0);
70 /* compares the version of a file read from the filesystem and
71 * the version specified in the File table
73 static int msi_compare_file_version(MSIFILE
*file
)
75 WCHAR version
[MAX_PATH
];
81 r
= MsiGetFileVersionW(file
->TargetPath
, version
, &size
, NULL
, NULL
);
82 if (r
!= ERROR_SUCCESS
)
85 return lstrcmpW(version
, file
->Version
);
88 static void schedule_install_files(MSIPACKAGE
*package
)
92 LIST_FOR_EACH_ENTRY(file
, &package
->files
, MSIFILE
, entry
)
94 if (file
->Component
->ActionRequest
!= INSTALLSTATE_LOCAL
)
96 TRACE("File %s is not scheduled for install\n", debugstr_w(file
->File
));
98 ui_progress(package
,2,file
->FileSize
,0,0);
99 file
->state
= msifs_skipped
;
104 static UINT
copy_file(MSIFILE
*file
, LPWSTR source
)
108 ret
= CopyFileW(source
, file
->TargetPath
, FALSE
);
110 return GetLastError();
112 SetFileAttributesW(file
->TargetPath
, FILE_ATTRIBUTE_NORMAL
);
114 file
->state
= msifs_installed
;
115 return ERROR_SUCCESS
;
118 static UINT
copy_install_file(MSIPACKAGE
*package
, MSIFILE
*file
, LPWSTR source
)
122 TRACE("Copying %s to %s\n", debugstr_w(source
),
123 debugstr_w(file
->TargetPath
));
125 gle
= copy_file(file
, source
);
126 if (gle
== ERROR_SUCCESS
)
129 if (gle
== ERROR_ALREADY_EXISTS
&& file
->state
== msifs_overwrite
)
131 TRACE("overwriting existing file\n");
132 return ERROR_SUCCESS
;
134 else if (gle
== ERROR_ACCESS_DENIED
)
136 SetFileAttributesW(file
->TargetPath
, FILE_ATTRIBUTE_NORMAL
);
138 gle
= copy_file(file
, source
);
139 TRACE("Overwriting existing file: %d\n", gle
);
141 if (gle
== ERROR_SHARING_VIOLATION
|| gle
== ERROR_USER_MAPPED_FILE
)
143 WCHAR tmpfileW
[MAX_PATH
], *pathW
, *p
;
146 TRACE("file in use, scheduling rename operation\n");
148 GetTempFileNameW(szBackSlash
, szMsi
, 0, tmpfileW
);
149 len
= strlenW(file
->TargetPath
) + strlenW(tmpfileW
) + 1;
150 if (!(pathW
= HeapAlloc(GetProcessHeap(), 0, len
* sizeof(WCHAR
))))
151 return ERROR_OUTOFMEMORY
;
153 strcpyW(pathW
, file
->TargetPath
);
154 if ((p
= strrchrW(pathW
, '\\'))) *p
= 0;
155 strcatW(pathW
, tmpfileW
);
157 if (CopyFileW(source
, pathW
, FALSE
) &&
158 MoveFileExW(file
->TargetPath
, NULL
, MOVEFILE_DELAY_UNTIL_REBOOT
) &&
159 MoveFileExW(pathW
, file
->TargetPath
, MOVEFILE_DELAY_UNTIL_REBOOT
))
161 file
->state
= msifs_installed
;
162 package
->need_reboot
= 1;
167 gle
= GetLastError();
168 WARN("failed to schedule rename operation: %d)\n", gle
);
170 HeapFree(GetProcessHeap(), 0, pathW
);
176 static BOOL
check_dest_hash_matches(MSIFILE
*file
)
178 MSIFILEHASHINFO hash
;
181 if (!file
->hash
.dwFileHashInfoSize
)
184 hash
.dwFileHashInfoSize
= sizeof(MSIFILEHASHINFO
);
185 r
= MsiGetFileHashW(file
->TargetPath
, 0, &hash
);
186 if (r
!= ERROR_SUCCESS
)
189 return !memcmp(&hash
, &file
->hash
, sizeof(MSIFILEHASHINFO
));
192 static BOOL
installfiles_cb(MSIPACKAGE
*package
, LPCWSTR file
, DWORD action
,
193 LPWSTR
*path
, DWORD
*attrs
, PVOID user
)
195 static MSIFILE
*f
= NULL
;
197 if (action
== MSICABEXTRACT_BEGINEXTRACT
)
199 f
= get_loaded_file(package
, file
);
202 WARN("unknown file in cabinet (%s)\n", debugstr_w(file
));
206 if (f
->state
!= msifs_missing
&& f
->state
!= msifs_overwrite
)
208 TRACE("Skipping extraction of %s\n", debugstr_w(file
));
212 msi_file_update_ui(package
, f
, szInstallFiles
);
214 *path
= strdupW(f
->TargetPath
);
215 *attrs
= f
->Attributes
;
217 else if (action
== MSICABEXTRACT_FILEEXTRACTED
)
219 f
->state
= msifs_installed
;
227 * ACTION_InstallFiles()
229 * For efficiency, this is done in two passes:
230 * 1) Correct all the TargetPaths and determine what files are to be installed.
231 * 2) Extract Cabinets and copy files.
233 UINT
ACTION_InstallFiles(MSIPACKAGE
*package
)
236 UINT rc
= ERROR_SUCCESS
;
239 /* increment progress bar each time action data is sent */
240 ui_progress(package
,1,1,0,0);
242 schedule_install_files(package
);
245 * Despite MSDN specifying that the CreateFolders action
246 * should be called before InstallFiles, some installers don't
247 * do that, and they seem to work correctly. We need to create
248 * directories here to make sure that the files can be copied.
250 msi_create_component_directories( package
);
252 mi
= msi_alloc_zero( sizeof(MSIMEDIAINFO
) );
254 LIST_FOR_EACH_ENTRY( file
, &package
->files
, MSIFILE
, entry
)
256 if (file
->state
!= msifs_missing
&& !mi
->is_continuous
&& file
->state
!= msifs_overwrite
)
259 if (check_dest_hash_matches(file
))
261 TRACE("File hashes match, not overwriting\n");
265 if (MsiGetFileVersionW(file
->TargetPath
, NULL
, NULL
, NULL
, NULL
) == ERROR_SUCCESS
&&
266 msi_compare_file_version(file
) >= 0)
268 TRACE("Destination file version greater, not overwriting\n");
272 if (file
->Sequence
> mi
->last_sequence
|| mi
->is_continuous
||
273 (file
->IsCompressed
&& !mi
->is_extracted
))
277 rc
= ready_media(package
, file
, mi
);
278 if (rc
!= ERROR_SUCCESS
)
280 ERR("Failed to ready media\n");
285 data
.package
= package
;
286 data
.cb
= installfiles_cb
;
289 if (file
->IsCompressed
&&
290 !msi_cabextract(package
, mi
, &data
))
292 ERR("Failed to extract cabinet: %s\n", debugstr_w(mi
->cabinet
));
293 rc
= ERROR_FUNCTION_FAILED
;
298 if (!file
->IsCompressed
)
300 LPWSTR source
= resolve_file_source(package
, file
);
302 TRACE("file paths %s to %s\n", debugstr_w(source
),
303 debugstr_w(file
->TargetPath
));
305 msi_file_update_ui(package
, file
, szInstallFiles
);
306 rc
= copy_install_file(package
, file
, source
);
307 if (rc
!= ERROR_SUCCESS
)
309 ERR("Failed to copy %s to %s (%d)\n", debugstr_w(source
),
310 debugstr_w(file
->TargetPath
), rc
);
311 rc
= ERROR_INSTALL_FAILURE
;
318 else if (file
->state
!= msifs_installed
)
320 ERR("compressed file wasn't extracted (%s)\n",
321 debugstr_w(file
->TargetPath
));
322 rc
= ERROR_INSTALL_FAILURE
;
327 msi_free_media_info(mi
);
331 #define is_dot_dir(x) ((x[0] == '.') && ((x[1] == 0) || ((x[1] == '.') && (x[2] == 0))))
342 static BOOL
msi_move_file(LPCWSTR source
, LPCWSTR dest
, int options
)
346 if (GetFileAttributesW(source
) == FILE_ATTRIBUTE_DIRECTORY
||
347 GetFileAttributesW(dest
) == FILE_ATTRIBUTE_DIRECTORY
)
349 WARN("Source or dest is directory, not moving\n");
353 if (options
== msidbMoveFileOptionsMove
)
355 TRACE("moving %s -> %s\n", debugstr_w(source
), debugstr_w(dest
));
356 ret
= MoveFileExW(source
, dest
, MOVEFILE_REPLACE_EXISTING
);
359 WARN("MoveFile failed: %d\n", GetLastError());
365 TRACE("copying %s -> %s\n", debugstr_w(source
), debugstr_w(dest
));
366 ret
= CopyFileW(source
, dest
, FALSE
);
369 WARN("CopyFile failed: %d\n", GetLastError());
377 static LPWSTR
wildcard_to_file(LPWSTR wildcard
, LPWSTR filename
)
380 DWORD dirlen
, pathlen
;
382 ptr
= strrchrW(wildcard
, '\\');
383 dirlen
= ptr
- wildcard
+ 1;
385 pathlen
= dirlen
+ lstrlenW(filename
) + 1;
386 path
= msi_alloc(pathlen
* sizeof(WCHAR
));
388 lstrcpynW(path
, wildcard
, dirlen
+ 1);
389 lstrcatW(path
, filename
);
394 static void free_file_entry(FILE_LIST
*file
)
396 msi_free(file
->source
);
397 msi_free(file
->dest
);
401 static void free_list(FILE_LIST
*list
)
403 while (!list_empty(&list
->entry
))
405 FILE_LIST
*file
= LIST_ENTRY(list_head(&list
->entry
), FILE_LIST
, entry
);
407 list_remove(&file
->entry
);
408 free_file_entry(file
);
412 static BOOL
add_wildcard(FILE_LIST
*files
, LPWSTR source
, LPWSTR dest
)
414 FILE_LIST
*new, *file
;
415 LPWSTR ptr
, filename
;
418 new = msi_alloc_zero(sizeof(FILE_LIST
));
422 new->source
= strdupW(source
);
423 ptr
= strrchrW(dest
, '\\') + 1;
424 filename
= strrchrW(new->source
, '\\') + 1;
426 new->sourcename
= filename
;
431 new->destname
= new->sourcename
;
433 size
= (ptr
- dest
) + lstrlenW(filename
) + 1;
434 new->dest
= msi_alloc(size
* sizeof(WCHAR
));
437 free_file_entry(new);
441 lstrcpynW(new->dest
, dest
, ptr
- dest
+ 1);
442 lstrcatW(new->dest
, filename
);
444 if (list_empty(&files
->entry
))
446 list_add_head(&files
->entry
, &new->entry
);
450 LIST_FOR_EACH_ENTRY(file
, &files
->entry
, FILE_LIST
, entry
)
452 if (lstrcmpW(source
, file
->source
) < 0)
454 list_add_before(&file
->entry
, &new->entry
);
459 list_add_after(&file
->entry
, &new->entry
);
463 static BOOL
move_files_wildcard(LPWSTR source
, LPWSTR dest
, int options
)
465 WIN32_FIND_DATAW wfd
;
469 FILE_LIST files
, *file
;
472 hfile
= FindFirstFileW(source
, &wfd
);
473 if (hfile
== INVALID_HANDLE_VALUE
) return FALSE
;
475 list_init(&files
.entry
);
477 for (res
= TRUE
; res
; res
= FindNextFileW(hfile
, &wfd
))
479 if (is_dot_dir(wfd
.cFileName
)) continue;
481 path
= wildcard_to_file(source
, wfd
.cFileName
);
488 add_wildcard(&files
, path
, dest
);
492 /* no files match the wildcard */
493 if (list_empty(&files
.entry
))
496 /* only the first wildcard match gets renamed to dest */
497 file
= LIST_ENTRY(list_head(&files
.entry
), FILE_LIST
, entry
);
498 size
= (strrchrW(file
->dest
, '\\') - file
->dest
) + lstrlenW(file
->destname
) + 2;
499 file
->dest
= msi_realloc(file
->dest
, size
* sizeof(WCHAR
));
506 /* file->dest may be shorter after the reallocation, so add a NULL
507 * terminator. This is needed for the call to strrchrW, as there will no
508 * longer be a NULL terminator within the bounds of the allocation in this case.
510 file
->dest
[size
- 1] = '\0';
511 lstrcpyW(strrchrW(file
->dest
, '\\') + 1, file
->destname
);
513 while (!list_empty(&files
.entry
))
515 file
= LIST_ENTRY(list_head(&files
.entry
), FILE_LIST
, entry
);
517 msi_move_file(file
->source
, file
->dest
, options
);
519 list_remove(&file
->entry
);
520 free_file_entry(file
);
531 static UINT
ITERATE_MoveFiles( MSIRECORD
*rec
, LPVOID param
)
533 MSIPACKAGE
*package
= param
;
536 LPCWSTR sourcename
, component
;
537 LPWSTR sourcedir
, destname
= NULL
, destdir
= NULL
, source
= NULL
, dest
= NULL
;
542 component
= MSI_RecordGetString(rec
, 2);
543 comp
= get_loaded_component(package
, component
);
545 return ERROR_SUCCESS
;
547 if (comp
->ActionRequest
!= INSTALLSTATE_LOCAL
&& comp
->ActionRequest
!= INSTALLSTATE_SOURCE
)
549 TRACE("Component not scheduled for installation: %s\n", debugstr_w(component
));
550 comp
->Action
= comp
->Installed
;
551 return ERROR_SUCCESS
;
553 comp
->Action
= comp
->ActionRequest
;
555 sourcename
= MSI_RecordGetString(rec
, 3);
556 options
= MSI_RecordGetInteger(rec
, 7);
558 sourcedir
= msi_dup_property(package
, MSI_RecordGetString(rec
, 5));
562 destdir
= msi_dup_property(package
, MSI_RecordGetString(rec
, 6));
568 if (GetFileAttributesW(sourcedir
) == INVALID_FILE_ATTRIBUTES
)
571 source
= strdupW(sourcedir
);
577 size
= lstrlenW(sourcedir
) + lstrlenW(sourcename
) + 2;
578 source
= msi_alloc(size
* sizeof(WCHAR
));
582 lstrcpyW(source
, sourcedir
);
583 if (source
[lstrlenW(source
) - 1] != '\\')
584 lstrcatW(source
, szBackSlash
);
585 lstrcatW(source
, sourcename
);
588 wildcards
= strchrW(source
, '*') || strchrW(source
, '?');
590 if (MSI_RecordIsNull(rec
, 4))
594 destname
= strdupW(sourcename
);
601 destname
= strdupW(MSI_RecordGetString(rec
, 4));
603 reduce_to_longfilename(destname
);
608 size
= lstrlenW(destname
);
610 size
+= lstrlenW(destdir
) + 2;
611 dest
= msi_alloc(size
* sizeof(WCHAR
));
615 lstrcpyW(dest
, destdir
);
616 if (dest
[lstrlenW(dest
) - 1] != '\\')
617 lstrcatW(dest
, szBackSlash
);
620 lstrcatW(dest
, destname
);
622 if (GetFileAttributesW(destdir
) == INVALID_FILE_ATTRIBUTES
)
624 ret
= CreateDirectoryW(destdir
, NULL
);
627 WARN("CreateDirectory failed: %d\n", GetLastError());
633 msi_move_file(source
, dest
, options
);
635 move_files_wildcard(source
, dest
, options
);
638 uirow
= MSI_CreateRecord( 9 );
639 MSI_RecordSetStringW( uirow
, 1, MSI_RecordGetString(rec
, 1) );
640 MSI_RecordSetInteger( uirow
, 6, 1 ); /* FIXME */
641 MSI_RecordSetStringW( uirow
, 9, destdir
);
642 ui_actiondata( package
, szMoveFiles
, uirow
);
643 msiobj_release( &uirow
->hdr
);
651 return ERROR_SUCCESS
;
654 UINT
ACTION_MoveFiles( MSIPACKAGE
*package
)
659 static const WCHAR ExecSeqQuery
[] =
660 {'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
661 '`','M','o','v','e','F','i','l','e','`',0};
663 rc
= MSI_DatabaseOpenViewW(package
->db
, ExecSeqQuery
, &view
);
664 if (rc
!= ERROR_SUCCESS
)
665 return ERROR_SUCCESS
;
667 rc
= MSI_IterateRecords(view
, NULL
, ITERATE_MoveFiles
, package
);
668 msiobj_release(&view
->hdr
);
673 static WCHAR
*get_duplicate_filename( MSIPACKAGE
*package
, MSIRECORD
*row
, const WCHAR
*file_key
, const WCHAR
*src
)
676 WCHAR
*dst_name
, *dst_path
, *dst
;
678 if (MSI_RecordIsNull( row
, 4 ))
680 len
= strlenW( src
) + 1;
681 if (!(dst_name
= msi_alloc( len
* sizeof(WCHAR
)))) return NULL
;
682 strcpyW( dst_name
, strrchrW( src
, '\\' ) + 1 );
686 MSI_RecordGetStringW( row
, 4, NULL
, &len
);
687 if (!(dst_name
= msi_alloc( ++len
* sizeof(WCHAR
) ))) return NULL
;
688 MSI_RecordGetStringW( row
, 4, dst_name
, &len
);
689 reduce_to_longfilename( dst_name
);
692 if (MSI_RecordIsNull( row
, 5 ))
695 dst_path
= strdupW( src
);
696 p
= strrchrW( dst_path
, '\\' );
701 const WCHAR
*dst_key
= MSI_RecordGetString( row
, 5 );
703 dst_path
= resolve_folder( package
, dst_key
, FALSE
, FALSE
, TRUE
, NULL
);
707 dst_path
= msi_dup_property( package
, dst_key
);
710 FIXME("Unable to get destination folder, try AppSearch properties\n");
711 msi_free( dst_name
);
717 dst
= build_directory_name( 2, dst_path
, dst_name
);
718 create_full_pathW( dst_path
);
720 msi_free( dst_name
);
721 msi_free( dst_path
);
725 static UINT
ITERATE_DuplicateFiles(MSIRECORD
*row
, LPVOID param
)
727 MSIPACKAGE
*package
= param
;
729 LPCWSTR file_key
, component
;
734 component
= MSI_RecordGetString(row
,2);
735 comp
= get_loaded_component(package
,component
);
737 return ERROR_SUCCESS
;
739 if (comp
->ActionRequest
!= INSTALLSTATE_LOCAL
)
741 TRACE("Component not scheduled for installation %s\n", debugstr_w(component
));
742 comp
->Action
= comp
->Installed
;
743 return ERROR_SUCCESS
;
745 comp
->Action
= INSTALLSTATE_LOCAL
;
747 file_key
= MSI_RecordGetString(row
,3);
750 ERR("Unable to get file key\n");
751 return ERROR_FUNCTION_FAILED
;
754 file
= get_loaded_file( package
, file_key
);
757 ERR("Original file unknown %s\n", debugstr_w(file_key
));
758 return ERROR_SUCCESS
;
761 dest
= get_duplicate_filename( package
, row
, file_key
, file
->TargetPath
);
764 WARN("Unable to get duplicate filename\n");
765 return ERROR_SUCCESS
;
768 TRACE("Duplicating file %s to %s\n", debugstr_w(file
->TargetPath
), debugstr_w(dest
));
770 if (!CopyFileW( file
->TargetPath
, dest
, TRUE
))
772 WARN("Failed to copy file %s -> %s (%u)\n",
773 debugstr_w(file
->TargetPath
), debugstr_w(dest
), GetLastError());
776 FIXME("We should track these duplicate files as well\n");
778 uirow
= MSI_CreateRecord( 9 );
779 MSI_RecordSetStringW( uirow
, 1, MSI_RecordGetString( row
, 1 ) );
780 MSI_RecordSetInteger( uirow
, 6, file
->FileSize
);
781 MSI_RecordSetStringW( uirow
, 9, MSI_RecordGetString( row
, 5 ) );
782 ui_actiondata( package
, szDuplicateFiles
, uirow
);
783 msiobj_release( &uirow
->hdr
);
786 return ERROR_SUCCESS
;
789 UINT
ACTION_DuplicateFiles(MSIPACKAGE
*package
)
793 static const WCHAR ExecSeqQuery
[] =
794 {'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
795 '`','D','u','p','l','i','c','a','t','e','F','i','l','e','`',0};
797 rc
= MSI_DatabaseOpenViewW(package
->db
, ExecSeqQuery
, &view
);
798 if (rc
!= ERROR_SUCCESS
)
799 return ERROR_SUCCESS
;
801 rc
= MSI_IterateRecords(view
, NULL
, ITERATE_DuplicateFiles
, package
);
802 msiobj_release(&view
->hdr
);
807 static UINT
ITERATE_RemoveDuplicateFiles( MSIRECORD
*row
, LPVOID param
)
809 MSIPACKAGE
*package
= param
;
811 LPCWSTR file_key
, component
;
816 component
= MSI_RecordGetString( row
, 2 );
817 comp
= get_loaded_component( package
, component
);
819 return ERROR_SUCCESS
;
821 if (comp
->ActionRequest
!= INSTALLSTATE_ABSENT
)
823 TRACE("Component not scheduled for removal %s\n", debugstr_w(component
));
824 comp
->Action
= comp
->Installed
;
825 return ERROR_SUCCESS
;
827 comp
->Action
= INSTALLSTATE_ABSENT
;
829 file_key
= MSI_RecordGetString( row
, 3 );
832 ERR("Unable to get file key\n");
833 return ERROR_FUNCTION_FAILED
;
836 file
= get_loaded_file( package
, file_key
);
839 ERR("Original file unknown %s\n", debugstr_w(file_key
));
840 return ERROR_SUCCESS
;
843 dest
= get_duplicate_filename( package
, row
, file_key
, file
->TargetPath
);
846 WARN("Unable to get duplicate filename\n");
847 return ERROR_SUCCESS
;
850 TRACE("Removing duplicate %s of %s\n", debugstr_w(dest
), debugstr_w(file
->TargetPath
));
852 if (!DeleteFileW( dest
))
854 WARN("Failed to delete duplicate file %s (%u)\n", debugstr_w(dest
), GetLastError());
857 uirow
= MSI_CreateRecord( 9 );
858 MSI_RecordSetStringW( uirow
, 1, MSI_RecordGetString( row
, 1 ) );
859 MSI_RecordSetStringW( uirow
, 9, MSI_RecordGetString( row
, 5 ) );
860 ui_actiondata( package
, szRemoveDuplicateFiles
, uirow
);
861 msiobj_release( &uirow
->hdr
);
864 return ERROR_SUCCESS
;
867 UINT
ACTION_RemoveDuplicateFiles( MSIPACKAGE
*package
)
871 static const WCHAR query
[] =
872 {'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
873 '`','D','u','p','l','i','c','a','t','e','F','i','l','e','`',0};
875 rc
= MSI_DatabaseOpenViewW( package
->db
, query
, &view
);
876 if (rc
!= ERROR_SUCCESS
)
877 return ERROR_SUCCESS
;
879 rc
= MSI_IterateRecords( view
, NULL
, ITERATE_RemoveDuplicateFiles
, package
);
880 msiobj_release( &view
->hdr
);
885 static BOOL
verify_comp_for_removal(MSICOMPONENT
*comp
, UINT install_mode
)
887 INSTALLSTATE request
= comp
->ActionRequest
;
889 if (request
== INSTALLSTATE_UNKNOWN
)
892 if (install_mode
== msidbRemoveFileInstallModeOnInstall
&&
893 (request
== INSTALLSTATE_LOCAL
|| request
== INSTALLSTATE_SOURCE
))
896 if (request
== INSTALLSTATE_ABSENT
)
898 if (!comp
->ComponentId
)
901 if (install_mode
== msidbRemoveFileInstallModeOnRemove
)
905 if (install_mode
== msidbRemoveFileInstallModeOnBoth
)
911 static UINT
ITERATE_RemoveFiles(MSIRECORD
*row
, LPVOID param
)
913 MSIPACKAGE
*package
= param
;
915 LPCWSTR component
, filename
, dirprop
;
917 LPWSTR dir
= NULL
, path
= NULL
;
921 component
= MSI_RecordGetString(row
, 2);
922 filename
= MSI_RecordGetString(row
, 3);
923 dirprop
= MSI_RecordGetString(row
, 4);
924 install_mode
= MSI_RecordGetInteger(row
, 5);
926 comp
= get_loaded_component(package
, component
);
929 ERR("Invalid component: %s\n", debugstr_w(component
));
930 return ERROR_FUNCTION_FAILED
;
933 if (!verify_comp_for_removal(comp
, install_mode
))
935 TRACE("Skipping removal due to missing conditions\n");
936 comp
->Action
= comp
->Installed
;
937 return ERROR_SUCCESS
;
940 dir
= msi_dup_property(package
, dirprop
);
942 return ERROR_OUTOFMEMORY
;
944 size
= (filename
!= NULL
) ? lstrlenW(filename
) : 0;
945 size
+= lstrlenW(dir
) + 2;
946 path
= msi_alloc(size
* sizeof(WCHAR
));
949 r
= ERROR_OUTOFMEMORY
;
956 PathAddBackslashW(path
);
957 lstrcatW(path
, filename
);
959 TRACE("Deleting misc file: %s\n", debugstr_w(path
));
964 TRACE("Removing misc directory: %s\n", debugstr_w(dir
));
965 RemoveDirectoryW(dir
);
971 return ERROR_SUCCESS
;
974 UINT
ACTION_RemoveFiles( MSIPACKAGE
*package
)
980 static const WCHAR query
[] = {
981 'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
982 '`','R','e','m','o','v','e','F','i','l','e','`',0};
983 static const WCHAR folder_query
[] = {
984 'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
985 '`','C','r','e','a','t','e','F','o','l','d','e','r','`',0};
987 r
= MSI_DatabaseOpenViewW(package
->db
, query
, &view
);
988 if (r
== ERROR_SUCCESS
)
990 MSI_IterateRecords(view
, NULL
, ITERATE_RemoveFiles
, package
);
991 msiobj_release(&view
->hdr
);
994 r
= MSI_DatabaseOpenViewW(package
->db
, folder_query
, &view
);
995 if (r
== ERROR_SUCCESS
)
996 msiobj_release(&view
->hdr
);
998 LIST_FOR_EACH_ENTRY( file
, &package
->files
, MSIFILE
, entry
)
1001 LPWSTR dir
, uipath
, p
;
1003 if ( file
->state
== msifs_installed
)
1004 ERR("removing installed file %s\n", debugstr_w(file
->TargetPath
));
1006 if ( file
->Component
->ActionRequest
!= INSTALLSTATE_ABSENT
||
1007 file
->Component
->Installed
== INSTALLSTATE_SOURCE
)
1010 /* don't remove a file if the old file
1011 * is strictly newer than the version to be installed
1013 if ( msi_compare_file_version( file
) < 0 )
1016 TRACE("removing %s\n", debugstr_w(file
->File
) );
1017 if (!DeleteFileW( file
->TargetPath
))
1019 WARN("failed to delete %s\n", debugstr_w(file
->TargetPath
));
1021 /* FIXME: check persistence for each directory */
1022 else if (r
&& (dir
= strdupW( file
->TargetPath
)))
1024 if ((p
= strrchrW( dir
, '\\' ))) *p
= 0;
1025 RemoveDirectoryW( dir
);
1028 file
->state
= msifs_missing
;
1031 uirow
= MSI_CreateRecord( 9 );
1032 MSI_RecordSetStringW( uirow
, 1, file
->FileName
);
1033 uipath
= strdupW( file
->TargetPath
);
1034 p
= strrchrW(uipath
,'\\');
1037 MSI_RecordSetStringW( uirow
, 9, uipath
);
1038 ui_actiondata( package
, szRemoveFiles
, uirow
);
1039 msiobj_release( &uirow
->hdr
);
1041 /* FIXME: call ui_progress here? */
1044 return ERROR_SUCCESS
;