Verified Commit 11cee2f8 authored by Tommaso Tocci's avatar Tommaso Tocci
Browse files

Check parent entity existance before making new node

Ensure that the parent directory exists before to make a new node
parent d57cc232
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -8,5 +8,6 @@ bool is_absolute_path(const std::string& path);
bool has_trailing_slash(const std::string& path);

std::string path_to_relative(const std::string& root_path, const std::string& complete_path);
std::string dirname(const std::string& path);

#endif //IFS_PATH_UTIL_HPP
+13 −0
Original line number Diff line number Diff line
@@ -56,3 +56,16 @@ std::string path_to_relative(const std::string& root_path, const std::string& ab

    return {rel_it_begin, rel_it_end};
}

std::string dirname(const std::string& path) {
    assert(path.size() > 1 || path.front() == PSP);
    assert(path.size() == 1 || !has_trailing_slash(path));

    auto parent_path_size = path.find_last_of(PSP);
    assert(parent_path_size != std::string::npos);
    if(parent_path_size == 0) {
        // parent is '/'
        parent_path_size = 1;
    }
    return path.substr(0, parent_path_size);
}
+14 −0
Original line number Diff line number Diff line
#include <sys/statfs.h>

#include <global/configure.hpp>
#include <global/path_util.hpp>
#include <preload/preload.hpp>
#include <preload/adafs_functions.hpp>
#include <preload/rpc/ld_rpc_metadentry.hpp>
@@ -78,6 +79,19 @@ int adafs_mk_node(const std::string& path, const mode_t mode) {
    //file type must be either regular file or directory
    assert(S_ISREG(mode) || S_ISDIR(mode));

    struct stat st;
    auto p_comp = dirname(path);
    auto ret = adafs_stat(p_comp, &st);
    if(ret != 0) {
        CTX->log()->debug("{}() parent component does not exists: '{}'", __func__, p_comp);
        errno = ENOENT;
        return -1;
    }
    if(!S_ISDIR(st.st_mode)) {
        CTX->log()->debug("{}() parent component is not a direcotory: '{}'", __func__, p_comp);
        errno = ENOTDIR;
        return -1;
    }
    return rpc_send_mk_node(path, mode);
}