Unverified Commit 8a5d3de0 authored by Tommaso Tocci's avatar Tommaso Tocci
Browse files

Client send relative paths

Paths are now made relative to the mountdir at client side.

The new relativize_path fucntion in the PreloadContext is used to check
if a path is relative to the pseudo-mount directory and to make it
relative
parent 9b82b532
Loading
Loading
Loading
Loading
+12 −0
Original line number Diff line number Diff line
#ifndef IFS_PATH_UTIL_HPP
#define IFS_PATH_UTIL_HPP

#include <string>

bool is_relative_path(const std::string& path);
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);

#endif //IFS_PATH_UTIL_HPP
+2 −0
Original line number Diff line number Diff line
@@ -27,6 +27,8 @@ class PreloadContext {

    void mountdir(const std::string& path);
    std::string mountdir() const;

    bool relativize_path(std::string& path) const;
};


+2 −0
Original line number Diff line number Diff line
set(DAEMON_SRC
    ../global/rpc/rpc_utils.cpp
    ../global/global_func.cpp
    ../global/path_util.cpp
    adafs_daemon.cpp
    adafs_ops/data.cpp
    adafs_ops/metadentry.cpp
@@ -22,6 +23,7 @@ set(DAEMON_HEADERS
    ../../include/global/rpc/ipc_types.hpp
    ../../include/global/rpc/rpc_types.hpp
    ../../include/global/rpc/rpc_utils.hpp
    ../../include/global/path_util.hpp
    ../../include/daemon/adafs_daemon.hpp
    ../../include/daemon/adafs_ops/data.hpp
    ../../include/daemon/adafs_ops/metadentry.hpp
+4 −9
Original line number Diff line number Diff line

#include <daemon/adafs_ops/data.hpp>
#include <boost/filesystem.hpp>
#include <global/path_util.hpp>

namespace bfs = boost::filesystem;
using namespace std;

std::string path_to_fspath(const std::string& path) {
    // root path is absolute as is the path comes in here which is hierarchically under root_path
    // XXX check if this can be done easier
    string fs_path;
    set_difference(path.begin(), path.end(), ADAFS_DATA->mountdir().begin(), ADAFS_DATA->mountdir().end(),
                   std::back_inserter(fs_path));
    if (fs_path.at(0) == '/') {
        fs_path = fs_path.substr(1, fs_path.size());
    }
    assert(is_absolute_path(path));
    auto fs_path = path.substr(1); //remove leading slash
    // replace / with : to avoid making a bunch of mkdirs to store the data in the underlying fs. XXX Can this be done with hashing?
    replace(fs_path.begin(), fs_path.end(), '/', ':');
    std::replace(fs_path.begin(), fs_path.end(), '/', ':');
    return fs_path;
}

+51 −0
Original line number Diff line number Diff line
#include <global/path_util.hpp>
#include <cassert>


const constexpr char PSP('/'); // PATH SEPARATOR

bool is_relative_path(const std::string& path) {
    return (!path.empty()) &&
           (path.front() != PSP);
}

bool is_absolute_path(const std::string& path) {
    return (!path.empty()) &&
           (path.front() == PSP);
}

bool has_trailing_slash(const std::string& path) {
    return (path.back() == PSP);
}

/* Make an absolute path relative to a root path
 *
 * Convert @absolute_path into a relative one with respect to the given @root_path.
 * If @absolute_path do not start at the given @root_path an empty string will be returned.
 * NOTE: Trailing slash will be stripped from the new constructed relative path.
 */
std::string path_to_relative(const std::string& root_path, const std::string& absolute_path) {
    assert(is_absolute_path(root_path));
    assert(is_absolute_path(absolute_path));
    assert(!has_trailing_slash(root_path));

    auto diff_its = std::mismatch(absolute_path.cbegin(), absolute_path.cend(), root_path.cbegin());
    if(diff_its.second != root_path.cend()){
        // complete path doesn't start with root_path
        return {};
    }
    
    // iterator to the starting char of the relative portion of the @absolute_path
    auto rel_it_begin = diff_its.first;

    // relative path start exactly after the root_path prefix
    assert(rel_it_begin == (absolute_path.cbegin() + root_path.size()));

    // iterator to the last char of the relative portion of the @absolute_path
    auto rel_it_end = absolute_path.cend();
    if(has_trailing_slash(absolute_path)) {
        --rel_it_end;
    }

    return {rel_it_begin, rel_it_end};
}
Loading