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