Skip to content

Commit 1ce5949

Browse files
committed
fix mounting on absolute paths, inprove mount size reset logic
1 parent b017f4d commit 1ce5949

File tree

4 files changed

+62
-18
lines changed

4 files changed

+62
-18
lines changed

CHANGELOG.md

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,16 @@
11
# Changelog
22

33

4+
## [v0.10.1]
5+
- Fix mounting on absolute paths
6+
- Improved mounted memory size reset logic
7+
8+
## [v0.10.0]
9+
- Optimized work with folders
10+
- Refactor Metadata and DirEntry structures
11+
- Reduce copy of DirEntry
12+
- Add fast direntry lookup by name
13+
414
## [v0.9.0]
515
- switch to ic-stable-structures v0.7
616
- refactor project structure
@@ -88,7 +98,8 @@
8898
## [v0.5.0]
8999
- *API change:* init with memory manager using memory index range rather than first memory index.
90100

91-
[unreleased]: https://github.com/wasm-forge/stable-fs/compare/v0.9.0...main
101+
[v0.10.1]: https://github.com/wasm-forge/stable-fs/compare/v0.10.0...v0.10.1
102+
[v0.10.0]: https://github.com/wasm-forge/stable-fs/compare/v0.9.0...v0.10.0
92103
[v0.9.0]: https://github.com/wasm-forge/stable-fs/compare/v0.8.1...v0.9.0
93104
[v0.8.1]: https://github.com/wasm-forge/stable-fs/compare/v0.8.0...v0.8.1
94105
[v0.8.0]: https://github.com/wasm-forge/stable-fs/compare/v0.7.3...v0.8.0

stable-fs/src/fs.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,8 @@ impl FileSystem {
186186
filename: &str,
187187
memory: Box<dyn Memory>,
188188
) -> Result<(), Error> {
189+
let filename = filename.strip_prefix('/').unwrap_or(filename);
190+
189191
// create a file for the mount
190192
let fd = self.open(
191193
self.root_fd,
@@ -207,6 +209,8 @@ impl FileSystem {
207209

208210
// initialize mounted memory with the data stored in the host file
209211
pub fn init_memory_file(&mut self, filename: &str) -> Result<(), Error> {
212+
let filename = filename.strip_prefix('/').unwrap_or(filename);
213+
210214
// create a file for the mount
211215
let fd = self.open(
212216
self.root_fd,
@@ -228,6 +232,8 @@ impl FileSystem {
228232

229233
// store content of the currently active memory file to the file system
230234
pub fn store_memory_file(&mut self, filename: &str) -> Result<(), Error> {
235+
let filename = filename.strip_prefix('/').unwrap_or(filename);
236+
231237
// create a file for the mount
232238
let fd = self.open(
233239
self.root_fd,
@@ -249,6 +255,8 @@ impl FileSystem {
249255

250256
// Unmount memory, the system will continue to work with the file in normal mode.
251257
pub fn unmount_memory_file(&mut self, filename: &str) -> Result<Box<dyn Memory>, Error> {
258+
let filename = filename.strip_prefix('/').unwrap_or(filename);
259+
252260
// create a file for the mount
253261
let fd = self.open(
254262
self.root_fd,

stable-fs/src/storage/stable.rs

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -989,14 +989,28 @@ impl<M: Memory> Storage for StableStorage<M> {
989989
// get the file metadata (we are not mounted at this point)
990990
let mut file_meta = self.get_metadata(node)?;
991991

992+
let memory_size = memory.size();
993+
992994
// activate mount
993995
self.active_mounts.insert(node, memory);
994996

995-
if let Ok(_old_mounted_meta) = self.get_metadata(node) {
996-
// do nothing, we already have the metadata
997+
if let Ok(old_mounted_meta) = self.get_metadata(node) {
998+
// the connected memory might have a different size from the old memory, we need to decide which size to use...
999+
//
1000+
// Solution:
1001+
// if the new memory size in bytes is smaller than the old mounted metadata file size,
1002+
// we assume this is a new and unknown memory, and reinitialize the metadata size to match the total number of bytes
1003+
// provided in memory
1004+
1005+
if memory_size * WASM_PAGE_SIZE_IN_BYTES < old_mounted_meta.size {
1006+
let mut meta = old_mounted_meta.clone();
1007+
1008+
meta.size = memory_size * WASM_PAGE_SIZE_IN_BYTES;
1009+
self.put_metadata(node, &meta)?;
1010+
}
9971011
} else {
998-
// take a copy of the file meta, set the size to 0 by default
999-
file_meta.size = 0;
1012+
// take a copy of the file meta, set the size to the size of the memory
1013+
file_meta.size = memory_size * WASM_PAGE_SIZE_IN_BYTES;
10001014

10011015
// update mounted metadata
10021016
self.put_metadata(node, &file_meta)?;

stable-fs/src/storage/transient.rs

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -332,24 +332,35 @@ impl Storage for TransientStorage {
332332
}
333333

334334
// do extra meta preparation
335-
let mut meta = self
336-
.metadata
337-
.get(&node)
338-
.ok_or(Error::NoSuchFileOrDirectory)?
339-
.clone();
335+
// get the file metadata (we are not mounted at this point)
336+
let mut file_meta = self.get_metadata(node)?;
340337

338+
let memory_size = memory.size();
339+
340+
// activate mount
341341
self.active_mounts.insert(node, memory);
342342

343-
let new_mounted_meta = if let Some(old_mounted_meta) = self.mounted_meta.get(&node) {
344-
// we can change here something for the new mounted meta
345-
old_mounted_meta.clone()
343+
if let Ok(old_mounted_meta) = self.get_metadata(node) {
344+
// the connected memory might have a different size from the old memory, we need to decide which size to use...
345+
//
346+
// Solution:
347+
// if the new memory size in bytes is smaller than the old mounted metadata file size,
348+
// we assume this is a new and unknown memory, and reinitialize the metadata size to match the total number of bytes
349+
// provided in memory
350+
351+
if memory_size * WASM_PAGE_SIZE_IN_BYTES < old_mounted_meta.size {
352+
let mut meta = old_mounted_meta.clone();
353+
354+
meta.size = memory_size * WASM_PAGE_SIZE_IN_BYTES;
355+
self.put_metadata(node, &meta)?;
356+
}
346357
} else {
347-
// take a copy of the file meta, set size to 0 by default
348-
meta.size = 0;
349-
meta
350-
};
358+
// take a copy of the file meta, set the size to the size of the memory
359+
file_meta.size = memory_size * WASM_PAGE_SIZE_IN_BYTES;
351360

352-
self.mounted_meta.insert(node, new_mounted_meta);
361+
// update mounted metadata
362+
self.put_metadata(node, &file_meta)?;
363+
};
353364

354365
Ok(())
355366
}

0 commit comments

Comments
 (0)