[VictoriaFalls] Add a get directory ipc call for victoria falls.

This commit is contained in:
Drew Galbraith 2023-11-26 14:48:01 -08:00
parent 7d7cb8024c
commit d0a3bf3936
9 changed files with 263 additions and 0 deletions

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_;