diff --git a/src/filesys/filehdr.rs b/src/filesys/filehdr.rs index 01fe523..9a2cf8f 100644 --- a/src/filesys/filehdr.rs +++ b/src/filesys/filehdr.rs @@ -1,4 +1,5 @@ use crate::drivers::drv_disk; +use crate::filesys::openfile; use crate::{simulator::disk::SECTOR_SIZE, utility::bitmap::BitMap}; pub const MAX_HEADER_SECTORS: i32 = 32; pub const DATAS_IN_FIRST_SECTOR: i32 = (SECTOR_SIZE - 5 * 8) /8 ; @@ -43,8 +44,8 @@ impl FileHdr { panic!("file size is too long"); } - self.num_sectors = (file_size + SECTOR_SIZE -1) / SECTOR_SIZE; - self.num_header_sectors = ((self.num_sectors - DATAS_IN_FIRST_SECTOR)+DATAS_IN_SECTOR-1) / DATAS_IN_SECTOR; + self.num_sectors = openfile::div_round_up(file_size, SECTOR_SIZE); + self.num_header_sectors = openfile::div_round_up(self.num_sectors-DATAS_IN_FIRST_SECTOR, DATAS_IN_SECTOR); // Check if there is enough free sectors for both of them if free_map.num_clear() < (self.num_sectors + self.num_header_sectors) as i32 { @@ -63,11 +64,14 @@ impl FileHdr { return true; } - pub fn re_allocate(&mut self, mut free_map: BitMap, old_file_size: i32, new_file_size: i32) -> bool { - let mut new_num_sectors = ((new_file_size + SECTOR_SIZE -1) / SECTOR_SIZE) - self.num_sectors; + pub fn re_allocate(&mut self, free_map: &mut BitMap, old_file_size: i32, new_file_size: i32) -> bool { + + openfile::div_round_up(1,1); + + let mut new_num_sectors = openfile::div_round_up(new_file_size, SECTOR_SIZE) - self.num_sectors; self.num_bytes = new_file_size; - let mut new_num_header_sectors = (((self.num_sectors - DATAS_IN_FIRST_SECTOR)+DATAS_IN_SECTOR-1) / DATAS_IN_SECTOR) - self.num_header_sectors; + let mut new_num_header_sectors = openfile::div_round_up(self.num_sectors-DATAS_IN_FIRST_SECTOR, DATAS_IN_SECTOR) - self.num_header_sectors; assert!(new_file_size <= MAX_FILE_LENGTH); diff --git a/src/filesys/filesys.rs b/src/filesys/filesys.rs index 2f47453..9e8aad4 100644 --- a/src/filesys/filesys.rs +++ b/src/filesys/filesys.rs @@ -1,4 +1,4 @@ -use crate::{kernel::mgerror::ErrorCode, simulator::disk, utility::bitmap::BitMap, drivers::drv_disk::DrvDisk}; +use crate::{kernel::mgerror::ErrorCode, simulator::disk, utility::bitmap::BitMap, drivers::drv_disk::DrvDisk, filesys::openfile}; use super::{filehdr::FileHdr, openfile::OpenFile, directory::Directory}; @@ -7,6 +7,9 @@ pub const FREE_MAP_SECTOR: i32 = 0; pub const DIRECTORY_SECTOR: i32 = 1; pub const NUM_DIR_ENTRIES: i32 = 10; pub const DIRECTORY_FILE_SIZE: i32 = 100; //std::mem::size_of() * NUM_DIR_ENTRIES; +pub const FREE_MAP_FILE_PATH : &str = "free_map_file"; +pub const DIRECTORY_FILE_PATH : &str = "directory_file"; + /// decompose the name of the first directory /// of the path given in argument, as well as the remainder of the path. @@ -335,14 +338,22 @@ impl Filesys { //TODO } + /* + Temporairement, et pour des raison de test, + Nous allons utiliser un fichier unix dans lequels + Donc pour le moment ces fonctions renvoient le nom fichier contenant ces infos + */ /// return the free map file (used by the open file table). - pub fn get_free_map_file(self) -> OpenFile { - self.free_map_file + /// + pub fn get_free_map_file() -> String { + //return self.free_map_file.get_name(); + return String::from(FREE_MAP_FILE_PATH); } /// return the base directory file (used by the open file table). - pub fn get_dir_file(self) -> OpenFile { - self.directory_file + pub fn get_dir_file() -> String { + //return self.directory_file.get_name(); + return String::from(DIRECTORY_FILE_PATH); } /// Create a directory in the Nachos file system (similar to UNIX create). diff --git a/src/filesys/openfile.rs b/src/filesys/openfile.rs index 53c4dd4..17ff6d6 100644 --- a/src/filesys/openfile.rs +++ b/src/filesys/openfile.rs @@ -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, num_bytes : i32, position : i32) -> i32 { + pub fn write_at(driver_disk : &mut DrvDisk, file : &mut OpenFile, from : &mut Vec, 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, num_bytes : i32) -> i32 { @@ -183,6 +195,11 @@ impl OpenFile { file.seekPosition += result; result } + + + pub fn get_name(&self) -> String { + return self.name.clone(); + } }