Tomáš Pecka | 491f4f2 | 2020-11-05 15:44:00 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2020 CESNET, https://photonics.cesnet.cz/ |
| 3 | * |
| 4 | * Written by Tomáš Pecka <tomas.pecka@fit.cvut.cz> |
| 5 | * |
| 6 | */ |
| 7 | |
Václav Kubernát | babbab9 | 2021-01-27 09:25:05 +0100 | [diff] [blame] | 8 | #include <cstring> |
| 9 | #include <fmt/format.h> |
Tomáš Pecka | 491f4f2 | 2020-11-05 15:44:00 +0100 | [diff] [blame] | 10 | #include <fstream> |
Václav Kubernát | babbab9 | 2021-01-27 09:25:05 +0100 | [diff] [blame] | 11 | #include <unistd.h> |
Tomáš Pecka | 491f4f2 | 2020-11-05 15:44:00 +0100 | [diff] [blame] | 12 | #include "io.h" |
| 13 | |
| 14 | namespace velia::utils { |
| 15 | |
| 16 | namespace { |
| 17 | |
| 18 | std::ifstream openStream(const std::filesystem::path& path) |
| 19 | { |
| 20 | std::ifstream ifs(path); |
| 21 | if (!ifs.is_open()) |
| 22 | throw std::invalid_argument("File '" + std::string(path) + "' does not exist."); |
| 23 | return ifs; |
| 24 | } |
| 25 | } |
| 26 | |
| 27 | /** @brief Reads a string from a file */ |
| 28 | std::string readFileString(const std::filesystem::path& path) |
| 29 | { |
| 30 | std::ifstream ifs(openStream(path)); |
| 31 | std::string res; |
| 32 | |
| 33 | if (ifs >> res) { |
| 34 | return res; |
| 35 | } |
| 36 | |
| 37 | throw std::domain_error("Could not read '" + std::string(path) + "'."); |
| 38 | } |
| 39 | |
| 40 | /** @brief Reads @p valuesCount 32bit values from a file */ |
| 41 | std::vector<uint32_t> readFileWords(const std::filesystem::path& path, int valuesCount) |
| 42 | { |
| 43 | std::ifstream ifs(openStream(path)); |
| 44 | std::vector<uint32_t> bytes; // no interesting data are wider than 32 bits (for now) |
| 45 | |
| 46 | uint32_t byte; |
| 47 | ifs >> std::hex; |
| 48 | while (valuesCount-- && ifs >> byte) { |
| 49 | bytes.push_back(byte); |
| 50 | } |
| 51 | |
| 52 | if (!ifs) { |
| 53 | throw std::domain_error("Could not read hex data from '" + std::string(path) + "'."); |
| 54 | } |
| 55 | |
| 56 | return bytes; |
| 57 | } |
| 58 | |
Tomáš Pecka | 9b1c967 | 2020-11-11 15:24:06 +0100 | [diff] [blame] | 59 | /** @brief Reads a int64_t number from a file. */ |
| 60 | int64_t readFileInt64(const std::filesystem::path& path) |
| 61 | { |
| 62 | std::ifstream ifs(openStream(path)); |
| 63 | int64_t res; |
| 64 | |
| 65 | if (ifs >> res) { |
| 66 | return res; |
| 67 | } |
| 68 | |
| 69 | throw std::domain_error("Could not read int64_t value from '" + std::string(path) + "'."); |
| 70 | } |
| 71 | |
Václav Kubernát | babbab9 | 2021-01-27 09:25:05 +0100 | [diff] [blame] | 72 | /** @brief Reads whole contents of `path`. Throws if file doesn't exist. */ |
| 73 | std::string readFileToString(const std::filesystem::path& path) |
| 74 | { |
| 75 | std::ifstream ifs(openStream(path)); |
| 76 | |
| 77 | std::istreambuf_iterator<char> begin(ifs), end; |
| 78 | return std::string(begin, end); |
| 79 | } |
| 80 | |
Tomáš Pecka | 5be83e4 | 2021-04-21 17:26:40 +0200 | [diff] [blame] | 81 | void writeFile(const std::string& path, const std::string_view& contents) |
| 82 | { |
| 83 | std::ofstream ofs(path); |
| 84 | if (!ofs.is_open()) { |
| 85 | throw std::invalid_argument("File '" + std::string(path) + "' could not be opened."); |
| 86 | } |
| 87 | |
| 88 | if (!(ofs << contents)) { |
| 89 | throw std::invalid_argument("File '" + std::string(path) + "' could not be written."); |
| 90 | } |
| 91 | } |
| 92 | |
Václav Kubernát | babbab9 | 2021-01-27 09:25:05 +0100 | [diff] [blame] | 93 | void safeWriteFile(const std::string& filename, const std::string_view& contents) |
| 94 | { |
| 95 | auto throwErr = [&filename] (const auto& what) { |
| 96 | throw std::runtime_error(fmt::format("Couldn't write file '{}' ({}) ({})", filename, what, std::strerror(errno))); |
| 97 | }; |
| 98 | // FIXME: not sure if just the tilde is fine... |
| 99 | auto tempFileName = (filename + "~"); |
| 100 | auto f = std::fopen(tempFileName.c_str(), "w"); |
| 101 | if (!f) { |
| 102 | throwErr("fopen"); |
| 103 | } |
| 104 | if (std::fwrite(contents.data(), contents.size(), 1, f) != 1) { |
| 105 | throwErr("fwrite"); |
| 106 | } |
| 107 | if (fsync(fileno(f)) == -1) { |
| 108 | throwErr("fsync"); |
| 109 | } |
| 110 | if (std::fclose(f) == -1) { |
| 111 | throwErr("fclose"); |
| 112 | } |
| 113 | |
| 114 | try { |
| 115 | std::filesystem::rename(tempFileName.c_str(), filename.c_str()); |
| 116 | } catch (std::filesystem::filesystem_error&) { |
| 117 | throwErr("rename"); |
| 118 | } |
| 119 | |
| 120 | auto dirName = std::filesystem::path(filename).parent_path(); |
| 121 | auto fdir = std::fopen(dirName.c_str(), "r"); |
| 122 | if (!fdir) { |
| 123 | throwErr("fopen"); |
| 124 | } |
| 125 | if (fsync(fileno(fdir)) == -1) { |
| 126 | throwErr("fsync"); |
| 127 | } |
| 128 | if (std::fclose(fdir) == -1) { |
| 129 | throwErr("fclose"); |
| 130 | } |
| 131 | } |
Tomáš Pecka | 491f4f2 | 2020-11-05 15:44:00 +0100 | [diff] [blame] | 132 | } |