This source file includes following definitions.
- source_moved_to_backup_
- Do
- Rollback
#include "chrome/installer/util/move_tree_work_item.h"
#include <shlwapi.h>
#include "base/file_util.h"
#include "base/logging.h"
#include "chrome/installer/util/duplicate_tree_detector.h"
#include "chrome/installer/util/logging_installer.h"
MoveTreeWorkItem::~MoveTreeWorkItem() {
}
MoveTreeWorkItem::MoveTreeWorkItem(const base::FilePath& source_path,
const base::FilePath& dest_path,
const base::FilePath& temp_dir,
MoveTreeOption duplicate_option)
: source_path_(source_path),
dest_path_(dest_path),
temp_dir_(temp_dir),
duplicate_option_(duplicate_option),
moved_to_dest_path_(false),
moved_to_backup_(false),
source_moved_to_backup_(false) {
}
bool MoveTreeWorkItem::Do() {
if (!base::PathExists(source_path_)) {
LOG(ERROR) << source_path_.value() << " does not exist";
return false;
}
if (base::PathExists(dest_path_)) {
if (!backup_path_.CreateUniqueTempDirUnderPath(temp_dir_)) {
PLOG(ERROR) << "Failed to get backup path in folder "
<< temp_dir_.value();
return false;
}
base::FilePath backup = backup_path_.path().Append(dest_path_.BaseName());
if (duplicate_option_ == CHECK_DUPLICATES) {
if (installer::IsIdenticalFileHierarchy(source_path_, dest_path_)) {
if (base::Move(source_path_, backup)) {
source_moved_to_backup_ = true;
VLOG(1) << "Moved source " << source_path_.value()
<< " to backup path " << backup.value();
return true;
} else {
LOG(ERROR) << "Failed to backup source " << source_path_.value()
<< " to backup path " << backup.value()
<< " for duplicate trees. Trying regular Move instead.";
}
} else {
VLOG(1) << "Source path " << source_path_.value()
<< " differs from " << dest_path_.value()
<< ", updating now.";
}
}
if (base::Move(dest_path_, backup)) {
moved_to_backup_ = true;
VLOG(1) << "Moved destination " << dest_path_.value()
<< " to backup path " << backup.value();
} else {
PLOG(ERROR) << "failed moving " << dest_path_.value()
<< " to " << backup.value();
return false;
}
}
if (base::Move(source_path_, dest_path_)) {
moved_to_dest_path_ = true;
VLOG(1) << "Moved source " << source_path_.value()
<< " to destination " << dest_path_.value();
} else {
PLOG(ERROR) << "failed move " << source_path_.value()
<< " to " << dest_path_.value();
return false;
}
return true;
}
void MoveTreeWorkItem::Rollback() {
if (moved_to_dest_path_ && !base::Move(dest_path_, source_path_))
LOG(ERROR) << "Can not move " << dest_path_.value()
<< " to " << source_path_.value();
base::FilePath backup = backup_path_.path().Append(dest_path_.BaseName());
if (moved_to_backup_ && !base::Move(backup, dest_path_)) {
LOG(ERROR) << "failed move " << backup.value()
<< " to " << dest_path_.value();
}
if (source_moved_to_backup_ && !base::Move(backup, source_path_)) {
LOG(ERROR) << "Can not restore " << backup.value()
<< " to " << source_path_.value();
}
}