use a of file for disk
This commit is contained in:
parent
bc970a9603
commit
2413d4dec6
@ -3,22 +3,86 @@ use std::fs::File;
|
|||||||
use std::io;
|
use std::io;
|
||||||
use std::io::prelude::*;
|
use std::io::prelude::*;
|
||||||
use std::io::SeekFrom;
|
use std::io::SeekFrom;
|
||||||
|
use std::os::unix::prelude::FileExt;
|
||||||
|
|
||||||
pub const SECTORS_PER_TRACK: i32 = 32;
|
pub const SECTORS_PER_TRACK: i32 = 32;
|
||||||
pub const NUM_TRACKS: i32 = 64;
|
pub const NUM_TRACKS: i32 = 64;
|
||||||
pub const NUM_SECTORS: i32 = SECTORS_PER_TRACK * NUM_TRACKS;
|
pub const NUM_SECTORS: i32 = SECTORS_PER_TRACK * NUM_TRACKS;
|
||||||
pub const SECTOR_SIZE: i32 = 4;
|
pub const SECTOR_SIZE: i32 = 4;//4 octects ?
|
||||||
|
|
||||||
|
|
||||||
|
pub const MAGIC_NUMBER : u32 = 0xABBACDDC;
|
||||||
|
//taille en octets, a confirmer
|
||||||
|
pub const DISK_SIZE: i32 = 4 + NUM_SECTORS*SECTOR_SIZE;//4 <=> sizeof(u32)
|
||||||
|
|
||||||
pub struct Disk {
|
pub struct Disk {
|
||||||
pub last_selector: i32,
|
pub last_sector: i32,//Secteur accédé lors de la précédente requête de lecture/ecriture
|
||||||
pub active: bool,
|
pub active: bool,//C'est notre lock -> Une seule requête de lecture/ecriture à la fois
|
||||||
|
pub disk_file : File //On simule le disque sur un fichier
|
||||||
|
//Manque une ref a une fonction type handler, fournie via constructeur dans Nachos
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Disk {
|
impl Disk {
|
||||||
pub fn init_disk() -> Disk {
|
pub fn init_disk() -> Disk {
|
||||||
|
|
||||||
|
//see Rust File impl FileExt for File For UNIX mode
|
||||||
|
let mut magic_buff : [u8 ; 4] = [0u8 ; 4];
|
||||||
|
let mut readed_magic_number : u32;
|
||||||
|
let disk_file : File;
|
||||||
|
|
||||||
|
let result_open = File::options().
|
||||||
|
read(true).
|
||||||
|
write(true).
|
||||||
|
open("disk_file");
|
||||||
|
|
||||||
|
match result_open {
|
||||||
|
|
||||||
|
Err(e) => {
|
||||||
|
//On creer le fichier et on y écrit le magic number
|
||||||
|
let magic_to_write : [u8 ; 5] = [0xAB, 0xBA, 0xCD, 0xDC, 0];
|
||||||
|
println!("Error opening file : {}", e);
|
||||||
|
|
||||||
|
disk_file = File::options().
|
||||||
|
read(true).
|
||||||
|
write(true).
|
||||||
|
create_new(true).
|
||||||
|
open("disk_file").unwrap();
|
||||||
|
|
||||||
|
disk_file.write_at(&magic_to_write[..], 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(f) => {
|
||||||
|
disk_file = f;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
match disk_file.read_at(&mut magic_buff[..], 0) {
|
||||||
|
|
||||||
|
Ok(t) => {
|
||||||
|
println!("init_disk :: on a lu {} octets", t);
|
||||||
|
readed_magic_number = ((magic_buff[0] as u32) <<24)
|
||||||
|
+ ((magic_buff[1] as u32) <<16)
|
||||||
|
+ ((magic_buff[2] as u32) <<8)
|
||||||
|
+ ((magic_buff[3] as u32) <<0);
|
||||||
|
|
||||||
|
if readed_magic_number != MAGIC_NUMBER {
|
||||||
|
panic!("init_disk :: Did not recognize magic number at the beginning of disk_file,
|
||||||
|
On a lu {:#08x}, Panic", readed_magic_number);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
println!("init_disk :: on a lu le magic_number {:#08x}", readed_magic_number);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Err(e) => {
|
||||||
|
println!("init_disk :: Error reading file : {}", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
Disk {
|
Disk {
|
||||||
last_selector: 0,
|
last_sector: 0,
|
||||||
active: false,
|
active: false,
|
||||||
|
disk_file: disk_file
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -97,11 +161,29 @@ impl Disk {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
pub fn stupid_fct(buf : &[u8]){
|
||||||
|
|
||||||
|
println!("Size of unknown sized parameter buffer : {}", buf.len());
|
||||||
|
for i in 0..buf.len() {
|
||||||
|
println!("buf[{}] = {}", i, buf[i]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod test {
|
mod test {
|
||||||
|
|
||||||
use crate::Disk;
|
use crate::Disk;
|
||||||
|
|
||||||
|
use super::stupid_fct;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_init_stupid_fct() {
|
||||||
|
let buffy: [u8 ; 6] = [1,2,3,4,5,6];
|
||||||
|
stupid_fct(&buffy);
|
||||||
|
|
||||||
|
//EN passant par une ref , on s'abstrait du besoin de connaitre la taille au moment de la compilation
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_init_disk() {
|
fn test_init_disk() {
|
||||||
let disk = Disk::init_disk();
|
let disk = Disk::init_disk();
|
||||||
|
BIN
test/disk.txt
BIN
test/disk.txt
Binary file not shown.
Loading…
x
Reference in New Issue
Block a user