msi: Update the UI in the MoveFiles action.
[wine.git] / dlls / msi / files.c
blob7ccb2c787d742b10f8dfab788ef3cbd7ff96e2d3
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;
53 LPWSTR uipath, p;
55 /* the UI chunk */
56 uirow = MSI_CreateRecord( 9 );
57 MSI_RecordSetStringW( uirow, 1, f->FileName );
58 uipath = strdupW( f->TargetPath );
59 p = strrchrW(uipath,'\\');
60 if (p)
61 p[1]=0;
62 MSI_RecordSetStringW( uirow, 9, uipath);
63 MSI_RecordSetInteger( uirow, 6, f->FileSize );
64 ui_actiondata( package, action, uirow);
65 msiobj_release( &uirow->hdr );
66 msi_free( uipath );
67 ui_progress( package, 2, f->FileSize, 0, 0);
70 /* compares the version of a file read from the filesystem and
71 * the version specified in the File table
73 static int msi_compare_file_version(MSIFILE *file)
75 WCHAR version[MAX_PATH];
76 DWORD size;
77 UINT r;
79 size = MAX_PATH;
80 version[0] = '\0';
81 r = MsiGetFileVersionW(file->TargetPath, version, &size, NULL, NULL);
82 if (r != ERROR_SUCCESS)
83 return 0;
85 return lstrcmpW(version, file->Version);
88 static void schedule_install_files(MSIPACKAGE *package)
90 MSIFILE *file;
92 LIST_FOR_EACH_ENTRY(file, &package->files, MSIFILE, entry)
94 if (file->Component->ActionRequest != INSTALLSTATE_LOCAL)
96 TRACE("File %s is not scheduled for install\n", debugstr_w(file->File));
98 ui_progress(package,2,file->FileSize,0,0);
99 file->state = msifs_skipped;
104 static UINT copy_file(MSIFILE *file, LPWSTR source)
106 BOOL ret;
108 ret = CopyFileW(source, file->TargetPath, FALSE);
109 if (!ret)
110 return GetLastError();
112 SetFileAttributesW(file->TargetPath, FILE_ATTRIBUTE_NORMAL);
114 file->state = msifs_installed;
115 return ERROR_SUCCESS;
118 static UINT copy_install_file(MSIPACKAGE *package, MSIFILE *file, LPWSTR source)
120 UINT gle;
122 TRACE("Copying %s to %s\n", debugstr_w(source),
123 debugstr_w(file->TargetPath));
125 gle = copy_file(file, source);
126 if (gle == ERROR_SUCCESS)
127 return gle;
129 if (gle == ERROR_ALREADY_EXISTS && file->state == msifs_overwrite)
131 TRACE("overwriting existing file\n");
132 return ERROR_SUCCESS;
134 else if (gle == ERROR_ACCESS_DENIED)
136 SetFileAttributesW(file->TargetPath, FILE_ATTRIBUTE_NORMAL);
138 gle = copy_file(file, source);
139 TRACE("Overwriting existing file: %d\n", gle);
141 if (gle == ERROR_SHARING_VIOLATION || gle == ERROR_USER_MAPPED_FILE)
143 WCHAR tmpfileW[MAX_PATH], *pathW, *p;
144 DWORD len;
146 TRACE("file in use, scheduling rename operation\n");
148 GetTempFileNameW(szBackSlash, szMsi, 0, tmpfileW);
149 len = strlenW(file->TargetPath) + strlenW(tmpfileW) + 1;
150 if (!(pathW = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR))))
151 return ERROR_OUTOFMEMORY;
153 strcpyW(pathW, file->TargetPath);
154 if ((p = strrchrW(pathW, '\\'))) *p = 0;
155 strcatW(pathW, tmpfileW);
157 if (CopyFileW(source, pathW, FALSE) &&
158 MoveFileExW(file->TargetPath, NULL, MOVEFILE_DELAY_UNTIL_REBOOT) &&
159 MoveFileExW(pathW, file->TargetPath, MOVEFILE_DELAY_UNTIL_REBOOT))
161 file->state = msifs_installed;
162 package->need_reboot = 1;
163 gle = ERROR_SUCCESS;
165 else
167 gle = GetLastError();
168 WARN("failed to schedule rename operation: %d)\n", gle);
170 HeapFree(GetProcessHeap(), 0, pathW);
173 return gle;
176 static BOOL check_dest_hash_matches(MSIFILE *file)
178 MSIFILEHASHINFO hash;
179 UINT r;
181 if (!file->hash.dwFileHashInfoSize)
182 return FALSE;
184 hash.dwFileHashInfoSize = sizeof(MSIFILEHASHINFO);
185 r = MsiGetFileHashW(file->TargetPath, 0, &hash);
186 if (r != ERROR_SUCCESS)
187 return FALSE;
189 return !memcmp(&hash, &file->hash, sizeof(MSIFILEHASHINFO));
192 static BOOL installfiles_cb(MSIPACKAGE *package, LPCWSTR file, DWORD action,
193 LPWSTR *path, DWORD *attrs, PVOID user)
195 static MSIFILE *f = NULL;
197 if (action == MSICABEXTRACT_BEGINEXTRACT)
199 f = get_loaded_file(package, file);
200 if (!f)
202 WARN("unknown file in cabinet (%s)\n", debugstr_w(file));
203 return FALSE;
206 if (f->state != msifs_missing && f->state != msifs_overwrite)
208 TRACE("Skipping extraction of %s\n", debugstr_w(file));
209 return FALSE;
212 msi_file_update_ui(package, f, szInstallFiles);
214 *path = strdupW(f->TargetPath);
215 *attrs = f->Attributes;
217 else if (action == MSICABEXTRACT_FILEEXTRACTED)
219 f->state = msifs_installed;
220 f = NULL;
223 return TRUE;
227 * ACTION_InstallFiles()
229 * For efficiency, this is done in two passes:
230 * 1) Correct all the TargetPaths and determine what files are to be installed.
231 * 2) Extract Cabinets and copy files.
233 UINT ACTION_InstallFiles(MSIPACKAGE *package)
235 MSIMEDIAINFO *mi;
236 UINT rc = ERROR_SUCCESS;
237 MSIFILE *file;
239 /* increment progress bar each time action data is sent */
240 ui_progress(package,1,1,0,0);
242 schedule_install_files(package);
245 * Despite MSDN specifying that the CreateFolders action
246 * should be called before InstallFiles, some installers don't
247 * do that, and they seem to work correctly. We need to create
248 * directories here to make sure that the files can be copied.
250 msi_create_component_directories( package );
252 mi = msi_alloc_zero( sizeof(MSIMEDIAINFO) );
254 LIST_FOR_EACH_ENTRY( file, &package->files, MSIFILE, entry )
256 if (file->state != msifs_missing && !mi->is_continuous && file->state != msifs_overwrite)
257 continue;
259 if (check_dest_hash_matches(file))
261 TRACE("File hashes match, not overwriting\n");
262 continue;
265 if (MsiGetFileVersionW(file->TargetPath, NULL, NULL, NULL, NULL) == ERROR_SUCCESS &&
266 msi_compare_file_version(file) >= 0)
268 TRACE("Destination file version greater, not overwriting\n");
269 continue;
272 if (file->Sequence > mi->last_sequence || mi->is_continuous ||
273 (file->IsCompressed && !mi->is_extracted))
275 MSICABDATA data;
277 rc = ready_media(package, file, mi);
278 if (rc != ERROR_SUCCESS)
280 ERR("Failed to ready media\n");
281 break;
284 data.mi = mi;
285 data.package = package;
286 data.cb = installfiles_cb;
287 data.user = NULL;
289 if (file->IsCompressed &&
290 !msi_cabextract(package, mi, &data))
292 ERR("Failed to extract cabinet: %s\n", debugstr_w(mi->cabinet));
293 rc = ERROR_FUNCTION_FAILED;
294 break;
298 if (!file->IsCompressed)
300 LPWSTR source = resolve_file_source(package, file);
302 TRACE("file paths %s to %s\n", debugstr_w(source),
303 debugstr_w(file->TargetPath));
305 msi_file_update_ui(package, file, szInstallFiles);
306 rc = copy_install_file(package, file, source);
307 if (rc != ERROR_SUCCESS)
309 ERR("Failed to copy %s to %s (%d)\n", debugstr_w(source),
310 debugstr_w(file->TargetPath), rc);
311 rc = ERROR_INSTALL_FAILURE;
312 msi_free(source);
313 break;
316 msi_free(source);
318 else if (file->state != msifs_installed)
320 ERR("compressed file wasn't extracted (%s)\n",
321 debugstr_w(file->TargetPath));
322 rc = ERROR_INSTALL_FAILURE;
323 break;
327 msi_free_media_info(mi);
328 return rc;
331 #define is_dot_dir(x) ((x[0] == '.') && ((x[1] == 0) || ((x[1] == '.') && (x[2] == 0))))
333 typedef struct
335 struct list entry;
336 LPWSTR sourcename;
337 LPWSTR destname;
338 LPWSTR source;
339 LPWSTR dest;
340 } FILE_LIST;
342 static BOOL msi_move_file(LPCWSTR source, LPCWSTR dest, int options)
344 BOOL ret;
346 if (GetFileAttributesW(source) == FILE_ATTRIBUTE_DIRECTORY ||
347 GetFileAttributesW(dest) == FILE_ATTRIBUTE_DIRECTORY)
349 WARN("Source or dest is directory, not moving\n");
350 return FALSE;
353 if (options == msidbMoveFileOptionsMove)
355 TRACE("moving %s -> %s\n", debugstr_w(source), debugstr_w(dest));
356 ret = MoveFileExW(source, dest, MOVEFILE_REPLACE_EXISTING);
357 if (!ret)
359 WARN("MoveFile failed: %d\n", GetLastError());
360 return FALSE;
363 else
365 TRACE("copying %s -> %s\n", debugstr_w(source), debugstr_w(dest));
366 ret = CopyFileW(source, dest, FALSE);
367 if (!ret)
369 WARN("CopyFile failed: %d\n", GetLastError());
370 return FALSE;
374 return TRUE;
377 static LPWSTR wildcard_to_file(LPWSTR wildcard, LPWSTR filename)
379 LPWSTR path, ptr;
380 DWORD dirlen, pathlen;
382 ptr = strrchrW(wildcard, '\\');
383 dirlen = ptr - wildcard + 1;
385 pathlen = dirlen + lstrlenW(filename) + 1;
386 path = msi_alloc(pathlen * sizeof(WCHAR));
388 lstrcpynW(path, wildcard, dirlen + 1);
389 lstrcatW(path, filename);
391 return path;
394 static void free_file_entry(FILE_LIST *file)
396 msi_free(file->source);
397 msi_free(file->dest);
398 msi_free(file);
401 static void free_list(FILE_LIST *list)
403 while (!list_empty(&list->entry))
405 FILE_LIST *file = LIST_ENTRY(list_head(&list->entry), FILE_LIST, entry);
407 list_remove(&file->entry);
408 free_file_entry(file);
412 static BOOL add_wildcard(FILE_LIST *files, LPWSTR source, LPWSTR dest)
414 FILE_LIST *new, *file;
415 LPWSTR ptr, filename;
416 DWORD size;
418 new = msi_alloc_zero(sizeof(FILE_LIST));
419 if (!new)
420 return FALSE;
422 new->source = strdupW(source);
423 ptr = strrchrW(dest, '\\') + 1;
424 filename = strrchrW(new->source, '\\') + 1;
426 new->sourcename = filename;
428 if (*ptr)
429 new->destname = ptr;
430 else
431 new->destname = new->sourcename;
433 size = (ptr - dest) + lstrlenW(filename) + 1;
434 new->dest = msi_alloc(size * sizeof(WCHAR));
435 if (!new->dest)
437 free_file_entry(new);
438 return FALSE;
441 lstrcpynW(new->dest, dest, ptr - dest + 1);
442 lstrcatW(new->dest, filename);
444 if (list_empty(&files->entry))
446 list_add_head(&files->entry, &new->entry);
447 return TRUE;
450 LIST_FOR_EACH_ENTRY(file, &files->entry, FILE_LIST, entry)
452 if (lstrcmpW(source, file->source) < 0)
454 list_add_before(&file->entry, &new->entry);
455 return TRUE;
459 list_add_after(&file->entry, &new->entry);
460 return TRUE;
463 static BOOL move_files_wildcard(LPWSTR source, LPWSTR dest, int options)
465 WIN32_FIND_DATAW wfd;
466 HANDLE hfile;
467 LPWSTR path;
468 BOOL res;
469 FILE_LIST files, *file;
470 DWORD size;
472 hfile = FindFirstFileW(source, &wfd);
473 if (hfile == INVALID_HANDLE_VALUE) return FALSE;
475 list_init(&files.entry);
477 for (res = TRUE; res; res = FindNextFileW(hfile, &wfd))
479 if (is_dot_dir(wfd.cFileName)) continue;
481 path = wildcard_to_file(source, wfd.cFileName);
482 if (!path)
484 res = FALSE;
485 goto done;
488 add_wildcard(&files, path, dest);
489 msi_free(path);
492 /* no files match the wildcard */
493 if (list_empty(&files.entry))
494 goto done;
496 /* only the first wildcard match gets renamed to dest */
497 file = LIST_ENTRY(list_head(&files.entry), FILE_LIST, entry);
498 size = (strrchrW(file->dest, '\\') - file->dest) + lstrlenW(file->destname) + 2;
499 file->dest = msi_realloc(file->dest, size * sizeof(WCHAR));
500 if (!file->dest)
502 res = FALSE;
503 goto done;
506 /* file->dest may be shorter after the reallocation, so add a NULL
507 * terminator. This is needed for the call to strrchrW, as there will no
508 * longer be a NULL terminator within the bounds of the allocation in this case.
510 file->dest[size - 1] = '\0';
511 lstrcpyW(strrchrW(file->dest, '\\') + 1, file->destname);
513 while (!list_empty(&files.entry))
515 file = LIST_ENTRY(list_head(&files.entry), FILE_LIST, entry);
517 msi_move_file(file->source, file->dest, options);
519 list_remove(&file->entry);
520 free_file_entry(file);
523 res = TRUE;
525 done:
526 free_list(&files);
527 FindClose(hfile);
528 return res;
531 static UINT ITERATE_MoveFiles( MSIRECORD *rec, LPVOID param )
533 MSIPACKAGE *package = param;
534 MSIRECORD *uirow;
535 MSICOMPONENT *comp;
536 LPCWSTR sourcename, component;
537 LPWSTR sourcedir, destname = NULL, destdir = NULL, source = NULL, dest = NULL;
538 int options;
539 DWORD size;
540 BOOL ret, wildcards;
542 component = MSI_RecordGetString(rec, 2);
543 comp = get_loaded_component(package, component);
544 if (!comp)
545 return ERROR_SUCCESS;
547 if (comp->ActionRequest != INSTALLSTATE_LOCAL && comp->ActionRequest != INSTALLSTATE_SOURCE)
549 TRACE("Component not scheduled for installation: %s\n", debugstr_w(component));
550 comp->Action = comp->Installed;
551 return ERROR_SUCCESS;
553 comp->Action = comp->ActionRequest;
555 sourcename = MSI_RecordGetString(rec, 3);
556 options = MSI_RecordGetInteger(rec, 7);
558 sourcedir = msi_dup_property(package, MSI_RecordGetString(rec, 5));
559 if (!sourcedir)
560 goto done;
562 destdir = msi_dup_property(package, MSI_RecordGetString(rec, 6));
563 if (!destdir)
564 goto done;
566 if (!sourcename)
568 if (GetFileAttributesW(sourcedir) == INVALID_FILE_ATTRIBUTES)
569 goto done;
571 source = strdupW(sourcedir);
572 if (!source)
573 goto done;
575 else
577 size = lstrlenW(sourcedir) + lstrlenW(sourcename) + 2;
578 source = msi_alloc(size * sizeof(WCHAR));
579 if (!source)
580 goto done;
582 lstrcpyW(source, sourcedir);
583 if (source[lstrlenW(source) - 1] != '\\')
584 lstrcatW(source, szBackSlash);
585 lstrcatW(source, sourcename);
588 wildcards = strchrW(source, '*') || strchrW(source, '?');
590 if (MSI_RecordIsNull(rec, 4))
592 if (!wildcards)
594 destname = strdupW(sourcename);
595 if (!destname)
596 goto done;
599 else
601 destname = strdupW(MSI_RecordGetString(rec, 4));
602 if (destname)
603 reduce_to_longfilename(destname);
606 size = 0;
607 if (destname)
608 size = lstrlenW(destname);
610 size += lstrlenW(destdir) + 2;
611 dest = msi_alloc(size * sizeof(WCHAR));
612 if (!dest)
613 goto done;
615 lstrcpyW(dest, destdir);
616 if (dest[lstrlenW(dest) - 1] != '\\')
617 lstrcatW(dest, szBackSlash);
619 if (destname)
620 lstrcatW(dest, destname);
622 if (GetFileAttributesW(destdir) == INVALID_FILE_ATTRIBUTES)
624 ret = CreateDirectoryW(destdir, NULL);
625 if (!ret)
627 WARN("CreateDirectory failed: %d\n", GetLastError());
628 goto done;
632 if (!wildcards)
633 msi_move_file(source, dest, options);
634 else
635 move_files_wildcard(source, dest, options);
637 done:
638 uirow = MSI_CreateRecord( 9 );
639 MSI_RecordSetStringW( uirow, 1, MSI_RecordGetString(rec, 1) );
640 MSI_RecordSetInteger( uirow, 6, 1 ); /* FIXME */
641 MSI_RecordSetStringW( uirow, 9, destdir );
642 ui_actiondata( package, szMoveFiles, uirow );
643 msiobj_release( &uirow->hdr );
645 msi_free(sourcedir);
646 msi_free(destdir);
647 msi_free(destname);
648 msi_free(source);
649 msi_free(dest);
651 return ERROR_SUCCESS;
654 UINT ACTION_MoveFiles( MSIPACKAGE *package )
656 UINT rc;
657 MSIQUERY *view;
659 static const WCHAR ExecSeqQuery[] =
660 {'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
661 '`','M','o','v','e','F','i','l','e','`',0};
663 rc = MSI_DatabaseOpenViewW(package->db, ExecSeqQuery, &view);
664 if (rc != ERROR_SUCCESS)
665 return ERROR_SUCCESS;
667 rc = MSI_IterateRecords(view, NULL, ITERATE_MoveFiles, package);
668 msiobj_release(&view->hdr);
670 return rc;
673 static WCHAR *get_duplicate_filename( MSIPACKAGE *package, MSIRECORD *row, const WCHAR *file_key, const WCHAR *src )
675 DWORD len;
676 WCHAR *dst_name, *dst_path, *dst;
678 if (MSI_RecordIsNull( row, 4 ))
680 len = strlenW( src ) + 1;
681 if (!(dst_name = msi_alloc( len * sizeof(WCHAR)))) return NULL;
682 strcpyW( dst_name, strrchrW( src, '\\' ) + 1 );
684 else
686 MSI_RecordGetStringW( row, 4, NULL, &len );
687 if (!(dst_name = msi_alloc( ++len * sizeof(WCHAR) ))) return NULL;
688 MSI_RecordGetStringW( row, 4, dst_name, &len );
689 reduce_to_longfilename( dst_name );
692 if (MSI_RecordIsNull( row, 5 ))
694 WCHAR *p;
695 dst_path = strdupW( src );
696 p = strrchrW( dst_path, '\\' );
697 if (p) *p = 0;
699 else
701 const WCHAR *dst_key = MSI_RecordGetString( row, 5 );
703 dst_path = resolve_folder( package, dst_key, FALSE, FALSE, TRUE, NULL );
704 if (!dst_path)
706 /* try a property */
707 dst_path = msi_dup_property( package, dst_key );
708 if (!dst_path)
710 FIXME("Unable to get destination folder, try AppSearch properties\n");
711 msi_free( dst_name );
712 return NULL;
717 dst = build_directory_name( 2, dst_path, dst_name );
718 create_full_pathW( dst_path );
720 msi_free( dst_name );
721 msi_free( dst_path );
722 return dst;
725 static UINT ITERATE_DuplicateFiles(MSIRECORD *row, LPVOID param)
727 MSIPACKAGE *package = param;
728 LPWSTR dest;
729 LPCWSTR file_key, component;
730 MSICOMPONENT *comp;
731 MSIRECORD *uirow;
732 MSIFILE *file;
734 component = MSI_RecordGetString(row,2);
735 comp = get_loaded_component(package,component);
736 if (!comp)
737 return ERROR_SUCCESS;
739 if (comp->ActionRequest != INSTALLSTATE_LOCAL)
741 TRACE("Component not scheduled for installation %s\n", debugstr_w(component));
742 comp->Action = comp->Installed;
743 return ERROR_SUCCESS;
745 comp->Action = INSTALLSTATE_LOCAL;
747 file_key = MSI_RecordGetString(row,3);
748 if (!file_key)
750 ERR("Unable to get file key\n");
751 return ERROR_FUNCTION_FAILED;
754 file = get_loaded_file( package, file_key );
755 if (!file)
757 ERR("Original file unknown %s\n", debugstr_w(file_key));
758 return ERROR_SUCCESS;
761 dest = get_duplicate_filename( package, row, file_key, file->TargetPath );
762 if (!dest)
764 WARN("Unable to get duplicate filename\n");
765 return ERROR_SUCCESS;
768 TRACE("Duplicating file %s to %s\n", debugstr_w(file->TargetPath), debugstr_w(dest));
770 if (!CopyFileW( file->TargetPath, dest, TRUE ))
772 WARN("Failed to copy file %s -> %s (%u)\n",
773 debugstr_w(file->TargetPath), debugstr_w(dest), GetLastError());
776 FIXME("We should track these duplicate files as well\n");
778 uirow = MSI_CreateRecord( 9 );
779 MSI_RecordSetStringW( uirow, 1, MSI_RecordGetString( row, 1 ) );
780 MSI_RecordSetInteger( uirow, 6, file->FileSize );
781 MSI_RecordSetStringW( uirow, 9, MSI_RecordGetString( row, 5 ) );
782 ui_actiondata( package, szDuplicateFiles, uirow );
783 msiobj_release( &uirow->hdr );
785 msi_free(dest);
786 return ERROR_SUCCESS;
789 UINT ACTION_DuplicateFiles(MSIPACKAGE *package)
791 UINT rc;
792 MSIQUERY * view;
793 static const WCHAR ExecSeqQuery[] =
794 {'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
795 '`','D','u','p','l','i','c','a','t','e','F','i','l','e','`',0};
797 rc = MSI_DatabaseOpenViewW(package->db, ExecSeqQuery, &view);
798 if (rc != ERROR_SUCCESS)
799 return ERROR_SUCCESS;
801 rc = MSI_IterateRecords(view, NULL, ITERATE_DuplicateFiles, package);
802 msiobj_release(&view->hdr);
804 return rc;
807 static UINT ITERATE_RemoveDuplicateFiles( MSIRECORD *row, LPVOID param )
809 MSIPACKAGE *package = param;
810 LPWSTR dest;
811 LPCWSTR file_key, component;
812 MSICOMPONENT *comp;
813 MSIRECORD *uirow;
814 MSIFILE *file;
816 component = MSI_RecordGetString( row, 2 );
817 comp = get_loaded_component( package, component );
818 if (!comp)
819 return ERROR_SUCCESS;
821 if (comp->ActionRequest != INSTALLSTATE_ABSENT)
823 TRACE("Component not scheduled for removal %s\n", debugstr_w(component));
824 comp->Action = comp->Installed;
825 return ERROR_SUCCESS;
827 comp->Action = INSTALLSTATE_ABSENT;
829 file_key = MSI_RecordGetString( row, 3 );
830 if (!file_key)
832 ERR("Unable to get file key\n");
833 return ERROR_FUNCTION_FAILED;
836 file = get_loaded_file( package, file_key );
837 if (!file)
839 ERR("Original file unknown %s\n", debugstr_w(file_key));
840 return ERROR_SUCCESS;
843 dest = get_duplicate_filename( package, row, file_key, file->TargetPath );
844 if (!dest)
846 WARN("Unable to get duplicate filename\n");
847 return ERROR_SUCCESS;
850 TRACE("Removing duplicate %s of %s\n", debugstr_w(dest), debugstr_w(file->TargetPath));
852 if (!DeleteFileW( dest ))
854 WARN("Failed to delete duplicate file %s (%u)\n", debugstr_w(dest), GetLastError());
857 uirow = MSI_CreateRecord( 9 );
858 MSI_RecordSetStringW( uirow, 1, MSI_RecordGetString( row, 1 ) );
859 MSI_RecordSetStringW( uirow, 9, MSI_RecordGetString( row, 5 ) );
860 ui_actiondata( package, szRemoveDuplicateFiles, uirow );
861 msiobj_release( &uirow->hdr );
863 msi_free(dest);
864 return ERROR_SUCCESS;
867 UINT ACTION_RemoveDuplicateFiles( MSIPACKAGE *package )
869 UINT rc;
870 MSIQUERY *view;
871 static const WCHAR query[] =
872 {'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
873 '`','D','u','p','l','i','c','a','t','e','F','i','l','e','`',0};
875 rc = MSI_DatabaseOpenViewW( package->db, query, &view );
876 if (rc != ERROR_SUCCESS)
877 return ERROR_SUCCESS;
879 rc = MSI_IterateRecords( view, NULL, ITERATE_RemoveDuplicateFiles, package );
880 msiobj_release( &view->hdr );
882 return rc;
885 static BOOL verify_comp_for_removal(MSICOMPONENT *comp, UINT install_mode)
887 INSTALLSTATE request = comp->ActionRequest;
889 if (request == INSTALLSTATE_UNKNOWN)
890 return FALSE;
892 if (install_mode == msidbRemoveFileInstallModeOnInstall &&
893 (request == INSTALLSTATE_LOCAL || request == INSTALLSTATE_SOURCE))
894 return TRUE;
896 if (request == INSTALLSTATE_ABSENT)
898 if (!comp->ComponentId)
899 return FALSE;
901 if (install_mode == msidbRemoveFileInstallModeOnRemove)
902 return TRUE;
905 if (install_mode == msidbRemoveFileInstallModeOnBoth)
906 return TRUE;
908 return FALSE;
911 static UINT ITERATE_RemoveFiles(MSIRECORD *row, LPVOID param)
913 MSIPACKAGE *package = param;
914 MSICOMPONENT *comp;
915 LPCWSTR component, filename, dirprop;
916 UINT install_mode;
917 LPWSTR dir = NULL, path = NULL;
918 DWORD size;
919 UINT r;
921 component = MSI_RecordGetString(row, 2);
922 filename = MSI_RecordGetString(row, 3);
923 dirprop = MSI_RecordGetString(row, 4);
924 install_mode = MSI_RecordGetInteger(row, 5);
926 comp = get_loaded_component(package, component);
927 if (!comp)
929 ERR("Invalid component: %s\n", debugstr_w(component));
930 return ERROR_FUNCTION_FAILED;
933 if (!verify_comp_for_removal(comp, install_mode))
935 TRACE("Skipping removal due to missing conditions\n");
936 comp->Action = comp->Installed;
937 return ERROR_SUCCESS;
940 dir = msi_dup_property(package, dirprop);
941 if (!dir)
942 return ERROR_OUTOFMEMORY;
944 size = (filename != NULL) ? lstrlenW(filename) : 0;
945 size += lstrlenW(dir) + 2;
946 path = msi_alloc(size * sizeof(WCHAR));
947 if (!path)
949 r = ERROR_OUTOFMEMORY;
950 goto done;
953 if (filename)
955 lstrcpyW(path, dir);
956 PathAddBackslashW(path);
957 lstrcatW(path, filename);
959 TRACE("Deleting misc file: %s\n", debugstr_w(path));
960 DeleteFileW(path);
962 else
964 TRACE("Removing misc directory: %s\n", debugstr_w(dir));
965 RemoveDirectoryW(dir);
968 done:
969 msi_free(path);
970 msi_free(dir);
971 return ERROR_SUCCESS;
974 UINT ACTION_RemoveFiles( MSIPACKAGE *package )
976 MSIQUERY *view;
977 MSIFILE *file;
978 UINT r;
980 static const WCHAR query[] = {
981 'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
982 '`','R','e','m','o','v','e','F','i','l','e','`',0};
983 static const WCHAR folder_query[] = {
984 'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
985 '`','C','r','e','a','t','e','F','o','l','d','e','r','`',0};
987 r = MSI_DatabaseOpenViewW(package->db, query, &view);
988 if (r == ERROR_SUCCESS)
990 MSI_IterateRecords(view, NULL, ITERATE_RemoveFiles, package);
991 msiobj_release(&view->hdr);
994 r = MSI_DatabaseOpenViewW(package->db, folder_query, &view);
995 if (r == ERROR_SUCCESS)
996 msiobj_release(&view->hdr);
998 LIST_FOR_EACH_ENTRY( file, &package->files, MSIFILE, entry )
1000 MSIRECORD *uirow;
1001 LPWSTR dir, uipath, p;
1003 if ( file->state == msifs_installed )
1004 ERR("removing installed file %s\n", debugstr_w(file->TargetPath));
1006 if ( file->Component->ActionRequest != INSTALLSTATE_ABSENT ||
1007 file->Component->Installed == INSTALLSTATE_SOURCE )
1008 continue;
1010 /* don't remove a file if the old file
1011 * is strictly newer than the version to be installed
1013 if ( msi_compare_file_version( file ) < 0 )
1014 continue;
1016 TRACE("removing %s\n", debugstr_w(file->File) );
1017 if (!DeleteFileW( file->TargetPath ))
1019 WARN("failed to delete %s\n", debugstr_w(file->TargetPath));
1021 /* FIXME: check persistence for each directory */
1022 else if (r && (dir = strdupW( file->TargetPath )))
1024 if ((p = strrchrW( dir, '\\' ))) *p = 0;
1025 RemoveDirectoryW( dir );
1026 msi_free( dir );
1028 file->state = msifs_missing;
1030 /* the UI chunk */
1031 uirow = MSI_CreateRecord( 9 );
1032 MSI_RecordSetStringW( uirow, 1, file->FileName );
1033 uipath = strdupW( file->TargetPath );
1034 p = strrchrW(uipath,'\\');
1035 if (p)
1036 p[1]=0;
1037 MSI_RecordSetStringW( uirow, 9, uipath);
1038 ui_actiondata( package, szRemoveFiles, uirow);
1039 msiobj_release( &uirow->hdr );
1040 msi_free( uipath );
1041 /* FIXME: call ui_progress here? */
1044 return ERROR_SUCCESS;