Chromium Code Reviews| 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/copy_tree_work_item.h" | 5 #include "chrome/installer/util/copy_tree_work_item.h" |
| 6 | 6 |
| 7 #include <shlwapi.h> | 7 #include <shlwapi.h> |
| 8 | 8 |
| 9 #include "base/files/file_util.h" | 9 #include "base/files/file_util.h" |
| 10 #include "base/logging.h" | 10 #include "base/logging.h" |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 64 return true; | 64 return true; |
| 65 } | 65 } |
| 66 } else if ((dest_exist) && | 66 } else if ((dest_exist) && |
| 67 (overwrite_option_ == WorkItem::IF_NOT_PRESENT)) { | 67 (overwrite_option_ == WorkItem::IF_NOT_PRESENT)) { |
| 68 // handle overwrite_option_ = IF_NOT_PRESENT case. | 68 // handle overwrite_option_ = IF_NOT_PRESENT case. |
| 69 return true; | 69 return true; |
| 70 } | 70 } |
| 71 | 71 |
| 72 // In all cases that reach here, move dest to a backup path. | 72 // In all cases that reach here, move dest to a backup path. |
| 73 if (dest_exist) { | 73 if (dest_exist) { |
| 74 if (!backup_path_.CreateUniqueTempDirUnderPath(temp_dir_)) { | 74 if (!backup_dir_.CreateUniqueTempDirUnderPath(temp_dir_)) { |
| 75 PLOG(ERROR) << "Failed to get backup path in folder " | 75 PLOG(ERROR) << "Failed to get backup path in folder " |
| 76 << temp_dir_.value(); | 76 << temp_dir_.value(); |
| 77 return false; | 77 return false; |
| 78 } | 78 } |
| 79 backup_path_ = backup_dir_.GetPath(); | |
|
gab
2016/09/28 16:31:59
This results in an extra copy though. I did read y
vabr (Chromium)
2016/09/28 20:06:36
Here yes, but one of the unit tests [1] uses backu
gab
2016/09/28 20:25:25
So a bool would do? I also haven't looked at the t
vabr (Chromium)
2016/09/28 20:33:10
Happy to switch to a bool.
| |
| 79 | 80 |
| 80 base::FilePath backup = backup_path_.path().Append(dest_path_.BaseName()); | 81 base::FilePath backup = backup_path_.Append(dest_path_.BaseName()); |
| 81 if (base::Move(dest_path_, backup)) { | 82 if (base::Move(dest_path_, backup)) { |
| 82 moved_to_backup_ = true; | 83 moved_to_backup_ = true; |
| 83 VLOG(1) << "Moved destination " << dest_path_.value() << | 84 VLOG(1) << "Moved destination " << dest_path_.value() << |
| 84 " to backup path " << backup.value(); | 85 " to backup path " << backup.value(); |
| 85 } else { | 86 } else { |
| 86 PLOG(ERROR) << "failed moving " << dest_path_.value() | 87 PLOG(ERROR) << "failed moving " << dest_path_.value() |
| 87 << " to " << backup.value(); | 88 << " to " << backup.value(); |
| 88 return false; | 89 return false; |
| 89 } | 90 } |
| 90 } | 91 } |
| (...skipping 15 matching lines...) Expand all Loading... | |
| 106 void CopyTreeWorkItem::RollbackImpl() { | 107 void CopyTreeWorkItem::RollbackImpl() { |
| 107 // Normally the delete operations below should not fail unless some | 108 // Normally the delete operations below should not fail unless some |
| 108 // programs like anti-virus are inspecting the files we just copied. | 109 // programs like anti-virus are inspecting the files we just copied. |
| 109 // If this does happen sometimes, we may consider using Move instead of | 110 // If this does happen sometimes, we may consider using Move instead of |
| 110 // Delete here. For now we just log the error and continue with the | 111 // Delete here. For now we just log the error and continue with the |
| 111 // rest of rollback operation. | 112 // rest of rollback operation. |
| 112 if (copied_to_dest_path_ && !base::DeleteFile(dest_path_, true)) { | 113 if (copied_to_dest_path_ && !base::DeleteFile(dest_path_, true)) { |
| 113 LOG(ERROR) << "Can not delete " << dest_path_.value(); | 114 LOG(ERROR) << "Can not delete " << dest_path_.value(); |
| 114 } | 115 } |
| 115 if (moved_to_backup_) { | 116 if (moved_to_backup_) { |
| 116 base::FilePath backup(backup_path_.path().Append(dest_path_.BaseName())); | 117 base::FilePath backup(backup_path_.Append(dest_path_.BaseName())); |
| 117 if (!base::Move(backup, dest_path_)) { | 118 if (!base::Move(backup, dest_path_)) { |
| 118 PLOG(ERROR) << "failed move " << backup.value() | 119 PLOG(ERROR) << "failed move " << backup.value() |
| 119 << " to " << dest_path_.value(); | 120 << " to " << dest_path_.value(); |
| 120 } | 121 } |
| 121 } | 122 } |
| 122 if (copied_to_alternate_path_ && | 123 if (copied_to_alternate_path_ && |
| 123 !base::DeleteFile(alternative_path_, true)) { | 124 !base::DeleteFile(alternative_path_, true)) { |
| 124 LOG(ERROR) << "Can not delete " << alternative_path_.value(); | 125 LOG(ERROR) << "Can not delete " << alternative_path_.value(); |
| 125 } | 126 } |
| 126 } | 127 } |
| 127 | 128 |
| 128 bool CopyTreeWorkItem::IsFileInUse(const base::FilePath& path) { | 129 bool CopyTreeWorkItem::IsFileInUse(const base::FilePath& path) { |
| 129 if (!base::PathExists(path)) | 130 if (!base::PathExists(path)) |
| 130 return false; | 131 return false; |
| 131 | 132 |
| 132 HANDLE handle = ::CreateFile(path.value().c_str(), FILE_ALL_ACCESS, | 133 HANDLE handle = ::CreateFile(path.value().c_str(), FILE_ALL_ACCESS, |
| 133 NULL, NULL, OPEN_EXISTING, NULL, NULL); | 134 NULL, NULL, OPEN_EXISTING, NULL, NULL); |
| 134 if (handle == INVALID_HANDLE_VALUE) | 135 if (handle == INVALID_HANDLE_VALUE) |
| 135 return true; | 136 return true; |
| 136 | 137 |
| 137 CloseHandle(handle); | 138 CloseHandle(handle); |
| 138 return false; | 139 return false; |
| 139 } | 140 } |
| OLD | NEW |