2023-04-19 16:20:44 +00:00
|
|
|
#include <dirent.h>
|
2023-04-13 16:33:04 +00:00
|
|
|
#include <errno.h>
|
2023-04-18 17:36:29 +00:00
|
|
|
#include <fcntl.h>
|
2023-05-02 08:50:39 +00:00
|
|
|
#include <luna/String.h>
|
|
|
|
#include <os/Directory.h>
|
|
|
|
#include <os/FileSystem.h>
|
2023-04-13 16:33:04 +00:00
|
|
|
#include <pwd.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/syscall.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
namespace os::FileSystem
|
|
|
|
{
|
|
|
|
bool exists(StringView path)
|
|
|
|
{
|
|
|
|
struct stat st;
|
|
|
|
|
|
|
|
if (stat(path.chars(), &st) < 0) return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool is_directory(StringView path)
|
|
|
|
{
|
|
|
|
struct stat st;
|
|
|
|
|
|
|
|
if (stat(path.chars(), &st) < 0) return false;
|
|
|
|
|
|
|
|
return S_ISDIR(st.st_mode);
|
|
|
|
}
|
|
|
|
|
|
|
|
Result<void> create_directory(StringView path, mode_t mode)
|
|
|
|
{
|
|
|
|
long rc = syscall(SYS_mkdir, path.chars(), mode);
|
|
|
|
|
2023-04-18 16:49:24 +00:00
|
|
|
return Result<void>::from_syscall(rc);
|
2023-04-13 16:33:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Result<void> remove(StringView path)
|
|
|
|
{
|
2023-04-19 16:20:44 +00:00
|
|
|
return removeat(AT_FDCWD, path);
|
|
|
|
}
|
|
|
|
|
|
|
|
Result<void> removeat(int dirfd, StringView path)
|
|
|
|
{
|
|
|
|
long rc = syscall(SYS_unlinkat, dirfd, path.chars(), 0);
|
2023-04-13 16:33:04 +00:00
|
|
|
|
2023-04-18 16:49:24 +00:00
|
|
|
return Result<void>::from_syscall(rc);
|
2023-04-13 16:33:04 +00:00
|
|
|
}
|
|
|
|
|
2023-04-19 16:20:44 +00:00
|
|
|
Result<void> remove_tree(StringView 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.error() != ENOTEMPTY) return rc.release_error();
|
|
|
|
|
2023-04-28 19:16:29 +00:00
|
|
|
auto dir = TRY(os::Directory::openat(dirfd, path));
|
2023-04-19 16:20:44 +00:00
|
|
|
|
2023-04-28 20:41:44 +00:00
|
|
|
Vector<String> entries = TRY(dir->list(os::Directory::Filter::ParentAndBase));
|
2023-04-19 16:20:44 +00:00
|
|
|
|
2023-04-28 19:16:29 +00:00
|
|
|
for (const auto& entry : entries) { TRY(remove_tree_at(dir->fd(), entry.view())); }
|
2023-04-19 16:20:44 +00:00
|
|
|
|
|
|
|
return removeat(dirfd, path);
|
|
|
|
}
|
|
|
|
|
2023-04-13 16:33:04 +00:00
|
|
|
Result<String> working_directory()
|
|
|
|
{
|
|
|
|
char* ptr = getcwd(NULL, 0);
|
|
|
|
if (!ptr) return err(errno);
|
|
|
|
return String { ptr };
|
|
|
|
}
|
|
|
|
|
|
|
|
Result<String> 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);
|
|
|
|
}
|
2023-04-18 16:46:19 +00:00
|
|
|
|
|
|
|
Result<void> change_directory(StringView path)
|
|
|
|
{
|
|
|
|
long rc = syscall(SYS_chdir, path.chars());
|
|
|
|
|
2023-04-18 16:49:24 +00:00
|
|
|
return Result<void>::from_syscall(rc);
|
2023-04-18 16:46:19 +00:00
|
|
|
}
|
2023-04-13 16:33:04 +00:00
|
|
|
}
|