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