From 5e519151c956b409ac95ead3b335229fb21a7829 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sosth=C3=A8ne=20Gu=C3=A9don?= Date: Tue, 27 Jun 2023 10:18:39 +0200 Subject: [PATCH 1/3] Add filesystem persistence test This adds tests for generating filesystems with a version. It also adds tests that those filesystems are read properly. This will help avoid breaking backward compatibility --- tests-old-fs/empty.bin | Bin 0 -> 22400 bytes tests-old-fs/recurse.bin | Bin 0 -> 22400 bytes tests-old-fs/root.bin | Bin 0 -> 22400 bytes tests/create_old_fs.rs | 188 +++++++++++++++++++++++++++++++++++++++ 4 files changed, 188 insertions(+) create mode 100644 tests-old-fs/empty.bin create mode 100644 tests-old-fs/recurse.bin create mode 100644 tests-old-fs/root.bin create mode 100644 tests/create_old_fs.rs diff --git a/tests-old-fs/empty.bin b/tests-old-fs/empty.bin new file mode 100644 index 0000000000000000000000000000000000000000..1eb401e047c27dcfcb44d1e2a4774035c4c74c4e GIT binary patch literal 22400 zcmeIwF%Cgd5CzcdOSJ5OTEil&L_#9@g~X56#w@@xEI~(s=UIZrZ_XrBOs2Uxxq~FP zak;mqcCj04gHM%^G^)>{07pN8esk`gfeA#3W7{~Eft|l6hJ0U<)mAg1NiIn2xV2V1JXh=-lAwX*?^;+9AAsB-p#+k)g z8J(2HsWX$Ke}Dt$Wf8F&8r zy??NMAsx*a>NX=9kQi!DnTnU>h5F3OQH8%m$2lv;8>td@^-<-Iqv?41RjB^_iLlN@#{ z%Z}3LL6*(6r@Y>K{kVbv0tg_000IagfB*srAbh=Cr=&&2$C~k? zxiOvm*9Ml|e4UY2JFUJrmFM%j`tOwN=F<9=H>1ZCQkJ{FUjv`cSTg4B7i0ZK+>xZu z$)dP~ErxUe-DYU8WH+8{)F%ewP7&T;Ry)w86-x%)etAv3k2#XGZc_e_U*GrJFO!Eo zJV*7XcTLowQwPlq2c^zjmB%o?m@iAG7xR+DxGYD;Y$|;&onD^Jf33PP3vl~h6*z%) zH|%*9)E8Z2qPAqiQs^GPnpxXE25edCPPZ4ZGgeT0x28uw#~DfL?wr)!k#?ET0d%Pt zkwTaIw0&+X&J^LjWu-%1bnRWKo401-kHiWH^qfFYci+{FdPjS>(pd86+n)O)1V#V> I1bRf^4_MQHVgLXD literal 0 HcmV?d00001 diff --git a/tests-old-fs/root.bin b/tests-old-fs/root.bin new file mode 100644 index 0000000000000000000000000000000000000000..6b7fad7913a5089b214ddfe59e8722018d567f0c GIT binary patch literal 22400 zcmeIwF=_%q6b9gjZW9nS5o`AXi4eP-AVtdb2Eq!$vI#haoo7n#VLXCex@^Xvc+Xwc5-NF^=fPz86vEUVGYYyZh>&&c!rUo3vc5>*_9T zQ@(KhoD1daTa&6LmAB=u{@~_ZxbG$=mnOOk#pFD)=thR`vt#oyUcTg)SOhwOB4_j8 zXZK>, 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, 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(); + } +} From 800bccf3758379356d915aea5264a0d5e0465799 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sosth=C3=A8ne=20Gu=C3=A9don?= Date: Fri, 9 Jun 2023 17:18:13 +0200 Subject: [PATCH 2/3] Bump littlefs2-sys --- Cargo.toml | 5 ++++- src/fs.rs | 1 + 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index 1cc635dfb..7a17bd3e8 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -22,7 +22,7 @@ default-features = false version = "0.2" [dependencies.littlefs2-sys] -version = "0.1.6" +version = "0.2" [dependencies.serde] version = "1" @@ -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" } diff --git a/src/fs.rs b/src/fs.rs index 4e1ef87b1..9a4904c79 100644 --- a/src/fs.rs +++ b/src/fs.rs @@ -132,6 +132,7 @@ impl Allocation { name_max: filename_max_plus_one.wrapping_sub(1), file_max, attr_max, + metadata_max: 0, }; Self { From f718fc2e8ef61b48225db6223d922c8cc4898cf4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sosth=C3=A8ne=20Gu=C3=A9don?= Date: Fri, 9 Jun 2023 17:20:27 +0200 Subject: [PATCH 3/3] Make metadata_max configurable through the `Storage` trait --- src/driver.rs | 6 ++++++ src/fs.rs | 3 ++- src/tests.rs | 4 ++-- 3 files changed, 10 insertions(+), 3 deletions(-) diff --git a/src/driver.rs b/src/driver.rs index fb42103c0..ffa50687e 100644 --- a/src/driver.rs +++ b/src/driver.rs @@ -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`. diff --git a/src/fs.rs b/src/fs.rs index 9a4904c79..3a4e162d1 100644 --- a/src/fs.rs +++ b/src/fs.rs @@ -61,6 +61,7 @@ impl Allocation { let lookahead_size: u32 = 8 * ::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); @@ -132,7 +133,7 @@ impl Allocation { name_max: filename_max_plus_one.wrapping_sub(1), file_max, attr_max, - metadata_max: 0, + metadata_max, }; Self { diff --git a/src/tests.rs b/src/tests.rs index 14a450a6e..5159d6ceb 100644 --- a/src/tests.rs +++ b/src/tests.rs @@ -41,8 +41,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]