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