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 These are
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 ui_actiondata( package, action, uirow );
62 msiobj_release( &uirow->hdr );
63 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 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 = get_loaded_folder( package, dir );
241 if (folder->State == 0)
243 create_full_pathW( 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 = 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;
285 * ACTION_InstallFiles()
287 * For efficiency, this is done in two passes:
288 * 1) Correct all the TargetPaths and determine what files are to be installed.
289 * 2) Extract Cabinets and copy files.
291 UINT ACTION_InstallFiles(MSIPACKAGE *package)
295 UINT rc = ERROR_SUCCESS;
298 /* increment progress bar each time action data is sent */
299 ui_progress(package,1,1,0,0);
301 schedule_install_files(package);
303 mi = msi_alloc_zero( sizeof(MSIMEDIAINFO) );
305 LIST_FOR_EACH_ENTRY( file, &package->files, MSIFILE, entry )
307 rc = msi_load_media_info( package, file->Sequence, mi );
308 if (rc != ERROR_SUCCESS)
310 ERR("Unable to load media info for %s (%u)\n", debugstr_w(file->File), rc);
311 return ERROR_FUNCTION_FAILED;
313 if (!file->Component->Enabled) continue;
315 if (file->state != msifs_hashmatch &&
316 (rc = ready_media( package, file->Sequence, file->IsCompressed, mi )))
318 ERR("Failed to ready media for %s\n", debugstr_w(file->File));
322 if (file->state != msifs_missing && !mi->is_continuous && file->state != msifs_overwrite)
325 if (file->Sequence > mi->last_sequence || mi->is_continuous ||
326 (file->IsCompressed && !mi->is_extracted))
331 data.package = package;
332 data.cb = installfiles_cb;
333 data.user = (PVOID)(UINT_PTR)mi->disk_id;
335 if (file->IsCompressed &&
336 !msi_cabextract(package, mi, &data))
338 ERR("Failed to extract cabinet: %s\n", debugstr_w(mi->cabinet));
339 rc = ERROR_INSTALL_FAILURE;
344 if (!file->IsCompressed)
346 LPWSTR source = resolve_file_source(package, file);
348 TRACE("copying %s to %s\n", debugstr_w(source), debugstr_w(file->TargetPath));
350 msi_file_update_ui(package, file, szInstallFiles);
351 if (!file->Component->assembly || file->Component->assembly->application)
353 msi_create_directory(package, file->Component->Directory);
355 rc = copy_install_file(package, file, source);
356 if (rc != ERROR_SUCCESS)
358 ERR("Failed to copy %s to %s (%d)\n", debugstr_w(source),
359 debugstr_w(file->TargetPath), rc);
360 rc = ERROR_INSTALL_FAILURE;
366 else if (file->state != msifs_installed && !(file->Attributes & msidbFileAttributesPatchAdded))
368 ERR("compressed file wasn't installed (%s)\n", debugstr_w(file->TargetPath));
369 rc = ERROR_INSTALL_FAILURE;
373 LIST_FOR_EACH_ENTRY( comp, &package->components, MSICOMPONENT, entry )
375 if (comp->ActionRequest == INSTALLSTATE_LOCAL && comp->Enabled &&
376 comp->assembly && !comp->assembly->installed)
378 rc = install_assembly( package, comp );
379 if (rc != ERROR_SUCCESS)
381 ERR("Failed to install assembly\n");
382 rc = ERROR_INSTALL_FAILURE;
389 msi_free_media_info(mi);
393 static BOOL load_mspatcha(void)
395 hmspatcha = LoadLibraryA("mspatcha.dll");
398 ERR("Failed to load mspatcha.dll: %d\n", GetLastError());
402 ApplyPatchToFileW = (void*)GetProcAddress(hmspatcha, "ApplyPatchToFileW");
403 if(!ApplyPatchToFileW)
405 ERR("GetProcAddress(ApplyPatchToFileW) failed: %d.\n", GetLastError());
412 static void unload_mspatch(void)
414 FreeLibrary(hmspatcha);
417 static BOOL patchfiles_cb(MSIPACKAGE *package, LPCWSTR file, DWORD action,
418 LPWSTR *path, DWORD *attrs, PVOID user)
420 static MSIFILEPATCH *p = NULL;
421 static WCHAR patch_path[MAX_PATH] = {0};
422 static WCHAR temp_folder[MAX_PATH] = {0};
424 if (action == MSICABEXTRACT_BEGINEXTRACT)
426 if (temp_folder[0] == '\0')
427 GetTempPathW(MAX_PATH, temp_folder);
429 p = get_loaded_filepatch(package, file);
432 TRACE("unknown file in cabinet (%s)\n", debugstr_w(file));
436 msi_file_update_ui(package, p->File, szPatchFiles);
438 GetTempFileNameW(temp_folder, NULL, 0, patch_path);
440 *path = strdupW(patch_path);
441 *attrs = p->File->Attributes;
443 else if (action == MSICABEXTRACT_FILEEXTRACTED)
445 WCHAR patched_file[MAX_PATH];
448 GetTempFileNameW(temp_folder, NULL, 0, patched_file);
450 br = ApplyPatchToFileW(patch_path, p->File->TargetPath, patched_file, 0);
453 /* FIXME: baseline cache */
455 DeleteFileW( p->File->TargetPath );
456 MoveFileW( patched_file, p->File->TargetPath );
461 ERR("Failed patch %s: %d.\n", debugstr_w(p->File->TargetPath), GetLastError());
463 DeleteFileW(patch_path);
470 UINT ACTION_PatchFiles( MSIPACKAGE *package )
474 UINT rc = ERROR_SUCCESS;
475 BOOL mspatcha_loaded = FALSE;
477 TRACE("%p\n", package);
479 /* increment progress bar each time action data is sent */
480 ui_progress(package,1,1,0,0);
482 mi = msi_alloc_zero( sizeof(MSIMEDIAINFO) );
484 LIST_FOR_EACH_ENTRY( patch, &package->filepatches, MSIFILEPATCH, entry )
486 MSIFILE *file = patch->File;
488 rc = msi_load_media_info( package, patch->Sequence, mi );
489 if (rc != ERROR_SUCCESS)
491 ERR("Unable to load media info for %s (%u)\n", debugstr_w(file->File), rc);
492 return ERROR_FUNCTION_FAILED;
494 if (!file->Component->Enabled) continue;
496 if (!patch->IsApplied)
500 rc = ready_media( package, patch->Sequence, TRUE, mi );
501 if (rc != ERROR_SUCCESS)
503 ERR("Failed to ready media for %s\n", debugstr_w(file->File));
507 if (!mspatcha_loaded && !load_mspatcha())
509 rc = ERROR_FUNCTION_FAILED;
512 mspatcha_loaded = TRUE;
515 data.package = package;
516 data.cb = patchfiles_cb;
517 data.user = (PVOID)(UINT_PTR)mi->disk_id;
519 if (!msi_cabextract(package, mi, &data))
521 ERR("Failed to extract cabinet: %s\n", debugstr_w(mi->cabinet));
522 rc = ERROR_INSTALL_FAILURE;
527 if (!patch->IsApplied && !(patch->Attributes & msidbPatchAttributesNonVital))
529 ERR("Failed to apply patch to file: %s\n", debugstr_w(file->File));
530 rc = ERROR_INSTALL_FAILURE;
536 msi_free_media_info(mi);
542 #define is_dot_dir(x) ((x[0] == '.') && ((x[1] == 0) || ((x[1] == '.') && (x[2] == 0))))
553 static BOOL msi_move_file(LPCWSTR source, LPCWSTR dest, int options)
557 if (GetFileAttributesW(source) == FILE_ATTRIBUTE_DIRECTORY ||
558 GetFileAttributesW(dest) == FILE_ATTRIBUTE_DIRECTORY)
560 WARN("Source or dest is directory, not moving\n");
564 if (options == msidbMoveFileOptionsMove)
566 TRACE("moving %s -> %s\n", debugstr_w(source), debugstr_w(dest));
567 ret = MoveFileExW(source, dest, MOVEFILE_REPLACE_EXISTING);
570 WARN("MoveFile failed: %d\n", GetLastError());
576 TRACE("copying %s -> %s\n", debugstr_w(source), debugstr_w(dest));
577 ret = CopyFileW(source, dest, FALSE);
580 WARN("CopyFile failed: %d\n", GetLastError());
588 static LPWSTR wildcard_to_file(LPWSTR wildcard, LPWSTR filename)
591 DWORD dirlen, pathlen;
593 ptr = strrchrW(wildcard, '\\');
594 dirlen = ptr - wildcard + 1;
596 pathlen = dirlen + lstrlenW(filename) + 1;
597 path = msi_alloc(pathlen * sizeof(WCHAR));
599 lstrcpynW(path, wildcard, dirlen + 1);
600 lstrcatW(path, filename);
605 static void free_file_entry(FILE_LIST *file)
607 msi_free(file->source);
608 msi_free(file->dest);
612 static void free_list(FILE_LIST *list)
614 while (!list_empty(&list->entry))
616 FILE_LIST *file = LIST_ENTRY(list_head(&list->entry), FILE_LIST, entry);
618 list_remove(&file->entry);
619 free_file_entry(file);
623 static BOOL add_wildcard(FILE_LIST *files, LPWSTR source, LPWSTR dest)
625 FILE_LIST *new, *file;
626 LPWSTR ptr, filename;
629 new = msi_alloc_zero(sizeof(FILE_LIST));
633 new->source = strdupW(source);
634 ptr = strrchrW(dest, '\\') + 1;
635 filename = strrchrW(new->source, '\\') + 1;
637 new->sourcename = filename;
642 new->destname = new->sourcename;
644 size = (ptr - dest) + lstrlenW(filename) + 1;
645 new->dest = msi_alloc(size * sizeof(WCHAR));
648 free_file_entry(new);
652 lstrcpynW(new->dest, dest, ptr - dest + 1);
653 lstrcatW(new->dest, filename);
655 if (list_empty(&files->entry))
657 list_add_head(&files->entry, &new->entry);
661 LIST_FOR_EACH_ENTRY(file, &files->entry, FILE_LIST, entry)
663 if (strcmpW( source, file->source ) < 0)
665 list_add_before(&file->entry, &new->entry);
670 list_add_after(&file->entry, &new->entry);
674 static BOOL move_files_wildcard(LPWSTR source, LPWSTR dest, int options)
676 WIN32_FIND_DATAW wfd;
680 FILE_LIST files, *file;
683 hfile = FindFirstFileW(source, &wfd);
684 if (hfile == INVALID_HANDLE_VALUE) return FALSE;
686 list_init(&files.entry);
688 for (res = TRUE; res; res = FindNextFileW(hfile, &wfd))
690 if (is_dot_dir(wfd.cFileName)) continue;
692 path = wildcard_to_file(source, wfd.cFileName);
699 add_wildcard(&files, path, dest);
703 /* no files match the wildcard */
704 if (list_empty(&files.entry))
707 /* only the first wildcard match gets renamed to dest */
708 file = LIST_ENTRY(list_head(&files.entry), FILE_LIST, entry);
709 size = (strrchrW(file->dest, '\\') - file->dest) + lstrlenW(file->destname) + 2;
710 file->dest = msi_realloc(file->dest, size * sizeof(WCHAR));
717 /* file->dest may be shorter after the reallocation, so add a NULL
718 * terminator. This is needed for the call to strrchrW, as there will no
719 * longer be a NULL terminator within the bounds of the allocation in this case.
721 file->dest[size - 1] = '\0';
722 lstrcpyW(strrchrW(file->dest, '\\') + 1, file->destname);
724 while (!list_empty(&files.entry))
726 file = LIST_ENTRY(list_head(&files.entry), FILE_LIST, entry);
728 msi_move_file(file->source, file->dest, options);
730 list_remove(&file->entry);
731 free_file_entry(file);
742 static UINT ITERATE_MoveFiles( MSIRECORD *rec, LPVOID param )
744 MSIPACKAGE *package = param;
747 LPCWSTR sourcename, component;
748 LPWSTR sourcedir, destname = NULL, destdir = NULL, source = NULL, dest = NULL;
753 component = MSI_RecordGetString(rec, 2);
754 comp = get_loaded_component(package, component);
756 return ERROR_SUCCESS;
760 TRACE("component is disabled\n");
761 return ERROR_SUCCESS;
764 if (comp->ActionRequest != INSTALLSTATE_LOCAL && comp->ActionRequest != INSTALLSTATE_SOURCE)
766 TRACE("Component not scheduled for installation: %s\n", debugstr_w(component));
767 comp->Action = comp->Installed;
768 return ERROR_SUCCESS;
770 comp->Action = comp->ActionRequest;
772 sourcename = MSI_RecordGetString(rec, 3);
773 options = MSI_RecordGetInteger(rec, 7);
775 sourcedir = msi_dup_property(package->db, MSI_RecordGetString(rec, 5));
779 destdir = msi_dup_property(package->db, MSI_RecordGetString(rec, 6));
785 if (GetFileAttributesW(sourcedir) == INVALID_FILE_ATTRIBUTES)
788 source = strdupW(sourcedir);
794 size = lstrlenW(sourcedir) + lstrlenW(sourcename) + 2;
795 source = msi_alloc(size * sizeof(WCHAR));
799 lstrcpyW(source, sourcedir);
800 if (source[lstrlenW(source) - 1] != '\\')
801 lstrcatW(source, szBackSlash);
802 lstrcatW(source, sourcename);
805 wildcards = strchrW(source, '*') || strchrW(source, '?');
807 if (MSI_RecordIsNull(rec, 4))
811 destname = strdupW(sourcename);
818 destname = strdupW(MSI_RecordGetString(rec, 4));
820 reduce_to_longfilename(destname);
825 size = lstrlenW(destname);
827 size += lstrlenW(destdir) + 2;
828 dest = msi_alloc(size * sizeof(WCHAR));
832 lstrcpyW(dest, destdir);
833 if (dest[lstrlenW(dest) - 1] != '\\')
834 lstrcatW(dest, szBackSlash);
837 lstrcatW(dest, destname);
839 if (GetFileAttributesW(destdir) == INVALID_FILE_ATTRIBUTES)
841 ret = CreateDirectoryW(destdir, NULL);
844 WARN("CreateDirectory failed: %d\n", GetLastError());
850 msi_move_file(source, dest, options);
852 move_files_wildcard(source, dest, options);
855 uirow = MSI_CreateRecord( 9 );
856 MSI_RecordSetStringW( uirow, 1, MSI_RecordGetString(rec, 1) );
857 MSI_RecordSetInteger( uirow, 6, 1 ); /* FIXME */
858 MSI_RecordSetStringW( uirow, 9, destdir );
859 ui_actiondata( package, szMoveFiles, uirow );
860 msiobj_release( &uirow->hdr );
868 return ERROR_SUCCESS;
871 UINT ACTION_MoveFiles( MSIPACKAGE *package )
876 static const WCHAR ExecSeqQuery[] =
877 {'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
878 '`','M','o','v','e','F','i','l','e','`',0};
880 rc = MSI_DatabaseOpenViewW(package->db, ExecSeqQuery, &view);
881 if (rc != ERROR_SUCCESS)
882 return ERROR_SUCCESS;
884 rc = MSI_IterateRecords(view, NULL, ITERATE_MoveFiles, package);
885 msiobj_release(&view->hdr);
890 static WCHAR *get_duplicate_filename( MSIPACKAGE *package, MSIRECORD *row, const WCHAR *file_key, const WCHAR *src )
893 WCHAR *dst_name, *dst_path, *dst;
895 if (MSI_RecordIsNull( row, 4 ))
897 len = strlenW( src ) + 1;
898 if (!(dst_name = msi_alloc( len * sizeof(WCHAR)))) return NULL;
899 strcpyW( dst_name, strrchrW( src, '\\' ) + 1 );
903 MSI_RecordGetStringW( row, 4, NULL, &len );
904 if (!(dst_name = msi_alloc( ++len * sizeof(WCHAR) ))) return NULL;
905 MSI_RecordGetStringW( row, 4, dst_name, &len );
906 reduce_to_longfilename( dst_name );
909 if (MSI_RecordIsNull( row, 5 ))
912 dst_path = strdupW( src );
913 p = strrchrW( dst_path, '\\' );
918 const WCHAR *dst_key = MSI_RecordGetString( row, 5 );
920 dst_path = strdupW( msi_get_target_folder( package, dst_key ) );
924 dst_path = msi_dup_property( package->db, dst_key );
927 FIXME("Unable to get destination folder, try AppSearch properties\n");
928 msi_free( dst_name );
934 dst = build_directory_name( 2, dst_path, dst_name );
935 create_full_pathW( dst_path );
937 msi_free( dst_name );
938 msi_free( dst_path );
942 static UINT ITERATE_DuplicateFiles(MSIRECORD *row, LPVOID param)
944 MSIPACKAGE *package = param;
946 LPCWSTR file_key, component;
951 component = MSI_RecordGetString(row,2);
952 comp = get_loaded_component(package,component);
954 return ERROR_SUCCESS;
958 TRACE("component is disabled\n");
959 return ERROR_SUCCESS;
962 if (comp->ActionRequest != INSTALLSTATE_LOCAL)
964 TRACE("Component not scheduled for installation %s\n", debugstr_w(component));
965 comp->Action = comp->Installed;
966 return ERROR_SUCCESS;
968 comp->Action = INSTALLSTATE_LOCAL;
970 file_key = MSI_RecordGetString(row,3);
973 ERR("Unable to get file key\n");
974 return ERROR_FUNCTION_FAILED;
977 file = get_loaded_file( package, file_key );
980 ERR("Original file unknown %s\n", debugstr_w(file_key));
981 return ERROR_SUCCESS;
984 dest = get_duplicate_filename( package, row, file_key, file->TargetPath );
987 WARN("Unable to get duplicate filename\n");
988 return ERROR_SUCCESS;
991 TRACE("Duplicating file %s to %s\n", debugstr_w(file->TargetPath), debugstr_w(dest));
993 if (!CopyFileW( file->TargetPath, dest, TRUE ))
995 WARN("Failed to copy file %s -> %s (%u)\n",
996 debugstr_w(file->TargetPath), debugstr_w(dest), GetLastError());
999 FIXME("We should track these duplicate files as well\n");
1001 uirow = MSI_CreateRecord( 9 );
1002 MSI_RecordSetStringW( uirow, 1, MSI_RecordGetString( row, 1 ) );
1003 MSI_RecordSetInteger( uirow, 6, file->FileSize );
1004 MSI_RecordSetStringW( uirow, 9, MSI_RecordGetString( row, 5 ) );
1005 ui_actiondata( package, szDuplicateFiles, uirow );
1006 msiobj_release( &uirow->hdr );
1009 return ERROR_SUCCESS;
1012 UINT ACTION_DuplicateFiles(MSIPACKAGE *package)
1016 static const WCHAR ExecSeqQuery[] =
1017 {'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
1018 '`','D','u','p','l','i','c','a','t','e','F','i','l','e','`',0};
1020 rc = MSI_DatabaseOpenViewW(package->db, ExecSeqQuery, &view);
1021 if (rc != ERROR_SUCCESS)
1022 return ERROR_SUCCESS;
1024 rc = MSI_IterateRecords(view, NULL, ITERATE_DuplicateFiles, package);
1025 msiobj_release(&view->hdr);
1030 static UINT ITERATE_RemoveDuplicateFiles( MSIRECORD *row, LPVOID param )
1032 MSIPACKAGE *package = param;
1034 LPCWSTR file_key, component;
1039 component = MSI_RecordGetString( row, 2 );
1040 comp = get_loaded_component( package, component );
1042 return ERROR_SUCCESS;
1046 TRACE("component is disabled\n");
1047 return ERROR_SUCCESS;
1050 if (comp->ActionRequest != INSTALLSTATE_ABSENT)
1052 TRACE("Component not scheduled for removal %s\n", debugstr_w(component));
1053 comp->Action = comp->Installed;
1054 return ERROR_SUCCESS;
1056 comp->Action = INSTALLSTATE_ABSENT;
1058 file_key = MSI_RecordGetString( row, 3 );
1061 ERR("Unable to get file key\n");
1062 return ERROR_FUNCTION_FAILED;
1065 file = get_loaded_file( package, file_key );
1068 ERR("Original file unknown %s\n", debugstr_w(file_key));
1069 return ERROR_SUCCESS;
1072 dest = get_duplicate_filename( package, row, file_key, file->TargetPath );
1075 WARN("Unable to get duplicate filename\n");
1076 return ERROR_SUCCESS;
1079 TRACE("Removing duplicate %s of %s\n", debugstr_w(dest), debugstr_w(file->TargetPath));
1081 if (!DeleteFileW( dest ))
1083 WARN("Failed to delete duplicate file %s (%u)\n", debugstr_w(dest), GetLastError());
1086 uirow = MSI_CreateRecord( 9 );
1087 MSI_RecordSetStringW( uirow, 1, MSI_RecordGetString( row, 1 ) );
1088 MSI_RecordSetStringW( uirow, 9, MSI_RecordGetString( row, 5 ) );
1089 ui_actiondata( package, szRemoveDuplicateFiles, uirow );
1090 msiobj_release( &uirow->hdr );
1093 return ERROR_SUCCESS;
1096 UINT ACTION_RemoveDuplicateFiles( MSIPACKAGE *package )
1100 static const WCHAR query[] =
1101 {'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
1102 '`','D','u','p','l','i','c','a','t','e','F','i','l','e','`',0};
1104 rc = MSI_DatabaseOpenViewW( package->db, query, &view );
1105 if (rc != ERROR_SUCCESS)
1106 return ERROR_SUCCESS;
1108 rc = MSI_IterateRecords( view, NULL, ITERATE_RemoveDuplicateFiles, package );
1109 msiobj_release( &view->hdr );
1114 static BOOL verify_comp_for_removal(MSICOMPONENT *comp, UINT install_mode)
1116 INSTALLSTATE request = comp->ActionRequest;
1119 if (request != INSTALLSTATE_SOURCE &&
1120 comp->Attributes & msidbComponentAttributesSourceOnly &&
1121 (install_mode == msidbRemoveFileInstallModeOnRemove ||
1122 install_mode == msidbRemoveFileInstallModeOnBoth)) return TRUE;
1126 case INSTALLSTATE_LOCAL:
1127 case INSTALLSTATE_SOURCE:
1128 if (install_mode == msidbRemoveFileInstallModeOnInstall ||
1129 install_mode == msidbRemoveFileInstallModeOnBoth) return TRUE;
1131 case INSTALLSTATE_ABSENT:
1132 if (install_mode == msidbRemoveFileInstallModeOnRemove ||
1133 install_mode == msidbRemoveFileInstallModeOnBoth) return TRUE;
1140 static UINT ITERATE_RemoveFiles(MSIRECORD *row, LPVOID param)
1142 MSIPACKAGE *package = param;
1145 LPCWSTR component, dirprop;
1147 LPWSTR dir = NULL, path = NULL, filename = NULL;
1149 UINT ret = ERROR_SUCCESS;
1151 component = MSI_RecordGetString(row, 2);
1152 dirprop = MSI_RecordGetString(row, 4);
1153 install_mode = MSI_RecordGetInteger(row, 5);
1155 comp = get_loaded_component(package, component);
1158 TRACE("component is disabled\n");
1159 return ERROR_SUCCESS;
1162 if (!verify_comp_for_removal(comp, install_mode))
1164 TRACE("Skipping removal due to install mode\n");
1165 comp->Action = comp->Installed;
1166 return ERROR_SUCCESS;
1169 if (comp->Attributes & msidbComponentAttributesPermanent)
1171 TRACE("permanent component, not removing file\n");
1172 return ERROR_SUCCESS;
1175 dir = msi_dup_property(package->db, dirprop);
1177 return ERROR_OUTOFMEMORY;
1180 if ((filename = strdupW( MSI_RecordGetString(row, 3) )))
1182 reduce_to_longfilename( filename );
1183 size = lstrlenW( filename );
1185 size += lstrlenW(dir) + 2;
1186 path = msi_alloc(size * sizeof(WCHAR));
1189 ret = ERROR_OUTOFMEMORY;
1195 lstrcpyW(path, dir);
1196 PathAddBackslashW(path);
1197 lstrcatW(path, filename);
1199 TRACE("Deleting misc file: %s\n", debugstr_w(path));
1204 TRACE("Removing misc directory: %s\n", debugstr_w(dir));
1205 RemoveDirectoryW(dir);
1209 uirow = MSI_CreateRecord( 9 );
1210 MSI_RecordSetStringW( uirow, 1, MSI_RecordGetString(row, 1) );
1211 MSI_RecordSetStringW( uirow, 9, dir );
1212 ui_actiondata( package, szRemoveFiles, uirow );
1213 msiobj_release( &uirow->hdr );
1221 static BOOL has_persistent_dir( MSIPACKAGE *package, MSICOMPONENT *comp )
1224 UINT r = ERROR_FUNCTION_FAILED;
1226 static const WCHAR query[] = {
1227 'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
1228 '`','C','r','e','a','t','e','F','o','l','d','e','r','`',' ','W','H','E','R','E',' ',
1229 '`','C','o','m','p','o','n','e','n','t','_','`',' ','=','\'','%','s','\'',' ','A','N','D',' ',
1230 '`','D','i','r','e','c','t','o','r','y','_','`',' ','=','\'','%','s','\'',0};
1232 if (!MSI_OpenQuery( package->db, &view, query, comp->Component, comp->Directory ))
1234 if (!MSI_ViewExecute( view, NULL ))
1237 if (!(r = MSI_ViewFetch( view, &rec )))
1239 TRACE("directory %s is persistent\n", debugstr_w(comp->Directory));
1240 msiobj_release( &rec->hdr );
1243 msiobj_release( &view->hdr );
1245 return (r == ERROR_SUCCESS);
1248 UINT ACTION_RemoveFiles( MSIPACKAGE *package )
1254 static const WCHAR query[] = {
1255 'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
1256 '`','R','e','m','o','v','e','F','i','l','e','`',0};
1258 r = MSI_DatabaseOpenViewW(package->db, query, &view);
1259 if (r == ERROR_SUCCESS)
1261 MSI_IterateRecords(view, NULL, ITERATE_RemoveFiles, package);
1262 msiobj_release(&view->hdr);
1265 LIST_FOR_EACH_ENTRY( file, &package->files, MSIFILE, entry )
1269 VS_FIXEDFILEINFO *ver;
1271 if ( file->state == msifs_installed )
1272 ERR("removing installed file %s\n", debugstr_w(file->TargetPath));
1274 if ( file->Component->ActionRequest != INSTALLSTATE_ABSENT ||
1275 file->Component->Installed == INSTALLSTATE_SOURCE )
1278 if (!file->Component->Enabled)
1280 TRACE("component is disabled\n");
1284 if (file->Component->Attributes & msidbComponentAttributesPermanent)
1286 TRACE("permanent component, not removing file\n");
1292 ver = msi_get_disk_file_version( file->TargetPath );
1293 if (ver && msi_compare_file_versions( ver, file->Version ) > 0)
1295 TRACE("newer version detected, not removing file\n");
1302 TRACE("removing %s\n", debugstr_w(file->File) );
1304 SetFileAttributesW( file->TargetPath, FILE_ATTRIBUTE_NORMAL );
1305 if (!DeleteFileW( file->TargetPath ))
1307 WARN("failed to delete %s (%u)\n", debugstr_w(file->TargetPath), GetLastError());
1309 else if (!has_persistent_dir( package, file->Component ))
1311 if ((dir = strdupW( file->TargetPath )))
1313 if ((p = strrchrW( dir, '\\' ))) *p = 0;
1314 RemoveDirectoryW( dir );
1318 file->state = msifs_missing;
1320 uirow = MSI_CreateRecord( 9 );
1321 MSI_RecordSetStringW( uirow, 1, file->FileName );
1322 MSI_RecordSetStringW( uirow, 9, file->Component->Directory );
1323 ui_actiondata( package, szRemoveFiles, uirow );
1324 msiobj_release( &uirow->hdr );
1325 /* FIXME: call ui_progress here? */
1328 return ERROR_SUCCESS;