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"
46 #include "wine/unicode.h"
48 WINE_DEFAULT_DEBUG_CHANNEL(msi);
50 static HMODULE hmspatcha;
51 static BOOL (WINAPI *ApplyPatchToFileW)(LPCWSTR, LPCWSTR, LPCWSTR, ULONG);
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_ui_actiondata( package, action, uirow );
62 msiobj_release( &uirow->hdr );
63 msi_ui_progress( package, 2, f->FileSize, 0, 0 );
66 static msi_file_state calculate_install_state( MSIFILE *file )
68 MSICOMPONENT *comp = file->Component;
69 VS_FIXEDFILEINFO *file_version;
74 if (comp->ActionRequest != INSTALLSTATE_LOCAL || !comp->Enabled ||
75 (comp->assembly && comp->assembly->installed))
77 TRACE("file %s is not scheduled for install\n", debugstr_w(file->File));
80 if ((comp->assembly && !comp->assembly->application && !comp->assembly->installed) ||
81 GetFileAttributesW( file->TargetPath ) == INVALID_FILE_ATTRIBUTES)
83 TRACE("file %s is missing\n", debugstr_w(file->File));
88 if ((file_version = msi_get_disk_file_version( file->TargetPath )))
90 TRACE("new %s old %u.%u.%u.%u\n", debugstr_w(file->Version),
91 HIWORD(file_version->dwFileVersionMS),
92 LOWORD(file_version->dwFileVersionMS),
93 HIWORD(file_version->dwFileVersionLS),
94 LOWORD(file_version->dwFileVersionLS));
96 if (msi_compare_file_versions( file_version, file->Version ) < 0)
97 state = msifs_overwrite;
100 TRACE("destination file version equal or greater, not overwriting\n");
101 state = msifs_present;
103 msi_free( file_version );
106 else if ((font_version = font_version_from_file( file->TargetPath )))
108 TRACE("new %s old %s\n", debugstr_w(file->Version), debugstr_w(font_version));
110 if (msi_compare_font_versions( font_version, file->Version ) < 0)
111 state = msifs_overwrite;
114 TRACE("destination file version equal or greater, not overwriting\n");
115 state = msifs_present;
117 msi_free( font_version );
121 if ((file_size = msi_get_disk_file_size( file->TargetPath )) != file->FileSize)
123 return msifs_overwrite;
125 if (file->hash.dwFileHashInfoSize)
127 if (msi_file_hash_matches( file ))
129 TRACE("file hashes match, not overwriting\n");
130 return msifs_hashmatch;
134 TRACE("file hashes do not match, overwriting\n");
135 return msifs_overwrite;
139 return msifs_present;
142 static void schedule_install_files(MSIPACKAGE *package)
146 LIST_FOR_EACH_ENTRY(file, &package->files, MSIFILE, entry)
148 MSICOMPONENT *comp = file->Component;
150 file->state = calculate_install_state( file );
151 if (file->state == msifs_overwrite && (comp->Attributes & msidbComponentAttributesNeverOverwrite))
153 TRACE("not overwriting %s\n", debugstr_w(file->TargetPath));
154 file->state = msifs_skipped;
156 comp->Action = INSTALLSTATE_LOCAL;
157 msi_ui_progress( package, 2, file->FileSize, 0, 0 );
161 static UINT copy_file(MSIFILE *file, LPWSTR source)
165 ret = CopyFileW(source, file->TargetPath, FALSE);
167 return GetLastError();
169 SetFileAttributesW(file->TargetPath, FILE_ATTRIBUTE_NORMAL);
171 file->state = msifs_installed;
172 return ERROR_SUCCESS;
175 static UINT copy_install_file(MSIPACKAGE *package, MSIFILE *file, LPWSTR source)
179 TRACE("Copying %s to %s\n", debugstr_w(source), debugstr_w(file->TargetPath));
181 gle = copy_file(file, source);
182 if (gle == ERROR_SUCCESS)
185 if (gle == ERROR_ALREADY_EXISTS && file->state == msifs_overwrite)
187 TRACE("overwriting existing file\n");
188 return ERROR_SUCCESS;
190 else if (gle == ERROR_ACCESS_DENIED)
192 SetFileAttributesW(file->TargetPath, FILE_ATTRIBUTE_NORMAL);
194 gle = copy_file(file, source);
195 TRACE("Overwriting existing file: %d\n", gle);
197 if (gle == ERROR_SHARING_VIOLATION || gle == ERROR_USER_MAPPED_FILE)
199 WCHAR tmpfileW[MAX_PATH], *pathW, *p;
202 TRACE("file in use, scheduling rename operation\n");
204 GetTempFileNameW(szBackSlash, szMsi, 0, tmpfileW);
205 len = strlenW(file->TargetPath) + strlenW(tmpfileW) + 1;
206 if (!(pathW = msi_alloc(len * sizeof(WCHAR))))
207 return ERROR_OUTOFMEMORY;
209 strcpyW(pathW, file->TargetPath);
210 if ((p = strrchrW(pathW, '\\'))) *p = 0;
211 strcatW(pathW, tmpfileW);
213 if (CopyFileW(source, pathW, FALSE) &&
214 MoveFileExW(file->TargetPath, NULL, MOVEFILE_DELAY_UNTIL_REBOOT) &&
215 MoveFileExW(pathW, file->TargetPath, MOVEFILE_DELAY_UNTIL_REBOOT))
217 file->state = msifs_installed;
218 package->need_reboot = 1;
223 gle = GetLastError();
224 WARN("failed to schedule rename operation: %d)\n", gle);
232 static UINT msi_create_directory( MSIPACKAGE *package, const WCHAR *dir )
235 const WCHAR *install_path;
237 install_path = msi_get_target_folder( package, dir );
238 if (!install_path) return ERROR_FUNCTION_FAILED;
240 folder = msi_get_loaded_folder( package, dir );
241 if (folder->State == 0)
243 msi_create_full_path( install_path );
246 return ERROR_SUCCESS;
249 static BOOL installfiles_cb(MSIPACKAGE *package, LPCWSTR file, DWORD action,
250 LPWSTR *path, DWORD *attrs, PVOID user)
252 static MSIFILE *f = NULL;
253 UINT_PTR disk_id = (UINT_PTR)user;
255 if (action == MSICABEXTRACT_BEGINEXTRACT)
257 f = msi_get_loaded_file(package, file);
260 TRACE("unknown file in cabinet (%s)\n", debugstr_w(file));
264 if (f->disk_id != disk_id || (f->state != msifs_missing && f->state != msifs_overwrite))
267 msi_file_update_ui(package, f, szInstallFiles);
268 if (!f->Component->assembly || f->Component->assembly->application)
270 msi_create_directory(package, f->Component->Directory);
272 *path = strdupW(f->TargetPath);
273 *attrs = f->Attributes;
275 else if (action == MSICABEXTRACT_FILEEXTRACTED)
277 f->state = msifs_installed;
284 WCHAR *msi_resolve_file_source( MSIPACKAGE *package, MSIFILE *file )
288 TRACE("Working to resolve source of file %s\n", debugstr_w(file->File));
290 if (file->IsCompressed) return NULL;
292 p = msi_resolve_source_folder( package, file->Component->Directory, NULL );
293 path = msi_build_directory_name( 2, p, file->ShortName );
295 if (file->LongName && GetFileAttributesW( path ) == INVALID_FILE_ATTRIBUTES)
298 path = msi_build_directory_name( 2, p, file->LongName );
301 TRACE("file %s source resolves to %s\n", debugstr_w(file->File), debugstr_w(path));
306 * ACTION_InstallFiles()
308 * For efficiency, this is done in two passes:
309 * 1) Correct all the TargetPaths and determine what files are to be installed.
310 * 2) Extract Cabinets and copy files.
312 UINT ACTION_InstallFiles(MSIPACKAGE *package)
316 UINT rc = ERROR_SUCCESS;
319 /* increment progress bar each time action data is sent */
320 msi_ui_progress( package, 1, 1, 0, 0 );
322 schedule_install_files(package);
323 mi = msi_alloc_zero( sizeof(MSIMEDIAINFO) );
325 LIST_FOR_EACH_ENTRY( file, &package->files, MSIFILE, entry )
327 rc = msi_load_media_info( package, file->Sequence, mi );
328 if (rc != ERROR_SUCCESS)
330 ERR("Unable to load media info for %s (%u)\n", debugstr_w(file->File), rc);
331 return ERROR_FUNCTION_FAILED;
333 if (!file->Component->Enabled) continue;
335 if (file->state != msifs_hashmatch &&
336 (rc = ready_media( package, file->Sequence, file->IsCompressed, mi )))
338 ERR("Failed to ready media for %s\n", debugstr_w(file->File));
342 if (file->state != msifs_missing && !mi->is_continuous && file->state != msifs_overwrite)
345 if (file->Sequence > mi->last_sequence || mi->is_continuous ||
346 (file->IsCompressed && !mi->is_extracted))
351 data.package = package;
352 data.cb = installfiles_cb;
353 data.user = (PVOID)(UINT_PTR)mi->disk_id;
355 if (file->IsCompressed &&
356 !msi_cabextract(package, mi, &data))
358 ERR("Failed to extract cabinet: %s\n", debugstr_w(mi->cabinet));
359 rc = ERROR_INSTALL_FAILURE;
364 if (!file->IsCompressed)
366 WCHAR *source = msi_resolve_file_source(package, file);
368 TRACE("copying %s to %s\n", debugstr_w(source), debugstr_w(file->TargetPath));
370 msi_file_update_ui(package, file, szInstallFiles);
371 if (!file->Component->assembly || file->Component->assembly->application)
373 msi_create_directory(package, file->Component->Directory);
375 rc = copy_install_file(package, file, source);
376 if (rc != ERROR_SUCCESS)
378 ERR("Failed to copy %s to %s (%d)\n", debugstr_w(source),
379 debugstr_w(file->TargetPath), rc);
380 rc = ERROR_INSTALL_FAILURE;
386 else if (file->state != msifs_installed && !(file->Attributes & msidbFileAttributesPatchAdded))
388 ERR("compressed file wasn't installed (%s)\n", debugstr_w(file->TargetPath));
389 rc = ERROR_INSTALL_FAILURE;
393 msi_init_assembly_caches( package );
394 LIST_FOR_EACH_ENTRY( comp, &package->components, MSICOMPONENT, entry )
396 if (comp->ActionRequest == INSTALLSTATE_LOCAL && comp->Enabled &&
397 comp->assembly && !comp->assembly->installed)
399 rc = msi_install_assembly( package, comp );
400 if (rc != ERROR_SUCCESS)
402 ERR("Failed to install assembly\n");
403 rc = ERROR_INSTALL_FAILURE;
408 msi_destroy_assembly_caches( package );
411 msi_free_media_info(mi);
415 static BOOL load_mspatcha(void)
417 hmspatcha = LoadLibraryA("mspatcha.dll");
420 ERR("Failed to load mspatcha.dll: %d\n", GetLastError());
424 ApplyPatchToFileW = (void*)GetProcAddress(hmspatcha, "ApplyPatchToFileW");
425 if(!ApplyPatchToFileW)
427 ERR("GetProcAddress(ApplyPatchToFileW) failed: %d.\n", GetLastError());
434 static void unload_mspatch(void)
436 FreeLibrary(hmspatcha);
439 static BOOL patchfiles_cb(MSIPACKAGE *package, LPCWSTR file, DWORD action,
440 LPWSTR *path, DWORD *attrs, PVOID user)
442 static MSIFILEPATCH *p = NULL;
443 static WCHAR patch_path[MAX_PATH] = {0};
444 static WCHAR temp_folder[MAX_PATH] = {0};
446 if (action == MSICABEXTRACT_BEGINEXTRACT)
448 if (temp_folder[0] == '\0')
449 GetTempPathW(MAX_PATH, temp_folder);
451 p = msi_get_loaded_filepatch(package, file);
454 TRACE("unknown file in cabinet (%s)\n", debugstr_w(file));
458 msi_file_update_ui(package, p->File, szPatchFiles);
460 GetTempFileNameW(temp_folder, NULL, 0, patch_path);
462 *path = strdupW(patch_path);
463 *attrs = p->File->Attributes;
465 else if (action == MSICABEXTRACT_FILEEXTRACTED)
467 WCHAR patched_file[MAX_PATH];
470 GetTempFileNameW(temp_folder, NULL, 0, patched_file);
472 br = ApplyPatchToFileW(patch_path, p->File->TargetPath, patched_file, 0);
475 /* FIXME: baseline cache */
477 DeleteFileW( p->File->TargetPath );
478 MoveFileW( patched_file, p->File->TargetPath );
483 ERR("Failed patch %s: %d.\n", debugstr_w(p->File->TargetPath), GetLastError());
485 DeleteFileW(patch_path);
492 UINT ACTION_PatchFiles( MSIPACKAGE *package )
496 UINT rc = ERROR_SUCCESS;
497 BOOL mspatcha_loaded = FALSE;
499 TRACE("%p\n", package);
501 /* increment progress bar each time action data is sent */
502 msi_ui_progress( package, 1, 1, 0, 0 );
504 mi = msi_alloc_zero( sizeof(MSIMEDIAINFO) );
506 LIST_FOR_EACH_ENTRY( patch, &package->filepatches, MSIFILEPATCH, entry )
508 MSIFILE *file = patch->File;
510 rc = msi_load_media_info( package, patch->Sequence, mi );
511 if (rc != ERROR_SUCCESS)
513 ERR("Unable to load media info for %s (%u)\n", debugstr_w(file->File), rc);
514 return ERROR_FUNCTION_FAILED;
516 if (!file->Component->Enabled) continue;
518 if (!patch->IsApplied)
522 rc = ready_media( package, patch->Sequence, TRUE, mi );
523 if (rc != ERROR_SUCCESS)
525 ERR("Failed to ready media for %s\n", debugstr_w(file->File));
529 if (!mspatcha_loaded && !load_mspatcha())
531 rc = ERROR_FUNCTION_FAILED;
534 mspatcha_loaded = TRUE;
537 data.package = package;
538 data.cb = patchfiles_cb;
539 data.user = (PVOID)(UINT_PTR)mi->disk_id;
541 if (!msi_cabextract(package, mi, &data))
543 ERR("Failed to extract cabinet: %s\n", debugstr_w(mi->cabinet));
544 rc = ERROR_INSTALL_FAILURE;
549 if (!patch->IsApplied && !(patch->Attributes & msidbPatchAttributesNonVital))
551 ERR("Failed to apply patch to file: %s\n", debugstr_w(file->File));
552 rc = ERROR_INSTALL_FAILURE;
558 msi_free_media_info(mi);
564 #define is_dot_dir(x) ((x[0] == '.') && ((x[1] == 0) || ((x[1] == '.') && (x[2] == 0))))
575 static BOOL msi_move_file(LPCWSTR source, LPCWSTR dest, int options)
579 if (GetFileAttributesW(source) == FILE_ATTRIBUTE_DIRECTORY ||
580 GetFileAttributesW(dest) == FILE_ATTRIBUTE_DIRECTORY)
582 WARN("Source or dest is directory, not moving\n");
586 if (options == msidbMoveFileOptionsMove)
588 TRACE("moving %s -> %s\n", debugstr_w(source), debugstr_w(dest));
589 ret = MoveFileExW(source, dest, MOVEFILE_REPLACE_EXISTING);
592 WARN("MoveFile failed: %d\n", GetLastError());
598 TRACE("copying %s -> %s\n", debugstr_w(source), debugstr_w(dest));
599 ret = CopyFileW(source, dest, FALSE);
602 WARN("CopyFile failed: %d\n", GetLastError());
610 static LPWSTR wildcard_to_file(LPWSTR wildcard, LPWSTR filename)
613 DWORD dirlen, pathlen;
615 ptr = strrchrW(wildcard, '\\');
616 dirlen = ptr - wildcard + 1;
618 pathlen = dirlen + lstrlenW(filename) + 1;
619 path = msi_alloc(pathlen * sizeof(WCHAR));
621 lstrcpynW(path, wildcard, dirlen + 1);
622 lstrcatW(path, filename);
627 static void free_file_entry(FILE_LIST *file)
629 msi_free(file->source);
630 msi_free(file->dest);
634 static void free_list(FILE_LIST *list)
636 while (!list_empty(&list->entry))
638 FILE_LIST *file = LIST_ENTRY(list_head(&list->entry), FILE_LIST, entry);
640 list_remove(&file->entry);
641 free_file_entry(file);
645 static BOOL add_wildcard(FILE_LIST *files, LPWSTR source, LPWSTR dest)
647 FILE_LIST *new, *file;
648 LPWSTR ptr, filename;
651 new = msi_alloc_zero(sizeof(FILE_LIST));
655 new->source = strdupW(source);
656 ptr = strrchrW(dest, '\\') + 1;
657 filename = strrchrW(new->source, '\\') + 1;
659 new->sourcename = filename;
664 new->destname = new->sourcename;
666 size = (ptr - dest) + lstrlenW(filename) + 1;
667 new->dest = msi_alloc(size * sizeof(WCHAR));
670 free_file_entry(new);
674 lstrcpynW(new->dest, dest, ptr - dest + 1);
675 lstrcatW(new->dest, filename);
677 if (list_empty(&files->entry))
679 list_add_head(&files->entry, &new->entry);
683 LIST_FOR_EACH_ENTRY(file, &files->entry, FILE_LIST, entry)
685 if (strcmpW( source, file->source ) < 0)
687 list_add_before(&file->entry, &new->entry);
692 list_add_after(&file->entry, &new->entry);
696 static BOOL move_files_wildcard(LPWSTR source, LPWSTR dest, int options)
698 WIN32_FIND_DATAW wfd;
702 FILE_LIST files, *file;
705 hfile = FindFirstFileW(source, &wfd);
706 if (hfile == INVALID_HANDLE_VALUE) return FALSE;
708 list_init(&files.entry);
710 for (res = TRUE; res; res = FindNextFileW(hfile, &wfd))
712 if (is_dot_dir(wfd.cFileName)) continue;
714 path = wildcard_to_file(source, wfd.cFileName);
721 add_wildcard(&files, path, dest);
725 /* no files match the wildcard */
726 if (list_empty(&files.entry))
729 /* only the first wildcard match gets renamed to dest */
730 file = LIST_ENTRY(list_head(&files.entry), FILE_LIST, entry);
731 size = (strrchrW(file->dest, '\\') - file->dest) + lstrlenW(file->destname) + 2;
732 file->dest = msi_realloc(file->dest, size * sizeof(WCHAR));
739 /* file->dest may be shorter after the reallocation, so add a NULL
740 * terminator. This is needed for the call to strrchrW, as there will no
741 * longer be a NULL terminator within the bounds of the allocation in this case.
743 file->dest[size - 1] = '\0';
744 lstrcpyW(strrchrW(file->dest, '\\') + 1, file->destname);
746 while (!list_empty(&files.entry))
748 file = LIST_ENTRY(list_head(&files.entry), FILE_LIST, entry);
750 msi_move_file(file->source, file->dest, options);
752 list_remove(&file->entry);
753 free_file_entry(file);
764 void msi_reduce_to_long_filename( WCHAR *filename )
766 WCHAR *p = strchrW( filename, '|' );
767 if (p) memmove( filename, p + 1, (strlenW( p + 1 ) + 1) * sizeof(WCHAR) );
770 static UINT ITERATE_MoveFiles( MSIRECORD *rec, LPVOID param )
772 MSIPACKAGE *package = param;
775 LPCWSTR sourcename, component;
776 LPWSTR sourcedir, destname = NULL, destdir = NULL, source = NULL, dest = NULL;
781 component = MSI_RecordGetString(rec, 2);
782 comp = msi_get_loaded_component(package, component);
784 return ERROR_SUCCESS;
788 TRACE("component is disabled\n");
789 return ERROR_SUCCESS;
792 if (comp->ActionRequest != INSTALLSTATE_LOCAL && comp->ActionRequest != INSTALLSTATE_SOURCE)
794 TRACE("Component not scheduled for installation: %s\n", debugstr_w(component));
795 comp->Action = comp->Installed;
796 return ERROR_SUCCESS;
798 comp->Action = comp->ActionRequest;
800 sourcename = MSI_RecordGetString(rec, 3);
801 options = MSI_RecordGetInteger(rec, 7);
803 sourcedir = msi_dup_property(package->db, MSI_RecordGetString(rec, 5));
807 destdir = msi_dup_property(package->db, MSI_RecordGetString(rec, 6));
813 if (GetFileAttributesW(sourcedir) == INVALID_FILE_ATTRIBUTES)
816 source = strdupW(sourcedir);
822 size = lstrlenW(sourcedir) + lstrlenW(sourcename) + 2;
823 source = msi_alloc(size * sizeof(WCHAR));
827 lstrcpyW(source, sourcedir);
828 if (source[lstrlenW(source) - 1] != '\\')
829 lstrcatW(source, szBackSlash);
830 lstrcatW(source, sourcename);
833 wildcards = strchrW(source, '*') || strchrW(source, '?');
835 if (MSI_RecordIsNull(rec, 4))
839 destname = strdupW(sourcename);
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 ret = CreateDirectoryW(destdir, NULL);
871 WARN("CreateDirectory failed: %d\n", GetLastError());
877 msi_move_file(source, dest, options);
879 move_files_wildcard(source, dest, options);
882 uirow = MSI_CreateRecord( 9 );
883 MSI_RecordSetStringW( uirow, 1, MSI_RecordGetString(rec, 1) );
884 MSI_RecordSetInteger( uirow, 6, 1 ); /* FIXME */
885 MSI_RecordSetStringW( uirow, 9, destdir );
886 msi_ui_actiondata( package, szMoveFiles, uirow );
887 msiobj_release( &uirow->hdr );
895 return ERROR_SUCCESS;
898 UINT ACTION_MoveFiles( MSIPACKAGE *package )
903 static const WCHAR ExecSeqQuery[] =
904 {'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
905 '`','M','o','v','e','F','i','l','e','`',0};
907 rc = MSI_DatabaseOpenViewW(package->db, ExecSeqQuery, &view);
908 if (rc != ERROR_SUCCESS)
909 return ERROR_SUCCESS;
911 rc = MSI_IterateRecords(view, NULL, ITERATE_MoveFiles, package);
912 msiobj_release(&view->hdr);
917 static WCHAR *get_duplicate_filename( MSIPACKAGE *package, MSIRECORD *row, const WCHAR *file_key, const WCHAR *src )
920 WCHAR *dst_name, *dst_path, *dst;
922 if (MSI_RecordIsNull( row, 4 ))
924 len = strlenW( src ) + 1;
925 if (!(dst_name = msi_alloc( len * sizeof(WCHAR)))) return NULL;
926 strcpyW( dst_name, strrchrW( src, '\\' ) + 1 );
930 MSI_RecordGetStringW( row, 4, NULL, &len );
931 if (!(dst_name = msi_alloc( ++len * sizeof(WCHAR) ))) return NULL;
932 MSI_RecordGetStringW( row, 4, dst_name, &len );
933 msi_reduce_to_long_filename( dst_name );
936 if (MSI_RecordIsNull( row, 5 ))
939 dst_path = strdupW( src );
940 p = strrchrW( dst_path, '\\' );
945 const WCHAR *dst_key = MSI_RecordGetString( row, 5 );
947 dst_path = strdupW( msi_get_target_folder( package, dst_key ) );
951 dst_path = msi_dup_property( package->db, dst_key );
954 FIXME("Unable to get destination folder, try AppSearch properties\n");
955 msi_free( dst_name );
961 dst = msi_build_directory_name( 2, dst_path, dst_name );
962 msi_create_full_path( dst_path );
964 msi_free( dst_name );
965 msi_free( dst_path );
969 static UINT ITERATE_DuplicateFiles(MSIRECORD *row, LPVOID param)
971 MSIPACKAGE *package = param;
973 LPCWSTR file_key, component;
978 component = MSI_RecordGetString(row,2);
979 comp = msi_get_loaded_component(package, component);
981 return ERROR_SUCCESS;
985 TRACE("component is disabled\n");
986 return ERROR_SUCCESS;
989 if (comp->ActionRequest != INSTALLSTATE_LOCAL)
991 TRACE("Component not scheduled for installation %s\n", debugstr_w(component));
992 comp->Action = comp->Installed;
993 return ERROR_SUCCESS;
995 comp->Action = INSTALLSTATE_LOCAL;
997 file_key = MSI_RecordGetString(row,3);
1000 ERR("Unable to get file key\n");
1001 return ERROR_FUNCTION_FAILED;
1004 file = msi_get_loaded_file( package, file_key );
1007 ERR("Original file unknown %s\n", debugstr_w(file_key));
1008 return ERROR_SUCCESS;
1011 dest = get_duplicate_filename( package, row, file_key, file->TargetPath );
1014 WARN("Unable to get duplicate filename\n");
1015 return ERROR_SUCCESS;
1018 TRACE("Duplicating file %s to %s\n", debugstr_w(file->TargetPath), debugstr_w(dest));
1020 if (!CopyFileW( file->TargetPath, dest, TRUE ))
1022 WARN("Failed to copy file %s -> %s (%u)\n",
1023 debugstr_w(file->TargetPath), debugstr_w(dest), GetLastError());
1026 FIXME("We should track these duplicate files as well\n");
1028 uirow = MSI_CreateRecord( 9 );
1029 MSI_RecordSetStringW( uirow, 1, MSI_RecordGetString( row, 1 ) );
1030 MSI_RecordSetInteger( uirow, 6, file->FileSize );
1031 MSI_RecordSetStringW( uirow, 9, MSI_RecordGetString( row, 5 ) );
1032 msi_ui_actiondata( package, szDuplicateFiles, uirow );
1033 msiobj_release( &uirow->hdr );
1036 return ERROR_SUCCESS;
1039 UINT ACTION_DuplicateFiles(MSIPACKAGE *package)
1043 static const WCHAR ExecSeqQuery[] =
1044 {'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
1045 '`','D','u','p','l','i','c','a','t','e','F','i','l','e','`',0};
1047 rc = MSI_DatabaseOpenViewW(package->db, ExecSeqQuery, &view);
1048 if (rc != ERROR_SUCCESS)
1049 return ERROR_SUCCESS;
1051 rc = MSI_IterateRecords(view, NULL, ITERATE_DuplicateFiles, package);
1052 msiobj_release(&view->hdr);
1057 static UINT ITERATE_RemoveDuplicateFiles( MSIRECORD *row, LPVOID param )
1059 MSIPACKAGE *package = param;
1061 LPCWSTR file_key, component;
1066 component = MSI_RecordGetString( row, 2 );
1067 comp = msi_get_loaded_component( package, component );
1069 return ERROR_SUCCESS;
1073 TRACE("component is disabled\n");
1074 return ERROR_SUCCESS;
1077 if (comp->ActionRequest != INSTALLSTATE_ABSENT)
1079 TRACE("Component not scheduled for removal %s\n", debugstr_w(component));
1080 comp->Action = comp->Installed;
1081 return ERROR_SUCCESS;
1083 comp->Action = INSTALLSTATE_ABSENT;
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("Removing duplicate %s of %s\n", debugstr_w(dest), debugstr_w(file->TargetPath));
1108 if (!DeleteFileW( dest ))
1110 WARN("Failed to delete duplicate file %s (%u)\n", debugstr_w(dest), GetLastError());
1113 uirow = MSI_CreateRecord( 9 );
1114 MSI_RecordSetStringW( uirow, 1, MSI_RecordGetString( row, 1 ) );
1115 MSI_RecordSetStringW( uirow, 9, MSI_RecordGetString( row, 5 ) );
1116 msi_ui_actiondata( package, szRemoveDuplicateFiles, uirow );
1117 msiobj_release( &uirow->hdr );
1120 return ERROR_SUCCESS;
1123 UINT ACTION_RemoveDuplicateFiles( MSIPACKAGE *package )
1127 static const WCHAR query[] =
1128 {'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
1129 '`','D','u','p','l','i','c','a','t','e','F','i','l','e','`',0};
1131 rc = MSI_DatabaseOpenViewW( package->db, query, &view );
1132 if (rc != ERROR_SUCCESS)
1133 return ERROR_SUCCESS;
1135 rc = MSI_IterateRecords( view, NULL, ITERATE_RemoveDuplicateFiles, package );
1136 msiobj_release( &view->hdr );
1141 static BOOL verify_comp_for_removal(MSICOMPONENT *comp, UINT install_mode)
1143 INSTALLSTATE request = comp->ActionRequest;
1146 if (request != INSTALLSTATE_SOURCE &&
1147 comp->Attributes & msidbComponentAttributesSourceOnly &&
1148 (install_mode == msidbRemoveFileInstallModeOnRemove ||
1149 install_mode == msidbRemoveFileInstallModeOnBoth)) return TRUE;
1153 case INSTALLSTATE_LOCAL:
1154 case INSTALLSTATE_SOURCE:
1155 if (install_mode == msidbRemoveFileInstallModeOnInstall ||
1156 install_mode == msidbRemoveFileInstallModeOnBoth) return TRUE;
1158 case INSTALLSTATE_ABSENT:
1159 if (install_mode == msidbRemoveFileInstallModeOnRemove ||
1160 install_mode == msidbRemoveFileInstallModeOnBoth) return TRUE;
1167 static UINT ITERATE_RemoveFiles(MSIRECORD *row, LPVOID param)
1169 MSIPACKAGE *package = param;
1172 LPCWSTR component, dirprop;
1174 LPWSTR dir = NULL, path = NULL, filename = NULL;
1176 UINT ret = ERROR_SUCCESS;
1178 component = MSI_RecordGetString(row, 2);
1179 dirprop = MSI_RecordGetString(row, 4);
1180 install_mode = MSI_RecordGetInteger(row, 5);
1182 comp = msi_get_loaded_component(package, component);
1185 TRACE("component is disabled\n");
1186 return ERROR_SUCCESS;
1189 if (!verify_comp_for_removal(comp, install_mode))
1191 TRACE("Skipping removal due to install mode\n");
1192 comp->Action = comp->Installed;
1193 return ERROR_SUCCESS;
1196 if (comp->Attributes & msidbComponentAttributesPermanent)
1198 TRACE("permanent component, not removing file\n");
1199 return ERROR_SUCCESS;
1202 dir = msi_dup_property(package->db, dirprop);
1204 return ERROR_OUTOFMEMORY;
1207 if ((filename = strdupW( MSI_RecordGetString(row, 3) )))
1209 msi_reduce_to_long_filename( filename );
1210 size = lstrlenW( filename );
1212 size += lstrlenW(dir) + 2;
1213 path = msi_alloc(size * sizeof(WCHAR));
1216 ret = ERROR_OUTOFMEMORY;
1222 lstrcpyW(path, dir);
1223 PathAddBackslashW(path);
1224 lstrcatW(path, filename);
1226 TRACE("Deleting misc file: %s\n", debugstr_w(path));
1231 TRACE("Removing misc directory: %s\n", debugstr_w(dir));
1232 RemoveDirectoryW(dir);
1236 uirow = MSI_CreateRecord( 9 );
1237 MSI_RecordSetStringW( uirow, 1, MSI_RecordGetString(row, 1) );
1238 MSI_RecordSetStringW( uirow, 9, dir );
1239 msi_ui_actiondata( package, szRemoveFiles, uirow );
1240 msiobj_release( &uirow->hdr );
1248 static BOOL has_persistent_dir( MSIPACKAGE *package, MSICOMPONENT *comp )
1251 UINT r = ERROR_FUNCTION_FAILED;
1253 static const WCHAR query[] = {
1254 'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
1255 '`','C','r','e','a','t','e','F','o','l','d','e','r','`',' ','W','H','E','R','E',' ',
1256 '`','C','o','m','p','o','n','e','n','t','_','`',' ','=','\'','%','s','\'',' ','A','N','D',' ',
1257 '`','D','i','r','e','c','t','o','r','y','_','`',' ','=','\'','%','s','\'',0};
1259 if (!MSI_OpenQuery( package->db, &view, query, comp->Component, comp->Directory ))
1261 if (!MSI_ViewExecute( view, NULL ))
1264 if (!(r = MSI_ViewFetch( view, &rec )))
1266 TRACE("directory %s is persistent\n", debugstr_w(comp->Directory));
1267 msiobj_release( &rec->hdr );
1270 msiobj_release( &view->hdr );
1272 return (r == ERROR_SUCCESS);
1275 UINT ACTION_RemoveFiles( MSIPACKAGE *package )
1281 static const WCHAR query[] = {
1282 'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
1283 '`','R','e','m','o','v','e','F','i','l','e','`',0};
1285 r = MSI_DatabaseOpenViewW(package->db, query, &view);
1286 if (r == ERROR_SUCCESS)
1288 MSI_IterateRecords(view, NULL, ITERATE_RemoveFiles, package);
1289 msiobj_release(&view->hdr);
1292 LIST_FOR_EACH_ENTRY( file, &package->files, MSIFILE, entry )
1296 VS_FIXEDFILEINFO *ver;
1298 if ( file->state == msifs_installed )
1299 ERR("removing installed file %s\n", debugstr_w(file->TargetPath));
1301 if ( file->Component->ActionRequest != INSTALLSTATE_ABSENT ||
1302 file->Component->Installed == INSTALLSTATE_SOURCE )
1305 if (!file->Component->Enabled)
1307 TRACE("component is disabled\n");
1311 if (file->Component->Attributes & msidbComponentAttributesPermanent)
1313 TRACE("permanent component, not removing file\n");
1319 ver = msi_get_disk_file_version( file->TargetPath );
1320 if (ver && msi_compare_file_versions( ver, file->Version ) > 0)
1322 TRACE("newer version detected, not removing file\n");
1329 TRACE("removing %s\n", debugstr_w(file->File) );
1331 SetFileAttributesW( file->TargetPath, FILE_ATTRIBUTE_NORMAL );
1332 if (!DeleteFileW( file->TargetPath ))
1334 WARN("failed to delete %s (%u)\n", debugstr_w(file->TargetPath), GetLastError());
1336 else if (!has_persistent_dir( package, file->Component ))
1338 if ((dir = strdupW( file->TargetPath )))
1340 if ((p = strrchrW( dir, '\\' ))) *p = 0;
1341 RemoveDirectoryW( dir );
1345 file->state = msifs_missing;
1347 uirow = MSI_CreateRecord( 9 );
1348 MSI_RecordSetStringW( uirow, 1, file->FileName );
1349 MSI_RecordSetStringW( uirow, 9, file->Component->Directory );
1350 msi_ui_actiondata( package, szRemoveFiles, uirow );
1351 msiobj_release( &uirow->hdr );
1352 /* FIXME: call msi_ui_progress here? */
1355 return ERROR_SUCCESS;