Skip to content

Commit

Permalink
Merge pull request #22 from uqbar-dao/bp/filefuncs
Browse files Browse the repository at this point in the history
vfs: add read_to_string and read_to_end
  • Loading branch information
dr-frmr authored Jan 8, 2024
2 parents ec733e0 + 7e3516e commit 6228e0f
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/vfs/directory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ impl Directory {
}

/// Opens or creates a directory at path.
/// If trying to create an existing file, will just give you the path.
/// If trying to create an existing directory, will just give you the path.
pub fn open_dir(path: &str, create: bool) -> anyhow::Result<Directory> {
if !create {
return Ok(Directory {
Expand Down
82 changes: 82 additions & 0 deletions src/vfs/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,63 @@ impl File {
}
}

/// Reads until end of file from current cursor position
/// Returns a vector of bytes.
pub fn read_to_end(&self) -> anyhow::Result<Vec<u8>> {
let request = VfsRequest {
path: self.path.clone(),
action: VfsAction::ReadToEnd,
};
let message = Request::new()
.target(("our", "vfs", "sys", "uqbar"))
.ipc(serde_json::to_vec(&request)?)
.send_and_await_response(5)?;

match message {
Ok(Message::Response { ipc, .. }) => {
let response = serde_json::from_slice::<VfsResponse>(&ipc)?;
match response {
VfsResponse::Read => {
let data = match get_payload() {
Some(bytes) => bytes.bytes,
None => return Err(anyhow::anyhow!("vfs: no read payload")),
};
Ok(data)
}
VfsResponse::Err(e) => Err(e.into()),
_ => Err(anyhow::anyhow!("vfs: unexpected response: {:?}", response)),
}
}
_ => Err(anyhow::anyhow!("vfs: unexpected message: {:?}", message)),
}
}

/// Reads until end of file from current cursor position, converts to String.
/// Throws error if bytes aren't valid utf-8.
/// Returns a vector of bytes.
pub fn read_to_string(&self) -> anyhow::Result<String> {
let request = VfsRequest {
path: self.path.clone(),
action: VfsAction::ReadToString,
};
let message = Request::new()
.target(("our", "vfs", "sys", "uqbar"))
.ipc(serde_json::to_vec(&request)?)
.send_and_await_response(5)?;

match message {
Ok(Message::Response { ipc, .. }) => {
let response = serde_json::from_slice::<VfsResponse>(&ipc)?;
match response {
VfsResponse::ReadToString(s) => Ok(s),
VfsResponse::Err(e) => Err(e.into()),
_ => Err(anyhow::anyhow!("vfs: unexpected response: {:?}", response)),
}
}
_ => Err(anyhow::anyhow!("vfs: unexpected message: {:?}", message)),
}
}

/// Write entire slice as the new file.
/// Truncates anything that existed at path before.
pub fn write(&self, buffer: &[u8]) -> anyhow::Result<()> {
Expand Down Expand Up @@ -159,6 +216,31 @@ impl File {
}
}

/// Write buffer to the end position of file.
pub fn append(&mut self, buffer: &[u8]) -> anyhow::Result<()> {
let request = VfsRequest {
path: self.path.clone(),
action: VfsAction::Append,
};
let message = Request::new()
.target(("our", "vfs", "sys", "uqbar"))
.ipc(serde_json::to_vec(&request)?)
.payload_bytes(buffer)
.send_and_await_response(5)?;

match message {
Ok(Message::Response { ipc, .. }) => {
let response = serde_json::from_slice::<VfsResponse>(&ipc)?;
match response {
VfsResponse::Ok => Ok(()),
VfsResponse::Err(e) => Err(e.into()),
_ => Err(anyhow::anyhow!("vfs: unexpected response: {:?}", response)),
}
}
_ => Err(anyhow::anyhow!("vfs: unexpected message: {:?}", message)),
}
}

/// Seek file to position.
/// Returns the new position.
pub fn seek(&mut self, pos: SeekFrom) -> anyhow::Result<u64> {
Expand Down

0 comments on commit 6228e0f

Please sign in to comment.