tools: Upgrade the config.guess/config.sub scripts.
[wine/wine-gecko.git] / dlls / msi / files.c
blob7202fb637cd2b1dad572b82afe90e763d2c708f0
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 BOOL patchfiles_cb(MSIPACKAGE *package, LPCWSTR file, DWORD action,
447 LPWSTR *path, DWORD *attrs, PVOID user)
449 static MSIFILEPATCH *p = NULL;
450 static WCHAR patch_path[MAX_PATH] = {0};
451 static WCHAR temp_folder[MAX_PATH] = {0};
453 if (action == MSICABEXTRACT_BEGINEXTRACT)
455 if (temp_folder[0] == '\0')
456 GetTempPathW(MAX_PATH, temp_folder);
458 p = msi_get_loaded_filepatch(package, file);
459 if (!p)
461 TRACE("unknown file in cabinet (%s)\n", debugstr_w(file));
462 return FALSE;
464 GetTempFileNameW(temp_folder, NULL, 0, patch_path);
466 *path = strdupW(patch_path);
467 *attrs = p->File->Attributes;
469 else if (action == MSICABEXTRACT_FILEEXTRACTED)
471 WCHAR patched_file[MAX_PATH];
472 BOOL br;
474 GetTempFileNameW(temp_folder, NULL, 0, patched_file);
476 br = ApplyPatchToFileW(patch_path, p->File->TargetPath, patched_file, 0);
477 if (br)
479 /* FIXME: baseline cache */
481 DeleteFileW( p->File->TargetPath );
482 MoveFileW( patched_file, p->File->TargetPath );
484 p->IsApplied = TRUE;
486 else
487 ERR("Failed patch %s: %d.\n", debugstr_w(p->File->TargetPath), GetLastError());
489 DeleteFileW(patch_path);
490 p = NULL;
493 return TRUE;
496 UINT ACTION_PatchFiles( MSIPACKAGE *package )
498 MSIFILEPATCH *patch;
499 MSIMEDIAINFO *mi;
500 UINT rc = ERROR_SUCCESS;
501 BOOL mspatcha_loaded = FALSE;
503 TRACE("%p\n", package);
505 mi = msi_alloc_zero( sizeof(MSIMEDIAINFO) );
507 LIST_FOR_EACH_ENTRY( patch, &package->filepatches, MSIFILEPATCH, entry )
509 MSIFILE *file = patch->File;
510 MSICOMPONENT *comp = file->Component;
512 rc = msi_load_media_info( package, patch->Sequence, mi );
513 if (rc != ERROR_SUCCESS)
515 ERR("Unable to load media info for %s (%u)\n", debugstr_w(file->File), rc);
516 rc = ERROR_FUNCTION_FAILED;
517 goto done;
519 comp->Action = msi_get_component_action( package, comp );
520 if (!comp->Enabled || comp->Action != INSTALLSTATE_LOCAL) continue;
522 if (!patch->IsApplied)
524 MSICABDATA data;
526 rc = ready_media( package, TRUE, mi );
527 if (rc != ERROR_SUCCESS)
529 ERR("Failed to ready media for %s\n", debugstr_w(file->File));
530 goto done;
533 if (!mspatcha_loaded && !load_mspatcha())
535 rc = ERROR_FUNCTION_FAILED;
536 goto done;
538 mspatcha_loaded = TRUE;
540 data.mi = mi;
541 data.package = package;
542 data.cb = patchfiles_cb;
543 data.user = (PVOID)(UINT_PTR)mi->disk_id;
545 if (!msi_cabextract(package, mi, &data))
547 ERR("Failed to extract cabinet: %s\n", debugstr_w(mi->cabinet));
548 rc = ERROR_INSTALL_FAILURE;
549 goto done;
553 if (!patch->IsApplied && !(patch->Attributes & msidbPatchAttributesNonVital))
555 ERR("Failed to apply patch to file: %s\n", debugstr_w(file->File));
556 rc = ERROR_INSTALL_FAILURE;
557 goto done;
561 done:
562 msi_free_media_info(mi);
563 if (mspatcha_loaded)
564 unload_mspatch();
565 return rc;
568 #define is_dot_dir(x) ((x[0] == '.') && ((x[1] == 0) || ((x[1] == '.') && (x[2] == 0))))
570 typedef struct
572 struct list entry;
573 LPWSTR sourcename;
574 LPWSTR destname;
575 LPWSTR source;
576 LPWSTR dest;
577 } FILE_LIST;
579 static BOOL msi_move_file(LPCWSTR source, LPCWSTR dest, int options)
581 BOOL ret;
583 if (GetFileAttributesW(source) == FILE_ATTRIBUTE_DIRECTORY ||
584 GetFileAttributesW(dest) == FILE_ATTRIBUTE_DIRECTORY)
586 WARN("Source or dest is directory, not moving\n");
587 return FALSE;
590 if (options == msidbMoveFileOptionsMove)
592 TRACE("moving %s -> %s\n", debugstr_w(source), debugstr_w(dest));
593 ret = MoveFileExW(source, dest, MOVEFILE_REPLACE_EXISTING);
594 if (!ret)
596 WARN("MoveFile failed: %d\n", GetLastError());
597 return FALSE;
600 else
602 TRACE("copying %s -> %s\n", debugstr_w(source), debugstr_w(dest));
603 ret = CopyFileW(source, dest, FALSE);
604 if (!ret)
606 WARN("CopyFile failed: %d\n", GetLastError());
607 return FALSE;
611 return TRUE;
614 static LPWSTR wildcard_to_file(LPWSTR wildcard, LPWSTR filename)
616 LPWSTR path, ptr;
617 DWORD dirlen, pathlen;
619 ptr = strrchrW(wildcard, '\\');
620 dirlen = ptr - wildcard + 1;
622 pathlen = dirlen + lstrlenW(filename) + 1;
623 path = msi_alloc(pathlen * sizeof(WCHAR));
625 lstrcpynW(path, wildcard, dirlen + 1);
626 lstrcatW(path, filename);
628 return path;
631 static void free_file_entry(FILE_LIST *file)
633 msi_free(file->source);
634 msi_free(file->dest);
635 msi_free(file);
638 static void free_list(FILE_LIST *list)
640 while (!list_empty(&list->entry))
642 FILE_LIST *file = LIST_ENTRY(list_head(&list->entry), FILE_LIST, entry);
644 list_remove(&file->entry);
645 free_file_entry(file);
649 static BOOL add_wildcard(FILE_LIST *files, LPWSTR source, LPWSTR dest)
651 FILE_LIST *new, *file;
652 LPWSTR ptr, filename;
653 DWORD size;
655 new = msi_alloc_zero(sizeof(FILE_LIST));
656 if (!new)
657 return FALSE;
659 new->source = strdupW(source);
660 ptr = strrchrW(dest, '\\') + 1;
661 filename = strrchrW(new->source, '\\') + 1;
663 new->sourcename = filename;
665 if (*ptr)
666 new->destname = ptr;
667 else
668 new->destname = new->sourcename;
670 size = (ptr - dest) + lstrlenW(filename) + 1;
671 new->dest = msi_alloc(size * sizeof(WCHAR));
672 if (!new->dest)
674 free_file_entry(new);
675 return FALSE;
678 lstrcpynW(new->dest, dest, ptr - dest + 1);
679 lstrcatW(new->dest, filename);
681 if (list_empty(&files->entry))
683 list_add_head(&files->entry, &new->entry);
684 return TRUE;
687 LIST_FOR_EACH_ENTRY(file, &files->entry, FILE_LIST, entry)
689 if (strcmpW( source, file->source ) < 0)
691 list_add_before(&file->entry, &new->entry);
692 return TRUE;
696 list_add_after(&file->entry, &new->entry);
697 return TRUE;
700 static BOOL move_files_wildcard(LPWSTR source, LPWSTR dest, int options)
702 WIN32_FIND_DATAW wfd;
703 HANDLE hfile;
704 LPWSTR path;
705 BOOL res;
706 FILE_LIST files, *file;
707 DWORD size;
709 hfile = FindFirstFileW(source, &wfd);
710 if (hfile == INVALID_HANDLE_VALUE) return FALSE;
712 list_init(&files.entry);
714 for (res = TRUE; res; res = FindNextFileW(hfile, &wfd))
716 if (is_dot_dir(wfd.cFileName)) continue;
718 path = wildcard_to_file(source, wfd.cFileName);
719 if (!path)
721 res = FALSE;
722 goto done;
725 add_wildcard(&files, path, dest);
726 msi_free(path);
729 /* no files match the wildcard */
730 if (list_empty(&files.entry))
731 goto done;
733 /* only the first wildcard match gets renamed to dest */
734 file = LIST_ENTRY(list_head(&files.entry), FILE_LIST, entry);
735 size = (strrchrW(file->dest, '\\') - file->dest) + lstrlenW(file->destname) + 2;
736 file->dest = msi_realloc(file->dest, size * sizeof(WCHAR));
737 if (!file->dest)
739 res = FALSE;
740 goto done;
743 /* file->dest may be shorter after the reallocation, so add a NULL
744 * terminator. This is needed for the call to strrchrW, as there will no
745 * longer be a NULL terminator within the bounds of the allocation in this case.
747 file->dest[size - 1] = '\0';
748 lstrcpyW(strrchrW(file->dest, '\\') + 1, file->destname);
750 while (!list_empty(&files.entry))
752 file = LIST_ENTRY(list_head(&files.entry), FILE_LIST, entry);
754 msi_move_file(file->source, file->dest, options);
756 list_remove(&file->entry);
757 free_file_entry(file);
760 res = TRUE;
762 done:
763 free_list(&files);
764 FindClose(hfile);
765 return res;
768 void msi_reduce_to_long_filename( WCHAR *filename )
770 WCHAR *p = strchrW( filename, '|' );
771 if (p) memmove( filename, p + 1, (strlenW( p + 1 ) + 1) * sizeof(WCHAR) );
774 static UINT ITERATE_MoveFiles( MSIRECORD *rec, LPVOID param )
776 MSIPACKAGE *package = param;
777 MSIRECORD *uirow;
778 MSICOMPONENT *comp;
779 LPCWSTR sourcename, component;
780 LPWSTR sourcedir, destname = NULL, destdir = NULL, source = NULL, dest = NULL;
781 int options;
782 DWORD size;
783 BOOL wildcards;
785 component = MSI_RecordGetString(rec, 2);
786 comp = msi_get_loaded_component(package, component);
787 if (!comp)
788 return ERROR_SUCCESS;
790 comp->Action = msi_get_component_action( package, comp );
791 if (comp->Action != INSTALLSTATE_LOCAL)
793 TRACE("component not scheduled for installation %s\n", debugstr_w(component));
794 return ERROR_SUCCESS;
797 sourcename = MSI_RecordGetString(rec, 3);
798 options = MSI_RecordGetInteger(rec, 7);
800 sourcedir = msi_dup_property(package->db, MSI_RecordGetString(rec, 5));
801 if (!sourcedir)
802 goto done;
804 destdir = msi_dup_property(package->db, MSI_RecordGetString(rec, 6));
805 if (!destdir)
806 goto done;
808 if (!sourcename)
810 if (GetFileAttributesW(sourcedir) == INVALID_FILE_ATTRIBUTES)
811 goto done;
813 source = strdupW(sourcedir);
814 if (!source)
815 goto done;
817 else
819 size = lstrlenW(sourcedir) + lstrlenW(sourcename) + 2;
820 source = msi_alloc(size * sizeof(WCHAR));
821 if (!source)
822 goto done;
824 lstrcpyW(source, sourcedir);
825 if (source[lstrlenW(source) - 1] != '\\')
826 lstrcatW(source, szBackSlash);
827 lstrcatW(source, sourcename);
830 wildcards = strchrW(source, '*') || strchrW(source, '?');
832 if (MSI_RecordIsNull(rec, 4))
834 if (!wildcards)
836 destname = strdupW(sourcename);
837 if (!destname)
838 goto done;
841 else
843 destname = strdupW(MSI_RecordGetString(rec, 4));
844 if (destname) msi_reduce_to_long_filename(destname);
847 size = 0;
848 if (destname)
849 size = lstrlenW(destname);
851 size += lstrlenW(destdir) + 2;
852 dest = msi_alloc(size * sizeof(WCHAR));
853 if (!dest)
854 goto done;
856 lstrcpyW(dest, destdir);
857 if (dest[lstrlenW(dest) - 1] != '\\')
858 lstrcatW(dest, szBackSlash);
860 if (destname)
861 lstrcatW(dest, destname);
863 if (GetFileAttributesW(destdir) == INVALID_FILE_ATTRIBUTES)
865 if (!msi_create_full_path(destdir))
867 WARN("failed to create directory %u\n", GetLastError());
868 goto done;
872 if (!wildcards)
873 msi_move_file(source, dest, options);
874 else
875 move_files_wildcard(source, dest, options);
877 done:
878 uirow = MSI_CreateRecord( 9 );
879 MSI_RecordSetStringW( uirow, 1, MSI_RecordGetString(rec, 1) );
880 MSI_RecordSetInteger( uirow, 6, 1 ); /* FIXME */
881 MSI_RecordSetStringW( uirow, 9, destdir );
882 msi_ui_actiondata( package, szMoveFiles, uirow );
883 msiobj_release( &uirow->hdr );
885 msi_free(sourcedir);
886 msi_free(destdir);
887 msi_free(destname);
888 msi_free(source);
889 msi_free(dest);
891 return ERROR_SUCCESS;
894 UINT ACTION_MoveFiles( MSIPACKAGE *package )
896 static const WCHAR query[] = {
897 'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
898 '`','M','o','v','e','F','i','l','e','`',0};
899 MSIQUERY *view;
900 UINT rc;
902 rc = MSI_DatabaseOpenViewW(package->db, query, &view);
903 if (rc != ERROR_SUCCESS)
904 return ERROR_SUCCESS;
906 rc = MSI_IterateRecords(view, NULL, ITERATE_MoveFiles, package);
907 msiobj_release(&view->hdr);
908 return rc;
911 static WCHAR *get_duplicate_filename( MSIPACKAGE *package, MSIRECORD *row, const WCHAR *file_key, const WCHAR *src )
913 DWORD len;
914 WCHAR *dst_name, *dst_path, *dst;
916 if (MSI_RecordIsNull( row, 4 ))
918 len = strlenW( src ) + 1;
919 if (!(dst_name = msi_alloc( len * sizeof(WCHAR)))) return NULL;
920 strcpyW( dst_name, strrchrW( src, '\\' ) + 1 );
922 else
924 MSI_RecordGetStringW( row, 4, NULL, &len );
925 if (!(dst_name = msi_alloc( ++len * sizeof(WCHAR) ))) return NULL;
926 MSI_RecordGetStringW( row, 4, dst_name, &len );
927 msi_reduce_to_long_filename( dst_name );
930 if (MSI_RecordIsNull( row, 5 ))
932 WCHAR *p;
933 dst_path = strdupW( src );
934 p = strrchrW( dst_path, '\\' );
935 if (p) *p = 0;
937 else
939 const WCHAR *dst_key = MSI_RecordGetString( row, 5 );
941 dst_path = strdupW( msi_get_target_folder( package, dst_key ) );
942 if (!dst_path)
944 /* try a property */
945 dst_path = msi_dup_property( package->db, dst_key );
946 if (!dst_path)
948 FIXME("Unable to get destination folder, try AppSearch properties\n");
949 msi_free( dst_name );
950 return NULL;
955 dst = msi_build_directory_name( 2, dst_path, dst_name );
956 msi_create_full_path( dst_path );
958 msi_free( dst_name );
959 msi_free( dst_path );
960 return dst;
963 static UINT ITERATE_DuplicateFiles(MSIRECORD *row, LPVOID param)
965 MSIPACKAGE *package = param;
966 LPWSTR dest;
967 LPCWSTR file_key, component;
968 MSICOMPONENT *comp;
969 MSIRECORD *uirow;
970 MSIFILE *file;
972 component = MSI_RecordGetString(row,2);
973 comp = msi_get_loaded_component(package, component);
974 if (!comp)
975 return ERROR_SUCCESS;
977 comp->Action = msi_get_component_action( package, comp );
978 if (comp->Action != INSTALLSTATE_LOCAL)
980 TRACE("component not scheduled for installation %s\n", debugstr_w(component));
981 return ERROR_SUCCESS;
984 file_key = MSI_RecordGetString(row,3);
985 if (!file_key)
987 ERR("Unable to get file key\n");
988 return ERROR_FUNCTION_FAILED;
991 file = msi_get_loaded_file( package, file_key );
992 if (!file)
994 ERR("Original file unknown %s\n", debugstr_w(file_key));
995 return ERROR_SUCCESS;
998 dest = get_duplicate_filename( package, row, file_key, file->TargetPath );
999 if (!dest)
1001 WARN("Unable to get duplicate filename\n");
1002 return ERROR_SUCCESS;
1005 TRACE("Duplicating file %s to %s\n", debugstr_w(file->TargetPath), debugstr_w(dest));
1007 if (!CopyFileW( file->TargetPath, dest, TRUE ))
1009 WARN("Failed to copy file %s -> %s (%u)\n",
1010 debugstr_w(file->TargetPath), debugstr_w(dest), GetLastError());
1013 FIXME("We should track these duplicate files as well\n");
1015 uirow = MSI_CreateRecord( 9 );
1016 MSI_RecordSetStringW( uirow, 1, MSI_RecordGetString( row, 1 ) );
1017 MSI_RecordSetInteger( uirow, 6, file->FileSize );
1018 MSI_RecordSetStringW( uirow, 9, MSI_RecordGetString( row, 5 ) );
1019 msi_ui_actiondata( package, szDuplicateFiles, uirow );
1020 msiobj_release( &uirow->hdr );
1022 msi_free(dest);
1023 return ERROR_SUCCESS;
1026 UINT ACTION_DuplicateFiles(MSIPACKAGE *package)
1028 static const WCHAR query[] = {
1029 'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
1030 '`','D','u','p','l','i','c','a','t','e','F','i','l','e','`',0};
1031 MSIQUERY *view;
1032 UINT rc;
1034 rc = MSI_DatabaseOpenViewW(package->db, query, &view);
1035 if (rc != ERROR_SUCCESS)
1036 return ERROR_SUCCESS;
1038 rc = MSI_IterateRecords(view, NULL, ITERATE_DuplicateFiles, package);
1039 msiobj_release(&view->hdr);
1040 return rc;
1043 static UINT ITERATE_RemoveDuplicateFiles( MSIRECORD *row, LPVOID param )
1045 MSIPACKAGE *package = param;
1046 LPWSTR dest;
1047 LPCWSTR file_key, component;
1048 MSICOMPONENT *comp;
1049 MSIRECORD *uirow;
1050 MSIFILE *file;
1052 component = MSI_RecordGetString( row, 2 );
1053 comp = msi_get_loaded_component( package, component );
1054 if (!comp)
1055 return ERROR_SUCCESS;
1057 comp->Action = msi_get_component_action( package, comp );
1058 if (comp->Action != INSTALLSTATE_ABSENT)
1060 TRACE("component not scheduled for removal %s\n", debugstr_w(component));
1061 return ERROR_SUCCESS;
1064 file_key = MSI_RecordGetString( row, 3 );
1065 if (!file_key)
1067 ERR("Unable to get file key\n");
1068 return ERROR_FUNCTION_FAILED;
1071 file = msi_get_loaded_file( package, file_key );
1072 if (!file)
1074 ERR("Original file unknown %s\n", debugstr_w(file_key));
1075 return ERROR_SUCCESS;
1078 dest = get_duplicate_filename( package, row, file_key, file->TargetPath );
1079 if (!dest)
1081 WARN("Unable to get duplicate filename\n");
1082 return ERROR_SUCCESS;
1085 TRACE("Removing duplicate %s of %s\n", debugstr_w(dest), debugstr_w(file->TargetPath));
1087 if (!DeleteFileW( dest ))
1089 WARN("Failed to delete duplicate file %s (%u)\n", debugstr_w(dest), GetLastError());
1092 uirow = MSI_CreateRecord( 9 );
1093 MSI_RecordSetStringW( uirow, 1, MSI_RecordGetString( row, 1 ) );
1094 MSI_RecordSetStringW( uirow, 9, MSI_RecordGetString( row, 5 ) );
1095 msi_ui_actiondata( package, szRemoveDuplicateFiles, uirow );
1096 msiobj_release( &uirow->hdr );
1098 msi_free(dest);
1099 return ERROR_SUCCESS;
1102 UINT ACTION_RemoveDuplicateFiles( MSIPACKAGE *package )
1104 static const WCHAR query[] = {
1105 'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
1106 '`','D','u','p','l','i','c','a','t','e','F','i','l','e','`',0};
1107 MSIQUERY *view;
1108 UINT rc;
1110 rc = MSI_DatabaseOpenViewW( package->db, query, &view );
1111 if (rc != ERROR_SUCCESS)
1112 return ERROR_SUCCESS;
1114 rc = MSI_IterateRecords( view, NULL, ITERATE_RemoveDuplicateFiles, package );
1115 msiobj_release( &view->hdr );
1116 return rc;
1119 static BOOL verify_comp_for_removal(MSICOMPONENT *comp, UINT install_mode)
1121 /* special case */
1122 if (comp->Action != INSTALLSTATE_SOURCE &&
1123 comp->Attributes & msidbComponentAttributesSourceOnly &&
1124 (install_mode == msidbRemoveFileInstallModeOnRemove ||
1125 install_mode == msidbRemoveFileInstallModeOnBoth)) return TRUE;
1127 switch (comp->Action)
1129 case INSTALLSTATE_LOCAL:
1130 case INSTALLSTATE_SOURCE:
1131 if (install_mode == msidbRemoveFileInstallModeOnInstall ||
1132 install_mode == msidbRemoveFileInstallModeOnBoth) return TRUE;
1133 break;
1134 case INSTALLSTATE_ABSENT:
1135 if (install_mode == msidbRemoveFileInstallModeOnRemove ||
1136 install_mode == msidbRemoveFileInstallModeOnBoth) return TRUE;
1137 break;
1138 default: break;
1140 return FALSE;
1143 static UINT ITERATE_RemoveFiles(MSIRECORD *row, LPVOID param)
1145 MSIPACKAGE *package = param;
1146 MSICOMPONENT *comp;
1147 MSIRECORD *uirow;
1148 LPCWSTR component, dirprop;
1149 UINT install_mode;
1150 LPWSTR dir = NULL, path = NULL, filename = NULL;
1151 DWORD size;
1152 UINT ret = ERROR_SUCCESS;
1154 component = MSI_RecordGetString(row, 2);
1155 dirprop = MSI_RecordGetString(row, 4);
1156 install_mode = MSI_RecordGetInteger(row, 5);
1158 comp = msi_get_loaded_component(package, component);
1159 if (!comp)
1160 return ERROR_SUCCESS;
1162 comp->Action = msi_get_component_action( package, comp );
1163 if (!verify_comp_for_removal(comp, install_mode))
1165 TRACE("Skipping removal due to install mode\n");
1166 return ERROR_SUCCESS;
1168 if (comp->assembly && !comp->assembly->application)
1170 return ERROR_SUCCESS;
1172 if (comp->Attributes & msidbComponentAttributesPermanent)
1174 TRACE("permanent component, not removing file\n");
1175 return ERROR_SUCCESS;
1178 dir = msi_dup_property(package->db, dirprop);
1179 if (!dir)
1181 WARN("directory property has no value\n");
1182 return ERROR_SUCCESS;
1184 size = 0;
1185 if ((filename = strdupW( MSI_RecordGetString(row, 3) )))
1187 msi_reduce_to_long_filename( filename );
1188 size = lstrlenW( filename );
1190 size += lstrlenW(dir) + 2;
1191 path = msi_alloc(size * sizeof(WCHAR));
1192 if (!path)
1194 ret = ERROR_OUTOFMEMORY;
1195 goto done;
1198 if (filename)
1200 lstrcpyW(path, dir);
1201 PathAddBackslashW(path);
1202 lstrcatW(path, filename);
1204 TRACE("Deleting misc file: %s\n", debugstr_w(path));
1205 DeleteFileW(path);
1207 else
1209 TRACE("Removing misc directory: %s\n", debugstr_w(dir));
1210 RemoveDirectoryW(dir);
1213 done:
1214 uirow = MSI_CreateRecord( 9 );
1215 MSI_RecordSetStringW( uirow, 1, MSI_RecordGetString(row, 1) );
1216 MSI_RecordSetStringW( uirow, 9, dir );
1217 msi_ui_actiondata( package, szRemoveFiles, uirow );
1218 msiobj_release( &uirow->hdr );
1220 msi_free(filename);
1221 msi_free(path);
1222 msi_free(dir);
1223 return ret;
1226 static void remove_folder( MSIFOLDER *folder )
1228 FolderList *fl;
1230 LIST_FOR_EACH_ENTRY( fl, &folder->children, FolderList, entry )
1232 remove_folder( fl->folder );
1234 if (!folder->persistent && folder->State != FOLDER_STATE_REMOVED)
1236 if (RemoveDirectoryW( folder->ResolvedTarget )) folder->State = FOLDER_STATE_REMOVED;
1240 UINT ACTION_RemoveFiles( MSIPACKAGE *package )
1242 static const WCHAR query[] = {
1243 'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
1244 '`','R','e','m','o','v','e','F','i','l','e','`',0};
1245 MSIQUERY *view;
1246 MSICOMPONENT *comp;
1247 MSIFILE *file;
1248 UINT r;
1250 r = MSI_DatabaseOpenViewW(package->db, query, &view);
1251 if (r == ERROR_SUCCESS)
1253 r = MSI_IterateRecords(view, NULL, ITERATE_RemoveFiles, package);
1254 msiobj_release(&view->hdr);
1255 if (r != ERROR_SUCCESS)
1256 return r;
1259 LIST_FOR_EACH_ENTRY( file, &package->files, MSIFILE, entry )
1261 MSIRECORD *uirow;
1262 VS_FIXEDFILEINFO *ver;
1264 comp = file->Component;
1265 msi_file_update_ui( package, file, szRemoveFiles );
1267 comp->Action = msi_get_component_action( package, comp );
1268 if (comp->Action != INSTALLSTATE_ABSENT || comp->Installed == INSTALLSTATE_SOURCE)
1269 continue;
1271 if (comp->assembly && !comp->assembly->application)
1272 continue;
1274 if (comp->Attributes & msidbComponentAttributesPermanent)
1276 TRACE("permanent component, not removing file\n");
1277 continue;
1280 if (file->Version)
1282 ver = msi_get_disk_file_version( file->TargetPath );
1283 if (ver && msi_compare_file_versions( ver, file->Version ) > 0)
1285 TRACE("newer version detected, not removing file\n");
1286 msi_free( ver );
1287 continue;
1289 msi_free( ver );
1292 if (file->state == msifs_installed)
1293 WARN("removing installed file %s\n", debugstr_w(file->TargetPath));
1295 TRACE("removing %s\n", debugstr_w(file->File) );
1297 SetFileAttributesW( file->TargetPath, FILE_ATTRIBUTE_NORMAL );
1298 if (!DeleteFileW( file->TargetPath ))
1300 WARN("failed to delete %s (%u)\n", debugstr_w(file->TargetPath), GetLastError());
1302 file->state = msifs_missing;
1304 uirow = MSI_CreateRecord( 9 );
1305 MSI_RecordSetStringW( uirow, 1, file->FileName );
1306 MSI_RecordSetStringW( uirow, 9, comp->Directory );
1307 msi_ui_actiondata( package, szRemoveFiles, uirow );
1308 msiobj_release( &uirow->hdr );
1311 LIST_FOR_EACH_ENTRY( comp, &package->components, MSICOMPONENT, entry )
1313 comp->Action = msi_get_component_action( package, comp );
1314 if (comp->Action != INSTALLSTATE_ABSENT) continue;
1316 if (comp->Attributes & msidbComponentAttributesPermanent)
1318 TRACE("permanent component, not removing directory\n");
1319 continue;
1321 if (comp->assembly && !comp->assembly->application)
1322 msi_uninstall_assembly( package, comp );
1323 else
1325 MSIFOLDER *folder = msi_get_loaded_folder( package, comp->Directory );
1326 remove_folder( folder );
1329 return ERROR_SUCCESS;