FileHdr write_back
This commit is contained in:
parent
00b4ed0066
commit
1a884f2e98
@ -1,13 +1,12 @@
|
|||||||
use crate::{simulator::disk::SECTOR_SIZE, utility::bitmap::BitMap};
|
use crate::{simulator::disk::SECTOR_SIZE, utility::bitmap::BitMap};
|
||||||
pub const MAX_HEADER_SECTORS: i32 = 32;
|
pub const MAX_HEADER_SECTORS: i32 = 32;
|
||||||
pub const DATAS_IN_FIRST_SECTOR: i32 = (SECTOR_SIZE - 5 * 8) /8;
|
pub const DATAS_IN_FIRST_SECTOR: i32 = (SECTOR_SIZE - 5 * 8) /8 ;
|
||||||
pub const DATAS_IN_SECTOR: i32 = (SECTOR_SIZE - 1 * 8) /8;
|
pub const DATAS_IN_SECTOR: i32 = (SECTOR_SIZE - 1 * 8) /8;
|
||||||
pub const MAX_DATA_SECTORS: i32 = ((MAX_HEADER_SECTORS-1) * DATAS_IN_SECTOR + DATAS_IN_FIRST_SECTOR);
|
pub const MAX_DATA_SECTORS: i32 = (MAX_HEADER_SECTORS-1) * DATAS_IN_SECTOR + DATAS_IN_FIRST_SECTOR;
|
||||||
pub const MAX_FILE_LENGTH: i32 = ((MAX_DATA_SECTORS) * SECTOR_SIZE);
|
pub const MAX_FILE_LENGTH: i32 = (MAX_DATA_SECTORS) * SECTOR_SIZE;
|
||||||
|
|
||||||
use crate::DrvDisk;
|
use crate::DrvDisk;
|
||||||
use crate::Disk;
|
use crate::Disk;
|
||||||
use crate::OpenFile;
|
|
||||||
|
|
||||||
use std::mem;
|
use std::mem;
|
||||||
|
|
||||||
@ -20,12 +19,13 @@ pub struct FileHdr {
|
|||||||
data_sectors: Vec<i32>,
|
data_sectors: Vec<i32>,
|
||||||
num_header_sectors: i32,
|
num_header_sectors: i32,
|
||||||
header_sectors: [i32; MAX_HEADER_SECTORS as usize],
|
header_sectors: [i32; MAX_HEADER_SECTORS as usize],
|
||||||
|
drv_disk: DrvDisk,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl FileHdr {
|
impl FileHdr {
|
||||||
|
|
||||||
|
|
||||||
pub fn init_file_hdr() -> FileHdr {
|
pub fn init_file_hdr(drv_disk_: DrvDisk) -> FileHdr {
|
||||||
FileHdr {
|
FileHdr {
|
||||||
is_dir: 0,
|
is_dir: 0,
|
||||||
num_bytes: 0,
|
num_bytes: 0,
|
||||||
@ -33,6 +33,7 @@ impl FileHdr {
|
|||||||
data_sectors: Vec::new(),
|
data_sectors: Vec::new(),
|
||||||
num_header_sectors: 0,
|
num_header_sectors: 0,
|
||||||
header_sectors: [0;MAX_HEADER_SECTORS as usize],
|
header_sectors: [0;MAX_HEADER_SECTORS as usize],
|
||||||
|
drv_disk: drv_disk_,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -47,7 +48,7 @@ impl FileHdr {
|
|||||||
self.num_header_sectors = ((self.num_sectors - DATAS_IN_FIRST_SECTOR)+DATAS_IN_SECTOR-1) / DATAS_IN_SECTOR;
|
self.num_header_sectors = ((self.num_sectors - DATAS_IN_FIRST_SECTOR)+DATAS_IN_SECTOR-1) / DATAS_IN_SECTOR;
|
||||||
|
|
||||||
// Check if there is enough free sectors for both of them
|
// Check if there is enough free sectors for both of them
|
||||||
if free_map.num_clear() < self.num_sectors + self.num_header_sectors {
|
if free_map.num_clear() < (self.num_sectors + self.num_header_sectors) as i32 {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -64,14 +65,14 @@ impl FileHdr {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn re_allocate(&mut self, mut free_map: BitMap, old_file_size: i32, new_file_size: i32) -> bool {
|
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;
|
let mut new_num_sectors = ((new_file_size + SECTOR_SIZE -1) / SECTOR_SIZE) - self.num_sectors;
|
||||||
self.num_bytes = new_file_size;
|
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 = (((self.num_sectors - DATAS_IN_FIRST_SECTOR)+DATAS_IN_SECTOR-1) / DATAS_IN_SECTOR) - self.num_header_sectors;
|
||||||
|
|
||||||
assert!(new_file_size <= MAX_FILE_LENGTH);
|
assert!(new_file_size <= MAX_FILE_LENGTH);
|
||||||
|
|
||||||
if free_map.num_clear() < new_num_sectors + new_num_header_sectors {
|
if free_map.num_clear() < (new_num_sectors + new_num_header_sectors) as i32{
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -101,18 +102,101 @@ impl FileHdr {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn fetch_from(&self, file: OpenFile) {
|
fn fetch_from(&mut self, sector: i32) {
|
||||||
file.read_at(self.table,self.table_size * mem::size_of::<DirectoryEntry>() as i32);
|
let mut sector_img = vec![0; SECTOR_SIZE as usize / std::mem::size_of::<i32>() ];
|
||||||
|
let mut data_sectors = vec![0; MAX_DATA_SECTORS as usize];
|
||||||
|
|
||||||
|
// Fills the temporary buffer with zeros
|
||||||
|
sector_img.fill(0);
|
||||||
|
|
||||||
|
// Read the header from the disk
|
||||||
|
// and put it in the temporary buffer
|
||||||
|
DrvDisk::read_sector(&mut self.drv_disk, sector, &mut sector_img);
|
||||||
|
|
||||||
|
// Set up the memory image of the file header
|
||||||
|
// from the newly read buffer
|
||||||
|
self.is_dir = sector_img[0] as i32;
|
||||||
|
self.num_bytes = sector_img[1] as i32;
|
||||||
|
self.num_sectors = sector_img[2] as i32;
|
||||||
|
self.num_header_sectors = sector_img[3] as i32;
|
||||||
|
|
||||||
|
// Get the first header sector
|
||||||
|
self.header_sectors[0] = *FileHdr::next_header_sector(sector_img);
|
||||||
|
|
||||||
|
// Get the number of the data sectors stored into
|
||||||
|
// the header sector in disk
|
||||||
|
for i in 4..DATAS_IN_SECTOR as usize {
|
||||||
|
data_sectors[i - 4] = sector_img[i];
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get the other numbers of header sectors and data sectors
|
||||||
|
for i in 0..self.num_header_sectors as usize {
|
||||||
|
// Fill the temporary buffer with zeroes
|
||||||
|
sector_img.fill(0);
|
||||||
|
|
||||||
|
DrvDisk::read_sector(&mut self.drv_disk, self.header_sectors[i], &mut sector_img);
|
||||||
|
|
||||||
|
for j in 0..DATAS_IN_SECTOR as usize {
|
||||||
|
data_sectors[DATAS_IN_FIRST_SECTOR as usize + i * DATAS_IN_SECTOR as usize + j] = sector_img[j];
|
||||||
|
}
|
||||||
|
|
||||||
|
// Make sure we don't go out of bounds
|
||||||
|
if i + 1 < self.num_header_sectors as usize {
|
||||||
|
self.header_sectors[i + 1] = *FileHdr::next_header_sector(&mut sector_img);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
pub fn write_back(&self, file: OpenFile) {
|
fn write_back(&self, sector: i32) {
|
||||||
file.write_at(self.table,self.table_size * mem::size_of::<DirectoryEntry>() as i32);
|
let mut sector_img = vec![0; DATAS_IN_SECTOR as usize];
|
||||||
|
|
||||||
|
// Fills the temporary buffer with zeroes
|
||||||
|
sector_img.fill(0);
|
||||||
|
|
||||||
|
// Fills the header of the first header sector
|
||||||
|
sector_img[0] = self.is_dir;
|
||||||
|
sector_img[1] = self.num_bytes;
|
||||||
|
sector_img[2] = self.num_sectors;
|
||||||
|
sector_img[3] = self.num_header_sectors;
|
||||||
|
|
||||||
|
// Fills the number of the data sectors and the first header
|
||||||
|
// sector in the temporary buffer
|
||||||
|
for i in 4..DATAS_IN_SECTOR as usize {
|
||||||
|
sector_img[i] = self.data_sectors[(i - 4)];
|
||||||
|
}
|
||||||
|
|
||||||
|
// Write the first header sector into disk
|
||||||
|
*FileHdr::next_header_sector(&mut sector_img) = self.header_sectors[0];
|
||||||
|
let mut vec_u8: Vec<u8> = sector_img.iter().map(|&x| x as u8).collect();
|
||||||
|
DrvDisk::write_sector(&mut self.drv_disk, sector, &mut vec_u8);
|
||||||
|
|
||||||
|
// Write the following header sectors into disk
|
||||||
|
for i in 0..self.num_header_sectors {
|
||||||
|
sector_img.fill(0);
|
||||||
|
|
||||||
|
for j in 0..DATAS_IN_SECTOR {
|
||||||
|
sector_img[j as usize] = self.data_sectors[(j + DATAS_IN_FIRST_SECTOR + i * DATAS_IN_SECTOR) as usize];
|
||||||
|
}
|
||||||
|
|
||||||
|
if i + 1 < self.num_header_sectors {
|
||||||
|
*FileHdr::next_header_sector(&mut sector_img) = self.header_sectors[(i + 1) as usize];
|
||||||
|
} else {
|
||||||
|
*FileHdr::next_header_sector(&mut sector_img) = 0;
|
||||||
|
}
|
||||||
|
let mut vec_u8: Vec<u8> = sector_img.iter().map(|&x| x as u8).collect();
|
||||||
|
DrvDisk::write_sector(&mut self.drv_disk, self.header_sectors[i as usize], &mut vec_u8);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn next_header_sector(hdr_sector: &mut [i32]) -> &mut i32 {
|
||||||
|
let sector_size = SECTOR_SIZE as usize/ std::mem::size_of::<i32>();
|
||||||
pub fn byte_to_sector(&self,offset: i32) -> i32 {
|
&mut hdr_sector[sector_size - 1]
|
||||||
return self.data_sectors[ (offset / SECTOR_SIZE) as usize];
|
}
|
||||||
|
|
||||||
|
pub fn byte_to_sector(&self,offset: usize) -> i32 {
|
||||||
|
return self.data_sectors[ offset / SECTOR_SIZE as usize];
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn file_length(&self) -> i32 {
|
pub fn file_length(&self) -> i32 {
|
||||||
@ -168,6 +252,9 @@ impl FileHdr {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod test {
|
mod test {
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user