msi: Reset file attributes before removing a file.
[wine.git] / dlls / msi / files.c
bloba044ec77357d0c6a9fbe2b0515e7e41d39b244eb
1 /*
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
25 * InstallFiles
26 * DuplicateFiles
27 * MoveFiles
28 * PatchFiles (TODO)
29 * RemoveDuplicateFiles
30 * RemoveFiles
33 #include <stdarg.h>
35 #include "windef.h"
36 #include "winbase.h"
37 #include "winerror.h"
38 #include "wine/debug.h"
39 #include "fdi.h"
40 #include "msi.h"
41 #include "msidefs.h"
42 #include "msipriv.h"
43 #include "winuser.h"
44 #include "winreg.h"
45 #include "shlwapi.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 )
52 MSIRECORD *uirow;
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)
65 MSIFILE *file;
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;
76 continue;
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)
92 BOOL ret;
94 ret = CopyFileW(source, file->TargetPath, FALSE);
95 if (!ret)
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)
106 UINT gle;
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)
112 return gle;
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;
129 DWORD len;
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;
148 gle = ERROR_SUCCESS;
150 else
152 gle = GetLastError();
153 WARN("failed to schedule rename operation: %d)\n", gle);
155 msi_free(pathW);
158 return gle;
161 static UINT msi_create_directory( MSIPACKAGE *package, const WCHAR *dir )
163 MSIFOLDER *folder;
164 WCHAR *install_path;
166 install_path = resolve_folder( package, dir, FALSE, FALSE, TRUE, &folder );
167 if (!install_path)
168 return ERROR_FUNCTION_FAILED;
170 if (folder->State == 0)
172 create_full_pathW( install_path );
173 folder->State = 2;
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);
188 if (!f)
190 TRACE("unknown file in cabinet (%s)\n", debugstr_w(file));
191 return FALSE;
194 if (f->disk_id != disk_id || (f->state != msifs_missing && f->state != msifs_overwrite))
195 return FALSE;
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;
208 f = NULL;
211 return TRUE;
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)
223 MSIMEDIAINFO *mi;
224 MSICOMPONENT *comp;
225 UINT rc = ERROR_SUCCESS;
226 MSIFILE *file;
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)
238 continue;
240 if (file->Sequence > mi->last_sequence || mi->is_continuous ||
241 (file->IsCompressed && !mi->is_extracted))
243 MSICABDATA data;
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));
249 goto done;
252 data.mi = mi;
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;
262 goto done;
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;
283 msi_free(source);
284 goto done;
286 msi_free(source);
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;
292 goto done;
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;
305 break;
310 done:
311 msi_free_media_info(mi);
312 return rc;
315 #define is_dot_dir(x) ((x[0] == '.') && ((x[1] == 0) || ((x[1] == '.') && (x[2] == 0))))
317 typedef struct
319 struct list entry;
320 LPWSTR sourcename;
321 LPWSTR destname;
322 LPWSTR source;
323 LPWSTR dest;
324 } FILE_LIST;
326 static BOOL msi_move_file(LPCWSTR source, LPCWSTR dest, int options)
328 BOOL ret;
330 if (GetFileAttributesW(source) == FILE_ATTRIBUTE_DIRECTORY ||
331 GetFileAttributesW(dest) == FILE_ATTRIBUTE_DIRECTORY)
333 WARN("Source or dest is directory, not moving\n");
334 return FALSE;
337 if (options == msidbMoveFileOptionsMove)
339 TRACE("moving %s -> %s\n", debugstr_w(source), debugstr_w(dest));
340 ret = MoveFileExW(source, dest, MOVEFILE_REPLACE_EXISTING);
341 if (!ret)
343 WARN("MoveFile failed: %d\n", GetLastError());
344 return FALSE;
347 else
349 TRACE("copying %s -> %s\n", debugstr_w(source), debugstr_w(dest));
350 ret = CopyFileW(source, dest, FALSE);
351 if (!ret)
353 WARN("CopyFile failed: %d\n", GetLastError());
354 return FALSE;
358 return TRUE;
361 static LPWSTR wildcard_to_file(LPWSTR wildcard, LPWSTR filename)
363 LPWSTR path, ptr;
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);
375 return path;
378 static void free_file_entry(FILE_LIST *file)
380 msi_free(file->source);
381 msi_free(file->dest);
382 msi_free(file);
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;
400 DWORD size;
402 new = msi_alloc_zero(sizeof(FILE_LIST));
403 if (!new)
404 return FALSE;
406 new->source = strdupW(source);
407 ptr = strrchrW(dest, '\\') + 1;
408 filename = strrchrW(new->source, '\\') + 1;
410 new->sourcename = filename;
412 if (*ptr)
413 new->destname = ptr;
414 else
415 new->destname = new->sourcename;
417 size = (ptr - dest) + lstrlenW(filename) + 1;
418 new->dest = msi_alloc(size * sizeof(WCHAR));
419 if (!new->dest)
421 free_file_entry(new);
422 return FALSE;
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);
431 return TRUE;
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);
439 return TRUE;
443 list_add_after(&file->entry, &new->entry);
444 return TRUE;
447 static BOOL move_files_wildcard(LPWSTR source, LPWSTR dest, int options)
449 WIN32_FIND_DATAW wfd;
450 HANDLE hfile;
451 LPWSTR path;
452 BOOL res;
453 FILE_LIST files, *file;
454 DWORD size;
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);
466 if (!path)
468 res = FALSE;
469 goto done;
472 add_wildcard(&files, path, dest);
473 msi_free(path);
476 /* no files match the wildcard */
477 if (list_empty(&files.entry))
478 goto done;
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));
484 if (!file->dest)
486 res = FALSE;
487 goto done;
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);
507 res = TRUE;
509 done:
510 free_list(&files);
511 FindClose(hfile);
512 return res;
515 static UINT ITERATE_MoveFiles( MSIRECORD *rec, LPVOID param )
517 MSIPACKAGE *package = param;
518 MSIRECORD *uirow;
519 MSICOMPONENT *comp;
520 LPCWSTR sourcename, component;
521 LPWSTR sourcedir, destname = NULL, destdir = NULL, source = NULL, dest = NULL;
522 int options;
523 DWORD size;
524 BOOL ret, wildcards;
526 component = MSI_RecordGetString(rec, 2);
527 comp = get_loaded_component(package, component);
528 if (!comp)
529 return ERROR_SUCCESS;
531 if (!comp->Enabled)
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));
549 if (!sourcedir)
550 goto done;
552 destdir = msi_dup_property(package->db, MSI_RecordGetString(rec, 6));
553 if (!destdir)
554 goto done;
556 if (!sourcename)
558 if (GetFileAttributesW(sourcedir) == INVALID_FILE_ATTRIBUTES)
559 goto done;
561 source = strdupW(sourcedir);
562 if (!source)
563 goto done;
565 else
567 size = lstrlenW(sourcedir) + lstrlenW(sourcename) + 2;
568 source = msi_alloc(size * sizeof(WCHAR));
569 if (!source)
570 goto done;
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))
582 if (!wildcards)
584 destname = strdupW(sourcename);
585 if (!destname)
586 goto done;
589 else
591 destname = strdupW(MSI_RecordGetString(rec, 4));
592 if (destname)
593 reduce_to_longfilename(destname);
596 size = 0;
597 if (destname)
598 size = lstrlenW(destname);
600 size += lstrlenW(destdir) + 2;
601 dest = msi_alloc(size * sizeof(WCHAR));
602 if (!dest)
603 goto done;
605 lstrcpyW(dest, destdir);
606 if (dest[lstrlenW(dest) - 1] != '\\')
607 lstrcatW(dest, szBackSlash);
609 if (destname)
610 lstrcatW(dest, destname);
612 if (GetFileAttributesW(destdir) == INVALID_FILE_ATTRIBUTES)
614 ret = CreateDirectoryW(destdir, NULL);
615 if (!ret)
617 WARN("CreateDirectory failed: %d\n", GetLastError());
618 goto done;
622 if (!wildcards)
623 msi_move_file(source, dest, options);
624 else
625 move_files_wildcard(source, dest, options);
627 done:
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 );
635 msi_free(sourcedir);
636 msi_free(destdir);
637 msi_free(destname);
638 msi_free(source);
639 msi_free(dest);
641 return ERROR_SUCCESS;
644 UINT ACTION_MoveFiles( MSIPACKAGE *package )
646 UINT rc;
647 MSIQUERY *view;
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);
660 return rc;
663 static WCHAR *get_duplicate_filename( MSIPACKAGE *package, MSIRECORD *row, const WCHAR *file_key, const WCHAR *src )
665 DWORD len;
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 );
674 else
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 ))
684 WCHAR *p;
685 dst_path = strdupW( src );
686 p = strrchrW( dst_path, '\\' );
687 if (p) *p = 0;
689 else
691 const WCHAR *dst_key = MSI_RecordGetString( row, 5 );
693 dst_path = resolve_folder( package, dst_key, FALSE, FALSE, TRUE, NULL );
694 if (!dst_path)
696 /* try a property */
697 dst_path = msi_dup_property( package->db, dst_key );
698 if (!dst_path)
700 FIXME("Unable to get destination folder, try AppSearch properties\n");
701 msi_free( dst_name );
702 return NULL;
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 );
712 return dst;
715 static UINT ITERATE_DuplicateFiles(MSIRECORD *row, LPVOID param)
717 MSIPACKAGE *package = param;
718 LPWSTR dest;
719 LPCWSTR file_key, component;
720 MSICOMPONENT *comp;
721 MSIRECORD *uirow;
722 MSIFILE *file;
724 component = MSI_RecordGetString(row,2);
725 comp = get_loaded_component(package,component);
726 if (!comp)
727 return ERROR_SUCCESS;
729 if (!comp->Enabled)
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);
744 if (!file_key)
746 ERR("Unable to get file key\n");
747 return ERROR_FUNCTION_FAILED;
750 file = get_loaded_file( package, file_key );
751 if (!file)
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 );
758 if (!dest)
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 );
781 msi_free(dest);
782 return ERROR_SUCCESS;
785 UINT ACTION_DuplicateFiles(MSIPACKAGE *package)
787 UINT rc;
788 MSIQUERY * view;
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);
800 return rc;
803 static UINT ITERATE_RemoveDuplicateFiles( MSIRECORD *row, LPVOID param )
805 MSIPACKAGE *package = param;
806 LPWSTR dest;
807 LPCWSTR file_key, component;
808 MSICOMPONENT *comp;
809 MSIRECORD *uirow;
810 MSIFILE *file;
812 component = MSI_RecordGetString( row, 2 );
813 comp = get_loaded_component( package, component );
814 if (!comp)
815 return ERROR_SUCCESS;
817 if (!comp->Enabled)
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 );
832 if (!file_key)
834 ERR("Unable to get file key\n");
835 return ERROR_FUNCTION_FAILED;
838 file = get_loaded_file( package, file_key );
839 if (!file)
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 );
846 if (!dest)
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 );
865 msi_free(dest);
866 return ERROR_SUCCESS;
869 UINT ACTION_RemoveDuplicateFiles( MSIPACKAGE *package )
871 UINT rc;
872 MSIQUERY *view;
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 );
884 return rc;
887 static BOOL verify_comp_for_removal(MSICOMPONENT *comp, UINT install_mode)
889 INSTALLSTATE request = comp->ActionRequest;
891 if (request == INSTALLSTATE_UNKNOWN)
892 return FALSE;
894 if (install_mode == msidbRemoveFileInstallModeOnInstall &&
895 (request == INSTALLSTATE_LOCAL || request == INSTALLSTATE_SOURCE))
896 return TRUE;
898 if (request == INSTALLSTATE_ABSENT)
900 if (!comp->ComponentId)
901 return FALSE;
903 if (install_mode == msidbRemoveFileInstallModeOnRemove)
904 return TRUE;
907 if (install_mode == msidbRemoveFileInstallModeOnBoth)
908 return TRUE;
910 return FALSE;
913 static UINT ITERATE_RemoveFiles(MSIRECORD *row, LPVOID param)
915 MSIPACKAGE *package = param;
916 MSICOMPONENT *comp;
917 MSIRECORD *uirow;
918 LPCWSTR component, filename, dirprop;
919 UINT install_mode;
920 LPWSTR dir = NULL, path = NULL;
921 DWORD size;
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);
930 if (!comp->Enabled)
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 missing conditions\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);
950 if (!dir)
951 return ERROR_OUTOFMEMORY;
953 size = (filename != NULL) ? lstrlenW(filename) : 0;
954 size += lstrlenW(dir) + 2;
955 path = msi_alloc(size * sizeof(WCHAR));
956 if (!path)
958 ret = ERROR_OUTOFMEMORY;
959 goto done;
962 if (filename)
964 lstrcpyW(path, dir);
965 PathAddBackslashW(path);
966 lstrcatW(path, filename);
968 TRACE("Deleting misc file: %s\n", debugstr_w(path));
969 DeleteFileW(path);
971 else
973 TRACE("Removing misc directory: %s\n", debugstr_w(dir));
974 RemoveDirectoryW(dir);
977 done:
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 );
984 msi_free(path);
985 msi_free(dir);
986 return ret;
989 UINT ACTION_RemoveFiles( MSIPACKAGE *package )
991 MSIQUERY *view;
992 MSIFILE *file;
993 UINT r;
995 static const WCHAR query[] = {
996 'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
997 '`','R','e','m','o','v','e','F','i','l','e','`',0};
998 static const WCHAR folder_query[] = {
999 'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
1000 '`','C','r','e','a','t','e','F','o','l','d','e','r','`',0};
1002 r = MSI_DatabaseOpenViewW(package->db, query, &view);
1003 if (r == ERROR_SUCCESS)
1005 MSI_IterateRecords(view, NULL, ITERATE_RemoveFiles, package);
1006 msiobj_release(&view->hdr);
1009 r = MSI_DatabaseOpenViewW(package->db, folder_query, &view);
1010 if (r == ERROR_SUCCESS)
1011 msiobj_release(&view->hdr);
1013 LIST_FOR_EACH_ENTRY( file, &package->files, MSIFILE, entry )
1015 MSIRECORD *uirow;
1016 LPWSTR dir, p;
1017 VS_FIXEDFILEINFO *ver;
1019 if ( file->state == msifs_installed )
1020 ERR("removing installed file %s\n", debugstr_w(file->TargetPath));
1022 if ( file->Component->ActionRequest != INSTALLSTATE_ABSENT ||
1023 file->Component->Installed == INSTALLSTATE_SOURCE )
1024 continue;
1026 if (!file->Component->Enabled)
1028 TRACE("component is disabled\n");
1029 continue;
1032 if (file->Component->Attributes & msidbComponentAttributesPermanent)
1034 TRACE("permanent component, not removing file\n");
1035 continue;
1038 if (file->Version)
1040 ver = msi_get_disk_file_version( file->TargetPath );
1041 if (ver && msi_compare_file_versions( ver, file->Version ) > 0)
1043 TRACE("newer version detected, not removing file\n");
1044 msi_free( ver );
1045 continue;
1047 msi_free( ver );
1050 TRACE("removing %s\n", debugstr_w(file->File) );
1052 SetFileAttributesW( file->TargetPath, FILE_ATTRIBUTE_NORMAL );
1053 if (!DeleteFileW( file->TargetPath ))
1055 WARN("failed to delete %s (%u)\n", debugstr_w(file->TargetPath), GetLastError());
1057 /* FIXME: check persistence for each directory */
1058 else if (r && (dir = strdupW( file->TargetPath )))
1060 if ((p = strrchrW( dir, '\\' ))) *p = 0;
1061 RemoveDirectoryW( dir );
1062 msi_free( dir );
1064 file->state = msifs_missing;
1066 uirow = MSI_CreateRecord( 9 );
1067 MSI_RecordSetStringW( uirow, 1, file->FileName );
1068 MSI_RecordSetStringW( uirow, 9, file->Component->Directory );
1069 ui_actiondata( package, szRemoveFiles, uirow );
1070 msiobj_release( &uirow->hdr );
1071 /* FIXME: call ui_progress here? */
1074 return ERROR_SUCCESS;