2018-10-10 09:53:07 +00:00
|
|
|
#pragma once
|
|
|
|
|
2018-10-16 09:21:49 +00:00
|
|
|
#include "DiskDevice.h"
|
2018-10-10 09:53:07 +00:00
|
|
|
#include "InodeIdentifier.h"
|
|
|
|
#include "InodeMetadata.h"
|
2018-10-14 19:19:27 +00:00
|
|
|
#include "Limits.h"
|
2018-10-14 20:57:41 +00:00
|
|
|
#include "UnixTypes.h"
|
2018-10-10 09:53:07 +00:00
|
|
|
#include <AK/ByteBuffer.h>
|
|
|
|
#include <AK/HashMap.h>
|
|
|
|
#include <AK/OwnPtr.h>
|
|
|
|
#include <AK/Retainable.h>
|
|
|
|
#include <AK/RetainPtr.h>
|
|
|
|
#include <AK/String.h>
|
2018-10-17 08:55:43 +00:00
|
|
|
#include <AK/Function.h>
|
|
|
|
#include <AK/kstdio.h>
|
2018-10-10 09:53:07 +00:00
|
|
|
|
|
|
|
static const dword mepoch = 476763780;
|
|
|
|
|
2018-11-13 22:44:54 +00:00
|
|
|
class CoreInode;
|
2018-11-07 10:37:54 +00:00
|
|
|
class FileDescriptor;
|
2018-10-26 22:14:24 +00:00
|
|
|
|
2018-10-10 09:53:07 +00:00
|
|
|
class FileSystem : public Retainable<FileSystem> {
|
|
|
|
public:
|
2018-10-19 09:20:49 +00:00
|
|
|
static void initializeGlobals();
|
2018-10-10 09:53:07 +00:00
|
|
|
virtual ~FileSystem();
|
|
|
|
|
|
|
|
dword id() const { return m_id; }
|
|
|
|
static FileSystem* fromID(dword);
|
|
|
|
|
|
|
|
virtual bool initialize() = 0;
|
2018-11-15 14:36:35 +00:00
|
|
|
virtual const char* class_name() const = 0;
|
2018-10-10 09:53:07 +00:00
|
|
|
virtual InodeIdentifier rootInode() const = 0;
|
|
|
|
virtual bool writeInode(InodeIdentifier, const ByteBuffer&) = 0;
|
|
|
|
virtual InodeMetadata inodeMetadata(InodeIdentifier) const = 0;
|
|
|
|
|
2018-11-15 14:36:35 +00:00
|
|
|
virtual Unix::ssize_t read_inode_bytes(InodeIdentifier, Unix::off_t offset, Unix::size_t count, byte* buffer, FileDescriptor*) const = 0;
|
2018-10-14 19:19:27 +00:00
|
|
|
|
2018-10-10 09:53:07 +00:00
|
|
|
struct DirectoryEntry {
|
2018-11-12 23:17:30 +00:00
|
|
|
DirectoryEntry(const char* name, InodeIdentifier, byte fileType);
|
|
|
|
DirectoryEntry(const char* name, Unix::size_t name_length, InodeIdentifier, byte fileType);
|
|
|
|
char name[256];
|
|
|
|
Unix::size_t name_length { 0 };
|
2018-10-10 09:53:07 +00:00
|
|
|
InodeIdentifier inode;
|
|
|
|
byte fileType { 0 };
|
|
|
|
};
|
|
|
|
|
2018-11-15 14:36:35 +00:00
|
|
|
virtual bool set_mtime(InodeIdentifier, dword timestamp) = 0;
|
|
|
|
virtual InodeIdentifier create_inode(InodeIdentifier parentInode, const String& name, Unix::mode_t, unsigned size) = 0;
|
|
|
|
virtual InodeIdentifier create_directory(InodeIdentifier parentInode, const String& name, Unix::mode_t) = 0;
|
2018-10-10 09:53:07 +00:00
|
|
|
|
2018-11-15 14:10:12 +00:00
|
|
|
virtual InodeIdentifier find_parent_of_inode(InodeIdentifier) const = 0;
|
2018-10-28 11:20:25 +00:00
|
|
|
|
2018-11-13 22:44:54 +00:00
|
|
|
virtual RetainPtr<CoreInode> get_inode(InodeIdentifier) const = 0;
|
2018-11-13 12:02:39 +00:00
|
|
|
|
2018-11-07 10:37:54 +00:00
|
|
|
ByteBuffer readEntireInode(InodeIdentifier, FileDescriptor* = nullptr) const;
|
2018-10-10 09:53:07 +00:00
|
|
|
|
|
|
|
protected:
|
|
|
|
FileSystem();
|
|
|
|
|
|
|
|
private:
|
|
|
|
dword m_id { 0 };
|
|
|
|
};
|
|
|
|
|
2018-11-13 22:44:54 +00:00
|
|
|
class CoreInode : public Retainable<CoreInode> {
|
2018-11-15 13:43:10 +00:00
|
|
|
friend class VFS;
|
2018-11-13 22:44:54 +00:00
|
|
|
public:
|
|
|
|
virtual ~CoreInode();
|
|
|
|
|
|
|
|
FileSystem& fs() { return m_fs; }
|
|
|
|
const FileSystem& fs() const { return m_fs; }
|
|
|
|
unsigned fsid() const;
|
|
|
|
unsigned index() const { return m_index; }
|
|
|
|
|
|
|
|
size_t size() const { return metadata().size; }
|
|
|
|
bool is_symlink() const { return metadata().isSymbolicLink(); }
|
|
|
|
bool is_directory() const { return metadata().isDirectory(); }
|
|
|
|
|
|
|
|
InodeIdentifier identifier() const { return { fsid(), index() }; }
|
|
|
|
const InodeMetadata& metadata() const { if (!m_metadata.isValid()) { populate_metadata(); } return m_metadata; }
|
|
|
|
|
2018-11-15 15:34:36 +00:00
|
|
|
ByteBuffer read_entire(FileDescriptor* = nullptr);
|
|
|
|
|
2018-11-13 22:44:54 +00:00
|
|
|
virtual Unix::ssize_t read_bytes(Unix::off_t, Unix::size_t, byte* buffer, FileDescriptor*) = 0;
|
|
|
|
virtual bool traverse_as_directory(Function<bool(const FileSystem::DirectoryEntry&)>) = 0;
|
2018-11-15 15:34:36 +00:00
|
|
|
virtual InodeIdentifier lookup(const String& name) = 0;
|
2018-11-15 16:04:55 +00:00
|
|
|
virtual String reverse_lookup(InodeIdentifier) = 0;
|
2018-11-13 22:44:54 +00:00
|
|
|
|
|
|
|
protected:
|
|
|
|
CoreInode(FileSystem& fs, unsigned index)
|
|
|
|
: m_fs(fs)
|
|
|
|
, m_index(index)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void populate_metadata() const = 0;
|
|
|
|
|
|
|
|
mutable InodeMetadata m_metadata;
|
|
|
|
private:
|
|
|
|
FileSystem& m_fs;
|
|
|
|
unsigned m_index { 0 };
|
|
|
|
};
|
|
|
|
|
2018-10-10 09:53:07 +00:00
|
|
|
inline FileSystem* InodeIdentifier::fileSystem()
|
|
|
|
{
|
|
|
|
return FileSystem::fromID(m_fileSystemID);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline const FileSystem* InodeIdentifier::fileSystem() const
|
|
|
|
{
|
|
|
|
return FileSystem::fromID(m_fileSystemID);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline InodeMetadata InodeIdentifier::metadata() const
|
|
|
|
{
|
|
|
|
if (!isValid())
|
|
|
|
return InodeMetadata();
|
|
|
|
return fileSystem()->inodeMetadata(*this);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline bool InodeIdentifier::isRootInode() const
|
|
|
|
{
|
|
|
|
return (*this) == fileSystem()->rootInode();
|
|
|
|
}
|
|
|
|
|
2018-11-13 12:02:39 +00:00
|
|
|
inline unsigned CoreInode::fsid() const
|
|
|
|
{
|
|
|
|
return m_fs.id();
|
|
|
|
}
|
|
|
|
|
2018-10-10 09:53:07 +00:00
|
|
|
namespace AK {
|
|
|
|
|
|
|
|
template<>
|
|
|
|
struct Traits<InodeIdentifier> {
|
|
|
|
// FIXME: This is a shitty hash.
|
2018-11-15 14:10:12 +00:00
|
|
|
static unsigned hash(const InodeIdentifier& inode) { return Traits<unsigned>::hash(inode.fsid()) + Traits<unsigned>::hash(inode.index()); }
|
|
|
|
static void dump(const InodeIdentifier& inode) { kprintf("%02u:%08u", inode.fsid(), inode.index()); }
|
2018-10-10 09:53:07 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|