Compare commits
No commits in common. "3a45f4af53abd18d92459981aa614258a77787d3" and "79a96bf5afcf06d188afc45d152ad9a3a3cb87a4" have entirely different histories.
3a45f4af53
...
79a96bf5af
@ -12,7 +12,7 @@ int main(int argc, char** argv)
|
|||||||
bool show_almost_all { false };
|
bool show_almost_all { false };
|
||||||
|
|
||||||
os::ArgumentParser parser;
|
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_all, 'a', "all"_sv);
|
||||||
parser.add_switch_argument(show_almost_all, 'A', "almost-all"_sv);
|
parser.add_switch_argument(show_almost_all, 'A', "almost-all"_sv);
|
||||||
parser.parse(argc, argv);
|
parser.parse(argc, argv);
|
||||||
|
@ -89,7 +89,5 @@ int main(int argc, char** argv)
|
|||||||
setgid(entry->pw_gid);
|
setgid(entry->pw_gid);
|
||||||
setuid(entry->pw_uid);
|
setuid(entry->pw_uid);
|
||||||
|
|
||||||
chdir(entry->pw_dir);
|
|
||||||
|
|
||||||
execl(entry->pw_shell, entry->pw_shell, NULL);
|
execl(entry->pw_shell, entry->pw_shell, NULL);
|
||||||
}
|
}
|
||||||
|
@ -35,7 +35,6 @@ 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
|
||||||
|
@ -13,15 +13,13 @@ namespace VFS
|
|||||||
return *root_fs->root_inode();
|
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));
|
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();
|
// FIXME: Properly handle relative paths.
|
||||||
else
|
|
||||||
current_inode = working_directory;
|
|
||||||
|
|
||||||
const char* section;
|
const char* section;
|
||||||
while (parser.next().try_set_value(section))
|
while (parser.next().try_set_value(section))
|
||||||
@ -33,12 +31,12 @@ namespace VFS
|
|||||||
return current_inode;
|
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 parser = TRY(PathParser::create(path));
|
||||||
auto parent_path = TRY(parser.dirname());
|
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);
|
if (!can_write(parent_inode, auth)) return err(EACCES);
|
||||||
|
|
||||||
@ -49,12 +47,12 @@ namespace VFS
|
|||||||
return parent_inode->create_subdirectory(child_name.chars());
|
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 parser = TRY(PathParser::create(path));
|
||||||
auto parent_path = TRY(parser.dirname());
|
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);
|
if (!can_write(parent_inode, auth)) return err(EACCES);
|
||||||
|
|
||||||
|
@ -175,14 +175,10 @@ namespace VFS
|
|||||||
|
|
||||||
extern SharedPtr<FileSystem> root_fs;
|
extern SharedPtr<FileSystem> root_fs;
|
||||||
|
|
||||||
Result<SharedPtr<Inode>> resolve_path(const char* path, Credentials auth,
|
Result<SharedPtr<Inode>> resolve_path(const char* path, Credentials auth);
|
||||||
SharedPtr<VFS::Inode> working_directory = {});
|
|
||||||
|
|
||||||
Result<SharedPtr<Inode>> create_directory(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);
|
||||||
|
|
||||||
Result<SharedPtr<Inode>> create_file(const char* path, Credentials auth,
|
|
||||||
SharedPtr<VFS::Inode> working_directory = {});
|
|
||||||
|
|
||||||
bool can_execute(SharedPtr<Inode> inode, Credentials auth);
|
bool can_execute(SharedPtr<Inode> inode, Credentials auth);
|
||||||
bool can_read(SharedPtr<Inode> inode, Credentials auth);
|
bool can_read(SharedPtr<Inode> inode, Credentials auth);
|
||||||
|
@ -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;
|
|
||||||
}
|
|
||||||
}
|
|
@ -104,8 +104,6 @@ Result<u64> sys_fork(Registers* regs, SyscallArgs)
|
|||||||
|
|
||||||
memcpy(¤t->regs, regs, sizeof(*regs));
|
memcpy(¤t->regs, regs, sizeof(*regs));
|
||||||
|
|
||||||
auto current_directory_path = TRY(current->current_directory_path.clone());
|
|
||||||
|
|
||||||
auto image = TRY(ThreadImage::clone_from_thread(current));
|
auto image = TRY(ThreadImage::clone_from_thread(current));
|
||||||
|
|
||||||
auto thread = TRY(new_thread());
|
auto thread = TRY(new_thread());
|
||||||
@ -116,8 +114,6 @@ Result<u64> sys_fork(Registers* regs, SyscallArgs)
|
|||||||
thread->fp_data.save();
|
thread->fp_data.save();
|
||||||
thread->name = current->name;
|
thread->name = current->name;
|
||||||
thread->auth = current->auth;
|
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]; }
|
for (int i = 0; i < FD_MAX; i++) { thread->fd_table[i] = current->fd_table[i]; }
|
||||||
|
|
||||||
|
@ -99,7 +99,7 @@ Result<u64> sys_chmod(Registers*, SyscallArgs args)
|
|||||||
|
|
||||||
Credentials& auth = Scheduler::current()->auth;
|
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);
|
if (auth.euid != 0 && auth.euid != inode->uid()) return err(EPERM);
|
||||||
|
|
||||||
|
@ -13,7 +13,7 @@ Result<u64> sys_mkdir(Registers*, SyscallArgs args)
|
|||||||
|
|
||||||
kinfoln("mkdir: attempting to create %s", path.chars());
|
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->chmod(mode);
|
||||||
inode->chown(current->auth.euid, current->auth.egid);
|
inode->chown(current->auth.euid, current->auth.egid);
|
||||||
|
|
||||||
|
@ -24,7 +24,7 @@ Result<u64> sys_mknod(Registers*, SyscallArgs args)
|
|||||||
auto dirname = TRY(parser.dirname());
|
auto dirname = TRY(parser.dirname());
|
||||||
auto basename = TRY(parser.basename());
|
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);
|
if (!VFS::can_write(parent, current->auth)) return err(EACCES);
|
||||||
|
|
||||||
auto inode = TRY(parent->fs().create_device_inode(maj, min));
|
auto inode = TRY(parent->fs().create_device_inode(maj, min));
|
||||||
|
@ -25,13 +25,12 @@ Result<u64> sys_open(Registers*, SyscallArgs args)
|
|||||||
if ((flags & O_RDWR) == 0) { return err(EINVAL); }
|
if ((flags & O_RDWR) == 0) { return err(EINVAL); }
|
||||||
|
|
||||||
int error;
|
int error;
|
||||||
bool ok =
|
bool ok = VFS::resolve_path(path.chars(), current->auth).try_set_value_or_error(inode, error);
|
||||||
VFS::resolve_path(path.chars(), current->auth, current->current_directory).try_set_value_or_error(inode, error);
|
|
||||||
if (!ok)
|
if (!ok)
|
||||||
{
|
{
|
||||||
if (error == ENOENT && (flags & O_CREAT))
|
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->chmod(mode);
|
||||||
inode->chown(current->auth.euid, current->auth.egid);
|
inode->chown(current->auth.euid, current->auth.egid);
|
||||||
}
|
}
|
||||||
|
@ -42,7 +42,7 @@ Result<u64> sys_stat(Registers*, SyscallArgs args)
|
|||||||
|
|
||||||
Thread* current = Scheduler::current();
|
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);
|
return do_stat(inode, st);
|
||||||
}
|
}
|
||||||
|
@ -8,7 +8,6 @@
|
|||||||
#include <luna/Result.h>
|
#include <luna/Result.h>
|
||||||
#include <luna/Stack.h>
|
#include <luna/Stack.h>
|
||||||
#include <luna/StaticString.h>
|
#include <luna/StaticString.h>
|
||||||
#include <luna/String.h>
|
|
||||||
|
|
||||||
#ifdef ARCH_X86_64
|
#ifdef ARCH_X86_64
|
||||||
#include "arch/x86_64/CPU.h"
|
#include "arch/x86_64/CPU.h"
|
||||||
@ -84,9 +83,6 @@ struct Thread : public LinkedListNode<Thread>
|
|||||||
|
|
||||||
StaticString<128> name;
|
StaticString<128> name;
|
||||||
|
|
||||||
String current_directory_path = {};
|
|
||||||
SharedPtr<VFS::Inode> current_directory = {};
|
|
||||||
|
|
||||||
PageDirectory* directory;
|
PageDirectory* directory;
|
||||||
|
|
||||||
bool is_idle()
|
bool is_idle()
|
||||||
|
@ -97,9 +97,6 @@ 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,10 +229,4 @@ 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,20 +11,11 @@ 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 is_absolute(StringView { m_original });
|
return *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(chdir)
|
_e(ioctl) _e(stat) _e(fstat)
|
||||||
|
|
||||||
enum Syscalls
|
enum Syscalls
|
||||||
{
|
{
|
||||||
|
@ -1,7 +1,6 @@
|
|||||||
#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)
|
||||||
{
|
{
|
||||||
@ -60,48 +59,3 @@ 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