Verified Commit d79c4910 authored by Tommaso Tocci's avatar Tommaso Tocci
Browse files

fix fstat

parent e62d4a33
Loading
Loading
Loading
Loading
+2 −1
Original line number Diff line number Diff line
@@ -8,7 +8,8 @@ int hook_openat(int dirfd, const char *cpath, int flags, mode_t mode);
int hook_close(int fd);
int hook_stat(const char* path, struct stat* buf);
int hook_lstat(const char* path, struct stat* buf);
int hook_fstat(unsigned int, struct stat* buf);
int hook_fstat(unsigned int fd, struct stat* buf);
int hook_fstatat(int dirfd, const char * cpath, struct stat * buf, int flags);
int hook_read(unsigned int fd, void* buf, size_t count);
int hook_write(unsigned int fd, void* buf, size_t count);
int hook_writev(unsigned long fd, const struct iovec * iov, unsigned long iovcnt);
+46 −0
Original line number Diff line number Diff line
@@ -100,6 +100,52 @@ int hook_fstat(unsigned int fd, struct stat* buf) {
    return syscall_no_intercept(SYS_fstat, fd, buf);
}

int hook_fstatat(int dirfd, const char * cpath, struct stat * buf, int flags) {
    if(cpath == nullptr || cpath[0] == '\0') {
        CTX->log()->error("{}() path is invalid", __func__);
        return -EINVAL;
    }

    CTX->log()->trace("{}() called with path '{}' and fd {}", __func__, cpath, dirfd);

    if(flags & AT_EMPTY_PATH) {
        CTX->log()->error("{}() AT_EMPTY_PATH flag not supported", __func__);
        return -ENOTSUP;
    }

    std::string resolved;

    if(cpath[0] != PSP) {
        // cpath is relative
        //TODO handle the case in which dirfd is AT_FDCWD
        if(!(CTX->file_map()->exist(dirfd))) {
            //TODO relative cpath could still lead to our FS
            return syscall_no_intercept(SYS_newfstatat, dirfd, cpath, buf, flags);
        }

        auto dir = CTX->file_map()->get_dir(dirfd);
        if(dir == nullptr) {
            CTX->log()->error("{}() dirfd is not a directory ", __func__);
            return -ENOTDIR;
        }

        std::string path = CTX->mountdir();
        path.append(dir->path());
        path.push_back(PSP);
        path.append(cpath);
        if(resolve_path(path, resolved)) {
            return with_errno(adafs_stat(resolved, buf));
        }
    } else {
        // Path is absolute

        if (CTX->relativize_path(cpath, resolved)) {
            return with_errno(adafs_stat(resolved, buf));
        }
    }
    return syscall_no_intercept(SYS_newfstatat, dirfd, resolved.c_str(), buf, flags);
}

int hook_read(unsigned int fd, void* buf, size_t count) {
    CTX->log()->trace("{}() called with fd {}, count {}", __func__, fd, count);
    if (CTX->file_map()->exist(fd)) {
+7 −0
Original line number Diff line number Diff line
@@ -56,6 +56,13 @@ static inline int hook(long syscall_number,
                            reinterpret_cast<struct stat*>(arg1));
        break;

    case SYS_newfstatat:
        *result = hook_fstatat(static_cast<int>(arg0),
                              reinterpret_cast<const char*>(arg1),
                              reinterpret_cast<struct stat *>(arg2),
                              static_cast<int>(arg3));
        break;

    case SYS_read:
        *result = hook_read(static_cast<unsigned int>(arg0),
                            reinterpret_cast<void*>(arg1),