-
Notifications
You must be signed in to change notification settings - Fork 824
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4698 from wasmerio/fix/fstat_size_with_seek
Fix calculation of stat.st_size in the presence of combined seeks and writes
- Loading branch information
Showing
3 changed files
with
59 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
use std::fs::{metadata, OpenOptions}; | ||
use std::io::{Seek, SeekFrom, Write}; | ||
|
||
fn main() { | ||
let mut file = OpenOptions::new() | ||
.write(true) | ||
.read(true) | ||
.create(true) | ||
.open("file") | ||
.unwrap(); | ||
|
||
write!(file, "hell").unwrap(); | ||
|
||
// We wrote 4 bytes | ||
let md = metadata("file").unwrap(); | ||
assert_eq!(md.len(), 4); | ||
|
||
assert_eq!(file.seek(SeekFrom::Start(0)).unwrap(), 0); | ||
|
||
write!(file, "eh").unwrap(); | ||
|
||
// We overwrote the first 2 bytes, should still have 4 bytes | ||
let md = metadata("file").unwrap(); | ||
assert_eq!(md.len(), 4); | ||
|
||
assert_eq!(file.seek(SeekFrom::Start(0)).unwrap(), 0); | ||
|
||
write!(file, "hello").unwrap(); | ||
|
||
// Now we wrote past the end, should have 5 bytes | ||
let md = metadata("file").unwrap(); | ||
assert_eq!(md.len(), 5); | ||
|
||
write!(file, " world!").unwrap(); | ||
|
||
// We wrote past the end entirely, should have 5 + 7 = 12 bytes | ||
let md = metadata("file").unwrap(); | ||
assert_eq!(md.len(), 12); | ||
} |