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_ << " does not exist"; | 29 LOG(ERROR) << source_path_.value() << " does not exist"; |
30 return false; | 30 return false; |
31 } | 31 } |
32 | 32 |
33 FilePath backup_path; | |
34 | |
35 // If dest_path_ exists, move destination to a backup path. | 33 // If dest_path_ exists, move destination to a backup path. |
36 if (file_util::PathExists(dest_path_)) { | 34 if (file_util::PathExists(dest_path_)) { |
37 // Generate a backup path that can keep the original files under dest_path_. | 35 // Generate a backup path that can keep the original files under dest_path_. |
38 if (!file_util::CreateTemporaryFileInDir(FilePath(temp_dir_), | 36 if (!file_util::CreateTemporaryFileInDir(FilePath(temp_dir_), |
39 &backup_path)) { | 37 &backup_path_)) { |
40 LOG(ERROR) << "Failed to get backup path in folder " << temp_dir_; | 38 LOG(ERROR) << "Failed to get backup path in folder " << temp_dir_.value(); |
41 return false; | 39 return false; |
42 } | 40 } |
43 | 41 |
44 backup_path_ = backup_path.value(); | |
45 | |
46 if (file_util::Move(dest_path_, backup_path_)) { | 42 if (file_util::Move(dest_path_, backup_path_)) { |
47 moved_to_backup_ = true; | 43 moved_to_backup_ = true; |
48 LOG(INFO) << "Moved destination " << dest_path_ | 44 LOG(INFO) << "Moved destination " << dest_path_.value() |
49 << " to backup path " << backup_path_; | 45 << " to backup path " << backup_path_.value(); |
50 } else { | 46 } else { |
51 LOG(ERROR) << "failed moving " << dest_path_ | 47 LOG(ERROR) << "failed moving " << dest_path_.value() |
52 << " to " << backup_path_; | 48 << " to " << backup_path_.value(); |
53 return false; | 49 return false; |
54 } | 50 } |
55 } | 51 } |
56 | 52 |
57 // Now move source to destination. | 53 // Now move source to destination. |
58 if (file_util::Move(source_path_, dest_path_)) { | 54 if (file_util::Move(source_path_, dest_path_)) { |
59 moved_to_dest_path_ = true; | 55 moved_to_dest_path_ = true; |
60 LOG(INFO) << "Moved source " << source_path_ | 56 LOG(INFO) << "Moved source " << source_path_.value() |
61 << " to destination " << dest_path_; | 57 << " to destination " << dest_path_.value(); |
62 } else { | 58 } else { |
63 LOG(ERROR) << "failed move " << source_path_ << " to " << dest_path_; | 59 LOG(ERROR) << "failed move " << source_path_.value() << " to " << |
| 60 dest_path_.value(); |
64 return false; | 61 return false; |
65 } | 62 } |
66 | 63 |
67 return true; | 64 return true; |
68 } | 65 } |
69 | 66 |
70 void MoveTreeWorkItem::Rollback() { | 67 void MoveTreeWorkItem::Rollback() { |
71 if (moved_to_dest_path_ && !file_util::Move(dest_path_, source_path_)) | 68 if (moved_to_dest_path_ && !file_util::Move(dest_path_, source_path_)) |
72 LOG(ERROR) << "Can not move " << dest_path_ << " to " << source_path_; | 69 LOG(ERROR) << "Can not move " << dest_path_.value() << |
| 70 " to " << source_path_.value(); |
73 | 71 |
74 if (moved_to_backup_ && !file_util::Move(backup_path_, dest_path_)) | 72 if (moved_to_backup_ && !file_util::Move(backup_path_, dest_path_)) |
75 LOG(ERROR) << "failed move " << backup_path_ << " to " << dest_path_; | 73 LOG(ERROR) << "failed move " << backup_path_.value() << |
| 74 " to " << dest_path_.value(); |
76 } | 75 } |
OLD | NEW |