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
;
102 file
->Component
->Action
= INSTALLSTATE_LOCAL
;
106 static UINT
copy_file(MSIFILE
*file
, LPWSTR source
)
110 ret
= CopyFileW(source
, file
->TargetPath
, FALSE
);
112 return GetLastError();
114 SetFileAttributesW(file
->TargetPath
, FILE_ATTRIBUTE_NORMAL
);
116 file
->state
= msifs_installed
;
117 return ERROR_SUCCESS
;
120 static UINT
copy_install_file(MSIPACKAGE
*package
, MSIFILE
*file
, LPWSTR source
)
124 TRACE("Copying %s to %s\n", debugstr_w(source
),
125 debugstr_w(file
->TargetPath
));
127 gle
= copy_file(file
, source
);
128 if (gle
== ERROR_SUCCESS
)
131 if (gle
== ERROR_ALREADY_EXISTS
&& file
->state
== msifs_overwrite
)
133 TRACE("overwriting existing file\n");
134 return ERROR_SUCCESS
;
136 else if (gle
== ERROR_ACCESS_DENIED
)
138 SetFileAttributesW(file
->TargetPath
, FILE_ATTRIBUTE_NORMAL
);
140 gle
= copy_file(file
, source
);
141 TRACE("Overwriting existing file: %d\n", gle
);
143 if (gle
== ERROR_SHARING_VIOLATION
|| gle
== ERROR_USER_MAPPED_FILE
)
145 WCHAR tmpfileW
[MAX_PATH
], *pathW
, *p
;
148 TRACE("file in use, scheduling rename operation\n");
150 GetTempFileNameW(szBackSlash
, szMsi
, 0, tmpfileW
);
151 len
= strlenW(file
->TargetPath
) + strlenW(tmpfileW
) + 1;
152 if (!(pathW
= HeapAlloc(GetProcessHeap(), 0, len
* sizeof(WCHAR
))))
153 return ERROR_OUTOFMEMORY
;
155 strcpyW(pathW
, file
->TargetPath
);
156 if ((p
= strrchrW(pathW
, '\\'))) *p
= 0;
157 strcatW(pathW
, tmpfileW
);
159 if (CopyFileW(source
, pathW
, FALSE
) &&
160 MoveFileExW(file
->TargetPath
, NULL
, MOVEFILE_DELAY_UNTIL_REBOOT
) &&
161 MoveFileExW(pathW
, file
->TargetPath
, MOVEFILE_DELAY_UNTIL_REBOOT
))
163 file
->state
= msifs_installed
;
164 package
->need_reboot
= 1;
169 gle
= GetLastError();
170 WARN("failed to schedule rename operation: %d)\n", gle
);
172 HeapFree(GetProcessHeap(), 0, pathW
);
178 static BOOL
check_dest_hash_matches(MSIFILE
*file
)
180 MSIFILEHASHINFO hash
;
183 if (!file
->hash
.dwFileHashInfoSize
)
186 hash
.dwFileHashInfoSize
= sizeof(MSIFILEHASHINFO
);
187 r
= MsiGetFileHashW(file
->TargetPath
, 0, &hash
);
188 if (r
!= ERROR_SUCCESS
)
191 return !memcmp(&hash
, &file
->hash
, sizeof(MSIFILEHASHINFO
));
194 static BOOL
installfiles_cb(MSIPACKAGE
*package
, LPCWSTR file
, DWORD action
,
195 LPWSTR
*path
, DWORD
*attrs
, PVOID user
)
197 static MSIFILE
*f
= NULL
;
199 if (action
== MSICABEXTRACT_BEGINEXTRACT
)
201 f
= get_loaded_file(package
, file
);
204 WARN("unknown file in cabinet (%s)\n", debugstr_w(file
));
208 if (f
->state
!= msifs_missing
&& f
->state
!= msifs_overwrite
)
210 TRACE("Skipping extraction of %s\n", debugstr_w(file
));
214 msi_file_update_ui(package
, f
, szInstallFiles
);
216 *path
= strdupW(f
->TargetPath
);
217 *attrs
= f
->Attributes
;
219 else if (action
== MSICABEXTRACT_FILEEXTRACTED
)
221 f
->state
= msifs_installed
;
229 * ACTION_InstallFiles()
231 * For efficiency, this is done in two passes:
232 * 1) Correct all the TargetPaths and determine what files are to be installed.
233 * 2) Extract Cabinets and copy files.
235 UINT
ACTION_InstallFiles(MSIPACKAGE
*package
)
238 UINT rc
= ERROR_SUCCESS
;
241 /* increment progress bar each time action data is sent */
242 ui_progress(package
,1,1,0,0);
244 schedule_install_files(package
);
247 * Despite MSDN specifying that the CreateFolders action
248 * should be called before InstallFiles, some installers don't
249 * do that, and they seem to work correctly. We need to create
250 * directories here to make sure that the files can be copied.
252 msi_create_component_directories( package
);
254 mi
= msi_alloc_zero( sizeof(MSIMEDIAINFO
) );
256 LIST_FOR_EACH_ENTRY( file
, &package
->files
, MSIFILE
, entry
)
258 if (file
->state
!= msifs_missing
&& !mi
->is_continuous
&& file
->state
!= msifs_overwrite
)
261 if (check_dest_hash_matches(file
))
263 TRACE("File hashes match, not overwriting\n");
267 if (MsiGetFileVersionW(file
->TargetPath
, NULL
, NULL
, NULL
, NULL
) == ERROR_SUCCESS
&&
268 msi_compare_file_version(file
) >= 0)
270 TRACE("Destination file version greater, not overwriting\n");
274 if (file
->Sequence
> mi
->last_sequence
|| mi
->is_continuous
||
275 (file
->IsCompressed
&& !mi
->is_extracted
))
279 rc
= ready_media(package
, file
, mi
);
280 if (rc
!= ERROR_SUCCESS
)
282 ERR("Failed to ready media\n");
287 data
.package
= package
;
288 data
.cb
= installfiles_cb
;
291 if (file
->IsCompressed
&&
292 !msi_cabextract(package
, mi
, &data
))
294 ERR("Failed to extract cabinet: %s\n", debugstr_w(mi
->cabinet
));
295 rc
= ERROR_FUNCTION_FAILED
;
300 if (!file
->IsCompressed
)
302 LPWSTR source
= resolve_file_source(package
, file
);
304 TRACE("file paths %s to %s\n", debugstr_w(source
),
305 debugstr_w(file
->TargetPath
));
307 msi_file_update_ui(package
, file
, szInstallFiles
);
308 rc
= copy_install_file(package
, file
, source
);
309 if (rc
!= ERROR_SUCCESS
)
311 ERR("Failed to copy %s to %s (%d)\n", debugstr_w(source
),
312 debugstr_w(file
->TargetPath
), rc
);
313 rc
= ERROR_INSTALL_FAILURE
;
320 else if (file
->state
!= msifs_installed
)
322 ERR("compressed file wasn't extracted (%s)\n",
323 debugstr_w(file
->TargetPath
));
324 rc
= ERROR_INSTALL_FAILURE
;
329 msi_free_media_info(mi
);
333 #define is_dot_dir(x) ((x[0] == '.') && ((x[1] == 0) || ((x[1] == '.') && (x[2] == 0))))
344 static BOOL
msi_move_file(LPCWSTR source
, LPCWSTR dest
, int options
)
348 if (GetFileAttributesW(source
) == FILE_ATTRIBUTE_DIRECTORY
||
349 GetFileAttributesW(dest
) == FILE_ATTRIBUTE_DIRECTORY
)
351 WARN("Source or dest is directory, not moving\n");
355 if (options
== msidbMoveFileOptionsMove
)
357 TRACE("moving %s -> %s\n", debugstr_w(source
), debugstr_w(dest
));
358 ret
= MoveFileExW(source
, dest
, MOVEFILE_REPLACE_EXISTING
);
361 WARN("MoveFile failed: %d\n", GetLastError());
367 TRACE("copying %s -> %s\n", debugstr_w(source
), debugstr_w(dest
));
368 ret
= CopyFileW(source
, dest
, FALSE
);
371 WARN("CopyFile failed: %d\n", GetLastError());
379 static LPWSTR
wildcard_to_file(LPWSTR wildcard
, LPWSTR filename
)
382 DWORD dirlen
, pathlen
;
384 ptr
= strrchrW(wildcard
, '\\');
385 dirlen
= ptr
- wildcard
+ 1;
387 pathlen
= dirlen
+ lstrlenW(filename
) + 1;
388 path
= msi_alloc(pathlen
* sizeof(WCHAR
));
390 lstrcpynW(path
, wildcard
, dirlen
+ 1);
391 lstrcatW(path
, filename
);
396 static void free_file_entry(FILE_LIST
*file
)
398 msi_free(file
->source
);
399 msi_free(file
->dest
);
403 static void free_list(FILE_LIST
*list
)
405 while (!list_empty(&list
->entry
))
407 FILE_LIST
*file
= LIST_ENTRY(list_head(&list
->entry
), FILE_LIST
, entry
);
409 list_remove(&file
->entry
);
410 free_file_entry(file
);
414 static BOOL
add_wildcard(FILE_LIST
*files
, LPWSTR source
, LPWSTR dest
)
416 FILE_LIST
*new, *file
;
417 LPWSTR ptr
, filename
;
420 new = msi_alloc_zero(sizeof(FILE_LIST
));
424 new->source
= strdupW(source
);
425 ptr
= strrchrW(dest
, '\\') + 1;
426 filename
= strrchrW(new->source
, '\\') + 1;
428 new->sourcename
= filename
;
433 new->destname
= new->sourcename
;
435 size
= (ptr
- dest
) + lstrlenW(filename
) + 1;
436 new->dest
= msi_alloc(size
* sizeof(WCHAR
));
439 free_file_entry(new);
443 lstrcpynW(new->dest
, dest
, ptr
- dest
+ 1);
444 lstrcatW(new->dest
, filename
);
446 if (list_empty(&files
->entry
))
448 list_add_head(&files
->entry
, &new->entry
);
452 LIST_FOR_EACH_ENTRY(file
, &files
->entry
, FILE_LIST
, entry
)
454 if (lstrcmpW(source
, file
->source
) < 0)
456 list_add_before(&file
->entry
, &new->entry
);
461 list_add_after(&file
->entry
, &new->entry
);
465 static BOOL
move_files_wildcard(LPWSTR source
, LPWSTR dest
, int options
)
467 WIN32_FIND_DATAW wfd
;
471 FILE_LIST files
, *file
;
474 hfile
= FindFirstFileW(source
, &wfd
);
475 if (hfile
== INVALID_HANDLE_VALUE
) return FALSE
;
477 list_init(&files
.entry
);
479 for (res
= TRUE
; res
; res
= FindNextFileW(hfile
, &wfd
))
481 if (is_dot_dir(wfd
.cFileName
)) continue;
483 path
= wildcard_to_file(source
, wfd
.cFileName
);
490 add_wildcard(&files
, path
, dest
);
494 /* no files match the wildcard */
495 if (list_empty(&files
.entry
))
498 /* only the first wildcard match gets renamed to dest */
499 file
= LIST_ENTRY(list_head(&files
.entry
), FILE_LIST
, entry
);
500 size
= (strrchrW(file
->dest
, '\\') - file
->dest
) + lstrlenW(file
->destname
) + 2;
501 file
->dest
= msi_realloc(file
->dest
, size
* sizeof(WCHAR
));
508 /* file->dest may be shorter after the reallocation, so add a NULL
509 * terminator. This is needed for the call to strrchrW, as there will no
510 * longer be a NULL terminator within the bounds of the allocation in this case.
512 file
->dest
[size
- 1] = '\0';
513 lstrcpyW(strrchrW(file
->dest
, '\\') + 1, file
->destname
);
515 while (!list_empty(&files
.entry
))
517 file
= LIST_ENTRY(list_head(&files
.entry
), FILE_LIST
, entry
);
519 msi_move_file(file
->source
, file
->dest
, options
);
521 list_remove(&file
->entry
);
522 free_file_entry(file
);
533 static UINT
ITERATE_MoveFiles( MSIRECORD
*rec
, LPVOID param
)
535 MSIPACKAGE
*package
= param
;
538 LPCWSTR sourcename
, component
;
539 LPWSTR sourcedir
, destname
= NULL
, destdir
= NULL
, source
= NULL
, dest
= NULL
;
544 component
= MSI_RecordGetString(rec
, 2);
545 comp
= get_loaded_component(package
, component
);
547 return ERROR_SUCCESS
;
549 if (comp
->ActionRequest
!= INSTALLSTATE_LOCAL
&& comp
->ActionRequest
!= INSTALLSTATE_SOURCE
)
551 TRACE("Component not scheduled for installation: %s\n", debugstr_w(component
));
552 comp
->Action
= comp
->Installed
;
553 return ERROR_SUCCESS
;
555 comp
->Action
= comp
->ActionRequest
;
557 sourcename
= MSI_RecordGetString(rec
, 3);
558 options
= MSI_RecordGetInteger(rec
, 7);
560 sourcedir
= msi_dup_property(package
, MSI_RecordGetString(rec
, 5));
564 destdir
= msi_dup_property(package
, MSI_RecordGetString(rec
, 6));
570 if (GetFileAttributesW(sourcedir
) == INVALID_FILE_ATTRIBUTES
)
573 source
= strdupW(sourcedir
);
579 size
= lstrlenW(sourcedir
) + lstrlenW(sourcename
) + 2;
580 source
= msi_alloc(size
* sizeof(WCHAR
));
584 lstrcpyW(source
, sourcedir
);
585 if (source
[lstrlenW(source
) - 1] != '\\')
586 lstrcatW(source
, szBackSlash
);
587 lstrcatW(source
, sourcename
);
590 wildcards
= strchrW(source
, '*') || strchrW(source
, '?');
592 if (MSI_RecordIsNull(rec
, 4))
596 destname
= strdupW(sourcename
);
603 destname
= strdupW(MSI_RecordGetString(rec
, 4));
605 reduce_to_longfilename(destname
);
610 size
= lstrlenW(destname
);
612 size
+= lstrlenW(destdir
) + 2;
613 dest
= msi_alloc(size
* sizeof(WCHAR
));
617 lstrcpyW(dest
, destdir
);
618 if (dest
[lstrlenW(dest
) - 1] != '\\')
619 lstrcatW(dest
, szBackSlash
);
622 lstrcatW(dest
, destname
);
624 if (GetFileAttributesW(destdir
) == INVALID_FILE_ATTRIBUTES
)
626 ret
= CreateDirectoryW(destdir
, NULL
);
629 WARN("CreateDirectory failed: %d\n", GetLastError());
635 msi_move_file(source
, dest
, options
);
637 move_files_wildcard(source
, dest
, options
);
640 uirow
= MSI_CreateRecord( 9 );
641 MSI_RecordSetStringW( uirow
, 1, MSI_RecordGetString(rec
, 1) );
642 MSI_RecordSetInteger( uirow
, 6, 1 ); /* FIXME */
643 MSI_RecordSetStringW( uirow
, 9, destdir
);
644 ui_actiondata( package
, szMoveFiles
, uirow
);
645 msiobj_release( &uirow
->hdr
);
653 return ERROR_SUCCESS
;
656 UINT
ACTION_MoveFiles( MSIPACKAGE
*package
)
661 static const WCHAR ExecSeqQuery
[] =
662 {'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
663 '`','M','o','v','e','F','i','l','e','`',0};
665 rc
= MSI_DatabaseOpenViewW(package
->db
, ExecSeqQuery
, &view
);
666 if (rc
!= ERROR_SUCCESS
)
667 return ERROR_SUCCESS
;
669 rc
= MSI_IterateRecords(view
, NULL
, ITERATE_MoveFiles
, package
);
670 msiobj_release(&view
->hdr
);
675 static WCHAR
*get_duplicate_filename( MSIPACKAGE
*package
, MSIRECORD
*row
, const WCHAR
*file_key
, const WCHAR
*src
)
678 WCHAR
*dst_name
, *dst_path
, *dst
;
680 if (MSI_RecordIsNull( row
, 4 ))
682 len
= strlenW( src
) + 1;
683 if (!(dst_name
= msi_alloc( len
* sizeof(WCHAR
)))) return NULL
;
684 strcpyW( dst_name
, strrchrW( src
, '\\' ) + 1 );
688 MSI_RecordGetStringW( row
, 4, NULL
, &len
);
689 if (!(dst_name
= msi_alloc( ++len
* sizeof(WCHAR
) ))) return NULL
;
690 MSI_RecordGetStringW( row
, 4, dst_name
, &len
);
691 reduce_to_longfilename( dst_name
);
694 if (MSI_RecordIsNull( row
, 5 ))
697 dst_path
= strdupW( src
);
698 p
= strrchrW( dst_path
, '\\' );
703 const WCHAR
*dst_key
= MSI_RecordGetString( row
, 5 );
705 dst_path
= resolve_folder( package
, dst_key
, FALSE
, FALSE
, TRUE
, NULL
);
709 dst_path
= msi_dup_property( package
, dst_key
);
712 FIXME("Unable to get destination folder, try AppSearch properties\n");
713 msi_free( dst_name
);
719 dst
= build_directory_name( 2, dst_path
, dst_name
);
720 create_full_pathW( dst_path
);
722 msi_free( dst_name
);
723 msi_free( dst_path
);
727 static UINT
ITERATE_DuplicateFiles(MSIRECORD
*row
, LPVOID param
)
729 MSIPACKAGE
*package
= param
;
731 LPCWSTR file_key
, component
;
736 component
= MSI_RecordGetString(row
,2);
737 comp
= get_loaded_component(package
,component
);
739 return ERROR_SUCCESS
;
741 if (comp
->ActionRequest
!= INSTALLSTATE_LOCAL
)
743 TRACE("Component not scheduled for installation %s\n", debugstr_w(component
));
744 comp
->Action
= comp
->Installed
;
745 return ERROR_SUCCESS
;
747 comp
->Action
= INSTALLSTATE_LOCAL
;
749 file_key
= MSI_RecordGetString(row
,3);
752 ERR("Unable to get file key\n");
753 return ERROR_FUNCTION_FAILED
;
756 file
= get_loaded_file( package
, file_key
);
759 ERR("Original file unknown %s\n", debugstr_w(file_key
));
760 return ERROR_SUCCESS
;
763 dest
= get_duplicate_filename( package
, row
, file_key
, file
->TargetPath
);
766 WARN("Unable to get duplicate filename\n");
767 return ERROR_SUCCESS
;
770 TRACE("Duplicating file %s to %s\n", debugstr_w(file
->TargetPath
), debugstr_w(dest
));
772 if (!CopyFileW( file
->TargetPath
, dest
, TRUE
))
774 WARN("Failed to copy file %s -> %s (%u)\n",
775 debugstr_w(file
->TargetPath
), debugstr_w(dest
), GetLastError());
778 FIXME("We should track these duplicate files as well\n");
780 uirow
= MSI_CreateRecord( 9 );
781 MSI_RecordSetStringW( uirow
, 1, MSI_RecordGetString( row
, 1 ) );
782 MSI_RecordSetInteger( uirow
, 6, file
->FileSize
);
783 MSI_RecordSetStringW( uirow
, 9, MSI_RecordGetString( row
, 5 ) );
784 ui_actiondata( package
, szDuplicateFiles
, uirow
);
785 msiobj_release( &uirow
->hdr
);
788 return ERROR_SUCCESS
;
791 UINT
ACTION_DuplicateFiles(MSIPACKAGE
*package
)
795 static const WCHAR ExecSeqQuery
[] =
796 {'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
797 '`','D','u','p','l','i','c','a','t','e','F','i','l','e','`',0};
799 rc
= MSI_DatabaseOpenViewW(package
->db
, ExecSeqQuery
, &view
);
800 if (rc
!= ERROR_SUCCESS
)
801 return ERROR_SUCCESS
;
803 rc
= MSI_IterateRecords(view
, NULL
, ITERATE_DuplicateFiles
, package
);
804 msiobj_release(&view
->hdr
);
809 static UINT
ITERATE_RemoveDuplicateFiles( MSIRECORD
*row
, LPVOID param
)
811 MSIPACKAGE
*package
= param
;
813 LPCWSTR file_key
, component
;
818 component
= MSI_RecordGetString( row
, 2 );
819 comp
= get_loaded_component( package
, component
);
821 return ERROR_SUCCESS
;
823 if (comp
->ActionRequest
!= INSTALLSTATE_ABSENT
)
825 TRACE("Component not scheduled for removal %s\n", debugstr_w(component
));
826 comp
->Action
= comp
->Installed
;
827 return ERROR_SUCCESS
;
829 comp
->Action
= INSTALLSTATE_ABSENT
;
831 file_key
= MSI_RecordGetString( row
, 3 );
834 ERR("Unable to get file key\n");
835 return ERROR_FUNCTION_FAILED
;
838 file
= get_loaded_file( package
, file_key
);
841 ERR("Original file unknown %s\n", debugstr_w(file_key
));
842 return ERROR_SUCCESS
;
845 dest
= get_duplicate_filename( package
, row
, file_key
, file
->TargetPath
);
848 WARN("Unable to get duplicate filename\n");
849 return ERROR_SUCCESS
;
852 TRACE("Removing duplicate %s of %s\n", debugstr_w(dest
), debugstr_w(file
->TargetPath
));
854 if (!DeleteFileW( dest
))
856 WARN("Failed to delete duplicate file %s (%u)\n", debugstr_w(dest
), GetLastError());
859 uirow
= MSI_CreateRecord( 9 );
860 MSI_RecordSetStringW( uirow
, 1, MSI_RecordGetString( row
, 1 ) );
861 MSI_RecordSetStringW( uirow
, 9, MSI_RecordGetString( row
, 5 ) );
862 ui_actiondata( package
, szRemoveDuplicateFiles
, uirow
);
863 msiobj_release( &uirow
->hdr
);
866 return ERROR_SUCCESS
;
869 UINT
ACTION_RemoveDuplicateFiles( MSIPACKAGE
*package
)
873 static const WCHAR query
[] =
874 {'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
875 '`','D','u','p','l','i','c','a','t','e','F','i','l','e','`',0};
877 rc
= MSI_DatabaseOpenViewW( package
->db
, query
, &view
);
878 if (rc
!= ERROR_SUCCESS
)
879 return ERROR_SUCCESS
;
881 rc
= MSI_IterateRecords( view
, NULL
, ITERATE_RemoveDuplicateFiles
, package
);
882 msiobj_release( &view
->hdr
);
887 static BOOL
verify_comp_for_removal(MSICOMPONENT
*comp
, UINT install_mode
)
889 INSTALLSTATE request
= comp
->ActionRequest
;
891 if (request
== INSTALLSTATE_UNKNOWN
)
894 if (install_mode
== msidbRemoveFileInstallModeOnInstall
&&
895 (request
== INSTALLSTATE_LOCAL
|| request
== INSTALLSTATE_SOURCE
))
898 if (request
== INSTALLSTATE_ABSENT
)
900 if (!comp
->ComponentId
)
903 if (install_mode
== msidbRemoveFileInstallModeOnRemove
)
907 if (install_mode
== msidbRemoveFileInstallModeOnBoth
)
913 static UINT
ITERATE_RemoveFiles(MSIRECORD
*row
, LPVOID param
)
915 MSIPACKAGE
*package
= param
;
918 LPCWSTR component
, filename
, dirprop
;
920 LPWSTR dir
= NULL
, path
= NULL
;
924 component
= MSI_RecordGetString(row
, 2);
925 filename
= MSI_RecordGetString(row
, 3);
926 dirprop
= MSI_RecordGetString(row
, 4);
927 install_mode
= MSI_RecordGetInteger(row
, 5);
929 comp
= get_loaded_component(package
, component
);
932 ERR("Invalid component: %s\n", debugstr_w(component
));
933 return ERROR_FUNCTION_FAILED
;
936 if (!verify_comp_for_removal(comp
, install_mode
))
938 TRACE("Skipping removal due to missing conditions\n");
939 comp
->Action
= comp
->Installed
;
940 return ERROR_SUCCESS
;
943 dir
= msi_dup_property(package
, dirprop
);
945 return ERROR_OUTOFMEMORY
;
947 size
= (filename
!= NULL
) ? lstrlenW(filename
) : 0;
948 size
+= lstrlenW(dir
) + 2;
949 path
= msi_alloc(size
* sizeof(WCHAR
));
952 r
= ERROR_OUTOFMEMORY
;
959 PathAddBackslashW(path
);
960 lstrcatW(path
, filename
);
962 TRACE("Deleting misc file: %s\n", debugstr_w(path
));
967 TRACE("Removing misc directory: %s\n", debugstr_w(dir
));
968 RemoveDirectoryW(dir
);
972 uirow
= MSI_CreateRecord( 9 );
973 MSI_RecordSetStringW( uirow
, 1, MSI_RecordGetString(row
, 1) );
974 MSI_RecordSetStringW( uirow
, 9, dir
);
975 ui_actiondata( package
, szRemoveFiles
, uirow
);
976 msiobj_release( &uirow
->hdr
);
980 return ERROR_SUCCESS
;
983 UINT
ACTION_RemoveFiles( MSIPACKAGE
*package
)
989 static const WCHAR query
[] = {
990 'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
991 '`','R','e','m','o','v','e','F','i','l','e','`',0};
992 static const WCHAR folder_query
[] = {
993 'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
994 '`','C','r','e','a','t','e','F','o','l','d','e','r','`',0};
996 r
= MSI_DatabaseOpenViewW(package
->db
, query
, &view
);
997 if (r
== ERROR_SUCCESS
)
999 MSI_IterateRecords(view
, NULL
, ITERATE_RemoveFiles
, package
);
1000 msiobj_release(&view
->hdr
);
1003 r
= MSI_DatabaseOpenViewW(package
->db
, folder_query
, &view
);
1004 if (r
== ERROR_SUCCESS
)
1005 msiobj_release(&view
->hdr
);
1007 LIST_FOR_EACH_ENTRY( file
, &package
->files
, MSIFILE
, entry
)
1010 LPWSTR dir
, uipath
, p
;
1012 if ( file
->state
== msifs_installed
)
1013 ERR("removing installed file %s\n", debugstr_w(file
->TargetPath
));
1015 if ( file
->Component
->ActionRequest
!= INSTALLSTATE_ABSENT
||
1016 file
->Component
->Installed
== INSTALLSTATE_SOURCE
)
1019 /* don't remove a file if the old file
1020 * is strictly newer than the version to be installed
1022 if ( msi_compare_file_version( file
) < 0 )
1025 TRACE("removing %s\n", debugstr_w(file
->File
) );
1026 if (!DeleteFileW( file
->TargetPath
))
1028 WARN("failed to delete %s\n", debugstr_w(file
->TargetPath
));
1030 /* FIXME: check persistence for each directory */
1031 else if (r
&& (dir
= strdupW( file
->TargetPath
)))
1033 if ((p
= strrchrW( dir
, '\\' ))) *p
= 0;
1034 RemoveDirectoryW( dir
);
1037 file
->state
= msifs_missing
;
1040 uirow
= MSI_CreateRecord( 9 );
1041 MSI_RecordSetStringW( uirow
, 1, file
->FileName
);
1042 uipath
= strdupW( file
->TargetPath
);
1043 p
= strrchrW(uipath
,'\\');
1046 MSI_RecordSetStringW( uirow
, 9, uipath
);
1047 ui_actiondata( package
, szRemoveFiles
, uirow
);
1048 msiobj_release( &uirow
->hdr
);
1050 /* FIXME: call ui_progress here? */
1053 return ERROR_SUCCESS
;