Use AsRef<Path> instead of AsRef<OsStr> in yanix functions (#1950)

* Use AsRef<Path> instead of AsRef<OsStr> in yanix functions.

`AsRef<Path>` makes these more consistent with `std` interfaces, making
them easier to use outside of wasi-common.

Also, refactor the conversion to `CString` into a helper function.

* Reduce clutter from fully-qualifying names.

* rustfmt
This commit is contained in:
Dan Gohman
2020-07-20 10:02:45 -07:00
committed by GitHub
parent 784e2f1480
commit 4c15a4daf2
6 changed files with 50 additions and 41 deletions

View File

@@ -1,7 +1,7 @@
//! This module consists of helper types and functions for dealing
//! with setting the file times specific to BSD-style *nixes.
use crate::filetime::FileTime;
use crate::from_success_code;
use crate::{cstr, from_success_code};
use std::ffi::CStr;
use std::fs::File;
use std::io::Result;
@@ -21,7 +21,6 @@ pub fn utimensat(
symlink_nofollow: bool,
) -> Result<()> {
use crate::filetime::to_timespec;
use std::ffi::CString;
use std::os::unix::prelude::*;
// Attempt to use the `utimensat` syscall, but if it's not supported by the
@@ -33,10 +32,10 @@ pub fn utimensat(
0
};
let p = CString::new(path.as_bytes())?;
let path = cstr(path)?;
let times = [to_timespec(&atime)?, to_timespec(&mtime)?];
return from_success_code(unsafe {
func(dirfd.as_raw_fd(), p.as_ptr(), times.as_ptr(), flags)
func(dirfd.as_raw_fd(), path.as_ptr(), times.as_ptr(), flags)
});
}

View File

@@ -1,7 +1,7 @@
//! This module consists of helper types and functions for dealing
//! with setting the file times specific to Emscripten.
use crate::filetime::FileTime;
use crate::from_success_code;
use crate::{cstr, from_success_code};
use std::fs::File;
use std::io::Result;
@@ -15,7 +15,6 @@ pub fn utimensat(
symlink_nofollow: bool,
) -> Result<()> {
use crate::filetime::to_timespec;
use std::ffi::CString;
use std::os::unix::prelude::*;
let flags = if symlink_nofollow {
@@ -23,9 +22,9 @@ pub fn utimensat(
} else {
0
};
let p = CString::new(path.as_bytes())?;
let path = cstr(path)?;
let times = [to_timespec(&atime)?, to_timespec(&mtime)?];
from_success_code(unsafe {
libc::utimensat(dirfd.as_raw_fd(), p.as_ptr(), times.as_ptr(), flags)
libc::utimensat(dirfd.as_raw_fd(), path.as_ptr(), times.as_ptr(), flags)
})
}

View File

@@ -1,7 +1,7 @@
//! This module consists of helper types and functions for dealing
//! with setting the file times specific to Linux.
use crate::filetime::FileTime;
use crate::from_success_code;
use crate::{cstr, from_success_code};
use std::fs::File;
use std::io::Result;
use std::sync::atomic::{AtomicBool, Ordering::Relaxed};
@@ -20,7 +20,6 @@ pub fn utimensat(
symlink_nofollow: bool,
) -> Result<()> {
use crate::filetime::to_timespec;
use std::ffi::CString;
use std::os::unix::prelude::*;
let flags = if symlink_nofollow {
@@ -33,13 +32,13 @@ pub fn utimensat(
// current kernel then fall back to an older syscall.
static INVALID: AtomicBool = AtomicBool::new(false);
if !INVALID.load(Relaxed) {
let p = CString::new(path.as_bytes())?;
let path = cstr(path)?;
let times = [to_timespec(&atime)?, to_timespec(&mtime)?];
let res = from_success_code(unsafe {
libc::syscall(
libc::SYS_utimensat,
dirfd.as_raw_fd(),
p.as_ptr(),
path.as_ptr(),
times.as_ptr(),
flags,
)

View File

@@ -1,6 +1,6 @@
use crate::filetime::FileTime;
use crate::filetime::FileTimeExt;
use crate::from_success_code;
use crate::{cstr, from_success_code};
use std::fs;
use std::io::Result;
@@ -15,16 +15,15 @@ pub fn utimesat(
mtime: FileTime,
symlink_nofollow: bool,
) -> Result<()> {
use std::ffi::CString;
use std::os::unix::prelude::*;
// emulate *at syscall by reading the path from a combination of
// (fd, path)
let p = CString::new(path.as_bytes())?;
let path = cstr(path)?;
let mut flags = libc::O_RDWR;
if symlink_nofollow {
flags |= libc::O_NOFOLLOW;
}
let fd = unsafe { libc::openat(dirfd.as_raw_fd(), p.as_ptr(), flags) };
let fd = unsafe { libc::openat(dirfd.as_raw_fd(), path.as_ptr(), flags) };
let f = unsafe { fs::File::from_raw_fd(fd) };
let (atime, mtime) = get_times(atime, mtime, || f.metadata().map_err(Into::into))?;
let times = [to_timeval(atime)?, to_timeval(mtime)?];