#include #include #include #include #include #include #include #include #include #include #include namespace os::FileSystem { bool exists(const Path& path) { struct stat st; if (stat(path, st).has_error()) return false; return true; } bool is_directory(const Path& path) { struct stat st; if (stat(path, st).has_error()) return false; return S_ISDIR(st.st_mode); } Result stat(const Path& path, struct stat& st) { long rc = syscall(SYS_fstatat, path.dirfd(), path.name().chars(), &st, path.is_empty_path() ? AT_EMPTY_PATH : 0); return Result::from_syscall(rc); } Result create_directory(StringView path, mode_t mode) { long rc = syscall(SYS_mkdir, path.chars(), mode); return Result::from_syscall(rc); } Result remove(const Path& path) { long rc = syscall(SYS_unlinkat, path.dirfd(), path.name().chars(), 0); return Result::from_syscall(rc); } Result remove_tree(const Path& path) { auto rc = remove(path); if (!rc.has_error()) return {}; if (rc.error() != ENOTEMPTY) return rc.release_error(); auto dir = TRY(os::Directory::open(path)); Vector entries = TRY(dir->list(os::Directory::Filter::ParentAndBase)); for (const auto& entry : entries) { TRY(remove_tree({ dir->fd(), entry.view() })); } return remove(path); } Result working_directory() { char* ptr = getcwd(NULL, 0); if (!ptr) return err(errno); return String { ptr }; } Result home_directory() { char* home = getenv("HOME"); if (home) return String::from_cstring(home); struct passwd* pw = getpwuid(getuid()); if (!pw) return err(ENOENT); return String::from_cstring(pw->pw_dir); } Result change_directory(StringView path) { long rc = syscall(SYS_chdir, path.chars()); return Result::from_syscall(rc); } }