Commit 812d6e5c authored by Marc Vef's avatar Marc Vef
Browse files

Metadata class, init and read metadata for root

parent 33d1d6e9
Loading
Loading
Loading
Loading
+2 −2
Original line number Diff line number Diff line
@@ -13,6 +13,6 @@ set(CMAKE_EXPORT_COMPILE_COMMANDS 1)
find_package(FUSE REQUIRED)

include_directories(${FUSE_INCLUDE_DIR} include/)
set(SOURCE_FILES src/main.cpp src/main.h src/fuse_ops.h)
set(SOURCE_FILES src/main.cpp src/main.h src/fuse_ops.h src/util.cpp src/metadata.h src/metadata.cpp src/metadata_ops.h src/metadata_ops.cpp)
add_executable(adafs ${SOURCE_FILES} src/main.cpp)
target_link_libraries(adafs ${FUSE_LIBRARIES})
 No newline at end of file
target_link_libraries(adafs ${FUSE_LIBRARIES} -lpthread -lboost_system -lboost_filesystem)
 No newline at end of file
+41 −8
Original line number Diff line number Diff line
@@ -5,7 +5,9 @@

static struct fuse_operations adafs_ops;

using namespace std;

std::shared_ptr<Metadata> md;

//TODO Implement all stat functions so that getattr works as expected
//bb_getattr(path="/", statbuf=0x8b9a8c60)
@@ -26,23 +28,55 @@ static struct fuse_operations adafs_ops;
//st_mtime = 0x58b6cfc9
//st_ctime = 0x58b6cfc9
int adafs_getattr(const char *path, struct stat *attr){

    if (strcmp(path, "/") == 0) {
        attr->st_mode = S_IFDIR | 0755;
        attr->st_nlink = 2;
        attr->st_ino = md->getInode_no();
        attr->st_mode = md->getMode();
        attr->st_nlink = md->getLink_count();
        attr->st_uid = md->getUid();
        attr->st_gid = md->getGid();
        attr->st_size = md->getSize();
        attr->st_blksize = 4096;
        attr->st_blocks = md->getBlocks();
        attr->st_atim.tv_sec = md->getAtime_();
        attr->st_mtim.tv_sec = md->getMtime_();
        attr->st_ctim.tv_sec = md->getCtime_();
        return 0;



//        attr->st_mode = S_IFDIR | 0755;
//        attr->st_nlink = 2;
    }

    if (strcmp(path, "/file") == 0) {
        attr->st_mode = S_IFREG | 0755;
        attr->st_nlink = 1;
        attr->st_size = strlen("blubb");
        ADAFS_DATA->logger->info(ADAFS_DATA->rootdir);
        return 0;
    }
    return -ENOENT;
}

void *adafs_init(struct fuse_conn_info *conn) {

    ADAFS_DATA->logger->info("init function"s);
    ADAFS_DATA->logger->info("uid_: {}", fuse_get_context()->uid);
    ADAFS_DATA->logger->info("gid_: {}", fuse_get_context()->gid);
    ADAFS_DATA->logger->info("pid: {0:d}", fuse_get_context()->pid);
    ADAFS_DATA->logger->info("rootdir: {}", ((struct adafs_data*)fuse_get_context()->private_data)->rootdir);
    //Initialize directory structure for metadata.
    boost::filesystem::create_directories(ADAFS_DATA->rootdir + "/meta/dentries"s);
    boost::filesystem::create_directories(ADAFS_DATA->rootdir + "/meta/inodes"s);
    boost::filesystem::create_directories(ADAFS_DATA->rootdir + "/data/chunks"s);
    //Init metadata
//    if (get_metadata("/") == -ENOENT) {
//        md = make_shared<Metadata>(S_IFDIR | S_IRWXU | S_IRWXG | S_IRWXO);
        md = make_shared<Metadata>(S_IFDIR | 0755);
//    }
    ADAFS_DATA->logger->info("Survived creating Metadata object"s);
    ADAFS_DATA->logger->flush();


    return ADAFS_DATA;
}

@@ -59,18 +93,17 @@ static void init_adafs_ops(fuse_operations *ops) {
}


#include <iostream>
#include <memory>

int main(int argc, char *argv[]) {
    //Initialize the mapping of Fuse functions
    init_adafs_ops(&adafs_ops);

    // create the adafs_data object (struct)
    auto a_data = std::make_shared<adafs_data>();
    auto a_data = make_unique<adafs_data>();
    //set the logger and initialize it with spdlog
    a_data->logger = spdlog::basic_logger_mt("basic_logger", "adafs.log");
    //extract the rootdir from argv and put it into rootdir of adafs_data
    a_data->rootdir = std::string(realpath(argv[argc-2], NULL));
    a_data->rootdir = string(realpath(argv[argc-2], NULL));
    argv[argc-2] = argv[argc-1];
    argv[argc-1] = NULL;
    argc--;
+16 −2
Original line number Diff line number Diff line
@@ -9,13 +9,27 @@

#include <fuse/fuse.h>
#include <string>
#include <iostream>
#include <cstdint>

//boost libraries
#include <boost/filesystem.hpp>

#include "spdlog/spdlog.h"
#include "metadata.h"

struct adafs_data {
    std::string                             rootdir;
    std::shared_ptr<spdlog::logger>         logger;
    std::int64_t                            inode_count;
    std::mutex                              inode_mutex;
};

#define ADAFS_DATA ((struct adafs_data*) fuse_get_context()->private_data)

namespace util {
    int reset_inode_no(void);
    ino_t generate_inode_no(void);
}

#endif //MAIN_H

fs/src/metadata.cpp

0 → 100644
+115 −0
Original line number Diff line number Diff line
//
// Created by draze on 3/5/17.
//

#include "metadata.h"

time_t Metadata::getAtime_() const {
    return atime_;
}

void Metadata::setAtime_(time_t atime_) {
    Metadata::atime_ = atime_;
}

time_t Metadata::getMtime_() const {
    return mtime_;
}

void Metadata::setMtime_(time_t mtime_) {
    Metadata::mtime_ = mtime_;
}

time_t Metadata::getCtime_() const {
    return ctime_;
}

void Metadata::setCtime_(time_t ctime_) {
    Metadata::ctime_ = ctime_;
}

void Metadata::setCtime(uint32_t ctime) {
    Metadata::ctime_ = ctime;
}

uint32_t Metadata::getUid() const {
    return uid_;
}

void Metadata::setUid(uint32_t uid) {
    Metadata::uid_ = uid;
}

uint32_t Metadata::getGid() const {
    return gid_;
}

void Metadata::setGid(uint32_t gid) {
    Metadata::gid_ = gid;
}

uint32_t Metadata::getMode() const {
    return mode_;
}

void Metadata::setMode(uint32_t mode) {
    Metadata::mode_ = mode;
}

uint64_t Metadata::getInode_no() const {
    return inode_no_;
}

void Metadata::setInode_no(uint64_t inode_no) {
    Metadata::inode_no_ = inode_no;
}

uint32_t Metadata::getLink_count() const {
    return link_count_;
}

void Metadata::setLink_count(uint32_t link_count) {
    Metadata::link_count_ = link_count;
}

uint32_t Metadata::getSize() const {
    return size_;
}

void Metadata::setSize(uint32_t size) {
    Metadata::size_ = size;
}

uint32_t Metadata::getBlocks() const {
    return blocks_;
}

void Metadata::setBlocks(uint32_t blocks) {
    Metadata::blocks_ = blocks;
}
//--------------------------------------------
Metadata::Metadata() {

}

Metadata::Metadata(mode_t mode) { //TODO add initializer list
    init_ACMtime();
    uid_ = fuse_get_context()->uid;
    gid_ = fuse_get_context()->gid;
    mode_ = mode;
    inode_no_ = util::generate_inode_no();
    link_count_ = 0;
    size_ = 0;
    blocks_ = 0;
}

void Metadata::init_ACMtime(void) {
    std::time_t time;
    std::time(&time);
    atime_ = time;
    mtime_ = time;
    ctime_ = time;
}


fs/src/metadata.h

0 → 100644
+74 −0
Original line number Diff line number Diff line

#ifndef FS_METADATA_H
#define FS_METADATA_H

#include "main.h"

//TODO we might want to replace this with GOOGLE PROTOBUF
class Metadata {

private:
    time_t atime_;             // access time. gets updated on file access unless mounted with noatime
    time_t mtime_;             // modify time. gets updated when file content is modified.
    time_t ctime_;             // change time. gets updated when the file attributes are changed AND when file content is modified.
    uint32_t uid_;
    uint32_t gid_;
    uint32_t mode_;
    uint64_t inode_no_;
    uint32_t link_count_;        // number of names for this inode (hardlinks)
    uint32_t size_;              // size_ in bytes, might be computed instead of stored
    uint32_t blocks_;            // allocated file system blocks_

    void init_ACMtime(void);

public:
    Metadata();
    Metadata(mode_t mode);

    //Getter and Setter
    time_t getAtime_() const;

    void setAtime_(time_t atime_);

    time_t getMtime_() const;

    void setMtime_(time_t mtime_);

    time_t getCtime_() const;

    void setCtime_(time_t ctime_);

    void setCtime(uint32_t ctime);

    uint32_t getUid() const;

    void setUid(uint32_t uid);

    uint32_t getGid() const;

    void setGid(uint32_t gid);

    uint32_t getMode() const;

    void setMode(uint32_t mode);

    uint64_t getInode_no() const;

    void setInode_no(uint64_t inode_no);

    uint32_t getLink_count() const;

    void setLink_count(uint32_t link_count);

    uint32_t getSize() const;

    void setSize(uint32_t size);

    uint32_t getBlocks() const;

    void setBlocks(uint32_t blocks);

};


#endif //FS_METADATA_H
Loading