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

Add FileType property to OpenFile class

This allowed to avoid improper static_cast to check downcasting
parent 26fac871
Loading
Loading
Loading
Loading
+3 −1
Original line number Diff line number Diff line
@@ -29,6 +29,7 @@ enum class FileType {

class OpenFile {
protected:
    FileType type_;
    std::string path_;
    std::array<bool, static_cast<int>(OpenFile_flags::flag_count)> flags_ = {false};
    unsigned long pos_;
@@ -38,7 +39,7 @@ protected:
public:
    // multiple threads may want to update the file position if fd has been duplicated by dup()

    OpenFile(const std::string& path, int flags);
    OpenFile(const std::string& path, int flags, FileType type = FileType::regular);

    ~OpenFile();

@@ -55,6 +56,7 @@ public:

    void set_flag(OpenFile_flags flag, bool value);

    FileType type() const;
};


+2 −3
Original line number Diff line number Diff line
@@ -17,9 +17,8 @@ FileType DirEntry::type() {
}


OpenDir::OpenDir(const std::string& path): OpenFile(path, 0) {
    pos_ = 0;
}
OpenDir::OpenDir(const std::string& path) :
    OpenFile(path, 0, FileType::directory) {}


void OpenDir::add(const std::string& name, const FileType& type) {
+10 −8
Original line number Diff line number Diff line
@@ -7,7 +7,10 @@

using namespace std;

OpenFile::OpenFile(const string& path, const int flags) : path_(path) {
OpenFile::OpenFile(const string& path, const int flags, FileType type) :
    type_(type),
    path_(path)
{
    // set flags to OpenFile
    if (flags & O_CREAT)
        flags_[to_underlying(OpenFile_flags::creat)] = true;
@@ -62,6 +65,10 @@ void OpenFile::set_flag(OpenFile_flags flag, bool value) {
    flags_[to_underlying(flag)] = value;
}

FileType OpenFile::type() const {
    return type_;
}

// OpenFileMap starts here

shared_ptr<OpenFile> OpenFileMap::get(int fd) {
@@ -76,12 +83,10 @@ shared_ptr<OpenFile> OpenFileMap::get(int fd) {

shared_ptr<OpenDir> OpenFileMap::get_dir(int dirfd) {
    auto f = get(dirfd);
    if(f == nullptr){
    if (f == nullptr || f->type() != FileType::directory) {
        return nullptr;
    }
    auto open_dir = static_pointer_cast<OpenDir>(f);
    // If open_file is not an OpenDir we are returning nullptr
    return open_dir;
    return static_pointer_cast<OpenDir>(f);
}

bool OpenFileMap::exist(const int fd) {
@@ -186,6 +191,3 @@ int OpenFileMap::get_fd_idx() {
    std::lock_guard<std::mutex> inode_lock(fd_idx_mutex);
    return fd_idx;
}