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 void msi_file_update_ui( MSIPACKAGE *package, MSIFILE *f, const WCHAR *action )
54 uirow = MSI_CreateRecord( 9 );
55 MSI_RecordSetStringW( uirow, 1, f->FileName );
56 MSI_RecordSetStringW( uirow, 9, f->Component->Directory );
57 MSI_RecordSetInteger( uirow, 6, f->FileSize );
58 ui_actiondata( package, action, uirow );
59 msiobj_release( &uirow->hdr );
60 ui_progress( package, 2, f->FileSize, 0, 0 );
63 static void schedule_install_files(MSIPACKAGE *package)
67 LIST_FOR_EACH_ENTRY(file, &package->files, MSIFILE, entry)
69 MSICOMPONENT *comp = file->Component;
71 if (comp->ActionRequest != INSTALLSTATE_LOCAL || !comp->Enabled ||
72 (comp->assembly && comp->assembly->installed))
74 TRACE("File %s is not scheduled for install\n", debugstr_w(file->File));
75 file->state = msifs_skipped;
78 comp->Action = INSTALLSTATE_LOCAL;
79 ui_progress( package, 2, file->FileSize, 0, 0 );
81 if (file->state == msifs_overwrite &&
82 (comp->Attributes & msidbComponentAttributesNeverOverwrite))
84 TRACE("not overwriting %s\n", debugstr_w(file->TargetPath));
85 file->state = msifs_skipped;
90 static UINT copy_file(MSIFILE *file, LPWSTR source)
94 ret = CopyFileW(source, file->TargetPath, FALSE);
96 return GetLastError();
98 SetFileAttributesW(file->TargetPath, FILE_ATTRIBUTE_NORMAL);
100 file->state = msifs_installed;
101 return ERROR_SUCCESS;
104 static UINT copy_install_file(MSIPACKAGE *package, MSIFILE *file, LPWSTR source)
108 TRACE("Copying %s to %s\n", debugstr_w(source), debugstr_w(file->TargetPath));
110 gle = copy_file(file, source);
111 if (gle == ERROR_SUCCESS)
114 if (gle == ERROR_ALREADY_EXISTS && file->state == msifs_overwrite)
116 TRACE("overwriting existing file\n");
117 return ERROR_SUCCESS;
119 else if (gle == ERROR_ACCESS_DENIED)
121 SetFileAttributesW(file->TargetPath, FILE_ATTRIBUTE_NORMAL);
123 gle = copy_file(file, source);
124 TRACE("Overwriting existing file: %d\n", gle);
126 if (gle == ERROR_SHARING_VIOLATION || gle == ERROR_USER_MAPPED_FILE)
128 WCHAR tmpfileW[MAX_PATH], *pathW, *p;
131 TRACE("file in use, scheduling rename operation\n");
133 GetTempFileNameW(szBackSlash, szMsi, 0, tmpfileW);
134 len = strlenW(file->TargetPath) + strlenW(tmpfileW) + 1;
135 if (!(pathW = msi_alloc(len * sizeof(WCHAR))))
136 return ERROR_OUTOFMEMORY;
138 strcpyW(pathW, file->TargetPath);
139 if ((p = strrchrW(pathW, '\\'))) *p = 0;
140 strcatW(pathW, tmpfileW);
142 if (CopyFileW(source, pathW, FALSE) &&
143 MoveFileExW(file->TargetPath, NULL, MOVEFILE_DELAY_UNTIL_REBOOT) &&
144 MoveFileExW(pathW, file->TargetPath, MOVEFILE_DELAY_UNTIL_REBOOT))
146 file->state = msifs_installed;
147 package->need_reboot = 1;
152 gle = GetLastError();
153 WARN("failed to schedule rename operation: %d)\n", gle);
161 static UINT msi_create_directory( MSIPACKAGE *package, const WCHAR *dir )
166 install_path = resolve_target_folder( package, dir, FALSE, TRUE, &folder );
168 return ERROR_FUNCTION_FAILED;
170 if (folder->State == 0)
172 create_full_pathW( install_path );
175 msi_free( install_path );
176 return ERROR_SUCCESS;
179 static BOOL installfiles_cb(MSIPACKAGE *package, LPCWSTR file, DWORD action,
180 LPWSTR *path, DWORD *attrs, PVOID user)
182 static MSIFILE *f = NULL;
183 UINT_PTR disk_id = (UINT_PTR)user;
185 if (action == MSICABEXTRACT_BEGINEXTRACT)
187 f = get_loaded_file(package, file);
190 TRACE("unknown file in cabinet (%s)\n", debugstr_w(file));
194 if (f->disk_id != disk_id || (f->state != msifs_missing && f->state != msifs_overwrite))
197 msi_file_update_ui(package, f, szInstallFiles);
198 if (!f->Component->assembly || f->Component->assembly->application)
200 msi_create_directory(package, f->Component->Directory);
202 *path = strdupW(f->TargetPath);
203 *attrs = f->Attributes;
205 else if (action == MSICABEXTRACT_FILEEXTRACTED)
207 f->state = msifs_installed;
215 * ACTION_InstallFiles()
217 * For efficiency, this is done in two passes:
218 * 1) Correct all the TargetPaths and determine what files are to be installed.
219 * 2) Extract Cabinets and copy files.
221 UINT ACTION_InstallFiles(MSIPACKAGE *package)
225 UINT rc = ERROR_SUCCESS;
228 /* increment progress bar each time action data is sent */
229 ui_progress(package,1,1,0,0);
231 schedule_install_files(package);
233 mi = msi_alloc_zero( sizeof(MSIMEDIAINFO) );
235 LIST_FOR_EACH_ENTRY( file, &package->files, MSIFILE, entry )
237 if (file->state != msifs_missing && !mi->is_continuous && file->state != msifs_overwrite)
240 if (file->Sequence > mi->last_sequence || mi->is_continuous ||
241 (file->IsCompressed && !mi->is_extracted))
245 rc = ready_media(package, file, mi);
246 if (rc != ERROR_SUCCESS)
248 ERR("Failed to ready media for %s\n", debugstr_w(file->File));
253 data.package = package;
254 data.cb = installfiles_cb;
255 data.user = (PVOID)(UINT_PTR)mi->disk_id;
257 if (file->IsCompressed &&
258 !msi_cabextract(package, mi, &data))
260 ERR("Failed to extract cabinet: %s\n", debugstr_w(mi->cabinet));
261 rc = ERROR_INSTALL_FAILURE;
266 if (!file->IsCompressed)
268 LPWSTR source = resolve_file_source(package, file);
270 TRACE("copying %s to %s\n", debugstr_w(source), debugstr_w(file->TargetPath));
272 msi_file_update_ui(package, file, szInstallFiles);
273 if (!file->Component->assembly || file->Component->assembly->application)
275 msi_create_directory(package, file->Component->Directory);
277 rc = copy_install_file(package, file, source);
278 if (rc != ERROR_SUCCESS)
280 ERR("Failed to copy %s to %s (%d)\n", debugstr_w(source),
281 debugstr_w(file->TargetPath), rc);
282 rc = ERROR_INSTALL_FAILURE;
288 else if (file->state != msifs_installed)
290 ERR("compressed file wasn't installed (%s)\n", debugstr_w(file->TargetPath));
291 rc = ERROR_INSTALL_FAILURE;
295 LIST_FOR_EACH_ENTRY( comp, &package->components, MSICOMPONENT, entry )
297 if (comp->ActionRequest == INSTALLSTATE_LOCAL && comp->Enabled &&
298 comp->assembly && !comp->assembly->installed)
300 rc = install_assembly( package, comp );
301 if (rc != ERROR_SUCCESS)
303 ERR("Failed to install assembly\n");
304 rc = ERROR_INSTALL_FAILURE;
311 msi_free_media_info(mi);
315 #define is_dot_dir(x) ((x[0] == '.') && ((x[1] == 0) || ((x[1] == '.') && (x[2] == 0))))
326 static BOOL msi_move_file(LPCWSTR source, LPCWSTR dest, int options)
330 if (GetFileAttributesW(source) == FILE_ATTRIBUTE_DIRECTORY ||
331 GetFileAttributesW(dest) == FILE_ATTRIBUTE_DIRECTORY)
333 WARN("Source or dest is directory, not moving\n");
337 if (options == msidbMoveFileOptionsMove)
339 TRACE("moving %s -> %s\n", debugstr_w(source), debugstr_w(dest));
340 ret = MoveFileExW(source, dest, MOVEFILE_REPLACE_EXISTING);
343 WARN("MoveFile failed: %d\n", GetLastError());
349 TRACE("copying %s -> %s\n", debugstr_w(source), debugstr_w(dest));
350 ret = CopyFileW(source, dest, FALSE);
353 WARN("CopyFile failed: %d\n", GetLastError());
361 static LPWSTR wildcard_to_file(LPWSTR wildcard, LPWSTR filename)
364 DWORD dirlen, pathlen;
366 ptr = strrchrW(wildcard, '\\');
367 dirlen = ptr - wildcard + 1;
369 pathlen = dirlen + lstrlenW(filename) + 1;
370 path = msi_alloc(pathlen * sizeof(WCHAR));
372 lstrcpynW(path, wildcard, dirlen + 1);
373 lstrcatW(path, filename);
378 static void free_file_entry(FILE_LIST *file)
380 msi_free(file->source);
381 msi_free(file->dest);
385 static void free_list(FILE_LIST *list)
387 while (!list_empty(&list->entry))
389 FILE_LIST *file = LIST_ENTRY(list_head(&list->entry), FILE_LIST, entry);
391 list_remove(&file->entry);
392 free_file_entry(file);
396 static BOOL add_wildcard(FILE_LIST *files, LPWSTR source, LPWSTR dest)
398 FILE_LIST *new, *file;
399 LPWSTR ptr, filename;
402 new = msi_alloc_zero(sizeof(FILE_LIST));
406 new->source = strdupW(source);
407 ptr = strrchrW(dest, '\\') + 1;
408 filename = strrchrW(new->source, '\\') + 1;
410 new->sourcename = filename;
415 new->destname = new->sourcename;
417 size = (ptr - dest) + lstrlenW(filename) + 1;
418 new->dest = msi_alloc(size * sizeof(WCHAR));
421 free_file_entry(new);
425 lstrcpynW(new->dest, dest, ptr - dest + 1);
426 lstrcatW(new->dest, filename);
428 if (list_empty(&files->entry))
430 list_add_head(&files->entry, &new->entry);
434 LIST_FOR_EACH_ENTRY(file, &files->entry, FILE_LIST, entry)
436 if (strcmpW( source, file->source ) < 0)
438 list_add_before(&file->entry, &new->entry);
443 list_add_after(&file->entry, &new->entry);
447 static BOOL move_files_wildcard(LPWSTR source, LPWSTR dest, int options)
449 WIN32_FIND_DATAW wfd;
453 FILE_LIST files, *file;
456 hfile = FindFirstFileW(source, &wfd);
457 if (hfile == INVALID_HANDLE_VALUE) return FALSE;
459 list_init(&files.entry);
461 for (res = TRUE; res; res = FindNextFileW(hfile, &wfd))
463 if (is_dot_dir(wfd.cFileName)) continue;
465 path = wildcard_to_file(source, wfd.cFileName);
472 add_wildcard(&files, path, dest);
476 /* no files match the wildcard */
477 if (list_empty(&files.entry))
480 /* only the first wildcard match gets renamed to dest */
481 file = LIST_ENTRY(list_head(&files.entry), FILE_LIST, entry);
482 size = (strrchrW(file->dest, '\\') - file->dest) + lstrlenW(file->destname) + 2;
483 file->dest = msi_realloc(file->dest, size * sizeof(WCHAR));
490 /* file->dest may be shorter after the reallocation, so add a NULL
491 * terminator. This is needed for the call to strrchrW, as there will no
492 * longer be a NULL terminator within the bounds of the allocation in this case.
494 file->dest[size - 1] = '\0';
495 lstrcpyW(strrchrW(file->dest, '\\') + 1, file->destname);
497 while (!list_empty(&files.entry))
499 file = LIST_ENTRY(list_head(&files.entry), FILE_LIST, entry);
501 msi_move_file(file->source, file->dest, options);
503 list_remove(&file->entry);
504 free_file_entry(file);
515 static UINT ITERATE_MoveFiles( MSIRECORD *rec, LPVOID param )
517 MSIPACKAGE *package = param;
520 LPCWSTR sourcename, component;
521 LPWSTR sourcedir, destname = NULL, destdir = NULL, source = NULL, dest = NULL;
526 component = MSI_RecordGetString(rec, 2);
527 comp = get_loaded_component(package, component);
529 return ERROR_SUCCESS;
533 TRACE("component is disabled\n");
534 return ERROR_SUCCESS;
537 if (comp->ActionRequest != INSTALLSTATE_LOCAL && comp->ActionRequest != INSTALLSTATE_SOURCE)
539 TRACE("Component not scheduled for installation: %s\n", debugstr_w(component));
540 comp->Action = comp->Installed;
541 return ERROR_SUCCESS;
543 comp->Action = comp->ActionRequest;
545 sourcename = MSI_RecordGetString(rec, 3);
546 options = MSI_RecordGetInteger(rec, 7);
548 sourcedir = msi_dup_property(package->db, MSI_RecordGetString(rec, 5));
552 destdir = msi_dup_property(package->db, MSI_RecordGetString(rec, 6));
558 if (GetFileAttributesW(sourcedir) == INVALID_FILE_ATTRIBUTES)
561 source = strdupW(sourcedir);
567 size = lstrlenW(sourcedir) + lstrlenW(sourcename) + 2;
568 source = msi_alloc(size * sizeof(WCHAR));
572 lstrcpyW(source, sourcedir);
573 if (source[lstrlenW(source) - 1] != '\\')
574 lstrcatW(source, szBackSlash);
575 lstrcatW(source, sourcename);
578 wildcards = strchrW(source, '*') || strchrW(source, '?');
580 if (MSI_RecordIsNull(rec, 4))
584 destname = strdupW(sourcename);
591 destname = strdupW(MSI_RecordGetString(rec, 4));
593 reduce_to_longfilename(destname);
598 size = lstrlenW(destname);
600 size += lstrlenW(destdir) + 2;
601 dest = msi_alloc(size * sizeof(WCHAR));
605 lstrcpyW(dest, destdir);
606 if (dest[lstrlenW(dest) - 1] != '\\')
607 lstrcatW(dest, szBackSlash);
610 lstrcatW(dest, destname);
612 if (GetFileAttributesW(destdir) == INVALID_FILE_ATTRIBUTES)
614 ret = CreateDirectoryW(destdir, NULL);
617 WARN("CreateDirectory failed: %d\n", GetLastError());
623 msi_move_file(source, dest, options);
625 move_files_wildcard(source, dest, options);
628 uirow = MSI_CreateRecord( 9 );
629 MSI_RecordSetStringW( uirow, 1, MSI_RecordGetString(rec, 1) );
630 MSI_RecordSetInteger( uirow, 6, 1 ); /* FIXME */
631 MSI_RecordSetStringW( uirow, 9, destdir );
632 ui_actiondata( package, szMoveFiles, uirow );
633 msiobj_release( &uirow->hdr );
641 return ERROR_SUCCESS;
644 UINT ACTION_MoveFiles( MSIPACKAGE *package )
649 static const WCHAR ExecSeqQuery[] =
650 {'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
651 '`','M','o','v','e','F','i','l','e','`',0};
653 rc = MSI_DatabaseOpenViewW(package->db, ExecSeqQuery, &view);
654 if (rc != ERROR_SUCCESS)
655 return ERROR_SUCCESS;
657 rc = MSI_IterateRecords(view, NULL, ITERATE_MoveFiles, package);
658 msiobj_release(&view->hdr);
663 static WCHAR *get_duplicate_filename( MSIPACKAGE *package, MSIRECORD *row, const WCHAR *file_key, const WCHAR *src )
666 WCHAR *dst_name, *dst_path, *dst;
668 if (MSI_RecordIsNull( row, 4 ))
670 len = strlenW( src ) + 1;
671 if (!(dst_name = msi_alloc( len * sizeof(WCHAR)))) return NULL;
672 strcpyW( dst_name, strrchrW( src, '\\' ) + 1 );
676 MSI_RecordGetStringW( row, 4, NULL, &len );
677 if (!(dst_name = msi_alloc( ++len * sizeof(WCHAR) ))) return NULL;
678 MSI_RecordGetStringW( row, 4, dst_name, &len );
679 reduce_to_longfilename( dst_name );
682 if (MSI_RecordIsNull( row, 5 ))
685 dst_path = strdupW( src );
686 p = strrchrW( dst_path, '\\' );
691 const WCHAR *dst_key = MSI_RecordGetString( row, 5 );
693 dst_path = resolve_target_folder( package, dst_key, FALSE, TRUE, NULL );
697 dst_path = msi_dup_property( package->db, dst_key );
700 FIXME("Unable to get destination folder, try AppSearch properties\n");
701 msi_free( dst_name );
707 dst = build_directory_name( 2, dst_path, dst_name );
708 create_full_pathW( dst_path );
710 msi_free( dst_name );
711 msi_free( dst_path );
715 static UINT ITERATE_DuplicateFiles(MSIRECORD *row, LPVOID param)
717 MSIPACKAGE *package = param;
719 LPCWSTR file_key, component;
724 component = MSI_RecordGetString(row,2);
725 comp = get_loaded_component(package,component);
727 return ERROR_SUCCESS;
731 TRACE("component is disabled\n");
732 return ERROR_SUCCESS;
735 if (comp->ActionRequest != INSTALLSTATE_LOCAL)
737 TRACE("Component not scheduled for installation %s\n", debugstr_w(component));
738 comp->Action = comp->Installed;
739 return ERROR_SUCCESS;
741 comp->Action = INSTALLSTATE_LOCAL;
743 file_key = MSI_RecordGetString(row,3);
746 ERR("Unable to get file key\n");
747 return ERROR_FUNCTION_FAILED;
750 file = get_loaded_file( package, file_key );
753 ERR("Original file unknown %s\n", debugstr_w(file_key));
754 return ERROR_SUCCESS;
757 dest = get_duplicate_filename( package, row, file_key, file->TargetPath );
760 WARN("Unable to get duplicate filename\n");
761 return ERROR_SUCCESS;
764 TRACE("Duplicating file %s to %s\n", debugstr_w(file->TargetPath), debugstr_w(dest));
766 if (!CopyFileW( file->TargetPath, dest, TRUE ))
768 WARN("Failed to copy file %s -> %s (%u)\n",
769 debugstr_w(file->TargetPath), debugstr_w(dest), GetLastError());
772 FIXME("We should track these duplicate files as well\n");
774 uirow = MSI_CreateRecord( 9 );
775 MSI_RecordSetStringW( uirow, 1, MSI_RecordGetString( row, 1 ) );
776 MSI_RecordSetInteger( uirow, 6, file->FileSize );
777 MSI_RecordSetStringW( uirow, 9, MSI_RecordGetString( row, 5 ) );
778 ui_actiondata( package, szDuplicateFiles, uirow );
779 msiobj_release( &uirow->hdr );
782 return ERROR_SUCCESS;
785 UINT ACTION_DuplicateFiles(MSIPACKAGE *package)
789 static const WCHAR ExecSeqQuery[] =
790 {'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
791 '`','D','u','p','l','i','c','a','t','e','F','i','l','e','`',0};
793 rc = MSI_DatabaseOpenViewW(package->db, ExecSeqQuery, &view);
794 if (rc != ERROR_SUCCESS)
795 return ERROR_SUCCESS;
797 rc = MSI_IterateRecords(view, NULL, ITERATE_DuplicateFiles, package);
798 msiobj_release(&view->hdr);
803 static UINT ITERATE_RemoveDuplicateFiles( MSIRECORD *row, LPVOID param )
805 MSIPACKAGE *package = param;
807 LPCWSTR file_key, component;
812 component = MSI_RecordGetString( row, 2 );
813 comp = get_loaded_component( package, component );
815 return ERROR_SUCCESS;
819 TRACE("component is disabled\n");
820 return ERROR_SUCCESS;
823 if (comp->ActionRequest != INSTALLSTATE_ABSENT)
825 TRACE("Component not scheduled for removal %s\n", debugstr_w(component));
826 comp->Action = comp->Installed;
827 return ERROR_SUCCESS;
829 comp->Action = INSTALLSTATE_ABSENT;
831 file_key = MSI_RecordGetString( row, 3 );
834 ERR("Unable to get file key\n");
835 return ERROR_FUNCTION_FAILED;
838 file = get_loaded_file( package, file_key );
841 ERR("Original file unknown %s\n", debugstr_w(file_key));
842 return ERROR_SUCCESS;
845 dest = get_duplicate_filename( package, row, file_key, file->TargetPath );
848 WARN("Unable to get duplicate filename\n");
849 return ERROR_SUCCESS;
852 TRACE("Removing duplicate %s of %s\n", debugstr_w(dest), debugstr_w(file->TargetPath));
854 if (!DeleteFileW( dest ))
856 WARN("Failed to delete duplicate file %s (%u)\n", debugstr_w(dest), GetLastError());
859 uirow = MSI_CreateRecord( 9 );
860 MSI_RecordSetStringW( uirow, 1, MSI_RecordGetString( row, 1 ) );
861 MSI_RecordSetStringW( uirow, 9, MSI_RecordGetString( row, 5 ) );
862 ui_actiondata( package, szRemoveDuplicateFiles, uirow );
863 msiobj_release( &uirow->hdr );
866 return ERROR_SUCCESS;
869 UINT ACTION_RemoveDuplicateFiles( MSIPACKAGE *package )
873 static const WCHAR query[] =
874 {'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
875 '`','D','u','p','l','i','c','a','t','e','F','i','l','e','`',0};
877 rc = MSI_DatabaseOpenViewW( package->db, query, &view );
878 if (rc != ERROR_SUCCESS)
879 return ERROR_SUCCESS;
881 rc = MSI_IterateRecords( view, NULL, ITERATE_RemoveDuplicateFiles, package );
882 msiobj_release( &view->hdr );
887 static BOOL verify_comp_for_removal(MSICOMPONENT *comp, UINT install_mode)
889 INSTALLSTATE request = comp->ActionRequest;
892 if (request != INSTALLSTATE_SOURCE &&
893 comp->Attributes & msidbComponentAttributesSourceOnly &&
894 (install_mode == msidbRemoveFileInstallModeOnRemove ||
895 install_mode == msidbRemoveFileInstallModeOnBoth)) return TRUE;
899 case INSTALLSTATE_LOCAL:
900 case INSTALLSTATE_SOURCE:
901 if (install_mode == msidbRemoveFileInstallModeOnInstall ||
902 install_mode == msidbRemoveFileInstallModeOnBoth) return TRUE;
904 case INSTALLSTATE_ABSENT:
905 if (install_mode == msidbRemoveFileInstallModeOnRemove ||
906 install_mode == msidbRemoveFileInstallModeOnBoth) return TRUE;
913 static UINT ITERATE_RemoveFiles(MSIRECORD *row, LPVOID param)
915 MSIPACKAGE *package = param;
918 LPCWSTR component, filename, dirprop;
920 LPWSTR dir = NULL, path = NULL;
922 UINT ret = ERROR_SUCCESS;
924 component = MSI_RecordGetString(row, 2);
925 filename = MSI_RecordGetString(row, 3);
926 dirprop = MSI_RecordGetString(row, 4);
927 install_mode = MSI_RecordGetInteger(row, 5);
929 comp = get_loaded_component(package, component);
932 TRACE("component is disabled\n");
933 return ERROR_SUCCESS;
936 if (!verify_comp_for_removal(comp, install_mode))
938 TRACE("Skipping removal due to install mode\n");
939 comp->Action = comp->Installed;
940 return ERROR_SUCCESS;
943 if (comp->Attributes & msidbComponentAttributesPermanent)
945 TRACE("permanent component, not removing file\n");
946 return ERROR_SUCCESS;
949 dir = msi_dup_property(package->db, dirprop);
951 return ERROR_OUTOFMEMORY;
953 size = (filename != NULL) ? lstrlenW(filename) : 0;
954 size += lstrlenW(dir) + 2;
955 path = msi_alloc(size * sizeof(WCHAR));
958 ret = ERROR_OUTOFMEMORY;
965 PathAddBackslashW(path);
966 lstrcatW(path, filename);
968 TRACE("Deleting misc file: %s\n", debugstr_w(path));
973 TRACE("Removing misc directory: %s\n", debugstr_w(dir));
974 RemoveDirectoryW(dir);
978 uirow = MSI_CreateRecord( 9 );
979 MSI_RecordSetStringW( uirow, 1, MSI_RecordGetString(row, 1) );
980 MSI_RecordSetStringW( uirow, 9, dir );
981 ui_actiondata( package, szRemoveFiles, uirow );
982 msiobj_release( &uirow->hdr );
989 static BOOL has_persistent_dir( MSIPACKAGE *package, MSICOMPONENT *comp )
992 UINT r = ERROR_FUNCTION_FAILED;
994 static const WCHAR query[] = {
995 'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
996 '`','C','r','e','a','t','e','F','o','l','d','e','r','`',' ','W','H','E','R','E',' ',
997 '`','C','o','m','p','o','n','e','n','t','_','`',' ','=','\'','%','s','\'',' ','A','N','D',' ',
998 '`','D','i','r','e','c','t','o','r','y','_','`',' ','=','\'','%','s','\'',0};
1000 if (!MSI_OpenQuery( package->db, &view, query, comp->Component, comp->Directory ))
1002 if (!MSI_ViewExecute( view, NULL ))
1005 if (!(r = MSI_ViewFetch( view, &rec )))
1007 TRACE("directory %s is persistent\n", debugstr_w(comp->Directory));
1008 msiobj_release( &rec->hdr );
1011 msiobj_release( &view->hdr );
1013 return (r == ERROR_SUCCESS);
1016 UINT ACTION_RemoveFiles( MSIPACKAGE *package )
1022 static const WCHAR query[] = {
1023 'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
1024 '`','R','e','m','o','v','e','F','i','l','e','`',0};
1026 r = MSI_DatabaseOpenViewW(package->db, query, &view);
1027 if (r == ERROR_SUCCESS)
1029 MSI_IterateRecords(view, NULL, ITERATE_RemoveFiles, package);
1030 msiobj_release(&view->hdr);
1033 LIST_FOR_EACH_ENTRY( file, &package->files, MSIFILE, entry )
1037 VS_FIXEDFILEINFO *ver;
1039 if ( file->state == msifs_installed )
1040 ERR("removing installed file %s\n", debugstr_w(file->TargetPath));
1042 if ( file->Component->ActionRequest != INSTALLSTATE_ABSENT ||
1043 file->Component->Installed == INSTALLSTATE_SOURCE )
1046 if (!file->Component->Enabled)
1048 TRACE("component is disabled\n");
1052 if (file->Component->Attributes & msidbComponentAttributesPermanent)
1054 TRACE("permanent component, not removing file\n");
1060 ver = msi_get_disk_file_version( file->TargetPath );
1061 if (ver && msi_compare_file_versions( ver, file->Version ) > 0)
1063 TRACE("newer version detected, not removing file\n");
1070 TRACE("removing %s\n", debugstr_w(file->File) );
1072 SetFileAttributesW( file->TargetPath, FILE_ATTRIBUTE_NORMAL );
1073 if (!DeleteFileW( file->TargetPath ))
1075 WARN("failed to delete %s (%u)\n", debugstr_w(file->TargetPath), GetLastError());
1077 else if (!has_persistent_dir( package, file->Component ))
1079 if ((dir = strdupW( file->TargetPath )))
1081 if ((p = strrchrW( dir, '\\' ))) *p = 0;
1082 RemoveDirectoryW( dir );
1086 file->state = msifs_missing;
1088 uirow = MSI_CreateRecord( 9 );
1089 MSI_RecordSetStringW( uirow, 1, file->FileName );
1090 MSI_RecordSetStringW( uirow, 9, file->Component->Directory );
1091 ui_actiondata( package, szRemoveFiles, uirow );
1092 msiobj_release( &uirow->hdr );
1093 /* FIXME: call ui_progress here? */
1096 return ERROR_SUCCESS;