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> |
Brian Silverman | 61175fb | 2016-03-13 15:35:56 -0400 | [diff] [blame] | 9 | #include <string> |
James Kuszmaul | 3ae4226 | 2019-11-08 12:33:41 -0800 | [diff] [blame] | 10 | #include <string_view> |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 11 | |
James Kuszmaul | 0625b0d | 2022-09-21 11:38:48 -0700 | [diff] [blame^] | 12 | #include "absl/strings/numbers.h" |
davidjevans | 8b9b52f | 2021-09-17 08:57:30 -0700 | [diff] [blame] | 13 | #include "absl/types/span.h" |
James Kuszmaul | 0625b0d | 2022-09-21 11:38:48 -0700 | [diff] [blame^] | 14 | #include "aos/scoped/scoped_fd.h" |
Brian Silverman | a9f2ec9 | 2020-10-06 18:00:53 -0700 | [diff] [blame] | 15 | #include "glog/logging.h" |
| 16 | |
Brian Silverman | 61175fb | 2016-03-13 15:35:56 -0400 | [diff] [blame] | 17 | namespace aos { |
| 18 | namespace util { |
| 19 | |
| 20 | // Returns the complete contents of filename. LOG(FATAL)s if any errors are |
| 21 | // encountered. |
James Kuszmaul | 3ae4226 | 2019-11-08 12:33:41 -0800 | [diff] [blame] | 22 | ::std::string ReadFileToStringOrDie(const std::string_view filename); |
Brian Silverman | 61175fb | 2016-03-13 15:35:56 -0400 | [diff] [blame] | 23 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 24 | // 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] | 25 | void WriteStringToFileOrDie(const std::string_view filename, |
Austin Schuh | e3fc053 | 2021-02-07 22:14:22 -0800 | [diff] [blame] | 26 | const std::string_view contents, |
| 27 | mode_t permissions = S_IRWXU); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 28 | |
Brian Silverman | a9f2ec9 | 2020-10-06 18:00:53 -0700 | [diff] [blame] | 29 | // Returns true if it succeeds or false if the filesystem is full. |
| 30 | bool MkdirPIfSpace(std::string_view path, mode_t mode); |
| 31 | |
| 32 | inline void MkdirP(std::string_view path, mode_t mode) { |
| 33 | CHECK(MkdirPIfSpace(path, mode)); |
| 34 | } |
Austin Schuh | fccb2d0 | 2020-01-26 16:11:19 -0800 | [diff] [blame] | 35 | |
James Kuszmaul | f817809 | 2020-05-10 18:46:45 -0700 | [diff] [blame] | 36 | bool PathExists(std::string_view path); |
| 37 | |
Austin Schuh | e991fe2 | 2020-11-18 16:53:39 -0800 | [diff] [blame] | 38 | // Recursively removes everything in the provided path. Ignores any errors it |
| 39 | // runs across. |
| 40 | void UnlinkRecursive(std::string_view path); |
| 41 | |
Austin Schuh | e4d1a68 | 2021-10-01 15:04:50 -0700 | [diff] [blame] | 42 | enum class FileOptions { kReadable, kWriteable }; |
| 43 | |
davidjevans | 8b9b52f | 2021-09-17 08:57:30 -0700 | [diff] [blame] | 44 | // Maps file from disk into memory |
Austin Schuh | e4d1a68 | 2021-10-01 15:04:50 -0700 | [diff] [blame] | 45 | std::shared_ptr<absl::Span<uint8_t>> MMapFile( |
| 46 | const std::string &path, FileOptions options = FileOptions::kReadable); |
davidjevans | 8b9b52f | 2021-09-17 08:57:30 -0700 | [diff] [blame] | 47 | |
James Kuszmaul | 0625b0d | 2022-09-21 11:38:48 -0700 | [diff] [blame^] | 48 | // Wrapper to handle reading the contents of a file into a buffer. Meant for |
| 49 | // situations where the malloc'ing of ReadFileToStringOrDie is inappropriate, |
| 50 | // but where you still want to read a file. |
| 51 | template <int kBufferSize = 1024> |
| 52 | class FileReader { |
| 53 | public: |
| 54 | FileReader(std::string_view filename) |
| 55 | : file_(open(::std::string(filename).c_str(), O_RDONLY)) { |
| 56 | PCHECK(file_.get() != -1) << ": opening " << filename; |
| 57 | memset(buffer_, 0, kBufferSize); |
| 58 | } |
| 59 | // Reads the entire contents of the file into the internal buffer and returns |
| 60 | // a string_view of it. |
| 61 | // Note: The result may not be null-terminated. |
| 62 | std::string_view ReadContents() { |
| 63 | PCHECK(0 == lseek(file_.get(), 0, SEEK_SET)); |
| 64 | const ssize_t result = read(file_.get(), buffer_, sizeof(buffer_)); |
| 65 | PCHECK(result >= 0); |
| 66 | return {buffer_, static_cast<size_t>(result)}; |
| 67 | } |
| 68 | // Calls ReadContents() and attempts to convert the result into an integer, or |
| 69 | // dies trying. |
| 70 | int ReadInt() { |
| 71 | int result; |
| 72 | std::string_view contents = ReadContents(); |
| 73 | CHECK(absl::SimpleAtoi(contents, &result)) |
| 74 | << "Failed to parse \"" << contents << "\" as int."; |
| 75 | return result; |
| 76 | } |
| 77 | |
| 78 | private: |
| 79 | aos::ScopedFD file_; |
| 80 | char buffer_[kBufferSize]; |
| 81 | }; |
| 82 | |
Brian Silverman | 61175fb | 2016-03-13 15:35:56 -0400 | [diff] [blame] | 83 | } // namespace util |
| 84 | } // namespace aos |
| 85 | |
John Park | 33858a3 | 2018-09-28 23:05:48 -0700 | [diff] [blame] | 86 | #endif // AOS_UTIL_FILE_H_ |