wined3d: Use a shader for unconverted blits.
[wine.git] / dlls / msi / files.c
blobd1e3ecfdf54a5d37d3102aef25150340e83e86fc
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 "patchapi.h"
47 #include "wine/unicode.h"
49 WINE_DEFAULT_DEBUG_CHANNEL(msi);
51 static void msi_file_update_ui( MSIPACKAGE *package, MSIFILE *f, const WCHAR *action )
53 MSIRECORD *uirow;
55 uirow = MSI_CreateRecord( 9 );
56 MSI_RecordSetStringW( uirow, 1, f->FileName );
57 MSI_RecordSetStringW( uirow, 9, f->Component->Directory );
58 MSI_RecordSetInteger( uirow, 6, f->FileSize );
59 msi_ui_actiondata( package, action, uirow );
60 msiobj_release( &uirow->hdr );
61 msi_ui_progress( package, 2, f->FileSize, 0, 0 );
64 static msi_file_state calculate_install_state( MSIPACKAGE *package, MSIFILE *file )
66 MSICOMPONENT *comp = file->Component;
67 VS_FIXEDFILEINFO *file_version;
68 WCHAR *font_version;
69 msi_file_state state;
70 DWORD size;
72 comp->Action = msi_get_component_action( package, comp );
73 if (!comp->Enabled || comp->Action != INSTALLSTATE_LOCAL || (comp->assembly && comp->assembly->installed))
75 TRACE("skipping %s (not scheduled for install)\n", debugstr_w(file->File));
76 return msifs_skipped;
78 if (!list_empty( &package->patches ) && file->disk_id < MSI_INITIAL_MEDIA_TRANSFORM_DISKID)
80 TRACE("skipping %s (not part of patch)\n", debugstr_w(file->File));
81 return msifs_skipped;
83 if ((msi_is_global_assembly( comp ) && !comp->assembly->installed) ||
84 GetFileAttributesW( file->TargetPath ) == INVALID_FILE_ATTRIBUTES)
86 TRACE("installing %s (missing)\n", debugstr_w(file->File));
87 return msifs_missing;
89 if (file->Version)
91 if ((file_version = msi_get_disk_file_version( file->TargetPath )))
93 if (msi_compare_file_versions( file_version, file->Version ) < 0)
95 TRACE("overwriting %s (new version %s old version %u.%u.%u.%u)\n",
96 debugstr_w(file->File), debugstr_w(file->Version),
97 HIWORD(file_version->dwFileVersionMS), LOWORD(file_version->dwFileVersionMS),
98 HIWORD(file_version->dwFileVersionLS), LOWORD(file_version->dwFileVersionLS));
99 state = msifs_overwrite;
101 else
103 TRACE("keeping %s (new version %s old version %u.%u.%u.%u)\n",
104 debugstr_w(file->File), debugstr_w(file->Version),
105 HIWORD(file_version->dwFileVersionMS), LOWORD(file_version->dwFileVersionMS),
106 HIWORD(file_version->dwFileVersionLS), LOWORD(file_version->dwFileVersionLS));
107 state = msifs_present;
109 msi_free( file_version );
110 return state;
112 else if ((font_version = msi_font_version_from_file( file->TargetPath )))
114 if (msi_compare_font_versions( font_version, file->Version ) < 0)
116 TRACE("overwriting %s (new version %s old version %u.%u.%u.%u)\n",
117 debugstr_w(file->File), debugstr_w(file->Version),
118 HIWORD(file_version->dwFileVersionMS), LOWORD(file_version->dwFileVersionMS),
119 HIWORD(file_version->dwFileVersionLS), LOWORD(file_version->dwFileVersionLS));
120 state = msifs_overwrite;
122 else
124 TRACE("keeping %s (new version %s old version %u.%u.%u.%u)\n",
125 debugstr_w(file->File), debugstr_w(file->Version),
126 HIWORD(file_version->dwFileVersionMS), LOWORD(file_version->dwFileVersionMS),
127 HIWORD(file_version->dwFileVersionLS), LOWORD(file_version->dwFileVersionLS));
128 state = msifs_present;
130 msi_free( font_version );
131 return state;
134 if ((size = msi_get_disk_file_size( file->TargetPath )) != file->FileSize)
136 TRACE("overwriting %s (old size %u new size %u)\n", debugstr_w(file->File), size, file->FileSize);
137 return msifs_overwrite;
139 if (file->hash.dwFileHashInfoSize)
141 if (msi_file_hash_matches( file ))
143 TRACE("keeping %s (hash match)\n", debugstr_w(file->File));
144 return msifs_hashmatch;
146 else
148 TRACE("overwriting %s (hash mismatch)\n", debugstr_w(file->File));
149 return msifs_overwrite;
152 /* assume present */
153 TRACE("keeping %s\n", debugstr_w(file->File));
154 return msifs_present;
157 static void schedule_install_files(MSIPACKAGE *package)
159 MSIFILE *file;
161 LIST_FOR_EACH_ENTRY(file, &package->files, MSIFILE, entry)
163 MSICOMPONENT *comp = file->Component;
165 file->state = calculate_install_state( package, file );
166 if (file->state == msifs_overwrite && (comp->Attributes & msidbComponentAttributesNeverOverwrite))
168 TRACE("not overwriting %s\n", debugstr_w(file->TargetPath));
169 file->state = msifs_skipped;
174 static UINT copy_file(MSIFILE *file, LPWSTR source)
176 BOOL ret;
178 ret = CopyFileW(source, file->TargetPath, FALSE);
179 if (!ret)
180 return GetLastError();
182 SetFileAttributesW(file->TargetPath, FILE_ATTRIBUTE_NORMAL);
184 file->state = msifs_installed;
185 return ERROR_SUCCESS;
188 static UINT copy_install_file(MSIPACKAGE *package, MSIFILE *file, LPWSTR source)
190 UINT gle;
192 TRACE("Copying %s to %s\n", debugstr_w(source), debugstr_w(file->TargetPath));
194 gle = copy_file(file, source);
195 if (gle == ERROR_SUCCESS)
196 return gle;
198 if (gle == ERROR_ALREADY_EXISTS && file->state == msifs_overwrite)
200 TRACE("overwriting existing file\n");
201 return ERROR_SUCCESS;
203 else if (gle == ERROR_ACCESS_DENIED)
205 SetFileAttributesW(file->TargetPath, FILE_ATTRIBUTE_NORMAL);
207 gle = copy_file(file, source);
208 TRACE("Overwriting existing file: %d\n", gle);
210 if (gle == ERROR_SHARING_VIOLATION || gle == ERROR_USER_MAPPED_FILE)
212 WCHAR *tmpfileW, *pathW, *p;
213 DWORD len;
215 TRACE("file in use, scheduling rename operation\n");
217 if (!(pathW = strdupW( file->TargetPath ))) return ERROR_OUTOFMEMORY;
218 if ((p = strrchrW(pathW, '\\'))) *p = 0;
219 len = strlenW( pathW ) + 16;
220 if (!(tmpfileW = msi_alloc(len * sizeof(WCHAR))))
222 msi_free( pathW );
223 return ERROR_OUTOFMEMORY;
225 if (!GetTempFileNameW( pathW, szMsi, 0, tmpfileW )) tmpfileW[0] = 0;
226 msi_free( pathW );
228 if (CopyFileW(source, tmpfileW, FALSE) &&
229 MoveFileExW(file->TargetPath, NULL, MOVEFILE_DELAY_UNTIL_REBOOT) &&
230 MoveFileExW(tmpfileW, file->TargetPath, MOVEFILE_DELAY_UNTIL_REBOOT))
232 file->state = msifs_installed;
233 package->need_reboot_at_end = 1;
234 gle = ERROR_SUCCESS;
236 else
238 gle = GetLastError();
239 WARN("failed to schedule rename operation: %d)\n", gle);
240 DeleteFileW( tmpfileW );
242 msi_free(tmpfileW);
245 return gle;
248 static UINT msi_create_directory( MSIPACKAGE *package, const WCHAR *dir )
250 MSIFOLDER *folder;
251 const WCHAR *install_path;
253 install_path = msi_get_target_folder( package, dir );
254 if (!install_path) return ERROR_FUNCTION_FAILED;
256 folder = msi_get_loaded_folder( package, dir );
257 if (folder->State == FOLDER_STATE_UNINITIALIZED)
259 msi_create_full_path( install_path );
260 folder->State = FOLDER_STATE_CREATED;
262 return ERROR_SUCCESS;
265 static MSIFILE *find_file( MSIPACKAGE *package, UINT disk_id, const WCHAR *filename )
267 MSIFILE *file;
269 LIST_FOR_EACH_ENTRY( file, &package->files, MSIFILE, entry )
271 if (file->disk_id == disk_id &&
272 file->state != msifs_installed &&
273 !strcmpiW( filename, file->File )) return file;
275 return NULL;
278 static BOOL installfiles_cb(MSIPACKAGE *package, LPCWSTR filename, DWORD action,
279 LPWSTR *path, DWORD *attrs, PVOID user)
281 MSIFILE *file = *(MSIFILE **)user;
283 if (action == MSICABEXTRACT_BEGINEXTRACT)
285 if (!(file = find_file( package, file->disk_id, filename )))
287 TRACE("unknown file in cabinet (%s)\n", debugstr_w(filename));
288 return FALSE;
290 if (file->state != msifs_missing && file->state != msifs_overwrite)
291 return FALSE;
293 if (!msi_is_global_assembly( file->Component ))
295 msi_create_directory( package, file->Component->Directory );
297 *path = strdupW( file->TargetPath );
298 *attrs = file->Attributes;
299 *(MSIFILE **)user = file;
301 else if (action == MSICABEXTRACT_FILEEXTRACTED)
303 if (!msi_is_global_assembly( file->Component )) file->state = msifs_installed;
306 return TRUE;
309 WCHAR *msi_resolve_file_source( MSIPACKAGE *package, MSIFILE *file )
311 WCHAR *p, *path;
313 TRACE("Working to resolve source of file %s\n", debugstr_w(file->File));
315 if (file->IsCompressed) return NULL;
317 p = msi_resolve_source_folder( package, file->Component->Directory, NULL );
318 path = msi_build_directory_name( 2, p, file->ShortName );
320 if (file->LongName && GetFileAttributesW( path ) == INVALID_FILE_ATTRIBUTES)
322 msi_free( path );
323 path = msi_build_directory_name( 2, p, file->LongName );
325 msi_free( p );
326 TRACE("file %s source resolves to %s\n", debugstr_w(file->File), debugstr_w(path));
327 return path;
331 * ACTION_InstallFiles()
333 * For efficiency, this is done in two passes:
334 * 1) Correct all the TargetPaths and determine what files are to be installed.
335 * 2) Extract Cabinets and copy files.
337 UINT ACTION_InstallFiles(MSIPACKAGE *package)
339 MSIMEDIAINFO *mi;
340 UINT rc = ERROR_SUCCESS;
341 MSIFILE *file;
343 schedule_install_files(package);
344 mi = msi_alloc_zero( sizeof(MSIMEDIAINFO) );
346 LIST_FOR_EACH_ENTRY( file, &package->files, MSIFILE, entry )
348 msi_file_update_ui( package, file, szInstallFiles );
350 rc = msi_load_media_info( package, file->Sequence, mi );
351 if (rc != ERROR_SUCCESS)
353 ERR("Unable to load media info for %s (%u)\n", debugstr_w(file->File), rc);
354 rc = ERROR_FUNCTION_FAILED;
355 goto done;
358 if (file->state != msifs_hashmatch &&
359 file->state != msifs_skipped &&
360 (file->state != msifs_present || !msi_get_property_int( package->db, szInstalled, 0 )) &&
361 (rc = ready_media( package, file->IsCompressed, mi )))
363 ERR("Failed to ready media for %s\n", debugstr_w(file->File));
364 goto done;
367 if (file->state != msifs_missing && !mi->is_continuous && file->state != msifs_overwrite)
368 continue;
370 if (file->Sequence > mi->last_sequence || mi->is_continuous ||
371 (file->IsCompressed && !mi->is_extracted))
373 MSICABDATA data;
374 MSIFILE *cursor = file;
376 data.mi = mi;
377 data.package = package;
378 data.cb = installfiles_cb;
379 data.user = &cursor;
381 if (file->IsCompressed && !msi_cabextract(package, mi, &data))
383 ERR("Failed to extract cabinet: %s\n", debugstr_w(mi->cabinet));
384 rc = ERROR_INSTALL_FAILURE;
385 goto done;
389 if (!file->IsCompressed)
391 WCHAR *source = msi_resolve_file_source(package, file);
393 TRACE("copying %s to %s\n", debugstr_w(source), debugstr_w(file->TargetPath));
395 if (!msi_is_global_assembly( file->Component ))
397 msi_create_directory(package, file->Component->Directory);
399 rc = copy_install_file(package, file, source);
400 if (rc != ERROR_SUCCESS)
402 ERR("Failed to copy %s to %s (%u)\n", debugstr_w(source), debugstr_w(file->TargetPath), rc);
403 rc = ERROR_INSTALL_FAILURE;
404 msi_free(source);
405 goto done;
407 msi_free(source);
409 else if (!msi_is_global_assembly( file->Component ) &&
410 file->state != msifs_installed && !(file->Attributes & msidbFileAttributesPatchAdded))
412 ERR("compressed file wasn't installed (%s)\n", debugstr_w(file->File));
413 rc = ERROR_INSTALL_FAILURE;
414 goto done;
417 LIST_FOR_EACH_ENTRY( file, &package->files, MSIFILE, entry )
419 MSICOMPONENT *comp = file->Component;
421 if (!msi_is_global_assembly( comp ) || comp->assembly->installed ||
422 (file->state != msifs_missing && file->state != msifs_overwrite)) continue;
424 rc = msi_install_assembly( package, comp );
425 if (rc != ERROR_SUCCESS)
427 ERR("Failed to install assembly\n");
428 rc = ERROR_INSTALL_FAILURE;
429 break;
431 file->state = msifs_installed;
434 done:
435 msi_free_media_info(mi);
436 return rc;
439 static MSIFILEPATCH *find_filepatch( MSIPACKAGE *package, UINT disk_id, const WCHAR *key )
441 MSIFILEPATCH *patch;
443 LIST_FOR_EACH_ENTRY( patch, &package->filepatches, MSIFILEPATCH, entry )
445 if (!patch->extracted && patch->disk_id == disk_id && !strcmpW( key, patch->File->File ))
446 return patch;
448 return NULL;
451 static BOOL patchfiles_cb(MSIPACKAGE *package, LPCWSTR file, DWORD action,
452 LPWSTR *path, DWORD *attrs, PVOID user)
454 MSIFILEPATCH *patch = *(MSIFILEPATCH **)user;
456 if (action == MSICABEXTRACT_BEGINEXTRACT)
458 if (!(patch = find_filepatch( package, patch->disk_id, file ))) return FALSE;
460 patch->path = msi_create_temp_file( package->db );
461 *path = strdupW( patch->path );
462 *attrs = patch->File->Attributes;
463 *(MSIFILEPATCH **)user = patch;
465 else if (action == MSICABEXTRACT_FILEEXTRACTED)
467 patch->extracted = TRUE;
470 return TRUE;
473 UINT ACTION_PatchFiles( MSIPACKAGE *package )
475 MSIFILEPATCH *patch;
476 MSIMEDIAINFO *mi;
477 UINT rc = ERROR_SUCCESS;
479 TRACE("%p\n", package);
481 mi = msi_alloc_zero( sizeof(MSIMEDIAINFO) );
483 TRACE("extracting files\n");
485 LIST_FOR_EACH_ENTRY( patch, &package->filepatches, MSIFILEPATCH, entry )
487 MSIFILE *file = patch->File;
488 MSICOMPONENT *comp = file->Component;
490 rc = msi_load_media_info( package, patch->Sequence, mi );
491 if (rc != ERROR_SUCCESS)
493 ERR("Unable to load media info for %s (%u)\n", debugstr_w(file->File), rc);
494 rc = ERROR_FUNCTION_FAILED;
495 goto done;
497 comp->Action = msi_get_component_action( package, comp );
498 if (!comp->Enabled || comp->Action != INSTALLSTATE_LOCAL) continue;
500 if (!patch->extracted)
502 MSICABDATA data;
503 MSIFILEPATCH *cursor = patch;
505 rc = ready_media( package, TRUE, mi );
506 if (rc != ERROR_SUCCESS)
508 ERR("Failed to ready media for %s\n", debugstr_w(file->File));
509 goto done;
511 data.mi = mi;
512 data.package = package;
513 data.cb = patchfiles_cb;
514 data.user = &cursor;
516 if (!msi_cabextract( package, mi, &data ))
518 ERR("Failed to extract cabinet: %s\n", debugstr_w(mi->cabinet));
519 rc = ERROR_INSTALL_FAILURE;
520 goto done;
525 TRACE("applying patches\n");
527 LIST_FOR_EACH_ENTRY( patch, &package->filepatches, MSIFILEPATCH, entry )
529 WCHAR *tmpfile;
530 BOOL ret;
532 if (!patch->path) continue;
534 if (!(tmpfile = msi_create_temp_file( package->db )))
536 rc = ERROR_INSTALL_FAILURE;
537 goto done;
539 ret = ApplyPatchToFileW( patch->path, patch->File->TargetPath, tmpfile, 0 );
540 if (ret)
542 DeleteFileW( patch->File->TargetPath );
543 MoveFileW( tmpfile, patch->File->TargetPath );
545 else
546 WARN("failed to patch %s: %08x\n", debugstr_w(patch->File->TargetPath), GetLastError());
548 DeleteFileW( patch->path );
549 DeleteFileW( tmpfile );
550 msi_free( tmpfile );
552 if (!ret && !(patch->Attributes & msidbPatchAttributesNonVital))
554 ERR("Failed to apply patch to file: %s\n", debugstr_w(patch->File->File));
555 rc = ERROR_INSTALL_FAILURE;
556 goto done;
560 done:
561 msi_free_media_info(mi);
562 return rc;
565 #define is_dot_dir(x) ((x[0] == '.') && ((x[1] == 0) || ((x[1] == '.') && (x[2] == 0))))
567 typedef struct
569 struct list entry;
570 LPWSTR sourcename;
571 LPWSTR destname;
572 LPWSTR source;
573 LPWSTR dest;
574 } FILE_LIST;
576 static BOOL msi_move_file(LPCWSTR source, LPCWSTR dest, int options)
578 BOOL ret;
580 if (GetFileAttributesW(source) == FILE_ATTRIBUTE_DIRECTORY ||
581 GetFileAttributesW(dest) == FILE_ATTRIBUTE_DIRECTORY)
583 WARN("Source or dest is directory, not moving\n");
584 return FALSE;
587 if (options == msidbMoveFileOptionsMove)
589 TRACE("moving %s -> %s\n", debugstr_w(source), debugstr_w(dest));
590 ret = MoveFileExW(source, dest, MOVEFILE_REPLACE_EXISTING);
591 if (!ret)
593 WARN("MoveFile failed: %d\n", GetLastError());
594 return FALSE;
597 else
599 TRACE("copying %s -> %s\n", debugstr_w(source), debugstr_w(dest));
600 ret = CopyFileW(source, dest, FALSE);
601 if (!ret)
603 WARN("CopyFile failed: %d\n", GetLastError());
604 return FALSE;
608 return TRUE;
611 static LPWSTR wildcard_to_file(LPWSTR wildcard, LPWSTR filename)
613 LPWSTR path, ptr;
614 DWORD dirlen, pathlen;
616 ptr = strrchrW(wildcard, '\\');
617 dirlen = ptr - wildcard + 1;
619 pathlen = dirlen + lstrlenW(filename) + 1;
620 path = msi_alloc(pathlen * sizeof(WCHAR));
622 lstrcpynW(path, wildcard, dirlen + 1);
623 lstrcatW(path, filename);
625 return path;
628 static void free_file_entry(FILE_LIST *file)
630 msi_free(file->source);
631 msi_free(file->dest);
632 msi_free(file);
635 static void free_list(FILE_LIST *list)
637 while (!list_empty(&list->entry))
639 FILE_LIST *file = LIST_ENTRY(list_head(&list->entry), FILE_LIST, entry);
641 list_remove(&file->entry);
642 free_file_entry(file);
646 static BOOL add_wildcard(FILE_LIST *files, LPWSTR source, LPWSTR dest)
648 FILE_LIST *new, *file;
649 LPWSTR ptr, filename;
650 DWORD size;
652 new = msi_alloc_zero(sizeof(FILE_LIST));
653 if (!new)
654 return FALSE;
656 new->source = strdupW(source);
657 ptr = strrchrW(dest, '\\') + 1;
658 filename = strrchrW(new->source, '\\') + 1;
660 new->sourcename = filename;
662 if (*ptr)
663 new->destname = ptr;
664 else
665 new->destname = new->sourcename;
667 size = (ptr - dest) + lstrlenW(filename) + 1;
668 new->dest = msi_alloc(size * sizeof(WCHAR));
669 if (!new->dest)
671 free_file_entry(new);
672 return FALSE;
675 lstrcpynW(new->dest, dest, ptr - dest + 1);
676 lstrcatW(new->dest, filename);
678 if (list_empty(&files->entry))
680 list_add_head(&files->entry, &new->entry);
681 return TRUE;
684 LIST_FOR_EACH_ENTRY(file, &files->entry, FILE_LIST, entry)
686 if (strcmpW( source, file->source ) < 0)
688 list_add_before(&file->entry, &new->entry);
689 return TRUE;
693 list_add_after(&file->entry, &new->entry);
694 return TRUE;
697 static BOOL move_files_wildcard(LPWSTR source, LPWSTR dest, int options)
699 WIN32_FIND_DATAW wfd;
700 HANDLE hfile;
701 LPWSTR path;
702 BOOL res;
703 FILE_LIST files, *file;
704 DWORD size;
706 hfile = FindFirstFileW(source, &wfd);
707 if (hfile == INVALID_HANDLE_VALUE) return FALSE;
709 list_init(&files.entry);
711 for (res = TRUE; res; res = FindNextFileW(hfile, &wfd))
713 if (is_dot_dir(wfd.cFileName)) continue;
715 path = wildcard_to_file(source, wfd.cFileName);
716 if (!path)
718 res = FALSE;
719 goto done;
722 add_wildcard(&files, path, dest);
723 msi_free(path);
726 /* no files match the wildcard */
727 if (list_empty(&files.entry))
728 goto done;
730 /* only the first wildcard match gets renamed to dest */
731 file = LIST_ENTRY(list_head(&files.entry), FILE_LIST, entry);
732 size = (strrchrW(file->dest, '\\') - file->dest) + lstrlenW(file->destname) + 2;
733 file->dest = msi_realloc(file->dest, size * sizeof(WCHAR));
734 if (!file->dest)
736 res = FALSE;
737 goto done;
740 /* file->dest may be shorter after the reallocation, so add a NULL
741 * terminator. This is needed for the call to strrchrW, as there will no
742 * longer be a NULL terminator within the bounds of the allocation in this case.
744 file->dest[size - 1] = '\0';
745 lstrcpyW(strrchrW(file->dest, '\\') + 1, file->destname);
747 while (!list_empty(&files.entry))
749 file = LIST_ENTRY(list_head(&files.entry), FILE_LIST, entry);
751 msi_move_file(file->source, file->dest, options);
753 list_remove(&file->entry);
754 free_file_entry(file);
757 res = TRUE;
759 done:
760 free_list(&files);
761 FindClose(hfile);
762 return res;
765 void msi_reduce_to_long_filename( WCHAR *filename )
767 WCHAR *p = strchrW( filename, '|' );
768 if (p) memmove( filename, p + 1, (strlenW( p + 1 ) + 1) * sizeof(WCHAR) );
771 static UINT ITERATE_MoveFiles( MSIRECORD *rec, LPVOID param )
773 MSIPACKAGE *package = param;
774 MSIRECORD *uirow;
775 MSICOMPONENT *comp;
776 LPCWSTR sourcename, component;
777 LPWSTR sourcedir, destname = NULL, destdir = NULL, source = NULL, dest = NULL;
778 int options;
779 DWORD size;
780 BOOL wildcards;
782 component = MSI_RecordGetString(rec, 2);
783 comp = msi_get_loaded_component(package, component);
784 if (!comp)
785 return ERROR_SUCCESS;
787 comp->Action = msi_get_component_action( package, comp );
788 if (comp->Action != INSTALLSTATE_LOCAL)
790 TRACE("component not scheduled for installation %s\n", debugstr_w(component));
791 return ERROR_SUCCESS;
794 sourcename = MSI_RecordGetString(rec, 3);
795 options = MSI_RecordGetInteger(rec, 7);
797 sourcedir = msi_dup_property(package->db, MSI_RecordGetString(rec, 5));
798 if (!sourcedir)
799 goto done;
801 destdir = msi_dup_property(package->db, MSI_RecordGetString(rec, 6));
802 if (!destdir)
803 goto done;
805 if (!sourcename)
807 if (GetFileAttributesW(sourcedir) == INVALID_FILE_ATTRIBUTES)
808 goto done;
810 source = strdupW(sourcedir);
811 if (!source)
812 goto done;
814 else
816 size = lstrlenW(sourcedir) + lstrlenW(sourcename) + 2;
817 source = msi_alloc(size * sizeof(WCHAR));
818 if (!source)
819 goto done;
821 lstrcpyW(source, sourcedir);
822 if (source[lstrlenW(source) - 1] != '\\')
823 lstrcatW(source, szBackSlash);
824 lstrcatW(source, sourcename);
827 wildcards = strchrW(source, '*') || strchrW(source, '?');
829 if (MSI_RecordIsNull(rec, 4))
831 if (!wildcards)
833 WCHAR *p;
834 if (sourcename)
835 destname = strdupW(sourcename);
836 else if ((p = strrchrW(sourcedir, '\\')))
837 destname = strdupW(p + 1);
838 else
839 destname = strdupW(sourcedir);
840 if (!destname)
841 goto done;
844 else
846 destname = strdupW(MSI_RecordGetString(rec, 4));
847 if (destname) msi_reduce_to_long_filename(destname);
850 size = 0;
851 if (destname)
852 size = lstrlenW(destname);
854 size += lstrlenW(destdir) + 2;
855 dest = msi_alloc(size * sizeof(WCHAR));
856 if (!dest)
857 goto done;
859 lstrcpyW(dest, destdir);
860 if (dest[lstrlenW(dest) - 1] != '\\')
861 lstrcatW(dest, szBackSlash);
863 if (destname)
864 lstrcatW(dest, destname);
866 if (GetFileAttributesW(destdir) == INVALID_FILE_ATTRIBUTES)
868 if (!msi_create_full_path(destdir))
870 WARN("failed to create directory %u\n", GetLastError());
871 goto done;
875 if (!wildcards)
876 msi_move_file(source, dest, options);
877 else
878 move_files_wildcard(source, dest, options);
880 done:
881 uirow = MSI_CreateRecord( 9 );
882 MSI_RecordSetStringW( uirow, 1, MSI_RecordGetString(rec, 1) );
883 MSI_RecordSetInteger( uirow, 6, 1 ); /* FIXME */
884 MSI_RecordSetStringW( uirow, 9, destdir );
885 msi_ui_actiondata( package, szMoveFiles, uirow );
886 msiobj_release( &uirow->hdr );
888 msi_free(sourcedir);
889 msi_free(destdir);
890 msi_free(destname);
891 msi_free(source);
892 msi_free(dest);
894 return ERROR_SUCCESS;
897 UINT ACTION_MoveFiles( MSIPACKAGE *package )
899 static const WCHAR query[] = {
900 'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
901 '`','M','o','v','e','F','i','l','e','`',0};
902 MSIQUERY *view;
903 UINT rc;
905 rc = MSI_DatabaseOpenViewW(package->db, query, &view);
906 if (rc != ERROR_SUCCESS)
907 return ERROR_SUCCESS;
909 rc = MSI_IterateRecords(view, NULL, ITERATE_MoveFiles, package);
910 msiobj_release(&view->hdr);
911 return rc;
914 static WCHAR *get_duplicate_filename( MSIPACKAGE *package, MSIRECORD *row, const WCHAR *file_key, const WCHAR *src )
916 DWORD len;
917 WCHAR *dst_name, *dst_path, *dst;
919 if (MSI_RecordIsNull( row, 4 ))
921 len = strlenW( src ) + 1;
922 if (!(dst_name = msi_alloc( len * sizeof(WCHAR)))) return NULL;
923 strcpyW( dst_name, strrchrW( src, '\\' ) + 1 );
925 else
927 MSI_RecordGetStringW( row, 4, NULL, &len );
928 if (!(dst_name = msi_alloc( ++len * sizeof(WCHAR) ))) return NULL;
929 MSI_RecordGetStringW( row, 4, dst_name, &len );
930 msi_reduce_to_long_filename( dst_name );
933 if (MSI_RecordIsNull( row, 5 ))
935 WCHAR *p;
936 dst_path = strdupW( src );
937 p = strrchrW( dst_path, '\\' );
938 if (p) *p = 0;
940 else
942 const WCHAR *dst_key = MSI_RecordGetString( row, 5 );
944 dst_path = strdupW( msi_get_target_folder( package, dst_key ) );
945 if (!dst_path)
947 /* try a property */
948 dst_path = msi_dup_property( package->db, dst_key );
949 if (!dst_path)
951 FIXME("Unable to get destination folder, try AppSearch properties\n");
952 msi_free( dst_name );
953 return NULL;
958 dst = msi_build_directory_name( 2, dst_path, dst_name );
959 msi_create_full_path( dst_path );
961 msi_free( dst_name );
962 msi_free( dst_path );
963 return dst;
966 static UINT ITERATE_DuplicateFiles(MSIRECORD *row, LPVOID param)
968 MSIPACKAGE *package = param;
969 LPWSTR dest;
970 LPCWSTR file_key, component;
971 MSICOMPONENT *comp;
972 MSIRECORD *uirow;
973 MSIFILE *file;
975 component = MSI_RecordGetString(row,2);
976 comp = msi_get_loaded_component(package, component);
977 if (!comp)
978 return ERROR_SUCCESS;
980 comp->Action = msi_get_component_action( package, comp );
981 if (comp->Action != INSTALLSTATE_LOCAL)
983 TRACE("component not scheduled for installation %s\n", debugstr_w(component));
984 return ERROR_SUCCESS;
987 file_key = MSI_RecordGetString(row,3);
988 if (!file_key)
990 ERR("Unable to get file key\n");
991 return ERROR_FUNCTION_FAILED;
994 file = msi_get_loaded_file( package, file_key );
995 if (!file)
997 ERR("Original file unknown %s\n", debugstr_w(file_key));
998 return ERROR_SUCCESS;
1001 dest = get_duplicate_filename( package, row, file_key, file->TargetPath );
1002 if (!dest)
1004 WARN("Unable to get duplicate filename\n");
1005 return ERROR_SUCCESS;
1008 TRACE("Duplicating file %s to %s\n", debugstr_w(file->TargetPath), debugstr_w(dest));
1010 if (!CopyFileW( file->TargetPath, dest, TRUE ))
1012 WARN("Failed to copy file %s -> %s (%u)\n",
1013 debugstr_w(file->TargetPath), debugstr_w(dest), GetLastError());
1016 FIXME("We should track these duplicate files as well\n");
1018 uirow = MSI_CreateRecord( 9 );
1019 MSI_RecordSetStringW( uirow, 1, MSI_RecordGetString( row, 1 ) );
1020 MSI_RecordSetInteger( uirow, 6, file->FileSize );
1021 MSI_RecordSetStringW( uirow, 9, MSI_RecordGetString( row, 5 ) );
1022 msi_ui_actiondata( package, szDuplicateFiles, uirow );
1023 msiobj_release( &uirow->hdr );
1025 msi_free(dest);
1026 return ERROR_SUCCESS;
1029 UINT ACTION_DuplicateFiles(MSIPACKAGE *package)
1031 static const WCHAR query[] = {
1032 'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
1033 '`','D','u','p','l','i','c','a','t','e','F','i','l','e','`',0};
1034 MSIQUERY *view;
1035 UINT rc;
1037 rc = MSI_DatabaseOpenViewW(package->db, query, &view);
1038 if (rc != ERROR_SUCCESS)
1039 return ERROR_SUCCESS;
1041 rc = MSI_IterateRecords(view, NULL, ITERATE_DuplicateFiles, package);
1042 msiobj_release(&view->hdr);
1043 return rc;
1046 static UINT ITERATE_RemoveDuplicateFiles( MSIRECORD *row, LPVOID param )
1048 MSIPACKAGE *package = param;
1049 LPWSTR dest;
1050 LPCWSTR file_key, component;
1051 MSICOMPONENT *comp;
1052 MSIRECORD *uirow;
1053 MSIFILE *file;
1055 component = MSI_RecordGetString( row, 2 );
1056 comp = msi_get_loaded_component( package, component );
1057 if (!comp)
1058 return ERROR_SUCCESS;
1060 comp->Action = msi_get_component_action( package, comp );
1061 if (comp->Action != INSTALLSTATE_ABSENT)
1063 TRACE("component not scheduled for removal %s\n", debugstr_w(component));
1064 return ERROR_SUCCESS;
1067 file_key = MSI_RecordGetString( row, 3 );
1068 if (!file_key)
1070 ERR("Unable to get file key\n");
1071 return ERROR_FUNCTION_FAILED;
1074 file = msi_get_loaded_file( package, file_key );
1075 if (!file)
1077 ERR("Original file unknown %s\n", debugstr_w(file_key));
1078 return ERROR_SUCCESS;
1081 dest = get_duplicate_filename( package, row, file_key, file->TargetPath );
1082 if (!dest)
1084 WARN("Unable to get duplicate filename\n");
1085 return ERROR_SUCCESS;
1088 TRACE("Removing duplicate %s of %s\n", debugstr_w(dest), debugstr_w(file->TargetPath));
1090 if (!DeleteFileW( dest ))
1092 WARN("Failed to delete duplicate file %s (%u)\n", debugstr_w(dest), GetLastError());
1095 uirow = MSI_CreateRecord( 9 );
1096 MSI_RecordSetStringW( uirow, 1, MSI_RecordGetString( row, 1 ) );
1097 MSI_RecordSetStringW( uirow, 9, MSI_RecordGetString( row, 5 ) );
1098 msi_ui_actiondata( package, szRemoveDuplicateFiles, uirow );
1099 msiobj_release( &uirow->hdr );
1101 msi_free(dest);
1102 return ERROR_SUCCESS;
1105 UINT ACTION_RemoveDuplicateFiles( MSIPACKAGE *package )
1107 static const WCHAR query[] = {
1108 'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
1109 '`','D','u','p','l','i','c','a','t','e','F','i','l','e','`',0};
1110 MSIQUERY *view;
1111 UINT rc;
1113 rc = MSI_DatabaseOpenViewW( package->db, query, &view );
1114 if (rc != ERROR_SUCCESS)
1115 return ERROR_SUCCESS;
1117 rc = MSI_IterateRecords( view, NULL, ITERATE_RemoveDuplicateFiles, package );
1118 msiobj_release( &view->hdr );
1119 return rc;
1122 static BOOL verify_comp_for_removal(MSICOMPONENT *comp, UINT install_mode)
1124 /* special case */
1125 if (comp->Action != INSTALLSTATE_SOURCE &&
1126 comp->Attributes & msidbComponentAttributesSourceOnly &&
1127 (install_mode == msidbRemoveFileInstallModeOnRemove ||
1128 install_mode == msidbRemoveFileInstallModeOnBoth)) return TRUE;
1130 switch (comp->Action)
1132 case INSTALLSTATE_LOCAL:
1133 case INSTALLSTATE_SOURCE:
1134 if (install_mode == msidbRemoveFileInstallModeOnInstall ||
1135 install_mode == msidbRemoveFileInstallModeOnBoth) return TRUE;
1136 break;
1137 case INSTALLSTATE_ABSENT:
1138 if (install_mode == msidbRemoveFileInstallModeOnRemove ||
1139 install_mode == msidbRemoveFileInstallModeOnBoth) return TRUE;
1140 break;
1141 default: break;
1143 return FALSE;
1146 static UINT ITERATE_RemoveFiles(MSIRECORD *row, LPVOID param)
1148 MSIPACKAGE *package = param;
1149 MSICOMPONENT *comp;
1150 MSIRECORD *uirow;
1151 LPCWSTR component, dirprop;
1152 UINT install_mode;
1153 LPWSTR dir = NULL, path = NULL, filename = NULL;
1154 DWORD size;
1155 UINT ret = ERROR_SUCCESS;
1157 component = MSI_RecordGetString(row, 2);
1158 dirprop = MSI_RecordGetString(row, 4);
1159 install_mode = MSI_RecordGetInteger(row, 5);
1161 comp = msi_get_loaded_component(package, component);
1162 if (!comp)
1163 return ERROR_SUCCESS;
1165 comp->Action = msi_get_component_action( package, comp );
1166 if (!verify_comp_for_removal(comp, install_mode))
1168 TRACE("Skipping removal due to install mode\n");
1169 return ERROR_SUCCESS;
1171 if (comp->assembly && !comp->assembly->application)
1173 return ERROR_SUCCESS;
1175 if (comp->Attributes & msidbComponentAttributesPermanent)
1177 TRACE("permanent component, not removing file\n");
1178 return ERROR_SUCCESS;
1181 dir = msi_dup_property(package->db, dirprop);
1182 if (!dir)
1184 WARN("directory property has no value\n");
1185 return ERROR_SUCCESS;
1187 size = 0;
1188 if ((filename = strdupW( MSI_RecordGetString(row, 3) )))
1190 msi_reduce_to_long_filename( filename );
1191 size = lstrlenW( filename );
1193 size += lstrlenW(dir) + 2;
1194 path = msi_alloc(size * sizeof(WCHAR));
1195 if (!path)
1197 ret = ERROR_OUTOFMEMORY;
1198 goto done;
1201 if (filename)
1203 lstrcpyW(path, dir);
1204 PathAddBackslashW(path);
1205 lstrcatW(path, filename);
1207 TRACE("Deleting misc file: %s\n", debugstr_w(path));
1208 DeleteFileW(path);
1210 else
1212 TRACE("Removing misc directory: %s\n", debugstr_w(dir));
1213 RemoveDirectoryW(dir);
1216 done:
1217 uirow = MSI_CreateRecord( 9 );
1218 MSI_RecordSetStringW( uirow, 1, MSI_RecordGetString(row, 1) );
1219 MSI_RecordSetStringW( uirow, 9, dir );
1220 msi_ui_actiondata( package, szRemoveFiles, uirow );
1221 msiobj_release( &uirow->hdr );
1223 msi_free(filename);
1224 msi_free(path);
1225 msi_free(dir);
1226 return ret;
1229 static void remove_folder( MSIFOLDER *folder )
1231 FolderList *fl;
1233 LIST_FOR_EACH_ENTRY( fl, &folder->children, FolderList, entry )
1235 remove_folder( fl->folder );
1237 if (!folder->persistent && folder->State != FOLDER_STATE_REMOVED)
1239 if (RemoveDirectoryW( folder->ResolvedTarget )) folder->State = FOLDER_STATE_REMOVED;
1243 UINT ACTION_RemoveFiles( MSIPACKAGE *package )
1245 static const WCHAR query[] = {
1246 'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
1247 '`','R','e','m','o','v','e','F','i','l','e','`',0};
1248 MSIQUERY *view;
1249 MSICOMPONENT *comp;
1250 MSIFILE *file;
1251 UINT r;
1253 r = MSI_DatabaseOpenViewW(package->db, query, &view);
1254 if (r == ERROR_SUCCESS)
1256 r = MSI_IterateRecords(view, NULL, ITERATE_RemoveFiles, package);
1257 msiobj_release(&view->hdr);
1258 if (r != ERROR_SUCCESS)
1259 return r;
1262 LIST_FOR_EACH_ENTRY( file, &package->files, MSIFILE, entry )
1264 MSIRECORD *uirow;
1265 VS_FIXEDFILEINFO *ver;
1267 comp = file->Component;
1268 msi_file_update_ui( package, file, szRemoveFiles );
1270 comp->Action = msi_get_component_action( package, comp );
1271 if (comp->Action != INSTALLSTATE_ABSENT || comp->Installed == INSTALLSTATE_SOURCE)
1272 continue;
1274 if (comp->assembly && !comp->assembly->application)
1275 continue;
1277 if (comp->Attributes & msidbComponentAttributesPermanent)
1279 TRACE("permanent component, not removing file\n");
1280 continue;
1283 if (file->Version)
1285 ver = msi_get_disk_file_version( file->TargetPath );
1286 if (ver && msi_compare_file_versions( ver, file->Version ) > 0)
1288 TRACE("newer version detected, not removing file\n");
1289 msi_free( ver );
1290 continue;
1292 msi_free( ver );
1295 if (file->state == msifs_installed)
1296 WARN("removing installed file %s\n", debugstr_w(file->TargetPath));
1298 TRACE("removing %s\n", debugstr_w(file->File) );
1300 SetFileAttributesW( file->TargetPath, FILE_ATTRIBUTE_NORMAL );
1301 if (!DeleteFileW( file->TargetPath ))
1303 WARN("failed to delete %s (%u)\n", debugstr_w(file->TargetPath), GetLastError());
1305 file->state = msifs_missing;
1307 uirow = MSI_CreateRecord( 9 );
1308 MSI_RecordSetStringW( uirow, 1, file->FileName );
1309 MSI_RecordSetStringW( uirow, 9, comp->Directory );
1310 msi_ui_actiondata( package, szRemoveFiles, uirow );
1311 msiobj_release( &uirow->hdr );
1314 LIST_FOR_EACH_ENTRY( comp, &package->components, MSICOMPONENT, entry )
1316 comp->Action = msi_get_component_action( package, comp );
1317 if (comp->Action != INSTALLSTATE_ABSENT) continue;
1319 if (comp->Attributes & msidbComponentAttributesPermanent)
1321 TRACE("permanent component, not removing directory\n");
1322 continue;
1324 if (comp->assembly && !comp->assembly->application)
1325 msi_uninstall_assembly( package, comp );
1326 else
1328 MSIFOLDER *folder = msi_get_loaded_folder( package, comp->Directory );
1329 remove_folder( folder );
1332 return ERROR_SUCCESS;