| OLD | NEW |
| (Empty) | |
| 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "chrome/installer/util/move_tree_work_item.h" |
| 6 |
| 7 #include <shlwapi.h> |
| 8 #include "base/file_util.h" |
| 9 #include "chrome/installer/util/logging_installer.h" |
| 10 |
| 11 MoveTreeWorkItem::~MoveTreeWorkItem() { |
| 12 if (file_util::PathExists(backup_path_)) { |
| 13 file_util::Delete(backup_path_, true); |
| 14 } |
| 15 } |
| 16 |
| 17 MoveTreeWorkItem::MoveTreeWorkItem(const std::wstring& source_path, |
| 18 const std::wstring& dest_path, |
| 19 const std::wstring& temp_dir) |
| 20 : source_path_(source_path), |
| 21 dest_path_(dest_path), |
| 22 temp_dir_(temp_dir), |
| 23 moved_to_dest_path_(false), |
| 24 moved_to_backup_(false) { |
| 25 } |
| 26 |
| 27 bool MoveTreeWorkItem::Do() { |
| 28 if (!file_util::PathExists(source_path_)) { |
| 29 LOG(ERROR) << source_path_ << " does not exist"; |
| 30 return false; |
| 31 } |
| 32 |
| 33 // If dest_path_ exists, move destination to a backup path. |
| 34 if (file_util::PathExists(dest_path_)) { |
| 35 // Generate a backup path that can keep the original files under dest_path_. |
| 36 if (!file_util::CreateTemporaryFileNameInDir(temp_dir_, &backup_path_)) { |
| 37 LOG(ERROR) << "Failed to get backup path in folder " << temp_dir_; |
| 38 return false; |
| 39 } |
| 40 |
| 41 if (file_util::Move(dest_path_, backup_path_)) { |
| 42 moved_to_backup_ = true; |
| 43 LOG(INFO) << "Moved destination " << dest_path_ |
| 44 << " to backup path " << backup_path_; |
| 45 } else { |
| 46 LOG(ERROR) << "failed moving " << dest_path_ << " to " << backup_path_; |
| 47 return false; |
| 48 } |
| 49 } |
| 50 |
| 51 // Now move source to destination. |
| 52 if (file_util::Move(source_path_, dest_path_)) { |
| 53 moved_to_dest_path_ = true; |
| 54 LOG(INFO) << "Moved source " << source_path_ |
| 55 << " to destination " << dest_path_; |
| 56 } else { |
| 57 LOG(ERROR) << "failed move " << source_path_ << " to " << dest_path_; |
| 58 return false; |
| 59 } |
| 60 |
| 61 return true; |
| 62 } |
| 63 |
| 64 void MoveTreeWorkItem::Rollback() { |
| 65 if (moved_to_dest_path_ && !file_util::Move(dest_path_, source_path_)) |
| 66 LOG(ERROR) << "Can not move " << dest_path_ << " to " << source_path_; |
| 67 |
| 68 if (moved_to_backup_ && !file_util::Move(backup_path_, dest_path_)) |
| 69 LOG(ERROR) << "failed move " << backup_path_ << " to " << dest_path_; |
| 70 } |
| OLD | NEW |