Commit 84f3bcea authored by Ramon Nou's avatar Ramon Nou
Browse files

Implemented unlink testing and ISDIR errno

parent dbcf7f7f
Loading
Loading
Loading
Loading
Loading
+7 −0
Original line number Diff line number Diff line
@@ -276,6 +276,13 @@ gkfs_remove(const std::string& path) {
    if(!md) {
        return -1;
    }

    if(S_ISDIR(md->mode())) {
        LOG(ERROR, "Cannot remove directory '{}'", path);
        errno = EISDIR;
        return -1;
    }

    auto err = gkfs::rpc::forward_remove(path);
    if(err) {
        errno = err;
+1 −0
Original line number Diff line number Diff line
@@ -62,6 +62,7 @@ add_executable(gkfs.io
    gkfs.io/getcwd_validate.cpp
    gkfs.io/symlink.cpp
    gkfs.io/directory_validate.cpp
    gkfs.io/unlink.cpp
)

include(FetchContent)
+3 −0
Original line number Diff line number Diff line
@@ -108,4 +108,7 @@ getcwd_validate_init(CLI::App& app);
void
symlink_init(CLI::App& app);

void
unlink_init(CLI::App& app);

#endif // IO_COMMANDS_HPP
+1 −0
Original line number Diff line number Diff line
@@ -62,6 +62,7 @@ init_commands(CLI::App& app) {
    chdir_init(app);
    getcwd_validate_init(app);
    symlink_init(app);
    unlink_init(app);
}


+98 −0
Original line number Diff line number Diff line
/*
  Copyright 2018-2022, Barcelona Supercomputing Center (BSC), Spain
  Copyright 2015-2022, Johannes Gutenberg Universitaet Mainz, Germany

  This software was partially supported by the
  EC H2020 funded project NEXTGenIO (Project ID: 671951, www.nextgenio.eu).

  This software was partially supported by the
  ADA-FS project under the SPPEXA project funded by the DFG.

  This file is part of GekkoFS.

  GekkoFS is free software: you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation, either version 3 of the License, or
  (at your option) any later version.

  GekkoFS is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with GekkoFS.  If not, see <https://www.gnu.org/licenses/>.

  SPDX-License-Identifier: GPL-3.0-or-later
*/

/* C++ includes */
#include <CLI11/CLI11.hpp>
#include <nlohmann/json.hpp>
#include <memory>
#include <fmt/format.h>
#include <commands.hpp>
#include <reflection.hpp>
#include <serialize.hpp>
#include <binary_buffer.hpp>

/* C includes */
#include <unistd.h>

using json = nlohmann::json;

struct unlink_options {
    bool verbose{};
    std::string pathname;

    REFL_DECL_STRUCT(unlink_options, REFL_DECL_MEMBER(bool, verbose),
                     REFL_DECL_MEMBER(std::string, pathname));
};

struct unlink_output {
    int retval;
    int errnum;

    REFL_DECL_STRUCT(unlink_output, REFL_DECL_MEMBER(int, retval),
                     REFL_DECL_MEMBER(int, errnum));
};

void
to_json(json& record, const unlink_output& out) {
    record = serialize(out);
}

void
unlink_exec(const unlink_options& opts) {

    auto fd = ::unlink(opts.pathname.c_str());

    if(opts.verbose) {
        fmt::print("unlink(pathname=\"{}\") = {}, errno: {} [{}]\n",
                   opts.pathname, errno, ::strerror(errno));
        return;
    }

    json out = unlink_output{fd, errno};
    fmt::print("{}\n", out.dump(2));

    return;
}

void
unlink_init(CLI::App& app) {

    // Create the option and subcommand objects
    auto opts = std::make_shared<unlink_options>();
    auto* cmd = app.add_subcommand("unlink", "Execute the unlink() system call");

    // Add options to cmd, binding them to opts
    cmd->add_flag("-v,--verbose", opts->verbose,
                  "Produce human readable output");

    cmd->add_option("pathname", opts->pathname, "File name")
            ->required()
            ->type_name("");

    cmd->callback([opts]() { unlink_exec(*opts); });
}
Loading