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