2018-10-10 12:53:07 +03:00
|
|
|
#pragma once
|
|
|
|
|
2018-10-16 12:21:49 +03:00
|
|
|
#include "DiskDevice.h"
|
2018-10-10 12:53:07 +03:00
|
|
|
#include "InodeIdentifier.h"
|
|
|
|
#include "InodeMetadata.h"
|
2018-10-14 22:19:27 +03:00
|
|
|
#include "Limits.h"
|
2018-10-14 23:57:41 +03:00
|
|
|
#include "UnixTypes.h"
|
2018-10-10 12:53:07 +03:00
|
|
|
#include <AK/ByteBuffer.h>
|
|
|
|
#include <AK/HashMap.h>
|
|
|
|
#include <AK/OwnPtr.h>
|
|
|
|
#include <AK/Retainable.h>
|
|
|
|
#include <AK/RetainPtr.h>
|
2018-12-04 02:27:16 +03:00
|
|
|
#include <AK/AKString.h>
|
2018-10-17 11:55:43 +03:00
|
|
|
#include <AK/Function.h>
|
|
|
|
#include <AK/kstdio.h>
|
2018-10-10 12:53:07 +03:00
|
|
|
|
|
|
|
static const dword mepoch = 476763780;
|
|
|
|
|
2018-12-19 23:18:28 +03:00
|
|
|
class Inode;
|
2018-11-07 13:37:54 +03:00
|
|
|
class FileDescriptor;
|
2018-10-27 01:14:24 +03:00
|
|
|
|
2018-11-15 19:13:10 +03:00
|
|
|
class FS : public Retainable<FS> {
|
2018-10-10 12:53:07 +03:00
|
|
|
public:
|
2018-10-19 12:20:49 +03:00
|
|
|
static void initializeGlobals();
|
2018-11-15 19:13:10 +03:00
|
|
|
virtual ~FS();
|
2018-10-10 12:53:07 +03:00
|
|
|
|
2018-12-03 02:20:00 +03:00
|
|
|
dword id() const { return m_fsid; }
|
|
|
|
static FS* from_fsid(dword);
|
2018-10-10 12:53:07 +03:00
|
|
|
|
|
|
|
virtual bool initialize() = 0;
|
2018-11-15 17:36:35 +03:00
|
|
|
virtual const char* class_name() const = 0;
|
2018-12-03 02:20:00 +03:00
|
|
|
virtual InodeIdentifier root_inode() const = 0;
|
|
|
|
virtual bool write_inode(InodeIdentifier, const ByteBuffer&) = 0;
|
|
|
|
virtual InodeMetadata inode_metadata(InodeIdentifier) const = 0;
|
2018-10-10 12:53:07 +03:00
|
|
|
|
2018-12-03 01:34:50 +03:00
|
|
|
virtual ssize_t read_inode_bytes(InodeIdentifier, Unix::off_t offset, size_t count, byte* buffer, FileDescriptor*) const = 0;
|
2018-10-14 22:19:27 +03:00
|
|
|
|
2018-10-10 12:53:07 +03:00
|
|
|
struct DirectoryEntry {
|
2018-11-13 02:17:30 +03:00
|
|
|
DirectoryEntry(const char* name, InodeIdentifier, byte fileType);
|
2018-12-03 01:34:50 +03:00
|
|
|
DirectoryEntry(const char* name, size_t name_length, InodeIdentifier, byte fileType);
|
2018-11-13 02:17:30 +03:00
|
|
|
char name[256];
|
2018-12-03 01:34:50 +03:00
|
|
|
size_t name_length { 0 };
|
2018-10-10 12:53:07 +03:00
|
|
|
InodeIdentifier inode;
|
|
|
|
byte fileType { 0 };
|
|
|
|
};
|
|
|
|
|
2018-12-19 23:14:55 +03:00
|
|
|
virtual int set_atime_and_mtime(InodeIdentifier, dword atime, dword mtime) = 0;
|
2018-11-18 16:57:41 +03:00
|
|
|
virtual InodeIdentifier create_inode(InodeIdentifier parentInode, const String& name, Unix::mode_t, unsigned size, int& error) = 0;
|
|
|
|
virtual InodeIdentifier create_directory(InodeIdentifier parentInode, const String& name, Unix::mode_t, int& error) = 0;
|
2018-10-10 12:53:07 +03:00
|
|
|
|
2018-11-15 17:10:12 +03:00
|
|
|
virtual InodeIdentifier find_parent_of_inode(InodeIdentifier) const = 0;
|
2018-10-28 14:20:25 +03:00
|
|
|
|
2018-12-19 23:18:28 +03:00
|
|
|
virtual RetainPtr<Inode> get_inode(InodeIdentifier) const = 0;
|
2018-11-13 15:02:39 +03:00
|
|
|
|
2018-12-03 02:20:00 +03:00
|
|
|
ByteBuffer read_entire_inode(InodeIdentifier, FileDescriptor* = nullptr) const;
|
2018-10-10 12:53:07 +03:00
|
|
|
|
|
|
|
protected:
|
2018-11-15 19:13:10 +03:00
|
|
|
FS();
|
2018-10-10 12:53:07 +03:00
|
|
|
|
|
|
|
private:
|
2018-12-03 02:20:00 +03:00
|
|
|
dword m_fsid { 0 };
|
2018-10-10 12:53:07 +03:00
|
|
|
};
|
|
|
|
|
2018-12-19 23:18:28 +03:00
|
|
|
class Inode : public Retainable<Inode> {
|
2018-11-15 16:43:10 +03:00
|
|
|
friend class VFS;
|
2018-11-14 01:44:54 +03:00
|
|
|
public:
|
2018-12-19 23:18:28 +03:00
|
|
|
virtual ~Inode();
|
2018-11-14 01:44:54 +03:00
|
|
|
|
2018-11-15 19:13:10 +03:00
|
|
|
FS& fs() { return m_fs; }
|
|
|
|
const FS& fs() const { return m_fs; }
|
2018-11-14 01:44:54 +03:00
|
|
|
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 18:34:36 +03:00
|
|
|
ByteBuffer read_entire(FileDescriptor* = nullptr);
|
|
|
|
|
2018-12-03 01:34:50 +03:00
|
|
|
virtual ssize_t read_bytes(Unix::off_t, size_t, byte* buffer, FileDescriptor*) = 0;
|
2018-11-15 19:13:10 +03:00
|
|
|
virtual bool traverse_as_directory(Function<bool(const FS::DirectoryEntry&)>) = 0;
|
2018-11-15 18:34:36 +03:00
|
|
|
virtual InodeIdentifier lookup(const String& name) = 0;
|
2018-11-15 19:04:55 +03:00
|
|
|
virtual String reverse_lookup(InodeIdentifier) = 0;
|
2018-11-14 01:44:54 +03:00
|
|
|
|
2018-12-19 23:14:55 +03:00
|
|
|
int set_atime_and_mtime(Unix::time_t atime, Unix::time_t mtime);
|
|
|
|
|
2018-11-14 01:44:54 +03:00
|
|
|
protected:
|
2018-12-19 23:18:28 +03:00
|
|
|
Inode(FS& fs, unsigned index)
|
2018-11-14 01:44:54 +03:00
|
|
|
: m_fs(fs)
|
|
|
|
, m_index(index)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void populate_metadata() const = 0;
|
|
|
|
|
|
|
|
mutable InodeMetadata m_metadata;
|
|
|
|
private:
|
2018-11-15 19:13:10 +03:00
|
|
|
FS& m_fs;
|
2018-11-14 01:44:54 +03:00
|
|
|
unsigned m_index { 0 };
|
|
|
|
};
|
|
|
|
|
2018-12-03 02:20:00 +03:00
|
|
|
inline FS* InodeIdentifier::fs()
|
2018-10-10 12:53:07 +03:00
|
|
|
{
|
2018-12-03 02:20:00 +03:00
|
|
|
return FS::from_fsid(m_fsid);
|
2018-10-10 12:53:07 +03:00
|
|
|
}
|
|
|
|
|
2018-12-03 02:20:00 +03:00
|
|
|
inline const FS* InodeIdentifier::fs() const
|
2018-10-10 12:53:07 +03:00
|
|
|
{
|
2018-12-03 02:20:00 +03:00
|
|
|
return FS::from_fsid(m_fsid);
|
2018-10-10 12:53:07 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
inline InodeMetadata InodeIdentifier::metadata() const
|
|
|
|
{
|
2018-12-03 02:20:00 +03:00
|
|
|
if (!is_valid())
|
2018-10-10 12:53:07 +03:00
|
|
|
return InodeMetadata();
|
2018-12-03 02:20:00 +03:00
|
|
|
return fs()->inode_metadata(*this);
|
2018-10-10 12:53:07 +03:00
|
|
|
}
|
|
|
|
|
2018-12-03 02:20:00 +03:00
|
|
|
inline bool InodeIdentifier::is_root_inode() const
|
2018-10-10 12:53:07 +03:00
|
|
|
{
|
2018-12-03 02:20:00 +03:00
|
|
|
return (*this) == fs()->root_inode();
|
2018-10-10 12:53:07 +03:00
|
|
|
}
|
|
|
|
|
2018-12-19 23:18:28 +03:00
|
|
|
inline unsigned Inode::fsid() const
|
2018-11-13 15:02:39 +03:00
|
|
|
{
|
|
|
|
return m_fs.id();
|
|
|
|
}
|
|
|
|
|
2018-10-10 12:53:07 +03:00
|
|
|
namespace AK {
|
|
|
|
|
|
|
|
template<>
|
|
|
|
struct Traits<InodeIdentifier> {
|
|
|
|
// FIXME: This is a shitty hash.
|
2018-11-15 17:10:12 +03: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 12:53:07 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|