Barretenberg
The ZK-SNARK library at the core of Aztec
Loading...
Searching...
No Matches
file_io.hpp
Go to the documentation of this file.
1#pragma once
5#include <cstdint>
6#include <cstring>
7#include <fcntl.h>
8#include <filesystem>
9#include <fstream>
10#include <ios>
11#include <sstream>
12#include <sys/stat.h>
13#include <unistd.h>
14#include <vector>
15
16namespace bb {
17inline size_t get_file_size(std::string const& filename)
18{
19 // Open the file in binary mode and move to the end.
20 std::ifstream file(filename, std::ios::binary | std::ios::ate);
21 if (!file) {
22 return 0;
23 }
24
25 file.seekg(0, std::ios::end);
26 return (size_t)file.tellg();
27}
28
29inline std::vector<uint8_t> read_file(const std::string& filename, size_t bytes = 0)
30{
31 // Used for stdin and non-seekable fds (pipes, process substitution) where we don't know the
32 // size in advance. Reads in 64 KiB chunks to avoid the O(n²) reallocation pattern that arises
33 // from the istreambuf_iterator range constructor.
34 constexpr size_t CHUNK = 65536;
35 auto read_chunked = [](int fd, const std::string& name) {
36 std::vector<uint8_t> result;
37 size_t total = 0;
38 ssize_t n = 0;
39 while (true) {
40 // Standard libraries will usually do geometric capacity growth here so that copying is amortized.
41 result.resize(total + CHUNK);
42 n = ::read(fd, result.data() + total, CHUNK);
43 if (n <= 0) {
44 break;
45 }
46 total += static_cast<size_t>(n);
47 }
48 result.resize(total);
49 if (n < 0) {
50 THROW std::runtime_error("Failed to read from " + name + ": " + strerror(errno));
51 }
52 return result;
53 };
54
55 // "-" is the conventional sentinel for stdin.
56 if (filename == "-") {
57 return read_chunked(STDIN_FILENO, "stdin");
58 }
59
60 // std::filesystem::file_size is a single stat() call — no seek-to-end / rewind needed.
61 // The error_code overload returns -1 and sets ec instead of throwing for non-regular
62 // files (pipes, devices), which we treat as "unknown size" and handle with chunked I/O.
63 std::error_code ec;
64 auto raw_size = std::filesystem::file_size(filename, ec);
65 std::optional<size_t> file_size = ec ? std::nullopt : std::optional<size_t>(static_cast<size_t>(raw_size));
66
67 int fd = open(filename.c_str(), O_RDONLY);
68 if (fd == -1) {
69 THROW std::runtime_error("Unable to open file: " + filename + " (" + strerror(errno) + ")");
70 }
71
72 std::vector<uint8_t> fileData;
73 if (!file_size.has_value()) {
74 // Size unknown (pipe, device, etc.): read without pre-allocation.
75 fileData = read_chunked(fd, filename);
76 } else {
77 // Pre-allocate exactly what we need: either the caller's limit or the whole file.
78 // Using POSIX read() directly avoids the extra buffering layer of std::ifstream.
79 size_t to_read = bytes == 0 ? *file_size : bytes;
80 fileData.resize(to_read);
81 size_t total = 0;
82 while (total < to_read) {
83 ssize_t got = ::read(fd, fileData.data() + total, to_read - total);
84 if (got < 0) {
85 close(fd);
86 THROW std::runtime_error("Failed to read file: " + filename + " (" + strerror(errno) + ")");
87 }
88 if (got == 0) {
89 break; // Unexpected EOF (file shrunk between stat and read).
90 }
91 total += static_cast<size_t>(got);
92 }
93 fileData.resize(total); // Shrink if the file was shorter than expected.
94 }
95 close(fd);
96 return fileData;
97}
98
99inline void write_file(const std::string& filename, std::span<const uint8_t> data)
100{
101 // For regular files, truncate and create if missing (O_TRUNC | O_CREAT).
102 // For FIFOs/pipes the file already exists and O_CREAT/O_TRUNC are no-ops, so
103 // the same flags work uniformly for both cases — no need to stat() first.
104 int fd = open(filename.c_str(), O_WRONLY | O_CREAT | O_TRUNC, 0644);
105 if (fd == -1) {
106 THROW std::runtime_error("Failed to open file for writing: " + filename + " (" + strerror(errno) + ")");
107 }
108
109 // Loop to handle short writes (required by POSIX, common on pipes and sockets).
110 size_t total_written = 0;
111 while (total_written < data.size()) {
112 ssize_t written = ::write(fd, data.data() + total_written, data.size() - total_written);
113 if (written < 0) {
114 close(fd);
115 THROW std::runtime_error("Failed to write to file: " + filename + " (" + strerror(errno) + ")");
116 }
117 total_written += static_cast<size_t>(written);
118 }
119 close(fd);
120}
121
122template <typename Fr> inline std::string field_elements_to_json(const std::vector<Fr>& fields)
123{
124 std::stringstream ss;
125 ss << "[";
126 for (size_t i = 0; i < fields.size(); ++i) {
127 ss << '"' << fields[i] << '"';
128 if (i != fields.size() - 1) {
129 ss << ",";
130 }
131 }
132 ss << "]";
133 return ss.str();
134}
135
143inline std::vector<uint8_t> read_vk_file(const std::filesystem::path& vk_path)
144{
145 try {
146 return read_file(vk_path);
147 } catch (const std::runtime_error&) {
148 THROW std::runtime_error("Unable to open file: " + vk_path.string() +
149 "\nGenerate a vk during proving by running `bb prove` with an additional `--write_vk` "
150 "flag, or run `bb write_vk` to generate a standalone vk."
151 "\nIf you already have a vk file, specify its path with `--vk_path <path>`.");
152 }
153}
154} // namespace bb
const std::vector< MemoryValue > data
Entry point for Barretenberg command-line interface.
Definition api.hpp:5
void read(B &it, field2< base_field, Params > &value)
std::vector< uint8_t > read_vk_file(const std::filesystem::path &vk_path)
Read a verification key file with an actionable error message if not found.
Definition file_io.hpp:143
void write(B &buf, field2< base_field, Params > const &value)
std::vector< uint8_t > read_file(const std::string &filename, size_t bytes=0)
Definition file_io.hpp:29
void write_file(const std::string &filename, std::span< const uint8_t > data)
Definition file_io.hpp:99
std::string field_elements_to_json(const std::vector< Fr > &fields)
Definition file_io.hpp:122
size_t get_file_size(std::string const &filename)
Definition file_io.hpp:17
constexpr decltype(auto) get(::tuplet::tuple< T... > &&t) noexcept
Definition tuple.hpp:13
#define THROW