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 #include "os/log.h"
32
33 namespace {
34
HandleError(const std::string & temp_path,int * dir_fd,FILE ** fp)35 void HandleError(const std::string& temp_path, int* dir_fd, FILE** fp) {
36 // This indicates there is a write issue. Unlink as partial data is not
37 // acceptable.
38 unlink(temp_path.c_str());
39 if (*fp) {
40 fclose(*fp);
41 *fp = nullptr;
42 }
43 if (*dir_fd != -1) {
44 close(*dir_fd);
45 *dir_fd = -1;
46 }
47 }
48
49 } // namespace
50
51 namespace bluetooth {
52 namespace os {
53
FileExists(const std::string & path)54 bool FileExists(const std::string& path) {
55 std::ifstream input(path, std::ios::binary | std::ios::ate);
56 return input.good();
57 }
58
RenameFile(const std::string & from,const std::string & to)59 bool RenameFile(const std::string& from, const std::string& to) {
60 if (std::rename(from.c_str(), to.c_str()) != 0) {
61 log::error("unable to rename file from '{}' to '{}', error: {}", from, to, strerror(errno));
62 return false;
63 }
64 return true;
65 }
66
ReadSmallFile(const std::string & path)67 std::optional<std::string> ReadSmallFile(const std::string& path) {
68 std::ifstream input(path, std::ios::binary | std::ios::ate);
69 if (!input) {
70 log::warn("Failed to open file '{}', error: {}", path, strerror(errno));
71 return std::nullopt;
72 }
73 auto file_size = input.tellg();
74 if (file_size < 0) {
75 log::warn("Failed to get file size for '{}', error: {}", path, strerror(errno));
76 return std::nullopt;
77 }
78 std::string result(file_size, '\0');
79 if (!input.seekg(0)) {
80 log::warn("Failed to go back to the beginning of file '{}', error: {}", path, strerror(errno));
81 return std::nullopt;
82 }
83 if (!input.read(result.data(), result.size())) {
84 log::warn("Failed to read file '{}', error: {}", path, strerror(errno));
85 return std::nullopt;
86 }
87 input.close();
88 return result;
89 }
90
WriteToFile(const std::string & path,const std::string & data)91 bool WriteToFile(const std::string& path, const std::string& data) {
92 log::assert_that(!path.empty(), "assert failed: !path.empty()");
93 // Steps to ensure content of data gets to disk:
94 //
95 // 1) Open and write to temp file (e.g. bt_config.conf.new).
96 // 2) Flush the stream buffer to the temp file.
97 // 3) Sync the temp file to disk with fsync().
98 // 4) Rename temp file to actual config file (e.g. bt_config.conf).
99 // This ensures atomic update.
100 // 5) Sync directory that has the conf file with fsync().
101 // This ensures directory entries are up-to-date.
102 //
103 // We are using traditional C type file methods because C++ std::filesystem and std::ofstream do not support:
104 // - Operation on directories
105 // - fsync() to ensure content is written to disk
106
107 // Build temp config file based on config file (e.g. bt_config.conf.new).
108 const std::string temp_path = path + ".new";
109
110 // Extract directory from file path (e.g. /data/misc/bluedroid).
111 // TODO: switch to std::filesystem::path::parent_path
112 std::string directory_path;
113 {
114 // Make a temporary variable as inputs to dirname() will be modified and return value points to input char array
115 // temp_path_for_dir must not be destroyed until results from dirname is 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(
193 "unable to commit file from '{}' to '{}', error: {}", temp_path, path, 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
FileCreatedTime(const std::string & path)219 std::optional<std::chrono::time_point<std::chrono::system_clock, std::chrono::nanoseconds>> FileCreatedTime(
220 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