summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDan Gohman <dev@sunfishcode.online>2021-11-19 14:29:37 -0800
committerDan Gohman <dev@sunfishcode.online>2021-12-02 13:03:32 -0800
commitcdce42d5a3b4c903d850f4aba7664d2885180336 (patch)
treedbcfef2de3a1a5501bd96a19fef98b6567afffe4
parenta3bd77d99b9812befedc741bb2749b3e686ddcb1 (diff)
downloadrust-cdce42d5a3b4c903d850f4aba7664d2885180336.tar.gz
Port several filesystem calls to rustix.
-rw-r--r--library/std/src/sys/unix/fs.rs51
1 files changed, 22 insertions, 29 deletions
diff --git a/library/std/src/sys/unix/fs.rs b/library/std/src/sys/unix/fs.rs
index c127859aa1f..c0be879d2e8 100644
--- a/library/std/src/sys/unix/fs.rs
+++ b/library/std/src/sys/unix/fs.rs
@@ -12,6 +12,7 @@ use crate::sys::fd::FileDesc;
use crate::sys::time::SystemTime;
use crate::sys::{cvt, cvt_r};
use crate::sys_common::{AsInner, AsInnerMut, FromInner, IntoInner};
+use rustix::fs::AtFlags;
#[cfg(any(
all(target_os = "linux", target_env = "gnu"),
@@ -46,7 +47,7 @@ use libc::fstatat64;
use libc::readdir_r as readdir64_r;
#[cfg(target_os = "android")]
use libc::{
- dirent as dirent64, fstat as fstat64, fstatat as fstatat64, ftruncate64,
+ dirent as dirent64, fstat as fstat64, fstatat as fstatat64,
lstat as lstat64, off64_t, open as open64, stat as stat64,
};
#[cfg(not(any(
@@ -56,13 +57,11 @@ use libc::{
target_os = "android"
)))]
use libc::{
- dirent as dirent64, fstat as fstat64, ftruncate as ftruncate64,
- lstat as lstat64, off_t as off64_t, open as open64, stat as stat64,
+ dirent as dirent64, fstat as fstat64, lstat as lstat64, off_t as off64_t, open as open64,
+ stat as stat64,
};
#[cfg(any(target_os = "linux", target_os = "emscripten", target_os = "l4re"))]
-use libc::{
- dirent64, fstat64, ftruncate64, lstat64, off64_t, open64, readdir64_r, stat64,
-};
+use libc::{dirent64, fstat64, lstat64, off64_t, open64, readdir64_r, stat64};
pub use crate::sys_common::fs::{remove_dir_all, try_exists};
@@ -789,26 +788,26 @@ impl File {
}
pub fn fsync(&self) -> io::Result<()> {
- cvt_r(|| unsafe { os_fsync(self.as_raw_fd()) })?;
+ rustix::io::with_retrying(|| os_fsync(self))?;
return Ok(());
#[cfg(any(target_os = "macos", target_os = "ios"))]
- unsafe fn os_fsync(fd: c_int) -> c_int {
- libc::fcntl(fd, libc::F_FULLFSYNC)
+ fn os_fsync(file: &File) -> rustix::io::Result<()> {
+ rustix::fs::fcntl_fullfsync(file)
}
#[cfg(not(any(target_os = "macos", target_os = "ios")))]
- unsafe fn os_fsync(fd: c_int) -> c_int {
- libc::fsync(fd)
+ fn os_fsync(file: &File) -> rustix::io::Result<()> {
+ rustix::fs::fsync(file)
}
}
pub fn datasync(&self) -> io::Result<()> {
- cvt_r(|| unsafe { os_datasync(self.as_raw_fd()) })?;
+ rustix::io::with_retrying(|| os_datasync(self))?;
return Ok(());
#[cfg(any(target_os = "macos", target_os = "ios"))]
- unsafe fn os_datasync(fd: c_int) -> c_int {
- libc::fcntl(fd, libc::F_FULLFSYNC)
+ fn os_datasync(file: &File) -> rustix::io::Result<()> {
+ rustix::fs::fcntl_fullfsync(file)
}
#[cfg(any(
target_os = "freebsd",
@@ -817,8 +816,8 @@ impl File {
target_os = "netbsd",
target_os = "openbsd"
))]
- unsafe fn os_datasync(fd: c_int) -> c_int {
- libc::fdatasync(fd)
+ fn os_datasync(file: &File) -> rustix::io::Result<()> {
+ rustix::fs::fdatasync(file)
}
#[cfg(not(any(
target_os = "android",
@@ -829,16 +828,14 @@ impl File {
target_os = "netbsd",
target_os = "openbsd"
)))]
- unsafe fn os_datasync(fd: c_int) -> c_int {
- libc::fsync(fd)
+ fn os_datasync(file: &File) -> rustix::io::Result<()> {
+ rustix::fs::fsync(file)
}
}
pub fn truncate(&self, size: u64) -> io::Result<()> {
- use crate::convert::TryInto;
- let size: off64_t =
- size.try_into().map_err(|e| io::Error::new(io::ErrorKind::InvalidInput, e))?;
- cvt_r(|| unsafe { ftruncate64(self.as_raw_fd(), size) }).map(drop)
+ rustix::io::with_retrying(|| rustix::fs::ftruncate(self, size))?;
+ Ok(())
}
pub fn read(&self, buf: &mut [u8]) -> io::Result<usize> {
@@ -1069,15 +1066,12 @@ pub fn readdir(p: &Path) -> io::Result<ReadDir> {
}
pub fn unlink(p: &Path) -> io::Result<()> {
- let p = cstr(p)?;
- cvt(unsafe { libc::unlink(p.as_ptr()) })?;
+ rustix::fs::unlinkat(&rustix::fs::cwd(), p, AtFlags::empty())?;
Ok(())
}
pub fn rename(old: &Path, new: &Path) -> io::Result<()> {
- let old = cstr(old)?;
- let new = cstr(new)?;
- cvt(unsafe { libc::rename(old.as_ptr(), new.as_ptr()) })?;
+ rustix::fs::renameat(&rustix::fs::cwd(), old, &rustix::fs::cwd(), new)?;
Ok(())
}
@@ -1088,8 +1082,7 @@ pub fn set_perm(p: &Path, perm: FilePermissions) -> io::Result<()> {
}
pub fn rmdir(p: &Path) -> io::Result<()> {
- let p = cstr(p)?;
- cvt(unsafe { libc::rmdir(p.as_ptr()) })?;
+ rustix::fs::unlinkat(&rustix::fs::cwd(), p, AtFlags::REMOVEDIR)?;
Ok(())
}