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 "base/file_util.h" | 5 #include "base/file_util.h" |
6 #include "base/logging.h" | 6 #include "base/logging.h" |
7 #include "chrome/installer/util/delete_tree_work_item.h" | 7 #include "chrome/installer/util/delete_tree_work_item.h" |
8 | 8 |
9 DeleteTreeWorkItem::~DeleteTreeWorkItem() { | 9 DeleteTreeWorkItem::~DeleteTreeWorkItem() { |
10 std::wstring tmp_dir = file_util::GetDirectoryFromPath(backup_path_); | 10 if (!backup_path_.empty()) { |
11 if (file_util::PathExists(tmp_dir)) { | 11 FilePath tmp_dir = backup_path_.DirName(); |
12 file_util::Delete(tmp_dir, true); | 12 if (file_util::PathExists(tmp_dir)) { |
| 13 file_util::Delete(tmp_dir, true); |
| 14 } |
13 } | 15 } |
14 tmp_dir = file_util::GetDirectoryFromPath(key_backup_path_); | 16 if (!key_backup_path_.empty()) { |
15 if (file_util::PathExists(tmp_dir)) { | 17 FilePath tmp_dir = key_backup_path_.DirName(); |
16 file_util::Delete(tmp_dir, true); | 18 if (file_util::PathExists(tmp_dir)) { |
| 19 file_util::Delete(tmp_dir, true); |
| 20 } |
17 } | 21 } |
18 } | 22 } |
19 | 23 |
20 DeleteTreeWorkItem::DeleteTreeWorkItem(const std::wstring& root_path, | 24 DeleteTreeWorkItem::DeleteTreeWorkItem(const std::wstring& root_path, |
21 const std::wstring& key_path) | 25 const std::wstring& key_path) |
22 : root_path_(root_path), | 26 : root_path_(root_path), |
23 key_path_(key_path) { | 27 key_path_(key_path) { |
24 } | 28 } |
25 | 29 |
26 // We first try to move key_path_ to backup_path. If it succeeds, we go ahead | 30 // We first try to move key_path_ to backup_path. If it succeeds, we go ahead |
27 // and move the rest. | 31 // and move the rest. |
28 bool DeleteTreeWorkItem::Do() { | 32 bool DeleteTreeWorkItem::Do() { |
29 if (!key_path_.empty() && file_util::PathExists(key_path_)) { | 33 if (!key_path_.empty() && file_util::PathExists(key_path_)) { |
30 if (!GetBackupPath(key_path_, &key_backup_path_) || | 34 if (!GetBackupPath(key_path_, &key_backup_path_) || |
31 !file_util::CopyDirectory(key_path_, key_backup_path_, true) || | 35 !file_util::CopyDirectory(key_path_, key_backup_path_, true) || |
32 !file_util::Delete(key_path_, true)) { | 36 !file_util::Delete(key_path_, true)) { |
33 LOG(ERROR) << "can not delete " << key_path_ | 37 LOG(ERROR) << "can not delete " << key_path_.value() |
34 << " OR copy it to backup path " << key_backup_path_; | 38 << " OR copy it to backup path " << key_backup_path_.value(); |
35 return false; | 39 return false; |
36 } | 40 } |
37 } | 41 } |
38 | 42 |
39 if (!root_path_.empty() && file_util::PathExists(root_path_)) { | 43 if (!root_path_.empty() && file_util::PathExists(root_path_)) { |
40 if (!GetBackupPath(root_path_, &backup_path_) || | 44 if (!GetBackupPath(root_path_, &backup_path_) || |
41 !file_util::CopyDirectory(root_path_, backup_path_, true) || | 45 !file_util::CopyDirectory(root_path_, backup_path_, true) || |
42 !file_util::Delete(root_path_, true)) { | 46 !file_util::Delete(root_path_, true)) { |
43 LOG(ERROR) << "can not delete " << root_path_ | 47 LOG(ERROR) << "can not delete " << root_path_.value() |
44 << " OR copy it to backup path " << backup_path_; | 48 << " OR copy it to backup path " << backup_path_.value(); |
45 return false; | 49 return false; |
46 } | 50 } |
47 } | 51 } |
48 return true; | 52 return true; |
49 } | 53 } |
50 | 54 |
51 // If there are files in backup paths move them back. | 55 // If there are files in backup paths move them back. |
52 void DeleteTreeWorkItem::Rollback() { | 56 void DeleteTreeWorkItem::Rollback() { |
53 if (!backup_path_.empty() && file_util::PathExists(backup_path_)) { | 57 if (!backup_path_.empty() && file_util::PathExists(backup_path_)) { |
54 file_util::Move(backup_path_, root_path_); | 58 file_util::Move(backup_path_, root_path_); |
55 } | 59 } |
56 if (!key_backup_path_.empty() && file_util::PathExists(key_backup_path_)) { | 60 if (!key_backup_path_.empty() && file_util::PathExists(key_backup_path_)) { |
57 file_util::Move(key_backup_path_, key_path_); | 61 file_util::Move(key_backup_path_, key_path_); |
58 } | 62 } |
59 } | 63 } |
60 | 64 |
61 bool DeleteTreeWorkItem::GetBackupPath(const std::wstring& for_path, | 65 bool DeleteTreeWorkItem::GetBackupPath(const FilePath& for_path, |
62 std::wstring* backup_path) { | 66 FilePath* backup_path) { |
63 if (!file_util::CreateNewTempDirectory(L"", backup_path)) { | 67 if (!file_util::CreateNewTempDirectory(L"", backup_path)) { |
64 // We assume that CreateNewTempDirectory() is doing its job well. | 68 // We assume that CreateNewTempDirectory() is doing its job well. |
65 LOG(ERROR) << "Couldn't get backup path for delete."; | 69 LOG(ERROR) << "Couldn't get backup path for delete."; |
66 return false; | 70 return false; |
67 } | 71 } |
68 std::wstring file_name = file_util::GetFilenameFromPath(for_path); | |
69 file_util::AppendToPath(backup_path, file_name); | |
70 | 72 |
| 73 *backup_path = backup_path->Append(for_path.BaseName()); |
71 return true; | 74 return true; |
72 } | 75 } |
OLD | NEW |