1
0
forked from Rativel/BurritOS

changing impl

This commit is contained in:
Moysan Gabriel
2023-04-18 23:48:57 +02:00
parent 9a23b1bb37
commit d6ac7671dc
3 changed files with 54 additions and 22 deletions

View File

@@ -1,7 +1,7 @@
use crate::{simulator::{self, disk}, drivers::drv_disk::{DrvDisk, self}};
use crate::{simulator::{self, disk}, drivers::drv_disk::{DrvDisk, self}, utility::bitmap::BitMap};
use super::filehdr::FileHdr;
use super::{filehdr::FileHdr, filesys::{Filesys, self}};
pub const MAX_FILE_NAME_SIZE : i32 = 500;
@@ -102,9 +102,8 @@ impl OpenFile {
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 {
pub fn write_at(driver_disk : &mut DrvDisk, file : &mut OpenFile, from : &mut Vec<u8>, num_bytes_to_write : 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();
@@ -115,29 +114,42 @@ impl OpenFile {
let mut first_aligned : bool;
let mut last_aligned : bool;
let mut num_bytes = num_bytes_to_write; //le nombre d'octets que l'on va réellement écrire, on ne pourra pas forcément ecrire la taille demandée en paramètre
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
*/
//bit_map represente les secteur du disque, pour savoir ou trouver de la place
let mut bit_map = BitMap::init_bitmap(disk::NUM_SECTORS as usize);
bit_map.fetch_from(filesys::FREE_MAP_FILE_PATH);
if !file.hdr.re_allocate(&mut bit_map, file_length, position + num_bytes){
//plus de place sur le disquen on se contente d'écrire ce que l'on peut
num_bytes = file_length - position;
}
else {
//write_back the header and freemap to disk
file.hdr.write_back(file.fSector, driver_disk);
bit_map.write_back(filesys::FREE_MAP_FILE_PATH);
}
}
else if position + num_bytes > file_length{
file.hdr.change_file_length(position + num_bytes);
}
first_sector = div_round_down(position, disk::SECTOR_SIZE);
last_sector = div_round_down(position + nbr_octet_ecrit - 1, disk::SECTOR_SIZE);
last_sector = div_round_down(position + num_bytes - 1, disk::SECTOR_SIZE);
nbr_sectors = last_sector + first_sector + 1;
let mut buffer = vec![0_u8; (nbr_sectors * disk::SECTOR_SIZE) as usize];
let mut sector_buffer = vec![0_u8; disk::SECTOR_SIZE as usize];
first_aligned = position == first_sector*disk::SECTOR_SIZE;//to use
last_aligned = position + nbr_octet_ecrit == (last_sector + 1)*disk::SECTOR_SIZE;//to use
last_aligned = position + num_bytes == (last_sector + 1)*disk::SECTOR_SIZE;//to use
/*
A CHANGER
@@ -152,7 +164,7 @@ impl OpenFile {
//on ecrase dans buffer avec les données de from
let start = position%disk::SECTOR_SIZE;
let end = start + nbr_octet_ecrit - 1;
let end = start + num_bytes - 1;
for i in start..(end + 1){
buffer[i as usize] = from[(i-start) as usize];
@@ -167,7 +179,7 @@ impl OpenFile {
//BESOIN DE CHANGER VEC U8 EN SLICE DANS les fonctions read & write de drv_disk & disk
}
nbr_octet_ecrit
num_bytes
}
pub fn read(driver_disk : &mut DrvDisk, file : &mut OpenFile, into : &mut Vec<u8>, num_bytes : i32) -> i32 {
@@ -183,6 +195,11 @@ impl OpenFile {
file.seekPosition += result;
result
}
pub fn get_name(&self) -> String {
return self.name.clone();
}
}