winemac: Prevent maximized windows from entering Cocoa full-screen mode.
[wine/multimedia.git] / dlls / msi / files.c
blob62e5f43606feb15643d57f0c17f4691dc434905b
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;
73 comp->Action = msi_get_component_action( package, comp );
74 if (comp->Action != INSTALLSTATE_LOCAL || (comp->assembly && comp->assembly->installed))
76 TRACE("file %s is not scheduled for install\n", debugstr_w(file->File));
77 return msifs_skipped;
79 if ((comp->assembly && !comp->assembly->application && !comp->assembly->installed) ||
80 GetFileAttributesW( file->TargetPath ) == INVALID_FILE_ATTRIBUTES)
82 TRACE("file %s is missing\n", debugstr_w(file->File));
83 return msifs_missing;
85 if (file->Version)
87 if ((file_version = msi_get_disk_file_version( file->TargetPath )))
89 TRACE("new %s old %u.%u.%u.%u\n", debugstr_w(file->Version),
90 HIWORD(file_version->dwFileVersionMS),
91 LOWORD(file_version->dwFileVersionMS),
92 HIWORD(file_version->dwFileVersionLS),
93 LOWORD(file_version->dwFileVersionLS));
95 if (msi_compare_file_versions( file_version, file->Version ) < 0)
96 state = msifs_overwrite;
97 else
99 TRACE("destination file version equal or greater, not overwriting\n");
100 state = msifs_present;
102 msi_free( file_version );
103 return state;
105 else if ((font_version = msi_font_version_from_file( file->TargetPath )))
107 TRACE("new %s old %s\n", debugstr_w(file->Version), debugstr_w(font_version));
109 if (msi_compare_font_versions( font_version, file->Version ) < 0)
110 state = msifs_overwrite;
111 else
113 TRACE("destination file version equal or greater, not overwriting\n");
114 state = msifs_present;
116 msi_free( font_version );
117 return state;
120 if (msi_get_disk_file_size( file->TargetPath ) != file->FileSize)
122 return msifs_overwrite;
124 if (file->hash.dwFileHashInfoSize)
126 if (msi_file_hash_matches( file ))
128 TRACE("file hashes match, not overwriting\n");
129 return msifs_hashmatch;
131 else
133 TRACE("file hashes do not match, overwriting\n");
134 return msifs_overwrite;
137 /* assume present */
138 return msifs_present;
141 static void schedule_install_files(MSIPACKAGE *package)
143 MSIFILE *file;
145 LIST_FOR_EACH_ENTRY(file, &package->files, MSIFILE, entry)
147 MSICOMPONENT *comp = file->Component;
149 file->state = calculate_install_state( package, file );
150 if (file->state == msifs_overwrite && (comp->Attributes & msidbComponentAttributesNeverOverwrite))
152 TRACE("not overwriting %s\n", debugstr_w(file->TargetPath));
153 file->state = msifs_skipped;
158 static UINT copy_file(MSIFILE *file, LPWSTR source)
160 BOOL ret;
162 ret = CopyFileW(source, file->TargetPath, FALSE);
163 if (!ret)
164 return GetLastError();
166 SetFileAttributesW(file->TargetPath, FILE_ATTRIBUTE_NORMAL);
168 file->state = msifs_installed;
169 return ERROR_SUCCESS;
172 static UINT copy_install_file(MSIPACKAGE *package, MSIFILE *file, LPWSTR source)
174 UINT gle;
176 TRACE("Copying %s to %s\n", debugstr_w(source), debugstr_w(file->TargetPath));
178 gle = copy_file(file, source);
179 if (gle == ERROR_SUCCESS)
180 return gle;
182 if (gle == ERROR_ALREADY_EXISTS && file->state == msifs_overwrite)
184 TRACE("overwriting existing file\n");
185 return ERROR_SUCCESS;
187 else if (gle == ERROR_ACCESS_DENIED)
189 SetFileAttributesW(file->TargetPath, FILE_ATTRIBUTE_NORMAL);
191 gle = copy_file(file, source);
192 TRACE("Overwriting existing file: %d\n", gle);
194 if (gle == ERROR_SHARING_VIOLATION || gle == ERROR_USER_MAPPED_FILE)
196 WCHAR *tmpfileW, *pathW, *p;
197 DWORD len;
199 TRACE("file in use, scheduling rename operation\n");
201 if (!(pathW = strdupW( file->TargetPath ))) return ERROR_OUTOFMEMORY;
202 if ((p = strrchrW(pathW, '\\'))) *p = 0;
203 len = strlenW( pathW ) + 16;
204 if (!(tmpfileW = msi_alloc(len * sizeof(WCHAR))))
206 msi_free( pathW );
207 return ERROR_OUTOFMEMORY;
209 if (!GetTempFileNameW( pathW, szMsi, 0, tmpfileW )) tmpfileW[0] = 0;
210 msi_free( pathW );
212 if (CopyFileW(source, tmpfileW, FALSE) &&
213 MoveFileExW(file->TargetPath, NULL, MOVEFILE_DELAY_UNTIL_REBOOT) &&
214 MoveFileExW(tmpfileW, file->TargetPath, MOVEFILE_DELAY_UNTIL_REBOOT))
216 file->state = msifs_installed;
217 package->need_reboot_at_end = 1;
218 gle = ERROR_SUCCESS;
220 else
222 gle = GetLastError();
223 WARN("failed to schedule rename operation: %d)\n", gle);
224 DeleteFileW( tmpfileW );
226 msi_free(tmpfileW);
229 return gle;
232 static UINT msi_create_directory( MSIPACKAGE *package, const WCHAR *dir )
234 MSIFOLDER *folder;
235 const WCHAR *install_path;
237 install_path = msi_get_target_folder( package, dir );
238 if (!install_path) return ERROR_FUNCTION_FAILED;
240 folder = msi_get_loaded_folder( package, dir );
241 if (folder->State == FOLDER_STATE_UNINITIALIZED)
243 msi_create_full_path( install_path );
244 folder->State = FOLDER_STATE_CREATED;
246 return ERROR_SUCCESS;
249 static MSIFILE *find_file( MSIPACKAGE *package, UINT disk_id, const WCHAR *filename )
251 MSIFILE *file;
253 LIST_FOR_EACH_ENTRY( file, &package->files, MSIFILE, entry )
255 if (file->disk_id == disk_id &&
256 file->state != msifs_installed &&
257 !strcmpiW( filename, file->File )) return file;
259 return NULL;
262 static BOOL installfiles_cb(MSIPACKAGE *package, LPCWSTR file, DWORD action,
263 LPWSTR *path, DWORD *attrs, PVOID user)
265 static MSIFILE *f = NULL;
266 UINT_PTR disk_id = (UINT_PTR)user;
268 if (action == MSICABEXTRACT_BEGINEXTRACT)
270 if (!(f = find_file( package, disk_id, file )))
272 TRACE("unknown file in cabinet (%s)\n", debugstr_w(file));
273 return FALSE;
275 if (f->disk_id != disk_id || (f->state != msifs_missing && f->state != msifs_overwrite))
276 return FALSE;
278 if (!f->Component->assembly || f->Component->assembly->application)
280 msi_create_directory(package, f->Component->Directory);
282 *path = strdupW(f->TargetPath);
283 *attrs = f->Attributes;
285 else if (action == MSICABEXTRACT_FILEEXTRACTED)
287 f->state = msifs_installed;
288 f = NULL;
291 return TRUE;
294 WCHAR *msi_resolve_file_source( MSIPACKAGE *package, MSIFILE *file )
296 WCHAR *p, *path;
298 TRACE("Working to resolve source of file %s\n", debugstr_w(file->File));
300 if (file->IsCompressed) return NULL;
302 p = msi_resolve_source_folder( package, file->Component->Directory, NULL );
303 path = msi_build_directory_name( 2, p, file->ShortName );
305 if (file->LongName && GetFileAttributesW( path ) == INVALID_FILE_ATTRIBUTES)
307 msi_free( path );
308 path = msi_build_directory_name( 2, p, file->LongName );
310 msi_free( p );
311 TRACE("file %s source resolves to %s\n", debugstr_w(file->File), debugstr_w(path));
312 return path;
316 * ACTION_InstallFiles()
318 * For efficiency, this is done in two passes:
319 * 1) Correct all the TargetPaths and determine what files are to be installed.
320 * 2) Extract Cabinets and copy files.
322 UINT ACTION_InstallFiles(MSIPACKAGE *package)
324 MSIMEDIAINFO *mi;
325 MSICOMPONENT *comp;
326 UINT rc = ERROR_SUCCESS;
327 MSIFILE *file;
329 schedule_install_files(package);
330 mi = msi_alloc_zero( sizeof(MSIMEDIAINFO) );
332 LIST_FOR_EACH_ENTRY( file, &package->files, MSIFILE, entry )
334 msi_file_update_ui( package, file, szInstallFiles );
336 rc = msi_load_media_info( package, file->Sequence, mi );
337 if (rc != ERROR_SUCCESS)
339 ERR("Unable to load media info for %s (%u)\n", debugstr_w(file->File), rc);
340 rc = ERROR_FUNCTION_FAILED;
341 goto done;
343 if (!file->Component->Enabled) continue;
345 if (file->state != msifs_hashmatch &&
346 file->state != msifs_skipped &&
347 (file->state != msifs_present || !msi_get_property_int( package->db, szInstalled, 0 )) &&
348 (rc = ready_media( package, file->IsCompressed, mi )))
350 ERR("Failed to ready media for %s\n", debugstr_w(file->File));
351 goto done;
354 if (file->state != msifs_missing && !mi->is_continuous && file->state != msifs_overwrite)
355 continue;
357 if (file->Sequence > mi->last_sequence || mi->is_continuous ||
358 (file->IsCompressed && !mi->is_extracted))
360 MSICABDATA data;
362 data.mi = mi;
363 data.package = package;
364 data.cb = installfiles_cb;
365 data.user = (PVOID)(UINT_PTR)mi->disk_id;
367 if (file->IsCompressed &&
368 !msi_cabextract(package, mi, &data))
370 ERR("Failed to extract cabinet: %s\n", debugstr_w(mi->cabinet));
371 rc = ERROR_INSTALL_FAILURE;
372 goto done;
376 if (!file->IsCompressed)
378 WCHAR *source = msi_resolve_file_source(package, file);
380 TRACE("copying %s to %s\n", debugstr_w(source), debugstr_w(file->TargetPath));
382 if (!file->Component->assembly || file->Component->assembly->application)
384 msi_create_directory(package, file->Component->Directory);
386 rc = copy_install_file(package, file, source);
387 if (rc != ERROR_SUCCESS)
389 ERR("Failed to copy %s to %s (%d)\n", debugstr_w(source),
390 debugstr_w(file->TargetPath), rc);
391 rc = ERROR_INSTALL_FAILURE;
392 msi_free(source);
393 goto done;
395 msi_free(source);
397 else if (file->state != msifs_installed && !(file->Attributes & msidbFileAttributesPatchAdded))
399 ERR("compressed file wasn't installed (%s)\n", debugstr_w(file->File));
400 rc = ERROR_INSTALL_FAILURE;
401 goto done;
404 LIST_FOR_EACH_ENTRY( comp, &package->components, MSICOMPONENT, entry )
406 comp->Action = msi_get_component_action( package, comp );
407 if (comp->Action == INSTALLSTATE_LOCAL && comp->assembly && !comp->assembly->installed)
409 rc = msi_install_assembly( package, comp );
410 if (rc != ERROR_SUCCESS)
412 ERR("Failed to install assembly\n");
413 rc = ERROR_INSTALL_FAILURE;
414 break;
419 done:
420 msi_free_media_info(mi);
421 return rc;
424 static BOOL load_mspatcha(void)
426 hmspatcha = LoadLibraryA("mspatcha.dll");
427 if (!hmspatcha)
429 ERR("Failed to load mspatcha.dll: %d\n", GetLastError());
430 return FALSE;
433 ApplyPatchToFileW = (void*)GetProcAddress(hmspatcha, "ApplyPatchToFileW");
434 if(!ApplyPatchToFileW)
436 ERR("GetProcAddress(ApplyPatchToFileW) failed: %d.\n", GetLastError());
437 return FALSE;
440 return TRUE;
443 static void unload_mspatch(void)
445 FreeLibrary(hmspatcha);
448 static MSIFILEPATCH *get_next_filepatch( MSIPACKAGE *package, const WCHAR *key )
450 MSIFILEPATCH *patch;
452 LIST_FOR_EACH_ENTRY( patch, &package->filepatches, MSIFILEPATCH, entry )
454 if (!patch->IsApplied && !strcmpW( key, patch->File->File )) return patch;
456 return NULL;
459 static BOOL patchfiles_cb(MSIPACKAGE *package, LPCWSTR file, DWORD action,
460 LPWSTR *path, DWORD *attrs, PVOID user)
462 static MSIFILEPATCH *p = NULL;
463 static WCHAR patch_path[MAX_PATH] = {0};
464 static WCHAR temp_folder[MAX_PATH] = {0};
466 if (action == MSICABEXTRACT_BEGINEXTRACT)
468 if (temp_folder[0] == '\0')
469 GetTempPathW(MAX_PATH, temp_folder);
471 if (!(p = get_next_filepatch(package, file)) || !p->File->Component->Enabled)
472 return FALSE;
474 GetTempFileNameW(temp_folder, NULL, 0, patch_path);
476 *path = strdupW(patch_path);
477 *attrs = p->File->Attributes;
479 else if (action == MSICABEXTRACT_FILEEXTRACTED)
481 WCHAR patched_file[MAX_PATH];
482 BOOL br;
484 GetTempFileNameW(temp_folder, NULL, 0, patched_file);
486 br = ApplyPatchToFileW(patch_path, p->File->TargetPath, patched_file, 0);
487 if (br)
489 /* FIXME: baseline cache */
491 DeleteFileW( p->File->TargetPath );
492 MoveFileW( patched_file, p->File->TargetPath );
494 p->IsApplied = TRUE;
496 else
497 ERR("Failed patch %s: %d.\n", debugstr_w(p->File->TargetPath), GetLastError());
499 DeleteFileW(patch_path);
500 p = NULL;
503 return TRUE;
506 UINT ACTION_PatchFiles( MSIPACKAGE *package )
508 MSIFILEPATCH *patch;
509 MSIMEDIAINFO *mi;
510 UINT rc = ERROR_SUCCESS;
511 BOOL mspatcha_loaded = FALSE;
513 TRACE("%p\n", package);
515 mi = msi_alloc_zero( sizeof(MSIMEDIAINFO) );
517 LIST_FOR_EACH_ENTRY( patch, &package->filepatches, MSIFILEPATCH, entry )
519 MSIFILE *file = patch->File;
520 MSICOMPONENT *comp = file->Component;
522 rc = msi_load_media_info( package, patch->Sequence, mi );
523 if (rc != ERROR_SUCCESS)
525 ERR("Unable to load media info for %s (%u)\n", debugstr_w(file->File), rc);
526 rc = ERROR_FUNCTION_FAILED;
527 goto done;
529 comp->Action = msi_get_component_action( package, comp );
530 if (!comp->Enabled || comp->Action != INSTALLSTATE_LOCAL) continue;
532 if (!patch->IsApplied)
534 MSICABDATA data;
536 rc = ready_media( package, TRUE, mi );
537 if (rc != ERROR_SUCCESS)
539 ERR("Failed to ready media for %s\n", debugstr_w(file->File));
540 goto done;
543 if (!mspatcha_loaded && !load_mspatcha())
545 rc = ERROR_FUNCTION_FAILED;
546 goto done;
548 mspatcha_loaded = TRUE;
550 data.mi = mi;
551 data.package = package;
552 data.cb = patchfiles_cb;
553 data.user = (PVOID)(UINT_PTR)mi->disk_id;
555 if (!msi_cabextract(package, mi, &data))
557 ERR("Failed to extract cabinet: %s\n", debugstr_w(mi->cabinet));
558 rc = ERROR_INSTALL_FAILURE;
559 goto done;
563 if (!patch->IsApplied && !(patch->Attributes & msidbPatchAttributesNonVital))
565 ERR("Failed to apply patch to file: %s\n", debugstr_w(file->File));
566 rc = ERROR_INSTALL_FAILURE;
567 goto done;
571 done:
572 msi_free_media_info(mi);
573 if (mspatcha_loaded)
574 unload_mspatch();
575 return rc;
578 #define is_dot_dir(x) ((x[0] == '.') && ((x[1] == 0) || ((x[1] == '.') && (x[2] == 0))))
580 typedef struct
582 struct list entry;
583 LPWSTR sourcename;
584 LPWSTR destname;
585 LPWSTR source;
586 LPWSTR dest;
587 } FILE_LIST;
589 static BOOL msi_move_file(LPCWSTR source, LPCWSTR dest, int options)
591 BOOL ret;
593 if (GetFileAttributesW(source) == FILE_ATTRIBUTE_DIRECTORY ||
594 GetFileAttributesW(dest) == FILE_ATTRIBUTE_DIRECTORY)
596 WARN("Source or dest is directory, not moving\n");
597 return FALSE;
600 if (options == msidbMoveFileOptionsMove)
602 TRACE("moving %s -> %s\n", debugstr_w(source), debugstr_w(dest));
603 ret = MoveFileExW(source, dest, MOVEFILE_REPLACE_EXISTING);
604 if (!ret)
606 WARN("MoveFile failed: %d\n", GetLastError());
607 return FALSE;
610 else
612 TRACE("copying %s -> %s\n", debugstr_w(source), debugstr_w(dest));
613 ret = CopyFileW(source, dest, FALSE);
614 if (!ret)
616 WARN("CopyFile failed: %d\n", GetLastError());
617 return FALSE;
621 return TRUE;
624 static LPWSTR wildcard_to_file(LPWSTR wildcard, LPWSTR filename)
626 LPWSTR path, ptr;
627 DWORD dirlen, pathlen;
629 ptr = strrchrW(wildcard, '\\');
630 dirlen = ptr - wildcard + 1;
632 pathlen = dirlen + lstrlenW(filename) + 1;
633 path = msi_alloc(pathlen * sizeof(WCHAR));
635 lstrcpynW(path, wildcard, dirlen + 1);
636 lstrcatW(path, filename);
638 return path;
641 static void free_file_entry(FILE_LIST *file)
643 msi_free(file->source);
644 msi_free(file->dest);
645 msi_free(file);
648 static void free_list(FILE_LIST *list)
650 while (!list_empty(&list->entry))
652 FILE_LIST *file = LIST_ENTRY(list_head(&list->entry), FILE_LIST, entry);
654 list_remove(&file->entry);
655 free_file_entry(file);
659 static BOOL add_wildcard(FILE_LIST *files, LPWSTR source, LPWSTR dest)
661 FILE_LIST *new, *file;
662 LPWSTR ptr, filename;
663 DWORD size;
665 new = msi_alloc_zero(sizeof(FILE_LIST));
666 if (!new)
667 return FALSE;
669 new->source = strdupW(source);
670 ptr = strrchrW(dest, '\\') + 1;
671 filename = strrchrW(new->source, '\\') + 1;
673 new->sourcename = filename;
675 if (*ptr)
676 new->destname = ptr;
677 else
678 new->destname = new->sourcename;
680 size = (ptr - dest) + lstrlenW(filename) + 1;
681 new->dest = msi_alloc(size * sizeof(WCHAR));
682 if (!new->dest)
684 free_file_entry(new);
685 return FALSE;
688 lstrcpynW(new->dest, dest, ptr - dest + 1);
689 lstrcatW(new->dest, filename);
691 if (list_empty(&files->entry))
693 list_add_head(&files->entry, &new->entry);
694 return TRUE;
697 LIST_FOR_EACH_ENTRY(file, &files->entry, FILE_LIST, entry)
699 if (strcmpW( source, file->source ) < 0)
701 list_add_before(&file->entry, &new->entry);
702 return TRUE;
706 list_add_after(&file->entry, &new->entry);
707 return TRUE;
710 static BOOL move_files_wildcard(LPWSTR source, LPWSTR dest, int options)
712 WIN32_FIND_DATAW wfd;
713 HANDLE hfile;
714 LPWSTR path;
715 BOOL res;
716 FILE_LIST files, *file;
717 DWORD size;
719 hfile = FindFirstFileW(source, &wfd);
720 if (hfile == INVALID_HANDLE_VALUE) return FALSE;
722 list_init(&files.entry);
724 for (res = TRUE; res; res = FindNextFileW(hfile, &wfd))
726 if (is_dot_dir(wfd.cFileName)) continue;
728 path = wildcard_to_file(source, wfd.cFileName);
729 if (!path)
731 res = FALSE;
732 goto done;
735 add_wildcard(&files, path, dest);
736 msi_free(path);
739 /* no files match the wildcard */
740 if (list_empty(&files.entry))
741 goto done;
743 /* only the first wildcard match gets renamed to dest */
744 file = LIST_ENTRY(list_head(&files.entry), FILE_LIST, entry);
745 size = (strrchrW(file->dest, '\\') - file->dest) + lstrlenW(file->destname) + 2;
746 file->dest = msi_realloc(file->dest, size * sizeof(WCHAR));
747 if (!file->dest)
749 res = FALSE;
750 goto done;
753 /* file->dest may be shorter after the reallocation, so add a NULL
754 * terminator. This is needed for the call to strrchrW, as there will no
755 * longer be a NULL terminator within the bounds of the allocation in this case.
757 file->dest[size - 1] = '\0';
758 lstrcpyW(strrchrW(file->dest, '\\') + 1, file->destname);
760 while (!list_empty(&files.entry))
762 file = LIST_ENTRY(list_head(&files.entry), FILE_LIST, entry);
764 msi_move_file(file->source, file->dest, options);
766 list_remove(&file->entry);
767 free_file_entry(file);
770 res = TRUE;
772 done:
773 free_list(&files);
774 FindClose(hfile);
775 return res;
778 void msi_reduce_to_long_filename( WCHAR *filename )
780 WCHAR *p = strchrW( filename, '|' );
781 if (p) memmove( filename, p + 1, (strlenW( p + 1 ) + 1) * sizeof(WCHAR) );
784 static UINT ITERATE_MoveFiles( MSIRECORD *rec, LPVOID param )
786 MSIPACKAGE *package = param;
787 MSIRECORD *uirow;
788 MSICOMPONENT *comp;
789 LPCWSTR sourcename, component;
790 LPWSTR sourcedir, destname = NULL, destdir = NULL, source = NULL, dest = NULL;
791 int options;
792 DWORD size;
793 BOOL wildcards;
795 component = MSI_RecordGetString(rec, 2);
796 comp = msi_get_loaded_component(package, component);
797 if (!comp)
798 return ERROR_SUCCESS;
800 comp->Action = msi_get_component_action( package, comp );
801 if (comp->Action != INSTALLSTATE_LOCAL)
803 TRACE("component not scheduled for installation %s\n", debugstr_w(component));
804 return ERROR_SUCCESS;
807 sourcename = MSI_RecordGetString(rec, 3);
808 options = MSI_RecordGetInteger(rec, 7);
810 sourcedir = msi_dup_property(package->db, MSI_RecordGetString(rec, 5));
811 if (!sourcedir)
812 goto done;
814 destdir = msi_dup_property(package->db, MSI_RecordGetString(rec, 6));
815 if (!destdir)
816 goto done;
818 if (!sourcename)
820 if (GetFileAttributesW(sourcedir) == INVALID_FILE_ATTRIBUTES)
821 goto done;
823 source = strdupW(sourcedir);
824 if (!source)
825 goto done;
827 else
829 size = lstrlenW(sourcedir) + lstrlenW(sourcename) + 2;
830 source = msi_alloc(size * sizeof(WCHAR));
831 if (!source)
832 goto done;
834 lstrcpyW(source, sourcedir);
835 if (source[lstrlenW(source) - 1] != '\\')
836 lstrcatW(source, szBackSlash);
837 lstrcatW(source, sourcename);
840 wildcards = strchrW(source, '*') || strchrW(source, '?');
842 if (MSI_RecordIsNull(rec, 4))
844 if (!wildcards)
846 WCHAR *p;
847 if (sourcename)
848 destname = strdupW(sourcename);
849 else if ((p = strrchrW(sourcedir, '\\')))
850 destname = strdupW(p + 1);
851 else
852 destname = strdupW(sourcedir);
853 if (!destname)
854 goto done;
857 else
859 destname = strdupW(MSI_RecordGetString(rec, 4));
860 if (destname) msi_reduce_to_long_filename(destname);
863 size = 0;
864 if (destname)
865 size = lstrlenW(destname);
867 size += lstrlenW(destdir) + 2;
868 dest = msi_alloc(size * sizeof(WCHAR));
869 if (!dest)
870 goto done;
872 lstrcpyW(dest, destdir);
873 if (dest[lstrlenW(dest) - 1] != '\\')
874 lstrcatW(dest, szBackSlash);
876 if (destname)
877 lstrcatW(dest, destname);
879 if (GetFileAttributesW(destdir) == INVALID_FILE_ATTRIBUTES)
881 if (!msi_create_full_path(destdir))
883 WARN("failed to create directory %u\n", GetLastError());
884 goto done;
888 if (!wildcards)
889 msi_move_file(source, dest, options);
890 else
891 move_files_wildcard(source, dest, options);
893 done:
894 uirow = MSI_CreateRecord( 9 );
895 MSI_RecordSetStringW( uirow, 1, MSI_RecordGetString(rec, 1) );
896 MSI_RecordSetInteger( uirow, 6, 1 ); /* FIXME */
897 MSI_RecordSetStringW( uirow, 9, destdir );
898 msi_ui_actiondata( package, szMoveFiles, uirow );
899 msiobj_release( &uirow->hdr );
901 msi_free(sourcedir);
902 msi_free(destdir);
903 msi_free(destname);
904 msi_free(source);
905 msi_free(dest);
907 return ERROR_SUCCESS;
910 UINT ACTION_MoveFiles( MSIPACKAGE *package )
912 static const WCHAR query[] = {
913 'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
914 '`','M','o','v','e','F','i','l','e','`',0};
915 MSIQUERY *view;
916 UINT rc;
918 rc = MSI_DatabaseOpenViewW(package->db, query, &view);
919 if (rc != ERROR_SUCCESS)
920 return ERROR_SUCCESS;
922 rc = MSI_IterateRecords(view, NULL, ITERATE_MoveFiles, package);
923 msiobj_release(&view->hdr);
924 return rc;
927 static WCHAR *get_duplicate_filename( MSIPACKAGE *package, MSIRECORD *row, const WCHAR *file_key, const WCHAR *src )
929 DWORD len;
930 WCHAR *dst_name, *dst_path, *dst;
932 if (MSI_RecordIsNull( row, 4 ))
934 len = strlenW( src ) + 1;
935 if (!(dst_name = msi_alloc( len * sizeof(WCHAR)))) return NULL;
936 strcpyW( dst_name, strrchrW( src, '\\' ) + 1 );
938 else
940 MSI_RecordGetStringW( row, 4, NULL, &len );
941 if (!(dst_name = msi_alloc( ++len * sizeof(WCHAR) ))) return NULL;
942 MSI_RecordGetStringW( row, 4, dst_name, &len );
943 msi_reduce_to_long_filename( dst_name );
946 if (MSI_RecordIsNull( row, 5 ))
948 WCHAR *p;
949 dst_path = strdupW( src );
950 p = strrchrW( dst_path, '\\' );
951 if (p) *p = 0;
953 else
955 const WCHAR *dst_key = MSI_RecordGetString( row, 5 );
957 dst_path = strdupW( msi_get_target_folder( package, dst_key ) );
958 if (!dst_path)
960 /* try a property */
961 dst_path = msi_dup_property( package->db, dst_key );
962 if (!dst_path)
964 FIXME("Unable to get destination folder, try AppSearch properties\n");
965 msi_free( dst_name );
966 return NULL;
971 dst = msi_build_directory_name( 2, dst_path, dst_name );
972 msi_create_full_path( dst_path );
974 msi_free( dst_name );
975 msi_free( dst_path );
976 return dst;
979 static UINT ITERATE_DuplicateFiles(MSIRECORD *row, LPVOID param)
981 MSIPACKAGE *package = param;
982 LPWSTR dest;
983 LPCWSTR file_key, component;
984 MSICOMPONENT *comp;
985 MSIRECORD *uirow;
986 MSIFILE *file;
988 component = MSI_RecordGetString(row,2);
989 comp = msi_get_loaded_component(package, component);
990 if (!comp)
991 return ERROR_SUCCESS;
993 comp->Action = msi_get_component_action( package, comp );
994 if (comp->Action != INSTALLSTATE_LOCAL)
996 TRACE("component not scheduled for installation %s\n", debugstr_w(component));
997 return ERROR_SUCCESS;
1000 file_key = MSI_RecordGetString(row,3);
1001 if (!file_key)
1003 ERR("Unable to get file key\n");
1004 return ERROR_FUNCTION_FAILED;
1007 file = msi_get_loaded_file( package, file_key );
1008 if (!file)
1010 ERR("Original file unknown %s\n", debugstr_w(file_key));
1011 return ERROR_SUCCESS;
1014 dest = get_duplicate_filename( package, row, file_key, file->TargetPath );
1015 if (!dest)
1017 WARN("Unable to get duplicate filename\n");
1018 return ERROR_SUCCESS;
1021 TRACE("Duplicating file %s to %s\n", debugstr_w(file->TargetPath), debugstr_w(dest));
1023 if (!CopyFileW( file->TargetPath, dest, TRUE ))
1025 WARN("Failed to copy file %s -> %s (%u)\n",
1026 debugstr_w(file->TargetPath), debugstr_w(dest), GetLastError());
1029 FIXME("We should track these duplicate files as well\n");
1031 uirow = MSI_CreateRecord( 9 );
1032 MSI_RecordSetStringW( uirow, 1, MSI_RecordGetString( row, 1 ) );
1033 MSI_RecordSetInteger( uirow, 6, file->FileSize );
1034 MSI_RecordSetStringW( uirow, 9, MSI_RecordGetString( row, 5 ) );
1035 msi_ui_actiondata( package, szDuplicateFiles, uirow );
1036 msiobj_release( &uirow->hdr );
1038 msi_free(dest);
1039 return ERROR_SUCCESS;
1042 UINT ACTION_DuplicateFiles(MSIPACKAGE *package)
1044 static const WCHAR query[] = {
1045 'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
1046 '`','D','u','p','l','i','c','a','t','e','F','i','l','e','`',0};
1047 MSIQUERY *view;
1048 UINT rc;
1050 rc = MSI_DatabaseOpenViewW(package->db, query, &view);
1051 if (rc != ERROR_SUCCESS)
1052 return ERROR_SUCCESS;
1054 rc = MSI_IterateRecords(view, NULL, ITERATE_DuplicateFiles, package);
1055 msiobj_release(&view->hdr);
1056 return rc;
1059 static UINT ITERATE_RemoveDuplicateFiles( MSIRECORD *row, LPVOID param )
1061 MSIPACKAGE *package = param;
1062 LPWSTR dest;
1063 LPCWSTR file_key, component;
1064 MSICOMPONENT *comp;
1065 MSIRECORD *uirow;
1066 MSIFILE *file;
1068 component = MSI_RecordGetString( row, 2 );
1069 comp = msi_get_loaded_component( package, component );
1070 if (!comp)
1071 return ERROR_SUCCESS;
1073 comp->Action = msi_get_component_action( package, comp );
1074 if (comp->Action != INSTALLSTATE_ABSENT)
1076 TRACE("component not scheduled for removal %s\n", debugstr_w(component));
1077 return ERROR_SUCCESS;
1080 file_key = MSI_RecordGetString( row, 3 );
1081 if (!file_key)
1083 ERR("Unable to get file key\n");
1084 return ERROR_FUNCTION_FAILED;
1087 file = msi_get_loaded_file( package, file_key );
1088 if (!file)
1090 ERR("Original file unknown %s\n", debugstr_w(file_key));
1091 return ERROR_SUCCESS;
1094 dest = get_duplicate_filename( package, row, file_key, file->TargetPath );
1095 if (!dest)
1097 WARN("Unable to get duplicate filename\n");
1098 return ERROR_SUCCESS;
1101 TRACE("Removing duplicate %s of %s\n", debugstr_w(dest), debugstr_w(file->TargetPath));
1103 if (!DeleteFileW( dest ))
1105 WARN("Failed to delete duplicate file %s (%u)\n", debugstr_w(dest), GetLastError());
1108 uirow = MSI_CreateRecord( 9 );
1109 MSI_RecordSetStringW( uirow, 1, MSI_RecordGetString( row, 1 ) );
1110 MSI_RecordSetStringW( uirow, 9, MSI_RecordGetString( row, 5 ) );
1111 msi_ui_actiondata( package, szRemoveDuplicateFiles, uirow );
1112 msiobj_release( &uirow->hdr );
1114 msi_free(dest);
1115 return ERROR_SUCCESS;
1118 UINT ACTION_RemoveDuplicateFiles( MSIPACKAGE *package )
1120 static const WCHAR query[] = {
1121 'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
1122 '`','D','u','p','l','i','c','a','t','e','F','i','l','e','`',0};
1123 MSIQUERY *view;
1124 UINT rc;
1126 rc = MSI_DatabaseOpenViewW( package->db, query, &view );
1127 if (rc != ERROR_SUCCESS)
1128 return ERROR_SUCCESS;
1130 rc = MSI_IterateRecords( view, NULL, ITERATE_RemoveDuplicateFiles, package );
1131 msiobj_release( &view->hdr );
1132 return rc;
1135 static BOOL verify_comp_for_removal(MSICOMPONENT *comp, UINT install_mode)
1137 /* special case */
1138 if (comp->Action != INSTALLSTATE_SOURCE &&
1139 comp->Attributes & msidbComponentAttributesSourceOnly &&
1140 (install_mode == msidbRemoveFileInstallModeOnRemove ||
1141 install_mode == msidbRemoveFileInstallModeOnBoth)) return TRUE;
1143 switch (comp->Action)
1145 case INSTALLSTATE_LOCAL:
1146 case INSTALLSTATE_SOURCE:
1147 if (install_mode == msidbRemoveFileInstallModeOnInstall ||
1148 install_mode == msidbRemoveFileInstallModeOnBoth) return TRUE;
1149 break;
1150 case INSTALLSTATE_ABSENT:
1151 if (install_mode == msidbRemoveFileInstallModeOnRemove ||
1152 install_mode == msidbRemoveFileInstallModeOnBoth) return TRUE;
1153 break;
1154 default: break;
1156 return FALSE;
1159 static UINT ITERATE_RemoveFiles(MSIRECORD *row, LPVOID param)
1161 MSIPACKAGE *package = param;
1162 MSICOMPONENT *comp;
1163 MSIRECORD *uirow;
1164 LPCWSTR component, dirprop;
1165 UINT install_mode;
1166 LPWSTR dir = NULL, path = NULL, filename = NULL;
1167 DWORD size;
1168 UINT ret = ERROR_SUCCESS;
1170 component = MSI_RecordGetString(row, 2);
1171 dirprop = MSI_RecordGetString(row, 4);
1172 install_mode = MSI_RecordGetInteger(row, 5);
1174 comp = msi_get_loaded_component(package, component);
1175 if (!comp)
1176 return ERROR_SUCCESS;
1178 comp->Action = msi_get_component_action( package, comp );
1179 if (!verify_comp_for_removal(comp, install_mode))
1181 TRACE("Skipping removal due to install mode\n");
1182 return ERROR_SUCCESS;
1184 if (comp->assembly && !comp->assembly->application)
1186 return ERROR_SUCCESS;
1188 if (comp->Attributes & msidbComponentAttributesPermanent)
1190 TRACE("permanent component, not removing file\n");
1191 return ERROR_SUCCESS;
1194 dir = msi_dup_property(package->db, dirprop);
1195 if (!dir)
1197 WARN("directory property has no value\n");
1198 return ERROR_SUCCESS;
1200 size = 0;
1201 if ((filename = strdupW( MSI_RecordGetString(row, 3) )))
1203 msi_reduce_to_long_filename( filename );
1204 size = lstrlenW( filename );
1206 size += lstrlenW(dir) + 2;
1207 path = msi_alloc(size * sizeof(WCHAR));
1208 if (!path)
1210 ret = ERROR_OUTOFMEMORY;
1211 goto done;
1214 if (filename)
1216 lstrcpyW(path, dir);
1217 PathAddBackslashW(path);
1218 lstrcatW(path, filename);
1220 TRACE("Deleting misc file: %s\n", debugstr_w(path));
1221 DeleteFileW(path);
1223 else
1225 TRACE("Removing misc directory: %s\n", debugstr_w(dir));
1226 RemoveDirectoryW(dir);
1229 done:
1230 uirow = MSI_CreateRecord( 9 );
1231 MSI_RecordSetStringW( uirow, 1, MSI_RecordGetString(row, 1) );
1232 MSI_RecordSetStringW( uirow, 9, dir );
1233 msi_ui_actiondata( package, szRemoveFiles, uirow );
1234 msiobj_release( &uirow->hdr );
1236 msi_free(filename);
1237 msi_free(path);
1238 msi_free(dir);
1239 return ret;
1242 static void remove_folder( MSIFOLDER *folder )
1244 FolderList *fl;
1246 LIST_FOR_EACH_ENTRY( fl, &folder->children, FolderList, entry )
1248 remove_folder( fl->folder );
1250 if (!folder->persistent && folder->State != FOLDER_STATE_REMOVED)
1252 if (RemoveDirectoryW( folder->ResolvedTarget )) folder->State = FOLDER_STATE_REMOVED;
1256 UINT ACTION_RemoveFiles( MSIPACKAGE *package )
1258 static const WCHAR query[] = {
1259 'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
1260 '`','R','e','m','o','v','e','F','i','l','e','`',0};
1261 MSIQUERY *view;
1262 MSICOMPONENT *comp;
1263 MSIFILE *file;
1264 UINT r;
1266 r = MSI_DatabaseOpenViewW(package->db, query, &view);
1267 if (r == ERROR_SUCCESS)
1269 r = MSI_IterateRecords(view, NULL, ITERATE_RemoveFiles, package);
1270 msiobj_release(&view->hdr);
1271 if (r != ERROR_SUCCESS)
1272 return r;
1275 LIST_FOR_EACH_ENTRY( file, &package->files, MSIFILE, entry )
1277 MSIRECORD *uirow;
1278 VS_FIXEDFILEINFO *ver;
1280 comp = file->Component;
1281 msi_file_update_ui( package, file, szRemoveFiles );
1283 comp->Action = msi_get_component_action( package, comp );
1284 if (comp->Action != INSTALLSTATE_ABSENT || comp->Installed == INSTALLSTATE_SOURCE)
1285 continue;
1287 if (comp->assembly && !comp->assembly->application)
1288 continue;
1290 if (comp->Attributes & msidbComponentAttributesPermanent)
1292 TRACE("permanent component, not removing file\n");
1293 continue;
1296 if (file->Version)
1298 ver = msi_get_disk_file_version( file->TargetPath );
1299 if (ver && msi_compare_file_versions( ver, file->Version ) > 0)
1301 TRACE("newer version detected, not removing file\n");
1302 msi_free( ver );
1303 continue;
1305 msi_free( ver );
1308 if (file->state == msifs_installed)
1309 WARN("removing installed file %s\n", debugstr_w(file->TargetPath));
1311 TRACE("removing %s\n", debugstr_w(file->File) );
1313 SetFileAttributesW( file->TargetPath, FILE_ATTRIBUTE_NORMAL );
1314 if (!DeleteFileW( file->TargetPath ))
1316 WARN("failed to delete %s (%u)\n", debugstr_w(file->TargetPath), GetLastError());
1318 file->state = msifs_missing;
1320 uirow = MSI_CreateRecord( 9 );
1321 MSI_RecordSetStringW( uirow, 1, file->FileName );
1322 MSI_RecordSetStringW( uirow, 9, comp->Directory );
1323 msi_ui_actiondata( package, szRemoveFiles, uirow );
1324 msiobj_release( &uirow->hdr );
1327 LIST_FOR_EACH_ENTRY( comp, &package->components, MSICOMPONENT, entry )
1329 comp->Action = msi_get_component_action( package, comp );
1330 if (comp->Action != INSTALLSTATE_ABSENT) continue;
1332 if (comp->Attributes & msidbComponentAttributesPermanent)
1334 TRACE("permanent component, not removing directory\n");
1335 continue;
1337 if (comp->assembly && !comp->assembly->application)
1338 msi_uninstall_assembly( package, comp );
1339 else
1341 MSIFOLDER *folder = msi_get_loaded_folder( package, comp->Directory );
1342 remove_folder( folder );
1345 return ERROR_SUCCESS;