1 /*
2 * Copyright (C) 2020 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #ifndef ANDROID_APEXD_APEXD_ROLLBACK_UTILS_H_
18 #define ANDROID_APEXD_APEXD_ROLLBACK_UTILS_H_
19
20 #include <filesystem>
21 #include <string>
22
23 #include <android-base/logging.h>
24 #include <android-base/macros.h>
25 #include <android-base/result.h>
26 #include <android-base/scopeguard.h>
27 #include <logwrap/logwrap.h>
28 #include <selinux/android.h>
29
30 using android::base::Error;
31 using android::base::Result;
32
33 namespace android {
34 namespace apex {
35
36 static constexpr const char* kCpPath = "/system/bin/cp";
37
38 /**
39 * Copies everything including directories from the "from" path to the "to"
40 * path. Note that this will fail if run before APEXes are mounted, due to a
41 * dependency on runtime.
42 */
copy_directory_recursive(const char * from,const char * to)43 int32_t copy_directory_recursive(const char* from, const char* to) {
44 const char* const argv[] = {
45 kCpPath,
46 "-F", /* delete any existing destination file first
47 (--remove-destination) */
48 "-p", /* preserve timestamps, ownership, and permissions */
49 "-R", /* recurse into subdirectories (DEST must be a directory) */
50 "-P", /* Do not follow symlinks [default] */
51 "-d", /* don't dereference symlinks */
52 from,
53 to};
54
55 LOG(DEBUG) << "Copying " << from << " to " << to;
56 return logwrap_fork_execvp(arraysize(argv), argv, nullptr, false, LOG_ALOG,
57 false, nullptr);
58 }
59
60 /**
61 * Deletes any files at to_path, and then copies all files and directories
62 * from from_path into to_path. Note that this must be run after APEXes are
63 * mounted.
64 */
ReplaceFiles(const std::string & from_path,const std::string & to_path)65 inline Result<void> ReplaceFiles(const std::string& from_path,
66 const std::string& to_path) {
67 namespace fs = std::filesystem;
68
69 std::error_code error_code;
70 fs::remove_all(to_path, error_code);
71 if (error_code) {
72 return Error() << "Failed to delete existing files at " << to_path << " : "
73 << error_code.message();
74 }
75
76 auto deleter = [&] {
77 std::error_code error_code;
78 fs::remove_all(to_path, error_code);
79 if (error_code) {
80 LOG(ERROR) << "Failed to clean up files at " << to_path << " : "
81 << error_code.message();
82 }
83 };
84 auto scope_guard = android::base::make_scope_guard(deleter);
85
86 int rc = copy_directory_recursive(from_path.c_str(), to_path.c_str());
87 if (rc != 0) {
88 return Error() << "Failed to copy from [" << from_path << "] to ["
89 << to_path << "]";
90 }
91 scope_guard.Disable();
92 return {};
93 }
94
RestoreconPath(const std::string & path)95 inline Result<void> RestoreconPath(const std::string& path) {
96 unsigned int seflags = SELINUX_ANDROID_RESTORECON_RECURSE;
97 if (selinux_android_restorecon(path.c_str(), seflags) < 0) {
98 return Error() << "Failed to restorecon " << path;
99 }
100 return {};
101 }
102
103 } // namespace apex
104 } // namespace android
105
106 #endif // ANDROID_APEXD_APEXD_ROLLBACK_UTILS_H_
107