summaryrefslogtreecommitdiff
path: root/ayafs/src/block_device/disk.rs
diff options
context:
space:
mode:
authorChuyan Zhang <me@zcy.moe>2023-11-30 02:15:06 -0800
committerChuyan Zhang <me@zcy.moe>2023-11-30 02:15:06 -0800
commit1eac97eea4ec0bcef0be061a2cba93a584355283 (patch)
treed98fb1f6e3811286a0733c9df21e467590635ad2 /ayafs/src/block_device/disk.rs
parentb3db8a5a710aa0890c80241ffb3fd9792bf1cbe7 (diff)
downloadmyfs-1eac97eea4ec0bcef0be061a2cba93a584355283.tar.gz
myfs-1eac97eea4ec0bcef0be061a2cba93a584355283.zip
Add real disk i/o, add mkfs.aya (not yet implemented)
Diffstat (limited to 'ayafs/src/block_device/disk.rs')
-rw-r--r--ayafs/src/block_device/disk.rs47
1 files changed, 47 insertions, 0 deletions
diff --git a/ayafs/src/block_device/disk.rs b/ayafs/src/block_device/disk.rs
new file mode 100644
index 0000000..3f0b018
--- /dev/null
+++ b/ayafs/src/block_device/disk.rs
@@ -0,0 +1,47 @@
+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