libos: Add a way to also list entry sizes, modes and mtimes
This commit is contained in:
parent
11a4f8cc90
commit
e864ef2d36
@ -224,7 +224,7 @@ static Result<void> load_services()
|
||||
{
|
||||
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);
|
||||
|
||||
for (const auto& entry : services) TRY(load_service({ dir->fd(), entry.view() }));
|
||||
|
@ -21,7 +21,17 @@ namespace os
|
||||
|
||||
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();
|
||||
|
||||
|
@ -2,6 +2,7 @@
|
||||
#include <fcntl.h>
|
||||
#include <luna/CString.h>
|
||||
#include <os/Directory.h>
|
||||
#include <os/FileSystem.h>
|
||||
#include <sys/syscall.h>
|
||||
#include <unistd.h>
|
||||
|
||||
@ -54,7 +55,7 @@ namespace os
|
||||
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;
|
||||
|
||||
@ -66,6 +67,31 @@ namespace os
|
||||
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()
|
||||
{
|
||||
rewinddir(m_dirp);
|
||||
|
@ -62,7 +62,7 @@ namespace os::FileSystem
|
||||
|
||||
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() })); }
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user