John Park | 33858a3 | 2018-09-28 23:05:48 -0700 | [diff] [blame] | 1 | #ifndef AOS_UTIL_FILE_H_ |
| 2 | #define AOS_UTIL_FILE_H_ |
Brian Silverman | 61175fb | 2016-03-13 15:35:56 -0400 | [diff] [blame] | 3 | |
James Kuszmaul | 0625b0d | 2022-09-21 11:38:48 -0700 | [diff] [blame] | 4 | #include <fcntl.h> |
Austin Schuh | e3fc053 | 2021-02-07 22:14:22 -0800 | [diff] [blame] | 5 | #include <sys/stat.h> |
James Kuszmaul | 0625b0d | 2022-09-21 11:38:48 -0700 | [diff] [blame] | 6 | #include <sys/types.h> |
davidjevans | 8b9b52f | 2021-09-17 08:57:30 -0700 | [diff] [blame] | 7 | |
| 8 | #include <memory> |
James Kuszmaul | 5a88d41 | 2023-01-27 15:55:55 -0800 | [diff] [blame^] | 9 | #include <optional> |
Brian Silverman | 61175fb | 2016-03-13 15:35:56 -0400 | [diff] [blame] | 10 | #include <string> |
James Kuszmaul | 3ae4226 | 2019-11-08 12:33:41 -0800 | [diff] [blame] | 11 | #include <string_view> |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 12 | |
James Kuszmaul | 0625b0d | 2022-09-21 11:38:48 -0700 | [diff] [blame] | 13 | #include "absl/strings/numbers.h" |
davidjevans | 8b9b52f | 2021-09-17 08:57:30 -0700 | [diff] [blame] | 14 | #include "absl/types/span.h" |
James Kuszmaul | 0625b0d | 2022-09-21 11:38:48 -0700 | [diff] [blame] | 15 | #include "aos/scoped/scoped_fd.h" |
James Kuszmaul | 5a88d41 | 2023-01-27 15:55:55 -0800 | [diff] [blame^] | 16 | #include "flatbuffers/util.h" |
Brian Silverman | a9f2ec9 | 2020-10-06 18:00:53 -0700 | [diff] [blame] | 17 | #include "glog/logging.h" |
| 18 | |
Brian Silverman | 61175fb | 2016-03-13 15:35:56 -0400 | [diff] [blame] | 19 | namespace aos { |
| 20 | namespace util { |
| 21 | |
| 22 | // Returns the complete contents of filename. LOG(FATAL)s if any errors are |
| 23 | // encountered. |
James Kuszmaul | 3ae4226 | 2019-11-08 12:33:41 -0800 | [diff] [blame] | 24 | ::std::string ReadFileToStringOrDie(const std::string_view filename); |
Brian Silverman | 61175fb | 2016-03-13 15:35:56 -0400 | [diff] [blame] | 25 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 26 | // Creates filename if it doesn't exist and sets the contents to contents. |
James Kuszmaul | 3ae4226 | 2019-11-08 12:33:41 -0800 | [diff] [blame] | 27 | void WriteStringToFileOrDie(const std::string_view filename, |
Austin Schuh | e3fc053 | 2021-02-07 22:14:22 -0800 | [diff] [blame] | 28 | const std::string_view contents, |
| 29 | mode_t permissions = S_IRWXU); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 30 | |
Brian Silverman | a9f2ec9 | 2020-10-06 18:00:53 -0700 | [diff] [blame] | 31 | // Returns true if it succeeds or false if the filesystem is full. |
| 32 | bool MkdirPIfSpace(std::string_view path, mode_t mode); |
| 33 | |
| 34 | inline void MkdirP(std::string_view path, mode_t mode) { |
| 35 | CHECK(MkdirPIfSpace(path, mode)); |
| 36 | } |
Austin Schuh | fccb2d0 | 2020-01-26 16:11:19 -0800 | [diff] [blame] | 37 | |
James Kuszmaul | f817809 | 2020-05-10 18:46:45 -0700 | [diff] [blame] | 38 | bool PathExists(std::string_view path); |
| 39 | |
Austin Schuh | e991fe2 | 2020-11-18 16:53:39 -0800 | [diff] [blame] | 40 | // Recursively removes everything in the provided path. Ignores any errors it |
| 41 | // runs across. |
| 42 | void UnlinkRecursive(std::string_view path); |
| 43 | |
Austin Schuh | e4d1a68 | 2021-10-01 15:04:50 -0700 | [diff] [blame] | 44 | enum class FileOptions { kReadable, kWriteable }; |
| 45 | |
davidjevans | 8b9b52f | 2021-09-17 08:57:30 -0700 | [diff] [blame] | 46 | // Maps file from disk into memory |
Austin Schuh | e4d1a68 | 2021-10-01 15:04:50 -0700 | [diff] [blame] | 47 | std::shared_ptr<absl::Span<uint8_t>> MMapFile( |
| 48 | const std::string &path, FileOptions options = FileOptions::kReadable); |
davidjevans | 8b9b52f | 2021-09-17 08:57:30 -0700 | [diff] [blame] | 49 | |
James Kuszmaul | 0625b0d | 2022-09-21 11:38:48 -0700 | [diff] [blame] | 50 | // Wrapper to handle reading the contents of a file into a buffer. Meant for |
| 51 | // situations where the malloc'ing of ReadFileToStringOrDie is inappropriate, |
| 52 | // but where you still want to read a file. |
James Kuszmaul | 0625b0d | 2022-09-21 11:38:48 -0700 | [diff] [blame] | 53 | class FileReader { |
| 54 | public: |
James Kuszmaul | 5a88d41 | 2023-01-27 15:55:55 -0800 | [diff] [blame^] | 55 | FileReader(std::string_view filename); |
James Kuszmaul | 0625b0d | 2022-09-21 11:38:48 -0700 | [diff] [blame] | 56 | // Reads the entire contents of the file into the internal buffer and returns |
| 57 | // a string_view of it. |
| 58 | // Note: The result may not be null-terminated. |
James Kuszmaul | 5a88d41 | 2023-01-27 15:55:55 -0800 | [diff] [blame^] | 59 | absl::Span<char> ReadContents(absl::Span<char> buffer); |
| 60 | // Returns the value of the file as a string, for a fixed-length file. |
| 61 | // Returns nullopt if the result is smaller than kSize. Ignores any |
| 62 | // bytes beyond kSize. |
| 63 | template <int kSize> |
| 64 | std::optional<std::array<char, kSize>> ReadString() { |
| 65 | std::array<char, kSize> result; |
| 66 | const absl::Span<char> used_span = |
| 67 | ReadContents(absl::Span<char>(result.data(), result.size())); |
| 68 | if (used_span.size() == kSize) { |
| 69 | return result; |
| 70 | } else { |
| 71 | return std::nullopt; |
| 72 | } |
James Kuszmaul | 0625b0d | 2022-09-21 11:38:48 -0700 | [diff] [blame] | 73 | } |
James Kuszmaul | 5a88d41 | 2023-01-27 15:55:55 -0800 | [diff] [blame^] | 74 | // Returns the value of the file as an integer. Crashes if it doesn't fit in a |
| 75 | // 32-bit integer. The value may start with 0x for a hex value, otherwise it |
| 76 | // must be base 10. |
| 77 | int32_t ReadInt32(); |
James Kuszmaul | 0625b0d | 2022-09-21 11:38:48 -0700 | [diff] [blame] | 78 | |
| 79 | private: |
| 80 | aos::ScopedFD file_; |
James Kuszmaul | 0625b0d | 2022-09-21 11:38:48 -0700 | [diff] [blame] | 81 | }; |
| 82 | |
James Kuszmaul | fd43f4e | 2022-12-16 15:19:35 -0800 | [diff] [blame] | 83 | // Simple interface to allow opening a file for writing and then writing it |
| 84 | // without any malloc's. |
James Kuszmaul | fd43f4e | 2022-12-16 15:19:35 -0800 | [diff] [blame] | 85 | class FileWriter { |
| 86 | public: |
| 87 | // The result of an individual call to WriteBytes(). |
| 88 | // Because WriteBytes() may repeatedly call write() when partial writes occur, |
| 89 | // it is possible for a non-zero number of bytes to have been written while |
| 90 | // still having an error because a late call to write() failed. |
| 91 | struct WriteResult { |
| 92 | // Total number of bytes successfully written to the file. |
| 93 | size_t bytes_written; |
| 94 | // If the write was successful (return_code > 0), equal to bytes_written. |
| 95 | // Otherwise, equal to the return value of the final call to write. |
| 96 | int return_code; |
| 97 | }; |
| 98 | |
| 99 | FileWriter(std::string_view filename, mode_t permissions = S_IRWXU); |
| 100 | |
| 101 | WriteResult WriteBytes(absl::Span<const uint8_t> bytes); |
| 102 | WriteResult WriteBytes(std::string_view bytes); |
| 103 | void WriteBytesOrDie(absl::Span<const uint8_t> bytes); |
| 104 | void WriteBytesOrDie(std::string_view bytes); |
| 105 | int fd() const { return file_.get(); } |
| 106 | |
| 107 | private: |
| 108 | aos::ScopedFD file_; |
| 109 | }; |
| 110 | |
Brian Silverman | 61175fb | 2016-03-13 15:35:56 -0400 | [diff] [blame] | 111 | } // namespace util |
| 112 | } // namespace aos |
| 113 | |
John Park | 33858a3 | 2018-09-28 23:05:48 -0700 | [diff] [blame] | 114 | #endif // AOS_UTIL_FILE_H_ |