Skip to content

Commit

Permalink
clean-up
Browse files Browse the repository at this point in the history
  • Loading branch information
wasm-forge committed Jul 29, 2024
1 parent c94c8a6 commit c6987d6
Show file tree
Hide file tree
Showing 10 changed files with 93 additions and 67 deletions.
12 changes: 7 additions & 5 deletions .github/workflows/test.yml → .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
name: Tests

on:
push:
branches: [ "main" ]
pull_request:
branches: [ "main" ]
on: [push, pull_request]

env:
CARGO_TERM_COLOR: always
Expand All @@ -28,6 +24,12 @@ jobs:
- name: Install wasi2ic
run: cargo install wasi2ic

- name: Check Format
run: cargo fmt --all -- --check

- name: Clippy
run: cargo clippy --tests --benches -- -D clippy::all

- name: Install cargo-llvm-cov
uses: taiki-e/install-action@cargo-llvm-cov

Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion src/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ impl FileSystem {
let buf = unsafe { std::slice::from_raw_parts(buf.buf, buf.len) };

let size = file.write_with_offset(written_size + offset, buf, self.storage.as_mut())?;

written_size += size;
}
self.put_file(fd, file);
Expand Down
27 changes: 18 additions & 9 deletions src/integration_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ fn active_canister() -> Principal {
fn setup_test_projects() {
use std::process::Command;
let _ = Command::new("bash")
.arg("build_tests.sh")
.arg("scripts/build_tests.sh")
.output()
.expect("Failed to execute command");
}
Expand Down Expand Up @@ -245,7 +245,6 @@ fn writing_file_after_upgrade() {
assert_eq!(result, "test5");
let result = fns::read_text(&pic, "test6.txt", 10i64, 5u64);
assert_eq!(result, "test6");

}

#[test]
Expand Down Expand Up @@ -357,19 +356,27 @@ fn long_paths_and_file_names() {

let content = read_text(&pic, &format!("{path}/{file_name}"), 0, 100000);
assert_eq!(expected_content, content);

let expected_content = "0123:123";
let content = read_text(&pic, &format!("{path}/3.txt"), 60, expected_content.len() as u64);
let content = read_text(
&pic,
&format!("{path}/3.txt"),
60,
expected_content.len() as u64,
);
assert_eq!(expected_content, content);

let expected_content = "A💖//13.txt";
let content = read_text(&pic, &format!("{path}/13.txt"), content_length as i64 - expected_content.len() as i64, 100);
let content = read_text(
&pic,
&format!("{path}/13.txt"),
content_length as i64 - expected_content.len() as i64,
100,
);

assert_eq!(expected_content, content);

}


#[test]
fn large_file_read() {
let pic = setup_initial_canister();
Expand All @@ -387,8 +394,10 @@ fn large_file_read() {

println!("instructions {instructions}, size {size}");

assert!(instructions < 3_000_000_000, "The call should take less than 3 billion instructions");
assert!(
instructions < 3_000_000_000,
"The call should take less than 3 billion instructions"
);

assert_eq!(size, 99_999_987);

}
68 changes: 36 additions & 32 deletions src/runtime/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,22 +107,15 @@ impl File {
buf: &mut [u8],
storage: &mut dyn Storage,
) -> Result<FileSize, Error> {

if buf.is_empty() {
return Ok(0 as FileSize);
}

let file_size = storage.get_metadata(self.node)?.size;

let read_size = storage.read_range(
self.node,
offset,
file_size,
buf,
)?;

let read_size = storage.read_range(self.node, offset, file_size, buf)?;

Ok(read_size as FileSize)

}

// Read file by chunks at the current file cursor, the cursor position will NOT be updated after reading.
Expand Down Expand Up @@ -161,7 +154,6 @@ impl File {
buf: &[u8],
storage: &mut dyn Storage,
) -> Result<FileSize, Error> {

let mut metadata = storage.get_metadata(self.node)?;
let end = offset + buf.len() as FileSize;
let chunk_infos = get_chunk_infos(offset, end);
Expand All @@ -180,7 +172,7 @@ impl File {
metadata.size = end;
storage.put_metadata(self.node, metadata)
}

Ok(written_size as FileSize)
}

Expand Down Expand Up @@ -381,7 +373,7 @@ mod tests {

let file = fs.get_test_file(fd);
let storage = fs.get_test_storage();

for i in 0..1000 {
let buf = [(i % 256) as u8; 10];
file.write_with_offset(i * 16, &buf, storage).unwrap();
Expand All @@ -391,16 +383,20 @@ mod tests {
let mut buf1 = [0; 13];
let mut buf2 = [0; 5000];
let mut buf3 = [0; 15000];

let r1 = file.read_with_offset_chunk(i * 17, &mut buf1, storage).unwrap() as usize;
let r2 = file.read_with_offset_chunk(i * 17, &mut buf2, storage).unwrap() as usize;
let _r3 = file.read_with_offset_chunk(i * 17, &mut buf3, storage).unwrap() as usize;

let r1 = file
.read_with_offset_chunk(i * 17, &mut buf1, storage)
.unwrap() as usize;
let r2 = file
.read_with_offset_chunk(i * 17, &mut buf2, storage)
.unwrap() as usize;
let _r3 = file
.read_with_offset_chunk(i * 17, &mut buf3, storage)
.unwrap() as usize;

assert_eq!(buf1[..r1], buf2[..r1]);
assert_eq!(buf2[..r2], buf3[..r2]);
}


}

#[test]
Expand All @@ -417,12 +413,13 @@ mod tests {
let buf = [(i % 256) as u8; 16];
file.write_with_offset(i * 16, &buf, storage).unwrap();
}

file.seek(-1000 * 16, Whence::END, storage).unwrap();

for i in 0..1000 {
let mut buf = [0; 16];
file.read_with_offset_chunk(i * 16, &mut buf, storage).unwrap();
file.read_with_offset_chunk(i * 16, &mut buf, storage)
.unwrap();

let expected = [(i % 256) as u8; 16];
assert_eq!(buf, expected);
Expand All @@ -443,30 +440,33 @@ mod tests {
let buf = [(i % 256) as u8; 16];
file.write_with_offset(i * 16, &buf, storage).unwrap();
}

for i in 0..1000 {
let mut buf1 = [0; 13];
let len1 = file.read_with_offset_chunk(i * 16, &mut buf1, storage).unwrap();
let len1 = file
.read_with_offset_chunk(i * 16, &mut buf1, storage)
.unwrap();

let mut buf2 = [0; 13];
let len2 = file.read_with_offset(i * 16, &mut buf2, storage).unwrap();

assert_eq!(buf1, buf2);
assert_eq!(len1, len2);
}

for i in 0..2050 {
let mut buf1 = [0; 5003];
let len1 = file.read_with_offset_chunk(i * 13, &mut buf1, storage).unwrap();
let len1 = file
.read_with_offset_chunk(i * 13, &mut buf1, storage)
.unwrap();

let mut buf2 = [0; 5003];
let len2 = file.read_with_offset(i * 13, &mut buf2, storage).unwrap();

assert_eq!(buf1, buf2);
assert_eq!(len1, len2);
}
}

}

#[test]
fn read_and_write_offset_vs_range_transient() {
Expand All @@ -482,27 +482,31 @@ mod tests {
let buf = [(i % 256) as u8; 16];
file.write_with_offset(i * 16, &buf, storage).unwrap();
}

for i in 0..1000 {
let mut buf1 = [0; 13];
let len1 = file.read_with_offset_chunk(i * 16, &mut buf1, storage).unwrap();
let len1 = file
.read_with_offset_chunk(i * 16, &mut buf1, storage)
.unwrap();

let mut buf2 = [0; 13];
let len2 = file.read_with_offset(i * 16, &mut buf2, storage).unwrap();

assert_eq!(buf1, buf2);
assert_eq!(len1, len2);
}

for i in 0..2050 {
let mut buf1 = [0; 5003];
let len1 = file.read_with_offset_chunk(i * 13, &mut buf1, storage).unwrap();
let len1 = file
.read_with_offset_chunk(i * 13, &mut buf1, storage)
.unwrap();

let mut buf2 = [0; 5003];
let len2 = file.read_with_offset(i * 13, &mut buf2, storage).unwrap();

assert_eq!(buf1, buf2);
assert_eq!(len1, len2);
}
}
}
}
2 changes: 1 addition & 1 deletion src/storage/dummy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ impl Storage for DummyStorage {
fn rm_filechunk(&mut self, _node: Node, _index: FileChunkIndex) {
panic!("Not supported")
}

fn read_range(
&self,
_node: Node,
Expand Down
37 changes: 23 additions & 14 deletions src/storage/stable.rs
Original file line number Diff line number Diff line change
@@ -1,22 +1,23 @@
use std::ops::Range;

use ic_stable_structures::{
memory_manager::{MemoryId, MemoryManager, VirtualMemory}, BTreeMap, Cell, Memory
memory_manager::{MemoryId, MemoryManager, VirtualMemory},
BTreeMap, Cell, Memory,
};

use crate::error::Error;

use super::{
types::{
DirEntry, DirEntryIndex, FileChunk, FileChunkIndex, FileSize, FileType, Header, Metadata, Node, Times, FILE_CHUNK_SIZE
DirEntry, DirEntryIndex, FileChunk, FileChunkIndex, FileSize, FileType, Header, Metadata,
Node, Times, FILE_CHUNK_SIZE,
},
Storage,
};

const ROOT_NODE: Node = 0;
const FS_VERSION: u32 = 1;


const DEFAULT_FIRST_MEMORY_INDEX: u8 = 229;

// the maximum index accepted as the end range
Expand All @@ -40,7 +41,10 @@ impl<M: Memory> StableStorage<M> {
pub fn new(memory: M) -> Self {
let memory_manager = MemoryManager::init(memory);

let mut storage = Self::new_with_memory_manager(&memory_manager, DEFAULT_FIRST_MEMORY_INDEX..DEFAULT_FIRST_MEMORY_INDEX+MEMORY_INDEX_COUNT);
let mut storage = Self::new_with_memory_manager(
&memory_manager,
DEFAULT_FIRST_MEMORY_INDEX..DEFAULT_FIRST_MEMORY_INDEX + MEMORY_INDEX_COUNT,
);

storage._memory_manager = Some(memory_manager);

Expand All @@ -51,13 +55,18 @@ impl<M: Memory> StableStorage<M> {
memory_manager: &MemoryManager<M>,
memory_indices: Range<u8>,
) -> StableStorage<M> {

if memory_indices.end - memory_indices.start < MEMORY_INDEX_COUNT {
panic!("The memory index range must include at least {} incides", MEMORY_INDEX_COUNT);
panic!(
"The memory index range must include at least {} incides",
MEMORY_INDEX_COUNT
);
}

if memory_indices.end > MAX_MEMORY_INDEX {
panic!("Last memory index must be less than or equal to {}", MAX_MEMORY_INDEX);
panic!(
"Last memory index must be less than or equal to {}",
MAX_MEMORY_INDEX
);
}

let header_memory = memory_manager.get(MemoryId::new(memory_indices.start));
Expand All @@ -79,7 +88,6 @@ impl<M: Memory> StableStorage<M> {
direntry: VirtualMemory<M>,
filechunk: VirtualMemory<M>,
) -> Self {

let default_header_value = Header {
version: FS_VERSION,
next_node: ROOT_NODE + 1,
Expand All @@ -94,7 +102,7 @@ impl<M: Memory> StableStorage<M> {
};

let version = result.header.get().version;

if version != FS_VERSION {
panic!("Unsupported file system version");
}
Expand Down Expand Up @@ -130,7 +138,6 @@ impl<M: Memory> Storage for StableStorage<M> {

// Generate the next available node ID.
fn new_node(&mut self) -> Node {

let mut header = self.header.get().clone();

self.metadata.last_key_value();
Expand Down Expand Up @@ -200,7 +207,6 @@ impl<M: Memory> Storage for StableStorage<M> {
file_size: FileSize,
buf: &mut [u8],
) -> Result<FileSize, Error> {

if offset >= file_size {
return Ok(0);
}
Expand All @@ -215,7 +221,6 @@ impl<M: Memory> Storage for StableStorage<M> {
let mut remainder = file_size - offset;

for ((nd, _idx), value) in self.filechunk.range(range) {

assert!(nd == node);

// finished reading, buffer full
Expand All @@ -225,11 +230,15 @@ impl<M: Memory> Storage for StableStorage<M> {

let chunk_space = FILE_CHUNK_SIZE as FileSize - chunk_offset;

let to_read = remainder.min(chunk_space).min(buf.len() as FileSize - size_read);
let to_read = remainder
.min(chunk_space)
.min(buf.len() as FileSize - size_read);

let write_buf = &mut buf[size_read as usize..size_read as usize + to_read as usize];

write_buf.copy_from_slice(&value.bytes[chunk_offset as usize..chunk_offset as usize + to_read as usize]);
write_buf.copy_from_slice(
&value.bytes[chunk_offset as usize..chunk_offset as usize + to_read as usize],
);

chunk_offset = 0;

Expand Down
Loading

0 comments on commit c6987d6

Please sign in to comment.