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

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

Issue 1976443005: Revert of Add best-effort/allow rollback flags on WorkItem. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@simple_list_tests
Patch Set: Created 4 years, 7 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
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "chrome/installer/util/delete_tree_work_item.h" 5 #include "chrome/installer/util/delete_tree_work_item.h"
6 6
7 #include <algorithm>
8 #include <limits>
9
7 #include "base/files/file_util.h" 10 #include "base/files/file_util.h"
8 #include "base/logging.h" 11 #include "base/logging.h"
9 12
10 DeleteTreeWorkItem::DeleteTreeWorkItem(const base::FilePath& root_path, 13 namespace {
11 const base::FilePath& temp_path)
12 : root_path_(root_path), temp_path_(temp_path) {}
13 14
14 DeleteTreeWorkItem::~DeleteTreeWorkItem() = default; 15 // Casts a value of an unsigned type to a signed type of the same size provided
16 // that there is no overflow.
17 template<typename L, typename R>
18 bool SafeCast(L left, R* right) {
19 DCHECK(right);
20 static_assert(sizeof(left) == sizeof(right),
21 "Items SafeCast is used with must be of the same size");
22 if (left > static_cast<L>(std::numeric_limits<R>::max()))
23 return false;
24 *right = static_cast<L>(left);
25 return true;
26 }
15 27
16 bool DeleteTreeWorkItem::DoImpl() { 28 } // namespace
29
30 DeleteTreeWorkItem::DeleteTreeWorkItem(
31 const base::FilePath& root_path,
32 const base::FilePath& temp_path,
33 const std::vector<base::FilePath>& key_paths)
34 : root_path_(root_path),
35 temp_path_(temp_path),
36 moved_to_backup_(false) {
37 if (!SafeCast(key_paths.size(), &num_key_files_)) {
38 NOTREACHED() << "Impossibly large key_paths collection";
39 } else if (num_key_files_ != 0) {
40 key_paths_.reset(new base::FilePath[num_key_files_]);
41 key_backup_paths_.reset(new base::ScopedTempDir[num_key_files_]);
42 std::copy(key_paths.begin(), key_paths.end(), &key_paths_[0]);
43 }
44 }
45
46 DeleteTreeWorkItem::~DeleteTreeWorkItem() {
47 }
48
49 // We first try to move key_path_ to backup_path. If it succeeds, we go ahead
50 // and move the rest.
51 bool DeleteTreeWorkItem::Do() {
52 // Go through all the key files and see if we can open them exclusively
53 // with only the FILE_SHARE_DELETE flag. Once we know we have all of them,
54 // we can delete them.
55 std::vector<HANDLE> opened_key_files;
56 opened_key_files.reserve(num_key_files_);
57 bool abort = false;
58 for (ptrdiff_t i = 0; !abort && i != num_key_files_; ++i) {
59 base::FilePath& key_file = key_paths_[i];
60 base::ScopedTempDir& backup = key_backup_paths_[i];
61 if (!ignore_failure_) {
62 if (!backup.CreateUniqueTempDirUnderPath(temp_path_)) {
63 PLOG(ERROR) << "Could not create temp dir in " << temp_path_.value();
64 abort = true;
65 } else if (!base::CopyFile(key_file,
66 backup.path().Append(key_file.BaseName()))) {
67 PLOG(ERROR) << "Could not back up " << key_file.value()
68 << " to directory " << backup.path().value();
69 abort = true;
70 if (!backup.Delete()) {
71 PLOG(ERROR) << "Could not clean up temp dir in "
72 << temp_path_.value();
73 }
74 }
75 }
76 if (!abort) {
77 HANDLE file = ::CreateFile(key_file.value().c_str(), FILE_ALL_ACCESS,
78 FILE_SHARE_DELETE, NULL, OPEN_EXISTING, 0,
79 NULL);
80 if (file != INVALID_HANDLE_VALUE) {
81 VLOG(1) << "Acquired exclusive lock for key file: " << key_file.value();
82 opened_key_files.push_back(file);
83 } else {
84 if (::GetLastError() != ERROR_FILE_NOT_FOUND)
85 abort = true;
86 PLOG(INFO) << "Failed to open " << key_file.value();
87 }
88 }
89 }
90
91 if (!abort) {
92 // We now hold exclusive locks with "share delete" permissions for each
93 // of the key files and also have created backups of those files.
94 // We can safely delete the key files now.
95 for (ptrdiff_t i = 0; !abort && i != num_key_files_; ++i) {
96 base::FilePath& key_file = key_paths_[i];
97 if (!base::DeleteFile(key_file, true)) {
98 // This should not really be possible because of the above.
99 PLOG(DFATAL) << "Unexpectedly could not delete " << key_file.value();
100 abort = true;
101 }
102 }
103 }
104
105 std::for_each(opened_key_files.begin(), opened_key_files.end(), CloseHandle);
106 opened_key_files.clear();
107
108 if (abort) {
109 LOG(ERROR) << "Could not exclusively hold all key files.";
110 return ignore_failure_;
111 }
112
113 // Now that we've taken care of the key files, take care of the rest.
17 if (root_path_.empty() || !base::PathExists(root_path_)) 114 if (root_path_.empty() || !base::PathExists(root_path_))
18 return true; 115 return true;
19 116
20 // If rollback is not enabled, try to delete the root without making a backup. 117 if (ignore_failure_) {
21 // When that fails, fall back to moving the root to the temporary backup path. 118 if (DeleteRoot())
22 // Consumers are responsible for making a best-effort attempt to remove the 119 return true;
23 // backup path. SelfCleaningTempDir is generally used for the backup path, so 120 // The file cannot be removed, but perhaps it can be moved into
24 // in the worst case the file(s) will be removed after the next reboot. 121 // the temporary backup path. Consumers are responsible for making
25 if (!rollback_enabled() && DeleteRoot()) 122 // a best-effort attempt to remove the backup path. SelfCleaningTempDir
123 // is generally used for the backup path, so in the
124 // worst case the file(s) will be removed after the next reboot.
125 MoveRootToBackup();
26 return true; 126 return true;
127 }
27 128
28 // Attempt to move the root to the backup. 129 // Attempt to move the root to the backup.
29 return MoveRootToBackup(); 130 return MoveRootToBackup();
30 } 131 }
31 132
32 void DeleteTreeWorkItem::RollbackImpl() { 133 // If there are files in backup paths move them back.
134 void DeleteTreeWorkItem::Rollback() {
135 if (ignore_failure_)
136 return;
137
33 if (moved_to_backup_) { 138 if (moved_to_backup_) {
34 base::FilePath backup = GetBackupPath(); 139 base::FilePath backup = GetBackupPath();
35 DCHECK(!backup.empty()); 140 DCHECK(!backup.empty());
36 if (base::PathExists(backup)) 141 if (base::PathExists(backup))
37 base::Move(backup, root_path_); 142 base::Move(backup, root_path_);
38 } 143 }
144
145 for (ptrdiff_t i = 0; i != num_key_files_; ++i) {
146 base::ScopedTempDir& backup_dir = key_backup_paths_[i];
147 if (!backup_dir.path().empty()) {
148 base::FilePath& key_file = key_paths_[i];
149 base::FilePath backup_file =
150 backup_dir.path().Append(key_file.BaseName());
151 if (base::PathExists(backup_file) &&
152 !base::Move(backup_file, key_file)) {
153 // This could happen if we could not delete the key file to begin with.
154 PLOG(WARNING) << "Rollback: Failed to move backup file back in place: "
155 << backup_file.value() << " to " << key_file.value();
156 }
157 }
158 }
39 } 159 }
40 160
41 base::FilePath DeleteTreeWorkItem::GetBackupPath() { 161 base::FilePath DeleteTreeWorkItem::GetBackupPath() {
42 if (backup_path_.path().empty() && 162 if (backup_path_.path().empty() &&
43 !backup_path_.CreateUniqueTempDirUnderPath(temp_path_)) { 163 !backup_path_.CreateUniqueTempDirUnderPath(temp_path_)) {
44 PLOG(ERROR) << "Failed to get backup path in folder " << temp_path_.value(); 164 PLOG(ERROR) << "Failed to get backup path in folder " << temp_path_.value();
45 return base::FilePath(); 165 return base::FilePath();
46 } 166 }
47 167
48 DCHECK(!backup_path_.path().empty()); 168 DCHECK(!backup_path_.path().empty());
(...skipping 12 matching lines...) Expand all
61 if (backup.empty()) 181 if (backup.empty())
62 return false; 182 return false;
63 if (base::Move(root_path_, backup)) { 183 if (base::Move(root_path_, backup)) {
64 moved_to_backup_ = true; 184 moved_to_backup_ = true;
65 return true; 185 return true;
66 } 186 }
67 PLOG(ERROR) << "Failed to move " << root_path_.value() 187 PLOG(ERROR) << "Failed to move " << root_path_.value()
68 << " to backup path " << backup.value(); 188 << " to backup path " << backup.value();
69 return false; 189 return false;
70 } 190 }
OLDNEW
« no previous file with comments | « chrome/installer/util/delete_tree_work_item.h ('k') | chrome/installer/util/delete_tree_work_item_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698