diff --git a/src/drivers/drv_disk.rs b/src/drivers/drv_disk.rs index e69de29..e5c77f7 100644 --- a/src/drivers/drv_disk.rs +++ b/src/drivers/drv_disk.rs @@ -0,0 +1,42 @@ +use crate::Disk; + +/// driver disk +pub struct DrvDisk { + disk: Disk +} + +impl DrvDisk { + ///initialize the disk ok the current driver + pub fn init_drv_disk(disk: Disk) -> DrvDisk { + DrvDisk { disk: disk } + } + + ///read inside the disk + /// + /// ### Parameters + /// + /// - **self** driver disk + /// - **sector_number** sector where to read the data + /// - **data** where the readed data will be stored + pub fn read_sector(&mut self, sector_number : i32, data : &mut Vec) { + match Disk::read_request(&mut self.disk,sector_number, data) { + Err(e) => println!("{:?}", e), + _ => () + } + } + + ///write inside the disk + /// + /// ### Parameters + /// + /// - **self** driver disk + /// - **sector_number** sector where to write the data + /// - **data** where the data to write is stored + pub fn write_sector(&mut self, sector_number : i32, data : &mut Vec) { + match Disk::write_request(&mut self.disk, sector_number, data) { + Err(e) => println!("{:?}", e), + _ => () + } + } + +} \ No newline at end of file diff --git a/src/drivers/mod.rs b/src/drivers/mod.rs new file mode 100644 index 0000000..7c1868e --- /dev/null +++ b/src/drivers/mod.rs @@ -0,0 +1 @@ +pub mod drv_disk; \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index e556b8e..a14cf47 100644 --- a/src/main.rs +++ b/src/main.rs @@ -12,15 +12,39 @@ mod simulator; mod kernel; /// module containing useful tools which can be use in most part of the OS to ease the development of the OS pub mod utility; +/// module containing drivers +pub mod drivers; use std::{rc::Rc, cell::RefCell}; use kernel::system::System; -use simulator::machine::Machine; +use simulator::{machine::Machine, print::print}; + +use drivers::drv_disk::DrvDisk; +use simulator::disk::Disk; + fn main() { let machine = Machine::init_machine(); let system = Rc::new(RefCell::new(System::new(machine))); + let disk = Disk::init_disk(); + let mut drv_disk = DrvDisk::init_drv_disk(disk); + + let mut data = Vec::new(); + data.push(1); data.push(1); data.push(1); + + let mut data2: Vec = Vec::new(); + + drv_disk.write_sector(0, &mut data); + + drv_disk.read_sector(0, &mut data2); + + if data == data2 { + print!("success"); + }else { + print!("fail"); + } + System::freeze(system); } diff --git a/src/simulator/disk.rs b/src/simulator/disk.rs index 5f0356e..6e0cbf7 100644 --- a/src/simulator/disk.rs +++ b/src/simulator/disk.rs @@ -1,5 +1,11 @@ use core::panic; -use std::{fs::{File, self}, io::{BufReader, Read, Seek, SeekFrom, Write}}; +use std::io; +use std::io::BufReader; +use std::io::prelude::*; +use std::fs::File; +use std::io::SeekFrom; + +use libc::close; pub const SECTORS_PER_TRACK : i32 = 32; pub const NUM_TRACKS : i32 = 64; @@ -7,22 +13,20 @@ pub const NUM_SECTORS : i32 = SECTORS_PER_TRACK * NUM_TRACKS; pub const SECTOR_SIZE : i32 = 128; pub struct Disk { - pub file : File, pub last_selector : i32, pub active : bool } impl Disk { - pub fn init_disk(path : String) -> Disk { + pub fn init_disk() -> Disk { Disk { - file : fs::File::open(path).unwrap(), last_selector : 0, active : false } } - pub fn read_request(disk : &mut Disk, sector_number : i32, data : &mut Vec) { + pub fn read_request(disk : &mut Disk, sector_number : i32, data : &mut Vec) -> io::Result<()> { if disk.active { panic!("Only one request at time"); } @@ -31,22 +35,36 @@ impl Disk { panic!("sector_number isn't right"); } - disk.file.seek(SeekFrom::Start((sector_number * SECTOR_SIZE) as u64)); - let mut reader = BufReader::new(&disk.file); - reader.read(data); + let mut f = File::open("test/disk")?; + f.seek(SeekFrom::Start((sector_number * SECTOR_SIZE) as u64))?; + let mut reader = BufReader::new(f); + + match reader.read(data) { + Ok(_) => println!("Data has been read successfully"), + Err(e) => println!("{:?}", e), + _ => () + } + Ok(()) } - pub fn write_request(disk : &mut Disk, sector_number : i32, data : &mut Vec) { + pub fn write_request(disk : &mut Disk, sector_number : i32, data : &mut Vec) -> io::Result<()> { if disk.active { panic!("Only one request at time"); } - if sector_number < 0 || sector_number >= NUM_SECTORS { panic!("sector_number isn't right"); } - - disk.file.seek(SeekFrom::Start((sector_number * SECTOR_SIZE) as u64)); - disk.file.write(data); + + let mut f = File::create("test/disk")?; + f.seek(SeekFrom::Start((sector_number * SECTOR_SIZE) as u64))?; + let res = f.write(data); + match res { + Ok(_) => println!("Data written successfully"), + Err(e) => println!("{:?}", e), + _ => () + } + Ok(()) } + } \ No newline at end of file diff --git a/test/disk b/test/disk new file mode 100644 index 0000000..e7d1ed7 --- /dev/null +++ b/test/disk @@ -0,0 +1 @@ + \ No newline at end of file