Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion library/std/src/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -382,7 +382,10 @@ pub fn read_to_string<P: AsRef<Path>>(path: P) -> io::Result<String> {
#[stable(feature = "fs_read_write_bytes", since = "1.26.0")]
pub fn write<P: AsRef<Path>, C: AsRef<[u8]>>(path: P, contents: C) -> io::Result<()> {
fn inner(path: &Path, contents: &[u8]) -> io::Result<()> {
File::create(path)?.write_all(contents)
let mut file = File::create(path)?;
file.write_all(contents)?;
file.close()?;
Ok(())
}
inner(path.as_ref(), contents.as_ref())
}
Expand Down Expand Up @@ -709,6 +712,11 @@ impl File {
OpenOptions::new()
}

/// Drops the file, returning any errors encountered.
pub(crate) fn close(self) -> io::Result<()> {
self.inner.close()
}

/// Attempts to sync all OS-internal file content and metadata to disk.
///
/// This function will attempt to ensure that all in-memory data reaches the
Expand Down
24 changes: 17 additions & 7 deletions library/std/src/os/fd/owned.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,6 @@ use super::raw::{AsRawFd, FromRawFd, IntoRawFd, RawFd};
use crate::fs;
use crate::marker::PhantomData;
use crate::mem::ManuallyDrop;
#[cfg(not(any(
target_arch = "wasm32",
target_env = "sgx",
target_os = "hermit",
target_os = "trusty",
target_os = "motor"
)))]
use crate::sys::cvt;
#[cfg(not(target_os = "trusty"))]
use crate::sys_common::{AsInner, FromInner, IntoInner};
Expand Down Expand Up @@ -94,6 +87,23 @@ impl OwnedFd {
pub fn try_clone(&self) -> crate::io::Result<Self> {
self.as_fd().try_clone_to_owned()
}

/// Drops the file descriptor, returning any errors encountered.
pub(crate) fn close(self) -> io::Result<()> {
let this = ManuallyDrop::new(self);
let result = unsafe {
#[cfg(not(target_os = "hermit"))]
{
#[cfg(unix)]
crate::sys::fs::debug_assert_fd_is_open(this.fd.as_inner());

libc::close(this.fd.as_inner())
}
#[cfg(target_os = "hermit")]
hermit_abi::close(this.fd.as_inner())
};
cvt(result).map(|_| ())
}
}

impl BorrowedFd<'_> {
Expand Down
6 changes: 6 additions & 0 deletions library/std/src/os/windows/io/handle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,12 @@ impl Drop for HandleOrNull {
}

impl OwnedHandle {
/// Drops the handle, returning any errors encountered.
pub(crate) fn close(self) -> io::Result<()> {
let this = ManuallyDrop::new(self);
cvt(unsafe { sys::c::CloseHandle(this.handle) }).map(|_| ())
}

/// Creates a new `OwnedHandle` instance that shares the same underlying
/// object as the existing `OwnedHandle` instance.
#[stable(feature = "io_safety", since = "1.63.0")]
Expand Down
4 changes: 4 additions & 0 deletions library/std/src/sys/fd/unix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,10 @@ const fn max_iov() -> usize {
}

impl FileDesc {
pub fn close(self) -> io::Result<()> {
self.0.close()
}

#[inline]
pub fn try_clone(&self) -> io::Result<Self> {
self.duplicate()
Expand Down
1 change: 1 addition & 0 deletions library/std/src/sys/fs/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ pub fn copy(from: &Path, to: &Path) -> io::Result<u64> {

let ret = io::copy(&mut reader, &mut writer)?;
writer.set_permissions(perm)?;
writer.close()?;
Ok(ret)
}

Expand Down
4 changes: 4 additions & 0 deletions library/std/src/sys/fs/hermit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,10 @@ impl File {
Ok(File(unsafe { FileDesc::from_raw_fd(fd as i32) }))
}

pub fn close(self) -> io::Result<()> {
self.0.close()
}

pub fn file_attr(&self) -> io::Result<FileAttr> {
let mut stat_val: stat_struct = unsafe { mem::zeroed() };
self.0.fstat(&mut stat_val)?;
Expand Down
4 changes: 4 additions & 0 deletions library/std/src/sys/fs/motor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,10 @@ impl File {
.map_err(map_motor_error)
}

pub fn close(self) -> io::Result<()> {
self.0.close()
}

pub fn file_attr(&self) -> io::Result<FileAttr> {
moto_rt::fs::get_file_attr(self.as_raw_fd())
.map(|inner| -> FileAttr { FileAttr { inner } })
Expand Down
14 changes: 12 additions & 2 deletions library/std/src/sys/fs/solid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use crate::ffi::{CStr, CString, OsStr, OsString};
use crate::fmt;
use crate::fs::TryLockError;
use crate::io::{self, BorrowedCursor, IoSlice, IoSliceMut, SeekFrom};
use crate::mem::MaybeUninit;
use crate::mem::{ManuallyDrop, MaybeUninit};
use crate::os::raw::{c_int, c_short};
use crate::os::solid::ffi::OsStrExt;
use crate::path::{Path, PathBuf};
Expand Down Expand Up @@ -333,6 +333,14 @@ impl File {
}
}

pub fn close(self) -> io::Result<()> {
let this = ManuallyDrop::new(self);
match error::SolidError::err_if_negative(unsafe { abi::SOLID_FS_Close(this.fd.raw()) }) {
Ok(_) => Ok(()),
Err(e) => Err(e.as_io_error()),
}
}

pub fn file_attr(&self) -> io::Result<FileAttr> {
unsupported()
}
Expand Down Expand Up @@ -620,5 +628,7 @@ pub fn copy(from: &Path, to: &Path) -> io::Result<u64> {
let mut reader = File::open(from)?;
let mut writer = File::create(to)?;

io::copy(&mut reader, &mut writer)
let ret = io::copy(&mut reader, &mut writer)?;
writer.close()?;
Ok(ret)
}
4 changes: 4 additions & 0 deletions library/std/src/sys/fs/uefi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,10 @@ impl File {
unsupported()
}

pub fn close(self) -> io::Result<()> {
self.0
}

pub fn file_attr(&self) -> io::Result<FileAttr> {
self.0
}
Expand Down
13 changes: 9 additions & 4 deletions library/std/src/sys/fs/unix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1231,6 +1231,10 @@ impl File {
Ok(File(unsafe { FileDesc::from_raw_fd(fd) }))
}

pub fn close(self) -> io::Result<()> {
self.0.close()
}

pub fn file_attr(&self) -> io::Result<FileAttr> {
let fd = self.as_raw_fd();

Expand Down Expand Up @@ -2324,11 +2328,12 @@ pub(in crate::sys) use cfm::CachedFileMetadata;
pub fn copy(from: &Path, to: &Path) -> io::Result<u64> {
let (reader, reader_metadata) = open_from(from)?;
let (writer, writer_metadata) = open_to_and_set_permissions(to, &reader_metadata)?;
let mut reader = cfm::CachedFileMetadata(reader, reader_metadata);
let mut writer = cfm::CachedFileMetadata(writer, writer_metadata);

io::copy(
&mut cfm::CachedFileMetadata(reader, reader_metadata),
&mut cfm::CachedFileMetadata(writer, writer_metadata),
)
let ret = io::copy(&mut reader, &mut writer)?;
writer.0.close()?;
Ok(ret)
}

#[cfg(target_vendor = "apple")]
Expand Down
4 changes: 4 additions & 0 deletions library/std/src/sys/fs/unsupported.rs
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,10 @@ impl File {
unsupported()
}

pub fn close(self) -> io::Result<()> {
self.0
}

pub fn file_attr(&self) -> io::Result<FileAttr> {
self.0
}
Expand Down
10 changes: 9 additions & 1 deletion library/std/src/sys/fs/vexos.rs
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,12 @@ impl File {
})
}

pub fn close(self) -> io::Result<()> {
// vexFileClose can't fail. Just implicitly drop `self`.
let _ = self;
Ok(())
}

pub fn file_attr(&self) -> io::Result<FileAttr> {
// `vexFileSize` returns -1 upon error, so u64::try_from will fail on error.
if let Ok(size) = u64::try_from(unsafe {
Expand Down Expand Up @@ -537,7 +543,9 @@ pub fn copy(from: &Path, to: &Path) -> io::Result<u64> {
let mut reader = File::open(from)?;
let mut writer = File::create(to)?;

io::copy(&mut reader, &mut writer)
let ret = io::copy(&mut reader, &mut writer)?;
writer.close()?;

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Even though this can't fail, I copied it for symmetry with the other implementations.

Ok(ret)
}

fn map_fresult(fresult: vex_sdk::FRESULT) -> io::Result<()> {
Expand Down
4 changes: 4 additions & 0 deletions library/std/src/sys/fs/windows.rs
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,10 @@ impl File {
}
}

pub fn close(self) -> io::Result<()> {
self.handle.close()
}

pub fn fsync(&self) -> io::Result<()> {
cvt(unsafe { c::FlushFileBuffers(self.handle.as_raw_handle()) })?;
Ok(())
Expand Down
4 changes: 4 additions & 0 deletions library/std/src/sys/pal/windows/handle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,10 @@ impl FromRawHandle for Handle {
}

impl Handle {
pub fn close(self) -> io::Result<()> {
self.0.close()
}

pub fn read(&self, buf: &mut [u8]) -> io::Result<usize> {
let res = unsafe { self.synchronous_read(buf.as_mut_ptr().cast(), buf.len(), None) };

Expand Down
Loading