Verified Commit 3435e9da authored by Tommaso Tocci's avatar Tommaso Tocci
Browse files

Store mountdir as splitted string components

parent 06c72fe6
Loading
Loading
Loading
Loading
+2 −0
Original line number Diff line number Diff line
@@ -2,6 +2,7 @@
#define IFS_PATH_UTIL_HPP

#include <string>
#include <vector>

#define PATH_MAX_LEN 4096 // 4k chars

@@ -12,5 +13,6 @@ 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);
std::string get_current_working_dir();
std::vector<std::string> split_path(const std::string& path);

#endif //IFS_PATH_UTIL_HPP
+5 −2
Original line number Diff line number Diff line
@@ -3,6 +3,7 @@

#include <extern/spdlog/spdlog.h>
#include <memory>
#include <vector>
#include <string>

/* Forward declarations */
@@ -45,6 +46,7 @@ class PreloadContext {
    std::shared_ptr<FsConfig> fs_conf_;

    std::string cwd_;
    std::vector<std::string> mountdir_components_;
    std::string mountdir_;
    bool initialized_;

@@ -61,10 +63,11 @@ class PreloadContext {
    std::shared_ptr<spdlog::logger> log() const;

    void mountdir(const std::string& path);
    std::string mountdir() const;
    const std::string& mountdir() const;
    const std::vector<std::string>& mountdir_components() const;

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

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

+15 −0
Original line number Diff line number Diff line
@@ -20,6 +20,21 @@ bool has_trailing_slash(const std::string& path) {
    return path.back() == PSP;
}

std::vector<std::string> split_path(const std::string& path) { 
    std::vector<std::string> tokens;
    size_t start = std::string::npos;
    size_t end = (path.front() != PSP)? 0 : 1;
    while(end != std::string::npos && end < path.size()) {
        start = end;
        end = path.find(PSP, start);
        tokens.push_back(path.substr(start, end - start));
        if(end != std::string::npos) {
            ++end;
        }
    } 
    return tokens;
}

/* Make an absolute path relative to a root path
 *
 * Convert @absolute_path into a relative one with respect to the given @root_path.
+1 −0
Original line number Diff line number Diff line
@@ -21,6 +21,7 @@ std::shared_ptr<spdlog::logger> PreloadContext::log() const {

void PreloadContext::mountdir(const std::string& path) {
    assert(is_absolute_path(path));
    mountdir_components_ = split_path(path);
    mountdir_ = path;
}