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 <fcntl.h>
43 : #include <unistd.h>
44 :
45 : using json = nlohmann::json;
46 :
47 494 : struct read_options {
48 : bool verbose{};
49 : std::string pathname;
50 : ::size_t count;
51 :
52 : REFL_DECL_STRUCT(read_options, REFL_DECL_MEMBER(bool, verbose),
53 : REFL_DECL_MEMBER(std::string, pathname),
54 : REFL_DECL_MEMBER(::size_t, count));
55 : };
56 :
57 10 : struct read_output {
58 : ::ssize_t retval;
59 : io::buffer buf;
60 : int errnum;
61 :
62 : REFL_DECL_STRUCT(read_output, REFL_DECL_MEMBER(::size_t, retval),
63 : REFL_DECL_MEMBER(void*, buf),
64 : REFL_DECL_MEMBER(int, errnum));
65 : };
66 :
67 : void
68 10 : to_json(json& record, const read_output& out) {
69 10 : record = serialize(out);
70 10 : }
71 :
72 : void
73 10 : read_exec(const read_options& opts) {
74 :
75 10 : auto fd = ::open(opts.pathname.c_str(), O_RDONLY);
76 :
77 10 : if(fd == -1) {
78 0 : if(opts.verbose) {
79 0 : fmt::print("read(pathname=\"{}\", count={}) = {}, errno: {} [{}]\n",
80 0 : opts.pathname, opts.count, fd, errno, ::strerror(errno));
81 0 : return;
82 : }
83 :
84 0 : json out = read_output{fd, nullptr, errno};
85 0 : fmt::print("{}\n", out.dump(2));
86 :
87 0 : return;
88 : }
89 :
90 20 : io::buffer buf(opts.count);
91 :
92 10 : auto rv = ::read(fd, buf.data(), opts.count);
93 :
94 10 : if(opts.verbose) {
95 0 : fmt::print("read(pathname=\"{}\", count={}) = {}, errno: {} [{}]\n",
96 0 : opts.pathname, opts.count, rv, errno, ::strerror(errno));
97 0 : return;
98 : }
99 :
100 30 : json out = read_output{rv, (rv != -1 ? buf : nullptr), errno};
101 20 : fmt::print("{}\n", out.dump(2));
102 : }
103 :
104 : void
105 247 : read_init(CLI::App& app) {
106 :
107 : // Create the option and subcommand objects
108 247 : auto opts = std::make_shared<read_options>();
109 247 : auto* cmd = app.add_subcommand("read", "Execute the read() system call");
110 :
111 : // Add options to cmd, binding them to opts
112 247 : cmd->add_flag("-v,--verbose", opts->verbose,
113 494 : "Produce human readable output");
114 :
115 494 : cmd->add_option("pathname", opts->pathname, "Directory name")
116 : ->required()
117 494 : ->type_name("");
118 :
119 494 : cmd->add_option("count", opts->count, "Number of bytes to read")
120 : ->required()
121 494 : ->type_name("");
122 :
123 998 : cmd->callback([opts]() { read_exec(*opts); });
124 247 : }
|