Compare commits
4 Commits
ff770b7328
...
6fbf97292a
Author | SHA1 | Date | |
---|---|---|---|
6fbf97292a | |||
4c66017807 | |||
5a9da55e05 | |||
6512e9549e |
@ -1,4 +1,6 @@
|
||||
#include "fs/VFS.h"
|
||||
#include "Log.h"
|
||||
#include <luna/PathParser.h>
|
||||
|
||||
namespace VFS
|
||||
{
|
||||
@ -6,6 +8,31 @@ namespace VFS
|
||||
|
||||
Inode& root_inode()
|
||||
{
|
||||
return root_fs->root_inode();
|
||||
return *root_fs->root_inode();
|
||||
}
|
||||
|
||||
Result<SharedPtr<Inode>> resolve_path(const char* path)
|
||||
{
|
||||
auto parser = TRY(PathParser::create(path));
|
||||
|
||||
kdbgln("vfs: trying to resolve path %s", path);
|
||||
|
||||
SharedPtr<Inode> current_inode;
|
||||
|
||||
if (parser.is_absolute()) { current_inode = root_fs->root_inode(); }
|
||||
else
|
||||
{
|
||||
kwarnln("vfs: cannot resolve path '%s', as relative paths are not supported yet", path);
|
||||
return err(ENOTSUP);
|
||||
}
|
||||
|
||||
const char* section;
|
||||
while (parser.next().try_set_value(section))
|
||||
{
|
||||
kdbgln("vfs: searching for entry '%s' in inode %zu", section, current_inode->inode_number());
|
||||
current_inode = TRY(current_inode->find(section));
|
||||
}
|
||||
|
||||
return current_inode;
|
||||
}
|
||||
}
|
||||
|
@ -60,7 +60,7 @@ namespace VFS
|
||||
class FileSystem
|
||||
{
|
||||
public:
|
||||
virtual Inode& root_inode() const = 0;
|
||||
virtual SharedPtr<Inode> root_inode() const = 0;
|
||||
|
||||
virtual Result<SharedPtr<Inode>> create_file_inode() = 0;
|
||||
|
||||
@ -71,7 +71,7 @@ namespace VFS
|
||||
|
||||
extern SharedPtr<FileSystem> root_fs;
|
||||
|
||||
Result<Inode*> resolve_path(const char* path);
|
||||
Result<SharedPtr<Inode>> resolve_path(const char* path);
|
||||
|
||||
Inode& root_inode();
|
||||
}
|
||||
|
@ -10,9 +10,9 @@ namespace TmpFS
|
||||
class FileSystem : public VFS::FileSystem
|
||||
{
|
||||
public:
|
||||
VFS::Inode& root_inode() const override
|
||||
SharedPtr<VFS::Inode> root_inode() const override
|
||||
{
|
||||
return *m_root_inode;
|
||||
return m_root_inode;
|
||||
}
|
||||
|
||||
Result<SharedPtr<VFS::Inode>> create_file_inode() override;
|
||||
|
@ -71,6 +71,10 @@ static Result<void> try_init_vfs()
|
||||
|
||||
kinfoln("etc inode's 'passwd' entry inode number: %zu", TRY(etc.find("passwd"))->inode_number());
|
||||
|
||||
auto& passwd = *TRY(VFS::resolve_path("/etc/passwd"));
|
||||
|
||||
kinfoln("/etc/passwd's inode number: %zu", passwd.inode_number());
|
||||
|
||||
return {};
|
||||
}
|
||||
|
||||
|
@ -17,6 +17,7 @@ set(FREESTANDING_SOURCES
|
||||
src/DebugLog.cpp
|
||||
src/Heap.cpp
|
||||
src/Spinlock.cpp
|
||||
src/PathParser.cpp
|
||||
src/UBSAN.cpp
|
||||
)
|
||||
|
||||
|
@ -13,6 +13,11 @@ extern "C"
|
||||
|
||||
int strcmp(const char* a, const char* b);
|
||||
|
||||
usize strspn(const char* str, const char* accept);
|
||||
usize strcspn(const char* str, const char* reject);
|
||||
|
||||
char* strtok(char* str, const char* delim);
|
||||
|
||||
usize wcslen(const wchar_t* str);
|
||||
|
||||
char* strdup(const char* str);
|
||||
|
28
libluna/include/luna/PathParser.h
Normal file
28
libluna/include/luna/PathParser.h
Normal file
@ -0,0 +1,28 @@
|
||||
#pragma once
|
||||
#include <luna/CString.h>
|
||||
#include <luna/Heap.h>
|
||||
|
||||
class PathParser
|
||||
{
|
||||
public:
|
||||
static Result<PathParser> create(const char* path);
|
||||
|
||||
PathParser(PathParser&& other);
|
||||
PathParser(const PathParser&) = delete;
|
||||
|
||||
~PathParser();
|
||||
|
||||
bool is_absolute() const
|
||||
{
|
||||
return *m_original == '/';
|
||||
}
|
||||
|
||||
Option<const char*> next();
|
||||
|
||||
private:
|
||||
PathParser(const char* original, char* copy);
|
||||
|
||||
const char* m_original { nullptr };
|
||||
char* m_copy { nullptr };
|
||||
bool m_already_called_next { false };
|
||||
};
|
@ -136,4 +136,71 @@ extern "C"
|
||||
dest[copy_len] = 0;
|
||||
return src_len;
|
||||
}
|
||||
|
||||
usize strcspn(const char* str, const char* reject)
|
||||
{
|
||||
const char* s = str;
|
||||
while (*s)
|
||||
{
|
||||
const char* rp = reject;
|
||||
while (*rp)
|
||||
{
|
||||
if (*s == *rp) return (usize)(s - str);
|
||||
rp++;
|
||||
}
|
||||
s++;
|
||||
}
|
||||
return (usize)(s - str);
|
||||
}
|
||||
|
||||
usize strspn(const char* str, const char* accept)
|
||||
{
|
||||
const char* s = str;
|
||||
while (*s)
|
||||
{
|
||||
const char* ap = accept;
|
||||
bool match = false;
|
||||
while (*ap)
|
||||
{
|
||||
if (*s == *ap)
|
||||
{
|
||||
match = true;
|
||||
break;
|
||||
}
|
||||
ap++;
|
||||
}
|
||||
if (!match) return (usize)(s - str);
|
||||
s++;
|
||||
}
|
||||
return (usize)(s - str);
|
||||
}
|
||||
|
||||
char* strtok(char* str, const char* delim)
|
||||
{
|
||||
static char* s = nullptr;
|
||||
if (str) s = str;
|
||||
if (!s) return nullptr;
|
||||
|
||||
if (*s)
|
||||
{
|
||||
usize skip = strspn(s, delim);
|
||||
s += skip;
|
||||
|
||||
if (*s == 0) return nullptr;
|
||||
|
||||
usize use = strcspn(s, delim);
|
||||
char* result = s;
|
||||
|
||||
if (s[use] != 0)
|
||||
{
|
||||
s[use] = 0;
|
||||
s += (use + 1);
|
||||
}
|
||||
else { s = nullptr; }
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
return nullptr;
|
||||
}
|
||||
}
|
||||
|
33
libluna/src/PathParser.cpp
Normal file
33
libluna/src/PathParser.cpp
Normal file
@ -0,0 +1,33 @@
|
||||
#include <luna/PathParser.h>
|
||||
|
||||
Result<PathParser> PathParser::create(const char* path)
|
||||
{
|
||||
char* copy = strdup(path);
|
||||
if (!copy) return err(ENOMEM);
|
||||
|
||||
return PathParser { path, copy };
|
||||
}
|
||||
|
||||
PathParser::PathParser(const char* original, char* copy) : m_original(original), m_copy(copy)
|
||||
{
|
||||
}
|
||||
|
||||
PathParser::PathParser(PathParser&& other) : m_original(other.m_original), m_copy(other.m_copy)
|
||||
{
|
||||
other.m_copy = nullptr;
|
||||
}
|
||||
|
||||
PathParser::~PathParser()
|
||||
{
|
||||
if (m_copy) free_impl(m_copy);
|
||||
}
|
||||
|
||||
Option<const char*> PathParser::next()
|
||||
{
|
||||
char* result = strtok(m_already_called_next ? nullptr : m_copy, "/");
|
||||
m_already_called_next = true;
|
||||
|
||||
if (!result) return {};
|
||||
|
||||
return result;
|
||||
}
|
@ -6,8 +6,8 @@ source $(dirname $0)/env.sh
|
||||
cd $LUNA_ROOT
|
||||
|
||||
SOURCES=($(find kernel/src -type f | grep -v "\.asm" | grep -v "bootboot.h"))
|
||||
SOURCES+=($(find luna/src -type f))
|
||||
SOURCES+=($(find luna/include/luna -type f | grep -v "Types.h"))
|
||||
SOURCES+=($(find libluna/src -type f))
|
||||
SOURCES+=($(find libluna/include/luna -type f | grep -v "Types.h"))
|
||||
|
||||
for f in ${SOURCES[@]}
|
||||
do
|
||||
|
Loading…
Reference in New Issue
Block a user