Verified Commit 58360b32 authored by Tommaso Tocci's avatar Tommaso Tocci
Browse files

relativize path support fd descriptor logic

relativize path now accepts a file descriptor.

If fd is AT_FDCWD relative path need to be considered with respect to
the current working directory. Otherwise the path is relative to the
directory with the given file descriptor
parent 50b0a755
Loading
Loading
Loading
Loading
+10 −1
Original line number Diff line number Diff line
@@ -36,6 +36,12 @@ struct FsConfig {
    std::string rpc_port;
};

enum class RelativizeStatus {
    internal,
    external,
    fd_unknown,
    fd_not_a_dir
};

class PreloadContext {
    private:
@@ -74,8 +80,11 @@ class PreloadContext {
    void cwd(const std::string& path);
    const std::string& cwd() const;

    bool relativize_path(const char * raw_path, std::string& relative_path) const;
    RelativizeStatus relativize_fd_path(int dirfd,
                                        const char * raw_path,
                                        std::string& relative_path) const;

    bool relativize_path(const char * raw_path, std::string& relative_path) const;
    const std::shared_ptr<OpenFileMap>& file_map() const;

    void distributor(std::shared_ptr<Distributor> distributor);
+46 −3
Original line number Diff line number Diff line
#include <preload/preload_context.hpp>

#include <preload/open_file_map.hpp>
#include <preload/open_dir.hpp>
#include <preload/resolve.hpp>
#include <global/path_util.hpp>
#include <cassert>
@@ -53,15 +54,57 @@ const std::string& PreloadContext::cwd() const {
    return cwd_;
}

bool PreloadContext::relativize_path(const char * raw_path, std::string& relative_path) const {
RelativizeStatus PreloadContext::relativize_fd_path(int dirfd,
                                                    const char * raw_path,
                                                    std::string& relative_path) const {

    // Relativize path should be called only after the library constructor has been executed
    assert(initialized_);
    // If we run the constructor we also already setup the mountdir
    assert(!mountdir_.empty());

    if(raw_path == nullptr || raw_path[0] == '\0') {
        return false;
    // We assume raw path is valid
    assert(raw_path != nullptr);

    std::string path;

    if (raw_path[0] != PSP) {
        // path is relative
        if (dirfd == AT_FDCWD) {
            // path is relative to cwd
            path = prepend_path(cwd_, raw_path);
        } else {
            if (!ofm_->exist(dirfd)) {
                return RelativizeStatus::fd_unknown;
            }
            // path is relative to fd
            auto dir = ofm_->get_dir(dirfd);
            if (dir == nullptr) {
                return RelativizeStatus::fd_not_a_dir;
            }
            path = mountdir_;
            path.append(dir->path());
            path.push_back(PSP);
            path.append(raw_path);
        }
    } else {
        path = raw_path;
    }

    if (resolve_path(path, relative_path)) {
        return RelativizeStatus::internal;
    }
    return RelativizeStatus::external;
}

bool PreloadContext::relativize_path(const char * raw_path, std::string& relative_path) const {
    // Relativize path should be called only after the library constructor has been executed
    assert(initialized_);
    // If we run the constructor we also already setup the mountdir
    assert(!mountdir_.empty());

    // We assume raw path is valid
    assert(raw_path != nullptr);

    std::string path;