Run clippy on initrd_maker

This commit is contained in:
pjht 2022-11-18 16:29:58 -06:00
parent 9e48568e22
commit 52d5e67f44

View File

@ -15,16 +15,16 @@ fn main() {
let file_name = file_path.file_name().unwrap();
let file_name_len =
u16::try_from(file_name.len()).expect("File name length greater than 256 bytes");
if file_name_len > MAX_NAME_LEN {
panic!(
"File {:?} has name longer than {} bytes",
file_name, MAX_NAME_LEN
);
}
assert!(
file_name_len <= MAX_NAME_LEN,
"File {:?} has name longer than {} bytes",
file_name,
MAX_NAME_LEN
);
let mut file = File::open(file_path).expect("File did not exist");
let length = u16::try_from(file.metadata().expect("Could not get file metadata").len())
.expect("File size greater than 64 KiB");
let file_num_blocks = (length / BLOCK_SIZE) + if length % BLOCK_SIZE != 0 { 1 } else { 0 };
let file_num_blocks = (length / BLOCK_SIZE) + if length % BLOCK_SIZE == 0 { 0 } else { 1 };
let mut header_block = Vec::new();
header_block.extend_from_slice(&length.to_be_bytes());
header_block.extend_from_slice(&file_num_blocks.to_be_bytes());
@ -35,10 +35,11 @@ fn main() {
archive
.write_all(&header_block)
.expect("Could not write to initrd");
let bytes_written =
io::copy(&mut file, &mut archive).expect("Could not read from file/write to initrd");
if (bytes_written as u16 % BLOCK_SIZE) != 0 {
let bytes_padding = (BLOCK_SIZE) - (bytes_written as u16 % BLOCK_SIZE);
#[allow(clippy::cast_possible_truncation)]
let bytes_written = io::copy(&mut file, &mut archive)
.expect("Could not read from file/write to initrd") as u16;
if (bytes_written % BLOCK_SIZE) != 0 {
let bytes_padding = (BLOCK_SIZE) - (bytes_written % BLOCK_SIZE);
for _ in 0..bytes_padding {
archive.write_all(&[0]).expect("Could not write to initrd");
}