2023-06-09 20:45:06 +00:00
|
|
|
/**
|
|
|
|
* @file FileSystem.h
|
|
|
|
* @author apio (cloudapio.eu)
|
|
|
|
* @brief APIs to read and modify the general file system.
|
|
|
|
*
|
|
|
|
* @copyright Copyright (c) 2023, the Luna authors.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2023-04-13 16:33:04 +00:00
|
|
|
#pragma once
|
|
|
|
#include <luna/Result.h>
|
|
|
|
#include <luna/StringView.h>
|
2023-05-03 15:37:26 +00:00
|
|
|
#include <os/Path.h>
|
2023-06-09 20:45:06 +00:00
|
|
|
#include <sys/stat.h>
|
2023-04-13 16:33:04 +00:00
|
|
|
#include <sys/types.h>
|
|
|
|
|
|
|
|
namespace os
|
|
|
|
{
|
|
|
|
namespace FileSystem
|
|
|
|
{
|
2023-06-09 20:45:06 +00:00
|
|
|
/**
|
|
|
|
* @brief Check whether a given path exists.
|
|
|
|
*
|
|
|
|
* @param path The path to check.
|
|
|
|
* @param follow_symlinks If path is a symbolic link, whether to follow it (defaults to true).
|
|
|
|
* @return true The path exists.
|
|
|
|
* @return false The path does not exist, or it was a symbolic link and following it leaded to a nonexistent
|
|
|
|
* path.
|
|
|
|
*/
|
2023-05-20 19:48:18 +00:00
|
|
|
bool exists(const Path& path, bool follow_symlinks = true);
|
2023-04-13 16:33:04 +00:00
|
|
|
|
2023-06-09 20:45:06 +00:00
|
|
|
/**
|
|
|
|
* @brief Check whether a given path is a directory.
|
|
|
|
*
|
|
|
|
* @param path The path to check.
|
|
|
|
* @param follow_symlinks If path is a symbolic link, whether to follow it (defaults to false).
|
|
|
|
* @return true The path is a directory, or the path was a symbolic link and following it leaded to a directory.
|
|
|
|
* @return false The path does not exist, or it is not a directory.
|
|
|
|
*/
|
2023-05-20 19:48:18 +00:00
|
|
|
bool is_directory(const Path& path, bool follow_symlinks = false);
|
2023-04-13 16:33:04 +00:00
|
|
|
|
2023-06-09 20:45:06 +00:00
|
|
|
/**
|
|
|
|
* @brief Retrieve file status information.
|
|
|
|
*
|
|
|
|
* @param path The path to examine.
|
|
|
|
* @param st The buffer to store file status information in.
|
|
|
|
* @param follow_symlinks If path is a symbolic link, whether to follow it and retrieve its target's status, or
|
|
|
|
* otherwise retrieve the symbolic link's status (defaults to true).
|
|
|
|
* @return Result<void> Whether the operation succeeded.
|
|
|
|
*/
|
2023-05-20 19:48:18 +00:00
|
|
|
Result<void> stat(const Path& path, struct stat& st, bool follow_symlinks = true);
|
2023-05-12 21:47:20 +00:00
|
|
|
|
2023-06-09 20:45:06 +00:00
|
|
|
/**
|
|
|
|
* @brief Create a new directory in the file system.
|
|
|
|
*
|
|
|
|
* @param path The path for the new directory.
|
|
|
|
* @param mode The file permissions to apply to the new directory.
|
|
|
|
* @return Result<void> Whether the operation succeeded.
|
|
|
|
*/
|
2023-04-13 16:33:04 +00:00
|
|
|
Result<void> create_directory(StringView path, mode_t mode);
|
|
|
|
|
2023-06-09 20:45:06 +00:00
|
|
|
/**
|
|
|
|
* @brief Remove a file or empty directory from the file system. (The file will continue to exist if there are
|
|
|
|
* other hard links to it or a process still has it open.)
|
|
|
|
*
|
|
|
|
* @param path The path to remove.
|
|
|
|
* @return Result<void> Whether the operation succeeded.
|
|
|
|
*/
|
2023-05-03 15:37:26 +00:00
|
|
|
Result<void> remove(const Path& path);
|
2023-04-19 16:20:44 +00:00
|
|
|
|
2023-06-09 20:45:06 +00:00
|
|
|
/**
|
|
|
|
* @brief Remove a directory tree from the file system recursively, deleting subfiles and subdirectories as
|
|
|
|
* well.
|
|
|
|
*
|
|
|
|
* @param path The path to remove.
|
|
|
|
* @return Result<void> Whether the operation succeeded.
|
|
|
|
*/
|
2023-05-03 15:37:26 +00:00
|
|
|
Result<void> remove_tree(const Path& path);
|
2023-04-13 16:33:04 +00:00
|
|
|
|
2023-06-09 20:45:06 +00:00
|
|
|
/**
|
|
|
|
* @brief Read the target of a symbolic link.
|
|
|
|
*
|
|
|
|
* @param path The symbolic link's path.
|
|
|
|
* @return Result<String> If the path was not a symbolic link, an empty string, otherwise the symbolic link's
|
|
|
|
* target.
|
|
|
|
*/
|
2023-05-23 13:42:38 +00:00
|
|
|
Result<String> readlink(const Path& path);
|
|
|
|
|
2023-06-09 20:45:06 +00:00
|
|
|
/**
|
|
|
|
* @brief Read the current process's working directory.
|
|
|
|
*
|
|
|
|
* @return Result<String> The working directory as an owned String object.
|
|
|
|
*/
|
2023-04-13 16:33:04 +00:00
|
|
|
Result<String> working_directory();
|
2023-06-09 20:45:06 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Fetch the current user' home directory.
|
|
|
|
*
|
|
|
|
* @return Result<String> The home directory as an owned String object.
|
|
|
|
*/
|
2023-04-13 16:33:04 +00:00
|
|
|
Result<String> home_directory();
|
2023-04-18 16:46:19 +00:00
|
|
|
|
2023-06-09 20:45:06 +00:00
|
|
|
/**
|
|
|
|
* @brief Change the current process's working directory.
|
|
|
|
*
|
|
|
|
* @param path The new working directory.
|
|
|
|
* @return Result<void> Whether the operation succeeded.
|
|
|
|
*/
|
2023-04-18 16:46:19 +00:00
|
|
|
Result<void> change_directory(StringView path);
|
2023-04-13 16:33:04 +00:00
|
|
|
}
|
|
|
|
}
|