summaryrefslogtreecommitdiff
path: root/ayafs/src/block_device
diff options
context:
space:
mode:
authorChuyan Zhang <me@zcy.moe>2023-11-30 12:01:11 -0800
committerChuyan Zhang <me@zcy.moe>2023-11-30 12:01:11 -0800
commitfd125947c9db0b33761414e65e919f73d9bf1815 (patch)
treec4c66d95ba85601427928aa7f23659590055d464 /ayafs/src/block_device
parent1eac97eea4ec0bcef0be061a2cba93a584355283 (diff)
downloadmyfs-fd125947c9db0b33761414e65e919f73d9bf1815.tar.gz
myfs-fd125947c9db0b33761414e65e919f73d9bf1815.zip
Refactor workspace
Diffstat (limited to 'ayafs/src/block_device')
-rw-r--r--ayafs/src/block_device/disk.rs47
-rw-r--r--ayafs/src/block_device/memory_disk.rs33
-rw-r--r--ayafs/src/block_device/mod.rs10
3 files changed, 0 insertions, 90 deletions
diff --git a/ayafs/src/block_device/disk.rs b/ayafs/src/block_device/disk.rs
deleted file mode 100644
index 3f0b018..0000000
--- a/ayafs/src/block_device/disk.rs
+++ /dev/null
@@ -1,47 +0,0 @@
-use std::cell::RefCell;
-use std::fs::File;
-use std::io::{Read, Seek, SeekFrom, Write};
-use std::path::{Path, PathBuf};
-use crate::block_device::{BLOCK_SIZE, BlockDevice};
-
-pub struct Disk {
- disk_path: PathBuf,
- device: RefCell<File>,
-}
-
-impl Disk {
- pub fn new(disk_path: PathBuf) -> Self {
- let device = File::options()
- .read(true)
- .write(true)
- .open(disk_path.as_path())
- .unwrap();
- // let device = File::open(disk_path.as_path()).unwrap();
- Self {
- disk_path,
- device: RefCell::new(device),
- }
- }
-}
-
-impl BlockDevice for Disk {
- fn read(&self, block_id: usize, buffer: &mut [u8]) {
- let mut device = self.device.borrow_mut();
- device
- .seek(SeekFrom::Start((block_id * BLOCK_SIZE) as u64))
- .expect("Unable to seek!");
- device
- .read_exact(buffer)
- .expect("Failed to read 4096 bytes!");
- }
-
- fn write(&self, block_id: usize, buffer: &[u8]) {
- let mut device = self.device.borrow_mut();
- device
- .seek(SeekFrom::Start((block_id * BLOCK_SIZE) as u64))
- .expect("Unable to seek!");
- device
- .write_all(buffer)
- .expect("Unable to write 4096 bytes!");
- }
-} \ No newline at end of file
diff --git a/ayafs/src/block_device/memory_disk.rs b/ayafs/src/block_device/memory_disk.rs
deleted file mode 100644
index 0639d3e..0000000
--- a/ayafs/src/block_device/memory_disk.rs
+++ /dev/null
@@ -1,33 +0,0 @@
-use crate::block_device::{BlockDevice, BLOCK_SIZE};
-use std::cell::RefCell;
-
-#[repr(C)]
-pub struct MemoryDisk {
- /// Emulating a block device with a segment of RAM,
- /// which is 64MiB == 4KiB per block * 16384 blocks
- pub arena: RefCell<Vec<u8>>,
-}
-
-impl MemoryDisk {
- pub fn new(block_number: usize) -> Self {
- Self {
- arena: RefCell::new(vec![0u8; BLOCK_SIZE * block_number]),
- }
- }
-}
-
-impl BlockDevice for MemoryDisk {
- fn read(&self, block_id: usize, buffer: &mut [u8]) {
- let block_front = block_id * BLOCK_SIZE;
- let block_back = block_front + BLOCK_SIZE;
- let arena = self.arena.borrow();
- buffer.copy_from_slice(&arena[block_front..block_back]);
- }
-
- fn write(&self, block_id: usize, buffer: &[u8]) {
- let block_front = block_id * BLOCK_SIZE;
- let block_back = block_front + BLOCK_SIZE;
- let mut arena = self.arena.borrow_mut();
- arena[block_front..block_back].copy_from_slice(buffer);
- }
-}
diff --git a/ayafs/src/block_device/mod.rs b/ayafs/src/block_device/mod.rs
deleted file mode 100644
index a1e6544..0000000
--- a/ayafs/src/block_device/mod.rs
+++ /dev/null
@@ -1,10 +0,0 @@
-/// Abstracts for block devices.
-/// Currently only a mock memory disk.
-pub mod memory_disk;
-pub mod disk;
-
-pub const BLOCK_SIZE: usize = 4096;
-pub trait BlockDevice {
- fn read(&self, block_id: usize, buffer: &mut [u8]);
- fn write(&self, block_id: usize, buffer: &[u8]);
-}