John Park | 33858a3 | 2018-09-28 23:05:48 -0700 | [diff] [blame] | 1 | #include "aos/util/file.h" |
Brian Silverman | 61175fb | 2016-03-13 15:35:56 -0400 | [diff] [blame] | 2 | |
| 3 | #include <fcntl.h> |
Austin Schuh | fccb2d0 | 2020-01-26 16:11:19 -0800 | [diff] [blame^] | 4 | #include <sys/stat.h> |
| 5 | #include <sys/types.h> |
Brian Silverman | 61175fb | 2016-03-13 15:35:56 -0400 | [diff] [blame] | 6 | #include <unistd.h> |
| 7 | |
James Kuszmaul | 3ae4226 | 2019-11-08 12:33:41 -0800 | [diff] [blame] | 8 | #include <string_view> |
| 9 | |
John Park | 33858a3 | 2018-09-28 23:05:48 -0700 | [diff] [blame] | 10 | #include "aos/scoped/scoped_fd.h" |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 11 | #include "glog/logging.h" |
Brian Silverman | 61175fb | 2016-03-13 15:35:56 -0400 | [diff] [blame] | 12 | |
| 13 | namespace aos { |
| 14 | namespace util { |
| 15 | |
James Kuszmaul | 3ae4226 | 2019-11-08 12:33:41 -0800 | [diff] [blame] | 16 | ::std::string ReadFileToStringOrDie(const std::string_view filename) { |
Brian Silverman | 61175fb | 2016-03-13 15:35:56 -0400 | [diff] [blame] | 17 | ::std::string r; |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 18 | ScopedFD fd(open(::std::string(filename).c_str(), O_RDONLY)); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 19 | PCHECK(fd.get() != -1) << ": opening " << filename; |
Brian Silverman | 61175fb | 2016-03-13 15:35:56 -0400 | [diff] [blame] | 20 | while (true) { |
| 21 | char buffer[1024]; |
| 22 | const ssize_t result = read(fd.get(), buffer, sizeof(buffer)); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 23 | PCHECK(result >= 0) << ": reading from " << filename; |
| 24 | if (result == 0) { |
Brian Silverman | 61175fb | 2016-03-13 15:35:56 -0400 | [diff] [blame] | 25 | break; |
| 26 | } |
| 27 | r.append(buffer, result); |
| 28 | } |
| 29 | return r; |
| 30 | } |
| 31 | |
James Kuszmaul | 3ae4226 | 2019-11-08 12:33:41 -0800 | [diff] [blame] | 32 | void WriteStringToFileOrDie(const std::string_view filename, |
| 33 | const std::string_view contents) { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 34 | ::std::string r; |
| 35 | ScopedFD fd(open(::std::string(filename).c_str(), |
| 36 | O_CREAT | O_WRONLY | O_TRUNC, S_IRWXU)); |
| 37 | PCHECK(fd.get() != -1) << ": opening " << filename; |
| 38 | size_t size_written = 0; |
| 39 | while (size_written != contents.size()) { |
| 40 | const ssize_t result = write(fd.get(), contents.data() + size_written, |
| 41 | contents.size() - size_written); |
| 42 | PCHECK(result >= 0) << ": reading from " << filename; |
| 43 | if (result == 0) { |
| 44 | break; |
| 45 | } |
| 46 | |
| 47 | size_written += result; |
| 48 | } |
| 49 | } |
| 50 | |
Austin Schuh | fccb2d0 | 2020-01-26 16:11:19 -0800 | [diff] [blame^] | 51 | void MkdirP(std::string_view path, mode_t mode) { |
| 52 | auto last_slash_pos = path.find_last_of("/"); |
| 53 | |
| 54 | std::string folder(last_slash_pos == std::string_view::npos |
| 55 | ? std::string_view("") |
| 56 | : path.substr(0, last_slash_pos)); |
| 57 | if (folder.empty()) return; |
| 58 | MkdirP(folder, mode); |
| 59 | const int result = mkdir(folder.c_str(), mode); |
| 60 | if (result == -1 && errno == EEXIST) { |
| 61 | VLOG(2) << folder << " already exists"; |
| 62 | return; |
| 63 | } else { |
| 64 | VLOG(1) << "Created " << folder; |
| 65 | } |
| 66 | PCHECK(result == 0) << ": Error creating " << folder; |
| 67 | } |
| 68 | |
Brian Silverman | 61175fb | 2016-03-13 15:35:56 -0400 | [diff] [blame] | 69 | } // namespace util |
| 70 | } // namespace aos |