switching from vec to slice
This commit is contained in:
parent
d6ac7671dc
commit
153c3d6618
@ -18,7 +18,7 @@ impl DrvDisk {
|
|||||||
/// - **self** driver disk
|
/// - **self** driver disk
|
||||||
/// - **sector_number** sector where to read the data
|
/// - **sector_number** sector where to read the data
|
||||||
/// - **data** where the readed data will be stored
|
/// - **data** where the readed data will be stored
|
||||||
pub fn read_sector(&mut self, sector_number: i32, data: &mut Vec<u8>) {
|
pub fn read_sector(&mut self, sector_number: i32, data: &mut [u8]) {
|
||||||
match Disk::read_request(&mut self.disk, sector_number, data) {
|
match Disk::read_request(&mut self.disk, sector_number, data) {
|
||||||
Err(e) => println!("{:?}", e),
|
Err(e) => println!("{:?}", e),
|
||||||
_ => (),
|
_ => (),
|
||||||
@ -32,7 +32,7 @@ impl DrvDisk {
|
|||||||
/// - **self** driver disk
|
/// - **self** driver disk
|
||||||
/// - **sector_number** sector where to write the data
|
/// - **sector_number** sector where to write the data
|
||||||
/// - **data** where the data to write is stored
|
/// - **data** where the data to write is stored
|
||||||
pub fn write_sector(&mut self, sector_number: i32, data: &mut Vec<u8>) {
|
pub fn write_sector(&mut self, sector_number: i32, data: &[u8]) {
|
||||||
match Disk::write_request(&mut self.disk, sector_number, data) {
|
match Disk::write_request(&mut self.disk, sector_number, data) {
|
||||||
Err(e) => println!("{:?}", e),
|
Err(e) => println!("{:?}", e),
|
||||||
_ => (),
|
_ => (),
|
||||||
|
@ -65,8 +65,6 @@ impl FileHdr {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn re_allocate(&mut self, free_map: &mut BitMap, old_file_size: i32, new_file_size: i32) -> bool {
|
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;
|
let mut new_num_sectors = openfile::div_round_up(new_file_size, SECTOR_SIZE) - self.num_sectors;
|
||||||
self.num_bytes = new_file_size;
|
self.num_bytes = new_file_size;
|
||||||
|
@ -91,7 +91,7 @@ impl Disk {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// read data from a disk, at a certain sector number
|
/// read data from a disk, at a certain sector number
|
||||||
///
|
/// on lit un et un seul secteur
|
||||||
/// `panic!` when the disk is already active or the sector number is impossible
|
/// `panic!` when the disk is already active or the sector number is impossible
|
||||||
///
|
///
|
||||||
/// ### parameters
|
/// ### parameters
|
||||||
@ -99,29 +99,31 @@ impl Disk {
|
|||||||
/// - **disk** to read from
|
/// - **disk** to read from
|
||||||
/// - **sector_number** sector where to read the data
|
/// - **sector_number** sector where to read the data
|
||||||
/// - **data** where the readed data will be stored
|
/// - **data** where the readed data will be stored
|
||||||
pub fn read_request(disk: &mut Disk, sector_number: i32, data: &mut Vec<u8>) -> io::Result<()> {
|
pub fn read_request(disk: &mut Disk, sector_number: i32, data: &mut [u8]) -> io::Result<()> {
|
||||||
if disk.active {
|
if disk.active {
|
||||||
panic!("Only one request at time");
|
panic!("Only one request at time");
|
||||||
}
|
}
|
||||||
|
|
||||||
if sector_number < 0 || sector_number >= NUM_SECTORS {
|
if sector_number < 0 || sector_number >= NUM_SECTORS {
|
||||||
panic!("sector_number isn't right");
|
panic!("sector_number isn't right");
|
||||||
}
|
}
|
||||||
|
if data.len() < (SECTOR_SIZE as usize){
|
||||||
|
panic!("disk::read_request, param slice too small, must be at least {} bytes", SECTOR_SIZE);
|
||||||
|
}
|
||||||
|
|
||||||
disk.active = true;
|
disk.active = true;
|
||||||
disk.disk_file.seek(SeekFrom::Start((sector_number * SECTOR_SIZE) as u64));
|
disk.disk_file.seek(SeekFrom::Start((sector_number * SECTOR_SIZE) as u64));
|
||||||
|
|
||||||
let mut buffer = [0; SECTOR_SIZE as usize];
|
//let mut buffer = [0; SECTOR_SIZE as usize];
|
||||||
disk.disk_file.read(&mut buffer);
|
disk.disk_file.read(&mut data[0..(SECTOR_SIZE as usize)]);
|
||||||
for byte in buffer {
|
/*for byte in buffer {
|
||||||
data.push(byte);
|
data.push(byte);
|
||||||
}
|
}*/
|
||||||
disk.active = false;
|
disk.active = false;
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
/// write data into a disk, at a certain sector number
|
/// write data into a disk, at a certain sector number
|
||||||
///
|
/// on écrit sur le disque un secteur, ni plus, ni moins
|
||||||
/// `panic!` when the disk is already active or the sector number is impossible
|
/// `panic!` when the disk is already active or the sector number is impossible
|
||||||
///
|
///
|
||||||
/// ### parameters
|
/// ### parameters
|
||||||
@ -129,10 +131,12 @@ impl Disk {
|
|||||||
/// - **disk** to write data into
|
/// - **disk** to write data into
|
||||||
/// - **sector_number** sector where to write the data
|
/// - **sector_number** sector where to write the data
|
||||||
/// - **data** where the data to write is stored
|
/// - **data** where the data to write is stored
|
||||||
|
///
|
||||||
pub fn write_request(
|
pub fn write_request(
|
||||||
disk: &mut Disk,
|
disk: &mut Disk,
|
||||||
sector_number: i32,
|
sector_number: i32,
|
||||||
data: &mut Vec<u8>,
|
//data: &mut Vec<u8>,
|
||||||
|
data: &[u8],
|
||||||
) -> io::Result<()> {
|
) -> io::Result<()> {
|
||||||
if disk.active {
|
if disk.active {
|
||||||
panic!("Only one request at time");
|
panic!("Only one request at time");
|
||||||
@ -140,10 +144,14 @@ impl Disk {
|
|||||||
if sector_number < 0 || sector_number >= NUM_SECTORS {
|
if sector_number < 0 || sector_number >= NUM_SECTORS {
|
||||||
panic!("sector_number isn't right");
|
panic!("sector_number isn't right");
|
||||||
}
|
}
|
||||||
|
if data.len() < (SECTOR_SIZE as usize) {
|
||||||
|
panic!("disk::write_request, param slice must contain at least enought to write a full sector")
|
||||||
|
}
|
||||||
|
|
||||||
disk.active = true;
|
disk.active = true;
|
||||||
disk.disk_file
|
disk.disk_file
|
||||||
.seek(SeekFrom::Start((sector_number * SECTOR_SIZE) as u64))?;
|
.seek(SeekFrom::Start((sector_number * SECTOR_SIZE) as u64))?;
|
||||||
|
/*
|
||||||
let mut i = 0;
|
let mut i = 0;
|
||||||
let mut buff = Vec::new();
|
let mut buff = Vec::new();
|
||||||
for value in data {
|
for value in data {
|
||||||
@ -153,8 +161,8 @@ impl Disk {
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
*/
|
||||||
let res = disk.disk_file.write(&buff);
|
let res = disk.disk_file.write(&data[0..(SECTOR_SIZE as usize)]);
|
||||||
match res {
|
match res {
|
||||||
Ok(_) => println!("Data written successfully"),
|
Ok(_) => println!("Data written successfully"),
|
||||||
Err(e) => println!("{:?}", e),
|
Err(e) => println!("{:?}", e),
|
||||||
|
Loading…
x
Reference in New Issue
Block a user