@@ -179,6 +179,9 @@ impl FatVolume {
179
179
where
180
180
D : BlockDevice ,
181
181
{
182
+ if cluster. 0 > ( u32:: MAX / 4 ) {
183
+ panic ! ( "next_cluster called on invalid cluster {:x?}" , cluster) ;
184
+ }
182
185
match & self . fat_specific_info {
183
186
FatSpecificInfo :: Fat16 ( _fat16_info) => {
184
187
let fat_offset = cluster. 0 * 2 ;
@@ -360,19 +363,24 @@ impl FatVolume {
360
363
FatSpecificInfo :: Fat32 ( fat32_info) => {
361
364
// All directories on FAT32 have a cluster chain but the root
362
365
// dir starts in a specified cluster.
363
- let mut first_dir_block_num = match dir. cluster {
364
- ClusterId :: ROOT_DIR => self . cluster_to_block ( fat32_info. first_root_dir_cluster ) ,
365
- _ => self . cluster_to_block ( dir. cluster ) ,
366
+ let mut current_cluster = match dir. cluster {
367
+ ClusterId :: ROOT_DIR => Some ( fat32_info. first_root_dir_cluster ) ,
368
+ _ => Some ( dir. cluster ) ,
366
369
} ;
367
- let mut current_cluster = Some ( dir. cluster ) ;
370
+ let mut first_dir_block_num = self . cluster_to_block ( dir. cluster ) ;
368
371
let mut blocks = [ Block :: new ( ) ] ;
369
372
370
373
let dir_size = BlockCount ( u32:: from ( self . blocks_per_cluster ) ) ;
374
+ // Walk the cluster chain until we run out of clusters
371
375
while let Some ( cluster) = current_cluster {
376
+ // Loop through the blocks in the cluster
372
377
for block in first_dir_block_num. range ( dir_size) {
378
+ // Read a block of directory entries
373
379
block_device
374
380
. read ( & mut blocks, block, "read_dir" )
375
381
. map_err ( Error :: DeviceError ) ?;
382
+ // Are any entries in the block we just loaded blank? If so
383
+ // we can use them.
376
384
for entry in 0 ..Block :: LEN / OnDiskDirEntry :: LEN {
377
385
let start = entry * OnDiskDirEntry :: LEN ;
378
386
let end = ( entry + 1 ) * OnDiskDirEntry :: LEN ;
@@ -397,6 +405,8 @@ impl FatVolume {
397
405
}
398
406
}
399
407
}
408
+ // Well none of the blocks in that cluster had any space in
409
+ // them, let's fetch another one.
400
410
let mut block_cache = BlockCache :: empty ( ) ;
401
411
current_cluster =
402
412
match self . next_cluster ( block_device, cluster, & mut block_cache) {
@@ -412,6 +422,8 @@ impl FatVolume {
412
422
_ => None ,
413
423
} ;
414
424
}
425
+ // We ran out of clusters in the chain, and apparently we weren't
426
+ // able to make the chain longer, so the disk must be full.
415
427
Err ( Error :: NotEnoughSpace )
416
428
}
417
429
}
0 commit comments