wined3d: read_from_framebuffer_texture() isn't suitable for readback of onscreen...
[wine/testsucceed.git] / dlls / msi / files.c
blob79d78143bb4c74f5f5fd8b1194b8f8ba72141866
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)
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 msi_create_directory(package, f->Component->Directory);
191 *path = strdupW(f->TargetPath);
192 *attrs = f->Attributes;
194 else if (action == MSICABEXTRACT_FILEEXTRACTED)
196 f->state = msifs_installed;
197 f = NULL;
200 return TRUE;
204 * ACTION_InstallFiles()
206 * For efficiency, this is done in two passes:
207 * 1) Correct all the TargetPaths and determine what files are to be installed.
208 * 2) Extract Cabinets and copy files.
210 UINT ACTION_InstallFiles(MSIPACKAGE *package)
212 MSIMEDIAINFO *mi;
213 UINT rc = ERROR_SUCCESS;
214 MSIFILE *file;
216 /* increment progress bar each time action data is sent */
217 ui_progress(package,1,1,0,0);
219 schedule_install_files(package);
221 mi = msi_alloc_zero( sizeof(MSIMEDIAINFO) );
223 LIST_FOR_EACH_ENTRY( file, &package->files, MSIFILE, entry )
225 if (file->state != msifs_missing && !mi->is_continuous && file->state != msifs_overwrite)
226 continue;
228 if (file->Sequence > mi->last_sequence || mi->is_continuous ||
229 (file->IsCompressed && !mi->is_extracted))
231 MSICABDATA data;
233 rc = ready_media(package, file, mi);
234 if (rc != ERROR_SUCCESS)
236 ERR("Failed to ready media for %s\n", debugstr_w(file->File));
237 break;
240 data.mi = mi;
241 data.package = package;
242 data.cb = installfiles_cb;
243 data.user = (PVOID)(UINT_PTR)mi->disk_id;
245 if (file->IsCompressed &&
246 !msi_cabextract(package, mi, &data))
248 ERR("Failed to extract cabinet: %s\n", debugstr_w(mi->cabinet));
249 rc = ERROR_INSTALL_FAILURE;
250 break;
254 if (!file->IsCompressed)
256 LPWSTR source = resolve_file_source(package, file);
258 TRACE("file paths %s to %s\n", debugstr_w(source),
259 debugstr_w(file->TargetPath));
261 msi_file_update_ui(package, file, szInstallFiles);
262 msi_create_directory(package, file->Component->Directory);
264 rc = copy_install_file(package, file, source);
265 if (rc != ERROR_SUCCESS)
267 ERR("Failed to copy %s to %s (%d)\n", debugstr_w(source),
268 debugstr_w(file->TargetPath), rc);
269 rc = ERROR_INSTALL_FAILURE;
270 msi_free(source);
271 break;
274 msi_free(source);
276 else if (file->state != msifs_installed)
278 ERR("compressed file wasn't installed (%s)\n", debugstr_w(file->TargetPath));
279 rc = ERROR_INSTALL_FAILURE;
280 break;
284 msi_free_media_info(mi);
285 return rc;
288 #define is_dot_dir(x) ((x[0] == '.') && ((x[1] == 0) || ((x[1] == '.') && (x[2] == 0))))
290 typedef struct
292 struct list entry;
293 LPWSTR sourcename;
294 LPWSTR destname;
295 LPWSTR source;
296 LPWSTR dest;
297 } FILE_LIST;
299 static BOOL msi_move_file(LPCWSTR source, LPCWSTR dest, int options)
301 BOOL ret;
303 if (GetFileAttributesW(source) == FILE_ATTRIBUTE_DIRECTORY ||
304 GetFileAttributesW(dest) == FILE_ATTRIBUTE_DIRECTORY)
306 WARN("Source or dest is directory, not moving\n");
307 return FALSE;
310 if (options == msidbMoveFileOptionsMove)
312 TRACE("moving %s -> %s\n", debugstr_w(source), debugstr_w(dest));
313 ret = MoveFileExW(source, dest, MOVEFILE_REPLACE_EXISTING);
314 if (!ret)
316 WARN("MoveFile failed: %d\n", GetLastError());
317 return FALSE;
320 else
322 TRACE("copying %s -> %s\n", debugstr_w(source), debugstr_w(dest));
323 ret = CopyFileW(source, dest, FALSE);
324 if (!ret)
326 WARN("CopyFile failed: %d\n", GetLastError());
327 return FALSE;
331 return TRUE;
334 static LPWSTR wildcard_to_file(LPWSTR wildcard, LPWSTR filename)
336 LPWSTR path, ptr;
337 DWORD dirlen, pathlen;
339 ptr = strrchrW(wildcard, '\\');
340 dirlen = ptr - wildcard + 1;
342 pathlen = dirlen + lstrlenW(filename) + 1;
343 path = msi_alloc(pathlen * sizeof(WCHAR));
345 lstrcpynW(path, wildcard, dirlen + 1);
346 lstrcatW(path, filename);
348 return path;
351 static void free_file_entry(FILE_LIST *file)
353 msi_free(file->source);
354 msi_free(file->dest);
355 msi_free(file);
358 static void free_list(FILE_LIST *list)
360 while (!list_empty(&list->entry))
362 FILE_LIST *file = LIST_ENTRY(list_head(&list->entry), FILE_LIST, entry);
364 list_remove(&file->entry);
365 free_file_entry(file);
369 static BOOL add_wildcard(FILE_LIST *files, LPWSTR source, LPWSTR dest)
371 FILE_LIST *new, *file;
372 LPWSTR ptr, filename;
373 DWORD size;
375 new = msi_alloc_zero(sizeof(FILE_LIST));
376 if (!new)
377 return FALSE;
379 new->source = strdupW(source);
380 ptr = strrchrW(dest, '\\') + 1;
381 filename = strrchrW(new->source, '\\') + 1;
383 new->sourcename = filename;
385 if (*ptr)
386 new->destname = ptr;
387 else
388 new->destname = new->sourcename;
390 size = (ptr - dest) + lstrlenW(filename) + 1;
391 new->dest = msi_alloc(size * sizeof(WCHAR));
392 if (!new->dest)
394 free_file_entry(new);
395 return FALSE;
398 lstrcpynW(new->dest, dest, ptr - dest + 1);
399 lstrcatW(new->dest, filename);
401 if (list_empty(&files->entry))
403 list_add_head(&files->entry, &new->entry);
404 return TRUE;
407 LIST_FOR_EACH_ENTRY(file, &files->entry, FILE_LIST, entry)
409 if (lstrcmpW(source, file->source) < 0)
411 list_add_before(&file->entry, &new->entry);
412 return TRUE;
416 list_add_after(&file->entry, &new->entry);
417 return TRUE;
420 static BOOL move_files_wildcard(LPWSTR source, LPWSTR dest, int options)
422 WIN32_FIND_DATAW wfd;
423 HANDLE hfile;
424 LPWSTR path;
425 BOOL res;
426 FILE_LIST files, *file;
427 DWORD size;
429 hfile = FindFirstFileW(source, &wfd);
430 if (hfile == INVALID_HANDLE_VALUE) return FALSE;
432 list_init(&files.entry);
434 for (res = TRUE; res; res = FindNextFileW(hfile, &wfd))
436 if (is_dot_dir(wfd.cFileName)) continue;
438 path = wildcard_to_file(source, wfd.cFileName);
439 if (!path)
441 res = FALSE;
442 goto done;
445 add_wildcard(&files, path, dest);
446 msi_free(path);
449 /* no files match the wildcard */
450 if (list_empty(&files.entry))
451 goto done;
453 /* only the first wildcard match gets renamed to dest */
454 file = LIST_ENTRY(list_head(&files.entry), FILE_LIST, entry);
455 size = (strrchrW(file->dest, '\\') - file->dest) + lstrlenW(file->destname) + 2;
456 file->dest = msi_realloc(file->dest, size * sizeof(WCHAR));
457 if (!file->dest)
459 res = FALSE;
460 goto done;
463 /* file->dest may be shorter after the reallocation, so add a NULL
464 * terminator. This is needed for the call to strrchrW, as there will no
465 * longer be a NULL terminator within the bounds of the allocation in this case.
467 file->dest[size - 1] = '\0';
468 lstrcpyW(strrchrW(file->dest, '\\') + 1, file->destname);
470 while (!list_empty(&files.entry))
472 file = LIST_ENTRY(list_head(&files.entry), FILE_LIST, entry);
474 msi_move_file(file->source, file->dest, options);
476 list_remove(&file->entry);
477 free_file_entry(file);
480 res = TRUE;
482 done:
483 free_list(&files);
484 FindClose(hfile);
485 return res;
488 static UINT ITERATE_MoveFiles( MSIRECORD *rec, LPVOID param )
490 MSIPACKAGE *package = param;
491 MSIRECORD *uirow;
492 MSICOMPONENT *comp;
493 LPCWSTR sourcename, component;
494 LPWSTR sourcedir, destname = NULL, destdir = NULL, source = NULL, dest = NULL;
495 int options;
496 DWORD size;
497 BOOL ret, wildcards;
499 component = MSI_RecordGetString(rec, 2);
500 comp = get_loaded_component(package, component);
501 if (!comp)
502 return ERROR_SUCCESS;
504 if (comp->ActionRequest != INSTALLSTATE_LOCAL && comp->ActionRequest != INSTALLSTATE_SOURCE)
506 TRACE("Component not scheduled for installation: %s\n", debugstr_w(component));
507 comp->Action = comp->Installed;
508 return ERROR_SUCCESS;
510 comp->Action = comp->ActionRequest;
512 sourcename = MSI_RecordGetString(rec, 3);
513 options = MSI_RecordGetInteger(rec, 7);
515 sourcedir = msi_dup_property(package->db, MSI_RecordGetString(rec, 5));
516 if (!sourcedir)
517 goto done;
519 destdir = msi_dup_property(package->db, MSI_RecordGetString(rec, 6));
520 if (!destdir)
521 goto done;
523 if (!sourcename)
525 if (GetFileAttributesW(sourcedir) == INVALID_FILE_ATTRIBUTES)
526 goto done;
528 source = strdupW(sourcedir);
529 if (!source)
530 goto done;
532 else
534 size = lstrlenW(sourcedir) + lstrlenW(sourcename) + 2;
535 source = msi_alloc(size * sizeof(WCHAR));
536 if (!source)
537 goto done;
539 lstrcpyW(source, sourcedir);
540 if (source[lstrlenW(source) - 1] != '\\')
541 lstrcatW(source, szBackSlash);
542 lstrcatW(source, sourcename);
545 wildcards = strchrW(source, '*') || strchrW(source, '?');
547 if (MSI_RecordIsNull(rec, 4))
549 if (!wildcards)
551 destname = strdupW(sourcename);
552 if (!destname)
553 goto done;
556 else
558 destname = strdupW(MSI_RecordGetString(rec, 4));
559 if (destname)
560 reduce_to_longfilename(destname);
563 size = 0;
564 if (destname)
565 size = lstrlenW(destname);
567 size += lstrlenW(destdir) + 2;
568 dest = msi_alloc(size * sizeof(WCHAR));
569 if (!dest)
570 goto done;
572 lstrcpyW(dest, destdir);
573 if (dest[lstrlenW(dest) - 1] != '\\')
574 lstrcatW(dest, szBackSlash);
576 if (destname)
577 lstrcatW(dest, destname);
579 if (GetFileAttributesW(destdir) == INVALID_FILE_ATTRIBUTES)
581 ret = CreateDirectoryW(destdir, NULL);
582 if (!ret)
584 WARN("CreateDirectory failed: %d\n", GetLastError());
585 goto done;
589 if (!wildcards)
590 msi_move_file(source, dest, options);
591 else
592 move_files_wildcard(source, dest, options);
594 done:
595 uirow = MSI_CreateRecord( 9 );
596 MSI_RecordSetStringW( uirow, 1, MSI_RecordGetString(rec, 1) );
597 MSI_RecordSetInteger( uirow, 6, 1 ); /* FIXME */
598 MSI_RecordSetStringW( uirow, 9, destdir );
599 ui_actiondata( package, szMoveFiles, uirow );
600 msiobj_release( &uirow->hdr );
602 msi_free(sourcedir);
603 msi_free(destdir);
604 msi_free(destname);
605 msi_free(source);
606 msi_free(dest);
608 return ERROR_SUCCESS;
611 UINT ACTION_MoveFiles( MSIPACKAGE *package )
613 UINT rc;
614 MSIQUERY *view;
616 static const WCHAR ExecSeqQuery[] =
617 {'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
618 '`','M','o','v','e','F','i','l','e','`',0};
620 rc = MSI_DatabaseOpenViewW(package->db, ExecSeqQuery, &view);
621 if (rc != ERROR_SUCCESS)
622 return ERROR_SUCCESS;
624 rc = MSI_IterateRecords(view, NULL, ITERATE_MoveFiles, package);
625 msiobj_release(&view->hdr);
627 return rc;
630 static WCHAR *get_duplicate_filename( MSIPACKAGE *package, MSIRECORD *row, const WCHAR *file_key, const WCHAR *src )
632 DWORD len;
633 WCHAR *dst_name, *dst_path, *dst;
635 if (MSI_RecordIsNull( row, 4 ))
637 len = strlenW( src ) + 1;
638 if (!(dst_name = msi_alloc( len * sizeof(WCHAR)))) return NULL;
639 strcpyW( dst_name, strrchrW( src, '\\' ) + 1 );
641 else
643 MSI_RecordGetStringW( row, 4, NULL, &len );
644 if (!(dst_name = msi_alloc( ++len * sizeof(WCHAR) ))) return NULL;
645 MSI_RecordGetStringW( row, 4, dst_name, &len );
646 reduce_to_longfilename( dst_name );
649 if (MSI_RecordIsNull( row, 5 ))
651 WCHAR *p;
652 dst_path = strdupW( src );
653 p = strrchrW( dst_path, '\\' );
654 if (p) *p = 0;
656 else
658 const WCHAR *dst_key = MSI_RecordGetString( row, 5 );
660 dst_path = resolve_folder( package, dst_key, FALSE, FALSE, TRUE, NULL );
661 if (!dst_path)
663 /* try a property */
664 dst_path = msi_dup_property( package->db, dst_key );
665 if (!dst_path)
667 FIXME("Unable to get destination folder, try AppSearch properties\n");
668 msi_free( dst_name );
669 return NULL;
674 dst = build_directory_name( 2, dst_path, dst_name );
675 create_full_pathW( dst_path );
677 msi_free( dst_name );
678 msi_free( dst_path );
679 return dst;
682 static UINT ITERATE_DuplicateFiles(MSIRECORD *row, LPVOID param)
684 MSIPACKAGE *package = param;
685 LPWSTR dest;
686 LPCWSTR file_key, component;
687 MSICOMPONENT *comp;
688 MSIRECORD *uirow;
689 MSIFILE *file;
691 component = MSI_RecordGetString(row,2);
692 comp = get_loaded_component(package,component);
693 if (!comp)
694 return ERROR_SUCCESS;
696 if (comp->ActionRequest != INSTALLSTATE_LOCAL)
698 TRACE("Component not scheduled for installation %s\n", debugstr_w(component));
699 comp->Action = comp->Installed;
700 return ERROR_SUCCESS;
702 comp->Action = INSTALLSTATE_LOCAL;
704 file_key = MSI_RecordGetString(row,3);
705 if (!file_key)
707 ERR("Unable to get file key\n");
708 return ERROR_FUNCTION_FAILED;
711 file = get_loaded_file( package, file_key );
712 if (!file)
714 ERR("Original file unknown %s\n", debugstr_w(file_key));
715 return ERROR_SUCCESS;
718 dest = get_duplicate_filename( package, row, file_key, file->TargetPath );
719 if (!dest)
721 WARN("Unable to get duplicate filename\n");
722 return ERROR_SUCCESS;
725 TRACE("Duplicating file %s to %s\n", debugstr_w(file->TargetPath), debugstr_w(dest));
727 if (!CopyFileW( file->TargetPath, dest, TRUE ))
729 WARN("Failed to copy file %s -> %s (%u)\n",
730 debugstr_w(file->TargetPath), debugstr_w(dest), GetLastError());
733 FIXME("We should track these duplicate files as well\n");
735 uirow = MSI_CreateRecord( 9 );
736 MSI_RecordSetStringW( uirow, 1, MSI_RecordGetString( row, 1 ) );
737 MSI_RecordSetInteger( uirow, 6, file->FileSize );
738 MSI_RecordSetStringW( uirow, 9, MSI_RecordGetString( row, 5 ) );
739 ui_actiondata( package, szDuplicateFiles, uirow );
740 msiobj_release( &uirow->hdr );
742 msi_free(dest);
743 return ERROR_SUCCESS;
746 UINT ACTION_DuplicateFiles(MSIPACKAGE *package)
748 UINT rc;
749 MSIQUERY * view;
750 static const WCHAR ExecSeqQuery[] =
751 {'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
752 '`','D','u','p','l','i','c','a','t','e','F','i','l','e','`',0};
754 rc = MSI_DatabaseOpenViewW(package->db, ExecSeqQuery, &view);
755 if (rc != ERROR_SUCCESS)
756 return ERROR_SUCCESS;
758 rc = MSI_IterateRecords(view, NULL, ITERATE_DuplicateFiles, package);
759 msiobj_release(&view->hdr);
761 return rc;
764 static UINT ITERATE_RemoveDuplicateFiles( MSIRECORD *row, LPVOID param )
766 MSIPACKAGE *package = param;
767 LPWSTR dest;
768 LPCWSTR file_key, component;
769 MSICOMPONENT *comp;
770 MSIRECORD *uirow;
771 MSIFILE *file;
773 component = MSI_RecordGetString( row, 2 );
774 comp = get_loaded_component( package, component );
775 if (!comp)
776 return ERROR_SUCCESS;
778 if (comp->ActionRequest != INSTALLSTATE_ABSENT)
780 TRACE("Component not scheduled for removal %s\n", debugstr_w(component));
781 comp->Action = comp->Installed;
782 return ERROR_SUCCESS;
784 comp->Action = INSTALLSTATE_ABSENT;
786 file_key = MSI_RecordGetString( row, 3 );
787 if (!file_key)
789 ERR("Unable to get file key\n");
790 return ERROR_FUNCTION_FAILED;
793 file = get_loaded_file( package, file_key );
794 if (!file)
796 ERR("Original file unknown %s\n", debugstr_w(file_key));
797 return ERROR_SUCCESS;
800 dest = get_duplicate_filename( package, row, file_key, file->TargetPath );
801 if (!dest)
803 WARN("Unable to get duplicate filename\n");
804 return ERROR_SUCCESS;
807 TRACE("Removing duplicate %s of %s\n", debugstr_w(dest), debugstr_w(file->TargetPath));
809 if (!DeleteFileW( dest ))
811 WARN("Failed to delete duplicate file %s (%u)\n", debugstr_w(dest), GetLastError());
814 uirow = MSI_CreateRecord( 9 );
815 MSI_RecordSetStringW( uirow, 1, MSI_RecordGetString( row, 1 ) );
816 MSI_RecordSetStringW( uirow, 9, MSI_RecordGetString( row, 5 ) );
817 ui_actiondata( package, szRemoveDuplicateFiles, uirow );
818 msiobj_release( &uirow->hdr );
820 msi_free(dest);
821 return ERROR_SUCCESS;
824 UINT ACTION_RemoveDuplicateFiles( MSIPACKAGE *package )
826 UINT rc;
827 MSIQUERY *view;
828 static const WCHAR query[] =
829 {'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
830 '`','D','u','p','l','i','c','a','t','e','F','i','l','e','`',0};
832 rc = MSI_DatabaseOpenViewW( package->db, query, &view );
833 if (rc != ERROR_SUCCESS)
834 return ERROR_SUCCESS;
836 rc = MSI_IterateRecords( view, NULL, ITERATE_RemoveDuplicateFiles, package );
837 msiobj_release( &view->hdr );
839 return rc;
842 static BOOL verify_comp_for_removal(MSICOMPONENT *comp, UINT install_mode)
844 INSTALLSTATE request = comp->ActionRequest;
846 if (request == INSTALLSTATE_UNKNOWN)
847 return FALSE;
849 if (install_mode == msidbRemoveFileInstallModeOnInstall &&
850 (request == INSTALLSTATE_LOCAL || request == INSTALLSTATE_SOURCE))
851 return TRUE;
853 if (request == INSTALLSTATE_ABSENT)
855 if (!comp->ComponentId)
856 return FALSE;
858 if (install_mode == msidbRemoveFileInstallModeOnRemove)
859 return TRUE;
862 if (install_mode == msidbRemoveFileInstallModeOnBoth)
863 return TRUE;
865 return FALSE;
868 static UINT ITERATE_RemoveFiles(MSIRECORD *row, LPVOID param)
870 MSIPACKAGE *package = param;
871 MSICOMPONENT *comp;
872 MSIRECORD *uirow;
873 LPCWSTR component, filename, dirprop;
874 UINT install_mode;
875 LPWSTR dir = NULL, path = NULL;
876 DWORD size;
877 UINT ret = ERROR_SUCCESS;
879 component = MSI_RecordGetString(row, 2);
880 filename = MSI_RecordGetString(row, 3);
881 dirprop = MSI_RecordGetString(row, 4);
882 install_mode = MSI_RecordGetInteger(row, 5);
884 comp = get_loaded_component(package, component);
885 if (!comp)
887 ERR("Invalid component: %s\n", debugstr_w(component));
888 return ERROR_FUNCTION_FAILED;
891 if (!verify_comp_for_removal(comp, install_mode))
893 TRACE("Skipping removal due to missing conditions\n");
894 comp->Action = comp->Installed;
895 return ERROR_SUCCESS;
898 dir = msi_dup_property(package->db, dirprop);
899 if (!dir)
900 return ERROR_OUTOFMEMORY;
902 size = (filename != NULL) ? lstrlenW(filename) : 0;
903 size += lstrlenW(dir) + 2;
904 path = msi_alloc(size * sizeof(WCHAR));
905 if (!path)
907 ret = ERROR_OUTOFMEMORY;
908 goto done;
911 if (filename)
913 lstrcpyW(path, dir);
914 PathAddBackslashW(path);
915 lstrcatW(path, filename);
917 TRACE("Deleting misc file: %s\n", debugstr_w(path));
918 DeleteFileW(path);
920 else
922 TRACE("Removing misc directory: %s\n", debugstr_w(dir));
923 RemoveDirectoryW(dir);
926 done:
927 uirow = MSI_CreateRecord( 9 );
928 MSI_RecordSetStringW( uirow, 1, MSI_RecordGetString(row, 1) );
929 MSI_RecordSetStringW( uirow, 9, dir );
930 ui_actiondata( package, szRemoveFiles, uirow );
931 msiobj_release( &uirow->hdr );
933 msi_free(path);
934 msi_free(dir);
935 return ret;
938 UINT ACTION_RemoveFiles( MSIPACKAGE *package )
940 MSIQUERY *view;
941 MSIFILE *file;
942 UINT r;
944 static const WCHAR query[] = {
945 'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
946 '`','R','e','m','o','v','e','F','i','l','e','`',0};
947 static const WCHAR folder_query[] = {
948 'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
949 '`','C','r','e','a','t','e','F','o','l','d','e','r','`',0};
951 r = MSI_DatabaseOpenViewW(package->db, query, &view);
952 if (r == ERROR_SUCCESS)
954 MSI_IterateRecords(view, NULL, ITERATE_RemoveFiles, package);
955 msiobj_release(&view->hdr);
958 r = MSI_DatabaseOpenViewW(package->db, folder_query, &view);
959 if (r == ERROR_SUCCESS)
960 msiobj_release(&view->hdr);
962 LIST_FOR_EACH_ENTRY( file, &package->files, MSIFILE, entry )
964 MSIRECORD *uirow;
965 LPWSTR dir, p;
966 VS_FIXEDFILEINFO *ver;
968 if ( file->state == msifs_installed )
969 ERR("removing installed file %s\n", debugstr_w(file->TargetPath));
971 if ( file->Component->ActionRequest != INSTALLSTATE_ABSENT ||
972 file->Component->Installed == INSTALLSTATE_SOURCE )
973 continue;
975 if (file->Version)
977 ver = msi_get_disk_file_version( file->TargetPath );
978 if (ver && msi_compare_file_versions( ver, file->Version ) > 0)
980 TRACE("newer version detected, not removing file\n");
981 msi_free( ver );
982 continue;
984 msi_free( ver );
987 TRACE("removing %s\n", debugstr_w(file->File) );
988 if (!DeleteFileW( file->TargetPath ))
990 WARN("failed to delete %s\n", debugstr_w(file->TargetPath));
992 /* FIXME: check persistence for each directory */
993 else if (r && (dir = strdupW( file->TargetPath )))
995 if ((p = strrchrW( dir, '\\' ))) *p = 0;
996 RemoveDirectoryW( dir );
997 msi_free( dir );
999 file->state = msifs_missing;
1001 uirow = MSI_CreateRecord( 9 );
1002 MSI_RecordSetStringW( uirow, 1, file->FileName );
1003 MSI_RecordSetStringW( uirow, 9, file->Component->Directory );
1004 ui_actiondata( package, szRemoveFiles, uirow );
1005 msiobj_release( &uirow->hdr );
1006 /* FIXME: call ui_progress here? */
1009 return ERROR_SUCCESS;