libos+init: Add a new Path class to handle both file descriptors and file paths
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
parent
abaf24d0da
commit
3eb78aa5f3
@ -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));
|
||||
|
||||
@ -242,11 +242,7 @@ static Result<void> load_services()
|
||||
auto services = TRY(dir->list(os::Directory::Filter::ParentAndBase));
|
||||
sort(services.begin(), services.end(), String::compare);
|
||||
|
||||
for (const auto& entry : services)
|
||||
{
|
||||
auto service_path = TRY(PathParser::join("/etc/init"_sv, entry.view()));
|
||||
TRY(load_service(service_path.view()));
|
||||
}
|
||||
for (const auto& entry : services) TRY(load_service({ dir->fd(), entry.view() }));
|
||||
|
||||
return {};
|
||||
}
|
||||
|
@ -10,6 +10,7 @@ set(SOURCES
|
||||
src/Process.cpp
|
||||
src/Directory.cpp
|
||||
src/Main.cpp
|
||||
src/Path.cpp
|
||||
)
|
||||
|
||||
add_library(os ${SOURCES})
|
||||
|
@ -3,15 +3,14 @@
|
||||
#include <luna/SharedPtr.h>
|
||||
#include <luna/String.h>
|
||||
#include <luna/Vector.h>
|
||||
#include <os/Path.h>
|
||||
|
||||
namespace os
|
||||
{
|
||||
class Directory
|
||||
{
|
||||
public:
|
||||
static Result<SharedPtr<Directory>> open(StringView path);
|
||||
|
||||
static Result<SharedPtr<Directory>> openat(int dirfd, StringView path);
|
||||
static Result<SharedPtr<Directory>> open(const Path& path);
|
||||
|
||||
enum class Filter
|
||||
{
|
||||
|
@ -4,6 +4,7 @@
|
||||
#include <luna/Result.h>
|
||||
#include <luna/SharedPtr.h>
|
||||
#include <luna/StringView.h>
|
||||
#include <os/Path.h>
|
||||
#include <sys/types.h>
|
||||
|
||||
namespace os
|
||||
@ -20,9 +21,9 @@ namespace os
|
||||
ReadAppend = O_RDWR | O_APPEND,
|
||||
};
|
||||
|
||||
static Result<SharedPtr<File>> open(StringView path, OpenMode flags);
|
||||
static Result<SharedPtr<File>> open_or_create(StringView path, OpenMode flags, mode_t mode = 0644);
|
||||
static Result<SharedPtr<File>> create(StringView path, OpenMode flags, mode_t mode = 0644);
|
||||
static Result<SharedPtr<File>> open(const Path& path, OpenMode flags);
|
||||
static Result<SharedPtr<File>> open_or_create(const Path& 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.
|
||||
@ -58,7 +59,7 @@ namespace os
|
||||
~File();
|
||||
|
||||
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();
|
||||
|
||||
Result<usize> raw_read(u8* buf, usize length);
|
||||
|
@ -1,23 +1,22 @@
|
||||
#pragma once
|
||||
#include <luna/Result.h>
|
||||
#include <luna/StringView.h>
|
||||
#include <os/Path.h>
|
||||
#include <sys/types.h>
|
||||
|
||||
namespace os
|
||||
{
|
||||
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> remove(StringView path);
|
||||
Result<void> removeat(int dirfd, StringView path);
|
||||
Result<void> remove(const Path& path);
|
||||
|
||||
Result<void> remove_tree(StringView path);
|
||||
Result<void> remove_tree_at(int dirfd, StringView path);
|
||||
Result<void> remove_tree(const Path& path);
|
||||
|
||||
Result<String> working_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
|
||||
{
|
||||
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({})));
|
||||
|
||||
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);
|
||||
long rc = syscall(SYS_openat, path.dirfd(), path.name().chars(), O_RDONLY | O_DIRECTORY, 0);
|
||||
int fd = TRY(Result<int>::from_syscall(rc));
|
||||
|
||||
DIR* dp = fdopendir(fd);
|
||||
|
@ -55,11 +55,11 @@ namespace os
|
||||
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({})));
|
||||
|
||||
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));
|
||||
|
||||
file->m_fd = fd;
|
||||
@ -67,17 +67,17 @@ namespace os
|
||||
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);
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
@ -12,20 +12,20 @@
|
||||
|
||||
namespace os::FileSystem
|
||||
{
|
||||
bool exists(StringView path)
|
||||
bool exists(const Path& path)
|
||||
{
|
||||
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;
|
||||
}
|
||||
|
||||
bool is_directory(StringView path)
|
||||
bool is_directory(const Path& path)
|
||||
{
|
||||
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);
|
||||
}
|
||||
@ -37,36 +37,26 @@ namespace os::FileSystem
|
||||
return Result<void>::from_syscall(rc);
|
||||
}
|
||||
|
||||
Result<void> remove(StringView path)
|
||||
Result<void> remove(const Path& path)
|
||||
{
|
||||
return removeat(AT_FDCWD, path);
|
||||
}
|
||||
|
||||
Result<void> removeat(int dirfd, StringView path)
|
||||
{
|
||||
long rc = syscall(SYS_unlinkat, dirfd, path.chars(), 0);
|
||||
long rc = syscall(SYS_unlinkat, path.dirfd(), path.name().chars(), 0);
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
Result<void> remove_tree_at(int dirfd, StringView path)
|
||||
{
|
||||
auto rc = removeat(dirfd, path);
|
||||
auto rc = remove(path);
|
||||
if (!rc.has_error()) return {};
|
||||
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));
|
||||
|
||||
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()
|
||||
|
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