This source file includes following definitions.
- SafeCast
- copied_to_backup_
- Do
- Rollback
#include "chrome/installer/util/delete_tree_work_item.h"
#include <algorithm>
#include <limits>
#include "base/file_util.h"
#include "base/logging.h"
namespace {
template<typename L, typename R>
bool SafeCast(L left, R* right) {
DCHECK(right);
COMPILE_ASSERT(sizeof(left) == sizeof(right),
must_add_support_for_crazy_data_types);
if (left > static_cast<L>(std::numeric_limits<R>::max()))
return false;
*right = static_cast<L>(left);
return true;
}
}
DeleteTreeWorkItem::DeleteTreeWorkItem(
const base::FilePath& root_path,
const base::FilePath& temp_path,
const std::vector<base::FilePath>& key_paths)
: root_path_(root_path),
temp_path_(temp_path),
copied_to_backup_(false) {
if (!SafeCast(key_paths.size(), &num_key_files_)) {
NOTREACHED() << "Impossibly large key_paths collection";
} else if (num_key_files_ != 0) {
key_paths_.reset(new base::FilePath[num_key_files_]);
key_backup_paths_.reset(new base::ScopedTempDir[num_key_files_]);
std::copy(key_paths.begin(), key_paths.end(), &key_paths_[0]);
}
}
DeleteTreeWorkItem::~DeleteTreeWorkItem() {
}
bool DeleteTreeWorkItem::Do() {
std::vector<HANDLE> opened_key_files;
opened_key_files.reserve(num_key_files_);
bool abort = false;
for (ptrdiff_t i = 0; !abort && i != num_key_files_; ++i) {
base::FilePath& key_file = key_paths_[i];
base::ScopedTempDir& backup = key_backup_paths_[i];
if (!ignore_failure_) {
if (!backup.CreateUniqueTempDirUnderPath(temp_path_)) {
PLOG(ERROR) << "Could not create temp dir in " << temp_path_.value();
abort = true;
} else if (!base::CopyFile(key_file,
backup.path().Append(key_file.BaseName()))) {
PLOG(ERROR) << "Could not back up " << key_file.value()
<< " to directory " << backup.path().value();
abort = true;
backup.Delete();
}
}
if (!abort) {
HANDLE file = ::CreateFile(key_file.value().c_str(), FILE_ALL_ACCESS,
FILE_SHARE_DELETE, NULL, OPEN_EXISTING, 0,
NULL);
if (file != INVALID_HANDLE_VALUE) {
VLOG(1) << "Acquired exclusive lock for key file: " << key_file.value();
opened_key_files.push_back(file);
} else {
if (::GetLastError() != ERROR_FILE_NOT_FOUND)
abort = true;
PLOG(INFO) << "Failed to open " << key_file.value();
}
}
}
if (!abort) {
for (ptrdiff_t i = 0; !abort && i != num_key_files_; ++i) {
base::FilePath& key_file = key_paths_[i];
if (!base::DeleteFile(key_file, true)) {
PLOG(DFATAL) << "Unexpectedly could not delete " << key_file.value();
abort = true;
}
}
}
std::for_each(opened_key_files.begin(), opened_key_files.end(), CloseHandle);
opened_key_files.clear();
if (abort) {
LOG(ERROR) << "Could not exclusively hold all key files.";
return ignore_failure_;
}
if (!root_path_.empty() && base::PathExists(root_path_)) {
if (!ignore_failure_) {
if (!backup_path_.CreateUniqueTempDirUnderPath(temp_path_)) {
PLOG(ERROR) << "Failed to get backup path in folder "
<< temp_path_.value();
return false;
} else {
base::FilePath backup =
backup_path_.path().Append(root_path_.BaseName());
if (!base::CopyDirectory(root_path_, backup, true)) {
LOG(ERROR) << "can not copy " << root_path_.value()
<< " to backup path " << backup.value();
return false;
} else {
copied_to_backup_ = true;
}
}
}
if (!base::DeleteFile(root_path_, true)) {
LOG(ERROR) << "can not delete " << root_path_.value();
return ignore_failure_;
}
}
return true;
}
void DeleteTreeWorkItem::Rollback() {
if (ignore_failure_)
return;
if (copied_to_backup_) {
DCHECK(!backup_path_.path().empty());
base::FilePath backup = backup_path_.path().Append(root_path_.BaseName());
if (base::PathExists(backup))
base::Move(backup, root_path_);
}
for (ptrdiff_t i = 0; i != num_key_files_; ++i) {
base::ScopedTempDir& backup_dir = key_backup_paths_[i];
if (!backup_dir.path().empty()) {
base::FilePath& key_file = key_paths_[i];
base::FilePath backup_file =
backup_dir.path().Append(key_file.BaseName());
if (base::PathExists(backup_file) &&
!base::Move(backup_file, key_file)) {
PLOG(WARNING) << "Rollback: Failed to move backup file back in place: "
<< backup_file.value() << " to " << key_file.value();
}
}
}
}