diff --git a/lib/mammoth/file/file.cpp b/lib/mammoth/file/file.cpp index 53407a8..ed6ce66 100644 --- a/lib/mammoth/file/file.cpp +++ b/lib/mammoth/file/file.cpp @@ -1,5 +1,6 @@ #include "file/file.h" +#include #include #include #include @@ -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(file_data_.vaddr()); } +glcr::ErrorOr> 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 files; + for (uint64_t i = 0; i < file_views.size(); i++) { + files.PushBack(file_views[i]); + } + return files; +} + } // namespace mmth diff --git a/lib/mammoth/file/file.h b/lib/mammoth/file/file.h index 176960c..7b55f2e 100644 --- a/lib/mammoth/file/file.h +++ b/lib/mammoth/file/file.h @@ -1,6 +1,9 @@ #pragma once +#include #include +#include +#include #include #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> ListDirectory(glcr::StringView path); + } // namespace mmth