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:
29 * RemoveDuplicateFiles
33 #define WIN32_NO_STATUS
35 #define COM_NO_WINDOWS_H
40 //#include "winbase.h"
41 //#include "winerror.h"
42 #include <wine/debug.h>
45 //#include "msidefs.h"
47 //#include "winuser.h"
50 #include <wine/unicode.h>
52 WINE_DEFAULT_DEBUG_CHANNEL(msi
);
54 static HMODULE hmspatcha
;
55 static BOOL (WINAPI
*ApplyPatchToFileW
)(LPCWSTR
, LPCWSTR
, LPCWSTR
, ULONG
);
57 static void msi_file_update_ui( MSIPACKAGE
*package
, MSIFILE
*f
, const WCHAR
*action
)
61 uirow
= MSI_CreateRecord( 9 );
62 MSI_RecordSetStringW( uirow
, 1, f
->FileName
);
63 MSI_RecordSetStringW( uirow
, 9, f
->Component
->Directory
);
64 MSI_RecordSetInteger( uirow
, 6, f
->FileSize
);
65 msi_ui_actiondata( package
, action
, uirow
);
66 msiobj_release( &uirow
->hdr
);
67 msi_ui_progress( package
, 2, f
->FileSize
, 0, 0 );
70 static msi_file_state
calculate_install_state( MSIPACKAGE
*package
, MSIFILE
*file
)
72 MSICOMPONENT
*comp
= file
->Component
;
73 VS_FIXEDFILEINFO
*file_version
;
77 comp
->Action
= msi_get_component_action( package
, comp
);
78 if (comp
->Action
!= INSTALLSTATE_LOCAL
|| (comp
->assembly
&& comp
->assembly
->installed
))
80 TRACE("file %s is not scheduled for install\n", debugstr_w(file
->File
));
83 if ((comp
->assembly
&& !comp
->assembly
->application
&& !comp
->assembly
->installed
) ||
84 GetFileAttributesW( file
->TargetPath
) == INVALID_FILE_ATTRIBUTES
)
86 TRACE("file %s is missing\n", debugstr_w(file
->File
));
91 if ((file_version
= msi_get_disk_file_version( file
->TargetPath
)))
93 TRACE("new %s old %u.%u.%u.%u\n", debugstr_w(file
->Version
),
94 HIWORD(file_version
->dwFileVersionMS
),
95 LOWORD(file_version
->dwFileVersionMS
),
96 HIWORD(file_version
->dwFileVersionLS
),
97 LOWORD(file_version
->dwFileVersionLS
));
99 if (msi_compare_file_versions( file_version
, file
->Version
) < 0)
100 state
= msifs_overwrite
;
103 TRACE("destination file version equal or greater, not overwriting\n");
104 state
= msifs_present
;
106 msi_free( file_version
);
109 else if ((font_version
= msi_font_version_from_file( file
->TargetPath
)))
111 TRACE("new %s old %s\n", debugstr_w(file
->Version
), debugstr_w(font_version
));
113 if (msi_compare_font_versions( font_version
, file
->Version
) < 0)
114 state
= msifs_overwrite
;
117 TRACE("destination file version equal or greater, not overwriting\n");
118 state
= msifs_present
;
120 msi_free( font_version
);
124 if (msi_get_disk_file_size( file
->TargetPath
) != file
->FileSize
)
126 return msifs_overwrite
;
128 if (file
->hash
.dwFileHashInfoSize
)
130 if (msi_file_hash_matches( file
))
132 TRACE("file hashes match, not overwriting\n");
133 return msifs_hashmatch
;
137 TRACE("file hashes do not match, overwriting\n");
138 return msifs_overwrite
;
142 return msifs_present
;
145 static void schedule_install_files(MSIPACKAGE
*package
)
149 LIST_FOR_EACH_ENTRY(file
, &package
->files
, MSIFILE
, entry
)
151 MSICOMPONENT
*comp
= file
->Component
;
153 file
->state
= calculate_install_state( package
, file
);
154 if (file
->state
== msifs_overwrite
&& (comp
->Attributes
& msidbComponentAttributesNeverOverwrite
))
156 TRACE("not overwriting %s\n", debugstr_w(file
->TargetPath
));
157 file
->state
= msifs_skipped
;
162 static UINT
copy_file(MSIFILE
*file
, LPWSTR source
)
166 ret
= CopyFileW(source
, file
->TargetPath
, FALSE
);
168 return GetLastError();
170 SetFileAttributesW(file
->TargetPath
, FILE_ATTRIBUTE_NORMAL
);
172 file
->state
= msifs_installed
;
173 return ERROR_SUCCESS
;
176 static UINT
copy_install_file(MSIPACKAGE
*package
, MSIFILE
*file
, LPWSTR source
)
180 TRACE("Copying %s to %s\n", debugstr_w(source
), debugstr_w(file
->TargetPath
));
182 gle
= copy_file(file
, source
);
183 if (gle
== ERROR_SUCCESS
)
186 if (gle
== ERROR_ALREADY_EXISTS
&& file
->state
== msifs_overwrite
)
188 TRACE("overwriting existing file\n");
189 return ERROR_SUCCESS
;
191 else if (gle
== ERROR_ACCESS_DENIED
)
193 SetFileAttributesW(file
->TargetPath
, FILE_ATTRIBUTE_NORMAL
);
195 gle
= copy_file(file
, source
);
196 TRACE("Overwriting existing file: %d\n", gle
);
198 if (gle
== ERROR_SHARING_VIOLATION
|| gle
== ERROR_USER_MAPPED_FILE
)
200 WCHAR
*tmpfileW
, *pathW
, *p
;
203 TRACE("file in use, scheduling rename operation\n");
205 if (!(pathW
= strdupW( file
->TargetPath
))) return ERROR_OUTOFMEMORY
;
206 if ((p
= strrchrW(pathW
, '\\'))) *p
= 0;
207 len
= strlenW( pathW
) + 16;
208 if (!(tmpfileW
= msi_alloc(len
* sizeof(WCHAR
))))
211 return ERROR_OUTOFMEMORY
;
213 if (!GetTempFileNameW( pathW
, szMsi
, 0, tmpfileW
)) tmpfileW
[0] = 0;
216 if (CopyFileW(source
, tmpfileW
, FALSE
) &&
217 MoveFileExW(file
->TargetPath
, NULL
, MOVEFILE_DELAY_UNTIL_REBOOT
) &&
218 MoveFileExW(tmpfileW
, file
->TargetPath
, MOVEFILE_DELAY_UNTIL_REBOOT
))
220 file
->state
= msifs_installed
;
221 package
->need_reboot_at_end
= 1;
226 gle
= GetLastError();
227 WARN("failed to schedule rename operation: %d)\n", gle
);
228 DeleteFileW( tmpfileW
);
236 static UINT
msi_create_directory( MSIPACKAGE
*package
, const WCHAR
*dir
)
239 const WCHAR
*install_path
;
241 install_path
= msi_get_target_folder( package
, dir
);
242 if (!install_path
) return ERROR_FUNCTION_FAILED
;
244 folder
= msi_get_loaded_folder( package
, dir
);
245 if (folder
->State
== FOLDER_STATE_UNINITIALIZED
)
247 msi_create_full_path( install_path
);
248 folder
->State
= FOLDER_STATE_CREATED
;
250 return ERROR_SUCCESS
;
253 static MSIFILE
*find_file( MSIPACKAGE
*package
, const WCHAR
*filename
)
257 LIST_FOR_EACH_ENTRY( file
, &package
->files
, MSIFILE
, entry
)
259 if (file
->state
!= msifs_installed
&& !strcmpiW( filename
, file
->File
)) return file
;
264 static BOOL
installfiles_cb(MSIPACKAGE
*package
, LPCWSTR file
, DWORD action
,
265 LPWSTR
*path
, DWORD
*attrs
, PVOID user
)
267 static MSIFILE
*f
= NULL
;
268 UINT_PTR disk_id
= (UINT_PTR
)user
;
270 if (action
== MSICABEXTRACT_BEGINEXTRACT
)
272 if (!(f
= find_file( package
, file
)))
274 TRACE("unknown file in cabinet (%s)\n", debugstr_w(file
));
277 if (f
->disk_id
!= disk_id
|| (f
->state
!= msifs_missing
&& f
->state
!= msifs_overwrite
))
280 if (!f
->Component
->assembly
|| f
->Component
->assembly
->application
)
282 msi_create_directory(package
, f
->Component
->Directory
);
284 *path
= strdupW(f
->TargetPath
);
285 *attrs
= f
->Attributes
;
287 else if (action
== MSICABEXTRACT_FILEEXTRACTED
)
289 f
->state
= msifs_installed
;
296 WCHAR
*msi_resolve_file_source( MSIPACKAGE
*package
, MSIFILE
*file
)
300 TRACE("Working to resolve source of file %s\n", debugstr_w(file
->File
));
302 if (file
->IsCompressed
) return NULL
;
304 p
= msi_resolve_source_folder( package
, file
->Component
->Directory
, NULL
);
305 path
= msi_build_directory_name( 2, p
, file
->ShortName
);
307 if (file
->LongName
&& GetFileAttributesW( path
) == INVALID_FILE_ATTRIBUTES
)
310 path
= msi_build_directory_name( 2, p
, file
->LongName
);
313 TRACE("file %s source resolves to %s\n", debugstr_w(file
->File
), debugstr_w(path
));
318 * ACTION_InstallFiles()
320 * For efficiency, this is done in two passes:
321 * 1) Correct all the TargetPaths and determine what files are to be installed.
322 * 2) Extract Cabinets and copy files.
324 UINT
ACTION_InstallFiles(MSIPACKAGE
*package
)
328 UINT rc
= ERROR_SUCCESS
;
331 schedule_install_files(package
);
332 mi
= msi_alloc_zero( sizeof(MSIMEDIAINFO
) );
334 LIST_FOR_EACH_ENTRY( file
, &package
->files
, MSIFILE
, entry
)
336 msi_file_update_ui( package
, file
, szInstallFiles
);
338 rc
= msi_load_media_info( package
, file
->Sequence
, mi
);
339 if (rc
!= ERROR_SUCCESS
)
341 ERR("Unable to load media info for %s (%u)\n", debugstr_w(file
->File
), rc
);
342 rc
= ERROR_FUNCTION_FAILED
;
345 if (!file
->Component
->Enabled
) continue;
347 if (file
->state
!= msifs_hashmatch
&&
348 file
->state
!= msifs_skipped
&&
349 (file
->state
!= msifs_present
|| !msi_get_property_int( package
->db
, szInstalled
, 0 )) &&
350 (rc
= ready_media( package
, file
->IsCompressed
, mi
)))
352 ERR("Failed to ready media for %s\n", debugstr_w(file
->File
));
356 if (file
->state
!= msifs_missing
&& !mi
->is_continuous
&& file
->state
!= msifs_overwrite
)
359 if (file
->Sequence
> mi
->last_sequence
|| mi
->is_continuous
||
360 (file
->IsCompressed
&& !mi
->is_extracted
))
365 data
.package
= package
;
366 data
.cb
= installfiles_cb
;
367 data
.user
= (PVOID
)(UINT_PTR
)mi
->disk_id
;
369 if (file
->IsCompressed
&&
370 !msi_cabextract(package
, mi
, &data
))
372 ERR("Failed to extract cabinet: %s\n", debugstr_w(mi
->cabinet
));
373 rc
= ERROR_INSTALL_FAILURE
;
378 if (!file
->IsCompressed
)
380 WCHAR
*source
= msi_resolve_file_source(package
, file
);
382 TRACE("copying %s to %s\n", debugstr_w(source
), debugstr_w(file
->TargetPath
));
384 if (!file
->Component
->assembly
|| file
->Component
->assembly
->application
)
386 msi_create_directory(package
, file
->Component
->Directory
);
388 rc
= copy_install_file(package
, file
, source
);
389 if (rc
!= ERROR_SUCCESS
)
391 ERR("Failed to copy %s to %s (%d)\n", debugstr_w(source
),
392 debugstr_w(file
->TargetPath
), rc
);
393 rc
= ERROR_INSTALL_FAILURE
;
399 else if (file
->state
!= msifs_installed
&& !(file
->Attributes
& msidbFileAttributesPatchAdded
))
401 ERR("compressed file wasn't installed (%s)\n", debugstr_w(file
->TargetPath
));
402 rc
= ERROR_INSTALL_FAILURE
;
406 LIST_FOR_EACH_ENTRY( comp
, &package
->components
, MSICOMPONENT
, entry
)
408 comp
->Action
= msi_get_component_action( package
, comp
);
409 if (comp
->Action
== INSTALLSTATE_LOCAL
&& comp
->assembly
&& !comp
->assembly
->installed
)
411 rc
= msi_install_assembly( package
, comp
);
412 if (rc
!= ERROR_SUCCESS
)
414 ERR("Failed to install assembly\n");
415 rc
= ERROR_INSTALL_FAILURE
;
422 msi_free_media_info(mi
);
426 static BOOL
load_mspatcha(void)
428 hmspatcha
= LoadLibraryA("mspatcha.dll");
431 ERR("Failed to load mspatcha.dll: %d\n", GetLastError());
435 ApplyPatchToFileW
= (void*)GetProcAddress(hmspatcha
, "ApplyPatchToFileW");
436 if(!ApplyPatchToFileW
)
438 ERR("GetProcAddress(ApplyPatchToFileW) failed: %d.\n", GetLastError());
445 static void unload_mspatch(void)
447 FreeLibrary(hmspatcha
);
450 static MSIFILEPATCH
*get_next_filepatch( MSIPACKAGE
*package
, const WCHAR
*key
)
454 LIST_FOR_EACH_ENTRY( patch
, &package
->filepatches
, MSIFILEPATCH
, entry
)
456 if (!patch
->IsApplied
&& !strcmpW( key
, patch
->File
->File
)) return patch
;
461 static BOOL
patchfiles_cb(MSIPACKAGE
*package
, LPCWSTR file
, DWORD action
,
462 LPWSTR
*path
, DWORD
*attrs
, PVOID user
)
464 static MSIFILEPATCH
*p
= NULL
;
465 static WCHAR patch_path
[MAX_PATH
] = {0};
466 static WCHAR temp_folder
[MAX_PATH
] = {0};
468 if (action
== MSICABEXTRACT_BEGINEXTRACT
)
470 if (temp_folder
[0] == '\0')
471 GetTempPathW(MAX_PATH
, temp_folder
);
473 if (!(p
= get_next_filepatch(package
, file
)) || !p
->File
->Component
->Enabled
)
476 GetTempFileNameW(temp_folder
, NULL
, 0, patch_path
);
478 *path
= strdupW(patch_path
);
479 *attrs
= p
->File
->Attributes
;
481 else if (action
== MSICABEXTRACT_FILEEXTRACTED
)
483 WCHAR patched_file
[MAX_PATH
];
486 GetTempFileNameW(temp_folder
, NULL
, 0, patched_file
);
488 br
= ApplyPatchToFileW(patch_path
, p
->File
->TargetPath
, patched_file
, 0);
491 /* FIXME: baseline cache */
493 DeleteFileW( p
->File
->TargetPath
);
494 MoveFileW( patched_file
, p
->File
->TargetPath
);
499 ERR("Failed patch %s: %d.\n", debugstr_w(p
->File
->TargetPath
), GetLastError());
501 DeleteFileW(patch_path
);
508 UINT
ACTION_PatchFiles( MSIPACKAGE
*package
)
512 UINT rc
= ERROR_SUCCESS
;
513 BOOL mspatcha_loaded
= FALSE
;
515 TRACE("%p\n", package
);
517 mi
= msi_alloc_zero( sizeof(MSIMEDIAINFO
) );
519 LIST_FOR_EACH_ENTRY( patch
, &package
->filepatches
, MSIFILEPATCH
, entry
)
521 MSIFILE
*file
= patch
->File
;
522 MSICOMPONENT
*comp
= file
->Component
;
524 rc
= msi_load_media_info( package
, patch
->Sequence
, mi
);
525 if (rc
!= ERROR_SUCCESS
)
527 ERR("Unable to load media info for %s (%u)\n", debugstr_w(file
->File
), rc
);
528 rc
= ERROR_FUNCTION_FAILED
;
531 comp
->Action
= msi_get_component_action( package
, comp
);
532 if (!comp
->Enabled
|| comp
->Action
!= INSTALLSTATE_LOCAL
) continue;
534 if (!patch
->IsApplied
)
538 rc
= ready_media( package
, TRUE
, mi
);
539 if (rc
!= ERROR_SUCCESS
)
541 ERR("Failed to ready media for %s\n", debugstr_w(file
->File
));
545 if (!mspatcha_loaded
&& !load_mspatcha())
547 rc
= ERROR_FUNCTION_FAILED
;
550 mspatcha_loaded
= TRUE
;
553 data
.package
= package
;
554 data
.cb
= patchfiles_cb
;
555 data
.user
= (PVOID
)(UINT_PTR
)mi
->disk_id
;
557 if (!msi_cabextract(package
, mi
, &data
))
559 ERR("Failed to extract cabinet: %s\n", debugstr_w(mi
->cabinet
));
560 rc
= ERROR_INSTALL_FAILURE
;
565 if (!patch
->IsApplied
&& !(patch
->Attributes
& msidbPatchAttributesNonVital
))
567 ERR("Failed to apply patch to file: %s\n", debugstr_w(file
->File
));
568 rc
= ERROR_INSTALL_FAILURE
;
574 msi_free_media_info(mi
);
580 #define is_dot_dir(x) ((x[0] == '.') && ((x[1] == 0) || ((x[1] == '.') && (x[2] == 0))))
591 static BOOL
msi_move_file(LPCWSTR source
, LPCWSTR dest
, int options
)
595 if (GetFileAttributesW(source
) == FILE_ATTRIBUTE_DIRECTORY
||
596 GetFileAttributesW(dest
) == FILE_ATTRIBUTE_DIRECTORY
)
598 WARN("Source or dest is directory, not moving\n");
602 if (options
== msidbMoveFileOptionsMove
)
604 TRACE("moving %s -> %s\n", debugstr_w(source
), debugstr_w(dest
));
605 ret
= MoveFileExW(source
, dest
, MOVEFILE_REPLACE_EXISTING
);
608 WARN("MoveFile failed: %d\n", GetLastError());
614 TRACE("copying %s -> %s\n", debugstr_w(source
), debugstr_w(dest
));
615 ret
= CopyFileW(source
, dest
, FALSE
);
618 WARN("CopyFile failed: %d\n", GetLastError());
626 static LPWSTR
wildcard_to_file(LPWSTR wildcard
, LPWSTR filename
)
629 DWORD dirlen
, pathlen
;
631 ptr
= strrchrW(wildcard
, '\\');
632 dirlen
= ptr
- wildcard
+ 1;
634 pathlen
= dirlen
+ lstrlenW(filename
) + 1;
635 path
= msi_alloc(pathlen
* sizeof(WCHAR
));
637 lstrcpynW(path
, wildcard
, dirlen
+ 1);
638 lstrcatW(path
, filename
);
643 static void free_file_entry(FILE_LIST
*file
)
645 msi_free(file
->source
);
646 msi_free(file
->dest
);
650 static void free_list(FILE_LIST
*list
)
652 while (!list_empty(&list
->entry
))
654 FILE_LIST
*file
= LIST_ENTRY(list_head(&list
->entry
), FILE_LIST
, entry
);
656 list_remove(&file
->entry
);
657 free_file_entry(file
);
661 static BOOL
add_wildcard(FILE_LIST
*files
, LPWSTR source
, LPWSTR dest
)
663 FILE_LIST
*new, *file
;
664 LPWSTR ptr
, filename
;
667 new = msi_alloc_zero(sizeof(FILE_LIST
));
671 new->source
= strdupW(source
);
672 ptr
= strrchrW(dest
, '\\') + 1;
673 filename
= strrchrW(new->source
, '\\') + 1;
675 new->sourcename
= filename
;
680 new->destname
= new->sourcename
;
682 size
= (ptr
- dest
) + lstrlenW(filename
) + 1;
683 new->dest
= msi_alloc(size
* sizeof(WCHAR
));
686 free_file_entry(new);
690 lstrcpynW(new->dest
, dest
, ptr
- dest
+ 1);
691 lstrcatW(new->dest
, filename
);
693 if (list_empty(&files
->entry
))
695 list_add_head(&files
->entry
, &new->entry
);
699 LIST_FOR_EACH_ENTRY(file
, &files
->entry
, FILE_LIST
, entry
)
701 if (strcmpW( source
, file
->source
) < 0)
703 list_add_before(&file
->entry
, &new->entry
);
708 list_add_after(&file
->entry
, &new->entry
);
712 static BOOL
move_files_wildcard(LPWSTR source
, LPWSTR dest
, int options
)
714 WIN32_FIND_DATAW wfd
;
718 FILE_LIST files
, *file
;
721 hfile
= FindFirstFileW(source
, &wfd
);
722 if (hfile
== INVALID_HANDLE_VALUE
) return FALSE
;
724 list_init(&files
.entry
);
726 for (res
= TRUE
; res
; res
= FindNextFileW(hfile
, &wfd
))
728 if (is_dot_dir(wfd
.cFileName
)) continue;
730 path
= wildcard_to_file(source
, wfd
.cFileName
);
737 add_wildcard(&files
, path
, dest
);
741 /* no files match the wildcard */
742 if (list_empty(&files
.entry
))
745 /* only the first wildcard match gets renamed to dest */
746 file
= LIST_ENTRY(list_head(&files
.entry
), FILE_LIST
, entry
);
747 size
= (strrchrW(file
->dest
, '\\') - file
->dest
) + lstrlenW(file
->destname
) + 2;
748 file
->dest
= msi_realloc(file
->dest
, size
* sizeof(WCHAR
));
755 /* file->dest may be shorter after the reallocation, so add a NULL
756 * terminator. This is needed for the call to strrchrW, as there will no
757 * longer be a NULL terminator within the bounds of the allocation in this case.
759 file
->dest
[size
- 1] = '\0';
760 lstrcpyW(strrchrW(file
->dest
, '\\') + 1, file
->destname
);
762 while (!list_empty(&files
.entry
))
764 file
= LIST_ENTRY(list_head(&files
.entry
), FILE_LIST
, entry
);
766 msi_move_file(file
->source
, file
->dest
, options
);
768 list_remove(&file
->entry
);
769 free_file_entry(file
);
780 void msi_reduce_to_long_filename( WCHAR
*filename
)
782 WCHAR
*p
= strchrW( filename
, '|' );
783 if (p
) memmove( filename
, p
+ 1, (strlenW( p
+ 1 ) + 1) * sizeof(WCHAR
) );
786 static UINT
ITERATE_MoveFiles( MSIRECORD
*rec
, LPVOID param
)
788 MSIPACKAGE
*package
= param
;
791 LPCWSTR sourcename
, component
;
792 LPWSTR sourcedir
, destname
= NULL
, destdir
= NULL
, source
= NULL
, dest
= NULL
;
797 component
= MSI_RecordGetString(rec
, 2);
798 comp
= msi_get_loaded_component(package
, component
);
800 return ERROR_SUCCESS
;
802 comp
->Action
= msi_get_component_action( package
, comp
);
803 if (comp
->Action
!= INSTALLSTATE_LOCAL
)
805 TRACE("component not scheduled for installation %s\n", debugstr_w(component
));
806 return ERROR_SUCCESS
;
809 sourcename
= MSI_RecordGetString(rec
, 3);
810 options
= MSI_RecordGetInteger(rec
, 7);
812 sourcedir
= msi_dup_property(package
->db
, MSI_RecordGetString(rec
, 5));
816 destdir
= msi_dup_property(package
->db
, MSI_RecordGetString(rec
, 6));
822 if (GetFileAttributesW(sourcedir
) == INVALID_FILE_ATTRIBUTES
)
825 source
= strdupW(sourcedir
);
831 size
= lstrlenW(sourcedir
) + lstrlenW(sourcename
) + 2;
832 source
= msi_alloc(size
* sizeof(WCHAR
));
836 lstrcpyW(source
, sourcedir
);
837 if (source
[lstrlenW(source
) - 1] != '\\')
838 lstrcatW(source
, szBackSlash
);
839 lstrcatW(source
, sourcename
);
842 wildcards
= strchrW(source
, '*') || strchrW(source
, '?');
844 if (MSI_RecordIsNull(rec
, 4))
848 destname
= strdupW(sourcename
);
855 destname
= strdupW(MSI_RecordGetString(rec
, 4));
856 if (destname
) msi_reduce_to_long_filename(destname
);
861 size
= lstrlenW(destname
);
863 size
+= lstrlenW(destdir
) + 2;
864 dest
= msi_alloc(size
* sizeof(WCHAR
));
868 lstrcpyW(dest
, destdir
);
869 if (dest
[lstrlenW(dest
) - 1] != '\\')
870 lstrcatW(dest
, szBackSlash
);
873 lstrcatW(dest
, destname
);
875 if (GetFileAttributesW(destdir
) == INVALID_FILE_ATTRIBUTES
)
877 if (!msi_create_full_path(destdir
))
879 WARN("failed to create directory %u\n", GetLastError());
885 msi_move_file(source
, dest
, options
);
887 move_files_wildcard(source
, dest
, options
);
890 uirow
= MSI_CreateRecord( 9 );
891 MSI_RecordSetStringW( uirow
, 1, MSI_RecordGetString(rec
, 1) );
892 MSI_RecordSetInteger( uirow
, 6, 1 ); /* FIXME */
893 MSI_RecordSetStringW( uirow
, 9, destdir
);
894 msi_ui_actiondata( package
, szMoveFiles
, uirow
);
895 msiobj_release( &uirow
->hdr
);
903 return ERROR_SUCCESS
;
906 UINT
ACTION_MoveFiles( MSIPACKAGE
*package
)
908 static const WCHAR query
[] = {
909 'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
910 '`','M','o','v','e','F','i','l','e','`',0};
914 rc
= MSI_DatabaseOpenViewW(package
->db
, query
, &view
);
915 if (rc
!= ERROR_SUCCESS
)
916 return ERROR_SUCCESS
;
918 rc
= MSI_IterateRecords(view
, NULL
, ITERATE_MoveFiles
, package
);
919 msiobj_release(&view
->hdr
);
923 static WCHAR
*get_duplicate_filename( MSIPACKAGE
*package
, MSIRECORD
*row
, const WCHAR
*file_key
, const WCHAR
*src
)
926 WCHAR
*dst_name
, *dst_path
, *dst
;
928 if (MSI_RecordIsNull( row
, 4 ))
930 len
= strlenW( src
) + 1;
931 if (!(dst_name
= msi_alloc( len
* sizeof(WCHAR
)))) return NULL
;
932 strcpyW( dst_name
, strrchrW( src
, '\\' ) + 1 );
936 MSI_RecordGetStringW( row
, 4, NULL
, &len
);
937 if (!(dst_name
= msi_alloc( ++len
* sizeof(WCHAR
) ))) return NULL
;
938 MSI_RecordGetStringW( row
, 4, dst_name
, &len
);
939 msi_reduce_to_long_filename( dst_name
);
942 if (MSI_RecordIsNull( row
, 5 ))
945 dst_path
= strdupW( src
);
946 p
= strrchrW( dst_path
, '\\' );
951 const WCHAR
*dst_key
= MSI_RecordGetString( row
, 5 );
953 dst_path
= strdupW( msi_get_target_folder( package
, dst_key
) );
957 dst_path
= msi_dup_property( package
->db
, dst_key
);
960 FIXME("Unable to get destination folder, try AppSearch properties\n");
961 msi_free( dst_name
);
967 dst
= msi_build_directory_name( 2, dst_path
, dst_name
);
968 msi_create_full_path( dst_path
);
970 msi_free( dst_name
);
971 msi_free( dst_path
);
975 static UINT
ITERATE_DuplicateFiles(MSIRECORD
*row
, LPVOID param
)
977 MSIPACKAGE
*package
= param
;
979 LPCWSTR file_key
, component
;
984 component
= MSI_RecordGetString(row
,2);
985 comp
= msi_get_loaded_component(package
, component
);
987 return ERROR_SUCCESS
;
989 comp
->Action
= msi_get_component_action( package
, comp
);
990 if (comp
->Action
!= INSTALLSTATE_LOCAL
)
992 TRACE("component not scheduled for installation %s\n", debugstr_w(component
));
993 return ERROR_SUCCESS
;
996 file_key
= MSI_RecordGetString(row
,3);
999 ERR("Unable to get file key\n");
1000 return ERROR_FUNCTION_FAILED
;
1003 file
= msi_get_loaded_file( package
, file_key
);
1006 ERR("Original file unknown %s\n", debugstr_w(file_key
));
1007 return ERROR_SUCCESS
;
1010 dest
= get_duplicate_filename( package
, row
, file_key
, file
->TargetPath
);
1013 WARN("Unable to get duplicate filename\n");
1014 return ERROR_SUCCESS
;
1017 TRACE("Duplicating file %s to %s\n", debugstr_w(file
->TargetPath
), debugstr_w(dest
));
1019 if (!CopyFileW( file
->TargetPath
, dest
, TRUE
))
1021 WARN("Failed to copy file %s -> %s (%u)\n",
1022 debugstr_w(file
->TargetPath
), debugstr_w(dest
), GetLastError());
1025 FIXME("We should track these duplicate files as well\n");
1027 uirow
= MSI_CreateRecord( 9 );
1028 MSI_RecordSetStringW( uirow
, 1, MSI_RecordGetString( row
, 1 ) );
1029 MSI_RecordSetInteger( uirow
, 6, file
->FileSize
);
1030 MSI_RecordSetStringW( uirow
, 9, MSI_RecordGetString( row
, 5 ) );
1031 msi_ui_actiondata( package
, szDuplicateFiles
, uirow
);
1032 msiobj_release( &uirow
->hdr
);
1035 return ERROR_SUCCESS
;
1038 UINT
ACTION_DuplicateFiles(MSIPACKAGE
*package
)
1040 static const WCHAR query
[] = {
1041 'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
1042 '`','D','u','p','l','i','c','a','t','e','F','i','l','e','`',0};
1046 rc
= MSI_DatabaseOpenViewW(package
->db
, query
, &view
);
1047 if (rc
!= ERROR_SUCCESS
)
1048 return ERROR_SUCCESS
;
1050 rc
= MSI_IterateRecords(view
, NULL
, ITERATE_DuplicateFiles
, package
);
1051 msiobj_release(&view
->hdr
);
1055 static UINT
ITERATE_RemoveDuplicateFiles( MSIRECORD
*row
, LPVOID param
)
1057 MSIPACKAGE
*package
= param
;
1059 LPCWSTR file_key
, component
;
1064 component
= MSI_RecordGetString( row
, 2 );
1065 comp
= msi_get_loaded_component( package
, component
);
1067 return ERROR_SUCCESS
;
1069 comp
->Action
= msi_get_component_action( package
, comp
);
1070 if (comp
->Action
!= INSTALLSTATE_ABSENT
)
1072 TRACE("component not scheduled for removal %s\n", debugstr_w(component
));
1073 return ERROR_SUCCESS
;
1076 file_key
= MSI_RecordGetString( row
, 3 );
1079 ERR("Unable to get file key\n");
1080 return ERROR_FUNCTION_FAILED
;
1083 file
= msi_get_loaded_file( package
, file_key
);
1086 ERR("Original file unknown %s\n", debugstr_w(file_key
));
1087 return ERROR_SUCCESS
;
1090 dest
= get_duplicate_filename( package
, row
, file_key
, file
->TargetPath
);
1093 WARN("Unable to get duplicate filename\n");
1094 return ERROR_SUCCESS
;
1097 TRACE("Removing duplicate %s of %s\n", debugstr_w(dest
), debugstr_w(file
->TargetPath
));
1099 if (!DeleteFileW( dest
))
1101 WARN("Failed to delete duplicate file %s (%u)\n", debugstr_w(dest
), GetLastError());
1104 uirow
= MSI_CreateRecord( 9 );
1105 MSI_RecordSetStringW( uirow
, 1, MSI_RecordGetString( row
, 1 ) );
1106 MSI_RecordSetStringW( uirow
, 9, MSI_RecordGetString( row
, 5 ) );
1107 msi_ui_actiondata( package
, szRemoveDuplicateFiles
, uirow
);
1108 msiobj_release( &uirow
->hdr
);
1111 return ERROR_SUCCESS
;
1114 UINT
ACTION_RemoveDuplicateFiles( MSIPACKAGE
*package
)
1116 static const WCHAR query
[] = {
1117 'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
1118 '`','D','u','p','l','i','c','a','t','e','F','i','l','e','`',0};
1122 rc
= MSI_DatabaseOpenViewW( package
->db
, query
, &view
);
1123 if (rc
!= ERROR_SUCCESS
)
1124 return ERROR_SUCCESS
;
1126 rc
= MSI_IterateRecords( view
, NULL
, ITERATE_RemoveDuplicateFiles
, package
);
1127 msiobj_release( &view
->hdr
);
1131 static BOOL
verify_comp_for_removal(MSICOMPONENT
*comp
, UINT install_mode
)
1134 if (comp
->Action
!= INSTALLSTATE_SOURCE
&&
1135 comp
->Attributes
& msidbComponentAttributesSourceOnly
&&
1136 (install_mode
== msidbRemoveFileInstallModeOnRemove
||
1137 install_mode
== msidbRemoveFileInstallModeOnBoth
)) return TRUE
;
1139 switch (comp
->Action
)
1141 case INSTALLSTATE_LOCAL
:
1142 case INSTALLSTATE_SOURCE
:
1143 if (install_mode
== msidbRemoveFileInstallModeOnInstall
||
1144 install_mode
== msidbRemoveFileInstallModeOnBoth
) return TRUE
;
1146 case INSTALLSTATE_ABSENT
:
1147 if (install_mode
== msidbRemoveFileInstallModeOnRemove
||
1148 install_mode
== msidbRemoveFileInstallModeOnBoth
) return TRUE
;
1155 static UINT
ITERATE_RemoveFiles(MSIRECORD
*row
, LPVOID param
)
1157 MSIPACKAGE
*package
= param
;
1160 LPCWSTR component
, dirprop
;
1162 LPWSTR dir
= NULL
, path
= NULL
, filename
= NULL
;
1164 UINT ret
= ERROR_SUCCESS
;
1166 component
= MSI_RecordGetString(row
, 2);
1167 dirprop
= MSI_RecordGetString(row
, 4);
1168 install_mode
= MSI_RecordGetInteger(row
, 5);
1170 comp
= msi_get_loaded_component(package
, component
);
1172 return ERROR_SUCCESS
;
1174 comp
->Action
= msi_get_component_action( package
, comp
);
1175 if (!verify_comp_for_removal(comp
, install_mode
))
1177 TRACE("Skipping removal due to install mode\n");
1178 return ERROR_SUCCESS
;
1180 if (comp
->assembly
&& !comp
->assembly
->application
)
1182 return ERROR_SUCCESS
;
1184 if (comp
->Attributes
& msidbComponentAttributesPermanent
)
1186 TRACE("permanent component, not removing file\n");
1187 return ERROR_SUCCESS
;
1190 dir
= msi_dup_property(package
->db
, dirprop
);
1193 WARN("directory property has no value\n");
1194 return ERROR_SUCCESS
;
1197 if ((filename
= strdupW( MSI_RecordGetString(row
, 3) )))
1199 msi_reduce_to_long_filename( filename
);
1200 size
= lstrlenW( filename
);
1202 size
+= lstrlenW(dir
) + 2;
1203 path
= msi_alloc(size
* sizeof(WCHAR
));
1206 ret
= ERROR_OUTOFMEMORY
;
1212 lstrcpyW(path
, dir
);
1213 PathAddBackslashW(path
);
1214 lstrcatW(path
, filename
);
1216 TRACE("Deleting misc file: %s\n", debugstr_w(path
));
1221 TRACE("Removing misc directory: %s\n", debugstr_w(dir
));
1222 RemoveDirectoryW(dir
);
1226 uirow
= MSI_CreateRecord( 9 );
1227 MSI_RecordSetStringW( uirow
, 1, MSI_RecordGetString(row
, 1) );
1228 MSI_RecordSetStringW( uirow
, 9, dir
);
1229 msi_ui_actiondata( package
, szRemoveFiles
, uirow
);
1230 msiobj_release( &uirow
->hdr
);
1238 static void remove_folder( MSIFOLDER
*folder
)
1242 LIST_FOR_EACH_ENTRY( fl
, &folder
->children
, FolderList
, entry
)
1244 remove_folder( fl
->folder
);
1246 if (!folder
->persistent
&& folder
->State
!= FOLDER_STATE_REMOVED
)
1248 if (RemoveDirectoryW( folder
->ResolvedTarget
)) folder
->State
= FOLDER_STATE_REMOVED
;
1252 UINT
ACTION_RemoveFiles( MSIPACKAGE
*package
)
1254 static const WCHAR query
[] = {
1255 'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
1256 '`','R','e','m','o','v','e','F','i','l','e','`',0};
1262 r
= MSI_DatabaseOpenViewW(package
->db
, query
, &view
);
1263 if (r
== ERROR_SUCCESS
)
1265 r
= MSI_IterateRecords(view
, NULL
, ITERATE_RemoveFiles
, package
);
1266 msiobj_release(&view
->hdr
);
1267 if (r
!= ERROR_SUCCESS
)
1271 LIST_FOR_EACH_ENTRY( file
, &package
->files
, MSIFILE
, entry
)
1274 VS_FIXEDFILEINFO
*ver
;
1276 comp
= file
->Component
;
1277 msi_file_update_ui( package
, file
, szRemoveFiles
);
1279 comp
->Action
= msi_get_component_action( package
, comp
);
1280 if (comp
->Action
!= INSTALLSTATE_ABSENT
|| comp
->Installed
== INSTALLSTATE_SOURCE
)
1283 if (comp
->assembly
&& !comp
->assembly
->application
)
1286 if (comp
->Attributes
& msidbComponentAttributesPermanent
)
1288 TRACE("permanent component, not removing file\n");
1294 ver
= msi_get_disk_file_version( file
->TargetPath
);
1295 if (ver
&& msi_compare_file_versions( ver
, file
->Version
) > 0)
1297 TRACE("newer version detected, not removing file\n");
1304 if (file
->state
== msifs_installed
)
1305 WARN("removing installed file %s\n", debugstr_w(file
->TargetPath
));
1307 TRACE("removing %s\n", debugstr_w(file
->File
) );
1309 SetFileAttributesW( file
->TargetPath
, FILE_ATTRIBUTE_NORMAL
);
1310 if (!DeleteFileW( file
->TargetPath
))
1312 WARN("failed to delete %s (%u)\n", debugstr_w(file
->TargetPath
), GetLastError());
1314 file
->state
= msifs_missing
;
1316 uirow
= MSI_CreateRecord( 9 );
1317 MSI_RecordSetStringW( uirow
, 1, file
->FileName
);
1318 MSI_RecordSetStringW( uirow
, 9, comp
->Directory
);
1319 msi_ui_actiondata( package
, szRemoveFiles
, uirow
);
1320 msiobj_release( &uirow
->hdr
);
1323 LIST_FOR_EACH_ENTRY( comp
, &package
->components
, MSICOMPONENT
, entry
)
1325 comp
->Action
= msi_get_component_action( package
, comp
);
1326 if (comp
->Action
!= INSTALLSTATE_ABSENT
) continue;
1328 if (comp
->Attributes
& msidbComponentAttributesPermanent
)
1330 TRACE("permanent component, not removing directory\n");
1333 if (comp
->assembly
&& !comp
->assembly
->application
)
1334 msi_uninstall_assembly( package
, comp
);
1337 MSIFOLDER
*folder
= msi_get_loaded_folder( package
, comp
->Directory
);
1338 remove_folder( folder
);
1341 return ERROR_SUCCESS
;