@@ -34,35 +34,35 @@ where
34
34
data
35
35
} ;
36
36
37
- for ( i, block) in ( start_block..=end_block) . enumerate ( ) {
38
- if !self . is_block_allocated ( file, block) ? {
39
- // TODO: we don't need to allocate if the full content of this block would be zero if the fs allows sparse files
40
- let free_block_address = self . allocate_block ( ) ?;
41
- if free_block_address. is_none ( ) {
42
- return Err ( Error :: NoSpace ) ;
43
- }
44
- let free_block_address = free_block_address. unwrap ( ) ;
45
-
46
- let inode = file. inode_mut ( ) ;
47
- let free_slot = inode. direct_ptrs ( )
48
- . enumerate ( )
49
- . find ( |( _, ptr) | ptr. is_none ( ) )
50
- . map ( |( i, _) | i) ;
51
- if let Some ( free_slot) = free_slot {
52
- inode. set_direct_ptr ( free_slot, Some ( free_block_address) ) ;
53
- self . write_inode ( file. inode_address ( ) , file) ?;
37
+ let mut chunks = data. chunks_exact ( block_size as usize ) ;
38
+ for ( ( i, block) , chunk) in ( start_block..=end_block) . enumerate ( ) . zip ( & mut chunks) {
39
+ let block_address =
40
+ if let Some ( block_address) = self . resolve_block_index ( file, block) ? {
41
+ block_address
54
42
} else {
55
- todo ! ( "allocate indirect block" ) ;
56
- }
57
- }
58
- }
43
+ // TODO: we don't need to allocate if the full content of this block would be zero if the fs allows sparse files
44
+ let free_block_address = self . allocate_block ( ) ?;
45
+ if free_block_address. is_none ( ) {
46
+ return Err ( Error :: NoSpace ) ;
47
+ }
48
+ let free_block_address = free_block_address. unwrap ( ) ;
59
49
60
- // we can now be certain that all blocks that we want to write into are allocated
50
+ let inode = file. inode_mut ( ) ;
51
+ let free_slot = inode. direct_ptrs ( )
52
+ . enumerate ( )
53
+ . find ( |( _, ptr) | ptr. is_none ( ) )
54
+ . map ( |( i, _) | i) ;
55
+ if let Some ( free_slot) = free_slot {
56
+ inode. set_direct_ptr ( free_slot, Some ( free_block_address) ) ;
57
+ self . write_inode ( file. inode_address ( ) , file) ?;
58
+ } else {
59
+ todo ! ( "allocate indirect block" ) ;
60
+ }
61
61
62
- let mut chunks = data . chunks_exact ( block_size as usize ) ;
63
- for ( block , data ) in ( start_block..=end_block ) . zip ( & mut chunks ) {
64
- let block_address = self . resolve_block_index ( & file , block ) ? . expect ( "we should have just allocated this block, it should be present" ) ;
65
- self . write_block ( block_address, data ) ?;
62
+ free_block_address
63
+ } ;
64
+
65
+ self . write_block ( block_address, chunk ) ?;
66
66
}
67
67
debug_assert_eq ! ( chunks. remainder( ) . len( ) , 0 , "data to write was not block aligned" ) ;
68
68
0 commit comments