blob: da5b29fddf1b598785e4fc0f118fa12277e1818b [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
James Kuszmaul0625b0d2022-09-21 11:38:48 -07004#include <fcntl.h>
Austin Schuhe3fc0532021-02-07 22:14:22 -08005#include <sys/stat.h>
James Kuszmaul0625b0d2022-09-21 11:38:48 -07006#include <sys/types.h>
davidjevans8b9b52f2021-09-17 08:57:30 -07007
8#include <memory>
James Kuszmaul5a88d412023-01-27 15:55:55 -08009#include <optional>
Brian Silverman61175fb2016-03-13 15:35:56 -040010#include <string>
James Kuszmaul3ae42262019-11-08 12:33:41 -080011#include <string_view>
Austin Schuhcb108412019-10-13 16:09:54 -070012
James Kuszmaul0625b0d2022-09-21 11:38:48 -070013#include "absl/strings/numbers.h"
davidjevans8b9b52f2021-09-17 08:57:30 -070014#include "absl/types/span.h"
James Kuszmaul5a88d412023-01-27 15:55:55 -080015#include "flatbuffers/util.h"
Brian Silvermana9f2ec92020-10-06 18:00:53 -070016#include "glog/logging.h"
17
Philipp Schrader790cb542023-07-05 21:06:52 -070018#include "aos/scoped/scoped_fd.h"
19
Brian Silverman61175fb2016-03-13 15:35:56 -040020namespace aos {
21namespace util {
22
23// Returns the complete contents of filename. LOG(FATAL)s if any errors are
24// encountered.
payton.rehlf8cb3932023-06-13 11:20:44 -070025std::string ReadFileToStringOrDie(const std::string_view filename);
26
27// Returns the complete contents of filename. Returns nullopt, but never dies
28// if any errors are encountered.
29std::optional<std::string> MaybeReadFileToString(
30 const std::string_view filename);
Brian Silverman61175fb2016-03-13 15:35:56 -040031
Adam Snaider96a0f4b2023-05-18 20:41:19 -070032// Returns the complete contents of filename as a byte vector. LOG(FATAL)s if
33// any errors are encountered.
34std::vector<uint8_t> ReadFileToVecOrDie(const std::string_view filename);
35
Alex Perrycb7da4b2019-08-28 19:35:56 -070036// Creates filename if it doesn't exist and sets the contents to contents.
James Kuszmaul3ae42262019-11-08 12:33:41 -080037void WriteStringToFileOrDie(const std::string_view filename,
Austin Schuhe3fc0532021-02-07 22:14:22 -080038 const std::string_view contents,
39 mode_t permissions = S_IRWXU);
Alex Perrycb7da4b2019-08-28 19:35:56 -070040
Brian Silvermana9f2ec92020-10-06 18:00:53 -070041// Returns true if it succeeds or false if the filesystem is full.
42bool MkdirPIfSpace(std::string_view path, mode_t mode);
43
44inline void MkdirP(std::string_view path, mode_t mode) {
45 CHECK(MkdirPIfSpace(path, mode));
46}
Austin Schuhfccb2d02020-01-26 16:11:19 -080047
James Kuszmaulf8178092020-05-10 18:46:45 -070048bool PathExists(std::string_view path);
49
Austin Schuhe991fe22020-11-18 16:53:39 -080050// Recursively removes everything in the provided path. Ignores any errors it
51// runs across.
52void UnlinkRecursive(std::string_view path);
53
Austin Schuhe4d1a682021-10-01 15:04:50 -070054enum class FileOptions { kReadable, kWriteable };
55
davidjevans8b9b52f2021-09-17 08:57:30 -070056// Maps file from disk into memory
Austin Schuhe4d1a682021-10-01 15:04:50 -070057std::shared_ptr<absl::Span<uint8_t>> MMapFile(
58 const std::string &path, FileOptions options = FileOptions::kReadable);
davidjevans8b9b52f2021-09-17 08:57:30 -070059
James Kuszmaul0625b0d2022-09-21 11:38:48 -070060// Wrapper to handle reading the contents of a file into a buffer. Meant for
61// situations where the malloc'ing of ReadFileToStringOrDie is inappropriate,
62// but where you still want to read a file.
James Kuszmaul0625b0d2022-09-21 11:38:48 -070063class FileReader {
64 public:
James Kuszmaul5a88d412023-01-27 15:55:55 -080065 FileReader(std::string_view filename);
James Kuszmaul0625b0d2022-09-21 11:38:48 -070066 // Reads the entire contents of the file into the internal buffer and returns
67 // a string_view of it.
68 // Note: The result may not be null-terminated.
James Kuszmaul5a88d412023-01-27 15:55:55 -080069 absl::Span<char> ReadContents(absl::Span<char> buffer);
70 // Returns the value of the file as a string, for a fixed-length file.
71 // Returns nullopt if the result is smaller than kSize. Ignores any
72 // bytes beyond kSize.
73 template <int kSize>
74 std::optional<std::array<char, kSize>> ReadString() {
75 std::array<char, kSize> result;
76 const absl::Span<char> used_span =
77 ReadContents(absl::Span<char>(result.data(), result.size()));
78 if (used_span.size() == kSize) {
79 return result;
80 } else {
81 return std::nullopt;
82 }
James Kuszmaul0625b0d2022-09-21 11:38:48 -070083 }
James Kuszmaul5a88d412023-01-27 15:55:55 -080084 // Returns the value of the file as an integer. Crashes if it doesn't fit in a
85 // 32-bit integer. The value may start with 0x for a hex value, otherwise it
86 // must be base 10.
87 int32_t ReadInt32();
James Kuszmaul0625b0d2022-09-21 11:38:48 -070088
89 private:
90 aos::ScopedFD file_;
James Kuszmaul0625b0d2022-09-21 11:38:48 -070091};
92
James Kuszmaulfd43f4e2022-12-16 15:19:35 -080093// Simple interface to allow opening a file for writing and then writing it
94// without any malloc's.
James Kuszmaulfd43f4e2022-12-16 15:19:35 -080095class FileWriter {
96 public:
97 // The result of an individual call to WriteBytes().
98 // Because WriteBytes() may repeatedly call write() when partial writes occur,
99 // it is possible for a non-zero number of bytes to have been written while
100 // still having an error because a late call to write() failed.
101 struct WriteResult {
102 // Total number of bytes successfully written to the file.
103 size_t bytes_written;
104 // If the write was successful (return_code > 0), equal to bytes_written.
105 // Otherwise, equal to the return value of the final call to write.
106 int return_code;
107 };
108
109 FileWriter(std::string_view filename, mode_t permissions = S_IRWXU);
110
111 WriteResult WriteBytes(absl::Span<const uint8_t> bytes);
112 WriteResult WriteBytes(std::string_view bytes);
113 void WriteBytesOrDie(absl::Span<const uint8_t> bytes);
114 void WriteBytesOrDie(std::string_view bytes);
115 int fd() const { return file_.get(); }
116
117 private:
118 aos::ScopedFD file_;
119};
120
Brian Silverman61175fb2016-03-13 15:35:56 -0400121} // namespace util
122} // namespace aos
123
John Park33858a32018-09-28 23:05:48 -0700124#endif // AOS_UTIL_FILE_H_