OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "chrome/installer/util/delete_old_versions.h" |
| 6 |
| 7 #include <map> |
| 8 #include <set> |
| 9 #include <vector> |
| 10 |
| 11 #include "base/bind.h" |
| 12 #include "base/file_version_info.h" |
| 13 #include "base/files/file.h" |
| 14 #include "base/files/file_enumerator.h" |
| 15 #include "base/files/file_path.h" |
| 16 #include "base/files/file_util.h" |
| 17 #include "base/logging.h" |
| 18 #include "base/memory/scoped_ptr.h" |
| 19 #include "base/stl_util.h" |
| 20 #include "base/strings/string16.h" |
| 21 #include "base/version.h" |
| 22 #include "chrome/installer/util/callback_work_item.h" |
| 23 #include "chrome/installer/util/installer_state.h" |
| 24 #include "chrome/installer/util/util_constants.h" |
| 25 #include "chrome/installer/util/work_item_list.h" |
| 26 |
| 27 namespace installer { |
| 28 |
| 29 namespace { |
| 30 |
| 31 using PathVector = std::vector<base::FilePath>; |
| 32 using DirectorySet = std::set<base::FilePath>; |
| 33 using ExecutableMap = std::map<base::FilePath, PathVector>; |
| 34 |
| 35 // Returns the name of the version directory for executable |exe_path|. |
| 36 base::FilePath GetExecutableVersionDirName(const base::FilePath& exe_path) { |
| 37 scoped_ptr<FileVersionInfo> file_version_info( |
| 38 FileVersionInfo::CreateFileVersionInfo(exe_path)); |
| 39 if (!file_version_info.get()) |
| 40 return base::FilePath(); |
| 41 return base::FilePath(file_version_info->file_version()); |
| 42 } |
| 43 |
| 44 // Returns the names of the old version directories found in |install_dir|. The |
| 45 // directories named after the version of chrome.exe or new_chrome.exe are |
| 46 // excluded. |
| 47 DirectorySet GetOldVersionDirectories(const base::FilePath& install_dir) { |
| 48 const base::FilePath new_chrome_exe_version_dir_name = |
| 49 GetExecutableVersionDirName(install_dir.Append(kChromeNewExe)); |
| 50 const base::FilePath chrome_exe_version_dir_name = |
| 51 GetExecutableVersionDirName(install_dir.Append(kChromeExe)); |
| 52 |
| 53 DirectorySet directories; |
| 54 base::FileEnumerator enum_directories(install_dir, false, |
| 55 base::FileEnumerator::DIRECTORIES); |
| 56 for (base::FilePath directory_path = enum_directories.Next(); |
| 57 !directory_path.empty(); directory_path = enum_directories.Next()) { |
| 58 const base::FilePath directory_name = directory_path.BaseName(); |
| 59 const base::Version version(directory_name.AsUTF8Unsafe()); |
| 60 const size_t kNumChromeVersionComponents = 4; |
| 61 if (version.IsValid() && |
| 62 version.components().size() == kNumChromeVersionComponents && |
| 63 directory_name != new_chrome_exe_version_dir_name && |
| 64 directory_name != chrome_exe_version_dir_name) { |
| 65 directories.insert(directory_name); |
| 66 } |
| 67 } |
| 68 return directories; |
| 69 } |
| 70 |
| 71 // Returns a map where the keys are version directory names and values are paths |
| 72 // of old_chrome*.exe executables found in |install_dir|. |
| 73 ExecutableMap GetOldExecutables(const base::FilePath& install_dir) { |
| 74 ExecutableMap executables; |
| 75 base::FileEnumerator enum_executables(install_dir, false, |
| 76 base::FileEnumerator::FILES, |
| 77 FILE_PATH_LITERAL("old_chrome*.exe")); |
| 78 for (base::FilePath exe_path = enum_executables.Next(); !exe_path.empty(); |
| 79 exe_path = enum_executables.Next()) { |
| 80 executables[GetExecutableVersionDirName(exe_path)].push_back(exe_path); |
| 81 } |
| 82 return executables; |
| 83 } |
| 84 |
| 85 // Deletes directories that are in |directories| and don't have a matching |
| 86 // executable in |executables|. Returns false if any such directories could not |
| 87 // be deleted. |
| 88 bool DeleteDirectoriesWithoutMatchingExecutable( |
| 89 const DirectorySet& directories, |
| 90 const ExecutableMap& executables, |
| 91 const base::FilePath& install_dir) { |
| 92 bool success = true; |
| 93 for (const base::FilePath& directory_name : directories) { |
| 94 // Delete the directory if it doesn't have a matching executable. |
| 95 if (!ContainsKey(executables, directory_name)) { |
| 96 const base::FilePath directory_path = install_dir.Append(directory_name); |
| 97 LOG(WARNING) << "Attempting to delete stray directory " |
| 98 << directory_path.value(); |
| 99 if (!base::DeleteFile(directory_path, true)) { |
| 100 PLOG(ERROR) << "Failed to delete stray directory " |
| 101 << directory_path.value(); |
| 102 success = false; |
| 103 } |
| 104 } |
| 105 } |
| 106 return success; |
| 107 } |
| 108 |
| 109 // Deletes executables that are in |executables| and don't have a matching |
| 110 // directory in |directories|. Returns false if any such files could not be |
| 111 // deleted. |
| 112 bool DeleteExecutablesWithoutMatchingDirectory( |
| 113 const DirectorySet& directories, |
| 114 const ExecutableMap& executables) { |
| 115 bool success = true; |
| 116 for (const auto& version_and_executables : executables) { |
| 117 const auto& version_dir_name = version_and_executables.first; |
| 118 const auto& executables_for_version = version_and_executables.second; |
| 119 |
| 120 // Don't delete the executables if they have a matching directory. |
| 121 if (ContainsValue(directories, version_dir_name)) |
| 122 continue; |
| 123 |
| 124 // Delete executables for version |version_dir_name|. |
| 125 for (const auto& executable_path : executables_for_version) { |
| 126 const base::FilePath executable_name = executable_path.BaseName(); |
| 127 LOG(WARNING) << "Attempting to delete stray executable " |
| 128 << executable_path.value(); |
| 129 if (!base::DeleteFile(executable_path, false)) { |
| 130 PLOG(ERROR) << "Failed to delete stray executable " |
| 131 << executable_path.value(); |
| 132 success = false; |
| 133 } |
| 134 } |
| 135 } |
| 136 return success; |
| 137 } |
| 138 |
| 139 // Opens |path| with options that prevent the file from being read or written |
| 140 // via another handle. As long as the returned object is alive, it is guaranteed |
| 141 // that |path| isn't in use. It can however be deleted. |
| 142 base::File GetFileLock(const base::FilePath& path) { |
| 143 return base::File(path, base::File::FLAG_OPEN | base::File::FLAG_READ | |
| 144 base::File::FLAG_EXCLUSIVE_READ | |
| 145 base::File::FLAG_EXCLUSIVE_WRITE | |
| 146 base::File::FLAG_SHARE_DELETE); |
| 147 } |
| 148 |
| 149 // Deletes |version_directory| and all executables in |version_executables| if |
| 150 // no .exe or .dll file for the version is in use. Returns false if any file |
| 151 // or directory for the version could not be deleted. |
| 152 bool DeleteVersion(const base::FilePath& version_directory, |
| 153 const PathVector& version_executables) { |
| 154 std::vector<base::File> locks; |
| 155 PathVector locked_file_paths; |
| 156 |
| 157 // Lock .exe/.dll files in |version_directory|. |
| 158 base::FileEnumerator enum_version_directory(version_directory, true, |
| 159 base::FileEnumerator::FILES); |
| 160 for (base::FilePath path = enum_version_directory.Next(); !path.empty(); |
| 161 path = enum_version_directory.Next()) { |
| 162 if (!path.MatchesExtension(FILE_PATH_LITERAL(".exe")) && |
| 163 !path.MatchesExtension(FILE_PATH_LITERAL(".dll"))) { |
| 164 continue; |
| 165 } |
| 166 locks.push_back(GetFileLock(path)); |
| 167 if (!locks.back().IsValid()) { |
| 168 LOG(WARNING) << "Failed to delete old version " |
| 169 << version_directory.value() << " because " << path.value() |
| 170 << " is in use."; |
| 171 return false; |
| 172 } |
| 173 locked_file_paths.push_back(path); |
| 174 } |
| 175 |
| 176 // Lock executables in |version_executables|. |
| 177 for (const base::FilePath& executable_path : version_executables) { |
| 178 locks.push_back(GetFileLock(executable_path)); |
| 179 if (!locks.back().IsValid()) { |
| 180 LOG(WARNING) << "Failed to delete old version " |
| 181 << version_directory.value() << " because " |
| 182 << executable_path.value() << " is in use."; |
| 183 return false; |
| 184 } |
| 185 locked_file_paths.push_back(executable_path); |
| 186 } |
| 187 |
| 188 bool success = true; |
| 189 |
| 190 // Delete locked files. The files won't actually be deleted until the locks |
| 191 // are released. |
| 192 for (const base::FilePath& locked_file_path : locked_file_paths) { |
| 193 if (!base::DeleteFile(locked_file_path, false)) { |
| 194 PLOG(ERROR) << "Failed to delete locked file " |
| 195 << locked_file_path.value(); |
| 196 success = false; |
| 197 } |
| 198 } |
| 199 |
| 200 // Release the locks, causing the locked files to actually be deleted. The |
| 201 // version directory can't be deleted before this is done. |
| 202 locks.clear(); |
| 203 |
| 204 // Delete the version directory. |
| 205 if (!base::DeleteFile(version_directory, true)) { |
| 206 PLOG(ERROR) << "Failed to delete version directory " |
| 207 << version_directory.value(); |
| 208 success = false; |
| 209 } |
| 210 |
| 211 return success; |
| 212 } |
| 213 |
| 214 // For each executable in |executables| that has a matching directory in |
| 215 // |directories|, tries to delete the executable and the matching directory. No |
| 216 // deletion occurs for a given version if a .exe or .dll file for that version |
| 217 // is in use. Returns false if any directory/executables pair could not be |
| 218 // deleted. |
| 219 bool DeleteMatchingExecutablesAndDirectories( |
| 220 const DirectorySet& directories, |
| 221 const ExecutableMap& executables, |
| 222 const base::FilePath& install_dir) { |
| 223 bool success = true; |
| 224 for (const auto& directory_name : directories) { |
| 225 // Don't delete the version unless the directory has at least one matching |
| 226 // executable. |
| 227 auto version_executables_it = executables.find(directory_name); |
| 228 if (version_executables_it == executables.end()) |
| 229 continue; |
| 230 |
| 231 // Try to delete all files for the version. |
| 232 success &= DeleteVersion(install_dir.Append(directory_name), |
| 233 version_executables_it->second); |
| 234 } |
| 235 return success; |
| 236 } |
| 237 |
| 238 bool DeleteOldVersions(const base::FilePath& install_dir) { |
| 239 const DirectorySet old_directories = GetOldVersionDirectories(install_dir); |
| 240 const ExecutableMap old_executables = GetOldExecutables(install_dir); |
| 241 |
| 242 bool success = true; |
| 243 success &= DeleteDirectoriesWithoutMatchingExecutable( |
| 244 old_directories, old_executables, install_dir); |
| 245 success &= DeleteExecutablesWithoutMatchingDirectory(old_directories, |
| 246 old_executables); |
| 247 success &= DeleteMatchingExecutablesAndDirectories( |
| 248 old_directories, old_executables, install_dir); |
| 249 |
| 250 return success; |
| 251 } |
| 252 |
| 253 bool DeleteOldVersionsCallback(const InstallerState& installer_state, |
| 254 const CallbackWorkItem& work_item) { |
| 255 // No rollback is possible for this action. |
| 256 if (work_item.IsRollback()) |
| 257 return true; |
| 258 |
| 259 installer_state.UpdateStage(REMOVING_OLD_VERSIONS); |
| 260 return DeleteOldVersions(installer_state.target_path()); |
| 261 } |
| 262 |
| 263 } // namespace |
| 264 |
| 265 void AddDeleteOldVersionsWorkItem(const InstallerState& installer_state, |
| 266 WorkItemList* work_item_list) { |
| 267 DCHECK(work_item_list); |
| 268 |
| 269 // TODO(fdoray): Launch a cleanup process when this fails during a regular |
| 270 // update or after a rename. crbug.com/451546 |
| 271 work_item_list |
| 272 ->AddCallbackWorkItem(base::Bind(&DeleteOldVersionsCallback, |
| 273 base::ConstRef(installer_state))) |
| 274 ->set_ignore_failure(true); |
| 275 } |
| 276 |
| 277 } // namespace installer |
OLD | NEW |