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 These are
29 * RemoveDuplicateFiles
38 #include "wine/debug.h"
46 #include "wine/unicode.h"
48 WINE_DEFAULT_DEBUG_CHANNEL(msi);
50 static void msi_file_update_ui( MSIPACKAGE *package, MSIFILE *f, const WCHAR *action )
56 uirow = MSI_CreateRecord( 9 );
57 MSI_RecordSetStringW( uirow, 1, f->FileName );
58 uipath = strdupW( f->TargetPath );
59 p = strrchrW(uipath,'\\');
62 MSI_RecordSetStringW( uirow, 9, uipath);
63 MSI_RecordSetInteger( uirow, 6, f->FileSize );
64 ui_actiondata( package, action, uirow);
65 msiobj_release( &uirow->hdr );
67 ui_progress( package, 2, f->FileSize, 0, 0);
70 /* compares the version of a file read from the filesystem and
71 * the version specified in the File table
73 static int msi_compare_file_version(MSIFILE *file)
75 WCHAR version[MAX_PATH];
81 r = MsiGetFileVersionW(file->TargetPath, version, &size, NULL, NULL);
82 if (r != ERROR_SUCCESS)
85 return lstrcmpW(version, file->Version);
88 static void schedule_install_files(MSIPACKAGE *package)
92 LIST_FOR_EACH_ENTRY(file, &package->files, MSIFILE, entry)
94 if (file->Component->ActionRequest != INSTALLSTATE_LOCAL)
96 TRACE("File %s is not scheduled for install\n", debugstr_w(file->File));
98 ui_progress(package,2,file->FileSize,0,0);
99 file->state = msifs_skipped;
104 static UINT copy_file(MSIFILE *file, LPWSTR source)
108 ret = CopyFileW(source, file->TargetPath, FALSE);
110 return GetLastError();
112 SetFileAttributesW(file->TargetPath, FILE_ATTRIBUTE_NORMAL);
114 file->state = msifs_installed;
115 return ERROR_SUCCESS;
118 static UINT copy_install_file(MSIPACKAGE *package, MSIFILE *file, LPWSTR source)
122 TRACE("Copying %s to %s\n", debugstr_w(source),
123 debugstr_w(file->TargetPath));
125 gle = copy_file(file, source);
126 if (gle == ERROR_SUCCESS)
129 if (gle == ERROR_ALREADY_EXISTS && file->state == msifs_overwrite)
131 TRACE("overwriting existing file\n");
132 return ERROR_SUCCESS;
134 else if (gle == ERROR_ACCESS_DENIED)
136 SetFileAttributesW(file->TargetPath, FILE_ATTRIBUTE_NORMAL);
138 gle = copy_file(file, source);
139 TRACE("Overwriting existing file: %d\n", gle);
141 if (gle == ERROR_SHARING_VIOLATION || gle == ERROR_USER_MAPPED_FILE)
143 WCHAR tmpfileW[MAX_PATH], *pathW, *p;
146 TRACE("file in use, scheduling rename operation\n");
148 GetTempFileNameW(szBackSlash, szMsi, 0, tmpfileW);
149 len = strlenW(file->TargetPath) + strlenW(tmpfileW) + 1;
150 if (!(pathW = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR))))
151 return ERROR_OUTOFMEMORY;
153 strcpyW(pathW, file->TargetPath);
154 if ((p = strrchrW(pathW, '\\'))) *p = 0;
155 strcatW(pathW, tmpfileW);
157 if (CopyFileW(source, pathW, FALSE) &&
158 MoveFileExW(file->TargetPath, NULL, MOVEFILE_DELAY_UNTIL_REBOOT) &&
159 MoveFileExW(pathW, file->TargetPath, MOVEFILE_DELAY_UNTIL_REBOOT))
161 file->state = msifs_installed;
162 package->need_reboot = 1;
167 gle = GetLastError();
168 WARN("failed to schedule rename operation: %d)\n", gle);
170 HeapFree(GetProcessHeap(), 0, pathW);
176 static BOOL check_dest_hash_matches(MSIFILE *file)
178 MSIFILEHASHINFO hash;
181 if (!file->hash.dwFileHashInfoSize)
184 hash.dwFileHashInfoSize = sizeof(MSIFILEHASHINFO);
185 r = MsiGetFileHashW(file->TargetPath, 0, &hash);
186 if (r != ERROR_SUCCESS)
189 return !memcmp(&hash, &file->hash, sizeof(MSIFILEHASHINFO));
192 static BOOL installfiles_cb(MSIPACKAGE *package, LPCWSTR file, DWORD action,
193 LPWSTR *path, DWORD *attrs, PVOID user)
195 static MSIFILE *f = NULL;
197 if (action == MSICABEXTRACT_BEGINEXTRACT)
199 f = get_loaded_file(package, file);
202 WARN("unknown file in cabinet (%s)\n", debugstr_w(file));
206 if (f->state != msifs_missing && f->state != msifs_overwrite)
208 TRACE("Skipping extraction of %s\n", debugstr_w(file));
212 msi_file_update_ui(package, f, szInstallFiles);
214 *path = strdupW(f->TargetPath);
215 *attrs = f->Attributes;
217 else if (action == MSICABEXTRACT_FILEEXTRACTED)
219 f->state = msifs_installed;
227 * ACTION_InstallFiles()
229 * For efficiency, this is done in two passes:
230 * 1) Correct all the TargetPaths and determine what files are to be installed.
231 * 2) Extract Cabinets and copy files.
233 UINT ACTION_InstallFiles(MSIPACKAGE *package)
236 UINT rc = ERROR_SUCCESS;
239 /* increment progress bar each time action data is sent */
240 ui_progress(package,1,1,0,0);
242 schedule_install_files(package);
245 * Despite MSDN specifying that the CreateFolders action
246 * should be called before InstallFiles, some installers don't
247 * do that, and they seem to work correctly. We need to create
248 * directories here to make sure that the files can be copied.
250 msi_create_component_directories( package );
252 mi = msi_alloc_zero( sizeof(MSIMEDIAINFO) );
254 LIST_FOR_EACH_ENTRY( file, &package->files, MSIFILE, entry )
256 if (file->state != msifs_missing && !mi->is_continuous && file->state != msifs_overwrite)
259 if (check_dest_hash_matches(file))
261 TRACE("File hashes match, not overwriting\n");
265 if (MsiGetFileVersionW(file->TargetPath, NULL, NULL, NULL, NULL) == ERROR_SUCCESS &&
266 msi_compare_file_version(file) >= 0)
268 TRACE("Destination file version greater, not overwriting\n");
272 if (file->Sequence > mi->last_sequence || mi->is_continuous ||
273 (file->IsCompressed && !mi->is_extracted))
277 rc = ready_media(package, file, mi);
278 if (rc != ERROR_SUCCESS)
280 ERR("Failed to ready media\n");
285 data.package = package;
286 data.cb = installfiles_cb;
289 if (file->IsCompressed &&
290 !msi_cabextract(package, mi, &data))
292 ERR("Failed to extract cabinet: %s\n", debugstr_w(mi->cabinet));
293 rc = ERROR_FUNCTION_FAILED;
298 if (!file->IsCompressed)
300 LPWSTR source = resolve_file_source(package, file);
302 TRACE("file paths %s to %s\n", debugstr_w(source),
303 debugstr_w(file->TargetPath));
305 msi_file_update_ui(package, file, szInstallFiles);
306 rc = copy_install_file(package, file, source);
307 if (rc != ERROR_SUCCESS)
309 ERR("Failed to copy %s to %s (%d)\n", debugstr_w(source),
310 debugstr_w(file->TargetPath), rc);
311 rc = ERROR_INSTALL_FAILURE;
318 else if (file->state != msifs_installed)
320 ERR("compressed file wasn't extracted (%s)\n",
321 debugstr_w(file->TargetPath));
322 rc = ERROR_INSTALL_FAILURE;
327 msi_free_media_info(mi);
331 static WCHAR *get_duplicate_filename( MSIPACKAGE *package, MSIRECORD *row, const WCHAR *file_key, const WCHAR *src )
334 WCHAR *dst_name, *dst_path, *dst;
336 if (MSI_RecordIsNull( row, 4 ))
338 len = strlenW( src ) + 1;
339 if (!(dst_name = msi_alloc( len * sizeof(WCHAR)))) return NULL;
340 strcpyW( dst_name, strrchrW( src, '\\' ) + 1 );
344 MSI_RecordGetStringW( row, 4, NULL, &len );
345 if (!(dst_name = msi_alloc( ++len * sizeof(WCHAR) ))) return NULL;
346 MSI_RecordGetStringW( row, 4, dst_name, &len );
347 reduce_to_longfilename( dst_name );
350 if (MSI_RecordIsNull( row, 5 ))
353 dst_path = strdupW( src );
354 p = strrchrW( dst_path, '\\' );
359 const WCHAR *dst_key = MSI_RecordGetString( row, 5 );
361 dst_path = resolve_folder( package, dst_key, FALSE, FALSE, TRUE, NULL );
365 dst_path = msi_dup_property( package, dst_key );
368 FIXME("Unable to get destination folder, try AppSearch properties\n");
369 msi_free( dst_name );
375 dst = build_directory_name( 2, dst_path, dst_name );
376 create_full_pathW( dst_path );
378 msi_free( dst_name );
379 msi_free( dst_path );
383 static UINT ITERATE_DuplicateFiles(MSIRECORD *row, LPVOID param)
385 MSIPACKAGE *package = param;
387 LPCWSTR file_key, component;
391 component = MSI_RecordGetString(row,2);
392 comp = get_loaded_component(package,component);
394 return ERROR_SUCCESS;
396 if (comp->ActionRequest != INSTALLSTATE_LOCAL)
398 TRACE("Component not scheduled for installation %s\n", debugstr_w(component));
399 comp->Action = comp->Installed;
400 return ERROR_SUCCESS;
402 comp->Action = INSTALLSTATE_LOCAL;
404 file_key = MSI_RecordGetString(row,3);
407 ERR("Unable to get file key\n");
408 return ERROR_FUNCTION_FAILED;
411 file = get_loaded_file( package, file_key );
414 ERR("Original file unknown %s\n", debugstr_w(file_key));
415 return ERROR_SUCCESS;
418 dest = get_duplicate_filename( package, row, file_key, file->TargetPath );
421 WARN("Unable to get duplicate filename\n");
422 return ERROR_SUCCESS;
425 TRACE("Duplicating file %s to %s\n", debugstr_w(file->TargetPath), debugstr_w(dest));
427 if (!CopyFileW( file->TargetPath, dest, TRUE ))
429 WARN("Failed to copy file %s -> %s (%u)\n",
430 debugstr_w(file->TargetPath), debugstr_w(dest), GetLastError());
433 FIXME("We should track these duplicate files as well\n");
435 msi_file_update_ui(package, file, szDuplicateFiles);
438 return ERROR_SUCCESS;
441 UINT ACTION_DuplicateFiles(MSIPACKAGE *package)
445 static const WCHAR ExecSeqQuery[] =
446 {'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
447 '`','D','u','p','l','i','c','a','t','e','F','i','l','e','`',0};
449 rc = MSI_DatabaseOpenViewW(package->db, ExecSeqQuery, &view);
450 if (rc != ERROR_SUCCESS)
451 return ERROR_SUCCESS;
453 rc = MSI_IterateRecords(view, NULL, ITERATE_DuplicateFiles, package);
454 msiobj_release(&view->hdr);
459 static UINT ITERATE_RemoveDuplicateFiles( MSIRECORD *row, LPVOID param )
461 MSIPACKAGE *package = param;
463 LPCWSTR file_key, component;
467 component = MSI_RecordGetString( row, 2 );
468 comp = get_loaded_component( package, component );
470 return ERROR_SUCCESS;
472 if (comp->ActionRequest != INSTALLSTATE_ABSENT)
474 TRACE("Component not scheduled for removal %s\n", debugstr_w(component));
475 comp->Action = comp->Installed;
476 return ERROR_SUCCESS;
478 comp->Action = INSTALLSTATE_ABSENT;
480 file_key = MSI_RecordGetString( row, 3 );
483 ERR("Unable to get file key\n");
484 return ERROR_FUNCTION_FAILED;
487 file = get_loaded_file( package, file_key );
490 ERR("Original file unknown %s\n", debugstr_w(file_key));
491 return ERROR_SUCCESS;
494 dest = get_duplicate_filename( package, row, file_key, file->TargetPath );
497 WARN("Unable to get duplicate filename\n");
498 return ERROR_SUCCESS;
501 TRACE("Removing duplicate %s of %s\n", debugstr_w(dest), debugstr_w(file->TargetPath));
503 if (!DeleteFileW( dest ))
505 WARN("Failed to delete duplicate file %s (%u)\n", debugstr_w(dest), GetLastError());
508 msi_file_update_ui( package, file, szRemoveDuplicateFiles );
511 return ERROR_SUCCESS;
514 UINT ACTION_RemoveDuplicateFiles( MSIPACKAGE *package )
518 static const WCHAR query[] =
519 {'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
520 '`','D','u','p','l','i','c','a','t','e','F','i','l','e','`',0};
522 rc = MSI_DatabaseOpenViewW( package->db, query, &view );
523 if (rc != ERROR_SUCCESS)
524 return ERROR_SUCCESS;
526 rc = MSI_IterateRecords( view, NULL, ITERATE_RemoveDuplicateFiles, package );
527 msiobj_release( &view->hdr );
532 static BOOL verify_comp_for_removal(MSICOMPONENT *comp, UINT install_mode)
534 INSTALLSTATE request = comp->ActionRequest;
536 if (request == INSTALLSTATE_UNKNOWN)
539 if (install_mode == msidbRemoveFileInstallModeOnInstall &&
540 (request == INSTALLSTATE_LOCAL || request == INSTALLSTATE_SOURCE))
543 if (request == INSTALLSTATE_ABSENT)
545 if (!comp->ComponentId)
548 if (install_mode == msidbRemoveFileInstallModeOnRemove)
552 if (install_mode == msidbRemoveFileInstallModeOnBoth)
558 static UINT ITERATE_RemoveFiles(MSIRECORD *row, LPVOID param)
560 MSIPACKAGE *package = param;
562 LPCWSTR component, filename, dirprop;
564 LPWSTR dir = NULL, path = NULL;
568 component = MSI_RecordGetString(row, 2);
569 filename = MSI_RecordGetString(row, 3);
570 dirprop = MSI_RecordGetString(row, 4);
571 install_mode = MSI_RecordGetInteger(row, 5);
573 comp = get_loaded_component(package, component);
576 ERR("Invalid component: %s\n", debugstr_w(component));
577 return ERROR_FUNCTION_FAILED;
580 if (!verify_comp_for_removal(comp, install_mode))
582 TRACE("Skipping removal due to missing conditions\n");
583 comp->Action = comp->Installed;
584 return ERROR_SUCCESS;
587 dir = msi_dup_property(package, dirprop);
589 return ERROR_OUTOFMEMORY;
591 size = (filename != NULL) ? lstrlenW(filename) : 0;
592 size += lstrlenW(dir) + 2;
593 path = msi_alloc(size * sizeof(WCHAR));
596 r = ERROR_OUTOFMEMORY;
603 PathAddBackslashW(path);
604 lstrcatW(path, filename);
606 TRACE("Deleting misc file: %s\n", debugstr_w(path));
611 TRACE("Removing misc directory: %s\n", debugstr_w(dir));
612 RemoveDirectoryW(dir);
618 return ERROR_SUCCESS;
621 UINT ACTION_RemoveFiles( MSIPACKAGE *package )
627 static const WCHAR query[] = {
628 'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
629 '`','R','e','m','o','v','e','F','i','l','e','`',0};
630 static const WCHAR folder_query[] = {
631 'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
632 '`','C','r','e','a','t','e','F','o','l','d','e','r','`',0};
634 r = MSI_DatabaseOpenViewW(package->db, query, &view);
635 if (r == ERROR_SUCCESS)
637 MSI_IterateRecords(view, NULL, ITERATE_RemoveFiles, package);
638 msiobj_release(&view->hdr);
641 r = MSI_DatabaseOpenViewW(package->db, folder_query, &view);
642 if (r == ERROR_SUCCESS)
643 msiobj_release(&view->hdr);
645 LIST_FOR_EACH_ENTRY( file, &package->files, MSIFILE, entry )
648 LPWSTR dir, uipath, p;
650 if ( file->state == msifs_installed )
651 ERR("removing installed file %s\n", debugstr_w(file->TargetPath));
653 if ( file->Component->ActionRequest != INSTALLSTATE_ABSENT ||
654 file->Component->Installed == INSTALLSTATE_SOURCE )
657 /* don't remove a file if the old file
658 * is strictly newer than the version to be installed
660 if ( msi_compare_file_version( file ) < 0 )
663 TRACE("removing %s\n", debugstr_w(file->File) );
664 if (!DeleteFileW( file->TargetPath ))
666 WARN("failed to delete %s\n", debugstr_w(file->TargetPath));
668 /* FIXME: check persistence for each directory */
669 else if (r && (dir = strdupW( file->TargetPath )))
671 if ((p = strrchrW( dir, '\\' ))) *p = 0;
672 RemoveDirectoryW( dir );
675 file->state = msifs_missing;
678 uirow = MSI_CreateRecord( 9 );
679 MSI_RecordSetStringW( uirow, 1, file->FileName );
680 uipath = strdupW( file->TargetPath );
681 p = strrchrW(uipath,'\\');
684 MSI_RecordSetStringW( uirow, 9, uipath);
685 ui_actiondata( package, szRemoveFiles, uirow);
686 msiobj_release( &uirow->hdr );
688 /* FIXME: call ui_progress here? */
691 return ERROR_SUCCESS;