1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252
|
// Copyright 2016 The Chromium Authors
// 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 <memory>
#include <set>
#include <vector>
#include "base/containers/contains.h"
#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/version.h"
#include "chrome/installer/util/util_constants.h"
namespace installer {
namespace {
using PathVector = std::vector<base::FilePath>;
using DirectorySet = std::set<base::FilePath>;
using ExecutableMap = std::map<base::FilePath, PathVector>;
// Returns the name of the version directory for executable |exe_path|.
base::FilePath GetExecutableVersionDirName(const base::FilePath& exe_path) {
std::unique_ptr<FileVersionInfo> file_version_info(
FileVersionInfo::CreateFileVersionInfo(exe_path));
if (!file_version_info.get())
return base::FilePath();
return base::FilePath::FromUTF16Unsafe(file_version_info->file_version());
}
// Returns the names of the old version directories found in |install_dir|. The
// directories named after the version of chrome.exe or new_chrome.exe are
// excluded.
DirectorySet GetOldVersionDirectories(const base::FilePath& install_dir) {
// TODO(crbug.com/40171016): Delete old version directory from all known
// locations.
const base::FilePath new_chrome_exe_version_dir_name =
GetExecutableVersionDirName(install_dir.Append(kChromeNewExe));
const base::FilePath chrome_exe_version_dir_name =
GetExecutableVersionDirName(install_dir.Append(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(directory_name.AsUTF8Unsafe());
const size_t kNumChromeVersionComponents = 4;
if (version.IsValid() &&
version.components().size() == kNumChromeVersionComponents &&
directory_name != new_chrome_exe_version_dir_name &&
directory_name != chrome_exe_version_dir_name) {
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|.
ExecutableMap GetOldExecutables(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|. Returns false if any such directories could not
// be deleted.
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 (!base::Contains(executables, directory_name)) {
const base::FilePath directory_path = install_dir.Append(directory_name);
LOG(WARNING) << "Attempting to delete stray directory "
<< directory_path.value();
if (!base::DeletePathRecursively(directory_path)) {
PLOG(ERROR) << "Failed to delete stray directory "
<< directory_path.value();
success = false;
}
}
}
return success;
}
// Deletes executables that are in |executables| and don't have a matching
// directory in |directories|. Returns false if any such files could not be
// deleted.
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 (base::Contains(directories, version_dir_name))
continue;
// Delete executables for version |version_dir_name|.
for (const auto& executable_path : executables_for_version) {
const base::FilePath executable_name = executable_path.BaseName();
LOG(WARNING) << "Attempting to delete stray executable "
<< executable_path.value();
if (!base::DeleteFile(executable_path)) {
PLOG(ERROR) << "Failed to delete stray executable "
<< executable_path.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) {
return base::File(path, base::File::FLAG_OPEN | base::File::FLAG_READ |
base::File::FLAG_WIN_EXCLUSIVE_READ |
base::File::FLAG_WIN_EXCLUSIVE_WRITE |
base::File::FLAG_WIN_SHARE_DELETE);
}
// Deletes |version_directory| and all executables in |version_executables| if
// no .exe or .dll file for the version is in use. Returns false if any file
// or directory for the version could not be deleted.
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)) {
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::DeletePathRecursively(version_directory)) {
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. Returns false if any directory/executables pair could not be
// deleted.
bool DeleteMatchingExecutablesAndDirectories(
const DirectorySet& directories,
const ExecutableMap& executables,
const base::FilePath& install_dir) {
bool success = true;
for (const auto& directory_name : directories) {
// Don't delete the version unless the directory has at least one matching
// executable.
auto version_executables_it = executables.find(directory_name);
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 old_directories = GetOldVersionDirectories(install_dir);
const ExecutableMap old_executables = GetOldExecutables(install_dir);
bool success = true;
success &= DeleteDirectoriesWithoutMatchingExecutable(
old_directories, old_executables, install_dir);
success &= DeleteExecutablesWithoutMatchingDirectory(old_directories,
old_executables);
success &= DeleteMatchingExecutablesAndDirectories(
old_directories, old_executables, install_dir);
return success;
}
} // namespace installer
|