kernel+libc: Add chdir()
This commit is contained in:
parent
2d30935fdb
commit
2a967f4b8b
@ -35,6 +35,7 @@ set(SOURCES
|
|||||||
src/sys/waitpid.cpp
|
src/sys/waitpid.cpp
|
||||||
src/sys/getdents.cpp
|
src/sys/getdents.cpp
|
||||||
src/sys/stat.cpp
|
src/sys/stat.cpp
|
||||||
|
src/sys/chdir.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
|
||||||
|
38
kernel/src/sys/chdir.cpp
Normal file
38
kernel/src/sys/chdir.cpp
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
@ -97,6 +97,9 @@ extern "C"
|
|||||||
/* Duplicate a file descriptor. */
|
/* Duplicate a file descriptor. */
|
||||||
int dup(int fd);
|
int dup(int fd);
|
||||||
|
|
||||||
|
/* Change the current working directory. */
|
||||||
|
int chdir(const char* path);
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
@ -229,4 +229,10 @@ extern "C"
|
|||||||
{
|
{
|
||||||
return fcntl(fd, F_DUPFD, 0);
|
return fcntl(fd, F_DUPFD, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int chdir(const char* path)
|
||||||
|
{
|
||||||
|
long rc = syscall(SYS_chdir, path);
|
||||||
|
__errno_return(rc, int);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -11,11 +11,20 @@ class PathParser
|
|||||||
PathParser(PathParser&& other);
|
PathParser(PathParser&& other);
|
||||||
PathParser(const PathParser&) = delete;
|
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();
|
~PathParser();
|
||||||
|
|
||||||
bool is_absolute() const
|
bool is_absolute() const
|
||||||
{
|
{
|
||||||
return *m_original == '/';
|
return is_absolute(StringView { m_original });
|
||||||
}
|
}
|
||||||
|
|
||||||
Result<String> basename();
|
Result<String> basename();
|
||||||
|
@ -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(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(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(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
|
enum Syscalls
|
||||||
{
|
{
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
#include <luna/CPath.h>
|
#include <luna/CPath.h>
|
||||||
#include <luna/PathParser.h>
|
#include <luna/PathParser.h>
|
||||||
#include <luna/ScopeGuard.h>
|
#include <luna/ScopeGuard.h>
|
||||||
|
#include <luna/StringBuilder.h>
|
||||||
|
|
||||||
Result<PathParser> PathParser::create(const char* path)
|
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.
|
// We must copy this as we cannot rely on the original string.
|
||||||
return String::from_cstring(result);
|
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();
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user