kernel+libc: Add chdir()

This commit is contained in:
apio 2023-04-11 22:15:21 +02:00
parent 2d30935fdb
commit 2a967f4b8b
Signed by: apio
GPG Key ID: B8A7D06E42258954
7 changed files with 105 additions and 2 deletions

View File

@ -35,6 +35,7 @@ set(SOURCES
src/sys/waitpid.cpp
src/sys/getdents.cpp
src/sys/stat.cpp
src/sys/chdir.cpp
src/fs/VFS.cpp
src/fs/tmpfs/FileSystem.cpp
src/fs/devices/DeviceRegistry.cpp

38
kernel/src/sys/chdir.cpp Normal file
View File

@ -0,0 +1,38 @@
#include "memory/MemoryManager.h"
#include "sys/Syscall.h"
#include "thread/Scheduler.h"
#include <luna/PathParser.h>
Result<u64> sys_chdir(Registers*, SyscallArgs args)
{
auto path = TRY(MemoryManager::strdup_from_user(args[0]));
Thread* current = Scheduler::current();
if (PathParser::is_absolute(path.view()))
{
SharedPtr<VFS::Inode> inode = TRY(VFS::resolve_path(path.chars(), current->auth));
if (inode->type() != VFS::InodeType::Directory) return err(ENOTDIR);
current->current_directory = inode;
current->current_directory_path = move(path);
return 0;
}
else
{
SharedPtr<VFS::Inode> inode = TRY(VFS::resolve_path(path.chars(), current->auth, current->current_directory));
if (inode->type() != VFS::InodeType::Directory) return err(ENOTDIR);
auto old_wdir = current->current_directory_path.view();
String new_path = TRY(PathParser::join(old_wdir.is_empty() ? "/"_sv : old_wdir, path.view()));
current->current_directory = inode;
current->current_directory_path = move(new_path);
return 0;
}
}

View File

@ -97,6 +97,9 @@ extern "C"
/* Duplicate a file descriptor. */
int dup(int fd);
/* Change the current working directory. */
int chdir(const char* path);
#ifdef __cplusplus
}
#endif

View File

@ -229,4 +229,10 @@ extern "C"
{
return fcntl(fd, F_DUPFD, 0);
}
int chdir(const char* path)
{
long rc = syscall(SYS_chdir, path);
__errno_return(rc, int);
}
}

View File

@ -11,11 +11,20 @@ class PathParser
PathParser(PathParser&& other);
PathParser(const PathParser&) = delete;
static Result<String> join(StringView path1, StringView path2);
static Result<String> realpath(StringView path);
static bool is_absolute(StringView path)
{
return *path.chars() == '/';
}
~PathParser();
bool is_absolute() const
{
return *m_original == '/';
return is_absolute(StringView { m_original });
}
Result<String> basename();

View File

@ -4,7 +4,7 @@
_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(execve) _e(mknod) _e(fork) _e(waitpid) _e(getppid) _e(fcntl) _e(getdents) _e(getuid) \
_e(geteuid) _e(getgid) _e(getegid) _e(setuid) _e(setgid) _e(seteuid) _e(setegid) _e(chmod) _e(chown) \
_e(ioctl) _e(stat) _e(fstat)
_e(ioctl) _e(stat) _e(fstat) _e(chdir)
enum Syscalls
{

View File

@ -1,6 +1,7 @@
#include <luna/CPath.h>
#include <luna/PathParser.h>
#include <luna/ScopeGuard.h>
#include <luna/StringBuilder.h>
Result<PathParser> PathParser::create(const char* path)
{
@ -59,3 +60,48 @@ Result<String> PathParser::dirname()
// We must copy this as we cannot rely on the original string.
return String::from_cstring(result);
}
Result<String> PathParser::join(StringView path1, StringView path2)
{
StringBuilder sb;
TRY(sb.add(path1));
if (path1[path1.length() - 1] != '/') TRY(sb.add('/'));
TRY(sb.add(path2));
String result = TRY(sb.string());
return realpath(result.view());
}
Result<String> PathParser::realpath(StringView path)
{
if (!is_absolute(path)) return String {};
Vector<StringView> parts;
PathParser parser = TRY(PathParser::create(path.chars()));
const char* part;
while (parser.next().try_set_value(part))
{
StringView view = part;
if (view == ".") continue;
if (view == "..")
{
parts.try_pop();
continue;
}
TRY(parts.try_append(view));
}
StringBuilder sb;
for (const auto& section : parts)
{
TRY(sb.add('/'));
TRY(sb.add(section));
}
return sb.string();
}