blob: 8089225f2494bbfba832743b2c4ec9860ca418bf [file] [log] [blame]
John Park33858a32018-09-28 23:05:48 -07001#ifndef AOS_UTIL_FILE_H_
2#define AOS_UTIL_FILE_H_
Brian Silverman61175fb2016-03-13 15:35:56 -04003
Austin Schuhe3fc0532021-02-07 22:14:22 -08004#include <sys/stat.h>
Brian Silverman61175fb2016-03-13 15:35:56 -04005#include <string>
James Kuszmaul3ae42262019-11-08 12:33:41 -08006#include <string_view>
Austin Schuhcb108412019-10-13 16:09:54 -07007
Brian Silvermana9f2ec92020-10-06 18:00:53 -07008#include "glog/logging.h"
9
Brian Silverman61175fb2016-03-13 15:35:56 -040010namespace aos {
11namespace util {
12
13// Returns the complete contents of filename. LOG(FATAL)s if any errors are
14// encountered.
James Kuszmaul3ae42262019-11-08 12:33:41 -080015::std::string ReadFileToStringOrDie(const std::string_view filename);
Brian Silverman61175fb2016-03-13 15:35:56 -040016
Alex Perrycb7da4b2019-08-28 19:35:56 -070017// Creates filename if it doesn't exist and sets the contents to contents.
James Kuszmaul3ae42262019-11-08 12:33:41 -080018void WriteStringToFileOrDie(const std::string_view filename,
Austin Schuhe3fc0532021-02-07 22:14:22 -080019 const std::string_view contents,
20 mode_t permissions = S_IRWXU);
Alex Perrycb7da4b2019-08-28 19:35:56 -070021
Brian Silvermana9f2ec92020-10-06 18:00:53 -070022// Returns true if it succeeds or false if the filesystem is full.
23bool MkdirPIfSpace(std::string_view path, mode_t mode);
24
25inline void MkdirP(std::string_view path, mode_t mode) {
26 CHECK(MkdirPIfSpace(path, mode));
27}
Austin Schuhfccb2d02020-01-26 16:11:19 -080028
James Kuszmaulf8178092020-05-10 18:46:45 -070029bool PathExists(std::string_view path);
30
Austin Schuhe991fe22020-11-18 16:53:39 -080031// Recursively removes everything in the provided path. Ignores any errors it
32// runs across.
33void UnlinkRecursive(std::string_view path);
34
Brian Silverman61175fb2016-03-13 15:35:56 -040035} // namespace util
36} // namespace aos
37
John Park33858a32018-09-28 23:05:48 -070038#endif // AOS_UTIL_FILE_H_