John Park | 33858a3 | 2018-09-28 23:05:48 -0700 | [diff] [blame] | 1 | #include "aos/util/file.h" |
Brian Silverman | 61175fb | 2016-03-13 15:35:56 -0400 | [diff] [blame] | 2 | |
Stephan Pleines | b117767 | 2024-05-27 17:48:32 -0700 | [diff] [blame] | 3 | #include <errno.h> |
Brian Silverman | 61175fb | 2016-03-13 15:35:56 -0400 | [diff] [blame] | 4 | #include <fcntl.h> |
Austin Schuh | e991fe2 | 2020-11-18 16:53:39 -0800 | [diff] [blame] | 5 | #include <fts.h> |
Stephan Pleines | b117767 | 2024-05-27 17:48:32 -0700 | [diff] [blame] | 6 | #include <stdio.h> |
| 7 | #include <string.h> |
davidjevans | 8b9b52f | 2021-09-17 08:57:30 -0700 | [diff] [blame] | 8 | #include <sys/mman.h> |
Austin Schuh | fccb2d0 | 2020-01-26 16:11:19 -0800 | [diff] [blame] | 9 | #include <sys/stat.h> |
| 10 | #include <sys/types.h> |
Brian Silverman | 61175fb | 2016-03-13 15:35:56 -0400 | [diff] [blame] | 11 | #include <unistd.h> |
| 12 | |
Stephan Pleines | b117767 | 2024-05-27 17:48:32 -0700 | [diff] [blame] | 13 | #include <algorithm> |
| 14 | #include <iterator> |
payton.rehl | f8cb393 | 2023-06-13 11:20:44 -0700 | [diff] [blame] | 15 | #include <optional> |
Stephan Pleines | b117767 | 2024-05-27 17:48:32 -0700 | [diff] [blame] | 16 | #include <ostream> |
James Kuszmaul | 3ae4226 | 2019-11-08 12:33:41 -0800 | [diff] [blame] | 17 | #include <string_view> |
Austin Schuh | e2df81b | 2021-08-16 10:52:27 -0700 | [diff] [blame] | 18 | #if __has_feature(memory_sanitizer) |
| 19 | #include <sanitizer/msan_interface.h> |
| 20 | #endif |
James Kuszmaul | 3ae4226 | 2019-11-08 12:33:41 -0800 | [diff] [blame] | 21 | |
Stephan Pleines | b117767 | 2024-05-27 17:48:32 -0700 | [diff] [blame] | 22 | #include "flatbuffers/util.h" |
| 23 | |
John Park | 33858a3 | 2018-09-28 23:05:48 -0700 | [diff] [blame] | 24 | #include "aos/scoped/scoped_fd.h" |
Brian Silverman | 61175fb | 2016-03-13 15:35:56 -0400 | [diff] [blame] | 25 | |
Stephan Pleines | f63bde8 | 2024-01-13 15:59:33 -0800 | [diff] [blame] | 26 | namespace aos::util { |
Brian Silverman | 61175fb | 2016-03-13 15:35:56 -0400 | [diff] [blame] | 27 | |
payton.rehl | f8cb393 | 2023-06-13 11:20:44 -0700 | [diff] [blame] | 28 | std::string ReadFileToStringOrDie(const std::string_view filename) { |
| 29 | std::optional<std::string> r = MaybeReadFileToString(filename); |
| 30 | PCHECK(r.has_value()) << "Failed to read " << filename << " to string"; |
| 31 | return r.value(); |
| 32 | } |
| 33 | |
| 34 | std::optional<std::string> MaybeReadFileToString( |
| 35 | const std::string_view filename) { |
| 36 | std::string r; |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 37 | ScopedFD fd(open(::std::string(filename).c_str(), O_RDONLY)); |
payton.rehl | f8cb393 | 2023-06-13 11:20:44 -0700 | [diff] [blame] | 38 | if (fd.get() == -1) { |
| 39 | PLOG(ERROR) << "Failed to open " << filename; |
| 40 | return std::nullopt; |
| 41 | } |
Brian Silverman | 61175fb | 2016-03-13 15:35:56 -0400 | [diff] [blame] | 42 | while (true) { |
| 43 | char buffer[1024]; |
| 44 | const ssize_t result = read(fd.get(), buffer, sizeof(buffer)); |
payton.rehl | f8cb393 | 2023-06-13 11:20:44 -0700 | [diff] [blame] | 45 | if (result < 0) { |
| 46 | PLOG(ERROR) << "Failed to read from " << filename; |
| 47 | return std::nullopt; |
| 48 | } |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 49 | if (result == 0) { |
Brian Silverman | 61175fb | 2016-03-13 15:35:56 -0400 | [diff] [blame] | 50 | break; |
| 51 | } |
| 52 | r.append(buffer, result); |
| 53 | } |
| 54 | return r; |
| 55 | } |
| 56 | |
Adam Snaider | 96a0f4b | 2023-05-18 20:41:19 -0700 | [diff] [blame] | 57 | std::vector<uint8_t> ReadFileToVecOrDie(const std::string_view filename) { |
| 58 | std::vector<uint8_t> r; |
| 59 | ScopedFD fd(open(::std::string(filename).c_str(), O_RDONLY)); |
| 60 | PCHECK(fd.get() != -1) << ": opening " << filename; |
| 61 | while (true) { |
| 62 | uint8_t buffer[1024]; |
| 63 | const ssize_t result = read(fd.get(), buffer, sizeof(buffer)); |
| 64 | PCHECK(result >= 0) << ": reading from " << filename; |
| 65 | if (result == 0) { |
| 66 | break; |
| 67 | } |
| 68 | std::copy(buffer, buffer + result, std::back_inserter(r)); |
| 69 | } |
| 70 | return r; |
| 71 | } |
| 72 | |
James Kuszmaul | 3ae4226 | 2019-11-08 12:33:41 -0800 | [diff] [blame] | 73 | void WriteStringToFileOrDie(const std::string_view filename, |
Austin Schuh | e3fc053 | 2021-02-07 22:14:22 -0800 | [diff] [blame] | 74 | const std::string_view contents, |
| 75 | mode_t permissions) { |
James Kuszmaul | fd43f4e | 2022-12-16 15:19:35 -0800 | [diff] [blame] | 76 | FileWriter writer(filename, permissions); |
| 77 | writer.WriteBytesOrDie( |
| 78 | {reinterpret_cast<const uint8_t *>(contents.data()), contents.size()}); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 79 | } |
| 80 | |
Brian Silverman | a9f2ec9 | 2020-10-06 18:00:53 -0700 | [diff] [blame] | 81 | bool MkdirPIfSpace(std::string_view path, mode_t mode) { |
Austin Schuh | fccb2d0 | 2020-01-26 16:11:19 -0800 | [diff] [blame] | 82 | auto last_slash_pos = path.find_last_of("/"); |
| 83 | |
| 84 | std::string folder(last_slash_pos == std::string_view::npos |
| 85 | ? std::string_view("") |
| 86 | : path.substr(0, last_slash_pos)); |
Brian Silverman | a9f2ec9 | 2020-10-06 18:00:53 -0700 | [diff] [blame] | 87 | if (folder.empty()) { |
| 88 | return true; |
| 89 | } |
| 90 | if (!MkdirPIfSpace(folder, mode)) { |
| 91 | return false; |
| 92 | } |
Austin Schuh | fccb2d0 | 2020-01-26 16:11:19 -0800 | [diff] [blame] | 93 | const int result = mkdir(folder.c_str(), mode); |
| 94 | if (result == -1 && errno == EEXIST) { |
| 95 | VLOG(2) << folder << " already exists"; |
Brian Silverman | a9f2ec9 | 2020-10-06 18:00:53 -0700 | [diff] [blame] | 96 | return true; |
| 97 | } else if (result == -1 && errno == ENOSPC) { |
| 98 | VLOG(2) << "Out of space"; |
| 99 | return false; |
Austin Schuh | fccb2d0 | 2020-01-26 16:11:19 -0800 | [diff] [blame] | 100 | } else { |
| 101 | VLOG(1) << "Created " << folder; |
| 102 | } |
| 103 | PCHECK(result == 0) << ": Error creating " << folder; |
Brian Silverman | a9f2ec9 | 2020-10-06 18:00:53 -0700 | [diff] [blame] | 104 | return true; |
Austin Schuh | fccb2d0 | 2020-01-26 16:11:19 -0800 | [diff] [blame] | 105 | } |
| 106 | |
James Kuszmaul | f817809 | 2020-05-10 18:46:45 -0700 | [diff] [blame] | 107 | bool PathExists(std::string_view path) { |
| 108 | struct stat buffer; |
| 109 | return stat(path.data(), &buffer) == 0; |
| 110 | } |
| 111 | |
Austin Schuh | e991fe2 | 2020-11-18 16:53:39 -0800 | [diff] [blame] | 112 | void UnlinkRecursive(std::string_view path) { |
| 113 | FTS *ftsp = NULL; |
| 114 | FTSENT *curr; |
| 115 | |
| 116 | // Cast needed (in C) because fts_open() takes a "char * const *", instead |
| 117 | // of a "const char * const *", which is only allowed in C++. fts_open() |
| 118 | // does not modify the argument. |
| 119 | std::string p(path); |
| 120 | char *files[] = {const_cast<char *>(p.c_str()), NULL}; |
| 121 | |
| 122 | // FTS_NOCHDIR - Avoid changing cwd, which could cause unexpected behavior |
| 123 | // in multithreaded programs |
| 124 | // FTS_PHYSICAL - Don't follow symlinks. Prevents deletion of files outside |
| 125 | // of the specified directory |
| 126 | // FTS_XDEV - Don't cross filesystem boundaries |
| 127 | ftsp = fts_open(files, FTS_NOCHDIR | FTS_PHYSICAL | FTS_XDEV, NULL); |
| 128 | if (!ftsp) { |
| 129 | return; |
| 130 | } |
| 131 | |
| 132 | while ((curr = fts_read(ftsp))) { |
Austin Schuh | e2df81b | 2021-08-16 10:52:27 -0700 | [diff] [blame] | 133 | #if __has_feature(memory_sanitizer) |
| 134 | // fts_read doesn't have propper msan interceptors. Unpoison it ourselves. |
| 135 | if (curr) { |
| 136 | __msan_unpoison(curr, sizeof(*curr)); |
| 137 | __msan_unpoison_string(curr->fts_accpath); |
| 138 | __msan_unpoison_string(curr->fts_path); |
| 139 | __msan_unpoison_string(curr->fts_name); |
| 140 | } |
| 141 | #endif |
Austin Schuh | e991fe2 | 2020-11-18 16:53:39 -0800 | [diff] [blame] | 142 | switch (curr->fts_info) { |
| 143 | case FTS_NS: |
| 144 | case FTS_DNR: |
| 145 | case FTS_ERR: |
| 146 | LOG(WARNING) << "Can't read " << curr->fts_accpath; |
| 147 | break; |
| 148 | |
| 149 | case FTS_DC: |
| 150 | case FTS_DOT: |
| 151 | case FTS_NSOK: |
| 152 | // Not reached unless FTS_LOGICAL, FTS_SEEDOT, or FTS_NOSTAT were |
| 153 | // passed to fts_open() |
| 154 | break; |
| 155 | |
| 156 | case FTS_D: |
| 157 | // Do nothing. Need depth-first search, so directories are deleted |
| 158 | // in FTS_DP |
| 159 | break; |
| 160 | |
| 161 | case FTS_DP: |
| 162 | case FTS_F: |
| 163 | case FTS_SL: |
| 164 | case FTS_SLNONE: |
| 165 | case FTS_DEFAULT: |
| 166 | VLOG(1) << "Removing " << curr->fts_path; |
| 167 | if (remove(curr->fts_accpath) < 0) { |
| 168 | LOG(WARNING) << curr->fts_path |
| 169 | << ": Failed to remove: " << strerror(curr->fts_errno); |
| 170 | } |
| 171 | break; |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | if (ftsp) { |
| 176 | fts_close(ftsp); |
| 177 | } |
| 178 | } |
| 179 | |
Austin Schuh | e4d1a68 | 2021-10-01 15:04:50 -0700 | [diff] [blame] | 180 | std::shared_ptr<absl::Span<uint8_t>> MMapFile(const std::string &path, |
| 181 | FileOptions options) { |
| 182 | int fd = |
| 183 | open(path.c_str(), options == FileOptions::kReadable ? O_RDONLY : O_RDWR); |
davidjevans | 8b9b52f | 2021-09-17 08:57:30 -0700 | [diff] [blame] | 184 | PCHECK(fd != -1) << "Unable to open file " << path; |
| 185 | struct stat sb; |
| 186 | PCHECK(fstat(fd, &sb) != -1) << ": Unable to get file size of " << path; |
Austin Schuh | e4d1a68 | 2021-10-01 15:04:50 -0700 | [diff] [blame] | 187 | uint8_t *start = reinterpret_cast<uint8_t *>(mmap( |
| 188 | NULL, sb.st_size, |
| 189 | options == FileOptions::kReadable ? PROT_READ : (PROT_READ | PROT_WRITE), |
| 190 | MAP_SHARED, fd, 0)); |
davidjevans | 8b9b52f | 2021-09-17 08:57:30 -0700 | [diff] [blame] | 191 | CHECK(start != MAP_FAILED) << ": Unable to open mapping to file " << path; |
| 192 | std::shared_ptr<absl::Span<uint8_t>> span = |
| 193 | std::shared_ptr<absl::Span<uint8_t>>( |
| 194 | new absl::Span<uint8_t>(start, sb.st_size), |
| 195 | [](absl::Span<uint8_t> *span) { |
Austin Schuh | e4d1a68 | 2021-10-01 15:04:50 -0700 | [diff] [blame] | 196 | PCHECK(msync(span->data(), span->size(), MS_SYNC) == 0) |
| 197 | << ": Failed to flush data before unmapping."; |
davidjevans | 8b9b52f | 2021-09-17 08:57:30 -0700 | [diff] [blame] | 198 | PCHECK(munmap(span->data(), span->size()) != -1); |
| 199 | delete span; |
| 200 | }); |
| 201 | close(fd); |
| 202 | return span; |
| 203 | } |
| 204 | |
Pallavi Madhukar | 1d39146 | 2024-05-13 18:46:48 -0700 | [diff] [blame^] | 205 | FileReader::FileReader(std::string_view filename, |
| 206 | FileReaderErrorType error_type) |
James Kuszmaul | 5a88d41 | 2023-01-27 15:55:55 -0800 | [diff] [blame] | 207 | : file_(open(::std::string(filename).c_str(), O_RDONLY)) { |
Pallavi Madhukar | 1d39146 | 2024-05-13 18:46:48 -0700 | [diff] [blame^] | 208 | if (!is_open()) { |
| 209 | PLOG_IF(FATAL, error_type == FileReaderErrorType::kFatal) |
| 210 | << ": opening " << filename; |
| 211 | PLOG(ERROR) << "opening " << filename; |
| 212 | } |
James Kuszmaul | 5a88d41 | 2023-01-27 15:55:55 -0800 | [diff] [blame] | 213 | } |
| 214 | |
Austin Schuh | 73ff641 | 2023-09-01 14:42:24 -0700 | [diff] [blame] | 215 | std::optional<absl::Span<char>> FileReader::ReadContents( |
| 216 | absl::Span<char> buffer) { |
James Kuszmaul | 5a88d41 | 2023-01-27 15:55:55 -0800 | [diff] [blame] | 217 | PCHECK(0 == lseek(file_.get(), 0, SEEK_SET)); |
| 218 | const ssize_t result = read(file_.get(), buffer.data(), buffer.size()); |
Austin Schuh | 73ff641 | 2023-09-01 14:42:24 -0700 | [diff] [blame] | 219 | if (result < 0) { |
| 220 | // Read timeout for an i2c request returns this. |
| 221 | if (errno == EREMOTEIO) { |
| 222 | return std::nullopt; |
| 223 | } |
| 224 | } |
| 225 | |
James Kuszmaul | 5a88d41 | 2023-01-27 15:55:55 -0800 | [diff] [blame] | 226 | PCHECK(result >= 0); |
Austin Schuh | 73ff641 | 2023-09-01 14:42:24 -0700 | [diff] [blame] | 227 | return absl::Span<char>{buffer.data(), static_cast<size_t>(result)}; |
James Kuszmaul | 5a88d41 | 2023-01-27 15:55:55 -0800 | [diff] [blame] | 228 | } |
| 229 | |
James Kuszmaul | fd43f4e | 2022-12-16 15:19:35 -0800 | [diff] [blame] | 230 | FileWriter::FileWriter(std::string_view filename, mode_t permissions) |
| 231 | : file_(open(::std::string(filename).c_str(), O_WRONLY | O_CREAT | O_TRUNC, |
| 232 | permissions)) { |
| 233 | PCHECK(file_.get() != -1) << ": opening " << filename; |
| 234 | } |
| 235 | |
James Kuszmaul | 5a88d41 | 2023-01-27 15:55:55 -0800 | [diff] [blame] | 236 | // absl::SimpleAtoi doesn't interpret a leading 0x as hex, which we need here. |
| 237 | // Instead, we use the flatbufers API, which unfortunately relies on NUL |
| 238 | // termination. |
Austin Schuh | 73ff641 | 2023-09-01 14:42:24 -0700 | [diff] [blame] | 239 | std::optional<int32_t> FileReader::ReadInt32() { |
James Kuszmaul | 5a88d41 | 2023-01-27 15:55:55 -0800 | [diff] [blame] | 240 | // Maximum characters for a 32-bit integer, +1 for the NUL. |
| 241 | // Hex is the same size with the leading 0x. |
| 242 | std::array<char, 11> buffer; |
| 243 | int32_t result; |
Austin Schuh | 73ff641 | 2023-09-01 14:42:24 -0700 | [diff] [blame] | 244 | const std::optional<absl::Span<char>> string_span = |
James Kuszmaul | 5a88d41 | 2023-01-27 15:55:55 -0800 | [diff] [blame] | 245 | ReadContents(absl::Span<char>(buffer.data(), buffer.size()) |
| 246 | .subspan(0, buffer.size() - 1)); |
Austin Schuh | 73ff641 | 2023-09-01 14:42:24 -0700 | [diff] [blame] | 247 | if (!string_span.has_value()) { |
| 248 | return std::nullopt; |
| 249 | } |
| 250 | |
James Kuszmaul | 5a88d41 | 2023-01-27 15:55:55 -0800 | [diff] [blame] | 251 | // Verify we found the newline. |
Austin Schuh | 73ff641 | 2023-09-01 14:42:24 -0700 | [diff] [blame] | 252 | CHECK_EQ(buffer[string_span->size() - 1], '\n'); |
James Kuszmaul | 5a88d41 | 2023-01-27 15:55:55 -0800 | [diff] [blame] | 253 | // Truncate the newline. |
Austin Schuh | 73ff641 | 2023-09-01 14:42:24 -0700 | [diff] [blame] | 254 | buffer[string_span->size() - 1] = '\0'; |
James Kuszmaul | 5a88d41 | 2023-01-27 15:55:55 -0800 | [diff] [blame] | 255 | CHECK(flatbuffers::StringToNumber(buffer.data(), &result)) |
| 256 | << ": Error parsing string to integer: " |
Austin Schuh | 73ff641 | 2023-09-01 14:42:24 -0700 | [diff] [blame] | 257 | << std::string_view(string_span->data(), string_span->size()); |
James Kuszmaul | 5a88d41 | 2023-01-27 15:55:55 -0800 | [diff] [blame] | 258 | |
| 259 | return result; |
| 260 | } |
| 261 | |
James Kuszmaul | fd43f4e | 2022-12-16 15:19:35 -0800 | [diff] [blame] | 262 | FileWriter::WriteResult FileWriter::WriteBytes( |
| 263 | absl::Span<const uint8_t> bytes) { |
| 264 | size_t size_written = 0; |
| 265 | while (size_written != bytes.size()) { |
| 266 | const ssize_t result = write(file_.get(), bytes.data() + size_written, |
| 267 | bytes.size() - size_written); |
| 268 | if (result < 0) { |
| 269 | return {size_written, static_cast<int>(result)}; |
| 270 | } |
| 271 | // Not really supposed to happen unless writing zero bytes without an error. |
| 272 | // See, e.g., |
| 273 | // https://stackoverflow.com/questions/2176443/is-a-return-value-of-0-from-write2-in-c-an-error |
| 274 | if (result == 0) { |
| 275 | return {size_written, static_cast<int>(result)}; |
| 276 | } |
| 277 | |
| 278 | size_written += result; |
| 279 | } |
| 280 | return {size_written, static_cast<int>(size_written)}; |
| 281 | } |
| 282 | |
| 283 | FileWriter::WriteResult FileWriter::WriteBytes(std::string_view bytes) { |
| 284 | return WriteBytes(absl::Span<const uint8_t>{ |
| 285 | reinterpret_cast<const uint8_t *>(bytes.data()), bytes.size()}); |
| 286 | } |
| 287 | |
| 288 | void FileWriter::WriteBytesOrDie(std::string_view bytes) { |
| 289 | WriteBytesOrDie(absl::Span<const uint8_t>{ |
| 290 | reinterpret_cast<const uint8_t *>(bytes.data()), bytes.size()}); |
| 291 | } |
| 292 | |
| 293 | void FileWriter::WriteBytesOrDie(absl::Span<const uint8_t> bytes) { |
| 294 | PCHECK(bytes.size() == WriteBytes(bytes).bytes_written) |
| 295 | << ": Failed to write " << bytes.size() << " bytes."; |
| 296 | } |
| 297 | |
Stephan Pleines | f63bde8 | 2024-01-13 15:59:33 -0800 | [diff] [blame] | 298 | } // namespace aos::util |