Compare commits

..

4 Commits

Author SHA1 Message Date
6fbf97292a
VFS: Implement resolve_path() using PathParser
All checks were successful
continuous-integration/drone/push Build is passing
Already works better than old luna (handles .. correctly)
2023-03-10 22:18:48 +01:00
4c66017807
libluna: Add PathParser, a C++-style way of iterating over path sections
Still uses strtok under the hood, though.
2023-03-10 22:17:16 +01:00
5a9da55e05
libluna/CString: Implement strspn and strcspn, and using those implement strtok 2023-03-10 21:32:18 +01:00
6512e9549e
tools: Make replace-stdint.sh search in libluna/ instead of luna/ 2023-03-10 21:30:59 +01:00
10 changed files with 173 additions and 8 deletions

View File

@ -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;
}
}

View File

@ -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();
}

View File

@ -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;

View File

@ -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 {};
}

View File

@ -17,6 +17,7 @@ set(FREESTANDING_SOURCES
src/DebugLog.cpp
src/Heap.cpp
src/Spinlock.cpp
src/PathParser.cpp
src/UBSAN.cpp
)

View File

@ -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);

View 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 };
};

View File

@ -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;
}
}

View 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;
}

View File

@ -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
@ -23,4 +23,4 @@ do
sed -i 's/size_t/usize/g' $f
sed -i 's/ssize_t/isize/g' $f
done
done