Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(272)

Side by Side Diff: chrome/installer/util/delete_old_versions.cc

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

Powered by Google App Engine
This is Rietveld 408576698