libos: Add a way to also list entry sizes, modes and mtimes

This commit is contained in:
apio 2023-05-28 21:50:48 +02:00
parent 11a4f8cc90
commit e864ef2d36
Signed by: apio
GPG Key ID: B8A7D06E42258954
4 changed files with 40 additions and 4 deletions

View File

@ -224,7 +224,7 @@ static Result<void> load_services()
{ {
auto dir = TRY(os::Directory::open("/etc/init")); auto dir = TRY(os::Directory::open("/etc/init"));
auto services = TRY(dir->list(os::Directory::Filter::ParentAndBase)); auto services = TRY(dir->list_names(os::Directory::Filter::ParentAndBase));
sort(services.begin(), services.end(), String::compare); sort(services.begin(), services.end(), String::compare);
for (const auto& entry : services) TRY(load_service({ dir->fd(), entry.view() })); for (const auto& entry : services) TRY(load_service({ dir->fd(), entry.view() }));

View File

@ -21,7 +21,17 @@ namespace os
Result<String> next(Filter filter); Result<String> next(Filter filter);
Result<Vector<String>> list(Filter filter); Result<Vector<String>> list_names(Filter filter);
struct Entry
{
String name;
mode_t mode;
usize size;
time_t mtime;
};
Result<Vector<Entry>> list(Filter filter);
void rewind(); void rewind();

View File

@ -2,6 +2,7 @@
#include <fcntl.h> #include <fcntl.h>
#include <luna/CString.h> #include <luna/CString.h>
#include <os/Directory.h> #include <os/Directory.h>
#include <os/FileSystem.h>
#include <sys/syscall.h> #include <sys/syscall.h>
#include <unistd.h> #include <unistd.h>
@ -54,7 +55,7 @@ namespace os
return String::from_cstring(ent->d_name); return String::from_cstring(ent->d_name);
} }
Result<Vector<String>> Directory::list(Filter filter) Result<Vector<String>> Directory::list_names(Filter filter)
{ {
Vector<String> result; Vector<String> result;
@ -66,6 +67,31 @@ namespace os
return result; return result;
} }
Result<Vector<Directory::Entry>> Directory::list(Filter filter)
{
Vector<Directory::Entry> result;
rewind();
String name {};
while ((name = TRY(next(filter))), !name.is_empty())
{
struct stat st;
TRY(FileSystem::stat({ dirfd(m_dirp), name.chars() }, st, false));
Directory::Entry entry {
.name = move(name),
.mode = st.st_mode,
.size = (usize)st.st_size,
.mtime = st.st_mtime,
};
TRY(result.try_append(move(entry)));
}
return result;
}
void Directory::rewind() void Directory::rewind()
{ {
rewinddir(m_dirp); rewinddir(m_dirp);

View File

@ -62,7 +62,7 @@ namespace os::FileSystem
auto dir = TRY(os::Directory::open(path)); auto dir = TRY(os::Directory::open(path));
Vector<String> entries = TRY(dir->list(os::Directory::Filter::ParentAndBase)); Vector<String> entries = TRY(dir->list_names(os::Directory::Filter::ParentAndBase));
for (const auto& entry : entries) { TRY(remove_tree({ dir->fd(), entry.view() })); } for (const auto& entry : entries) { TRY(remove_tree({ dir->fd(), entry.view() })); }