2023-06-09 20:45:06 +00:00
|
|
|
/**
|
|
|
|
* @file Directory.h
|
|
|
|
* @author apio (cloudapio.eu)
|
|
|
|
* @brief A C++-friendly API for directory access.
|
|
|
|
*
|
|
|
|
* @copyright Copyright (c) 2023, the Luna authors.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2023-04-28 19:15:41 +00:00
|
|
|
#pragma once
|
|
|
|
#include <dirent.h>
|
|
|
|
#include <luna/SharedPtr.h>
|
|
|
|
#include <luna/String.h>
|
2023-04-28 20:41:44 +00:00
|
|
|
#include <luna/Vector.h>
|
2023-05-03 15:37:26 +00:00
|
|
|
#include <os/Path.h>
|
2023-04-28 19:15:41 +00:00
|
|
|
|
|
|
|
namespace os
|
|
|
|
{
|
2023-06-09 20:45:06 +00:00
|
|
|
/**
|
|
|
|
* @brief An object-oriented directory handle, which is closed when all references to it go out of scope.
|
|
|
|
*/
|
2023-07-30 09:32:46 +00:00
|
|
|
class Directory : public Shareable
|
2023-04-28 19:15:41 +00:00
|
|
|
{
|
|
|
|
public:
|
2023-06-09 20:45:06 +00:00
|
|
|
/**
|
|
|
|
* @brief Create a new Directory object from a path, and open the directory.
|
|
|
|
*
|
|
|
|
* @param path The path to open.
|
|
|
|
* @return Result<SharedPtr<Directory>> A new Directory object.
|
|
|
|
*/
|
2023-05-03 15:37:26 +00:00
|
|
|
static Result<SharedPtr<Directory>> open(const Path& path);
|
2023-04-28 19:15:41 +00:00
|
|
|
|
2023-06-09 20:45:06 +00:00
|
|
|
/**
|
|
|
|
* @brief A filter to skip certain directory entries.
|
|
|
|
*/
|
2023-04-28 19:15:41 +00:00
|
|
|
enum class Filter
|
|
|
|
{
|
2023-06-09 20:45:06 +00:00
|
|
|
None, // Do not skip anything.
|
|
|
|
Hidden, // Skip all hidden files (that start with a '.' character).
|
|
|
|
ParentAndBase // Skip only the '.' and '..' entries.
|
2023-04-28 19:15:41 +00:00
|
|
|
};
|
|
|
|
|
2023-06-09 20:45:06 +00:00
|
|
|
/**
|
|
|
|
* @brief Read the next entry name from this Directory.
|
|
|
|
*
|
|
|
|
* @param filter The entries to filter out.
|
|
|
|
* @return Result<String> The next entry's name as an owned String object, or an empty string if
|
|
|
|
* there are no more entries.
|
|
|
|
*/
|
2023-04-28 19:15:41 +00:00
|
|
|
Result<String> next(Filter filter);
|
|
|
|
|
2023-06-09 20:45:06 +00:00
|
|
|
/**
|
|
|
|
* @brief Read the list of entry names from this Directory.
|
|
|
|
*
|
|
|
|
* @param filter The entries to filter out.
|
|
|
|
* @return Result<Vector<String>> The list of entry names.
|
|
|
|
*/
|
2023-05-28 19:50:48 +00:00
|
|
|
Result<Vector<String>> list_names(Filter filter);
|
|
|
|
|
2023-06-09 20:45:06 +00:00
|
|
|
/**
|
|
|
|
* @brief A directory entry with extra information.
|
|
|
|
*/
|
2023-05-28 19:50:48 +00:00
|
|
|
struct Entry
|
|
|
|
{
|
2023-06-09 20:45:06 +00:00
|
|
|
String name; // The entry's name.
|
|
|
|
mode_t mode; // The entry's file permissions.
|
|
|
|
usize size; // The entry's size.
|
|
|
|
time_t mtime; // The entry's last modification time.
|
2023-05-28 19:50:48 +00:00
|
|
|
};
|
|
|
|
|
2023-06-09 20:45:06 +00:00
|
|
|
/**
|
|
|
|
* @brief Read the list of entries from this Directory, fetching extra information for each one.
|
|
|
|
*
|
|
|
|
* @param filter The entries to filter out.
|
|
|
|
* @return Result<Vector<Entry>> The list of entries.
|
|
|
|
*/
|
2023-05-28 19:50:48 +00:00
|
|
|
Result<Vector<Entry>> list(Filter filter);
|
2023-04-28 20:41:44 +00:00
|
|
|
|
2023-06-09 20:45:06 +00:00
|
|
|
/**
|
|
|
|
* @brief Rewinds this Directory back to the beginning of its entry list.
|
|
|
|
*/
|
2023-04-28 20:41:44 +00:00
|
|
|
void rewind();
|
|
|
|
|
2023-06-09 20:45:06 +00:00
|
|
|
/**
|
|
|
|
* @brief Returns the file descriptor associated with this Directory.
|
|
|
|
*
|
|
|
|
* @return int The file descriptor.
|
|
|
|
*/
|
2023-04-28 19:15:41 +00:00
|
|
|
int fd()
|
|
|
|
{
|
|
|
|
return dirfd(m_dirp);
|
|
|
|
}
|
|
|
|
|
|
|
|
~Directory();
|
|
|
|
Directory(Badge<Directory>);
|
|
|
|
|
2023-06-09 20:45:06 +00:00
|
|
|
private:
|
2023-04-28 19:15:41 +00:00
|
|
|
DIR* m_dirp { nullptr };
|
|
|
|
};
|
|
|
|
}
|