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 msi_file_state
calculate_install_state( MSIFILE
*file
)
65 MSICOMPONENT
*comp
= file
->Component
;
66 VS_FIXEDFILEINFO
*file_version
;
71 if (comp
->ActionRequest
!= INSTALLSTATE_LOCAL
|| !comp
->Enabled
||
72 (comp
->assembly
&& comp
->assembly
->installed
))
74 TRACE("file %s is not scheduled for install\n", debugstr_w(file
->File
));
77 if ((comp
->assembly
&& !comp
->assembly
->application
&& !comp
->assembly
->installed
) ||
78 GetFileAttributesW( file
->TargetPath
) == INVALID_FILE_ATTRIBUTES
)
80 TRACE("file %s is missing\n", debugstr_w(file
->File
));
85 if ((file_version
= msi_get_disk_file_version( file
->TargetPath
)))
87 TRACE("new %s old %u.%u.%u.%u\n", debugstr_w(file
->Version
),
88 HIWORD(file_version
->dwFileVersionMS
),
89 LOWORD(file_version
->dwFileVersionMS
),
90 HIWORD(file_version
->dwFileVersionLS
),
91 LOWORD(file_version
->dwFileVersionLS
));
93 if (msi_compare_file_versions( file_version
, file
->Version
) < 0)
94 state
= msifs_overwrite
;
97 TRACE("destination file version equal or greater, not overwriting\n");
98 state
= msifs_present
;
100 msi_free( file_version
);
103 else if ((font_version
= font_version_from_file( file
->TargetPath
)))
105 TRACE("new %s old %s\n", debugstr_w(file
->Version
), debugstr_w(font_version
));
107 if (msi_compare_font_versions( font_version
, file
->Version
) < 0)
108 state
= msifs_overwrite
;
111 TRACE("destination file version equal or greater, not overwriting\n");
112 state
= msifs_present
;
114 msi_free( font_version
);
118 if ((file_size
= msi_get_disk_file_size( file
->TargetPath
)) != file
->FileSize
)
120 return msifs_overwrite
;
122 if (file
->hash
.dwFileHashInfoSize
)
124 if (msi_file_hash_matches( file
))
126 TRACE("file hashes match, not overwriting\n");
127 return msifs_hashmatch
;
131 TRACE("file hashes do not match, overwriting\n");
132 return msifs_overwrite
;
136 return msifs_present
;
139 static void schedule_install_files(MSIPACKAGE
*package
)
143 LIST_FOR_EACH_ENTRY(file
, &package
->files
, MSIFILE
, entry
)
145 MSICOMPONENT
*comp
= file
->Component
;
147 file
->state
= calculate_install_state( file
);
148 if (file
->state
== msifs_overwrite
&& (comp
->Attributes
& msidbComponentAttributesNeverOverwrite
))
150 TRACE("not overwriting %s\n", debugstr_w(file
->TargetPath
));
151 file
->state
= msifs_skipped
;
153 comp
->Action
= INSTALLSTATE_LOCAL
;
154 ui_progress( package
, 2, file
->FileSize
, 0, 0 );
158 static UINT
copy_file(MSIFILE
*file
, LPWSTR source
)
162 ret
= CopyFileW(source
, file
->TargetPath
, FALSE
);
164 return GetLastError();
166 SetFileAttributesW(file
->TargetPath
, FILE_ATTRIBUTE_NORMAL
);
168 file
->state
= msifs_installed
;
169 return ERROR_SUCCESS
;
172 static UINT
copy_install_file(MSIPACKAGE
*package
, MSIFILE
*file
, LPWSTR source
)
176 TRACE("Copying %s to %s\n", debugstr_w(source
), debugstr_w(file
->TargetPath
));
178 gle
= copy_file(file
, source
);
179 if (gle
== ERROR_SUCCESS
)
182 if (gle
== ERROR_ALREADY_EXISTS
&& file
->state
== msifs_overwrite
)
184 TRACE("overwriting existing file\n");
185 return ERROR_SUCCESS
;
187 else if (gle
== ERROR_ACCESS_DENIED
)
189 SetFileAttributesW(file
->TargetPath
, FILE_ATTRIBUTE_NORMAL
);
191 gle
= copy_file(file
, source
);
192 TRACE("Overwriting existing file: %d\n", gle
);
194 if (gle
== ERROR_SHARING_VIOLATION
|| gle
== ERROR_USER_MAPPED_FILE
)
196 WCHAR tmpfileW
[MAX_PATH
], *pathW
, *p
;
199 TRACE("file in use, scheduling rename operation\n");
201 GetTempFileNameW(szBackSlash
, szMsi
, 0, tmpfileW
);
202 len
= strlenW(file
->TargetPath
) + strlenW(tmpfileW
) + 1;
203 if (!(pathW
= msi_alloc(len
* sizeof(WCHAR
))))
204 return ERROR_OUTOFMEMORY
;
206 strcpyW(pathW
, file
->TargetPath
);
207 if ((p
= strrchrW(pathW
, '\\'))) *p
= 0;
208 strcatW(pathW
, tmpfileW
);
210 if (CopyFileW(source
, pathW
, FALSE
) &&
211 MoveFileExW(file
->TargetPath
, NULL
, MOVEFILE_DELAY_UNTIL_REBOOT
) &&
212 MoveFileExW(pathW
, file
->TargetPath
, MOVEFILE_DELAY_UNTIL_REBOOT
))
214 file
->state
= msifs_installed
;
215 package
->need_reboot
= 1;
220 gle
= GetLastError();
221 WARN("failed to schedule rename operation: %d)\n", gle
);
229 static UINT
msi_create_directory( MSIPACKAGE
*package
, const WCHAR
*dir
)
234 install_path
= resolve_target_folder( package
, dir
, FALSE
, TRUE
, &folder
);
236 return ERROR_FUNCTION_FAILED
;
238 if (folder
->State
== 0)
240 create_full_pathW( install_path
);
243 msi_free( install_path
);
244 return ERROR_SUCCESS
;
247 static BOOL
installfiles_cb(MSIPACKAGE
*package
, LPCWSTR file
, DWORD action
,
248 LPWSTR
*path
, DWORD
*attrs
, PVOID user
)
250 static MSIFILE
*f
= NULL
;
251 UINT_PTR disk_id
= (UINT_PTR
)user
;
253 if (action
== MSICABEXTRACT_BEGINEXTRACT
)
255 f
= get_loaded_file(package
, file
);
258 TRACE("unknown file in cabinet (%s)\n", debugstr_w(file
));
262 if (f
->disk_id
!= disk_id
|| (f
->state
!= msifs_missing
&& f
->state
!= msifs_overwrite
))
265 msi_file_update_ui(package
, f
, szInstallFiles
);
266 if (!f
->Component
->assembly
|| f
->Component
->assembly
->application
)
268 msi_create_directory(package
, f
->Component
->Directory
);
270 *path
= strdupW(f
->TargetPath
);
271 *attrs
= f
->Attributes
;
273 else if (action
== MSICABEXTRACT_FILEEXTRACTED
)
275 f
->state
= msifs_installed
;
283 * ACTION_InstallFiles()
285 * For efficiency, this is done in two passes:
286 * 1) Correct all the TargetPaths and determine what files are to be installed.
287 * 2) Extract Cabinets and copy files.
289 UINT
ACTION_InstallFiles(MSIPACKAGE
*package
)
293 UINT rc
= ERROR_SUCCESS
;
296 /* increment progress bar each time action data is sent */
297 ui_progress(package
,1,1,0,0);
299 schedule_install_files(package
);
301 mi
= msi_alloc_zero( sizeof(MSIMEDIAINFO
) );
303 LIST_FOR_EACH_ENTRY( file
, &package
->files
, MSIFILE
, entry
)
305 rc
= msi_load_media_info( package
, file
, mi
);
306 if (rc
!= ERROR_SUCCESS
)
308 ERR("Unable to load media info for %s (%u)\n", debugstr_w(file
->File
), rc
);
309 return ERROR_FUNCTION_FAILED
;
311 if (!file
->Component
->Enabled
) continue;
313 if (file
->state
!= msifs_hashmatch
&& (rc
= ready_media( package
, file
, mi
)))
315 ERR("Failed to ready media for %s\n", debugstr_w(file
->File
));
319 if (file
->state
!= msifs_missing
&& !mi
->is_continuous
&& file
->state
!= msifs_overwrite
)
322 if (file
->Sequence
> mi
->last_sequence
|| mi
->is_continuous
||
323 (file
->IsCompressed
&& !mi
->is_extracted
))
328 data
.package
= package
;
329 data
.cb
= installfiles_cb
;
330 data
.user
= (PVOID
)(UINT_PTR
)mi
->disk_id
;
332 if (file
->IsCompressed
&&
333 !msi_cabextract(package
, mi
, &data
))
335 ERR("Failed to extract cabinet: %s\n", debugstr_w(mi
->cabinet
));
336 rc
= ERROR_INSTALL_FAILURE
;
341 if (!file
->IsCompressed
)
343 LPWSTR source
= resolve_file_source(package
, file
);
345 TRACE("copying %s to %s\n", debugstr_w(source
), debugstr_w(file
->TargetPath
));
347 msi_file_update_ui(package
, file
, szInstallFiles
);
348 if (!file
->Component
->assembly
|| file
->Component
->assembly
->application
)
350 msi_create_directory(package
, file
->Component
->Directory
);
352 rc
= copy_install_file(package
, file
, source
);
353 if (rc
!= ERROR_SUCCESS
)
355 ERR("Failed to copy %s to %s (%d)\n", debugstr_w(source
),
356 debugstr_w(file
->TargetPath
), rc
);
357 rc
= ERROR_INSTALL_FAILURE
;
363 else if (file
->state
!= msifs_installed
)
365 ERR("compressed file wasn't installed (%s)\n", debugstr_w(file
->TargetPath
));
366 rc
= ERROR_INSTALL_FAILURE
;
370 LIST_FOR_EACH_ENTRY( comp
, &package
->components
, MSICOMPONENT
, entry
)
372 if (comp
->ActionRequest
== INSTALLSTATE_LOCAL
&& comp
->Enabled
&&
373 comp
->assembly
&& !comp
->assembly
->installed
)
375 rc
= install_assembly( package
, comp
);
376 if (rc
!= ERROR_SUCCESS
)
378 ERR("Failed to install assembly\n");
379 rc
= ERROR_INSTALL_FAILURE
;
386 msi_free_media_info(mi
);
390 #define is_dot_dir(x) ((x[0] == '.') && ((x[1] == 0) || ((x[1] == '.') && (x[2] == 0))))
401 static BOOL
msi_move_file(LPCWSTR source
, LPCWSTR dest
, int options
)
405 if (GetFileAttributesW(source
) == FILE_ATTRIBUTE_DIRECTORY
||
406 GetFileAttributesW(dest
) == FILE_ATTRIBUTE_DIRECTORY
)
408 WARN("Source or dest is directory, not moving\n");
412 if (options
== msidbMoveFileOptionsMove
)
414 TRACE("moving %s -> %s\n", debugstr_w(source
), debugstr_w(dest
));
415 ret
= MoveFileExW(source
, dest
, MOVEFILE_REPLACE_EXISTING
);
418 WARN("MoveFile failed: %d\n", GetLastError());
424 TRACE("copying %s -> %s\n", debugstr_w(source
), debugstr_w(dest
));
425 ret
= CopyFileW(source
, dest
, FALSE
);
428 WARN("CopyFile failed: %d\n", GetLastError());
436 static LPWSTR
wildcard_to_file(LPWSTR wildcard
, LPWSTR filename
)
439 DWORD dirlen
, pathlen
;
441 ptr
= strrchrW(wildcard
, '\\');
442 dirlen
= ptr
- wildcard
+ 1;
444 pathlen
= dirlen
+ lstrlenW(filename
) + 1;
445 path
= msi_alloc(pathlen
* sizeof(WCHAR
));
447 lstrcpynW(path
, wildcard
, dirlen
+ 1);
448 lstrcatW(path
, filename
);
453 static void free_file_entry(FILE_LIST
*file
)
455 msi_free(file
->source
);
456 msi_free(file
->dest
);
460 static void free_list(FILE_LIST
*list
)
462 while (!list_empty(&list
->entry
))
464 FILE_LIST
*file
= LIST_ENTRY(list_head(&list
->entry
), FILE_LIST
, entry
);
466 list_remove(&file
->entry
);
467 free_file_entry(file
);
471 static BOOL
add_wildcard(FILE_LIST
*files
, LPWSTR source
, LPWSTR dest
)
473 FILE_LIST
*new, *file
;
474 LPWSTR ptr
, filename
;
477 new = msi_alloc_zero(sizeof(FILE_LIST
));
481 new->source
= strdupW(source
);
482 ptr
= strrchrW(dest
, '\\') + 1;
483 filename
= strrchrW(new->source
, '\\') + 1;
485 new->sourcename
= filename
;
490 new->destname
= new->sourcename
;
492 size
= (ptr
- dest
) + lstrlenW(filename
) + 1;
493 new->dest
= msi_alloc(size
* sizeof(WCHAR
));
496 free_file_entry(new);
500 lstrcpynW(new->dest
, dest
, ptr
- dest
+ 1);
501 lstrcatW(new->dest
, filename
);
503 if (list_empty(&files
->entry
))
505 list_add_head(&files
->entry
, &new->entry
);
509 LIST_FOR_EACH_ENTRY(file
, &files
->entry
, FILE_LIST
, entry
)
511 if (strcmpW( source
, file
->source
) < 0)
513 list_add_before(&file
->entry
, &new->entry
);
518 list_add_after(&file
->entry
, &new->entry
);
522 static BOOL
move_files_wildcard(LPWSTR source
, LPWSTR dest
, int options
)
524 WIN32_FIND_DATAW wfd
;
528 FILE_LIST files
, *file
;
531 hfile
= FindFirstFileW(source
, &wfd
);
532 if (hfile
== INVALID_HANDLE_VALUE
) return FALSE
;
534 list_init(&files
.entry
);
536 for (res
= TRUE
; res
; res
= FindNextFileW(hfile
, &wfd
))
538 if (is_dot_dir(wfd
.cFileName
)) continue;
540 path
= wildcard_to_file(source
, wfd
.cFileName
);
547 add_wildcard(&files
, path
, dest
);
551 /* no files match the wildcard */
552 if (list_empty(&files
.entry
))
555 /* only the first wildcard match gets renamed to dest */
556 file
= LIST_ENTRY(list_head(&files
.entry
), FILE_LIST
, entry
);
557 size
= (strrchrW(file
->dest
, '\\') - file
->dest
) + lstrlenW(file
->destname
) + 2;
558 file
->dest
= msi_realloc(file
->dest
, size
* sizeof(WCHAR
));
565 /* file->dest may be shorter after the reallocation, so add a NULL
566 * terminator. This is needed for the call to strrchrW, as there will no
567 * longer be a NULL terminator within the bounds of the allocation in this case.
569 file
->dest
[size
- 1] = '\0';
570 lstrcpyW(strrchrW(file
->dest
, '\\') + 1, file
->destname
);
572 while (!list_empty(&files
.entry
))
574 file
= LIST_ENTRY(list_head(&files
.entry
), FILE_LIST
, entry
);
576 msi_move_file(file
->source
, file
->dest
, options
);
578 list_remove(&file
->entry
);
579 free_file_entry(file
);
590 static UINT
ITERATE_MoveFiles( MSIRECORD
*rec
, LPVOID param
)
592 MSIPACKAGE
*package
= param
;
595 LPCWSTR sourcename
, component
;
596 LPWSTR sourcedir
, destname
= NULL
, destdir
= NULL
, source
= NULL
, dest
= NULL
;
601 component
= MSI_RecordGetString(rec
, 2);
602 comp
= get_loaded_component(package
, component
);
604 return ERROR_SUCCESS
;
608 TRACE("component is disabled\n");
609 return ERROR_SUCCESS
;
612 if (comp
->ActionRequest
!= INSTALLSTATE_LOCAL
&& comp
->ActionRequest
!= INSTALLSTATE_SOURCE
)
614 TRACE("Component not scheduled for installation: %s\n", debugstr_w(component
));
615 comp
->Action
= comp
->Installed
;
616 return ERROR_SUCCESS
;
618 comp
->Action
= comp
->ActionRequest
;
620 sourcename
= MSI_RecordGetString(rec
, 3);
621 options
= MSI_RecordGetInteger(rec
, 7);
623 sourcedir
= msi_dup_property(package
->db
, MSI_RecordGetString(rec
, 5));
627 destdir
= msi_dup_property(package
->db
, MSI_RecordGetString(rec
, 6));
633 if (GetFileAttributesW(sourcedir
) == INVALID_FILE_ATTRIBUTES
)
636 source
= strdupW(sourcedir
);
642 size
= lstrlenW(sourcedir
) + lstrlenW(sourcename
) + 2;
643 source
= msi_alloc(size
* sizeof(WCHAR
));
647 lstrcpyW(source
, sourcedir
);
648 if (source
[lstrlenW(source
) - 1] != '\\')
649 lstrcatW(source
, szBackSlash
);
650 lstrcatW(source
, sourcename
);
653 wildcards
= strchrW(source
, '*') || strchrW(source
, '?');
655 if (MSI_RecordIsNull(rec
, 4))
659 destname
= strdupW(sourcename
);
666 destname
= strdupW(MSI_RecordGetString(rec
, 4));
668 reduce_to_longfilename(destname
);
673 size
= lstrlenW(destname
);
675 size
+= lstrlenW(destdir
) + 2;
676 dest
= msi_alloc(size
* sizeof(WCHAR
));
680 lstrcpyW(dest
, destdir
);
681 if (dest
[lstrlenW(dest
) - 1] != '\\')
682 lstrcatW(dest
, szBackSlash
);
685 lstrcatW(dest
, destname
);
687 if (GetFileAttributesW(destdir
) == INVALID_FILE_ATTRIBUTES
)
689 ret
= CreateDirectoryW(destdir
, NULL
);
692 WARN("CreateDirectory failed: %d\n", GetLastError());
698 msi_move_file(source
, dest
, options
);
700 move_files_wildcard(source
, dest
, options
);
703 uirow
= MSI_CreateRecord( 9 );
704 MSI_RecordSetStringW( uirow
, 1, MSI_RecordGetString(rec
, 1) );
705 MSI_RecordSetInteger( uirow
, 6, 1 ); /* FIXME */
706 MSI_RecordSetStringW( uirow
, 9, destdir
);
707 ui_actiondata( package
, szMoveFiles
, uirow
);
708 msiobj_release( &uirow
->hdr
);
716 return ERROR_SUCCESS
;
719 UINT
ACTION_MoveFiles( MSIPACKAGE
*package
)
724 static const WCHAR ExecSeqQuery
[] =
725 {'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
726 '`','M','o','v','e','F','i','l','e','`',0};
728 rc
= MSI_DatabaseOpenViewW(package
->db
, ExecSeqQuery
, &view
);
729 if (rc
!= ERROR_SUCCESS
)
730 return ERROR_SUCCESS
;
732 rc
= MSI_IterateRecords(view
, NULL
, ITERATE_MoveFiles
, package
);
733 msiobj_release(&view
->hdr
);
738 static WCHAR
*get_duplicate_filename( MSIPACKAGE
*package
, MSIRECORD
*row
, const WCHAR
*file_key
, const WCHAR
*src
)
741 WCHAR
*dst_name
, *dst_path
, *dst
;
743 if (MSI_RecordIsNull( row
, 4 ))
745 len
= strlenW( src
) + 1;
746 if (!(dst_name
= msi_alloc( len
* sizeof(WCHAR
)))) return NULL
;
747 strcpyW( dst_name
, strrchrW( src
, '\\' ) + 1 );
751 MSI_RecordGetStringW( row
, 4, NULL
, &len
);
752 if (!(dst_name
= msi_alloc( ++len
* sizeof(WCHAR
) ))) return NULL
;
753 MSI_RecordGetStringW( row
, 4, dst_name
, &len
);
754 reduce_to_longfilename( dst_name
);
757 if (MSI_RecordIsNull( row
, 5 ))
760 dst_path
= strdupW( src
);
761 p
= strrchrW( dst_path
, '\\' );
766 const WCHAR
*dst_key
= MSI_RecordGetString( row
, 5 );
768 dst_path
= resolve_target_folder( package
, dst_key
, FALSE
, TRUE
, NULL
);
772 dst_path
= msi_dup_property( package
->db
, dst_key
);
775 FIXME("Unable to get destination folder, try AppSearch properties\n");
776 msi_free( dst_name
);
782 dst
= build_directory_name( 2, dst_path
, dst_name
);
783 create_full_pathW( dst_path
);
785 msi_free( dst_name
);
786 msi_free( dst_path
);
790 static UINT
ITERATE_DuplicateFiles(MSIRECORD
*row
, LPVOID param
)
792 MSIPACKAGE
*package
= param
;
794 LPCWSTR file_key
, component
;
799 component
= MSI_RecordGetString(row
,2);
800 comp
= get_loaded_component(package
,component
);
802 return ERROR_SUCCESS
;
806 TRACE("component is disabled\n");
807 return ERROR_SUCCESS
;
810 if (comp
->ActionRequest
!= INSTALLSTATE_LOCAL
)
812 TRACE("Component not scheduled for installation %s\n", debugstr_w(component
));
813 comp
->Action
= comp
->Installed
;
814 return ERROR_SUCCESS
;
816 comp
->Action
= INSTALLSTATE_LOCAL
;
818 file_key
= MSI_RecordGetString(row
,3);
821 ERR("Unable to get file key\n");
822 return ERROR_FUNCTION_FAILED
;
825 file
= get_loaded_file( package
, file_key
);
828 ERR("Original file unknown %s\n", debugstr_w(file_key
));
829 return ERROR_SUCCESS
;
832 dest
= get_duplicate_filename( package
, row
, file_key
, file
->TargetPath
);
835 WARN("Unable to get duplicate filename\n");
836 return ERROR_SUCCESS
;
839 TRACE("Duplicating file %s to %s\n", debugstr_w(file
->TargetPath
), debugstr_w(dest
));
841 if (!CopyFileW( file
->TargetPath
, dest
, TRUE
))
843 WARN("Failed to copy file %s -> %s (%u)\n",
844 debugstr_w(file
->TargetPath
), debugstr_w(dest
), GetLastError());
847 FIXME("We should track these duplicate files as well\n");
849 uirow
= MSI_CreateRecord( 9 );
850 MSI_RecordSetStringW( uirow
, 1, MSI_RecordGetString( row
, 1 ) );
851 MSI_RecordSetInteger( uirow
, 6, file
->FileSize
);
852 MSI_RecordSetStringW( uirow
, 9, MSI_RecordGetString( row
, 5 ) );
853 ui_actiondata( package
, szDuplicateFiles
, uirow
);
854 msiobj_release( &uirow
->hdr
);
857 return ERROR_SUCCESS
;
860 UINT
ACTION_DuplicateFiles(MSIPACKAGE
*package
)
864 static const WCHAR ExecSeqQuery
[] =
865 {'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
866 '`','D','u','p','l','i','c','a','t','e','F','i','l','e','`',0};
868 rc
= MSI_DatabaseOpenViewW(package
->db
, ExecSeqQuery
, &view
);
869 if (rc
!= ERROR_SUCCESS
)
870 return ERROR_SUCCESS
;
872 rc
= MSI_IterateRecords(view
, NULL
, ITERATE_DuplicateFiles
, package
);
873 msiobj_release(&view
->hdr
);
878 static UINT
ITERATE_RemoveDuplicateFiles( MSIRECORD
*row
, LPVOID param
)
880 MSIPACKAGE
*package
= param
;
882 LPCWSTR file_key
, component
;
887 component
= MSI_RecordGetString( row
, 2 );
888 comp
= get_loaded_component( package
, component
);
890 return ERROR_SUCCESS
;
894 TRACE("component is disabled\n");
895 return ERROR_SUCCESS
;
898 if (comp
->ActionRequest
!= INSTALLSTATE_ABSENT
)
900 TRACE("Component not scheduled for removal %s\n", debugstr_w(component
));
901 comp
->Action
= comp
->Installed
;
902 return ERROR_SUCCESS
;
904 comp
->Action
= INSTALLSTATE_ABSENT
;
906 file_key
= MSI_RecordGetString( row
, 3 );
909 ERR("Unable to get file key\n");
910 return ERROR_FUNCTION_FAILED
;
913 file
= get_loaded_file( package
, file_key
);
916 ERR("Original file unknown %s\n", debugstr_w(file_key
));
917 return ERROR_SUCCESS
;
920 dest
= get_duplicate_filename( package
, row
, file_key
, file
->TargetPath
);
923 WARN("Unable to get duplicate filename\n");
924 return ERROR_SUCCESS
;
927 TRACE("Removing duplicate %s of %s\n", debugstr_w(dest
), debugstr_w(file
->TargetPath
));
929 if (!DeleteFileW( dest
))
931 WARN("Failed to delete duplicate file %s (%u)\n", debugstr_w(dest
), GetLastError());
934 uirow
= MSI_CreateRecord( 9 );
935 MSI_RecordSetStringW( uirow
, 1, MSI_RecordGetString( row
, 1 ) );
936 MSI_RecordSetStringW( uirow
, 9, MSI_RecordGetString( row
, 5 ) );
937 ui_actiondata( package
, szRemoveDuplicateFiles
, uirow
);
938 msiobj_release( &uirow
->hdr
);
941 return ERROR_SUCCESS
;
944 UINT
ACTION_RemoveDuplicateFiles( MSIPACKAGE
*package
)
948 static const WCHAR query
[] =
949 {'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
950 '`','D','u','p','l','i','c','a','t','e','F','i','l','e','`',0};
952 rc
= MSI_DatabaseOpenViewW( package
->db
, query
, &view
);
953 if (rc
!= ERROR_SUCCESS
)
954 return ERROR_SUCCESS
;
956 rc
= MSI_IterateRecords( view
, NULL
, ITERATE_RemoveDuplicateFiles
, package
);
957 msiobj_release( &view
->hdr
);
962 static BOOL
verify_comp_for_removal(MSICOMPONENT
*comp
, UINT install_mode
)
964 INSTALLSTATE request
= comp
->ActionRequest
;
967 if (request
!= INSTALLSTATE_SOURCE
&&
968 comp
->Attributes
& msidbComponentAttributesSourceOnly
&&
969 (install_mode
== msidbRemoveFileInstallModeOnRemove
||
970 install_mode
== msidbRemoveFileInstallModeOnBoth
)) return TRUE
;
974 case INSTALLSTATE_LOCAL
:
975 case INSTALLSTATE_SOURCE
:
976 if (install_mode
== msidbRemoveFileInstallModeOnInstall
||
977 install_mode
== msidbRemoveFileInstallModeOnBoth
) return TRUE
;
979 case INSTALLSTATE_ABSENT
:
980 if (install_mode
== msidbRemoveFileInstallModeOnRemove
||
981 install_mode
== msidbRemoveFileInstallModeOnBoth
) return TRUE
;
988 static UINT
ITERATE_RemoveFiles(MSIRECORD
*row
, LPVOID param
)
990 MSIPACKAGE
*package
= param
;
993 LPCWSTR component
, dirprop
;
995 LPWSTR dir
= NULL
, path
= NULL
, filename
= NULL
;
997 UINT ret
= ERROR_SUCCESS
;
999 component
= MSI_RecordGetString(row
, 2);
1000 dirprop
= MSI_RecordGetString(row
, 4);
1001 install_mode
= MSI_RecordGetInteger(row
, 5);
1003 comp
= get_loaded_component(package
, component
);
1006 TRACE("component is disabled\n");
1007 return ERROR_SUCCESS
;
1010 if (!verify_comp_for_removal(comp
, install_mode
))
1012 TRACE("Skipping removal due to install mode\n");
1013 comp
->Action
= comp
->Installed
;
1014 return ERROR_SUCCESS
;
1017 if (comp
->Attributes
& msidbComponentAttributesPermanent
)
1019 TRACE("permanent component, not removing file\n");
1020 return ERROR_SUCCESS
;
1023 dir
= msi_dup_property(package
->db
, dirprop
);
1025 return ERROR_OUTOFMEMORY
;
1028 if ((filename
= strdupW( MSI_RecordGetString(row
, 3) )))
1030 reduce_to_longfilename( filename
);
1031 size
= lstrlenW( filename
);
1033 size
+= lstrlenW(dir
) + 2;
1034 path
= msi_alloc(size
* sizeof(WCHAR
));
1037 ret
= ERROR_OUTOFMEMORY
;
1043 lstrcpyW(path
, dir
);
1044 PathAddBackslashW(path
);
1045 lstrcatW(path
, filename
);
1047 TRACE("Deleting misc file: %s\n", debugstr_w(path
));
1052 TRACE("Removing misc directory: %s\n", debugstr_w(dir
));
1053 RemoveDirectoryW(dir
);
1057 uirow
= MSI_CreateRecord( 9 );
1058 MSI_RecordSetStringW( uirow
, 1, MSI_RecordGetString(row
, 1) );
1059 MSI_RecordSetStringW( uirow
, 9, dir
);
1060 ui_actiondata( package
, szRemoveFiles
, uirow
);
1061 msiobj_release( &uirow
->hdr
);
1069 static BOOL
has_persistent_dir( MSIPACKAGE
*package
, MSICOMPONENT
*comp
)
1072 UINT r
= ERROR_FUNCTION_FAILED
;
1074 static const WCHAR query
[] = {
1075 'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
1076 '`','C','r','e','a','t','e','F','o','l','d','e','r','`',' ','W','H','E','R','E',' ',
1077 '`','C','o','m','p','o','n','e','n','t','_','`',' ','=','\'','%','s','\'',' ','A','N','D',' ',
1078 '`','D','i','r','e','c','t','o','r','y','_','`',' ','=','\'','%','s','\'',0};
1080 if (!MSI_OpenQuery( package
->db
, &view
, query
, comp
->Component
, comp
->Directory
))
1082 if (!MSI_ViewExecute( view
, NULL
))
1085 if (!(r
= MSI_ViewFetch( view
, &rec
)))
1087 TRACE("directory %s is persistent\n", debugstr_w(comp
->Directory
));
1088 msiobj_release( &rec
->hdr
);
1091 msiobj_release( &view
->hdr
);
1093 return (r
== ERROR_SUCCESS
);
1096 UINT
ACTION_RemoveFiles( MSIPACKAGE
*package
)
1102 static const WCHAR query
[] = {
1103 'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
1104 '`','R','e','m','o','v','e','F','i','l','e','`',0};
1106 r
= MSI_DatabaseOpenViewW(package
->db
, query
, &view
);
1107 if (r
== ERROR_SUCCESS
)
1109 MSI_IterateRecords(view
, NULL
, ITERATE_RemoveFiles
, package
);
1110 msiobj_release(&view
->hdr
);
1113 LIST_FOR_EACH_ENTRY( file
, &package
->files
, MSIFILE
, entry
)
1117 VS_FIXEDFILEINFO
*ver
;
1119 if ( file
->state
== msifs_installed
)
1120 ERR("removing installed file %s\n", debugstr_w(file
->TargetPath
));
1122 if ( file
->Component
->ActionRequest
!= INSTALLSTATE_ABSENT
||
1123 file
->Component
->Installed
== INSTALLSTATE_SOURCE
)
1126 if (!file
->Component
->Enabled
)
1128 TRACE("component is disabled\n");
1132 if (file
->Component
->Attributes
& msidbComponentAttributesPermanent
)
1134 TRACE("permanent component, not removing file\n");
1140 ver
= msi_get_disk_file_version( file
->TargetPath
);
1141 if (ver
&& msi_compare_file_versions( ver
, file
->Version
) > 0)
1143 TRACE("newer version detected, not removing file\n");
1150 TRACE("removing %s\n", debugstr_w(file
->File
) );
1152 SetFileAttributesW( file
->TargetPath
, FILE_ATTRIBUTE_NORMAL
);
1153 if (!DeleteFileW( file
->TargetPath
))
1155 WARN("failed to delete %s (%u)\n", debugstr_w(file
->TargetPath
), GetLastError());
1157 else if (!has_persistent_dir( package
, file
->Component
))
1159 if ((dir
= strdupW( file
->TargetPath
)))
1161 if ((p
= strrchrW( dir
, '\\' ))) *p
= 0;
1162 RemoveDirectoryW( dir
);
1166 file
->state
= msifs_missing
;
1168 uirow
= MSI_CreateRecord( 9 );
1169 MSI_RecordSetStringW( uirow
, 1, file
->FileName
);
1170 MSI_RecordSetStringW( uirow
, 9, file
->Component
->Directory
);
1171 ui_actiondata( package
, szRemoveFiles
, uirow
);
1172 msiobj_release( &uirow
->hdr
);
1173 /* FIXME: call ui_progress here? */
1176 return ERROR_SUCCESS
;