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
38 #include "wine/debug.h"
47 #include "wine/unicode.h"
49 WINE_DEFAULT_DEBUG_CHANNEL(msi
);
51 static void msi_file_update_ui( MSIPACKAGE
*package
, MSIFILE
*f
, const WCHAR
*action
)
55 uirow
= MSI_CreateRecord( 9 );
56 MSI_RecordSetStringW( uirow
, 1, f
->FileName
);
57 MSI_RecordSetStringW( uirow
, 9, f
->Component
->Directory
);
58 MSI_RecordSetInteger( uirow
, 6, f
->FileSize
);
59 msi_ui_actiondata( package
, action
, uirow
);
60 msiobj_release( &uirow
->hdr
);
61 msi_ui_progress( package
, 2, f
->FileSize
, 0, 0 );
64 static msi_file_state
calculate_install_state( MSIPACKAGE
*package
, MSIFILE
*file
)
66 MSICOMPONENT
*comp
= file
->Component
;
67 VS_FIXEDFILEINFO
*file_version
;
72 comp
->Action
= msi_get_component_action( package
, comp
);
73 if (!comp
->Enabled
|| comp
->Action
!= INSTALLSTATE_LOCAL
|| (comp
->assembly
&& comp
->assembly
->installed
))
75 TRACE("skipping %s (not scheduled for install)\n", debugstr_w(file
->File
));
78 if (!list_empty( &package
->patches
) && file
->disk_id
< MSI_INITIAL_MEDIA_TRANSFORM_DISKID
)
80 TRACE("skipping %s (not part of patch)\n", debugstr_w(file
->File
));
83 if ((msi_is_global_assembly( comp
) && !comp
->assembly
->installed
) ||
84 GetFileAttributesW( file
->TargetPath
) == INVALID_FILE_ATTRIBUTES
)
86 TRACE("installing %s (missing)\n", debugstr_w(file
->File
));
91 if ((file_version
= msi_get_disk_file_version( file
->TargetPath
)))
93 if (msi_compare_file_versions( file_version
, file
->Version
) < 0)
95 TRACE("overwriting %s (new version %s old version %u.%u.%u.%u)\n",
96 debugstr_w(file
->File
), debugstr_w(file
->Version
),
97 HIWORD(file_version
->dwFileVersionMS
), LOWORD(file_version
->dwFileVersionMS
),
98 HIWORD(file_version
->dwFileVersionLS
), LOWORD(file_version
->dwFileVersionLS
));
99 state
= msifs_overwrite
;
103 TRACE("keeping %s (new version %s old version %u.%u.%u.%u)\n",
104 debugstr_w(file
->File
), debugstr_w(file
->Version
),
105 HIWORD(file_version
->dwFileVersionMS
), LOWORD(file_version
->dwFileVersionMS
),
106 HIWORD(file_version
->dwFileVersionLS
), LOWORD(file_version
->dwFileVersionLS
));
107 state
= msifs_present
;
109 msi_free( file_version
);
112 else if ((font_version
= msi_font_version_from_file( file
->TargetPath
)))
114 if (msi_compare_font_versions( font_version
, file
->Version
) < 0)
116 TRACE("overwriting %s (new version %s old version %u.%u.%u.%u)\n",
117 debugstr_w(file
->File
), debugstr_w(file
->Version
),
118 HIWORD(file_version
->dwFileVersionMS
), LOWORD(file_version
->dwFileVersionMS
),
119 HIWORD(file_version
->dwFileVersionLS
), LOWORD(file_version
->dwFileVersionLS
));
120 state
= msifs_overwrite
;
124 TRACE("keeping %s (new version %s old version %u.%u.%u.%u)\n",
125 debugstr_w(file
->File
), debugstr_w(file
->Version
),
126 HIWORD(file_version
->dwFileVersionMS
), LOWORD(file_version
->dwFileVersionMS
),
127 HIWORD(file_version
->dwFileVersionLS
), LOWORD(file_version
->dwFileVersionLS
));
128 state
= msifs_present
;
130 msi_free( font_version
);
134 if ((size
= msi_get_disk_file_size( file
->TargetPath
)) != file
->FileSize
)
136 TRACE("overwriting %s (old size %u new size %u)\n", debugstr_w(file
->File
), size
, file
->FileSize
);
137 return msifs_overwrite
;
139 if (file
->hash
.dwFileHashInfoSize
)
141 if (msi_file_hash_matches( file
))
143 TRACE("keeping %s (hash match)\n", debugstr_w(file
->File
));
144 return msifs_hashmatch
;
148 TRACE("overwriting %s (hash mismatch)\n", debugstr_w(file
->File
));
149 return msifs_overwrite
;
153 TRACE("keeping %s\n", debugstr_w(file
->File
));
154 return msifs_present
;
157 static void schedule_install_files(MSIPACKAGE
*package
)
161 LIST_FOR_EACH_ENTRY(file
, &package
->files
, MSIFILE
, entry
)
163 MSICOMPONENT
*comp
= file
->Component
;
165 file
->state
= calculate_install_state( package
, file
);
166 if (file
->state
== msifs_overwrite
&& (comp
->Attributes
& msidbComponentAttributesNeverOverwrite
))
168 TRACE("not overwriting %s\n", debugstr_w(file
->TargetPath
));
169 file
->state
= msifs_skipped
;
174 static UINT
copy_file(MSIFILE
*file
, LPWSTR source
)
178 ret
= CopyFileW(source
, file
->TargetPath
, FALSE
);
180 return GetLastError();
182 SetFileAttributesW(file
->TargetPath
, FILE_ATTRIBUTE_NORMAL
);
184 file
->state
= msifs_installed
;
185 return ERROR_SUCCESS
;
188 static UINT
copy_install_file(MSIPACKAGE
*package
, MSIFILE
*file
, LPWSTR source
)
192 TRACE("Copying %s to %s\n", debugstr_w(source
), debugstr_w(file
->TargetPath
));
194 gle
= copy_file(file
, source
);
195 if (gle
== ERROR_SUCCESS
)
198 if (gle
== ERROR_ALREADY_EXISTS
&& file
->state
== msifs_overwrite
)
200 TRACE("overwriting existing file\n");
201 return ERROR_SUCCESS
;
203 else if (gle
== ERROR_ACCESS_DENIED
)
205 SetFileAttributesW(file
->TargetPath
, FILE_ATTRIBUTE_NORMAL
);
207 gle
= copy_file(file
, source
);
208 TRACE("Overwriting existing file: %d\n", gle
);
210 if (gle
== ERROR_SHARING_VIOLATION
|| gle
== ERROR_USER_MAPPED_FILE
)
212 WCHAR
*tmpfileW
, *pathW
, *p
;
215 TRACE("file in use, scheduling rename operation\n");
217 if (!(pathW
= strdupW( file
->TargetPath
))) return ERROR_OUTOFMEMORY
;
218 if ((p
= strrchrW(pathW
, '\\'))) *p
= 0;
219 len
= strlenW( pathW
) + 16;
220 if (!(tmpfileW
= msi_alloc(len
* sizeof(WCHAR
))))
223 return ERROR_OUTOFMEMORY
;
225 if (!GetTempFileNameW( pathW
, szMsi
, 0, tmpfileW
)) tmpfileW
[0] = 0;
228 if (CopyFileW(source
, tmpfileW
, FALSE
) &&
229 MoveFileExW(file
->TargetPath
, NULL
, MOVEFILE_DELAY_UNTIL_REBOOT
) &&
230 MoveFileExW(tmpfileW
, file
->TargetPath
, MOVEFILE_DELAY_UNTIL_REBOOT
))
232 file
->state
= msifs_installed
;
233 package
->need_reboot_at_end
= 1;
238 gle
= GetLastError();
239 WARN("failed to schedule rename operation: %d)\n", gle
);
240 DeleteFileW( tmpfileW
);
248 static UINT
msi_create_directory( MSIPACKAGE
*package
, const WCHAR
*dir
)
251 const WCHAR
*install_path
;
253 install_path
= msi_get_target_folder( package
, dir
);
254 if (!install_path
) return ERROR_FUNCTION_FAILED
;
256 folder
= msi_get_loaded_folder( package
, dir
);
257 if (folder
->State
== FOLDER_STATE_UNINITIALIZED
)
259 msi_create_full_path( install_path
);
260 folder
->State
= FOLDER_STATE_CREATED
;
262 return ERROR_SUCCESS
;
265 static MSIFILE
*find_file( MSIPACKAGE
*package
, UINT disk_id
, const WCHAR
*filename
)
269 LIST_FOR_EACH_ENTRY( file
, &package
->files
, MSIFILE
, entry
)
271 if (file
->disk_id
== disk_id
&&
272 file
->state
!= msifs_installed
&&
273 !strcmpiW( filename
, file
->File
)) return file
;
278 static BOOL
installfiles_cb(MSIPACKAGE
*package
, LPCWSTR filename
, DWORD action
,
279 LPWSTR
*path
, DWORD
*attrs
, PVOID user
)
281 MSIFILE
*file
= *(MSIFILE
**)user
;
283 if (action
== MSICABEXTRACT_BEGINEXTRACT
)
285 if (!(file
= find_file( package
, file
->disk_id
, filename
)))
287 TRACE("unknown file in cabinet (%s)\n", debugstr_w(filename
));
290 if (file
->state
!= msifs_missing
&& file
->state
!= msifs_overwrite
)
293 if (!msi_is_global_assembly( file
->Component
))
295 msi_create_directory( package
, file
->Component
->Directory
);
297 *path
= strdupW( file
->TargetPath
);
298 *attrs
= file
->Attributes
;
299 *(MSIFILE
**)user
= file
;
301 else if (action
== MSICABEXTRACT_FILEEXTRACTED
)
303 if (!msi_is_global_assembly( file
->Component
)) file
->state
= msifs_installed
;
309 WCHAR
*msi_resolve_file_source( MSIPACKAGE
*package
, MSIFILE
*file
)
313 TRACE("Working to resolve source of file %s\n", debugstr_w(file
->File
));
315 if (file
->IsCompressed
) return NULL
;
317 p
= msi_resolve_source_folder( package
, file
->Component
->Directory
, NULL
);
318 path
= msi_build_directory_name( 2, p
, file
->ShortName
);
320 if (file
->LongName
&& GetFileAttributesW( path
) == INVALID_FILE_ATTRIBUTES
)
323 path
= msi_build_directory_name( 2, p
, file
->LongName
);
326 TRACE("file %s source resolves to %s\n", debugstr_w(file
->File
), debugstr_w(path
));
331 * ACTION_InstallFiles()
333 * For efficiency, this is done in two passes:
334 * 1) Correct all the TargetPaths and determine what files are to be installed.
335 * 2) Extract Cabinets and copy files.
337 UINT
ACTION_InstallFiles(MSIPACKAGE
*package
)
340 UINT rc
= ERROR_SUCCESS
;
343 schedule_install_files(package
);
344 mi
= msi_alloc_zero( sizeof(MSIMEDIAINFO
) );
346 LIST_FOR_EACH_ENTRY( file
, &package
->files
, MSIFILE
, entry
)
348 msi_file_update_ui( package
, file
, szInstallFiles
);
350 rc
= msi_load_media_info( package
, file
->Sequence
, mi
);
351 if (rc
!= ERROR_SUCCESS
)
353 ERR("Unable to load media info for %s (%u)\n", debugstr_w(file
->File
), rc
);
354 rc
= ERROR_FUNCTION_FAILED
;
358 if (file
->state
!= msifs_hashmatch
&&
359 file
->state
!= msifs_skipped
&&
360 (file
->state
!= msifs_present
|| !msi_get_property_int( package
->db
, szInstalled
, 0 )) &&
361 (rc
= ready_media( package
, file
->IsCompressed
, mi
)))
363 ERR("Failed to ready media for %s\n", debugstr_w(file
->File
));
367 if (file
->state
!= msifs_missing
&& !mi
->is_continuous
&& file
->state
!= msifs_overwrite
)
370 if (file
->Sequence
> mi
->last_sequence
|| mi
->is_continuous
||
371 (file
->IsCompressed
&& !mi
->is_extracted
))
374 MSIFILE
*cursor
= file
;
377 data
.package
= package
;
378 data
.cb
= installfiles_cb
;
381 if (file
->IsCompressed
&& !msi_cabextract(package
, mi
, &data
))
383 ERR("Failed to extract cabinet: %s\n", debugstr_w(mi
->cabinet
));
384 rc
= ERROR_INSTALL_FAILURE
;
389 if (!file
->IsCompressed
)
391 WCHAR
*source
= msi_resolve_file_source(package
, file
);
393 TRACE("copying %s to %s\n", debugstr_w(source
), debugstr_w(file
->TargetPath
));
395 if (!msi_is_global_assembly( file
->Component
))
397 msi_create_directory(package
, file
->Component
->Directory
);
399 rc
= copy_install_file(package
, file
, source
);
400 if (rc
!= ERROR_SUCCESS
)
402 ERR("Failed to copy %s to %s (%u)\n", debugstr_w(source
), debugstr_w(file
->TargetPath
), rc
);
403 rc
= ERROR_INSTALL_FAILURE
;
409 else if (!msi_is_global_assembly( file
->Component
) &&
410 file
->state
!= msifs_installed
&& !(file
->Attributes
& msidbFileAttributesPatchAdded
))
412 ERR("compressed file wasn't installed (%s)\n", debugstr_w(file
->File
));
413 rc
= ERROR_INSTALL_FAILURE
;
417 LIST_FOR_EACH_ENTRY( file
, &package
->files
, MSIFILE
, entry
)
419 MSICOMPONENT
*comp
= file
->Component
;
421 if (!msi_is_global_assembly( comp
) || comp
->assembly
->installed
||
422 (file
->state
!= msifs_missing
&& file
->state
!= msifs_overwrite
)) continue;
424 rc
= msi_install_assembly( package
, comp
);
425 if (rc
!= ERROR_SUCCESS
)
427 ERR("Failed to install assembly\n");
428 rc
= ERROR_INSTALL_FAILURE
;
431 file
->state
= msifs_installed
;
435 msi_free_media_info(mi
);
439 static MSIFILEPATCH
*find_filepatch( MSIPACKAGE
*package
, UINT disk_id
, const WCHAR
*key
)
443 LIST_FOR_EACH_ENTRY( patch
, &package
->filepatches
, MSIFILEPATCH
, entry
)
445 if (!patch
->extracted
&& patch
->disk_id
== disk_id
&& !strcmpW( key
, patch
->File
->File
))
451 static BOOL
patchfiles_cb(MSIPACKAGE
*package
, LPCWSTR file
, DWORD action
,
452 LPWSTR
*path
, DWORD
*attrs
, PVOID user
)
454 MSIFILEPATCH
*patch
= *(MSIFILEPATCH
**)user
;
456 if (action
== MSICABEXTRACT_BEGINEXTRACT
)
458 if (!(patch
= find_filepatch( package
, patch
->disk_id
, file
))) return FALSE
;
460 patch
->path
= msi_create_temp_file( package
->db
);
461 *path
= strdupW( patch
->path
);
462 *attrs
= patch
->File
->Attributes
;
463 *(MSIFILEPATCH
**)user
= patch
;
465 else if (action
== MSICABEXTRACT_FILEEXTRACTED
)
467 patch
->extracted
= TRUE
;
473 UINT
ACTION_PatchFiles( MSIPACKAGE
*package
)
477 UINT rc
= ERROR_SUCCESS
;
479 TRACE("%p\n", package
);
481 mi
= msi_alloc_zero( sizeof(MSIMEDIAINFO
) );
483 TRACE("extracting files\n");
485 LIST_FOR_EACH_ENTRY( patch
, &package
->filepatches
, MSIFILEPATCH
, entry
)
487 MSIFILE
*file
= patch
->File
;
488 MSICOMPONENT
*comp
= file
->Component
;
490 rc
= msi_load_media_info( package
, patch
->Sequence
, mi
);
491 if (rc
!= ERROR_SUCCESS
)
493 ERR("Unable to load media info for %s (%u)\n", debugstr_w(file
->File
), rc
);
494 rc
= ERROR_FUNCTION_FAILED
;
497 comp
->Action
= msi_get_component_action( package
, comp
);
498 if (!comp
->Enabled
|| comp
->Action
!= INSTALLSTATE_LOCAL
) continue;
500 if (!patch
->extracted
)
503 MSIFILEPATCH
*cursor
= patch
;
505 rc
= ready_media( package
, TRUE
, mi
);
506 if (rc
!= ERROR_SUCCESS
)
508 ERR("Failed to ready media for %s\n", debugstr_w(file
->File
));
512 data
.package
= package
;
513 data
.cb
= patchfiles_cb
;
516 if (!msi_cabextract( package
, mi
, &data
))
518 ERR("Failed to extract cabinet: %s\n", debugstr_w(mi
->cabinet
));
519 rc
= ERROR_INSTALL_FAILURE
;
525 TRACE("applying patches\n");
527 LIST_FOR_EACH_ENTRY( patch
, &package
->filepatches
, MSIFILEPATCH
, entry
)
532 if (!patch
->path
) continue;
534 if (!(tmpfile
= msi_create_temp_file( package
->db
)))
536 rc
= ERROR_INSTALL_FAILURE
;
539 ret
= ApplyPatchToFileW( patch
->path
, patch
->File
->TargetPath
, tmpfile
, 0 );
542 DeleteFileW( patch
->File
->TargetPath
);
543 MoveFileW( tmpfile
, patch
->File
->TargetPath
);
546 WARN("failed to patch %s: %08x\n", debugstr_w(patch
->File
->TargetPath
), GetLastError());
548 DeleteFileW( patch
->path
);
549 DeleteFileW( tmpfile
);
552 if (!ret
&& !(patch
->Attributes
& msidbPatchAttributesNonVital
))
554 ERR("Failed to apply patch to file: %s\n", debugstr_w(patch
->File
->File
));
555 rc
= ERROR_INSTALL_FAILURE
;
561 msi_free_media_info(mi
);
565 #define is_dot_dir(x) ((x[0] == '.') && ((x[1] == 0) || ((x[1] == '.') && (x[2] == 0))))
576 static BOOL
msi_move_file(LPCWSTR source
, LPCWSTR dest
, int options
)
580 if (GetFileAttributesW(source
) == FILE_ATTRIBUTE_DIRECTORY
||
581 GetFileAttributesW(dest
) == FILE_ATTRIBUTE_DIRECTORY
)
583 WARN("Source or dest is directory, not moving\n");
587 if (options
== msidbMoveFileOptionsMove
)
589 TRACE("moving %s -> %s\n", debugstr_w(source
), debugstr_w(dest
));
590 ret
= MoveFileExW(source
, dest
, MOVEFILE_REPLACE_EXISTING
);
593 WARN("MoveFile failed: %d\n", GetLastError());
599 TRACE("copying %s -> %s\n", debugstr_w(source
), debugstr_w(dest
));
600 ret
= CopyFileW(source
, dest
, FALSE
);
603 WARN("CopyFile failed: %d\n", GetLastError());
611 static LPWSTR
wildcard_to_file(LPWSTR wildcard
, LPWSTR filename
)
614 DWORD dirlen
, pathlen
;
616 ptr
= strrchrW(wildcard
, '\\');
617 dirlen
= ptr
- wildcard
+ 1;
619 pathlen
= dirlen
+ lstrlenW(filename
) + 1;
620 path
= msi_alloc(pathlen
* sizeof(WCHAR
));
622 lstrcpynW(path
, wildcard
, dirlen
+ 1);
623 lstrcatW(path
, filename
);
628 static void free_file_entry(FILE_LIST
*file
)
630 msi_free(file
->source
);
631 msi_free(file
->dest
);
635 static void free_list(FILE_LIST
*list
)
637 while (!list_empty(&list
->entry
))
639 FILE_LIST
*file
= LIST_ENTRY(list_head(&list
->entry
), FILE_LIST
, entry
);
641 list_remove(&file
->entry
);
642 free_file_entry(file
);
646 static BOOL
add_wildcard(FILE_LIST
*files
, LPWSTR source
, LPWSTR dest
)
648 FILE_LIST
*new, *file
;
649 LPWSTR ptr
, filename
;
652 new = msi_alloc_zero(sizeof(FILE_LIST
));
656 new->source
= strdupW(source
);
657 ptr
= strrchrW(dest
, '\\') + 1;
658 filename
= strrchrW(new->source
, '\\') + 1;
660 new->sourcename
= filename
;
665 new->destname
= new->sourcename
;
667 size
= (ptr
- dest
) + lstrlenW(filename
) + 1;
668 new->dest
= msi_alloc(size
* sizeof(WCHAR
));
671 free_file_entry(new);
675 lstrcpynW(new->dest
, dest
, ptr
- dest
+ 1);
676 lstrcatW(new->dest
, filename
);
678 if (list_empty(&files
->entry
))
680 list_add_head(&files
->entry
, &new->entry
);
684 LIST_FOR_EACH_ENTRY(file
, &files
->entry
, FILE_LIST
, entry
)
686 if (strcmpW( source
, file
->source
) < 0)
688 list_add_before(&file
->entry
, &new->entry
);
693 list_add_after(&file
->entry
, &new->entry
);
697 static BOOL
move_files_wildcard(LPWSTR source
, LPWSTR dest
, int options
)
699 WIN32_FIND_DATAW wfd
;
703 FILE_LIST files
, *file
;
706 hfile
= FindFirstFileW(source
, &wfd
);
707 if (hfile
== INVALID_HANDLE_VALUE
) return FALSE
;
709 list_init(&files
.entry
);
711 for (res
= TRUE
; res
; res
= FindNextFileW(hfile
, &wfd
))
713 if (is_dot_dir(wfd
.cFileName
)) continue;
715 path
= wildcard_to_file(source
, wfd
.cFileName
);
722 add_wildcard(&files
, path
, dest
);
726 /* no files match the wildcard */
727 if (list_empty(&files
.entry
))
730 /* only the first wildcard match gets renamed to dest */
731 file
= LIST_ENTRY(list_head(&files
.entry
), FILE_LIST
, entry
);
732 size
= (strrchrW(file
->dest
, '\\') - file
->dest
) + lstrlenW(file
->destname
) + 2;
733 file
->dest
= msi_realloc(file
->dest
, size
* sizeof(WCHAR
));
740 /* file->dest may be shorter after the reallocation, so add a NULL
741 * terminator. This is needed for the call to strrchrW, as there will no
742 * longer be a NULL terminator within the bounds of the allocation in this case.
744 file
->dest
[size
- 1] = '\0';
745 lstrcpyW(strrchrW(file
->dest
, '\\') + 1, file
->destname
);
747 while (!list_empty(&files
.entry
))
749 file
= LIST_ENTRY(list_head(&files
.entry
), FILE_LIST
, entry
);
751 msi_move_file(file
->source
, file
->dest
, options
);
753 list_remove(&file
->entry
);
754 free_file_entry(file
);
765 void msi_reduce_to_long_filename( WCHAR
*filename
)
767 WCHAR
*p
= strchrW( filename
, '|' );
768 if (p
) memmove( filename
, p
+ 1, (strlenW( p
+ 1 ) + 1) * sizeof(WCHAR
) );
771 static UINT
ITERATE_MoveFiles( MSIRECORD
*rec
, LPVOID param
)
773 MSIPACKAGE
*package
= param
;
776 LPCWSTR sourcename
, component
;
777 LPWSTR sourcedir
, destname
= NULL
, destdir
= NULL
, source
= NULL
, dest
= NULL
;
782 component
= MSI_RecordGetString(rec
, 2);
783 comp
= msi_get_loaded_component(package
, component
);
785 return ERROR_SUCCESS
;
787 comp
->Action
= msi_get_component_action( package
, comp
);
788 if (comp
->Action
!= INSTALLSTATE_LOCAL
)
790 TRACE("component not scheduled for installation %s\n", debugstr_w(component
));
791 return ERROR_SUCCESS
;
794 sourcename
= MSI_RecordGetString(rec
, 3);
795 options
= MSI_RecordGetInteger(rec
, 7);
797 sourcedir
= msi_dup_property(package
->db
, MSI_RecordGetString(rec
, 5));
801 destdir
= msi_dup_property(package
->db
, MSI_RecordGetString(rec
, 6));
807 if (GetFileAttributesW(sourcedir
) == INVALID_FILE_ATTRIBUTES
)
810 source
= strdupW(sourcedir
);
816 size
= lstrlenW(sourcedir
) + lstrlenW(sourcename
) + 2;
817 source
= msi_alloc(size
* sizeof(WCHAR
));
821 lstrcpyW(source
, sourcedir
);
822 if (source
[lstrlenW(source
) - 1] != '\\')
823 lstrcatW(source
, szBackSlash
);
824 lstrcatW(source
, sourcename
);
827 wildcards
= strchrW(source
, '*') || strchrW(source
, '?');
829 if (MSI_RecordIsNull(rec
, 4))
835 destname
= strdupW(sourcename
);
836 else if ((p
= strrchrW(sourcedir
, '\\')))
837 destname
= strdupW(p
+ 1);
839 destname
= strdupW(sourcedir
);
846 destname
= strdupW(MSI_RecordGetString(rec
, 4));
847 if (destname
) msi_reduce_to_long_filename(destname
);
852 size
= lstrlenW(destname
);
854 size
+= lstrlenW(destdir
) + 2;
855 dest
= msi_alloc(size
* sizeof(WCHAR
));
859 lstrcpyW(dest
, destdir
);
860 if (dest
[lstrlenW(dest
) - 1] != '\\')
861 lstrcatW(dest
, szBackSlash
);
864 lstrcatW(dest
, destname
);
866 if (GetFileAttributesW(destdir
) == INVALID_FILE_ATTRIBUTES
)
868 if (!msi_create_full_path(destdir
))
870 WARN("failed to create directory %u\n", GetLastError());
876 msi_move_file(source
, dest
, options
);
878 move_files_wildcard(source
, dest
, options
);
881 uirow
= MSI_CreateRecord( 9 );
882 MSI_RecordSetStringW( uirow
, 1, MSI_RecordGetString(rec
, 1) );
883 MSI_RecordSetInteger( uirow
, 6, 1 ); /* FIXME */
884 MSI_RecordSetStringW( uirow
, 9, destdir
);
885 msi_ui_actiondata( package
, szMoveFiles
, uirow
);
886 msiobj_release( &uirow
->hdr
);
894 return ERROR_SUCCESS
;
897 UINT
ACTION_MoveFiles( MSIPACKAGE
*package
)
899 static const WCHAR query
[] = {
900 'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
901 '`','M','o','v','e','F','i','l','e','`',0};
905 rc
= MSI_DatabaseOpenViewW(package
->db
, query
, &view
);
906 if (rc
!= ERROR_SUCCESS
)
907 return ERROR_SUCCESS
;
909 rc
= MSI_IterateRecords(view
, NULL
, ITERATE_MoveFiles
, package
);
910 msiobj_release(&view
->hdr
);
914 static WCHAR
*get_duplicate_filename( MSIPACKAGE
*package
, MSIRECORD
*row
, const WCHAR
*file_key
, const WCHAR
*src
)
917 WCHAR
*dst_name
, *dst_path
, *dst
;
919 if (MSI_RecordIsNull( row
, 4 ))
921 len
= strlenW( src
) + 1;
922 if (!(dst_name
= msi_alloc( len
* sizeof(WCHAR
)))) return NULL
;
923 strcpyW( dst_name
, strrchrW( src
, '\\' ) + 1 );
927 MSI_RecordGetStringW( row
, 4, NULL
, &len
);
928 if (!(dst_name
= msi_alloc( ++len
* sizeof(WCHAR
) ))) return NULL
;
929 MSI_RecordGetStringW( row
, 4, dst_name
, &len
);
930 msi_reduce_to_long_filename( dst_name
);
933 if (MSI_RecordIsNull( row
, 5 ))
936 dst_path
= strdupW( src
);
937 p
= strrchrW( dst_path
, '\\' );
942 const WCHAR
*dst_key
= MSI_RecordGetString( row
, 5 );
944 dst_path
= strdupW( msi_get_target_folder( package
, dst_key
) );
948 dst_path
= msi_dup_property( package
->db
, dst_key
);
951 FIXME("Unable to get destination folder, try AppSearch properties\n");
952 msi_free( dst_name
);
958 dst
= msi_build_directory_name( 2, dst_path
, dst_name
);
959 msi_create_full_path( dst_path
);
961 msi_free( dst_name
);
962 msi_free( dst_path
);
966 static UINT
ITERATE_DuplicateFiles(MSIRECORD
*row
, LPVOID param
)
968 MSIPACKAGE
*package
= param
;
970 LPCWSTR file_key
, component
;
975 component
= MSI_RecordGetString(row
,2);
976 comp
= msi_get_loaded_component(package
, component
);
978 return ERROR_SUCCESS
;
980 comp
->Action
= msi_get_component_action( package
, comp
);
981 if (comp
->Action
!= INSTALLSTATE_LOCAL
)
983 TRACE("component not scheduled for installation %s\n", debugstr_w(component
));
984 return ERROR_SUCCESS
;
987 file_key
= MSI_RecordGetString(row
,3);
990 ERR("Unable to get file key\n");
991 return ERROR_FUNCTION_FAILED
;
994 file
= msi_get_loaded_file( package
, file_key
);
997 ERR("Original file unknown %s\n", debugstr_w(file_key
));
998 return ERROR_SUCCESS
;
1001 dest
= get_duplicate_filename( package
, row
, file_key
, file
->TargetPath
);
1004 WARN("Unable to get duplicate filename\n");
1005 return ERROR_SUCCESS
;
1008 TRACE("Duplicating file %s to %s\n", debugstr_w(file
->TargetPath
), debugstr_w(dest
));
1010 if (!CopyFileW( file
->TargetPath
, dest
, TRUE
))
1012 WARN("Failed to copy file %s -> %s (%u)\n",
1013 debugstr_w(file
->TargetPath
), debugstr_w(dest
), GetLastError());
1016 FIXME("We should track these duplicate files as well\n");
1018 uirow
= MSI_CreateRecord( 9 );
1019 MSI_RecordSetStringW( uirow
, 1, MSI_RecordGetString( row
, 1 ) );
1020 MSI_RecordSetInteger( uirow
, 6, file
->FileSize
);
1021 MSI_RecordSetStringW( uirow
, 9, MSI_RecordGetString( row
, 5 ) );
1022 msi_ui_actiondata( package
, szDuplicateFiles
, uirow
);
1023 msiobj_release( &uirow
->hdr
);
1026 return ERROR_SUCCESS
;
1029 UINT
ACTION_DuplicateFiles(MSIPACKAGE
*package
)
1031 static const WCHAR query
[] = {
1032 'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
1033 '`','D','u','p','l','i','c','a','t','e','F','i','l','e','`',0};
1037 rc
= MSI_DatabaseOpenViewW(package
->db
, query
, &view
);
1038 if (rc
!= ERROR_SUCCESS
)
1039 return ERROR_SUCCESS
;
1041 rc
= MSI_IterateRecords(view
, NULL
, ITERATE_DuplicateFiles
, package
);
1042 msiobj_release(&view
->hdr
);
1046 static UINT
ITERATE_RemoveDuplicateFiles( MSIRECORD
*row
, LPVOID param
)
1048 MSIPACKAGE
*package
= param
;
1050 LPCWSTR file_key
, component
;
1055 component
= MSI_RecordGetString( row
, 2 );
1056 comp
= msi_get_loaded_component( package
, component
);
1058 return ERROR_SUCCESS
;
1060 comp
->Action
= msi_get_component_action( package
, comp
);
1061 if (comp
->Action
!= INSTALLSTATE_ABSENT
)
1063 TRACE("component not scheduled for removal %s\n", debugstr_w(component
));
1064 return ERROR_SUCCESS
;
1067 file_key
= MSI_RecordGetString( row
, 3 );
1070 ERR("Unable to get file key\n");
1071 return ERROR_FUNCTION_FAILED
;
1074 file
= msi_get_loaded_file( package
, file_key
);
1077 ERR("Original file unknown %s\n", debugstr_w(file_key
));
1078 return ERROR_SUCCESS
;
1081 dest
= get_duplicate_filename( package
, row
, file_key
, file
->TargetPath
);
1084 WARN("Unable to get duplicate filename\n");
1085 return ERROR_SUCCESS
;
1088 TRACE("Removing duplicate %s of %s\n", debugstr_w(dest
), debugstr_w(file
->TargetPath
));
1090 if (!DeleteFileW( dest
))
1092 WARN("Failed to delete duplicate file %s (%u)\n", debugstr_w(dest
), GetLastError());
1095 uirow
= MSI_CreateRecord( 9 );
1096 MSI_RecordSetStringW( uirow
, 1, MSI_RecordGetString( row
, 1 ) );
1097 MSI_RecordSetStringW( uirow
, 9, MSI_RecordGetString( row
, 5 ) );
1098 msi_ui_actiondata( package
, szRemoveDuplicateFiles
, uirow
);
1099 msiobj_release( &uirow
->hdr
);
1102 return ERROR_SUCCESS
;
1105 UINT
ACTION_RemoveDuplicateFiles( MSIPACKAGE
*package
)
1107 static const WCHAR query
[] = {
1108 'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
1109 '`','D','u','p','l','i','c','a','t','e','F','i','l','e','`',0};
1113 rc
= MSI_DatabaseOpenViewW( package
->db
, query
, &view
);
1114 if (rc
!= ERROR_SUCCESS
)
1115 return ERROR_SUCCESS
;
1117 rc
= MSI_IterateRecords( view
, NULL
, ITERATE_RemoveDuplicateFiles
, package
);
1118 msiobj_release( &view
->hdr
);
1122 static BOOL
verify_comp_for_removal(MSICOMPONENT
*comp
, UINT install_mode
)
1125 if (comp
->Action
!= INSTALLSTATE_SOURCE
&&
1126 comp
->Attributes
& msidbComponentAttributesSourceOnly
&&
1127 (install_mode
== msidbRemoveFileInstallModeOnRemove
||
1128 install_mode
== msidbRemoveFileInstallModeOnBoth
)) return TRUE
;
1130 switch (comp
->Action
)
1132 case INSTALLSTATE_LOCAL
:
1133 case INSTALLSTATE_SOURCE
:
1134 if (install_mode
== msidbRemoveFileInstallModeOnInstall
||
1135 install_mode
== msidbRemoveFileInstallModeOnBoth
) return TRUE
;
1137 case INSTALLSTATE_ABSENT
:
1138 if (install_mode
== msidbRemoveFileInstallModeOnRemove
||
1139 install_mode
== msidbRemoveFileInstallModeOnBoth
) return TRUE
;
1146 static UINT
ITERATE_RemoveFiles(MSIRECORD
*row
, LPVOID param
)
1148 MSIPACKAGE
*package
= param
;
1151 LPCWSTR component
, dirprop
;
1153 LPWSTR dir
= NULL
, path
= NULL
, filename
= NULL
;
1155 UINT ret
= ERROR_SUCCESS
;
1157 component
= MSI_RecordGetString(row
, 2);
1158 dirprop
= MSI_RecordGetString(row
, 4);
1159 install_mode
= MSI_RecordGetInteger(row
, 5);
1161 comp
= msi_get_loaded_component(package
, component
);
1163 return ERROR_SUCCESS
;
1165 comp
->Action
= msi_get_component_action( package
, comp
);
1166 if (!verify_comp_for_removal(comp
, install_mode
))
1168 TRACE("Skipping removal due to install mode\n");
1169 return ERROR_SUCCESS
;
1171 if (comp
->assembly
&& !comp
->assembly
->application
)
1173 return ERROR_SUCCESS
;
1175 if (comp
->Attributes
& msidbComponentAttributesPermanent
)
1177 TRACE("permanent component, not removing file\n");
1178 return ERROR_SUCCESS
;
1181 dir
= msi_dup_property(package
->db
, dirprop
);
1184 WARN("directory property has no value\n");
1185 return ERROR_SUCCESS
;
1188 if ((filename
= strdupW( MSI_RecordGetString(row
, 3) )))
1190 msi_reduce_to_long_filename( filename
);
1191 size
= lstrlenW( filename
);
1193 size
+= lstrlenW(dir
) + 2;
1194 path
= msi_alloc(size
* sizeof(WCHAR
));
1197 ret
= ERROR_OUTOFMEMORY
;
1203 lstrcpyW(path
, dir
);
1204 PathAddBackslashW(path
);
1205 lstrcatW(path
, filename
);
1207 TRACE("Deleting misc file: %s\n", debugstr_w(path
));
1212 TRACE("Removing misc directory: %s\n", debugstr_w(dir
));
1213 RemoveDirectoryW(dir
);
1217 uirow
= MSI_CreateRecord( 9 );
1218 MSI_RecordSetStringW( uirow
, 1, MSI_RecordGetString(row
, 1) );
1219 MSI_RecordSetStringW( uirow
, 9, dir
);
1220 msi_ui_actiondata( package
, szRemoveFiles
, uirow
);
1221 msiobj_release( &uirow
->hdr
);
1229 static void remove_folder( MSIFOLDER
*folder
)
1233 LIST_FOR_EACH_ENTRY( fl
, &folder
->children
, FolderList
, entry
)
1235 remove_folder( fl
->folder
);
1237 if (!folder
->persistent
&& folder
->State
!= FOLDER_STATE_REMOVED
)
1239 if (RemoveDirectoryW( folder
->ResolvedTarget
)) folder
->State
= FOLDER_STATE_REMOVED
;
1243 UINT
ACTION_RemoveFiles( MSIPACKAGE
*package
)
1245 static const WCHAR query
[] = {
1246 'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
1247 '`','R','e','m','o','v','e','F','i','l','e','`',0};
1253 r
= MSI_DatabaseOpenViewW(package
->db
, query
, &view
);
1254 if (r
== ERROR_SUCCESS
)
1256 r
= MSI_IterateRecords(view
, NULL
, ITERATE_RemoveFiles
, package
);
1257 msiobj_release(&view
->hdr
);
1258 if (r
!= ERROR_SUCCESS
)
1262 LIST_FOR_EACH_ENTRY( file
, &package
->files
, MSIFILE
, entry
)
1265 VS_FIXEDFILEINFO
*ver
;
1267 comp
= file
->Component
;
1268 msi_file_update_ui( package
, file
, szRemoveFiles
);
1270 comp
->Action
= msi_get_component_action( package
, comp
);
1271 if (comp
->Action
!= INSTALLSTATE_ABSENT
|| comp
->Installed
== INSTALLSTATE_SOURCE
)
1274 if (comp
->assembly
&& !comp
->assembly
->application
)
1277 if (comp
->Attributes
& msidbComponentAttributesPermanent
)
1279 TRACE("permanent component, not removing file\n");
1285 ver
= msi_get_disk_file_version( file
->TargetPath
);
1286 if (ver
&& msi_compare_file_versions( ver
, file
->Version
) > 0)
1288 TRACE("newer version detected, not removing file\n");
1295 if (file
->state
== msifs_installed
)
1296 WARN("removing installed file %s\n", debugstr_w(file
->TargetPath
));
1298 TRACE("removing %s\n", debugstr_w(file
->File
) );
1300 SetFileAttributesW( file
->TargetPath
, FILE_ATTRIBUTE_NORMAL
);
1301 if (!DeleteFileW( file
->TargetPath
))
1303 WARN("failed to delete %s (%u)\n", debugstr_w(file
->TargetPath
), GetLastError());
1305 file
->state
= msifs_missing
;
1307 uirow
= MSI_CreateRecord( 9 );
1308 MSI_RecordSetStringW( uirow
, 1, file
->FileName
);
1309 MSI_RecordSetStringW( uirow
, 9, comp
->Directory
);
1310 msi_ui_actiondata( package
, szRemoveFiles
, uirow
);
1311 msiobj_release( &uirow
->hdr
);
1314 LIST_FOR_EACH_ENTRY( comp
, &package
->components
, MSICOMPONENT
, entry
)
1316 comp
->Action
= msi_get_component_action( package
, comp
);
1317 if (comp
->Action
!= INSTALLSTATE_ABSENT
) continue;
1319 if (comp
->Attributes
& msidbComponentAttributesPermanent
)
1321 TRACE("permanent component, not removing directory\n");
1324 if (comp
->assembly
&& !comp
->assembly
->application
)
1325 msi_uninstall_assembly( package
, comp
);
1328 MSIFOLDER
*folder
= msi_get_loaded_folder( package
, comp
->Directory
);
1329 remove_folder( folder
);
1332 return ERROR_SUCCESS
;