open_file impl
This commit is contained in:
parent
856cbdfe01
commit
2549103636
@ -20,6 +20,18 @@ pub struct FileHdr {
|
|||||||
|
|
||||||
impl FileHdr {
|
impl FileHdr {
|
||||||
|
|
||||||
|
//Juste histoire d'avoir un constructeur, temporaire
|
||||||
|
pub fn new() -> FileHdr {
|
||||||
|
FileHdr{
|
||||||
|
is_dir : 0,
|
||||||
|
num_bytes : 0,
|
||||||
|
num_sectors : 0,
|
||||||
|
data_sectors : Vec::new(),
|
||||||
|
num_header_sectors : 0,
|
||||||
|
header_sectors : [0 ; MAX_HEADER_SECTORS as usize]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub fn allocate(&mut self, mut free_map: BitMap, file_size: i32) -> bool {
|
pub fn allocate(&mut self, mut free_map: BitMap, file_size: i32) -> bool {
|
||||||
self.num_bytes = file_size;
|
self.num_bytes = file_size;
|
||||||
if file_size > MAX_FILE_LENGTH {
|
if file_size > MAX_FILE_LENGTH {
|
||||||
|
@ -0,0 +1,161 @@
|
|||||||
|
|
||||||
|
use crate::{simulator::{self, disk}, drivers::drv_disk::{DrvDisk, self}};
|
||||||
|
|
||||||
|
use super::filehdr::FileHdr;
|
||||||
|
|
||||||
|
|
||||||
|
pub const MAX_FILE_NAME_SIZE : i32 = 500;
|
||||||
|
|
||||||
|
pub struct OpenFile {
|
||||||
|
pub name : String, // the file's
|
||||||
|
pub hdr : FileHdr, // Header for this file
|
||||||
|
pub seekPosition : i32, // Current position in the file (in byte)
|
||||||
|
pub fSector : i32 // this file first sector on the disk (fichiers stockés de maniere contigue)
|
||||||
|
}
|
||||||
|
|
||||||
|
//division arrondie vers le bas
|
||||||
|
pub fn div_round_down(a : i32, b : i32) -> i32{
|
||||||
|
a/b
|
||||||
|
}
|
||||||
|
|
||||||
|
//division arrondie vers le haut
|
||||||
|
pub fn div_round_up(a : i32, b : i32) -> i32{
|
||||||
|
let mut result = a/b;
|
||||||
|
|
||||||
|
if a%b > 0 {
|
||||||
|
result + 1
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
result
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
impl OpenFile {
|
||||||
|
|
||||||
|
/*
|
||||||
|
Creer un "fichier ouvert", charge le file_hdr depuis disk
|
||||||
|
*/
|
||||||
|
pub fn open_file(sector : i32) -> OpenFile{
|
||||||
|
let file_hdr : FileHdr = FileHdr::new();
|
||||||
|
let name : String = String::from("");
|
||||||
|
|
||||||
|
//ici appel a fetchFrom(sector) sur file_hdr
|
||||||
|
|
||||||
|
OpenFile { name: name,
|
||||||
|
hdr: file_hdr,
|
||||||
|
seekPosition: 0,
|
||||||
|
fSector: sector }
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn seek(file : &mut OpenFile, position : i32){
|
||||||
|
file.seekPosition = position;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* params :
|
||||||
|
into : buffer de reception
|
||||||
|
num_bytes : nombre d'octets à lire
|
||||||
|
position : position du premier octet à lire dans le fichier (header compté??)
|
||||||
|
*/
|
||||||
|
pub fn read_at(driver_disk : &mut DrvDisk, file : &mut OpenFile, into : &mut Vec<u8>, num_bytes : i32, position : i32) -> i32 {
|
||||||
|
|
||||||
|
let mut nbr_octet_lu = num_bytes;
|
||||||
|
let file_length = file.hdr.file_length();
|
||||||
|
let mut first_sector : i32;
|
||||||
|
let mut last_sector : i32;
|
||||||
|
let mut num_sectors : i32;
|
||||||
|
let mut start : i32;
|
||||||
|
let mut end : i32;
|
||||||
|
|
||||||
|
if ( num_bytes <= 0)||( position < 0)||( position >= file_length) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (position + num_bytes > file_length) {
|
||||||
|
nbr_octet_lu = file_length - position;
|
||||||
|
}
|
||||||
|
|
||||||
|
first_sector = div_round_down(position,disk::SECTOR_SIZE);
|
||||||
|
last_sector = div_round_up(position + num_bytes - 1, disk::SECTOR_SIZE);
|
||||||
|
num_sectors = last_sector - first_sector + 1;
|
||||||
|
|
||||||
|
let mut buffer = vec![0_u8; (num_sectors * disk::SECTOR_SIZE) as usize];
|
||||||
|
let mut sector_buffer = vec![0_u8; disk::SECTOR_SIZE as usize];
|
||||||
|
|
||||||
|
for i in first_sector..(last_sector + 1){
|
||||||
|
|
||||||
|
DrvDisk::read_sector(driver_disk, i, &mut sector_buffer);
|
||||||
|
|
||||||
|
for j in 0..sector_buffer.len(){
|
||||||
|
buffer.push(sector_buffer[j]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
start = position%disk::SECTOR_SIZE;
|
||||||
|
end = start + nbr_octet_lu - 1;
|
||||||
|
|
||||||
|
for i in start..(end + 1){
|
||||||
|
into.push(buffer[i as usize]);
|
||||||
|
}
|
||||||
|
|
||||||
|
nbr_octet_lu
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/* params :
|
||||||
|
from : données à écrire
|
||||||
|
num_bytes : nombre d'octets à écrire depuis from
|
||||||
|
position : position du premier octet à écrire dans le fichier(header compté??)
|
||||||
|
*/
|
||||||
|
pub fn write_at(driver_disk : &mut DrvDisk, file : &mut OpenFile, from : &mut Vec<u8>, num_bytes : i32, position : i32) -> i32 {
|
||||||
|
|
||||||
|
let mut nbr_octet_ecrit = num_bytes;
|
||||||
|
let mut file_length = file.hdr.file_length();
|
||||||
|
let mut max_file_length = file.hdr.max_file_length();
|
||||||
|
|
||||||
|
let mut first_sector : i32;
|
||||||
|
let mut last_sector : i32;
|
||||||
|
let mut nbr_sectors : i32;
|
||||||
|
|
||||||
|
let mut first_aligned : bool;
|
||||||
|
let mut last_aligned : bool;
|
||||||
|
|
||||||
|
if (num_bytes <= 0)||(position < 0) ||(position > file_length){
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
if num_bytes + file_length > max_file_length {
|
||||||
|
/*
|
||||||
|
PARTIE MANQUANTE : allocation de nouveau secteurs si fichier trop petit pour l'ecriture
|
||||||
|
need FetchFrom
|
||||||
|
*/
|
||||||
|
}
|
||||||
|
else if position + num_bytes > file_length{
|
||||||
|
file.hdr.change_file_length(position + num_bytes);
|
||||||
|
}
|
||||||
|
0
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn read(driver_disk : &mut DrvDisk, file : &mut OpenFile, into : &mut Vec<u8>, num_bytes : i32) -> i32 {
|
||||||
|
|
||||||
|
let result = OpenFile::read_at(driver_disk, file, into, num_bytes, file.seekPosition);
|
||||||
|
file.seekPosition += result;
|
||||||
|
result
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn write(driver_disk : &mut DrvDisk, file : &mut OpenFile, from : &mut Vec<u8>, num_bytes : i32) -> i32 {
|
||||||
|
|
||||||
|
let result = OpenFile::write_at(driver_disk, file, from, num_bytes, file.seekPosition);
|
||||||
|
file.seekPosition += result;
|
||||||
|
result
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user