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
40 #include "wine/debug.h"
49 #include "wine/unicode.h"
51 WINE_DEFAULT_DEBUG_CHANNEL(msi
);
53 static void msi_file_update_ui( MSIPACKAGE
*package
, MSIFILE
*f
, const WCHAR
*action
)
57 uirow
= MSI_CreateRecord( 9 );
58 MSI_RecordSetStringW( uirow
, 1, f
->FileName
);
59 MSI_RecordSetStringW( uirow
, 9, f
->Component
->Directory
);
60 MSI_RecordSetInteger( uirow
, 6, f
->FileSize
);
61 MSI_ProcessMessage(package
, INSTALLMESSAGE_ACTIONDATA
, uirow
);
62 msiobj_release( &uirow
->hdr
);
63 msi_ui_progress( package
, 2, f
->FileSize
, 0, 0 );
66 static BOOL
is_registered_patch_media( MSIPACKAGE
*package
, UINT disk_id
)
70 LIST_FOR_EACH_ENTRY( patch
, &package
->patches
, MSIPATCHINFO
, entry
)
72 if (patch
->disk_id
== disk_id
&& patch
->registered
) return TRUE
;
77 static BOOL
is_obsoleted_by_patch( MSIPACKAGE
*package
, MSIFILE
*file
)
79 if (!list_empty( &package
->patches
) && file
->disk_id
< MSI_INITIAL_MEDIA_TRANSFORM_DISKID
)
81 if (!msi_get_property_int( package
->db
, szInstalled
, 0 )) return FALSE
;
84 if (is_registered_patch_media( package
, file
->disk_id
)) return TRUE
;
88 static msi_file_state
calculate_install_state( MSIPACKAGE
*package
, MSIFILE
*file
)
90 MSICOMPONENT
*comp
= file
->Component
;
91 VS_FIXEDFILEINFO
*file_version
;
96 comp
->Action
= msi_get_component_action( package
, comp
);
97 if (!comp
->Enabled
|| comp
->Action
!= INSTALLSTATE_LOCAL
|| (comp
->assembly
&& comp
->assembly
->installed
))
99 TRACE("skipping %s (not scheduled for install)\n", debugstr_w(file
->File
));
100 return msifs_skipped
;
102 if (is_obsoleted_by_patch( package
, file
))
104 TRACE("skipping %s (obsoleted by patch)\n", debugstr_w(file
->File
));
105 return msifs_skipped
;
107 if ((msi_is_global_assembly( comp
) && !comp
->assembly
->installed
) ||
108 GetFileAttributesW( file
->TargetPath
) == INVALID_FILE_ATTRIBUTES
)
110 TRACE("installing %s (missing)\n", debugstr_w(file
->File
));
111 return msifs_missing
;
115 if ((file_version
= msi_get_disk_file_version( file
->TargetPath
)))
117 if (msi_compare_file_versions( file_version
, file
->Version
) < 0)
119 TRACE("overwriting %s (new version %s old version %u.%u.%u.%u)\n",
120 debugstr_w(file
->File
), debugstr_w(file
->Version
),
121 HIWORD(file_version
->dwFileVersionMS
), LOWORD(file_version
->dwFileVersionMS
),
122 HIWORD(file_version
->dwFileVersionLS
), LOWORD(file_version
->dwFileVersionLS
));
123 state
= msifs_overwrite
;
127 TRACE("keeping %s (new version %s old version %u.%u.%u.%u)\n",
128 debugstr_w(file
->File
), debugstr_w(file
->Version
),
129 HIWORD(file_version
->dwFileVersionMS
), LOWORD(file_version
->dwFileVersionMS
),
130 HIWORD(file_version
->dwFileVersionLS
), LOWORD(file_version
->dwFileVersionLS
));
131 state
= msifs_present
;
133 msi_free( file_version
);
136 else if ((font_version
= msi_font_version_from_file( file
->TargetPath
)))
138 if (msi_compare_font_versions( font_version
, file
->Version
) < 0)
140 TRACE("overwriting %s (new version %s old version %s)\n",
141 debugstr_w(file
->File
), debugstr_w(file
->Version
), debugstr_w(font_version
));
142 state
= msifs_overwrite
;
146 TRACE("keeping %s (new version %s old version %s)\n",
147 debugstr_w(file
->File
), debugstr_w(file
->Version
), debugstr_w(font_version
));
148 state
= msifs_present
;
150 msi_free( font_version
);
154 if ((size
= msi_get_disk_file_size( file
->TargetPath
)) != file
->FileSize
)
156 TRACE("overwriting %s (old size %u new size %u)\n", debugstr_w(file
->File
), size
, file
->FileSize
);
157 return msifs_overwrite
;
159 if (file
->hash
.dwFileHashInfoSize
)
161 if (msi_file_hash_matches( file
))
163 TRACE("keeping %s (hash match)\n", debugstr_w(file
->File
));
164 return msifs_hashmatch
;
168 TRACE("overwriting %s (hash mismatch)\n", debugstr_w(file
->File
));
169 return msifs_overwrite
;
173 TRACE("keeping %s\n", debugstr_w(file
->File
));
174 return msifs_present
;
177 static void schedule_install_files(MSIPACKAGE
*package
)
181 LIST_FOR_EACH_ENTRY(file
, &package
->files
, MSIFILE
, entry
)
183 MSICOMPONENT
*comp
= file
->Component
;
185 file
->state
= calculate_install_state( package
, file
);
186 if (file
->state
== msifs_overwrite
&& (comp
->Attributes
& msidbComponentAttributesNeverOverwrite
))
188 TRACE("not overwriting %s\n", debugstr_w(file
->TargetPath
));
189 file
->state
= msifs_skipped
;
194 static UINT
copy_file(MSIFILE
*file
, LPWSTR source
)
198 ret
= CopyFileW(source
, file
->TargetPath
, FALSE
);
200 return GetLastError();
202 SetFileAttributesW(file
->TargetPath
, FILE_ATTRIBUTE_NORMAL
);
204 file
->state
= msifs_installed
;
205 return ERROR_SUCCESS
;
208 static UINT
copy_install_file(MSIPACKAGE
*package
, MSIFILE
*file
, LPWSTR source
)
212 TRACE("Copying %s to %s\n", debugstr_w(source
), debugstr_w(file
->TargetPath
));
214 gle
= copy_file(file
, source
);
215 if (gle
== ERROR_SUCCESS
)
218 if (gle
== ERROR_ALREADY_EXISTS
&& file
->state
== msifs_overwrite
)
220 TRACE("overwriting existing file\n");
221 return ERROR_SUCCESS
;
223 else if (gle
== ERROR_ACCESS_DENIED
)
225 SetFileAttributesW(file
->TargetPath
, FILE_ATTRIBUTE_NORMAL
);
227 gle
= copy_file(file
, source
);
228 TRACE("Overwriting existing file: %d\n", gle
);
230 if (gle
== ERROR_SHARING_VIOLATION
|| gle
== ERROR_USER_MAPPED_FILE
)
232 WCHAR
*tmpfileW
, *pathW
, *p
;
235 TRACE("file in use, scheduling rename operation\n");
237 if (!(pathW
= strdupW( file
->TargetPath
))) return ERROR_OUTOFMEMORY
;
238 if ((p
= strrchrW(pathW
, '\\'))) *p
= 0;
239 len
= strlenW( pathW
) + 16;
240 if (!(tmpfileW
= msi_alloc(len
* sizeof(WCHAR
))))
243 return ERROR_OUTOFMEMORY
;
245 if (!GetTempFileNameW( pathW
, szMsi
, 0, tmpfileW
)) tmpfileW
[0] = 0;
248 if (CopyFileW(source
, tmpfileW
, FALSE
) &&
249 MoveFileExW(file
->TargetPath
, NULL
, MOVEFILE_DELAY_UNTIL_REBOOT
) &&
250 MoveFileExW(tmpfileW
, file
->TargetPath
, MOVEFILE_DELAY_UNTIL_REBOOT
))
252 file
->state
= msifs_installed
;
253 package
->need_reboot_at_end
= 1;
258 gle
= GetLastError();
259 WARN("failed to schedule rename operation: %d)\n", gle
);
260 DeleteFileW( tmpfileW
);
268 static UINT
msi_create_directory( MSIPACKAGE
*package
, const WCHAR
*dir
)
271 const WCHAR
*install_path
;
273 install_path
= msi_get_target_folder( package
, dir
);
274 if (!install_path
) return ERROR_FUNCTION_FAILED
;
276 folder
= msi_get_loaded_folder( package
, dir
);
277 if (folder
->State
== FOLDER_STATE_UNINITIALIZED
)
279 msi_create_full_path( install_path
);
280 folder
->State
= FOLDER_STATE_CREATED
;
282 return ERROR_SUCCESS
;
285 static MSIFILE
*find_file( MSIPACKAGE
*package
, UINT disk_id
, const WCHAR
*filename
)
289 LIST_FOR_EACH_ENTRY( file
, &package
->files
, MSIFILE
, entry
)
291 if (file
->disk_id
== disk_id
&&
292 file
->state
!= msifs_installed
&&
293 !strcmpiW( filename
, file
->File
)) return file
;
298 static BOOL
installfiles_cb(MSIPACKAGE
*package
, LPCWSTR filename
, DWORD action
,
299 LPWSTR
*path
, DWORD
*attrs
, PVOID user
)
301 MSIFILE
*file
= *(MSIFILE
**)user
;
303 if (action
== MSICABEXTRACT_BEGINEXTRACT
)
305 if (!(file
= find_file( package
, file
->disk_id
, filename
)))
307 TRACE("unknown file in cabinet (%s)\n", debugstr_w(filename
));
310 if (file
->state
!= msifs_missing
&& file
->state
!= msifs_overwrite
)
313 if (!msi_is_global_assembly( file
->Component
))
315 msi_create_directory( package
, file
->Component
->Directory
);
317 *path
= strdupW( file
->TargetPath
);
318 *attrs
= file
->Attributes
;
319 *(MSIFILE
**)user
= file
;
321 else if (action
== MSICABEXTRACT_FILEEXTRACTED
)
323 if (!msi_is_global_assembly( file
->Component
)) file
->state
= msifs_installed
;
329 WCHAR
*msi_resolve_file_source( MSIPACKAGE
*package
, MSIFILE
*file
)
333 TRACE("Working to resolve source of file %s\n", debugstr_w(file
->File
));
335 if (file
->IsCompressed
) return NULL
;
337 p
= msi_resolve_source_folder( package
, file
->Component
->Directory
, NULL
);
338 path
= msi_build_directory_name( 2, p
, file
->ShortName
);
340 if (file
->LongName
&& GetFileAttributesW( path
) == INVALID_FILE_ATTRIBUTES
)
343 path
= msi_build_directory_name( 2, p
, file
->LongName
);
346 TRACE("file %s source resolves to %s\n", debugstr_w(file
->File
), debugstr_w(path
));
351 * ACTION_InstallFiles()
353 * For efficiency, this is done in two passes:
354 * 1) Correct all the TargetPaths and determine what files are to be installed.
355 * 2) Extract Cabinets and copy files.
357 UINT
ACTION_InstallFiles(MSIPACKAGE
*package
)
360 UINT rc
= ERROR_SUCCESS
;
363 schedule_install_files(package
);
364 mi
= msi_alloc_zero( sizeof(MSIMEDIAINFO
) );
366 LIST_FOR_EACH_ENTRY( file
, &package
->files
, MSIFILE
, entry
)
368 msi_file_update_ui( package
, file
, szInstallFiles
);
370 rc
= msi_load_media_info( package
, file
->Sequence
, mi
);
371 if (rc
!= ERROR_SUCCESS
)
373 ERR("Unable to load media info for %s (%u)\n", debugstr_w(file
->File
), rc
);
374 rc
= ERROR_FUNCTION_FAILED
;
378 if (file
->state
!= msifs_hashmatch
&&
379 file
->state
!= msifs_skipped
&&
380 (file
->state
!= msifs_present
|| !msi_get_property_int( package
->db
, szInstalled
, 0 )) &&
381 (rc
= ready_media( package
, file
->IsCompressed
, mi
)))
383 ERR("Failed to ready media for %s\n", debugstr_w(file
->File
));
387 if (file
->state
!= msifs_missing
&& !mi
->is_continuous
&& file
->state
!= msifs_overwrite
)
390 if (file
->Sequence
> mi
->last_sequence
|| mi
->is_continuous
||
391 (file
->IsCompressed
&& !mi
->is_extracted
))
394 MSIFILE
*cursor
= file
;
397 data
.package
= package
;
398 data
.cb
= installfiles_cb
;
401 if (file
->IsCompressed
&& !msi_cabextract(package
, mi
, &data
))
403 ERR("Failed to extract cabinet: %s\n", debugstr_w(mi
->cabinet
));
404 rc
= ERROR_INSTALL_FAILURE
;
409 if (!file
->IsCompressed
)
411 WCHAR
*source
= msi_resolve_file_source(package
, file
);
413 TRACE("copying %s to %s\n", debugstr_w(source
), debugstr_w(file
->TargetPath
));
415 if (!msi_is_global_assembly( file
->Component
))
417 msi_create_directory(package
, file
->Component
->Directory
);
419 rc
= copy_install_file(package
, file
, source
);
420 if (rc
!= ERROR_SUCCESS
)
422 ERR("Failed to copy %s to %s (%u)\n", debugstr_w(source
), debugstr_w(file
->TargetPath
), rc
);
423 rc
= ERROR_INSTALL_FAILURE
;
429 else if (!msi_is_global_assembly( file
->Component
) &&
430 file
->state
!= msifs_installed
&& !(file
->Attributes
& msidbFileAttributesPatchAdded
))
432 ERR("compressed file wasn't installed (%s)\n", debugstr_w(file
->File
));
433 rc
= ERROR_INSTALL_FAILURE
;
437 LIST_FOR_EACH_ENTRY( file
, &package
->files
, MSIFILE
, entry
)
439 MSICOMPONENT
*comp
= file
->Component
;
441 if (!msi_is_global_assembly( comp
) || comp
->assembly
->installed
||
442 (file
->state
!= msifs_missing
&& file
->state
!= msifs_overwrite
)) continue;
444 rc
= msi_install_assembly( package
, comp
);
445 if (rc
!= ERROR_SUCCESS
)
447 ERR("Failed to install assembly\n");
448 rc
= ERROR_INSTALL_FAILURE
;
451 file
->state
= msifs_installed
;
455 msi_free_media_info(mi
);
459 static MSIFILEPATCH
*find_filepatch( MSIPACKAGE
*package
, UINT disk_id
, const WCHAR
*key
)
463 LIST_FOR_EACH_ENTRY( patch
, &package
->filepatches
, MSIFILEPATCH
, entry
)
465 if (!patch
->extracted
&& patch
->disk_id
== disk_id
&& !strcmpW( key
, patch
->File
->File
))
471 static BOOL
patchfiles_cb(MSIPACKAGE
*package
, LPCWSTR file
, DWORD action
,
472 LPWSTR
*path
, DWORD
*attrs
, PVOID user
)
474 MSIFILEPATCH
*patch
= *(MSIFILEPATCH
**)user
;
476 if (action
== MSICABEXTRACT_BEGINEXTRACT
)
480 if (is_registered_patch_media( package
, patch
->disk_id
) ||
481 !(patch
= find_filepatch( package
, patch
->disk_id
, file
))) return FALSE
;
483 comp
= patch
->File
->Component
;
484 comp
->Action
= msi_get_component_action( package
, comp
);
485 if (!comp
->Enabled
|| comp
->Action
!= INSTALLSTATE_LOCAL
)
487 TRACE("file %s component %s not installed or disabled\n",
488 debugstr_w(patch
->File
->File
), debugstr_w(comp
->Component
));
492 patch
->path
= msi_create_temp_file( package
->db
);
493 *path
= strdupW( patch
->path
);
494 *attrs
= patch
->File
->Attributes
;
495 *(MSIFILEPATCH
**)user
= patch
;
497 else if (action
== MSICABEXTRACT_FILEEXTRACTED
)
499 patch
->extracted
= TRUE
;
505 static UINT
patch_file( MSIPACKAGE
*package
, MSIFILEPATCH
*patch
)
507 UINT r
= ERROR_SUCCESS
;
508 WCHAR
*tmpfile
= msi_create_temp_file( package
->db
);
510 if (!tmpfile
) return ERROR_INSTALL_FAILURE
;
511 if (ApplyPatchToFileW( patch
->path
, patch
->File
->TargetPath
, tmpfile
, 0 ))
513 DeleteFileW( patch
->File
->TargetPath
);
514 MoveFileW( tmpfile
, patch
->File
->TargetPath
);
518 WARN("failed to patch %s: %08x\n", debugstr_w(patch
->File
->TargetPath
), GetLastError());
519 r
= ERROR_INSTALL_FAILURE
;
521 DeleteFileW( patch
->path
);
522 DeleteFileW( tmpfile
);
527 static UINT
patch_assembly( MSIPACKAGE
*package
, MSIASSEMBLY
*assembly
, MSIFILEPATCH
*patch
)
529 UINT r
= ERROR_FUNCTION_FAILED
;
533 if (!(iter
= msi_create_assembly_enum( package
, assembly
->display_name
)))
534 return ERROR_FUNCTION_FAILED
;
536 while ((IAssemblyEnum_GetNextAssembly( iter
, NULL
, &name
, 0 ) == S_OK
))
538 WCHAR
*displayname
, *path
;
542 hr
= IAssemblyName_GetDisplayName( name
, NULL
, &len
, 0 );
543 if (hr
!= E_NOT_SUFFICIENT_BUFFER
|| !(displayname
= msi_alloc( len
* sizeof(WCHAR
) )))
546 hr
= IAssemblyName_GetDisplayName( name
, displayname
, &len
, 0 );
549 msi_free( displayname
);
553 if ((path
= msi_get_assembly_path( package
, displayname
)))
555 if (!CopyFileW( path
, patch
->File
->TargetPath
, FALSE
))
557 ERR("Failed to copy file %s -> %s (%u)\n", debugstr_w(path
),
558 debugstr_w(patch
->File
->TargetPath
), GetLastError() );
560 msi_free( displayname
);
561 IAssemblyName_Release( name
);
564 r
= patch_file( package
, patch
);
568 msi_free( displayname
);
569 IAssemblyName_Release( name
);
570 if (r
== ERROR_SUCCESS
) break;
573 IAssemblyEnum_Release( iter
);
577 UINT
ACTION_PatchFiles( MSIPACKAGE
*package
)
581 UINT rc
= ERROR_SUCCESS
;
583 TRACE("%p\n", package
);
585 mi
= msi_alloc_zero( sizeof(MSIMEDIAINFO
) );
587 TRACE("extracting files\n");
589 LIST_FOR_EACH_ENTRY( patch
, &package
->filepatches
, MSIFILEPATCH
, entry
)
591 MSIFILE
*file
= patch
->File
;
592 MSICOMPONENT
*comp
= file
->Component
;
594 rc
= msi_load_media_info( package
, patch
->Sequence
, mi
);
595 if (rc
!= ERROR_SUCCESS
)
597 ERR("Unable to load media info for %s (%u)\n", debugstr_w(file
->File
), rc
);
598 rc
= ERROR_FUNCTION_FAILED
;
601 comp
->Action
= msi_get_component_action( package
, comp
);
602 if (!comp
->Enabled
|| comp
->Action
!= INSTALLSTATE_LOCAL
) continue;
604 if (!patch
->extracted
)
607 MSIFILEPATCH
*cursor
= patch
;
609 rc
= ready_media( package
, TRUE
, mi
);
610 if (rc
!= ERROR_SUCCESS
)
612 ERR("Failed to ready media for %s\n", debugstr_w(file
->File
));
616 data
.package
= package
;
617 data
.cb
= patchfiles_cb
;
620 if (!msi_cabextract( package
, mi
, &data
))
622 ERR("Failed to extract cabinet: %s\n", debugstr_w(mi
->cabinet
));
623 rc
= ERROR_INSTALL_FAILURE
;
629 TRACE("applying patches\n");
631 LIST_FOR_EACH_ENTRY( patch
, &package
->filepatches
, MSIFILEPATCH
, entry
)
633 MSICOMPONENT
*comp
= patch
->File
->Component
;
635 if (!patch
->path
) continue;
637 if (msi_is_global_assembly( comp
))
638 rc
= patch_assembly( package
, comp
->assembly
, patch
);
640 rc
= patch_file( package
, patch
);
642 if (rc
&& !(patch
->Attributes
& msidbPatchAttributesNonVital
))
644 ERR("Failed to apply patch to file: %s\n", debugstr_w(patch
->File
->File
));
648 if (msi_is_global_assembly( comp
))
650 if ((rc
= msi_install_assembly( package
, comp
)))
652 ERR("Failed to install patched assembly\n");
659 msi_free_media_info(mi
);
663 #define is_dot_dir(x) ((x[0] == '.') && ((x[1] == 0) || ((x[1] == '.') && (x[2] == 0))))
674 static BOOL
msi_move_file(LPCWSTR source
, LPCWSTR dest
, int options
)
678 if (GetFileAttributesW(source
) == FILE_ATTRIBUTE_DIRECTORY
||
679 GetFileAttributesW(dest
) == FILE_ATTRIBUTE_DIRECTORY
)
681 WARN("Source or dest is directory, not moving\n");
685 if (options
== msidbMoveFileOptionsMove
)
687 TRACE("moving %s -> %s\n", debugstr_w(source
), debugstr_w(dest
));
688 ret
= MoveFileExW(source
, dest
, MOVEFILE_REPLACE_EXISTING
);
691 WARN("MoveFile failed: %d\n", GetLastError());
697 TRACE("copying %s -> %s\n", debugstr_w(source
), debugstr_w(dest
));
698 ret
= CopyFileW(source
, dest
, FALSE
);
701 WARN("CopyFile failed: %d\n", GetLastError());
709 static LPWSTR
wildcard_to_file(LPWSTR wildcard
, LPWSTR filename
)
712 DWORD dirlen
, pathlen
;
714 ptr
= strrchrW(wildcard
, '\\');
715 dirlen
= ptr
- wildcard
+ 1;
717 pathlen
= dirlen
+ lstrlenW(filename
) + 1;
718 path
= msi_alloc(pathlen
* sizeof(WCHAR
));
720 lstrcpynW(path
, wildcard
, dirlen
+ 1);
721 lstrcatW(path
, filename
);
726 static void free_file_entry(FILE_LIST
*file
)
728 msi_free(file
->source
);
729 msi_free(file
->dest
);
733 static void free_list(FILE_LIST
*list
)
735 while (!list_empty(&list
->entry
))
737 FILE_LIST
*file
= LIST_ENTRY(list_head(&list
->entry
), FILE_LIST
, entry
);
739 list_remove(&file
->entry
);
740 free_file_entry(file
);
744 static BOOL
add_wildcard(FILE_LIST
*files
, LPWSTR source
, LPWSTR dest
)
746 FILE_LIST
*new, *file
;
747 LPWSTR ptr
, filename
;
750 new = msi_alloc_zero(sizeof(FILE_LIST
));
754 new->source
= strdupW(source
);
755 ptr
= strrchrW(dest
, '\\') + 1;
756 filename
= strrchrW(new->source
, '\\') + 1;
758 new->sourcename
= filename
;
763 new->destname
= new->sourcename
;
765 size
= (ptr
- dest
) + lstrlenW(filename
) + 1;
766 new->dest
= msi_alloc(size
* sizeof(WCHAR
));
769 free_file_entry(new);
773 lstrcpynW(new->dest
, dest
, ptr
- dest
+ 1);
774 lstrcatW(new->dest
, filename
);
776 if (list_empty(&files
->entry
))
778 list_add_head(&files
->entry
, &new->entry
);
782 LIST_FOR_EACH_ENTRY(file
, &files
->entry
, FILE_LIST
, entry
)
784 if (strcmpW( source
, file
->source
) < 0)
786 list_add_before(&file
->entry
, &new->entry
);
791 list_add_after(&file
->entry
, &new->entry
);
795 static BOOL
move_files_wildcard(LPWSTR source
, LPWSTR dest
, int options
)
797 WIN32_FIND_DATAW wfd
;
801 FILE_LIST files
, *file
;
804 hfile
= FindFirstFileW(source
, &wfd
);
805 if (hfile
== INVALID_HANDLE_VALUE
) return FALSE
;
807 list_init(&files
.entry
);
809 for (res
= TRUE
; res
; res
= FindNextFileW(hfile
, &wfd
))
811 if (is_dot_dir(wfd
.cFileName
)) continue;
813 path
= wildcard_to_file(source
, wfd
.cFileName
);
820 add_wildcard(&files
, path
, dest
);
824 /* no files match the wildcard */
825 if (list_empty(&files
.entry
))
828 /* only the first wildcard match gets renamed to dest */
829 file
= LIST_ENTRY(list_head(&files
.entry
), FILE_LIST
, entry
);
830 size
= (strrchrW(file
->dest
, '\\') - file
->dest
) + lstrlenW(file
->destname
) + 2;
831 file
->dest
= msi_realloc(file
->dest
, size
* sizeof(WCHAR
));
838 /* file->dest may be shorter after the reallocation, so add a NULL
839 * terminator. This is needed for the call to strrchrW, as there will no
840 * longer be a NULL terminator within the bounds of the allocation in this case.
842 file
->dest
[size
- 1] = '\0';
843 lstrcpyW(strrchrW(file
->dest
, '\\') + 1, file
->destname
);
845 while (!list_empty(&files
.entry
))
847 file
= LIST_ENTRY(list_head(&files
.entry
), FILE_LIST
, entry
);
849 msi_move_file(file
->source
, file
->dest
, options
);
851 list_remove(&file
->entry
);
852 free_file_entry(file
);
863 void msi_reduce_to_long_filename( WCHAR
*filename
)
865 WCHAR
*p
= strchrW( filename
, '|' );
866 if (p
) memmove( filename
, p
+ 1, (strlenW( p
+ 1 ) + 1) * sizeof(WCHAR
) );
869 static UINT
ITERATE_MoveFiles( MSIRECORD
*rec
, LPVOID param
)
871 MSIPACKAGE
*package
= param
;
874 LPCWSTR sourcename
, component
;
875 LPWSTR sourcedir
, destname
= NULL
, destdir
= NULL
, source
= NULL
, dest
= NULL
;
880 component
= MSI_RecordGetString(rec
, 2);
881 comp
= msi_get_loaded_component(package
, component
);
883 return ERROR_SUCCESS
;
885 comp
->Action
= msi_get_component_action( package
, comp
);
886 if (comp
->Action
!= INSTALLSTATE_LOCAL
)
888 TRACE("component not scheduled for installation %s\n", debugstr_w(component
));
889 return ERROR_SUCCESS
;
892 sourcename
= MSI_RecordGetString(rec
, 3);
893 options
= MSI_RecordGetInteger(rec
, 7);
895 sourcedir
= msi_dup_property(package
->db
, MSI_RecordGetString(rec
, 5));
899 destdir
= msi_dup_property(package
->db
, MSI_RecordGetString(rec
, 6));
905 if (GetFileAttributesW(sourcedir
) == INVALID_FILE_ATTRIBUTES
)
908 source
= strdupW(sourcedir
);
914 size
= lstrlenW(sourcedir
) + lstrlenW(sourcename
) + 2;
915 source
= msi_alloc(size
* sizeof(WCHAR
));
919 lstrcpyW(source
, sourcedir
);
920 if (source
[lstrlenW(source
) - 1] != '\\')
921 lstrcatW(source
, szBackSlash
);
922 lstrcatW(source
, sourcename
);
925 wildcards
= strchrW(source
, '*') || strchrW(source
, '?');
927 if (MSI_RecordIsNull(rec
, 4))
933 destname
= strdupW(sourcename
);
934 else if ((p
= strrchrW(sourcedir
, '\\')))
935 destname
= strdupW(p
+ 1);
937 destname
= strdupW(sourcedir
);
944 destname
= strdupW(MSI_RecordGetString(rec
, 4));
945 if (destname
) msi_reduce_to_long_filename(destname
);
950 size
= lstrlenW(destname
);
952 size
+= lstrlenW(destdir
) + 2;
953 dest
= msi_alloc(size
* sizeof(WCHAR
));
957 lstrcpyW(dest
, destdir
);
958 if (dest
[lstrlenW(dest
) - 1] != '\\')
959 lstrcatW(dest
, szBackSlash
);
962 lstrcatW(dest
, destname
);
964 if (GetFileAttributesW(destdir
) == INVALID_FILE_ATTRIBUTES
)
966 if (!msi_create_full_path(destdir
))
968 WARN("failed to create directory %u\n", GetLastError());
974 msi_move_file(source
, dest
, options
);
976 move_files_wildcard(source
, dest
, options
);
979 uirow
= MSI_CreateRecord( 9 );
980 MSI_RecordSetStringW( uirow
, 1, MSI_RecordGetString(rec
, 1) );
981 MSI_RecordSetInteger( uirow
, 6, 1 ); /* FIXME */
982 MSI_RecordSetStringW( uirow
, 9, destdir
);
983 MSI_ProcessMessage(package
, INSTALLMESSAGE_ACTIONDATA
, uirow
);
984 msiobj_release( &uirow
->hdr
);
992 return ERROR_SUCCESS
;
995 UINT
ACTION_MoveFiles( MSIPACKAGE
*package
)
997 static const WCHAR query
[] = {
998 'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
999 '`','M','o','v','e','F','i','l','e','`',0};
1003 rc
= MSI_DatabaseOpenViewW(package
->db
, query
, &view
);
1004 if (rc
!= ERROR_SUCCESS
)
1005 return ERROR_SUCCESS
;
1007 rc
= MSI_IterateRecords(view
, NULL
, ITERATE_MoveFiles
, package
);
1008 msiobj_release(&view
->hdr
);
1012 static WCHAR
*get_duplicate_filename( MSIPACKAGE
*package
, MSIRECORD
*row
, const WCHAR
*file_key
, const WCHAR
*src
)
1015 WCHAR
*dst_name
, *dst_path
, *dst
;
1017 if (MSI_RecordIsNull( row
, 4 ))
1019 len
= strlenW( src
) + 1;
1020 if (!(dst_name
= msi_alloc( len
* sizeof(WCHAR
)))) return NULL
;
1021 strcpyW( dst_name
, strrchrW( src
, '\\' ) + 1 );
1025 MSI_RecordGetStringW( row
, 4, NULL
, &len
);
1026 if (!(dst_name
= msi_alloc( ++len
* sizeof(WCHAR
) ))) return NULL
;
1027 MSI_RecordGetStringW( row
, 4, dst_name
, &len
);
1028 msi_reduce_to_long_filename( dst_name
);
1031 if (MSI_RecordIsNull( row
, 5 ))
1034 dst_path
= strdupW( src
);
1035 p
= strrchrW( dst_path
, '\\' );
1040 const WCHAR
*dst_key
= MSI_RecordGetString( row
, 5 );
1042 dst_path
= strdupW( msi_get_target_folder( package
, dst_key
) );
1045 /* try a property */
1046 dst_path
= msi_dup_property( package
->db
, dst_key
);
1049 FIXME("Unable to get destination folder, try AppSearch properties\n");
1050 msi_free( dst_name
);
1056 dst
= msi_build_directory_name( 2, dst_path
, dst_name
);
1057 msi_create_full_path( dst_path
);
1059 msi_free( dst_name
);
1060 msi_free( dst_path
);
1064 static UINT
ITERATE_DuplicateFiles(MSIRECORD
*row
, LPVOID param
)
1066 MSIPACKAGE
*package
= param
;
1068 LPCWSTR file_key
, component
;
1073 component
= MSI_RecordGetString(row
,2);
1074 comp
= msi_get_loaded_component(package
, component
);
1076 return ERROR_SUCCESS
;
1078 comp
->Action
= msi_get_component_action( package
, comp
);
1079 if (comp
->Action
!= INSTALLSTATE_LOCAL
)
1081 TRACE("component not scheduled for installation %s\n", debugstr_w(component
));
1082 return ERROR_SUCCESS
;
1085 file_key
= MSI_RecordGetString(row
,3);
1088 ERR("Unable to get file key\n");
1089 return ERROR_FUNCTION_FAILED
;
1092 file
= msi_get_loaded_file( package
, file_key
);
1095 ERR("Original file unknown %s\n", debugstr_w(file_key
));
1096 return ERROR_SUCCESS
;
1099 dest
= get_duplicate_filename( package
, row
, file_key
, file
->TargetPath
);
1102 WARN("Unable to get duplicate filename\n");
1103 return ERROR_SUCCESS
;
1106 TRACE("Duplicating file %s to %s\n", debugstr_w(file
->TargetPath
), debugstr_w(dest
));
1108 if (!CopyFileW( file
->TargetPath
, dest
, TRUE
))
1110 WARN("Failed to copy file %s -> %s (%u)\n",
1111 debugstr_w(file
->TargetPath
), debugstr_w(dest
), GetLastError());
1114 FIXME("We should track these duplicate files as well\n");
1116 uirow
= MSI_CreateRecord( 9 );
1117 MSI_RecordSetStringW( uirow
, 1, MSI_RecordGetString( row
, 1 ) );
1118 MSI_RecordSetInteger( uirow
, 6, file
->FileSize
);
1119 MSI_RecordSetStringW( uirow
, 9, MSI_RecordGetString( row
, 5 ) );
1120 MSI_ProcessMessage(package
, INSTALLMESSAGE_ACTIONDATA
, uirow
);
1121 msiobj_release( &uirow
->hdr
);
1124 return ERROR_SUCCESS
;
1127 UINT
ACTION_DuplicateFiles(MSIPACKAGE
*package
)
1129 static const WCHAR query
[] = {
1130 'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
1131 '`','D','u','p','l','i','c','a','t','e','F','i','l','e','`',0};
1135 rc
= MSI_DatabaseOpenViewW(package
->db
, query
, &view
);
1136 if (rc
!= ERROR_SUCCESS
)
1137 return ERROR_SUCCESS
;
1139 rc
= MSI_IterateRecords(view
, NULL
, ITERATE_DuplicateFiles
, package
);
1140 msiobj_release(&view
->hdr
);
1144 static UINT
ITERATE_RemoveDuplicateFiles( MSIRECORD
*row
, LPVOID param
)
1146 MSIPACKAGE
*package
= param
;
1148 LPCWSTR file_key
, component
;
1153 component
= MSI_RecordGetString( row
, 2 );
1154 comp
= msi_get_loaded_component( package
, component
);
1156 return ERROR_SUCCESS
;
1158 comp
->Action
= msi_get_component_action( package
, comp
);
1159 if (comp
->Action
!= INSTALLSTATE_ABSENT
)
1161 TRACE("component not scheduled for removal %s\n", debugstr_w(component
));
1162 return ERROR_SUCCESS
;
1165 file_key
= MSI_RecordGetString( row
, 3 );
1168 ERR("Unable to get file key\n");
1169 return ERROR_FUNCTION_FAILED
;
1172 file
= msi_get_loaded_file( package
, file_key
);
1175 ERR("Original file unknown %s\n", debugstr_w(file_key
));
1176 return ERROR_SUCCESS
;
1179 dest
= get_duplicate_filename( package
, row
, file_key
, file
->TargetPath
);
1182 WARN("Unable to get duplicate filename\n");
1183 return ERROR_SUCCESS
;
1186 TRACE("Removing duplicate %s of %s\n", debugstr_w(dest
), debugstr_w(file
->TargetPath
));
1188 if (!DeleteFileW( dest
))
1190 WARN("Failed to delete duplicate file %s (%u)\n", debugstr_w(dest
), GetLastError());
1193 uirow
= MSI_CreateRecord( 9 );
1194 MSI_RecordSetStringW( uirow
, 1, MSI_RecordGetString( row
, 1 ) );
1195 MSI_RecordSetStringW( uirow
, 9, MSI_RecordGetString( row
, 5 ) );
1196 MSI_ProcessMessage(package
, INSTALLMESSAGE_ACTIONDATA
, uirow
);
1197 msiobj_release( &uirow
->hdr
);
1200 return ERROR_SUCCESS
;
1203 UINT
ACTION_RemoveDuplicateFiles( MSIPACKAGE
*package
)
1205 static const WCHAR query
[] = {
1206 'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
1207 '`','D','u','p','l','i','c','a','t','e','F','i','l','e','`',0};
1211 rc
= MSI_DatabaseOpenViewW( package
->db
, query
, &view
);
1212 if (rc
!= ERROR_SUCCESS
)
1213 return ERROR_SUCCESS
;
1215 rc
= MSI_IterateRecords( view
, NULL
, ITERATE_RemoveDuplicateFiles
, package
);
1216 msiobj_release( &view
->hdr
);
1220 static BOOL
verify_comp_for_removal(MSICOMPONENT
*comp
, UINT install_mode
)
1223 if (comp
->Action
!= INSTALLSTATE_SOURCE
&&
1224 comp
->Attributes
& msidbComponentAttributesSourceOnly
&&
1225 (install_mode
== msidbRemoveFileInstallModeOnRemove
||
1226 install_mode
== msidbRemoveFileInstallModeOnBoth
)) return TRUE
;
1228 switch (comp
->Action
)
1230 case INSTALLSTATE_LOCAL
:
1231 case INSTALLSTATE_SOURCE
:
1232 if (install_mode
== msidbRemoveFileInstallModeOnInstall
||
1233 install_mode
== msidbRemoveFileInstallModeOnBoth
) return TRUE
;
1235 case INSTALLSTATE_ABSENT
:
1236 if (install_mode
== msidbRemoveFileInstallModeOnRemove
||
1237 install_mode
== msidbRemoveFileInstallModeOnBoth
) return TRUE
;
1244 static UINT
ITERATE_RemoveFiles(MSIRECORD
*row
, LPVOID param
)
1246 MSIPACKAGE
*package
= param
;
1249 LPCWSTR component
, dirprop
;
1251 LPWSTR dir
= NULL
, path
= NULL
, filename
= NULL
;
1253 UINT ret
= ERROR_SUCCESS
;
1255 component
= MSI_RecordGetString(row
, 2);
1256 dirprop
= MSI_RecordGetString(row
, 4);
1257 install_mode
= MSI_RecordGetInteger(row
, 5);
1259 comp
= msi_get_loaded_component(package
, component
);
1261 return ERROR_SUCCESS
;
1263 comp
->Action
= msi_get_component_action( package
, comp
);
1264 if (!verify_comp_for_removal(comp
, install_mode
))
1266 TRACE("Skipping removal due to install mode\n");
1267 return ERROR_SUCCESS
;
1269 if (comp
->assembly
&& !comp
->assembly
->application
)
1271 return ERROR_SUCCESS
;
1273 if (comp
->Attributes
& msidbComponentAttributesPermanent
)
1275 TRACE("permanent component, not removing file\n");
1276 return ERROR_SUCCESS
;
1279 dir
= msi_dup_property(package
->db
, dirprop
);
1282 WARN("directory property has no value\n");
1283 return ERROR_SUCCESS
;
1286 if ((filename
= strdupW( MSI_RecordGetString(row
, 3) )))
1288 msi_reduce_to_long_filename( filename
);
1289 size
= lstrlenW( filename
);
1291 size
+= lstrlenW(dir
) + 2;
1292 path
= msi_alloc(size
* sizeof(WCHAR
));
1295 ret
= ERROR_OUTOFMEMORY
;
1301 lstrcpyW(path
, dir
);
1302 PathAddBackslashW(path
);
1303 lstrcatW(path
, filename
);
1305 TRACE("Deleting misc file: %s\n", debugstr_w(path
));
1310 TRACE("Removing misc directory: %s\n", debugstr_w(dir
));
1311 RemoveDirectoryW(dir
);
1315 uirow
= MSI_CreateRecord( 9 );
1316 MSI_RecordSetStringW( uirow
, 1, MSI_RecordGetString(row
, 1) );
1317 MSI_RecordSetStringW( uirow
, 9, dir
);
1318 MSI_ProcessMessage(package
, INSTALLMESSAGE_ACTIONDATA
, uirow
);
1319 msiobj_release( &uirow
->hdr
);
1327 static void remove_folder( MSIFOLDER
*folder
)
1331 LIST_FOR_EACH_ENTRY( fl
, &folder
->children
, FolderList
, entry
)
1333 remove_folder( fl
->folder
);
1335 if (!folder
->persistent
&& folder
->State
!= FOLDER_STATE_REMOVED
)
1337 if (RemoveDirectoryW( folder
->ResolvedTarget
)) folder
->State
= FOLDER_STATE_REMOVED
;
1341 UINT
ACTION_RemoveFiles( MSIPACKAGE
*package
)
1343 static const WCHAR query
[] = {
1344 'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
1345 '`','R','e','m','o','v','e','F','i','l','e','`',0};
1351 r
= MSI_DatabaseOpenViewW(package
->db
, query
, &view
);
1352 if (r
== ERROR_SUCCESS
)
1354 r
= MSI_IterateRecords(view
, NULL
, ITERATE_RemoveFiles
, package
);
1355 msiobj_release(&view
->hdr
);
1356 if (r
!= ERROR_SUCCESS
)
1360 LIST_FOR_EACH_ENTRY( file
, &package
->files
, MSIFILE
, entry
)
1363 VS_FIXEDFILEINFO
*ver
;
1365 comp
= file
->Component
;
1366 msi_file_update_ui( package
, file
, szRemoveFiles
);
1368 comp
->Action
= msi_get_component_action( package
, comp
);
1369 if (comp
->Action
!= INSTALLSTATE_ABSENT
|| comp
->Installed
== INSTALLSTATE_SOURCE
)
1372 if (comp
->assembly
&& !comp
->assembly
->application
)
1375 if (comp
->Attributes
& msidbComponentAttributesPermanent
)
1377 TRACE("permanent component, not removing file\n");
1383 ver
= msi_get_disk_file_version( file
->TargetPath
);
1384 if (ver
&& msi_compare_file_versions( ver
, file
->Version
) > 0)
1386 TRACE("newer version detected, not removing file\n");
1393 if (file
->state
== msifs_installed
)
1394 WARN("removing installed file %s\n", debugstr_w(file
->TargetPath
));
1396 TRACE("removing %s\n", debugstr_w(file
->File
) );
1398 SetFileAttributesW( file
->TargetPath
, FILE_ATTRIBUTE_NORMAL
);
1399 if (!DeleteFileW( file
->TargetPath
))
1401 WARN("failed to delete %s (%u)\n", debugstr_w(file
->TargetPath
), GetLastError());
1403 file
->state
= msifs_missing
;
1405 uirow
= MSI_CreateRecord( 9 );
1406 MSI_RecordSetStringW( uirow
, 1, file
->FileName
);
1407 MSI_RecordSetStringW( uirow
, 9, comp
->Directory
);
1408 MSI_ProcessMessage(package
, INSTALLMESSAGE_ACTIONDATA
, uirow
);
1409 msiobj_release( &uirow
->hdr
);
1412 LIST_FOR_EACH_ENTRY( comp
, &package
->components
, MSICOMPONENT
, entry
)
1414 comp
->Action
= msi_get_component_action( package
, comp
);
1415 if (comp
->Action
!= INSTALLSTATE_ABSENT
) continue;
1417 if (comp
->Attributes
& msidbComponentAttributesPermanent
)
1419 TRACE("permanent component, not removing directory\n");
1422 if (comp
->assembly
&& !comp
->assembly
->application
)
1423 msi_uninstall_assembly( package
, comp
);
1426 MSIFOLDER
*folder
= msi_get_loaded_folder( package
, comp
->Directory
);
1427 remove_folder( folder
);
1430 return ERROR_SUCCESS
;