Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
Murmele committed Jun 6, 2024
1 parent 2c425a1 commit 2071649
Show file tree
Hide file tree
Showing 7 changed files with 86 additions and 1 deletion.
5 changes: 5 additions & 0 deletions mount_disk.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/bin/bash

mkdir -p DISK1_MOUNT
sudo losetup -o 135266304 /dev/loop0 DISK1.img
sudo mount /dev/loop0 DISK1_MOUNT
3 changes: 2 additions & 1 deletion src/volume_mgr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ pub struct VolumeManager<
T: TimeSource,
<D as BlockDevice>::Error: core::fmt::Debug,
{
pub(crate) block_device: D,
/// test
pub block_device: D,
pub(crate) time_source: T,
id_generator: SearchIdGenerator,
open_volumes: Vec<VolumeInfo, MAX_VOLUMES>,
Expand Down
5 changes: 5 additions & 0 deletions tests/mount_disk.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/bin/bash

mkdir -p DISK1_MOUNT
sudo losetup -o 135266304 /dev/loop0 disk.img
sudo mount /dev/loop0 DISK1_MOUNT
4 changes: 4 additions & 0 deletions tests/unmount_disk.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/bin/bash

sudo umount /dev/loop0
sudo losetup -d /dev/loop0
17 changes: 17 additions & 0 deletions tests/utils/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,10 @@ impl<T> RamDisk<T> {
contents: std::cell::RefCell::new(contents),
}
}

pub fn content(&self) -> std::cell::Ref<'_, T> {
return self.contents.borrow();
}
}

impl<T> BlockDevice for RamDisk<T>
Expand Down Expand Up @@ -143,12 +147,25 @@ fn unpack_disk(gzip_bytes: &[u8]) -> Result<Vec<u8>, Error> {
Ok(output)
}

pub fn pack_disk(buf: &[u8]) -> &'static str {
//let mut encoder = flate2::write::GzEncoder::new(Vec::new(), flate2::Compression::default());
//encoder.write_all(buf).unwrap();
//let res = encoder.finish().unwrap();
let mut f = std::fs::File::create("DISK1.img").unwrap();
f.write_all(buf).unwrap();
return "DISK1.img";
}

/// Turn some gzipped bytes into a block device,
pub fn make_block_device(gzip_bytes: &[u8]) -> Result<RamDisk<Vec<u8>>, Error> {
let data = unpack_disk(gzip_bytes)?;
Ok(RamDisk::new(data))
}

pub fn make_block_device_no_zip(bytes: &Vec<u8>) -> RamDisk<Vec<u8>> {
RamDisk::new(bytes.to_vec())
}

pub struct TestTimeSource {
fixed: embedded_sdmmc::Timestamp,
}
Expand Down
49 changes: 49 additions & 0 deletions tests/write_file.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
//! File opening related tests
use std::borrow::Borrow;

use embedded_sdmmc::{Mode, VolumeIdx, VolumeManager, VolumeOpenMode};

mod utils;
Expand Down Expand Up @@ -102,6 +104,53 @@ fn flush_file() {
assert_eq!(entry.size, 64 * 3);
}

#[test]
fn write_file_not_correct_closed() {
let time_source = utils::make_time_source();
let disk = utils::make_block_device(utils::DISK_SOURCE).unwrap();
let mut volume_mgr: VolumeManager<utils::RamDisk<Vec<u8>>, utils::TestTimeSource, 4, 2, 1> =
VolumeManager::new_with_limits(disk, time_source, 0xAA00_0000);
let volume = volume_mgr
.open_raw_volume(VolumeIdx(0), VolumeOpenMode::ReadWrite)
.expect("open volume");
let root_dir = volume_mgr.open_root_dir(volume).expect("open root dir");

// Open with string
let f = volume_mgr
.open_file_in_dir(root_dir, "README.TXT", Mode::ReadWriteTruncate)
.expect("open file");

// Write some data to the file
volume_mgr.write(f, b"Hello").expect("file write");

// Check that the file length is zero in the directory entry, as we haven't
// flushed yet
let entry = volume_mgr
.find_directory_entry(root_dir, "README.TXT")
.expect("find entry");
assert_eq!(entry.size, 0);

volume_mgr.flush_file(f).expect("flush");
volume_mgr.close_file(f).unwrap();
volume_mgr.close_dir(root_dir).unwrap();
volume_mgr.close_volume(volume).unwrap();

let c = volume_mgr.block_device.content();

utils::pack_disk(c.borrow());

// let disk = utils::make_block_device_no_zip(c.borrow());
// let time_source = utils::make_time_source();
// let mut volume_mgr: VolumeManager<utils::RamDisk<Vec<u8>>, utils::TestTimeSource, 4, 2, 1> =
// VolumeManager::new_with_limits(disk, time_source, 0xAA00_0000);
// let volume = volume_mgr
// .open_raw_volume(VolumeIdx(0), VolumeOpenMode::ReadWrite)
// .expect("open volume");

// // Dirty because it was not properly closed before
// assert_eq!(volume_mgr.volume_status_dirty(volume).unwrap(), true);
}

// ****************************************************************************
//
// End Of File
Expand Down
4 changes: 4 additions & 0 deletions unmount_disk.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/bin/bash

sudo umount /dev/loop0
sudo losetup -d /dev/loop0

0 comments on commit 2071649

Please sign in to comment.