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> | 7 #include <algorithm> |
8 #include <limits> | 8 #include <limits> |
9 | 9 |
10 #include "base/files/file_util.h" | 10 #include "base/files/file_util.h" |
(...skipping 30 matching lines...) Expand all Loading... |
41 key_backup_paths_.reset(new base::ScopedTempDir[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]); | 42 std::copy(key_paths.begin(), key_paths.end(), &key_paths_[0]); |
43 } | 43 } |
44 } | 44 } |
45 | 45 |
46 DeleteTreeWorkItem::~DeleteTreeWorkItem() { | 46 DeleteTreeWorkItem::~DeleteTreeWorkItem() { |
47 } | 47 } |
48 | 48 |
49 // We first try to move key_path_ to backup_path. If it succeeds, we go ahead | 49 // We first try to move key_path_ to backup_path. If it succeeds, we go ahead |
50 // and move the rest. | 50 // and move the rest. |
51 bool DeleteTreeWorkItem::Do() { | 51 bool DeleteTreeWorkItem::DoImpl() { |
52 // Go through all the key files and see if we can open them exclusively | 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, | 53 // with only the FILE_SHARE_DELETE flag. Once we know we have all of them, |
54 // we can delete them. | 54 // we can delete them. |
55 std::vector<HANDLE> opened_key_files; | 55 std::vector<HANDLE> opened_key_files; |
56 opened_key_files.reserve(num_key_files_); | 56 opened_key_files.reserve(num_key_files_); |
57 bool abort = false; | 57 bool abort = false; |
58 for (ptrdiff_t i = 0; !abort && i != num_key_files_; ++i) { | 58 for (ptrdiff_t i = 0; !abort && i != num_key_files_; ++i) { |
59 base::FilePath& key_file = key_paths_[i]; | 59 base::FilePath& key_file = key_paths_[i]; |
60 base::ScopedTempDir& backup = key_backup_paths_[i]; | 60 base::ScopedTempDir& backup = key_backup_paths_[i]; |
61 if (!ignore_failure_) { | 61 if (rollback_enabled()) { |
62 if (!backup.CreateUniqueTempDirUnderPath(temp_path_)) { | 62 if (!backup.CreateUniqueTempDirUnderPath(temp_path_)) { |
63 PLOG(ERROR) << "Could not create temp dir in " << temp_path_.value(); | 63 PLOG(ERROR) << "Could not create temp dir in " << temp_path_.value(); |
64 abort = true; | 64 abort = true; |
65 } else if (!base::CopyFile(key_file, | 65 } else if (!base::CopyFile(key_file, |
66 backup.path().Append(key_file.BaseName()))) { | 66 backup.path().Append(key_file.BaseName()))) { |
67 PLOG(ERROR) << "Could not back up " << key_file.value() | 67 PLOG(ERROR) << "Could not back up " << key_file.value() |
68 << " to directory " << backup.path().value(); | 68 << " to directory " << backup.path().value(); |
69 abort = true; | 69 abort = true; |
70 if (!backup.Delete()) { | 70 if (!backup.Delete()) { |
71 PLOG(ERROR) << "Could not clean up temp dir in " | 71 PLOG(ERROR) << "Could not clean up temp dir in " |
(...skipping 28 matching lines...) Expand all Loading... |
100 abort = true; | 100 abort = true; |
101 } | 101 } |
102 } | 102 } |
103 } | 103 } |
104 | 104 |
105 std::for_each(opened_key_files.begin(), opened_key_files.end(), CloseHandle); | 105 std::for_each(opened_key_files.begin(), opened_key_files.end(), CloseHandle); |
106 opened_key_files.clear(); | 106 opened_key_files.clear(); |
107 | 107 |
108 if (abort) { | 108 if (abort) { |
109 LOG(ERROR) << "Could not exclusively hold all key files."; | 109 LOG(ERROR) << "Could not exclusively hold all key files."; |
110 return ignore_failure_; | 110 return false; |
111 } | 111 } |
112 | 112 |
113 // Now that we've taken care of the key files, take care of the rest. | 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_)) | 114 if (root_path_.empty() || !base::PathExists(root_path_)) |
115 return true; | 115 return true; |
116 | 116 |
117 if (ignore_failure_) { | 117 // If rollback is not enabled, try to delete the root without making a backup. |
118 if (DeleteRoot()) | 118 // When that fails, fall back to moving the root to the temporary backup path. |
119 return true; | 119 // 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 | 120 // backup path. SelfCleaningTempDir is generally used for the backup path, so |
121 // the temporary backup path. Consumers are responsible for making | 121 // 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 | 122 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; | 123 return true; |
127 } | |
128 | 124 |
129 // Attempt to move the root to the backup. | 125 // Attempt to move the root to the backup. |
130 return MoveRootToBackup(); | 126 return MoveRootToBackup(); |
131 } | 127 } |
132 | 128 |
133 // If there are files in backup paths move them back. | 129 // If there are files in backup paths move them back. |
134 void DeleteTreeWorkItem::Rollback() { | 130 void DeleteTreeWorkItem::RollbackImpl() { |
135 if (ignore_failure_) | |
136 return; | |
137 | |
138 if (moved_to_backup_) { | 131 if (moved_to_backup_) { |
139 base::FilePath backup = GetBackupPath(); | 132 base::FilePath backup = GetBackupPath(); |
140 DCHECK(!backup.empty()); | 133 DCHECK(!backup.empty()); |
141 if (base::PathExists(backup)) | 134 if (base::PathExists(backup)) |
142 base::Move(backup, root_path_); | 135 base::Move(backup, root_path_); |
143 } | 136 } |
144 | 137 |
145 for (ptrdiff_t i = 0; i != num_key_files_; ++i) { | 138 for (ptrdiff_t i = 0; i != num_key_files_; ++i) { |
146 base::ScopedTempDir& backup_dir = key_backup_paths_[i]; | 139 base::ScopedTempDir& backup_dir = key_backup_paths_[i]; |
147 if (!backup_dir.path().empty()) { | 140 if (!backup_dir.path().empty()) { |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
181 if (backup.empty()) | 174 if (backup.empty()) |
182 return false; | 175 return false; |
183 if (base::Move(root_path_, backup)) { | 176 if (base::Move(root_path_, backup)) { |
184 moved_to_backup_ = true; | 177 moved_to_backup_ = true; |
185 return true; | 178 return true; |
186 } | 179 } |
187 PLOG(ERROR) << "Failed to move " << root_path_.value() | 180 PLOG(ERROR) << "Failed to move " << root_path_.value() |
188 << " to backup path " << backup.value(); | 181 << " to backup path " << backup.value(); |
189 return false; | 182 return false; |
190 } | 183 } |
OLD | NEW |