Line data Source code
1 : /*
2 : Copyright 2018-2024, Barcelona Supercomputing Center (BSC), Spain
3 : Copyright 2015-2024, Johannes Gutenberg Universitaet Mainz, Germany
4 :
5 : This software was partially supported by the
6 : EC H2020 funded project NEXTGenIO (Project ID: 671951, www.nextgenio.eu).
7 :
8 : This software was partially supported by the
9 : ADA-FS project under the SPPEXA project funded by the DFG.
10 :
11 : This file is part of GekkoFS.
12 :
13 : GekkoFS is free software: you can redistribute it and/or modify
14 : it under the terms of the GNU General Public License as published by
15 : the Free Software Foundation, either version 3 of the License, or
16 : (at your option) any later version.
17 :
18 : GekkoFS is distributed in the hope that it will be useful,
19 : but WITHOUT ANY WARRANTY; without even the implied warranty of
20 : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 : GNU General Public License for more details.
22 :
23 : You should have received a copy of the GNU General Public License
24 : along with GekkoFS. If not, see <https://www.gnu.org/licenses/>.
25 :
26 : SPDX-License-Identifier: GPL-3.0-or-later
27 : */
28 :
29 : /* C++ includes */
30 : #include <CLI/CLI.hpp>
31 : #include <nlohmann/json.hpp>
32 : #include <memory>
33 : #include <fmt/format.h>
34 : #include <commands.hpp>
35 : #include <reflection.hpp>
36 : #include <serialize.hpp>
37 : #include <binary_buffer.hpp>
38 :
39 : /* C includes */
40 : #include <sys/types.h>
41 : #include <sys/stat.h>
42 : #include <sys/uio.h>
43 : #include <fcntl.h>
44 : #include <unistd.h>
45 :
46 : using json = nlohmann::json;
47 :
48 494 : struct preadv_options {
49 : bool verbose{};
50 : std::string pathname;
51 : ::size_t count_0;
52 : ::size_t count_1;
53 : ::size_t offset;
54 :
55 : REFL_DECL_STRUCT(preadv_options, REFL_DECL_MEMBER(bool, verbose),
56 : REFL_DECL_MEMBER(std::string, pathname),
57 : REFL_DECL_MEMBER(::size_t, count_0),
58 : REFL_DECL_MEMBER(::size_t, count_1),
59 : REFL_DECL_MEMBER(::size_t, offset));
60 : };
61 :
62 1 : struct preadv_output {
63 : ::ssize_t retval;
64 : io::buffer buf_0;
65 : io::buffer buf_1;
66 : int errnum;
67 :
68 : REFL_DECL_STRUCT(preadv_output, REFL_DECL_MEMBER(::size_t, retval),
69 : REFL_DECL_MEMBER(void*, buf_0),
70 : REFL_DECL_MEMBER(void*, buf_1),
71 : REFL_DECL_MEMBER(int, errnum));
72 : };
73 :
74 : void
75 1 : to_json(json& record, const preadv_output& out) {
76 1 : record = serialize(out);
77 1 : }
78 :
79 : void
80 1 : preadv_exec(const preadv_options& opts) {
81 :
82 1 : auto fd = ::open(opts.pathname.c_str(), O_RDONLY);
83 :
84 1 : if(fd == -1) {
85 0 : if(opts.verbose) {
86 0 : fmt::print(
87 : "preadv(pathname=\"{}\", count_0={}, count_1={}, offset={}) = {}, errno: {} [{}]\n",
88 0 : opts.pathname, opts.count_0, opts.count_1, opts.offset, fd,
89 0 : errno, ::strerror(errno));
90 0 : return;
91 : }
92 :
93 0 : json out = preadv_output{fd, nullptr, nullptr, errno};
94 0 : fmt::print("{}\n", out.dump(2));
95 :
96 0 : return;
97 : }
98 :
99 2 : io::buffer buf_0(opts.count_0);
100 2 : io::buffer buf_1(opts.count_1);
101 :
102 1 : struct iovec iov[2];
103 :
104 1 : iov[0].iov_base = buf_0.data();
105 1 : iov[1].iov_base = buf_1.data();
106 :
107 1 : iov[0].iov_len = opts.count_0;
108 1 : iov[1].iov_len = opts.count_1;
109 :
110 1 : int rv = ::preadv(fd, iov, 2, opts.offset);
111 :
112 1 : if(opts.verbose) {
113 0 : fmt::print(
114 : "preadv(pathname=\"{}\", count_0={}, count_1={}, offset={}) = {}, errno: {} [{}]\n",
115 0 : opts.pathname, opts.count_0, opts.count_1, opts.offset, rv,
116 0 : errno, ::strerror(errno));
117 0 : return;
118 : }
119 :
120 1 : json out = preadv_output{rv, (rv != -1 ? buf_0 : nullptr),
121 3 : (rv != -1 ? buf_1 : nullptr), errno};
122 2 : fmt::print("{}\n", out.dump(2));
123 : }
124 :
125 : void
126 247 : preadv_init(CLI::App& app) {
127 :
128 : // Create the option and subcommand objects
129 247 : auto opts = std::make_shared<preadv_options>();
130 247 : auto* cmd =
131 247 : app.add_subcommand("preadv", "Execute the preadv() system call");
132 :
133 : // Add options to cmd, binding them to opts
134 247 : cmd->add_flag("-v,--verbose", opts->verbose,
135 494 : "Produce human readable output");
136 :
137 494 : cmd->add_option("pathname", opts->pathname, "Directory name")
138 : ->required()
139 494 : ->type_name("");
140 :
141 247 : cmd->add_option("count_0", opts->count_0,
142 494 : "Number of bytes to read to buffer 0")
143 : ->required()
144 494 : ->type_name("");
145 :
146 247 : cmd->add_option("count_1", opts->count_1,
147 494 : "Number of bytes to read to buffer 1")
148 : ->required()
149 494 : ->type_name("");
150 :
151 494 : cmd->add_option("offset", opts->offset, "Offset to read")
152 : ->required()
153 494 : ->type_name("");
154 :
155 989 : cmd->callback([opts]() { preadv_exec(*opts); });
156 247 : }
|