Skip to content

Commit

Permalink
clean-up
Browse files Browse the repository at this point in the history
  • Loading branch information
wasm-forge committed Aug 7, 2024
1 parent 5d8be0b commit 66dc545
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 35 deletions.
4 changes: 2 additions & 2 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ pub enum Error {
InvalidOpenFlags,
InvalidFdFlags,
FileAlreadyExists,
FileIsNotMounted,
IsMountedAlready,
MemoryFileIsNotMounted,
MemoryFileIsMountedAlready,
NameTooLong,
DirectoryNotEmpty,
ExpectedToRemoveFile,
Expand Down
38 changes: 9 additions & 29 deletions src/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,8 @@ impl FileSystem {
}
}

// mounte memory file on the top of the given file name, if the file does not exist, it will be created.
// The method fails if the file system couldn't create the file.
// mount memory on the top of the given host file name, if the file does not exist, it will be created.
// The method fails if the file system could not open or create the file.
pub fn mount_memory_file(
&mut self,
filename: &str,
Expand Down Expand Up @@ -141,29 +141,6 @@ impl FileSystem {
self.storage.init_mounted_memory(node)
}

// helper method to set the size of the mounted memory
pub fn set_memory_file_size(
&mut self,
filename: &str,
new_size: FileSize,
) -> Result<(), Error> {
let root_node = self.get_node(self.root_fd())?;

let node = find_node(root_node, filename, self.storage.as_ref())?;

if !self.storage.is_mounted(node) {
return Err(Error::FileIsNotMounted);
}

let mut metadata = self.metadata_from_node(node)?;

metadata.size = new_size;

self.storage.put_metadata(node, metadata);

Ok(())
}

// store content of the currently active memory file to the file system
pub fn store_memory_file(&mut self, filename: &str) -> Result<(), Error> {
// create a file for the mount
Expand All @@ -181,7 +158,7 @@ impl FileSystem {
self.storage.store_mounted_memory(node)
}

// Unmount memory file, the system will continue to work with its own memory
// Unmount memory, the system will continue to work with the file in normal mode.
pub fn unmount_memory_file(&mut self, filename: &str) -> Result<Box<dyn Memory>, Error> {
// create a file for the mount
let fd = self.open_or_create(
Expand All @@ -200,7 +177,7 @@ impl FileSystem {
Ok(memory)
}

// Get dir entry for a given directory and the directory index.
// Get directory entry for a given directory file descriptor and the entry index.
pub fn get_direntry(&self, fd: Fd, index: DirEntryIndex) -> Result<DirEntry, Error> {
self.get_dir(fd)?.get_entry(index, self.storage.as_ref())
}
Expand Down Expand Up @@ -1355,8 +1332,11 @@ mod tests {
memory1.write(i as u64 * len as u64, content.as_bytes());
}

fs.set_memory_file_size(file_name, len as FileSize * count as FileSize)
.unwrap();
let fd = fs.open_or_create(fs.root_fd, file_name, FdStat::default(), OpenFlags::empty(), 0).unwrap();
let mut metadata = fs.metadata(fd).unwrap();
metadata.size = len as FileSize * count as FileSize;
fs.set_metadata(fd, metadata).unwrap();
fs.close(fd).unwrap();

// store memory into a file
fs.store_memory_file(file_name).unwrap();
Expand Down
4 changes: 2 additions & 2 deletions src/storage/stable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,7 @@ impl<M: Memory> Storage for StableStorage<M> {

fn mount_node(&mut self, node: Node, memory: Box<dyn Memory>) -> Result<(), Error> {
if self.is_mounted(node) {
return Err(Error::IsMountedAlready);
return Err(Error::MemoryFileIsMountedAlready);
}

// do extra meta preparation
Expand All @@ -372,7 +372,7 @@ impl<M: Memory> Storage for StableStorage<M> {
fn unmount_node(&mut self, node: Node) -> Result<Box<dyn Memory>, Error> {
let memory = self.active_mounts.remove(&node);

memory.ok_or(Error::FileIsNotMounted)
memory.ok_or(Error::MemoryFileIsNotMounted)
}

fn is_mounted(&self, node: Node) -> bool {
Expand Down
4 changes: 2 additions & 2 deletions src/storage/transient.rs
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ impl Storage for TransientStorage {

fn mount_node(&mut self, node: Node, memory: Box<dyn Memory>) -> Result<(), Error> {
if self.is_mounted(node) {
return Err(Error::IsMountedAlready);
return Err(Error::MemoryFileIsMountedAlready);
}

// do extra meta preparation
Expand All @@ -262,7 +262,7 @@ impl Storage for TransientStorage {
fn unmount_node(&mut self, node: Node) -> Result<Box<dyn Memory>, Error> {
let memory = self.active_mounts.remove(&node);

memory.ok_or(Error::FileIsNotMounted)
memory.ok_or(Error::MemoryFileIsNotMounted)
}

fn is_mounted(&self, node: Node) -> bool {
Expand Down

0 comments on commit 66dc545

Please sign in to comment.