Fix reading long files.

This commit is contained in:
Rafał Harabień 2017-09-23 22:22:25 +02:00
parent e44d7fc6c2
commit 50d020d05c
3 changed files with 23 additions and 4 deletions

View File

@ -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<FatTable> = match fat_type {
FatType::Fat12 => Box::new(FatTable12::read(&mut rdr, table_size_bytes as usize)?),

View File

@ -53,7 +53,7 @@ pub trait FatTable {
impl FatTable for FatTable32 {
fn get_next_cluster(&self, cluster: u32) -> Option<u32> {
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<u32> {
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)

View File

@ -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]