nouvelle version du driver et du disk
This commit is contained in:
@ -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<u8>) {
|
||||
pub fn read_request(disk : &mut Disk, sector_number : i32, data : &mut Vec<u8>) -> 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<u8>) {
|
||||
pub fn write_request(disk : &mut Disk, sector_number : i32, data : &mut Vec<u8>) -> 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(())
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user