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 | |
| 4 | #include <string> |
James Kuszmaul | 3ae4226 | 2019-11-08 12:33:41 -0800 | [diff] [blame] | 5 | #include <string_view> |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 6 | |
Brian Silverman | a9f2ec9 | 2020-10-06 18:00:53 -0700 | [diff] [blame^] | 7 | #include "glog/logging.h" |
| 8 | |
Brian Silverman | 61175fb | 2016-03-13 15:35:56 -0400 | [diff] [blame] | 9 | namespace aos { |
| 10 | namespace util { |
| 11 | |
| 12 | // Returns the complete contents of filename. LOG(FATAL)s if any errors are |
| 13 | // encountered. |
James Kuszmaul | 3ae4226 | 2019-11-08 12:33:41 -0800 | [diff] [blame] | 14 | ::std::string ReadFileToStringOrDie(const std::string_view filename); |
Brian Silverman | 61175fb | 2016-03-13 15:35:56 -0400 | [diff] [blame] | 15 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 16 | // 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] | 17 | void WriteStringToFileOrDie(const std::string_view filename, |
| 18 | const std::string_view contents); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 19 | |
Brian Silverman | a9f2ec9 | 2020-10-06 18:00:53 -0700 | [diff] [blame^] | 20 | // Returns true if it succeeds or false if the filesystem is full. |
| 21 | bool MkdirPIfSpace(std::string_view path, mode_t mode); |
| 22 | |
| 23 | inline void MkdirP(std::string_view path, mode_t mode) { |
| 24 | CHECK(MkdirPIfSpace(path, mode)); |
| 25 | } |
Austin Schuh | fccb2d0 | 2020-01-26 16:11:19 -0800 | [diff] [blame] | 26 | |
James Kuszmaul | f817809 | 2020-05-10 18:46:45 -0700 | [diff] [blame] | 27 | bool PathExists(std::string_view path); |
| 28 | |
Brian Silverman | 61175fb | 2016-03-13 15:35:56 -0400 | [diff] [blame] | 29 | } // namespace util |
| 30 | } // namespace aos |
| 31 | |
John Park | 33858a3 | 2018-09-28 23:05:48 -0700 | [diff] [blame] | 32 | #endif // AOS_UTIL_FILE_H_ |