Luna/libos/src/FileSystem.cpp

106 lines
2.6 KiB
C++
Raw Normal View History

/**
* @file FileSystem.cpp
* @author apio (cloudapio.eu)
* @brief APIs to read and modify the general file system.
*
* @copyright Copyright (c) 2023, the Luna authors.
*
*/
#include <dirent.h>
2023-04-13 16:33:04 +00:00
#include <errno.h>
#include <fcntl.h>
#include <luna/Buffer.h>
2023-05-23 13:42:38 +00:00
#include <luna/PathParser.h>
#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 <unistd.h>
namespace os::FileSystem
{
2023-05-20 19:48:18 +00:00
bool exists(const Path& path, bool follow_symlinks)
2023-04-13 16:33:04 +00:00
{
struct stat st;
2023-05-20 19:48:18 +00:00
if (stat(path, st, follow_symlinks).has_error()) return false;
2023-04-13 16:33:04 +00:00
return true;
}
2023-05-20 19:48:18 +00:00
bool is_directory(const Path& path, bool follow_symlinks)
2023-04-13 16:33:04 +00:00
{
struct stat st;
2023-05-20 19:48:18 +00:00
if (stat(path, st, follow_symlinks).has_error()) return false;
2023-04-13 16:33:04 +00:00
return S_ISDIR(st.st_mode);
}
2023-05-20 19:48:18 +00:00
Result<void> stat(const Path& path, struct stat& st, bool follow_symlinks)
2023-05-12 21:47:20 +00:00
{
int rc = fstatat(path.dirfd(), path.name().chars(), &st,
(int)(path.is_empty_path() | (follow_symlinks ? 0 : AT_SYMLINK_NOFOLLOW)));
2023-05-12 21:47:20 +00:00
if (rc < 0) return err(errno);
return {};
2023-05-12 21:47:20 +00:00
}
2023-04-13 16:33:04 +00:00
Result<void> create_directory(StringView path, mode_t mode)
{
int rc = mkdir(path.chars(), mode);
if (rc < 0) return err(errno);
return {};
2023-04-13 16:33:04 +00:00
}
Result<void> remove(const Path& path)
2023-04-13 16:33:04 +00:00
{
// FIXME: This will not work on many operating systems that require rmdir() for directories.
int rc = unlinkat(path.dirfd(), path.name().chars(), 0);
if (rc < 0) return err(errno);
return {};
2023-04-13 16:33:04 +00:00
}
2023-05-23 13:42:38 +00:00
Result<String> readlink(const Path& path)
{
struct stat st;
TRY(stat(path, st, false));
if (!S_ISLNK(st.st_mode)) return String {};
Buffer buf = TRY(Buffer::create_sized(st.st_size + 1));
memset(buf.data(), 0, buf.size());
2023-05-23 13:42:38 +00:00
ssize_t nread = readlinkat(path.dirfd(), path.name().chars(), (char*)buf.data(), st.st_size);
if (nread < 0) return err(errno);
2023-05-23 13:42:38 +00:00
return String { (char*)buf.release_data(), (usize)nread };
2023-05-23 13:42:38 +00:00
}
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)
{
int rc = chdir(path.chars());
if (rc < 0) return err(errno);
return {};
2023-04-18 16:46:19 +00:00
}
2023-04-13 16:33:04 +00:00
}