msi: Support remote calls to MsiEnumComponentCosts.
[wine/multimedia.git] / dlls / msi / files.c
blob53ed2ab63da02562dcf4daf1ac4cc49d30cf825e
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
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 ui_actiondata( package, action, uirow );
62 msiobj_release( &uirow->hdr );
63 ui_progress( package, 2, f->FileSize, 0, 0 );
66 static msi_file_state calculate_install_state( 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 if (comp->ActionRequest != INSTALLSTATE_LOCAL || !comp->Enabled ||
75 (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( 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 comp->Action = INSTALLSTATE_LOCAL;
157 ui_progress( package, 2, file->FileSize, 0, 0 );
161 static UINT copy_file(MSIFILE *file, LPWSTR source)
163 BOOL ret;
165 ret = CopyFileW(source, file->TargetPath, FALSE);
166 if (!ret)
167 return GetLastError();
169 SetFileAttributesW(file->TargetPath, FILE_ATTRIBUTE_NORMAL);
171 file->state = msifs_installed;
172 return ERROR_SUCCESS;
175 static UINT copy_install_file(MSIPACKAGE *package, MSIFILE *file, LPWSTR source)
177 UINT gle;
179 TRACE("Copying %s to %s\n", debugstr_w(source), debugstr_w(file->TargetPath));
181 gle = copy_file(file, source);
182 if (gle == ERROR_SUCCESS)
183 return gle;
185 if (gle == ERROR_ALREADY_EXISTS && file->state == msifs_overwrite)
187 TRACE("overwriting existing file\n");
188 return ERROR_SUCCESS;
190 else if (gle == ERROR_ACCESS_DENIED)
192 SetFileAttributesW(file->TargetPath, FILE_ATTRIBUTE_NORMAL);
194 gle = copy_file(file, source);
195 TRACE("Overwriting existing file: %d\n", gle);
197 if (gle == ERROR_SHARING_VIOLATION || gle == ERROR_USER_MAPPED_FILE)
199 WCHAR tmpfileW[MAX_PATH], *pathW, *p;
200 DWORD len;
202 TRACE("file in use, scheduling rename operation\n");
204 GetTempFileNameW(szBackSlash, szMsi, 0, tmpfileW);
205 len = strlenW(file->TargetPath) + strlenW(tmpfileW) + 1;
206 if (!(pathW = msi_alloc(len * sizeof(WCHAR))))
207 return ERROR_OUTOFMEMORY;
209 strcpyW(pathW, file->TargetPath);
210 if ((p = strrchrW(pathW, '\\'))) *p = 0;
211 strcatW(pathW, tmpfileW);
213 if (CopyFileW(source, pathW, FALSE) &&
214 MoveFileExW(file->TargetPath, NULL, MOVEFILE_DELAY_UNTIL_REBOOT) &&
215 MoveFileExW(pathW, file->TargetPath, MOVEFILE_DELAY_UNTIL_REBOOT))
217 file->state = msifs_installed;
218 package->need_reboot = 1;
219 gle = ERROR_SUCCESS;
221 else
223 gle = GetLastError();
224 WARN("failed to schedule rename operation: %d)\n", gle);
226 msi_free(pathW);
229 return gle;
232 static UINT msi_create_directory( MSIPACKAGE *package, const WCHAR *dir )
234 MSIFOLDER *folder;
235 WCHAR *install_path;
237 install_path = resolve_target_folder( package, dir, FALSE, TRUE, &folder );
238 if (!install_path)
239 return ERROR_FUNCTION_FAILED;
241 if (folder->State == 0)
243 create_full_pathW( install_path );
244 folder->State = 2;
246 msi_free( install_path );
247 return ERROR_SUCCESS;
250 static BOOL installfiles_cb(MSIPACKAGE *package, LPCWSTR file, DWORD action,
251 LPWSTR *path, DWORD *attrs, PVOID user)
253 static MSIFILE *f = NULL;
254 UINT_PTR disk_id = (UINT_PTR)user;
256 if (action == MSICABEXTRACT_BEGINEXTRACT)
258 f = get_loaded_file(package, file);
259 if (!f)
261 TRACE("unknown file in cabinet (%s)\n", debugstr_w(file));
262 return FALSE;
265 if (f->disk_id != disk_id || (f->state != msifs_missing && f->state != msifs_overwrite))
266 return FALSE;
268 msi_file_update_ui(package, f, szInstallFiles);
269 if (!f->Component->assembly || f->Component->assembly->application)
271 msi_create_directory(package, f->Component->Directory);
273 *path = strdupW(f->TargetPath);
274 *attrs = f->Attributes;
276 else if (action == MSICABEXTRACT_FILEEXTRACTED)
278 f->state = msifs_installed;
279 f = NULL;
282 return TRUE;
286 * ACTION_InstallFiles()
288 * For efficiency, this is done in two passes:
289 * 1) Correct all the TargetPaths and determine what files are to be installed.
290 * 2) Extract Cabinets and copy files.
292 UINT ACTION_InstallFiles(MSIPACKAGE *package)
294 MSIMEDIAINFO *mi;
295 MSICOMPONENT *comp;
296 UINT rc = ERROR_SUCCESS;
297 MSIFILE *file;
299 /* increment progress bar each time action data is sent */
300 ui_progress(package,1,1,0,0);
302 schedule_install_files(package);
304 mi = msi_alloc_zero( sizeof(MSIMEDIAINFO) );
306 LIST_FOR_EACH_ENTRY( file, &package->files, MSIFILE, entry )
308 rc = msi_load_media_info( package, file->Sequence, mi );
309 if (rc != ERROR_SUCCESS)
311 ERR("Unable to load media info for %s (%u)\n", debugstr_w(file->File), rc);
312 return ERROR_FUNCTION_FAILED;
314 if (!file->Component->Enabled) continue;
316 if (file->state != msifs_hashmatch &&
317 (rc = ready_media( package, file->Sequence, file->IsCompressed, mi )))
319 ERR("Failed to ready media for %s\n", debugstr_w(file->File));
320 goto done;
323 if (file->state != msifs_missing && !mi->is_continuous && file->state != msifs_overwrite)
324 continue;
326 if (file->Sequence > mi->last_sequence || mi->is_continuous ||
327 (file->IsCompressed && !mi->is_extracted))
329 MSICABDATA data;
331 data.mi = mi;
332 data.package = package;
333 data.cb = installfiles_cb;
334 data.user = (PVOID)(UINT_PTR)mi->disk_id;
336 if (file->IsCompressed &&
337 !msi_cabextract(package, mi, &data))
339 ERR("Failed to extract cabinet: %s\n", debugstr_w(mi->cabinet));
340 rc = ERROR_INSTALL_FAILURE;
341 goto done;
345 if (!file->IsCompressed)
347 LPWSTR source = resolve_file_source(package, file);
349 TRACE("copying %s to %s\n", debugstr_w(source), debugstr_w(file->TargetPath));
351 msi_file_update_ui(package, file, szInstallFiles);
352 if (!file->Component->assembly || file->Component->assembly->application)
354 msi_create_directory(package, file->Component->Directory);
356 rc = copy_install_file(package, file, source);
357 if (rc != ERROR_SUCCESS)
359 ERR("Failed to copy %s to %s (%d)\n", debugstr_w(source),
360 debugstr_w(file->TargetPath), rc);
361 rc = ERROR_INSTALL_FAILURE;
362 msi_free(source);
363 goto done;
365 msi_free(source);
367 else if (file->state != msifs_installed && !(file->Attributes & msidbFileAttributesPatchAdded))
369 ERR("compressed file wasn't installed (%s)\n", debugstr_w(file->TargetPath));
370 rc = ERROR_INSTALL_FAILURE;
371 goto done;
374 LIST_FOR_EACH_ENTRY( comp, &package->components, MSICOMPONENT, entry )
376 if (comp->ActionRequest == INSTALLSTATE_LOCAL && comp->Enabled &&
377 comp->assembly && !comp->assembly->installed)
379 rc = install_assembly( package, comp );
380 if (rc != ERROR_SUCCESS)
382 ERR("Failed to install assembly\n");
383 rc = ERROR_INSTALL_FAILURE;
384 break;
389 done:
390 msi_free_media_info(mi);
391 return rc;
394 static BOOL load_mspatcha(void)
396 hmspatcha = LoadLibraryA("mspatcha.dll");
397 if (!hmspatcha)
399 ERR("Failed to load mspatcha.dll: %d\n", GetLastError());
400 return FALSE;
403 ApplyPatchToFileW = (void*)GetProcAddress(hmspatcha, "ApplyPatchToFileW");
404 if(!ApplyPatchToFileW)
406 ERR("GetProcAddress(ApplyPatchToFileW) failed: %d.\n", GetLastError());
407 return FALSE;
410 return TRUE;
413 static void unload_mspatch(void)
415 FreeLibrary(hmspatcha);
418 static BOOL patchfiles_cb(MSIPACKAGE *package, LPCWSTR file, DWORD action,
419 LPWSTR *path, DWORD *attrs, PVOID user)
421 static MSIFILEPATCH *p = NULL;
422 static WCHAR patch_path[MAX_PATH] = {0};
423 static WCHAR temp_folder[MAX_PATH] = {0};
425 if (action == MSICABEXTRACT_BEGINEXTRACT)
427 if (temp_folder[0] == '\0')
428 GetTempPathW(MAX_PATH, temp_folder);
430 p = get_loaded_filepatch(package, file);
431 if (!p)
433 TRACE("unknown file in cabinet (%s)\n", debugstr_w(file));
434 return FALSE;
437 msi_file_update_ui(package, p->File, szPatchFiles);
439 GetTempFileNameW(temp_folder, NULL, 0, patch_path);
441 *path = strdupW(patch_path);
442 *attrs = p->File->Attributes;
444 else if (action == MSICABEXTRACT_FILEEXTRACTED)
446 WCHAR patched_file[MAX_PATH];
447 BOOL br;
449 GetTempFileNameW(temp_folder, NULL, 0, patched_file);
451 br = ApplyPatchToFileW(patch_path, p->File->TargetPath, patched_file, 0);
452 if (br)
454 /* FIXME: baseline cache */
456 DeleteFileW( p->File->TargetPath );
457 MoveFileW( patched_file, p->File->TargetPath );
459 p->IsApplied = TRUE;
461 else
462 ERR("Failed patch %s: %d.\n", debugstr_w(p->File->TargetPath), GetLastError());
464 DeleteFileW(patch_path);
465 p = NULL;
468 return TRUE;
471 UINT ACTION_PatchFiles( MSIPACKAGE *package )
473 MSIFILEPATCH *patch;
474 MSIMEDIAINFO *mi;
475 UINT rc = ERROR_SUCCESS;
476 BOOL mspatcha_loaded = FALSE;
478 TRACE("%p\n", package);
480 /* increment progress bar each time action data is sent */
481 ui_progress(package,1,1,0,0);
483 mi = msi_alloc_zero( sizeof(MSIMEDIAINFO) );
485 LIST_FOR_EACH_ENTRY( patch, &package->filepatches, MSIFILEPATCH, entry )
487 MSIFILE *file = patch->File;
489 rc = msi_load_media_info( package, patch->Sequence, mi );
490 if (rc != ERROR_SUCCESS)
492 ERR("Unable to load media info for %s (%u)\n", debugstr_w(file->File), rc);
493 return ERROR_FUNCTION_FAILED;
495 if (!file->Component->Enabled) continue;
497 if (!patch->IsApplied)
499 MSICABDATA data;
501 rc = ready_media( package, patch->Sequence, TRUE, mi );
502 if (rc != ERROR_SUCCESS)
504 ERR("Failed to ready media for %s\n", debugstr_w(file->File));
505 goto done;
508 if (!mspatcha_loaded && !load_mspatcha())
510 rc = ERROR_FUNCTION_FAILED;
511 goto done;
513 mspatcha_loaded = TRUE;
515 data.mi = mi;
516 data.package = package;
517 data.cb = patchfiles_cb;
518 data.user = (PVOID)(UINT_PTR)mi->disk_id;
520 if (!msi_cabextract(package, mi, &data))
522 ERR("Failed to extract cabinet: %s\n", debugstr_w(mi->cabinet));
523 rc = ERROR_INSTALL_FAILURE;
524 goto done;
528 if (!patch->IsApplied && !(patch->Attributes & msidbPatchAttributesNonVital))
530 ERR("Failed to apply patch to file: %s\n", debugstr_w(file->File));
531 rc = ERROR_INSTALL_FAILURE;
532 goto done;
536 done:
537 msi_free_media_info(mi);
538 if (mspatcha_loaded)
539 unload_mspatch();
540 return rc;
543 #define is_dot_dir(x) ((x[0] == '.') && ((x[1] == 0) || ((x[1] == '.') && (x[2] == 0))))
545 typedef struct
547 struct list entry;
548 LPWSTR sourcename;
549 LPWSTR destname;
550 LPWSTR source;
551 LPWSTR dest;
552 } FILE_LIST;
554 static BOOL msi_move_file(LPCWSTR source, LPCWSTR dest, int options)
556 BOOL ret;
558 if (GetFileAttributesW(source) == FILE_ATTRIBUTE_DIRECTORY ||
559 GetFileAttributesW(dest) == FILE_ATTRIBUTE_DIRECTORY)
561 WARN("Source or dest is directory, not moving\n");
562 return FALSE;
565 if (options == msidbMoveFileOptionsMove)
567 TRACE("moving %s -> %s\n", debugstr_w(source), debugstr_w(dest));
568 ret = MoveFileExW(source, dest, MOVEFILE_REPLACE_EXISTING);
569 if (!ret)
571 WARN("MoveFile failed: %d\n", GetLastError());
572 return FALSE;
575 else
577 TRACE("copying %s -> %s\n", debugstr_w(source), debugstr_w(dest));
578 ret = CopyFileW(source, dest, FALSE);
579 if (!ret)
581 WARN("CopyFile failed: %d\n", GetLastError());
582 return FALSE;
586 return TRUE;
589 static LPWSTR wildcard_to_file(LPWSTR wildcard, LPWSTR filename)
591 LPWSTR path, ptr;
592 DWORD dirlen, pathlen;
594 ptr = strrchrW(wildcard, '\\');
595 dirlen = ptr - wildcard + 1;
597 pathlen = dirlen + lstrlenW(filename) + 1;
598 path = msi_alloc(pathlen * sizeof(WCHAR));
600 lstrcpynW(path, wildcard, dirlen + 1);
601 lstrcatW(path, filename);
603 return path;
606 static void free_file_entry(FILE_LIST *file)
608 msi_free(file->source);
609 msi_free(file->dest);
610 msi_free(file);
613 static void free_list(FILE_LIST *list)
615 while (!list_empty(&list->entry))
617 FILE_LIST *file = LIST_ENTRY(list_head(&list->entry), FILE_LIST, entry);
619 list_remove(&file->entry);
620 free_file_entry(file);
624 static BOOL add_wildcard(FILE_LIST *files, LPWSTR source, LPWSTR dest)
626 FILE_LIST *new, *file;
627 LPWSTR ptr, filename;
628 DWORD size;
630 new = msi_alloc_zero(sizeof(FILE_LIST));
631 if (!new)
632 return FALSE;
634 new->source = strdupW(source);
635 ptr = strrchrW(dest, '\\') + 1;
636 filename = strrchrW(new->source, '\\') + 1;
638 new->sourcename = filename;
640 if (*ptr)
641 new->destname = ptr;
642 else
643 new->destname = new->sourcename;
645 size = (ptr - dest) + lstrlenW(filename) + 1;
646 new->dest = msi_alloc(size * sizeof(WCHAR));
647 if (!new->dest)
649 free_file_entry(new);
650 return FALSE;
653 lstrcpynW(new->dest, dest, ptr - dest + 1);
654 lstrcatW(new->dest, filename);
656 if (list_empty(&files->entry))
658 list_add_head(&files->entry, &new->entry);
659 return TRUE;
662 LIST_FOR_EACH_ENTRY(file, &files->entry, FILE_LIST, entry)
664 if (strcmpW( source, file->source ) < 0)
666 list_add_before(&file->entry, &new->entry);
667 return TRUE;
671 list_add_after(&file->entry, &new->entry);
672 return TRUE;
675 static BOOL move_files_wildcard(LPWSTR source, LPWSTR dest, int options)
677 WIN32_FIND_DATAW wfd;
678 HANDLE hfile;
679 LPWSTR path;
680 BOOL res;
681 FILE_LIST files, *file;
682 DWORD size;
684 hfile = FindFirstFileW(source, &wfd);
685 if (hfile == INVALID_HANDLE_VALUE) return FALSE;
687 list_init(&files.entry);
689 for (res = TRUE; res; res = FindNextFileW(hfile, &wfd))
691 if (is_dot_dir(wfd.cFileName)) continue;
693 path = wildcard_to_file(source, wfd.cFileName);
694 if (!path)
696 res = FALSE;
697 goto done;
700 add_wildcard(&files, path, dest);
701 msi_free(path);
704 /* no files match the wildcard */
705 if (list_empty(&files.entry))
706 goto done;
708 /* only the first wildcard match gets renamed to dest */
709 file = LIST_ENTRY(list_head(&files.entry), FILE_LIST, entry);
710 size = (strrchrW(file->dest, '\\') - file->dest) + lstrlenW(file->destname) + 2;
711 file->dest = msi_realloc(file->dest, size * sizeof(WCHAR));
712 if (!file->dest)
714 res = FALSE;
715 goto done;
718 /* file->dest may be shorter after the reallocation, so add a NULL
719 * terminator. This is needed for the call to strrchrW, as there will no
720 * longer be a NULL terminator within the bounds of the allocation in this case.
722 file->dest[size - 1] = '\0';
723 lstrcpyW(strrchrW(file->dest, '\\') + 1, file->destname);
725 while (!list_empty(&files.entry))
727 file = LIST_ENTRY(list_head(&files.entry), FILE_LIST, entry);
729 msi_move_file(file->source, file->dest, options);
731 list_remove(&file->entry);
732 free_file_entry(file);
735 res = TRUE;
737 done:
738 free_list(&files);
739 FindClose(hfile);
740 return res;
743 static UINT ITERATE_MoveFiles( MSIRECORD *rec, LPVOID param )
745 MSIPACKAGE *package = param;
746 MSIRECORD *uirow;
747 MSICOMPONENT *comp;
748 LPCWSTR sourcename, component;
749 LPWSTR sourcedir, destname = NULL, destdir = NULL, source = NULL, dest = NULL;
750 int options;
751 DWORD size;
752 BOOL ret, wildcards;
754 component = MSI_RecordGetString(rec, 2);
755 comp = get_loaded_component(package, component);
756 if (!comp)
757 return ERROR_SUCCESS;
759 if (!comp->Enabled)
761 TRACE("component is disabled\n");
762 return ERROR_SUCCESS;
765 if (comp->ActionRequest != INSTALLSTATE_LOCAL && comp->ActionRequest != INSTALLSTATE_SOURCE)
767 TRACE("Component not scheduled for installation: %s\n", debugstr_w(component));
768 comp->Action = comp->Installed;
769 return ERROR_SUCCESS;
771 comp->Action = comp->ActionRequest;
773 sourcename = MSI_RecordGetString(rec, 3);
774 options = MSI_RecordGetInteger(rec, 7);
776 sourcedir = msi_dup_property(package->db, MSI_RecordGetString(rec, 5));
777 if (!sourcedir)
778 goto done;
780 destdir = msi_dup_property(package->db, MSI_RecordGetString(rec, 6));
781 if (!destdir)
782 goto done;
784 if (!sourcename)
786 if (GetFileAttributesW(sourcedir) == INVALID_FILE_ATTRIBUTES)
787 goto done;
789 source = strdupW(sourcedir);
790 if (!source)
791 goto done;
793 else
795 size = lstrlenW(sourcedir) + lstrlenW(sourcename) + 2;
796 source = msi_alloc(size * sizeof(WCHAR));
797 if (!source)
798 goto done;
800 lstrcpyW(source, sourcedir);
801 if (source[lstrlenW(source) - 1] != '\\')
802 lstrcatW(source, szBackSlash);
803 lstrcatW(source, sourcename);
806 wildcards = strchrW(source, '*') || strchrW(source, '?');
808 if (MSI_RecordIsNull(rec, 4))
810 if (!wildcards)
812 destname = strdupW(sourcename);
813 if (!destname)
814 goto done;
817 else
819 destname = strdupW(MSI_RecordGetString(rec, 4));
820 if (destname)
821 reduce_to_longfilename(destname);
824 size = 0;
825 if (destname)
826 size = lstrlenW(destname);
828 size += lstrlenW(destdir) + 2;
829 dest = msi_alloc(size * sizeof(WCHAR));
830 if (!dest)
831 goto done;
833 lstrcpyW(dest, destdir);
834 if (dest[lstrlenW(dest) - 1] != '\\')
835 lstrcatW(dest, szBackSlash);
837 if (destname)
838 lstrcatW(dest, destname);
840 if (GetFileAttributesW(destdir) == INVALID_FILE_ATTRIBUTES)
842 ret = CreateDirectoryW(destdir, NULL);
843 if (!ret)
845 WARN("CreateDirectory failed: %d\n", GetLastError());
846 goto done;
850 if (!wildcards)
851 msi_move_file(source, dest, options);
852 else
853 move_files_wildcard(source, dest, options);
855 done:
856 uirow = MSI_CreateRecord( 9 );
857 MSI_RecordSetStringW( uirow, 1, MSI_RecordGetString(rec, 1) );
858 MSI_RecordSetInteger( uirow, 6, 1 ); /* FIXME */
859 MSI_RecordSetStringW( uirow, 9, destdir );
860 ui_actiondata( package, szMoveFiles, uirow );
861 msiobj_release( &uirow->hdr );
863 msi_free(sourcedir);
864 msi_free(destdir);
865 msi_free(destname);
866 msi_free(source);
867 msi_free(dest);
869 return ERROR_SUCCESS;
872 UINT ACTION_MoveFiles( MSIPACKAGE *package )
874 UINT rc;
875 MSIQUERY *view;
877 static const WCHAR ExecSeqQuery[] =
878 {'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
879 '`','M','o','v','e','F','i','l','e','`',0};
881 rc = MSI_DatabaseOpenViewW(package->db, ExecSeqQuery, &view);
882 if (rc != ERROR_SUCCESS)
883 return ERROR_SUCCESS;
885 rc = MSI_IterateRecords(view, NULL, ITERATE_MoveFiles, package);
886 msiobj_release(&view->hdr);
888 return rc;
891 static WCHAR *get_duplicate_filename( MSIPACKAGE *package, MSIRECORD *row, const WCHAR *file_key, const WCHAR *src )
893 DWORD len;
894 WCHAR *dst_name, *dst_path, *dst;
896 if (MSI_RecordIsNull( row, 4 ))
898 len = strlenW( src ) + 1;
899 if (!(dst_name = msi_alloc( len * sizeof(WCHAR)))) return NULL;
900 strcpyW( dst_name, strrchrW( src, '\\' ) + 1 );
902 else
904 MSI_RecordGetStringW( row, 4, NULL, &len );
905 if (!(dst_name = msi_alloc( ++len * sizeof(WCHAR) ))) return NULL;
906 MSI_RecordGetStringW( row, 4, dst_name, &len );
907 reduce_to_longfilename( dst_name );
910 if (MSI_RecordIsNull( row, 5 ))
912 WCHAR *p;
913 dst_path = strdupW( src );
914 p = strrchrW( dst_path, '\\' );
915 if (p) *p = 0;
917 else
919 const WCHAR *dst_key = MSI_RecordGetString( row, 5 );
921 dst_path = resolve_target_folder( package, dst_key, FALSE, TRUE, NULL );
922 if (!dst_path)
924 /* try a property */
925 dst_path = msi_dup_property( package->db, dst_key );
926 if (!dst_path)
928 FIXME("Unable to get destination folder, try AppSearch properties\n");
929 msi_free( dst_name );
930 return NULL;
935 dst = build_directory_name( 2, dst_path, dst_name );
936 create_full_pathW( dst_path );
938 msi_free( dst_name );
939 msi_free( dst_path );
940 return dst;
943 static UINT ITERATE_DuplicateFiles(MSIRECORD *row, LPVOID param)
945 MSIPACKAGE *package = param;
946 LPWSTR dest;
947 LPCWSTR file_key, component;
948 MSICOMPONENT *comp;
949 MSIRECORD *uirow;
950 MSIFILE *file;
952 component = MSI_RecordGetString(row,2);
953 comp = get_loaded_component(package,component);
954 if (!comp)
955 return ERROR_SUCCESS;
957 if (!comp->Enabled)
959 TRACE("component is disabled\n");
960 return ERROR_SUCCESS;
963 if (comp->ActionRequest != INSTALLSTATE_LOCAL)
965 TRACE("Component not scheduled for installation %s\n", debugstr_w(component));
966 comp->Action = comp->Installed;
967 return ERROR_SUCCESS;
969 comp->Action = INSTALLSTATE_LOCAL;
971 file_key = MSI_RecordGetString(row,3);
972 if (!file_key)
974 ERR("Unable to get file key\n");
975 return ERROR_FUNCTION_FAILED;
978 file = get_loaded_file( package, file_key );
979 if (!file)
981 ERR("Original file unknown %s\n", debugstr_w(file_key));
982 return ERROR_SUCCESS;
985 dest = get_duplicate_filename( package, row, file_key, file->TargetPath );
986 if (!dest)
988 WARN("Unable to get duplicate filename\n");
989 return ERROR_SUCCESS;
992 TRACE("Duplicating file %s to %s\n", debugstr_w(file->TargetPath), debugstr_w(dest));
994 if (!CopyFileW( file->TargetPath, dest, TRUE ))
996 WARN("Failed to copy file %s -> %s (%u)\n",
997 debugstr_w(file->TargetPath), debugstr_w(dest), GetLastError());
1000 FIXME("We should track these duplicate files as well\n");
1002 uirow = MSI_CreateRecord( 9 );
1003 MSI_RecordSetStringW( uirow, 1, MSI_RecordGetString( row, 1 ) );
1004 MSI_RecordSetInteger( uirow, 6, file->FileSize );
1005 MSI_RecordSetStringW( uirow, 9, MSI_RecordGetString( row, 5 ) );
1006 ui_actiondata( package, szDuplicateFiles, uirow );
1007 msiobj_release( &uirow->hdr );
1009 msi_free(dest);
1010 return ERROR_SUCCESS;
1013 UINT ACTION_DuplicateFiles(MSIPACKAGE *package)
1015 UINT rc;
1016 MSIQUERY * view;
1017 static const WCHAR ExecSeqQuery[] =
1018 {'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
1019 '`','D','u','p','l','i','c','a','t','e','F','i','l','e','`',0};
1021 rc = MSI_DatabaseOpenViewW(package->db, ExecSeqQuery, &view);
1022 if (rc != ERROR_SUCCESS)
1023 return ERROR_SUCCESS;
1025 rc = MSI_IterateRecords(view, NULL, ITERATE_DuplicateFiles, package);
1026 msiobj_release(&view->hdr);
1028 return rc;
1031 static UINT ITERATE_RemoveDuplicateFiles( MSIRECORD *row, LPVOID param )
1033 MSIPACKAGE *package = param;
1034 LPWSTR dest;
1035 LPCWSTR file_key, component;
1036 MSICOMPONENT *comp;
1037 MSIRECORD *uirow;
1038 MSIFILE *file;
1040 component = MSI_RecordGetString( row, 2 );
1041 comp = get_loaded_component( package, component );
1042 if (!comp)
1043 return ERROR_SUCCESS;
1045 if (!comp->Enabled)
1047 TRACE("component is disabled\n");
1048 return ERROR_SUCCESS;
1051 if (comp->ActionRequest != INSTALLSTATE_ABSENT)
1053 TRACE("Component not scheduled for removal %s\n", debugstr_w(component));
1054 comp->Action = comp->Installed;
1055 return ERROR_SUCCESS;
1057 comp->Action = INSTALLSTATE_ABSENT;
1059 file_key = MSI_RecordGetString( row, 3 );
1060 if (!file_key)
1062 ERR("Unable to get file key\n");
1063 return ERROR_FUNCTION_FAILED;
1066 file = get_loaded_file( package, file_key );
1067 if (!file)
1069 ERR("Original file unknown %s\n", debugstr_w(file_key));
1070 return ERROR_SUCCESS;
1073 dest = get_duplicate_filename( package, row, file_key, file->TargetPath );
1074 if (!dest)
1076 WARN("Unable to get duplicate filename\n");
1077 return ERROR_SUCCESS;
1080 TRACE("Removing duplicate %s of %s\n", debugstr_w(dest), debugstr_w(file->TargetPath));
1082 if (!DeleteFileW( dest ))
1084 WARN("Failed to delete duplicate file %s (%u)\n", debugstr_w(dest), GetLastError());
1087 uirow = MSI_CreateRecord( 9 );
1088 MSI_RecordSetStringW( uirow, 1, MSI_RecordGetString( row, 1 ) );
1089 MSI_RecordSetStringW( uirow, 9, MSI_RecordGetString( row, 5 ) );
1090 ui_actiondata( package, szRemoveDuplicateFiles, uirow );
1091 msiobj_release( &uirow->hdr );
1093 msi_free(dest);
1094 return ERROR_SUCCESS;
1097 UINT ACTION_RemoveDuplicateFiles( MSIPACKAGE *package )
1099 UINT rc;
1100 MSIQUERY *view;
1101 static const WCHAR query[] =
1102 {'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
1103 '`','D','u','p','l','i','c','a','t','e','F','i','l','e','`',0};
1105 rc = MSI_DatabaseOpenViewW( package->db, query, &view );
1106 if (rc != ERROR_SUCCESS)
1107 return ERROR_SUCCESS;
1109 rc = MSI_IterateRecords( view, NULL, ITERATE_RemoveDuplicateFiles, package );
1110 msiobj_release( &view->hdr );
1112 return rc;
1115 static BOOL verify_comp_for_removal(MSICOMPONENT *comp, UINT install_mode)
1117 INSTALLSTATE request = comp->ActionRequest;
1119 /* special case */
1120 if (request != INSTALLSTATE_SOURCE &&
1121 comp->Attributes & msidbComponentAttributesSourceOnly &&
1122 (install_mode == msidbRemoveFileInstallModeOnRemove ||
1123 install_mode == msidbRemoveFileInstallModeOnBoth)) return TRUE;
1125 switch (request)
1127 case INSTALLSTATE_LOCAL:
1128 case INSTALLSTATE_SOURCE:
1129 if (install_mode == msidbRemoveFileInstallModeOnInstall ||
1130 install_mode == msidbRemoveFileInstallModeOnBoth) return TRUE;
1131 break;
1132 case INSTALLSTATE_ABSENT:
1133 if (install_mode == msidbRemoveFileInstallModeOnRemove ||
1134 install_mode == msidbRemoveFileInstallModeOnBoth) return TRUE;
1135 break;
1136 default: break;
1138 return FALSE;
1141 static UINT ITERATE_RemoveFiles(MSIRECORD *row, LPVOID param)
1143 MSIPACKAGE *package = param;
1144 MSICOMPONENT *comp;
1145 MSIRECORD *uirow;
1146 LPCWSTR component, dirprop;
1147 UINT install_mode;
1148 LPWSTR dir = NULL, path = NULL, filename = NULL;
1149 DWORD size;
1150 UINT ret = ERROR_SUCCESS;
1152 component = MSI_RecordGetString(row, 2);
1153 dirprop = MSI_RecordGetString(row, 4);
1154 install_mode = MSI_RecordGetInteger(row, 5);
1156 comp = get_loaded_component(package, component);
1157 if (!comp->Enabled)
1159 TRACE("component is disabled\n");
1160 return ERROR_SUCCESS;
1163 if (!verify_comp_for_removal(comp, install_mode))
1165 TRACE("Skipping removal due to install mode\n");
1166 comp->Action = comp->Installed;
1167 return ERROR_SUCCESS;
1170 if (comp->Attributes & msidbComponentAttributesPermanent)
1172 TRACE("permanent component, not removing file\n");
1173 return ERROR_SUCCESS;
1176 dir = msi_dup_property(package->db, dirprop);
1177 if (!dir)
1178 return ERROR_OUTOFMEMORY;
1180 size = 0;
1181 if ((filename = strdupW( MSI_RecordGetString(row, 3) )))
1183 reduce_to_longfilename( filename );
1184 size = lstrlenW( filename );
1186 size += lstrlenW(dir) + 2;
1187 path = msi_alloc(size * sizeof(WCHAR));
1188 if (!path)
1190 ret = ERROR_OUTOFMEMORY;
1191 goto done;
1194 if (filename)
1196 lstrcpyW(path, dir);
1197 PathAddBackslashW(path);
1198 lstrcatW(path, filename);
1200 TRACE("Deleting misc file: %s\n", debugstr_w(path));
1201 DeleteFileW(path);
1203 else
1205 TRACE("Removing misc directory: %s\n", debugstr_w(dir));
1206 RemoveDirectoryW(dir);
1209 done:
1210 uirow = MSI_CreateRecord( 9 );
1211 MSI_RecordSetStringW( uirow, 1, MSI_RecordGetString(row, 1) );
1212 MSI_RecordSetStringW( uirow, 9, dir );
1213 ui_actiondata( package, szRemoveFiles, uirow );
1214 msiobj_release( &uirow->hdr );
1216 msi_free(filename);
1217 msi_free(path);
1218 msi_free(dir);
1219 return ret;
1222 static BOOL has_persistent_dir( MSIPACKAGE *package, MSICOMPONENT *comp )
1224 MSIQUERY *view;
1225 UINT r = ERROR_FUNCTION_FAILED;
1227 static const WCHAR query[] = {
1228 'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
1229 '`','C','r','e','a','t','e','F','o','l','d','e','r','`',' ','W','H','E','R','E',' ',
1230 '`','C','o','m','p','o','n','e','n','t','_','`',' ','=','\'','%','s','\'',' ','A','N','D',' ',
1231 '`','D','i','r','e','c','t','o','r','y','_','`',' ','=','\'','%','s','\'',0};
1233 if (!MSI_OpenQuery( package->db, &view, query, comp->Component, comp->Directory ))
1235 if (!MSI_ViewExecute( view, NULL ))
1237 MSIRECORD *rec;
1238 if (!(r = MSI_ViewFetch( view, &rec )))
1240 TRACE("directory %s is persistent\n", debugstr_w(comp->Directory));
1241 msiobj_release( &rec->hdr );
1244 msiobj_release( &view->hdr );
1246 return (r == ERROR_SUCCESS);
1249 UINT ACTION_RemoveFiles( MSIPACKAGE *package )
1251 MSIQUERY *view;
1252 MSIFILE *file;
1253 UINT r;
1255 static const WCHAR query[] = {
1256 'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
1257 '`','R','e','m','o','v','e','F','i','l','e','`',0};
1259 r = MSI_DatabaseOpenViewW(package->db, query, &view);
1260 if (r == ERROR_SUCCESS)
1262 MSI_IterateRecords(view, NULL, ITERATE_RemoveFiles, package);
1263 msiobj_release(&view->hdr);
1266 LIST_FOR_EACH_ENTRY( file, &package->files, MSIFILE, entry )
1268 MSIRECORD *uirow;
1269 LPWSTR dir, p;
1270 VS_FIXEDFILEINFO *ver;
1272 if ( file->state == msifs_installed )
1273 ERR("removing installed file %s\n", debugstr_w(file->TargetPath));
1275 if ( file->Component->ActionRequest != INSTALLSTATE_ABSENT ||
1276 file->Component->Installed == INSTALLSTATE_SOURCE )
1277 continue;
1279 if (!file->Component->Enabled)
1281 TRACE("component is disabled\n");
1282 continue;
1285 if (file->Component->Attributes & msidbComponentAttributesPermanent)
1287 TRACE("permanent component, not removing file\n");
1288 continue;
1291 if (file->Version)
1293 ver = msi_get_disk_file_version( file->TargetPath );
1294 if (ver && msi_compare_file_versions( ver, file->Version ) > 0)
1296 TRACE("newer version detected, not removing file\n");
1297 msi_free( ver );
1298 continue;
1300 msi_free( ver );
1303 TRACE("removing %s\n", debugstr_w(file->File) );
1305 SetFileAttributesW( file->TargetPath, FILE_ATTRIBUTE_NORMAL );
1306 if (!DeleteFileW( file->TargetPath ))
1308 WARN("failed to delete %s (%u)\n", debugstr_w(file->TargetPath), GetLastError());
1310 else if (!has_persistent_dir( package, file->Component ))
1312 if ((dir = strdupW( file->TargetPath )))
1314 if ((p = strrchrW( dir, '\\' ))) *p = 0;
1315 RemoveDirectoryW( dir );
1316 msi_free( dir );
1319 file->state = msifs_missing;
1321 uirow = MSI_CreateRecord( 9 );
1322 MSI_RecordSetStringW( uirow, 1, file->FileName );
1323 MSI_RecordSetStringW( uirow, 9, file->Component->Directory );
1324 ui_actiondata( package, szRemoveFiles, uirow );
1325 msiobj_release( &uirow->hdr );
1326 /* FIXME: call ui_progress here? */
1329 return ERROR_SUCCESS;