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