Compare commits
2 Commits
0d54d0ece1
...
0847cfcb65
Author | SHA1 | Date | |
---|---|---|---|
0847cfcb65 | |||
8eb4d693ac |
@ -33,6 +33,7 @@ set(SOURCES
|
|||||||
src/sys/mkdir.cpp
|
src/sys/mkdir.cpp
|
||||||
src/sys/mknod.cpp
|
src/sys/mknod.cpp
|
||||||
src/sys/waitpid.cpp
|
src/sys/waitpid.cpp
|
||||||
|
src/sys/getdents.cpp
|
||||||
src/fs/VFS.cpp
|
src/fs/VFS.cpp
|
||||||
src/fs/tmpfs/FileSystem.cpp
|
src/fs/tmpfs/FileSystem.cpp
|
||||||
src/fs/devices/DeviceRegistry.cpp
|
src/fs/devices/DeviceRegistry.cpp
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
#include <luna/SharedPtr.h>
|
#include <luna/SharedPtr.h>
|
||||||
|
#include <luna/StaticString.h>
|
||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
|
|
||||||
namespace VFS
|
namespace VFS
|
||||||
@ -12,6 +13,14 @@ namespace VFS
|
|||||||
};
|
};
|
||||||
|
|
||||||
class FileSystem;
|
class FileSystem;
|
||||||
|
class Inode;
|
||||||
|
|
||||||
|
struct DirectoryEntry
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
SharedPtr<Inode> inode;
|
||||||
|
StaticString<128> name;
|
||||||
|
};
|
||||||
|
|
||||||
class Inode
|
class Inode
|
||||||
{
|
{
|
||||||
@ -19,6 +28,8 @@ namespace VFS
|
|||||||
// Directory-specific methods
|
// Directory-specific methods
|
||||||
virtual Result<SharedPtr<Inode>> find(const char* name) const = 0;
|
virtual Result<SharedPtr<Inode>> find(const char* name) const = 0;
|
||||||
|
|
||||||
|
virtual Option<DirectoryEntry> get(usize index) const = 0;
|
||||||
|
|
||||||
virtual Result<SharedPtr<Inode>> create_file(const char* name) = 0;
|
virtual Result<SharedPtr<Inode>> create_file(const char* name) = 0;
|
||||||
|
|
||||||
virtual Result<SharedPtr<Inode>> create_subdirectory(const char* name) = 0;
|
virtual Result<SharedPtr<Inode>> create_subdirectory(const char* name) = 0;
|
||||||
@ -60,6 +71,11 @@ namespace VFS
|
|||||||
return err(ENOTDIR);
|
return err(ENOTDIR);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Option<DirectoryEntry> get(usize) const override
|
||||||
|
{
|
||||||
|
return {};
|
||||||
|
}
|
||||||
|
|
||||||
Result<SharedPtr<Inode>> create_file(const char*) override
|
Result<SharedPtr<Inode>> create_file(const char*) override
|
||||||
{
|
{
|
||||||
return err(ENOTDIR);
|
return err(ENOTDIR);
|
||||||
@ -96,6 +112,11 @@ namespace VFS
|
|||||||
return err(ENOTDIR);
|
return err(ENOTDIR);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Option<DirectoryEntry> get(usize) const override
|
||||||
|
{
|
||||||
|
return {};
|
||||||
|
}
|
||||||
|
|
||||||
Result<SharedPtr<Inode>> create_file(const char*) override
|
Result<SharedPtr<Inode>> create_file(const char*) override
|
||||||
{
|
{
|
||||||
return err(ENOTDIR);
|
return err(ENOTDIR);
|
||||||
|
@ -63,9 +63,18 @@ namespace TmpFS
|
|||||||
return err(ENOENT);
|
return err(ENOENT);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Option<VFS::DirectoryEntry> DirInode::get(usize index) const
|
||||||
|
{
|
||||||
|
if (index >= m_entries.size()) return {};
|
||||||
|
|
||||||
|
return m_entries[index];
|
||||||
|
}
|
||||||
|
|
||||||
Result<void> DirInode::add_entry(SharedPtr<VFS::Inode> inode, const char* name)
|
Result<void> DirInode::add_entry(SharedPtr<VFS::Inode> inode, const char* name)
|
||||||
{
|
{
|
||||||
Entry entry { inode, name };
|
if (find(name).has_value()) return err(EEXIST);
|
||||||
|
|
||||||
|
VFS::DirectoryEntry entry { inode, name };
|
||||||
|
|
||||||
TRY(m_entries.try_append(move(entry)));
|
TRY(m_entries.try_append(move(entry)));
|
||||||
|
|
||||||
@ -74,8 +83,6 @@ namespace TmpFS
|
|||||||
|
|
||||||
Result<SharedPtr<VFS::Inode>> DirInode::create_file(const char* name)
|
Result<SharedPtr<VFS::Inode>> DirInode::create_file(const char* name)
|
||||||
{
|
{
|
||||||
if (find(name).has_value()) return err(EEXIST);
|
|
||||||
|
|
||||||
auto inode = TRY(m_fs->create_file_inode());
|
auto inode = TRY(m_fs->create_file_inode());
|
||||||
|
|
||||||
TRY(add_entry(inode, name));
|
TRY(add_entry(inode, name));
|
||||||
@ -85,8 +92,6 @@ namespace TmpFS
|
|||||||
|
|
||||||
Result<SharedPtr<VFS::Inode>> DirInode::create_subdirectory(const char* name)
|
Result<SharedPtr<VFS::Inode>> DirInode::create_subdirectory(const char* name)
|
||||||
{
|
{
|
||||||
if (find(name).has_value()) return err(EEXIST);
|
|
||||||
|
|
||||||
auto inode = TRY(m_fs->create_dir_inode(m_self));
|
auto inode = TRY(m_fs->create_dir_inode(m_self));
|
||||||
|
|
||||||
TRY(add_entry(inode, name));
|
TRY(add_entry(inode, name));
|
||||||
|
@ -185,6 +185,7 @@ namespace TmpFS
|
|||||||
}
|
}
|
||||||
|
|
||||||
Result<SharedPtr<VFS::Inode>> find(const char* name) const override;
|
Result<SharedPtr<VFS::Inode>> find(const char* name) const override;
|
||||||
|
Option<VFS::DirectoryEntry> get(usize index) const override;
|
||||||
|
|
||||||
Result<usize> read(u8*, usize, usize) const override
|
Result<usize> read(u8*, usize, usize) const override
|
||||||
{
|
{
|
||||||
@ -251,12 +252,6 @@ namespace TmpFS
|
|||||||
|
|
||||||
SharedPtr<VFS::Inode> m_self;
|
SharedPtr<VFS::Inode> m_self;
|
||||||
|
|
||||||
struct Entry
|
Vector<VFS::DirectoryEntry> m_entries;
|
||||||
{
|
|
||||||
SharedPtr<VFS::Inode> inode;
|
|
||||||
StaticString<128> name;
|
|
||||||
};
|
|
||||||
|
|
||||||
Vector<Entry> m_entries;
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
40
kernel/src/sys/getdents.cpp
Normal file
40
kernel/src/sys/getdents.cpp
Normal file
@ -0,0 +1,40 @@
|
|||||||
|
#include "fs/VFS.h"
|
||||||
|
#include "memory/MemoryManager.h"
|
||||||
|
#include "sys/Syscall.h"
|
||||||
|
#include "thread/Scheduler.h"
|
||||||
|
#include <bits/getdents.h>
|
||||||
|
|
||||||
|
Result<u64> sys_getdents(Registers*, SyscallArgs args)
|
||||||
|
{
|
||||||
|
int fd = (int)args[0];
|
||||||
|
luna_dirent* ent = (luna_dirent*)args[1];
|
||||||
|
usize count = (usize)args[2];
|
||||||
|
|
||||||
|
Thread* current = Scheduler::current();
|
||||||
|
|
||||||
|
auto& descriptor = *TRY(current->resolve_fd(fd));
|
||||||
|
|
||||||
|
if (descriptor.inode->type() != VFS::InodeType::Directory) return err(ENOTDIR);
|
||||||
|
|
||||||
|
usize nwrite = 0;
|
||||||
|
while (nwrite < count)
|
||||||
|
{
|
||||||
|
auto maybe_entry = descriptor.inode->get(descriptor.offset);
|
||||||
|
if (!maybe_entry.has_value()) break;
|
||||||
|
|
||||||
|
descriptor.offset++;
|
||||||
|
|
||||||
|
auto entry = maybe_entry.release_value();
|
||||||
|
|
||||||
|
luna_dirent kent;
|
||||||
|
kent.inode = entry.inode->inode_number();
|
||||||
|
strlcpy(kent.name, entry.name.chars(), entry.name.length() + 1);
|
||||||
|
|
||||||
|
if (!MemoryManager::copy_to_user_typed(ent, &kent)) return err(EFAULT);
|
||||||
|
|
||||||
|
ent++;
|
||||||
|
nwrite++;
|
||||||
|
}
|
||||||
|
|
||||||
|
return nwrite;
|
||||||
|
}
|
@ -24,8 +24,6 @@ Result<u64> sys_mknod(Registers*, SyscallArgs args)
|
|||||||
|
|
||||||
auto parent = TRY(VFS::resolve_path(dirname.chars()));
|
auto parent = TRY(VFS::resolve_path(dirname.chars()));
|
||||||
|
|
||||||
if (parent->find(basename.chars()).has_value()) return err(EEXIST);
|
|
||||||
|
|
||||||
auto inode = TRY(parent->fs().create_device_inode(maj, min));
|
auto inode = TRY(parent->fs().create_device_inode(maj, min));
|
||||||
TRY(parent->add_entry(inode, basename.chars()));
|
TRY(parent->add_entry(inode, basename.chars()));
|
||||||
|
|
||||||
|
14
libc/include/bits/getdents.h
Normal file
14
libc/include/bits/getdents.h
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
/* bits/getdents.h: Definitions used for the getdents() system call. */
|
||||||
|
|
||||||
|
#ifndef _BITS_GETDENTS_H
|
||||||
|
#define _BITS_GETDENTS_H
|
||||||
|
|
||||||
|
#include <sys/types.h>
|
||||||
|
|
||||||
|
typedef struct
|
||||||
|
{
|
||||||
|
ino_t inode;
|
||||||
|
char name[129];
|
||||||
|
} luna_dirent;
|
||||||
|
|
||||||
|
#endif
|
@ -15,6 +15,7 @@ typedef __u16_t mode_t;
|
|||||||
typedef __u64_t useconds_t;
|
typedef __u64_t useconds_t;
|
||||||
typedef __i64_t off_t;
|
typedef __i64_t off_t;
|
||||||
typedef __u64_t dev_t;
|
typedef __u64_t dev_t;
|
||||||
|
typedef __u64_t ino_t;
|
||||||
|
|
||||||
typedef off_t fpos_t;
|
typedef off_t fpos_t;
|
||||||
|
|
||||||
|
@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
#define enumerate_syscalls(_e) \
|
#define enumerate_syscalls(_e) \
|
||||||
_e(exit) _e(clock_gettime) _e(mmap) _e(munmap) _e(usleep) _e(open) _e(close) _e(read) _e(getpid) _e(write) \
|
_e(exit) _e(clock_gettime) _e(mmap) _e(munmap) _e(usleep) _e(open) _e(close) _e(read) _e(getpid) _e(write) \
|
||||||
_e(lseek) _e(mkdir) _e(exec) _e(mknod) _e(fork) _e(waitpid) _e(getppid) _e(fcntl)
|
_e(lseek) _e(mkdir) _e(exec) _e(mknod) _e(fork) _e(waitpid) _e(getppid) _e(fcntl) _e(getdents)
|
||||||
|
|
||||||
enum Syscalls
|
enum Syscalls
|
||||||
{
|
{
|
||||||
|
Loading…
Reference in New Issue
Block a user