1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 13:57:35 +00:00

Use FileHandle from VFS.

This commit is contained in:
Andreas Kling 2018-10-18 10:28:09 +02:00
parent e86cadc7af
commit 89851a9ded
4 changed files with 24 additions and 105 deletions

View file

@ -1,35 +0,0 @@
#pragma once
#include "types.h"
#include "RefCounted.h"
namespace FileSystem {
void initialize();
class VirtualNode : public RefCounted<VirtualNode> {
public:
DWORD saneValue = 0x850209;
virtual ~VirtualNode();
DWORD index() const { return m_index; }
const String& path() const { return m_path; }
virtual size_t size() const = 0;
virtual uid_t uid() const = 0;
virtual gid_t gid() const = 0;
virtual size_t mode() const = 0;
virtual size_t read(BYTE* outbuf, size_t start, size_t maxLength) = 0;
protected:
VirtualNode(DWORD index, String&& path);
private:
DWORD m_index { 0 };
String m_path;
};
RefPtr<VirtualNode> createVirtualNode(String&& path);
}

View file

@ -5,7 +5,8 @@
#include "StdLib.h" #include "StdLib.h"
#include "i386.h" #include "i386.h"
#include "system.h" #include "system.h"
#include "FileSystem.h" #include <VirtualFileSystem/FileHandle.h>
#include <VirtualFileSystem/VirtualFileSystem.h>
#include "MemoryManager.h" #include "MemoryManager.h"
Task* current; Task* current;
@ -343,76 +344,35 @@ Task* Task::fromIPCHandle(IPC::Handle handle)
return nullptr; return nullptr;
} }
class FileHandle {
public:
FileHandle() { }
~FileHandle() { }
int seek(int offset);
size_t read(void* buffer, int bufferSize);
int fd() const { return m_fd; }
static FileHandle* fromFileDescriptor(int fd);
//private:
RefPtr<FileSystem::VirtualNode> m_vnode;
int m_fd { -1 };
size_t m_offset { 0 };
};
size_t FileHandle::read(void* buffer, int bufferSize)
{
Task::checkSanity("FileHandle::read");
size_t nread = m_vnode->read((BYTE*)buffer, m_offset, bufferSize);
m_offset += nread;
return nread;
}
int FileHandle::seek(int offset)
{
if (!m_vnode)
return -1;
ASSERT(offset >= 0);
if ((unsigned)offset >= m_vnode->size())
return -1;
m_offset = offset;
return m_offset;
}
FileHandle* FileHandle::fromFileDescriptor(int fd)
{
return current->fileHandleIfExists(fd);
}
FileHandle* Task::fileHandleIfExists(int fd) FileHandle* Task::fileHandleIfExists(int fd)
{ {
if (fd < 0) if (fd < 0)
return nullptr; return nullptr;
if ((unsigned)fd < m_fileHandles.size()) if ((unsigned)fd < m_fileHandles.size())
return m_fileHandles[fd]; return m_fileHandles[fd].ptr();
return nullptr; return nullptr;
} }
int Task::sys$seek(int fd, int offset) int Task::sys$seek(int fd, int offset)
{ {
auto* handle = FileHandle::fromFileDescriptor(fd); auto* handle = fileHandleIfExists(fd);
if (!handle) if (!handle)
return -1; return -1;
return handle->seek(offset); return handle->seek(offset, SEEK_SET);
} }
int Task::sys$read(int fd, void* outbuf, size_t nread) ssize_t Task::sys$read(int fd, void* outbuf, size_t nread)
{ {
Task::checkSanity("Task::sys$read"); Task::checkSanity("Task::sys$read");
kprintf("Task::sys$read: called(%d, %p, %u)\n", fd, outbuf, nread); kprintf("Task::sys$read: called(%d, %p, %u)\n", fd, outbuf, nread);
auto* handle = FileHandle::fromFileDescriptor(fd); auto* handle = fileHandleIfExists(fd);
kprintf("Task::sys$read: handle=%p\n", handle); kprintf("Task::sys$read: handle=%p\n", handle);
if (!handle) { if (!handle) {
kprintf("Task::sys$read: handle not found :(\n"); kprintf("Task::sys$read: handle not found :(\n");
return -1; return -1;
} }
kprintf("call read on handle=%p\n", handle); kprintf("call read on handle=%p\n", handle);
nread = handle->read(outbuf, nread); nread = handle->read((byte*)outbuf, nread);
kprintf("called read\n"); kprintf("called read\n");
kprintf("Task::sys$read: nread=%u\n", nread); kprintf("Task::sys$read: nread=%u\n", nread);
return nread; return nread;
@ -420,9 +380,10 @@ int Task::sys$read(int fd, void* outbuf, size_t nread)
int Task::sys$close(int fd) int Task::sys$close(int fd)
{ {
auto* handle = FileHandle::fromFileDescriptor(fd); auto* handle = fileHandleIfExists(fd);
if (!handle) if (!handle)
return -1; return -1;
// FIXME: Implement.
return 0; return 0;
} }
@ -438,25 +399,17 @@ int Task::sys$open(const char* path, size_t pathLength)
FileHandle* Task::openFile(String&& path) FileHandle* Task::openFile(String&& path)
{ {
#if 0 kprintf("calling vfs::open with vfs=%p, path='%s'\n", &VirtualFileSystem::the(), path.characters());
auto vnode = FileSystem::createVirtualNode(move(path)); auto handle = VirtualFileSystem::the().open(move(path));
if (!vnode) { if (!handle) {
kprintf("createVirtualNode failed\n"); kprintf("vfs::open() failed\n");
return nullptr; return nullptr;
} }
#endif handle->setFD(m_fileHandles.size());
#if 0 kprintf("vfs::open() worked! handle=%p, fd=%d\n", handle.ptr(), handle->fd());
FileHandle* fh = new FileHandle; m_fileHandles.append(move(handle)); // FIXME: allow non-move Vector::append
kprintf("made new FileHandle\n"); kprintf("Task::openFile(): FileHandle{%p} fd=%d\n", handle.ptr(), handle->fd());
fh->m_fd = m_fileHandles.size(); return m_fileHandles.last().ptr();
kprintf(" fd = %d\n", fh->m_fd);
fh->m_vnode = move(vnode);
kprintf(" vnode = %p\n", fh->m_vnode.ptr());
m_fileHandles.append(move(fh)); // FIXME: allow non-move Vector::append
kprintf("Task::openFile(): FileHandle{%p} fd=%d\n", fh, fh->m_fd);
return fh;
#endif
return nullptr;
} }
int Task::sys$kill(pid_t pid, int sig) int Task::sys$kill(pid_t pid, int sig)

View file

@ -57,7 +57,6 @@ public:
const FarPtr& farPtr() const { return m_farPtr; } const FarPtr& farPtr() const { return m_farPtr; }
FileHandle* fileHandleIfExists(int fd); FileHandle* fileHandleIfExists(int fd);
FileHandle* createFileHandle();
bool acceptsMessageFrom(Task&); bool acceptsMessageFrom(Task&);
@ -118,7 +117,7 @@ private:
DWORD m_wakeupTime { 0 }; DWORD m_wakeupTime { 0 };
TSS32 m_tss; TSS32 m_tss;
Descriptor* m_ldtEntries { nullptr }; Descriptor* m_ldtEntries { nullptr };
Vector<FileHandle*> m_fileHandles; Vector<OwnPtr<FileHandle>> m_fileHandles;
RingLevel m_ring { Ring0 }; RingLevel m_ring { Ring0 };
int m_error { 0 }; int m_error { 0 };
}; };

View file

@ -153,11 +153,12 @@ void init()
vfs->mountRoot(e2fs.copyRef()); vfs->mountRoot(e2fs.copyRef());
// new Task(motd_main, "motd", IPC::Handle::MotdTask, Task::Ring0); //new Task(motd_main, "motd", IPC::Handle::MotdTask, Task::Ring0);
new Task(user_main, "user", IPC::Handle::UserTask, Task::Ring3); new Task(user_main, "user", IPC::Handle::UserTask, Task::Ring3);
vfs->listDirectory("/"); //vfs->listDirectory("/");
#if 1
{ {
auto motdFile = vfs->open("/motd.txt"); auto motdFile = vfs->open("/motd.txt");
ASSERT(motdFile); ASSERT(motdFile);
@ -167,6 +168,7 @@ void init()
kprintf("%c", motdData[i]); kprintf("%c", motdData[i]);
} }
} }
#endif
// The idle task will spend its eternity here for now. // The idle task will spend its eternity here for now.
for (;;) { for (;;) {