Skip to content

Commit 7a7af7b

Browse files
Flush dir_entry on file close.
Requires mutable access to the volume in order to do the write.
1 parent f46ef4d commit 7a7af7b

File tree

7 files changed

+29
-30
lines changed

7 files changed

+29
-30
lines changed

examples/create_test.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ fn main() {
167167
let num_written1 = volume_mgr.write(&mut volume, &mut f, &buffer1[..]).unwrap();
168168
let num_written = volume_mgr.write(&mut volume, &mut f, &buffer[..]).unwrap();
169169
println!("Number of bytes written: {}\n", num_written + num_written1);
170-
volume_mgr.close_file(&volume, f).unwrap();
170+
volume_mgr.close_file(&mut volume, f).unwrap();
171171

172172
let mut f = volume_mgr
173173
.open_file_in_dir(
@@ -198,7 +198,7 @@ fn main() {
198198
}
199199
}
200200
println!("EOF");
201-
volume_mgr.close_file(&volume, f).unwrap();
201+
volume_mgr.close_file(&mut volume, f).unwrap();
202202
}
203203
}
204204
}

examples/delete_test.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ fn main() {
154154
Err(error) => println!("\tCannot delete file: {:?}", error),
155155
}
156156
println!("\tClosing {}...", FILE_TO_DELETE);
157-
volume_mgr.close_file(&volume, f).unwrap();
157+
volume_mgr.close_file(&mut volume, f).unwrap();
158158

159159
match volume_mgr.delete_file_in_dir(&volume, &root_dir, FILE_TO_DELETE) {
160160
Ok(()) => println!("\tDeleted {}.", FILE_TO_DELETE),

examples/readme_test.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ fn main() -> Result<(), Error> {
109109
print!("{}", *b as char);
110110
}
111111
}
112-
volume_mgr.close_file(&volume0, my_file)?;
112+
volume_mgr.close_file(&mut volume0, my_file)?;
113113
volume_mgr.close_dir(&volume0, root_dir);
114114
Ok(())
115115
}

examples/test_mount.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ fn main() {
158158
assert!(volume_mgr
159159
.open_file_in_dir(&mut volume, &root_dir, FILE_TO_PRINT, Mode::ReadOnly)
160160
.is_err());
161-
volume_mgr.close_file(&volume, f).unwrap();
161+
volume_mgr.close_file(&mut volume, f).unwrap();
162162

163163
let test_dir = volume_mgr.open_dir(&volume, &root_dir, "TEST").unwrap();
164164
// Check we can't open it twice
@@ -187,7 +187,7 @@ fn main() {
187187
}
188188
}
189189
println!("Checksum over {} bytes: {}", f.length(), csum);
190-
volume_mgr.close_file(&volume, f).unwrap();
190+
volume_mgr.close_file(&mut volume, f).unwrap();
191191

192192
assert!(volume_mgr.open_root_dir(&volume).is_err());
193193
volume_mgr.close_dir(&volume, root_dir);

examples/write_test.rs

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ fn main() {
153153
}
154154
println!("EOF\n");
155155
let mut file_size = f.length() as usize;
156-
volume_mgr.close_file(&volume, f).unwrap();
156+
volume_mgr.close_file(&mut volume, f).unwrap();
157157

158158
let mut f = volume_mgr
159159
.open_file_in_dir(&mut volume, &root_dir, FILE_TO_WRITE, Mode::ReadWriteAppend)
@@ -170,10 +170,7 @@ fn main() {
170170
for b in &buffer[..] {
171171
csum += u32::from(*b);
172172
}
173-
println!(
174-
"Number of bytes appendedß: {}\n",
175-
num_written + num_written1
176-
);
173+
println!("Number of bytes appended: {}\n", num_written + num_written1);
177174
file_size += num_written;
178175
file_size += num_written1;
179176

@@ -190,7 +187,7 @@ fn main() {
190187
}
191188
}
192189
println!("EOF");
193-
volume_mgr.close_file(&volume, f).unwrap();
190+
volume_mgr.close_file(&mut volume, f).unwrap();
194191

195192
println!("\tFinding {}...", FILE_TO_WRITE);
196193
let dir_ent = volume_mgr
@@ -201,7 +198,12 @@ fn main() {
201198
let mut f = volume_mgr
202199
.open_file_in_dir(&mut volume, &root_dir, FILE_TO_WRITE, Mode::ReadWriteAppend)
203200
.unwrap();
204-
println!("\nReading from file {}\n", FILE_TO_WRITE);
201+
println!(
202+
"\nReading from file {}, len {}\n",
203+
FILE_TO_WRITE,
204+
f.length()
205+
);
206+
f.seek_from_start(0).unwrap();
205207
println!("FILE STARTS:");
206208
let mut csum2 = 0;
207209
while !f.eof() {
@@ -217,7 +219,7 @@ fn main() {
217219
}
218220
println!("EOF\n");
219221
assert_eq!(f.length() as usize, file_size);
220-
volume_mgr.close_file(&volume, f).unwrap();
222+
volume_mgr.close_file(&mut volume, f).unwrap();
221223

222224
assert_eq!(csum, csum2);
223225

@@ -254,7 +256,7 @@ fn main() {
254256
}
255257
}
256258
println!("EOF");
257-
volume_mgr.close_file(&volume, f).unwrap();
259+
volume_mgr.close_file(&mut volume, f).unwrap();
258260
}
259261
}
260262
}

src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@
6565
//! print!("{}", *b as char);
6666
//! }
6767
//! }
68-
//! volume_mgr.close_file(&volume0, my_file)?;
68+
//! volume_mgr.close_file(&mut volume0, my_file)?;
6969
//! volume_mgr.close_dir(&volume0, root_dir);
7070
//! # Ok(())
7171
//! # }

src/volume_mgr.rs

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -658,25 +658,22 @@ where
658658
file.seek_from_current(to_copy).unwrap();
659659
file.entry.attributes.set_archive(true);
660660
file.entry.mtime = self.timesource.get_timestamp();
661-
debug!("Updating FAT info sector");
662-
match &mut volume.volume_type {
663-
VolumeType::Fat(fat) => {
664-
fat.update_info_sector(self)?;
665-
debug!("Updating dir entry");
666-
self.write_entry_to_disk(fat.get_fat_type(), &file.entry)?;
667-
}
668-
}
661+
// Entry update deferred to file close, for performance.
669662
}
670663
Ok(written)
671664
}
672665

673666
/// Close a file with the given full path.
674-
pub fn close_file(&mut self, volume: &Volume, file: File) -> Result<(), Error<D::Error>> {
675-
// We don't strictly speaking need the volume in order to close a
676-
// directory, as we don't flush anything to disk at this point. However,
677-
// we take it in case we change this in the future and closing a file
678-
// does then cause some disk write to occur.
679-
let _ = volume;
667+
pub fn close_file(&mut self, volume: &mut Volume, file: File) -> Result<(), Error<D::Error>> {
668+
match volume.volume_type {
669+
VolumeType::Fat(ref mut fat) => {
670+
debug!("Updating FAT info sector");
671+
fat.update_info_sector(self)?;
672+
debug!("Updating dir entry {:?}", file.entry);
673+
let fat_type = fat.get_fat_type();
674+
self.write_entry_to_disk(fat_type, &file.entry)?;
675+
}
676+
};
680677

681678
// Unwrap, because we should never be in a situation where we're attempting to close a file
682679
// with an ID which doesn't exist in our open files list.

0 commit comments

Comments
 (0)