2018-10-10 11:53:07 +02:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <AK/HashMap.h>
|
|
|
|
#include <AK/OwnPtr.h>
|
|
|
|
#include <AK/RetainPtr.h>
|
|
|
|
#include <AK/String.h>
|
|
|
|
#include <AK/Vector.h>
|
2018-10-24 12:43:52 +02:00
|
|
|
#include <AK/Function.h>
|
2018-10-10 11:53:07 +02:00
|
|
|
#include "InodeIdentifier.h"
|
2018-10-24 12:43:52 +02:00
|
|
|
#include "InodeMetadata.h"
|
2018-10-14 21:19:27 +02:00
|
|
|
#include "Limits.h"
|
2018-10-24 12:43:52 +02:00
|
|
|
#include "FileSystem.h"
|
2018-10-10 11:53:07 +02:00
|
|
|
|
2018-10-28 14:11:51 +01:00
|
|
|
#define O_RDONLY 0
|
|
|
|
#define O_WRONLY 1
|
|
|
|
#define O_RDWR 2
|
|
|
|
#define O_DIRECTORY 00200000
|
|
|
|
#define O_NOFOLLOW 00400000
|
|
|
|
#define O_NOFOLLOW_NOERROR 0x4000000
|
|
|
|
|
2018-10-14 02:59:22 +02:00
|
|
|
class CharacterDevice;
|
2018-11-07 11:37:54 +01:00
|
|
|
class FileDescriptor;
|
2018-10-10 11:53:07 +02:00
|
|
|
|
2018-10-30 13:59:29 +01:00
|
|
|
inline constexpr dword encodedDevice(unsigned major, unsigned minor)
|
|
|
|
{
|
|
|
|
return (minor & 0xff) | (major << 8) | ((minor & ~0xff) << 12);
|
|
|
|
}
|
|
|
|
|
2018-10-10 11:53:07 +02:00
|
|
|
class VirtualFileSystem {
|
2018-10-31 23:19:15 +01:00
|
|
|
AK_MAKE_ETERNAL
|
2018-10-10 11:53:07 +02:00
|
|
|
public:
|
2018-10-19 11:20:49 +02:00
|
|
|
static void initializeGlobals();
|
|
|
|
|
2018-10-26 18:43:25 +02:00
|
|
|
class Mount {
|
|
|
|
public:
|
|
|
|
Mount(InodeIdentifier host, RetainPtr<FileSystem>&&);
|
|
|
|
|
|
|
|
InodeIdentifier host() const { return m_host; }
|
|
|
|
InodeIdentifier guest() const { return m_guest; }
|
|
|
|
|
|
|
|
const FileSystem& fileSystem() const { return *m_fileSystem; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
InodeIdentifier m_host;
|
|
|
|
InodeIdentifier m_guest;
|
|
|
|
RetainPtr<FileSystem> m_fileSystem;
|
|
|
|
};
|
|
|
|
|
2018-10-10 11:53:07 +02:00
|
|
|
struct Node {
|
|
|
|
InodeIdentifier inode;
|
2018-10-24 12:43:52 +02:00
|
|
|
const InodeMetadata& metadata() const;
|
2018-10-10 11:53:07 +02:00
|
|
|
|
2018-10-30 15:33:37 +01:00
|
|
|
bool inUse() const { return inode.isValid() || m_characterDevice; }
|
2018-10-10 11:53:07 +02:00
|
|
|
|
2018-10-14 02:59:22 +02:00
|
|
|
bool isCharacterDevice() const { return m_characterDevice; }
|
|
|
|
CharacterDevice* characterDevice() { return m_characterDevice; }
|
2018-10-30 22:03:02 +01:00
|
|
|
const CharacterDevice* characterDevice() const { return m_characterDevice; }
|
2018-10-14 02:59:22 +02:00
|
|
|
|
2018-10-10 11:53:07 +02:00
|
|
|
void retain();
|
|
|
|
void release();
|
|
|
|
|
|
|
|
FileSystem* fileSystem() { return inode.fileSystem(); }
|
|
|
|
const FileSystem* fileSystem() const { return inode.fileSystem(); }
|
|
|
|
|
2018-10-24 12:43:52 +02:00
|
|
|
VirtualFileSystem* vfs() { return m_vfs; }
|
|
|
|
const VirtualFileSystem* vfs() const { return m_vfs; }
|
|
|
|
|
2018-10-10 11:53:07 +02:00
|
|
|
private:
|
|
|
|
friend class VirtualFileSystem;
|
2018-10-24 12:43:52 +02:00
|
|
|
VirtualFileSystem* m_vfs { nullptr };
|
2018-10-10 11:53:07 +02:00
|
|
|
unsigned retainCount { 0 };
|
2018-10-14 02:59:22 +02:00
|
|
|
CharacterDevice* m_characterDevice { nullptr };
|
2018-10-24 12:43:52 +02:00
|
|
|
mutable InodeMetadata m_cachedMetadata;
|
2018-10-10 11:53:07 +02:00
|
|
|
};
|
|
|
|
|
2018-10-26 18:43:25 +02:00
|
|
|
static VirtualFileSystem& the() PURE;
|
2018-10-18 10:27:07 +02:00
|
|
|
|
2018-10-10 11:53:07 +02:00
|
|
|
VirtualFileSystem();
|
|
|
|
~VirtualFileSystem();
|
|
|
|
|
2018-10-28 12:20:25 +01:00
|
|
|
bool isDirectory(const String& path, InodeIdentifier base = InodeIdentifier());
|
2018-10-10 11:53:07 +02:00
|
|
|
void listDirectory(const String& path);
|
|
|
|
void listDirectoryRecursively(const String& path);
|
|
|
|
|
|
|
|
unsigned maxNodeCount() const { return m_maxNodeCount; }
|
|
|
|
unsigned allocatedNodeCount() const { return m_maxNodeCount - m_nodeFreeList.size(); }
|
|
|
|
|
|
|
|
Node* root() { return m_rootNode.ptr(); }
|
|
|
|
const Node* root() const { return m_rootNode.ptr(); }
|
|
|
|
|
|
|
|
bool mountRoot(RetainPtr<FileSystem>&&);
|
|
|
|
bool mount(RetainPtr<FileSystem>&&, const String& path);
|
|
|
|
|
2018-11-07 11:37:54 +01:00
|
|
|
RetainPtr<FileDescriptor> open(CharacterDevice&, int options);
|
|
|
|
RetainPtr<FileDescriptor> open(const String& path, int& error, int options = 0, InodeIdentifier base = InodeIdentifier());
|
|
|
|
RetainPtr<FileDescriptor> create(const String& path, InodeIdentifier base = InodeIdentifier());
|
|
|
|
RetainPtr<FileDescriptor> mkdir(const String& path, InodeIdentifier base = InodeIdentifier());
|
2018-10-10 11:53:07 +02:00
|
|
|
|
|
|
|
bool isRoot(InodeIdentifier) const;
|
|
|
|
|
|
|
|
bool touch(const String&path);
|
|
|
|
|
2018-10-30 13:59:29 +01:00
|
|
|
void registerCharacterDevice(CharacterDevice&);
|
2018-10-14 02:59:22 +02:00
|
|
|
|
2018-10-26 18:43:25 +02:00
|
|
|
size_t mountCount() const { return m_mounts.size(); }
|
|
|
|
void forEachMount(Function<void(const Mount&)>) const;
|
|
|
|
|
2018-10-28 12:20:25 +01:00
|
|
|
String absolutePath(InodeIdentifier);
|
|
|
|
|
2018-10-10 11:53:07 +02:00
|
|
|
private:
|
2018-11-07 11:37:54 +01:00
|
|
|
friend class FileDescriptor;
|
2018-10-24 12:43:52 +02:00
|
|
|
|
|
|
|
void enumerateDirectoryInode(InodeIdentifier, Function<bool(const FileSystem::DirectoryEntry&)>);
|
2018-10-29 22:43:39 +01:00
|
|
|
InodeIdentifier resolvePath(const String& path, int& error, InodeIdentifier base = InodeIdentifier(), int options = 0);
|
|
|
|
InodeIdentifier resolveSymbolicLink(InodeIdentifier base, InodeIdentifier symlinkInode, int& error);
|
2018-10-10 11:53:07 +02:00
|
|
|
|
|
|
|
RetainPtr<Node> allocateNode();
|
|
|
|
void freeNode(Node*);
|
|
|
|
|
|
|
|
RetainPtr<Node> makeNode(InodeIdentifier);
|
2018-10-30 15:33:37 +01:00
|
|
|
RetainPtr<Node> makeNode(CharacterDevice&);
|
2018-10-10 11:53:07 +02:00
|
|
|
RetainPtr<Node> getOrCreateNode(InodeIdentifier);
|
2018-10-30 15:33:37 +01:00
|
|
|
RetainPtr<Node> getOrCreateNode(CharacterDevice&);
|
2018-10-10 11:53:07 +02:00
|
|
|
|
|
|
|
Mount* findMountForHost(InodeIdentifier);
|
|
|
|
Mount* findMountForGuest(InodeIdentifier);
|
|
|
|
|
|
|
|
HashMap<InodeIdentifier, Node*> m_inode2vnode;
|
2018-10-14 02:59:22 +02:00
|
|
|
HashMap<dword, Node*> m_device2vnode;
|
2018-10-10 11:53:07 +02:00
|
|
|
|
|
|
|
Vector<OwnPtr<Mount>> m_mounts;
|
|
|
|
|
|
|
|
unsigned m_maxNodeCount { 0 };
|
|
|
|
Node* m_nodes { nullptr };
|
|
|
|
|
|
|
|
Vector<Node*> m_nodeFreeList;
|
|
|
|
|
|
|
|
RetainPtr<Node> m_rootNode;
|
2018-10-14 02:59:22 +02:00
|
|
|
|
|
|
|
HashMap<dword, CharacterDevice*> m_characterDevices;
|
2018-10-10 11:53:07 +02:00
|
|
|
};
|
|
|
|
|