blob: 67185b86f140fb147526ebcf596c0c2b1963cc6d [file] [log] [blame]
Jiakai Zhanga2ba6962022-07-04 16:56:12 +01001/*
2 * Copyright (C) 2022 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#include "file_utils.h"
18
19#include <fcntl.h>
20#include <sys/stat.h>
21#include <sys/types.h>
22#include <unistd.h>
23
24#include <filesystem>
25#include <memory>
26#include <string>
27#include <string_view>
28#include <system_error>
29#include <utility>
30
31#include "aidl/com/android/server/art/FsPermission.h"
32#include "android-base/errors.h"
33#include "android-base/logging.h"
34#include "android-base/result.h"
35#include "android-base/scopeguard.h"
36#include "android-base/stringprintf.h"
37#include "base/os.h"
38#include "base/unix_file/fd_file.h"
39
40namespace art {
41namespace artd {
42
43namespace {
44
45using ::aidl::com::android::server::art::FsPermission;
46using ::android::base::make_scope_guard;
47using ::android::base::Result;
48using ::android::base::StringPrintf;
49
50void UnlinkIfExists(const std::string& path) {
51 std::error_code ec;
52 if (!std::filesystem::remove(path, ec)) {
53 if (ec.value() != ENOENT) {
54 LOG(WARNING) << StringPrintf(
55 "Failed to remove file '%s': %s", path.c_str(), ec.message().c_str());
56 }
57 }
58}
59
60} // namespace
61
62Result<std::unique_ptr<NewFile>> NewFile::Create(const std::string& path,
63 const FsPermission& fs_permission) {
64 std::unique_ptr<NewFile> output_file(new NewFile(path, fs_permission));
65 OR_RETURN(output_file->Init());
66 return output_file;
67}
68
69NewFile::~NewFile() { Cleanup(); }
70
71Result<void> NewFile::Keep() {
72 if (close(std::exchange(fd_, -1)) != 0) {
73 return ErrnoErrorf("Failed to close file '{}'", temp_path_);
74 }
75 return {};
76}
77
78Result<void> NewFile::CommitOrAbandon() {
79 auto cleanup = make_scope_guard([this] { Unlink(); });
80 OR_RETURN(Keep());
81 std::error_code ec;
82 std::filesystem::rename(temp_path_, final_path_, ec);
83 if (ec) {
84 return Errorf(
85 "Failed to move new file '{}' to path '{}': {}", temp_path_, final_path_, ec.message());
86 }
87 cleanup.Disable();
88 committed_ = true;
89 return {};
90}
91
92void NewFile::Cleanup() {
93 if (fd_ >= 0) {
94 Unlink();
95 if (close(std::exchange(fd_, -1)) != 0) {
96 // Nothing we can do. If the file is already unlinked, it will go away when the process exits.
97 PLOG(WARNING) << "Failed to close file '" << temp_path_ << "'";
98 }
99 }
100}
101
102Result<void> NewFile::Init() {
103 mode_t mode = FsPermissionToMode(fs_permission_);
104 // "<path_>.XXXXXX.tmp".
105 temp_path_ = BuildTempPath(final_path_, "XXXXXX");
106 fd_ = mkstemps(temp_path_.data(), /*suffixlen=*/4);
107 if (fd_ < 0) {
108 return ErrnoErrorf("Failed to create temp file for '{}'", final_path_);
109 }
110 temp_id_ = temp_path_.substr(/*pos=*/final_path_.length() + 1, /*count=*/6);
111 if (fchmod(fd_, mode) != 0) {
112 return ErrnoErrorf("Failed to chmod file '{}'", temp_path_);
113 }
114 if (fchown(fd_, fs_permission_.uid, fs_permission_.gid) != 0) {
115 return ErrnoErrorf("Failed to chown file '{}'", temp_path_);
116 }
117 return {};
118}
119
120void NewFile::Unlink() {
121 // This should never fail. We were able to create the file, so we should be able to remove it.
122 UnlinkIfExists(temp_path_);
123}
124
125Result<void> NewFile::CommitAllOrAbandon(const std::vector<NewFile*>& files_to_commit,
126 const std::vector<std::string_view>& files_to_remove) {
127 std::vector<std::pair<std::string_view, std::string>> moved_files;
128
129 auto cleanup = make_scope_guard([&]() {
130 // Clean up new files.
131 for (NewFile* new_file : files_to_commit) {
132 if (new_file->committed_) {
133 UnlinkIfExists(new_file->FinalPath());
134 } else {
135 new_file->Cleanup();
136 }
137 }
138
139 // Move old files back.
140 for (const auto& [original_path, temp_path] : moved_files) {
141 std::error_code ec;
142 std::filesystem::rename(temp_path, original_path, ec);
143 if (ec) {
144 // This should never happen. We were able to move the file from `original_path` to
145 // `temp_path`. We should be able to move it back.
146 LOG(WARNING) << StringPrintf(
147 "Failed to move old file '%s' back from temporary path '%s': %s",
148 std::string(original_path).c_str(),
149 std::string(temp_path).c_str(),
150 ec.message().c_str());
151 }
152 }
153 });
154
155 // Move old files to temporary locations.
156 std::vector<std::string_view> all_files_to_remove;
157 for (NewFile* file : files_to_commit) {
158 all_files_to_remove.push_back(file->FinalPath());
159 }
160 all_files_to_remove.insert(
161 all_files_to_remove.end(), files_to_remove.begin(), files_to_remove.end());
162
163 for (std::string_view original_path : all_files_to_remove) {
164 std::error_code ec;
165 std::filesystem::file_status status = std::filesystem::status(original_path, ec);
166 if (!std::filesystem::status_known(status)) {
167 return Errorf("Failed to get status of old file '{}': {}", original_path, ec.message());
168 }
169 if (std::filesystem::is_directory(status)) {
170 return ErrnoErrorf("Old file '{}' is a directory", original_path);
171 }
172 if (std::filesystem::exists(status)) {
173 std::string temp_path = BuildTempPath(original_path, "XXXXXX");
174 int fd = mkstemps(temp_path.data(), /*suffixlen=*/4);
175 if (fd < 0) {
176 return ErrnoErrorf("Failed to create temporary path for old file '{}'", original_path);
177 }
178 close(fd);
179
180 std::filesystem::rename(original_path, temp_path, ec);
181 if (ec) {
182 UnlinkIfExists(temp_path);
183 return Errorf("Failed to move old file '{}' to temporary path '{}': {}",
184 original_path,
185 temp_path,
186 ec.message());
187 }
188
189 moved_files.push_back({original_path, std::move(temp_path)});
190 }
191 }
192
193 // Commit new files.
194 for (NewFile* file : files_to_commit) {
195 OR_RETURN(file->CommitOrAbandon());
196 }
197
198 cleanup.Disable();
199
200 // Clean up old files.
201 for (const auto& [original_path, temp_path] : moved_files) {
202 // This should never fail. We were able to move the file to `temp_path`. We should be able to
203 // remove it.
204 UnlinkIfExists(temp_path);
205 }
206
207 return {};
208}
209
210std::string NewFile::BuildTempPath(std::string_view final_path, const std::string& id) {
211 return StringPrintf("%s.%s.tmp", std::string(final_path).c_str(), id.c_str());
212}
213
214Result<std::unique_ptr<File>> OpenFileForReading(const std::string& path) {
215 std::unique_ptr<File> file(OS::OpenFileForReading(path.c_str()));
216 if (file == nullptr) {
217 return ErrnoErrorf("Failed to open file '{}'", path);
218 }
219 return file;
220}
221
222mode_t FsPermissionToMode(const FsPermission& fs_permission) {
223 return S_IRUSR | S_IWUSR | S_IRGRP | (fs_permission.isOtherReadable ? S_IROTH : 0) |
224 (fs_permission.isOtherExecutable ? S_IXOTH : 0);
225}
226
227} // namespace artd
228} // namespace art