Skip to content

Commit

Permalink
Reading from a WebCFile never advanced its cursor
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael-F-Bryan committed Mar 16, 2023
1 parent 3c2be49 commit ec67851
Showing 1 changed file with 40 additions and 11 deletions.
51 changes: 40 additions & 11 deletions lib/vfs/src/webc_fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use crate::{
FileOpener, FileSystem, FsError, Metadata, OpenOptions, OpenOptionsConfig, ReadDir, VirtualFile,
};
use anyhow::anyhow;
use std::convert::TryInto;
use std::convert::{TryFrom, TryInto};
use std::io::{self, Error as IoError, ErrorKind as IoErrorKind, SeekFrom};
use std::ops::Deref;
use std::path::Path;
Expand Down Expand Up @@ -177,7 +177,7 @@ where
T: Deref<Target = WebC<'static>>,
{
fn poll_read(
self: Pin<&mut Self>,
mut self: Pin<&mut Self>,
_cx: &mut Context<'_>,
buf: &mut tokio::io::ReadBuf<'_>,
) -> Poll<io::Result<()>> {
Expand All @@ -194,16 +194,15 @@ where
.get_file_bytes(&self.entry)
.map_err(|e| IoError::new(IoErrorKind::NotFound, e))?;

let cursor: usize = self.cursor.try_into().unwrap_or(u32::MAX as usize);
let _start = cursor.min(bytes.len());
let bytes = &bytes[cursor..];
let start: usize = self.cursor.try_into().unwrap();
let remaining = &bytes[start..];
let bytes_read = remaining.len().min(buf.remaining());
let end = start + bytes_read;
let bytes = &bytes[start..end];

buf.put_slice(bytes);
self.cursor += u64::try_from(bytes_read).unwrap();

if bytes.len() > buf.remaining() {
let remaining = buf.remaining();
buf.put_slice(&bytes[..remaining]);
} else {
buf.put_slice(bytes);
}
Poll::Ready(Ok(()))
}
}
Expand Down Expand Up @@ -446,3 +445,33 @@ fn translate_file_type(f: FsEntryType) -> crate::FileType {
fifo: false,
}
}

#[cfg(test)]
mod tests {
use tokio::io::AsyncReadExt;
use webc::v1::{ParseOptions, WebCOwned};

use super::*;

#[tokio::test]
async fn read_a_file_from_the_webc_fs() {
let webc: &[u8] = include_bytes!("../../c-api/examples/assets/python-0.1.0.wasmer");
let options = ParseOptions::default();
let webc = WebCOwned::parse(webc.to_vec(), &options).unwrap();

let fs = WebcFileSystem::init_all(Arc::new(webc));

let mut f = fs
.new_open_options()
.read(true)
.open(Path::new("/lib/python3.6/collections/abc.py"))
.unwrap();

let mut abc_py = String::new();
f.read_to_string(&mut abc_py).await.unwrap();
assert_eq!(
abc_py,
"from _collections_abc import *\nfrom _collections_abc import __all__\n"
);
}
}

0 comments on commit ec67851

Please sign in to comment.