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 | |
Austin Schuh | e3fc053 | 2021-02-07 22:14:22 -0800 | [diff] [blame] | 4 | #include <sys/stat.h> |
davidjevans | 8b9b52f | 2021-09-17 08:57:30 -0700 | [diff] [blame] | 5 | |
| 6 | #include <memory> |
Brian Silverman | 61175fb | 2016-03-13 15:35:56 -0400 | [diff] [blame] | 7 | #include <string> |
James Kuszmaul | 3ae4226 | 2019-11-08 12:33:41 -0800 | [diff] [blame] | 8 | #include <string_view> |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 9 | |
davidjevans | 8b9b52f | 2021-09-17 08:57:30 -0700 | [diff] [blame] | 10 | #include "absl/types/span.h" |
Brian Silverman | a9f2ec9 | 2020-10-06 18:00:53 -0700 | [diff] [blame] | 11 | #include "glog/logging.h" |
| 12 | |
Brian Silverman | 61175fb | 2016-03-13 15:35:56 -0400 | [diff] [blame] | 13 | namespace aos { |
| 14 | namespace util { |
| 15 | |
| 16 | // Returns the complete contents of filename. LOG(FATAL)s if any errors are |
| 17 | // encountered. |
James Kuszmaul | 3ae4226 | 2019-11-08 12:33:41 -0800 | [diff] [blame] | 18 | ::std::string ReadFileToStringOrDie(const std::string_view filename); |
Brian Silverman | 61175fb | 2016-03-13 15:35:56 -0400 | [diff] [blame] | 19 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 20 | // 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] | 21 | void WriteStringToFileOrDie(const std::string_view filename, |
Austin Schuh | e3fc053 | 2021-02-07 22:14:22 -0800 | [diff] [blame] | 22 | const std::string_view contents, |
| 23 | mode_t permissions = S_IRWXU); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 24 | |
Brian Silverman | a9f2ec9 | 2020-10-06 18:00:53 -0700 | [diff] [blame] | 25 | // Returns true if it succeeds or false if the filesystem is full. |
| 26 | bool MkdirPIfSpace(std::string_view path, mode_t mode); |
| 27 | |
| 28 | inline void MkdirP(std::string_view path, mode_t mode) { |
| 29 | CHECK(MkdirPIfSpace(path, mode)); |
| 30 | } |
Austin Schuh | fccb2d0 | 2020-01-26 16:11:19 -0800 | [diff] [blame] | 31 | |
James Kuszmaul | f817809 | 2020-05-10 18:46:45 -0700 | [diff] [blame] | 32 | bool PathExists(std::string_view path); |
| 33 | |
Austin Schuh | e991fe2 | 2020-11-18 16:53:39 -0800 | [diff] [blame] | 34 | // Recursively removes everything in the provided path. Ignores any errors it |
| 35 | // runs across. |
| 36 | void UnlinkRecursive(std::string_view path); |
| 37 | |
Austin Schuh | e4d1a68 | 2021-10-01 15:04:50 -0700 | [diff] [blame^] | 38 | enum class FileOptions { kReadable, kWriteable }; |
| 39 | |
davidjevans | 8b9b52f | 2021-09-17 08:57:30 -0700 | [diff] [blame] | 40 | // Maps file from disk into memory |
Austin Schuh | e4d1a68 | 2021-10-01 15:04:50 -0700 | [diff] [blame^] | 41 | std::shared_ptr<absl::Span<uint8_t>> MMapFile( |
| 42 | const std::string &path, FileOptions options = FileOptions::kReadable); |
davidjevans | 8b9b52f | 2021-09-17 08:57:30 -0700 | [diff] [blame] | 43 | |
Brian Silverman | 61175fb | 2016-03-13 15:35:56 -0400 | [diff] [blame] | 44 | } // namespace util |
| 45 | } // namespace aos |
| 46 | |
John Park | 33858a3 | 2018-09-28 23:05:48 -0700 | [diff] [blame] | 47 | #endif // AOS_UTIL_FILE_H_ |