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
installfiles_cb(MSIPACKAGE
*package
, LPCWSTR file
, DWORD action
,
179 LPWSTR
*path
, DWORD
*attrs
, PVOID user
)
181 static MSIFILE
*f
= NULL
;
183 if (action
== MSICABEXTRACT_BEGINEXTRACT
)
185 f
= get_loaded_file(package
, file
);
188 WARN("unknown file in cabinet (%s)\n", debugstr_w(file
));
192 if (f
->state
!= msifs_missing
&& f
->state
!= msifs_overwrite
)
194 TRACE("Skipping extraction of %s\n", debugstr_w(file
));
198 msi_file_update_ui(package
, f
, szInstallFiles
);
200 *path
= strdupW(f
->TargetPath
);
201 *attrs
= f
->Attributes
;
203 else if (action
== MSICABEXTRACT_FILEEXTRACTED
)
205 f
->state
= msifs_installed
;
213 * ACTION_InstallFiles()
215 * For efficiency, this is done in two passes:
216 * 1) Correct all the TargetPaths and determine what files are to be installed.
217 * 2) Extract Cabinets and copy files.
219 UINT
ACTION_InstallFiles(MSIPACKAGE
*package
)
222 UINT rc
= ERROR_SUCCESS
;
225 /* increment progress bar each time action data is sent */
226 ui_progress(package
,1,1,0,0);
228 schedule_install_files(package
);
231 * Despite MSDN specifying that the CreateFolders action
232 * should be called before InstallFiles, some installers don't
233 * do that, and they seem to work correctly. We need to create
234 * directories here to make sure that the files can be copied.
236 msi_create_component_directories( package
);
238 mi
= msi_alloc_zero( sizeof(MSIMEDIAINFO
) );
240 LIST_FOR_EACH_ENTRY( file
, &package
->files
, MSIFILE
, entry
)
242 if (file
->state
!= msifs_missing
&& !mi
->is_continuous
&& file
->state
!= msifs_overwrite
)
245 if (file
->Sequence
> mi
->last_sequence
|| mi
->is_continuous
||
246 (file
->IsCompressed
&& !mi
->is_extracted
))
250 rc
= ready_media(package
, file
, mi
);
251 if (rc
!= ERROR_SUCCESS
)
253 ERR("Failed to ready media\n");
258 data
.package
= package
;
259 data
.cb
= installfiles_cb
;
262 if (file
->IsCompressed
&&
263 !msi_cabextract(package
, mi
, &data
))
265 ERR("Failed to extract cabinet: %s\n", debugstr_w(mi
->cabinet
));
266 rc
= ERROR_INSTALL_FAILURE
;
271 if (!file
->IsCompressed
)
273 LPWSTR source
= resolve_file_source(package
, file
);
275 TRACE("file paths %s to %s\n", debugstr_w(source
),
276 debugstr_w(file
->TargetPath
));
278 msi_file_update_ui(package
, file
, szInstallFiles
);
279 rc
= copy_install_file(package
, file
, source
);
280 if (rc
!= ERROR_SUCCESS
)
282 ERR("Failed to copy %s to %s (%d)\n", debugstr_w(source
),
283 debugstr_w(file
->TargetPath
), rc
);
284 rc
= ERROR_INSTALL_FAILURE
;
291 else if (file
->state
!= msifs_installed
)
293 ERR("compressed file wasn't extracted (%s)\n",
294 debugstr_w(file
->TargetPath
));
295 rc
= ERROR_INSTALL_FAILURE
;
300 msi_free_media_info(mi
);
304 #define is_dot_dir(x) ((x[0] == '.') && ((x[1] == 0) || ((x[1] == '.') && (x[2] == 0))))
315 static BOOL
msi_move_file(LPCWSTR source
, LPCWSTR dest
, int options
)
319 if (GetFileAttributesW(source
) == FILE_ATTRIBUTE_DIRECTORY
||
320 GetFileAttributesW(dest
) == FILE_ATTRIBUTE_DIRECTORY
)
322 WARN("Source or dest is directory, not moving\n");
326 if (options
== msidbMoveFileOptionsMove
)
328 TRACE("moving %s -> %s\n", debugstr_w(source
), debugstr_w(dest
));
329 ret
= MoveFileExW(source
, dest
, MOVEFILE_REPLACE_EXISTING
);
332 WARN("MoveFile failed: %d\n", GetLastError());
338 TRACE("copying %s -> %s\n", debugstr_w(source
), debugstr_w(dest
));
339 ret
= CopyFileW(source
, dest
, FALSE
);
342 WARN("CopyFile failed: %d\n", GetLastError());
350 static LPWSTR
wildcard_to_file(LPWSTR wildcard
, LPWSTR filename
)
353 DWORD dirlen
, pathlen
;
355 ptr
= strrchrW(wildcard
, '\\');
356 dirlen
= ptr
- wildcard
+ 1;
358 pathlen
= dirlen
+ lstrlenW(filename
) + 1;
359 path
= msi_alloc(pathlen
* sizeof(WCHAR
));
361 lstrcpynW(path
, wildcard
, dirlen
+ 1);
362 lstrcatW(path
, filename
);
367 static void free_file_entry(FILE_LIST
*file
)
369 msi_free(file
->source
);
370 msi_free(file
->dest
);
374 static void free_list(FILE_LIST
*list
)
376 while (!list_empty(&list
->entry
))
378 FILE_LIST
*file
= LIST_ENTRY(list_head(&list
->entry
), FILE_LIST
, entry
);
380 list_remove(&file
->entry
);
381 free_file_entry(file
);
385 static BOOL
add_wildcard(FILE_LIST
*files
, LPWSTR source
, LPWSTR dest
)
387 FILE_LIST
*new, *file
;
388 LPWSTR ptr
, filename
;
391 new = msi_alloc_zero(sizeof(FILE_LIST
));
395 new->source
= strdupW(source
);
396 ptr
= strrchrW(dest
, '\\') + 1;
397 filename
= strrchrW(new->source
, '\\') + 1;
399 new->sourcename
= filename
;
404 new->destname
= new->sourcename
;
406 size
= (ptr
- dest
) + lstrlenW(filename
) + 1;
407 new->dest
= msi_alloc(size
* sizeof(WCHAR
));
410 free_file_entry(new);
414 lstrcpynW(new->dest
, dest
, ptr
- dest
+ 1);
415 lstrcatW(new->dest
, filename
);
417 if (list_empty(&files
->entry
))
419 list_add_head(&files
->entry
, &new->entry
);
423 LIST_FOR_EACH_ENTRY(file
, &files
->entry
, FILE_LIST
, entry
)
425 if (lstrcmpW(source
, file
->source
) < 0)
427 list_add_before(&file
->entry
, &new->entry
);
432 list_add_after(&file
->entry
, &new->entry
);
436 static BOOL
move_files_wildcard(LPWSTR source
, LPWSTR dest
, int options
)
438 WIN32_FIND_DATAW wfd
;
442 FILE_LIST files
, *file
;
445 hfile
= FindFirstFileW(source
, &wfd
);
446 if (hfile
== INVALID_HANDLE_VALUE
) return FALSE
;
448 list_init(&files
.entry
);
450 for (res
= TRUE
; res
; res
= FindNextFileW(hfile
, &wfd
))
452 if (is_dot_dir(wfd
.cFileName
)) continue;
454 path
= wildcard_to_file(source
, wfd
.cFileName
);
461 add_wildcard(&files
, path
, dest
);
465 /* no files match the wildcard */
466 if (list_empty(&files
.entry
))
469 /* only the first wildcard match gets renamed to dest */
470 file
= LIST_ENTRY(list_head(&files
.entry
), FILE_LIST
, entry
);
471 size
= (strrchrW(file
->dest
, '\\') - file
->dest
) + lstrlenW(file
->destname
) + 2;
472 file
->dest
= msi_realloc(file
->dest
, size
* sizeof(WCHAR
));
479 /* file->dest may be shorter after the reallocation, so add a NULL
480 * terminator. This is needed for the call to strrchrW, as there will no
481 * longer be a NULL terminator within the bounds of the allocation in this case.
483 file
->dest
[size
- 1] = '\0';
484 lstrcpyW(strrchrW(file
->dest
, '\\') + 1, file
->destname
);
486 while (!list_empty(&files
.entry
))
488 file
= LIST_ENTRY(list_head(&files
.entry
), FILE_LIST
, entry
);
490 msi_move_file(file
->source
, file
->dest
, options
);
492 list_remove(&file
->entry
);
493 free_file_entry(file
);
504 static UINT
ITERATE_MoveFiles( MSIRECORD
*rec
, LPVOID param
)
506 MSIPACKAGE
*package
= param
;
509 LPCWSTR sourcename
, component
;
510 LPWSTR sourcedir
, destname
= NULL
, destdir
= NULL
, source
= NULL
, dest
= NULL
;
515 component
= MSI_RecordGetString(rec
, 2);
516 comp
= get_loaded_component(package
, component
);
518 return ERROR_SUCCESS
;
520 if (comp
->ActionRequest
!= INSTALLSTATE_LOCAL
&& comp
->ActionRequest
!= INSTALLSTATE_SOURCE
)
522 TRACE("Component not scheduled for installation: %s\n", debugstr_w(component
));
523 comp
->Action
= comp
->Installed
;
524 return ERROR_SUCCESS
;
526 comp
->Action
= comp
->ActionRequest
;
528 sourcename
= MSI_RecordGetString(rec
, 3);
529 options
= MSI_RecordGetInteger(rec
, 7);
531 sourcedir
= msi_dup_property(package
->db
, MSI_RecordGetString(rec
, 5));
535 destdir
= msi_dup_property(package
->db
, MSI_RecordGetString(rec
, 6));
541 if (GetFileAttributesW(sourcedir
) == INVALID_FILE_ATTRIBUTES
)
544 source
= strdupW(sourcedir
);
550 size
= lstrlenW(sourcedir
) + lstrlenW(sourcename
) + 2;
551 source
= msi_alloc(size
* sizeof(WCHAR
));
555 lstrcpyW(source
, sourcedir
);
556 if (source
[lstrlenW(source
) - 1] != '\\')
557 lstrcatW(source
, szBackSlash
);
558 lstrcatW(source
, sourcename
);
561 wildcards
= strchrW(source
, '*') || strchrW(source
, '?');
563 if (MSI_RecordIsNull(rec
, 4))
567 destname
= strdupW(sourcename
);
574 destname
= strdupW(MSI_RecordGetString(rec
, 4));
576 reduce_to_longfilename(destname
);
581 size
= lstrlenW(destname
);
583 size
+= lstrlenW(destdir
) + 2;
584 dest
= msi_alloc(size
* sizeof(WCHAR
));
588 lstrcpyW(dest
, destdir
);
589 if (dest
[lstrlenW(dest
) - 1] != '\\')
590 lstrcatW(dest
, szBackSlash
);
593 lstrcatW(dest
, destname
);
595 if (GetFileAttributesW(destdir
) == INVALID_FILE_ATTRIBUTES
)
597 ret
= CreateDirectoryW(destdir
, NULL
);
600 WARN("CreateDirectory failed: %d\n", GetLastError());
606 msi_move_file(source
, dest
, options
);
608 move_files_wildcard(source
, dest
, options
);
611 uirow
= MSI_CreateRecord( 9 );
612 MSI_RecordSetStringW( uirow
, 1, MSI_RecordGetString(rec
, 1) );
613 MSI_RecordSetInteger( uirow
, 6, 1 ); /* FIXME */
614 MSI_RecordSetStringW( uirow
, 9, destdir
);
615 ui_actiondata( package
, szMoveFiles
, uirow
);
616 msiobj_release( &uirow
->hdr
);
624 return ERROR_SUCCESS
;
627 UINT
ACTION_MoveFiles( MSIPACKAGE
*package
)
632 static const WCHAR ExecSeqQuery
[] =
633 {'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
634 '`','M','o','v','e','F','i','l','e','`',0};
636 rc
= MSI_DatabaseOpenViewW(package
->db
, ExecSeqQuery
, &view
);
637 if (rc
!= ERROR_SUCCESS
)
638 return ERROR_SUCCESS
;
640 rc
= MSI_IterateRecords(view
, NULL
, ITERATE_MoveFiles
, package
);
641 msiobj_release(&view
->hdr
);
646 static WCHAR
*get_duplicate_filename( MSIPACKAGE
*package
, MSIRECORD
*row
, const WCHAR
*file_key
, const WCHAR
*src
)
649 WCHAR
*dst_name
, *dst_path
, *dst
;
651 if (MSI_RecordIsNull( row
, 4 ))
653 len
= strlenW( src
) + 1;
654 if (!(dst_name
= msi_alloc( len
* sizeof(WCHAR
)))) return NULL
;
655 strcpyW( dst_name
, strrchrW( src
, '\\' ) + 1 );
659 MSI_RecordGetStringW( row
, 4, NULL
, &len
);
660 if (!(dst_name
= msi_alloc( ++len
* sizeof(WCHAR
) ))) return NULL
;
661 MSI_RecordGetStringW( row
, 4, dst_name
, &len
);
662 reduce_to_longfilename( dst_name
);
665 if (MSI_RecordIsNull( row
, 5 ))
668 dst_path
= strdupW( src
);
669 p
= strrchrW( dst_path
, '\\' );
674 const WCHAR
*dst_key
= MSI_RecordGetString( row
, 5 );
676 dst_path
= resolve_folder( package
, dst_key
, FALSE
, FALSE
, TRUE
, NULL
);
680 dst_path
= msi_dup_property( package
->db
, dst_key
);
683 FIXME("Unable to get destination folder, try AppSearch properties\n");
684 msi_free( dst_name
);
690 dst
= build_directory_name( 2, dst_path
, dst_name
);
691 create_full_pathW( dst_path
);
693 msi_free( dst_name
);
694 msi_free( dst_path
);
698 static UINT
ITERATE_DuplicateFiles(MSIRECORD
*row
, LPVOID param
)
700 MSIPACKAGE
*package
= param
;
702 LPCWSTR file_key
, component
;
707 component
= MSI_RecordGetString(row
,2);
708 comp
= get_loaded_component(package
,component
);
710 return ERROR_SUCCESS
;
712 if (comp
->ActionRequest
!= INSTALLSTATE_LOCAL
)
714 TRACE("Component not scheduled for installation %s\n", debugstr_w(component
));
715 comp
->Action
= comp
->Installed
;
716 return ERROR_SUCCESS
;
718 comp
->Action
= INSTALLSTATE_LOCAL
;
720 file_key
= MSI_RecordGetString(row
,3);
723 ERR("Unable to get file key\n");
724 return ERROR_FUNCTION_FAILED
;
727 file
= get_loaded_file( package
, file_key
);
730 ERR("Original file unknown %s\n", debugstr_w(file_key
));
731 return ERROR_SUCCESS
;
734 dest
= get_duplicate_filename( package
, row
, file_key
, file
->TargetPath
);
737 WARN("Unable to get duplicate filename\n");
738 return ERROR_SUCCESS
;
741 TRACE("Duplicating file %s to %s\n", debugstr_w(file
->TargetPath
), debugstr_w(dest
));
743 if (!CopyFileW( file
->TargetPath
, dest
, TRUE
))
745 WARN("Failed to copy file %s -> %s (%u)\n",
746 debugstr_w(file
->TargetPath
), debugstr_w(dest
), GetLastError());
749 FIXME("We should track these duplicate files as well\n");
751 uirow
= MSI_CreateRecord( 9 );
752 MSI_RecordSetStringW( uirow
, 1, MSI_RecordGetString( row
, 1 ) );
753 MSI_RecordSetInteger( uirow
, 6, file
->FileSize
);
754 MSI_RecordSetStringW( uirow
, 9, MSI_RecordGetString( row
, 5 ) );
755 ui_actiondata( package
, szDuplicateFiles
, uirow
);
756 msiobj_release( &uirow
->hdr
);
759 return ERROR_SUCCESS
;
762 UINT
ACTION_DuplicateFiles(MSIPACKAGE
*package
)
766 static const WCHAR ExecSeqQuery
[] =
767 {'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
768 '`','D','u','p','l','i','c','a','t','e','F','i','l','e','`',0};
770 rc
= MSI_DatabaseOpenViewW(package
->db
, ExecSeqQuery
, &view
);
771 if (rc
!= ERROR_SUCCESS
)
772 return ERROR_SUCCESS
;
774 rc
= MSI_IterateRecords(view
, NULL
, ITERATE_DuplicateFiles
, package
);
775 msiobj_release(&view
->hdr
);
780 static UINT
ITERATE_RemoveDuplicateFiles( MSIRECORD
*row
, LPVOID param
)
782 MSIPACKAGE
*package
= param
;
784 LPCWSTR file_key
, component
;
789 component
= MSI_RecordGetString( row
, 2 );
790 comp
= get_loaded_component( package
, component
);
792 return ERROR_SUCCESS
;
794 if (comp
->ActionRequest
!= INSTALLSTATE_ABSENT
)
796 TRACE("Component not scheduled for removal %s\n", debugstr_w(component
));
797 comp
->Action
= comp
->Installed
;
798 return ERROR_SUCCESS
;
800 comp
->Action
= INSTALLSTATE_ABSENT
;
802 file_key
= MSI_RecordGetString( row
, 3 );
805 ERR("Unable to get file key\n");
806 return ERROR_FUNCTION_FAILED
;
809 file
= get_loaded_file( package
, file_key
);
812 ERR("Original file unknown %s\n", debugstr_w(file_key
));
813 return ERROR_SUCCESS
;
816 dest
= get_duplicate_filename( package
, row
, file_key
, file
->TargetPath
);
819 WARN("Unable to get duplicate filename\n");
820 return ERROR_SUCCESS
;
823 TRACE("Removing duplicate %s of %s\n", debugstr_w(dest
), debugstr_w(file
->TargetPath
));
825 if (!DeleteFileW( dest
))
827 WARN("Failed to delete duplicate file %s (%u)\n", debugstr_w(dest
), GetLastError());
830 uirow
= MSI_CreateRecord( 9 );
831 MSI_RecordSetStringW( uirow
, 1, MSI_RecordGetString( row
, 1 ) );
832 MSI_RecordSetStringW( uirow
, 9, MSI_RecordGetString( row
, 5 ) );
833 ui_actiondata( package
, szRemoveDuplicateFiles
, uirow
);
834 msiobj_release( &uirow
->hdr
);
837 return ERROR_SUCCESS
;
840 UINT
ACTION_RemoveDuplicateFiles( MSIPACKAGE
*package
)
844 static const WCHAR query
[] =
845 {'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
846 '`','D','u','p','l','i','c','a','t','e','F','i','l','e','`',0};
848 rc
= MSI_DatabaseOpenViewW( package
->db
, query
, &view
);
849 if (rc
!= ERROR_SUCCESS
)
850 return ERROR_SUCCESS
;
852 rc
= MSI_IterateRecords( view
, NULL
, ITERATE_RemoveDuplicateFiles
, package
);
853 msiobj_release( &view
->hdr
);
858 static BOOL
verify_comp_for_removal(MSICOMPONENT
*comp
, UINT install_mode
)
860 INSTALLSTATE request
= comp
->ActionRequest
;
862 if (request
== INSTALLSTATE_UNKNOWN
)
865 if (install_mode
== msidbRemoveFileInstallModeOnInstall
&&
866 (request
== INSTALLSTATE_LOCAL
|| request
== INSTALLSTATE_SOURCE
))
869 if (request
== INSTALLSTATE_ABSENT
)
871 if (!comp
->ComponentId
)
874 if (install_mode
== msidbRemoveFileInstallModeOnRemove
)
878 if (install_mode
== msidbRemoveFileInstallModeOnBoth
)
884 static UINT
ITERATE_RemoveFiles(MSIRECORD
*row
, LPVOID param
)
886 MSIPACKAGE
*package
= param
;
889 LPCWSTR component
, filename
, dirprop
;
891 LPWSTR dir
= NULL
, path
= NULL
;
895 component
= MSI_RecordGetString(row
, 2);
896 filename
= MSI_RecordGetString(row
, 3);
897 dirprop
= MSI_RecordGetString(row
, 4);
898 install_mode
= MSI_RecordGetInteger(row
, 5);
900 comp
= get_loaded_component(package
, component
);
903 ERR("Invalid component: %s\n", debugstr_w(component
));
904 return ERROR_FUNCTION_FAILED
;
907 if (!verify_comp_for_removal(comp
, install_mode
))
909 TRACE("Skipping removal due to missing conditions\n");
910 comp
->Action
= comp
->Installed
;
911 return ERROR_SUCCESS
;
914 dir
= msi_dup_property(package
->db
, dirprop
);
916 return ERROR_OUTOFMEMORY
;
918 size
= (filename
!= NULL
) ? lstrlenW(filename
) : 0;
919 size
+= lstrlenW(dir
) + 2;
920 path
= msi_alloc(size
* sizeof(WCHAR
));
923 r
= ERROR_OUTOFMEMORY
;
930 PathAddBackslashW(path
);
931 lstrcatW(path
, filename
);
933 TRACE("Deleting misc file: %s\n", debugstr_w(path
));
938 TRACE("Removing misc directory: %s\n", debugstr_w(dir
));
939 RemoveDirectoryW(dir
);
943 uirow
= MSI_CreateRecord( 9 );
944 MSI_RecordSetStringW( uirow
, 1, MSI_RecordGetString(row
, 1) );
945 MSI_RecordSetStringW( uirow
, 9, dir
);
946 ui_actiondata( package
, szRemoveFiles
, uirow
);
947 msiobj_release( &uirow
->hdr
);
951 return ERROR_SUCCESS
;
954 UINT
ACTION_RemoveFiles( MSIPACKAGE
*package
)
960 static const WCHAR query
[] = {
961 'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
962 '`','R','e','m','o','v','e','F','i','l','e','`',0};
963 static const WCHAR folder_query
[] = {
964 'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
965 '`','C','r','e','a','t','e','F','o','l','d','e','r','`',0};
967 r
= MSI_DatabaseOpenViewW(package
->db
, query
, &view
);
968 if (r
== ERROR_SUCCESS
)
970 MSI_IterateRecords(view
, NULL
, ITERATE_RemoveFiles
, package
);
971 msiobj_release(&view
->hdr
);
974 r
= MSI_DatabaseOpenViewW(package
->db
, folder_query
, &view
);
975 if (r
== ERROR_SUCCESS
)
976 msiobj_release(&view
->hdr
);
978 LIST_FOR_EACH_ENTRY( file
, &package
->files
, MSIFILE
, entry
)
981 LPWSTR dir
, uipath
, p
;
983 if ( file
->state
== msifs_installed
)
984 ERR("removing installed file %s\n", debugstr_w(file
->TargetPath
));
986 if ( file
->Component
->ActionRequest
!= INSTALLSTATE_ABSENT
||
987 file
->Component
->Installed
== INSTALLSTATE_SOURCE
)
990 /* don't remove a file if the old file
991 * is strictly newer than the version to be installed
993 if ( msi_compare_file_version( file
) < 0 )
996 TRACE("removing %s\n", debugstr_w(file
->File
) );
997 if (!DeleteFileW( file
->TargetPath
))
999 WARN("failed to delete %s\n", debugstr_w(file
->TargetPath
));
1001 /* FIXME: check persistence for each directory */
1002 else if (r
&& (dir
= strdupW( file
->TargetPath
)))
1004 if ((p
= strrchrW( dir
, '\\' ))) *p
= 0;
1005 RemoveDirectoryW( dir
);
1008 file
->state
= msifs_missing
;
1011 uirow
= MSI_CreateRecord( 9 );
1012 MSI_RecordSetStringW( uirow
, 1, file
->FileName
);
1013 uipath
= strdupW( file
->TargetPath
);
1014 p
= strrchrW(uipath
,'\\');
1017 MSI_RecordSetStringW( uirow
, 9, uipath
);
1018 ui_actiondata( package
, szRemoveFiles
, uirow
);
1019 msiobj_release( &uirow
->hdr
);
1021 /* FIXME: call ui_progress here? */
1024 return ERROR_SUCCESS
;