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 :
38 : /* C includes */
39 : #include <sys/types.h>
40 : #include <sys/vfs.h>
41 : #include <unistd.h>
42 :
43 : using json = nlohmann::json;
44 :
45 494 : struct statfs_options {
46 : bool verbose{};
47 : std::string pathname;
48 :
49 : REFL_DECL_STRUCT(statfs_options, REFL_DECL_MEMBER(bool, verbose),
50 : REFL_DECL_MEMBER(std::string, pathname));
51 : };
52 :
53 : struct statfs_output {
54 : int retval;
55 : int errnum;
56 : struct ::statfs statfsbuf;
57 :
58 : REFL_DECL_STRUCT(statfs_output, REFL_DECL_MEMBER(int, retval),
59 : REFL_DECL_MEMBER(int, errnum),
60 : REFL_DECL_MEMBER(struct ::statfs, statfsbuf));
61 : };
62 :
63 : void
64 1 : to_json(json& record, const statfs_output& out) {
65 1 : record = serialize(out);
66 1 : }
67 :
68 : void
69 1 : statfs_exec(const statfs_options& opts) {
70 :
71 1 : struct ::statfs statfsbuf;
72 :
73 1 : auto rv = ::statfs(opts.pathname.c_str(), &statfsbuf);
74 :
75 1 : if(opts.verbose) {
76 0 : fmt::print("statfs(pathname=\"{}\") = {}, errno: {} [{}]\n",
77 0 : opts.pathname, rv, errno, ::strerror(errno));
78 0 : return;
79 : }
80 :
81 1 : json out = statfs_output{rv, errno, statfsbuf};
82 2 : fmt::print("{}\n", out.dump(2));
83 : }
84 :
85 : void
86 247 : statfs_init(CLI::App& app) {
87 :
88 : // Create the option and subcommand objects
89 247 : auto opts = std::make_shared<statfs_options>();
90 247 : auto* cmd = app.add_subcommand("statfs", "Execute the statfs() system call");
91 :
92 : // Add options to cmd, binding them to opts
93 247 : cmd->add_flag("-v,--verbose", opts->verbose,
94 494 : "Produce human readable output");
95 :
96 494 : cmd->add_option("pathname", opts->pathname, "Directory name")
97 : ->required()
98 494 : ->type_name("");
99 :
100 989 : cmd->callback([opts]() { statfs_exec(*opts); });
101 247 : }
|