blob: afefa86aa879c575fde4a7246de818557563176c [file] [log] [blame]
John Park33858a32018-09-28 23:05:48 -07001#include "aos/util/file.h"
Brian Silverman61175fb2016-03-13 15:35:56 -04002
3#include <fcntl.h>
Austin Schuhfccb2d02020-01-26 16:11:19 -08004#include <sys/stat.h>
5#include <sys/types.h>
Brian Silverman61175fb2016-03-13 15:35:56 -04006#include <unistd.h>
7
James Kuszmaul3ae42262019-11-08 12:33:41 -08008#include <string_view>
9
John Park33858a32018-09-28 23:05:48 -070010#include "aos/scoped/scoped_fd.h"
Brian Silverman61175fb2016-03-13 15:35:56 -040011
12namespace aos {
13namespace util {
14
James Kuszmaul3ae42262019-11-08 12:33:41 -080015::std::string ReadFileToStringOrDie(const std::string_view filename) {
Brian Silverman61175fb2016-03-13 15:35:56 -040016 ::std::string r;
Austin Schuhcb108412019-10-13 16:09:54 -070017 ScopedFD fd(open(::std::string(filename).c_str(), O_RDONLY));
Alex Perrycb7da4b2019-08-28 19:35:56 -070018 PCHECK(fd.get() != -1) << ": opening " << filename;
Brian Silverman61175fb2016-03-13 15:35:56 -040019 while (true) {
20 char buffer[1024];
21 const ssize_t result = read(fd.get(), buffer, sizeof(buffer));
Alex Perrycb7da4b2019-08-28 19:35:56 -070022 PCHECK(result >= 0) << ": reading from " << filename;
23 if (result == 0) {
Brian Silverman61175fb2016-03-13 15:35:56 -040024 break;
25 }
26 r.append(buffer, result);
27 }
28 return r;
29}
30
James Kuszmaul3ae42262019-11-08 12:33:41 -080031void WriteStringToFileOrDie(const std::string_view filename,
32 const std::string_view contents) {
Alex Perrycb7da4b2019-08-28 19:35:56 -070033 ::std::string r;
34 ScopedFD fd(open(::std::string(filename).c_str(),
35 O_CREAT | O_WRONLY | O_TRUNC, S_IRWXU));
36 PCHECK(fd.get() != -1) << ": opening " << filename;
37 size_t size_written = 0;
38 while (size_written != contents.size()) {
39 const ssize_t result = write(fd.get(), contents.data() + size_written,
40 contents.size() - size_written);
41 PCHECK(result >= 0) << ": reading from " << filename;
42 if (result == 0) {
43 break;
44 }
45
46 size_written += result;
47 }
48}
49
Brian Silvermana9f2ec92020-10-06 18:00:53 -070050bool MkdirPIfSpace(std::string_view path, mode_t mode) {
Austin Schuhfccb2d02020-01-26 16:11:19 -080051 auto last_slash_pos = path.find_last_of("/");
52
53 std::string folder(last_slash_pos == std::string_view::npos
54 ? std::string_view("")
55 : path.substr(0, last_slash_pos));
Brian Silvermana9f2ec92020-10-06 18:00:53 -070056 if (folder.empty()) {
57 return true;
58 }
59 if (!MkdirPIfSpace(folder, mode)) {
60 return false;
61 }
Austin Schuhfccb2d02020-01-26 16:11:19 -080062 const int result = mkdir(folder.c_str(), mode);
63 if (result == -1 && errno == EEXIST) {
64 VLOG(2) << folder << " already exists";
Brian Silvermana9f2ec92020-10-06 18:00:53 -070065 return true;
66 } else if (result == -1 && errno == ENOSPC) {
67 VLOG(2) << "Out of space";
68 return false;
Austin Schuhfccb2d02020-01-26 16:11:19 -080069 } else {
70 VLOG(1) << "Created " << folder;
71 }
72 PCHECK(result == 0) << ": Error creating " << folder;
Brian Silvermana9f2ec92020-10-06 18:00:53 -070073 return true;
Austin Schuhfccb2d02020-01-26 16:11:19 -080074}
75
James Kuszmaulf8178092020-05-10 18:46:45 -070076bool PathExists(std::string_view path) {
77 struct stat buffer;
78 return stat(path.data(), &buffer) == 0;
79}
80
Brian Silverman61175fb2016-03-13 15:35:56 -040081} // namespace util
82} // namespace aos