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