Skip to content

Commit

Permalink
More fixes.
Browse files Browse the repository at this point in the history
Remove more uses of the std::os traits, and update the build.rs scripts
to fix Rust feature detection.
  • Loading branch information
sunfishcode committed Jan 10, 2024
1 parent 206624d commit 1c14105
Show file tree
Hide file tree
Showing 8 changed files with 173 additions and 53 deletions.
50 changes: 45 additions & 5 deletions cap-fs-ext/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,60 @@ fn use_feature(feature: &str) {

/// Test whether the rustc at `var("RUSTC")` supports the given feature.
fn has_feature(feature: &str) -> bool {
can_compile(&format!(
"#![allow(stable_features)]\n#![feature({})]",
feature
))
}

/// Test whether the rustc at `var("RUSTC")` can compile the given code.
fn can_compile<T: AsRef<str>>(test: T) -> bool {
use std::process::Stdio;

let out_dir = var("OUT_DIR").unwrap();
let rustc = var("RUSTC").unwrap();
let target = var("TARGET").unwrap();

// Use `RUSTC_WRAPPER` if it's set, unless it's set to an empty string,
// as documented [here].
// [here]: https://doc.rust-lang.org/cargo/reference/environment-variables.html#environment-variables-cargo-reads
let wrapper = var("RUSTC_WRAPPER")
.ok()
.and_then(|w| if w.is_empty() { None } else { Some(w) });

let mut child = std::process::Command::new(rustc)
.arg("--crate-type=rlib") // Don't require `main`.
let mut cmd = if let Some(wrapper) = wrapper {
let mut cmd = std::process::Command::new(wrapper);
// The wrapper's first argument is supposed to be the path to rustc.
cmd.arg(rustc);
cmd
} else {
std::process::Command::new(rustc)
};

cmd.arg("--crate-type=rlib") // Don't require `main`.
.arg("--emit=metadata") // Do as little as possible but still parse.
.arg("--target")
.arg(target)
.arg("--out-dir")
.arg(out_dir) // Put the output somewhere inconsequential.
.arg(out_dir); // Put the output somewhere inconsequential.

// If Cargo wants to set RUSTFLAGS, use that.
if let Ok(rustflags) = var("CARGO_ENCODED_RUSTFLAGS") {
if !rustflags.is_empty() {
for arg in rustflags.split('\x1f') {
cmd.arg(arg);
}
}
}

let mut child = cmd
.arg("-") // Read from stdin.
.stdin(std::process::Stdio::piped()) // Stdin is a pipe.
.stdin(Stdio::piped()) // Stdin is a pipe.
.stderr(Stdio::null()) // Errors from feature detection aren't interesting and can be confusing.
.spawn()
.unwrap();

writeln!(child.stdin.take().unwrap(), "#![feature({})]", feature).unwrap();
writeln!(child.stdin.take().unwrap(), "{}", test.as_ref()).unwrap();

child.wait().unwrap().success()
}
50 changes: 45 additions & 5 deletions cap-primitives/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,20 +25,60 @@ fn use_feature(feature: &str) {

/// Test whether the rustc at `var("RUSTC")` supports the given feature.
fn has_feature(feature: &str) -> bool {
can_compile(&format!(
"#![allow(stable_features)]\n#![feature({})]",
feature
))
}

/// Test whether the rustc at `var("RUSTC")` can compile the given code.
fn can_compile<T: AsRef<str>>(test: T) -> bool {
use std::process::Stdio;

let out_dir = var("OUT_DIR").unwrap();
let rustc = var("RUSTC").unwrap();
let target = var("TARGET").unwrap();

// Use `RUSTC_WRAPPER` if it's set, unless it's set to an empty string,
// as documented [here].
// [here]: https://doc.rust-lang.org/cargo/reference/environment-variables.html#environment-variables-cargo-reads
let wrapper = var("RUSTC_WRAPPER")
.ok()
.and_then(|w| if w.is_empty() { None } else { Some(w) });

let mut child = std::process::Command::new(rustc)
.arg("--crate-type=rlib") // Don't require `main`.
let mut cmd = if let Some(wrapper) = wrapper {
let mut cmd = std::process::Command::new(wrapper);
// The wrapper's first argument is supposed to be the path to rustc.
cmd.arg(rustc);
cmd
} else {
std::process::Command::new(rustc)
};

cmd.arg("--crate-type=rlib") // Don't require `main`.
.arg("--emit=metadata") // Do as little as possible but still parse.
.arg("--target")
.arg(target)
.arg("--out-dir")
.arg(out_dir) // Put the output somewhere inconsequential.
.arg(out_dir); // Put the output somewhere inconsequential.

// If Cargo wants to set RUSTFLAGS, use that.
if let Ok(rustflags) = var("CARGO_ENCODED_RUSTFLAGS") {
if !rustflags.is_empty() {
for arg in rustflags.split('\x1f') {
cmd.arg(arg);
}
}
}

let mut child = cmd
.arg("-") // Read from stdin.
.stdin(std::process::Stdio::piped()) // Stdin is a pipe.
.stdin(Stdio::piped()) // Stdin is a pipe.
.stderr(Stdio::null()) // Errors from feature detection aren't interesting and can be confusing.
.spawn()
.unwrap();

writeln!(child.stdin.take().unwrap(), "#![feature({})]", feature).unwrap();
writeln!(child.stdin.take().unwrap(), "{}", test.as_ref()).unwrap();

child.wait().unwrap().success()
}
54 changes: 30 additions & 24 deletions cap-primitives/src/fs/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -274,131 +274,134 @@ pub trait MetadataExt {
/// Returns the value of the `nFileSize{High,Low}` fields of this metadata.
fn file_size(&self) -> u64;
/// Returns the value of the `dwVolumeSerialNumber` field of this metadata.
#[cfg(windows_by_handle)]
fn volume_serial_number(&self) -> Option<u32>;
/// Returns the value of the `nNumberOfLinks` field of this metadata.
#[cfg(windows_by_handle)]
fn number_of_links(&self) -> Option<u32>;
/// Returns the value of the `nFileIndex{Low,High}` fields of this metadata.
#[cfg(windows_by_handle)]
fn file_index(&self) -> Option<u64>;
}

#[cfg(unix)]
impl MetadataExt for Metadata {
#[inline]
fn dev(&self) -> u64 {
rustix::fs::MetadataExt::dev(&self.ext)
crate::fs::MetadataExt::dev(&self.ext)
}

#[inline]
fn ino(&self) -> u64 {
rustix::fs::MetadataExt::ino(&self.ext)
crate::fs::MetadataExt::ino(&self.ext)
}

#[inline]
fn mode(&self) -> u32 {
rustix::fs::MetadataExt::mode(&self.ext)
crate::fs::MetadataExt::mode(&self.ext)
}

#[inline]
fn nlink(&self) -> u64 {
rustix::fs::MetadataExt::nlink(&self.ext)
crate::fs::MetadataExt::nlink(&self.ext)
}

#[inline]
fn uid(&self) -> u32 {
rustix::fs::MetadataExt::uid(&self.ext)
crate::fs::MetadataExt::uid(&self.ext)
}

#[inline]
fn gid(&self) -> u32 {
rustix::fs::MetadataExt::gid(&self.ext)
crate::fs::MetadataExt::gid(&self.ext)
}

#[inline]
fn rdev(&self) -> u64 {
rustix::fs::MetadataExt::rdev(&self.ext)
crate::fs::MetadataExt::rdev(&self.ext)
}

#[inline]
fn size(&self) -> u64 {
rustix::fs::MetadataExt::size(&self.ext)
crate::fs::MetadataExt::size(&self.ext)
}

#[inline]
fn atime(&self) -> i64 {
rustix::fs::MetadataExt::atime(&self.ext)
crate::fs::MetadataExt::atime(&self.ext)
}

#[inline]
fn atime_nsec(&self) -> i64 {
rustix::fs::MetadataExt::atime_nsec(&self.ext)
crate::fs::MetadataExt::atime_nsec(&self.ext)
}

#[inline]
fn mtime(&self) -> i64 {
rustix::fs::MetadataExt::mtime(&self.ext)
crate::fs::MetadataExt::mtime(&self.ext)
}

#[inline]
fn mtime_nsec(&self) -> i64 {
rustix::fs::MetadataExt::mtime_nsec(&self.ext)
crate::fs::MetadataExt::mtime_nsec(&self.ext)
}

#[inline]
fn ctime(&self) -> i64 {
rustix::fs::MetadataExt::ctime(&self.ext)
crate::fs::MetadataExt::ctime(&self.ext)
}

#[inline]
fn ctime_nsec(&self) -> i64 {
rustix::fs::MetadataExt::ctime_nsec(&self.ext)
crate::fs::MetadataExt::ctime_nsec(&self.ext)
}

#[inline]
fn blksize(&self) -> u64 {
rustix::fs::MetadataExt::blksize(&self.ext)
crate::fs::MetadataExt::blksize(&self.ext)
}

#[inline]
fn blocks(&self) -> u64 {
rustix::fs::MetadataExt::blocks(&self.ext)
crate::fs::MetadataExt::blocks(&self.ext)
}
}

#[cfg(target_os = "wasi")]
impl MetadataExt for Metadata {
#[inline]
fn dev(&self) -> u64 {
rustix::fs::MetadataExt::dev(&self.ext)
crate::fs::MetadataExt::dev(&self.ext)
}

#[inline]
fn ino(&self) -> u64 {
rustix::fs::MetadataExt::ino(&self.ext)
crate::fs::MetadataExt::ino(&self.ext)
}

#[inline]
fn nlink(&self) -> u64 {
rustix::fs::MetadataExt::nlink(&self.ext)
crate::fs::MetadataExt::nlink(&self.ext)
}

#[inline]
fn size(&self) -> u64 {
rustix::fs::MetadataExt::size(&self.ext)
crate::fs::MetadataExt::size(&self.ext)
}

#[inline]
fn atim(&self) -> u64 {
rustix::fs::MetadataExt::atim(&self.ext)
crate::fs::MetadataExt::atim(&self.ext)
}

#[inline]
fn mtim(&self) -> u64 {
rustix::fs::MetadataExt::mtim(&self.ext)
crate::fs::MetadataExt::mtim(&self.ext)
}

#[inline]
fn ctim(&self) -> u64 {
rustix::fs::MetadataExt::ctim(&self.ext)
crate::fs::MetadataExt::ctim(&self.ext)
}
}

Expand Down Expand Up @@ -485,7 +488,7 @@ impl MetadataExt for Metadata {
}
}

#[cfg(all(windows, windows_by_handle))]
#[cfg(windows)]
impl MetadataExt for Metadata {
#[inline]
fn file_attributes(&self) -> u32 {
Expand Down Expand Up @@ -513,16 +516,19 @@ impl MetadataExt for Metadata {
}

#[inline]
#[cfg(windows_by_handle)]
fn volume_serial_number(&self) -> Option<u32> {
self.ext.volume_serial_number()
}

#[inline]
#[cfg(windows_by_handle)]
fn number_of_links(&self) -> Option<u32> {
self.ext.number_of_links()
}

#[inline]
#[cfg(windows_by_handle)]
fn file_index(&self) -> Option<u64> {
self.ext.file_index()
}
Expand Down
2 changes: 1 addition & 1 deletion cap-primitives/src/rustix/fs/metadata_ext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,7 @@ fn system_time_from_rustix(sec: i64, nsec: u64) -> Option<SystemTime> {
}
}

impl rustix::fs::MetadataExt for ImplMetadataExt {
impl crate::fs::MetadataExt for ImplMetadataExt {
#[inline]
fn dev(&self) -> u64 {
self.dev
Expand Down
2 changes: 1 addition & 1 deletion cap-primitives/src/windows/fs/is_same_file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ pub(crate) fn is_same_file(a: &fs::File, b: &fs::File) -> io::Result<bool> {
#[cfg(windows_by_handle)]
#[allow(dead_code)]
pub(crate) fn is_same_file_metadata(a: &Metadata, b: &Metadata) -> io::Result<bool> {
use std::os::windows::fs::MetadataExt;
use crate::fs::MetadataExt;
Ok(a.volume_serial_number() == b.volume_serial_number() && a.file_index() == b.file_index())
}

Expand Down
Loading

0 comments on commit 1c14105

Please sign in to comment.