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