Release 1.3.7.
[wine/gsoc-2012-control.git] / dlls / msi / files.c
blob4dcf5bd324d268b0161ac6e274db7006b724f024
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 || f->Component->assembly->application)
191 msi_create_directory(package, f->Component->Directory);
193 *path = strdupW(f->TargetPath);
194 *attrs = f->Attributes;
196 else if (action == MSICABEXTRACT_FILEEXTRACTED)
198 f->state = msifs_installed;
199 f = NULL;
202 return TRUE;
206 * ACTION_InstallFiles()
208 * For efficiency, this is done in two passes:
209 * 1) Correct all the TargetPaths and determine what files are to be installed.
210 * 2) Extract Cabinets and copy files.
212 UINT ACTION_InstallFiles(MSIPACKAGE *package)
214 MSIMEDIAINFO *mi;
215 MSICOMPONENT *comp;
216 UINT rc = ERROR_SUCCESS;
217 MSIFILE *file;
219 /* increment progress bar each time action data is sent */
220 ui_progress(package,1,1,0,0);
222 schedule_install_files(package);
224 mi = msi_alloc_zero( sizeof(MSIMEDIAINFO) );
226 LIST_FOR_EACH_ENTRY( file, &package->files, MSIFILE, entry )
228 if (file->state != msifs_missing && !mi->is_continuous && file->state != msifs_overwrite)
229 continue;
231 if (file->state == msifs_overwrite &&
232 (file->Component->Attributes & msidbComponentAttributesNeverOverwrite))
234 TRACE("not overwriting %s\n", debugstr_w(file->TargetPath));
235 file->state = msifs_skipped;
236 continue;
239 if (file->Sequence > mi->last_sequence || mi->is_continuous ||
240 (file->IsCompressed && !mi->is_extracted))
242 MSICABDATA data;
244 rc = ready_media(package, file, mi);
245 if (rc != ERROR_SUCCESS)
247 ERR("Failed to ready media for %s\n", debugstr_w(file->File));
248 goto done;
251 data.mi = mi;
252 data.package = package;
253 data.cb = installfiles_cb;
254 data.user = (PVOID)(UINT_PTR)mi->disk_id;
256 if (file->IsCompressed &&
257 !msi_cabextract(package, mi, &data))
259 ERR("Failed to extract cabinet: %s\n", debugstr_w(mi->cabinet));
260 rc = ERROR_INSTALL_FAILURE;
261 goto done;
265 if (!file->IsCompressed)
267 LPWSTR source = resolve_file_source(package, file);
269 TRACE("copying %s to %s\n", debugstr_w(source), debugstr_w(file->TargetPath));
271 msi_file_update_ui(package, file, szInstallFiles);
272 if (!file->Component->assembly || file->Component->assembly->application)
274 msi_create_directory(package, file->Component->Directory);
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 goto done;
285 msi_free(source);
287 else if (file->state != msifs_installed)
289 ERR("compressed file wasn't installed (%s)\n", debugstr_w(file->TargetPath));
290 rc = ERROR_INSTALL_FAILURE;
291 goto done;
294 LIST_FOR_EACH_ENTRY( comp, &package->components, MSICOMPONENT, entry )
296 if (comp->Enabled && comp->assembly && !comp->assembly->installed)
298 rc = install_assembly( package, comp );
299 if (rc != ERROR_SUCCESS)
301 ERR("Failed to install assembly\n");
302 rc = ERROR_INSTALL_FAILURE;
303 break;
308 done:
309 msi_free_media_info(mi);
310 return rc;
313 #define is_dot_dir(x) ((x[0] == '.') && ((x[1] == 0) || ((x[1] == '.') && (x[2] == 0))))
315 typedef struct
317 struct list entry;
318 LPWSTR sourcename;
319 LPWSTR destname;
320 LPWSTR source;
321 LPWSTR dest;
322 } FILE_LIST;
324 static BOOL msi_move_file(LPCWSTR source, LPCWSTR dest, int options)
326 BOOL ret;
328 if (GetFileAttributesW(source) == FILE_ATTRIBUTE_DIRECTORY ||
329 GetFileAttributesW(dest) == FILE_ATTRIBUTE_DIRECTORY)
331 WARN("Source or dest is directory, not moving\n");
332 return FALSE;
335 if (options == msidbMoveFileOptionsMove)
337 TRACE("moving %s -> %s\n", debugstr_w(source), debugstr_w(dest));
338 ret = MoveFileExW(source, dest, MOVEFILE_REPLACE_EXISTING);
339 if (!ret)
341 WARN("MoveFile failed: %d\n", GetLastError());
342 return FALSE;
345 else
347 TRACE("copying %s -> %s\n", debugstr_w(source), debugstr_w(dest));
348 ret = CopyFileW(source, dest, FALSE);
349 if (!ret)
351 WARN("CopyFile failed: %d\n", GetLastError());
352 return FALSE;
356 return TRUE;
359 static LPWSTR wildcard_to_file(LPWSTR wildcard, LPWSTR filename)
361 LPWSTR path, ptr;
362 DWORD dirlen, pathlen;
364 ptr = strrchrW(wildcard, '\\');
365 dirlen = ptr - wildcard + 1;
367 pathlen = dirlen + lstrlenW(filename) + 1;
368 path = msi_alloc(pathlen * sizeof(WCHAR));
370 lstrcpynW(path, wildcard, dirlen + 1);
371 lstrcatW(path, filename);
373 return path;
376 static void free_file_entry(FILE_LIST *file)
378 msi_free(file->source);
379 msi_free(file->dest);
380 msi_free(file);
383 static void free_list(FILE_LIST *list)
385 while (!list_empty(&list->entry))
387 FILE_LIST *file = LIST_ENTRY(list_head(&list->entry), FILE_LIST, entry);
389 list_remove(&file->entry);
390 free_file_entry(file);
394 static BOOL add_wildcard(FILE_LIST *files, LPWSTR source, LPWSTR dest)
396 FILE_LIST *new, *file;
397 LPWSTR ptr, filename;
398 DWORD size;
400 new = msi_alloc_zero(sizeof(FILE_LIST));
401 if (!new)
402 return FALSE;
404 new->source = strdupW(source);
405 ptr = strrchrW(dest, '\\') + 1;
406 filename = strrchrW(new->source, '\\') + 1;
408 new->sourcename = filename;
410 if (*ptr)
411 new->destname = ptr;
412 else
413 new->destname = new->sourcename;
415 size = (ptr - dest) + lstrlenW(filename) + 1;
416 new->dest = msi_alloc(size * sizeof(WCHAR));
417 if (!new->dest)
419 free_file_entry(new);
420 return FALSE;
423 lstrcpynW(new->dest, dest, ptr - dest + 1);
424 lstrcatW(new->dest, filename);
426 if (list_empty(&files->entry))
428 list_add_head(&files->entry, &new->entry);
429 return TRUE;
432 LIST_FOR_EACH_ENTRY(file, &files->entry, FILE_LIST, entry)
434 if (strcmpW( source, file->source ) < 0)
436 list_add_before(&file->entry, &new->entry);
437 return TRUE;
441 list_add_after(&file->entry, &new->entry);
442 return TRUE;
445 static BOOL move_files_wildcard(LPWSTR source, LPWSTR dest, int options)
447 WIN32_FIND_DATAW wfd;
448 HANDLE hfile;
449 LPWSTR path;
450 BOOL res;
451 FILE_LIST files, *file;
452 DWORD size;
454 hfile = FindFirstFileW(source, &wfd);
455 if (hfile == INVALID_HANDLE_VALUE) return FALSE;
457 list_init(&files.entry);
459 for (res = TRUE; res; res = FindNextFileW(hfile, &wfd))
461 if (is_dot_dir(wfd.cFileName)) continue;
463 path = wildcard_to_file(source, wfd.cFileName);
464 if (!path)
466 res = FALSE;
467 goto done;
470 add_wildcard(&files, path, dest);
471 msi_free(path);
474 /* no files match the wildcard */
475 if (list_empty(&files.entry))
476 goto done;
478 /* only the first wildcard match gets renamed to dest */
479 file = LIST_ENTRY(list_head(&files.entry), FILE_LIST, entry);
480 size = (strrchrW(file->dest, '\\') - file->dest) + lstrlenW(file->destname) + 2;
481 file->dest = msi_realloc(file->dest, size * sizeof(WCHAR));
482 if (!file->dest)
484 res = FALSE;
485 goto done;
488 /* file->dest may be shorter after the reallocation, so add a NULL
489 * terminator. This is needed for the call to strrchrW, as there will no
490 * longer be a NULL terminator within the bounds of the allocation in this case.
492 file->dest[size - 1] = '\0';
493 lstrcpyW(strrchrW(file->dest, '\\') + 1, file->destname);
495 while (!list_empty(&files.entry))
497 file = LIST_ENTRY(list_head(&files.entry), FILE_LIST, entry);
499 msi_move_file(file->source, file->dest, options);
501 list_remove(&file->entry);
502 free_file_entry(file);
505 res = TRUE;
507 done:
508 free_list(&files);
509 FindClose(hfile);
510 return res;
513 static UINT ITERATE_MoveFiles( MSIRECORD *rec, LPVOID param )
515 MSIPACKAGE *package = param;
516 MSIRECORD *uirow;
517 MSICOMPONENT *comp;
518 LPCWSTR sourcename, component;
519 LPWSTR sourcedir, destname = NULL, destdir = NULL, source = NULL, dest = NULL;
520 int options;
521 DWORD size;
522 BOOL ret, wildcards;
524 component = MSI_RecordGetString(rec, 2);
525 comp = get_loaded_component(package, component);
526 if (!comp)
527 return ERROR_SUCCESS;
529 if (!comp->Enabled)
531 TRACE("component is disabled\n");
532 return ERROR_SUCCESS;
535 if (comp->ActionRequest != INSTALLSTATE_LOCAL && comp->ActionRequest != INSTALLSTATE_SOURCE)
537 TRACE("Component not scheduled for installation: %s\n", debugstr_w(component));
538 comp->Action = comp->Installed;
539 return ERROR_SUCCESS;
541 comp->Action = comp->ActionRequest;
543 sourcename = MSI_RecordGetString(rec, 3);
544 options = MSI_RecordGetInteger(rec, 7);
546 sourcedir = msi_dup_property(package->db, MSI_RecordGetString(rec, 5));
547 if (!sourcedir)
548 goto done;
550 destdir = msi_dup_property(package->db, MSI_RecordGetString(rec, 6));
551 if (!destdir)
552 goto done;
554 if (!sourcename)
556 if (GetFileAttributesW(sourcedir) == INVALID_FILE_ATTRIBUTES)
557 goto done;
559 source = strdupW(sourcedir);
560 if (!source)
561 goto done;
563 else
565 size = lstrlenW(sourcedir) + lstrlenW(sourcename) + 2;
566 source = msi_alloc(size * sizeof(WCHAR));
567 if (!source)
568 goto done;
570 lstrcpyW(source, sourcedir);
571 if (source[lstrlenW(source) - 1] != '\\')
572 lstrcatW(source, szBackSlash);
573 lstrcatW(source, sourcename);
576 wildcards = strchrW(source, '*') || strchrW(source, '?');
578 if (MSI_RecordIsNull(rec, 4))
580 if (!wildcards)
582 destname = strdupW(sourcename);
583 if (!destname)
584 goto done;
587 else
589 destname = strdupW(MSI_RecordGetString(rec, 4));
590 if (destname)
591 reduce_to_longfilename(destname);
594 size = 0;
595 if (destname)
596 size = lstrlenW(destname);
598 size += lstrlenW(destdir) + 2;
599 dest = msi_alloc(size * sizeof(WCHAR));
600 if (!dest)
601 goto done;
603 lstrcpyW(dest, destdir);
604 if (dest[lstrlenW(dest) - 1] != '\\')
605 lstrcatW(dest, szBackSlash);
607 if (destname)
608 lstrcatW(dest, destname);
610 if (GetFileAttributesW(destdir) == INVALID_FILE_ATTRIBUTES)
612 ret = CreateDirectoryW(destdir, NULL);
613 if (!ret)
615 WARN("CreateDirectory failed: %d\n", GetLastError());
616 goto done;
620 if (!wildcards)
621 msi_move_file(source, dest, options);
622 else
623 move_files_wildcard(source, dest, options);
625 done:
626 uirow = MSI_CreateRecord( 9 );
627 MSI_RecordSetStringW( uirow, 1, MSI_RecordGetString(rec, 1) );
628 MSI_RecordSetInteger( uirow, 6, 1 ); /* FIXME */
629 MSI_RecordSetStringW( uirow, 9, destdir );
630 ui_actiondata( package, szMoveFiles, uirow );
631 msiobj_release( &uirow->hdr );
633 msi_free(sourcedir);
634 msi_free(destdir);
635 msi_free(destname);
636 msi_free(source);
637 msi_free(dest);
639 return ERROR_SUCCESS;
642 UINT ACTION_MoveFiles( MSIPACKAGE *package )
644 UINT rc;
645 MSIQUERY *view;
647 static const WCHAR ExecSeqQuery[] =
648 {'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
649 '`','M','o','v','e','F','i','l','e','`',0};
651 rc = MSI_DatabaseOpenViewW(package->db, ExecSeqQuery, &view);
652 if (rc != ERROR_SUCCESS)
653 return ERROR_SUCCESS;
655 rc = MSI_IterateRecords(view, NULL, ITERATE_MoveFiles, package);
656 msiobj_release(&view->hdr);
658 return rc;
661 static WCHAR *get_duplicate_filename( MSIPACKAGE *package, MSIRECORD *row, const WCHAR *file_key, const WCHAR *src )
663 DWORD len;
664 WCHAR *dst_name, *dst_path, *dst;
666 if (MSI_RecordIsNull( row, 4 ))
668 len = strlenW( src ) + 1;
669 if (!(dst_name = msi_alloc( len * sizeof(WCHAR)))) return NULL;
670 strcpyW( dst_name, strrchrW( src, '\\' ) + 1 );
672 else
674 MSI_RecordGetStringW( row, 4, NULL, &len );
675 if (!(dst_name = msi_alloc( ++len * sizeof(WCHAR) ))) return NULL;
676 MSI_RecordGetStringW( row, 4, dst_name, &len );
677 reduce_to_longfilename( dst_name );
680 if (MSI_RecordIsNull( row, 5 ))
682 WCHAR *p;
683 dst_path = strdupW( src );
684 p = strrchrW( dst_path, '\\' );
685 if (p) *p = 0;
687 else
689 const WCHAR *dst_key = MSI_RecordGetString( row, 5 );
691 dst_path = resolve_folder( package, dst_key, FALSE, FALSE, TRUE, NULL );
692 if (!dst_path)
694 /* try a property */
695 dst_path = msi_dup_property( package->db, dst_key );
696 if (!dst_path)
698 FIXME("Unable to get destination folder, try AppSearch properties\n");
699 msi_free( dst_name );
700 return NULL;
705 dst = build_directory_name( 2, dst_path, dst_name );
706 create_full_pathW( dst_path );
708 msi_free( dst_name );
709 msi_free( dst_path );
710 return dst;
713 static UINT ITERATE_DuplicateFiles(MSIRECORD *row, LPVOID param)
715 MSIPACKAGE *package = param;
716 LPWSTR dest;
717 LPCWSTR file_key, component;
718 MSICOMPONENT *comp;
719 MSIRECORD *uirow;
720 MSIFILE *file;
722 component = MSI_RecordGetString(row,2);
723 comp = get_loaded_component(package,component);
724 if (!comp)
725 return ERROR_SUCCESS;
727 if (!comp->Enabled)
729 TRACE("component is disabled\n");
730 return ERROR_SUCCESS;
733 if (comp->ActionRequest != INSTALLSTATE_LOCAL)
735 TRACE("Component not scheduled for installation %s\n", debugstr_w(component));
736 comp->Action = comp->Installed;
737 return ERROR_SUCCESS;
739 comp->Action = INSTALLSTATE_LOCAL;
741 file_key = MSI_RecordGetString(row,3);
742 if (!file_key)
744 ERR("Unable to get file key\n");
745 return ERROR_FUNCTION_FAILED;
748 file = get_loaded_file( package, file_key );
749 if (!file)
751 ERR("Original file unknown %s\n", debugstr_w(file_key));
752 return ERROR_SUCCESS;
755 dest = get_duplicate_filename( package, row, file_key, file->TargetPath );
756 if (!dest)
758 WARN("Unable to get duplicate filename\n");
759 return ERROR_SUCCESS;
762 TRACE("Duplicating file %s to %s\n", debugstr_w(file->TargetPath), debugstr_w(dest));
764 if (!CopyFileW( file->TargetPath, dest, TRUE ))
766 WARN("Failed to copy file %s -> %s (%u)\n",
767 debugstr_w(file->TargetPath), debugstr_w(dest), GetLastError());
770 FIXME("We should track these duplicate files as well\n");
772 uirow = MSI_CreateRecord( 9 );
773 MSI_RecordSetStringW( uirow, 1, MSI_RecordGetString( row, 1 ) );
774 MSI_RecordSetInteger( uirow, 6, file->FileSize );
775 MSI_RecordSetStringW( uirow, 9, MSI_RecordGetString( row, 5 ) );
776 ui_actiondata( package, szDuplicateFiles, uirow );
777 msiobj_release( &uirow->hdr );
779 msi_free(dest);
780 return ERROR_SUCCESS;
783 UINT ACTION_DuplicateFiles(MSIPACKAGE *package)
785 UINT rc;
786 MSIQUERY * view;
787 static const WCHAR ExecSeqQuery[] =
788 {'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
789 '`','D','u','p','l','i','c','a','t','e','F','i','l','e','`',0};
791 rc = MSI_DatabaseOpenViewW(package->db, ExecSeqQuery, &view);
792 if (rc != ERROR_SUCCESS)
793 return ERROR_SUCCESS;
795 rc = MSI_IterateRecords(view, NULL, ITERATE_DuplicateFiles, package);
796 msiobj_release(&view->hdr);
798 return rc;
801 static UINT ITERATE_RemoveDuplicateFiles( MSIRECORD *row, LPVOID param )
803 MSIPACKAGE *package = param;
804 LPWSTR dest;
805 LPCWSTR file_key, component;
806 MSICOMPONENT *comp;
807 MSIRECORD *uirow;
808 MSIFILE *file;
810 component = MSI_RecordGetString( row, 2 );
811 comp = get_loaded_component( package, component );
812 if (!comp)
813 return ERROR_SUCCESS;
815 if (!comp->Enabled)
817 TRACE("component is disabled\n");
818 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 MSIRECORD *uirow;
916 LPCWSTR component, filename, dirprop;
917 UINT install_mode;
918 LPWSTR dir = NULL, path = NULL;
919 DWORD size;
920 UINT ret = ERROR_SUCCESS;
922 component = MSI_RecordGetString(row, 2);
923 filename = MSI_RecordGetString(row, 3);
924 dirprop = MSI_RecordGetString(row, 4);
925 install_mode = MSI_RecordGetInteger(row, 5);
927 comp = get_loaded_component(package, component);
928 if (!comp)
930 ERR("Invalid component: %s\n", debugstr_w(component));
931 return ERROR_FUNCTION_FAILED;
934 if (!comp->Enabled)
936 TRACE("component is disabled\n");
937 return ERROR_SUCCESS;
940 if (!verify_comp_for_removal(comp, install_mode))
942 TRACE("Skipping removal due to missing conditions\n");
943 comp->Action = comp->Installed;
944 return ERROR_SUCCESS;
947 dir = msi_dup_property(package->db, dirprop);
948 if (!dir)
949 return ERROR_OUTOFMEMORY;
951 size = (filename != NULL) ? lstrlenW(filename) : 0;
952 size += lstrlenW(dir) + 2;
953 path = msi_alloc(size * sizeof(WCHAR));
954 if (!path)
956 ret = ERROR_OUTOFMEMORY;
957 goto done;
960 if (filename)
962 lstrcpyW(path, dir);
963 PathAddBackslashW(path);
964 lstrcatW(path, filename);
966 TRACE("Deleting misc file: %s\n", debugstr_w(path));
967 DeleteFileW(path);
969 else
971 TRACE("Removing misc directory: %s\n", debugstr_w(dir));
972 RemoveDirectoryW(dir);
975 done:
976 uirow = MSI_CreateRecord( 9 );
977 MSI_RecordSetStringW( uirow, 1, MSI_RecordGetString(row, 1) );
978 MSI_RecordSetStringW( uirow, 9, dir );
979 ui_actiondata( package, szRemoveFiles, uirow );
980 msiobj_release( &uirow->hdr );
982 msi_free(path);
983 msi_free(dir);
984 return ret;
987 UINT ACTION_RemoveFiles( MSIPACKAGE *package )
989 MSIQUERY *view;
990 MSIFILE *file;
991 UINT r;
993 static const WCHAR query[] = {
994 'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
995 '`','R','e','m','o','v','e','F','i','l','e','`',0};
996 static const WCHAR folder_query[] = {
997 'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
998 '`','C','r','e','a','t','e','F','o','l','d','e','r','`',0};
1000 r = MSI_DatabaseOpenViewW(package->db, query, &view);
1001 if (r == ERROR_SUCCESS)
1003 MSI_IterateRecords(view, NULL, ITERATE_RemoveFiles, package);
1004 msiobj_release(&view->hdr);
1007 r = MSI_DatabaseOpenViewW(package->db, folder_query, &view);
1008 if (r == ERROR_SUCCESS)
1009 msiobj_release(&view->hdr);
1011 LIST_FOR_EACH_ENTRY( file, &package->files, MSIFILE, entry )
1013 MSIRECORD *uirow;
1014 LPWSTR dir, p;
1015 VS_FIXEDFILEINFO *ver;
1017 if ( file->state == msifs_installed )
1018 ERR("removing installed file %s\n", debugstr_w(file->TargetPath));
1020 if ( file->Component->ActionRequest != INSTALLSTATE_ABSENT ||
1021 file->Component->Installed == INSTALLSTATE_SOURCE )
1022 continue;
1024 if (!file->Component->Enabled)
1026 TRACE("component is disabled\n");
1027 continue;
1030 if (file->Version)
1032 ver = msi_get_disk_file_version( file->TargetPath );
1033 if (ver && msi_compare_file_versions( ver, file->Version ) > 0)
1035 TRACE("newer version detected, not removing file\n");
1036 msi_free( ver );
1037 continue;
1039 msi_free( ver );
1042 TRACE("removing %s\n", debugstr_w(file->File) );
1043 if (!DeleteFileW( file->TargetPath ))
1045 WARN("failed to delete %s\n", debugstr_w(file->TargetPath));
1047 /* FIXME: check persistence for each directory */
1048 else if (r && (dir = strdupW( file->TargetPath )))
1050 if ((p = strrchrW( dir, '\\' ))) *p = 0;
1051 RemoveDirectoryW( dir );
1052 msi_free( dir );
1054 file->state = msifs_missing;
1056 uirow = MSI_CreateRecord( 9 );
1057 MSI_RecordSetStringW( uirow, 1, file->FileName );
1058 MSI_RecordSetStringW( uirow, 9, file->Component->Directory );
1059 ui_actiondata( package, szRemoveFiles, uirow );
1060 msiobj_release( &uirow->hdr );
1061 /* FIXME: call ui_progress here? */
1064 return ERROR_SUCCESS;