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