Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expose more directory iteration methods #33

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ default-features = false
version = "0.2"

[dependencies.littlefs2-sys]
version = "0.1.6"
version = "0.2"

[dependencies.serde]
version = "1"
Expand Down Expand Up @@ -57,3 +57,6 @@ log-error = []
# member `char name[LFS_NAME_MAX+1]`.
# This means that if we change `traits::Storage::FILENAME_MAX_PLUS_ONE`,
# we need to pass this on!

[patch.crates-io]
littlefs2-sys = { git = "https://github.com/sosthene-nitrokey/littlefs2-sys.git", branch = "update" }
6 changes: 6 additions & 0 deletions src/driver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,12 @@ pub trait Storage {
/// Value zero is invalid, must be positive or -1.
const BLOCK_CYCLES: isize = -1;

// Optional upper limit on total space given to metadata pairs in bytes. On
// devices with large blocks (e.g. 128kB) setting this to a low size (2-8kB)
// can help bound the metadata compaction time. Must be <= block_size.
// Defaults to block_size when zero.
const METADATA_MAX: u32 = 0;

/// littlefs uses a read cache, a write cache, and one cache per per file.
/// Must be a multiple of `READ_SIZE` and `WRITE_SIZE`.
/// Must be a factor of `BLOCK_SIZE`.
Expand Down
68 changes: 68 additions & 0 deletions src/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ impl<Storage: driver::Storage> Allocation<Storage> {
let lookahead_size: u32 = 8 * <Storage as driver::Storage>::LOOKAHEAD_SIZE::U32;
let block_cycles: i32 = Storage::BLOCK_CYCLES as _;
let block_count: u32 = Storage::BLOCK_COUNT as _;
let metadata_max = Storage::METADATA_MAX;

debug_assert!(block_cycles >= -1);
debug_assert!(block_cycles != 0);
Expand Down Expand Up @@ -132,6 +133,7 @@ impl<Storage: driver::Storage> Allocation<Storage> {
name_max: filename_max_plus_one.wrapping_sub(1),
file_max,
attr_max,
metadata_max,
};

Self {
Expand Down Expand Up @@ -1031,13 +1033,79 @@ impl ReadDirAllocation {
}
}

#[derive(Debug, Clone, Copy, Eq, PartialEq, Ord, PartialOrd, Hash)]
pub struct DirIterationTell {
tell_result: u32,
}

pub struct ReadDir<'a, 'b, S: driver::Storage> {
alloc: RefCell<&'b mut ReadDirAllocation>,
fs: &'b Filesystem<'a, S>,
#[cfg(feature = "dir-entry-path")]
path: PathBuf,
}

impl<'a, 'b, S: driver::Storage> ReadDir<'a, 'b, S> {
/// Return the position of the directory
///
/// The returned offset is only meant to be consumed by seek and may not make
/// sense, but does indicate the current position in the directory iteration.
///
/// Returns the position of the directory, which can be returned to using [`seek`](Self::seek).
pub fn tell(&self) -> Result<DirIterationTell> {
let value = unsafe {
ll::lfs_dir_tell(
&mut self.fs.alloc.borrow_mut().state,
&mut self.alloc.borrow_mut().state,
)
};
if value < 0 {
Err(io::result_from((), value).unwrap_err())
} else {
Ok(DirIterationTell {
tell_result: value as u32,
})
}
}

/// Change the position of the directory
///
/// The new off must be a value previous returned from [`tell`](Self::tell) and specifies
/// an absolute offset in the directory seek.
pub fn seek(&mut self, state: DirIterationTell) -> Result<DirIterationTell> {
let value = unsafe {
ll::lfs_dir_seek(
&mut self.fs.alloc.borrow_mut().state,
&mut self.alloc.borrow_mut().state,
state.tell_result,
)
};
if value < 0 {
Err(io::result_from((), value).unwrap_err())
} else {
Ok(DirIterationTell {
tell_result: value as u32,
})
}
}

/// Change the position of the directory to the beginning of the directory
pub fn rewind(&mut self) -> Result<()> {
let res = unsafe {
ll::lfs_dir_rewind(
&mut self.fs.alloc.borrow_mut().state,
&mut self.alloc.borrow_mut().state,
)
};

if res < 0 {
Err(io::result_from((), res).unwrap_err())
} else {
Ok(())
}
}
}

impl<'a, 'b, S: driver::Storage> Iterator for ReadDir<'a, 'b, S> {
type Item = Result<DirEntry>;

Expand Down
50 changes: 38 additions & 12 deletions src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use crate::{
driver,
fs::{Attribute, File, Filesystem},
io::{Error, OpenSeekFrom, Read, Result, SeekFrom},
path::PathBuf,
};

ram_storage!(
Expand Down Expand Up @@ -41,8 +42,8 @@ ram_storage!(

#[test]
fn version() {
assert_eq!(crate::version().format, (2, 0));
assert_eq!(crate::version().backend, (2, 2));
assert_eq!(crate::version().format, (2, 1));
assert_eq!(crate::version().backend, (2, 6));
}

#[test]
Expand Down Expand Up @@ -480,31 +481,56 @@ fn test_iter_dirs() {
file.set_len(37)?;
fs.create_file_and_then(b"/tmp/file.b\0".try_into()?, |file| file.set_len(42))
})?;
let mut tells = Vec::new();

fs.read_dir_and_then(b"/tmp\0".try_into()?, |dir| {
fs.read_dir_and_then(b"/tmp\0".try_into()?, |mut dir| {
let mut found_files: usize = 0;
let mut sizes = [0usize; 4];
let mut i = 0;

for (i, entry) in dir.enumerate() {
tells.push(dir.tell()?);
while let Some(entry) = dir.next() {
tells.push(dir.tell()?);
let entry = entry?;

// assert_eq!(entry.file_name(), match i {
// 0 => b".\0",
// 1 => b"..\0",
// 2 => b"file.a\0",
// 3 => b"file.b\0",
// _ => panic!("oh noes"),
// });
let expected_name = match i {
0 => PathBuf::from(b".\0".as_slice()),
1 => PathBuf::from(b"..\0".as_slice()),
2 => PathBuf::from(b"file.a\0".as_slice()),
3 => PathBuf::from(b"file.b\0".as_slice()),
_ => panic!("oh noes"),
};

assert_eq!(entry.file_name(), &*expected_name);

sizes[i] = entry.metadata().len();
found_files += 1;
i += 1;
}

assert_eq!(sizes, [0, 0, 37, 42]);
assert_eq!(found_files, 4);

assert_eq!(tells.len(), 5);

for (i, tell) in tells.iter().enumerate() {
dir.rewind().unwrap();
let mut found_files: usize = 0;
let mut sizes = Vec::new();
dir.seek(*tell)?;

for entry in &mut dir {
let entry = entry?;
sizes.push(entry.metadata().len());
found_files += 1;
}

assert_eq!(sizes, [0, 0, 37, 42][i..]);
assert_eq!(found_files, 4 - i);
}
Ok(())
})
.unwrap();
Ok(())
})
.unwrap();
}
Expand Down
Binary file added tests-old-fs/empty.bin
Binary file not shown.
Binary file added tests-old-fs/recurse.bin
Binary file not shown.
Binary file added tests-old-fs/root.bin
Binary file not shown.
188 changes: 188 additions & 0 deletions tests/create_old_fs.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,188 @@
use littlefs2::{
consts, driver,
fs::Filesystem,
io::Result,
path,
path::{Path, PathBuf},
ram_storage,
};

ram_storage!(
name=RamStorage,
backend=Ram,
trait=driver::Storage,
erase_value=0xff,
read_size=20*5,
write_size=20*7,
cache_size_ty=consts::U700,
block_size=20*35,
block_count=32,
lookahead_size_ty=consts::U16,
filename_max_plus_one_ty=consts::U256,
path_max_plus_one_ty=consts::U256,
result=Result,
);

struct FileTest {
name: &'static Path,
content: &'static [u8],
}

struct DirTest {
files: &'static [FileTest],
sub_dirs: &'static [(&'static Path, DirTest)],
}

struct FsTest {
root: DirTest,
name: &'static str,
}

const EMPTY_DIR: FsTest = FsTest {
name: "empty.bin",
root: DirTest {
files: &[],
sub_dirs: &[],
},
};

const ROOT_FULL: FsTest = FsTest {
name: "root.bin",
root: DirTest {
files: &[
FileTest {
name: path!("test_file.txt"),
content: b"Test content - test_file.txt",
},
FileTest {
name: path!("test_file2.txt"),
content: b"Test content - test_file2.txt",
},
FileTest {
name: path!("test_file3.txt"),
content: b"Test content - test_file3.txt",
},
],
sub_dirs: &[],
},
};

const RECURSE: FsTest = FsTest {
name: "recurse.bin",
root: DirTest {
files: ROOT_FULL.root.files,
sub_dirs: &[
(
path!("root1"),
DirTest {
files: &[
FileTest {
name: path!("test_sub_file.txt"),
content: b"Test content - test_sub_file.txt",
},
FileTest {
name: path!("test_sub_file2.txt"),
content: b"Test content - test_sub_file2.txt",
},
],
sub_dirs: &[(
path!("sub-dir"),
DirTest {
files: &[
FileTest {
name: path!("test_sub_sub_file.txt"),
content: b"Test content - test_sub_sub_file.txt",
},
FileTest {
name: path!("test_sub_sub_file2.txt"),
content: b"Test content - test_sub_sub_file2.txt",
},
],
sub_dirs: &[],
},
)],
},
),
(
path!("root2"),
DirTest {
files: &[],
sub_dirs: &[],
},
),
],
},
};

const ALL: &[FsTest] = &[EMPTY_DIR, ROOT_FULL, RECURSE];

fn write_dir(fs: &Filesystem<RamStorage>, dir: &DirTest, current_dir: PathBuf) {
println!("Writing current_dir: {current_dir}");
for f in dir.files {
let mut buf = current_dir.clone();
buf.push(f.name);
println!(
"Writing {}, ({})",
f.name,
std::str::from_utf8(f.content).unwrap()
);
fs.write(&buf, f.content).unwrap();
}

for (name, d) in dir.sub_dirs {
let mut buf = current_dir.clone();
buf.push(name);
fs.create_dir(&buf).unwrap();
write_dir(fs, d, buf);
}
}

fn read_dir(fs: &Filesystem<RamStorage>, dir: &DirTest, current_dir: PathBuf) {
println!("Reading current_dir: {current_dir}");
for f in dir.files {
let mut buf = current_dir.clone();
buf.push(f.name);
dbg!(&buf);
let read = fs.read::<1024>(&buf).unwrap();
assert_eq!(std::str::from_utf8(&read), std::str::from_utf8(f.content));
}

for (name, d) in dir.sub_dirs {
let mut buf = current_dir.clone();
buf.push(name);
read_dir(fs, d, buf);
}
}

#[test]
#[ignore]
fn create() {
for fs_test in ALL {
println!("Got to test: {}", fs_test.name);
let mut backend = Ram::default();
let mut storage = RamStorage::new(&mut backend);
Filesystem::format(&mut storage).unwrap();
Filesystem::mount_and_then(&mut storage, |fs| {
write_dir(fs, &fs_test.root, PathBuf::new());
Ok(())
})
.unwrap();
std::fs::write(format!("tests-old-fs/{}", fs_test.name), &backend.buf).unwrap();
}
}

#[test]
fn read() {
for fs_test in ALL {
println!("Got to test: {}", fs_test.name);
let mut backend = Ram::default();
let buf = std::fs::read(format!("tests-old-fs/{}", fs_test.name)).unwrap();
backend.buf.copy_from_slice(&buf);
let mut storage = RamStorage::new(&mut backend);
Filesystem::mount_and_then(&mut storage, |fs| {
read_dir(fs, &fs_test.root, PathBuf::new());
Ok(())
})
.unwrap();
}
}