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 )
237 install_path = resolve_target_folder( package, dir, FALSE, TRUE, &folder );
239 return ERROR_FUNCTION_FAILED;
241 if (folder->State == 0)
243 create_full_pathW( install_path );
246 msi_free( install_path );
247 return ERROR_SUCCESS;
250 static BOOL installfiles_cb(MSIPACKAGE *package, LPCWSTR file, DWORD action,
251 LPWSTR *path, DWORD *attrs, PVOID user)
253 static MSIFILE *f = NULL;
254 UINT_PTR disk_id = (UINT_PTR)user;
256 if (action == MSICABEXTRACT_BEGINEXTRACT)
258 f = get_loaded_file(package, file);
261 TRACE("unknown file in cabinet (%s)\n", debugstr_w(file));
265 if (f->disk_id != disk_id || (f->state != msifs_missing && f->state != msifs_overwrite))
268 msi_file_update_ui(package, f, szInstallFiles);
269 if (!f->Component->assembly || f->Component->assembly->application)
271 msi_create_directory(package, f->Component->Directory);
273 *path = strdupW(f->TargetPath);
274 *attrs = f->Attributes;
276 else if (action == MSICABEXTRACT_FILEEXTRACTED)
278 f->state = msifs_installed;
286 * ACTION_InstallFiles()
288 * For efficiency, this is done in two passes:
289 * 1) Correct all the TargetPaths and determine what files are to be installed.
290 * 2) Extract Cabinets and copy files.
292 UINT ACTION_InstallFiles(MSIPACKAGE *package)
296 UINT rc = ERROR_SUCCESS;
299 /* increment progress bar each time action data is sent */
300 ui_progress(package,1,1,0,0);
302 schedule_install_files(package);
304 mi = msi_alloc_zero( sizeof(MSIMEDIAINFO) );
306 LIST_FOR_EACH_ENTRY( file, &package->files, MSIFILE, entry )
308 rc = msi_load_media_info( package, file->Sequence, mi );
309 if (rc != ERROR_SUCCESS)
311 ERR("Unable to load media info for %s (%u)\n", debugstr_w(file->File), rc);
312 return ERROR_FUNCTION_FAILED;
314 if (!file->Component->Enabled) continue;
316 if (file->state != msifs_hashmatch &&
317 (rc = ready_media( package, file->Sequence, file->IsCompressed, mi )))
319 ERR("Failed to ready media for %s\n", debugstr_w(file->File));
323 if (file->state != msifs_missing && !mi->is_continuous && file->state != msifs_overwrite)
326 if (file->Sequence > mi->last_sequence || mi->is_continuous ||
327 (file->IsCompressed && !mi->is_extracted))
332 data.package = package;
333 data.cb = installfiles_cb;
334 data.user = (PVOID)(UINT_PTR)mi->disk_id;
336 if (file->IsCompressed &&
337 !msi_cabextract(package, mi, &data))
339 ERR("Failed to extract cabinet: %s\n", debugstr_w(mi->cabinet));
340 rc = ERROR_INSTALL_FAILURE;
345 if (!file->IsCompressed)
347 LPWSTR source = resolve_file_source(package, file);
349 TRACE("copying %s to %s\n", debugstr_w(source), debugstr_w(file->TargetPath));
351 msi_file_update_ui(package, file, szInstallFiles);
352 if (!file->Component->assembly || file->Component->assembly->application)
354 msi_create_directory(package, file->Component->Directory);
356 rc = copy_install_file(package, file, source);
357 if (rc != ERROR_SUCCESS)
359 ERR("Failed to copy %s to %s (%d)\n", debugstr_w(source),
360 debugstr_w(file->TargetPath), rc);
361 rc = ERROR_INSTALL_FAILURE;
367 else if (file->state != msifs_installed)
369 ERR("compressed file wasn't installed (%s)\n", debugstr_w(file->TargetPath));
370 rc = ERROR_INSTALL_FAILURE;
374 LIST_FOR_EACH_ENTRY( comp, &package->components, MSICOMPONENT, entry )
376 if (comp->ActionRequest == INSTALLSTATE_LOCAL && comp->Enabled &&
377 comp->assembly && !comp->assembly->installed)
379 rc = install_assembly( package, comp );
380 if (rc != ERROR_SUCCESS)
382 ERR("Failed to install assembly\n");
383 rc = ERROR_INSTALL_FAILURE;
390 msi_free_media_info(mi);
394 static BOOL load_mspatcha(void)
396 hmspatcha = LoadLibraryA("mspatcha.dll");
399 ERR("Failed to load mspatcha.dll: %d\n", GetLastError());
403 ApplyPatchToFileW = (void*)GetProcAddress(hmspatcha, "ApplyPatchToFileW");
404 if(!ApplyPatchToFileW)
406 ERR("GetProcAddress(ApplyPatchToFileW) failed: %d.\n", GetLastError());
413 static void unload_mspatch(void)
415 FreeLibrary(hmspatcha);
418 static BOOL patchfiles_cb(MSIPACKAGE *package, LPCWSTR file, DWORD action,
419 LPWSTR *path, DWORD *attrs, PVOID user)
421 static MSIFILEPATCH *p = NULL;
422 static WCHAR patch_path[MAX_PATH] = {0};
423 static WCHAR temp_folder[MAX_PATH] = {0};
425 if (action == MSICABEXTRACT_BEGINEXTRACT)
427 if (temp_folder[0] == '\0')
428 GetTempPathW(MAX_PATH, temp_folder);
430 p = get_loaded_filepatch(package, file);
433 TRACE("unknown file in cabinet (%s)\n", debugstr_w(file));
437 msi_file_update_ui(package, p->File, szPatchFiles);
439 GetTempFileNameW(temp_folder, NULL, 0, patch_path);
441 *path = strdupW(patch_path);
442 *attrs = p->File->Attributes;
444 else if (action == MSICABEXTRACT_FILEEXTRACTED)
446 WCHAR patched_file[MAX_PATH];
449 GetTempFileNameW(temp_folder, NULL, 0, patched_file);
451 br = ApplyPatchToFileW(patch_path, p->File->TargetPath, patched_file, 0);
454 /* FIXME: baseline cache */
456 DeleteFileW( p->File->TargetPath );
457 MoveFileW( patched_file, p->File->TargetPath );
462 ERR("Failed patch %s: %d.\n", debugstr_w(p->File->TargetPath), GetLastError());
464 DeleteFileW(patch_path);
471 UINT ACTION_PatchFiles( MSIPACKAGE *package )
475 UINT rc = ERROR_SUCCESS;
476 BOOL mspatcha_loaded = FALSE;
478 TRACE("%p\n", package);
480 /* increment progress bar each time action data is sent */
481 ui_progress(package,1,1,0,0);
483 mi = msi_alloc_zero( sizeof(MSIMEDIAINFO) );
485 LIST_FOR_EACH_ENTRY( patch, &package->filepatches, MSIFILEPATCH, entry )
487 MSIFILE *file = patch->File;
489 rc = msi_load_media_info( package, patch->Sequence, mi );
490 if (rc != ERROR_SUCCESS)
492 ERR("Unable to load media info for %s (%u)\n", debugstr_w(file->File), rc);
493 return ERROR_FUNCTION_FAILED;
495 if (!file->Component->Enabled) continue;
497 if (!patch->IsApplied)
501 rc = ready_media( package, patch->Sequence, TRUE, mi );
502 if (rc != ERROR_SUCCESS)
504 ERR("Failed to ready media for %s\n", debugstr_w(file->File));
508 if (!mspatcha_loaded && !load_mspatcha())
510 rc = ERROR_FUNCTION_FAILED;
513 mspatcha_loaded = TRUE;
516 data.package = package;
517 data.cb = patchfiles_cb;
518 data.user = (PVOID)(UINT_PTR)mi->disk_id;
520 if (!msi_cabextract(package, mi, &data))
522 ERR("Failed to extract cabinet: %s\n", debugstr_w(mi->cabinet));
523 rc = ERROR_INSTALL_FAILURE;
528 if (!patch->IsApplied && !(patch->Attributes & msidbPatchAttributesNonVital))
530 ERR("Failed to apply patch to file: %s\n", debugstr_w(file->File));
531 rc = ERROR_INSTALL_FAILURE;
537 msi_free_media_info(mi);
543 #define is_dot_dir(x) ((x[0] == '.') && ((x[1] == 0) || ((x[1] == '.') && (x[2] == 0))))
554 static BOOL msi_move_file(LPCWSTR source, LPCWSTR dest, int options)
558 if (GetFileAttributesW(source) == FILE_ATTRIBUTE_DIRECTORY ||
559 GetFileAttributesW(dest) == FILE_ATTRIBUTE_DIRECTORY)
561 WARN("Source or dest is directory, not moving\n");
565 if (options == msidbMoveFileOptionsMove)
567 TRACE("moving %s -> %s\n", debugstr_w(source), debugstr_w(dest));
568 ret = MoveFileExW(source, dest, MOVEFILE_REPLACE_EXISTING);
571 WARN("MoveFile failed: %d\n", GetLastError());
577 TRACE("copying %s -> %s\n", debugstr_w(source), debugstr_w(dest));
578 ret = CopyFileW(source, dest, FALSE);
581 WARN("CopyFile failed: %d\n", GetLastError());
589 static LPWSTR wildcard_to_file(LPWSTR wildcard, LPWSTR filename)
592 DWORD dirlen, pathlen;
594 ptr = strrchrW(wildcard, '\\');
595 dirlen = ptr - wildcard + 1;
597 pathlen = dirlen + lstrlenW(filename) + 1;
598 path = msi_alloc(pathlen * sizeof(WCHAR));
600 lstrcpynW(path, wildcard, dirlen + 1);
601 lstrcatW(path, filename);
606 static void free_file_entry(FILE_LIST *file)
608 msi_free(file->source);
609 msi_free(file->dest);
613 static void free_list(FILE_LIST *list)
615 while (!list_empty(&list->entry))
617 FILE_LIST *file = LIST_ENTRY(list_head(&list->entry), FILE_LIST, entry);
619 list_remove(&file->entry);
620 free_file_entry(file);
624 static BOOL add_wildcard(FILE_LIST *files, LPWSTR source, LPWSTR dest)
626 FILE_LIST *new, *file;
627 LPWSTR ptr, filename;
630 new = msi_alloc_zero(sizeof(FILE_LIST));
634 new->source = strdupW(source);
635 ptr = strrchrW(dest, '\\') + 1;
636 filename = strrchrW(new->source, '\\') + 1;
638 new->sourcename = filename;
643 new->destname = new->sourcename;
645 size = (ptr - dest) + lstrlenW(filename) + 1;
646 new->dest = msi_alloc(size * sizeof(WCHAR));
649 free_file_entry(new);
653 lstrcpynW(new->dest, dest, ptr - dest + 1);
654 lstrcatW(new->dest, filename);
656 if (list_empty(&files->entry))
658 list_add_head(&files->entry, &new->entry);
662 LIST_FOR_EACH_ENTRY(file, &files->entry, FILE_LIST, entry)
664 if (strcmpW( source, file->source ) < 0)
666 list_add_before(&file->entry, &new->entry);
671 list_add_after(&file->entry, &new->entry);
675 static BOOL move_files_wildcard(LPWSTR source, LPWSTR dest, int options)
677 WIN32_FIND_DATAW wfd;
681 FILE_LIST files, *file;
684 hfile = FindFirstFileW(source, &wfd);
685 if (hfile == INVALID_HANDLE_VALUE) return FALSE;
687 list_init(&files.entry);
689 for (res = TRUE; res; res = FindNextFileW(hfile, &wfd))
691 if (is_dot_dir(wfd.cFileName)) continue;
693 path = wildcard_to_file(source, wfd.cFileName);
700 add_wildcard(&files, path, dest);
704 /* no files match the wildcard */
705 if (list_empty(&files.entry))
708 /* only the first wildcard match gets renamed to dest */
709 file = LIST_ENTRY(list_head(&files.entry), FILE_LIST, entry);
710 size = (strrchrW(file->dest, '\\') - file->dest) + lstrlenW(file->destname) + 2;
711 file->dest = msi_realloc(file->dest, size * sizeof(WCHAR));
718 /* file->dest may be shorter after the reallocation, so add a NULL
719 * terminator. This is needed for the call to strrchrW, as there will no
720 * longer be a NULL terminator within the bounds of the allocation in this case.
722 file->dest[size - 1] = '\0';
723 lstrcpyW(strrchrW(file->dest, '\\') + 1, file->destname);
725 while (!list_empty(&files.entry))
727 file = LIST_ENTRY(list_head(&files.entry), FILE_LIST, entry);
729 msi_move_file(file->source, file->dest, options);
731 list_remove(&file->entry);
732 free_file_entry(file);
743 static UINT ITERATE_MoveFiles( MSIRECORD *rec, LPVOID param )
745 MSIPACKAGE *package = param;
748 LPCWSTR sourcename, component;
749 LPWSTR sourcedir, destname = NULL, destdir = NULL, source = NULL, dest = NULL;
754 component = MSI_RecordGetString(rec, 2);
755 comp = get_loaded_component(package, component);
757 return ERROR_SUCCESS;
761 TRACE("component is disabled\n");
762 return ERROR_SUCCESS;
765 if (comp->ActionRequest != INSTALLSTATE_LOCAL && comp->ActionRequest != INSTALLSTATE_SOURCE)
767 TRACE("Component not scheduled for installation: %s\n", debugstr_w(component));
768 comp->Action = comp->Installed;
769 return ERROR_SUCCESS;
771 comp->Action = comp->ActionRequest;
773 sourcename = MSI_RecordGetString(rec, 3);
774 options = MSI_RecordGetInteger(rec, 7);
776 sourcedir = msi_dup_property(package->db, MSI_RecordGetString(rec, 5));
780 destdir = msi_dup_property(package->db, MSI_RecordGetString(rec, 6));
786 if (GetFileAttributesW(sourcedir) == INVALID_FILE_ATTRIBUTES)
789 source = strdupW(sourcedir);
795 size = lstrlenW(sourcedir) + lstrlenW(sourcename) + 2;
796 source = msi_alloc(size * sizeof(WCHAR));
800 lstrcpyW(source, sourcedir);
801 if (source[lstrlenW(source) - 1] != '\\')
802 lstrcatW(source, szBackSlash);
803 lstrcatW(source, sourcename);
806 wildcards = strchrW(source, '*') || strchrW(source, '?');
808 if (MSI_RecordIsNull(rec, 4))
812 destname = strdupW(sourcename);
819 destname = strdupW(MSI_RecordGetString(rec, 4));
821 reduce_to_longfilename(destname);
826 size = lstrlenW(destname);
828 size += lstrlenW(destdir) + 2;
829 dest = msi_alloc(size * sizeof(WCHAR));
833 lstrcpyW(dest, destdir);
834 if (dest[lstrlenW(dest) - 1] != '\\')
835 lstrcatW(dest, szBackSlash);
838 lstrcatW(dest, destname);
840 if (GetFileAttributesW(destdir) == INVALID_FILE_ATTRIBUTES)
842 ret = CreateDirectoryW(destdir, NULL);
845 WARN("CreateDirectory failed: %d\n", GetLastError());
851 msi_move_file(source, dest, options);
853 move_files_wildcard(source, dest, options);
856 uirow = MSI_CreateRecord( 9 );
857 MSI_RecordSetStringW( uirow, 1, MSI_RecordGetString(rec, 1) );
858 MSI_RecordSetInteger( uirow, 6, 1 ); /* FIXME */
859 MSI_RecordSetStringW( uirow, 9, destdir );
860 ui_actiondata( package, szMoveFiles, uirow );
861 msiobj_release( &uirow->hdr );
869 return ERROR_SUCCESS;
872 UINT ACTION_MoveFiles( MSIPACKAGE *package )
877 static const WCHAR ExecSeqQuery[] =
878 {'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
879 '`','M','o','v','e','F','i','l','e','`',0};
881 rc = MSI_DatabaseOpenViewW(package->db, ExecSeqQuery, &view);
882 if (rc != ERROR_SUCCESS)
883 return ERROR_SUCCESS;
885 rc = MSI_IterateRecords(view, NULL, ITERATE_MoveFiles, package);
886 msiobj_release(&view->hdr);
891 static WCHAR *get_duplicate_filename( MSIPACKAGE *package, MSIRECORD *row, const WCHAR *file_key, const WCHAR *src )
894 WCHAR *dst_name, *dst_path, *dst;
896 if (MSI_RecordIsNull( row, 4 ))
898 len = strlenW( src ) + 1;
899 if (!(dst_name = msi_alloc( len * sizeof(WCHAR)))) return NULL;
900 strcpyW( dst_name, strrchrW( src, '\\' ) + 1 );
904 MSI_RecordGetStringW( row, 4, NULL, &len );
905 if (!(dst_name = msi_alloc( ++len * sizeof(WCHAR) ))) return NULL;
906 MSI_RecordGetStringW( row, 4, dst_name, &len );
907 reduce_to_longfilename( dst_name );
910 if (MSI_RecordIsNull( row, 5 ))
913 dst_path = strdupW( src );
914 p = strrchrW( dst_path, '\\' );
919 const WCHAR *dst_key = MSI_RecordGetString( row, 5 );
921 dst_path = resolve_target_folder( package, dst_key, FALSE, TRUE, NULL );
925 dst_path = msi_dup_property( package->db, dst_key );
928 FIXME("Unable to get destination folder, try AppSearch properties\n");
929 msi_free( dst_name );
935 dst = build_directory_name( 2, dst_path, dst_name );
936 create_full_pathW( dst_path );
938 msi_free( dst_name );
939 msi_free( dst_path );
943 static UINT ITERATE_DuplicateFiles(MSIRECORD *row, LPVOID param)
945 MSIPACKAGE *package = param;
947 LPCWSTR file_key, component;
952 component = MSI_RecordGetString(row,2);
953 comp = get_loaded_component(package,component);
955 return ERROR_SUCCESS;
959 TRACE("component is disabled\n");
960 return ERROR_SUCCESS;
963 if (comp->ActionRequest != INSTALLSTATE_LOCAL)
965 TRACE("Component not scheduled for installation %s\n", debugstr_w(component));
966 comp->Action = comp->Installed;
967 return ERROR_SUCCESS;
969 comp->Action = INSTALLSTATE_LOCAL;
971 file_key = MSI_RecordGetString(row,3);
974 ERR("Unable to get file key\n");
975 return ERROR_FUNCTION_FAILED;
978 file = get_loaded_file( package, file_key );
981 ERR("Original file unknown %s\n", debugstr_w(file_key));
982 return ERROR_SUCCESS;
985 dest = get_duplicate_filename( package, row, file_key, file->TargetPath );
988 WARN("Unable to get duplicate filename\n");
989 return ERROR_SUCCESS;
992 TRACE("Duplicating file %s to %s\n", debugstr_w(file->TargetPath), debugstr_w(dest));
994 if (!CopyFileW( file->TargetPath, dest, TRUE ))
996 WARN("Failed to copy file %s -> %s (%u)\n",
997 debugstr_w(file->TargetPath), debugstr_w(dest), GetLastError());
1000 FIXME("We should track these duplicate files as well\n");
1002 uirow = MSI_CreateRecord( 9 );
1003 MSI_RecordSetStringW( uirow, 1, MSI_RecordGetString( row, 1 ) );
1004 MSI_RecordSetInteger( uirow, 6, file->FileSize );
1005 MSI_RecordSetStringW( uirow, 9, MSI_RecordGetString( row, 5 ) );
1006 ui_actiondata( package, szDuplicateFiles, uirow );
1007 msiobj_release( &uirow->hdr );
1010 return ERROR_SUCCESS;
1013 UINT ACTION_DuplicateFiles(MSIPACKAGE *package)
1017 static const WCHAR ExecSeqQuery[] =
1018 {'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
1019 '`','D','u','p','l','i','c','a','t','e','F','i','l','e','`',0};
1021 rc = MSI_DatabaseOpenViewW(package->db, ExecSeqQuery, &view);
1022 if (rc != ERROR_SUCCESS)
1023 return ERROR_SUCCESS;
1025 rc = MSI_IterateRecords(view, NULL, ITERATE_DuplicateFiles, package);
1026 msiobj_release(&view->hdr);
1031 static UINT ITERATE_RemoveDuplicateFiles( MSIRECORD *row, LPVOID param )
1033 MSIPACKAGE *package = param;
1035 LPCWSTR file_key, component;
1040 component = MSI_RecordGetString( row, 2 );
1041 comp = get_loaded_component( package, component );
1043 return ERROR_SUCCESS;
1047 TRACE("component is disabled\n");
1048 return ERROR_SUCCESS;
1051 if (comp->ActionRequest != INSTALLSTATE_ABSENT)
1053 TRACE("Component not scheduled for removal %s\n", debugstr_w(component));
1054 comp->Action = comp->Installed;
1055 return ERROR_SUCCESS;
1057 comp->Action = INSTALLSTATE_ABSENT;
1059 file_key = MSI_RecordGetString( row, 3 );
1062 ERR("Unable to get file key\n");
1063 return ERROR_FUNCTION_FAILED;
1066 file = get_loaded_file( package, file_key );
1069 ERR("Original file unknown %s\n", debugstr_w(file_key));
1070 return ERROR_SUCCESS;
1073 dest = get_duplicate_filename( package, row, file_key, file->TargetPath );
1076 WARN("Unable to get duplicate filename\n");
1077 return ERROR_SUCCESS;
1080 TRACE("Removing duplicate %s of %s\n", debugstr_w(dest), debugstr_w(file->TargetPath));
1082 if (!DeleteFileW( dest ))
1084 WARN("Failed to delete duplicate file %s (%u)\n", debugstr_w(dest), GetLastError());
1087 uirow = MSI_CreateRecord( 9 );
1088 MSI_RecordSetStringW( uirow, 1, MSI_RecordGetString( row, 1 ) );
1089 MSI_RecordSetStringW( uirow, 9, MSI_RecordGetString( row, 5 ) );
1090 ui_actiondata( package, szRemoveDuplicateFiles, uirow );
1091 msiobj_release( &uirow->hdr );
1094 return ERROR_SUCCESS;
1097 UINT ACTION_RemoveDuplicateFiles( MSIPACKAGE *package )
1101 static const WCHAR query[] =
1102 {'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
1103 '`','D','u','p','l','i','c','a','t','e','F','i','l','e','`',0};
1105 rc = MSI_DatabaseOpenViewW( package->db, query, &view );
1106 if (rc != ERROR_SUCCESS)
1107 return ERROR_SUCCESS;
1109 rc = MSI_IterateRecords( view, NULL, ITERATE_RemoveDuplicateFiles, package );
1110 msiobj_release( &view->hdr );
1115 static BOOL verify_comp_for_removal(MSICOMPONENT *comp, UINT install_mode)
1117 INSTALLSTATE request = comp->ActionRequest;
1120 if (request != INSTALLSTATE_SOURCE &&
1121 comp->Attributes & msidbComponentAttributesSourceOnly &&
1122 (install_mode == msidbRemoveFileInstallModeOnRemove ||
1123 install_mode == msidbRemoveFileInstallModeOnBoth)) return TRUE;
1127 case INSTALLSTATE_LOCAL:
1128 case INSTALLSTATE_SOURCE:
1129 if (install_mode == msidbRemoveFileInstallModeOnInstall ||
1130 install_mode == msidbRemoveFileInstallModeOnBoth) return TRUE;
1132 case INSTALLSTATE_ABSENT:
1133 if (install_mode == msidbRemoveFileInstallModeOnRemove ||
1134 install_mode == msidbRemoveFileInstallModeOnBoth) return TRUE;
1141 static UINT ITERATE_RemoveFiles(MSIRECORD *row, LPVOID param)
1143 MSIPACKAGE *package = param;
1146 LPCWSTR component, dirprop;
1148 LPWSTR dir = NULL, path = NULL, filename = NULL;
1150 UINT ret = ERROR_SUCCESS;
1152 component = MSI_RecordGetString(row, 2);
1153 dirprop = MSI_RecordGetString(row, 4);
1154 install_mode = MSI_RecordGetInteger(row, 5);
1156 comp = get_loaded_component(package, component);
1159 TRACE("component is disabled\n");
1160 return ERROR_SUCCESS;
1163 if (!verify_comp_for_removal(comp, install_mode))
1165 TRACE("Skipping removal due to install mode\n");
1166 comp->Action = comp->Installed;
1167 return ERROR_SUCCESS;
1170 if (comp->Attributes & msidbComponentAttributesPermanent)
1172 TRACE("permanent component, not removing file\n");
1173 return ERROR_SUCCESS;
1176 dir = msi_dup_property(package->db, dirprop);
1178 return ERROR_OUTOFMEMORY;
1181 if ((filename = strdupW( MSI_RecordGetString(row, 3) )))
1183 reduce_to_longfilename( filename );
1184 size = lstrlenW( filename );
1186 size += lstrlenW(dir) + 2;
1187 path = msi_alloc(size * sizeof(WCHAR));
1190 ret = ERROR_OUTOFMEMORY;
1196 lstrcpyW(path, dir);
1197 PathAddBackslashW(path);
1198 lstrcatW(path, filename);
1200 TRACE("Deleting misc file: %s\n", debugstr_w(path));
1205 TRACE("Removing misc directory: %s\n", debugstr_w(dir));
1206 RemoveDirectoryW(dir);
1210 uirow = MSI_CreateRecord( 9 );
1211 MSI_RecordSetStringW( uirow, 1, MSI_RecordGetString(row, 1) );
1212 MSI_RecordSetStringW( uirow, 9, dir );
1213 ui_actiondata( package, szRemoveFiles, uirow );
1214 msiobj_release( &uirow->hdr );
1222 static BOOL has_persistent_dir( MSIPACKAGE *package, MSICOMPONENT *comp )
1225 UINT r = ERROR_FUNCTION_FAILED;
1227 static const WCHAR query[] = {
1228 'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
1229 '`','C','r','e','a','t','e','F','o','l','d','e','r','`',' ','W','H','E','R','E',' ',
1230 '`','C','o','m','p','o','n','e','n','t','_','`',' ','=','\'','%','s','\'',' ','A','N','D',' ',
1231 '`','D','i','r','e','c','t','o','r','y','_','`',' ','=','\'','%','s','\'',0};
1233 if (!MSI_OpenQuery( package->db, &view, query, comp->Component, comp->Directory ))
1235 if (!MSI_ViewExecute( view, NULL ))
1238 if (!(r = MSI_ViewFetch( view, &rec )))
1240 TRACE("directory %s is persistent\n", debugstr_w(comp->Directory));
1241 msiobj_release( &rec->hdr );
1244 msiobj_release( &view->hdr );
1246 return (r == ERROR_SUCCESS);
1249 UINT ACTION_RemoveFiles( MSIPACKAGE *package )
1255 static const WCHAR query[] = {
1256 'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
1257 '`','R','e','m','o','v','e','F','i','l','e','`',0};
1259 r = MSI_DatabaseOpenViewW(package->db, query, &view);
1260 if (r == ERROR_SUCCESS)
1262 MSI_IterateRecords(view, NULL, ITERATE_RemoveFiles, package);
1263 msiobj_release(&view->hdr);
1266 LIST_FOR_EACH_ENTRY( file, &package->files, MSIFILE, entry )
1270 VS_FIXEDFILEINFO *ver;
1272 if ( file->state == msifs_installed )
1273 ERR("removing installed file %s\n", debugstr_w(file->TargetPath));
1275 if ( file->Component->ActionRequest != INSTALLSTATE_ABSENT ||
1276 file->Component->Installed == INSTALLSTATE_SOURCE )
1279 if (!file->Component->Enabled)
1281 TRACE("component is disabled\n");
1285 if (file->Component->Attributes & msidbComponentAttributesPermanent)
1287 TRACE("permanent component, not removing file\n");
1293 ver = msi_get_disk_file_version( file->TargetPath );
1294 if (ver && msi_compare_file_versions( ver, file->Version ) > 0)
1296 TRACE("newer version detected, not removing file\n");
1303 TRACE("removing %s\n", debugstr_w(file->File) );
1305 SetFileAttributesW( file->TargetPath, FILE_ATTRIBUTE_NORMAL );
1306 if (!DeleteFileW( file->TargetPath ))
1308 WARN("failed to delete %s (%u)\n", debugstr_w(file->TargetPath), GetLastError());
1310 else if (!has_persistent_dir( package, file->Component ))
1312 if ((dir = strdupW( file->TargetPath )))
1314 if ((p = strrchrW( dir, '\\' ))) *p = 0;
1315 RemoveDirectoryW( dir );
1319 file->state = msifs_missing;
1321 uirow = MSI_CreateRecord( 9 );
1322 MSI_RecordSetStringW( uirow, 1, file->FileName );
1323 MSI_RecordSetStringW( uirow, 9, file->Component->Directory );
1324 ui_actiondata( package, szRemoveFiles, uirow );
1325 msiobj_release( &uirow->hdr );
1326 /* FIXME: call ui_progress here? */
1329 return ERROR_SUCCESS;