mshtml: Added IHTMLObjectElement::get_vspace implementation.
[wine/multimedia.git] / dlls / msi / files.c
blob47aa74fbb8061214592d7c71e54ec57d7b7a6d25
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 if (file->Component->ActionRequest != INSTALLSTATE_LOCAL || !file->Component->Enabled)
71 TRACE("File %s is not scheduled for install\n", debugstr_w(file->File));
73 ui_progress(package,2,file->FileSize,0,0);
74 file->state = msifs_skipped;
76 else
77 file->Component->Action = INSTALLSTATE_LOCAL;
81 static UINT copy_file(MSIFILE *file, LPWSTR source)
83 BOOL ret;
85 ret = CopyFileW(source, file->TargetPath, FALSE);
86 if (!ret)
87 return GetLastError();
89 SetFileAttributesW(file->TargetPath, FILE_ATTRIBUTE_NORMAL);
91 file->state = msifs_installed;
92 return ERROR_SUCCESS;
95 static UINT copy_install_file(MSIPACKAGE *package, MSIFILE *file, LPWSTR source)
97 UINT gle;
99 TRACE("Copying %s to %s\n", debugstr_w(source), debugstr_w(file->TargetPath));
101 gle = copy_file(file, source);
102 if (gle == ERROR_SUCCESS)
103 return gle;
105 if (gle == ERROR_ALREADY_EXISTS && file->state == msifs_overwrite)
107 TRACE("overwriting existing file\n");
108 return ERROR_SUCCESS;
110 else if (gle == ERROR_ACCESS_DENIED)
112 SetFileAttributesW(file->TargetPath, FILE_ATTRIBUTE_NORMAL);
114 gle = copy_file(file, source);
115 TRACE("Overwriting existing file: %d\n", gle);
117 if (gle == ERROR_SHARING_VIOLATION || gle == ERROR_USER_MAPPED_FILE)
119 WCHAR tmpfileW[MAX_PATH], *pathW, *p;
120 DWORD len;
122 TRACE("file in use, scheduling rename operation\n");
124 GetTempFileNameW(szBackSlash, szMsi, 0, tmpfileW);
125 len = strlenW(file->TargetPath) + strlenW(tmpfileW) + 1;
126 if (!(pathW = msi_alloc(len * sizeof(WCHAR))))
127 return ERROR_OUTOFMEMORY;
129 strcpyW(pathW, file->TargetPath);
130 if ((p = strrchrW(pathW, '\\'))) *p = 0;
131 strcatW(pathW, tmpfileW);
133 if (CopyFileW(source, pathW, FALSE) &&
134 MoveFileExW(file->TargetPath, NULL, MOVEFILE_DELAY_UNTIL_REBOOT) &&
135 MoveFileExW(pathW, file->TargetPath, MOVEFILE_DELAY_UNTIL_REBOOT))
137 file->state = msifs_installed;
138 package->need_reboot = 1;
139 gle = ERROR_SUCCESS;
141 else
143 gle = GetLastError();
144 WARN("failed to schedule rename operation: %d)\n", gle);
146 msi_free(pathW);
149 return gle;
152 static UINT msi_create_directory( MSIPACKAGE *package, const WCHAR *dir )
154 MSIFOLDER *folder;
155 WCHAR *install_path;
157 install_path = resolve_folder( package, dir, FALSE, FALSE, TRUE, &folder );
158 if (!install_path)
159 return ERROR_FUNCTION_FAILED;
161 if (folder->State == 0)
163 create_full_pathW( install_path );
164 folder->State = 2;
166 msi_free( install_path );
167 return ERROR_SUCCESS;
170 static BOOL installfiles_cb(MSIPACKAGE *package, LPCWSTR file, DWORD action,
171 LPWSTR *path, DWORD *attrs, PVOID user)
173 static MSIFILE *f = NULL;
174 UINT_PTR disk_id = (UINT_PTR)user;
176 if (action == MSICABEXTRACT_BEGINEXTRACT)
178 f = get_loaded_file(package, file);
179 if (!f)
181 WARN("unknown file in cabinet (%s)\n", debugstr_w(file));
182 return FALSE;
185 if (f->disk_id != disk_id || (f->state != msifs_missing && f->state != msifs_overwrite))
186 return FALSE;
188 msi_file_update_ui(package, f, szInstallFiles);
189 if (!f->Component->assembly)
190 msi_create_directory(package, f->Component->Directory);
192 *path = strdupW(f->TargetPath);
193 *attrs = f->Attributes;
195 else if (action == MSICABEXTRACT_FILEEXTRACTED)
197 f->state = msifs_installed;
198 f = NULL;
201 return TRUE;
205 * ACTION_InstallFiles()
207 * For efficiency, this is done in two passes:
208 * 1) Correct all the TargetPaths and determine what files are to be installed.
209 * 2) Extract Cabinets and copy files.
211 UINT ACTION_InstallFiles(MSIPACKAGE *package)
213 MSIMEDIAINFO *mi;
214 MSICOMPONENT *comp;
215 UINT rc = ERROR_SUCCESS;
216 MSIFILE *file;
218 /* increment progress bar each time action data is sent */
219 ui_progress(package,1,1,0,0);
221 schedule_install_files(package);
223 mi = msi_alloc_zero( sizeof(MSIMEDIAINFO) );
225 LIST_FOR_EACH_ENTRY( file, &package->files, MSIFILE, entry )
227 if (file->state != msifs_missing && !mi->is_continuous && file->state != msifs_overwrite)
228 continue;
230 if (file->Sequence > mi->last_sequence || mi->is_continuous ||
231 (file->IsCompressed && !mi->is_extracted))
233 MSICABDATA data;
235 rc = ready_media(package, file, mi);
236 if (rc != ERROR_SUCCESS)
238 ERR("Failed to ready media for %s\n", debugstr_w(file->File));
239 goto done;
242 data.mi = mi;
243 data.package = package;
244 data.cb = installfiles_cb;
245 data.user = (PVOID)(UINT_PTR)mi->disk_id;
247 if (file->IsCompressed &&
248 !msi_cabextract(package, mi, &data))
250 ERR("Failed to extract cabinet: %s\n", debugstr_w(mi->cabinet));
251 rc = ERROR_INSTALL_FAILURE;
252 goto done;
256 if (!file->IsCompressed)
258 LPWSTR source = resolve_file_source(package, file);
260 TRACE("copying %s to %s\n", debugstr_w(source), debugstr_w(file->TargetPath));
262 msi_file_update_ui(package, file, szInstallFiles);
263 if (!file->Component->assembly)
264 msi_create_directory(package, file->Component->Directory);
266 rc = copy_install_file(package, file, source);
267 if (rc != ERROR_SUCCESS)
269 ERR("Failed to copy %s to %s (%d)\n", debugstr_w(source),
270 debugstr_w(file->TargetPath), rc);
271 rc = ERROR_INSTALL_FAILURE;
272 msi_free(source);
273 goto done;
275 msi_free(source);
277 else if (file->state != msifs_installed)
279 ERR("compressed file wasn't installed (%s)\n", debugstr_w(file->TargetPath));
280 rc = ERROR_INSTALL_FAILURE;
281 goto done;
284 LIST_FOR_EACH_ENTRY( comp, &package->components, MSICOMPONENT, entry )
286 if (comp->Enabled && comp->assembly && !comp->assembly->installed)
288 rc = install_assembly( package, comp );
289 if (rc != ERROR_SUCCESS)
291 ERR("Failed to install assembly\n");
292 rc = ERROR_INSTALL_FAILURE;
293 break;
298 done:
299 msi_free_media_info(mi);
300 return rc;
303 #define is_dot_dir(x) ((x[0] == '.') && ((x[1] == 0) || ((x[1] == '.') && (x[2] == 0))))
305 typedef struct
307 struct list entry;
308 LPWSTR sourcename;
309 LPWSTR destname;
310 LPWSTR source;
311 LPWSTR dest;
312 } FILE_LIST;
314 static BOOL msi_move_file(LPCWSTR source, LPCWSTR dest, int options)
316 BOOL ret;
318 if (GetFileAttributesW(source) == FILE_ATTRIBUTE_DIRECTORY ||
319 GetFileAttributesW(dest) == FILE_ATTRIBUTE_DIRECTORY)
321 WARN("Source or dest is directory, not moving\n");
322 return FALSE;
325 if (options == msidbMoveFileOptionsMove)
327 TRACE("moving %s -> %s\n", debugstr_w(source), debugstr_w(dest));
328 ret = MoveFileExW(source, dest, MOVEFILE_REPLACE_EXISTING);
329 if (!ret)
331 WARN("MoveFile failed: %d\n", GetLastError());
332 return FALSE;
335 else
337 TRACE("copying %s -> %s\n", debugstr_w(source), debugstr_w(dest));
338 ret = CopyFileW(source, dest, FALSE);
339 if (!ret)
341 WARN("CopyFile failed: %d\n", GetLastError());
342 return FALSE;
346 return TRUE;
349 static LPWSTR wildcard_to_file(LPWSTR wildcard, LPWSTR filename)
351 LPWSTR path, ptr;
352 DWORD dirlen, pathlen;
354 ptr = strrchrW(wildcard, '\\');
355 dirlen = ptr - wildcard + 1;
357 pathlen = dirlen + lstrlenW(filename) + 1;
358 path = msi_alloc(pathlen * sizeof(WCHAR));
360 lstrcpynW(path, wildcard, dirlen + 1);
361 lstrcatW(path, filename);
363 return path;
366 static void free_file_entry(FILE_LIST *file)
368 msi_free(file->source);
369 msi_free(file->dest);
370 msi_free(file);
373 static void free_list(FILE_LIST *list)
375 while (!list_empty(&list->entry))
377 FILE_LIST *file = LIST_ENTRY(list_head(&list->entry), FILE_LIST, entry);
379 list_remove(&file->entry);
380 free_file_entry(file);
384 static BOOL add_wildcard(FILE_LIST *files, LPWSTR source, LPWSTR dest)
386 FILE_LIST *new, *file;
387 LPWSTR ptr, filename;
388 DWORD size;
390 new = msi_alloc_zero(sizeof(FILE_LIST));
391 if (!new)
392 return FALSE;
394 new->source = strdupW(source);
395 ptr = strrchrW(dest, '\\') + 1;
396 filename = strrchrW(new->source, '\\') + 1;
398 new->sourcename = filename;
400 if (*ptr)
401 new->destname = ptr;
402 else
403 new->destname = new->sourcename;
405 size = (ptr - dest) + lstrlenW(filename) + 1;
406 new->dest = msi_alloc(size * sizeof(WCHAR));
407 if (!new->dest)
409 free_file_entry(new);
410 return FALSE;
413 lstrcpynW(new->dest, dest, ptr - dest + 1);
414 lstrcatW(new->dest, filename);
416 if (list_empty(&files->entry))
418 list_add_head(&files->entry, &new->entry);
419 return TRUE;
422 LIST_FOR_EACH_ENTRY(file, &files->entry, FILE_LIST, entry)
424 if (strcmpW( source, file->source ) < 0)
426 list_add_before(&file->entry, &new->entry);
427 return TRUE;
431 list_add_after(&file->entry, &new->entry);
432 return TRUE;
435 static BOOL move_files_wildcard(LPWSTR source, LPWSTR dest, int options)
437 WIN32_FIND_DATAW wfd;
438 HANDLE hfile;
439 LPWSTR path;
440 BOOL res;
441 FILE_LIST files, *file;
442 DWORD size;
444 hfile = FindFirstFileW(source, &wfd);
445 if (hfile == INVALID_HANDLE_VALUE) return FALSE;
447 list_init(&files.entry);
449 for (res = TRUE; res; res = FindNextFileW(hfile, &wfd))
451 if (is_dot_dir(wfd.cFileName)) continue;
453 path = wildcard_to_file(source, wfd.cFileName);
454 if (!path)
456 res = FALSE;
457 goto done;
460 add_wildcard(&files, path, dest);
461 msi_free(path);
464 /* no files match the wildcard */
465 if (list_empty(&files.entry))
466 goto done;
468 /* only the first wildcard match gets renamed to dest */
469 file = LIST_ENTRY(list_head(&files.entry), FILE_LIST, entry);
470 size = (strrchrW(file->dest, '\\') - file->dest) + lstrlenW(file->destname) + 2;
471 file->dest = msi_realloc(file->dest, size * sizeof(WCHAR));
472 if (!file->dest)
474 res = FALSE;
475 goto done;
478 /* file->dest may be shorter after the reallocation, so add a NULL
479 * terminator. This is needed for the call to strrchrW, as there will no
480 * longer be a NULL terminator within the bounds of the allocation in this case.
482 file->dest[size - 1] = '\0';
483 lstrcpyW(strrchrW(file->dest, '\\') + 1, file->destname);
485 while (!list_empty(&files.entry))
487 file = LIST_ENTRY(list_head(&files.entry), FILE_LIST, entry);
489 msi_move_file(file->source, file->dest, options);
491 list_remove(&file->entry);
492 free_file_entry(file);
495 res = TRUE;
497 done:
498 free_list(&files);
499 FindClose(hfile);
500 return res;
503 static UINT ITERATE_MoveFiles( MSIRECORD *rec, LPVOID param )
505 MSIPACKAGE *package = param;
506 MSIRECORD *uirow;
507 MSICOMPONENT *comp;
508 LPCWSTR sourcename, component;
509 LPWSTR sourcedir, destname = NULL, destdir = NULL, source = NULL, dest = NULL;
510 int options;
511 DWORD size;
512 BOOL ret, wildcards;
514 component = MSI_RecordGetString(rec, 2);
515 comp = get_loaded_component(package, component);
516 if (!comp)
517 return ERROR_SUCCESS;
519 if (!comp->Enabled)
521 TRACE("component is disabled\n");
522 return ERROR_SUCCESS;
525 if (comp->ActionRequest != INSTALLSTATE_LOCAL && comp->ActionRequest != INSTALLSTATE_SOURCE)
527 TRACE("Component not scheduled for installation: %s\n", debugstr_w(component));
528 comp->Action = comp->Installed;
529 return ERROR_SUCCESS;
531 comp->Action = comp->ActionRequest;
533 sourcename = MSI_RecordGetString(rec, 3);
534 options = MSI_RecordGetInteger(rec, 7);
536 sourcedir = msi_dup_property(package->db, MSI_RecordGetString(rec, 5));
537 if (!sourcedir)
538 goto done;
540 destdir = msi_dup_property(package->db, MSI_RecordGetString(rec, 6));
541 if (!destdir)
542 goto done;
544 if (!sourcename)
546 if (GetFileAttributesW(sourcedir) == INVALID_FILE_ATTRIBUTES)
547 goto done;
549 source = strdupW(sourcedir);
550 if (!source)
551 goto done;
553 else
555 size = lstrlenW(sourcedir) + lstrlenW(sourcename) + 2;
556 source = msi_alloc(size * sizeof(WCHAR));
557 if (!source)
558 goto done;
560 lstrcpyW(source, sourcedir);
561 if (source[lstrlenW(source) - 1] != '\\')
562 lstrcatW(source, szBackSlash);
563 lstrcatW(source, sourcename);
566 wildcards = strchrW(source, '*') || strchrW(source, '?');
568 if (MSI_RecordIsNull(rec, 4))
570 if (!wildcards)
572 destname = strdupW(sourcename);
573 if (!destname)
574 goto done;
577 else
579 destname = strdupW(MSI_RecordGetString(rec, 4));
580 if (destname)
581 reduce_to_longfilename(destname);
584 size = 0;
585 if (destname)
586 size = lstrlenW(destname);
588 size += lstrlenW(destdir) + 2;
589 dest = msi_alloc(size * sizeof(WCHAR));
590 if (!dest)
591 goto done;
593 lstrcpyW(dest, destdir);
594 if (dest[lstrlenW(dest) - 1] != '\\')
595 lstrcatW(dest, szBackSlash);
597 if (destname)
598 lstrcatW(dest, destname);
600 if (GetFileAttributesW(destdir) == INVALID_FILE_ATTRIBUTES)
602 ret = CreateDirectoryW(destdir, NULL);
603 if (!ret)
605 WARN("CreateDirectory failed: %d\n", GetLastError());
606 goto done;
610 if (!wildcards)
611 msi_move_file(source, dest, options);
612 else
613 move_files_wildcard(source, dest, options);
615 done:
616 uirow = MSI_CreateRecord( 9 );
617 MSI_RecordSetStringW( uirow, 1, MSI_RecordGetString(rec, 1) );
618 MSI_RecordSetInteger( uirow, 6, 1 ); /* FIXME */
619 MSI_RecordSetStringW( uirow, 9, destdir );
620 ui_actiondata( package, szMoveFiles, uirow );
621 msiobj_release( &uirow->hdr );
623 msi_free(sourcedir);
624 msi_free(destdir);
625 msi_free(destname);
626 msi_free(source);
627 msi_free(dest);
629 return ERROR_SUCCESS;
632 UINT ACTION_MoveFiles( MSIPACKAGE *package )
634 UINT rc;
635 MSIQUERY *view;
637 static const WCHAR ExecSeqQuery[] =
638 {'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
639 '`','M','o','v','e','F','i','l','e','`',0};
641 rc = MSI_DatabaseOpenViewW(package->db, ExecSeqQuery, &view);
642 if (rc != ERROR_SUCCESS)
643 return ERROR_SUCCESS;
645 rc = MSI_IterateRecords(view, NULL, ITERATE_MoveFiles, package);
646 msiobj_release(&view->hdr);
648 return rc;
651 static WCHAR *get_duplicate_filename( MSIPACKAGE *package, MSIRECORD *row, const WCHAR *file_key, const WCHAR *src )
653 DWORD len;
654 WCHAR *dst_name, *dst_path, *dst;
656 if (MSI_RecordIsNull( row, 4 ))
658 len = strlenW( src ) + 1;
659 if (!(dst_name = msi_alloc( len * sizeof(WCHAR)))) return NULL;
660 strcpyW( dst_name, strrchrW( src, '\\' ) + 1 );
662 else
664 MSI_RecordGetStringW( row, 4, NULL, &len );
665 if (!(dst_name = msi_alloc( ++len * sizeof(WCHAR) ))) return NULL;
666 MSI_RecordGetStringW( row, 4, dst_name, &len );
667 reduce_to_longfilename( dst_name );
670 if (MSI_RecordIsNull( row, 5 ))
672 WCHAR *p;
673 dst_path = strdupW( src );
674 p = strrchrW( dst_path, '\\' );
675 if (p) *p = 0;
677 else
679 const WCHAR *dst_key = MSI_RecordGetString( row, 5 );
681 dst_path = resolve_folder( package, dst_key, FALSE, FALSE, TRUE, NULL );
682 if (!dst_path)
684 /* try a property */
685 dst_path = msi_dup_property( package->db, dst_key );
686 if (!dst_path)
688 FIXME("Unable to get destination folder, try AppSearch properties\n");
689 msi_free( dst_name );
690 return NULL;
695 dst = build_directory_name( 2, dst_path, dst_name );
696 create_full_pathW( dst_path );
698 msi_free( dst_name );
699 msi_free( dst_path );
700 return dst;
703 static UINT ITERATE_DuplicateFiles(MSIRECORD *row, LPVOID param)
705 MSIPACKAGE *package = param;
706 LPWSTR dest;
707 LPCWSTR file_key, component;
708 MSICOMPONENT *comp;
709 MSIRECORD *uirow;
710 MSIFILE *file;
712 component = MSI_RecordGetString(row,2);
713 comp = get_loaded_component(package,component);
714 if (!comp)
715 return ERROR_SUCCESS;
717 if (!comp->Enabled)
719 TRACE("component is disabled\n");
720 return ERROR_SUCCESS;
723 if (comp->ActionRequest != INSTALLSTATE_LOCAL)
725 TRACE("Component not scheduled for installation %s\n", debugstr_w(component));
726 comp->Action = comp->Installed;
727 return ERROR_SUCCESS;
729 comp->Action = INSTALLSTATE_LOCAL;
731 file_key = MSI_RecordGetString(row,3);
732 if (!file_key)
734 ERR("Unable to get file key\n");
735 return ERROR_FUNCTION_FAILED;
738 file = get_loaded_file( package, file_key );
739 if (!file)
741 ERR("Original file unknown %s\n", debugstr_w(file_key));
742 return ERROR_SUCCESS;
745 dest = get_duplicate_filename( package, row, file_key, file->TargetPath );
746 if (!dest)
748 WARN("Unable to get duplicate filename\n");
749 return ERROR_SUCCESS;
752 TRACE("Duplicating file %s to %s\n", debugstr_w(file->TargetPath), debugstr_w(dest));
754 if (!CopyFileW( file->TargetPath, dest, TRUE ))
756 WARN("Failed to copy file %s -> %s (%u)\n",
757 debugstr_w(file->TargetPath), debugstr_w(dest), GetLastError());
760 FIXME("We should track these duplicate files as well\n");
762 uirow = MSI_CreateRecord( 9 );
763 MSI_RecordSetStringW( uirow, 1, MSI_RecordGetString( row, 1 ) );
764 MSI_RecordSetInteger( uirow, 6, file->FileSize );
765 MSI_RecordSetStringW( uirow, 9, MSI_RecordGetString( row, 5 ) );
766 ui_actiondata( package, szDuplicateFiles, uirow );
767 msiobj_release( &uirow->hdr );
769 msi_free(dest);
770 return ERROR_SUCCESS;
773 UINT ACTION_DuplicateFiles(MSIPACKAGE *package)
775 UINT rc;
776 MSIQUERY * view;
777 static const WCHAR ExecSeqQuery[] =
778 {'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
779 '`','D','u','p','l','i','c','a','t','e','F','i','l','e','`',0};
781 rc = MSI_DatabaseOpenViewW(package->db, ExecSeqQuery, &view);
782 if (rc != ERROR_SUCCESS)
783 return ERROR_SUCCESS;
785 rc = MSI_IterateRecords(view, NULL, ITERATE_DuplicateFiles, package);
786 msiobj_release(&view->hdr);
788 return rc;
791 static UINT ITERATE_RemoveDuplicateFiles( MSIRECORD *row, LPVOID param )
793 MSIPACKAGE *package = param;
794 LPWSTR dest;
795 LPCWSTR file_key, component;
796 MSICOMPONENT *comp;
797 MSIRECORD *uirow;
798 MSIFILE *file;
800 component = MSI_RecordGetString( row, 2 );
801 comp = get_loaded_component( package, component );
802 if (!comp)
803 return ERROR_SUCCESS;
805 if (!comp->Enabled)
807 TRACE("component is disabled\n");
808 return ERROR_SUCCESS;
811 if (comp->ActionRequest != INSTALLSTATE_ABSENT)
813 TRACE("Component not scheduled for removal %s\n", debugstr_w(component));
814 comp->Action = comp->Installed;
815 return ERROR_SUCCESS;
817 comp->Action = INSTALLSTATE_ABSENT;
819 file_key = MSI_RecordGetString( row, 3 );
820 if (!file_key)
822 ERR("Unable to get file key\n");
823 return ERROR_FUNCTION_FAILED;
826 file = get_loaded_file( package, file_key );
827 if (!file)
829 ERR("Original file unknown %s\n", debugstr_w(file_key));
830 return ERROR_SUCCESS;
833 dest = get_duplicate_filename( package, row, file_key, file->TargetPath );
834 if (!dest)
836 WARN("Unable to get duplicate filename\n");
837 return ERROR_SUCCESS;
840 TRACE("Removing duplicate %s of %s\n", debugstr_w(dest), debugstr_w(file->TargetPath));
842 if (!DeleteFileW( dest ))
844 WARN("Failed to delete duplicate file %s (%u)\n", debugstr_w(dest), GetLastError());
847 uirow = MSI_CreateRecord( 9 );
848 MSI_RecordSetStringW( uirow, 1, MSI_RecordGetString( row, 1 ) );
849 MSI_RecordSetStringW( uirow, 9, MSI_RecordGetString( row, 5 ) );
850 ui_actiondata( package, szRemoveDuplicateFiles, uirow );
851 msiobj_release( &uirow->hdr );
853 msi_free(dest);
854 return ERROR_SUCCESS;
857 UINT ACTION_RemoveDuplicateFiles( MSIPACKAGE *package )
859 UINT rc;
860 MSIQUERY *view;
861 static const WCHAR query[] =
862 {'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
863 '`','D','u','p','l','i','c','a','t','e','F','i','l','e','`',0};
865 rc = MSI_DatabaseOpenViewW( package->db, query, &view );
866 if (rc != ERROR_SUCCESS)
867 return ERROR_SUCCESS;
869 rc = MSI_IterateRecords( view, NULL, ITERATE_RemoveDuplicateFiles, package );
870 msiobj_release( &view->hdr );
872 return rc;
875 static BOOL verify_comp_for_removal(MSICOMPONENT *comp, UINT install_mode)
877 INSTALLSTATE request = comp->ActionRequest;
879 if (request == INSTALLSTATE_UNKNOWN)
880 return FALSE;
882 if (install_mode == msidbRemoveFileInstallModeOnInstall &&
883 (request == INSTALLSTATE_LOCAL || request == INSTALLSTATE_SOURCE))
884 return TRUE;
886 if (request == INSTALLSTATE_ABSENT)
888 if (!comp->ComponentId)
889 return FALSE;
891 if (install_mode == msidbRemoveFileInstallModeOnRemove)
892 return TRUE;
895 if (install_mode == msidbRemoveFileInstallModeOnBoth)
896 return TRUE;
898 return FALSE;
901 static UINT ITERATE_RemoveFiles(MSIRECORD *row, LPVOID param)
903 MSIPACKAGE *package = param;
904 MSICOMPONENT *comp;
905 MSIRECORD *uirow;
906 LPCWSTR component, filename, dirprop;
907 UINT install_mode;
908 LPWSTR dir = NULL, path = NULL;
909 DWORD size;
910 UINT ret = ERROR_SUCCESS;
912 component = MSI_RecordGetString(row, 2);
913 filename = MSI_RecordGetString(row, 3);
914 dirprop = MSI_RecordGetString(row, 4);
915 install_mode = MSI_RecordGetInteger(row, 5);
917 comp = get_loaded_component(package, component);
918 if (!comp)
920 ERR("Invalid component: %s\n", debugstr_w(component));
921 return ERROR_FUNCTION_FAILED;
924 if (!comp->Enabled)
926 TRACE("component is disabled\n");
927 return ERROR_SUCCESS;
930 if (!verify_comp_for_removal(comp, install_mode))
932 TRACE("Skipping removal due to missing conditions\n");
933 comp->Action = comp->Installed;
934 return ERROR_SUCCESS;
937 dir = msi_dup_property(package->db, dirprop);
938 if (!dir)
939 return ERROR_OUTOFMEMORY;
941 size = (filename != NULL) ? lstrlenW(filename) : 0;
942 size += lstrlenW(dir) + 2;
943 path = msi_alloc(size * sizeof(WCHAR));
944 if (!path)
946 ret = ERROR_OUTOFMEMORY;
947 goto done;
950 if (filename)
952 lstrcpyW(path, dir);
953 PathAddBackslashW(path);
954 lstrcatW(path, filename);
956 TRACE("Deleting misc file: %s\n", debugstr_w(path));
957 DeleteFileW(path);
959 else
961 TRACE("Removing misc directory: %s\n", debugstr_w(dir));
962 RemoveDirectoryW(dir);
965 done:
966 uirow = MSI_CreateRecord( 9 );
967 MSI_RecordSetStringW( uirow, 1, MSI_RecordGetString(row, 1) );
968 MSI_RecordSetStringW( uirow, 9, dir );
969 ui_actiondata( package, szRemoveFiles, uirow );
970 msiobj_release( &uirow->hdr );
972 msi_free(path);
973 msi_free(dir);
974 return ret;
977 UINT ACTION_RemoveFiles( MSIPACKAGE *package )
979 MSIQUERY *view;
980 MSIFILE *file;
981 UINT r;
983 static const WCHAR query[] = {
984 'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
985 '`','R','e','m','o','v','e','F','i','l','e','`',0};
986 static const WCHAR folder_query[] = {
987 'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
988 '`','C','r','e','a','t','e','F','o','l','d','e','r','`',0};
990 r = MSI_DatabaseOpenViewW(package->db, query, &view);
991 if (r == ERROR_SUCCESS)
993 MSI_IterateRecords(view, NULL, ITERATE_RemoveFiles, package);
994 msiobj_release(&view->hdr);
997 r = MSI_DatabaseOpenViewW(package->db, folder_query, &view);
998 if (r == ERROR_SUCCESS)
999 msiobj_release(&view->hdr);
1001 LIST_FOR_EACH_ENTRY( file, &package->files, MSIFILE, entry )
1003 MSIRECORD *uirow;
1004 LPWSTR dir, p;
1005 VS_FIXEDFILEINFO *ver;
1007 if ( file->state == msifs_installed )
1008 ERR("removing installed file %s\n", debugstr_w(file->TargetPath));
1010 if ( file->Component->ActionRequest != INSTALLSTATE_ABSENT ||
1011 file->Component->Installed == INSTALLSTATE_SOURCE )
1012 continue;
1014 if (!file->Component->Enabled)
1016 TRACE("component is disabled\n");
1017 continue;
1020 if (file->Version)
1022 ver = msi_get_disk_file_version( file->TargetPath );
1023 if (ver && msi_compare_file_versions( ver, file->Version ) > 0)
1025 TRACE("newer version detected, not removing file\n");
1026 msi_free( ver );
1027 continue;
1029 msi_free( ver );
1032 TRACE("removing %s\n", debugstr_w(file->File) );
1033 if (!DeleteFileW( file->TargetPath ))
1035 WARN("failed to delete %s\n", debugstr_w(file->TargetPath));
1037 /* FIXME: check persistence for each directory */
1038 else if (r && (dir = strdupW( file->TargetPath )))
1040 if ((p = strrchrW( dir, '\\' ))) *p = 0;
1041 RemoveDirectoryW( dir );
1042 msi_free( dir );
1044 file->state = msifs_missing;
1046 uirow = MSI_CreateRecord( 9 );
1047 MSI_RecordSetStringW( uirow, 1, file->FileName );
1048 MSI_RecordSetStringW( uirow, 9, file->Component->Directory );
1049 ui_actiondata( package, szRemoveFiles, uirow );
1050 msiobj_release( &uirow->hdr );
1051 /* FIXME: call ui_progress here? */
1054 return ERROR_SUCCESS;