Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. |
|
robertshield
2011/10/14 21:28:35
BWAHAHA!
grt (UTC plus 2)
2011/10/14 22:55:25
My head is hung in shame.
| |
| 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/file_util.h" | 10 #include "base/file_util.h" |
| 11 #include "base/logging.h" | 11 #include "base/logging.h" |
| (...skipping 13 matching lines...) Expand all Loading... | |
| 25 return true; | 25 return true; |
| 26 } | 26 } |
| 27 | 27 |
| 28 } // namespace | 28 } // namespace |
| 29 | 29 |
| 30 DeleteTreeWorkItem::DeleteTreeWorkItem( | 30 DeleteTreeWorkItem::DeleteTreeWorkItem( |
| 31 const FilePath& root_path, | 31 const FilePath& root_path, |
| 32 const FilePath& temp_path, | 32 const FilePath& temp_path, |
| 33 const std::vector<FilePath>& key_paths) | 33 const std::vector<FilePath>& key_paths) |
| 34 : root_path_(root_path), | 34 : root_path_(root_path), |
| 35 temp_path_(temp_path) { | 35 temp_path_(temp_path), |
| 36 copied_to_backup_(false) { | |
| 36 if (!SafeCast(key_paths.size(), &num_key_files_)) { | 37 if (!SafeCast(key_paths.size(), &num_key_files_)) { |
| 37 NOTREACHED() << "Impossibly large key_paths collection"; | 38 NOTREACHED() << "Impossibly large key_paths collection"; |
| 38 } else if (num_key_files_ != 0) { | 39 } else if (num_key_files_ != 0) { |
| 39 key_paths_.reset(new FilePath[num_key_files_]); | 40 key_paths_.reset(new FilePath[num_key_files_]); |
| 40 key_backup_paths_.reset(new ScopedTempDir[num_key_files_]); | 41 key_backup_paths_.reset(new ScopedTempDir[num_key_files_]); |
| 41 std::copy(key_paths.begin(), key_paths.end(), &key_paths_[0]); | 42 std::copy(key_paths.begin(), key_paths.end(), &key_paths_[0]); |
| 42 } | 43 } |
| 43 } | 44 } |
| 44 | 45 |
| 45 DeleteTreeWorkItem::~DeleteTreeWorkItem() { | 46 DeleteTreeWorkItem::~DeleteTreeWorkItem() { |
| 46 } | 47 } |
| 47 | 48 |
| 48 // 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 |
| 49 // and move the rest. | 50 // and move the rest. |
| 50 bool DeleteTreeWorkItem::Do() { | 51 bool DeleteTreeWorkItem::Do() { |
| 51 // 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 |
| 52 // 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, |
| 53 // we can delete them. | 54 // we can delete them. |
| 54 std::vector<HANDLE> opened_key_files; | 55 std::vector<HANDLE> opened_key_files; |
| 55 opened_key_files.reserve(num_key_files_); | 56 opened_key_files.reserve(num_key_files_); |
| 56 bool abort = false; | 57 bool abort = false; |
| 57 for (ptrdiff_t i = 0; !abort && i != num_key_files_; ++i) { | 58 for (ptrdiff_t i = 0; !abort && i != num_key_files_; ++i) { |
| 58 FilePath& key_file = key_paths_[i]; | 59 FilePath& key_file = key_paths_[i]; |
| 59 ScopedTempDir& backup = key_backup_paths_[i]; | 60 ScopedTempDir& backup = key_backup_paths_[i]; |
| 60 if (!backup.CreateUniqueTempDirUnderPath(temp_path_)) { | 61 if (!ignore_failure_) { |
| 61 PLOG(ERROR) << "Could not create temp dir in " << temp_path_.value(); | 62 if (!backup.CreateUniqueTempDirUnderPath(temp_path_)) { |
| 62 abort = true; | 63 PLOG(ERROR) << "Could not create temp dir in " << temp_path_.value(); |
| 63 } else if (!file_util::CopyFile(key_file, | 64 abort = true; |
| 64 backup.path().Append(key_file.BaseName()))) { | 65 } else if (!file_util::CopyFile(key_file, |
| 65 PLOG(ERROR) << "Could not back up " << key_file.value() | 66 backup.path().Append(key_file.BaseName()))) { |
| 66 << " to directory " << backup.path().value(); | 67 PLOG(ERROR) << "Could not back up " << key_file.value() |
| 67 abort = true; | 68 << " to directory " << backup.path().value(); |
| 68 } else { | 69 abort = true; |
| 70 backup.Delete(); | |
| 71 } | |
| 72 } | |
| 73 if (!abort) { | |
| 69 HANDLE file = ::CreateFile(key_file.value().c_str(), FILE_ALL_ACCESS, | 74 HANDLE file = ::CreateFile(key_file.value().c_str(), FILE_ALL_ACCESS, |
| 70 FILE_SHARE_DELETE, NULL, OPEN_EXISTING, 0, | 75 FILE_SHARE_DELETE, NULL, OPEN_EXISTING, 0, |
| 71 NULL); | 76 NULL); |
| 72 if (file != INVALID_HANDLE_VALUE) { | 77 if (file != INVALID_HANDLE_VALUE) { |
| 73 VLOG(1) << "Acquired exclusive lock for key file: " << key_file.value(); | 78 VLOG(1) << "Acquired exclusive lock for key file: " << key_file.value(); |
| 74 opened_key_files.push_back(file); | 79 opened_key_files.push_back(file); |
| 75 } else { | 80 } else { |
| 76 if (::GetLastError() != ERROR_FILE_NOT_FOUND) | 81 if (::GetLastError() != ERROR_FILE_NOT_FOUND) |
| 77 abort = true; | 82 abort = true; |
| 78 PLOG(INFO) << "Failed to open " << key_file.value(); | 83 PLOG(INFO) << "Failed to open " << key_file.value(); |
| (...skipping 29 matching lines...) Expand all Loading... | |
| 108 if (!backup_path_.CreateUniqueTempDirUnderPath(temp_path_)) { | 113 if (!backup_path_.CreateUniqueTempDirUnderPath(temp_path_)) { |
| 109 PLOG(ERROR) << "Failed to get backup path in folder " | 114 PLOG(ERROR) << "Failed to get backup path in folder " |
| 110 << temp_path_.value(); | 115 << temp_path_.value(); |
| 111 return false; | 116 return false; |
| 112 } else { | 117 } else { |
| 113 FilePath backup = backup_path_.path().Append(root_path_.BaseName()); | 118 FilePath backup = backup_path_.path().Append(root_path_.BaseName()); |
| 114 if (!file_util::CopyDirectory(root_path_, backup, true)) { | 119 if (!file_util::CopyDirectory(root_path_, backup, true)) { |
| 115 LOG(ERROR) << "can not copy " << root_path_.value() | 120 LOG(ERROR) << "can not copy " << root_path_.value() |
| 116 << " to backup path " << backup.value(); | 121 << " to backup path " << backup.value(); |
| 117 return false; | 122 return false; |
| 123 } else { | |
| 124 copied_to_backup_ = true; | |
| 118 } | 125 } |
| 119 } | 126 } |
| 120 } | 127 } |
| 121 if (!file_util::Delete(root_path_, true)) { | 128 if (!file_util::Delete(root_path_, true)) { |
| 122 LOG(ERROR) << "can not delete " << root_path_.value(); | 129 LOG(ERROR) << "can not delete " << root_path_.value(); |
| 123 return ignore_failure_; | 130 return ignore_failure_; |
| 124 } | 131 } |
| 125 } | 132 } |
| 126 | 133 |
| 127 return true; | 134 return true; |
| 128 } | 135 } |
| 129 | 136 |
| 130 // If there are files in backup paths move them back. | 137 // If there are files in backup paths move them back. |
| 131 void DeleteTreeWorkItem::Rollback() { | 138 void DeleteTreeWorkItem::Rollback() { |
| 132 if (ignore_failure_) | 139 if (ignore_failure_) |
| 133 return; | 140 return; |
| 134 | 141 |
| 135 if (!backup_path_.path().empty()) { | 142 if (copied_to_backup_) { |
| 143 DCHECK(!backup_path_.path().empty()); | |
| 136 FilePath backup = backup_path_.path().Append(root_path_.BaseName()); | 144 FilePath backup = backup_path_.path().Append(root_path_.BaseName()); |
| 137 if (file_util::PathExists(backup)) | 145 if (file_util::PathExists(backup)) |
| 138 file_util::Move(backup, root_path_); | 146 file_util::Move(backup, root_path_); |
| 139 } | 147 } |
| 140 | 148 |
| 141 for (ptrdiff_t i = 0; i != num_key_files_; ++i) { | 149 for (ptrdiff_t i = 0; i != num_key_files_; ++i) { |
| 142 ScopedTempDir& backup_dir = key_backup_paths_[i]; | 150 ScopedTempDir& backup_dir = key_backup_paths_[i]; |
| 143 if (!backup_dir.path().empty()) { | 151 if (!backup_dir.path().empty()) { |
| 144 FilePath& key_file = key_paths_[i]; | 152 FilePath& key_file = key_paths_[i]; |
| 145 FilePath backup_file = backup_dir.path().Append(key_file.BaseName()); | 153 FilePath backup_file = backup_dir.path().Append(key_file.BaseName()); |
| 146 if (file_util::PathExists(backup_file) && | 154 if (file_util::PathExists(backup_file) && |
| 147 !file_util::Move(backup_file, key_file)) { | 155 !file_util::Move(backup_file, key_file)) { |
| 148 // This could happen if we could not delete the key file to begin with. | 156 // This could happen if we could not delete the key file to begin with. |
| 149 PLOG(WARNING) << "Rollback: Failed to move backup file back in place: " | 157 PLOG(WARNING) << "Rollback: Failed to move backup file back in place: " |
| 150 << backup_file.value() << " to " << key_file.value(); | 158 << backup_file.value() << " to " << key_file.value(); |
| 151 } | 159 } |
| 152 } | 160 } |
| 153 } | 161 } |
| 154 } | 162 } |
| OLD | NEW |