shell32: Initialize fAnyOperationsAborted in SHFileOperation.
[wine.git] / dlls / msi / files.c
blob11913efd618f7ff1b221e5093425bbf1571c0182
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:
25 * InstallFiles
26 * DuplicateFiles
27 * MoveFiles
28 * PatchFiles
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 HMODULE hmspatcha;
51 static BOOL (WINAPI *ApplyPatchToFileW)(LPCWSTR, LPCWSTR, LPCWSTR, ULONG);
53 static void msi_file_update_ui( MSIPACKAGE *package, MSIFILE *f, const WCHAR *action )
55 MSIRECORD *uirow;
57 uirow = MSI_CreateRecord( 9 );
58 MSI_RecordSetStringW( uirow, 1, f->FileName );
59 MSI_RecordSetStringW( uirow, 9, f->Component->Directory );
60 MSI_RecordSetInteger( uirow, 6, f->FileSize );
61 msi_ui_actiondata( package, action, uirow );
62 msiobj_release( &uirow->hdr );
63 msi_ui_progress( package, 2, f->FileSize, 0, 0 );
66 static msi_file_state calculate_install_state( MSIPACKAGE *package, MSIFILE *file )
68 MSICOMPONENT *comp = file->Component;
69 VS_FIXEDFILEINFO *file_version;
70 WCHAR *font_version;
71 msi_file_state state;
72 DWORD file_size;
74 comp->Action = msi_get_component_action( package, comp );
75 if (comp->Action != INSTALLSTATE_LOCAL || (comp->assembly && comp->assembly->installed))
77 TRACE("file %s is not scheduled for install\n", debugstr_w(file->File));
78 return msifs_skipped;
80 if ((comp->assembly && !comp->assembly->application && !comp->assembly->installed) ||
81 GetFileAttributesW( file->TargetPath ) == INVALID_FILE_ATTRIBUTES)
83 TRACE("file %s is missing\n", debugstr_w(file->File));
84 return msifs_missing;
86 if (file->Version)
88 if ((file_version = msi_get_disk_file_version( file->TargetPath )))
90 TRACE("new %s old %u.%u.%u.%u\n", debugstr_w(file->Version),
91 HIWORD(file_version->dwFileVersionMS),
92 LOWORD(file_version->dwFileVersionMS),
93 HIWORD(file_version->dwFileVersionLS),
94 LOWORD(file_version->dwFileVersionLS));
96 if (msi_compare_file_versions( file_version, file->Version ) < 0)
97 state = msifs_overwrite;
98 else
100 TRACE("destination file version equal or greater, not overwriting\n");
101 state = msifs_present;
103 msi_free( file_version );
104 return state;
106 else if ((font_version = msi_font_version_from_file( file->TargetPath )))
108 TRACE("new %s old %s\n", debugstr_w(file->Version), debugstr_w(font_version));
110 if (msi_compare_font_versions( font_version, file->Version ) < 0)
111 state = msifs_overwrite;
112 else
114 TRACE("destination file version equal or greater, not overwriting\n");
115 state = msifs_present;
117 msi_free( font_version );
118 return state;
121 if ((file_size = msi_get_disk_file_size( file->TargetPath )) != file->FileSize)
123 return msifs_overwrite;
125 if (file->hash.dwFileHashInfoSize)
127 if (msi_file_hash_matches( file ))
129 TRACE("file hashes match, not overwriting\n");
130 return msifs_hashmatch;
132 else
134 TRACE("file hashes do not match, overwriting\n");
135 return msifs_overwrite;
138 /* assume present */
139 return msifs_present;
142 static void schedule_install_files(MSIPACKAGE *package)
144 MSIFILE *file;
146 LIST_FOR_EACH_ENTRY(file, &package->files, MSIFILE, entry)
148 MSICOMPONENT *comp = file->Component;
150 file->state = calculate_install_state( package, file );
151 if (file->state == msifs_overwrite && (comp->Attributes & msidbComponentAttributesNeverOverwrite))
153 TRACE("not overwriting %s\n", debugstr_w(file->TargetPath));
154 file->state = msifs_skipped;
159 static UINT copy_file(MSIFILE *file, LPWSTR source)
161 BOOL ret;
163 ret = CopyFileW(source, file->TargetPath, FALSE);
164 if (!ret)
165 return GetLastError();
167 SetFileAttributesW(file->TargetPath, FILE_ATTRIBUTE_NORMAL);
169 file->state = msifs_installed;
170 return ERROR_SUCCESS;
173 static UINT copy_install_file(MSIPACKAGE *package, MSIFILE *file, LPWSTR source)
175 UINT gle;
177 TRACE("Copying %s to %s\n", debugstr_w(source), debugstr_w(file->TargetPath));
179 gle = copy_file(file, source);
180 if (gle == ERROR_SUCCESS)
181 return gle;
183 if (gle == ERROR_ALREADY_EXISTS && file->state == msifs_overwrite)
185 TRACE("overwriting existing file\n");
186 return ERROR_SUCCESS;
188 else if (gle == ERROR_ACCESS_DENIED)
190 SetFileAttributesW(file->TargetPath, FILE_ATTRIBUTE_NORMAL);
192 gle = copy_file(file, source);
193 TRACE("Overwriting existing file: %d\n", gle);
195 if (gle == ERROR_SHARING_VIOLATION || gle == ERROR_USER_MAPPED_FILE)
197 WCHAR *tmpfileW, *pathW, *p;
198 DWORD len;
200 TRACE("file in use, scheduling rename operation\n");
202 if (!(pathW = strdupW( file->TargetPath ))) return ERROR_OUTOFMEMORY;
203 if ((p = strrchrW(pathW, '\\'))) *p = 0;
204 len = strlenW( pathW ) + 16;
205 if (!(tmpfileW = msi_alloc(len * sizeof(WCHAR))))
207 msi_free( pathW );
208 return ERROR_OUTOFMEMORY;
210 if (!GetTempFileNameW( pathW, szMsi, 0, tmpfileW )) tmpfileW[0] = 0;
211 msi_free( pathW );
213 if (CopyFileW(source, tmpfileW, FALSE) &&
214 MoveFileExW(file->TargetPath, NULL, MOVEFILE_DELAY_UNTIL_REBOOT) &&
215 MoveFileExW(tmpfileW, file->TargetPath, MOVEFILE_DELAY_UNTIL_REBOOT))
217 file->state = msifs_installed;
218 package->need_reboot_at_end = 1;
219 gle = ERROR_SUCCESS;
221 else
223 gle = GetLastError();
224 WARN("failed to schedule rename operation: %d)\n", gle);
225 DeleteFileW( tmpfileW );
227 msi_free(tmpfileW);
230 return gle;
233 static UINT msi_create_directory( MSIPACKAGE *package, const WCHAR *dir )
235 MSIFOLDER *folder;
236 const WCHAR *install_path;
238 install_path = msi_get_target_folder( package, dir );
239 if (!install_path) return ERROR_FUNCTION_FAILED;
241 folder = msi_get_loaded_folder( package, dir );
242 if (folder->State == FOLDER_STATE_UNINITIALIZED)
244 msi_create_full_path( install_path );
245 folder->State = FOLDER_STATE_CREATED;
247 return ERROR_SUCCESS;
250 static MSIFILE *find_file( MSIPACKAGE *package, const WCHAR *filename )
252 MSIFILE *file;
254 LIST_FOR_EACH_ENTRY( file, &package->files, MSIFILE, entry )
256 if (file->state != msifs_installed && !strcmpiW( filename, file->File )) return file;
258 return NULL;
261 static BOOL installfiles_cb(MSIPACKAGE *package, LPCWSTR file, DWORD action,
262 LPWSTR *path, DWORD *attrs, PVOID user)
264 static MSIFILE *f = NULL;
265 UINT_PTR disk_id = (UINT_PTR)user;
267 if (action == MSICABEXTRACT_BEGINEXTRACT)
269 if (!(f = find_file( package, file )))
271 TRACE("unknown file in cabinet (%s)\n", debugstr_w(file));
272 return FALSE;
274 if (f->disk_id != disk_id || (f->state != msifs_missing && f->state != msifs_overwrite))
275 return FALSE;
277 if (!f->Component->assembly || f->Component->assembly->application)
279 msi_create_directory(package, f->Component->Directory);
281 *path = strdupW(f->TargetPath);
282 *attrs = f->Attributes;
284 else if (action == MSICABEXTRACT_FILEEXTRACTED)
286 f->state = msifs_installed;
287 f = NULL;
290 return TRUE;
293 WCHAR *msi_resolve_file_source( MSIPACKAGE *package, MSIFILE *file )
295 WCHAR *p, *path;
297 TRACE("Working to resolve source of file %s\n", debugstr_w(file->File));
299 if (file->IsCompressed) return NULL;
301 p = msi_resolve_source_folder( package, file->Component->Directory, NULL );
302 path = msi_build_directory_name( 2, p, file->ShortName );
304 if (file->LongName && GetFileAttributesW( path ) == INVALID_FILE_ATTRIBUTES)
306 msi_free( path );
307 path = msi_build_directory_name( 2, p, file->LongName );
309 msi_free( p );
310 TRACE("file %s source resolves to %s\n", debugstr_w(file->File), debugstr_w(path));
311 return path;
315 * ACTION_InstallFiles()
317 * For efficiency, this is done in two passes:
318 * 1) Correct all the TargetPaths and determine what files are to be installed.
319 * 2) Extract Cabinets and copy files.
321 UINT ACTION_InstallFiles(MSIPACKAGE *package)
323 MSIMEDIAINFO *mi;
324 MSICOMPONENT *comp;
325 UINT rc = ERROR_SUCCESS;
326 MSIFILE *file;
328 schedule_install_files(package);
329 mi = msi_alloc_zero( sizeof(MSIMEDIAINFO) );
331 LIST_FOR_EACH_ENTRY( file, &package->files, MSIFILE, entry )
333 msi_file_update_ui( package, file, szInstallFiles );
335 rc = msi_load_media_info( package, file->Sequence, mi );
336 if (rc != ERROR_SUCCESS)
338 ERR("Unable to load media info for %s (%u)\n", debugstr_w(file->File), rc);
339 return ERROR_FUNCTION_FAILED;
341 if (!file->Component->Enabled) continue;
343 if (file->state != msifs_hashmatch &&
344 file->state != msifs_skipped &&
345 (file->state != msifs_present || !msi_get_property_int( package->db, szInstalled, 0 )) &&
346 (rc = ready_media( package, file->IsCompressed, mi )))
348 ERR("Failed to ready media for %s\n", debugstr_w(file->File));
349 goto done;
352 if (file->state != msifs_missing && !mi->is_continuous && file->state != msifs_overwrite)
353 continue;
355 if (file->Sequence > mi->last_sequence || mi->is_continuous ||
356 (file->IsCompressed && !mi->is_extracted))
358 MSICABDATA data;
360 data.mi = mi;
361 data.package = package;
362 data.cb = installfiles_cb;
363 data.user = (PVOID)(UINT_PTR)mi->disk_id;
365 if (file->IsCompressed &&
366 !msi_cabextract(package, mi, &data))
368 ERR("Failed to extract cabinet: %s\n", debugstr_w(mi->cabinet));
369 rc = ERROR_INSTALL_FAILURE;
370 goto done;
374 if (!file->IsCompressed)
376 WCHAR *source = msi_resolve_file_source(package, file);
378 TRACE("copying %s to %s\n", debugstr_w(source), debugstr_w(file->TargetPath));
380 if (!file->Component->assembly || file->Component->assembly->application)
382 msi_create_directory(package, file->Component->Directory);
384 rc = copy_install_file(package, file, source);
385 if (rc != ERROR_SUCCESS)
387 ERR("Failed to copy %s to %s (%d)\n", debugstr_w(source),
388 debugstr_w(file->TargetPath), rc);
389 rc = ERROR_INSTALL_FAILURE;
390 msi_free(source);
391 goto done;
393 msi_free(source);
395 else if (file->state != msifs_installed && !(file->Attributes & msidbFileAttributesPatchAdded))
397 ERR("compressed file wasn't installed (%s)\n", debugstr_w(file->TargetPath));
398 rc = ERROR_INSTALL_FAILURE;
399 goto done;
402 LIST_FOR_EACH_ENTRY( comp, &package->components, MSICOMPONENT, entry )
404 comp->Action = msi_get_component_action( package, comp );
405 if (comp->Action == INSTALLSTATE_LOCAL && comp->assembly && !comp->assembly->installed)
407 rc = msi_install_assembly( package, comp );
408 if (rc != ERROR_SUCCESS)
410 ERR("Failed to install assembly\n");
411 rc = ERROR_INSTALL_FAILURE;
412 break;
417 done:
418 msi_free_media_info(mi);
419 return rc;
422 static BOOL load_mspatcha(void)
424 hmspatcha = LoadLibraryA("mspatcha.dll");
425 if (!hmspatcha)
427 ERR("Failed to load mspatcha.dll: %d\n", GetLastError());
428 return FALSE;
431 ApplyPatchToFileW = (void*)GetProcAddress(hmspatcha, "ApplyPatchToFileW");
432 if(!ApplyPatchToFileW)
434 ERR("GetProcAddress(ApplyPatchToFileW) failed: %d.\n", GetLastError());
435 return FALSE;
438 return TRUE;
441 static void unload_mspatch(void)
443 FreeLibrary(hmspatcha);
446 static BOOL patchfiles_cb(MSIPACKAGE *package, LPCWSTR file, DWORD action,
447 LPWSTR *path, DWORD *attrs, PVOID user)
449 static MSIFILEPATCH *p = NULL;
450 static WCHAR patch_path[MAX_PATH] = {0};
451 static WCHAR temp_folder[MAX_PATH] = {0};
453 if (action == MSICABEXTRACT_BEGINEXTRACT)
455 if (temp_folder[0] == '\0')
456 GetTempPathW(MAX_PATH, temp_folder);
458 p = msi_get_loaded_filepatch(package, file);
459 if (!p)
461 TRACE("unknown file in cabinet (%s)\n", debugstr_w(file));
462 return FALSE;
464 GetTempFileNameW(temp_folder, NULL, 0, patch_path);
466 *path = strdupW(patch_path);
467 *attrs = p->File->Attributes;
469 else if (action == MSICABEXTRACT_FILEEXTRACTED)
471 WCHAR patched_file[MAX_PATH];
472 BOOL br;
474 GetTempFileNameW(temp_folder, NULL, 0, patched_file);
476 br = ApplyPatchToFileW(patch_path, p->File->TargetPath, patched_file, 0);
477 if (br)
479 /* FIXME: baseline cache */
481 DeleteFileW( p->File->TargetPath );
482 MoveFileW( patched_file, p->File->TargetPath );
484 p->IsApplied = TRUE;
486 else
487 ERR("Failed patch %s: %d.\n", debugstr_w(p->File->TargetPath), GetLastError());
489 DeleteFileW(patch_path);
490 p = NULL;
493 return TRUE;
496 UINT ACTION_PatchFiles( MSIPACKAGE *package )
498 MSIFILEPATCH *patch;
499 MSIMEDIAINFO *mi;
500 UINT rc = ERROR_SUCCESS;
501 BOOL mspatcha_loaded = FALSE;
503 TRACE("%p\n", package);
505 mi = msi_alloc_zero( sizeof(MSIMEDIAINFO) );
507 LIST_FOR_EACH_ENTRY( patch, &package->filepatches, MSIFILEPATCH, entry )
509 MSIFILE *file = patch->File;
510 MSICOMPONENT *comp = file->Component;
512 rc = msi_load_media_info( package, patch->Sequence, mi );
513 if (rc != ERROR_SUCCESS)
515 ERR("Unable to load media info for %s (%u)\n", debugstr_w(file->File), rc);
516 return ERROR_FUNCTION_FAILED;
518 comp->Action = msi_get_component_action( package, comp );
519 if (!comp->Enabled || comp->Action != INSTALLSTATE_LOCAL) continue;
521 if (!patch->IsApplied)
523 MSICABDATA data;
525 rc = ready_media( package, TRUE, mi );
526 if (rc != ERROR_SUCCESS)
528 ERR("Failed to ready media for %s\n", debugstr_w(file->File));
529 goto done;
532 if (!mspatcha_loaded && !load_mspatcha())
534 rc = ERROR_FUNCTION_FAILED;
535 goto done;
537 mspatcha_loaded = TRUE;
539 data.mi = mi;
540 data.package = package;
541 data.cb = patchfiles_cb;
542 data.user = (PVOID)(UINT_PTR)mi->disk_id;
544 if (!msi_cabextract(package, mi, &data))
546 ERR("Failed to extract cabinet: %s\n", debugstr_w(mi->cabinet));
547 rc = ERROR_INSTALL_FAILURE;
548 goto done;
552 if (!patch->IsApplied && !(patch->Attributes & msidbPatchAttributesNonVital))
554 ERR("Failed to apply patch to file: %s\n", debugstr_w(file->File));
555 rc = ERROR_INSTALL_FAILURE;
556 goto done;
560 done:
561 msi_free_media_info(mi);
562 if (mspatcha_loaded)
563 unload_mspatch();
564 return rc;
567 #define is_dot_dir(x) ((x[0] == '.') && ((x[1] == 0) || ((x[1] == '.') && (x[2] == 0))))
569 typedef struct
571 struct list entry;
572 LPWSTR sourcename;
573 LPWSTR destname;
574 LPWSTR source;
575 LPWSTR dest;
576 } FILE_LIST;
578 static BOOL msi_move_file(LPCWSTR source, LPCWSTR dest, int options)
580 BOOL ret;
582 if (GetFileAttributesW(source) == FILE_ATTRIBUTE_DIRECTORY ||
583 GetFileAttributesW(dest) == FILE_ATTRIBUTE_DIRECTORY)
585 WARN("Source or dest is directory, not moving\n");
586 return FALSE;
589 if (options == msidbMoveFileOptionsMove)
591 TRACE("moving %s -> %s\n", debugstr_w(source), debugstr_w(dest));
592 ret = MoveFileExW(source, dest, MOVEFILE_REPLACE_EXISTING);
593 if (!ret)
595 WARN("MoveFile failed: %d\n", GetLastError());
596 return FALSE;
599 else
601 TRACE("copying %s -> %s\n", debugstr_w(source), debugstr_w(dest));
602 ret = CopyFileW(source, dest, FALSE);
603 if (!ret)
605 WARN("CopyFile failed: %d\n", GetLastError());
606 return FALSE;
610 return TRUE;
613 static LPWSTR wildcard_to_file(LPWSTR wildcard, LPWSTR filename)
615 LPWSTR path, ptr;
616 DWORD dirlen, pathlen;
618 ptr = strrchrW(wildcard, '\\');
619 dirlen = ptr - wildcard + 1;
621 pathlen = dirlen + lstrlenW(filename) + 1;
622 path = msi_alloc(pathlen * sizeof(WCHAR));
624 lstrcpynW(path, wildcard, dirlen + 1);
625 lstrcatW(path, filename);
627 return path;
630 static void free_file_entry(FILE_LIST *file)
632 msi_free(file->source);
633 msi_free(file->dest);
634 msi_free(file);
637 static void free_list(FILE_LIST *list)
639 while (!list_empty(&list->entry))
641 FILE_LIST *file = LIST_ENTRY(list_head(&list->entry), FILE_LIST, entry);
643 list_remove(&file->entry);
644 free_file_entry(file);
648 static BOOL add_wildcard(FILE_LIST *files, LPWSTR source, LPWSTR dest)
650 FILE_LIST *new, *file;
651 LPWSTR ptr, filename;
652 DWORD size;
654 new = msi_alloc_zero(sizeof(FILE_LIST));
655 if (!new)
656 return FALSE;
658 new->source = strdupW(source);
659 ptr = strrchrW(dest, '\\') + 1;
660 filename = strrchrW(new->source, '\\') + 1;
662 new->sourcename = filename;
664 if (*ptr)
665 new->destname = ptr;
666 else
667 new->destname = new->sourcename;
669 size = (ptr - dest) + lstrlenW(filename) + 1;
670 new->dest = msi_alloc(size * sizeof(WCHAR));
671 if (!new->dest)
673 free_file_entry(new);
674 return FALSE;
677 lstrcpynW(new->dest, dest, ptr - dest + 1);
678 lstrcatW(new->dest, filename);
680 if (list_empty(&files->entry))
682 list_add_head(&files->entry, &new->entry);
683 return TRUE;
686 LIST_FOR_EACH_ENTRY(file, &files->entry, FILE_LIST, entry)
688 if (strcmpW( source, file->source ) < 0)
690 list_add_before(&file->entry, &new->entry);
691 return TRUE;
695 list_add_after(&file->entry, &new->entry);
696 return TRUE;
699 static BOOL move_files_wildcard(LPWSTR source, LPWSTR dest, int options)
701 WIN32_FIND_DATAW wfd;
702 HANDLE hfile;
703 LPWSTR path;
704 BOOL res;
705 FILE_LIST files, *file;
706 DWORD size;
708 hfile = FindFirstFileW(source, &wfd);
709 if (hfile == INVALID_HANDLE_VALUE) return FALSE;
711 list_init(&files.entry);
713 for (res = TRUE; res; res = FindNextFileW(hfile, &wfd))
715 if (is_dot_dir(wfd.cFileName)) continue;
717 path = wildcard_to_file(source, wfd.cFileName);
718 if (!path)
720 res = FALSE;
721 goto done;
724 add_wildcard(&files, path, dest);
725 msi_free(path);
728 /* no files match the wildcard */
729 if (list_empty(&files.entry))
730 goto done;
732 /* only the first wildcard match gets renamed to dest */
733 file = LIST_ENTRY(list_head(&files.entry), FILE_LIST, entry);
734 size = (strrchrW(file->dest, '\\') - file->dest) + lstrlenW(file->destname) + 2;
735 file->dest = msi_realloc(file->dest, size * sizeof(WCHAR));
736 if (!file->dest)
738 res = FALSE;
739 goto done;
742 /* file->dest may be shorter after the reallocation, so add a NULL
743 * terminator. This is needed for the call to strrchrW, as there will no
744 * longer be a NULL terminator within the bounds of the allocation in this case.
746 file->dest[size - 1] = '\0';
747 lstrcpyW(strrchrW(file->dest, '\\') + 1, file->destname);
749 while (!list_empty(&files.entry))
751 file = LIST_ENTRY(list_head(&files.entry), FILE_LIST, entry);
753 msi_move_file(file->source, file->dest, options);
755 list_remove(&file->entry);
756 free_file_entry(file);
759 res = TRUE;
761 done:
762 free_list(&files);
763 FindClose(hfile);
764 return res;
767 void msi_reduce_to_long_filename( WCHAR *filename )
769 WCHAR *p = strchrW( filename, '|' );
770 if (p) memmove( filename, p + 1, (strlenW( p + 1 ) + 1) * sizeof(WCHAR) );
773 static UINT ITERATE_MoveFiles( MSIRECORD *rec, LPVOID param )
775 MSIPACKAGE *package = param;
776 MSIRECORD *uirow;
777 MSICOMPONENT *comp;
778 LPCWSTR sourcename, component;
779 LPWSTR sourcedir, destname = NULL, destdir = NULL, source = NULL, dest = NULL;
780 int options;
781 DWORD size;
782 BOOL ret, wildcards;
784 component = MSI_RecordGetString(rec, 2);
785 comp = msi_get_loaded_component(package, component);
786 if (!comp)
787 return ERROR_SUCCESS;
789 comp->Action = msi_get_component_action( package, comp );
790 if (comp->Action != INSTALLSTATE_LOCAL)
792 TRACE("component not scheduled for installation %s\n", debugstr_w(component));
793 return ERROR_SUCCESS;
796 sourcename = MSI_RecordGetString(rec, 3);
797 options = MSI_RecordGetInteger(rec, 7);
799 sourcedir = msi_dup_property(package->db, MSI_RecordGetString(rec, 5));
800 if (!sourcedir)
801 goto done;
803 destdir = msi_dup_property(package->db, MSI_RecordGetString(rec, 6));
804 if (!destdir)
805 goto done;
807 if (!sourcename)
809 if (GetFileAttributesW(sourcedir) == INVALID_FILE_ATTRIBUTES)
810 goto done;
812 source = strdupW(sourcedir);
813 if (!source)
814 goto done;
816 else
818 size = lstrlenW(sourcedir) + lstrlenW(sourcename) + 2;
819 source = msi_alloc(size * sizeof(WCHAR));
820 if (!source)
821 goto done;
823 lstrcpyW(source, sourcedir);
824 if (source[lstrlenW(source) - 1] != '\\')
825 lstrcatW(source, szBackSlash);
826 lstrcatW(source, sourcename);
829 wildcards = strchrW(source, '*') || strchrW(source, '?');
831 if (MSI_RecordIsNull(rec, 4))
833 if (!wildcards)
835 destname = strdupW(sourcename);
836 if (!destname)
837 goto done;
840 else
842 destname = strdupW(MSI_RecordGetString(rec, 4));
843 if (destname) msi_reduce_to_long_filename(destname);
846 size = 0;
847 if (destname)
848 size = lstrlenW(destname);
850 size += lstrlenW(destdir) + 2;
851 dest = msi_alloc(size * sizeof(WCHAR));
852 if (!dest)
853 goto done;
855 lstrcpyW(dest, destdir);
856 if (dest[lstrlenW(dest) - 1] != '\\')
857 lstrcatW(dest, szBackSlash);
859 if (destname)
860 lstrcatW(dest, destname);
862 if (GetFileAttributesW(destdir) == INVALID_FILE_ATTRIBUTES)
864 if (!(ret = msi_create_full_path(destdir)))
866 WARN("failed to create directory %u\n", GetLastError());
867 goto done;
871 if (!wildcards)
872 msi_move_file(source, dest, options);
873 else
874 move_files_wildcard(source, dest, options);
876 done:
877 uirow = MSI_CreateRecord( 9 );
878 MSI_RecordSetStringW( uirow, 1, MSI_RecordGetString(rec, 1) );
879 MSI_RecordSetInteger( uirow, 6, 1 ); /* FIXME */
880 MSI_RecordSetStringW( uirow, 9, destdir );
881 msi_ui_actiondata( package, szMoveFiles, uirow );
882 msiobj_release( &uirow->hdr );
884 msi_free(sourcedir);
885 msi_free(destdir);
886 msi_free(destname);
887 msi_free(source);
888 msi_free(dest);
890 return ERROR_SUCCESS;
893 UINT ACTION_MoveFiles( MSIPACKAGE *package )
895 static const WCHAR query[] = {
896 'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
897 '`','M','o','v','e','F','i','l','e','`',0};
898 MSIQUERY *view;
899 UINT rc;
901 rc = MSI_DatabaseOpenViewW(package->db, query, &view);
902 if (rc != ERROR_SUCCESS)
903 return ERROR_SUCCESS;
905 rc = MSI_IterateRecords(view, NULL, ITERATE_MoveFiles, package);
906 msiobj_release(&view->hdr);
907 return rc;
910 static WCHAR *get_duplicate_filename( MSIPACKAGE *package, MSIRECORD *row, const WCHAR *file_key, const WCHAR *src )
912 DWORD len;
913 WCHAR *dst_name, *dst_path, *dst;
915 if (MSI_RecordIsNull( row, 4 ))
917 len = strlenW( src ) + 1;
918 if (!(dst_name = msi_alloc( len * sizeof(WCHAR)))) return NULL;
919 strcpyW( dst_name, strrchrW( src, '\\' ) + 1 );
921 else
923 MSI_RecordGetStringW( row, 4, NULL, &len );
924 if (!(dst_name = msi_alloc( ++len * sizeof(WCHAR) ))) return NULL;
925 MSI_RecordGetStringW( row, 4, dst_name, &len );
926 msi_reduce_to_long_filename( dst_name );
929 if (MSI_RecordIsNull( row, 5 ))
931 WCHAR *p;
932 dst_path = strdupW( src );
933 p = strrchrW( dst_path, '\\' );
934 if (p) *p = 0;
936 else
938 const WCHAR *dst_key = MSI_RecordGetString( row, 5 );
940 dst_path = strdupW( msi_get_target_folder( package, dst_key ) );
941 if (!dst_path)
943 /* try a property */
944 dst_path = msi_dup_property( package->db, dst_key );
945 if (!dst_path)
947 FIXME("Unable to get destination folder, try AppSearch properties\n");
948 msi_free( dst_name );
949 return NULL;
954 dst = msi_build_directory_name( 2, dst_path, dst_name );
955 msi_create_full_path( dst_path );
957 msi_free( dst_name );
958 msi_free( dst_path );
959 return dst;
962 static UINT ITERATE_DuplicateFiles(MSIRECORD *row, LPVOID param)
964 MSIPACKAGE *package = param;
965 LPWSTR dest;
966 LPCWSTR file_key, component;
967 MSICOMPONENT *comp;
968 MSIRECORD *uirow;
969 MSIFILE *file;
971 component = MSI_RecordGetString(row,2);
972 comp = msi_get_loaded_component(package, component);
973 if (!comp)
974 return ERROR_SUCCESS;
976 comp->Action = msi_get_component_action( package, comp );
977 if (comp->Action != INSTALLSTATE_LOCAL)
979 TRACE("component not scheduled for installation %s\n", debugstr_w(component));
980 return ERROR_SUCCESS;
983 file_key = MSI_RecordGetString(row,3);
984 if (!file_key)
986 ERR("Unable to get file key\n");
987 return ERROR_FUNCTION_FAILED;
990 file = msi_get_loaded_file( package, file_key );
991 if (!file)
993 ERR("Original file unknown %s\n", debugstr_w(file_key));
994 return ERROR_SUCCESS;
997 dest = get_duplicate_filename( package, row, file_key, file->TargetPath );
998 if (!dest)
1000 WARN("Unable to get duplicate filename\n");
1001 return ERROR_SUCCESS;
1004 TRACE("Duplicating file %s to %s\n", debugstr_w(file->TargetPath), debugstr_w(dest));
1006 if (!CopyFileW( file->TargetPath, dest, TRUE ))
1008 WARN("Failed to copy file %s -> %s (%u)\n",
1009 debugstr_w(file->TargetPath), debugstr_w(dest), GetLastError());
1012 FIXME("We should track these duplicate files as well\n");
1014 uirow = MSI_CreateRecord( 9 );
1015 MSI_RecordSetStringW( uirow, 1, MSI_RecordGetString( row, 1 ) );
1016 MSI_RecordSetInteger( uirow, 6, file->FileSize );
1017 MSI_RecordSetStringW( uirow, 9, MSI_RecordGetString( row, 5 ) );
1018 msi_ui_actiondata( package, szDuplicateFiles, uirow );
1019 msiobj_release( &uirow->hdr );
1021 msi_free(dest);
1022 return ERROR_SUCCESS;
1025 UINT ACTION_DuplicateFiles(MSIPACKAGE *package)
1027 static const WCHAR query[] = {
1028 'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
1029 '`','D','u','p','l','i','c','a','t','e','F','i','l','e','`',0};
1030 MSIQUERY *view;
1031 UINT rc;
1033 rc = MSI_DatabaseOpenViewW(package->db, query, &view);
1034 if (rc != ERROR_SUCCESS)
1035 return ERROR_SUCCESS;
1037 rc = MSI_IterateRecords(view, NULL, ITERATE_DuplicateFiles, package);
1038 msiobj_release(&view->hdr);
1039 return rc;
1042 static UINT ITERATE_RemoveDuplicateFiles( MSIRECORD *row, LPVOID param )
1044 MSIPACKAGE *package = param;
1045 LPWSTR dest;
1046 LPCWSTR file_key, component;
1047 MSICOMPONENT *comp;
1048 MSIRECORD *uirow;
1049 MSIFILE *file;
1051 component = MSI_RecordGetString( row, 2 );
1052 comp = msi_get_loaded_component( package, component );
1053 if (!comp)
1054 return ERROR_SUCCESS;
1056 comp->Action = msi_get_component_action( package, comp );
1057 if (comp->Action != INSTALLSTATE_ABSENT)
1059 TRACE("component not scheduled for removal %s\n", debugstr_w(component));
1060 return ERROR_SUCCESS;
1063 file_key = MSI_RecordGetString( row, 3 );
1064 if (!file_key)
1066 ERR("Unable to get file key\n");
1067 return ERROR_FUNCTION_FAILED;
1070 file = msi_get_loaded_file( package, file_key );
1071 if (!file)
1073 ERR("Original file unknown %s\n", debugstr_w(file_key));
1074 return ERROR_SUCCESS;
1077 dest = get_duplicate_filename( package, row, file_key, file->TargetPath );
1078 if (!dest)
1080 WARN("Unable to get duplicate filename\n");
1081 return ERROR_SUCCESS;
1084 TRACE("Removing duplicate %s of %s\n", debugstr_w(dest), debugstr_w(file->TargetPath));
1086 if (!DeleteFileW( dest ))
1088 WARN("Failed to delete duplicate file %s (%u)\n", debugstr_w(dest), GetLastError());
1091 uirow = MSI_CreateRecord( 9 );
1092 MSI_RecordSetStringW( uirow, 1, MSI_RecordGetString( row, 1 ) );
1093 MSI_RecordSetStringW( uirow, 9, MSI_RecordGetString( row, 5 ) );
1094 msi_ui_actiondata( package, szRemoveDuplicateFiles, uirow );
1095 msiobj_release( &uirow->hdr );
1097 msi_free(dest);
1098 return ERROR_SUCCESS;
1101 UINT ACTION_RemoveDuplicateFiles( MSIPACKAGE *package )
1103 static const WCHAR query[] = {
1104 'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
1105 '`','D','u','p','l','i','c','a','t','e','F','i','l','e','`',0};
1106 MSIQUERY *view;
1107 UINT rc;
1109 rc = MSI_DatabaseOpenViewW( package->db, query, &view );
1110 if (rc != ERROR_SUCCESS)
1111 return ERROR_SUCCESS;
1113 rc = MSI_IterateRecords( view, NULL, ITERATE_RemoveDuplicateFiles, package );
1114 msiobj_release( &view->hdr );
1115 return rc;
1118 static BOOL verify_comp_for_removal(MSICOMPONENT *comp, UINT install_mode)
1120 /* special case */
1121 if (comp->Action != INSTALLSTATE_SOURCE &&
1122 comp->Attributes & msidbComponentAttributesSourceOnly &&
1123 (install_mode == msidbRemoveFileInstallModeOnRemove ||
1124 install_mode == msidbRemoveFileInstallModeOnBoth)) return TRUE;
1126 switch (comp->Action)
1128 case INSTALLSTATE_LOCAL:
1129 case INSTALLSTATE_SOURCE:
1130 if (install_mode == msidbRemoveFileInstallModeOnInstall ||
1131 install_mode == msidbRemoveFileInstallModeOnBoth) return TRUE;
1132 break;
1133 case INSTALLSTATE_ABSENT:
1134 if (install_mode == msidbRemoveFileInstallModeOnRemove ||
1135 install_mode == msidbRemoveFileInstallModeOnBoth) return TRUE;
1136 break;
1137 default: break;
1139 return FALSE;
1142 static UINT ITERATE_RemoveFiles(MSIRECORD *row, LPVOID param)
1144 MSIPACKAGE *package = param;
1145 MSICOMPONENT *comp;
1146 MSIRECORD *uirow;
1147 LPCWSTR component, dirprop;
1148 UINT install_mode;
1149 LPWSTR dir = NULL, path = NULL, filename = NULL;
1150 DWORD size;
1151 UINT ret = ERROR_SUCCESS;
1153 component = MSI_RecordGetString(row, 2);
1154 dirprop = MSI_RecordGetString(row, 4);
1155 install_mode = MSI_RecordGetInteger(row, 5);
1157 comp = msi_get_loaded_component(package, component);
1158 if (!comp)
1159 return ERROR_SUCCESS;
1161 comp->Action = msi_get_component_action( package, comp );
1162 if (!verify_comp_for_removal(comp, install_mode))
1164 TRACE("Skipping removal due to install mode\n");
1165 return ERROR_SUCCESS;
1167 if (comp->assembly && !comp->assembly->application)
1169 return ERROR_SUCCESS;
1171 if (comp->Attributes & msidbComponentAttributesPermanent)
1173 TRACE("permanent component, not removing file\n");
1174 return ERROR_SUCCESS;
1177 dir = msi_dup_property(package->db, dirprop);
1178 if (!dir)
1180 WARN("directory property has no value\n");
1181 return ERROR_SUCCESS;
1183 size = 0;
1184 if ((filename = strdupW( MSI_RecordGetString(row, 3) )))
1186 msi_reduce_to_long_filename( filename );
1187 size = lstrlenW( filename );
1189 size += lstrlenW(dir) + 2;
1190 path = msi_alloc(size * sizeof(WCHAR));
1191 if (!path)
1193 ret = ERROR_OUTOFMEMORY;
1194 goto done;
1197 if (filename)
1199 lstrcpyW(path, dir);
1200 PathAddBackslashW(path);
1201 lstrcatW(path, filename);
1203 TRACE("Deleting misc file: %s\n", debugstr_w(path));
1204 DeleteFileW(path);
1206 else
1208 TRACE("Removing misc directory: %s\n", debugstr_w(dir));
1209 RemoveDirectoryW(dir);
1212 done:
1213 uirow = MSI_CreateRecord( 9 );
1214 MSI_RecordSetStringW( uirow, 1, MSI_RecordGetString(row, 1) );
1215 MSI_RecordSetStringW( uirow, 9, dir );
1216 msi_ui_actiondata( package, szRemoveFiles, uirow );
1217 msiobj_release( &uirow->hdr );
1219 msi_free(filename);
1220 msi_free(path);
1221 msi_free(dir);
1222 return ret;
1225 static void remove_folder( MSIFOLDER *folder )
1227 FolderList *fl;
1229 LIST_FOR_EACH_ENTRY( fl, &folder->children, FolderList, entry )
1231 remove_folder( fl->folder );
1233 if (!folder->persistent && folder->State != FOLDER_STATE_REMOVED)
1235 if (RemoveDirectoryW( folder->ResolvedTarget )) folder->State = FOLDER_STATE_REMOVED;
1239 UINT ACTION_RemoveFiles( MSIPACKAGE *package )
1241 static const WCHAR query[] = {
1242 'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
1243 '`','R','e','m','o','v','e','F','i','l','e','`',0};
1244 MSIQUERY *view;
1245 MSICOMPONENT *comp;
1246 MSIFILE *file;
1247 UINT r;
1249 r = MSI_DatabaseOpenViewW(package->db, query, &view);
1250 if (r == ERROR_SUCCESS)
1252 r = MSI_IterateRecords(view, NULL, ITERATE_RemoveFiles, package);
1253 msiobj_release(&view->hdr);
1254 if (r != ERROR_SUCCESS)
1255 return r;
1258 LIST_FOR_EACH_ENTRY( file, &package->files, MSIFILE, entry )
1260 MSIRECORD *uirow;
1261 VS_FIXEDFILEINFO *ver;
1263 comp = file->Component;
1264 msi_file_update_ui( package, file, szRemoveFiles );
1266 comp->Action = msi_get_component_action( package, comp );
1267 if (comp->Action != INSTALLSTATE_ABSENT || comp->Installed == INSTALLSTATE_SOURCE)
1268 continue;
1270 if (comp->assembly && !comp->assembly->application)
1271 continue;
1273 if (comp->Attributes & msidbComponentAttributesPermanent)
1275 TRACE("permanent component, not removing file\n");
1276 continue;
1279 if (file->Version)
1281 ver = msi_get_disk_file_version( file->TargetPath );
1282 if (ver && msi_compare_file_versions( ver, file->Version ) > 0)
1284 TRACE("newer version detected, not removing file\n");
1285 msi_free( ver );
1286 continue;
1288 msi_free( ver );
1291 if (file->state == msifs_installed)
1292 WARN("removing installed file %s\n", debugstr_w(file->TargetPath));
1294 TRACE("removing %s\n", debugstr_w(file->File) );
1296 SetFileAttributesW( file->TargetPath, FILE_ATTRIBUTE_NORMAL );
1297 if (!DeleteFileW( file->TargetPath ))
1299 WARN("failed to delete %s (%u)\n", debugstr_w(file->TargetPath), GetLastError());
1301 file->state = msifs_missing;
1303 uirow = MSI_CreateRecord( 9 );
1304 MSI_RecordSetStringW( uirow, 1, file->FileName );
1305 MSI_RecordSetStringW( uirow, 9, comp->Directory );
1306 msi_ui_actiondata( package, szRemoveFiles, uirow );
1307 msiobj_release( &uirow->hdr );
1310 LIST_FOR_EACH_ENTRY( comp, &package->components, MSICOMPONENT, entry )
1312 comp->Action = msi_get_component_action( package, comp );
1313 if (comp->Action != INSTALLSTATE_ABSENT) continue;
1315 if (comp->Attributes & msidbComponentAttributesPermanent)
1317 TRACE("permanent component, not removing directory\n");
1318 continue;
1320 if (comp->assembly && !comp->assembly->application)
1321 msi_uninstall_assembly( package, comp );
1322 else
1324 MSIFOLDER *folder = msi_get_loaded_folder( package, comp->Directory );
1325 remove_folder( folder );
1328 return ERROR_SUCCESS;