Skip to content

Commit 2f3bbe8

Browse files
authored
Simplify some filesystem extensions on Windows (#43)
This commit it borne out of CI failures on bytecodealliance/wasmtime#7638. Investigating this failure has revealed a number of aspects here which I've attempted to address in this PR. The notable changes here are: * The current code in this crate was handling the case where `FileExt::seek_write` on Windows was leaving intermediate bytes as undefined when a write happened beyond the end of a file. I believe that this is due to an error in the documentation of the Rust standard library which I've submitted rust-lang/rust#120452 to fix. * Removing handling of "always write zeros" handles the primary failure of the PR bytecodealliance/wasmtime#7638 which is that `write_vectored_at` was always returning 0 on Windows for writes past the end of the file. This is because Windows doesn't have a vectored file write so the vector chosen was the first nonempty vector which was the one containing zeros to extend the file. That meant that the method always returned zero. * Previously the methods here used file locking which appeared to handle the case where the file was calculated and then the write happened. Given that this no longer happens I've removed the locking here. * The `write_all_at` method had a loop around `reopen_write` handling the `Interrupted` error but no other methods did, so I opted to remove the loop and leave that to the internals of `reopen_write` if necessary. * Other methods related to this are all simplified to directly use `seek_write` and avoid handling the case where writes past the end need to write zeros (as zeros are guaranteed by Windows). Overall my hope is to use this to unblock bytecodealliance/wasmtime#7638 to get more platform-agnostic behavior for writing beyond the end of a file.
1 parent 0c57561 commit 2f3bbe8

File tree

2 files changed

+20
-126
lines changed

2 files changed

+20
-126
lines changed

src/fs/file_io_ext.rs

+19-126
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ use rustix::io::{preadv, pwritev};
2525
use std::io::{self, IoSlice, IoSliceMut, Seek, SeekFrom};
2626
use std::slice;
2727
#[cfg(windows)]
28-
use {cap_fs_ext::Reopen, fd_lock::RwLock, std::fs, std::os::windows::fs::FileExt};
28+
use {cap_fs_ext::Reopen, std::fs, std::os::windows::fs::FileExt};
2929
#[cfg(not(windows))]
3030
use {rustix::fs::tell, rustix::fs::FileExt};
3131

@@ -703,144 +703,37 @@ impl FileIoExt for std::fs::File {
703703
fn write_at(&self, buf: &[u8], offset: u64) -> io::Result<usize> {
704704
// Windows' `seek_write` modifies the current position in the file, so
705705
// re-open the file to leave the original open file unmodified.
706-
//
707-
// We take a lock so that we can test for writing past the end of the
708-
// file and implement writing zeros if the offset is past the end.
709-
//
710-
// Windows documentation [says]:
711-
//
712-
// > A write operation increases the size of the file to the file
713-
// > pointer position plus the size of the buffer written, which results
714-
// > in the intervening bytes uninitialized.
715-
//
716-
// [says]: https://learn.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-setfilepointer#remarks
717-
//
718-
// However, our desired behavior is to fill the intervening bytes with
719-
// zeros, so we do it ourselves.
720-
let reopened = reopen_write(self)?;
721-
let mut reopened = RwLock::new(reopened);
722-
let reopened = reopened.write()?;
723-
let mut seek_offset = offset;
724-
let mut write_buf = buf;
725-
let mut prepend_zeros;
726-
let reopened_size = reopened.metadata()?.len();
727-
let num_zeros = offset.saturating_sub(reopened_size);
728-
let num_zeros: usize = num_zeros
729-
.try_into()
730-
.map_err(|_| io::Error::new(io::ErrorKind::OutOfMemory, "write_all_vectored_at"))?;
731-
if num_zeros > 0 {
732-
prepend_zeros = vec![0_u8; num_zeros];
733-
prepend_zeros.extend_from_slice(buf);
734-
seek_offset = reopened_size;
735-
write_buf = &prepend_zeros;
736-
}
737-
let num_written = reopened
738-
.seek_write(write_buf, seek_offset)?
739-
.saturating_sub(num_zeros);
740-
Ok(num_written)
706+
reopen_write(self)?.seek_write(buf, offset)
741707
}
742708

743709
#[inline]
744-
fn write_all_at(&self, buf: &[u8], offset: u64) -> io::Result<()> {
745-
// Similar to `read_exact_at`, re-open the file so that we can do a seek and
746-
// leave the original file unmodified.
747-
//
748-
// Similar to `write_at`, we take a lock to do this.
749-
let reopened = loop {
750-
match reopen_write(self) {
751-
Ok(file) => break file,
752-
Err(err) if err.kind() == io::ErrorKind::Interrupted => continue,
753-
Err(err) => return Err(err),
754-
}
755-
};
756-
let mut reopened = RwLock::new(reopened);
757-
let reopened = reopened.write()?;
758-
let mut seek_offset = offset;
759-
let mut write_buf = buf;
760-
let mut prepend_zeros;
761-
let reopened_size = reopened.metadata()?.len();
762-
let num_zeros = offset.saturating_sub(reopened_size);
763-
let num_zeros: usize = num_zeros
764-
.try_into()
765-
.map_err(|_| io::Error::new(io::ErrorKind::OutOfMemory, "write_all_vectored_at"))?;
766-
if num_zeros > 0 {
767-
prepend_zeros = vec![0_u8; num_zeros];
768-
prepend_zeros.extend_from_slice(buf);
769-
seek_offset = reopened_size;
770-
write_buf = &prepend_zeros;
771-
}
772-
loop {
773-
match reopened.seek(SeekFrom::Start(seek_offset)) {
774-
Ok(_) => break,
775-
Err(err) if err.kind() == io::ErrorKind::Interrupted => continue,
776-
Err(err) => return Err(err),
777-
}
710+
fn write_all_at(&self, mut buf: &[u8], mut offset: u64) -> io::Result<()> {
711+
// Similar to `read_exact_at`, re-open the file so that we can do a seek
712+
// and leave the original file unmodified.
713+
let reopened = reopen_write(self)?;
714+
while buf.len() > 0 {
715+
let n = reopened.seek_write(buf, offset)?;
716+
offset += u64::try_from(n).unwrap();
717+
buf = &buf[n..];
778718
}
779-
reopened.write_all(write_buf)?;
780719
Ok(())
781720
}
782721

783722
#[inline]
784723
fn write_vectored_at(&self, bufs: &[IoSlice], offset: u64) -> io::Result<usize> {
785-
// Similar to `read_vectored_at`, re-open the file to avoid adjusting
786-
// the current position of the already-open file.
787-
//
788-
// Similar to `write_at`, we take a lock to do this.
789-
let reopened = reopen_write(self)?;
790-
let mut reopened = RwLock::new(reopened);
791-
let reopened = reopened.write()?;
792-
let mut seek_offset = offset;
793-
let mut write_bufs = bufs;
794-
let zeros;
795-
let mut prepend_zeros;
796-
let reopened_size = reopened.metadata()?.len();
797-
let num_zeros = offset.saturating_sub(reopened_size);
798-
let num_zeros: usize = num_zeros
799-
.try_into()
800-
.map_err(|_| io::Error::new(io::ErrorKind::OutOfMemory, "write_vectored_at"))?;
801-
if num_zeros > 0 {
802-
zeros = vec![0_u8; num_zeros];
803-
prepend_zeros = vec![IoSlice::new(&zeros)];
804-
prepend_zeros.extend_from_slice(bufs);
805-
seek_offset = reopened_size;
806-
write_bufs = &prepend_zeros;
724+
// Windows doesn't have a vectored write for files, so pick the first
725+
// non-empty slice and write that.
726+
match bufs.iter().find(|p| p.len() > 0) {
727+
Some(buf) => self.write_at(buf, offset),
728+
None => Ok(0),
807729
}
808-
reopened.seek(SeekFrom::Start(seek_offset))?;
809-
let num_written = reopened
810-
.write_vectored(write_bufs)?
811-
.saturating_sub(num_zeros);
812-
Ok(num_written)
813730
}
814731

815732
#[inline]
816-
fn write_all_vectored_at(&self, bufs: &mut [IoSlice], offset: u64) -> io::Result<()> {
817-
// Similar to `read_vectored_at`, re-open the file to avoid adjusting
818-
// the current position of the already-open file.
819-
//
820-
// Similar to `write_at`, we take a lock to do this.
821-
let reopened = loop {
822-
match reopen_write(self) {
823-
Ok(file) => break file,
824-
Err(err) if err.kind() == io::ErrorKind::Interrupted => continue,
825-
Err(err) => return Err(err),
826-
}
827-
};
828-
let mut reopened = RwLock::new(reopened);
829-
let reopened = reopened.write()?;
830-
let reopened_size = reopened.metadata()?.len();
831-
let num_zeros = offset.saturating_sub(reopened_size);
832-
let num_zeros: usize = num_zeros
833-
.try_into()
834-
.map_err(|_| io::Error::new(io::ErrorKind::OutOfMemory, "write_vectored_at"))?;
835-
if num_zeros > 0 {
836-
let zeros = vec![0_u8; num_zeros];
837-
let mut prepend_zeros = vec![IoSlice::new(&zeros)];
838-
prepend_zeros.extend_from_slice(bufs);
839-
reopened.seek(SeekFrom::Start(reopened_size))?;
840-
reopened.write_all_vectored(&mut prepend_zeros)?;
841-
} else {
842-
reopened.seek(SeekFrom::Start(offset))?;
843-
reopened.write_all_vectored(bufs)?;
733+
fn write_all_vectored_at(&self, bufs: &mut [IoSlice], mut offset: u64) -> io::Result<()> {
734+
for buf in bufs {
735+
self.write_all_at(buf, offset)?;
736+
offset += u64::try_from(buf.len()).unwrap();
844737
}
845738
Ok(())
846739
}

tests/vectored_at.rs

+1
Original file line numberDiff line numberDiff line change
@@ -412,6 +412,7 @@ fn write_vectored_after_end() {
412412
let buf1 = b"MNOPQRST".to_vec();
413413
let bufs = vec![IoSlice::new(&buf0), IoSlice::new(&buf1)];
414414
let nwritten = check!(file.write_vectored_at(&bufs, 32));
415+
assert!(nwritten > 0);
415416
assert_eq!(check!(file.stream_position()), 26);
416417
let mut back = String::new();
417418
check!(file.seek(std::io::SeekFrom::Start(0)));

0 commit comments

Comments
 (0)