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
)
54 uirow
= MSI_CreateRecord( 9 );
55 MSI_RecordSetStringW( uirow
, 1, f
->FileName
);
56 MSI_RecordSetStringW( uirow
, 9, f
->Component
->Directory
);
57 MSI_RecordSetInteger( uirow
, 6, f
->FileSize
);
58 ui_actiondata( package
, action
, uirow
);
59 msiobj_release( &uirow
->hdr
);
60 ui_progress( package
, 2, f
->FileSize
, 0, 0 );
63 static void schedule_install_files(MSIPACKAGE
*package
)
67 LIST_FOR_EACH_ENTRY(file
, &package
->files
, MSIFILE
, entry
)
69 if (file
->Component
->ActionRequest
!= INSTALLSTATE_LOCAL
|| !file
->Component
->Enabled
)
71 TRACE("File %s is not scheduled for install\n", debugstr_w(file
->File
));
73 ui_progress(package
,2,file
->FileSize
,0,0);
74 file
->state
= msifs_skipped
;
77 file
->Component
->Action
= INSTALLSTATE_LOCAL
;
81 static UINT
copy_file(MSIFILE
*file
, LPWSTR source
)
85 ret
= CopyFileW(source
, file
->TargetPath
, FALSE
);
87 return GetLastError();
89 SetFileAttributesW(file
->TargetPath
, FILE_ATTRIBUTE_NORMAL
);
91 file
->state
= msifs_installed
;
95 static UINT
copy_install_file(MSIPACKAGE
*package
, MSIFILE
*file
, LPWSTR source
)
99 TRACE("Copying %s to %s\n", debugstr_w(source
), debugstr_w(file
->TargetPath
));
101 gle
= copy_file(file
, source
);
102 if (gle
== ERROR_SUCCESS
)
105 if (gle
== ERROR_ALREADY_EXISTS
&& file
->state
== msifs_overwrite
)
107 TRACE("overwriting existing file\n");
108 return ERROR_SUCCESS
;
110 else if (gle
== ERROR_ACCESS_DENIED
)
112 SetFileAttributesW(file
->TargetPath
, FILE_ATTRIBUTE_NORMAL
);
114 gle
= copy_file(file
, source
);
115 TRACE("Overwriting existing file: %d\n", gle
);
117 if (gle
== ERROR_SHARING_VIOLATION
|| gle
== ERROR_USER_MAPPED_FILE
)
119 WCHAR tmpfileW
[MAX_PATH
], *pathW
, *p
;
122 TRACE("file in use, scheduling rename operation\n");
124 GetTempFileNameW(szBackSlash
, szMsi
, 0, tmpfileW
);
125 len
= strlenW(file
->TargetPath
) + strlenW(tmpfileW
) + 1;
126 if (!(pathW
= msi_alloc(len
* sizeof(WCHAR
))))
127 return ERROR_OUTOFMEMORY
;
129 strcpyW(pathW
, file
->TargetPath
);
130 if ((p
= strrchrW(pathW
, '\\'))) *p
= 0;
131 strcatW(pathW
, tmpfileW
);
133 if (CopyFileW(source
, pathW
, FALSE
) &&
134 MoveFileExW(file
->TargetPath
, NULL
, MOVEFILE_DELAY_UNTIL_REBOOT
) &&
135 MoveFileExW(pathW
, file
->TargetPath
, MOVEFILE_DELAY_UNTIL_REBOOT
))
137 file
->state
= msifs_installed
;
138 package
->need_reboot
= 1;
143 gle
= GetLastError();
144 WARN("failed to schedule rename operation: %d)\n", gle
);
152 static UINT
msi_create_directory( MSIPACKAGE
*package
, const WCHAR
*dir
)
157 install_path
= resolve_folder( package
, dir
, FALSE
, FALSE
, TRUE
, &folder
);
159 return ERROR_FUNCTION_FAILED
;
161 if (folder
->State
== 0)
163 create_full_pathW( install_path
);
166 msi_free( install_path
);
167 return ERROR_SUCCESS
;
170 static BOOL
installfiles_cb(MSIPACKAGE
*package
, LPCWSTR file
, DWORD action
,
171 LPWSTR
*path
, DWORD
*attrs
, PVOID user
)
173 static MSIFILE
*f
= NULL
;
174 UINT_PTR disk_id
= (UINT_PTR
)user
;
176 if (action
== MSICABEXTRACT_BEGINEXTRACT
)
178 f
= get_loaded_file(package
, file
);
181 WARN("unknown file in cabinet (%s)\n", debugstr_w(file
));
185 if (f
->disk_id
!= disk_id
|| (f
->state
!= msifs_missing
&& f
->state
!= msifs_overwrite
))
188 msi_file_update_ui(package
, f
, szInstallFiles
);
189 if (!f
->Component
->assembly
)
190 msi_create_directory(package
, f
->Component
->Directory
);
192 *path
= strdupW(f
->TargetPath
);
193 *attrs
= f
->Attributes
;
195 else if (action
== MSICABEXTRACT_FILEEXTRACTED
)
197 f
->state
= msifs_installed
;
205 * ACTION_InstallFiles()
207 * For efficiency, this is done in two passes:
208 * 1) Correct all the TargetPaths and determine what files are to be installed.
209 * 2) Extract Cabinets and copy files.
211 UINT
ACTION_InstallFiles(MSIPACKAGE
*package
)
215 UINT rc
= ERROR_SUCCESS
;
218 /* increment progress bar each time action data is sent */
219 ui_progress(package
,1,1,0,0);
221 schedule_install_files(package
);
223 mi
= msi_alloc_zero( sizeof(MSIMEDIAINFO
) );
225 LIST_FOR_EACH_ENTRY( file
, &package
->files
, MSIFILE
, entry
)
227 if (file
->state
!= msifs_missing
&& !mi
->is_continuous
&& file
->state
!= msifs_overwrite
)
230 if (file
->Sequence
> mi
->last_sequence
|| mi
->is_continuous
||
231 (file
->IsCompressed
&& !mi
->is_extracted
))
235 rc
= ready_media(package
, file
, mi
);
236 if (rc
!= ERROR_SUCCESS
)
238 ERR("Failed to ready media for %s\n", debugstr_w(file
->File
));
243 data
.package
= package
;
244 data
.cb
= installfiles_cb
;
245 data
.user
= (PVOID
)(UINT_PTR
)mi
->disk_id
;
247 if (file
->IsCompressed
&&
248 !msi_cabextract(package
, mi
, &data
))
250 ERR("Failed to extract cabinet: %s\n", debugstr_w(mi
->cabinet
));
251 rc
= ERROR_INSTALL_FAILURE
;
256 if (!file
->IsCompressed
)
258 LPWSTR source
= resolve_file_source(package
, file
);
260 TRACE("copying %s to %s\n", debugstr_w(source
), debugstr_w(file
->TargetPath
));
262 msi_file_update_ui(package
, file
, szInstallFiles
);
263 if (!file
->Component
->assembly
)
264 msi_create_directory(package
, file
->Component
->Directory
);
266 rc
= copy_install_file(package
, file
, source
);
267 if (rc
!= ERROR_SUCCESS
)
269 ERR("Failed to copy %s to %s (%d)\n", debugstr_w(source
),
270 debugstr_w(file
->TargetPath
), rc
);
271 rc
= ERROR_INSTALL_FAILURE
;
277 else if (file
->state
!= msifs_installed
)
279 ERR("compressed file wasn't installed (%s)\n", debugstr_w(file
->TargetPath
));
280 rc
= ERROR_INSTALL_FAILURE
;
284 LIST_FOR_EACH_ENTRY( comp
, &package
->components
, MSICOMPONENT
, entry
)
286 if (comp
->Enabled
&& comp
->assembly
&& !comp
->assembly
->installed
)
288 rc
= install_assembly( package
, comp
);
289 if (rc
!= ERROR_SUCCESS
)
291 ERR("Failed to install assembly\n");
292 rc
= ERROR_INSTALL_FAILURE
;
299 msi_free_media_info(mi
);
303 #define is_dot_dir(x) ((x[0] == '.') && ((x[1] == 0) || ((x[1] == '.') && (x[2] == 0))))
314 static BOOL
msi_move_file(LPCWSTR source
, LPCWSTR dest
, int options
)
318 if (GetFileAttributesW(source
) == FILE_ATTRIBUTE_DIRECTORY
||
319 GetFileAttributesW(dest
) == FILE_ATTRIBUTE_DIRECTORY
)
321 WARN("Source or dest is directory, not moving\n");
325 if (options
== msidbMoveFileOptionsMove
)
327 TRACE("moving %s -> %s\n", debugstr_w(source
), debugstr_w(dest
));
328 ret
= MoveFileExW(source
, dest
, MOVEFILE_REPLACE_EXISTING
);
331 WARN("MoveFile failed: %d\n", GetLastError());
337 TRACE("copying %s -> %s\n", debugstr_w(source
), debugstr_w(dest
));
338 ret
= CopyFileW(source
, dest
, FALSE
);
341 WARN("CopyFile failed: %d\n", GetLastError());
349 static LPWSTR
wildcard_to_file(LPWSTR wildcard
, LPWSTR filename
)
352 DWORD dirlen
, pathlen
;
354 ptr
= strrchrW(wildcard
, '\\');
355 dirlen
= ptr
- wildcard
+ 1;
357 pathlen
= dirlen
+ lstrlenW(filename
) + 1;
358 path
= msi_alloc(pathlen
* sizeof(WCHAR
));
360 lstrcpynW(path
, wildcard
, dirlen
+ 1);
361 lstrcatW(path
, filename
);
366 static void free_file_entry(FILE_LIST
*file
)
368 msi_free(file
->source
);
369 msi_free(file
->dest
);
373 static void free_list(FILE_LIST
*list
)
375 while (!list_empty(&list
->entry
))
377 FILE_LIST
*file
= LIST_ENTRY(list_head(&list
->entry
), FILE_LIST
, entry
);
379 list_remove(&file
->entry
);
380 free_file_entry(file
);
384 static BOOL
add_wildcard(FILE_LIST
*files
, LPWSTR source
, LPWSTR dest
)
386 FILE_LIST
*new, *file
;
387 LPWSTR ptr
, filename
;
390 new = msi_alloc_zero(sizeof(FILE_LIST
));
394 new->source
= strdupW(source
);
395 ptr
= strrchrW(dest
, '\\') + 1;
396 filename
= strrchrW(new->source
, '\\') + 1;
398 new->sourcename
= filename
;
403 new->destname
= new->sourcename
;
405 size
= (ptr
- dest
) + lstrlenW(filename
) + 1;
406 new->dest
= msi_alloc(size
* sizeof(WCHAR
));
409 free_file_entry(new);
413 lstrcpynW(new->dest
, dest
, ptr
- dest
+ 1);
414 lstrcatW(new->dest
, filename
);
416 if (list_empty(&files
->entry
))
418 list_add_head(&files
->entry
, &new->entry
);
422 LIST_FOR_EACH_ENTRY(file
, &files
->entry
, FILE_LIST
, entry
)
424 if (strcmpW( source
, file
->source
) < 0)
426 list_add_before(&file
->entry
, &new->entry
);
431 list_add_after(&file
->entry
, &new->entry
);
435 static BOOL
move_files_wildcard(LPWSTR source
, LPWSTR dest
, int options
)
437 WIN32_FIND_DATAW wfd
;
441 FILE_LIST files
, *file
;
444 hfile
= FindFirstFileW(source
, &wfd
);
445 if (hfile
== INVALID_HANDLE_VALUE
) return FALSE
;
447 list_init(&files
.entry
);
449 for (res
= TRUE
; res
; res
= FindNextFileW(hfile
, &wfd
))
451 if (is_dot_dir(wfd
.cFileName
)) continue;
453 path
= wildcard_to_file(source
, wfd
.cFileName
);
460 add_wildcard(&files
, path
, dest
);
464 /* no files match the wildcard */
465 if (list_empty(&files
.entry
))
468 /* only the first wildcard match gets renamed to dest */
469 file
= LIST_ENTRY(list_head(&files
.entry
), FILE_LIST
, entry
);
470 size
= (strrchrW(file
->dest
, '\\') - file
->dest
) + lstrlenW(file
->destname
) + 2;
471 file
->dest
= msi_realloc(file
->dest
, size
* sizeof(WCHAR
));
478 /* file->dest may be shorter after the reallocation, so add a NULL
479 * terminator. This is needed for the call to strrchrW, as there will no
480 * longer be a NULL terminator within the bounds of the allocation in this case.
482 file
->dest
[size
- 1] = '\0';
483 lstrcpyW(strrchrW(file
->dest
, '\\') + 1, file
->destname
);
485 while (!list_empty(&files
.entry
))
487 file
= LIST_ENTRY(list_head(&files
.entry
), FILE_LIST
, entry
);
489 msi_move_file(file
->source
, file
->dest
, options
);
491 list_remove(&file
->entry
);
492 free_file_entry(file
);
503 static UINT
ITERATE_MoveFiles( MSIRECORD
*rec
, LPVOID param
)
505 MSIPACKAGE
*package
= param
;
508 LPCWSTR sourcename
, component
;
509 LPWSTR sourcedir
, destname
= NULL
, destdir
= NULL
, source
= NULL
, dest
= NULL
;
514 component
= MSI_RecordGetString(rec
, 2);
515 comp
= get_loaded_component(package
, component
);
517 return ERROR_SUCCESS
;
521 TRACE("component is disabled\n");
522 return ERROR_SUCCESS
;
525 if (comp
->ActionRequest
!= INSTALLSTATE_LOCAL
&& comp
->ActionRequest
!= INSTALLSTATE_SOURCE
)
527 TRACE("Component not scheduled for installation: %s\n", debugstr_w(component
));
528 comp
->Action
= comp
->Installed
;
529 return ERROR_SUCCESS
;
531 comp
->Action
= comp
->ActionRequest
;
533 sourcename
= MSI_RecordGetString(rec
, 3);
534 options
= MSI_RecordGetInteger(rec
, 7);
536 sourcedir
= msi_dup_property(package
->db
, MSI_RecordGetString(rec
, 5));
540 destdir
= msi_dup_property(package
->db
, MSI_RecordGetString(rec
, 6));
546 if (GetFileAttributesW(sourcedir
) == INVALID_FILE_ATTRIBUTES
)
549 source
= strdupW(sourcedir
);
555 size
= lstrlenW(sourcedir
) + lstrlenW(sourcename
) + 2;
556 source
= msi_alloc(size
* sizeof(WCHAR
));
560 lstrcpyW(source
, sourcedir
);
561 if (source
[lstrlenW(source
) - 1] != '\\')
562 lstrcatW(source
, szBackSlash
);
563 lstrcatW(source
, sourcename
);
566 wildcards
= strchrW(source
, '*') || strchrW(source
, '?');
568 if (MSI_RecordIsNull(rec
, 4))
572 destname
= strdupW(sourcename
);
579 destname
= strdupW(MSI_RecordGetString(rec
, 4));
581 reduce_to_longfilename(destname
);
586 size
= lstrlenW(destname
);
588 size
+= lstrlenW(destdir
) + 2;
589 dest
= msi_alloc(size
* sizeof(WCHAR
));
593 lstrcpyW(dest
, destdir
);
594 if (dest
[lstrlenW(dest
) - 1] != '\\')
595 lstrcatW(dest
, szBackSlash
);
598 lstrcatW(dest
, destname
);
600 if (GetFileAttributesW(destdir
) == INVALID_FILE_ATTRIBUTES
)
602 ret
= CreateDirectoryW(destdir
, NULL
);
605 WARN("CreateDirectory failed: %d\n", GetLastError());
611 msi_move_file(source
, dest
, options
);
613 move_files_wildcard(source
, dest
, options
);
616 uirow
= MSI_CreateRecord( 9 );
617 MSI_RecordSetStringW( uirow
, 1, MSI_RecordGetString(rec
, 1) );
618 MSI_RecordSetInteger( uirow
, 6, 1 ); /* FIXME */
619 MSI_RecordSetStringW( uirow
, 9, destdir
);
620 ui_actiondata( package
, szMoveFiles
, uirow
);
621 msiobj_release( &uirow
->hdr
);
629 return ERROR_SUCCESS
;
632 UINT
ACTION_MoveFiles( MSIPACKAGE
*package
)
637 static const WCHAR ExecSeqQuery
[] =
638 {'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
639 '`','M','o','v','e','F','i','l','e','`',0};
641 rc
= MSI_DatabaseOpenViewW(package
->db
, ExecSeqQuery
, &view
);
642 if (rc
!= ERROR_SUCCESS
)
643 return ERROR_SUCCESS
;
645 rc
= MSI_IterateRecords(view
, NULL
, ITERATE_MoveFiles
, package
);
646 msiobj_release(&view
->hdr
);
651 static WCHAR
*get_duplicate_filename( MSIPACKAGE
*package
, MSIRECORD
*row
, const WCHAR
*file_key
, const WCHAR
*src
)
654 WCHAR
*dst_name
, *dst_path
, *dst
;
656 if (MSI_RecordIsNull( row
, 4 ))
658 len
= strlenW( src
) + 1;
659 if (!(dst_name
= msi_alloc( len
* sizeof(WCHAR
)))) return NULL
;
660 strcpyW( dst_name
, strrchrW( src
, '\\' ) + 1 );
664 MSI_RecordGetStringW( row
, 4, NULL
, &len
);
665 if (!(dst_name
= msi_alloc( ++len
* sizeof(WCHAR
) ))) return NULL
;
666 MSI_RecordGetStringW( row
, 4, dst_name
, &len
);
667 reduce_to_longfilename( dst_name
);
670 if (MSI_RecordIsNull( row
, 5 ))
673 dst_path
= strdupW( src
);
674 p
= strrchrW( dst_path
, '\\' );
679 const WCHAR
*dst_key
= MSI_RecordGetString( row
, 5 );
681 dst_path
= resolve_folder( package
, dst_key
, FALSE
, FALSE
, TRUE
, NULL
);
685 dst_path
= msi_dup_property( package
->db
, dst_key
);
688 FIXME("Unable to get destination folder, try AppSearch properties\n");
689 msi_free( dst_name
);
695 dst
= build_directory_name( 2, dst_path
, dst_name
);
696 create_full_pathW( dst_path
);
698 msi_free( dst_name
);
699 msi_free( dst_path
);
703 static UINT
ITERATE_DuplicateFiles(MSIRECORD
*row
, LPVOID param
)
705 MSIPACKAGE
*package
= param
;
707 LPCWSTR file_key
, component
;
712 component
= MSI_RecordGetString(row
,2);
713 comp
= get_loaded_component(package
,component
);
715 return ERROR_SUCCESS
;
719 TRACE("component is disabled\n");
720 return ERROR_SUCCESS
;
723 if (comp
->ActionRequest
!= INSTALLSTATE_LOCAL
)
725 TRACE("Component not scheduled for installation %s\n", debugstr_w(component
));
726 comp
->Action
= comp
->Installed
;
727 return ERROR_SUCCESS
;
729 comp
->Action
= INSTALLSTATE_LOCAL
;
731 file_key
= MSI_RecordGetString(row
,3);
734 ERR("Unable to get file key\n");
735 return ERROR_FUNCTION_FAILED
;
738 file
= get_loaded_file( package
, file_key
);
741 ERR("Original file unknown %s\n", debugstr_w(file_key
));
742 return ERROR_SUCCESS
;
745 dest
= get_duplicate_filename( package
, row
, file_key
, file
->TargetPath
);
748 WARN("Unable to get duplicate filename\n");
749 return ERROR_SUCCESS
;
752 TRACE("Duplicating file %s to %s\n", debugstr_w(file
->TargetPath
), debugstr_w(dest
));
754 if (!CopyFileW( file
->TargetPath
, dest
, TRUE
))
756 WARN("Failed to copy file %s -> %s (%u)\n",
757 debugstr_w(file
->TargetPath
), debugstr_w(dest
), GetLastError());
760 FIXME("We should track these duplicate files as well\n");
762 uirow
= MSI_CreateRecord( 9 );
763 MSI_RecordSetStringW( uirow
, 1, MSI_RecordGetString( row
, 1 ) );
764 MSI_RecordSetInteger( uirow
, 6, file
->FileSize
);
765 MSI_RecordSetStringW( uirow
, 9, MSI_RecordGetString( row
, 5 ) );
766 ui_actiondata( package
, szDuplicateFiles
, uirow
);
767 msiobj_release( &uirow
->hdr
);
770 return ERROR_SUCCESS
;
773 UINT
ACTION_DuplicateFiles(MSIPACKAGE
*package
)
777 static const WCHAR ExecSeqQuery
[] =
778 {'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
779 '`','D','u','p','l','i','c','a','t','e','F','i','l','e','`',0};
781 rc
= MSI_DatabaseOpenViewW(package
->db
, ExecSeqQuery
, &view
);
782 if (rc
!= ERROR_SUCCESS
)
783 return ERROR_SUCCESS
;
785 rc
= MSI_IterateRecords(view
, NULL
, ITERATE_DuplicateFiles
, package
);
786 msiobj_release(&view
->hdr
);
791 static UINT
ITERATE_RemoveDuplicateFiles( MSIRECORD
*row
, LPVOID param
)
793 MSIPACKAGE
*package
= param
;
795 LPCWSTR file_key
, component
;
800 component
= MSI_RecordGetString( row
, 2 );
801 comp
= get_loaded_component( package
, component
);
803 return ERROR_SUCCESS
;
807 TRACE("component is disabled\n");
808 return ERROR_SUCCESS
;
811 if (comp
->ActionRequest
!= INSTALLSTATE_ABSENT
)
813 TRACE("Component not scheduled for removal %s\n", debugstr_w(component
));
814 comp
->Action
= comp
->Installed
;
815 return ERROR_SUCCESS
;
817 comp
->Action
= INSTALLSTATE_ABSENT
;
819 file_key
= MSI_RecordGetString( row
, 3 );
822 ERR("Unable to get file key\n");
823 return ERROR_FUNCTION_FAILED
;
826 file
= get_loaded_file( package
, file_key
);
829 ERR("Original file unknown %s\n", debugstr_w(file_key
));
830 return ERROR_SUCCESS
;
833 dest
= get_duplicate_filename( package
, row
, file_key
, file
->TargetPath
);
836 WARN("Unable to get duplicate filename\n");
837 return ERROR_SUCCESS
;
840 TRACE("Removing duplicate %s of %s\n", debugstr_w(dest
), debugstr_w(file
->TargetPath
));
842 if (!DeleteFileW( dest
))
844 WARN("Failed to delete duplicate file %s (%u)\n", debugstr_w(dest
), GetLastError());
847 uirow
= MSI_CreateRecord( 9 );
848 MSI_RecordSetStringW( uirow
, 1, MSI_RecordGetString( row
, 1 ) );
849 MSI_RecordSetStringW( uirow
, 9, MSI_RecordGetString( row
, 5 ) );
850 ui_actiondata( package
, szRemoveDuplicateFiles
, uirow
);
851 msiobj_release( &uirow
->hdr
);
854 return ERROR_SUCCESS
;
857 UINT
ACTION_RemoveDuplicateFiles( MSIPACKAGE
*package
)
861 static const WCHAR query
[] =
862 {'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
863 '`','D','u','p','l','i','c','a','t','e','F','i','l','e','`',0};
865 rc
= MSI_DatabaseOpenViewW( package
->db
, query
, &view
);
866 if (rc
!= ERROR_SUCCESS
)
867 return ERROR_SUCCESS
;
869 rc
= MSI_IterateRecords( view
, NULL
, ITERATE_RemoveDuplicateFiles
, package
);
870 msiobj_release( &view
->hdr
);
875 static BOOL
verify_comp_for_removal(MSICOMPONENT
*comp
, UINT install_mode
)
877 INSTALLSTATE request
= comp
->ActionRequest
;
879 if (request
== INSTALLSTATE_UNKNOWN
)
882 if (install_mode
== msidbRemoveFileInstallModeOnInstall
&&
883 (request
== INSTALLSTATE_LOCAL
|| request
== INSTALLSTATE_SOURCE
))
886 if (request
== INSTALLSTATE_ABSENT
)
888 if (!comp
->ComponentId
)
891 if (install_mode
== msidbRemoveFileInstallModeOnRemove
)
895 if (install_mode
== msidbRemoveFileInstallModeOnBoth
)
901 static UINT
ITERATE_RemoveFiles(MSIRECORD
*row
, LPVOID param
)
903 MSIPACKAGE
*package
= param
;
906 LPCWSTR component
, filename
, dirprop
;
908 LPWSTR dir
= NULL
, path
= NULL
;
910 UINT ret
= ERROR_SUCCESS
;
912 component
= MSI_RecordGetString(row
, 2);
913 filename
= MSI_RecordGetString(row
, 3);
914 dirprop
= MSI_RecordGetString(row
, 4);
915 install_mode
= MSI_RecordGetInteger(row
, 5);
917 comp
= get_loaded_component(package
, component
);
920 ERR("Invalid component: %s\n", debugstr_w(component
));
921 return ERROR_FUNCTION_FAILED
;
926 TRACE("component is disabled\n");
927 return ERROR_SUCCESS
;
930 if (!verify_comp_for_removal(comp
, install_mode
))
932 TRACE("Skipping removal due to missing conditions\n");
933 comp
->Action
= comp
->Installed
;
934 return ERROR_SUCCESS
;
937 dir
= msi_dup_property(package
->db
, dirprop
);
939 return ERROR_OUTOFMEMORY
;
941 size
= (filename
!= NULL
) ? lstrlenW(filename
) : 0;
942 size
+= lstrlenW(dir
) + 2;
943 path
= msi_alloc(size
* sizeof(WCHAR
));
946 ret
= ERROR_OUTOFMEMORY
;
953 PathAddBackslashW(path
);
954 lstrcatW(path
, filename
);
956 TRACE("Deleting misc file: %s\n", debugstr_w(path
));
961 TRACE("Removing misc directory: %s\n", debugstr_w(dir
));
962 RemoveDirectoryW(dir
);
966 uirow
= MSI_CreateRecord( 9 );
967 MSI_RecordSetStringW( uirow
, 1, MSI_RecordGetString(row
, 1) );
968 MSI_RecordSetStringW( uirow
, 9, dir
);
969 ui_actiondata( package
, szRemoveFiles
, uirow
);
970 msiobj_release( &uirow
->hdr
);
977 UINT
ACTION_RemoveFiles( MSIPACKAGE
*package
)
983 static const WCHAR query
[] = {
984 'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
985 '`','R','e','m','o','v','e','F','i','l','e','`',0};
986 static const WCHAR folder_query
[] = {
987 'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
988 '`','C','r','e','a','t','e','F','o','l','d','e','r','`',0};
990 r
= MSI_DatabaseOpenViewW(package
->db
, query
, &view
);
991 if (r
== ERROR_SUCCESS
)
993 MSI_IterateRecords(view
, NULL
, ITERATE_RemoveFiles
, package
);
994 msiobj_release(&view
->hdr
);
997 r
= MSI_DatabaseOpenViewW(package
->db
, folder_query
, &view
);
998 if (r
== ERROR_SUCCESS
)
999 msiobj_release(&view
->hdr
);
1001 LIST_FOR_EACH_ENTRY( file
, &package
->files
, MSIFILE
, entry
)
1005 VS_FIXEDFILEINFO
*ver
;
1007 if ( file
->state
== msifs_installed
)
1008 ERR("removing installed file %s\n", debugstr_w(file
->TargetPath
));
1010 if ( file
->Component
->ActionRequest
!= INSTALLSTATE_ABSENT
||
1011 file
->Component
->Installed
== INSTALLSTATE_SOURCE
)
1014 if (!file
->Component
->Enabled
)
1016 TRACE("component is disabled\n");
1022 ver
= msi_get_disk_file_version( file
->TargetPath
);
1023 if (ver
&& msi_compare_file_versions( ver
, file
->Version
) > 0)
1025 TRACE("newer version detected, not removing file\n");
1032 TRACE("removing %s\n", debugstr_w(file
->File
) );
1033 if (!DeleteFileW( file
->TargetPath
))
1035 WARN("failed to delete %s\n", debugstr_w(file
->TargetPath
));
1037 /* FIXME: check persistence for each directory */
1038 else if (r
&& (dir
= strdupW( file
->TargetPath
)))
1040 if ((p
= strrchrW( dir
, '\\' ))) *p
= 0;
1041 RemoveDirectoryW( dir
);
1044 file
->state
= msifs_missing
;
1046 uirow
= MSI_CreateRecord( 9 );
1047 MSI_RecordSetStringW( uirow
, 1, file
->FileName
);
1048 MSI_RecordSetStringW( uirow
, 9, file
->Component
->Directory
);
1049 ui_actiondata( package
, szRemoveFiles
, uirow
);
1050 msiobj_release( &uirow
->hdr
);
1051 /* FIXME: call ui_progress here? */
1054 return ERROR_SUCCESS
;