msi: Create temp files in the destination directory instead of the root.
[wine/multimedia.git] / dlls / msi / files.c
blob94e5e6cdbf988dbf68e3a51a8ee769e27d7003a0
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 = 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;
156 msi_ui_progress( package, 2, file->FileSize, 0, 0 );
160 static UINT copy_file(MSIFILE *file, LPWSTR source)
162 BOOL ret;
164 ret = CopyFileW(source, file->TargetPath, FALSE);
165 if (!ret)
166 return GetLastError();
168 SetFileAttributesW(file->TargetPath, FILE_ATTRIBUTE_NORMAL);
170 file->state = msifs_installed;
171 return ERROR_SUCCESS;
174 static UINT copy_install_file(MSIPACKAGE *package, MSIFILE *file, LPWSTR source)
176 UINT gle;
178 TRACE("Copying %s to %s\n", debugstr_w(source), debugstr_w(file->TargetPath));
180 gle = copy_file(file, source);
181 if (gle == ERROR_SUCCESS)
182 return gle;
184 if (gle == ERROR_ALREADY_EXISTS && file->state == msifs_overwrite)
186 TRACE("overwriting existing file\n");
187 return ERROR_SUCCESS;
189 else if (gle == ERROR_ACCESS_DENIED)
191 SetFileAttributesW(file->TargetPath, FILE_ATTRIBUTE_NORMAL);
193 gle = copy_file(file, source);
194 TRACE("Overwriting existing file: %d\n", gle);
196 if (gle == ERROR_SHARING_VIOLATION || gle == ERROR_USER_MAPPED_FILE)
198 WCHAR *tmpfileW, *pathW, *p;
199 DWORD len;
201 TRACE("file in use, scheduling rename operation\n");
203 if (!(pathW = strdupW( file->TargetPath ))) return ERROR_OUTOFMEMORY;
204 if ((p = strrchrW(pathW, '\\'))) *p = 0;
205 len = strlenW( pathW ) + 16;
206 if (!(tmpfileW = msi_alloc(len * sizeof(WCHAR))))
208 msi_free( pathW );
209 return ERROR_OUTOFMEMORY;
211 if (!GetTempFileNameW( pathW, szMsi, 0, tmpfileW )) tmpfileW[0] = 0;
212 msi_free( pathW );
214 if (CopyFileW(source, tmpfileW, FALSE) &&
215 MoveFileExW(file->TargetPath, NULL, MOVEFILE_DELAY_UNTIL_REBOOT) &&
216 MoveFileExW(tmpfileW, file->TargetPath, MOVEFILE_DELAY_UNTIL_REBOOT))
218 file->state = msifs_installed;
219 package->need_reboot = 1;
220 gle = ERROR_SUCCESS;
222 else
224 gle = GetLastError();
225 WARN("failed to schedule rename operation: %d)\n", gle);
226 DeleteFileW( tmpfileW );
228 msi_free(tmpfileW);
231 return gle;
234 static UINT msi_create_directory( MSIPACKAGE *package, const WCHAR *dir )
236 MSIFOLDER *folder;
237 const WCHAR *install_path;
239 install_path = msi_get_target_folder( package, dir );
240 if (!install_path) return ERROR_FUNCTION_FAILED;
242 folder = msi_get_loaded_folder( package, dir );
243 if (folder->State == 0)
245 msi_create_full_path( install_path );
246 folder->State = 2;
248 return ERROR_SUCCESS;
251 static BOOL installfiles_cb(MSIPACKAGE *package, LPCWSTR file, DWORD action,
252 LPWSTR *path, DWORD *attrs, PVOID user)
254 static MSIFILE *f = NULL;
255 UINT_PTR disk_id = (UINT_PTR)user;
257 if (action == MSICABEXTRACT_BEGINEXTRACT)
259 f = msi_get_loaded_file(package, file);
260 if (!f)
262 TRACE("unknown file in cabinet (%s)\n", debugstr_w(file));
263 return FALSE;
266 if (f->disk_id != disk_id || (f->state != msifs_missing && f->state != msifs_overwrite))
267 return FALSE;
269 msi_file_update_ui(package, f, szInstallFiles);
270 if (!f->Component->assembly || f->Component->assembly->application)
272 msi_create_directory(package, f->Component->Directory);
274 *path = strdupW(f->TargetPath);
275 *attrs = f->Attributes;
277 else if (action == MSICABEXTRACT_FILEEXTRACTED)
279 f->state = msifs_installed;
280 f = NULL;
283 return TRUE;
286 WCHAR *msi_resolve_file_source( MSIPACKAGE *package, MSIFILE *file )
288 WCHAR *p, *path;
290 TRACE("Working to resolve source of file %s\n", debugstr_w(file->File));
292 if (file->IsCompressed) return NULL;
294 p = msi_resolve_source_folder( package, file->Component->Directory, NULL );
295 path = msi_build_directory_name( 2, p, file->ShortName );
297 if (file->LongName && GetFileAttributesW( path ) == INVALID_FILE_ATTRIBUTES)
299 msi_free( path );
300 path = msi_build_directory_name( 2, p, file->LongName );
302 msi_free( p );
303 TRACE("file %s source resolves to %s\n", debugstr_w(file->File), debugstr_w(path));
304 return path;
308 * ACTION_InstallFiles()
310 * For efficiency, this is done in two passes:
311 * 1) Correct all the TargetPaths and determine what files are to be installed.
312 * 2) Extract Cabinets and copy files.
314 UINT ACTION_InstallFiles(MSIPACKAGE *package)
316 MSIMEDIAINFO *mi;
317 MSICOMPONENT *comp;
318 UINT rc = ERROR_SUCCESS;
319 MSIFILE *file;
321 /* increment progress bar each time action data is sent */
322 msi_ui_progress( package, 1, 1, 0, 0 );
324 schedule_install_files(package);
325 mi = msi_alloc_zero( sizeof(MSIMEDIAINFO) );
327 LIST_FOR_EACH_ENTRY( file, &package->files, MSIFILE, entry )
329 rc = msi_load_media_info( package, file->Sequence, mi );
330 if (rc != ERROR_SUCCESS)
332 ERR("Unable to load media info for %s (%u)\n", debugstr_w(file->File), rc);
333 return ERROR_FUNCTION_FAILED;
335 if (!file->Component->Enabled) continue;
337 if (file->state != msifs_hashmatch &&
338 (rc = ready_media( package, file->Sequence, file->IsCompressed, mi )))
340 ERR("Failed to ready media for %s\n", debugstr_w(file->File));
341 goto done;
344 if (file->state != msifs_missing && !mi->is_continuous && file->state != msifs_overwrite)
345 continue;
347 if (file->Sequence > mi->last_sequence || mi->is_continuous ||
348 (file->IsCompressed && !mi->is_extracted))
350 MSICABDATA data;
352 data.mi = mi;
353 data.package = package;
354 data.cb = installfiles_cb;
355 data.user = (PVOID)(UINT_PTR)mi->disk_id;
357 if (file->IsCompressed &&
358 !msi_cabextract(package, mi, &data))
360 ERR("Failed to extract cabinet: %s\n", debugstr_w(mi->cabinet));
361 rc = ERROR_INSTALL_FAILURE;
362 goto done;
366 if (!file->IsCompressed)
368 WCHAR *source = msi_resolve_file_source(package, file);
370 TRACE("copying %s to %s\n", debugstr_w(source), debugstr_w(file->TargetPath));
372 msi_file_update_ui(package, file, szInstallFiles);
373 if (!file->Component->assembly || file->Component->assembly->application)
375 msi_create_directory(package, file->Component->Directory);
377 rc = copy_install_file(package, file, source);
378 if (rc != ERROR_SUCCESS)
380 ERR("Failed to copy %s to %s (%d)\n", debugstr_w(source),
381 debugstr_w(file->TargetPath), rc);
382 rc = ERROR_INSTALL_FAILURE;
383 msi_free(source);
384 goto done;
386 msi_free(source);
388 else if (file->state != msifs_installed && !(file->Attributes & msidbFileAttributesPatchAdded))
390 ERR("compressed file wasn't installed (%s)\n", debugstr_w(file->TargetPath));
391 rc = ERROR_INSTALL_FAILURE;
392 goto done;
395 msi_init_assembly_caches( package );
396 LIST_FOR_EACH_ENTRY( comp, &package->components, MSICOMPONENT, entry )
398 comp->Action = msi_get_component_action( package, comp );
399 if (comp->Action == INSTALLSTATE_LOCAL && comp->assembly && !comp->assembly->installed)
401 rc = msi_install_assembly( package, comp );
402 if (rc != ERROR_SUCCESS)
404 ERR("Failed to install assembly\n");
405 rc = ERROR_INSTALL_FAILURE;
406 break;
410 msi_destroy_assembly_caches( package );
412 done:
413 msi_free_media_info(mi);
414 return rc;
417 static BOOL load_mspatcha(void)
419 hmspatcha = LoadLibraryA("mspatcha.dll");
420 if (!hmspatcha)
422 ERR("Failed to load mspatcha.dll: %d\n", GetLastError());
423 return FALSE;
426 ApplyPatchToFileW = (void*)GetProcAddress(hmspatcha, "ApplyPatchToFileW");
427 if(!ApplyPatchToFileW)
429 ERR("GetProcAddress(ApplyPatchToFileW) failed: %d.\n", GetLastError());
430 return FALSE;
433 return TRUE;
436 static void unload_mspatch(void)
438 FreeLibrary(hmspatcha);
441 static BOOL patchfiles_cb(MSIPACKAGE *package, LPCWSTR file, DWORD action,
442 LPWSTR *path, DWORD *attrs, PVOID user)
444 static MSIFILEPATCH *p = NULL;
445 static WCHAR patch_path[MAX_PATH] = {0};
446 static WCHAR temp_folder[MAX_PATH] = {0};
448 if (action == MSICABEXTRACT_BEGINEXTRACT)
450 if (temp_folder[0] == '\0')
451 GetTempPathW(MAX_PATH, temp_folder);
453 p = msi_get_loaded_filepatch(package, file);
454 if (!p)
456 TRACE("unknown file in cabinet (%s)\n", debugstr_w(file));
457 return FALSE;
460 msi_file_update_ui(package, p->File, szPatchFiles);
462 GetTempFileNameW(temp_folder, NULL, 0, patch_path);
464 *path = strdupW(patch_path);
465 *attrs = p->File->Attributes;
467 else if (action == MSICABEXTRACT_FILEEXTRACTED)
469 WCHAR patched_file[MAX_PATH];
470 BOOL br;
472 GetTempFileNameW(temp_folder, NULL, 0, patched_file);
474 br = ApplyPatchToFileW(patch_path, p->File->TargetPath, patched_file, 0);
475 if (br)
477 /* FIXME: baseline cache */
479 DeleteFileW( p->File->TargetPath );
480 MoveFileW( patched_file, p->File->TargetPath );
482 p->IsApplied = TRUE;
484 else
485 ERR("Failed patch %s: %d.\n", debugstr_w(p->File->TargetPath), GetLastError());
487 DeleteFileW(patch_path);
488 p = NULL;
491 return TRUE;
494 UINT ACTION_PatchFiles( MSIPACKAGE *package )
496 MSIFILEPATCH *patch;
497 MSIMEDIAINFO *mi;
498 UINT rc = ERROR_SUCCESS;
499 BOOL mspatcha_loaded = FALSE;
501 TRACE("%p\n", package);
503 /* increment progress bar each time action data is sent */
504 msi_ui_progress( package, 1, 1, 0, 0 );
506 mi = msi_alloc_zero( sizeof(MSIMEDIAINFO) );
508 LIST_FOR_EACH_ENTRY( patch, &package->filepatches, MSIFILEPATCH, entry )
510 MSIFILE *file = patch->File;
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 if (!file->Component->Enabled) continue;
520 if (!patch->IsApplied)
522 MSICABDATA data;
524 rc = ready_media( package, patch->Sequence, TRUE, mi );
525 if (rc != ERROR_SUCCESS)
527 ERR("Failed to ready media for %s\n", debugstr_w(file->File));
528 goto done;
531 if (!mspatcha_loaded && !load_mspatcha())
533 rc = ERROR_FUNCTION_FAILED;
534 goto done;
536 mspatcha_loaded = TRUE;
538 data.mi = mi;
539 data.package = package;
540 data.cb = patchfiles_cb;
541 data.user = (PVOID)(UINT_PTR)mi->disk_id;
543 if (!msi_cabextract(package, mi, &data))
545 ERR("Failed to extract cabinet: %s\n", debugstr_w(mi->cabinet));
546 rc = ERROR_INSTALL_FAILURE;
547 goto done;
551 if (!patch->IsApplied && !(patch->Attributes & msidbPatchAttributesNonVital))
553 ERR("Failed to apply patch to file: %s\n", debugstr_w(file->File));
554 rc = ERROR_INSTALL_FAILURE;
555 goto done;
559 done:
560 msi_free_media_info(mi);
561 if (mspatcha_loaded)
562 unload_mspatch();
563 return rc;
566 #define is_dot_dir(x) ((x[0] == '.') && ((x[1] == 0) || ((x[1] == '.') && (x[2] == 0))))
568 typedef struct
570 struct list entry;
571 LPWSTR sourcename;
572 LPWSTR destname;
573 LPWSTR source;
574 LPWSTR dest;
575 } FILE_LIST;
577 static BOOL msi_move_file(LPCWSTR source, LPCWSTR dest, int options)
579 BOOL ret;
581 if (GetFileAttributesW(source) == FILE_ATTRIBUTE_DIRECTORY ||
582 GetFileAttributesW(dest) == FILE_ATTRIBUTE_DIRECTORY)
584 WARN("Source or dest is directory, not moving\n");
585 return FALSE;
588 if (options == msidbMoveFileOptionsMove)
590 TRACE("moving %s -> %s\n", debugstr_w(source), debugstr_w(dest));
591 ret = MoveFileExW(source, dest, MOVEFILE_REPLACE_EXISTING);
592 if (!ret)
594 WARN("MoveFile failed: %d\n", GetLastError());
595 return FALSE;
598 else
600 TRACE("copying %s -> %s\n", debugstr_w(source), debugstr_w(dest));
601 ret = CopyFileW(source, dest, FALSE);
602 if (!ret)
604 WARN("CopyFile failed: %d\n", GetLastError());
605 return FALSE;
609 return TRUE;
612 static LPWSTR wildcard_to_file(LPWSTR wildcard, LPWSTR filename)
614 LPWSTR path, ptr;
615 DWORD dirlen, pathlen;
617 ptr = strrchrW(wildcard, '\\');
618 dirlen = ptr - wildcard + 1;
620 pathlen = dirlen + lstrlenW(filename) + 1;
621 path = msi_alloc(pathlen * sizeof(WCHAR));
623 lstrcpynW(path, wildcard, dirlen + 1);
624 lstrcatW(path, filename);
626 return path;
629 static void free_file_entry(FILE_LIST *file)
631 msi_free(file->source);
632 msi_free(file->dest);
633 msi_free(file);
636 static void free_list(FILE_LIST *list)
638 while (!list_empty(&list->entry))
640 FILE_LIST *file = LIST_ENTRY(list_head(&list->entry), FILE_LIST, entry);
642 list_remove(&file->entry);
643 free_file_entry(file);
647 static BOOL add_wildcard(FILE_LIST *files, LPWSTR source, LPWSTR dest)
649 FILE_LIST *new, *file;
650 LPWSTR ptr, filename;
651 DWORD size;
653 new = msi_alloc_zero(sizeof(FILE_LIST));
654 if (!new)
655 return FALSE;
657 new->source = strdupW(source);
658 ptr = strrchrW(dest, '\\') + 1;
659 filename = strrchrW(new->source, '\\') + 1;
661 new->sourcename = filename;
663 if (*ptr)
664 new->destname = ptr;
665 else
666 new->destname = new->sourcename;
668 size = (ptr - dest) + lstrlenW(filename) + 1;
669 new->dest = msi_alloc(size * sizeof(WCHAR));
670 if (!new->dest)
672 free_file_entry(new);
673 return FALSE;
676 lstrcpynW(new->dest, dest, ptr - dest + 1);
677 lstrcatW(new->dest, filename);
679 if (list_empty(&files->entry))
681 list_add_head(&files->entry, &new->entry);
682 return TRUE;
685 LIST_FOR_EACH_ENTRY(file, &files->entry, FILE_LIST, entry)
687 if (strcmpW( source, file->source ) < 0)
689 list_add_before(&file->entry, &new->entry);
690 return TRUE;
694 list_add_after(&file->entry, &new->entry);
695 return TRUE;
698 static BOOL move_files_wildcard(LPWSTR source, LPWSTR dest, int options)
700 WIN32_FIND_DATAW wfd;
701 HANDLE hfile;
702 LPWSTR path;
703 BOOL res;
704 FILE_LIST files, *file;
705 DWORD size;
707 hfile = FindFirstFileW(source, &wfd);
708 if (hfile == INVALID_HANDLE_VALUE) return FALSE;
710 list_init(&files.entry);
712 for (res = TRUE; res; res = FindNextFileW(hfile, &wfd))
714 if (is_dot_dir(wfd.cFileName)) continue;
716 path = wildcard_to_file(source, wfd.cFileName);
717 if (!path)
719 res = FALSE;
720 goto done;
723 add_wildcard(&files, path, dest);
724 msi_free(path);
727 /* no files match the wildcard */
728 if (list_empty(&files.entry))
729 goto done;
731 /* only the first wildcard match gets renamed to dest */
732 file = LIST_ENTRY(list_head(&files.entry), FILE_LIST, entry);
733 size = (strrchrW(file->dest, '\\') - file->dest) + lstrlenW(file->destname) + 2;
734 file->dest = msi_realloc(file->dest, size * sizeof(WCHAR));
735 if (!file->dest)
737 res = FALSE;
738 goto done;
741 /* file->dest may be shorter after the reallocation, so add a NULL
742 * terminator. This is needed for the call to strrchrW, as there will no
743 * longer be a NULL terminator within the bounds of the allocation in this case.
745 file->dest[size - 1] = '\0';
746 lstrcpyW(strrchrW(file->dest, '\\') + 1, file->destname);
748 while (!list_empty(&files.entry))
750 file = LIST_ENTRY(list_head(&files.entry), FILE_LIST, entry);
752 msi_move_file(file->source, file->dest, options);
754 list_remove(&file->entry);
755 free_file_entry(file);
758 res = TRUE;
760 done:
761 free_list(&files);
762 FindClose(hfile);
763 return res;
766 void msi_reduce_to_long_filename( WCHAR *filename )
768 WCHAR *p = strchrW( filename, '|' );
769 if (p) memmove( filename, p + 1, (strlenW( p + 1 ) + 1) * sizeof(WCHAR) );
772 static UINT ITERATE_MoveFiles( MSIRECORD *rec, LPVOID param )
774 MSIPACKAGE *package = param;
775 MSIRECORD *uirow;
776 MSICOMPONENT *comp;
777 LPCWSTR sourcename, component;
778 LPWSTR sourcedir, destname = NULL, destdir = NULL, source = NULL, dest = NULL;
779 int options;
780 DWORD size;
781 BOOL ret, wildcards;
783 component = MSI_RecordGetString(rec, 2);
784 comp = msi_get_loaded_component(package, component);
785 if (!comp)
786 return ERROR_SUCCESS;
788 comp->Action = msi_get_component_action( package, comp );
789 if (comp->Action != INSTALLSTATE_LOCAL)
791 TRACE("component not scheduled for installation %s\n", debugstr_w(component));
792 return ERROR_SUCCESS;
795 sourcename = MSI_RecordGetString(rec, 3);
796 options = MSI_RecordGetInteger(rec, 7);
798 sourcedir = msi_dup_property(package->db, MSI_RecordGetString(rec, 5));
799 if (!sourcedir)
800 goto done;
802 destdir = msi_dup_property(package->db, MSI_RecordGetString(rec, 6));
803 if (!destdir)
804 goto done;
806 if (!sourcename)
808 if (GetFileAttributesW(sourcedir) == INVALID_FILE_ATTRIBUTES)
809 goto done;
811 source = strdupW(sourcedir);
812 if (!source)
813 goto done;
815 else
817 size = lstrlenW(sourcedir) + lstrlenW(sourcename) + 2;
818 source = msi_alloc(size * sizeof(WCHAR));
819 if (!source)
820 goto done;
822 lstrcpyW(source, sourcedir);
823 if (source[lstrlenW(source) - 1] != '\\')
824 lstrcatW(source, szBackSlash);
825 lstrcatW(source, sourcename);
828 wildcards = strchrW(source, '*') || strchrW(source, '?');
830 if (MSI_RecordIsNull(rec, 4))
832 if (!wildcards)
834 destname = strdupW(sourcename);
835 if (!destname)
836 goto done;
839 else
841 destname = strdupW(MSI_RecordGetString(rec, 4));
842 if (destname) msi_reduce_to_long_filename(destname);
845 size = 0;
846 if (destname)
847 size = lstrlenW(destname);
849 size += lstrlenW(destdir) + 2;
850 dest = msi_alloc(size * sizeof(WCHAR));
851 if (!dest)
852 goto done;
854 lstrcpyW(dest, destdir);
855 if (dest[lstrlenW(dest) - 1] != '\\')
856 lstrcatW(dest, szBackSlash);
858 if (destname)
859 lstrcatW(dest, destname);
861 if (GetFileAttributesW(destdir) == INVALID_FILE_ATTRIBUTES)
863 if (!(ret = msi_create_full_path(destdir)))
865 WARN("failed to create directory %u\n", GetLastError());
866 goto done;
870 if (!wildcards)
871 msi_move_file(source, dest, options);
872 else
873 move_files_wildcard(source, dest, options);
875 done:
876 uirow = MSI_CreateRecord( 9 );
877 MSI_RecordSetStringW( uirow, 1, MSI_RecordGetString(rec, 1) );
878 MSI_RecordSetInteger( uirow, 6, 1 ); /* FIXME */
879 MSI_RecordSetStringW( uirow, 9, destdir );
880 msi_ui_actiondata( package, szMoveFiles, uirow );
881 msiobj_release( &uirow->hdr );
883 msi_free(sourcedir);
884 msi_free(destdir);
885 msi_free(destname);
886 msi_free(source);
887 msi_free(dest);
889 return ERROR_SUCCESS;
892 UINT ACTION_MoveFiles( MSIPACKAGE *package )
894 UINT rc;
895 MSIQUERY *view;
897 static const WCHAR ExecSeqQuery[] =
898 {'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
899 '`','M','o','v','e','F','i','l','e','`',0};
901 rc = MSI_DatabaseOpenViewW(package->db, ExecSeqQuery, &view);
902 if (rc != ERROR_SUCCESS)
903 return ERROR_SUCCESS;
905 rc = MSI_IterateRecords(view, NULL, ITERATE_MoveFiles, package);
906 msiobj_release(&view->hdr);
908 return rc;
911 static WCHAR *get_duplicate_filename( MSIPACKAGE *package, MSIRECORD *row, const WCHAR *file_key, const WCHAR *src )
913 DWORD len;
914 WCHAR *dst_name, *dst_path, *dst;
916 if (MSI_RecordIsNull( row, 4 ))
918 len = strlenW( src ) + 1;
919 if (!(dst_name = msi_alloc( len * sizeof(WCHAR)))) return NULL;
920 strcpyW( dst_name, strrchrW( src, '\\' ) + 1 );
922 else
924 MSI_RecordGetStringW( row, 4, NULL, &len );
925 if (!(dst_name = msi_alloc( ++len * sizeof(WCHAR) ))) return NULL;
926 MSI_RecordGetStringW( row, 4, dst_name, &len );
927 msi_reduce_to_long_filename( dst_name );
930 if (MSI_RecordIsNull( row, 5 ))
932 WCHAR *p;
933 dst_path = strdupW( src );
934 p = strrchrW( dst_path, '\\' );
935 if (p) *p = 0;
937 else
939 const WCHAR *dst_key = MSI_RecordGetString( row, 5 );
941 dst_path = strdupW( msi_get_target_folder( package, dst_key ) );
942 if (!dst_path)
944 /* try a property */
945 dst_path = msi_dup_property( package->db, dst_key );
946 if (!dst_path)
948 FIXME("Unable to get destination folder, try AppSearch properties\n");
949 msi_free( dst_name );
950 return NULL;
955 dst = msi_build_directory_name( 2, dst_path, dst_name );
956 msi_create_full_path( dst_path );
958 msi_free( dst_name );
959 msi_free( dst_path );
960 return dst;
963 static UINT ITERATE_DuplicateFiles(MSIRECORD *row, LPVOID param)
965 MSIPACKAGE *package = param;
966 LPWSTR dest;
967 LPCWSTR file_key, component;
968 MSICOMPONENT *comp;
969 MSIRECORD *uirow;
970 MSIFILE *file;
972 component = MSI_RecordGetString(row,2);
973 comp = msi_get_loaded_component(package, component);
974 if (!comp)
975 return ERROR_SUCCESS;
977 comp->Action = msi_get_component_action( package, comp );
978 if (comp->Action != INSTALLSTATE_LOCAL)
980 TRACE("component not scheduled for installation %s\n", debugstr_w(component));
981 return ERROR_SUCCESS;
984 file_key = MSI_RecordGetString(row,3);
985 if (!file_key)
987 ERR("Unable to get file key\n");
988 return ERROR_FUNCTION_FAILED;
991 file = msi_get_loaded_file( package, file_key );
992 if (!file)
994 ERR("Original file unknown %s\n", debugstr_w(file_key));
995 return ERROR_SUCCESS;
998 dest = get_duplicate_filename( package, row, file_key, file->TargetPath );
999 if (!dest)
1001 WARN("Unable to get duplicate filename\n");
1002 return ERROR_SUCCESS;
1005 TRACE("Duplicating file %s to %s\n", debugstr_w(file->TargetPath), debugstr_w(dest));
1007 if (!CopyFileW( file->TargetPath, dest, TRUE ))
1009 WARN("Failed to copy file %s -> %s (%u)\n",
1010 debugstr_w(file->TargetPath), debugstr_w(dest), GetLastError());
1013 FIXME("We should track these duplicate files as well\n");
1015 uirow = MSI_CreateRecord( 9 );
1016 MSI_RecordSetStringW( uirow, 1, MSI_RecordGetString( row, 1 ) );
1017 MSI_RecordSetInteger( uirow, 6, file->FileSize );
1018 MSI_RecordSetStringW( uirow, 9, MSI_RecordGetString( row, 5 ) );
1019 msi_ui_actiondata( package, szDuplicateFiles, uirow );
1020 msiobj_release( &uirow->hdr );
1022 msi_free(dest);
1023 return ERROR_SUCCESS;
1026 UINT ACTION_DuplicateFiles(MSIPACKAGE *package)
1028 UINT rc;
1029 MSIQUERY * view;
1030 static const WCHAR ExecSeqQuery[] =
1031 {'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
1032 '`','D','u','p','l','i','c','a','t','e','F','i','l','e','`',0};
1034 rc = MSI_DatabaseOpenViewW(package->db, ExecSeqQuery, &view);
1035 if (rc != ERROR_SUCCESS)
1036 return ERROR_SUCCESS;
1038 rc = MSI_IterateRecords(view, NULL, ITERATE_DuplicateFiles, package);
1039 msiobj_release(&view->hdr);
1041 return rc;
1044 static UINT ITERATE_RemoveDuplicateFiles( MSIRECORD *row, LPVOID param )
1046 MSIPACKAGE *package = param;
1047 LPWSTR dest;
1048 LPCWSTR file_key, component;
1049 MSICOMPONENT *comp;
1050 MSIRECORD *uirow;
1051 MSIFILE *file;
1053 component = MSI_RecordGetString( row, 2 );
1054 comp = msi_get_loaded_component( package, component );
1055 if (!comp)
1056 return ERROR_SUCCESS;
1058 comp->Action = msi_get_component_action( package, comp );
1059 if (comp->Action != INSTALLSTATE_ABSENT)
1061 TRACE("component not scheduled for removal %s\n", debugstr_w(component));
1062 return ERROR_SUCCESS;
1065 file_key = MSI_RecordGetString( row, 3 );
1066 if (!file_key)
1068 ERR("Unable to get file key\n");
1069 return ERROR_FUNCTION_FAILED;
1072 file = msi_get_loaded_file( package, file_key );
1073 if (!file)
1075 ERR("Original file unknown %s\n", debugstr_w(file_key));
1076 return ERROR_SUCCESS;
1079 dest = get_duplicate_filename( package, row, file_key, file->TargetPath );
1080 if (!dest)
1082 WARN("Unable to get duplicate filename\n");
1083 return ERROR_SUCCESS;
1086 TRACE("Removing duplicate %s of %s\n", debugstr_w(dest), debugstr_w(file->TargetPath));
1088 if (!DeleteFileW( dest ))
1090 WARN("Failed to delete duplicate file %s (%u)\n", debugstr_w(dest), GetLastError());
1093 uirow = MSI_CreateRecord( 9 );
1094 MSI_RecordSetStringW( uirow, 1, MSI_RecordGetString( row, 1 ) );
1095 MSI_RecordSetStringW( uirow, 9, MSI_RecordGetString( row, 5 ) );
1096 msi_ui_actiondata( package, szRemoveDuplicateFiles, uirow );
1097 msiobj_release( &uirow->hdr );
1099 msi_free(dest);
1100 return ERROR_SUCCESS;
1103 UINT ACTION_RemoveDuplicateFiles( MSIPACKAGE *package )
1105 UINT rc;
1106 MSIQUERY *view;
1107 static const WCHAR query[] =
1108 {'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
1109 '`','D','u','p','l','i','c','a','t','e','F','i','l','e','`',0};
1111 rc = MSI_DatabaseOpenViewW( package->db, query, &view );
1112 if (rc != ERROR_SUCCESS)
1113 return ERROR_SUCCESS;
1115 rc = MSI_IterateRecords( view, NULL, ITERATE_RemoveDuplicateFiles, package );
1116 msiobj_release( &view->hdr );
1118 return rc;
1121 static BOOL verify_comp_for_removal(MSICOMPONENT *comp, UINT install_mode)
1123 /* special case */
1124 if (comp->Action != INSTALLSTATE_SOURCE &&
1125 comp->Attributes & msidbComponentAttributesSourceOnly &&
1126 (install_mode == msidbRemoveFileInstallModeOnRemove ||
1127 install_mode == msidbRemoveFileInstallModeOnBoth)) return TRUE;
1129 switch (comp->Action)
1131 case INSTALLSTATE_LOCAL:
1132 case INSTALLSTATE_SOURCE:
1133 if (install_mode == msidbRemoveFileInstallModeOnInstall ||
1134 install_mode == msidbRemoveFileInstallModeOnBoth) return TRUE;
1135 break;
1136 case INSTALLSTATE_ABSENT:
1137 if (install_mode == msidbRemoveFileInstallModeOnRemove ||
1138 install_mode == msidbRemoveFileInstallModeOnBoth) return TRUE;
1139 break;
1140 default: break;
1142 return FALSE;
1145 static UINT ITERATE_RemoveFiles(MSIRECORD *row, LPVOID param)
1147 MSIPACKAGE *package = param;
1148 MSICOMPONENT *comp;
1149 MSIRECORD *uirow;
1150 LPCWSTR component, dirprop;
1151 UINT install_mode;
1152 LPWSTR dir = NULL, path = NULL, filename = NULL;
1153 DWORD size;
1154 UINT ret = ERROR_SUCCESS;
1156 component = MSI_RecordGetString(row, 2);
1157 dirprop = MSI_RecordGetString(row, 4);
1158 install_mode = MSI_RecordGetInteger(row, 5);
1160 comp = msi_get_loaded_component(package, component);
1161 if (!comp)
1162 return ERROR_SUCCESS;
1164 comp->Action = msi_get_component_action( package, comp );
1165 if (!verify_comp_for_removal(comp, install_mode))
1167 TRACE("Skipping removal due to install mode\n");
1168 return ERROR_SUCCESS;
1170 if (comp->assembly && !comp->assembly->application)
1172 return ERROR_SUCCESS;
1174 if (comp->Attributes & msidbComponentAttributesPermanent)
1176 TRACE("permanent component, not removing file\n");
1177 return ERROR_SUCCESS;
1180 dir = msi_dup_property(package->db, dirprop);
1181 if (!dir)
1182 return ERROR_OUTOFMEMORY;
1184 size = 0;
1185 if ((filename = strdupW( MSI_RecordGetString(row, 3) )))
1187 msi_reduce_to_long_filename( filename );
1188 size = lstrlenW( filename );
1190 size += lstrlenW(dir) + 2;
1191 path = msi_alloc(size * sizeof(WCHAR));
1192 if (!path)
1194 ret = ERROR_OUTOFMEMORY;
1195 goto done;
1198 if (filename)
1200 lstrcpyW(path, dir);
1201 PathAddBackslashW(path);
1202 lstrcatW(path, filename);
1204 TRACE("Deleting misc file: %s\n", debugstr_w(path));
1205 DeleteFileW(path);
1207 else
1209 TRACE("Removing misc directory: %s\n", debugstr_w(dir));
1210 RemoveDirectoryW(dir);
1213 done:
1214 uirow = MSI_CreateRecord( 9 );
1215 MSI_RecordSetStringW( uirow, 1, MSI_RecordGetString(row, 1) );
1216 MSI_RecordSetStringW( uirow, 9, dir );
1217 msi_ui_actiondata( package, szRemoveFiles, uirow );
1218 msiobj_release( &uirow->hdr );
1220 msi_free(filename);
1221 msi_free(path);
1222 msi_free(dir);
1223 return ret;
1226 static BOOL has_persistent_dir( MSIPACKAGE *package, MSICOMPONENT *comp )
1228 MSIQUERY *view;
1229 UINT r = ERROR_FUNCTION_FAILED;
1231 static const WCHAR query[] = {
1232 'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
1233 '`','C','r','e','a','t','e','F','o','l','d','e','r','`',' ','W','H','E','R','E',' ',
1234 '`','C','o','m','p','o','n','e','n','t','_','`',' ','=','\'','%','s','\'',' ','A','N','D',' ',
1235 '`','D','i','r','e','c','t','o','r','y','_','`',' ','=','\'','%','s','\'',0};
1237 if (!MSI_OpenQuery( package->db, &view, query, comp->Component, comp->Directory ))
1239 if (!MSI_ViewExecute( view, NULL ))
1241 MSIRECORD *rec;
1242 if (!(r = MSI_ViewFetch( view, &rec )))
1244 TRACE("directory %s is persistent\n", debugstr_w(comp->Directory));
1245 msiobj_release( &rec->hdr );
1248 msiobj_release( &view->hdr );
1250 return (r == ERROR_SUCCESS);
1253 UINT ACTION_RemoveFiles( MSIPACKAGE *package )
1255 MSIQUERY *view;
1256 MSIFILE *file;
1257 UINT r;
1259 static const WCHAR query[] = {
1260 'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
1261 '`','R','e','m','o','v','e','F','i','l','e','`',0};
1263 r = MSI_DatabaseOpenViewW(package->db, query, &view);
1264 if (r == ERROR_SUCCESS)
1266 MSI_IterateRecords(view, NULL, ITERATE_RemoveFiles, package);
1267 msiobj_release(&view->hdr);
1270 LIST_FOR_EACH_ENTRY( file, &package->files, MSIFILE, entry )
1272 MSIRECORD *uirow;
1273 LPWSTR dir, p;
1274 VS_FIXEDFILEINFO *ver;
1275 MSICOMPONENT *comp = file->Component;
1277 comp->Action = msi_get_component_action( package, comp );
1278 if (comp->Action != INSTALLSTATE_ABSENT || comp->Installed == INSTALLSTATE_SOURCE)
1279 continue;
1281 if (comp->assembly && !comp->assembly->application)
1282 continue;
1284 if (comp->Attributes & msidbComponentAttributesPermanent)
1286 TRACE("permanent component, not removing file\n");
1287 continue;
1290 if (file->Version)
1292 ver = msi_get_disk_file_version( file->TargetPath );
1293 if (ver && msi_compare_file_versions( ver, file->Version ) > 0)
1295 TRACE("newer version detected, not removing file\n");
1296 msi_free( ver );
1297 continue;
1299 msi_free( ver );
1302 if (file->state == msifs_installed)
1303 WARN("removing installed file %s\n", debugstr_w(file->TargetPath));
1305 TRACE("removing %s\n", debugstr_w(file->File) );
1307 SetFileAttributesW( file->TargetPath, FILE_ATTRIBUTE_NORMAL );
1308 if (!DeleteFileW( file->TargetPath ))
1310 WARN("failed to delete %s (%u)\n", debugstr_w(file->TargetPath), GetLastError());
1312 else if (!has_persistent_dir( package, comp ))
1314 if ((dir = strdupW( file->TargetPath )))
1316 if ((p = strrchrW( dir, '\\' ))) *p = 0;
1317 RemoveDirectoryW( dir );
1318 msi_free( dir );
1321 file->state = msifs_missing;
1323 uirow = MSI_CreateRecord( 9 );
1324 MSI_RecordSetStringW( uirow, 1, file->FileName );
1325 MSI_RecordSetStringW( uirow, 9, comp->Directory );
1326 msi_ui_actiondata( package, szRemoveFiles, uirow );
1327 msiobj_release( &uirow->hdr );
1328 /* FIXME: call msi_ui_progress here? */
1331 return ERROR_SUCCESS;