Compare commits

...

3 Commits

12 changed files with 325 additions and 8 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

View File

@ -1,5 +1,9 @@
#include "terminal.h"
#include <glacier/string/str_format.h>
#include <glacier/string/str_split.h>
#include <mammoth/file/file.h>
void Terminal::HandleCharacter(char c) {
console_.WriteChar(c);
if (c == '\n') {
@ -14,11 +18,34 @@ void Terminal::HandleCharacter(char c) {
}
void Terminal::ExecuteCommand(const glcr::String& command) {
if (command == "help") {
auto tokens = glcr::StrSplit(command, ' ');
if (tokens.size() == 0) {
console_.WriteChar('>');
return;
}
glcr::StringView cmd = tokens[0];
if (cmd == "help") {
console_.WriteString("Available Commands: cwd\n");
} else if (command == "cwd") {
} else if (cmd == "cwd") {
console_.WriteString(cwd_);
console_.WriteChar('\n');
} else if (cmd == "ls") {
glcr::StringView directory;
if (tokens.size() > 1) {
directory = tokens[1];
} else {
directory = cwd_;
}
auto files_or = mmth::ListDirectory(directory);
if (!files_or.ok()) {
console_.WriteString(glcr::StrFormat("Error: {}\n", files_or.error()));
} else {
auto& files = files_or.value();
for (uint64_t i = 0; i < files.size(); i++) {
console_.WriteString(files[i]);
console_.WriteChar('\n');
}
}
} else {
console_.WriteString("Unknown command: ");
console_.WriteString(command);

View File

@ -1,5 +1,6 @@
interface VFS {
method OpenFile(OpenFileRequest) -> (OpenFileResponse);
method GetDirectory(GetDirectoryRequest) -> (Directory);
}
message OpenFileRequest {
@ -11,3 +12,12 @@ message OpenFileResponse {
u64 size;
capability memory;
}
message GetDirectoryRequest {
string path;
}
message Directory {
// , separated list of filenames until we have repeated strings.
string filenames;
}

View File

@ -39,3 +39,35 @@ glcr::ErrorCode VFSClient::OpenFile(const OpenFileRequest& request, OpenFileResp
}
glcr::ErrorCode VFSClient::GetDirectory(const GetDirectoryRequest& request, Directory& response) {
uint64_t buffer_size = kBufferSize;
uint64_t cap_size = kCapBufferSize;
const uint32_t kSentinel = 0xBEEFDEAD;
buffer_.WriteAt<uint32_t>(0, kSentinel);
buffer_.WriteAt<uint64_t>(8, 1);
cap_buffer_.Reset();
uint64_t length = request.SerializeToBytes(buffer_, /*offset=*/16, cap_buffer_);
buffer_.WriteAt<uint32_t>(4, 16 + length);
z_cap_t reply_port_cap;
RET_ERR(ZEndpointSend(endpoint_, 16 + length, buffer_.RawPtr(), cap_buffer_.UsedSlots(), cap_buffer_.RawPtr(), &reply_port_cap));
// FIXME: Add a way to zero out the first buffer.
RET_ERR(ZReplyPortRecv(reply_port_cap, &buffer_size, buffer_.RawPtr(), &cap_size, cap_buffer_.RawPtr()));
if (buffer_.At<uint32_t>(0) != kSentinel) {
return glcr::INVALID_RESPONSE;
}
// Check Response Code.
RET_ERR(buffer_.At<uint64_t>(8));
response.ParseFromBytes(buffer_, 16, cap_buffer_);
return glcr::OK;
}

View File

@ -19,6 +19,8 @@ class VFSClient {
[[nodiscard]] glcr::ErrorCode OpenFile(const OpenFileRequest& request, OpenFileResponse& response);
[[nodiscard]] glcr::ErrorCode GetDirectory(const GetDirectoryRequest& request, Directory& response);
private:
z_cap_t endpoint_;
uint64_t kBufferSize = 0x1000;

View File

@ -164,5 +164,123 @@ uint64_t OpenFileResponse::SerializeToBytes(glcr::ByteBuffer& bytes, uint64_t of
// The next extension pointer is the length of the message.
WriteHeader(bytes, offset, core_size, next_extension);
return next_extension;
}
void GetDirectoryRequest::ParseFromBytes(const glcr::ByteBuffer& bytes, uint64_t offset) {
ParseFromBytesInternal(bytes, offset);
}
void GetDirectoryRequest::ParseFromBytes(const glcr::ByteBuffer& bytes, uint64_t offset, const glcr::CapBuffer& caps) {
ParseFromBytesInternal(bytes, offset);
}
void GetDirectoryRequest::ParseFromBytesInternal(const glcr::ByteBuffer& bytes, uint64_t offset) {
CheckHeader(bytes);
// Parse path.
auto path_pointer = bytes.At<ExtPointer>(offset + header_size + (8 * 0));
set_path(bytes.StringAt(offset + path_pointer.offset, path_pointer.length));
}
uint64_t GetDirectoryRequest::SerializeToBytes(glcr::ByteBuffer& bytes, uint64_t offset) const {
uint32_t next_extension = header_size + 8 * 1;
const uint32_t core_size = next_extension;
// Write path.
ExtPointer path_ptr{
.offset = next_extension,
// FIXME: Check downcast of str length.
.length = (uint32_t)path().length(),
};
bytes.WriteStringAt(offset + next_extension, path());
next_extension += path_ptr.length;
bytes.WriteAt<ExtPointer>(offset + header_size + (8 * 0), path_ptr);
// The next extension pointer is the length of the message.
WriteHeader(bytes, offset, core_size, next_extension);
return next_extension;
}
uint64_t GetDirectoryRequest::SerializeToBytes(glcr::ByteBuffer& bytes, uint64_t offset, glcr::CapBuffer& caps) const {
uint32_t next_extension = header_size + 8 * 1;
const uint32_t core_size = next_extension;
uint64_t next_cap = 0;
// Write path.
ExtPointer path_ptr{
.offset = next_extension,
// FIXME: Check downcast of str length.
.length = (uint32_t)path().length(),
};
bytes.WriteStringAt(offset + next_extension, path());
next_extension += path_ptr.length;
bytes.WriteAt<ExtPointer>(offset + header_size + (8 * 0), path_ptr);
// The next extension pointer is the length of the message.
WriteHeader(bytes, offset, core_size, next_extension);
return next_extension;
}
void Directory::ParseFromBytes(const glcr::ByteBuffer& bytes, uint64_t offset) {
ParseFromBytesInternal(bytes, offset);
}
void Directory::ParseFromBytes(const glcr::ByteBuffer& bytes, uint64_t offset, const glcr::CapBuffer& caps) {
ParseFromBytesInternal(bytes, offset);
}
void Directory::ParseFromBytesInternal(const glcr::ByteBuffer& bytes, uint64_t offset) {
CheckHeader(bytes);
// Parse filenames.
auto filenames_pointer = bytes.At<ExtPointer>(offset + header_size + (8 * 0));
set_filenames(bytes.StringAt(offset + filenames_pointer.offset, filenames_pointer.length));
}
uint64_t Directory::SerializeToBytes(glcr::ByteBuffer& bytes, uint64_t offset) const {
uint32_t next_extension = header_size + 8 * 1;
const uint32_t core_size = next_extension;
// Write filenames.
ExtPointer filenames_ptr{
.offset = next_extension,
// FIXME: Check downcast of str length.
.length = (uint32_t)filenames().length(),
};
bytes.WriteStringAt(offset + next_extension, filenames());
next_extension += filenames_ptr.length;
bytes.WriteAt<ExtPointer>(offset + header_size + (8 * 0), filenames_ptr);
// The next extension pointer is the length of the message.
WriteHeader(bytes, offset, core_size, next_extension);
return next_extension;
}
uint64_t Directory::SerializeToBytes(glcr::ByteBuffer& bytes, uint64_t offset, glcr::CapBuffer& caps) const {
uint32_t next_extension = header_size + 8 * 1;
const uint32_t core_size = next_extension;
uint64_t next_cap = 0;
// Write filenames.
ExtPointer filenames_ptr{
.offset = next_extension,
// FIXME: Check downcast of str length.
.length = (uint32_t)filenames().length(),
};
bytes.WriteStringAt(offset + next_extension, filenames());
next_extension += filenames_ptr.length;
bytes.WriteAt<ExtPointer>(offset + header_size + (8 * 0), filenames_ptr);
// The next extension pointer is the length of the message.
WriteHeader(bytes, offset, core_size, next_extension);
return next_extension;
}

View File

@ -49,6 +49,46 @@ class OpenFileResponse {
uint64_t size_;
z_cap_t memory_;
// Parses everything except for caps.
void ParseFromBytesInternal(const glcr::ByteBuffer&, uint64_t offset);
};
class GetDirectoryRequest {
public:
GetDirectoryRequest() {}
// Delete copy and move until implemented.
GetDirectoryRequest(const GetDirectoryRequest&) = delete;
GetDirectoryRequest(GetDirectoryRequest&&) = delete;
void ParseFromBytes(const glcr::ByteBuffer&, uint64_t offset);
void ParseFromBytes(const glcr::ByteBuffer&, uint64_t offset, const glcr::CapBuffer&);
uint64_t SerializeToBytes(glcr::ByteBuffer&, uint64_t offset) const;
uint64_t SerializeToBytes(glcr::ByteBuffer&, uint64_t offset, glcr::CapBuffer&) const;
const glcr::String& path() const { return path_; }
void set_path(const glcr::String& value) { path_ = value; }
private:
glcr::String path_;
// Parses everything except for caps.
void ParseFromBytesInternal(const glcr::ByteBuffer&, uint64_t offset);
};
class Directory {
public:
Directory() {}
// Delete copy and move until implemented.
Directory(const Directory&) = delete;
Directory(Directory&&) = delete;
void ParseFromBytes(const glcr::ByteBuffer&, uint64_t offset);
void ParseFromBytes(const glcr::ByteBuffer&, uint64_t offset, const glcr::CapBuffer&);
uint64_t SerializeToBytes(glcr::ByteBuffer&, uint64_t offset) const;
uint64_t SerializeToBytes(glcr::ByteBuffer&, uint64_t offset, glcr::CapBuffer&) const;
const glcr::String& filenames() const { return filenames_; }
void set_filenames(const glcr::String& value) { filenames_ = value; }
private:
glcr::String filenames_;
// Parses everything except for caps.
void ParseFromBytesInternal(const glcr::ByteBuffer&, uint64_t offset);
};

View File

@ -97,6 +97,17 @@ glcr::ErrorCode VFSServerBase::HandleRequest(const glcr::ByteBuffer& request,
resp_length = yunq_response.SerializeToBytes(response, kHeaderSize, resp_caps);
break;
}
case 1: {
GetDirectoryRequest yunq_request;
Directory yunq_response;
yunq_request.ParseFromBytes(request, kHeaderSize, req_caps);
RET_ERR(HandleGetDirectory(yunq_request, yunq_response));
resp_length = yunq_response.SerializeToBytes(response, kHeaderSize, resp_caps);
break;
}
default: {
return glcr::UNIMPLEMENTED;
}

View File

@ -23,6 +23,8 @@ class VFSServerBase {
[[nodiscard]] virtual glcr::ErrorCode HandleOpenFile(const OpenFileRequest&, OpenFileResponse&) = 0;
[[nodiscard]] virtual glcr::ErrorCode HandleGetDirectory(const GetDirectoryRequest&, Directory&) = 0;
private:
z_cap_t endpoint_;

View File

@ -63,3 +63,48 @@ glcr::ErrorCode VFSServer::HandleOpenFile(const OpenFileRequest& request,
response.set_size(inode->size);
return glcr::OK;
}
glcr::ErrorCode VFSServer::HandleGetDirectory(
const GetDirectoryRequest& request, Directory& response) {
auto path_tokens = glcr::StrSplit(request.path(), '/');
if (path_tokens.at(0) != "") {
return glcr::INVALID_ARGUMENT;
}
// If there is a trailing slash we can get rid of the empty string.
if (path_tokens.at(path_tokens.size() - 1) == "") {
path_tokens.PopBack();
}
ASSIGN_OR_RETURN(auto files, driver_.ReadDirectory(2));
for (uint64_t i = 1; i < path_tokens.size(); i++) {
bool found_token = false;
for (uint64_t j = 0; j < files.size() && !found_token; j++) {
if (path_tokens.at(i) ==
glcr::StringView(files.at(j).name, files.at(j).name_len)) {
ASSIGN_OR_RETURN(files, driver_.ReadDirectory(files.at(j).inode));
found_token = true;
}
}
if (!found_token) {
dbgln("Directory '{}' not found.",
glcr::String(path_tokens.at(i)).cstr());
return glcr::NOT_FOUND;
}
}
glcr::VariableStringBuilder filelist;
for (uint64_t i = 0; i < files.size(); i++) {
filelist.PushBack(glcr::StringView(files.at(i).name, files.at(i).name_len));
filelist.PushBack(',');
}
// Remove trailing comma.
if (filelist.size() > 0) {
filelist.DeleteLast();
}
response.set_filenames(filelist.ToString());
return glcr::OK;
}

View File

@ -12,6 +12,9 @@ class VFSServer : public VFSServerBase {
glcr::ErrorCode HandleOpenFile(const OpenFileRequest&,
OpenFileResponse&) override;
glcr::ErrorCode HandleGetDirectory(const GetDirectoryRequest&,
Directory&) override;
private:
// FIXME: Don't store this as a reference.
Ext2Driver& driver_;