include: Use the hard-float calling convention for Windows APIs on ARM
[wine.git] / dlls / msi / files.c
blob02ff8ec28c017a129c0fd205c3759f481ac60877
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 #define COBJMACROS
37 #include "windef.h"
38 #include "winbase.h"
39 #include "winerror.h"
40 #include "wine/debug.h"
41 #include "fdi.h"
42 #include "msi.h"
43 #include "msidefs.h"
44 #include "msipriv.h"
45 #include "winuser.h"
46 #include "winreg.h"
47 #include "shlwapi.h"
48 #include "patchapi.h"
49 #include "wine/unicode.h"
51 WINE_DEFAULT_DEBUG_CHANNEL(msi);
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_ProcessMessage(package, INSTALLMESSAGE_ACTIONDATA, uirow);
62 msiobj_release( &uirow->hdr );
63 msi_ui_progress( package, 2, f->FileSize, 0, 0 );
66 static BOOL is_registered_patch_media( MSIPACKAGE *package, UINT disk_id )
68 MSIPATCHINFO *patch;
70 LIST_FOR_EACH_ENTRY( patch, &package->patches, MSIPATCHINFO, entry )
72 if (patch->disk_id == disk_id && patch->registered) return TRUE;
74 return FALSE;
77 static BOOL is_obsoleted_by_patch( MSIPACKAGE *package, MSIFILE *file )
79 if (!list_empty( &package->patches ) && file->disk_id < MSI_INITIAL_MEDIA_TRANSFORM_DISKID)
81 if (!msi_get_property_int( package->db, szInstalled, 0 )) return FALSE;
82 return TRUE;
84 if (is_registered_patch_media( package, file->disk_id )) return TRUE;
85 return FALSE;
88 static msi_file_state calculate_install_state( MSIPACKAGE *package, MSIFILE *file )
90 MSICOMPONENT *comp = file->Component;
91 VS_FIXEDFILEINFO *file_version;
92 WCHAR *font_version;
93 msi_file_state state;
94 DWORD size;
96 comp->Action = msi_get_component_action( package, comp );
97 if (!comp->Enabled || comp->Action != INSTALLSTATE_LOCAL || (comp->assembly && comp->assembly->installed))
99 TRACE("skipping %s (not scheduled for install)\n", debugstr_w(file->File));
100 return msifs_skipped;
102 if (is_obsoleted_by_patch( package, file ))
104 TRACE("skipping %s (obsoleted by patch)\n", debugstr_w(file->File));
105 return msifs_skipped;
107 if ((msi_is_global_assembly( comp ) && !comp->assembly->installed) ||
108 GetFileAttributesW( file->TargetPath ) == INVALID_FILE_ATTRIBUTES)
110 TRACE("installing %s (missing)\n", debugstr_w(file->File));
111 return msifs_missing;
113 if (file->Version)
115 if ((file_version = msi_get_disk_file_version( file->TargetPath )))
117 if (msi_compare_file_versions( file_version, file->Version ) < 0)
119 TRACE("overwriting %s (new version %s old version %u.%u.%u.%u)\n",
120 debugstr_w(file->File), debugstr_w(file->Version),
121 HIWORD(file_version->dwFileVersionMS), LOWORD(file_version->dwFileVersionMS),
122 HIWORD(file_version->dwFileVersionLS), LOWORD(file_version->dwFileVersionLS));
123 state = msifs_overwrite;
125 else
127 TRACE("keeping %s (new version %s old version %u.%u.%u.%u)\n",
128 debugstr_w(file->File), debugstr_w(file->Version),
129 HIWORD(file_version->dwFileVersionMS), LOWORD(file_version->dwFileVersionMS),
130 HIWORD(file_version->dwFileVersionLS), LOWORD(file_version->dwFileVersionLS));
131 state = msifs_present;
133 msi_free( file_version );
134 return state;
136 else if ((font_version = msi_font_version_from_file( file->TargetPath )))
138 if (msi_compare_font_versions( font_version, file->Version ) < 0)
140 TRACE("overwriting %s (new version %s old version %s)\n",
141 debugstr_w(file->File), debugstr_w(file->Version), debugstr_w(font_version));
142 state = msifs_overwrite;
144 else
146 TRACE("keeping %s (new version %s old version %s)\n",
147 debugstr_w(file->File), debugstr_w(file->Version), debugstr_w(font_version));
148 state = msifs_present;
150 msi_free( font_version );
151 return state;
154 if ((size = msi_get_disk_file_size( file->TargetPath )) != file->FileSize)
156 TRACE("overwriting %s (old size %u new size %u)\n", debugstr_w(file->File), size, file->FileSize);
157 return msifs_overwrite;
159 if (file->hash.dwFileHashInfoSize)
161 if (msi_file_hash_matches( file ))
163 TRACE("keeping %s (hash match)\n", debugstr_w(file->File));
164 return msifs_hashmatch;
166 else
168 TRACE("overwriting %s (hash mismatch)\n", debugstr_w(file->File));
169 return msifs_overwrite;
172 /* assume present */
173 TRACE("keeping %s\n", debugstr_w(file->File));
174 return msifs_present;
177 static void schedule_install_files(MSIPACKAGE *package)
179 MSIFILE *file;
181 LIST_FOR_EACH_ENTRY(file, &package->files, MSIFILE, entry)
183 MSICOMPONENT *comp = file->Component;
185 file->state = calculate_install_state( package, file );
186 if (file->state == msifs_overwrite && (comp->Attributes & msidbComponentAttributesNeverOverwrite))
188 TRACE("not overwriting %s\n", debugstr_w(file->TargetPath));
189 file->state = msifs_skipped;
194 static UINT copy_file(MSIFILE *file, LPWSTR source)
196 BOOL ret;
198 ret = CopyFileW(source, file->TargetPath, FALSE);
199 if (!ret)
200 return GetLastError();
202 SetFileAttributesW(file->TargetPath, FILE_ATTRIBUTE_NORMAL);
204 file->state = msifs_installed;
205 return ERROR_SUCCESS;
208 static UINT copy_install_file(MSIPACKAGE *package, MSIFILE *file, LPWSTR source)
210 UINT gle;
212 TRACE("Copying %s to %s\n", debugstr_w(source), debugstr_w(file->TargetPath));
214 gle = copy_file(file, source);
215 if (gle == ERROR_SUCCESS)
216 return gle;
218 if (gle == ERROR_ALREADY_EXISTS && file->state == msifs_overwrite)
220 TRACE("overwriting existing file\n");
221 return ERROR_SUCCESS;
223 else if (gle == ERROR_ACCESS_DENIED)
225 SetFileAttributesW(file->TargetPath, FILE_ATTRIBUTE_NORMAL);
227 gle = copy_file(file, source);
228 TRACE("Overwriting existing file: %d\n", gle);
230 if (gle == ERROR_SHARING_VIOLATION || gle == ERROR_USER_MAPPED_FILE)
232 WCHAR *tmpfileW, *pathW, *p;
233 DWORD len;
235 TRACE("file in use, scheduling rename operation\n");
237 if (!(pathW = strdupW( file->TargetPath ))) return ERROR_OUTOFMEMORY;
238 if ((p = strrchrW(pathW, '\\'))) *p = 0;
239 len = strlenW( pathW ) + 16;
240 if (!(tmpfileW = msi_alloc(len * sizeof(WCHAR))))
242 msi_free( pathW );
243 return ERROR_OUTOFMEMORY;
245 if (!GetTempFileNameW( pathW, szMsi, 0, tmpfileW )) tmpfileW[0] = 0;
246 msi_free( pathW );
248 if (CopyFileW(source, tmpfileW, FALSE) &&
249 MoveFileExW(file->TargetPath, NULL, MOVEFILE_DELAY_UNTIL_REBOOT) &&
250 MoveFileExW(tmpfileW, file->TargetPath, MOVEFILE_DELAY_UNTIL_REBOOT))
252 file->state = msifs_installed;
253 package->need_reboot_at_end = 1;
254 gle = ERROR_SUCCESS;
256 else
258 gle = GetLastError();
259 WARN("failed to schedule rename operation: %d)\n", gle);
260 DeleteFileW( tmpfileW );
262 msi_free(tmpfileW);
265 return gle;
268 static UINT msi_create_directory( MSIPACKAGE *package, const WCHAR *dir )
270 MSIFOLDER *folder;
271 const WCHAR *install_path;
273 install_path = msi_get_target_folder( package, dir );
274 if (!install_path) return ERROR_FUNCTION_FAILED;
276 folder = msi_get_loaded_folder( package, dir );
277 if (folder->State == FOLDER_STATE_UNINITIALIZED)
279 msi_create_full_path( install_path );
280 folder->State = FOLDER_STATE_CREATED;
282 return ERROR_SUCCESS;
285 static MSIFILE *find_file( MSIPACKAGE *package, UINT disk_id, const WCHAR *filename )
287 MSIFILE *file;
289 LIST_FOR_EACH_ENTRY( file, &package->files, MSIFILE, entry )
291 if (file->disk_id == disk_id &&
292 file->state != msifs_installed &&
293 !strcmpiW( filename, file->File )) return file;
295 return NULL;
298 static BOOL installfiles_cb(MSIPACKAGE *package, LPCWSTR filename, DWORD action,
299 LPWSTR *path, DWORD *attrs, PVOID user)
301 MSIFILE *file = *(MSIFILE **)user;
303 if (action == MSICABEXTRACT_BEGINEXTRACT)
305 if (!(file = find_file( package, file->disk_id, filename )))
307 TRACE("unknown file in cabinet (%s)\n", debugstr_w(filename));
308 return FALSE;
310 if (file->state != msifs_missing && file->state != msifs_overwrite)
311 return FALSE;
313 if (!msi_is_global_assembly( file->Component ))
315 msi_create_directory( package, file->Component->Directory );
317 *path = strdupW( file->TargetPath );
318 *attrs = file->Attributes;
319 *(MSIFILE **)user = file;
321 else if (action == MSICABEXTRACT_FILEEXTRACTED)
323 if (!msi_is_global_assembly( file->Component )) file->state = msifs_installed;
326 return TRUE;
329 WCHAR *msi_resolve_file_source( MSIPACKAGE *package, MSIFILE *file )
331 WCHAR *p, *path;
333 TRACE("Working to resolve source of file %s\n", debugstr_w(file->File));
335 if (file->IsCompressed) return NULL;
337 p = msi_resolve_source_folder( package, file->Component->Directory, NULL );
338 path = msi_build_directory_name( 2, p, file->ShortName );
340 if (file->LongName && GetFileAttributesW( path ) == INVALID_FILE_ATTRIBUTES)
342 msi_free( path );
343 path = msi_build_directory_name( 2, p, file->LongName );
345 msi_free( p );
346 TRACE("file %s source resolves to %s\n", debugstr_w(file->File), debugstr_w(path));
347 return path;
351 * ACTION_InstallFiles()
353 * For efficiency, this is done in two passes:
354 * 1) Correct all the TargetPaths and determine what files are to be installed.
355 * 2) Extract Cabinets and copy files.
357 UINT ACTION_InstallFiles(MSIPACKAGE *package)
359 MSIMEDIAINFO *mi;
360 UINT rc = ERROR_SUCCESS;
361 MSIFILE *file;
363 schedule_install_files(package);
364 mi = msi_alloc_zero( sizeof(MSIMEDIAINFO) );
366 LIST_FOR_EACH_ENTRY( file, &package->files, MSIFILE, entry )
368 msi_file_update_ui( package, file, szInstallFiles );
370 rc = msi_load_media_info( package, file->Sequence, mi );
371 if (rc != ERROR_SUCCESS)
373 ERR("Unable to load media info for %s (%u)\n", debugstr_w(file->File), rc);
374 rc = ERROR_FUNCTION_FAILED;
375 goto done;
378 if (file->state != msifs_hashmatch &&
379 file->state != msifs_skipped &&
380 (file->state != msifs_present || !msi_get_property_int( package->db, szInstalled, 0 )) &&
381 (rc = ready_media( package, file->IsCompressed, mi )))
383 ERR("Failed to ready media for %s\n", debugstr_w(file->File));
384 goto done;
387 if (file->state != msifs_missing && !mi->is_continuous && file->state != msifs_overwrite)
388 continue;
390 if (file->Sequence > mi->last_sequence || mi->is_continuous ||
391 (file->IsCompressed && !mi->is_extracted))
393 MSICABDATA data;
394 MSIFILE *cursor = file;
396 data.mi = mi;
397 data.package = package;
398 data.cb = installfiles_cb;
399 data.user = &cursor;
401 if (file->IsCompressed && !msi_cabextract(package, mi, &data))
403 ERR("Failed to extract cabinet: %s\n", debugstr_w(mi->cabinet));
404 rc = ERROR_INSTALL_FAILURE;
405 goto done;
409 if (!file->IsCompressed)
411 WCHAR *source = msi_resolve_file_source(package, file);
413 TRACE("copying %s to %s\n", debugstr_w(source), debugstr_w(file->TargetPath));
415 if (!msi_is_global_assembly( file->Component ))
417 msi_create_directory(package, file->Component->Directory);
419 rc = copy_install_file(package, file, source);
420 if (rc != ERROR_SUCCESS)
422 ERR("Failed to copy %s to %s (%u)\n", debugstr_w(source), debugstr_w(file->TargetPath), rc);
423 rc = ERROR_INSTALL_FAILURE;
424 msi_free(source);
425 goto done;
427 msi_free(source);
429 else if (!msi_is_global_assembly( file->Component ) &&
430 file->state != msifs_installed && !(file->Attributes & msidbFileAttributesPatchAdded))
432 ERR("compressed file wasn't installed (%s)\n", debugstr_w(file->File));
433 rc = ERROR_INSTALL_FAILURE;
434 goto done;
437 LIST_FOR_EACH_ENTRY( file, &package->files, MSIFILE, entry )
439 MSICOMPONENT *comp = file->Component;
441 if (!msi_is_global_assembly( comp ) || comp->assembly->installed ||
442 (file->state != msifs_missing && file->state != msifs_overwrite)) continue;
444 rc = msi_install_assembly( package, comp );
445 if (rc != ERROR_SUCCESS)
447 ERR("Failed to install assembly\n");
448 rc = ERROR_INSTALL_FAILURE;
449 break;
451 file->state = msifs_installed;
454 done:
455 msi_free_media_info(mi);
456 return rc;
459 static MSIFILEPATCH *find_filepatch( MSIPACKAGE *package, UINT disk_id, const WCHAR *key )
461 MSIFILEPATCH *patch;
463 LIST_FOR_EACH_ENTRY( patch, &package->filepatches, MSIFILEPATCH, entry )
465 if (!patch->extracted && patch->disk_id == disk_id && !strcmpW( key, patch->File->File ))
466 return patch;
468 return NULL;
471 static BOOL patchfiles_cb(MSIPACKAGE *package, LPCWSTR file, DWORD action,
472 LPWSTR *path, DWORD *attrs, PVOID user)
474 MSIFILEPATCH *patch = *(MSIFILEPATCH **)user;
476 if (action == MSICABEXTRACT_BEGINEXTRACT)
478 MSICOMPONENT *comp;
480 if (is_registered_patch_media( package, patch->disk_id ) ||
481 !(patch = find_filepatch( package, patch->disk_id, file ))) return FALSE;
483 comp = patch->File->Component;
484 comp->Action = msi_get_component_action( package, comp );
485 if (!comp->Enabled || comp->Action != INSTALLSTATE_LOCAL)
487 TRACE("file %s component %s not installed or disabled\n",
488 debugstr_w(patch->File->File), debugstr_w(comp->Component));
489 return FALSE;
492 patch->path = msi_create_temp_file( package->db );
493 *path = strdupW( patch->path );
494 *attrs = patch->File->Attributes;
495 *(MSIFILEPATCH **)user = patch;
497 else if (action == MSICABEXTRACT_FILEEXTRACTED)
499 patch->extracted = TRUE;
502 return TRUE;
505 static UINT patch_file( MSIPACKAGE *package, MSIFILEPATCH *patch )
507 UINT r = ERROR_SUCCESS;
508 WCHAR *tmpfile = msi_create_temp_file( package->db );
510 if (!tmpfile) return ERROR_INSTALL_FAILURE;
511 if (ApplyPatchToFileW( patch->path, patch->File->TargetPath, tmpfile, 0 ))
513 DeleteFileW( patch->File->TargetPath );
514 MoveFileW( tmpfile, patch->File->TargetPath );
516 else
518 WARN("failed to patch %s: %08x\n", debugstr_w(patch->File->TargetPath), GetLastError());
519 r = ERROR_INSTALL_FAILURE;
521 DeleteFileW( patch->path );
522 DeleteFileW( tmpfile );
523 msi_free( tmpfile );
524 return r;
527 static UINT patch_assembly( MSIPACKAGE *package, MSIASSEMBLY *assembly, MSIFILEPATCH *patch )
529 UINT r = ERROR_FUNCTION_FAILED;
530 IAssemblyName *name;
531 IAssemblyEnum *iter;
533 if (!(iter = msi_create_assembly_enum( package, assembly->display_name )))
534 return ERROR_FUNCTION_FAILED;
536 while ((IAssemblyEnum_GetNextAssembly( iter, NULL, &name, 0 ) == S_OK))
538 WCHAR *displayname, *path;
539 UINT len = 0;
540 HRESULT hr;
542 hr = IAssemblyName_GetDisplayName( name, NULL, &len, 0 );
543 if (hr != E_NOT_SUFFICIENT_BUFFER || !(displayname = msi_alloc( len * sizeof(WCHAR) )))
544 break;
546 hr = IAssemblyName_GetDisplayName( name, displayname, &len, 0 );
547 if (FAILED( hr ))
549 msi_free( displayname );
550 break;
553 if ((path = msi_get_assembly_path( package, displayname )))
555 if (!CopyFileW( path, patch->File->TargetPath, FALSE ))
557 ERR("Failed to copy file %s -> %s (%u)\n", debugstr_w(path),
558 debugstr_w(patch->File->TargetPath), GetLastError() );
559 msi_free( path );
560 msi_free( displayname );
561 IAssemblyName_Release( name );
562 break;
564 r = patch_file( package, patch );
565 msi_free( path );
568 msi_free( displayname );
569 IAssemblyName_Release( name );
570 if (r == ERROR_SUCCESS) break;
573 IAssemblyEnum_Release( iter );
574 return r;
577 UINT ACTION_PatchFiles( MSIPACKAGE *package )
579 MSIFILEPATCH *patch;
580 MSIMEDIAINFO *mi;
581 UINT rc = ERROR_SUCCESS;
583 TRACE("%p\n", package);
585 mi = msi_alloc_zero( sizeof(MSIMEDIAINFO) );
587 TRACE("extracting files\n");
589 LIST_FOR_EACH_ENTRY( patch, &package->filepatches, MSIFILEPATCH, entry )
591 MSIFILE *file = patch->File;
592 MSICOMPONENT *comp = file->Component;
594 rc = msi_load_media_info( package, patch->Sequence, mi );
595 if (rc != ERROR_SUCCESS)
597 ERR("Unable to load media info for %s (%u)\n", debugstr_w(file->File), rc);
598 rc = ERROR_FUNCTION_FAILED;
599 goto done;
601 comp->Action = msi_get_component_action( package, comp );
602 if (!comp->Enabled || comp->Action != INSTALLSTATE_LOCAL) continue;
604 if (!patch->extracted)
606 MSICABDATA data;
607 MSIFILEPATCH *cursor = patch;
609 rc = ready_media( package, TRUE, mi );
610 if (rc != ERROR_SUCCESS)
612 ERR("Failed to ready media for %s\n", debugstr_w(file->File));
613 goto done;
615 data.mi = mi;
616 data.package = package;
617 data.cb = patchfiles_cb;
618 data.user = &cursor;
620 if (!msi_cabextract( package, mi, &data ))
622 ERR("Failed to extract cabinet: %s\n", debugstr_w(mi->cabinet));
623 rc = ERROR_INSTALL_FAILURE;
624 goto done;
629 TRACE("applying patches\n");
631 LIST_FOR_EACH_ENTRY( patch, &package->filepatches, MSIFILEPATCH, entry )
633 MSICOMPONENT *comp = patch->File->Component;
635 if (!patch->path) continue;
637 if (msi_is_global_assembly( comp ))
638 rc = patch_assembly( package, comp->assembly, patch );
639 else
640 rc = patch_file( package, patch );
642 if (rc && !(patch->Attributes & msidbPatchAttributesNonVital))
644 ERR("Failed to apply patch to file: %s\n", debugstr_w(patch->File->File));
645 break;
648 if (msi_is_global_assembly( comp ))
650 if ((rc = msi_install_assembly( package, comp )))
652 ERR("Failed to install patched assembly\n");
653 break;
658 done:
659 msi_free_media_info(mi);
660 return rc;
663 #define is_dot_dir(x) ((x[0] == '.') && ((x[1] == 0) || ((x[1] == '.') && (x[2] == 0))))
665 typedef struct
667 struct list entry;
668 LPWSTR sourcename;
669 LPWSTR destname;
670 LPWSTR source;
671 LPWSTR dest;
672 } FILE_LIST;
674 static BOOL msi_move_file(LPCWSTR source, LPCWSTR dest, int options)
676 BOOL ret;
678 if (GetFileAttributesW(source) == FILE_ATTRIBUTE_DIRECTORY ||
679 GetFileAttributesW(dest) == FILE_ATTRIBUTE_DIRECTORY)
681 WARN("Source or dest is directory, not moving\n");
682 return FALSE;
685 if (options == msidbMoveFileOptionsMove)
687 TRACE("moving %s -> %s\n", debugstr_w(source), debugstr_w(dest));
688 ret = MoveFileExW(source, dest, MOVEFILE_REPLACE_EXISTING);
689 if (!ret)
691 WARN("MoveFile failed: %d\n", GetLastError());
692 return FALSE;
695 else
697 TRACE("copying %s -> %s\n", debugstr_w(source), debugstr_w(dest));
698 ret = CopyFileW(source, dest, FALSE);
699 if (!ret)
701 WARN("CopyFile failed: %d\n", GetLastError());
702 return FALSE;
706 return TRUE;
709 static LPWSTR wildcard_to_file(LPWSTR wildcard, LPWSTR filename)
711 LPWSTR path, ptr;
712 DWORD dirlen, pathlen;
714 ptr = strrchrW(wildcard, '\\');
715 dirlen = ptr - wildcard + 1;
717 pathlen = dirlen + lstrlenW(filename) + 1;
718 path = msi_alloc(pathlen * sizeof(WCHAR));
720 lstrcpynW(path, wildcard, dirlen + 1);
721 lstrcatW(path, filename);
723 return path;
726 static void free_file_entry(FILE_LIST *file)
728 msi_free(file->source);
729 msi_free(file->dest);
730 msi_free(file);
733 static void free_list(FILE_LIST *list)
735 while (!list_empty(&list->entry))
737 FILE_LIST *file = LIST_ENTRY(list_head(&list->entry), FILE_LIST, entry);
739 list_remove(&file->entry);
740 free_file_entry(file);
744 static BOOL add_wildcard(FILE_LIST *files, LPWSTR source, LPWSTR dest)
746 FILE_LIST *new, *file;
747 LPWSTR ptr, filename;
748 DWORD size;
750 new = msi_alloc_zero(sizeof(FILE_LIST));
751 if (!new)
752 return FALSE;
754 new->source = strdupW(source);
755 ptr = strrchrW(dest, '\\') + 1;
756 filename = strrchrW(new->source, '\\') + 1;
758 new->sourcename = filename;
760 if (*ptr)
761 new->destname = ptr;
762 else
763 new->destname = new->sourcename;
765 size = (ptr - dest) + lstrlenW(filename) + 1;
766 new->dest = msi_alloc(size * sizeof(WCHAR));
767 if (!new->dest)
769 free_file_entry(new);
770 return FALSE;
773 lstrcpynW(new->dest, dest, ptr - dest + 1);
774 lstrcatW(new->dest, filename);
776 if (list_empty(&files->entry))
778 list_add_head(&files->entry, &new->entry);
779 return TRUE;
782 LIST_FOR_EACH_ENTRY(file, &files->entry, FILE_LIST, entry)
784 if (strcmpW( source, file->source ) < 0)
786 list_add_before(&file->entry, &new->entry);
787 return TRUE;
791 list_add_after(&file->entry, &new->entry);
792 return TRUE;
795 static BOOL move_files_wildcard(LPWSTR source, LPWSTR dest, int options)
797 WIN32_FIND_DATAW wfd;
798 HANDLE hfile;
799 LPWSTR path;
800 BOOL res;
801 FILE_LIST files, *file;
802 DWORD size;
804 hfile = FindFirstFileW(source, &wfd);
805 if (hfile == INVALID_HANDLE_VALUE) return FALSE;
807 list_init(&files.entry);
809 for (res = TRUE; res; res = FindNextFileW(hfile, &wfd))
811 if (is_dot_dir(wfd.cFileName)) continue;
813 path = wildcard_to_file(source, wfd.cFileName);
814 if (!path)
816 res = FALSE;
817 goto done;
820 add_wildcard(&files, path, dest);
821 msi_free(path);
824 /* no files match the wildcard */
825 if (list_empty(&files.entry))
826 goto done;
828 /* only the first wildcard match gets renamed to dest */
829 file = LIST_ENTRY(list_head(&files.entry), FILE_LIST, entry);
830 size = (strrchrW(file->dest, '\\') - file->dest) + lstrlenW(file->destname) + 2;
831 file->dest = msi_realloc(file->dest, size * sizeof(WCHAR));
832 if (!file->dest)
834 res = FALSE;
835 goto done;
838 /* file->dest may be shorter after the reallocation, so add a NULL
839 * terminator. This is needed for the call to strrchrW, as there will no
840 * longer be a NULL terminator within the bounds of the allocation in this case.
842 file->dest[size - 1] = '\0';
843 lstrcpyW(strrchrW(file->dest, '\\') + 1, file->destname);
845 while (!list_empty(&files.entry))
847 file = LIST_ENTRY(list_head(&files.entry), FILE_LIST, entry);
849 msi_move_file(file->source, file->dest, options);
851 list_remove(&file->entry);
852 free_file_entry(file);
855 res = TRUE;
857 done:
858 free_list(&files);
859 FindClose(hfile);
860 return res;
863 void msi_reduce_to_long_filename( WCHAR *filename )
865 WCHAR *p = strchrW( filename, '|' );
866 if (p) memmove( filename, p + 1, (strlenW( p + 1 ) + 1) * sizeof(WCHAR) );
869 static UINT ITERATE_MoveFiles( MSIRECORD *rec, LPVOID param )
871 MSIPACKAGE *package = param;
872 MSIRECORD *uirow;
873 MSICOMPONENT *comp;
874 LPCWSTR sourcename, component;
875 LPWSTR sourcedir, destname = NULL, destdir = NULL, source = NULL, dest = NULL;
876 int options;
877 DWORD size;
878 BOOL wildcards;
880 component = MSI_RecordGetString(rec, 2);
881 comp = msi_get_loaded_component(package, component);
882 if (!comp)
883 return ERROR_SUCCESS;
885 comp->Action = msi_get_component_action( package, comp );
886 if (comp->Action != INSTALLSTATE_LOCAL)
888 TRACE("component not scheduled for installation %s\n", debugstr_w(component));
889 return ERROR_SUCCESS;
892 sourcename = MSI_RecordGetString(rec, 3);
893 options = MSI_RecordGetInteger(rec, 7);
895 sourcedir = msi_dup_property(package->db, MSI_RecordGetString(rec, 5));
896 if (!sourcedir)
897 goto done;
899 destdir = msi_dup_property(package->db, MSI_RecordGetString(rec, 6));
900 if (!destdir)
901 goto done;
903 if (!sourcename)
905 if (GetFileAttributesW(sourcedir) == INVALID_FILE_ATTRIBUTES)
906 goto done;
908 source = strdupW(sourcedir);
909 if (!source)
910 goto done;
912 else
914 size = lstrlenW(sourcedir) + lstrlenW(sourcename) + 2;
915 source = msi_alloc(size * sizeof(WCHAR));
916 if (!source)
917 goto done;
919 lstrcpyW(source, sourcedir);
920 if (source[lstrlenW(source) - 1] != '\\')
921 lstrcatW(source, szBackSlash);
922 lstrcatW(source, sourcename);
925 wildcards = strchrW(source, '*') || strchrW(source, '?');
927 if (MSI_RecordIsNull(rec, 4))
929 if (!wildcards)
931 WCHAR *p;
932 if (sourcename)
933 destname = strdupW(sourcename);
934 else if ((p = strrchrW(sourcedir, '\\')))
935 destname = strdupW(p + 1);
936 else
937 destname = strdupW(sourcedir);
938 if (!destname)
939 goto done;
942 else
944 destname = strdupW(MSI_RecordGetString(rec, 4));
945 if (destname) msi_reduce_to_long_filename(destname);
948 size = 0;
949 if (destname)
950 size = lstrlenW(destname);
952 size += lstrlenW(destdir) + 2;
953 dest = msi_alloc(size * sizeof(WCHAR));
954 if (!dest)
955 goto done;
957 lstrcpyW(dest, destdir);
958 if (dest[lstrlenW(dest) - 1] != '\\')
959 lstrcatW(dest, szBackSlash);
961 if (destname)
962 lstrcatW(dest, destname);
964 if (GetFileAttributesW(destdir) == INVALID_FILE_ATTRIBUTES)
966 if (!msi_create_full_path(destdir))
968 WARN("failed to create directory %u\n", GetLastError());
969 goto done;
973 if (!wildcards)
974 msi_move_file(source, dest, options);
975 else
976 move_files_wildcard(source, dest, options);
978 done:
979 uirow = MSI_CreateRecord( 9 );
980 MSI_RecordSetStringW( uirow, 1, MSI_RecordGetString(rec, 1) );
981 MSI_RecordSetInteger( uirow, 6, 1 ); /* FIXME */
982 MSI_RecordSetStringW( uirow, 9, destdir );
983 MSI_ProcessMessage(package, INSTALLMESSAGE_ACTIONDATA, uirow);
984 msiobj_release( &uirow->hdr );
986 msi_free(sourcedir);
987 msi_free(destdir);
988 msi_free(destname);
989 msi_free(source);
990 msi_free(dest);
992 return ERROR_SUCCESS;
995 UINT ACTION_MoveFiles( MSIPACKAGE *package )
997 static const WCHAR query[] = {
998 'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
999 '`','M','o','v','e','F','i','l','e','`',0};
1000 MSIQUERY *view;
1001 UINT rc;
1003 rc = MSI_DatabaseOpenViewW(package->db, query, &view);
1004 if (rc != ERROR_SUCCESS)
1005 return ERROR_SUCCESS;
1007 rc = MSI_IterateRecords(view, NULL, ITERATE_MoveFiles, package);
1008 msiobj_release(&view->hdr);
1009 return rc;
1012 static WCHAR *get_duplicate_filename( MSIPACKAGE *package, MSIRECORD *row, const WCHAR *file_key, const WCHAR *src )
1014 DWORD len;
1015 WCHAR *dst_name, *dst_path, *dst;
1017 if (MSI_RecordIsNull( row, 4 ))
1019 len = strlenW( src ) + 1;
1020 if (!(dst_name = msi_alloc( len * sizeof(WCHAR)))) return NULL;
1021 strcpyW( dst_name, strrchrW( src, '\\' ) + 1 );
1023 else
1025 MSI_RecordGetStringW( row, 4, NULL, &len );
1026 if (!(dst_name = msi_alloc( ++len * sizeof(WCHAR) ))) return NULL;
1027 MSI_RecordGetStringW( row, 4, dst_name, &len );
1028 msi_reduce_to_long_filename( dst_name );
1031 if (MSI_RecordIsNull( row, 5 ))
1033 WCHAR *p;
1034 dst_path = strdupW( src );
1035 p = strrchrW( dst_path, '\\' );
1036 if (p) *p = 0;
1038 else
1040 const WCHAR *dst_key = MSI_RecordGetString( row, 5 );
1042 dst_path = strdupW( msi_get_target_folder( package, dst_key ) );
1043 if (!dst_path)
1045 /* try a property */
1046 dst_path = msi_dup_property( package->db, dst_key );
1047 if (!dst_path)
1049 FIXME("Unable to get destination folder, try AppSearch properties\n");
1050 msi_free( dst_name );
1051 return NULL;
1056 dst = msi_build_directory_name( 2, dst_path, dst_name );
1057 msi_create_full_path( dst_path );
1059 msi_free( dst_name );
1060 msi_free( dst_path );
1061 return dst;
1064 static UINT ITERATE_DuplicateFiles(MSIRECORD *row, LPVOID param)
1066 MSIPACKAGE *package = param;
1067 LPWSTR dest;
1068 LPCWSTR file_key, component;
1069 MSICOMPONENT *comp;
1070 MSIRECORD *uirow;
1071 MSIFILE *file;
1073 component = MSI_RecordGetString(row,2);
1074 comp = msi_get_loaded_component(package, component);
1075 if (!comp)
1076 return ERROR_SUCCESS;
1078 comp->Action = msi_get_component_action( package, comp );
1079 if (comp->Action != INSTALLSTATE_LOCAL)
1081 TRACE("component not scheduled for installation %s\n", debugstr_w(component));
1082 return ERROR_SUCCESS;
1085 file_key = MSI_RecordGetString(row,3);
1086 if (!file_key)
1088 ERR("Unable to get file key\n");
1089 return ERROR_FUNCTION_FAILED;
1092 file = msi_get_loaded_file( package, file_key );
1093 if (!file)
1095 ERR("Original file unknown %s\n", debugstr_w(file_key));
1096 return ERROR_SUCCESS;
1099 dest = get_duplicate_filename( package, row, file_key, file->TargetPath );
1100 if (!dest)
1102 WARN("Unable to get duplicate filename\n");
1103 return ERROR_SUCCESS;
1106 TRACE("Duplicating file %s to %s\n", debugstr_w(file->TargetPath), debugstr_w(dest));
1108 if (!CopyFileW( file->TargetPath, dest, TRUE ))
1110 WARN("Failed to copy file %s -> %s (%u)\n",
1111 debugstr_w(file->TargetPath), debugstr_w(dest), GetLastError());
1114 FIXME("We should track these duplicate files as well\n");
1116 uirow = MSI_CreateRecord( 9 );
1117 MSI_RecordSetStringW( uirow, 1, MSI_RecordGetString( row, 1 ) );
1118 MSI_RecordSetInteger( uirow, 6, file->FileSize );
1119 MSI_RecordSetStringW( uirow, 9, MSI_RecordGetString( row, 5 ) );
1120 MSI_ProcessMessage(package, INSTALLMESSAGE_ACTIONDATA, uirow);
1121 msiobj_release( &uirow->hdr );
1123 msi_free(dest);
1124 return ERROR_SUCCESS;
1127 UINT ACTION_DuplicateFiles(MSIPACKAGE *package)
1129 static const WCHAR query[] = {
1130 'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
1131 '`','D','u','p','l','i','c','a','t','e','F','i','l','e','`',0};
1132 MSIQUERY *view;
1133 UINT rc;
1135 rc = MSI_DatabaseOpenViewW(package->db, query, &view);
1136 if (rc != ERROR_SUCCESS)
1137 return ERROR_SUCCESS;
1139 rc = MSI_IterateRecords(view, NULL, ITERATE_DuplicateFiles, package);
1140 msiobj_release(&view->hdr);
1141 return rc;
1144 static UINT ITERATE_RemoveDuplicateFiles( MSIRECORD *row, LPVOID param )
1146 MSIPACKAGE *package = param;
1147 LPWSTR dest;
1148 LPCWSTR file_key, component;
1149 MSICOMPONENT *comp;
1150 MSIRECORD *uirow;
1151 MSIFILE *file;
1153 component = MSI_RecordGetString( row, 2 );
1154 comp = msi_get_loaded_component( package, component );
1155 if (!comp)
1156 return ERROR_SUCCESS;
1158 comp->Action = msi_get_component_action( package, comp );
1159 if (comp->Action != INSTALLSTATE_ABSENT)
1161 TRACE("component not scheduled for removal %s\n", debugstr_w(component));
1162 return ERROR_SUCCESS;
1165 file_key = MSI_RecordGetString( row, 3 );
1166 if (!file_key)
1168 ERR("Unable to get file key\n");
1169 return ERROR_FUNCTION_FAILED;
1172 file = msi_get_loaded_file( package, file_key );
1173 if (!file)
1175 ERR("Original file unknown %s\n", debugstr_w(file_key));
1176 return ERROR_SUCCESS;
1179 dest = get_duplicate_filename( package, row, file_key, file->TargetPath );
1180 if (!dest)
1182 WARN("Unable to get duplicate filename\n");
1183 return ERROR_SUCCESS;
1186 TRACE("Removing duplicate %s of %s\n", debugstr_w(dest), debugstr_w(file->TargetPath));
1188 if (!DeleteFileW( dest ))
1190 WARN("Failed to delete duplicate file %s (%u)\n", debugstr_w(dest), GetLastError());
1193 uirow = MSI_CreateRecord( 9 );
1194 MSI_RecordSetStringW( uirow, 1, MSI_RecordGetString( row, 1 ) );
1195 MSI_RecordSetStringW( uirow, 9, MSI_RecordGetString( row, 5 ) );
1196 MSI_ProcessMessage(package, INSTALLMESSAGE_ACTIONDATA, uirow);
1197 msiobj_release( &uirow->hdr );
1199 msi_free(dest);
1200 return ERROR_SUCCESS;
1203 UINT ACTION_RemoveDuplicateFiles( MSIPACKAGE *package )
1205 static const WCHAR query[] = {
1206 'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
1207 '`','D','u','p','l','i','c','a','t','e','F','i','l','e','`',0};
1208 MSIQUERY *view;
1209 UINT rc;
1211 rc = MSI_DatabaseOpenViewW( package->db, query, &view );
1212 if (rc != ERROR_SUCCESS)
1213 return ERROR_SUCCESS;
1215 rc = MSI_IterateRecords( view, NULL, ITERATE_RemoveDuplicateFiles, package );
1216 msiobj_release( &view->hdr );
1217 return rc;
1220 static BOOL verify_comp_for_removal(MSICOMPONENT *comp, UINT install_mode)
1222 /* special case */
1223 if (comp->Action != INSTALLSTATE_SOURCE &&
1224 comp->Attributes & msidbComponentAttributesSourceOnly &&
1225 (install_mode == msidbRemoveFileInstallModeOnRemove ||
1226 install_mode == msidbRemoveFileInstallModeOnBoth)) return TRUE;
1228 switch (comp->Action)
1230 case INSTALLSTATE_LOCAL:
1231 case INSTALLSTATE_SOURCE:
1232 if (install_mode == msidbRemoveFileInstallModeOnInstall ||
1233 install_mode == msidbRemoveFileInstallModeOnBoth) return TRUE;
1234 break;
1235 case INSTALLSTATE_ABSENT:
1236 if (install_mode == msidbRemoveFileInstallModeOnRemove ||
1237 install_mode == msidbRemoveFileInstallModeOnBoth) return TRUE;
1238 break;
1239 default: break;
1241 return FALSE;
1244 static UINT ITERATE_RemoveFiles(MSIRECORD *row, LPVOID param)
1246 MSIPACKAGE *package = param;
1247 MSICOMPONENT *comp;
1248 MSIRECORD *uirow;
1249 LPCWSTR component, dirprop;
1250 UINT install_mode;
1251 LPWSTR dir = NULL, path = NULL, filename = NULL;
1252 DWORD size;
1253 UINT ret = ERROR_SUCCESS;
1255 component = MSI_RecordGetString(row, 2);
1256 dirprop = MSI_RecordGetString(row, 4);
1257 install_mode = MSI_RecordGetInteger(row, 5);
1259 comp = msi_get_loaded_component(package, component);
1260 if (!comp)
1261 return ERROR_SUCCESS;
1263 comp->Action = msi_get_component_action( package, comp );
1264 if (!verify_comp_for_removal(comp, install_mode))
1266 TRACE("Skipping removal due to install mode\n");
1267 return ERROR_SUCCESS;
1269 if (comp->assembly && !comp->assembly->application)
1271 return ERROR_SUCCESS;
1273 if (comp->Attributes & msidbComponentAttributesPermanent)
1275 TRACE("permanent component, not removing file\n");
1276 return ERROR_SUCCESS;
1279 dir = msi_dup_property(package->db, dirprop);
1280 if (!dir)
1282 WARN("directory property has no value\n");
1283 return ERROR_SUCCESS;
1285 size = 0;
1286 if ((filename = strdupW( MSI_RecordGetString(row, 3) )))
1288 msi_reduce_to_long_filename( filename );
1289 size = lstrlenW( filename );
1291 size += lstrlenW(dir) + 2;
1292 path = msi_alloc(size * sizeof(WCHAR));
1293 if (!path)
1295 ret = ERROR_OUTOFMEMORY;
1296 goto done;
1299 if (filename)
1301 lstrcpyW(path, dir);
1302 PathAddBackslashW(path);
1303 lstrcatW(path, filename);
1305 TRACE("Deleting misc file: %s\n", debugstr_w(path));
1306 DeleteFileW(path);
1308 else
1310 TRACE("Removing misc directory: %s\n", debugstr_w(dir));
1311 RemoveDirectoryW(dir);
1314 done:
1315 uirow = MSI_CreateRecord( 9 );
1316 MSI_RecordSetStringW( uirow, 1, MSI_RecordGetString(row, 1) );
1317 MSI_RecordSetStringW( uirow, 9, dir );
1318 MSI_ProcessMessage(package, INSTALLMESSAGE_ACTIONDATA, uirow);
1319 msiobj_release( &uirow->hdr );
1321 msi_free(filename);
1322 msi_free(path);
1323 msi_free(dir);
1324 return ret;
1327 static void remove_folder( MSIFOLDER *folder )
1329 FolderList *fl;
1331 LIST_FOR_EACH_ENTRY( fl, &folder->children, FolderList, entry )
1333 remove_folder( fl->folder );
1335 if (!folder->persistent && folder->State != FOLDER_STATE_REMOVED)
1337 if (RemoveDirectoryW( folder->ResolvedTarget )) folder->State = FOLDER_STATE_REMOVED;
1341 UINT ACTION_RemoveFiles( MSIPACKAGE *package )
1343 static const WCHAR query[] = {
1344 'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
1345 '`','R','e','m','o','v','e','F','i','l','e','`',0};
1346 MSIQUERY *view;
1347 MSICOMPONENT *comp;
1348 MSIFILE *file;
1349 UINT r;
1351 r = MSI_DatabaseOpenViewW(package->db, query, &view);
1352 if (r == ERROR_SUCCESS)
1354 r = MSI_IterateRecords(view, NULL, ITERATE_RemoveFiles, package);
1355 msiobj_release(&view->hdr);
1356 if (r != ERROR_SUCCESS)
1357 return r;
1360 LIST_FOR_EACH_ENTRY( file, &package->files, MSIFILE, entry )
1362 MSIRECORD *uirow;
1363 VS_FIXEDFILEINFO *ver;
1365 comp = file->Component;
1366 msi_file_update_ui( package, file, szRemoveFiles );
1368 comp->Action = msi_get_component_action( package, comp );
1369 if (comp->Action != INSTALLSTATE_ABSENT || comp->Installed == INSTALLSTATE_SOURCE)
1370 continue;
1372 if (comp->assembly && !comp->assembly->application)
1373 continue;
1375 if (comp->Attributes & msidbComponentAttributesPermanent)
1377 TRACE("permanent component, not removing file\n");
1378 continue;
1381 if (file->Version)
1383 ver = msi_get_disk_file_version( file->TargetPath );
1384 if (ver && msi_compare_file_versions( ver, file->Version ) > 0)
1386 TRACE("newer version detected, not removing file\n");
1387 msi_free( ver );
1388 continue;
1390 msi_free( ver );
1393 if (file->state == msifs_installed)
1394 WARN("removing installed file %s\n", debugstr_w(file->TargetPath));
1396 TRACE("removing %s\n", debugstr_w(file->File) );
1398 SetFileAttributesW( file->TargetPath, FILE_ATTRIBUTE_NORMAL );
1399 if (!DeleteFileW( file->TargetPath ))
1401 WARN("failed to delete %s (%u)\n", debugstr_w(file->TargetPath), GetLastError());
1403 file->state = msifs_missing;
1405 uirow = MSI_CreateRecord( 9 );
1406 MSI_RecordSetStringW( uirow, 1, file->FileName );
1407 MSI_RecordSetStringW( uirow, 9, comp->Directory );
1408 MSI_ProcessMessage(package, INSTALLMESSAGE_ACTIONDATA, uirow);
1409 msiobj_release( &uirow->hdr );
1412 LIST_FOR_EACH_ENTRY( comp, &package->components, MSICOMPONENT, entry )
1414 comp->Action = msi_get_component_action( package, comp );
1415 if (comp->Action != INSTALLSTATE_ABSENT) continue;
1417 if (comp->Attributes & msidbComponentAttributesPermanent)
1419 TRACE("permanent component, not removing directory\n");
1420 continue;
1422 if (comp->assembly && !comp->assembly->application)
1423 msi_uninstall_assembly( package, comp );
1424 else
1426 MSIFOLDER *folder = msi_get_loaded_folder( package, comp->Directory );
1427 remove_folder( folder );
1430 return ERROR_SUCCESS;