msi: Implement deferral for standard and custom actions.
[wine.git] / dlls / msi / files.c
blob5a88c147e0bceb022e666c780ecbb32d3598c68f
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);
203 return ERROR_SUCCESS;
206 static UINT copy_install_file(MSIPACKAGE *package, MSIFILE *file, LPWSTR source)
208 UINT gle;
210 TRACE("Copying %s to %s\n", debugstr_w(source), debugstr_w(file->TargetPath));
212 gle = copy_file(file, source);
213 if (gle == ERROR_SUCCESS)
214 return gle;
216 if (gle == ERROR_ALREADY_EXISTS && file->state == msifs_overwrite)
218 TRACE("overwriting existing file\n");
219 return ERROR_SUCCESS;
221 else if (gle == ERROR_ACCESS_DENIED)
223 SetFileAttributesW(file->TargetPath, FILE_ATTRIBUTE_NORMAL);
225 gle = copy_file(file, source);
226 TRACE("Overwriting existing file: %d\n", gle);
228 if (gle == ERROR_SHARING_VIOLATION || gle == ERROR_USER_MAPPED_FILE)
230 WCHAR *tmpfileW, *pathW, *p;
231 DWORD len;
233 TRACE("file in use, scheduling rename operation\n");
235 if (!(pathW = strdupW( file->TargetPath ))) return ERROR_OUTOFMEMORY;
236 if ((p = strrchrW(pathW, '\\'))) *p = 0;
237 len = strlenW( pathW ) + 16;
238 if (!(tmpfileW = msi_alloc(len * sizeof(WCHAR))))
240 msi_free( pathW );
241 return ERROR_OUTOFMEMORY;
243 if (!GetTempFileNameW( pathW, szMsi, 0, tmpfileW )) tmpfileW[0] = 0;
244 msi_free( pathW );
246 if (CopyFileW(source, tmpfileW, FALSE) &&
247 MoveFileExW(file->TargetPath, NULL, MOVEFILE_DELAY_UNTIL_REBOOT) &&
248 MoveFileExW(tmpfileW, file->TargetPath, MOVEFILE_DELAY_UNTIL_REBOOT))
250 package->need_reboot_at_end = 1;
251 gle = ERROR_SUCCESS;
253 else
255 gle = GetLastError();
256 WARN("failed to schedule rename operation: %d)\n", gle);
257 DeleteFileW( tmpfileW );
259 msi_free(tmpfileW);
262 return gle;
265 static UINT msi_create_directory( MSIPACKAGE *package, const WCHAR *dir )
267 MSIFOLDER *folder;
268 const WCHAR *install_path;
270 install_path = msi_get_target_folder( package, dir );
271 if (!install_path) return ERROR_FUNCTION_FAILED;
273 folder = msi_get_loaded_folder( package, dir );
274 if (folder->State == FOLDER_STATE_UNINITIALIZED)
276 msi_create_full_path( install_path );
277 folder->State = FOLDER_STATE_CREATED;
279 return ERROR_SUCCESS;
282 static MSIFILE *find_file( MSIPACKAGE *package, UINT disk_id, const WCHAR *filename )
284 MSIFILE *file;
286 LIST_FOR_EACH_ENTRY( file, &package->files, MSIFILE, entry )
288 if (file->disk_id == disk_id &&
289 file->state != msifs_installed &&
290 !strcmpiW( filename, file->File )) return file;
292 return NULL;
295 static BOOL installfiles_cb(MSIPACKAGE *package, LPCWSTR filename, DWORD action,
296 LPWSTR *path, DWORD *attrs, PVOID user)
298 MSIFILE *file = *(MSIFILE **)user;
300 if (action == MSICABEXTRACT_BEGINEXTRACT)
302 if (!(file = find_file( package, file->disk_id, filename )))
304 TRACE("unknown file in cabinet (%s)\n", debugstr_w(filename));
305 return FALSE;
307 if (file->state != msifs_missing && file->state != msifs_overwrite)
308 return FALSE;
310 if (!msi_is_global_assembly( file->Component ))
312 msi_create_directory( package, file->Component->Directory );
314 *path = strdupW( file->TargetPath );
315 *attrs = file->Attributes;
316 *(MSIFILE **)user = file;
318 else if (action == MSICABEXTRACT_FILEEXTRACTED)
320 if (!msi_is_global_assembly( file->Component )) file->state = msifs_installed;
323 return TRUE;
326 WCHAR *msi_resolve_file_source( MSIPACKAGE *package, MSIFILE *file )
328 WCHAR *p, *path;
330 TRACE("Working to resolve source of file %s\n", debugstr_w(file->File));
332 if (file->IsCompressed) return NULL;
334 p = msi_resolve_source_folder( package, file->Component->Directory, NULL );
335 path = msi_build_directory_name( 2, p, file->ShortName );
337 if (file->LongName && GetFileAttributesW( path ) == INVALID_FILE_ATTRIBUTES)
339 msi_free( path );
340 path = msi_build_directory_name( 2, p, file->LongName );
342 msi_free( p );
343 TRACE("file %s source resolves to %s\n", debugstr_w(file->File), debugstr_w(path));
344 return path;
348 * ACTION_InstallFiles()
350 * For efficiency, this is done in two passes:
351 * 1) Correct all the TargetPaths and determine what files are to be installed.
352 * 2) Extract Cabinets and copy files.
354 UINT ACTION_InstallFiles(MSIPACKAGE *package)
356 MSIMEDIAINFO *mi;
357 UINT rc = ERROR_SUCCESS;
358 MSIFILE *file;
360 msi_set_sourcedir_props(package, FALSE);
362 if (package->script == SCRIPT_NONE)
363 return msi_schedule_action(package, SCRIPT_INSTALL, szInstallFiles);
365 schedule_install_files(package);
366 mi = msi_alloc_zero( sizeof(MSIMEDIAINFO) );
368 LIST_FOR_EACH_ENTRY( file, &package->files, MSIFILE, entry )
370 BOOL is_global_assembly = msi_is_global_assembly( file->Component );
372 msi_file_update_ui( package, file, szInstallFiles );
374 rc = msi_load_media_info( package, file->Sequence, mi );
375 if (rc != ERROR_SUCCESS)
377 ERR("Unable to load media info for %s (%u)\n", debugstr_w(file->File), rc);
378 rc = ERROR_FUNCTION_FAILED;
379 goto done;
382 if (file->state != msifs_hashmatch &&
383 file->state != msifs_skipped &&
384 (file->state != msifs_present || !msi_get_property_int( package->db, szInstalled, 0 )) &&
385 (rc = ready_media( package, file->IsCompressed, mi )))
387 ERR("Failed to ready media for %s\n", debugstr_w(file->File));
388 goto done;
391 if (file->state != msifs_missing && !mi->is_continuous && file->state != msifs_overwrite)
392 continue;
394 if (file->Sequence > mi->last_sequence || mi->is_continuous ||
395 (file->IsCompressed && !mi->is_extracted))
397 MSICABDATA data;
398 MSIFILE *cursor = file;
400 data.mi = mi;
401 data.package = package;
402 data.cb = installfiles_cb;
403 data.user = &cursor;
405 if (file->IsCompressed && !msi_cabextract(package, mi, &data))
407 ERR("Failed to extract cabinet: %s\n", debugstr_w(mi->cabinet));
408 rc = ERROR_INSTALL_FAILURE;
409 goto done;
413 if (!file->IsCompressed)
415 WCHAR *source = msi_resolve_file_source(package, file);
417 TRACE("copying %s to %s\n", debugstr_w(source), debugstr_w(file->TargetPath));
419 if (!is_global_assembly)
421 msi_create_directory(package, file->Component->Directory);
423 rc = copy_install_file(package, file, source);
424 if (rc != ERROR_SUCCESS)
426 ERR("Failed to copy %s to %s (%u)\n", debugstr_w(source), debugstr_w(file->TargetPath), rc);
427 rc = ERROR_INSTALL_FAILURE;
428 msi_free(source);
429 goto done;
431 if (!is_global_assembly) file->state = msifs_installed;
432 msi_free(source);
434 else if (!is_global_assembly && file->state != msifs_installed &&
435 !(file->Attributes & msidbFileAttributesPatchAdded))
437 ERR("compressed file wasn't installed (%s)\n", debugstr_w(file->File));
438 rc = ERROR_INSTALL_FAILURE;
439 goto done;
442 LIST_FOR_EACH_ENTRY( file, &package->files, MSIFILE, entry )
444 MSICOMPONENT *comp = file->Component;
446 if (!msi_is_global_assembly( comp ) || comp->assembly->installed ||
447 (file->state != msifs_missing && file->state != msifs_overwrite)) continue;
449 rc = msi_install_assembly( package, comp );
450 if (rc != ERROR_SUCCESS)
452 ERR("Failed to install assembly\n");
453 rc = ERROR_INSTALL_FAILURE;
454 break;
456 file->state = msifs_installed;
459 done:
460 msi_free_media_info(mi);
461 return rc;
464 static MSIFILEPATCH *find_filepatch( MSIPACKAGE *package, UINT disk_id, const WCHAR *key )
466 MSIFILEPATCH *patch;
468 LIST_FOR_EACH_ENTRY( patch, &package->filepatches, MSIFILEPATCH, entry )
470 if (!patch->extracted && patch->disk_id == disk_id && !strcmpW( key, patch->File->File ))
471 return patch;
473 return NULL;
476 static BOOL patchfiles_cb(MSIPACKAGE *package, LPCWSTR file, DWORD action,
477 LPWSTR *path, DWORD *attrs, PVOID user)
479 MSIFILEPATCH *patch = *(MSIFILEPATCH **)user;
481 if (action == MSICABEXTRACT_BEGINEXTRACT)
483 MSICOMPONENT *comp;
485 if (is_registered_patch_media( package, patch->disk_id ) ||
486 !(patch = find_filepatch( package, patch->disk_id, file ))) return FALSE;
488 comp = patch->File->Component;
489 comp->Action = msi_get_component_action( package, comp );
490 if (!comp->Enabled || comp->Action != INSTALLSTATE_LOCAL)
492 TRACE("file %s component %s not installed or disabled\n",
493 debugstr_w(patch->File->File), debugstr_w(comp->Component));
494 return FALSE;
497 patch->path = msi_create_temp_file( package->db );
498 *path = strdupW( patch->path );
499 *attrs = patch->File->Attributes;
500 *(MSIFILEPATCH **)user = patch;
502 else if (action == MSICABEXTRACT_FILEEXTRACTED)
504 patch->extracted = TRUE;
507 return TRUE;
510 static UINT patch_file( MSIPACKAGE *package, MSIFILEPATCH *patch )
512 UINT r = ERROR_SUCCESS;
513 WCHAR *tmpfile = msi_create_temp_file( package->db );
515 if (!tmpfile) return ERROR_INSTALL_FAILURE;
516 if (ApplyPatchToFileW( patch->path, patch->File->TargetPath, tmpfile, 0 ))
518 DeleteFileW( patch->File->TargetPath );
519 MoveFileW( tmpfile, patch->File->TargetPath );
521 else
523 WARN("failed to patch %s: %08x\n", debugstr_w(patch->File->TargetPath), GetLastError());
524 r = ERROR_INSTALL_FAILURE;
526 DeleteFileW( patch->path );
527 DeleteFileW( tmpfile );
528 msi_free( tmpfile );
529 return r;
532 static UINT patch_assembly( MSIPACKAGE *package, MSIASSEMBLY *assembly, MSIFILEPATCH *patch )
534 UINT r = ERROR_FUNCTION_FAILED;
535 IAssemblyName *name;
536 IAssemblyEnum *iter;
538 if (!(iter = msi_create_assembly_enum( package, assembly->display_name )))
539 return ERROR_FUNCTION_FAILED;
541 while ((IAssemblyEnum_GetNextAssembly( iter, NULL, &name, 0 ) == S_OK))
543 WCHAR *displayname, *path;
544 UINT len = 0;
545 HRESULT hr;
547 hr = IAssemblyName_GetDisplayName( name, NULL, &len, 0 );
548 if (hr != E_NOT_SUFFICIENT_BUFFER || !(displayname = msi_alloc( len * sizeof(WCHAR) )))
549 break;
551 hr = IAssemblyName_GetDisplayName( name, displayname, &len, 0 );
552 if (FAILED( hr ))
554 msi_free( displayname );
555 break;
558 if ((path = msi_get_assembly_path( package, displayname )))
560 if (!CopyFileW( path, patch->File->TargetPath, FALSE ))
562 ERR("Failed to copy file %s -> %s (%u)\n", debugstr_w(path),
563 debugstr_w(patch->File->TargetPath), GetLastError() );
564 msi_free( path );
565 msi_free( displayname );
566 IAssemblyName_Release( name );
567 break;
569 r = patch_file( package, patch );
570 msi_free( path );
573 msi_free( displayname );
574 IAssemblyName_Release( name );
575 if (r == ERROR_SUCCESS) break;
578 IAssemblyEnum_Release( iter );
579 return r;
582 UINT ACTION_PatchFiles( MSIPACKAGE *package )
584 MSIFILEPATCH *patch;
585 MSIMEDIAINFO *mi;
586 UINT rc = ERROR_SUCCESS;
588 TRACE("%p\n", package);
590 if (package->script == SCRIPT_NONE)
591 return msi_schedule_action(package, SCRIPT_INSTALL, szPatchFiles);
593 mi = msi_alloc_zero( sizeof(MSIMEDIAINFO) );
595 TRACE("extracting files\n");
597 LIST_FOR_EACH_ENTRY( patch, &package->filepatches, MSIFILEPATCH, entry )
599 MSIFILE *file = patch->File;
600 MSICOMPONENT *comp = file->Component;
602 rc = msi_load_media_info( package, patch->Sequence, mi );
603 if (rc != ERROR_SUCCESS)
605 ERR("Unable to load media info for %s (%u)\n", debugstr_w(file->File), rc);
606 rc = ERROR_FUNCTION_FAILED;
607 goto done;
609 comp->Action = msi_get_component_action( package, comp );
610 if (!comp->Enabled || comp->Action != INSTALLSTATE_LOCAL) continue;
612 if (!patch->extracted)
614 MSICABDATA data;
615 MSIFILEPATCH *cursor = patch;
617 rc = ready_media( package, TRUE, mi );
618 if (rc != ERROR_SUCCESS)
620 ERR("Failed to ready media for %s\n", debugstr_w(file->File));
621 goto done;
623 data.mi = mi;
624 data.package = package;
625 data.cb = patchfiles_cb;
626 data.user = &cursor;
628 if (!msi_cabextract( package, mi, &data ))
630 ERR("Failed to extract cabinet: %s\n", debugstr_w(mi->cabinet));
631 rc = ERROR_INSTALL_FAILURE;
632 goto done;
637 TRACE("applying patches\n");
639 LIST_FOR_EACH_ENTRY( patch, &package->filepatches, MSIFILEPATCH, entry )
641 MSICOMPONENT *comp = patch->File->Component;
643 if (!patch->path) continue;
645 if (msi_is_global_assembly( comp ))
646 rc = patch_assembly( package, comp->assembly, patch );
647 else
648 rc = patch_file( package, patch );
650 if (rc && !(patch->Attributes & msidbPatchAttributesNonVital))
652 ERR("Failed to apply patch to file: %s\n", debugstr_w(patch->File->File));
653 break;
656 if (msi_is_global_assembly( comp ))
658 if ((rc = msi_install_assembly( package, comp )))
660 ERR("Failed to install patched assembly\n");
661 break;
666 done:
667 msi_free_media_info(mi);
668 return rc;
671 #define is_dot_dir(x) ((x[0] == '.') && ((x[1] == 0) || ((x[1] == '.') && (x[2] == 0))))
673 typedef struct
675 struct list entry;
676 LPWSTR sourcename;
677 LPWSTR destname;
678 LPWSTR source;
679 LPWSTR dest;
680 } FILE_LIST;
682 static BOOL msi_move_file(LPCWSTR source, LPCWSTR dest, int options)
684 BOOL ret;
686 if (GetFileAttributesW(source) == FILE_ATTRIBUTE_DIRECTORY ||
687 GetFileAttributesW(dest) == FILE_ATTRIBUTE_DIRECTORY)
689 WARN("Source or dest is directory, not moving\n");
690 return FALSE;
693 if (options == msidbMoveFileOptionsMove)
695 TRACE("moving %s -> %s\n", debugstr_w(source), debugstr_w(dest));
696 ret = MoveFileExW(source, dest, MOVEFILE_REPLACE_EXISTING);
697 if (!ret)
699 WARN("MoveFile failed: %d\n", GetLastError());
700 return FALSE;
703 else
705 TRACE("copying %s -> %s\n", debugstr_w(source), debugstr_w(dest));
706 ret = CopyFileW(source, dest, FALSE);
707 if (!ret)
709 WARN("CopyFile failed: %d\n", GetLastError());
710 return FALSE;
714 return TRUE;
717 static LPWSTR wildcard_to_file(LPWSTR wildcard, LPWSTR filename)
719 LPWSTR path, ptr;
720 DWORD dirlen, pathlen;
722 ptr = strrchrW(wildcard, '\\');
723 dirlen = ptr - wildcard + 1;
725 pathlen = dirlen + lstrlenW(filename) + 1;
726 path = msi_alloc(pathlen * sizeof(WCHAR));
728 lstrcpynW(path, wildcard, dirlen + 1);
729 lstrcatW(path, filename);
731 return path;
734 static void free_file_entry(FILE_LIST *file)
736 msi_free(file->source);
737 msi_free(file->dest);
738 msi_free(file);
741 static void free_list(FILE_LIST *list)
743 while (!list_empty(&list->entry))
745 FILE_LIST *file = LIST_ENTRY(list_head(&list->entry), FILE_LIST, entry);
747 list_remove(&file->entry);
748 free_file_entry(file);
752 static BOOL add_wildcard(FILE_LIST *files, LPWSTR source, LPWSTR dest)
754 FILE_LIST *new, *file;
755 LPWSTR ptr, filename;
756 DWORD size;
758 new = msi_alloc_zero(sizeof(FILE_LIST));
759 if (!new)
760 return FALSE;
762 new->source = strdupW(source);
763 ptr = strrchrW(dest, '\\') + 1;
764 filename = strrchrW(new->source, '\\') + 1;
766 new->sourcename = filename;
768 if (*ptr)
769 new->destname = ptr;
770 else
771 new->destname = new->sourcename;
773 size = (ptr - dest) + lstrlenW(filename) + 1;
774 new->dest = msi_alloc(size * sizeof(WCHAR));
775 if (!new->dest)
777 free_file_entry(new);
778 return FALSE;
781 lstrcpynW(new->dest, dest, ptr - dest + 1);
782 lstrcatW(new->dest, filename);
784 if (list_empty(&files->entry))
786 list_add_head(&files->entry, &new->entry);
787 return TRUE;
790 LIST_FOR_EACH_ENTRY(file, &files->entry, FILE_LIST, entry)
792 if (strcmpW( source, file->source ) < 0)
794 list_add_before(&file->entry, &new->entry);
795 return TRUE;
799 list_add_after(&file->entry, &new->entry);
800 return TRUE;
803 static BOOL move_files_wildcard(LPWSTR source, LPWSTR dest, int options)
805 WIN32_FIND_DATAW wfd;
806 HANDLE hfile;
807 LPWSTR path;
808 BOOL res;
809 FILE_LIST files, *file;
810 DWORD size;
812 hfile = FindFirstFileW(source, &wfd);
813 if (hfile == INVALID_HANDLE_VALUE) return FALSE;
815 list_init(&files.entry);
817 for (res = TRUE; res; res = FindNextFileW(hfile, &wfd))
819 if (is_dot_dir(wfd.cFileName)) continue;
821 path = wildcard_to_file(source, wfd.cFileName);
822 if (!path)
824 res = FALSE;
825 goto done;
828 add_wildcard(&files, path, dest);
829 msi_free(path);
832 /* no files match the wildcard */
833 if (list_empty(&files.entry))
834 goto done;
836 /* only the first wildcard match gets renamed to dest */
837 file = LIST_ENTRY(list_head(&files.entry), FILE_LIST, entry);
838 size = (strrchrW(file->dest, '\\') - file->dest) + lstrlenW(file->destname) + 2;
839 file->dest = msi_realloc(file->dest, size * sizeof(WCHAR));
840 if (!file->dest)
842 res = FALSE;
843 goto done;
846 /* file->dest may be shorter after the reallocation, so add a NULL
847 * terminator. This is needed for the call to strrchrW, as there will no
848 * longer be a NULL terminator within the bounds of the allocation in this case.
850 file->dest[size - 1] = '\0';
851 lstrcpyW(strrchrW(file->dest, '\\') + 1, file->destname);
853 while (!list_empty(&files.entry))
855 file = LIST_ENTRY(list_head(&files.entry), FILE_LIST, entry);
857 msi_move_file(file->source, file->dest, options);
859 list_remove(&file->entry);
860 free_file_entry(file);
863 res = TRUE;
865 done:
866 free_list(&files);
867 FindClose(hfile);
868 return res;
871 void msi_reduce_to_long_filename( WCHAR *filename )
873 WCHAR *p = strchrW( filename, '|' );
874 if (p) memmove( filename, p + 1, (strlenW( p + 1 ) + 1) * sizeof(WCHAR) );
877 static UINT ITERATE_MoveFiles( MSIRECORD *rec, LPVOID param )
879 MSIPACKAGE *package = param;
880 MSIRECORD *uirow;
881 MSICOMPONENT *comp;
882 LPCWSTR sourcename, component;
883 LPWSTR sourcedir, destname = NULL, destdir = NULL, source = NULL, dest = NULL;
884 int options;
885 DWORD size;
886 BOOL wildcards;
888 component = MSI_RecordGetString(rec, 2);
889 comp = msi_get_loaded_component(package, component);
890 if (!comp)
891 return ERROR_SUCCESS;
893 comp->Action = msi_get_component_action( package, comp );
894 if (comp->Action != INSTALLSTATE_LOCAL)
896 TRACE("component not scheduled for installation %s\n", debugstr_w(component));
897 return ERROR_SUCCESS;
900 sourcename = MSI_RecordGetString(rec, 3);
901 options = MSI_RecordGetInteger(rec, 7);
903 sourcedir = msi_dup_property(package->db, MSI_RecordGetString(rec, 5));
904 if (!sourcedir)
905 goto done;
907 destdir = msi_dup_property(package->db, MSI_RecordGetString(rec, 6));
908 if (!destdir)
909 goto done;
911 if (!sourcename)
913 if (GetFileAttributesW(sourcedir) == INVALID_FILE_ATTRIBUTES)
914 goto done;
916 source = strdupW(sourcedir);
917 if (!source)
918 goto done;
920 else
922 size = lstrlenW(sourcedir) + lstrlenW(sourcename) + 2;
923 source = msi_alloc(size * sizeof(WCHAR));
924 if (!source)
925 goto done;
927 lstrcpyW(source, sourcedir);
928 if (source[lstrlenW(source) - 1] != '\\')
929 lstrcatW(source, szBackSlash);
930 lstrcatW(source, sourcename);
933 wildcards = strchrW(source, '*') || strchrW(source, '?');
935 if (MSI_RecordIsNull(rec, 4))
937 if (!wildcards)
939 WCHAR *p;
940 if (sourcename)
941 destname = strdupW(sourcename);
942 else if ((p = strrchrW(sourcedir, '\\')))
943 destname = strdupW(p + 1);
944 else
945 destname = strdupW(sourcedir);
946 if (!destname)
947 goto done;
950 else
952 destname = strdupW(MSI_RecordGetString(rec, 4));
953 if (destname) msi_reduce_to_long_filename(destname);
956 size = 0;
957 if (destname)
958 size = lstrlenW(destname);
960 size += lstrlenW(destdir) + 2;
961 dest = msi_alloc(size * sizeof(WCHAR));
962 if (!dest)
963 goto done;
965 lstrcpyW(dest, destdir);
966 if (dest[lstrlenW(dest) - 1] != '\\')
967 lstrcatW(dest, szBackSlash);
969 if (destname)
970 lstrcatW(dest, destname);
972 if (GetFileAttributesW(destdir) == INVALID_FILE_ATTRIBUTES)
974 if (!msi_create_full_path(destdir))
976 WARN("failed to create directory %u\n", GetLastError());
977 goto done;
981 if (!wildcards)
982 msi_move_file(source, dest, options);
983 else
984 move_files_wildcard(source, dest, options);
986 done:
987 uirow = MSI_CreateRecord( 9 );
988 MSI_RecordSetStringW( uirow, 1, MSI_RecordGetString(rec, 1) );
989 MSI_RecordSetInteger( uirow, 6, 1 ); /* FIXME */
990 MSI_RecordSetStringW( uirow, 9, destdir );
991 MSI_ProcessMessage(package, INSTALLMESSAGE_ACTIONDATA, uirow);
992 msiobj_release( &uirow->hdr );
994 msi_free(sourcedir);
995 msi_free(destdir);
996 msi_free(destname);
997 msi_free(source);
998 msi_free(dest);
1000 return ERROR_SUCCESS;
1003 UINT ACTION_MoveFiles( MSIPACKAGE *package )
1005 static const WCHAR query[] = {
1006 'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
1007 '`','M','o','v','e','F','i','l','e','`',0};
1008 MSIQUERY *view;
1009 UINT rc;
1011 if (package->script == SCRIPT_NONE)
1012 return msi_schedule_action(package, SCRIPT_INSTALL, szMoveFiles);
1014 rc = MSI_DatabaseOpenViewW(package->db, query, &view);
1015 if (rc != ERROR_SUCCESS)
1016 return ERROR_SUCCESS;
1018 rc = MSI_IterateRecords(view, NULL, ITERATE_MoveFiles, package);
1019 msiobj_release(&view->hdr);
1020 return rc;
1023 static WCHAR *get_duplicate_filename( MSIPACKAGE *package, MSIRECORD *row, const WCHAR *file_key, const WCHAR *src )
1025 DWORD len;
1026 WCHAR *dst_name, *dst_path, *dst;
1028 if (MSI_RecordIsNull( row, 4 ))
1030 len = strlenW( src ) + 1;
1031 if (!(dst_name = msi_alloc( len * sizeof(WCHAR)))) return NULL;
1032 strcpyW( dst_name, strrchrW( src, '\\' ) + 1 );
1034 else
1036 MSI_RecordGetStringW( row, 4, NULL, &len );
1037 if (!(dst_name = msi_alloc( ++len * sizeof(WCHAR) ))) return NULL;
1038 MSI_RecordGetStringW( row, 4, dst_name, &len );
1039 msi_reduce_to_long_filename( dst_name );
1042 if (MSI_RecordIsNull( row, 5 ))
1044 WCHAR *p;
1045 dst_path = strdupW( src );
1046 p = strrchrW( dst_path, '\\' );
1047 if (p) *p = 0;
1049 else
1051 const WCHAR *dst_key = MSI_RecordGetString( row, 5 );
1053 dst_path = strdupW( msi_get_target_folder( package, dst_key ) );
1054 if (!dst_path)
1056 /* try a property */
1057 dst_path = msi_dup_property( package->db, dst_key );
1058 if (!dst_path)
1060 FIXME("Unable to get destination folder, try AppSearch properties\n");
1061 msi_free( dst_name );
1062 return NULL;
1067 dst = msi_build_directory_name( 2, dst_path, dst_name );
1068 msi_create_full_path( dst_path );
1070 msi_free( dst_name );
1071 msi_free( dst_path );
1072 return dst;
1075 static UINT ITERATE_DuplicateFiles(MSIRECORD *row, LPVOID param)
1077 MSIPACKAGE *package = param;
1078 LPWSTR dest;
1079 LPCWSTR file_key, component;
1080 MSICOMPONENT *comp;
1081 MSIRECORD *uirow;
1082 MSIFILE *file;
1084 component = MSI_RecordGetString(row,2);
1085 comp = msi_get_loaded_component(package, component);
1086 if (!comp)
1087 return ERROR_SUCCESS;
1089 comp->Action = msi_get_component_action( package, comp );
1090 if (comp->Action != INSTALLSTATE_LOCAL)
1092 TRACE("component not scheduled for installation %s\n", debugstr_w(component));
1093 return ERROR_SUCCESS;
1096 file_key = MSI_RecordGetString(row,3);
1097 if (!file_key)
1099 ERR("Unable to get file key\n");
1100 return ERROR_FUNCTION_FAILED;
1103 file = msi_get_loaded_file( package, file_key );
1104 if (!file)
1106 ERR("Original file unknown %s\n", debugstr_w(file_key));
1107 return ERROR_SUCCESS;
1110 dest = get_duplicate_filename( package, row, file_key, file->TargetPath );
1111 if (!dest)
1113 WARN("Unable to get duplicate filename\n");
1114 return ERROR_SUCCESS;
1117 TRACE("Duplicating file %s to %s\n", debugstr_w(file->TargetPath), debugstr_w(dest));
1119 if (!CopyFileW( file->TargetPath, dest, TRUE ))
1121 WARN("Failed to copy file %s -> %s (%u)\n",
1122 debugstr_w(file->TargetPath), debugstr_w(dest), GetLastError());
1125 FIXME("We should track these duplicate files as well\n");
1127 uirow = MSI_CreateRecord( 9 );
1128 MSI_RecordSetStringW( uirow, 1, MSI_RecordGetString( row, 1 ) );
1129 MSI_RecordSetInteger( uirow, 6, file->FileSize );
1130 MSI_RecordSetStringW( uirow, 9, MSI_RecordGetString( row, 5 ) );
1131 MSI_ProcessMessage(package, INSTALLMESSAGE_ACTIONDATA, uirow);
1132 msiobj_release( &uirow->hdr );
1134 msi_free(dest);
1135 return ERROR_SUCCESS;
1138 UINT ACTION_DuplicateFiles(MSIPACKAGE *package)
1140 static const WCHAR query[] = {
1141 'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
1142 '`','D','u','p','l','i','c','a','t','e','F','i','l','e','`',0};
1143 MSIQUERY *view;
1144 UINT rc;
1146 if (package->script == SCRIPT_NONE)
1147 return msi_schedule_action(package, SCRIPT_INSTALL, szDuplicateFiles);
1149 rc = MSI_DatabaseOpenViewW(package->db, query, &view);
1150 if (rc != ERROR_SUCCESS)
1151 return ERROR_SUCCESS;
1153 rc = MSI_IterateRecords(view, NULL, ITERATE_DuplicateFiles, package);
1154 msiobj_release(&view->hdr);
1155 return rc;
1158 static UINT ITERATE_RemoveDuplicateFiles( MSIRECORD *row, LPVOID param )
1160 MSIPACKAGE *package = param;
1161 LPWSTR dest;
1162 LPCWSTR file_key, component;
1163 MSICOMPONENT *comp;
1164 MSIRECORD *uirow;
1165 MSIFILE *file;
1167 component = MSI_RecordGetString( row, 2 );
1168 comp = msi_get_loaded_component( package, component );
1169 if (!comp)
1170 return ERROR_SUCCESS;
1172 comp->Action = msi_get_component_action( package, comp );
1173 if (comp->Action != INSTALLSTATE_ABSENT)
1175 TRACE("component not scheduled for removal %s\n", debugstr_w(component));
1176 return ERROR_SUCCESS;
1179 file_key = MSI_RecordGetString( row, 3 );
1180 if (!file_key)
1182 ERR("Unable to get file key\n");
1183 return ERROR_FUNCTION_FAILED;
1186 file = msi_get_loaded_file( package, file_key );
1187 if (!file)
1189 ERR("Original file unknown %s\n", debugstr_w(file_key));
1190 return ERROR_SUCCESS;
1193 dest = get_duplicate_filename( package, row, file_key, file->TargetPath );
1194 if (!dest)
1196 WARN("Unable to get duplicate filename\n");
1197 return ERROR_SUCCESS;
1200 TRACE("Removing duplicate %s of %s\n", debugstr_w(dest), debugstr_w(file->TargetPath));
1202 if (!DeleteFileW( dest ))
1204 WARN("Failed to delete duplicate file %s (%u)\n", debugstr_w(dest), GetLastError());
1207 uirow = MSI_CreateRecord( 9 );
1208 MSI_RecordSetStringW( uirow, 1, MSI_RecordGetString( row, 1 ) );
1209 MSI_RecordSetStringW( uirow, 9, MSI_RecordGetString( row, 5 ) );
1210 MSI_ProcessMessage(package, INSTALLMESSAGE_ACTIONDATA, uirow);
1211 msiobj_release( &uirow->hdr );
1213 msi_free(dest);
1214 return ERROR_SUCCESS;
1217 UINT ACTION_RemoveDuplicateFiles( MSIPACKAGE *package )
1219 static const WCHAR query[] = {
1220 'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
1221 '`','D','u','p','l','i','c','a','t','e','F','i','l','e','`',0};
1222 MSIQUERY *view;
1223 UINT rc;
1225 if (package->script == SCRIPT_NONE)
1226 return msi_schedule_action(package, SCRIPT_INSTALL, szRemoveDuplicateFiles);
1228 rc = MSI_DatabaseOpenViewW( package->db, query, &view );
1229 if (rc != ERROR_SUCCESS)
1230 return ERROR_SUCCESS;
1232 rc = MSI_IterateRecords( view, NULL, ITERATE_RemoveDuplicateFiles, package );
1233 msiobj_release( &view->hdr );
1234 return rc;
1237 static BOOL verify_comp_for_removal(MSICOMPONENT *comp, UINT install_mode)
1239 /* special case */
1240 if (comp->Action != INSTALLSTATE_SOURCE &&
1241 comp->Attributes & msidbComponentAttributesSourceOnly &&
1242 (install_mode == msidbRemoveFileInstallModeOnRemove ||
1243 install_mode == msidbRemoveFileInstallModeOnBoth)) return TRUE;
1245 switch (comp->Action)
1247 case INSTALLSTATE_LOCAL:
1248 case INSTALLSTATE_SOURCE:
1249 if (install_mode == msidbRemoveFileInstallModeOnInstall ||
1250 install_mode == msidbRemoveFileInstallModeOnBoth) return TRUE;
1251 break;
1252 case INSTALLSTATE_ABSENT:
1253 if (install_mode == msidbRemoveFileInstallModeOnRemove ||
1254 install_mode == msidbRemoveFileInstallModeOnBoth) return TRUE;
1255 break;
1256 default: break;
1258 return FALSE;
1261 static UINT ITERATE_RemoveFiles(MSIRECORD *row, LPVOID param)
1263 MSIPACKAGE *package = param;
1264 MSICOMPONENT *comp;
1265 MSIRECORD *uirow;
1266 LPCWSTR component, dirprop;
1267 UINT install_mode;
1268 LPWSTR dir = NULL, path = NULL, filename = NULL;
1269 DWORD size;
1270 UINT ret = ERROR_SUCCESS;
1272 component = MSI_RecordGetString(row, 2);
1273 dirprop = MSI_RecordGetString(row, 4);
1274 install_mode = MSI_RecordGetInteger(row, 5);
1276 comp = msi_get_loaded_component(package, component);
1277 if (!comp)
1278 return ERROR_SUCCESS;
1280 comp->Action = msi_get_component_action( package, comp );
1281 if (!verify_comp_for_removal(comp, install_mode))
1283 TRACE("Skipping removal due to install mode\n");
1284 return ERROR_SUCCESS;
1286 if (comp->assembly && !comp->assembly->application)
1288 return ERROR_SUCCESS;
1290 if (comp->Attributes & msidbComponentAttributesPermanent)
1292 TRACE("permanent component, not removing file\n");
1293 return ERROR_SUCCESS;
1296 dir = msi_dup_property(package->db, dirprop);
1297 if (!dir)
1299 WARN("directory property has no value\n");
1300 return ERROR_SUCCESS;
1302 size = 0;
1303 if ((filename = strdupW( MSI_RecordGetString(row, 3) )))
1305 msi_reduce_to_long_filename( filename );
1306 size = lstrlenW( filename );
1308 size += lstrlenW(dir) + 2;
1309 path = msi_alloc(size * sizeof(WCHAR));
1310 if (!path)
1312 ret = ERROR_OUTOFMEMORY;
1313 goto done;
1316 if (filename)
1318 lstrcpyW(path, dir);
1319 PathAddBackslashW(path);
1320 lstrcatW(path, filename);
1322 TRACE("Deleting misc file: %s\n", debugstr_w(path));
1323 DeleteFileW(path);
1325 else
1327 TRACE("Removing misc directory: %s\n", debugstr_w(dir));
1328 RemoveDirectoryW(dir);
1331 done:
1332 uirow = MSI_CreateRecord( 9 );
1333 MSI_RecordSetStringW( uirow, 1, MSI_RecordGetString(row, 1) );
1334 MSI_RecordSetStringW( uirow, 9, dir );
1335 MSI_ProcessMessage(package, INSTALLMESSAGE_ACTIONDATA, uirow);
1336 msiobj_release( &uirow->hdr );
1338 msi_free(filename);
1339 msi_free(path);
1340 msi_free(dir);
1341 return ret;
1344 static void remove_folder( MSIFOLDER *folder )
1346 FolderList *fl;
1348 LIST_FOR_EACH_ENTRY( fl, &folder->children, FolderList, entry )
1350 remove_folder( fl->folder );
1352 if (!folder->persistent && folder->State != FOLDER_STATE_REMOVED)
1354 if (RemoveDirectoryW( folder->ResolvedTarget )) folder->State = FOLDER_STATE_REMOVED;
1358 UINT ACTION_RemoveFiles( MSIPACKAGE *package )
1360 static const WCHAR query[] = {
1361 'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
1362 '`','R','e','m','o','v','e','F','i','l','e','`',0};
1363 MSIQUERY *view;
1364 MSICOMPONENT *comp;
1365 MSIFILE *file;
1366 UINT r;
1368 if (package->script == SCRIPT_NONE)
1369 return msi_schedule_action(package, SCRIPT_INSTALL, szRemoveFiles);
1371 r = MSI_DatabaseOpenViewW(package->db, query, &view);
1372 if (r == ERROR_SUCCESS)
1374 r = MSI_IterateRecords(view, NULL, ITERATE_RemoveFiles, package);
1375 msiobj_release(&view->hdr);
1376 if (r != ERROR_SUCCESS)
1377 return r;
1380 LIST_FOR_EACH_ENTRY( file, &package->files, MSIFILE, entry )
1382 MSIRECORD *uirow;
1383 VS_FIXEDFILEINFO *ver;
1385 comp = file->Component;
1386 msi_file_update_ui( package, file, szRemoveFiles );
1388 comp->Action = msi_get_component_action( package, comp );
1389 if (comp->Action != INSTALLSTATE_ABSENT || comp->Installed == INSTALLSTATE_SOURCE)
1390 continue;
1392 if (comp->assembly && !comp->assembly->application)
1393 continue;
1395 if (comp->Attributes & msidbComponentAttributesPermanent)
1397 TRACE("permanent component, not removing file\n");
1398 continue;
1401 if (file->Version)
1403 ver = msi_get_disk_file_version( file->TargetPath );
1404 if (ver && msi_compare_file_versions( ver, file->Version ) > 0)
1406 TRACE("newer version detected, not removing file\n");
1407 msi_free( ver );
1408 continue;
1410 msi_free( ver );
1413 if (file->state == msifs_installed)
1414 WARN("removing installed file %s\n", debugstr_w(file->TargetPath));
1416 TRACE("removing %s\n", debugstr_w(file->File) );
1418 SetFileAttributesW( file->TargetPath, FILE_ATTRIBUTE_NORMAL );
1419 if (!DeleteFileW( file->TargetPath ))
1421 WARN("failed to delete %s (%u)\n", debugstr_w(file->TargetPath), GetLastError());
1423 file->state = msifs_missing;
1425 uirow = MSI_CreateRecord( 9 );
1426 MSI_RecordSetStringW( uirow, 1, file->FileName );
1427 MSI_RecordSetStringW( uirow, 9, comp->Directory );
1428 MSI_ProcessMessage(package, INSTALLMESSAGE_ACTIONDATA, uirow);
1429 msiobj_release( &uirow->hdr );
1432 LIST_FOR_EACH_ENTRY( comp, &package->components, MSICOMPONENT, entry )
1434 comp->Action = msi_get_component_action( package, comp );
1435 if (comp->Action != INSTALLSTATE_ABSENT) continue;
1437 if (comp->Attributes & msidbComponentAttributesPermanent)
1439 TRACE("permanent component, not removing directory\n");
1440 continue;
1442 if (comp->assembly && !comp->assembly->application)
1443 msi_uninstall_assembly( package, comp );
1444 else
1446 MSIFOLDER *folder = msi_get_loaded_folder( package, comp->Directory );
1447 remove_folder( folder );
1450 return ERROR_SUCCESS;