Commit b097152b authored by Marc Vef's avatar Marc Vef
Browse files

ifs: added pwrite and extraction of O_APPEND flag

parent 69b41f67
Loading
Loading
Loading
Loading
+8 −2
Original line number Diff line number Diff line
@@ -11,11 +11,13 @@
class OpenFile {
private:
    const char* path_;
    bool append_flag_;

    int fd_;
    FILE* tmp_file_;

public:
    OpenFile(const char* path);
    OpenFile(const char* path, const bool append_flag);

    ~OpenFile();

@@ -30,6 +32,10 @@ public:

    void fd(int fd_);

    bool append_flag() const;

    void append_flag(bool append_flag);

};


@@ -47,7 +53,7 @@ public:
    OpenFile* get(int fd);
    bool exist(const int fd);

    int add(const char* path);
    int add(const char* path, const bool append);
    bool remove(const int fd);

};
+1 −0
Original line number Diff line number Diff line
@@ -41,6 +41,7 @@ extern "C" {
#define ld_puts puts

#define ld_write write
#define ld_pwrite pwrite
#define ld_read read
#define ld_pread pread
#define ld_pread64 pread64
+11 −3
Original line number Diff line number Diff line
@@ -6,7 +6,7 @@

using namespace std;

OpenFile::OpenFile(const char* path) : path_(path) {
OpenFile::OpenFile(const char* path, const bool append_flag) : path_(path), append_flag_(append_flag) {
    tmp_file_ = tmpfile(); // create a temporary file in memory and
    fd_ = fileno(tmp_file_); // get a valid file descriptor from the kernel
}
@@ -39,6 +39,14 @@ void OpenFile::annul_fd() {
        fclose(tmp_file_);
}

bool OpenFile::append_flag() const {
    return append_flag_;
}

void OpenFile::append_flag(bool append_flag) {
    OpenFile::append_flag_ = append_flag;
}

OpenFile* OpenFileMap::get(int fd) {
    lock_guard<mutex> lock(files_mutex_);
    auto f = files_.find(fd);
@@ -55,8 +63,8 @@ bool OpenFileMap::exist(const int fd) {
    return !(f == files_.end());
}

int OpenFileMap::add(const char* path) {
    OpenFile file{path};
int OpenFileMap::add(const char* path, const bool append) {
    OpenFile file{path, append};
    lock_guard<mutex> lock(files_mutex_);
    files_.insert(make_pair(file.fd(), file));
    return file.fd();
+23 −3
Original line number Diff line number Diff line
@@ -47,6 +47,7 @@ void* libc_access;
void* libc_puts;

void* libc_write;
void* libc_pwrite;
void* libc_read;
void* libc_pread;
void* libc_pread64;
@@ -72,7 +73,7 @@ int ld_open(const char* path, int flags, ...) {
        va_end(vl);
    }
    if (is_fs_path(path)) {
        auto fd = file_map.add(path);
        auto fd = file_map.add(path, (flags & O_APPEND) != 0);
        // TODO call daemon and return if successful return the above fd. if unsuccessful delete fd remove file from map
#ifndef MARGOIPC

@@ -180,7 +181,9 @@ int ld_puts(const char* str) {

ssize_t ld_write(int fd, const void* buf, size_t count) {
    if (file_map.exist(fd)) {
        auto path = file_map.get(fd)->path(); // TODO use this to send to the daemon (call directly)
        auto adafs_fd = file_map.get(fd);
        auto path = adafs_fd->path(); // TODO use this to send to the daemon (call directly)
        auto append_flag = adafs_fd->append_flag();
        // TODO call daemon and return size written
#ifndef MARGOIPC

@@ -192,10 +195,26 @@ ssize_t ld_write(int fd, const void* buf, size_t count) {
    return (reinterpret_cast<decltype(&write)>(libc_write))(fd, buf, count);
}

ssize_t ld_pwrite(int fd, const void* buf, size_t count, off_t offset) {
    if (file_map.exist(fd)) {
        auto adafs_fd = file_map.get(fd);
        auto path = adafs_fd->path(); // TODO use this to send to the daemon (call directly)
        auto append_flag = adafs_fd->append_flag();
        // TODO call daemon and return size written
#ifndef MARGOIPC

#else

#endif
        return 0;
    }
    return (reinterpret_cast<decltype(&pwrite)>(libc_pwrite))(fd, buf, count, offset);
}

ssize_t ld_read(int fd, void* buf, size_t count) {
    if (file_map.exist(fd)) {
        auto path = file_map.get(fd)->path(); // TODO use this to send to the daemon (call directly)
        // TODO call daemon and return size written
        // TODO call daemon and return size read
#ifndef MARGOIPC

#else
@@ -387,6 +406,7 @@ void init_preload(void) {
    libc_puts = dlsym(libc, "puts");

    libc_write = dlsym(libc, "write");
    libc_pwrite = dlsym(libc, "pwrite");
    libc_read = dlsym(libc, "read");
    libc_pread = dlsym(libc, "pread");
    libc_pread64 = dlsym(libc, "pread64");