|
1 | 1 | use core::convert::TryInto; |
2 | 2 | use generic_array::typenum::consts; |
| 3 | +use littlefs2_core::PathBuf; |
3 | 4 |
|
4 | 5 | use crate::{ |
5 | | - fs::{Allocation, Attribute, File, Filesystem}, |
| 6 | + driver::Storage, |
| 7 | + fs::{Allocation, Attribute, File, Filesystem, MountFlags}, |
6 | 8 | io::{Error, OpenSeekFrom, Read, Result, SeekFrom}, |
7 | 9 | path, BACKEND_VERSION, DISK_VERSION, |
8 | 10 | }; |
@@ -35,6 +37,20 @@ ram_storage!( |
35 | 37 | path_max_plus_one_ty = consts::U256, |
36 | 38 | ); |
37 | 39 |
|
| 40 | +ram_storage!( |
| 41 | + name = LargerRamStorage, |
| 42 | + backend = LargerRam, |
| 43 | + erase_value = 0xff, |
| 44 | + read_size = 20 * 5, |
| 45 | + write_size = 20 * 7, |
| 46 | + cache_size_ty = consts::U700, |
| 47 | + block_size = 20 * 35, |
| 48 | + block_count = 64, |
| 49 | + lookahead_size_ty = consts::U16, |
| 50 | + filename_max_plus_one_ty = consts::U256, |
| 51 | + path_max_plus_one_ty = consts::U256, |
| 52 | +); |
| 53 | + |
38 | 54 | #[test] |
39 | 55 | fn version() { |
40 | 56 | assert_eq!((BACKEND_VERSION.major(), BACKEND_VERSION.minor()), (2, 9)); |
@@ -545,3 +561,54 @@ fn test_mount_or_else_clobber_alloc() { |
545 | 561 | // t.compile_fail("tests/ui/*-fail.rs"); |
546 | 562 | // t.pass("tests/ui/*-pass.rs"); |
547 | 563 | // } |
| 564 | + |
| 565 | +#[test] |
| 566 | +fn shrinking() { |
| 567 | + let backend = &mut Ram::default(); |
| 568 | + let storage = &mut RamStorage::new(backend); |
| 569 | + let alloc = &mut Allocation::new(); |
| 570 | + |
| 571 | + Filesystem::format(storage).unwrap(); |
| 572 | + let _fs = Filesystem::mount(alloc, storage).unwrap(); |
| 573 | + |
| 574 | + let larger_backend = &mut LargerRam::default(); |
| 575 | + larger_backend.buf[..backend.buf.len()].copy_from_slice(&backend.buf); |
| 576 | + let larger_storage = &mut LargerRamStorage::new(larger_backend); |
| 577 | + let larger_alloc = &mut Allocation::new(); |
| 578 | + assert!(matches!( |
| 579 | + Filesystem::mount(larger_alloc, larger_storage), |
| 580 | + Err(Error::INVALID) |
| 581 | + )); |
| 582 | + |
| 583 | + let larger_alloc = &mut Allocation::with_config(crate::fs::Config { |
| 584 | + mount_flags: MountFlags::DISABLE_BLOCK_COUNT_CHECK, |
| 585 | + }); |
| 586 | + |
| 587 | + let fs = Filesystem::mount(larger_alloc, larger_storage).unwrap(); |
| 588 | + fs.grow(LargerRamStorage::BLOCK_COUNT).unwrap(); |
| 589 | + fs.shrink(RamStorage::BLOCK_COUNT).unwrap(); |
| 590 | +} |
| 591 | + |
| 592 | +#[test] |
| 593 | +fn shrinking_full() { |
| 594 | + let larger_backend = &mut LargerRam::default(); |
| 595 | + let larger_storage = &mut LargerRamStorage::new(larger_backend); |
| 596 | + let larger_alloc = &mut Allocation::new(); |
| 597 | + Filesystem::format(larger_storage).unwrap(); |
| 598 | + let fs = Filesystem::mount(larger_alloc, larger_storage).unwrap(); |
| 599 | + |
| 600 | + for i in 0.. { |
| 601 | + let path = format!("file-{i}"); |
| 602 | + let contents = &[0; 1024]; |
| 603 | + match fs.write(&PathBuf::try_from(&*path).unwrap(), contents) { |
| 604 | + Ok(_) => continue, |
| 605 | + Err(Error::NO_SPACE) => break, |
| 606 | + Err(err) => panic!("{err:?}"), |
| 607 | + } |
| 608 | + } |
| 609 | + |
| 610 | + assert!(matches!( |
| 611 | + fs.shrink(RamStorage::BLOCK_COUNT), |
| 612 | + Err(Error::DIR_NOT_EMPTY) |
| 613 | + )) |
| 614 | +} |
0 commit comments