From 6230fe91f92dd5877be2e10835bbe06f94b932fd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafa=C5=82=20Harabie=C5=84?= Date: Sat, 26 May 2018 17:02:36 +0200 Subject: [PATCH] Do not ignore 2 last clusters when allocating --- src/table.rs | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/table.rs b/src/table.rs index f3744c2..cf997af 100644 --- a/src/table.rs +++ b/src/table.rs @@ -13,6 +13,8 @@ type Fat12 = Fat; type Fat16 = Fat; type Fat32 = Fat; +const RESERVED_FAT_ENTRIES: u32 = 2; + #[derive(Debug, Clone, Copy)] enum FatValue { Free, @@ -62,11 +64,12 @@ fn find_free_cluster(fat: &mut ReadSeek, fat_type: FatType, start_cluster: u32, } pub(crate) fn alloc_cluster(fat: &mut DiskSlice, fat_type: FatType, prev_cluster: Option, hint: Option, total_clusters: u32) -> io::Result { + let end_cluster = total_clusters + RESERVED_FAT_ENTRIES; let start_cluster = match hint { - Some(n) if n < total_clusters => n, + Some(n) if n < end_cluster => n, _ => 2, }; - let new_cluster = match find_free_cluster(fat, fat_type, start_cluster, total_clusters) { + let new_cluster = match find_free_cluster(fat, fat_type, start_cluster, end_cluster) { Ok(n) => n, Err(_) if start_cluster > 2 => find_free_cluster(fat, fat_type, 2, start_cluster)?, Err(e) => return Err(e),