Index: chrome/installer/util/delete_old_versions.cc |
diff --git a/chrome/installer/util/delete_old_versions.cc b/chrome/installer/util/delete_old_versions.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..9f4da97058a9b237177f8cb539d400d47d7fecb5 |
--- /dev/null |
+++ b/chrome/installer/util/delete_old_versions.cc |
@@ -0,0 +1,254 @@ |
+// Copyright 2016 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "chrome/installer/util/delete_old_versions.h" |
+ |
+#include <map> |
+#include <set> |
+#include <vector> |
+ |
+#include "base/file_version_info.h" |
+#include "base/files/file.h" |
+#include "base/files/file_enumerator.h" |
+#include "base/files/file_path.h" |
+#include "base/files/file_util.h" |
+#include "base/logging.h" |
+#include "base/memory/scoped_ptr.h" |
+#include "base/stl_util.h" |
+#include "base/strings/string16.h" |
+#include "base/strings/utf_string_conversions.h" |
+#include "base/version.h" |
+#include "chrome/installer/util/callback_work_item.h" |
+#include "chrome/installer/util/util_constants.h" |
+ |
+namespace { |
+ |
+using PathVector = std::vector<base::FilePath>; |
+ |
+// Returns the name of the version directory for executable |exe_path|. |
+base::FilePath GetExecutableVersionDirName(const base::FilePath& exe_path) { |
+ scoped_ptr<FileVersionInfo> file_version_info( |
+ FileVersionInfo::CreateFileVersionInfo(exe_path)); |
+ if (!file_version_info.get()) |
+ return base::FilePath(); |
+ return base::FilePath(file_version_info->file_version()); |
+} |
+ |
+// Returns the names of the version directories found in |install_dir|. The |
+// directories named after the version of chrome.exe or new_chrome.exe are |
+// excluded. |
+using DirectorySet = std::set<base::FilePath>; |
grt (UTC plus 2)
2016/02/24 20:06:17
nit: move all of these aliases up to the top of th
fdoray
2016/02/25 18:26:47
Done.
|
+DirectorySet GetDirectories(const base::FilePath& install_dir) { |
grt (UTC plus 2)
2016/02/24 20:06:16
nit: GetVersionDirectories since it is careful not
fdoray
2016/02/25 18:26:47
Done.
|
+ const base::FilePath new_chrome_exe_version_dir_name = |
+ GetExecutableVersionDirName(install_dir.Append(installer::kChromeNewExe)); |
+ const base::FilePath chrome_exe_version_dir_name = |
+ GetExecutableVersionDirName(install_dir.Append(installer::kChromeExe)); |
+ |
+ DirectorySet directories; |
+ base::FileEnumerator enum_directories(install_dir, false, |
+ base::FileEnumerator::DIRECTORIES); |
+ for (base::FilePath directory_path = enum_directories.Next(); |
+ !directory_path.empty(); directory_path = enum_directories.Next()) { |
+ const base::FilePath directory_name = directory_path.BaseName(); |
+ const base::Version version(base::UTF16ToUTF8(directory_name.value())); |
grt (UTC plus 2)
2016/02/24 20:06:16
base::UTF16ToUTF8(directory_name.value()) -> direc
fdoray
2016/02/25 18:26:47
Done.
|
+ const size_t kNumChromeVersionComponents = 4; |
+ if (directory_name != new_chrome_exe_version_dir_name && |
grt (UTC plus 2)
2016/02/24 20:06:16
nit: version.IsValid() and the size check are both
fdoray
2016/02/25 18:26:47
Done.
|
+ directory_name != chrome_exe_version_dir_name && version.IsValid() && |
+ version.components().size() == kNumChromeVersionComponents) { |
+ directories.insert(directory_name); |
+ } |
+ } |
+ return directories; |
+} |
+ |
+// Returns a map where the keys are version directory names and values are paths |
+// of old_chrome*.exe executables found in |install_dir|. |
+using ExecutableMap = std::map<base::FilePath, PathVector>; |
+ExecutableMap GetExecutables(const base::FilePath& install_dir) { |
+ ExecutableMap executables; |
+ base::FileEnumerator enum_executables(install_dir, false, |
+ base::FileEnumerator::FILES, |
+ FILE_PATH_LITERAL("old_chrome*.exe")); |
+ for (base::FilePath exe_path = enum_executables.Next(); !exe_path.empty(); |
+ exe_path = enum_executables.Next()) { |
+ executables[GetExecutableVersionDirName(exe_path)].push_back(exe_path); |
+ } |
+ return executables; |
+} |
+ |
+// Deletes directories that are in |directories| and don't have a matching |
+// executable in |executables|. |
grt (UTC plus 2)
2016/02/24 20:06:17
"Returns false if any such directories could not b
fdoray
2016/02/25 18:26:47
Done.
|
+bool DeleteDirectoriesWithoutMatchingExecutable( |
+ const DirectorySet& directories, |
+ const ExecutableMap& executables, |
+ const base::FilePath& install_dir) { |
+ bool success = true; |
+ for (const base::FilePath& directory_name : directories) { |
+ // Delete the directory if it doesn't have a matching executable. |
+ if (!ContainsKey(executables, directory_name)) { |
+ LOG(WARNING) << "Attempting to delete stray directory " |
+ << directory_name.value(); |
+ if (!base::DeleteFile(install_dir.Append(directory_name), true)) { |
grt (UTC plus 2)
2016/02/24 20:06:16
nit: put install_dir.Append(directory_name) in a l
fdoray
2016/02/25 18:26:47
Done.
|
+ PLOG(ERROR) << "Failed to delete stray directory " |
+ << directory_name.value(); |
+ success = false; |
+ } |
+ } |
+ } |
+ return success; |
+} |
+ |
+// Deletes executables that are in |executables| and don't have a matching |
+// directory in |directories|. |
grt (UTC plus 2)
2016/02/24 20:06:17
"Returns false if any such files could not be dele
fdoray
2016/02/25 18:26:47
Done.
|
+bool DeleteExecutablesWithoutMatchingDirectory( |
+ const DirectorySet& directories, |
+ const ExecutableMap& executables) { |
+ bool success = true; |
+ for (const auto& version_and_executables : executables) { |
+ const auto& version_dir_name = version_and_executables.first; |
+ const auto& executables_for_version = version_and_executables.second; |
+ |
+ // Don't delete the executables if they have a matching directory. |
+ if (ContainsValue(directories, version_dir_name)) |
+ continue; |
+ |
+ // Delete executables for version |version|. |
grt (UTC plus 2)
2016/02/24 20:06:17
version_dir_name
fdoray
2016/02/25 18:26:47
Done.
|
+ for (const auto& executable_path : executables_for_version) { |
+ const base::FilePath executable_name = executable_path.BaseName(); |
grt (UTC plus 2)
2016/02/24 20:06:17
nit: use executable_path in the log messages
fdoray
2016/02/25 18:26:47
Done.
|
+ LOG(WARNING) << "Attempting to delete stray executable " |
+ << executable_name.value(); |
+ if (!base::DeleteFile(executable_path, false)) { |
+ PLOG(ERROR) << "Failed to delete stray executable " |
+ << executable_name.value(); |
+ success = false; |
+ } |
+ } |
+ } |
+ return success; |
+} |
+ |
+// Opens |path| with options that prevent the file from being read or written |
+// via another handle. As long as the returned object is alive, it is guaranteed |
+// that |path| isn't in use. It can however be deleted. |
+base::File GetFileLock(const base::FilePath path) { |
grt (UTC plus 2)
2016/02/24 20:06:17
const base::FilePath&
fdoray
2016/02/25 18:26:47
Done.
|
+ return base::File(path, base::File::FLAG_OPEN | base::File::FLAG_READ | |
+ base::File::FLAG_EXCLUSIVE_READ | |
+ base::File::FLAG_EXCLUSIVE_WRITE | |
+ base::File::FLAG_SHARE_DELETE); |
+} |
+ |
+// Deletes |version_directory| and all executables in |version_executables| if |
+// no .exe or .dll file for the version is in use. |
+bool DeleteVersion(const base::FilePath& version_directory, |
+ const PathVector& version_executables) { |
+ std::vector<base::File> locks; |
+ PathVector locked_file_paths; |
+ |
+ // Lock .exe/.dll files in |version_directory|. |
+ base::FileEnumerator enum_version_directory(version_directory, true, |
+ base::FileEnumerator::FILES); |
+ for (base::FilePath path = enum_version_directory.Next(); !path.empty(); |
+ path = enum_version_directory.Next()) { |
+ if (!path.MatchesExtension(FILE_PATH_LITERAL(".exe")) && |
+ !path.MatchesExtension(FILE_PATH_LITERAL(".dll"))) { |
+ continue; |
+ } |
+ locks.push_back(GetFileLock(path)); |
+ if (!locks.back().IsValid()) { |
+ LOG(WARNING) << "Failed to delete old version " |
+ << version_directory.value() << " because " << path.value() |
+ << " is in use."; |
+ return false; |
+ } |
+ locked_file_paths.push_back(path); |
+ } |
+ |
+ // Lock executables in |version_executables|. |
+ for (const base::FilePath& executable_path : version_executables) { |
+ locks.push_back(GetFileLock(executable_path)); |
+ if (!locks.back().IsValid()) { |
+ LOG(WARNING) << "Failed to delete old version " |
+ << version_directory.value() << " because " |
+ << executable_path.value() << " is in use."; |
+ return false; |
+ } |
+ locked_file_paths.push_back(executable_path); |
+ } |
+ |
+ bool success = true; |
+ |
+ // Delete locked files. The files won't actually be deleted until the locks |
+ // are released. |
+ for (const base::FilePath& locked_file_path : locked_file_paths) { |
+ if (!base::DeleteFile(locked_file_path, false)) { |
+ PLOG(ERROR) << "Failed to delete locked file " |
+ << locked_file_path.value(); |
+ success = false; |
+ } |
+ } |
+ |
+ // Release the locks, causing the locked files to actually be deleted. The |
+ // version directory can't be deleted before this is done. |
+ locks.clear(); |
+ |
+ // Delete the version directory. |
+ if (!base::DeleteFile(version_directory, true)) { |
+ PLOG(ERROR) << "Failed to delete version directory " |
+ << version_directory.value(); |
+ success = false; |
+ } |
+ |
+ return success; |
+} |
+ |
+// For each executable in |executables| that has a matching directory in |
+// |directories|, tries to delete the executable and the matching directory. No |
+// deletion occurs for a given version if a .exe or .dll file for that version |
+// is in use. |
+bool DeleteMatchingExecutablesAndDirectories( |
+ const DirectorySet& directories, |
+ const ExecutableMap& executables, |
+ const base::FilePath& install_dir) { |
+ bool success = true; |
+ for (const auto& directory_name : directories) { |
+ ExecutableMap::const_iterator version_executables_it = |
grt (UTC plus 2)
2016/02/24 20:06:17
auto?
fdoray
2016/02/25 18:26:47
Done.
|
+ executables.find(directory_name); |
+ |
+ // Don't delete the version unless the directory has at least one matching |
grt (UTC plus 2)
2016/02/24 20:06:17
nit: move this comment above the call to find()
fdoray
2016/02/25 18:26:47
Done.
|
+ // executable. |
+ if (version_executables_it == executables.end()) |
+ continue; |
+ |
+ // Try to delete all files for the version. |
+ success &= DeleteVersion(install_dir.Append(directory_name), |
+ version_executables_it->second); |
+ } |
+ return success; |
+} |
+ |
+} // namespace |
+ |
+bool DeleteOldVersions(const base::FilePath& install_dir) { |
+ const DirectorySet directories = GetDirectories(install_dir); |
+ const ExecutableMap executables = GetExecutables(install_dir); |
+ |
+ bool success = true; |
+ success &= DeleteDirectoriesWithoutMatchingExecutable( |
+ directories, executables, install_dir); |
+ success &= |
+ DeleteExecutablesWithoutMatchingDirectory(directories, executables); |
+ success &= DeleteMatchingExecutablesAndDirectories(directories, executables, |
+ install_dir); |
+ |
+ return success; |
+} |
+ |
+bool DeleteOldVersionsCallback(const base::FilePath& install_dir, |
+ const CallbackWorkItem& work_item) { |
+ // No rollback is possible for this action. |
+ if (work_item.IsRollback()) |
+ return true; |
+ |
+ return DeleteOldVersions(install_dir); |
+} |