Compare commits

..

No commits in common. "3eb78aa5f34398d65fad56e6fe66b19b29401c3b" and "1215c38d757939dbd0d6bf0564e1dee7386e90f8" have entirely different histories.

15 changed files with 70 additions and 102 deletions

View File

@ -132,9 +132,9 @@ static void start_service(Service& service)
} }
} }
static Result<void> load_service(const os::Path& path) static Result<void> load_service(StringView path)
{ {
fprintf(g_init_log, "[init] reading service file: %s\n", path.name().chars()); fprintf(g_init_log, "[init] reading service file: %s\n", path.chars());
auto file = TRY(os::File::open(path, os::File::ReadOnly)); auto file = TRY(os::File::open(path, os::File::ReadOnly));
@ -242,7 +242,11 @@ static Result<void> load_services()
auto services = TRY(dir->list(os::Directory::Filter::ParentAndBase)); auto services = TRY(dir->list(os::Directory::Filter::ParentAndBase));
sort(services.begin(), services.end(), String::compare); sort(services.begin(), services.end(), String::compare);
for (const auto& entry : services) TRY(load_service({ dir->fd(), entry.view() })); for (const auto& entry : services)
{
auto service_path = TRY(PathParser::join("/etc/init"_sv, entry.view()));
TRY(load_service(service_path.view()));
}
return {}; return {};
} }

View File

@ -1,13 +1,12 @@
#pragma once #pragma once
#include <luna/Result.h> #include <luna/Result.h>
#include <luna/StringView.h>
struct Registers; struct Registers;
namespace CPU namespace CPU
{ {
Result<StringView> identify(); Result<const char*> identify();
StringView platform_string(); const char* platform_string();
void platform_init(); void platform_init();
void platform_finish_init(); void platform_finish_init();

View File

@ -184,7 +184,7 @@ static bool test_nx()
namespace CPU namespace CPU
{ {
Result<StringView> identify() Result<const char*> identify()
{ {
static char brand_string[49]; static char brand_string[49];
@ -198,12 +198,12 @@ namespace CPU
brand_string[48] = 0; // null-terminate it :) brand_string[48] = 0; // null-terminate it :)
return StringView { brand_string, 48 }; return brand_string;
} }
StringView platform_string() const char* platform_string()
{ {
return "x86_64"_sv; return "x86_64";
} }
void platform_init() void platform_init()

View File

@ -41,8 +41,8 @@ Result<void> init()
// Default hostname if nobody from userspace changes it // Default hostname if nobody from userspace changes it
set_host_name("moon"_sv); set_host_name("moon"_sv);
kinfoln("Current platform: %s", CPU::platform_string().chars()); kinfoln("Current platform: %s", CPU::platform_string());
kinfoln("Current processor: %s", CPU::identify().value_or("(unknown)"_sv).chars()); kinfoln("Current processor: %s", CPU::identify().value_or("(unknown)"));
kinfoln("Total memory: %s", to_dynamic_unit(MemoryManager::total()).release_value().chars()); kinfoln("Total memory: %s", to_dynamic_unit(MemoryManager::total()).release_value().chars());
kinfoln("Free memory: %s", to_dynamic_unit(MemoryManager::free()).release_value().chars()); kinfoln("Free memory: %s", to_dynamic_unit(MemoryManager::free()).release_value().chars());

View File

@ -27,7 +27,7 @@ Result<u64> sys_uname(Registers*, SyscallArgs args)
// FIXME: Hardcode this at build time instead of in code (should be the short commit hash). // FIXME: Hardcode this at build time instead of in code (should be the short commit hash).
strncpy(result.version, "alpha", _UTSNAME_LENGTH); strncpy(result.version, "alpha", _UTSNAME_LENGTH);
strncpy(result.machine, CPU::platform_string().chars(), _UTSNAME_LENGTH); strncpy(result.machine, CPU::platform_string(), _UTSNAME_LENGTH);
if (!MemoryManager::copy_to_user_typed(buf, &result)) return err(EFAULT); if (!MemoryManager::copy_to_user_typed(buf, &result)) return err(EFAULT);

View File

@ -63,7 +63,7 @@ namespace ELFLoader
if (elf_header.e_machine != EM_MACH) if (elf_header.e_machine != EM_MACH)
{ {
kerrorln("Error while loading ELF: ELF object's target architecture does not match the current one (%s)", kerrorln("Error while loading ELF: ELF object's target architecture does not match the current one (%s)",
CPU::platform_string().chars()); CPU::platform_string());
return err(ENOEXEC); return err(ENOEXEC);
} }

View File

@ -10,7 +10,6 @@ set(SOURCES
src/Process.cpp src/Process.cpp
src/Directory.cpp src/Directory.cpp
src/Main.cpp src/Main.cpp
src/Path.cpp
) )
add_library(os ${SOURCES}) add_library(os ${SOURCES})

View File

@ -3,14 +3,15 @@
#include <luna/SharedPtr.h> #include <luna/SharedPtr.h>
#include <luna/String.h> #include <luna/String.h>
#include <luna/Vector.h> #include <luna/Vector.h>
#include <os/Path.h>
namespace os namespace os
{ {
class Directory class Directory
{ {
public: public:
static Result<SharedPtr<Directory>> open(const Path& path); static Result<SharedPtr<Directory>> open(StringView path);
static Result<SharedPtr<Directory>> openat(int dirfd, StringView path);
enum class Filter enum class Filter
{ {

View File

@ -4,7 +4,6 @@
#include <luna/Result.h> #include <luna/Result.h>
#include <luna/SharedPtr.h> #include <luna/SharedPtr.h>
#include <luna/StringView.h> #include <luna/StringView.h>
#include <os/Path.h>
#include <sys/types.h> #include <sys/types.h>
namespace os namespace os
@ -21,9 +20,9 @@ namespace os
ReadAppend = O_RDWR | O_APPEND, ReadAppend = O_RDWR | O_APPEND,
}; };
static Result<SharedPtr<File>> open(const Path& path, OpenMode flags); static Result<SharedPtr<File>> open(StringView path, OpenMode flags);
static Result<SharedPtr<File>> open_or_create(const Path& path, OpenMode flags, mode_t mode = 0644); static Result<SharedPtr<File>> open_or_create(StringView path, OpenMode flags, mode_t mode = 0644);
static Result<SharedPtr<File>> create(const Path& path, OpenMode flags, mode_t mode = 0644); static Result<SharedPtr<File>> create(StringView path, OpenMode flags, mode_t mode = 0644);
/* /*
If path is "-", return standard input (as is common for many CLI apps). Otherwise, open path for reading. If path is "-", return standard input (as is common for many CLI apps). Otherwise, open path for reading.
@ -59,7 +58,7 @@ namespace os
~File(); ~File();
private: private:
static Result<SharedPtr<File>> construct(const Path& path, int flags, mode_t mode); static Result<SharedPtr<File>> construct(StringView path, int flags, mode_t mode);
static void initialize_standard_streams(); static void initialize_standard_streams();
Result<usize> raw_read(u8* buf, usize length); Result<usize> raw_read(u8* buf, usize length);

View File

@ -1,22 +1,23 @@
#pragma once #pragma once
#include <luna/Result.h> #include <luna/Result.h>
#include <luna/StringView.h> #include <luna/StringView.h>
#include <os/Path.h>
#include <sys/types.h> #include <sys/types.h>
namespace os namespace os
{ {
namespace FileSystem namespace FileSystem
{ {
bool exists(const Path& path); bool exists(StringView path);
bool is_directory(const Path& path); bool is_directory(StringView path);
Result<void> create_directory(StringView path, mode_t mode); Result<void> create_directory(StringView path, mode_t mode);
Result<void> remove(const Path& path); Result<void> remove(StringView path);
Result<void> removeat(int dirfd, StringView path);
Result<void> remove_tree(const Path& path); Result<void> remove_tree(StringView path);
Result<void> remove_tree_at(int dirfd, StringView path);
Result<String> working_directory(); Result<String> working_directory();
Result<String> home_directory(); Result<String> home_directory();

View File

@ -1,36 +0,0 @@
#pragma once
#include <fcntl.h>
#include <luna/StringView.h>
namespace os
{
class Path
{
public:
Path(const char* path);
Path(StringView path);
Path(int fd);
Path(int dirfd, StringView name);
int dirfd() const
{
return m_dirfd.value_or(AT_FDCWD);
}
StringView name() const
{
return m_name.value_or(""_sv);
}
int is_empty_path() const
{
if (m_dirfd.has_value() && !m_name.has_value()) return AT_EMPTY_PATH;
return 0;
}
private:
Option<int> m_dirfd;
Option<StringView> m_name;
};
}

View File

@ -18,12 +18,23 @@ static bool should_skip_entry(const char* name, os::Directory::Filter filter)
namespace os namespace os
{ {
Result<SharedPtr<Directory>> Directory::open(StringView path)
Result<SharedPtr<Directory>> Directory::open(const Path& path)
{ {
auto dir = TRY(adopt_shared_if_nonnull(new (std::nothrow) Directory({}))); auto dir = TRY(adopt_shared_if_nonnull(new (std::nothrow) Directory({})));
long rc = syscall(SYS_openat, path.dirfd(), path.name().chars(), O_RDONLY | O_DIRECTORY, 0); DIR* dp = opendir(path.chars());
if (!dp) return err(errno);
dir->m_dirp = dp;
return dir;
}
Result<SharedPtr<Directory>> Directory::openat(int dirfd, StringView path)
{
auto dir = TRY(adopt_shared_if_nonnull(new (std::nothrow) Directory({})));
long rc = syscall(SYS_openat, dirfd, path.chars(), O_RDONLY | O_DIRECTORY, 0);
int fd = TRY(Result<int>::from_syscall(rc)); int fd = TRY(Result<int>::from_syscall(rc));
DIR* dp = fdopendir(fd); DIR* dp = fdopendir(fd);

View File

@ -55,11 +55,11 @@ namespace os
return g_stderr; return g_stderr;
} }
Result<SharedPtr<File>> File::construct(const Path& path, int flags, mode_t mode) Result<SharedPtr<File>> File::construct(StringView path, int flags, mode_t mode)
{ {
auto file = TRY(adopt_shared_if_nonnull(new (std::nothrow) File({}))); auto file = TRY(adopt_shared_if_nonnull(new (std::nothrow) File({})));
long rc = syscall(SYS_openat, path.dirfd(), path.name().chars(), flags, mode); long rc = syscall(SYS_openat, AT_FDCWD, path.chars(), flags, mode);
int fd = TRY(Result<int>::from_syscall(rc)); int fd = TRY(Result<int>::from_syscall(rc));
file->m_fd = fd; file->m_fd = fd;
@ -67,17 +67,17 @@ namespace os
return file; return file;
} }
Result<SharedPtr<File>> File::open(const Path& path, OpenMode flags) Result<SharedPtr<File>> File::open(StringView path, OpenMode flags)
{ {
return construct(path, (int)flags, 0); return construct(path, (int)flags, 0);
} }
Result<SharedPtr<File>> File::open_or_create(const Path& path, OpenMode flags, mode_t mode) Result<SharedPtr<File>> File::open_or_create(StringView path, OpenMode flags, mode_t mode)
{ {
return construct(path, (int)flags | O_CREAT, mode); return construct(path, (int)flags | O_CREAT, mode);
} }
Result<SharedPtr<File>> File::create(const Path& path, OpenMode flags, mode_t mode) Result<SharedPtr<File>> File::create(StringView path, OpenMode flags, mode_t mode)
{ {
return construct(path, (int)flags | (O_CREAT | O_EXCL), mode); return construct(path, (int)flags | (O_CREAT | O_EXCL), mode);
} }

View File

@ -12,20 +12,20 @@
namespace os::FileSystem namespace os::FileSystem
{ {
bool exists(const Path& path) bool exists(StringView path)
{ {
struct stat st; struct stat st;
if (fstatat(path.dirfd(), path.name().chars(), &st, path.is_empty_path()) < 0) return false; if (stat(path.chars(), &st) < 0) return false;
return true; return true;
} }
bool is_directory(const Path& path) bool is_directory(StringView path)
{ {
struct stat st; struct stat st;
if (fstatat(path.dirfd(), path.name().chars(), &st, path.is_empty_path()) < 0) return false; if (stat(path.chars(), &st) < 0) return false;
return S_ISDIR(st.st_mode); return S_ISDIR(st.st_mode);
} }
@ -37,26 +37,36 @@ namespace os::FileSystem
return Result<void>::from_syscall(rc); return Result<void>::from_syscall(rc);
} }
Result<void> remove(const Path& path) Result<void> remove(StringView path)
{ {
long rc = syscall(SYS_unlinkat, path.dirfd(), path.name().chars(), 0); return removeat(AT_FDCWD, path);
}
Result<void> removeat(int dirfd, StringView path)
{
long rc = syscall(SYS_unlinkat, dirfd, path.chars(), 0);
return Result<void>::from_syscall(rc); return Result<void>::from_syscall(rc);
} }
Result<void> remove_tree(const Path& path) Result<void> remove_tree(StringView path)
{ {
auto rc = remove(path); return remove_tree_at(AT_FDCWD, path);
}
Result<void> remove_tree_at(int dirfd, StringView path)
{
auto rc = removeat(dirfd, path);
if (!rc.has_error()) return {}; if (!rc.has_error()) return {};
if (rc.error() != ENOTEMPTY) return rc.release_error(); if (rc.error() != ENOTEMPTY) return rc.release_error();
auto dir = TRY(os::Directory::open(path)); auto dir = TRY(os::Directory::openat(dirfd, path));
Vector<String> entries = TRY(dir->list(os::Directory::Filter::ParentAndBase)); Vector<String> entries = TRY(dir->list(os::Directory::Filter::ParentAndBase));
for (const auto& entry : entries) { TRY(remove_tree({ dir->fd(), entry.view() })); } for (const auto& entry : entries) { TRY(remove_tree_at(dir->fd(), entry.view())); }
return remove(path); return removeat(dirfd, path);
} }
Result<String> working_directory() Result<String> working_directory()

View File

@ -1,20 +0,0 @@
#include <os/Path.h>
namespace os
{
Path::Path(const char* path) : m_dirfd(), m_name(path)
{
}
Path::Path(StringView path) : m_dirfd(), m_name(path)
{
}
Path::Path(int fd) : m_dirfd(fd), m_name()
{
}
Path::Path(int dirfd, StringView name) : m_dirfd(dirfd), m_name(name)
{
}
}