Compare commits
2 Commits
1215c38d75
...
3eb78aa5f3
Author | SHA1 | Date | |
---|---|---|---|
3eb78aa5f3 | |||
abaf24d0da |
@ -132,9 +132,9 @@ static void start_service(Service& service)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static Result<void> load_service(StringView path)
|
static Result<void> load_service(const os::Path& path)
|
||||||
{
|
{
|
||||||
fprintf(g_init_log, "[init] reading service file: %s\n", path.chars());
|
fprintf(g_init_log, "[init] reading service file: %s\n", path.name().chars());
|
||||||
|
|
||||||
auto file = TRY(os::File::open(path, os::File::ReadOnly));
|
auto file = TRY(os::File::open(path, os::File::ReadOnly));
|
||||||
|
|
||||||
@ -242,11 +242,7 @@ 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)
|
for (const auto& entry : services) TRY(load_service({ dir->fd(), entry.view() }));
|
||||||
{
|
|
||||||
auto service_path = TRY(PathParser::join("/etc/init"_sv, entry.view()));
|
|
||||||
TRY(load_service(service_path.view()));
|
|
||||||
}
|
|
||||||
|
|
||||||
return {};
|
return {};
|
||||||
}
|
}
|
||||||
|
@ -1,12 +1,13 @@
|
|||||||
#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<const char*> identify();
|
Result<StringView> identify();
|
||||||
const char* platform_string();
|
StringView platform_string();
|
||||||
|
|
||||||
void platform_init();
|
void platform_init();
|
||||||
void platform_finish_init();
|
void platform_finish_init();
|
||||||
|
@ -184,7 +184,7 @@ static bool test_nx()
|
|||||||
|
|
||||||
namespace CPU
|
namespace CPU
|
||||||
{
|
{
|
||||||
Result<const char*> identify()
|
Result<StringView> 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 brand_string;
|
return StringView { brand_string, 48 };
|
||||||
}
|
}
|
||||||
|
|
||||||
const char* platform_string()
|
StringView platform_string()
|
||||||
{
|
{
|
||||||
return "x86_64";
|
return "x86_64"_sv;
|
||||||
}
|
}
|
||||||
|
|
||||||
void platform_init()
|
void platform_init()
|
||||||
|
@ -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());
|
kinfoln("Current platform: %s", CPU::platform_string().chars());
|
||||||
kinfoln("Current processor: %s", CPU::identify().value_or("(unknown)"));
|
kinfoln("Current processor: %s", CPU::identify().value_or("(unknown)"_sv).chars());
|
||||||
|
|
||||||
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());
|
||||||
|
@ -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(), _UTSNAME_LENGTH);
|
strncpy(result.machine, CPU::platform_string().chars(), _UTSNAME_LENGTH);
|
||||||
|
|
||||||
if (!MemoryManager::copy_to_user_typed(buf, &result)) return err(EFAULT);
|
if (!MemoryManager::copy_to_user_typed(buf, &result)) return err(EFAULT);
|
||||||
|
|
||||||
|
@ -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());
|
CPU::platform_string().chars());
|
||||||
return err(ENOEXEC);
|
return err(ENOEXEC);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -10,6 +10,7 @@ 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})
|
||||||
|
@ -3,15 +3,14 @@
|
|||||||
#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(StringView path);
|
static Result<SharedPtr<Directory>> open(const Path& path);
|
||||||
|
|
||||||
static Result<SharedPtr<Directory>> openat(int dirfd, StringView path);
|
|
||||||
|
|
||||||
enum class Filter
|
enum class Filter
|
||||||
{
|
{
|
||||||
|
@ -4,6 +4,7 @@
|
|||||||
#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
|
||||||
@ -20,9 +21,9 @@ namespace os
|
|||||||
ReadAppend = O_RDWR | O_APPEND,
|
ReadAppend = O_RDWR | O_APPEND,
|
||||||
};
|
};
|
||||||
|
|
||||||
static Result<SharedPtr<File>> open(StringView path, OpenMode flags);
|
static Result<SharedPtr<File>> open(const Path& path, OpenMode flags);
|
||||||
static Result<SharedPtr<File>> open_or_create(StringView path, OpenMode flags, mode_t mode = 0644);
|
static Result<SharedPtr<File>> open_or_create(const Path& path, OpenMode flags, mode_t mode = 0644);
|
||||||
static Result<SharedPtr<File>> create(StringView path, OpenMode flags, mode_t mode = 0644);
|
static Result<SharedPtr<File>> create(const Path& 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.
|
||||||
@ -58,7 +59,7 @@ namespace os
|
|||||||
~File();
|
~File();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
static Result<SharedPtr<File>> construct(StringView path, int flags, mode_t mode);
|
static Result<SharedPtr<File>> construct(const Path& 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);
|
||||||
|
@ -1,23 +1,22 @@
|
|||||||
#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(StringView path);
|
bool exists(const Path& path);
|
||||||
|
|
||||||
bool is_directory(StringView path);
|
bool is_directory(const Path& path);
|
||||||
|
|
||||||
Result<void> create_directory(StringView path, mode_t mode);
|
Result<void> create_directory(StringView path, mode_t mode);
|
||||||
|
|
||||||
Result<void> remove(StringView path);
|
Result<void> remove(const Path& path);
|
||||||
Result<void> removeat(int dirfd, StringView path);
|
|
||||||
|
|
||||||
Result<void> remove_tree(StringView path);
|
Result<void> remove_tree(const Path& 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();
|
||||||
|
36
libos/include/os/Path.h
Normal file
36
libos/include/os/Path.h
Normal file
@ -0,0 +1,36 @@
|
|||||||
|
#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;
|
||||||
|
};
|
||||||
|
}
|
@ -18,23 +18,12 @@ 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({})));
|
||||||
|
|
||||||
DIR* dp = opendir(path.chars());
|
long rc = syscall(SYS_openat, path.dirfd(), path.name().chars(), O_RDONLY | O_DIRECTORY, 0);
|
||||||
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);
|
||||||
|
@ -55,11 +55,11 @@ namespace os
|
|||||||
return g_stderr;
|
return g_stderr;
|
||||||
}
|
}
|
||||||
|
|
||||||
Result<SharedPtr<File>> File::construct(StringView path, int flags, mode_t mode)
|
Result<SharedPtr<File>> File::construct(const Path& 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, AT_FDCWD, path.chars(), flags, mode);
|
long rc = syscall(SYS_openat, path.dirfd(), path.name().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(StringView path, OpenMode flags)
|
Result<SharedPtr<File>> File::open(const Path& path, OpenMode flags)
|
||||||
{
|
{
|
||||||
return construct(path, (int)flags, 0);
|
return construct(path, (int)flags, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
Result<SharedPtr<File>> File::open_or_create(StringView path, OpenMode flags, mode_t mode)
|
Result<SharedPtr<File>> File::open_or_create(const Path& 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(StringView path, OpenMode flags, mode_t mode)
|
Result<SharedPtr<File>> File::create(const Path& 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);
|
||||||
}
|
}
|
||||||
|
@ -12,20 +12,20 @@
|
|||||||
|
|
||||||
namespace os::FileSystem
|
namespace os::FileSystem
|
||||||
{
|
{
|
||||||
bool exists(StringView path)
|
bool exists(const Path& path)
|
||||||
{
|
{
|
||||||
struct stat st;
|
struct stat st;
|
||||||
|
|
||||||
if (stat(path.chars(), &st) < 0) return false;
|
if (fstatat(path.dirfd(), path.name().chars(), &st, path.is_empty_path()) < 0) return false;
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool is_directory(StringView path)
|
bool is_directory(const Path& path)
|
||||||
{
|
{
|
||||||
struct stat st;
|
struct stat st;
|
||||||
|
|
||||||
if (stat(path.chars(), &st) < 0) return false;
|
if (fstatat(path.dirfd(), path.name().chars(), &st, path.is_empty_path()) < 0) return false;
|
||||||
|
|
||||||
return S_ISDIR(st.st_mode);
|
return S_ISDIR(st.st_mode);
|
||||||
}
|
}
|
||||||
@ -37,36 +37,26 @@ namespace os::FileSystem
|
|||||||
return Result<void>::from_syscall(rc);
|
return Result<void>::from_syscall(rc);
|
||||||
}
|
}
|
||||||
|
|
||||||
Result<void> remove(StringView path)
|
Result<void> remove(const Path& path)
|
||||||
{
|
{
|
||||||
return removeat(AT_FDCWD, path);
|
long rc = syscall(SYS_unlinkat, path.dirfd(), path.name().chars(), 0);
|
||||||
}
|
|
||||||
|
|
||||||
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(StringView path)
|
Result<void> remove_tree(const Path& path)
|
||||||
{
|
{
|
||||||
return remove_tree_at(AT_FDCWD, path);
|
auto rc = remove(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::openat(dirfd, path));
|
auto dir = TRY(os::Directory::open(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_at(dir->fd(), entry.view())); }
|
for (const auto& entry : entries) { TRY(remove_tree({ dir->fd(), entry.view() })); }
|
||||||
|
|
||||||
return removeat(dirfd, path);
|
return remove(path);
|
||||||
}
|
}
|
||||||
|
|
||||||
Result<String> working_directory()
|
Result<String> working_directory()
|
||||||
|
20
libos/src/Path.cpp
Normal file
20
libos/src/Path.cpp
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
#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)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user