blob: 7c06ece72d86b1e09f62a1293753afc790b13a98 [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>
davidjevans8b9b52f2021-09-17 08:57:30 -07005
6#include <memory>
Brian Silverman61175fb2016-03-13 15:35:56 -04007#include <string>
James Kuszmaul3ae42262019-11-08 12:33:41 -08008#include <string_view>
Austin Schuhcb108412019-10-13 16:09:54 -07009
davidjevans8b9b52f2021-09-17 08:57:30 -070010#include "absl/types/span.h"
Brian Silvermana9f2ec92020-10-06 18:00:53 -070011#include "glog/logging.h"
12
Brian Silverman61175fb2016-03-13 15:35:56 -040013namespace aos {
14namespace util {
15
16// Returns the complete contents of filename. LOG(FATAL)s if any errors are
17// encountered.
James Kuszmaul3ae42262019-11-08 12:33:41 -080018::std::string ReadFileToStringOrDie(const std::string_view filename);
Brian Silverman61175fb2016-03-13 15:35:56 -040019
Alex Perrycb7da4b2019-08-28 19:35:56 -070020// Creates filename if it doesn't exist and sets the contents to contents.
James Kuszmaul3ae42262019-11-08 12:33:41 -080021void WriteStringToFileOrDie(const std::string_view filename,
Austin Schuhe3fc0532021-02-07 22:14:22 -080022 const std::string_view contents,
23 mode_t permissions = S_IRWXU);
Alex Perrycb7da4b2019-08-28 19:35:56 -070024
Brian Silvermana9f2ec92020-10-06 18:00:53 -070025// Returns true if it succeeds or false if the filesystem is full.
26bool MkdirPIfSpace(std::string_view path, mode_t mode);
27
28inline void MkdirP(std::string_view path, mode_t mode) {
29 CHECK(MkdirPIfSpace(path, mode));
30}
Austin Schuhfccb2d02020-01-26 16:11:19 -080031
James Kuszmaulf8178092020-05-10 18:46:45 -070032bool PathExists(std::string_view path);
33
Austin Schuhe991fe22020-11-18 16:53:39 -080034// Recursively removes everything in the provided path. Ignores any errors it
35// runs across.
36void UnlinkRecursive(std::string_view path);
37
davidjevans8b9b52f2021-09-17 08:57:30 -070038// Maps file from disk into memory
39std::shared_ptr<absl::Span<uint8_t>> MMapFile(const std::string &path);
40
Brian Silverman61175fb2016-03-13 15:35:56 -040041} // namespace util
42} // namespace aos
43
John Park33858a32018-09-28 23:05:48 -070044#endif // AOS_UTIL_FILE_H_