From 50d020d05c44a8e50e5aa4564a778901e916d47e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafa=C5=82=20Harabie=C5=84?= Date: Sat, 23 Sep 2017 22:22:25 +0200 Subject: [PATCH] Fix reading long files. --- src/fs.rs | 3 ++- src/table.rs | 6 +++--- tests/integration-test.rs | 18 ++++++++++++++++++ 3 files changed, 23 insertions(+), 4 deletions(-) diff --git a/src/fs.rs b/src/fs.rs index f2e88ca..7734a53 100644 --- a/src/fs.rs +++ b/src/fs.rs @@ -143,7 +143,8 @@ impl FatFileSystem { println!("fat_type_label {}", fat_type_label_str); } - // FIXME: other versions + let fat_offset = boot.bpb.reserved_sector_count * boot.bpb.bytes_per_sector; + rdr.seek(SeekFrom::Start(fat_offset as u64))?; let table_size_bytes = table_size * boot.bpb.bytes_per_sector as u32; let table: Box = match fat_type { FatType::Fat12 => Box::new(FatTable12::read(&mut rdr, table_size_bytes as usize)?), diff --git a/src/table.rs b/src/table.rs index ec7a016..e9e858c 100644 --- a/src/table.rs +++ b/src/table.rs @@ -53,7 +53,7 @@ pub trait FatTable { impl FatTable for FatTable32 { fn get_next_cluster(&self, cluster: u32) -> Option { let val = self.table[cluster as usize] & 0x0FFFFFFF; - if val >= 0x0FFFFFF7 { + if val <= 1 || val >= 0x0FFFFFF7 { None } else { Some(val) @@ -64,7 +64,7 @@ impl FatTable for FatTable32 { impl FatTable for FatTable16 { fn get_next_cluster(&self, cluster: u32) -> Option { let val = self.table[cluster as usize]; - if val >= 0xFFF7 { + if val <= 1 || val >= 0xFFF7 { None } else { Some(val as u32) @@ -83,7 +83,7 @@ impl FatTable for FatTable12 { } else { val1 | (val2 & 0x0F) }; - if val >= 0xFF7 { + if val <= 1 || val >= 0xFF7 { None } else { Some(val as u32) diff --git a/tests/integration-test.rs b/tests/integration-test.rs index 0a0d47a..fc1b4bc 100644 --- a/tests/integration-test.rs +++ b/tests/integration-test.rs @@ -2,10 +2,13 @@ extern crate rustfat; use std::fs::File; use std::io::BufReader; +use std::io::prelude::*; use std::str; use rustfat::FatFileSystem; +const TEST_TEXT: &'static str = "Rust is cool!\n"; + fn test_img(name: &str) { let file = File::open(name).unwrap(); let buf_rdr = BufReader::new(file); @@ -16,6 +19,21 @@ fn test_img(name: &str) { assert_eq!(entries[0].get_name(), "LONG.TXT"); assert_eq!(entries[1].get_name(), "SHORT.TXT"); assert_eq!(entries[2].get_name(), "VERY"); + + { + let mut short_file = entries[1].get_file(); + let mut buf = Vec::new(); + short_file.read_to_end(&mut buf).unwrap(); + assert_eq!(str::from_utf8(&buf).unwrap(), TEST_TEXT); + } + + { + let mut long_file = entries[0].get_file(); + let mut buf = Vec::new(); + long_file.read_to_end(&mut buf).unwrap(); + assert_eq!(str::from_utf8(&buf).unwrap(), TEST_TEXT.repeat(1000)); + } + } #[test]