Skip to content

Commit

Permalink
feat(vfs) Update Metadata.len when updating the file buffer.
Browse files Browse the repository at this point in the history
  • Loading branch information
Hywan committed Sep 2, 2021
1 parent 378550a commit 9581dec
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 7 deletions.
32 changes: 25 additions & 7 deletions lib/vfs/src/mem_fs/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ impl VirtualFile for FileHandle {
};

match fs.storage.get(self.inode) {
Some(Node::File { file, .. }) => file.buffer.len().try_into().unwrap_or(0),
Some(Node::File { file, .. }) => file.len().try_into().unwrap_or(0),
_ => 0,
}
}
Expand All @@ -107,9 +107,11 @@ impl VirtualFile for FileHandle {
.map_err(|_| FsError::Lock)?;

match fs.storage.get_mut(self.inode) {
Some(Node::File { file, .. }) => file
.buffer
.resize(new_size.try_into().map_err(|_| FsError::UnknownError)?, 0),
Some(Node::File { file, metadata, .. }) => {
file.buffer
.resize(new_size.try_into().map_err(|_| FsError::UnknownError)?, 0);
metadata.len = new_size;
}
_ => return Err(FsError::NotAFile),
}

Expand Down Expand Up @@ -561,8 +563,8 @@ impl Write for FileHandle {
io::Error::new(io::ErrorKind::Other, "failed to acquire a write lock")
})?;

let file = match fs.storage.get_mut(self.inode) {
Some(Node::File { file, .. }) => file,
let (file, metadata) = match fs.storage.get_mut(self.inode) {
Some(Node::File { file, metadata, .. }) => (file, metadata),
_ => {
return Err(io::Error::new(
io::ErrorKind::NotFound,
Expand All @@ -571,7 +573,11 @@ impl Write for FileHandle {
}
};

file.write(buf)
let bytes_written = file.write(buf)?;

metadata.len = file.len().try_into().unwrap();

Ok(bytes_written)
}

fn flush(&mut self) -> io::Result<()> {
Expand Down Expand Up @@ -687,6 +693,10 @@ mod test_read_write_seek {
.open(path!("/foo.txt"))
.expect("failed to create a new file");

assert!(
matches!(fs.metadata(path!("/foo.txt")), Ok(Metadata { len: 0, .. })),
"checking the `metadata.len` is 0",
);
assert!(
matches!(file.write(b"foobarbazqux"), Ok(12)),
"writing `foobarbazqux`",
Expand Down Expand Up @@ -715,6 +725,10 @@ mod test_read_write_seek {
"reading more bytes than available",
);
assert_eq!(buffer[..12], b"foobarbazqux"[..], "checking the 12 bytes");
assert!(
matches!(fs.metadata(path!("/foo.txt")), Ok(Metadata { len: 12, .. })),
"checking the `metadata.len` is 0",
);
}

#[test]
Expand Down Expand Up @@ -847,6 +861,10 @@ impl File {
self.buffer.clear();
self.cursor = 0;
}

pub(super) fn len(&self) -> usize {
self.buffer.len()
}
}

impl Read for File {
Expand Down
1 change: 1 addition & 0 deletions lib/vfs/src/mem_fs/file_opener.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ impl crate::FileOpener for FileOpener {
// Truncate if needed.
if truncate {
file.truncate();
metadata.len = 0;
}

// Move the cursor to the end if needed.
Expand Down

0 comments on commit 9581dec

Please sign in to comment.