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 : #include <binary_buffer.hpp>
38 :
39 : /* C includes */
40 : #include <sys/types.h>
41 : #include <sys/stat.h>
42 : #include <fcntl.h>
43 : #include <unistd.h>
44 :
45 : using json = nlohmann::json;
46 :
47 536 : struct open_options {
48 : bool verbose{};
49 : std::string pathname;
50 : int flags;
51 : ::mode_t mode;
52 :
53 : REFL_DECL_STRUCT(open_options, REFL_DECL_MEMBER(bool, verbose),
54 : REFL_DECL_MEMBER(std::string, pathname),
55 : REFL_DECL_MEMBER(int, flags),
56 : REFL_DECL_MEMBER(::mode_t, mode));
57 : };
58 :
59 : struct open_output {
60 : int retval;
61 : int errnum;
62 :
63 : REFL_DECL_STRUCT(open_output, REFL_DECL_MEMBER(int, retval),
64 : REFL_DECL_MEMBER(int, errnum));
65 : };
66 :
67 : void
68 62 : to_json(json& record, const open_output& out) {
69 62 : record = serialize(out);
70 62 : }
71 :
72 : void
73 62 : open_exec(const open_options& opts) {
74 :
75 62 : auto fd = ::open(opts.pathname.c_str(), opts.flags, opts.mode);
76 :
77 62 : if(opts.verbose) {
78 0 : fmt::print(
79 : "open(pathname=\"{}\", flags={}, mode={:#04o}) = {}, errno: {} [{}]\n",
80 0 : opts.pathname, opts.flags, opts.mode, fd, errno,
81 0 : ::strerror(errno));
82 0 : return;
83 : }
84 :
85 62 : json out = open_output{fd, errno};
86 62 : fmt::print("{}\n", out.dump(2));
87 :
88 62 : return;
89 : }
90 :
91 : void
92 268 : open_init(CLI::App& app) {
93 :
94 : // Create the option and subcommand objects
95 268 : auto opts = std::make_shared<open_options>();
96 268 : auto* cmd = app.add_subcommand("open", "Execute the open() system call");
97 :
98 : // Add options to cmd, binding them to opts
99 268 : cmd->add_flag("-v,--verbose", opts->verbose,
100 536 : "Produce human readable output");
101 :
102 536 : cmd->add_option("pathname", opts->pathname, "Directory name")
103 : ->required()
104 536 : ->type_name("");
105 :
106 536 : cmd->add_option("flags", opts->flags, "Open flags")
107 : ->required()
108 536 : ->type_name("")
109 268 : ->check(CLI::NonNegativeNumber);
110 :
111 268 : cmd->add_option("mode", opts->mode,
112 536 : "Octal mode specified for the new directory (e.g. 0664)")
113 : //->required()
114 268 : ->default_val(0)
115 536 : ->type_name("")
116 268 : ->check(CLI::NonNegativeNumber);
117 :
118 :
119 1134 : cmd->callback([opts]() { open_exec(*opts); });
120 268 : }
|