msi: Handle more than one patch per file.
[wine/multimedia.git] / dlls / msi / files.c
blob96584dfa5e1b90c73ba363c4ceafcfb5dad9cf6b
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))) return FALSE;
471 GetTempFileNameW(temp_folder, NULL, 0, patch_path);
473 *path = strdupW(patch_path);
474 *attrs = p->File->Attributes;
476 else if (action == MSICABEXTRACT_FILEEXTRACTED)
478 WCHAR patched_file[MAX_PATH];
479 BOOL br;
481 GetTempFileNameW(temp_folder, NULL, 0, patched_file);
483 br = ApplyPatchToFileW(patch_path, p->File->TargetPath, patched_file, 0);
484 if (br)
486 /* FIXME: baseline cache */
488 DeleteFileW( p->File->TargetPath );
489 MoveFileW( patched_file, p->File->TargetPath );
491 p->IsApplied = TRUE;
493 else
494 ERR("Failed patch %s: %d.\n", debugstr_w(p->File->TargetPath), GetLastError());
496 DeleteFileW(patch_path);
497 p = NULL;
500 return TRUE;
503 UINT ACTION_PatchFiles( MSIPACKAGE *package )
505 MSIFILEPATCH *patch;
506 MSIMEDIAINFO *mi;
507 UINT rc = ERROR_SUCCESS;
508 BOOL mspatcha_loaded = FALSE;
510 TRACE("%p\n", package);
512 mi = msi_alloc_zero( sizeof(MSIMEDIAINFO) );
514 LIST_FOR_EACH_ENTRY( patch, &package->filepatches, MSIFILEPATCH, entry )
516 MSIFILE *file = patch->File;
517 MSICOMPONENT *comp = file->Component;
519 rc = msi_load_media_info( package, patch->Sequence, mi );
520 if (rc != ERROR_SUCCESS)
522 ERR("Unable to load media info for %s (%u)\n", debugstr_w(file->File), rc);
523 rc = ERROR_FUNCTION_FAILED;
524 goto done;
526 comp->Action = msi_get_component_action( package, comp );
527 if (!comp->Enabled || comp->Action != INSTALLSTATE_LOCAL) continue;
529 if (!patch->IsApplied)
531 MSICABDATA data;
533 rc = ready_media( package, TRUE, mi );
534 if (rc != ERROR_SUCCESS)
536 ERR("Failed to ready media for %s\n", debugstr_w(file->File));
537 goto done;
540 if (!mspatcha_loaded && !load_mspatcha())
542 rc = ERROR_FUNCTION_FAILED;
543 goto done;
545 mspatcha_loaded = TRUE;
547 data.mi = mi;
548 data.package = package;
549 data.cb = patchfiles_cb;
550 data.user = (PVOID)(UINT_PTR)mi->disk_id;
552 if (!msi_cabextract(package, mi, &data))
554 ERR("Failed to extract cabinet: %s\n", debugstr_w(mi->cabinet));
555 rc = ERROR_INSTALL_FAILURE;
556 goto done;
560 if (!patch->IsApplied && !(patch->Attributes & msidbPatchAttributesNonVital))
562 ERR("Failed to apply patch to file: %s\n", debugstr_w(file->File));
563 rc = ERROR_INSTALL_FAILURE;
564 goto done;
568 done:
569 msi_free_media_info(mi);
570 if (mspatcha_loaded)
571 unload_mspatch();
572 return rc;
575 #define is_dot_dir(x) ((x[0] == '.') && ((x[1] == 0) || ((x[1] == '.') && (x[2] == 0))))
577 typedef struct
579 struct list entry;
580 LPWSTR sourcename;
581 LPWSTR destname;
582 LPWSTR source;
583 LPWSTR dest;
584 } FILE_LIST;
586 static BOOL msi_move_file(LPCWSTR source, LPCWSTR dest, int options)
588 BOOL ret;
590 if (GetFileAttributesW(source) == FILE_ATTRIBUTE_DIRECTORY ||
591 GetFileAttributesW(dest) == FILE_ATTRIBUTE_DIRECTORY)
593 WARN("Source or dest is directory, not moving\n");
594 return FALSE;
597 if (options == msidbMoveFileOptionsMove)
599 TRACE("moving %s -> %s\n", debugstr_w(source), debugstr_w(dest));
600 ret = MoveFileExW(source, dest, MOVEFILE_REPLACE_EXISTING);
601 if (!ret)
603 WARN("MoveFile failed: %d\n", GetLastError());
604 return FALSE;
607 else
609 TRACE("copying %s -> %s\n", debugstr_w(source), debugstr_w(dest));
610 ret = CopyFileW(source, dest, FALSE);
611 if (!ret)
613 WARN("CopyFile failed: %d\n", GetLastError());
614 return FALSE;
618 return TRUE;
621 static LPWSTR wildcard_to_file(LPWSTR wildcard, LPWSTR filename)
623 LPWSTR path, ptr;
624 DWORD dirlen, pathlen;
626 ptr = strrchrW(wildcard, '\\');
627 dirlen = ptr - wildcard + 1;
629 pathlen = dirlen + lstrlenW(filename) + 1;
630 path = msi_alloc(pathlen * sizeof(WCHAR));
632 lstrcpynW(path, wildcard, dirlen + 1);
633 lstrcatW(path, filename);
635 return path;
638 static void free_file_entry(FILE_LIST *file)
640 msi_free(file->source);
641 msi_free(file->dest);
642 msi_free(file);
645 static void free_list(FILE_LIST *list)
647 while (!list_empty(&list->entry))
649 FILE_LIST *file = LIST_ENTRY(list_head(&list->entry), FILE_LIST, entry);
651 list_remove(&file->entry);
652 free_file_entry(file);
656 static BOOL add_wildcard(FILE_LIST *files, LPWSTR source, LPWSTR dest)
658 FILE_LIST *new, *file;
659 LPWSTR ptr, filename;
660 DWORD size;
662 new = msi_alloc_zero(sizeof(FILE_LIST));
663 if (!new)
664 return FALSE;
666 new->source = strdupW(source);
667 ptr = strrchrW(dest, '\\') + 1;
668 filename = strrchrW(new->source, '\\') + 1;
670 new->sourcename = filename;
672 if (*ptr)
673 new->destname = ptr;
674 else
675 new->destname = new->sourcename;
677 size = (ptr - dest) + lstrlenW(filename) + 1;
678 new->dest = msi_alloc(size * sizeof(WCHAR));
679 if (!new->dest)
681 free_file_entry(new);
682 return FALSE;
685 lstrcpynW(new->dest, dest, ptr - dest + 1);
686 lstrcatW(new->dest, filename);
688 if (list_empty(&files->entry))
690 list_add_head(&files->entry, &new->entry);
691 return TRUE;
694 LIST_FOR_EACH_ENTRY(file, &files->entry, FILE_LIST, entry)
696 if (strcmpW( source, file->source ) < 0)
698 list_add_before(&file->entry, &new->entry);
699 return TRUE;
703 list_add_after(&file->entry, &new->entry);
704 return TRUE;
707 static BOOL move_files_wildcard(LPWSTR source, LPWSTR dest, int options)
709 WIN32_FIND_DATAW wfd;
710 HANDLE hfile;
711 LPWSTR path;
712 BOOL res;
713 FILE_LIST files, *file;
714 DWORD size;
716 hfile = FindFirstFileW(source, &wfd);
717 if (hfile == INVALID_HANDLE_VALUE) return FALSE;
719 list_init(&files.entry);
721 for (res = TRUE; res; res = FindNextFileW(hfile, &wfd))
723 if (is_dot_dir(wfd.cFileName)) continue;
725 path = wildcard_to_file(source, wfd.cFileName);
726 if (!path)
728 res = FALSE;
729 goto done;
732 add_wildcard(&files, path, dest);
733 msi_free(path);
736 /* no files match the wildcard */
737 if (list_empty(&files.entry))
738 goto done;
740 /* only the first wildcard match gets renamed to dest */
741 file = LIST_ENTRY(list_head(&files.entry), FILE_LIST, entry);
742 size = (strrchrW(file->dest, '\\') - file->dest) + lstrlenW(file->destname) + 2;
743 file->dest = msi_realloc(file->dest, size * sizeof(WCHAR));
744 if (!file->dest)
746 res = FALSE;
747 goto done;
750 /* file->dest may be shorter after the reallocation, so add a NULL
751 * terminator. This is needed for the call to strrchrW, as there will no
752 * longer be a NULL terminator within the bounds of the allocation in this case.
754 file->dest[size - 1] = '\0';
755 lstrcpyW(strrchrW(file->dest, '\\') + 1, file->destname);
757 while (!list_empty(&files.entry))
759 file = LIST_ENTRY(list_head(&files.entry), FILE_LIST, entry);
761 msi_move_file(file->source, file->dest, options);
763 list_remove(&file->entry);
764 free_file_entry(file);
767 res = TRUE;
769 done:
770 free_list(&files);
771 FindClose(hfile);
772 return res;
775 void msi_reduce_to_long_filename( WCHAR *filename )
777 WCHAR *p = strchrW( filename, '|' );
778 if (p) memmove( filename, p + 1, (strlenW( p + 1 ) + 1) * sizeof(WCHAR) );
781 static UINT ITERATE_MoveFiles( MSIRECORD *rec, LPVOID param )
783 MSIPACKAGE *package = param;
784 MSIRECORD *uirow;
785 MSICOMPONENT *comp;
786 LPCWSTR sourcename, component;
787 LPWSTR sourcedir, destname = NULL, destdir = NULL, source = NULL, dest = NULL;
788 int options;
789 DWORD size;
790 BOOL wildcards;
792 component = MSI_RecordGetString(rec, 2);
793 comp = msi_get_loaded_component(package, component);
794 if (!comp)
795 return ERROR_SUCCESS;
797 comp->Action = msi_get_component_action( package, comp );
798 if (comp->Action != INSTALLSTATE_LOCAL)
800 TRACE("component not scheduled for installation %s\n", debugstr_w(component));
801 return ERROR_SUCCESS;
804 sourcename = MSI_RecordGetString(rec, 3);
805 options = MSI_RecordGetInteger(rec, 7);
807 sourcedir = msi_dup_property(package->db, MSI_RecordGetString(rec, 5));
808 if (!sourcedir)
809 goto done;
811 destdir = msi_dup_property(package->db, MSI_RecordGetString(rec, 6));
812 if (!destdir)
813 goto done;
815 if (!sourcename)
817 if (GetFileAttributesW(sourcedir) == INVALID_FILE_ATTRIBUTES)
818 goto done;
820 source = strdupW(sourcedir);
821 if (!source)
822 goto done;
824 else
826 size = lstrlenW(sourcedir) + lstrlenW(sourcename) + 2;
827 source = msi_alloc(size * sizeof(WCHAR));
828 if (!source)
829 goto done;
831 lstrcpyW(source, sourcedir);
832 if (source[lstrlenW(source) - 1] != '\\')
833 lstrcatW(source, szBackSlash);
834 lstrcatW(source, sourcename);
837 wildcards = strchrW(source, '*') || strchrW(source, '?');
839 if (MSI_RecordIsNull(rec, 4))
841 if (!wildcards)
843 destname = strdupW(sourcename);
844 if (!destname)
845 goto done;
848 else
850 destname = strdupW(MSI_RecordGetString(rec, 4));
851 if (destname) msi_reduce_to_long_filename(destname);
854 size = 0;
855 if (destname)
856 size = lstrlenW(destname);
858 size += lstrlenW(destdir) + 2;
859 dest = msi_alloc(size * sizeof(WCHAR));
860 if (!dest)
861 goto done;
863 lstrcpyW(dest, destdir);
864 if (dest[lstrlenW(dest) - 1] != '\\')
865 lstrcatW(dest, szBackSlash);
867 if (destname)
868 lstrcatW(dest, destname);
870 if (GetFileAttributesW(destdir) == INVALID_FILE_ATTRIBUTES)
872 if (!msi_create_full_path(destdir))
874 WARN("failed to create directory %u\n", GetLastError());
875 goto done;
879 if (!wildcards)
880 msi_move_file(source, dest, options);
881 else
882 move_files_wildcard(source, dest, options);
884 done:
885 uirow = MSI_CreateRecord( 9 );
886 MSI_RecordSetStringW( uirow, 1, MSI_RecordGetString(rec, 1) );
887 MSI_RecordSetInteger( uirow, 6, 1 ); /* FIXME */
888 MSI_RecordSetStringW( uirow, 9, destdir );
889 msi_ui_actiondata( package, szMoveFiles, uirow );
890 msiobj_release( &uirow->hdr );
892 msi_free(sourcedir);
893 msi_free(destdir);
894 msi_free(destname);
895 msi_free(source);
896 msi_free(dest);
898 return ERROR_SUCCESS;
901 UINT ACTION_MoveFiles( MSIPACKAGE *package )
903 static const WCHAR query[] = {
904 'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
905 '`','M','o','v','e','F','i','l','e','`',0};
906 MSIQUERY *view;
907 UINT rc;
909 rc = MSI_DatabaseOpenViewW(package->db, query, &view);
910 if (rc != ERROR_SUCCESS)
911 return ERROR_SUCCESS;
913 rc = MSI_IterateRecords(view, NULL, ITERATE_MoveFiles, package);
914 msiobj_release(&view->hdr);
915 return rc;
918 static WCHAR *get_duplicate_filename( MSIPACKAGE *package, MSIRECORD *row, const WCHAR *file_key, const WCHAR *src )
920 DWORD len;
921 WCHAR *dst_name, *dst_path, *dst;
923 if (MSI_RecordIsNull( row, 4 ))
925 len = strlenW( src ) + 1;
926 if (!(dst_name = msi_alloc( len * sizeof(WCHAR)))) return NULL;
927 strcpyW( dst_name, strrchrW( src, '\\' ) + 1 );
929 else
931 MSI_RecordGetStringW( row, 4, NULL, &len );
932 if (!(dst_name = msi_alloc( ++len * sizeof(WCHAR) ))) return NULL;
933 MSI_RecordGetStringW( row, 4, dst_name, &len );
934 msi_reduce_to_long_filename( dst_name );
937 if (MSI_RecordIsNull( row, 5 ))
939 WCHAR *p;
940 dst_path = strdupW( src );
941 p = strrchrW( dst_path, '\\' );
942 if (p) *p = 0;
944 else
946 const WCHAR *dst_key = MSI_RecordGetString( row, 5 );
948 dst_path = strdupW( msi_get_target_folder( package, dst_key ) );
949 if (!dst_path)
951 /* try a property */
952 dst_path = msi_dup_property( package->db, dst_key );
953 if (!dst_path)
955 FIXME("Unable to get destination folder, try AppSearch properties\n");
956 msi_free( dst_name );
957 return NULL;
962 dst = msi_build_directory_name( 2, dst_path, dst_name );
963 msi_create_full_path( dst_path );
965 msi_free( dst_name );
966 msi_free( dst_path );
967 return dst;
970 static UINT ITERATE_DuplicateFiles(MSIRECORD *row, LPVOID param)
972 MSIPACKAGE *package = param;
973 LPWSTR dest;
974 LPCWSTR file_key, component;
975 MSICOMPONENT *comp;
976 MSIRECORD *uirow;
977 MSIFILE *file;
979 component = MSI_RecordGetString(row,2);
980 comp = msi_get_loaded_component(package, component);
981 if (!comp)
982 return ERROR_SUCCESS;
984 comp->Action = msi_get_component_action( package, comp );
985 if (comp->Action != INSTALLSTATE_LOCAL)
987 TRACE("component not scheduled for installation %s\n", debugstr_w(component));
988 return ERROR_SUCCESS;
991 file_key = MSI_RecordGetString(row,3);
992 if (!file_key)
994 ERR("Unable to get file key\n");
995 return ERROR_FUNCTION_FAILED;
998 file = msi_get_loaded_file( package, file_key );
999 if (!file)
1001 ERR("Original file unknown %s\n", debugstr_w(file_key));
1002 return ERROR_SUCCESS;
1005 dest = get_duplicate_filename( package, row, file_key, file->TargetPath );
1006 if (!dest)
1008 WARN("Unable to get duplicate filename\n");
1009 return ERROR_SUCCESS;
1012 TRACE("Duplicating file %s to %s\n", debugstr_w(file->TargetPath), debugstr_w(dest));
1014 if (!CopyFileW( file->TargetPath, dest, TRUE ))
1016 WARN("Failed to copy file %s -> %s (%u)\n",
1017 debugstr_w(file->TargetPath), debugstr_w(dest), GetLastError());
1020 FIXME("We should track these duplicate files as well\n");
1022 uirow = MSI_CreateRecord( 9 );
1023 MSI_RecordSetStringW( uirow, 1, MSI_RecordGetString( row, 1 ) );
1024 MSI_RecordSetInteger( uirow, 6, file->FileSize );
1025 MSI_RecordSetStringW( uirow, 9, MSI_RecordGetString( row, 5 ) );
1026 msi_ui_actiondata( package, szDuplicateFiles, uirow );
1027 msiobj_release( &uirow->hdr );
1029 msi_free(dest);
1030 return ERROR_SUCCESS;
1033 UINT ACTION_DuplicateFiles(MSIPACKAGE *package)
1035 static const WCHAR query[] = {
1036 'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
1037 '`','D','u','p','l','i','c','a','t','e','F','i','l','e','`',0};
1038 MSIQUERY *view;
1039 UINT rc;
1041 rc = MSI_DatabaseOpenViewW(package->db, query, &view);
1042 if (rc != ERROR_SUCCESS)
1043 return ERROR_SUCCESS;
1045 rc = MSI_IterateRecords(view, NULL, ITERATE_DuplicateFiles, package);
1046 msiobj_release(&view->hdr);
1047 return rc;
1050 static UINT ITERATE_RemoveDuplicateFiles( MSIRECORD *row, LPVOID param )
1052 MSIPACKAGE *package = param;
1053 LPWSTR dest;
1054 LPCWSTR file_key, component;
1055 MSICOMPONENT *comp;
1056 MSIRECORD *uirow;
1057 MSIFILE *file;
1059 component = MSI_RecordGetString( row, 2 );
1060 comp = msi_get_loaded_component( package, component );
1061 if (!comp)
1062 return ERROR_SUCCESS;
1064 comp->Action = msi_get_component_action( package, comp );
1065 if (comp->Action != INSTALLSTATE_ABSENT)
1067 TRACE("component not scheduled for removal %s\n", debugstr_w(component));
1068 return ERROR_SUCCESS;
1071 file_key = MSI_RecordGetString( row, 3 );
1072 if (!file_key)
1074 ERR("Unable to get file key\n");
1075 return ERROR_FUNCTION_FAILED;
1078 file = msi_get_loaded_file( package, file_key );
1079 if (!file)
1081 ERR("Original file unknown %s\n", debugstr_w(file_key));
1082 return ERROR_SUCCESS;
1085 dest = get_duplicate_filename( package, row, file_key, file->TargetPath );
1086 if (!dest)
1088 WARN("Unable to get duplicate filename\n");
1089 return ERROR_SUCCESS;
1092 TRACE("Removing duplicate %s of %s\n", debugstr_w(dest), debugstr_w(file->TargetPath));
1094 if (!DeleteFileW( dest ))
1096 WARN("Failed to delete duplicate file %s (%u)\n", debugstr_w(dest), GetLastError());
1099 uirow = MSI_CreateRecord( 9 );
1100 MSI_RecordSetStringW( uirow, 1, MSI_RecordGetString( row, 1 ) );
1101 MSI_RecordSetStringW( uirow, 9, MSI_RecordGetString( row, 5 ) );
1102 msi_ui_actiondata( package, szRemoveDuplicateFiles, uirow );
1103 msiobj_release( &uirow->hdr );
1105 msi_free(dest);
1106 return ERROR_SUCCESS;
1109 UINT ACTION_RemoveDuplicateFiles( MSIPACKAGE *package )
1111 static const WCHAR query[] = {
1112 'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
1113 '`','D','u','p','l','i','c','a','t','e','F','i','l','e','`',0};
1114 MSIQUERY *view;
1115 UINT rc;
1117 rc = MSI_DatabaseOpenViewW( package->db, query, &view );
1118 if (rc != ERROR_SUCCESS)
1119 return ERROR_SUCCESS;
1121 rc = MSI_IterateRecords( view, NULL, ITERATE_RemoveDuplicateFiles, package );
1122 msiobj_release( &view->hdr );
1123 return rc;
1126 static BOOL verify_comp_for_removal(MSICOMPONENT *comp, UINT install_mode)
1128 /* special case */
1129 if (comp->Action != INSTALLSTATE_SOURCE &&
1130 comp->Attributes & msidbComponentAttributesSourceOnly &&
1131 (install_mode == msidbRemoveFileInstallModeOnRemove ||
1132 install_mode == msidbRemoveFileInstallModeOnBoth)) return TRUE;
1134 switch (comp->Action)
1136 case INSTALLSTATE_LOCAL:
1137 case INSTALLSTATE_SOURCE:
1138 if (install_mode == msidbRemoveFileInstallModeOnInstall ||
1139 install_mode == msidbRemoveFileInstallModeOnBoth) return TRUE;
1140 break;
1141 case INSTALLSTATE_ABSENT:
1142 if (install_mode == msidbRemoveFileInstallModeOnRemove ||
1143 install_mode == msidbRemoveFileInstallModeOnBoth) return TRUE;
1144 break;
1145 default: break;
1147 return FALSE;
1150 static UINT ITERATE_RemoveFiles(MSIRECORD *row, LPVOID param)
1152 MSIPACKAGE *package = param;
1153 MSICOMPONENT *comp;
1154 MSIRECORD *uirow;
1155 LPCWSTR component, dirprop;
1156 UINT install_mode;
1157 LPWSTR dir = NULL, path = NULL, filename = NULL;
1158 DWORD size;
1159 UINT ret = ERROR_SUCCESS;
1161 component = MSI_RecordGetString(row, 2);
1162 dirprop = MSI_RecordGetString(row, 4);
1163 install_mode = MSI_RecordGetInteger(row, 5);
1165 comp = msi_get_loaded_component(package, component);
1166 if (!comp)
1167 return ERROR_SUCCESS;
1169 comp->Action = msi_get_component_action( package, comp );
1170 if (!verify_comp_for_removal(comp, install_mode))
1172 TRACE("Skipping removal due to install mode\n");
1173 return ERROR_SUCCESS;
1175 if (comp->assembly && !comp->assembly->application)
1177 return ERROR_SUCCESS;
1179 if (comp->Attributes & msidbComponentAttributesPermanent)
1181 TRACE("permanent component, not removing file\n");
1182 return ERROR_SUCCESS;
1185 dir = msi_dup_property(package->db, dirprop);
1186 if (!dir)
1188 WARN("directory property has no value\n");
1189 return ERROR_SUCCESS;
1191 size = 0;
1192 if ((filename = strdupW( MSI_RecordGetString(row, 3) )))
1194 msi_reduce_to_long_filename( filename );
1195 size = lstrlenW( filename );
1197 size += lstrlenW(dir) + 2;
1198 path = msi_alloc(size * sizeof(WCHAR));
1199 if (!path)
1201 ret = ERROR_OUTOFMEMORY;
1202 goto done;
1205 if (filename)
1207 lstrcpyW(path, dir);
1208 PathAddBackslashW(path);
1209 lstrcatW(path, filename);
1211 TRACE("Deleting misc file: %s\n", debugstr_w(path));
1212 DeleteFileW(path);
1214 else
1216 TRACE("Removing misc directory: %s\n", debugstr_w(dir));
1217 RemoveDirectoryW(dir);
1220 done:
1221 uirow = MSI_CreateRecord( 9 );
1222 MSI_RecordSetStringW( uirow, 1, MSI_RecordGetString(row, 1) );
1223 MSI_RecordSetStringW( uirow, 9, dir );
1224 msi_ui_actiondata( package, szRemoveFiles, uirow );
1225 msiobj_release( &uirow->hdr );
1227 msi_free(filename);
1228 msi_free(path);
1229 msi_free(dir);
1230 return ret;
1233 static void remove_folder( MSIFOLDER *folder )
1235 FolderList *fl;
1237 LIST_FOR_EACH_ENTRY( fl, &folder->children, FolderList, entry )
1239 remove_folder( fl->folder );
1241 if (!folder->persistent && folder->State != FOLDER_STATE_REMOVED)
1243 if (RemoveDirectoryW( folder->ResolvedTarget )) folder->State = FOLDER_STATE_REMOVED;
1247 UINT ACTION_RemoveFiles( MSIPACKAGE *package )
1249 static const WCHAR query[] = {
1250 'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
1251 '`','R','e','m','o','v','e','F','i','l','e','`',0};
1252 MSIQUERY *view;
1253 MSICOMPONENT *comp;
1254 MSIFILE *file;
1255 UINT r;
1257 r = MSI_DatabaseOpenViewW(package->db, query, &view);
1258 if (r == ERROR_SUCCESS)
1260 r = MSI_IterateRecords(view, NULL, ITERATE_RemoveFiles, package);
1261 msiobj_release(&view->hdr);
1262 if (r != ERROR_SUCCESS)
1263 return r;
1266 LIST_FOR_EACH_ENTRY( file, &package->files, MSIFILE, entry )
1268 MSIRECORD *uirow;
1269 VS_FIXEDFILEINFO *ver;
1271 comp = file->Component;
1272 msi_file_update_ui( package, file, szRemoveFiles );
1274 comp->Action = msi_get_component_action( package, comp );
1275 if (comp->Action != INSTALLSTATE_ABSENT || comp->Installed == INSTALLSTATE_SOURCE)
1276 continue;
1278 if (comp->assembly && !comp->assembly->application)
1279 continue;
1281 if (comp->Attributes & msidbComponentAttributesPermanent)
1283 TRACE("permanent component, not removing file\n");
1284 continue;
1287 if (file->Version)
1289 ver = msi_get_disk_file_version( file->TargetPath );
1290 if (ver && msi_compare_file_versions( ver, file->Version ) > 0)
1292 TRACE("newer version detected, not removing file\n");
1293 msi_free( ver );
1294 continue;
1296 msi_free( ver );
1299 if (file->state == msifs_installed)
1300 WARN("removing installed file %s\n", debugstr_w(file->TargetPath));
1302 TRACE("removing %s\n", debugstr_w(file->File) );
1304 SetFileAttributesW( file->TargetPath, FILE_ATTRIBUTE_NORMAL );
1305 if (!DeleteFileW( file->TargetPath ))
1307 WARN("failed to delete %s (%u)\n", debugstr_w(file->TargetPath), GetLastError());
1309 file->state = msifs_missing;
1311 uirow = MSI_CreateRecord( 9 );
1312 MSI_RecordSetStringW( uirow, 1, file->FileName );
1313 MSI_RecordSetStringW( uirow, 9, comp->Directory );
1314 msi_ui_actiondata( package, szRemoveFiles, uirow );
1315 msiobj_release( &uirow->hdr );
1318 LIST_FOR_EACH_ENTRY( comp, &package->components, MSICOMPONENT, entry )
1320 comp->Action = msi_get_component_action( package, comp );
1321 if (comp->Action != INSTALLSTATE_ABSENT) continue;
1323 if (comp->Attributes & msidbComponentAttributesPermanent)
1325 TRACE("permanent component, not removing directory\n");
1326 continue;
1328 if (comp->assembly && !comp->assembly->application)
1329 msi_uninstall_assembly( package, comp );
1330 else
1332 MSIFOLDER *folder = msi_get_loaded_folder( package, comp->Directory );
1333 remove_folder( folder );
1336 return ERROR_SUCCESS;