d3dx9: Shader assembler ps_3_0 support.
[wine/wine-gecko.git] / dlls / msi / files.c
blobf66d8154ea7a6d4052d03c364a0bceacfe3d214c
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;
101 else
102 file->Component->Action = INSTALLSTATE_LOCAL;
106 static UINT copy_file(MSIFILE *file, LPWSTR source)
108 BOOL ret;
110 ret = CopyFileW(source, file->TargetPath, FALSE);
111 if (!ret)
112 return GetLastError();
114 SetFileAttributesW(file->TargetPath, FILE_ATTRIBUTE_NORMAL);
116 file->state = msifs_installed;
117 return ERROR_SUCCESS;
120 static UINT copy_install_file(MSIPACKAGE *package, MSIFILE *file, LPWSTR source)
122 UINT gle;
124 TRACE("Copying %s to %s\n", debugstr_w(source), debugstr_w(file->TargetPath));
126 gle = copy_file(file, source);
127 if (gle == ERROR_SUCCESS)
128 return gle;
130 if (gle == ERROR_ALREADY_EXISTS && file->state == msifs_overwrite)
132 TRACE("overwriting existing file\n");
133 return ERROR_SUCCESS;
135 else if (gle == ERROR_ACCESS_DENIED)
137 SetFileAttributesW(file->TargetPath, FILE_ATTRIBUTE_NORMAL);
139 gle = copy_file(file, source);
140 TRACE("Overwriting existing file: %d\n", gle);
142 if (gle == ERROR_SHARING_VIOLATION || gle == ERROR_USER_MAPPED_FILE)
144 WCHAR tmpfileW[MAX_PATH], *pathW, *p;
145 DWORD len;
147 TRACE("file in use, scheduling rename operation\n");
149 GetTempFileNameW(szBackSlash, szMsi, 0, tmpfileW);
150 len = strlenW(file->TargetPath) + strlenW(tmpfileW) + 1;
151 if (!(pathW = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR))))
152 return ERROR_OUTOFMEMORY;
154 strcpyW(pathW, file->TargetPath);
155 if ((p = strrchrW(pathW, '\\'))) *p = 0;
156 strcatW(pathW, tmpfileW);
158 if (CopyFileW(source, pathW, FALSE) &&
159 MoveFileExW(file->TargetPath, NULL, MOVEFILE_DELAY_UNTIL_REBOOT) &&
160 MoveFileExW(pathW, file->TargetPath, MOVEFILE_DELAY_UNTIL_REBOOT))
162 file->state = msifs_installed;
163 package->need_reboot = 1;
164 gle = ERROR_SUCCESS;
166 else
168 gle = GetLastError();
169 WARN("failed to schedule rename operation: %d)\n", gle);
171 HeapFree(GetProcessHeap(), 0, pathW);
174 return gle;
177 static BOOL installfiles_cb(MSIPACKAGE *package, LPCWSTR file, DWORD action,
178 LPWSTR *path, DWORD *attrs, PVOID user)
180 static MSIFILE *f = NULL;
181 MSIMEDIAINFO *mi = user;
183 if (action == MSICABEXTRACT_BEGINEXTRACT)
185 f = get_loaded_file(package, file);
186 if (!f)
188 WARN("unknown file in cabinet (%s)\n", debugstr_w(file));
189 return FALSE;
192 if (f->disk_id != mi->disk_id || (f->state != msifs_missing && f->state != msifs_overwrite))
193 return FALSE;
195 msi_file_update_ui(package, f, szInstallFiles);
197 *path = strdupW(f->TargetPath);
198 *attrs = f->Attributes;
200 else if (action == MSICABEXTRACT_FILEEXTRACTED)
202 f->state = msifs_installed;
203 f = NULL;
206 return TRUE;
210 * ACTION_InstallFiles()
212 * For efficiency, this is done in two passes:
213 * 1) Correct all the TargetPaths and determine what files are to be installed.
214 * 2) Extract Cabinets and copy files.
216 UINT ACTION_InstallFiles(MSIPACKAGE *package)
218 MSIMEDIAINFO *mi;
219 UINT rc = ERROR_SUCCESS;
220 MSIFILE *file;
222 /* increment progress bar each time action data is sent */
223 ui_progress(package,1,1,0,0);
225 schedule_install_files(package);
228 * Despite MSDN specifying that the CreateFolders action
229 * should be called before InstallFiles, some installers don't
230 * do that, and they seem to work correctly. We need to create
231 * directories here to make sure that the files can be copied.
233 msi_create_component_directories( package );
235 mi = msi_alloc_zero( sizeof(MSIMEDIAINFO) );
237 LIST_FOR_EACH_ENTRY( file, &package->files, MSIFILE, entry )
239 if (file->state != msifs_missing && !mi->is_continuous && file->state != msifs_overwrite)
240 continue;
242 if (file->Sequence > mi->last_sequence || mi->is_continuous ||
243 (file->IsCompressed && !mi->is_extracted))
245 MSICABDATA data;
247 rc = ready_media(package, file, mi);
248 if (rc != ERROR_SUCCESS)
250 ERR("Failed to ready media for %s\n", debugstr_w(file->File));
251 break;
254 data.mi = mi;
255 data.package = package;
256 data.cb = installfiles_cb;
257 data.user = mi;
259 if (file->IsCompressed &&
260 !msi_cabextract(package, mi, &data))
262 ERR("Failed to extract cabinet: %s\n", debugstr_w(mi->cabinet));
263 rc = ERROR_INSTALL_FAILURE;
264 break;
268 if (!file->IsCompressed)
270 LPWSTR source = resolve_file_source(package, file);
272 TRACE("file paths %s to %s\n", debugstr_w(source),
273 debugstr_w(file->TargetPath));
275 msi_file_update_ui(package, file, szInstallFiles);
276 rc = copy_install_file(package, file, source);
277 if (rc != ERROR_SUCCESS)
279 ERR("Failed to copy %s to %s (%d)\n", debugstr_w(source),
280 debugstr_w(file->TargetPath), rc);
281 rc = ERROR_INSTALL_FAILURE;
282 msi_free(source);
283 break;
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 break;
296 msi_free_media_info(mi);
297 return rc;
300 #define is_dot_dir(x) ((x[0] == '.') && ((x[1] == 0) || ((x[1] == '.') && (x[2] == 0))))
302 typedef struct
304 struct list entry;
305 LPWSTR sourcename;
306 LPWSTR destname;
307 LPWSTR source;
308 LPWSTR dest;
309 } FILE_LIST;
311 static BOOL msi_move_file(LPCWSTR source, LPCWSTR dest, int options)
313 BOOL ret;
315 if (GetFileAttributesW(source) == FILE_ATTRIBUTE_DIRECTORY ||
316 GetFileAttributesW(dest) == FILE_ATTRIBUTE_DIRECTORY)
318 WARN("Source or dest is directory, not moving\n");
319 return FALSE;
322 if (options == msidbMoveFileOptionsMove)
324 TRACE("moving %s -> %s\n", debugstr_w(source), debugstr_w(dest));
325 ret = MoveFileExW(source, dest, MOVEFILE_REPLACE_EXISTING);
326 if (!ret)
328 WARN("MoveFile failed: %d\n", GetLastError());
329 return FALSE;
332 else
334 TRACE("copying %s -> %s\n", debugstr_w(source), debugstr_w(dest));
335 ret = CopyFileW(source, dest, FALSE);
336 if (!ret)
338 WARN("CopyFile failed: %d\n", GetLastError());
339 return FALSE;
343 return TRUE;
346 static LPWSTR wildcard_to_file(LPWSTR wildcard, LPWSTR filename)
348 LPWSTR path, ptr;
349 DWORD dirlen, pathlen;
351 ptr = strrchrW(wildcard, '\\');
352 dirlen = ptr - wildcard + 1;
354 pathlen = dirlen + lstrlenW(filename) + 1;
355 path = msi_alloc(pathlen * sizeof(WCHAR));
357 lstrcpynW(path, wildcard, dirlen + 1);
358 lstrcatW(path, filename);
360 return path;
363 static void free_file_entry(FILE_LIST *file)
365 msi_free(file->source);
366 msi_free(file->dest);
367 msi_free(file);
370 static void free_list(FILE_LIST *list)
372 while (!list_empty(&list->entry))
374 FILE_LIST *file = LIST_ENTRY(list_head(&list->entry), FILE_LIST, entry);
376 list_remove(&file->entry);
377 free_file_entry(file);
381 static BOOL add_wildcard(FILE_LIST *files, LPWSTR source, LPWSTR dest)
383 FILE_LIST *new, *file;
384 LPWSTR ptr, filename;
385 DWORD size;
387 new = msi_alloc_zero(sizeof(FILE_LIST));
388 if (!new)
389 return FALSE;
391 new->source = strdupW(source);
392 ptr = strrchrW(dest, '\\') + 1;
393 filename = strrchrW(new->source, '\\') + 1;
395 new->sourcename = filename;
397 if (*ptr)
398 new->destname = ptr;
399 else
400 new->destname = new->sourcename;
402 size = (ptr - dest) + lstrlenW(filename) + 1;
403 new->dest = msi_alloc(size * sizeof(WCHAR));
404 if (!new->dest)
406 free_file_entry(new);
407 return FALSE;
410 lstrcpynW(new->dest, dest, ptr - dest + 1);
411 lstrcatW(new->dest, filename);
413 if (list_empty(&files->entry))
415 list_add_head(&files->entry, &new->entry);
416 return TRUE;
419 LIST_FOR_EACH_ENTRY(file, &files->entry, FILE_LIST, entry)
421 if (lstrcmpW(source, file->source) < 0)
423 list_add_before(&file->entry, &new->entry);
424 return TRUE;
428 list_add_after(&file->entry, &new->entry);
429 return TRUE;
432 static BOOL move_files_wildcard(LPWSTR source, LPWSTR dest, int options)
434 WIN32_FIND_DATAW wfd;
435 HANDLE hfile;
436 LPWSTR path;
437 BOOL res;
438 FILE_LIST files, *file;
439 DWORD size;
441 hfile = FindFirstFileW(source, &wfd);
442 if (hfile == INVALID_HANDLE_VALUE) return FALSE;
444 list_init(&files.entry);
446 for (res = TRUE; res; res = FindNextFileW(hfile, &wfd))
448 if (is_dot_dir(wfd.cFileName)) continue;
450 path = wildcard_to_file(source, wfd.cFileName);
451 if (!path)
453 res = FALSE;
454 goto done;
457 add_wildcard(&files, path, dest);
458 msi_free(path);
461 /* no files match the wildcard */
462 if (list_empty(&files.entry))
463 goto done;
465 /* only the first wildcard match gets renamed to dest */
466 file = LIST_ENTRY(list_head(&files.entry), FILE_LIST, entry);
467 size = (strrchrW(file->dest, '\\') - file->dest) + lstrlenW(file->destname) + 2;
468 file->dest = msi_realloc(file->dest, size * sizeof(WCHAR));
469 if (!file->dest)
471 res = FALSE;
472 goto done;
475 /* file->dest may be shorter after the reallocation, so add a NULL
476 * terminator. This is needed for the call to strrchrW, as there will no
477 * longer be a NULL terminator within the bounds of the allocation in this case.
479 file->dest[size - 1] = '\0';
480 lstrcpyW(strrchrW(file->dest, '\\') + 1, file->destname);
482 while (!list_empty(&files.entry))
484 file = LIST_ENTRY(list_head(&files.entry), FILE_LIST, entry);
486 msi_move_file(file->source, file->dest, options);
488 list_remove(&file->entry);
489 free_file_entry(file);
492 res = TRUE;
494 done:
495 free_list(&files);
496 FindClose(hfile);
497 return res;
500 static UINT ITERATE_MoveFiles( MSIRECORD *rec, LPVOID param )
502 MSIPACKAGE *package = param;
503 MSIRECORD *uirow;
504 MSICOMPONENT *comp;
505 LPCWSTR sourcename, component;
506 LPWSTR sourcedir, destname = NULL, destdir = NULL, source = NULL, dest = NULL;
507 int options;
508 DWORD size;
509 BOOL ret, wildcards;
511 component = MSI_RecordGetString(rec, 2);
512 comp = get_loaded_component(package, component);
513 if (!comp)
514 return ERROR_SUCCESS;
516 if (comp->ActionRequest != INSTALLSTATE_LOCAL && comp->ActionRequest != INSTALLSTATE_SOURCE)
518 TRACE("Component not scheduled for installation: %s\n", debugstr_w(component));
519 comp->Action = comp->Installed;
520 return ERROR_SUCCESS;
522 comp->Action = comp->ActionRequest;
524 sourcename = MSI_RecordGetString(rec, 3);
525 options = MSI_RecordGetInteger(rec, 7);
527 sourcedir = msi_dup_property(package->db, MSI_RecordGetString(rec, 5));
528 if (!sourcedir)
529 goto done;
531 destdir = msi_dup_property(package->db, MSI_RecordGetString(rec, 6));
532 if (!destdir)
533 goto done;
535 if (!sourcename)
537 if (GetFileAttributesW(sourcedir) == INVALID_FILE_ATTRIBUTES)
538 goto done;
540 source = strdupW(sourcedir);
541 if (!source)
542 goto done;
544 else
546 size = lstrlenW(sourcedir) + lstrlenW(sourcename) + 2;
547 source = msi_alloc(size * sizeof(WCHAR));
548 if (!source)
549 goto done;
551 lstrcpyW(source, sourcedir);
552 if (source[lstrlenW(source) - 1] != '\\')
553 lstrcatW(source, szBackSlash);
554 lstrcatW(source, sourcename);
557 wildcards = strchrW(source, '*') || strchrW(source, '?');
559 if (MSI_RecordIsNull(rec, 4))
561 if (!wildcards)
563 destname = strdupW(sourcename);
564 if (!destname)
565 goto done;
568 else
570 destname = strdupW(MSI_RecordGetString(rec, 4));
571 if (destname)
572 reduce_to_longfilename(destname);
575 size = 0;
576 if (destname)
577 size = lstrlenW(destname);
579 size += lstrlenW(destdir) + 2;
580 dest = msi_alloc(size * sizeof(WCHAR));
581 if (!dest)
582 goto done;
584 lstrcpyW(dest, destdir);
585 if (dest[lstrlenW(dest) - 1] != '\\')
586 lstrcatW(dest, szBackSlash);
588 if (destname)
589 lstrcatW(dest, destname);
591 if (GetFileAttributesW(destdir) == INVALID_FILE_ATTRIBUTES)
593 ret = CreateDirectoryW(destdir, NULL);
594 if (!ret)
596 WARN("CreateDirectory failed: %d\n", GetLastError());
597 goto done;
601 if (!wildcards)
602 msi_move_file(source, dest, options);
603 else
604 move_files_wildcard(source, dest, options);
606 done:
607 uirow = MSI_CreateRecord( 9 );
608 MSI_RecordSetStringW( uirow, 1, MSI_RecordGetString(rec, 1) );
609 MSI_RecordSetInteger( uirow, 6, 1 ); /* FIXME */
610 MSI_RecordSetStringW( uirow, 9, destdir );
611 ui_actiondata( package, szMoveFiles, uirow );
612 msiobj_release( &uirow->hdr );
614 msi_free(sourcedir);
615 msi_free(destdir);
616 msi_free(destname);
617 msi_free(source);
618 msi_free(dest);
620 return ERROR_SUCCESS;
623 UINT ACTION_MoveFiles( MSIPACKAGE *package )
625 UINT rc;
626 MSIQUERY *view;
628 static const WCHAR ExecSeqQuery[] =
629 {'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
630 '`','M','o','v','e','F','i','l','e','`',0};
632 rc = MSI_DatabaseOpenViewW(package->db, ExecSeqQuery, &view);
633 if (rc != ERROR_SUCCESS)
634 return ERROR_SUCCESS;
636 rc = MSI_IterateRecords(view, NULL, ITERATE_MoveFiles, package);
637 msiobj_release(&view->hdr);
639 return rc;
642 static WCHAR *get_duplicate_filename( MSIPACKAGE *package, MSIRECORD *row, const WCHAR *file_key, const WCHAR *src )
644 DWORD len;
645 WCHAR *dst_name, *dst_path, *dst;
647 if (MSI_RecordIsNull( row, 4 ))
649 len = strlenW( src ) + 1;
650 if (!(dst_name = msi_alloc( len * sizeof(WCHAR)))) return NULL;
651 strcpyW( dst_name, strrchrW( src, '\\' ) + 1 );
653 else
655 MSI_RecordGetStringW( row, 4, NULL, &len );
656 if (!(dst_name = msi_alloc( ++len * sizeof(WCHAR) ))) return NULL;
657 MSI_RecordGetStringW( row, 4, dst_name, &len );
658 reduce_to_longfilename( dst_name );
661 if (MSI_RecordIsNull( row, 5 ))
663 WCHAR *p;
664 dst_path = strdupW( src );
665 p = strrchrW( dst_path, '\\' );
666 if (p) *p = 0;
668 else
670 const WCHAR *dst_key = MSI_RecordGetString( row, 5 );
672 dst_path = resolve_folder( package, dst_key, FALSE, FALSE, TRUE, NULL );
673 if (!dst_path)
675 /* try a property */
676 dst_path = msi_dup_property( package->db, dst_key );
677 if (!dst_path)
679 FIXME("Unable to get destination folder, try AppSearch properties\n");
680 msi_free( dst_name );
681 return NULL;
686 dst = build_directory_name( 2, dst_path, dst_name );
687 create_full_pathW( dst_path );
689 msi_free( dst_name );
690 msi_free( dst_path );
691 return dst;
694 static UINT ITERATE_DuplicateFiles(MSIRECORD *row, LPVOID param)
696 MSIPACKAGE *package = param;
697 LPWSTR dest;
698 LPCWSTR file_key, component;
699 MSICOMPONENT *comp;
700 MSIRECORD *uirow;
701 MSIFILE *file;
703 component = MSI_RecordGetString(row,2);
704 comp = get_loaded_component(package,component);
705 if (!comp)
706 return ERROR_SUCCESS;
708 if (comp->ActionRequest != INSTALLSTATE_LOCAL)
710 TRACE("Component not scheduled for installation %s\n", debugstr_w(component));
711 comp->Action = comp->Installed;
712 return ERROR_SUCCESS;
714 comp->Action = INSTALLSTATE_LOCAL;
716 file_key = MSI_RecordGetString(row,3);
717 if (!file_key)
719 ERR("Unable to get file key\n");
720 return ERROR_FUNCTION_FAILED;
723 file = get_loaded_file( package, file_key );
724 if (!file)
726 ERR("Original file unknown %s\n", debugstr_w(file_key));
727 return ERROR_SUCCESS;
730 dest = get_duplicate_filename( package, row, file_key, file->TargetPath );
731 if (!dest)
733 WARN("Unable to get duplicate filename\n");
734 return ERROR_SUCCESS;
737 TRACE("Duplicating file %s to %s\n", debugstr_w(file->TargetPath), debugstr_w(dest));
739 if (!CopyFileW( file->TargetPath, dest, TRUE ))
741 WARN("Failed to copy file %s -> %s (%u)\n",
742 debugstr_w(file->TargetPath), debugstr_w(dest), GetLastError());
745 FIXME("We should track these duplicate files as well\n");
747 uirow = MSI_CreateRecord( 9 );
748 MSI_RecordSetStringW( uirow, 1, MSI_RecordGetString( row, 1 ) );
749 MSI_RecordSetInteger( uirow, 6, file->FileSize );
750 MSI_RecordSetStringW( uirow, 9, MSI_RecordGetString( row, 5 ) );
751 ui_actiondata( package, szDuplicateFiles, uirow );
752 msiobj_release( &uirow->hdr );
754 msi_free(dest);
755 return ERROR_SUCCESS;
758 UINT ACTION_DuplicateFiles(MSIPACKAGE *package)
760 UINT rc;
761 MSIQUERY * view;
762 static const WCHAR ExecSeqQuery[] =
763 {'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
764 '`','D','u','p','l','i','c','a','t','e','F','i','l','e','`',0};
766 rc = MSI_DatabaseOpenViewW(package->db, ExecSeqQuery, &view);
767 if (rc != ERROR_SUCCESS)
768 return ERROR_SUCCESS;
770 rc = MSI_IterateRecords(view, NULL, ITERATE_DuplicateFiles, package);
771 msiobj_release(&view->hdr);
773 return rc;
776 static UINT ITERATE_RemoveDuplicateFiles( MSIRECORD *row, LPVOID param )
778 MSIPACKAGE *package = param;
779 LPWSTR dest;
780 LPCWSTR file_key, component;
781 MSICOMPONENT *comp;
782 MSIRECORD *uirow;
783 MSIFILE *file;
785 component = MSI_RecordGetString( row, 2 );
786 comp = get_loaded_component( package, component );
787 if (!comp)
788 return ERROR_SUCCESS;
790 if (comp->ActionRequest != INSTALLSTATE_ABSENT)
792 TRACE("Component not scheduled for removal %s\n", debugstr_w(component));
793 comp->Action = comp->Installed;
794 return ERROR_SUCCESS;
796 comp->Action = INSTALLSTATE_ABSENT;
798 file_key = MSI_RecordGetString( row, 3 );
799 if (!file_key)
801 ERR("Unable to get file key\n");
802 return ERROR_FUNCTION_FAILED;
805 file = get_loaded_file( package, file_key );
806 if (!file)
808 ERR("Original file unknown %s\n", debugstr_w(file_key));
809 return ERROR_SUCCESS;
812 dest = get_duplicate_filename( package, row, file_key, file->TargetPath );
813 if (!dest)
815 WARN("Unable to get duplicate filename\n");
816 return ERROR_SUCCESS;
819 TRACE("Removing duplicate %s of %s\n", debugstr_w(dest), debugstr_w(file->TargetPath));
821 if (!DeleteFileW( dest ))
823 WARN("Failed to delete duplicate file %s (%u)\n", debugstr_w(dest), GetLastError());
826 uirow = MSI_CreateRecord( 9 );
827 MSI_RecordSetStringW( uirow, 1, MSI_RecordGetString( row, 1 ) );
828 MSI_RecordSetStringW( uirow, 9, MSI_RecordGetString( row, 5 ) );
829 ui_actiondata( package, szRemoveDuplicateFiles, uirow );
830 msiobj_release( &uirow->hdr );
832 msi_free(dest);
833 return ERROR_SUCCESS;
836 UINT ACTION_RemoveDuplicateFiles( MSIPACKAGE *package )
838 UINT rc;
839 MSIQUERY *view;
840 static const WCHAR query[] =
841 {'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
842 '`','D','u','p','l','i','c','a','t','e','F','i','l','e','`',0};
844 rc = MSI_DatabaseOpenViewW( package->db, query, &view );
845 if (rc != ERROR_SUCCESS)
846 return ERROR_SUCCESS;
848 rc = MSI_IterateRecords( view, NULL, ITERATE_RemoveDuplicateFiles, package );
849 msiobj_release( &view->hdr );
851 return rc;
854 static BOOL verify_comp_for_removal(MSICOMPONENT *comp, UINT install_mode)
856 INSTALLSTATE request = comp->ActionRequest;
858 if (request == INSTALLSTATE_UNKNOWN)
859 return FALSE;
861 if (install_mode == msidbRemoveFileInstallModeOnInstall &&
862 (request == INSTALLSTATE_LOCAL || request == INSTALLSTATE_SOURCE))
863 return TRUE;
865 if (request == INSTALLSTATE_ABSENT)
867 if (!comp->ComponentId)
868 return FALSE;
870 if (install_mode == msidbRemoveFileInstallModeOnRemove)
871 return TRUE;
874 if (install_mode == msidbRemoveFileInstallModeOnBoth)
875 return TRUE;
877 return FALSE;
880 static UINT ITERATE_RemoveFiles(MSIRECORD *row, LPVOID param)
882 MSIPACKAGE *package = param;
883 MSICOMPONENT *comp;
884 MSIRECORD *uirow;
885 LPCWSTR component, filename, dirprop;
886 UINT install_mode;
887 LPWSTR dir = NULL, path = NULL;
888 DWORD size;
889 UINT ret = ERROR_SUCCESS;
891 component = MSI_RecordGetString(row, 2);
892 filename = MSI_RecordGetString(row, 3);
893 dirprop = MSI_RecordGetString(row, 4);
894 install_mode = MSI_RecordGetInteger(row, 5);
896 comp = get_loaded_component(package, component);
897 if (!comp)
899 ERR("Invalid component: %s\n", debugstr_w(component));
900 return ERROR_FUNCTION_FAILED;
903 if (!verify_comp_for_removal(comp, install_mode))
905 TRACE("Skipping removal due to missing conditions\n");
906 comp->Action = comp->Installed;
907 return ERROR_SUCCESS;
910 dir = msi_dup_property(package->db, dirprop);
911 if (!dir)
912 return ERROR_OUTOFMEMORY;
914 size = (filename != NULL) ? lstrlenW(filename) : 0;
915 size += lstrlenW(dir) + 2;
916 path = msi_alloc(size * sizeof(WCHAR));
917 if (!path)
919 ret = ERROR_OUTOFMEMORY;
920 goto done;
923 if (filename)
925 lstrcpyW(path, dir);
926 PathAddBackslashW(path);
927 lstrcatW(path, filename);
929 TRACE("Deleting misc file: %s\n", debugstr_w(path));
930 DeleteFileW(path);
932 else
934 TRACE("Removing misc directory: %s\n", debugstr_w(dir));
935 RemoveDirectoryW(dir);
938 done:
939 uirow = MSI_CreateRecord( 9 );
940 MSI_RecordSetStringW( uirow, 1, MSI_RecordGetString(row, 1) );
941 MSI_RecordSetStringW( uirow, 9, dir );
942 ui_actiondata( package, szRemoveFiles, uirow );
943 msiobj_release( &uirow->hdr );
945 msi_free(path);
946 msi_free(dir);
947 return ret;
950 UINT ACTION_RemoveFiles( MSIPACKAGE *package )
952 MSIQUERY *view;
953 MSIFILE *file;
954 UINT r;
956 static const WCHAR query[] = {
957 'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
958 '`','R','e','m','o','v','e','F','i','l','e','`',0};
959 static const WCHAR folder_query[] = {
960 'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
961 '`','C','r','e','a','t','e','F','o','l','d','e','r','`',0};
963 r = MSI_DatabaseOpenViewW(package->db, query, &view);
964 if (r == ERROR_SUCCESS)
966 MSI_IterateRecords(view, NULL, ITERATE_RemoveFiles, package);
967 msiobj_release(&view->hdr);
970 r = MSI_DatabaseOpenViewW(package->db, folder_query, &view);
971 if (r == ERROR_SUCCESS)
972 msiobj_release(&view->hdr);
974 LIST_FOR_EACH_ENTRY( file, &package->files, MSIFILE, entry )
976 MSIRECORD *uirow;
977 LPWSTR dir, uipath, p;
979 if ( file->state == msifs_installed )
980 ERR("removing installed file %s\n", debugstr_w(file->TargetPath));
982 if ( file->Component->ActionRequest != INSTALLSTATE_ABSENT ||
983 file->Component->Installed == INSTALLSTATE_SOURCE )
984 continue;
986 /* don't remove a file if the old file
987 * is strictly newer than the version to be installed
989 if ( msi_compare_file_version( file ) < 0 )
990 continue;
992 TRACE("removing %s\n", debugstr_w(file->File) );
993 if (!DeleteFileW( file->TargetPath ))
995 WARN("failed to delete %s\n", debugstr_w(file->TargetPath));
997 /* FIXME: check persistence for each directory */
998 else if (r && (dir = strdupW( file->TargetPath )))
1000 if ((p = strrchrW( dir, '\\' ))) *p = 0;
1001 RemoveDirectoryW( dir );
1002 msi_free( dir );
1004 file->state = msifs_missing;
1006 /* the UI chunk */
1007 uirow = MSI_CreateRecord( 9 );
1008 MSI_RecordSetStringW( uirow, 1, file->FileName );
1009 uipath = strdupW( file->TargetPath );
1010 p = strrchrW(uipath,'\\');
1011 if (p)
1012 p[1]=0;
1013 MSI_RecordSetStringW( uirow, 9, uipath);
1014 ui_actiondata( package, szRemoveFiles, uirow);
1015 msiobj_release( &uirow->hdr );
1016 msi_free( uipath );
1017 /* FIXME: call ui_progress here? */
1020 return ERROR_SUCCESS;