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