netapi32: Default to CP_UTF8 when WINEUNIXCP is not set.
[wine.git] / dlls / msi / files.c
blob61694ee19ae90352d4338ade524f0defcf0cf148
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
22 * Actions dealing with files:
24 * InstallFiles
25 * DuplicateFiles
26 * MoveFiles
27 * PatchFiles
28 * RemoveDuplicateFiles
29 * RemoveFiles
32 #include <stdarg.h>
34 #define COBJMACROS
36 #include "windef.h"
37 #include "winbase.h"
38 #include "winerror.h"
39 #include "fdi.h"
40 #include "msi.h"
41 #include "msidefs.h"
42 #include "msipriv.h"
43 #include "winuser.h"
44 #include "winreg.h"
45 #include "shlwapi.h"
46 #include "patchapi.h"
47 #include "wine/debug.h"
49 WINE_DEFAULT_DEBUG_CHANNEL(msi);
51 HANDLE msi_create_file( MSIPACKAGE *package, const WCHAR *filename, DWORD access, DWORD sharing, DWORD creation,
52 DWORD flags )
54 HANDLE handle;
55 msi_disable_fs_redirection( package );
56 handle = CreateFileW( filename, access, sharing, NULL, creation, flags, NULL );
57 msi_revert_fs_redirection( package );
58 return handle;
61 static BOOL msi_copy_file( MSIPACKAGE *package, const WCHAR *src, const WCHAR *dst, BOOL fail_if_exists )
63 BOOL ret;
64 msi_disable_fs_redirection( package );
65 ret = CopyFileW( src, dst, fail_if_exists );
66 msi_revert_fs_redirection( package );
67 return ret;
70 BOOL msi_delete_file( MSIPACKAGE *package, const WCHAR *filename )
72 BOOL ret;
73 msi_disable_fs_redirection( package );
74 ret = DeleteFileW( filename );
75 msi_revert_fs_redirection( package );
76 return ret;
79 static BOOL msi_create_directory( MSIPACKAGE *package, const WCHAR *path )
81 BOOL ret;
82 msi_disable_fs_redirection( package );
83 ret = CreateDirectoryW( path, NULL );
84 msi_revert_fs_redirection( package );
85 return ret;
88 BOOL msi_remove_directory( MSIPACKAGE *package, const WCHAR *path )
90 BOOL ret;
91 msi_disable_fs_redirection( package );
92 ret = RemoveDirectoryW( path );
93 msi_revert_fs_redirection( package );
94 return ret;
97 BOOL msi_set_file_attributes( MSIPACKAGE *package, const WCHAR *filename, DWORD attrs )
99 BOOL ret;
100 msi_disable_fs_redirection( package );
101 ret = SetFileAttributesW( filename, attrs );
102 msi_revert_fs_redirection( package );
103 return ret;
106 DWORD msi_get_file_attributes( MSIPACKAGE *package, const WCHAR *path )
108 DWORD attrs;
109 msi_disable_fs_redirection( package );
110 attrs = GetFileAttributesW( path );
111 msi_revert_fs_redirection( package );
112 return attrs;
115 HANDLE msi_find_first_file( MSIPACKAGE *package, const WCHAR *filename, WIN32_FIND_DATAW *data )
117 HANDLE handle;
118 msi_disable_fs_redirection( package );
119 handle = FindFirstFileW( filename, data );
120 msi_revert_fs_redirection( package );
121 return handle;
124 BOOL msi_find_next_file( MSIPACKAGE *package, HANDLE handle, WIN32_FIND_DATAW *data )
126 BOOL ret;
127 msi_disable_fs_redirection( package );
128 ret = FindNextFileW( handle, data );
129 msi_revert_fs_redirection( package );
130 return ret;
133 BOOL msi_move_file( MSIPACKAGE *package, const WCHAR *from, const WCHAR *to, DWORD flags )
135 BOOL ret;
136 msi_disable_fs_redirection( package );
137 ret = MoveFileExW( from, to, flags );
138 msi_revert_fs_redirection( package );
139 return ret;
142 static BOOL msi_apply_filepatch( MSIPACKAGE *package, const WCHAR *patch, const WCHAR *old, const WCHAR *new )
144 BOOL ret;
145 msi_disable_fs_redirection( package );
146 ret = ApplyPatchToFileW( patch, old, new, 0 );
147 msi_revert_fs_redirection( package );
148 return ret;
151 DWORD msi_get_file_version_info( MSIPACKAGE *package, const WCHAR *path, DWORD buflen, BYTE *buffer )
153 DWORD size, handle;
154 msi_disable_fs_redirection( package );
155 if (buffer) size = GetFileVersionInfoW( path, 0, buflen, buffer );
156 else size = GetFileVersionInfoSizeW( path, &handle );
157 msi_revert_fs_redirection( package );
158 return size;
161 VS_FIXEDFILEINFO *msi_get_disk_file_version( MSIPACKAGE *package, const WCHAR *filename )
163 VS_FIXEDFILEINFO *ptr, *ret;
164 DWORD version_size, size;
165 void *version;
167 if (!(version_size = msi_get_file_version_info( package, filename, 0, NULL ))) return NULL;
168 if (!(version = msi_alloc( version_size ))) return NULL;
170 msi_get_file_version_info( package, filename, version_size, version );
172 if (!VerQueryValueW( version, L"\\", (void **)&ptr, &size ))
174 msi_free( version );
175 return NULL;
178 if (!(ret = msi_alloc( size )))
180 msi_free( version );
181 return NULL;
184 memcpy( ret, ptr, size );
185 msi_free( version );
186 return ret;
189 DWORD msi_get_disk_file_size( MSIPACKAGE *package, const WCHAR *filename )
191 DWORD size;
192 HANDLE file;
193 file = msi_create_file( package, filename, GENERIC_READ, FILE_SHARE_READ, OPEN_EXISTING, 0 );
194 if (file == INVALID_HANDLE_VALUE) return INVALID_FILE_SIZE;
195 size = GetFileSize( file, NULL );
196 CloseHandle( file );
197 return size;
200 /* Recursively create all directories in the path. */
201 BOOL msi_create_full_path( MSIPACKAGE *package, const WCHAR *path )
203 BOOL ret = TRUE;
204 WCHAR *new_path;
205 int len;
207 if (!(new_path = msi_alloc( (lstrlenW( path ) + 1) * sizeof(WCHAR) ))) return FALSE;
208 lstrcpyW( new_path, path );
210 while ((len = lstrlenW( new_path )) && new_path[len - 1] == '\\')
211 new_path[len - 1] = 0;
213 while (!msi_create_directory( package, new_path ))
215 WCHAR *slash;
216 DWORD last_error = GetLastError();
217 if (last_error == ERROR_ALREADY_EXISTS) break;
218 if (last_error != ERROR_PATH_NOT_FOUND)
220 ret = FALSE;
221 break;
223 if (!(slash = wcsrchr( new_path, '\\' )))
225 ret = FALSE;
226 break;
228 len = slash - new_path;
229 new_path[len] = 0;
230 if (!msi_create_full_path( package, new_path ))
232 ret = FALSE;
233 break;
235 new_path[len] = '\\';
237 msi_free( new_path );
238 return ret;
241 static void msi_file_update_ui( MSIPACKAGE *package, MSIFILE *f, const WCHAR *action )
243 MSIRECORD *uirow;
245 uirow = MSI_CreateRecord( 9 );
246 MSI_RecordSetStringW( uirow, 1, f->FileName );
247 MSI_RecordSetStringW( uirow, 9, f->Component->Directory );
248 MSI_RecordSetInteger( uirow, 6, f->FileSize );
249 MSI_ProcessMessage(package, INSTALLMESSAGE_ACTIONDATA, uirow);
250 msiobj_release( &uirow->hdr );
251 msi_ui_progress( package, 2, f->FileSize, 0, 0 );
254 static BOOL is_registered_patch_media( MSIPACKAGE *package, UINT disk_id )
256 MSIPATCHINFO *patch;
258 LIST_FOR_EACH_ENTRY( patch, &package->patches, MSIPATCHINFO, entry )
260 if (patch->disk_id == disk_id && patch->registered) return TRUE;
262 return FALSE;
265 static BOOL is_obsoleted_by_patch( MSIPACKAGE *package, MSIFILE *file )
267 if (!list_empty( &package->patches ) && file->disk_id < MSI_INITIAL_MEDIA_TRANSFORM_DISKID)
269 if (!msi_get_property_int( package->db, L"Installed", 0 )) return FALSE;
270 return TRUE;
272 if (is_registered_patch_media( package, file->disk_id )) return TRUE;
273 return FALSE;
276 static BOOL file_hash_matches( MSIPACKAGE *package, MSIFILE *file )
278 UINT r;
279 MSIFILEHASHINFO hash;
281 hash.dwFileHashInfoSize = sizeof(hash);
282 r = msi_get_filehash( package, file->TargetPath, &hash );
283 if (r != ERROR_SUCCESS)
284 return FALSE;
286 return !memcmp( &hash, &file->hash, sizeof(hash) );
289 static msi_file_state calculate_install_state( MSIPACKAGE *package, MSIFILE *file )
291 MSICOMPONENT *comp = file->Component;
292 VS_FIXEDFILEINFO *file_version;
293 WCHAR *font_version;
294 msi_file_state state;
295 DWORD size;
297 comp->Action = msi_get_component_action( package, comp );
298 if (!comp->Enabled || comp->Action != INSTALLSTATE_LOCAL || (comp->assembly && comp->assembly->installed))
300 TRACE("skipping %s (not scheduled for install)\n", debugstr_w(file->File));
301 return msifs_skipped;
303 if (is_obsoleted_by_patch( package, file ))
305 TRACE("skipping %s (obsoleted by patch)\n", debugstr_w(file->File));
306 return msifs_skipped;
308 if ((msi_is_global_assembly( comp ) && !comp->assembly->installed) ||
309 msi_get_file_attributes( package, file->TargetPath ) == INVALID_FILE_ATTRIBUTES)
311 TRACE("installing %s (missing)\n", debugstr_w(file->File));
312 return msifs_missing;
314 if (file->Version)
316 if ((file_version = msi_get_disk_file_version( package, file->TargetPath )))
318 if (msi_compare_file_versions( file_version, file->Version ) < 0)
320 TRACE("overwriting %s (new version %s old version %u.%u.%u.%u)\n",
321 debugstr_w(file->File), debugstr_w(file->Version),
322 HIWORD(file_version->dwFileVersionMS), LOWORD(file_version->dwFileVersionMS),
323 HIWORD(file_version->dwFileVersionLS), LOWORD(file_version->dwFileVersionLS));
324 state = msifs_overwrite;
326 else
328 TRACE("keeping %s (new version %s old version %u.%u.%u.%u)\n",
329 debugstr_w(file->File), debugstr_w(file->Version),
330 HIWORD(file_version->dwFileVersionMS), LOWORD(file_version->dwFileVersionMS),
331 HIWORD(file_version->dwFileVersionLS), LOWORD(file_version->dwFileVersionLS));
332 state = msifs_present;
334 msi_free( file_version );
335 return state;
337 else if ((font_version = msi_get_font_file_version( package, file->TargetPath )))
339 if (msi_compare_font_versions( font_version, file->Version ) < 0)
341 TRACE("overwriting %s (new version %s old version %s)\n",
342 debugstr_w(file->File), debugstr_w(file->Version), debugstr_w(font_version));
343 state = msifs_overwrite;
345 else
347 TRACE("keeping %s (new version %s old version %s)\n",
348 debugstr_w(file->File), debugstr_w(file->Version), debugstr_w(font_version));
349 state = msifs_present;
351 msi_free( font_version );
352 return state;
355 if ((size = msi_get_disk_file_size( package, file->TargetPath )) != file->FileSize)
357 TRACE("overwriting %s (old size %u new size %u)\n", debugstr_w(file->File), size, file->FileSize);
358 return msifs_overwrite;
360 if (file->hash.dwFileHashInfoSize)
362 if (file_hash_matches( package, file ))
364 TRACE("keeping %s (hash match)\n", debugstr_w(file->File));
365 return msifs_hashmatch;
367 else
369 TRACE("overwriting %s (hash mismatch)\n", debugstr_w(file->File));
370 return msifs_overwrite;
373 /* assume present */
374 TRACE("keeping %s\n", debugstr_w(file->File));
375 return msifs_present;
378 static void schedule_install_files(MSIPACKAGE *package)
380 MSIFILE *file;
382 LIST_FOR_EACH_ENTRY(file, &package->files, MSIFILE, entry)
384 MSICOMPONENT *comp = file->Component;
386 file->state = calculate_install_state( package, file );
387 if (file->state == msifs_overwrite && (comp->Attributes & msidbComponentAttributesNeverOverwrite))
389 TRACE("not overwriting %s\n", debugstr_w(file->TargetPath));
390 file->state = msifs_skipped;
395 static UINT copy_file( MSIPACKAGE *package, MSIFILE *file, WCHAR *source )
397 BOOL ret;
399 ret = msi_copy_file( package, source, file->TargetPath, FALSE );
400 if (!ret)
401 return GetLastError();
403 msi_set_file_attributes( package, file->TargetPath, FILE_ATTRIBUTE_NORMAL );
404 return ERROR_SUCCESS;
407 static UINT copy_install_file(MSIPACKAGE *package, MSIFILE *file, LPWSTR source)
409 UINT gle;
411 TRACE("Copying %s to %s\n", debugstr_w(source), debugstr_w(file->TargetPath));
413 gle = copy_file( package, file, source );
414 if (gle == ERROR_SUCCESS)
415 return gle;
417 if (gle == ERROR_ALREADY_EXISTS && file->state == msifs_overwrite)
419 TRACE("overwriting existing file\n");
420 return ERROR_SUCCESS;
422 else if (gle == ERROR_ACCESS_DENIED)
424 msi_set_file_attributes( package, file->TargetPath, FILE_ATTRIBUTE_NORMAL );
426 gle = copy_file( package, file, source );
427 TRACE("Overwriting existing file: %d\n", gle);
429 if (gle == ERROR_SHARING_VIOLATION || gle == ERROR_USER_MAPPED_FILE)
431 WCHAR *tmpfileW, *pathW, *p;
432 DWORD len;
434 TRACE("file in use, scheduling rename operation\n");
436 if (!(pathW = strdupW( file->TargetPath ))) return ERROR_OUTOFMEMORY;
437 if ((p = wcsrchr(pathW, '\\'))) *p = 0;
438 len = lstrlenW( pathW ) + 16;
439 if (!(tmpfileW = msi_alloc(len * sizeof(WCHAR))))
441 msi_free( pathW );
442 return ERROR_OUTOFMEMORY;
444 if (!GetTempFileNameW( pathW, L"msi", 0, tmpfileW )) tmpfileW[0] = 0;
445 msi_free( pathW );
447 if (msi_copy_file( package, source, tmpfileW, FALSE ) &&
448 msi_move_file( package, file->TargetPath, NULL, MOVEFILE_DELAY_UNTIL_REBOOT ) &&
449 msi_move_file( package, tmpfileW, file->TargetPath, MOVEFILE_DELAY_UNTIL_REBOOT ))
451 package->need_reboot_at_end = 1;
452 gle = ERROR_SUCCESS;
454 else
456 gle = GetLastError();
457 WARN("failed to schedule rename operation: %d)\n", gle);
458 DeleteFileW( tmpfileW );
460 msi_free(tmpfileW);
463 return gle;
466 static UINT create_directory( MSIPACKAGE *package, const WCHAR *dir )
468 MSIFOLDER *folder;
469 const WCHAR *install_path;
471 install_path = msi_get_target_folder( package, dir );
472 if (!install_path) return ERROR_FUNCTION_FAILED;
474 folder = msi_get_loaded_folder( package, dir );
475 if (folder->State == FOLDER_STATE_UNINITIALIZED)
477 msi_create_full_path( package, install_path );
478 folder->State = FOLDER_STATE_CREATED;
480 return ERROR_SUCCESS;
483 static MSIFILE *find_file( MSIPACKAGE *package, UINT disk_id, const WCHAR *filename )
485 MSIFILE *file;
487 LIST_FOR_EACH_ENTRY( file, &package->files, MSIFILE, entry )
489 if (file->disk_id == disk_id &&
490 file->state != msifs_installed &&
491 !wcsicmp( filename, file->File )) return file;
493 return NULL;
496 static BOOL installfiles_cb(MSIPACKAGE *package, LPCWSTR filename, DWORD action,
497 LPWSTR *path, DWORD *attrs, PVOID user)
499 MSIFILE *file = *(MSIFILE **)user;
501 if (action == MSICABEXTRACT_BEGINEXTRACT)
503 if (!(file = find_file( package, file->disk_id, filename )))
505 TRACE("unknown file in cabinet (%s)\n", debugstr_w(filename));
506 return FALSE;
508 if (file->state != msifs_missing && file->state != msifs_overwrite)
509 return FALSE;
511 if (!msi_is_global_assembly( file->Component ))
513 create_directory( package, file->Component->Directory );
515 *path = strdupW( file->TargetPath );
516 *attrs = file->Attributes;
517 *(MSIFILE **)user = file;
519 else if (action == MSICABEXTRACT_FILEEXTRACTED)
521 if (!msi_is_global_assembly( file->Component )) file->state = msifs_installed;
524 return TRUE;
527 WCHAR *msi_resolve_file_source( MSIPACKAGE *package, MSIFILE *file )
529 WCHAR *p, *path;
531 TRACE("Working to resolve source of file %s\n", debugstr_w(file->File));
533 if (file->IsCompressed) return NULL;
535 p = msi_resolve_source_folder( package, file->Component->Directory, NULL );
536 path = msi_build_directory_name( 2, p, file->ShortName );
538 if (file->LongName && msi_get_file_attributes( package, path ) == INVALID_FILE_ATTRIBUTES)
540 msi_free( path );
541 path = msi_build_directory_name( 2, p, file->LongName );
543 msi_free( p );
544 TRACE("file %s source resolves to %s\n", debugstr_w(file->File), debugstr_w(path));
545 return path;
549 * ACTION_InstallFiles()
551 * For efficiency, this is done in two passes:
552 * 1) Correct all the TargetPaths and determine what files are to be installed.
553 * 2) Extract Cabinets and copy files.
555 UINT ACTION_InstallFiles(MSIPACKAGE *package)
557 MSIMEDIAINFO *mi;
558 UINT rc = ERROR_SUCCESS;
559 MSIFILE *file;
561 msi_set_sourcedir_props(package, FALSE);
563 if (package->script == SCRIPT_NONE)
564 return msi_schedule_action(package, SCRIPT_INSTALL, L"InstallFiles");
566 schedule_install_files(package);
567 mi = msi_alloc_zero( sizeof(MSIMEDIAINFO) );
569 LIST_FOR_EACH_ENTRY( file, &package->files, MSIFILE, entry )
571 BOOL is_global_assembly = msi_is_global_assembly( file->Component );
573 msi_file_update_ui( package, file, L"InstallFiles" );
575 rc = msi_load_media_info( package, file->Sequence, mi );
576 if (rc != ERROR_SUCCESS)
578 ERR("Unable to load media info for %s (%u)\n", debugstr_w(file->File), rc);
579 rc = ERROR_FUNCTION_FAILED;
580 goto done;
583 if (file->state != msifs_hashmatch &&
584 file->state != msifs_skipped &&
585 (file->state != msifs_present || !msi_get_property_int( package->db, L"Installed", 0 )) &&
586 (rc = ready_media( package, file->IsCompressed, mi )))
588 ERR("Failed to ready media for %s\n", debugstr_w(file->File));
589 goto done;
592 if (file->state != msifs_missing && !mi->is_continuous && file->state != msifs_overwrite)
593 continue;
595 if (file->Sequence > mi->last_sequence || mi->is_continuous ||
596 (file->IsCompressed && !mi->is_extracted))
598 MSICABDATA data;
599 MSIFILE *cursor = file;
601 data.mi = mi;
602 data.package = package;
603 data.cb = installfiles_cb;
604 data.user = &cursor;
606 if (file->IsCompressed && !msi_cabextract(package, mi, &data))
608 ERR("Failed to extract cabinet: %s\n", debugstr_w(mi->cabinet));
609 rc = ERROR_INSTALL_FAILURE;
610 goto done;
614 if (!file->IsCompressed)
616 WCHAR *source = msi_resolve_file_source(package, file);
618 TRACE("copying %s to %s\n", debugstr_w(source), debugstr_w(file->TargetPath));
620 if (!is_global_assembly)
622 create_directory(package, file->Component->Directory);
624 rc = copy_install_file(package, file, source);
625 if (rc != ERROR_SUCCESS)
627 ERR("Failed to copy %s to %s (%u)\n", debugstr_w(source), debugstr_w(file->TargetPath), rc);
628 rc = ERROR_INSTALL_FAILURE;
629 msi_free(source);
630 goto done;
632 if (!is_global_assembly) file->state = msifs_installed;
633 msi_free(source);
635 else if (!is_global_assembly && file->state != msifs_installed &&
636 !(file->Attributes & msidbFileAttributesPatchAdded))
638 ERR("compressed file wasn't installed (%s)\n", debugstr_w(file->File));
639 rc = ERROR_INSTALL_FAILURE;
640 goto done;
643 LIST_FOR_EACH_ENTRY( file, &package->files, MSIFILE, entry )
645 MSICOMPONENT *comp = file->Component;
647 if (!msi_is_global_assembly( comp ) || comp->assembly->installed ||
648 (file->state != msifs_missing && file->state != msifs_overwrite)) continue;
650 rc = msi_install_assembly( package, comp );
651 if (rc != ERROR_SUCCESS)
653 ERR("Failed to install assembly\n");
654 rc = ERROR_INSTALL_FAILURE;
655 break;
657 file->state = msifs_installed;
660 done:
661 msi_free_media_info(mi);
662 return rc;
665 static MSIFILEPATCH *find_filepatch( MSIPACKAGE *package, UINT disk_id, const WCHAR *key )
667 MSIFILEPATCH *patch;
669 LIST_FOR_EACH_ENTRY( patch, &package->filepatches, MSIFILEPATCH, entry )
671 if (!patch->extracted && patch->disk_id == disk_id && !wcscmp( key, patch->File->File ))
672 return patch;
674 return NULL;
677 static BOOL patchfiles_cb(MSIPACKAGE *package, LPCWSTR file, DWORD action,
678 LPWSTR *path, DWORD *attrs, PVOID user)
680 MSIFILEPATCH *patch = *(MSIFILEPATCH **)user;
682 if (action == MSICABEXTRACT_BEGINEXTRACT)
684 MSICOMPONENT *comp;
686 if (is_registered_patch_media( package, patch->disk_id ) ||
687 !(patch = find_filepatch( package, patch->disk_id, file ))) return FALSE;
689 comp = patch->File->Component;
690 comp->Action = msi_get_component_action( package, comp );
691 if (!comp->Enabled || comp->Action != INSTALLSTATE_LOCAL)
693 TRACE("file %s component %s not installed or disabled\n",
694 debugstr_w(patch->File->File), debugstr_w(comp->Component));
695 return FALSE;
698 patch->path = msi_create_temp_file( package->db );
699 *path = strdupW( patch->path );
700 *attrs = patch->File->Attributes;
701 *(MSIFILEPATCH **)user = patch;
703 else if (action == MSICABEXTRACT_FILEEXTRACTED)
705 patch->extracted = TRUE;
708 return TRUE;
711 static UINT patch_file( MSIPACKAGE *package, MSIFILEPATCH *patch )
713 UINT r = ERROR_SUCCESS;
714 WCHAR *tmpfile = msi_create_temp_file( package->db );
716 if (!tmpfile) return ERROR_INSTALL_FAILURE;
717 if (msi_apply_filepatch( package, patch->path, patch->File->TargetPath, tmpfile ))
719 msi_delete_file( package, patch->File->TargetPath );
720 msi_move_file( package, tmpfile, patch->File->TargetPath, 0 );
722 else
724 WARN("failed to patch %s: %08x\n", debugstr_w(patch->File->TargetPath), GetLastError());
725 r = ERROR_INSTALL_FAILURE;
727 DeleteFileW( patch->path );
728 DeleteFileW( tmpfile );
729 msi_free( tmpfile );
730 return r;
733 static UINT patch_assembly( MSIPACKAGE *package, MSIASSEMBLY *assembly, MSIFILEPATCH *patch )
735 UINT r = ERROR_FUNCTION_FAILED;
736 IAssemblyName *name;
737 IAssemblyEnum *iter;
739 if (!(iter = msi_create_assembly_enum( package, assembly->display_name )))
740 return ERROR_FUNCTION_FAILED;
742 while ((IAssemblyEnum_GetNextAssembly( iter, NULL, &name, 0 ) == S_OK))
744 WCHAR *displayname, *path;
745 UINT len = 0;
746 HRESULT hr;
748 hr = IAssemblyName_GetDisplayName( name, NULL, &len, 0 );
749 if (hr != E_NOT_SUFFICIENT_BUFFER || !(displayname = msi_alloc( len * sizeof(WCHAR) )))
750 break;
752 hr = IAssemblyName_GetDisplayName( name, displayname, &len, 0 );
753 if (FAILED( hr ))
755 msi_free( displayname );
756 break;
759 if ((path = msi_get_assembly_path( package, displayname )))
761 if (!msi_copy_file( package, path, patch->File->TargetPath, FALSE ))
763 ERR("Failed to copy file %s -> %s (%u)\n", debugstr_w(path),
764 debugstr_w(patch->File->TargetPath), GetLastError() );
765 msi_free( path );
766 msi_free( displayname );
767 IAssemblyName_Release( name );
768 break;
770 r = patch_file( package, patch );
771 msi_free( path );
774 msi_free( displayname );
775 IAssemblyName_Release( name );
776 if (r == ERROR_SUCCESS) break;
779 IAssemblyEnum_Release( iter );
780 return r;
783 UINT ACTION_PatchFiles( MSIPACKAGE *package )
785 MSIFILEPATCH *patch;
786 MSIMEDIAINFO *mi;
787 UINT rc = ERROR_SUCCESS;
789 TRACE("%p\n", package);
791 if (package->script == SCRIPT_NONE)
792 return msi_schedule_action(package, SCRIPT_INSTALL, L"PatchFiles");
794 mi = msi_alloc_zero( sizeof(MSIMEDIAINFO) );
796 TRACE("extracting files\n");
798 LIST_FOR_EACH_ENTRY( patch, &package->filepatches, MSIFILEPATCH, entry )
800 MSIFILE *file = patch->File;
801 MSICOMPONENT *comp = file->Component;
803 rc = msi_load_media_info( package, patch->Sequence, mi );
804 if (rc != ERROR_SUCCESS)
806 ERR("Unable to load media info for %s (%u)\n", debugstr_w(file->File), rc);
807 rc = ERROR_FUNCTION_FAILED;
808 goto done;
810 comp->Action = msi_get_component_action( package, comp );
811 if (!comp->Enabled || comp->Action != INSTALLSTATE_LOCAL) continue;
813 if (!patch->extracted)
815 MSICABDATA data;
816 MSIFILEPATCH *cursor = patch;
818 rc = ready_media( package, TRUE, mi );
819 if (rc != ERROR_SUCCESS)
821 ERR("Failed to ready media for %s\n", debugstr_w(file->File));
822 goto done;
824 data.mi = mi;
825 data.package = package;
826 data.cb = patchfiles_cb;
827 data.user = &cursor;
829 if (!msi_cabextract( package, mi, &data ))
831 ERR("Failed to extract cabinet: %s\n", debugstr_w(mi->cabinet));
832 rc = ERROR_INSTALL_FAILURE;
833 goto done;
838 TRACE("applying patches\n");
840 LIST_FOR_EACH_ENTRY( patch, &package->filepatches, MSIFILEPATCH, entry )
842 MSICOMPONENT *comp = patch->File->Component;
844 if (!patch->path) continue;
846 if (msi_is_global_assembly( comp ))
847 rc = patch_assembly( package, comp->assembly, patch );
848 else
849 rc = patch_file( package, patch );
851 if (rc && !(patch->Attributes & msidbPatchAttributesNonVital))
853 ERR("Failed to apply patch to file: %s\n", debugstr_w(patch->File->File));
854 break;
857 if (msi_is_global_assembly( comp ))
859 if ((rc = msi_install_assembly( package, comp )))
861 ERR("Failed to install patched assembly\n");
862 break;
867 done:
868 msi_free_media_info(mi);
869 return rc;
872 #define is_dot_dir(x) ((x[0] == '.') && ((x[1] == 0) || ((x[1] == '.') && (x[2] == 0))))
874 typedef struct
876 struct list entry;
877 LPWSTR sourcename;
878 LPWSTR destname;
879 LPWSTR source;
880 LPWSTR dest;
881 } FILE_LIST;
883 static BOOL move_file( MSIPACKAGE *package, const WCHAR *source, const WCHAR *dest, int options )
885 BOOL ret;
887 if (msi_get_file_attributes( package, source ) == FILE_ATTRIBUTE_DIRECTORY ||
888 msi_get_file_attributes( package, dest ) == FILE_ATTRIBUTE_DIRECTORY)
890 WARN("Source or dest is directory, not moving\n");
891 return FALSE;
894 if (options == msidbMoveFileOptionsMove)
896 TRACE("moving %s -> %s\n", debugstr_w(source), debugstr_w(dest));
897 ret = msi_move_file( package, source, dest, MOVEFILE_REPLACE_EXISTING );
898 if (!ret)
900 WARN("msi_move_file failed: %u\n", GetLastError());
901 return FALSE;
904 else
906 TRACE("copying %s -> %s\n", debugstr_w(source), debugstr_w(dest));
907 ret = msi_copy_file( package, source, dest, FALSE );
908 if (!ret)
910 WARN("msi_copy_file failed: %u\n", GetLastError());
911 return FALSE;
915 return TRUE;
918 static WCHAR *wildcard_to_file( const WCHAR *wildcard, const WCHAR *filename )
920 const WCHAR *ptr;
921 WCHAR *path;
922 DWORD dirlen, pathlen;
924 ptr = wcsrchr(wildcard, '\\');
925 dirlen = ptr - wildcard + 1;
927 pathlen = dirlen + lstrlenW(filename) + 1;
928 if (!(path = msi_alloc(pathlen * sizeof(WCHAR)))) return NULL;
930 lstrcpynW(path, wildcard, dirlen + 1);
931 lstrcatW(path, filename);
933 return path;
936 static void free_file_entry(FILE_LIST *file)
938 msi_free(file->source);
939 msi_free(file->dest);
940 msi_free(file);
943 static void free_list(FILE_LIST *list)
945 while (!list_empty(&list->entry))
947 FILE_LIST *file = LIST_ENTRY(list_head(&list->entry), FILE_LIST, entry);
949 list_remove(&file->entry);
950 free_file_entry(file);
954 static BOOL add_wildcard( FILE_LIST *files, const WCHAR *source, WCHAR *dest )
956 FILE_LIST *new, *file;
957 WCHAR *ptr, *filename;
958 DWORD size;
960 new = msi_alloc_zero(sizeof(FILE_LIST));
961 if (!new)
962 return FALSE;
964 new->source = strdupW(source);
965 ptr = wcsrchr(dest, '\\') + 1;
966 filename = wcsrchr(new->source, '\\') + 1;
968 new->sourcename = filename;
970 if (*ptr)
971 new->destname = ptr;
972 else
973 new->destname = new->sourcename;
975 size = (ptr - dest) + lstrlenW(filename) + 1;
976 new->dest = msi_alloc(size * sizeof(WCHAR));
977 if (!new->dest)
979 free_file_entry(new);
980 return FALSE;
983 lstrcpynW(new->dest, dest, ptr - dest + 1);
984 lstrcatW(new->dest, filename);
986 if (list_empty(&files->entry))
988 list_add_head(&files->entry, &new->entry);
989 return TRUE;
992 LIST_FOR_EACH_ENTRY(file, &files->entry, FILE_LIST, entry)
994 if (wcscmp( source, file->source ) < 0)
996 list_add_before(&file->entry, &new->entry);
997 return TRUE;
1001 list_add_after(&file->entry, &new->entry);
1002 return TRUE;
1005 static BOOL move_files_wildcard( MSIPACKAGE *package, const WCHAR *source, WCHAR *dest, int options )
1007 WIN32_FIND_DATAW wfd;
1008 HANDLE hfile;
1009 LPWSTR path;
1010 BOOL res;
1011 FILE_LIST files, *file;
1012 DWORD size;
1014 hfile = msi_find_first_file( package, source, &wfd );
1015 if (hfile == INVALID_HANDLE_VALUE) return FALSE;
1017 list_init(&files.entry);
1019 for (res = TRUE; res; res = msi_find_next_file( package, hfile, &wfd ))
1021 if (is_dot_dir(wfd.cFileName)) continue;
1023 path = wildcard_to_file( source, wfd.cFileName );
1024 if (!path)
1026 res = FALSE;
1027 goto done;
1030 add_wildcard(&files, path, dest);
1031 msi_free(path);
1034 /* no files match the wildcard */
1035 if (list_empty(&files.entry))
1036 goto done;
1038 /* only the first wildcard match gets renamed to dest */
1039 file = LIST_ENTRY(list_head(&files.entry), FILE_LIST, entry);
1040 size = (wcsrchr(file->dest, '\\') - file->dest) + lstrlenW(file->destname) + 2;
1041 file->dest = msi_realloc(file->dest, size * sizeof(WCHAR));
1042 if (!file->dest)
1044 res = FALSE;
1045 goto done;
1048 /* file->dest may be shorter after the reallocation, so add a NULL
1049 * terminator. This is needed for the call to wcsrchr, as there will no
1050 * longer be a NULL terminator within the bounds of the allocation in this case.
1052 file->dest[size - 1] = '\0';
1053 lstrcpyW(wcsrchr(file->dest, '\\') + 1, file->destname);
1055 while (!list_empty(&files.entry))
1057 file = LIST_ENTRY(list_head(&files.entry), FILE_LIST, entry);
1059 move_file( package, file->source, file->dest, options );
1061 list_remove(&file->entry);
1062 free_file_entry(file);
1065 res = TRUE;
1067 done:
1068 free_list(&files);
1069 FindClose(hfile);
1070 return res;
1073 void msi_reduce_to_long_filename( WCHAR *filename )
1075 WCHAR *p = wcschr( filename, '|' );
1076 if (p) memmove( filename, p + 1, (lstrlenW( p + 1 ) + 1) * sizeof(WCHAR) );
1079 static UINT ITERATE_MoveFiles( MSIRECORD *rec, LPVOID param )
1081 MSIPACKAGE *package = param;
1082 MSIRECORD *uirow;
1083 MSICOMPONENT *comp;
1084 LPCWSTR sourcename, component;
1085 LPWSTR sourcedir, destname = NULL, destdir = NULL, source = NULL, dest = NULL;
1086 int options;
1087 DWORD size;
1088 BOOL wildcards;
1090 component = MSI_RecordGetString(rec, 2);
1091 comp = msi_get_loaded_component(package, component);
1092 if (!comp)
1093 return ERROR_SUCCESS;
1095 comp->Action = msi_get_component_action( package, comp );
1096 if (comp->Action != INSTALLSTATE_LOCAL)
1098 TRACE("component not scheduled for installation %s\n", debugstr_w(component));
1099 return ERROR_SUCCESS;
1102 sourcename = MSI_RecordGetString(rec, 3);
1103 options = MSI_RecordGetInteger(rec, 7);
1105 sourcedir = msi_dup_property(package->db, MSI_RecordGetString(rec, 5));
1106 if (!sourcedir)
1107 goto done;
1109 destdir = msi_dup_property(package->db, MSI_RecordGetString(rec, 6));
1110 if (!destdir)
1111 goto done;
1113 if (!sourcename)
1115 if (msi_get_file_attributes( package, sourcedir ) == INVALID_FILE_ATTRIBUTES)
1116 goto done;
1118 source = strdupW(sourcedir);
1119 if (!source)
1120 goto done;
1122 else
1124 size = lstrlenW(sourcedir) + lstrlenW(sourcename) + 2;
1125 source = msi_alloc(size * sizeof(WCHAR));
1126 if (!source)
1127 goto done;
1129 lstrcpyW(source, sourcedir);
1130 if (source[lstrlenW(source) - 1] != '\\')
1131 lstrcatW(source, L"\\");
1132 lstrcatW(source, sourcename);
1135 wildcards = wcschr(source, '*') || wcschr(source, '?');
1137 if (MSI_RecordIsNull(rec, 4))
1139 if (!wildcards)
1141 WCHAR *p;
1142 if (sourcename)
1143 destname = strdupW(sourcename);
1144 else if ((p = wcsrchr(sourcedir, '\\')))
1145 destname = strdupW(p + 1);
1146 else
1147 destname = strdupW(sourcedir);
1148 if (!destname)
1149 goto done;
1152 else
1154 destname = strdupW(MSI_RecordGetString(rec, 4));
1155 if (destname) msi_reduce_to_long_filename(destname);
1158 size = 0;
1159 if (destname)
1160 size = lstrlenW(destname);
1162 size += lstrlenW(destdir) + 2;
1163 dest = msi_alloc(size * sizeof(WCHAR));
1164 if (!dest)
1165 goto done;
1167 lstrcpyW(dest, destdir);
1168 if (dest[lstrlenW(dest) - 1] != '\\')
1169 lstrcatW(dest, L"\\");
1171 if (destname)
1172 lstrcatW(dest, destname);
1174 if (msi_get_file_attributes( package, destdir ) == INVALID_FILE_ATTRIBUTES)
1176 if (!msi_create_full_path( package, destdir ))
1178 WARN("failed to create directory %u\n", GetLastError());
1179 goto done;
1183 if (!wildcards)
1184 move_file( package, source, dest, options );
1185 else
1186 move_files_wildcard( package, source, dest, options );
1188 done:
1189 uirow = MSI_CreateRecord( 9 );
1190 MSI_RecordSetStringW( uirow, 1, MSI_RecordGetString(rec, 1) );
1191 MSI_RecordSetInteger( uirow, 6, 1 ); /* FIXME */
1192 MSI_RecordSetStringW( uirow, 9, destdir );
1193 MSI_ProcessMessage(package, INSTALLMESSAGE_ACTIONDATA, uirow);
1194 msiobj_release( &uirow->hdr );
1196 msi_free(sourcedir);
1197 msi_free(destdir);
1198 msi_free(destname);
1199 msi_free(source);
1200 msi_free(dest);
1202 return ERROR_SUCCESS;
1205 UINT ACTION_MoveFiles( MSIPACKAGE *package )
1207 MSIQUERY *view;
1208 UINT rc;
1210 if (package->script == SCRIPT_NONE)
1211 return msi_schedule_action(package, SCRIPT_INSTALL, L"MoveFiles");
1213 rc = MSI_DatabaseOpenViewW(package->db, L"SELECT * FROM `MoveFile`", &view);
1214 if (rc != ERROR_SUCCESS)
1215 return ERROR_SUCCESS;
1217 rc = MSI_IterateRecords(view, NULL, ITERATE_MoveFiles, package);
1218 msiobj_release(&view->hdr);
1219 return rc;
1222 static WCHAR *get_duplicate_filename( MSIPACKAGE *package, MSIRECORD *row, const WCHAR *file_key, const WCHAR *src )
1224 DWORD len;
1225 WCHAR *dst_name, *dst_path, *dst;
1227 if (MSI_RecordIsNull( row, 4 ))
1229 len = lstrlenW( src ) + 1;
1230 if (!(dst_name = msi_alloc( len * sizeof(WCHAR)))) return NULL;
1231 lstrcpyW( dst_name, wcsrchr( src, '\\' ) + 1 );
1233 else
1235 MSI_RecordGetStringW( row, 4, NULL, &len );
1236 if (!(dst_name = msi_alloc( ++len * sizeof(WCHAR) ))) return NULL;
1237 MSI_RecordGetStringW( row, 4, dst_name, &len );
1238 msi_reduce_to_long_filename( dst_name );
1241 if (MSI_RecordIsNull( row, 5 ))
1243 WCHAR *p;
1244 dst_path = strdupW( src );
1245 p = wcsrchr( dst_path, '\\' );
1246 if (p) *p = 0;
1248 else
1250 const WCHAR *dst_key = MSI_RecordGetString( row, 5 );
1252 dst_path = strdupW( msi_get_target_folder( package, dst_key ) );
1253 if (!dst_path)
1255 /* try a property */
1256 dst_path = msi_dup_property( package->db, dst_key );
1257 if (!dst_path)
1259 FIXME("Unable to get destination folder, try AppSearch properties\n");
1260 msi_free( dst_name );
1261 return NULL;
1266 dst = msi_build_directory_name( 2, dst_path, dst_name );
1267 msi_create_full_path( package, dst_path );
1269 msi_free( dst_name );
1270 msi_free( dst_path );
1271 return dst;
1274 static UINT ITERATE_DuplicateFiles(MSIRECORD *row, LPVOID param)
1276 MSIPACKAGE *package = param;
1277 LPWSTR dest;
1278 LPCWSTR file_key, component;
1279 MSICOMPONENT *comp;
1280 MSIRECORD *uirow;
1281 MSIFILE *file;
1283 component = MSI_RecordGetString(row,2);
1284 comp = msi_get_loaded_component(package, component);
1285 if (!comp)
1286 return ERROR_SUCCESS;
1288 comp->Action = msi_get_component_action( package, comp );
1289 if (comp->Action != INSTALLSTATE_LOCAL)
1291 TRACE("component not scheduled for installation %s\n", debugstr_w(component));
1292 return ERROR_SUCCESS;
1295 file_key = MSI_RecordGetString(row,3);
1296 if (!file_key)
1298 ERR("Unable to get file key\n");
1299 return ERROR_FUNCTION_FAILED;
1302 file = msi_get_loaded_file( package, file_key );
1303 if (!file)
1305 ERR("Original file unknown %s\n", debugstr_w(file_key));
1306 return ERROR_SUCCESS;
1309 dest = get_duplicate_filename( package, row, file_key, file->TargetPath );
1310 if (!dest)
1312 WARN("Unable to get duplicate filename\n");
1313 return ERROR_SUCCESS;
1316 TRACE("Duplicating file %s to %s\n", debugstr_w(file->TargetPath), debugstr_w(dest));
1317 if (!msi_copy_file( package, file->TargetPath, dest, TRUE ))
1319 WARN("Failed to copy file %s -> %s (%u)\n",
1320 debugstr_w(file->TargetPath), debugstr_w(dest), GetLastError());
1322 FIXME("We should track these duplicate files as well\n");
1324 uirow = MSI_CreateRecord( 9 );
1325 MSI_RecordSetStringW( uirow, 1, MSI_RecordGetString( row, 1 ) );
1326 MSI_RecordSetInteger( uirow, 6, file->FileSize );
1327 MSI_RecordSetStringW( uirow, 9, MSI_RecordGetString( row, 5 ) );
1328 MSI_ProcessMessage(package, INSTALLMESSAGE_ACTIONDATA, uirow);
1329 msiobj_release( &uirow->hdr );
1331 msi_free(dest);
1332 return ERROR_SUCCESS;
1335 UINT ACTION_DuplicateFiles(MSIPACKAGE *package)
1337 MSIQUERY *view;
1338 UINT rc;
1340 if (package->script == SCRIPT_NONE)
1341 return msi_schedule_action(package, SCRIPT_INSTALL, L"DuplicateFiles");
1343 rc = MSI_DatabaseOpenViewW(package->db, L"SELECT * FROM `DuplicateFile`", &view);
1344 if (rc != ERROR_SUCCESS)
1345 return ERROR_SUCCESS;
1347 rc = MSI_IterateRecords(view, NULL, ITERATE_DuplicateFiles, package);
1348 msiobj_release(&view->hdr);
1349 return rc;
1352 static UINT ITERATE_RemoveDuplicateFiles( MSIRECORD *row, LPVOID param )
1354 MSIPACKAGE *package = param;
1355 LPWSTR dest;
1356 LPCWSTR file_key, component;
1357 MSICOMPONENT *comp;
1358 MSIRECORD *uirow;
1359 MSIFILE *file;
1361 component = MSI_RecordGetString( row, 2 );
1362 comp = msi_get_loaded_component( package, component );
1363 if (!comp)
1364 return ERROR_SUCCESS;
1366 comp->Action = msi_get_component_action( package, comp );
1367 if (comp->Action != INSTALLSTATE_ABSENT)
1369 TRACE("component not scheduled for removal %s\n", debugstr_w(component));
1370 return ERROR_SUCCESS;
1373 file_key = MSI_RecordGetString( row, 3 );
1374 if (!file_key)
1376 ERR("Unable to get file key\n");
1377 return ERROR_FUNCTION_FAILED;
1380 file = msi_get_loaded_file( package, file_key );
1381 if (!file)
1383 ERR("Original file unknown %s\n", debugstr_w(file_key));
1384 return ERROR_SUCCESS;
1387 dest = get_duplicate_filename( package, row, file_key, file->TargetPath );
1388 if (!dest)
1390 WARN("Unable to get duplicate filename\n");
1391 return ERROR_SUCCESS;
1394 TRACE("Removing duplicate %s of %s\n", debugstr_w(dest), debugstr_w(file->TargetPath));
1395 if (!msi_delete_file( package, dest ))
1397 WARN("Failed to delete duplicate file %s (%u)\n", debugstr_w(dest), GetLastError());
1400 uirow = MSI_CreateRecord( 9 );
1401 MSI_RecordSetStringW( uirow, 1, MSI_RecordGetString( row, 1 ) );
1402 MSI_RecordSetStringW( uirow, 9, MSI_RecordGetString( row, 5 ) );
1403 MSI_ProcessMessage(package, INSTALLMESSAGE_ACTIONDATA, uirow);
1404 msiobj_release( &uirow->hdr );
1406 msi_free(dest);
1407 return ERROR_SUCCESS;
1410 UINT ACTION_RemoveDuplicateFiles( MSIPACKAGE *package )
1412 MSIQUERY *view;
1413 UINT rc;
1415 if (package->script == SCRIPT_NONE)
1416 return msi_schedule_action(package, SCRIPT_INSTALL, L"RemoveDuplicateFiles");
1418 rc = MSI_DatabaseOpenViewW( package->db, L"SELECT * FROM `DuplicateFile`", &view );
1419 if (rc != ERROR_SUCCESS)
1420 return ERROR_SUCCESS;
1422 rc = MSI_IterateRecords( view, NULL, ITERATE_RemoveDuplicateFiles, package );
1423 msiobj_release( &view->hdr );
1424 return rc;
1427 static BOOL verify_comp_for_removal(MSICOMPONENT *comp, UINT install_mode)
1429 /* special case */
1430 if (comp->Action != INSTALLSTATE_SOURCE &&
1431 comp->Attributes & msidbComponentAttributesSourceOnly &&
1432 (install_mode == msidbRemoveFileInstallModeOnRemove ||
1433 install_mode == msidbRemoveFileInstallModeOnBoth)) return TRUE;
1435 switch (comp->Action)
1437 case INSTALLSTATE_LOCAL:
1438 case INSTALLSTATE_SOURCE:
1439 if (install_mode == msidbRemoveFileInstallModeOnInstall ||
1440 install_mode == msidbRemoveFileInstallModeOnBoth) return TRUE;
1441 break;
1442 case INSTALLSTATE_ABSENT:
1443 if (install_mode == msidbRemoveFileInstallModeOnRemove ||
1444 install_mode == msidbRemoveFileInstallModeOnBoth) return TRUE;
1445 break;
1446 default: break;
1448 return FALSE;
1451 static UINT ITERATE_RemoveFiles(MSIRECORD *row, LPVOID param)
1453 MSIPACKAGE *package = param;
1454 MSICOMPONENT *comp;
1455 MSIRECORD *uirow;
1456 LPCWSTR component, dirprop;
1457 UINT install_mode;
1458 LPWSTR dir = NULL, path = NULL, filename = NULL;
1459 DWORD size;
1460 UINT ret = ERROR_SUCCESS;
1462 component = MSI_RecordGetString(row, 2);
1463 dirprop = MSI_RecordGetString(row, 4);
1464 install_mode = MSI_RecordGetInteger(row, 5);
1466 comp = msi_get_loaded_component(package, component);
1467 if (!comp)
1468 return ERROR_SUCCESS;
1470 comp->Action = msi_get_component_action( package, comp );
1471 if (!verify_comp_for_removal(comp, install_mode))
1473 TRACE("Skipping removal due to install mode\n");
1474 return ERROR_SUCCESS;
1476 if (comp->assembly && !comp->assembly->application)
1478 return ERROR_SUCCESS;
1480 if (comp->Attributes & msidbComponentAttributesPermanent)
1482 TRACE("permanent component, not removing file\n");
1483 return ERROR_SUCCESS;
1486 dir = msi_dup_property(package->db, dirprop);
1487 if (!dir)
1489 WARN("directory property has no value\n");
1490 return ERROR_SUCCESS;
1492 size = 0;
1493 if ((filename = strdupW( MSI_RecordGetString(row, 3) )))
1495 msi_reduce_to_long_filename( filename );
1496 size = lstrlenW( filename );
1498 size += lstrlenW(dir) + 2;
1499 path = msi_alloc(size * sizeof(WCHAR));
1500 if (!path)
1502 ret = ERROR_OUTOFMEMORY;
1503 goto done;
1506 if (filename)
1508 lstrcpyW(path, dir);
1509 PathAddBackslashW(path);
1510 lstrcatW(path, filename);
1512 TRACE("Deleting misc file: %s\n", debugstr_w(path));
1513 msi_delete_file( package, path );
1515 else
1517 TRACE("Removing misc directory: %s\n", debugstr_w(dir));
1518 msi_remove_directory( package, dir );
1521 done:
1522 uirow = MSI_CreateRecord( 9 );
1523 MSI_RecordSetStringW( uirow, 1, MSI_RecordGetString(row, 1) );
1524 MSI_RecordSetStringW( uirow, 9, dir );
1525 MSI_ProcessMessage(package, INSTALLMESSAGE_ACTIONDATA, uirow);
1526 msiobj_release( &uirow->hdr );
1528 msi_free(filename);
1529 msi_free(path);
1530 msi_free(dir);
1531 return ret;
1534 static void remove_folder( MSIFOLDER *folder )
1536 FolderList *fl;
1538 LIST_FOR_EACH_ENTRY( fl, &folder->children, FolderList, entry )
1540 remove_folder( fl->folder );
1542 if (!folder->persistent && folder->State != FOLDER_STATE_REMOVED)
1544 if (RemoveDirectoryW( folder->ResolvedTarget )) folder->State = FOLDER_STATE_REMOVED;
1548 UINT ACTION_RemoveFiles( MSIPACKAGE *package )
1550 MSIQUERY *view;
1551 MSICOMPONENT *comp;
1552 MSIFILE *file;
1553 UINT r;
1555 if (package->script == SCRIPT_NONE)
1556 return msi_schedule_action(package, SCRIPT_INSTALL, L"RemoveFiles");
1558 r = MSI_DatabaseOpenViewW(package->db, L"SELECT * FROM `RemoveFile`", &view);
1559 if (r == ERROR_SUCCESS)
1561 r = MSI_IterateRecords(view, NULL, ITERATE_RemoveFiles, package);
1562 msiobj_release(&view->hdr);
1563 if (r != ERROR_SUCCESS)
1564 return r;
1567 LIST_FOR_EACH_ENTRY( file, &package->files, MSIFILE, entry )
1569 MSIRECORD *uirow;
1570 VS_FIXEDFILEINFO *ver;
1572 comp = file->Component;
1573 msi_file_update_ui( package, file, L"RemoveFiles" );
1575 comp->Action = msi_get_component_action( package, comp );
1576 if (comp->Action != INSTALLSTATE_ABSENT || comp->Installed == INSTALLSTATE_SOURCE)
1577 continue;
1579 if (comp->assembly && !comp->assembly->application)
1580 continue;
1582 if (comp->Attributes & msidbComponentAttributesPermanent)
1584 TRACE("permanent component, not removing file\n");
1585 continue;
1588 if (file->Version)
1590 ver = msi_get_disk_file_version( package, file->TargetPath );
1591 if (ver && msi_compare_file_versions( ver, file->Version ) > 0)
1593 TRACE("newer version detected, not removing file\n");
1594 msi_free( ver );
1595 continue;
1597 msi_free( ver );
1600 if (file->state == msifs_installed)
1601 WARN("removing installed file %s\n", debugstr_w(file->TargetPath));
1603 TRACE("removing %s\n", debugstr_w(file->File) );
1605 msi_set_file_attributes( package, file->TargetPath, FILE_ATTRIBUTE_NORMAL );
1606 if (!msi_delete_file( package, file->TargetPath ))
1608 WARN("failed to delete %s (%u)\n", debugstr_w(file->TargetPath), GetLastError());
1610 file->state = msifs_missing;
1612 uirow = MSI_CreateRecord( 9 );
1613 MSI_RecordSetStringW( uirow, 1, file->FileName );
1614 MSI_RecordSetStringW( uirow, 9, comp->Directory );
1615 MSI_ProcessMessage(package, INSTALLMESSAGE_ACTIONDATA, uirow);
1616 msiobj_release( &uirow->hdr );
1619 LIST_FOR_EACH_ENTRY( comp, &package->components, MSICOMPONENT, entry )
1621 comp->Action = msi_get_component_action( package, comp );
1622 if (comp->Action != INSTALLSTATE_ABSENT) continue;
1624 if (comp->Attributes & msidbComponentAttributesPermanent)
1626 TRACE("permanent component, not removing directory\n");
1627 continue;
1629 if (comp->assembly && !comp->assembly->application)
1630 msi_uninstall_assembly( package, comp );
1631 else
1633 MSIFOLDER *folder = msi_get_loaded_folder( package, comp->Directory );
1634 if (folder) remove_folder( folder );
1637 return ERROR_SUCCESS;