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 | |
Jan Kundrát | 3fc09b9 | 2024-10-15 15:45:38 +0200 | [diff] [blame] | 81 | /** @brief Read the entire content of `path` into a vector of bytes */ |
| 82 | std::vector<uint8_t> readFileToBytes(const std::filesystem::path& path) |
| 83 | { |
| 84 | std::ifstream ifs(openStream(path)); |
| 85 | return {std::istreambuf_iterator<char>{ifs}, {}}; |
| 86 | } |
| 87 | |
| 88 | |
Tomáš Pecka | 5be83e4 | 2021-04-21 17:26:40 +0200 | [diff] [blame] | 89 | void writeFile(const std::string& path, const std::string_view& contents) |
| 90 | { |
| 91 | std::ofstream ofs(path); |
| 92 | if (!ofs.is_open()) { |
| 93 | throw std::invalid_argument("File '" + std::string(path) + "' could not be opened."); |
| 94 | } |
| 95 | |
| 96 | if (!(ofs << contents)) { |
| 97 | throw std::invalid_argument("File '" + std::string(path) + "' could not be written."); |
| 98 | } |
| 99 | } |
| 100 | |
Václav Kubernát | babbab9 | 2021-01-27 09:25:05 +0100 | [diff] [blame] | 101 | void safeWriteFile(const std::string& filename, const std::string_view& contents) |
| 102 | { |
| 103 | auto throwErr = [&filename] (const auto& what) { |
| 104 | throw std::runtime_error(fmt::format("Couldn't write file '{}' ({}) ({})", filename, what, std::strerror(errno))); |
| 105 | }; |
| 106 | // FIXME: not sure if just the tilde is fine... |
| 107 | auto tempFileName = (filename + "~"); |
| 108 | auto f = std::fopen(tempFileName.c_str(), "w"); |
| 109 | if (!f) { |
| 110 | throwErr("fopen"); |
| 111 | } |
| 112 | if (std::fwrite(contents.data(), contents.size(), 1, f) != 1) { |
| 113 | throwErr("fwrite"); |
| 114 | } |
| 115 | if (fsync(fileno(f)) == -1) { |
| 116 | throwErr("fsync"); |
| 117 | } |
| 118 | if (std::fclose(f) == -1) { |
| 119 | throwErr("fclose"); |
| 120 | } |
| 121 | |
| 122 | try { |
| 123 | std::filesystem::rename(tempFileName.c_str(), filename.c_str()); |
| 124 | } catch (std::filesystem::filesystem_error&) { |
| 125 | throwErr("rename"); |
| 126 | } |
| 127 | |
| 128 | auto dirName = std::filesystem::path(filename).parent_path(); |
| 129 | auto fdir = std::fopen(dirName.c_str(), "r"); |
| 130 | if (!fdir) { |
| 131 | throwErr("fopen"); |
| 132 | } |
| 133 | if (fsync(fileno(fdir)) == -1) { |
| 134 | throwErr("fsync"); |
| 135 | } |
| 136 | if (std::fclose(fdir) == -1) { |
| 137 | throwErr("fclose"); |
| 138 | } |
| 139 | } |
Tomáš Pecka | 491f4f2 | 2020-11-05 15:44:00 +0100 | [diff] [blame] | 140 | } |