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