OLD | NEW |
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 | |
10 #include "base/files/file_util.h" | 7 #include "base/files/file_util.h" |
11 #include "base/logging.h" | 8 #include "base/logging.h" |
12 | 9 |
13 namespace { | 10 DeleteTreeWorkItem::DeleteTreeWorkItem(const base::FilePath& root_path, |
| 11 const base::FilePath& temp_path) |
| 12 : root_path_(root_path), temp_path_(temp_path) {} |
14 | 13 |
15 // Casts a value of an unsigned type to a signed type of the same size provided | 14 DeleteTreeWorkItem::~DeleteTreeWorkItem() = default; |
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 } | |
27 | 15 |
28 } // namespace | 16 bool DeleteTreeWorkItem::DoImpl() { |
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. | |
114 if (root_path_.empty() || !base::PathExists(root_path_)) | 17 if (root_path_.empty() || !base::PathExists(root_path_)) |
115 return true; | 18 return true; |
116 | 19 |
117 if (ignore_failure_) { | 20 // If rollback is not enabled, try to delete the root without making a backup. |
118 if (DeleteRoot()) | 21 // When that fails, fall back to moving the root to the temporary backup path. |
119 return true; | 22 // Consumers are responsible for making a best-effort attempt to remove the |
120 // The file cannot be removed, but perhaps it can be moved into | 23 // backup path. SelfCleaningTempDir is generally used for the backup path, so |
121 // the temporary backup path. Consumers are responsible for making | 24 // in the worst case the file(s) will be removed after the next reboot. |
122 // a best-effort attempt to remove the backup path. SelfCleaningTempDir | 25 if (!rollback_enabled() && DeleteRoot()) |
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(); | |
126 return true; | 26 return true; |
127 } | |
128 | 27 |
129 // Attempt to move the root to the backup. | 28 // Attempt to move the root to the backup. |
130 return MoveRootToBackup(); | 29 return MoveRootToBackup(); |
131 } | 30 } |
132 | 31 |
133 // If there are files in backup paths move them back. | 32 void DeleteTreeWorkItem::RollbackImpl() { |
134 void DeleteTreeWorkItem::Rollback() { | |
135 if (ignore_failure_) | |
136 return; | |
137 | |
138 if (moved_to_backup_) { | 33 if (moved_to_backup_) { |
139 base::FilePath backup = GetBackupPath(); | 34 base::FilePath backup = GetBackupPath(); |
140 DCHECK(!backup.empty()); | 35 DCHECK(!backup.empty()); |
141 if (base::PathExists(backup)) | 36 if (base::PathExists(backup)) |
142 base::Move(backup, root_path_); | 37 base::Move(backup, root_path_); |
143 } | 38 } |
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 } | |
159 } | 39 } |
160 | 40 |
161 base::FilePath DeleteTreeWorkItem::GetBackupPath() { | 41 base::FilePath DeleteTreeWorkItem::GetBackupPath() { |
162 if (backup_path_.path().empty() && | 42 if (backup_path_.path().empty() && |
163 !backup_path_.CreateUniqueTempDirUnderPath(temp_path_)) { | 43 !backup_path_.CreateUniqueTempDirUnderPath(temp_path_)) { |
164 PLOG(ERROR) << "Failed to get backup path in folder " << temp_path_.value(); | 44 PLOG(ERROR) << "Failed to get backup path in folder " << temp_path_.value(); |
165 return base::FilePath(); | 45 return base::FilePath(); |
166 } | 46 } |
167 | 47 |
168 DCHECK(!backup_path_.path().empty()); | 48 DCHECK(!backup_path_.path().empty()); |
(...skipping 12 matching lines...) Expand all Loading... |
181 if (backup.empty()) | 61 if (backup.empty()) |
182 return false; | 62 return false; |
183 if (base::Move(root_path_, backup)) { | 63 if (base::Move(root_path_, backup)) { |
184 moved_to_backup_ = true; | 64 moved_to_backup_ = true; |
185 return true; | 65 return true; |
186 } | 66 } |
187 PLOG(ERROR) << "Failed to move " << root_path_.value() | 67 PLOG(ERROR) << "Failed to move " << root_path_.value() |
188 << " to backup path " << backup.value(); | 68 << " to backup path " << backup.value(); |
189 return false; | 69 return false; |
190 } | 70 } |
OLD | NEW |