[Mammoth] Add a call for listing a directory.

This commit is contained in:
Drew Galbraith 2023-11-26 14:48:20 -08:00
parent d0a3bf3936
commit e0bf7a9a1c
2 changed files with 33 additions and 6 deletions

View File

@ -1,5 +1,6 @@
#include "file/file.h"
#include <glacier/string/str_split.h>
#include <victoriafalls/victoriafalls.yunq.client.h>
#include <yellowstone/yellowstone.yunq.client.h>
#include <zglobal.h>
@ -11,12 +12,8 @@ namespace {
VFSClient* gVfsClient = nullptr;
} // namespace
void SetVfsCap(z_cap_t vfs_cap) { gVfsClient = new VFSClient(vfs_cap); }
File File::Open(glcr::StringView path) {
if (gVfsClient == 0) {
void GetVfsClientIfNeeded() {
if (gVfsClient == nullptr) {
YellowstoneClient client(gInitEndpointCap);
GetEndpointRequest yreq;
@ -26,6 +23,14 @@ File File::Open(glcr::StringView path) {
gVfsClient = new VFSClient(yresp.endpoint());
}
}
} // namespace
void SetVfsCap(z_cap_t vfs_cap) { gVfsClient = new VFSClient(vfs_cap); }
File File::Open(glcr::StringView path) {
GetVfsClientIfNeeded();
OpenFileRequest req;
req.set_path(path);
@ -44,4 +49,20 @@ uint8_t* File::byte_ptr() {
return reinterpret_cast<uint8_t*>(file_data_.vaddr());
}
glcr::ErrorOr<glcr::Vector<glcr::String>> ListDirectory(glcr::StringView path) {
GetVfsClientIfNeeded();
GetDirectoryRequest req;
req.set_path(path);
Directory dir;
RET_ERR(gVfsClient->GetDirectory(req, dir));
auto file_views = glcr::StrSplit(dir.filenames(), ',');
glcr::Vector<glcr::String> files;
for (uint64_t i = 0; i < file_views.size(); i++) {
files.PushBack(file_views[i]);
}
return files;
}
} // namespace mmth

View File

@ -1,6 +1,9 @@
#pragma once
#include <glacier/container/vector.h>
#include <glacier/memory/move.h>
#include <glacier/status/error_or.h>
#include <glacier/string/string.h>
#include <glacier/string/string_view.h>
#include "mammoth/util/memory_region.h"
@ -29,4 +32,7 @@ class File {
: file_data_(glcr::Move(file)), size_(size) {}
};
// TODO: Move this to a separate file.
glcr::ErrorOr<glcr::Vector<glcr::String>> ListDirectory(glcr::StringView path);
} // namespace mmth