Avi Drissman | 4a8573c | 2022-09-09 19:35:54 | [diff] [blame] | 1 | // Copyright 2020 The Chromium Authors |
Yann Dago | 78100ea | 2020-02-04 17:46:34 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "chrome/browser/downgrade/downgrade_utils.h" |
| 6 | |
| 7 | #include "base/files/file_enumerator.h" |
| 8 | #include "base/files/file_util.h" |
Hans Wennborg | f6ad69c | 2020-06-18 18:02:32 | [diff] [blame] | 9 | #include "base/logging.h" |
Yann Dago | 78100ea | 2020-02-04 17:46:34 | [diff] [blame] | 10 | #include "build/build_config.h" |
| 11 | #include "chrome/browser/downgrade/user_data_downgrade.h" |
| 12 | |
Xiaohan Wang | fb1e615d | 2022-01-15 22:25:34 | [diff] [blame] | 13 | #if BUILDFLAG(IS_WIN) |
Yann Dago | 78100ea | 2020-02-04 17:46:34 | [diff] [blame] | 14 | #include <windows.h> |
Xiaohan Wang | fb1e615d | 2022-01-15 22:25:34 | [diff] [blame] | 15 | #elif BUILDFLAG(IS_POSIX) |
Yann Dago | 78100ea | 2020-02-04 17:46:34 | [diff] [blame] | 16 | #include "base/files/file.h" |
| 17 | #endif |
| 18 | |
| 19 | namespace downgrade { |
| 20 | |
| 21 | base::FilePath GetTempDirNameForDelete(const base::FilePath& dir, |
| 22 | const base::FilePath& name) { |
| 23 | if (dir.empty()) |
| 24 | return base::FilePath(); |
| 25 | |
| 26 | return base::GetUniquePath( |
| 27 | dir.Append(name).AddExtension(kDowngradeDeleteSuffix)); |
| 28 | } |
| 29 | |
| 30 | bool MoveWithoutFallback(const base::FilePath& source, |
| 31 | const base::FilePath& target) { |
Xiaohan Wang | fb1e615d | 2022-01-15 22:25:34 | [diff] [blame] | 32 | #if BUILDFLAG(IS_WIN) |
Yann Dago | 78100ea | 2020-02-04 17:46:34 | [diff] [blame] | 33 | // TODO(grt): check whether or not this is sufficiently atomic when |source| |
| 34 | // is on a network share. |
| 35 | auto result = ::MoveFileEx(source.value().c_str(), target.value().c_str(), 0); |
| 36 | PLOG_IF(ERROR, !result) << source << " -> " << target; |
| 37 | return result; |
Hzj_jie | 0416317 | 2024-04-29 19:37:16 | [diff] [blame] | 38 | #elif BUILDFLAG(IS_POSIX) |
Yann Dago | 78100ea | 2020-02-04 17:46:34 | [diff] [blame] | 39 | // Windows compatibility: if |target| exists, |source| and |target| |
| 40 | // must be the same type, either both files, or both directories. |
| 41 | base::stat_wrapper_t target_info; |
danakj | 9fa839d | 2024-05-03 16:34:22 | [diff] [blame] | 42 | if (base::File::Stat(target, &target_info) == 0) { |
Yann Dago | 78100ea | 2020-02-04 17:46:34 | [diff] [blame] | 43 | base::stat_wrapper_t source_info; |
danakj | 9fa839d | 2024-05-03 16:34:22 | [diff] [blame] | 44 | if (base::File::Stat(source, &source_info) != 0) { |
Yann Dago | 78100ea | 2020-02-04 17:46:34 | [diff] [blame] | 45 | return false; |
danakj | 9fa839d | 2024-05-03 16:34:22 | [diff] [blame] | 46 | } |
Yann Dago | 78100ea | 2020-02-04 17:46:34 | [diff] [blame] | 47 | if (S_ISDIR(target_info.st_mode) != S_ISDIR(source_info.st_mode)) |
| 48 | return false; |
| 49 | } |
| 50 | |
| 51 | auto result = rename(source.value().c_str(), target.value().c_str()); |
| 52 | PLOG_IF(ERROR, result) << source << " -> " << target; |
| 53 | return !result; |
| 54 | #endif |
| 55 | } |
| 56 | |
Yann Dago | f20d6f0 | 2021-09-20 17:21:02 | [diff] [blame] | 57 | bool MoveContents(const base::FilePath& source, |
| 58 | const base::FilePath& target, |
| 59 | ExclusionPredicate exclusion_predicate) { |
Yann Dago | 78100ea | 2020-02-04 17:46:34 | [diff] [blame] | 60 | // Implementation note: moving is better than deleting in this case since it |
| 61 | // avoids certain failure modes. For example: on Windows, a file that is open |
| 62 | // with FILE_SHARE_DELETE can be moved or marked for deletion. If it is moved |
| 63 | // aside, the containing directory may then be eligible for deletion. If, on |
| 64 | // the other hand, it is marked for deletion, it cannot be moved nor can its |
| 65 | // containing directory be moved or deleted. |
Yann Dago | f20d6f0 | 2021-09-20 17:21:02 | [diff] [blame] | 66 | bool all_succeeded = base::CreateDirectory(target); |
| 67 | if (!all_succeeded) { |
Yann Dago | 78100ea | 2020-02-04 17:46:34 | [diff] [blame] | 68 | PLOG(ERROR) << target; |
Yann Dago | f20d6f0 | 2021-09-20 17:21:02 | [diff] [blame] | 69 | return all_succeeded; |
Yann Dago | 78100ea | 2020-02-04 17:46:34 | [diff] [blame] | 70 | } |
| 71 | |
Yann Dago | 78100ea | 2020-02-04 17:46:34 | [diff] [blame] | 72 | base::FileEnumerator enumerator( |
| 73 | source, false, |
| 74 | base::FileEnumerator::FILES | base::FileEnumerator::DIRECTORIES); |
| 75 | for (base::FilePath path = enumerator.Next(); !path.empty(); |
| 76 | path = enumerator.Next()) { |
| 77 | const base::FileEnumerator::FileInfo info = enumerator.GetInfo(); |
| 78 | const base::FilePath name = info.GetName(); |
| 79 | if (exclusion_predicate && exclusion_predicate.Run(name)) |
| 80 | continue; |
| 81 | const base::FilePath this_target = target.Append(name); |
| 82 | // A directory can be moved unless any file within it is open. A simple file |
| 83 | // can be moved unless it is opened without FILE_SHARE_DELETE. (As with most |
| 84 | // things in life, there are exceptions to this rule, but they are |
| 85 | // uncommon. For example, a file opened without FILE_SHARE_DELETE can be |
| 86 | // moved as long as it was opened only with some combination of |
| 87 | // READ_CONTROL, WRITE_DAC, WRITE_OWNER, and SYNCHRONIZE access rights. |
| 88 | // Since this short list excludes such useful rights as FILE_EXECUTE, |
| 89 | // FILE_READ_DATA, and most anything else one would want a file for, it's |
| 90 | // likely an uncommon scenario. See OpenFileTest in base/files for more.) |
| 91 | if (MoveWithoutFallback(path, this_target)) |
| 92 | continue; |
| 93 | if (!info.IsDirectory()) { |
Yann Dago | f20d6f0 | 2021-09-20 17:21:02 | [diff] [blame] | 94 | all_succeeded = false; |
Yann Dago | 78100ea | 2020-02-04 17:46:34 | [diff] [blame] | 95 | continue; |
| 96 | } |
Yann Dago | f20d6f0 | 2021-09-20 17:21:02 | [diff] [blame] | 97 | MoveContents(path, this_target, ExclusionPredicate()); |
Yann Dago | 78100ea | 2020-02-04 17:46:34 | [diff] [blame] | 98 | // If everything within the directory was moved, it may be possible to |
| 99 | // delete it now. |
Lei Zhang | fa6861ee | 2020-06-25 20:37:16 | [diff] [blame] | 100 | if (!base::DeleteFile(path)) |
Yann Dago | f20d6f0 | 2021-09-20 17:21:02 | [diff] [blame] | 101 | all_succeeded = false; |
Yann Dago | 78100ea | 2020-02-04 17:46:34 | [diff] [blame] | 102 | } |
Yann Dago | f20d6f0 | 2021-09-20 17:21:02 | [diff] [blame] | 103 | return all_succeeded; |
Yann Dago | 78100ea | 2020-02-04 17:46:34 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | } // namespace downgrade |