| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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/move_tree_work_item.h" | 5 #include "chrome/installer/util/move_tree_work_item.h" |
| 6 | 6 |
| 7 #include <shlwapi.h> | 7 #include <shlwapi.h> |
| 8 | 8 |
| 9 #include "base/file_util.h" | 9 #include "base/file_util.h" |
| 10 #include "base/logging.h" | 10 #include "base/logging.h" |
| 11 #include "chrome/installer/util/logging_installer.h" | 11 #include "chrome/installer/util/logging_installer.h" |
| 12 | 12 |
| 13 MoveTreeWorkItem::~MoveTreeWorkItem() { | 13 MoveTreeWorkItem::~MoveTreeWorkItem() { |
| 14 if (file_util::PathExists(backup_path_)) { | |
| 15 file_util::Delete(backup_path_, true); | |
| 16 } | |
| 17 } | 14 } |
| 18 | 15 |
| 19 MoveTreeWorkItem::MoveTreeWorkItem(const FilePath& source_path, | 16 MoveTreeWorkItem::MoveTreeWorkItem(const FilePath& source_path, |
| 20 const FilePath& dest_path, | 17 const FilePath& dest_path, |
| 21 const FilePath& temp_dir) | 18 const FilePath& temp_dir) |
| 22 : source_path_(source_path), | 19 : source_path_(source_path), |
| 23 dest_path_(dest_path), | 20 dest_path_(dest_path), |
| 24 temp_dir_(temp_dir), | 21 temp_dir_(temp_dir), |
| 25 moved_to_dest_path_(false), | 22 moved_to_dest_path_(false), |
| 26 moved_to_backup_(false) { | 23 moved_to_backup_(false) { |
| 27 } | 24 } |
| 28 | 25 |
| 29 bool MoveTreeWorkItem::Do() { | 26 bool MoveTreeWorkItem::Do() { |
| 30 if (!file_util::PathExists(source_path_)) { | 27 if (!file_util::PathExists(source_path_)) { |
| 31 LOG(ERROR) << source_path_.value() << " does not exist"; | 28 LOG(ERROR) << source_path_.value() << " does not exist"; |
| 32 return false; | 29 return false; |
| 33 } | 30 } |
| 34 | 31 |
| 35 // If dest_path_ exists, move destination to a backup path. | 32 // If dest_path_ exists, move destination to a backup path. |
| 36 if (file_util::PathExists(dest_path_)) { | 33 if (file_util::PathExists(dest_path_)) { |
| 37 // Generate a backup path that can keep the original files under dest_path_. | 34 // Generate a backup path that can keep the original files under dest_path_. |
| 38 if (!file_util::CreateTemporaryFileInDir(FilePath(temp_dir_), | 35 if (!backup_path_.CreateUniqueTempDirUnderPath(temp_dir_)) { |
| 39 &backup_path_)) { | 36 PLOG(ERROR) << "Failed to get backup path in folder " |
| 40 LOG(ERROR) << "Failed to get backup path in folder " << temp_dir_.value(); | 37 << temp_dir_.value(); |
| 41 return false; | 38 return false; |
| 42 } | 39 } |
| 43 | 40 |
| 44 if (file_util::Move(dest_path_, backup_path_)) { | 41 FilePath backup = backup_path_.path().Append(dest_path_.BaseName()); |
| 42 |
| 43 if (file_util::Move(dest_path_, backup)) { |
| 45 moved_to_backup_ = true; | 44 moved_to_backup_ = true; |
| 46 VLOG(1) << "Moved destination " << dest_path_.value() | 45 VLOG(1) << "Moved destination " << dest_path_.value() |
| 47 << " to backup path " << backup_path_.value(); | 46 << " to backup path " << backup.value(); |
| 48 } else { | 47 } else { |
| 49 LOG(ERROR) << "failed moving " << dest_path_.value() | 48 LOG(ERROR) << "failed moving " << dest_path_.value() |
| 50 << " to " << backup_path_.value(); | 49 << " to " << backup.value(); |
| 51 return false; | 50 return false; |
| 52 } | 51 } |
| 53 } | 52 } |
| 54 | 53 |
| 55 // Now move source to destination. | 54 // Now move source to destination. |
| 56 if (file_util::Move(source_path_, dest_path_)) { | 55 if (file_util::Move(source_path_, dest_path_)) { |
| 57 moved_to_dest_path_ = true; | 56 moved_to_dest_path_ = true; |
| 58 VLOG(1) << "Moved source " << source_path_.value() | 57 VLOG(1) << "Moved source " << source_path_.value() |
| 59 << " to destination " << dest_path_.value(); | 58 << " to destination " << dest_path_.value(); |
| 60 } else { | 59 } else { |
| 61 LOG(ERROR) << "failed move " << source_path_.value() | 60 LOG(ERROR) << "failed move " << source_path_.value() |
| 62 << " to " << dest_path_.value(); | 61 << " to " << dest_path_.value(); |
| 63 return false; | 62 return false; |
| 64 } | 63 } |
| 65 | 64 |
| 66 return true; | 65 return true; |
| 67 } | 66 } |
| 68 | 67 |
| 69 void MoveTreeWorkItem::Rollback() { | 68 void MoveTreeWorkItem::Rollback() { |
| 70 if (moved_to_dest_path_ && !file_util::Move(dest_path_, source_path_)) | 69 if (moved_to_dest_path_ && !file_util::Move(dest_path_, source_path_)) |
| 71 LOG(ERROR) << "Can not move " << dest_path_.value() | 70 LOG(ERROR) << "Can not move " << dest_path_.value() |
| 72 << " to " << source_path_.value(); | 71 << " to " << source_path_.value(); |
| 73 | 72 |
| 74 if (moved_to_backup_ && !file_util::Move(backup_path_, dest_path_)) | 73 if (moved_to_backup_) { |
| 75 LOG(ERROR) << "failed move " << backup_path_.value() | 74 FilePath backup = backup_path_.path().Append(dest_path_.BaseName()); |
| 76 << " to " << dest_path_.value(); | 75 if (!file_util::Move(backup, dest_path_)) { |
| 76 LOG(ERROR) << "failed move " << backup.value() |
| 77 << " to " << dest_path_.value(); |
| 78 } |
| 79 } |
| 77 } | 80 } |
| OLD | NEW |