riched32: Don't bother to unregister classes at process exit.
[wine/wine-gecko.git] / dlls / msi / files.c
blobd53da91da6ef062d595cce5abf336178afa9b1d1
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, const WCHAR *filename )
251 MSIFILE *file;
253 LIST_FOR_EACH_ENTRY( file, &package->files, MSIFILE, entry )
255 if (file->state != msifs_installed && !strcmpiW( filename, file->File )) return file;
257 return NULL;
260 static BOOL installfiles_cb(MSIPACKAGE *package, LPCWSTR file, DWORD action,
261 LPWSTR *path, DWORD *attrs, PVOID user)
263 static MSIFILE *f = NULL;
264 UINT_PTR disk_id = (UINT_PTR)user;
266 if (action == MSICABEXTRACT_BEGINEXTRACT)
268 if (!(f = find_file( package, file )))
270 TRACE("unknown file in cabinet (%s)\n", debugstr_w(file));
271 return FALSE;
273 if (f->disk_id != disk_id || (f->state != msifs_missing && f->state != msifs_overwrite))
274 return FALSE;
276 if (!f->Component->assembly || f->Component->assembly->application)
278 msi_create_directory(package, f->Component->Directory);
280 *path = strdupW(f->TargetPath);
281 *attrs = f->Attributes;
283 else if (action == MSICABEXTRACT_FILEEXTRACTED)
285 f->state = msifs_installed;
286 f = NULL;
289 return TRUE;
292 WCHAR *msi_resolve_file_source( MSIPACKAGE *package, MSIFILE *file )
294 WCHAR *p, *path;
296 TRACE("Working to resolve source of file %s\n", debugstr_w(file->File));
298 if (file->IsCompressed) return NULL;
300 p = msi_resolve_source_folder( package, file->Component->Directory, NULL );
301 path = msi_build_directory_name( 2, p, file->ShortName );
303 if (file->LongName && GetFileAttributesW( path ) == INVALID_FILE_ATTRIBUTES)
305 msi_free( path );
306 path = msi_build_directory_name( 2, p, file->LongName );
308 msi_free( p );
309 TRACE("file %s source resolves to %s\n", debugstr_w(file->File), debugstr_w(path));
310 return path;
314 * ACTION_InstallFiles()
316 * For efficiency, this is done in two passes:
317 * 1) Correct all the TargetPaths and determine what files are to be installed.
318 * 2) Extract Cabinets and copy files.
320 UINT ACTION_InstallFiles(MSIPACKAGE *package)
322 MSIMEDIAINFO *mi;
323 MSICOMPONENT *comp;
324 UINT rc = ERROR_SUCCESS;
325 MSIFILE *file;
327 schedule_install_files(package);
328 mi = msi_alloc_zero( sizeof(MSIMEDIAINFO) );
330 LIST_FOR_EACH_ENTRY( file, &package->files, MSIFILE, entry )
332 msi_file_update_ui( package, file, szInstallFiles );
334 rc = msi_load_media_info( package, file->Sequence, mi );
335 if (rc != ERROR_SUCCESS)
337 ERR("Unable to load media info for %s (%u)\n", debugstr_w(file->File), rc);
338 rc = ERROR_FUNCTION_FAILED;
339 goto done;
341 if (!file->Component->Enabled) continue;
343 if (file->state != msifs_hashmatch &&
344 file->state != msifs_skipped &&
345 (file->state != msifs_present || !msi_get_property_int( package->db, szInstalled, 0 )) &&
346 (rc = ready_media( package, file->IsCompressed, mi )))
348 ERR("Failed to ready media for %s\n", debugstr_w(file->File));
349 goto done;
352 if (file->state != msifs_missing && !mi->is_continuous && file->state != msifs_overwrite)
353 continue;
355 if (file->Sequence > mi->last_sequence || mi->is_continuous ||
356 (file->IsCompressed && !mi->is_extracted))
358 MSICABDATA data;
360 data.mi = mi;
361 data.package = package;
362 data.cb = installfiles_cb;
363 data.user = (PVOID)(UINT_PTR)mi->disk_id;
365 if (file->IsCompressed &&
366 !msi_cabextract(package, mi, &data))
368 ERR("Failed to extract cabinet: %s\n", debugstr_w(mi->cabinet));
369 rc = ERROR_INSTALL_FAILURE;
370 goto done;
374 if (!file->IsCompressed)
376 WCHAR *source = msi_resolve_file_source(package, file);
378 TRACE("copying %s to %s\n", debugstr_w(source), debugstr_w(file->TargetPath));
380 if (!file->Component->assembly || file->Component->assembly->application)
382 msi_create_directory(package, file->Component->Directory);
384 rc = copy_install_file(package, file, source);
385 if (rc != ERROR_SUCCESS)
387 ERR("Failed to copy %s to %s (%d)\n", debugstr_w(source),
388 debugstr_w(file->TargetPath), rc);
389 rc = ERROR_INSTALL_FAILURE;
390 msi_free(source);
391 goto done;
393 msi_free(source);
395 else if (file->state != msifs_installed && !(file->Attributes & msidbFileAttributesPatchAdded))
397 ERR("compressed file wasn't installed (%s)\n", debugstr_w(file->TargetPath));
398 rc = ERROR_INSTALL_FAILURE;
399 goto done;
402 LIST_FOR_EACH_ENTRY( comp, &package->components, MSICOMPONENT, entry )
404 comp->Action = msi_get_component_action( package, comp );
405 if (comp->Action == INSTALLSTATE_LOCAL && comp->assembly && !comp->assembly->installed)
407 rc = msi_install_assembly( package, comp );
408 if (rc != ERROR_SUCCESS)
410 ERR("Failed to install assembly\n");
411 rc = ERROR_INSTALL_FAILURE;
412 break;
417 done:
418 msi_free_media_info(mi);
419 return rc;
422 static BOOL load_mspatcha(void)
424 hmspatcha = LoadLibraryA("mspatcha.dll");
425 if (!hmspatcha)
427 ERR("Failed to load mspatcha.dll: %d\n", GetLastError());
428 return FALSE;
431 ApplyPatchToFileW = (void*)GetProcAddress(hmspatcha, "ApplyPatchToFileW");
432 if(!ApplyPatchToFileW)
434 ERR("GetProcAddress(ApplyPatchToFileW) failed: %d.\n", GetLastError());
435 return FALSE;
438 return TRUE;
441 static void unload_mspatch(void)
443 FreeLibrary(hmspatcha);
446 static MSIFILEPATCH *get_next_filepatch( MSIPACKAGE *package, const WCHAR *key )
448 MSIFILEPATCH *patch;
450 LIST_FOR_EACH_ENTRY( patch, &package->filepatches, MSIFILEPATCH, entry )
452 if (!patch->IsApplied && !strcmpW( key, patch->File->File )) return patch;
454 return NULL;
457 static BOOL patchfiles_cb(MSIPACKAGE *package, LPCWSTR file, DWORD action,
458 LPWSTR *path, DWORD *attrs, PVOID user)
460 static MSIFILEPATCH *p = NULL;
461 static WCHAR patch_path[MAX_PATH] = {0};
462 static WCHAR temp_folder[MAX_PATH] = {0};
464 if (action == MSICABEXTRACT_BEGINEXTRACT)
466 if (temp_folder[0] == '\0')
467 GetTempPathW(MAX_PATH, temp_folder);
469 if (!(p = get_next_filepatch(package, file)) || !p->File->Component->Enabled)
470 return FALSE;
472 GetTempFileNameW(temp_folder, NULL, 0, patch_path);
474 *path = strdupW(patch_path);
475 *attrs = p->File->Attributes;
477 else if (action == MSICABEXTRACT_FILEEXTRACTED)
479 WCHAR patched_file[MAX_PATH];
480 BOOL br;
482 GetTempFileNameW(temp_folder, NULL, 0, patched_file);
484 br = ApplyPatchToFileW(patch_path, p->File->TargetPath, patched_file, 0);
485 if (br)
487 /* FIXME: baseline cache */
489 DeleteFileW( p->File->TargetPath );
490 MoveFileW( patched_file, p->File->TargetPath );
492 p->IsApplied = TRUE;
494 else
495 ERR("Failed patch %s: %d.\n", debugstr_w(p->File->TargetPath), GetLastError());
497 DeleteFileW(patch_path);
498 p = NULL;
501 return TRUE;
504 UINT ACTION_PatchFiles( MSIPACKAGE *package )
506 MSIFILEPATCH *patch;
507 MSIMEDIAINFO *mi;
508 UINT rc = ERROR_SUCCESS;
509 BOOL mspatcha_loaded = FALSE;
511 TRACE("%p\n", package);
513 mi = msi_alloc_zero( sizeof(MSIMEDIAINFO) );
515 LIST_FOR_EACH_ENTRY( patch, &package->filepatches, MSIFILEPATCH, entry )
517 MSIFILE *file = patch->File;
518 MSICOMPONENT *comp = file->Component;
520 rc = msi_load_media_info( package, patch->Sequence, mi );
521 if (rc != ERROR_SUCCESS)
523 ERR("Unable to load media info for %s (%u)\n", debugstr_w(file->File), rc);
524 rc = ERROR_FUNCTION_FAILED;
525 goto done;
527 comp->Action = msi_get_component_action( package, comp );
528 if (!comp->Enabled || comp->Action != INSTALLSTATE_LOCAL) continue;
530 if (!patch->IsApplied)
532 MSICABDATA data;
534 rc = ready_media( package, TRUE, mi );
535 if (rc != ERROR_SUCCESS)
537 ERR("Failed to ready media for %s\n", debugstr_w(file->File));
538 goto done;
541 if (!mspatcha_loaded && !load_mspatcha())
543 rc = ERROR_FUNCTION_FAILED;
544 goto done;
546 mspatcha_loaded = TRUE;
548 data.mi = mi;
549 data.package = package;
550 data.cb = patchfiles_cb;
551 data.user = (PVOID)(UINT_PTR)mi->disk_id;
553 if (!msi_cabextract(package, mi, &data))
555 ERR("Failed to extract cabinet: %s\n", debugstr_w(mi->cabinet));
556 rc = ERROR_INSTALL_FAILURE;
557 goto done;
561 if (!patch->IsApplied && !(patch->Attributes & msidbPatchAttributesNonVital))
563 ERR("Failed to apply patch to file: %s\n", debugstr_w(file->File));
564 rc = ERROR_INSTALL_FAILURE;
565 goto done;
569 done:
570 msi_free_media_info(mi);
571 if (mspatcha_loaded)
572 unload_mspatch();
573 return rc;
576 #define is_dot_dir(x) ((x[0] == '.') && ((x[1] == 0) || ((x[1] == '.') && (x[2] == 0))))
578 typedef struct
580 struct list entry;
581 LPWSTR sourcename;
582 LPWSTR destname;
583 LPWSTR source;
584 LPWSTR dest;
585 } FILE_LIST;
587 static BOOL msi_move_file(LPCWSTR source, LPCWSTR dest, int options)
589 BOOL ret;
591 if (GetFileAttributesW(source) == FILE_ATTRIBUTE_DIRECTORY ||
592 GetFileAttributesW(dest) == FILE_ATTRIBUTE_DIRECTORY)
594 WARN("Source or dest is directory, not moving\n");
595 return FALSE;
598 if (options == msidbMoveFileOptionsMove)
600 TRACE("moving %s -> %s\n", debugstr_w(source), debugstr_w(dest));
601 ret = MoveFileExW(source, dest, MOVEFILE_REPLACE_EXISTING);
602 if (!ret)
604 WARN("MoveFile failed: %d\n", GetLastError());
605 return FALSE;
608 else
610 TRACE("copying %s -> %s\n", debugstr_w(source), debugstr_w(dest));
611 ret = CopyFileW(source, dest, FALSE);
612 if (!ret)
614 WARN("CopyFile failed: %d\n", GetLastError());
615 return FALSE;
619 return TRUE;
622 static LPWSTR wildcard_to_file(LPWSTR wildcard, LPWSTR filename)
624 LPWSTR path, ptr;
625 DWORD dirlen, pathlen;
627 ptr = strrchrW(wildcard, '\\');
628 dirlen = ptr - wildcard + 1;
630 pathlen = dirlen + lstrlenW(filename) + 1;
631 path = msi_alloc(pathlen * sizeof(WCHAR));
633 lstrcpynW(path, wildcard, dirlen + 1);
634 lstrcatW(path, filename);
636 return path;
639 static void free_file_entry(FILE_LIST *file)
641 msi_free(file->source);
642 msi_free(file->dest);
643 msi_free(file);
646 static void free_list(FILE_LIST *list)
648 while (!list_empty(&list->entry))
650 FILE_LIST *file = LIST_ENTRY(list_head(&list->entry), FILE_LIST, entry);
652 list_remove(&file->entry);
653 free_file_entry(file);
657 static BOOL add_wildcard(FILE_LIST *files, LPWSTR source, LPWSTR dest)
659 FILE_LIST *new, *file;
660 LPWSTR ptr, filename;
661 DWORD size;
663 new = msi_alloc_zero(sizeof(FILE_LIST));
664 if (!new)
665 return FALSE;
667 new->source = strdupW(source);
668 ptr = strrchrW(dest, '\\') + 1;
669 filename = strrchrW(new->source, '\\') + 1;
671 new->sourcename = filename;
673 if (*ptr)
674 new->destname = ptr;
675 else
676 new->destname = new->sourcename;
678 size = (ptr - dest) + lstrlenW(filename) + 1;
679 new->dest = msi_alloc(size * sizeof(WCHAR));
680 if (!new->dest)
682 free_file_entry(new);
683 return FALSE;
686 lstrcpynW(new->dest, dest, ptr - dest + 1);
687 lstrcatW(new->dest, filename);
689 if (list_empty(&files->entry))
691 list_add_head(&files->entry, &new->entry);
692 return TRUE;
695 LIST_FOR_EACH_ENTRY(file, &files->entry, FILE_LIST, entry)
697 if (strcmpW( source, file->source ) < 0)
699 list_add_before(&file->entry, &new->entry);
700 return TRUE;
704 list_add_after(&file->entry, &new->entry);
705 return TRUE;
708 static BOOL move_files_wildcard(LPWSTR source, LPWSTR dest, int options)
710 WIN32_FIND_DATAW wfd;
711 HANDLE hfile;
712 LPWSTR path;
713 BOOL res;
714 FILE_LIST files, *file;
715 DWORD size;
717 hfile = FindFirstFileW(source, &wfd);
718 if (hfile == INVALID_HANDLE_VALUE) return FALSE;
720 list_init(&files.entry);
722 for (res = TRUE; res; res = FindNextFileW(hfile, &wfd))
724 if (is_dot_dir(wfd.cFileName)) continue;
726 path = wildcard_to_file(source, wfd.cFileName);
727 if (!path)
729 res = FALSE;
730 goto done;
733 add_wildcard(&files, path, dest);
734 msi_free(path);
737 /* no files match the wildcard */
738 if (list_empty(&files.entry))
739 goto done;
741 /* only the first wildcard match gets renamed to dest */
742 file = LIST_ENTRY(list_head(&files.entry), FILE_LIST, entry);
743 size = (strrchrW(file->dest, '\\') - file->dest) + lstrlenW(file->destname) + 2;
744 file->dest = msi_realloc(file->dest, size * sizeof(WCHAR));
745 if (!file->dest)
747 res = FALSE;
748 goto done;
751 /* file->dest may be shorter after the reallocation, so add a NULL
752 * terminator. This is needed for the call to strrchrW, as there will no
753 * longer be a NULL terminator within the bounds of the allocation in this case.
755 file->dest[size - 1] = '\0';
756 lstrcpyW(strrchrW(file->dest, '\\') + 1, file->destname);
758 while (!list_empty(&files.entry))
760 file = LIST_ENTRY(list_head(&files.entry), FILE_LIST, entry);
762 msi_move_file(file->source, file->dest, options);
764 list_remove(&file->entry);
765 free_file_entry(file);
768 res = TRUE;
770 done:
771 free_list(&files);
772 FindClose(hfile);
773 return res;
776 void msi_reduce_to_long_filename( WCHAR *filename )
778 WCHAR *p = strchrW( filename, '|' );
779 if (p) memmove( filename, p + 1, (strlenW( p + 1 ) + 1) * sizeof(WCHAR) );
782 static UINT ITERATE_MoveFiles( MSIRECORD *rec, LPVOID param )
784 MSIPACKAGE *package = param;
785 MSIRECORD *uirow;
786 MSICOMPONENT *comp;
787 LPCWSTR sourcename, component;
788 LPWSTR sourcedir, destname = NULL, destdir = NULL, source = NULL, dest = NULL;
789 int options;
790 DWORD size;
791 BOOL wildcards;
793 component = MSI_RecordGetString(rec, 2);
794 comp = msi_get_loaded_component(package, component);
795 if (!comp)
796 return ERROR_SUCCESS;
798 comp->Action = msi_get_component_action( package, comp );
799 if (comp->Action != INSTALLSTATE_LOCAL)
801 TRACE("component not scheduled for installation %s\n", debugstr_w(component));
802 return ERROR_SUCCESS;
805 sourcename = MSI_RecordGetString(rec, 3);
806 options = MSI_RecordGetInteger(rec, 7);
808 sourcedir = msi_dup_property(package->db, MSI_RecordGetString(rec, 5));
809 if (!sourcedir)
810 goto done;
812 destdir = msi_dup_property(package->db, MSI_RecordGetString(rec, 6));
813 if (!destdir)
814 goto done;
816 if (!sourcename)
818 if (GetFileAttributesW(sourcedir) == INVALID_FILE_ATTRIBUTES)
819 goto done;
821 source = strdupW(sourcedir);
822 if (!source)
823 goto done;
825 else
827 size = lstrlenW(sourcedir) + lstrlenW(sourcename) + 2;
828 source = msi_alloc(size * sizeof(WCHAR));
829 if (!source)
830 goto done;
832 lstrcpyW(source, sourcedir);
833 if (source[lstrlenW(source) - 1] != '\\')
834 lstrcatW(source, szBackSlash);
835 lstrcatW(source, sourcename);
838 wildcards = strchrW(source, '*') || strchrW(source, '?');
840 if (MSI_RecordIsNull(rec, 4))
842 if (!wildcards)
844 destname = strdupW(sourcename);
845 if (!destname)
846 goto done;
849 else
851 destname = strdupW(MSI_RecordGetString(rec, 4));
852 if (destname) msi_reduce_to_long_filename(destname);
855 size = 0;
856 if (destname)
857 size = lstrlenW(destname);
859 size += lstrlenW(destdir) + 2;
860 dest = msi_alloc(size * sizeof(WCHAR));
861 if (!dest)
862 goto done;
864 lstrcpyW(dest, destdir);
865 if (dest[lstrlenW(dest) - 1] != '\\')
866 lstrcatW(dest, szBackSlash);
868 if (destname)
869 lstrcatW(dest, destname);
871 if (GetFileAttributesW(destdir) == INVALID_FILE_ATTRIBUTES)
873 if (!msi_create_full_path(destdir))
875 WARN("failed to create directory %u\n", GetLastError());
876 goto done;
880 if (!wildcards)
881 msi_move_file(source, dest, options);
882 else
883 move_files_wildcard(source, dest, options);
885 done:
886 uirow = MSI_CreateRecord( 9 );
887 MSI_RecordSetStringW( uirow, 1, MSI_RecordGetString(rec, 1) );
888 MSI_RecordSetInteger( uirow, 6, 1 ); /* FIXME */
889 MSI_RecordSetStringW( uirow, 9, destdir );
890 msi_ui_actiondata( package, szMoveFiles, uirow );
891 msiobj_release( &uirow->hdr );
893 msi_free(sourcedir);
894 msi_free(destdir);
895 msi_free(destname);
896 msi_free(source);
897 msi_free(dest);
899 return ERROR_SUCCESS;
902 UINT ACTION_MoveFiles( MSIPACKAGE *package )
904 static const WCHAR query[] = {
905 'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
906 '`','M','o','v','e','F','i','l','e','`',0};
907 MSIQUERY *view;
908 UINT rc;
910 rc = MSI_DatabaseOpenViewW(package->db, query, &view);
911 if (rc != ERROR_SUCCESS)
912 return ERROR_SUCCESS;
914 rc = MSI_IterateRecords(view, NULL, ITERATE_MoveFiles, package);
915 msiobj_release(&view->hdr);
916 return rc;
919 static WCHAR *get_duplicate_filename( MSIPACKAGE *package, MSIRECORD *row, const WCHAR *file_key, const WCHAR *src )
921 DWORD len;
922 WCHAR *dst_name, *dst_path, *dst;
924 if (MSI_RecordIsNull( row, 4 ))
926 len = strlenW( src ) + 1;
927 if (!(dst_name = msi_alloc( len * sizeof(WCHAR)))) return NULL;
928 strcpyW( dst_name, strrchrW( src, '\\' ) + 1 );
930 else
932 MSI_RecordGetStringW( row, 4, NULL, &len );
933 if (!(dst_name = msi_alloc( ++len * sizeof(WCHAR) ))) return NULL;
934 MSI_RecordGetStringW( row, 4, dst_name, &len );
935 msi_reduce_to_long_filename( dst_name );
938 if (MSI_RecordIsNull( row, 5 ))
940 WCHAR *p;
941 dst_path = strdupW( src );
942 p = strrchrW( dst_path, '\\' );
943 if (p) *p = 0;
945 else
947 const WCHAR *dst_key = MSI_RecordGetString( row, 5 );
949 dst_path = strdupW( msi_get_target_folder( package, dst_key ) );
950 if (!dst_path)
952 /* try a property */
953 dst_path = msi_dup_property( package->db, dst_key );
954 if (!dst_path)
956 FIXME("Unable to get destination folder, try AppSearch properties\n");
957 msi_free( dst_name );
958 return NULL;
963 dst = msi_build_directory_name( 2, dst_path, dst_name );
964 msi_create_full_path( dst_path );
966 msi_free( dst_name );
967 msi_free( dst_path );
968 return dst;
971 static UINT ITERATE_DuplicateFiles(MSIRECORD *row, LPVOID param)
973 MSIPACKAGE *package = param;
974 LPWSTR dest;
975 LPCWSTR file_key, component;
976 MSICOMPONENT *comp;
977 MSIRECORD *uirow;
978 MSIFILE *file;
980 component = MSI_RecordGetString(row,2);
981 comp = msi_get_loaded_component(package, component);
982 if (!comp)
983 return ERROR_SUCCESS;
985 comp->Action = msi_get_component_action( package, comp );
986 if (comp->Action != INSTALLSTATE_LOCAL)
988 TRACE("component not scheduled for installation %s\n", debugstr_w(component));
989 return ERROR_SUCCESS;
992 file_key = MSI_RecordGetString(row,3);
993 if (!file_key)
995 ERR("Unable to get file key\n");
996 return ERROR_FUNCTION_FAILED;
999 file = msi_get_loaded_file( package, file_key );
1000 if (!file)
1002 ERR("Original file unknown %s\n", debugstr_w(file_key));
1003 return ERROR_SUCCESS;
1006 dest = get_duplicate_filename( package, row, file_key, file->TargetPath );
1007 if (!dest)
1009 WARN("Unable to get duplicate filename\n");
1010 return ERROR_SUCCESS;
1013 TRACE("Duplicating file %s to %s\n", debugstr_w(file->TargetPath), debugstr_w(dest));
1015 if (!CopyFileW( file->TargetPath, dest, TRUE ))
1017 WARN("Failed to copy file %s -> %s (%u)\n",
1018 debugstr_w(file->TargetPath), debugstr_w(dest), GetLastError());
1021 FIXME("We should track these duplicate files as well\n");
1023 uirow = MSI_CreateRecord( 9 );
1024 MSI_RecordSetStringW( uirow, 1, MSI_RecordGetString( row, 1 ) );
1025 MSI_RecordSetInteger( uirow, 6, file->FileSize );
1026 MSI_RecordSetStringW( uirow, 9, MSI_RecordGetString( row, 5 ) );
1027 msi_ui_actiondata( package, szDuplicateFiles, uirow );
1028 msiobj_release( &uirow->hdr );
1030 msi_free(dest);
1031 return ERROR_SUCCESS;
1034 UINT ACTION_DuplicateFiles(MSIPACKAGE *package)
1036 static const WCHAR query[] = {
1037 'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
1038 '`','D','u','p','l','i','c','a','t','e','F','i','l','e','`',0};
1039 MSIQUERY *view;
1040 UINT rc;
1042 rc = MSI_DatabaseOpenViewW(package->db, query, &view);
1043 if (rc != ERROR_SUCCESS)
1044 return ERROR_SUCCESS;
1046 rc = MSI_IterateRecords(view, NULL, ITERATE_DuplicateFiles, package);
1047 msiobj_release(&view->hdr);
1048 return rc;
1051 static UINT ITERATE_RemoveDuplicateFiles( MSIRECORD *row, LPVOID param )
1053 MSIPACKAGE *package = param;
1054 LPWSTR dest;
1055 LPCWSTR file_key, component;
1056 MSICOMPONENT *comp;
1057 MSIRECORD *uirow;
1058 MSIFILE *file;
1060 component = MSI_RecordGetString( row, 2 );
1061 comp = msi_get_loaded_component( package, component );
1062 if (!comp)
1063 return ERROR_SUCCESS;
1065 comp->Action = msi_get_component_action( package, comp );
1066 if (comp->Action != INSTALLSTATE_ABSENT)
1068 TRACE("component not scheduled for removal %s\n", debugstr_w(component));
1069 return ERROR_SUCCESS;
1072 file_key = MSI_RecordGetString( row, 3 );
1073 if (!file_key)
1075 ERR("Unable to get file key\n");
1076 return ERROR_FUNCTION_FAILED;
1079 file = msi_get_loaded_file( package, file_key );
1080 if (!file)
1082 ERR("Original file unknown %s\n", debugstr_w(file_key));
1083 return ERROR_SUCCESS;
1086 dest = get_duplicate_filename( package, row, file_key, file->TargetPath );
1087 if (!dest)
1089 WARN("Unable to get duplicate filename\n");
1090 return ERROR_SUCCESS;
1093 TRACE("Removing duplicate %s of %s\n", debugstr_w(dest), debugstr_w(file->TargetPath));
1095 if (!DeleteFileW( dest ))
1097 WARN("Failed to delete duplicate file %s (%u)\n", debugstr_w(dest), GetLastError());
1100 uirow = MSI_CreateRecord( 9 );
1101 MSI_RecordSetStringW( uirow, 1, MSI_RecordGetString( row, 1 ) );
1102 MSI_RecordSetStringW( uirow, 9, MSI_RecordGetString( row, 5 ) );
1103 msi_ui_actiondata( package, szRemoveDuplicateFiles, uirow );
1104 msiobj_release( &uirow->hdr );
1106 msi_free(dest);
1107 return ERROR_SUCCESS;
1110 UINT ACTION_RemoveDuplicateFiles( MSIPACKAGE *package )
1112 static const WCHAR query[] = {
1113 'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
1114 '`','D','u','p','l','i','c','a','t','e','F','i','l','e','`',0};
1115 MSIQUERY *view;
1116 UINT rc;
1118 rc = MSI_DatabaseOpenViewW( package->db, query, &view );
1119 if (rc != ERROR_SUCCESS)
1120 return ERROR_SUCCESS;
1122 rc = MSI_IterateRecords( view, NULL, ITERATE_RemoveDuplicateFiles, package );
1123 msiobj_release( &view->hdr );
1124 return rc;
1127 static BOOL verify_comp_for_removal(MSICOMPONENT *comp, UINT install_mode)
1129 /* special case */
1130 if (comp->Action != INSTALLSTATE_SOURCE &&
1131 comp->Attributes & msidbComponentAttributesSourceOnly &&
1132 (install_mode == msidbRemoveFileInstallModeOnRemove ||
1133 install_mode == msidbRemoveFileInstallModeOnBoth)) return TRUE;
1135 switch (comp->Action)
1137 case INSTALLSTATE_LOCAL:
1138 case INSTALLSTATE_SOURCE:
1139 if (install_mode == msidbRemoveFileInstallModeOnInstall ||
1140 install_mode == msidbRemoveFileInstallModeOnBoth) return TRUE;
1141 break;
1142 case INSTALLSTATE_ABSENT:
1143 if (install_mode == msidbRemoveFileInstallModeOnRemove ||
1144 install_mode == msidbRemoveFileInstallModeOnBoth) return TRUE;
1145 break;
1146 default: break;
1148 return FALSE;
1151 static UINT ITERATE_RemoveFiles(MSIRECORD *row, LPVOID param)
1153 MSIPACKAGE *package = param;
1154 MSICOMPONENT *comp;
1155 MSIRECORD *uirow;
1156 LPCWSTR component, dirprop;
1157 UINT install_mode;
1158 LPWSTR dir = NULL, path = NULL, filename = NULL;
1159 DWORD size;
1160 UINT ret = ERROR_SUCCESS;
1162 component = MSI_RecordGetString(row, 2);
1163 dirprop = MSI_RecordGetString(row, 4);
1164 install_mode = MSI_RecordGetInteger(row, 5);
1166 comp = msi_get_loaded_component(package, component);
1167 if (!comp)
1168 return ERROR_SUCCESS;
1170 comp->Action = msi_get_component_action( package, comp );
1171 if (!verify_comp_for_removal(comp, install_mode))
1173 TRACE("Skipping removal due to install mode\n");
1174 return ERROR_SUCCESS;
1176 if (comp->assembly && !comp->assembly->application)
1178 return ERROR_SUCCESS;
1180 if (comp->Attributes & msidbComponentAttributesPermanent)
1182 TRACE("permanent component, not removing file\n");
1183 return ERROR_SUCCESS;
1186 dir = msi_dup_property(package->db, dirprop);
1187 if (!dir)
1189 WARN("directory property has no value\n");
1190 return ERROR_SUCCESS;
1192 size = 0;
1193 if ((filename = strdupW( MSI_RecordGetString(row, 3) )))
1195 msi_reduce_to_long_filename( filename );
1196 size = lstrlenW( filename );
1198 size += lstrlenW(dir) + 2;
1199 path = msi_alloc(size * sizeof(WCHAR));
1200 if (!path)
1202 ret = ERROR_OUTOFMEMORY;
1203 goto done;
1206 if (filename)
1208 lstrcpyW(path, dir);
1209 PathAddBackslashW(path);
1210 lstrcatW(path, filename);
1212 TRACE("Deleting misc file: %s\n", debugstr_w(path));
1213 DeleteFileW(path);
1215 else
1217 TRACE("Removing misc directory: %s\n", debugstr_w(dir));
1218 RemoveDirectoryW(dir);
1221 done:
1222 uirow = MSI_CreateRecord( 9 );
1223 MSI_RecordSetStringW( uirow, 1, MSI_RecordGetString(row, 1) );
1224 MSI_RecordSetStringW( uirow, 9, dir );
1225 msi_ui_actiondata( package, szRemoveFiles, uirow );
1226 msiobj_release( &uirow->hdr );
1228 msi_free(filename);
1229 msi_free(path);
1230 msi_free(dir);
1231 return ret;
1234 static void remove_folder( MSIFOLDER *folder )
1236 FolderList *fl;
1238 LIST_FOR_EACH_ENTRY( fl, &folder->children, FolderList, entry )
1240 remove_folder( fl->folder );
1242 if (!folder->persistent && folder->State != FOLDER_STATE_REMOVED)
1244 if (RemoveDirectoryW( folder->ResolvedTarget )) folder->State = FOLDER_STATE_REMOVED;
1248 UINT ACTION_RemoveFiles( MSIPACKAGE *package )
1250 static const WCHAR query[] = {
1251 'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
1252 '`','R','e','m','o','v','e','F','i','l','e','`',0};
1253 MSIQUERY *view;
1254 MSICOMPONENT *comp;
1255 MSIFILE *file;
1256 UINT r;
1258 r = MSI_DatabaseOpenViewW(package->db, query, &view);
1259 if (r == ERROR_SUCCESS)
1261 r = MSI_IterateRecords(view, NULL, ITERATE_RemoveFiles, package);
1262 msiobj_release(&view->hdr);
1263 if (r != ERROR_SUCCESS)
1264 return r;
1267 LIST_FOR_EACH_ENTRY( file, &package->files, MSIFILE, entry )
1269 MSIRECORD *uirow;
1270 VS_FIXEDFILEINFO *ver;
1272 comp = file->Component;
1273 msi_file_update_ui( package, file, szRemoveFiles );
1275 comp->Action = msi_get_component_action( package, comp );
1276 if (comp->Action != INSTALLSTATE_ABSENT || comp->Installed == INSTALLSTATE_SOURCE)
1277 continue;
1279 if (comp->assembly && !comp->assembly->application)
1280 continue;
1282 if (comp->Attributes & msidbComponentAttributesPermanent)
1284 TRACE("permanent component, not removing file\n");
1285 continue;
1288 if (file->Version)
1290 ver = msi_get_disk_file_version( file->TargetPath );
1291 if (ver && msi_compare_file_versions( ver, file->Version ) > 0)
1293 TRACE("newer version detected, not removing file\n");
1294 msi_free( ver );
1295 continue;
1297 msi_free( ver );
1300 if (file->state == msifs_installed)
1301 WARN("removing installed file %s\n", debugstr_w(file->TargetPath));
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 file->state = msifs_missing;
1312 uirow = MSI_CreateRecord( 9 );
1313 MSI_RecordSetStringW( uirow, 1, file->FileName );
1314 MSI_RecordSetStringW( uirow, 9, comp->Directory );
1315 msi_ui_actiondata( package, szRemoveFiles, uirow );
1316 msiobj_release( &uirow->hdr );
1319 LIST_FOR_EACH_ENTRY( comp, &package->components, MSICOMPONENT, entry )
1321 comp->Action = msi_get_component_action( package, comp );
1322 if (comp->Action != INSTALLSTATE_ABSENT) continue;
1324 if (comp->Attributes & msidbComponentAttributesPermanent)
1326 TRACE("permanent component, not removing directory\n");
1327 continue;
1329 if (comp->assembly && !comp->assembly->application)
1330 msi_uninstall_assembly( package, comp );
1331 else
1333 MSIFOLDER *folder = msi_get_loaded_folder( package, comp->Directory );
1334 remove_folder( folder );
1337 return ERROR_SUCCESS;