Newer
Older
#ifndef IFS_OPEN_FILE_MAP_HPP
#define IFS_OPEN_FILE_MAP_HPP
#include <map>
#include <mutex>
#include <memory>
enum class OpenFile_flags {
append = 0,
creat,
trunc,
rdonly,
wronly,
rdwr,
flag_count // this is purely used as a size variable of this enum class
};
std::string path_;
std::array<bool, static_cast<int>(OpenFile_flags::flag_count)> flags_ = {false};
std::mutex pos_mutex_;
std::mutex flag_mutex_;
// multiple threads may want to update the file position if fd has been duplicated by dup()
OpenFile(const std::string& path, int flags);
std::string path() const;
void path(const std::string& path_);
const bool get_flag(OpenFile_flags flag);
void set_flag(OpenFile_flags flag, bool value);
std::map<int, std::shared_ptr<OpenFile>> files_;
std::recursive_mutex files_mutex_;
int safe_generate_fd_idx_();
std::shared_ptr<OpenFile> get(int fd);
int add(std::string path, int flags);
int dup(int oldfd);
int dup2(int oldfd, int newfd);