1
0
forked from Rativel/BurritOS
Files
BurritOS/src/filesys/fsmisc.rs
2023-03-23 12:59:33 +01:00

66 lines
1.6 KiB
Rust

use std::{fs::File, io::{Seek, SeekFrom, Read}};
use super::filesys;
pub const TRANSFER_SIZE: usize = 10;
/// copy the contents of the UNIX file "from" to the Nachos file "to"
///
/// `panic!` when the file from doesn't exist
///
/// - **from** file UNIX
/// - **to** BurritOS file
pub fn copy(from : &str, to : &str) {
let file_from_opt = File::options().read(true).open(from);
let mut file_from : File;
match file_from_opt {
Err(e) => {
panic!("Copy: couldn't open Unix file {}", from);
}
Ok(f) => {
file_from = f;
}
}
let mut file_length_buf = [0; 1];
file_from.seek(SeekFrom::End(2));
file_from.read(&mut file_length_buf);
file_from.seek(SeekFrom::Start(0));
let file_length = file_length_buf[0];
filesys::create(to, file_length);
let open_file = filesys::open(to);
let mut buffer = [0; TRANSFER_SIZE];
loop {
let amount_read = file_from.read(&mut buffer).expect("copy : couldn't read the UNIX file");
open_file.write(&buffer[..amount_read]);
if amount_read != TRANSFER_SIZE {
break;
}
}
}
/// Print the contents of the Nachos file "name".
///
/// `panic!` when the file name doewn't exist
///
/// - **name** of the BurritOS file
pub fn print(name : &str) {
let open_file = filesys::open(name);
let mut buffer = [0; TRANSFER_SIZE];
loop {
let amount_read = open_file.read(&mut buffer);
for i in 0..amount_read {
print!("{:1x} ", buffer[i]);
}
if amount_read != TRANSFER_SIZE {
break;
}
}
}