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
);
203 return ERROR_SUCCESS
;
206 static UINT
copy_install_file(MSIPACKAGE
*package
, MSIFILE
*file
, LPWSTR source
)
210 TRACE("Copying %s to %s\n", debugstr_w(source
), debugstr_w(file
->TargetPath
));
212 gle
= copy_file(file
, source
);
213 if (gle
== ERROR_SUCCESS
)
216 if (gle
== ERROR_ALREADY_EXISTS
&& file
->state
== msifs_overwrite
)
218 TRACE("overwriting existing file\n");
219 return ERROR_SUCCESS
;
221 else if (gle
== ERROR_ACCESS_DENIED
)
223 SetFileAttributesW(file
->TargetPath
, FILE_ATTRIBUTE_NORMAL
);
225 gle
= copy_file(file
, source
);
226 TRACE("Overwriting existing file: %d\n", gle
);
228 if (gle
== ERROR_SHARING_VIOLATION
|| gle
== ERROR_USER_MAPPED_FILE
)
230 WCHAR
*tmpfileW
, *pathW
, *p
;
233 TRACE("file in use, scheduling rename operation\n");
235 if (!(pathW
= strdupW( file
->TargetPath
))) return ERROR_OUTOFMEMORY
;
236 if ((p
= strrchrW(pathW
, '\\'))) *p
= 0;
237 len
= strlenW( pathW
) + 16;
238 if (!(tmpfileW
= msi_alloc(len
* sizeof(WCHAR
))))
241 return ERROR_OUTOFMEMORY
;
243 if (!GetTempFileNameW( pathW
, szMsi
, 0, tmpfileW
)) tmpfileW
[0] = 0;
246 if (CopyFileW(source
, tmpfileW
, FALSE
) &&
247 MoveFileExW(file
->TargetPath
, NULL
, MOVEFILE_DELAY_UNTIL_REBOOT
) &&
248 MoveFileExW(tmpfileW
, file
->TargetPath
, MOVEFILE_DELAY_UNTIL_REBOOT
))
250 package
->need_reboot_at_end
= 1;
255 gle
= GetLastError();
256 WARN("failed to schedule rename operation: %d)\n", gle
);
257 DeleteFileW( tmpfileW
);
265 static UINT
msi_create_directory( MSIPACKAGE
*package
, const WCHAR
*dir
)
268 const WCHAR
*install_path
;
270 install_path
= msi_get_target_folder( package
, dir
);
271 if (!install_path
) return ERROR_FUNCTION_FAILED
;
273 folder
= msi_get_loaded_folder( package
, dir
);
274 if (folder
->State
== FOLDER_STATE_UNINITIALIZED
)
276 msi_create_full_path( install_path
);
277 folder
->State
= FOLDER_STATE_CREATED
;
279 return ERROR_SUCCESS
;
282 static MSIFILE
*find_file( MSIPACKAGE
*package
, UINT disk_id
, const WCHAR
*filename
)
286 LIST_FOR_EACH_ENTRY( file
, &package
->files
, MSIFILE
, entry
)
288 if (file
->disk_id
== disk_id
&&
289 file
->state
!= msifs_installed
&&
290 !strcmpiW( filename
, file
->File
)) return file
;
295 static BOOL
installfiles_cb(MSIPACKAGE
*package
, LPCWSTR filename
, DWORD action
,
296 LPWSTR
*path
, DWORD
*attrs
, PVOID user
)
298 MSIFILE
*file
= *(MSIFILE
**)user
;
300 if (action
== MSICABEXTRACT_BEGINEXTRACT
)
302 if (!(file
= find_file( package
, file
->disk_id
, filename
)))
304 TRACE("unknown file in cabinet (%s)\n", debugstr_w(filename
));
307 if (file
->state
!= msifs_missing
&& file
->state
!= msifs_overwrite
)
310 if (!msi_is_global_assembly( file
->Component
))
312 msi_create_directory( package
, file
->Component
->Directory
);
314 *path
= strdupW( file
->TargetPath
);
315 *attrs
= file
->Attributes
;
316 *(MSIFILE
**)user
= file
;
318 else if (action
== MSICABEXTRACT_FILEEXTRACTED
)
320 if (!msi_is_global_assembly( file
->Component
)) file
->state
= msifs_installed
;
326 WCHAR
*msi_resolve_file_source( MSIPACKAGE
*package
, MSIFILE
*file
)
330 TRACE("Working to resolve source of file %s\n", debugstr_w(file
->File
));
332 if (file
->IsCompressed
) return NULL
;
334 p
= msi_resolve_source_folder( package
, file
->Component
->Directory
, NULL
);
335 path
= msi_build_directory_name( 2, p
, file
->ShortName
);
337 if (file
->LongName
&& GetFileAttributesW( path
) == INVALID_FILE_ATTRIBUTES
)
340 path
= msi_build_directory_name( 2, p
, file
->LongName
);
343 TRACE("file %s source resolves to %s\n", debugstr_w(file
->File
), debugstr_w(path
));
348 * ACTION_InstallFiles()
350 * For efficiency, this is done in two passes:
351 * 1) Correct all the TargetPaths and determine what files are to be installed.
352 * 2) Extract Cabinets and copy files.
354 UINT
ACTION_InstallFiles(MSIPACKAGE
*package
)
357 UINT rc
= ERROR_SUCCESS
;
360 msi_set_sourcedir_props(package
, FALSE
);
362 if (package
->script
== SCRIPT_NONE
)
363 return msi_schedule_action(package
, SCRIPT_INSTALL
, szInstallFiles
);
365 schedule_install_files(package
);
366 mi
= msi_alloc_zero( sizeof(MSIMEDIAINFO
) );
368 LIST_FOR_EACH_ENTRY( file
, &package
->files
, MSIFILE
, entry
)
370 BOOL is_global_assembly
= msi_is_global_assembly( file
->Component
);
372 msi_file_update_ui( package
, file
, szInstallFiles
);
374 rc
= msi_load_media_info( package
, file
->Sequence
, mi
);
375 if (rc
!= ERROR_SUCCESS
)
377 ERR("Unable to load media info for %s (%u)\n", debugstr_w(file
->File
), rc
);
378 rc
= ERROR_FUNCTION_FAILED
;
382 if (file
->state
!= msifs_hashmatch
&&
383 file
->state
!= msifs_skipped
&&
384 (file
->state
!= msifs_present
|| !msi_get_property_int( package
->db
, szInstalled
, 0 )) &&
385 (rc
= ready_media( package
, file
->IsCompressed
, mi
)))
387 ERR("Failed to ready media for %s\n", debugstr_w(file
->File
));
391 if (file
->state
!= msifs_missing
&& !mi
->is_continuous
&& file
->state
!= msifs_overwrite
)
394 if (file
->Sequence
> mi
->last_sequence
|| mi
->is_continuous
||
395 (file
->IsCompressed
&& !mi
->is_extracted
))
398 MSIFILE
*cursor
= file
;
401 data
.package
= package
;
402 data
.cb
= installfiles_cb
;
405 if (file
->IsCompressed
&& !msi_cabextract(package
, mi
, &data
))
407 ERR("Failed to extract cabinet: %s\n", debugstr_w(mi
->cabinet
));
408 rc
= ERROR_INSTALL_FAILURE
;
413 if (!file
->IsCompressed
)
415 WCHAR
*source
= msi_resolve_file_source(package
, file
);
417 TRACE("copying %s to %s\n", debugstr_w(source
), debugstr_w(file
->TargetPath
));
419 if (!is_global_assembly
)
421 msi_create_directory(package
, file
->Component
->Directory
);
423 rc
= copy_install_file(package
, file
, source
);
424 if (rc
!= ERROR_SUCCESS
)
426 ERR("Failed to copy %s to %s (%u)\n", debugstr_w(source
), debugstr_w(file
->TargetPath
), rc
);
427 rc
= ERROR_INSTALL_FAILURE
;
431 if (!is_global_assembly
) file
->state
= msifs_installed
;
434 else if (!is_global_assembly
&& file
->state
!= msifs_installed
&&
435 !(file
->Attributes
& msidbFileAttributesPatchAdded
))
437 ERR("compressed file wasn't installed (%s)\n", debugstr_w(file
->File
));
438 rc
= ERROR_INSTALL_FAILURE
;
442 LIST_FOR_EACH_ENTRY( file
, &package
->files
, MSIFILE
, entry
)
444 MSICOMPONENT
*comp
= file
->Component
;
446 if (!msi_is_global_assembly( comp
) || comp
->assembly
->installed
||
447 (file
->state
!= msifs_missing
&& file
->state
!= msifs_overwrite
)) continue;
449 rc
= msi_install_assembly( package
, comp
);
450 if (rc
!= ERROR_SUCCESS
)
452 ERR("Failed to install assembly\n");
453 rc
= ERROR_INSTALL_FAILURE
;
456 file
->state
= msifs_installed
;
460 msi_free_media_info(mi
);
464 static MSIFILEPATCH
*find_filepatch( MSIPACKAGE
*package
, UINT disk_id
, const WCHAR
*key
)
468 LIST_FOR_EACH_ENTRY( patch
, &package
->filepatches
, MSIFILEPATCH
, entry
)
470 if (!patch
->extracted
&& patch
->disk_id
== disk_id
&& !strcmpW( key
, patch
->File
->File
))
476 static BOOL
patchfiles_cb(MSIPACKAGE
*package
, LPCWSTR file
, DWORD action
,
477 LPWSTR
*path
, DWORD
*attrs
, PVOID user
)
479 MSIFILEPATCH
*patch
= *(MSIFILEPATCH
**)user
;
481 if (action
== MSICABEXTRACT_BEGINEXTRACT
)
485 if (is_registered_patch_media( package
, patch
->disk_id
) ||
486 !(patch
= find_filepatch( package
, patch
->disk_id
, file
))) return FALSE
;
488 comp
= patch
->File
->Component
;
489 comp
->Action
= msi_get_component_action( package
, comp
);
490 if (!comp
->Enabled
|| comp
->Action
!= INSTALLSTATE_LOCAL
)
492 TRACE("file %s component %s not installed or disabled\n",
493 debugstr_w(patch
->File
->File
), debugstr_w(comp
->Component
));
497 patch
->path
= msi_create_temp_file( package
->db
);
498 *path
= strdupW( patch
->path
);
499 *attrs
= patch
->File
->Attributes
;
500 *(MSIFILEPATCH
**)user
= patch
;
502 else if (action
== MSICABEXTRACT_FILEEXTRACTED
)
504 patch
->extracted
= TRUE
;
510 static UINT
patch_file( MSIPACKAGE
*package
, MSIFILEPATCH
*patch
)
512 UINT r
= ERROR_SUCCESS
;
513 WCHAR
*tmpfile
= msi_create_temp_file( package
->db
);
515 if (!tmpfile
) return ERROR_INSTALL_FAILURE
;
516 if (ApplyPatchToFileW( patch
->path
, patch
->File
->TargetPath
, tmpfile
, 0 ))
518 DeleteFileW( patch
->File
->TargetPath
);
519 MoveFileW( tmpfile
, patch
->File
->TargetPath
);
523 WARN("failed to patch %s: %08x\n", debugstr_w(patch
->File
->TargetPath
), GetLastError());
524 r
= ERROR_INSTALL_FAILURE
;
526 DeleteFileW( patch
->path
);
527 DeleteFileW( tmpfile
);
532 static UINT
patch_assembly( MSIPACKAGE
*package
, MSIASSEMBLY
*assembly
, MSIFILEPATCH
*patch
)
534 UINT r
= ERROR_FUNCTION_FAILED
;
538 if (!(iter
= msi_create_assembly_enum( package
, assembly
->display_name
)))
539 return ERROR_FUNCTION_FAILED
;
541 while ((IAssemblyEnum_GetNextAssembly( iter
, NULL
, &name
, 0 ) == S_OK
))
543 WCHAR
*displayname
, *path
;
547 hr
= IAssemblyName_GetDisplayName( name
, NULL
, &len
, 0 );
548 if (hr
!= E_NOT_SUFFICIENT_BUFFER
|| !(displayname
= msi_alloc( len
* sizeof(WCHAR
) )))
551 hr
= IAssemblyName_GetDisplayName( name
, displayname
, &len
, 0 );
554 msi_free( displayname
);
558 if ((path
= msi_get_assembly_path( package
, displayname
)))
560 if (!CopyFileW( path
, patch
->File
->TargetPath
, FALSE
))
562 ERR("Failed to copy file %s -> %s (%u)\n", debugstr_w(path
),
563 debugstr_w(patch
->File
->TargetPath
), GetLastError() );
565 msi_free( displayname
);
566 IAssemblyName_Release( name
);
569 r
= patch_file( package
, patch
);
573 msi_free( displayname
);
574 IAssemblyName_Release( name
);
575 if (r
== ERROR_SUCCESS
) break;
578 IAssemblyEnum_Release( iter
);
582 UINT
ACTION_PatchFiles( MSIPACKAGE
*package
)
586 UINT rc
= ERROR_SUCCESS
;
588 TRACE("%p\n", package
);
590 if (package
->script
== SCRIPT_NONE
)
591 return msi_schedule_action(package
, SCRIPT_INSTALL
, szPatchFiles
);
593 mi
= msi_alloc_zero( sizeof(MSIMEDIAINFO
) );
595 TRACE("extracting files\n");
597 LIST_FOR_EACH_ENTRY( patch
, &package
->filepatches
, MSIFILEPATCH
, entry
)
599 MSIFILE
*file
= patch
->File
;
600 MSICOMPONENT
*comp
= file
->Component
;
602 rc
= msi_load_media_info( package
, patch
->Sequence
, mi
);
603 if (rc
!= ERROR_SUCCESS
)
605 ERR("Unable to load media info for %s (%u)\n", debugstr_w(file
->File
), rc
);
606 rc
= ERROR_FUNCTION_FAILED
;
609 comp
->Action
= msi_get_component_action( package
, comp
);
610 if (!comp
->Enabled
|| comp
->Action
!= INSTALLSTATE_LOCAL
) continue;
612 if (!patch
->extracted
)
615 MSIFILEPATCH
*cursor
= patch
;
617 rc
= ready_media( package
, TRUE
, mi
);
618 if (rc
!= ERROR_SUCCESS
)
620 ERR("Failed to ready media for %s\n", debugstr_w(file
->File
));
624 data
.package
= package
;
625 data
.cb
= patchfiles_cb
;
628 if (!msi_cabextract( package
, mi
, &data
))
630 ERR("Failed to extract cabinet: %s\n", debugstr_w(mi
->cabinet
));
631 rc
= ERROR_INSTALL_FAILURE
;
637 TRACE("applying patches\n");
639 LIST_FOR_EACH_ENTRY( patch
, &package
->filepatches
, MSIFILEPATCH
, entry
)
641 MSICOMPONENT
*comp
= patch
->File
->Component
;
643 if (!patch
->path
) continue;
645 if (msi_is_global_assembly( comp
))
646 rc
= patch_assembly( package
, comp
->assembly
, patch
);
648 rc
= patch_file( package
, patch
);
650 if (rc
&& !(patch
->Attributes
& msidbPatchAttributesNonVital
))
652 ERR("Failed to apply patch to file: %s\n", debugstr_w(patch
->File
->File
));
656 if (msi_is_global_assembly( comp
))
658 if ((rc
= msi_install_assembly( package
, comp
)))
660 ERR("Failed to install patched assembly\n");
667 msi_free_media_info(mi
);
671 #define is_dot_dir(x) ((x[0] == '.') && ((x[1] == 0) || ((x[1] == '.') && (x[2] == 0))))
682 static BOOL
msi_move_file(LPCWSTR source
, LPCWSTR dest
, int options
)
686 if (GetFileAttributesW(source
) == FILE_ATTRIBUTE_DIRECTORY
||
687 GetFileAttributesW(dest
) == FILE_ATTRIBUTE_DIRECTORY
)
689 WARN("Source or dest is directory, not moving\n");
693 if (options
== msidbMoveFileOptionsMove
)
695 TRACE("moving %s -> %s\n", debugstr_w(source
), debugstr_w(dest
));
696 ret
= MoveFileExW(source
, dest
, MOVEFILE_REPLACE_EXISTING
);
699 WARN("MoveFile failed: %d\n", GetLastError());
705 TRACE("copying %s -> %s\n", debugstr_w(source
), debugstr_w(dest
));
706 ret
= CopyFileW(source
, dest
, FALSE
);
709 WARN("CopyFile failed: %d\n", GetLastError());
717 static LPWSTR
wildcard_to_file(LPWSTR wildcard
, LPWSTR filename
)
720 DWORD dirlen
, pathlen
;
722 ptr
= strrchrW(wildcard
, '\\');
723 dirlen
= ptr
- wildcard
+ 1;
725 pathlen
= dirlen
+ lstrlenW(filename
) + 1;
726 path
= msi_alloc(pathlen
* sizeof(WCHAR
));
728 lstrcpynW(path
, wildcard
, dirlen
+ 1);
729 lstrcatW(path
, filename
);
734 static void free_file_entry(FILE_LIST
*file
)
736 msi_free(file
->source
);
737 msi_free(file
->dest
);
741 static void free_list(FILE_LIST
*list
)
743 while (!list_empty(&list
->entry
))
745 FILE_LIST
*file
= LIST_ENTRY(list_head(&list
->entry
), FILE_LIST
, entry
);
747 list_remove(&file
->entry
);
748 free_file_entry(file
);
752 static BOOL
add_wildcard(FILE_LIST
*files
, LPWSTR source
, LPWSTR dest
)
754 FILE_LIST
*new, *file
;
755 LPWSTR ptr
, filename
;
758 new = msi_alloc_zero(sizeof(FILE_LIST
));
762 new->source
= strdupW(source
);
763 ptr
= strrchrW(dest
, '\\') + 1;
764 filename
= strrchrW(new->source
, '\\') + 1;
766 new->sourcename
= filename
;
771 new->destname
= new->sourcename
;
773 size
= (ptr
- dest
) + lstrlenW(filename
) + 1;
774 new->dest
= msi_alloc(size
* sizeof(WCHAR
));
777 free_file_entry(new);
781 lstrcpynW(new->dest
, dest
, ptr
- dest
+ 1);
782 lstrcatW(new->dest
, filename
);
784 if (list_empty(&files
->entry
))
786 list_add_head(&files
->entry
, &new->entry
);
790 LIST_FOR_EACH_ENTRY(file
, &files
->entry
, FILE_LIST
, entry
)
792 if (strcmpW( source
, file
->source
) < 0)
794 list_add_before(&file
->entry
, &new->entry
);
799 list_add_after(&file
->entry
, &new->entry
);
803 static BOOL
move_files_wildcard(LPWSTR source
, LPWSTR dest
, int options
)
805 WIN32_FIND_DATAW wfd
;
809 FILE_LIST files
, *file
;
812 hfile
= FindFirstFileW(source
, &wfd
);
813 if (hfile
== INVALID_HANDLE_VALUE
) return FALSE
;
815 list_init(&files
.entry
);
817 for (res
= TRUE
; res
; res
= FindNextFileW(hfile
, &wfd
))
819 if (is_dot_dir(wfd
.cFileName
)) continue;
821 path
= wildcard_to_file(source
, wfd
.cFileName
);
828 add_wildcard(&files
, path
, dest
);
832 /* no files match the wildcard */
833 if (list_empty(&files
.entry
))
836 /* only the first wildcard match gets renamed to dest */
837 file
= LIST_ENTRY(list_head(&files
.entry
), FILE_LIST
, entry
);
838 size
= (strrchrW(file
->dest
, '\\') - file
->dest
) + lstrlenW(file
->destname
) + 2;
839 file
->dest
= msi_realloc(file
->dest
, size
* sizeof(WCHAR
));
846 /* file->dest may be shorter after the reallocation, so add a NULL
847 * terminator. This is needed for the call to strrchrW, as there will no
848 * longer be a NULL terminator within the bounds of the allocation in this case.
850 file
->dest
[size
- 1] = '\0';
851 lstrcpyW(strrchrW(file
->dest
, '\\') + 1, file
->destname
);
853 while (!list_empty(&files
.entry
))
855 file
= LIST_ENTRY(list_head(&files
.entry
), FILE_LIST
, entry
);
857 msi_move_file(file
->source
, file
->dest
, options
);
859 list_remove(&file
->entry
);
860 free_file_entry(file
);
871 void msi_reduce_to_long_filename( WCHAR
*filename
)
873 WCHAR
*p
= strchrW( filename
, '|' );
874 if (p
) memmove( filename
, p
+ 1, (strlenW( p
+ 1 ) + 1) * sizeof(WCHAR
) );
877 static UINT
ITERATE_MoveFiles( MSIRECORD
*rec
, LPVOID param
)
879 MSIPACKAGE
*package
= param
;
882 LPCWSTR sourcename
, component
;
883 LPWSTR sourcedir
, destname
= NULL
, destdir
= NULL
, source
= NULL
, dest
= NULL
;
888 component
= MSI_RecordGetString(rec
, 2);
889 comp
= msi_get_loaded_component(package
, component
);
891 return ERROR_SUCCESS
;
893 comp
->Action
= msi_get_component_action( package
, comp
);
894 if (comp
->Action
!= INSTALLSTATE_LOCAL
)
896 TRACE("component not scheduled for installation %s\n", debugstr_w(component
));
897 return ERROR_SUCCESS
;
900 sourcename
= MSI_RecordGetString(rec
, 3);
901 options
= MSI_RecordGetInteger(rec
, 7);
903 sourcedir
= msi_dup_property(package
->db
, MSI_RecordGetString(rec
, 5));
907 destdir
= msi_dup_property(package
->db
, MSI_RecordGetString(rec
, 6));
913 if (GetFileAttributesW(sourcedir
) == INVALID_FILE_ATTRIBUTES
)
916 source
= strdupW(sourcedir
);
922 size
= lstrlenW(sourcedir
) + lstrlenW(sourcename
) + 2;
923 source
= msi_alloc(size
* sizeof(WCHAR
));
927 lstrcpyW(source
, sourcedir
);
928 if (source
[lstrlenW(source
) - 1] != '\\')
929 lstrcatW(source
, szBackSlash
);
930 lstrcatW(source
, sourcename
);
933 wildcards
= strchrW(source
, '*') || strchrW(source
, '?');
935 if (MSI_RecordIsNull(rec
, 4))
941 destname
= strdupW(sourcename
);
942 else if ((p
= strrchrW(sourcedir
, '\\')))
943 destname
= strdupW(p
+ 1);
945 destname
= strdupW(sourcedir
);
952 destname
= strdupW(MSI_RecordGetString(rec
, 4));
953 if (destname
) msi_reduce_to_long_filename(destname
);
958 size
= lstrlenW(destname
);
960 size
+= lstrlenW(destdir
) + 2;
961 dest
= msi_alloc(size
* sizeof(WCHAR
));
965 lstrcpyW(dest
, destdir
);
966 if (dest
[lstrlenW(dest
) - 1] != '\\')
967 lstrcatW(dest
, szBackSlash
);
970 lstrcatW(dest
, destname
);
972 if (GetFileAttributesW(destdir
) == INVALID_FILE_ATTRIBUTES
)
974 if (!msi_create_full_path(destdir
))
976 WARN("failed to create directory %u\n", GetLastError());
982 msi_move_file(source
, dest
, options
);
984 move_files_wildcard(source
, dest
, options
);
987 uirow
= MSI_CreateRecord( 9 );
988 MSI_RecordSetStringW( uirow
, 1, MSI_RecordGetString(rec
, 1) );
989 MSI_RecordSetInteger( uirow
, 6, 1 ); /* FIXME */
990 MSI_RecordSetStringW( uirow
, 9, destdir
);
991 MSI_ProcessMessage(package
, INSTALLMESSAGE_ACTIONDATA
, uirow
);
992 msiobj_release( &uirow
->hdr
);
1000 return ERROR_SUCCESS
;
1003 UINT
ACTION_MoveFiles( MSIPACKAGE
*package
)
1005 static const WCHAR query
[] = {
1006 'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
1007 '`','M','o','v','e','F','i','l','e','`',0};
1011 if (package
->script
== SCRIPT_NONE
)
1012 return msi_schedule_action(package
, SCRIPT_INSTALL
, szMoveFiles
);
1014 rc
= MSI_DatabaseOpenViewW(package
->db
, query
, &view
);
1015 if (rc
!= ERROR_SUCCESS
)
1016 return ERROR_SUCCESS
;
1018 rc
= MSI_IterateRecords(view
, NULL
, ITERATE_MoveFiles
, package
);
1019 msiobj_release(&view
->hdr
);
1023 static WCHAR
*get_duplicate_filename( MSIPACKAGE
*package
, MSIRECORD
*row
, const WCHAR
*file_key
, const WCHAR
*src
)
1026 WCHAR
*dst_name
, *dst_path
, *dst
;
1028 if (MSI_RecordIsNull( row
, 4 ))
1030 len
= strlenW( src
) + 1;
1031 if (!(dst_name
= msi_alloc( len
* sizeof(WCHAR
)))) return NULL
;
1032 strcpyW( dst_name
, strrchrW( src
, '\\' ) + 1 );
1036 MSI_RecordGetStringW( row
, 4, NULL
, &len
);
1037 if (!(dst_name
= msi_alloc( ++len
* sizeof(WCHAR
) ))) return NULL
;
1038 MSI_RecordGetStringW( row
, 4, dst_name
, &len
);
1039 msi_reduce_to_long_filename( dst_name
);
1042 if (MSI_RecordIsNull( row
, 5 ))
1045 dst_path
= strdupW( src
);
1046 p
= strrchrW( dst_path
, '\\' );
1051 const WCHAR
*dst_key
= MSI_RecordGetString( row
, 5 );
1053 dst_path
= strdupW( msi_get_target_folder( package
, dst_key
) );
1056 /* try a property */
1057 dst_path
= msi_dup_property( package
->db
, dst_key
);
1060 FIXME("Unable to get destination folder, try AppSearch properties\n");
1061 msi_free( dst_name
);
1067 dst
= msi_build_directory_name( 2, dst_path
, dst_name
);
1068 msi_create_full_path( dst_path
);
1070 msi_free( dst_name
);
1071 msi_free( dst_path
);
1075 static UINT
ITERATE_DuplicateFiles(MSIRECORD
*row
, LPVOID param
)
1077 MSIPACKAGE
*package
= param
;
1079 LPCWSTR file_key
, component
;
1084 component
= MSI_RecordGetString(row
,2);
1085 comp
= msi_get_loaded_component(package
, component
);
1087 return ERROR_SUCCESS
;
1089 comp
->Action
= msi_get_component_action( package
, comp
);
1090 if (comp
->Action
!= INSTALLSTATE_LOCAL
)
1092 TRACE("component not scheduled for installation %s\n", debugstr_w(component
));
1093 return ERROR_SUCCESS
;
1096 file_key
= MSI_RecordGetString(row
,3);
1099 ERR("Unable to get file key\n");
1100 return ERROR_FUNCTION_FAILED
;
1103 file
= msi_get_loaded_file( package
, file_key
);
1106 ERR("Original file unknown %s\n", debugstr_w(file_key
));
1107 return ERROR_SUCCESS
;
1110 dest
= get_duplicate_filename( package
, row
, file_key
, file
->TargetPath
);
1113 WARN("Unable to get duplicate filename\n");
1114 return ERROR_SUCCESS
;
1117 TRACE("Duplicating file %s to %s\n", debugstr_w(file
->TargetPath
), debugstr_w(dest
));
1119 if (!CopyFileW( file
->TargetPath
, dest
, TRUE
))
1121 WARN("Failed to copy file %s -> %s (%u)\n",
1122 debugstr_w(file
->TargetPath
), debugstr_w(dest
), GetLastError());
1125 FIXME("We should track these duplicate files as well\n");
1127 uirow
= MSI_CreateRecord( 9 );
1128 MSI_RecordSetStringW( uirow
, 1, MSI_RecordGetString( row
, 1 ) );
1129 MSI_RecordSetInteger( uirow
, 6, file
->FileSize
);
1130 MSI_RecordSetStringW( uirow
, 9, MSI_RecordGetString( row
, 5 ) );
1131 MSI_ProcessMessage(package
, INSTALLMESSAGE_ACTIONDATA
, uirow
);
1132 msiobj_release( &uirow
->hdr
);
1135 return ERROR_SUCCESS
;
1138 UINT
ACTION_DuplicateFiles(MSIPACKAGE
*package
)
1140 static const WCHAR query
[] = {
1141 'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
1142 '`','D','u','p','l','i','c','a','t','e','F','i','l','e','`',0};
1146 if (package
->script
== SCRIPT_NONE
)
1147 return msi_schedule_action(package
, SCRIPT_INSTALL
, szDuplicateFiles
);
1149 rc
= MSI_DatabaseOpenViewW(package
->db
, query
, &view
);
1150 if (rc
!= ERROR_SUCCESS
)
1151 return ERROR_SUCCESS
;
1153 rc
= MSI_IterateRecords(view
, NULL
, ITERATE_DuplicateFiles
, package
);
1154 msiobj_release(&view
->hdr
);
1158 static UINT
ITERATE_RemoveDuplicateFiles( MSIRECORD
*row
, LPVOID param
)
1160 MSIPACKAGE
*package
= param
;
1162 LPCWSTR file_key
, component
;
1167 component
= MSI_RecordGetString( row
, 2 );
1168 comp
= msi_get_loaded_component( package
, component
);
1170 return ERROR_SUCCESS
;
1172 comp
->Action
= msi_get_component_action( package
, comp
);
1173 if (comp
->Action
!= INSTALLSTATE_ABSENT
)
1175 TRACE("component not scheduled for removal %s\n", debugstr_w(component
));
1176 return ERROR_SUCCESS
;
1179 file_key
= MSI_RecordGetString( row
, 3 );
1182 ERR("Unable to get file key\n");
1183 return ERROR_FUNCTION_FAILED
;
1186 file
= msi_get_loaded_file( package
, file_key
);
1189 ERR("Original file unknown %s\n", debugstr_w(file_key
));
1190 return ERROR_SUCCESS
;
1193 dest
= get_duplicate_filename( package
, row
, file_key
, file
->TargetPath
);
1196 WARN("Unable to get duplicate filename\n");
1197 return ERROR_SUCCESS
;
1200 TRACE("Removing duplicate %s of %s\n", debugstr_w(dest
), debugstr_w(file
->TargetPath
));
1202 if (!DeleteFileW( dest
))
1204 WARN("Failed to delete duplicate file %s (%u)\n", debugstr_w(dest
), GetLastError());
1207 uirow
= MSI_CreateRecord( 9 );
1208 MSI_RecordSetStringW( uirow
, 1, MSI_RecordGetString( row
, 1 ) );
1209 MSI_RecordSetStringW( uirow
, 9, MSI_RecordGetString( row
, 5 ) );
1210 MSI_ProcessMessage(package
, INSTALLMESSAGE_ACTIONDATA
, uirow
);
1211 msiobj_release( &uirow
->hdr
);
1214 return ERROR_SUCCESS
;
1217 UINT
ACTION_RemoveDuplicateFiles( MSIPACKAGE
*package
)
1219 static const WCHAR query
[] = {
1220 'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
1221 '`','D','u','p','l','i','c','a','t','e','F','i','l','e','`',0};
1225 if (package
->script
== SCRIPT_NONE
)
1226 return msi_schedule_action(package
, SCRIPT_INSTALL
, szRemoveDuplicateFiles
);
1228 rc
= MSI_DatabaseOpenViewW( package
->db
, query
, &view
);
1229 if (rc
!= ERROR_SUCCESS
)
1230 return ERROR_SUCCESS
;
1232 rc
= MSI_IterateRecords( view
, NULL
, ITERATE_RemoveDuplicateFiles
, package
);
1233 msiobj_release( &view
->hdr
);
1237 static BOOL
verify_comp_for_removal(MSICOMPONENT
*comp
, UINT install_mode
)
1240 if (comp
->Action
!= INSTALLSTATE_SOURCE
&&
1241 comp
->Attributes
& msidbComponentAttributesSourceOnly
&&
1242 (install_mode
== msidbRemoveFileInstallModeOnRemove
||
1243 install_mode
== msidbRemoveFileInstallModeOnBoth
)) return TRUE
;
1245 switch (comp
->Action
)
1247 case INSTALLSTATE_LOCAL
:
1248 case INSTALLSTATE_SOURCE
:
1249 if (install_mode
== msidbRemoveFileInstallModeOnInstall
||
1250 install_mode
== msidbRemoveFileInstallModeOnBoth
) return TRUE
;
1252 case INSTALLSTATE_ABSENT
:
1253 if (install_mode
== msidbRemoveFileInstallModeOnRemove
||
1254 install_mode
== msidbRemoveFileInstallModeOnBoth
) return TRUE
;
1261 static UINT
ITERATE_RemoveFiles(MSIRECORD
*row
, LPVOID param
)
1263 MSIPACKAGE
*package
= param
;
1266 LPCWSTR component
, dirprop
;
1268 LPWSTR dir
= NULL
, path
= NULL
, filename
= NULL
;
1270 UINT ret
= ERROR_SUCCESS
;
1272 component
= MSI_RecordGetString(row
, 2);
1273 dirprop
= MSI_RecordGetString(row
, 4);
1274 install_mode
= MSI_RecordGetInteger(row
, 5);
1276 comp
= msi_get_loaded_component(package
, component
);
1278 return ERROR_SUCCESS
;
1280 comp
->Action
= msi_get_component_action( package
, comp
);
1281 if (!verify_comp_for_removal(comp
, install_mode
))
1283 TRACE("Skipping removal due to install mode\n");
1284 return ERROR_SUCCESS
;
1286 if (comp
->assembly
&& !comp
->assembly
->application
)
1288 return ERROR_SUCCESS
;
1290 if (comp
->Attributes
& msidbComponentAttributesPermanent
)
1292 TRACE("permanent component, not removing file\n");
1293 return ERROR_SUCCESS
;
1296 dir
= msi_dup_property(package
->db
, dirprop
);
1299 WARN("directory property has no value\n");
1300 return ERROR_SUCCESS
;
1303 if ((filename
= strdupW( MSI_RecordGetString(row
, 3) )))
1305 msi_reduce_to_long_filename( filename
);
1306 size
= lstrlenW( filename
);
1308 size
+= lstrlenW(dir
) + 2;
1309 path
= msi_alloc(size
* sizeof(WCHAR
));
1312 ret
= ERROR_OUTOFMEMORY
;
1318 lstrcpyW(path
, dir
);
1319 PathAddBackslashW(path
);
1320 lstrcatW(path
, filename
);
1322 TRACE("Deleting misc file: %s\n", debugstr_w(path
));
1327 TRACE("Removing misc directory: %s\n", debugstr_w(dir
));
1328 RemoveDirectoryW(dir
);
1332 uirow
= MSI_CreateRecord( 9 );
1333 MSI_RecordSetStringW( uirow
, 1, MSI_RecordGetString(row
, 1) );
1334 MSI_RecordSetStringW( uirow
, 9, dir
);
1335 MSI_ProcessMessage(package
, INSTALLMESSAGE_ACTIONDATA
, uirow
);
1336 msiobj_release( &uirow
->hdr
);
1344 static void remove_folder( MSIFOLDER
*folder
)
1348 LIST_FOR_EACH_ENTRY( fl
, &folder
->children
, FolderList
, entry
)
1350 remove_folder( fl
->folder
);
1352 if (!folder
->persistent
&& folder
->State
!= FOLDER_STATE_REMOVED
)
1354 if (RemoveDirectoryW( folder
->ResolvedTarget
)) folder
->State
= FOLDER_STATE_REMOVED
;
1358 UINT
ACTION_RemoveFiles( MSIPACKAGE
*package
)
1360 static const WCHAR query
[] = {
1361 'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
1362 '`','R','e','m','o','v','e','F','i','l','e','`',0};
1368 if (package
->script
== SCRIPT_NONE
)
1369 return msi_schedule_action(package
, SCRIPT_INSTALL
, szRemoveFiles
);
1371 r
= MSI_DatabaseOpenViewW(package
->db
, query
, &view
);
1372 if (r
== ERROR_SUCCESS
)
1374 r
= MSI_IterateRecords(view
, NULL
, ITERATE_RemoveFiles
, package
);
1375 msiobj_release(&view
->hdr
);
1376 if (r
!= ERROR_SUCCESS
)
1380 LIST_FOR_EACH_ENTRY( file
, &package
->files
, MSIFILE
, entry
)
1383 VS_FIXEDFILEINFO
*ver
;
1385 comp
= file
->Component
;
1386 msi_file_update_ui( package
, file
, szRemoveFiles
);
1388 comp
->Action
= msi_get_component_action( package
, comp
);
1389 if (comp
->Action
!= INSTALLSTATE_ABSENT
|| comp
->Installed
== INSTALLSTATE_SOURCE
)
1392 if (comp
->assembly
&& !comp
->assembly
->application
)
1395 if (comp
->Attributes
& msidbComponentAttributesPermanent
)
1397 TRACE("permanent component, not removing file\n");
1403 ver
= msi_get_disk_file_version( file
->TargetPath
);
1404 if (ver
&& msi_compare_file_versions( ver
, file
->Version
) > 0)
1406 TRACE("newer version detected, not removing file\n");
1413 if (file
->state
== msifs_installed
)
1414 WARN("removing installed file %s\n", debugstr_w(file
->TargetPath
));
1416 TRACE("removing %s\n", debugstr_w(file
->File
) );
1418 SetFileAttributesW( file
->TargetPath
, FILE_ATTRIBUTE_NORMAL
);
1419 if (!DeleteFileW( file
->TargetPath
))
1421 WARN("failed to delete %s (%u)\n", debugstr_w(file
->TargetPath
), GetLastError());
1423 file
->state
= msifs_missing
;
1425 uirow
= MSI_CreateRecord( 9 );
1426 MSI_RecordSetStringW( uirow
, 1, file
->FileName
);
1427 MSI_RecordSetStringW( uirow
, 9, comp
->Directory
);
1428 MSI_ProcessMessage(package
, INSTALLMESSAGE_ACTIONDATA
, uirow
);
1429 msiobj_release( &uirow
->hdr
);
1432 LIST_FOR_EACH_ENTRY( comp
, &package
->components
, MSICOMPONENT
, entry
)
1434 comp
->Action
= msi_get_component_action( package
, comp
);
1435 if (comp
->Action
!= INSTALLSTATE_ABSENT
) continue;
1437 if (comp
->Attributes
& msidbComponentAttributesPermanent
)
1439 TRACE("permanent component, not removing directory\n");
1442 if (comp
->assembly
&& !comp
->assembly
->application
)
1443 msi_uninstall_assembly( package
, comp
);
1446 MSIFOLDER
*folder
= msi_get_loaded_folder( package
, comp
->Directory
);
1447 remove_folder( folder
);
1450 return ERROR_SUCCESS
;