Compare commits

..

No commits in common. "3a45f4af53abd18d92459981aa614258a77787d3" and "79a96bf5afcf06d188afc45d152ad9a3a3cb87a4" have entirely different histories.

18 changed files with 19 additions and 139 deletions

View File

@ -12,7 +12,7 @@ int main(int argc, char** argv)
bool show_almost_all { false };
os::ArgumentParser parser;
parser.add_positional_argument(pathname, "directory"_sv, "."_sv);
parser.add_positional_argument(pathname, "directory"_sv, "/"_sv);
parser.add_switch_argument(show_all, 'a', "all"_sv);
parser.add_switch_argument(show_almost_all, 'A', "almost-all"_sv);
parser.parse(argc, argv);

View File

@ -89,7 +89,5 @@ int main(int argc, char** argv)
setgid(entry->pw_gid);
setuid(entry->pw_uid);
chdir(entry->pw_dir);
execl(entry->pw_shell, entry->pw_shell, NULL);
}

View File

@ -35,7 +35,6 @@ 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

View File

@ -13,15 +13,13 @@ namespace VFS
return *root_fs->root_inode();
}
Result<SharedPtr<Inode>> resolve_path(const char* path, Credentials auth, SharedPtr<Inode> working_directory)
Result<SharedPtr<Inode>> resolve_path(const char* path, Credentials auth)
{
auto parser = TRY(PathParser::create(path));
SharedPtr<Inode> current_inode;
SharedPtr<Inode> current_inode = root_fs->root_inode();
if (parser.is_absolute() || !working_directory) current_inode = root_fs->root_inode();
else
current_inode = working_directory;
// FIXME: Properly handle relative paths.
const char* section;
while (parser.next().try_set_value(section))
@ -33,12 +31,12 @@ namespace VFS
return current_inode;
}
Result<SharedPtr<Inode>> create_directory(const char* path, Credentials auth, SharedPtr<Inode> working_directory)
Result<SharedPtr<Inode>> create_directory(const char* path, Credentials auth)
{
auto parser = TRY(PathParser::create(path));
auto parent_path = TRY(parser.dirname());
auto parent_inode = TRY(resolve_path(parent_path.chars(), auth, working_directory));
auto parent_inode = TRY(resolve_path(parent_path.chars(), auth));
if (!can_write(parent_inode, auth)) return err(EACCES);
@ -49,12 +47,12 @@ namespace VFS
return parent_inode->create_subdirectory(child_name.chars());
}
Result<SharedPtr<Inode>> create_file(const char* path, Credentials auth, SharedPtr<Inode> working_directory)
Result<SharedPtr<Inode>> create_file(const char* path, Credentials auth)
{
auto parser = TRY(PathParser::create(path));
auto parent_path = TRY(parser.dirname());
auto parent_inode = TRY(resolve_path(parent_path.chars(), auth, working_directory));
auto parent_inode = TRY(resolve_path(parent_path.chars(), auth));
if (!can_write(parent_inode, auth)) return err(EACCES);

View File

@ -175,14 +175,10 @@ namespace VFS
extern SharedPtr<FileSystem> root_fs;
Result<SharedPtr<Inode>> resolve_path(const char* path, Credentials auth,
SharedPtr<VFS::Inode> working_directory = {});
Result<SharedPtr<Inode>> resolve_path(const char* path, Credentials auth);
Result<SharedPtr<Inode>> create_directory(const char* path, Credentials auth,
SharedPtr<VFS::Inode> working_directory = {});
Result<SharedPtr<Inode>> create_file(const char* path, Credentials auth,
SharedPtr<VFS::Inode> working_directory = {});
Result<SharedPtr<Inode>> create_directory(const char* path, Credentials auth);
Result<SharedPtr<Inode>> create_file(const char* path, Credentials auth);
bool can_execute(SharedPtr<Inode> inode, Credentials auth);
bool can_read(SharedPtr<Inode> inode, Credentials auth);

View File

@ -1,38 +0,0 @@
#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

@ -104,8 +104,6 @@ Result<u64> sys_fork(Registers* regs, SyscallArgs)
memcpy(&current->regs, regs, sizeof(*regs));
auto current_directory_path = TRY(current->current_directory_path.clone());
auto image = TRY(ThreadImage::clone_from_thread(current));
auto thread = TRY(new_thread());
@ -116,8 +114,6 @@ Result<u64> sys_fork(Registers* regs, SyscallArgs)
thread->fp_data.save();
thread->name = current->name;
thread->auth = current->auth;
thread->current_directory = current->current_directory;
thread->current_directory_path = move(current_directory_path);
for (int i = 0; i < FD_MAX; i++) { thread->fd_table[i] = current->fd_table[i]; }

View File

@ -99,7 +99,7 @@ Result<u64> sys_chmod(Registers*, SyscallArgs args)
Credentials& auth = Scheduler::current()->auth;
auto inode = TRY(VFS::resolve_path(path.chars(), auth, Scheduler::current()->current_directory));
auto inode = TRY(VFS::resolve_path(path.chars(), auth));
if (auth.euid != 0 && auth.euid != inode->uid()) return err(EPERM);

View File

@ -13,7 +13,7 @@ Result<u64> sys_mkdir(Registers*, SyscallArgs args)
kinfoln("mkdir: attempting to create %s", path.chars());
auto inode = TRY(VFS::create_directory(path.chars(), current->auth, current->current_directory));
auto inode = TRY(VFS::create_directory(path.chars(), current->auth));
inode->chmod(mode);
inode->chown(current->auth.euid, current->auth.egid);

View File

@ -24,7 +24,7 @@ Result<u64> sys_mknod(Registers*, SyscallArgs args)
auto dirname = TRY(parser.dirname());
auto basename = TRY(parser.basename());
auto parent = TRY(VFS::resolve_path(dirname.chars(), current->auth, current->current_directory));
auto parent = TRY(VFS::resolve_path(dirname.chars(), current->auth));
if (!VFS::can_write(parent, current->auth)) return err(EACCES);
auto inode = TRY(parent->fs().create_device_inode(maj, min));

View File

@ -25,13 +25,12 @@ Result<u64> sys_open(Registers*, SyscallArgs args)
if ((flags & O_RDWR) == 0) { return err(EINVAL); }
int error;
bool ok =
VFS::resolve_path(path.chars(), current->auth, current->current_directory).try_set_value_or_error(inode, error);
bool ok = VFS::resolve_path(path.chars(), current->auth).try_set_value_or_error(inode, error);
if (!ok)
{
if (error == ENOENT && (flags & O_CREAT))
{
inode = TRY(VFS::create_file(path.chars(), current->auth, current->current_directory));
inode = TRY(VFS::create_file(path.chars(), current->auth));
inode->chmod(mode);
inode->chown(current->auth.euid, current->auth.egid);
}

View File

@ -42,7 +42,7 @@ Result<u64> sys_stat(Registers*, SyscallArgs args)
Thread* current = Scheduler::current();
auto inode = TRY(VFS::resolve_path(path.chars(), current->auth, current->current_directory));
auto inode = TRY(VFS::resolve_path(path.chars(), current->auth));
return do_stat(inode, st);
}

View File

@ -8,7 +8,6 @@
#include <luna/Result.h>
#include <luna/Stack.h>
#include <luna/StaticString.h>
#include <luna/String.h>
#ifdef ARCH_X86_64
#include "arch/x86_64/CPU.h"
@ -84,9 +83,6 @@ struct Thread : public LinkedListNode<Thread>
StaticString<128> name;
String current_directory_path = {};
SharedPtr<VFS::Inode> current_directory = {};
PageDirectory* directory;
bool is_idle()

View File

@ -97,9 +97,6 @@ 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,10 +229,4 @@ 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,20 +11,11 @@ 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 is_absolute(StringView { m_original });
return *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(chdir)
_e(ioctl) _e(stat) _e(fstat)
enum Syscalls
{

View File

@ -1,7 +1,6 @@
#include <luna/CPath.h>
#include <luna/PathParser.h>
#include <luna/ScopeGuard.h>
#include <luna/StringBuilder.h>
Result<PathParser> PathParser::create(const char* path)
{
@ -60,48 +59,3 @@ 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();
}