1 /*
2 * Copyright 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 #include "os/files.h"
18
19 #include <bluetooth/log.h>
20 #include <fcntl.h>
21 #include <libgen.h>
22 #include <sys/stat.h>
23 #include <unistd.h>
24
25 #include <cerrno>
26 #include <cstring>
27 #include <fstream>
28 #include <streambuf>
29 #include <string>
30
31 namespace {
32
HandleError(const std::string & temp_path,int * dir_fd,FILE ** fp)33 void HandleError(const std::string& temp_path, int* dir_fd, FILE** fp) {
34 // This indicates there is a write issue. Unlink as partial data is not
35 // acceptable.
36 unlink(temp_path.c_str());
37 if (*fp) {
38 fclose(*fp);
39 *fp = nullptr;
40 }
41 if (*dir_fd != -1) {
42 close(*dir_fd);
43 *dir_fd = -1;
44 }
45 }
46
47 } // namespace
48
49 namespace bluetooth {
50 namespace os {
51
FileExists(const std::string & path)52 bool FileExists(const std::string& path) {
53 std::ifstream input(path, std::ios::binary | std::ios::ate);
54 return input.good();
55 }
56
RenameFile(const std::string & from,const std::string & to)57 bool RenameFile(const std::string& from, const std::string& to) {
58 if (std::rename(from.c_str(), to.c_str()) != 0) {
59 log::error("unable to rename file from '{}' to '{}', error: {}", from, to, strerror(errno));
60 return false;
61 }
62 return true;
63 }
64
ReadSmallFile(const std::string & path)65 std::optional<std::string> ReadSmallFile(const std::string& path) {
66 std::ifstream input(path, std::ios::binary | std::ios::ate);
67 if (!input) {
68 log::warn("Failed to open file '{}', error: {}", path, strerror(errno));
69 return std::nullopt;
70 }
71 auto file_size = input.tellg();
72 if (file_size < 0) {
73 log::warn("Failed to get file size for '{}', error: {}", path, strerror(errno));
74 return std::nullopt;
75 }
76 std::string result(file_size, '\0');
77 if (!input.seekg(0)) {
78 log::warn("Failed to go back to the beginning of file '{}', error: {}", path, strerror(errno));
79 return std::nullopt;
80 }
81 if (!input.read(result.data(), result.size())) {
82 log::warn("Failed to read file '{}', error: {}", path, strerror(errno));
83 return std::nullopt;
84 }
85 input.close();
86 return result;
87 }
88
WriteToFile(const std::string & path,const std::string & data)89 bool WriteToFile(const std::string& path, const std::string& data) {
90 log::assert_that(!path.empty(), "assert failed: !path.empty()");
91 // Steps to ensure content of data gets to disk:
92 //
93 // 1) Open and write to temp file (e.g. bt_config.conf.new).
94 // 2) Flush the stream buffer to the temp file.
95 // 3) Sync the temp file to disk with fsync().
96 // 4) Rename temp file to actual config file (e.g. bt_config.conf).
97 // This ensures atomic update.
98 // 5) Sync directory that has the conf file with fsync().
99 // This ensures directory entries are up-to-date.
100 //
101 // We are using traditional C type file methods because C++ std::filesystem and std::ofstream do
102 // not support:
103 // - Operation on directories
104 // - fsync() to ensure content is written to disk
105
106 // Build temp config file based on config file (e.g. bt_config.conf.new).
107 const std::string temp_path = path + ".new";
108
109 // Extract directory from file path (e.g. /data/misc/bluedroid).
110 // TODO: switch to std::filesystem::path::parent_path
111 std::string directory_path;
112 {
113 // Make a temporary variable as inputs to dirname() will be modified and return value points to
114 // input char array temp_path_for_dir must not be destroyed until results from dirname is
115 // appended to directory_path
116 std::string temp_path_for_dir(path);
117 directory_path.append(dirname(temp_path_for_dir.data()));
118 }
119 if (directory_path.empty()) {
120 log::error("error extracting directory from '{}', error: {}", path, strerror(errno));
121 return false;
122 }
123
124 int dir_fd = open(directory_path.c_str(), O_RDONLY | O_DIRECTORY);
125 if (dir_fd < 0) {
126 log::error("unable to open dir '{}', error: {}", directory_path, strerror(errno));
127 return false;
128 }
129
130 FILE* fp = std::fopen(temp_path.c_str(), "wt");
131 if (!fp) {
132 log::error("unable to open file '{}', error: {}", temp_path, strerror(errno));
133 HandleError(temp_path, &dir_fd, &fp);
134 return false;
135 }
136
137 if (std::fprintf(fp, "%s", data.c_str()) < 0) {
138 log::error("unable to write to file '{}', error: {}", temp_path, strerror(errno));
139 HandleError(temp_path, &dir_fd, &fp);
140 return false;
141 }
142
143 // Flush the stream buffer to the temp file.
144 if (std::fflush(fp) != 0) {
145 log::error("unable to flush buffer to file '{}', error: {}", temp_path, strerror(errno));
146 HandleError(temp_path, &dir_fd, &fp);
147 return false;
148 }
149
150 // Sync written temp file out to disk. fsync() is blocking until data makes it
151 // to disk.
152 if (fsync(fileno(fp)) != 0) {
153 log::warn("unable to fsync file '{}', error: {}", temp_path, strerror(errno));
154 // Allow fsync to fail and continue
155 }
156
157 if (std::fclose(fp) != 0) {
158 log::error("unable to close file '{}', error: {}", temp_path, strerror(errno));
159 HandleError(temp_path, &dir_fd, &fp);
160 return false;
161 }
162 fp = nullptr;
163
164 // Change the file's permissions to Read/Write by User and Group
165 if (chmod(temp_path.c_str(), S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP) != 0) {
166 log::error("unable to change file permissions '{}', error: {}", temp_path, strerror(errno));
167
168 struct stat dirstat {};
169 if (fstat(dir_fd, &dirstat) == 0) {
170 log::error("dir st_mode = 0x{:02x}", dirstat.st_mode);
171 log::error("dir uid = {}", dirstat.st_uid);
172 log::error("dir gid = {}", dirstat.st_gid);
173 } else {
174 log::error("unable to call fstat on the directory, error: {}", strerror(errno));
175 }
176
177 struct stat filestat {};
178 if (stat(temp_path.c_str(), &filestat) == 0) {
179 log::error("file st_mode = 0x{:02x}", filestat.st_mode);
180 log::error("file uid = {}", filestat.st_uid);
181 log::error("file gid = {}", filestat.st_gid);
182 } else {
183 log::error("unable to call stat, error: {}", strerror(errno));
184 }
185
186 HandleError(temp_path, &dir_fd, &fp);
187 return false;
188 }
189
190 // Rename written temp file to the actual config file.
191 if (std::rename(temp_path.c_str(), path.c_str()) != 0) {
192 log::error("unable to commit file from '{}' to '{}', error: {}", temp_path, path,
193 strerror(errno));
194 HandleError(temp_path, &dir_fd, &fp);
195 return false;
196 }
197
198 // This should ensure the directory is updated as well.
199 if (fsync(dir_fd) != 0) {
200 log::warn("unable to fsync dir '{}', error: {}", directory_path, strerror(errno));
201 }
202
203 if (close(dir_fd) != 0) {
204 log::error("unable to close dir '{}', error: {}", directory_path, strerror(errno));
205 HandleError(temp_path, &dir_fd, &fp);
206 return false;
207 }
208 return true;
209 }
210
RemoveFile(const std::string & path)211 bool RemoveFile(const std::string& path) {
212 if (remove(path.c_str()) != 0) {
213 log::error("unable to remove file '{}', error: {}", path, strerror(errno));
214 return false;
215 }
216 return true;
217 }
218
219 std::optional<std::chrono::time_point<std::chrono::system_clock, std::chrono::nanoseconds>>
FileCreatedTime(const std::string & path)220 FileCreatedTime(const std::string& path) {
221 struct stat file_info;
222 if (stat(path.c_str(), &file_info) != 0) {
223 log::error("unable to read '{}' file metadata, error: {}", path, strerror(errno));
224 return std::nullopt;
225 }
226 using namespace std::chrono;
227 using namespace std::chrono_literals;
228 auto created_ts = file_info.st_ctim;
229 auto d = seconds{created_ts.tv_sec} + nanoseconds{created_ts.tv_nsec};
230 return time_point<system_clock>(duration_cast<system_clock::duration>(d));
231 }
232
233 } // namespace os
234 } // namespace bluetooth
235