From a11be0acb25096c8a2ab48cc2f36a0f6a2b79a89 Mon Sep 17 00:00:00 2001 From: Dan Gohman Date: Thu, 21 Nov 2024 17:10:58 -0800 Subject: [PATCH 01/11] Backport build.rs fixes to 2.x. --- build.rs | 16 ++++++----- cap-fs-ext/build.rs | 55 ++++++++++++++++++++++++++++++++----- cap-primitives/build.rs | 60 ++++++++++++++++++++++++++++++++++++----- cap-std/build.rs | 58 ++++++++++++++++++++++++++++++++++----- 4 files changed, 162 insertions(+), 27 deletions(-) diff --git a/build.rs b/build.rs index 02dc64fa..65ce304d 100644 --- a/build.rs +++ b/build.rs @@ -17,6 +17,9 @@ fn main() { // https://doc.rust-lang.org/unstable-book/library-features/windows-file-type-ext.html use_feature_or_nothing("windows_file_type_ext"); + // Cfgs that users may set. + println!("cargo:rustc-check-cfg=cfg(racy_asserts)"); + // Don't rerun this on changes other than build.rs, as we only depend on // the rustc version. println!("cargo:rerun-if-changed=build.rs"); @@ -26,6 +29,7 @@ fn use_feature_or_nothing(feature: &str) { if has_feature(feature) { use_feature(feature); } + println!("cargo:rustc-check-cfg=cfg({})", feature); } fn use_feature(feature: &str) { @@ -34,7 +38,7 @@ 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!( + can_compile(format!( "#![allow(stable_features)]\n#![feature({})]", feature )) @@ -44,12 +48,11 @@ fn has_feature(feature: &str) -> bool { fn can_compile>(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]. + // 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() @@ -68,8 +71,9 @@ fn can_compile>(test: T) -> bool { .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("-o") + .arg("-") + .stdout(Stdio::null()); // We don't care about the output (only whether it builds or not) // If Cargo wants to set RUSTFLAGS, use that. if let Ok(rustflags) = var("CARGO_ENCODED_RUSTFLAGS") { diff --git a/cap-fs-ext/build.rs b/cap-fs-ext/build.rs index 6e8f4d3f..1716d753 100644 --- a/cap-fs-ext/build.rs +++ b/cap-fs-ext/build.rs @@ -13,6 +13,7 @@ fn use_feature_or_nothing(feature: &str) { if has_feature(feature) { use_feature(feature); } + println!("cargo:rustc-check-cfg=cfg({})", feature); } fn use_feature(feature: &str) { @@ -21,20 +22,60 @@ fn use_feature(feature: &str) { /// Test whether the rustc at `var("RUSTC")` supports the given feature. fn has_feature(feature: &str) -> bool { - let out_dir = var("OUT_DIR").unwrap(); + can_compile(&format!( + "#![allow(stable_features)]\n#![feature({})]", + feature + )) +} + +/// Test whether the rustc at `var("RUSTC")` can compile the given code. +fn can_compile>(test: T) -> bool { + use std::process::Stdio; + 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("--out-dir") - .arg(out_dir) // Put the output somewhere inconsequential. + .arg("--target") + .arg(target) + .arg("-o") + .arg("-") + .stdout(Stdio::null()); // We don't care about the output (only whether it builds or not) + + // 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() } diff --git a/cap-primitives/build.rs b/cap-primitives/build.rs index 966799b5..db78e77d 100644 --- a/cap-primitives/build.rs +++ b/cap-primitives/build.rs @@ -8,6 +8,11 @@ fn main() { use_feature_or_nothing("io_error_more"); // https://github.com/rust-lang/rust/issues/86442 use_feature_or_nothing("io_error_uncategorized"); + // Cfgs that users may set. + println!("cargo:rustc-check-cfg=cfg(racy_asserts)"); + println!("cargo:rustc-check-cfg=cfg(emulate_second_only_system)"); + println!("cargo:rustc-check-cfg=cfg(io_lifetimes_use_std)"); + // Don't rerun this on changes other than build.rs, as we only depend on // the rustc version. println!("cargo:rerun-if-changed=build.rs"); @@ -17,6 +22,7 @@ fn use_feature_or_nothing(feature: &str) { if has_feature(feature) { use_feature(feature); } + println!("cargo:rustc-check-cfg=cfg({})", feature); } fn use_feature(feature: &str) { @@ -25,20 +31,60 @@ fn use_feature(feature: &str) { /// Test whether the rustc at `var("RUSTC")` supports the given feature. fn has_feature(feature: &str) -> bool { - let out_dir = var("OUT_DIR").unwrap(); + can_compile(&format!( + "#![allow(stable_features)]\n#![feature({})]", + feature + )) +} + +/// Test whether the rustc at `var("RUSTC")` can compile the given code. +fn can_compile>(test: T) -> bool { + use std::process::Stdio; + 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("--out-dir") - .arg(out_dir) // Put the output somewhere inconsequential. + .arg("--target") + .arg(target) + .arg("-o") + .arg("-") + .stdout(Stdio::null()); // We don't care about the output (only whether it builds or not) + + // 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() } diff --git a/cap-std/build.rs b/cap-std/build.rs index a11d1016..c5a557a8 100644 --- a/cap-std/build.rs +++ b/cap-std/build.rs @@ -5,6 +5,9 @@ fn main() { use_feature_or_nothing("can_vector"); // https://github.com/rust-lang/rust/issues/69941 use_feature_or_nothing("write_all_vectored"); // https://github.com/rust-lang/rust/issues/70436 + // Cfgs that users may set. + println!("cargo:rustc-check-cfg=cfg(io_lifetimes_use_std)"); + // Don't rerun this on changes other than build.rs, as we only depend on // the rustc version. println!("cargo:rerun-if-changed=build.rs"); @@ -14,6 +17,7 @@ fn use_feature_or_nothing(feature: &str) { if has_feature(feature) { use_feature(feature); } + println!("cargo:rustc-check-cfg=cfg({})", feature); } fn use_feature(feature: &str) { @@ -22,20 +26,60 @@ fn use_feature(feature: &str) { /// Test whether the rustc at `var("RUSTC")` supports the given feature. fn has_feature(feature: &str) -> bool { - let out_dir = var("OUT_DIR").unwrap(); + can_compile(&format!( + "#![allow(stable_features)]\n#![feature({})]", + feature + )) +} + +/// Test whether the rustc at `var("RUSTC")` can compile the given code. +fn can_compile>(test: T) -> bool { + use std::process::Stdio; + 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("--out-dir") - .arg(out_dir) // Put the output somewhere inconsequential. + .arg("--target") + .arg(target) + .arg("-o") + .arg("-") + .stdout(Stdio::null()); // We don't care about the output (only whether it builds or not) + + // 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() } From 7bfdde23911ec47f78d4cc13b5be620bcd0ffdbf Mon Sep 17 00:00:00 2001 From: Dan Gohman Date: Thu, 21 Nov 2024 17:13:28 -0800 Subject: [PATCH 02/11] Fix warnings on recent Rust compilers. --- cap-std/build.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/cap-std/build.rs b/cap-std/build.rs index c5a557a8..f3afe13c 100644 --- a/cap-std/build.rs +++ b/cap-std/build.rs @@ -4,6 +4,7 @@ use std::io::Write; fn main() { use_feature_or_nothing("can_vector"); // https://github.com/rust-lang/rust/issues/69941 use_feature_or_nothing("write_all_vectored"); // https://github.com/rust-lang/rust/issues/70436 + use_feature_or_nothing("windows_file_type_ext"); // Cfgs that users may set. println!("cargo:rustc-check-cfg=cfg(io_lifetimes_use_std)"); From 8c0ad612657fc7e35fd2b6a59258b6d4ae6062bf Mon Sep 17 00:00:00 2001 From: Dan Gohman Date: Thu, 21 Nov 2024 17:14:53 -0800 Subject: [PATCH 03/11] Fix the build of timezone.rs on recent Rust versions. --- cap-time-ext/src/timezone.rs | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/cap-time-ext/src/timezone.rs b/cap-time-ext/src/timezone.rs index 4cc440e8..7002452d 100644 --- a/cap-time-ext/src/timezone.rs +++ b/cap-time-ext/src/timezone.rs @@ -4,9 +4,18 @@ use iana_time_zone::get_timezone; /// A reference to a timezone resource. pub struct Timezone(()); +/// An error type returned by `Timezone::timezone_name`. #[derive(Debug)] pub struct TimezoneError(String); +impl std::error::Error for TimezoneError {} + +impl std::fmt::Display for TimezoneError { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + self.0.fmt(f) + } +} + impl Timezone { /// Constructs a new instance of `Self`. /// From dce22b9208a8b8cfa9ba77a7245631d713218fbe Mon Sep 17 00:00:00 2001 From: Dan Gohman Date: Thu, 21 Nov 2024 17:18:27 -0800 Subject: [PATCH 04/11] Use docsrs instead of doc_cfg. --- cap-std/Cargo.toml | 2 +- cap-std/src/lib.rs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/cap-std/Cargo.toml b/cap-std/Cargo.toml index d5ea0e28..13c12fc5 100644 --- a/cap-std/Cargo.toml +++ b/cap-std/Cargo.toml @@ -14,7 +14,7 @@ edition = "2021" [package.metadata.docs.rs] all-features = true -rustdoc-args = ["--cfg=doc_cfg"] +rustdoc-args = ["--cfg=docsrs"] [dependencies] arf-strings = { version = "0.7.0", optional = true } diff --git a/cap-std/src/lib.rs b/cap-std/src/lib.rs index 5345fbeb..aa369c07 100644 --- a/cap-std/src/lib.rs +++ b/cap-std/src/lib.rs @@ -23,7 +23,7 @@ //! [`Pool`]: net::Pool #![deny(missing_docs)] -#![cfg_attr(doc_cfg, feature(doc_cfg, doc_auto_cfg))] +#![cfg_attr(docsrs, feature(doc_cfg, doc_auto_cfg))] #![cfg_attr(target_os = "wasi", feature(wasi_ext))] #![cfg_attr(can_vector, feature(can_vector))] #![cfg_attr(write_all_vectored, feature(write_all_vectored))] From e48184059fe88daa3cb4c4b0056a6ae950446926 Mon Sep 17 00:00:00 2001 From: Dan Gohman Date: Thu, 21 Nov 2024 17:18:47 -0800 Subject: [PATCH 05/11] Update CI for 2.x. --- .cirrus.yml | 12 +++++----- .github/actions/install-rust/action.yml | 2 +- .github/workflows/main.yml | 32 ++++++++++++------------- 3 files changed, 23 insertions(+), 23 deletions(-) diff --git a/.cirrus.yml b/.cirrus.yml index 5a7e9522..d6fa71b4 100644 --- a/.cirrus.yml +++ b/.cirrus.yml @@ -2,9 +2,9 @@ # at revision 7f4774e76bd5cb9ccb7140d71ef9be9c16009cdf. task: - name: stable x86_64-unknown-freebsd-14-snap + name: stable x86_64-unknown-freebsd-15-snap freebsd_instance: - image_family: freebsd-14-0-snap + image_family: freebsd-15-0-snap setup_script: - curl https://sh.rustup.rs -sSf --output rustup.sh - sh rustup.sh --default-toolchain stable -y --profile=minimal @@ -15,9 +15,9 @@ task: - cargo test --features=fs_utf8 --workspace task: - name: stable x86_64-unknown-freebsd-13 + name: stable x86_64-unknown-freebsd-14 freebsd_instance: - image_family: freebsd-13-2 + image_family: freebsd-14-0 setup_script: - curl https://sh.rustup.rs -sSf --output rustup.sh - sh rustup.sh --default-toolchain stable -y --profile=minimal @@ -28,9 +28,9 @@ task: - cargo test --features=fs_utf8 --workspace task: - name: stable x86_64-unknown-freebsd-12 + name: stable x86_64-unknown-freebsd-13 freebsd_instance: - image_family: freebsd-12-4 + image_family: freebsd-13-3 setup_script: - curl https://sh.rustup.rs -sSf --output rustup.sh - sh rustup.sh --default-toolchain stable -y --profile=minimal diff --git a/.github/actions/install-rust/action.yml b/.github/actions/install-rust/action.yml index 6afc01d6..11c05760 100644 --- a/.github/actions/install-rust/action.yml +++ b/.github/actions/install-rust/action.yml @@ -8,5 +8,5 @@ inputs: default: 'stable' runs: - using: node16 + using: node20 main: 'main.js' diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 598da3c3..5fe6b4e4 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -11,7 +11,7 @@ jobs: name: Rustfmt runs-on: ubuntu-latest steps: - - uses: actions/checkout@v3 + - uses: actions/checkout@v4 with: submodules: true - uses: ./.github/actions/install-rust @@ -34,7 +34,7 @@ jobs: rust: beta steps: - - uses: actions/checkout@v3 + - uses: actions/checkout@v4 with: submodules: true - uses: ./.github/actions/install-rust @@ -88,7 +88,7 @@ jobs: rust: beta steps: - - uses: actions/checkout@v3 + - uses: actions/checkout@v4 with: submodules: true - uses: ./.github/actions/install-rust @@ -116,7 +116,7 @@ jobs: rust: nightly steps: - - uses: actions/checkout@v3 + - uses: actions/checkout@v4 with: submodules: true - uses: ./.github/actions/install-rust @@ -150,7 +150,7 @@ jobs: riscv64gc-unknown-linux-gnu arm-unknown-linux-gnueabihf aarch64-linux-android - wasm32-wasi + wasm32-wasip1 - run: cargo check --workspace --all-targets --all-features --release -vv - run: cargo check --workspace --all-targets --all-features --release -vv --target=x86_64-unknown-linux-musl - run: cargo check --workspace --all-targets --all-features --release -vv --target=x86_64-unknown-linux-gnux32 @@ -164,7 +164,7 @@ jobs: - run: cargo check --workspace --all-targets --all-features --release -vv --target=riscv64gc-unknown-linux-gnu - run: cargo check --workspace --all-targets --all-features --release -vv --target=arm-unknown-linux-gnueabihf - run: cargo check --workspace --all-targets --all-features --release -vv --target=aarch64-linux-android - - run: cd cap-std && cargo check --features=fs_utf8 --release -vv --target=wasm32-wasi + - run: cd cap-std && cargo check --features=fs_utf8 --release -vv --target=wasm32-wasip1 check_cross_nightly_windows: name: Check Cross-Compilation on Rust nightly on Windows @@ -178,7 +178,7 @@ jobs: rust: nightly steps: - - uses: actions/checkout@v3 + - uses: actions/checkout@v4 with: submodules: true - uses: ./.github/actions/install-rust @@ -201,7 +201,7 @@ jobs: runs-on: ${{ matrix.os }} strategy: matrix: - build: [stable, windows-latest, windows-2019, macos-latest, macos-11, beta, ubuntu-20.04, aarch64-ubuntu] + build: [stable, windows-latest, windows-2019, macos-latest, macos-12, beta, ubuntu-20.04, aarch64-ubuntu] include: - build: stable os: ubuntu-latest @@ -215,8 +215,8 @@ jobs: - build: macos-latest os: macos-latest rust: stable - - build: macos-11 - os: macos-11 + - build: macos-12 + os: macos-12 rust: stable - build: beta os: ubuntu-latest @@ -234,7 +234,7 @@ jobs: qemu_target: aarch64-linux-user steps: - - uses: actions/checkout@v3 + - uses: actions/checkout@v4 with: submodules: true - uses: ./.github/actions/install-rust @@ -291,7 +291,7 @@ jobs: rust: nightly steps: - - uses: actions/checkout@v3 + - uses: actions/checkout@v4 with: submodules: true - uses: ./.github/actions/install-rust @@ -311,7 +311,7 @@ jobs: rust: stable steps: - - uses: actions/checkout@v3 + - uses: actions/checkout@v4 with: submodules: true - uses: ./.github/actions/install-rust @@ -345,7 +345,7 @@ jobs: RUSTFLAGS: --cfg linux_raw RUSTDOCFLAGS: --cfg linux_raw steps: - - uses: actions/checkout@v3 + - uses: actions/checkout@v4 with: submodules: true - uses: ./.github/actions/install-rust @@ -365,7 +365,7 @@ jobs: rust: 1.63 steps: - - uses: actions/checkout@v3 + - uses: actions/checkout@v4 with: submodules: true - uses: ./.github/actions/install-rust @@ -381,7 +381,7 @@ jobs: name: Fuzz Targets runs-on: ubuntu-latest steps: - - uses: actions/checkout@v3 + - uses: actions/checkout@v4 with: submodules: true - uses: ./.github/actions/install-rust From 6733f362a72e6213f3bc81ae42663161e37b2426 Mon Sep 17 00:00:00 2001 From: Dan Gohman Date: Thu, 21 Nov 2024 17:33:17 -0800 Subject: [PATCH 06/11] Fix warnings about "async_std". --- cap-fs-ext/Cargo.toml | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/cap-fs-ext/Cargo.toml b/cap-fs-ext/Cargo.toml index 05b3e964..d2ee7947 100644 --- a/cap-fs-ext/Cargo.toml +++ b/cap-fs-ext/Cargo.toml @@ -40,3 +40,9 @@ features = [ [dev-dependencies] cap-tempfile = { path = "../cap-tempfile" } + +[lints.rust.unexpected_cfgs] +level = "warn" +check-cfg = [ + 'cfg(feature, values("async_std"))' +] From 77644e2bec5ba5e753b4017811952f717770ad69 Mon Sep 17 00:00:00 2001 From: Dan Gohman Date: Fri, 22 Nov 2024 09:23:44 -0800 Subject: [PATCH 07/11] Fix a warning. --- tests/sys_common/symlink_junction.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/sys_common/symlink_junction.rs b/tests/sys_common/symlink_junction.rs index a774880d..1d4b3822 100644 --- a/tests/sys_common/symlink_junction.rs +++ b/tests/sys_common/symlink_junction.rs @@ -54,6 +54,7 @@ pub fn symlink_junction_utf8, Q: AsRef>( /// /// This is enough for almost all of the buffers we're likely to work with in /// the Windows APIs we use. +#[cfg(windows)] #[repr(C, align(8))] #[derive(Copy, Clone)] struct Align8(pub T); From bdcbc6f29733b5b1cc43d192e2f76afe5ad96fed Mon Sep 17 00:00:00 2001 From: Dan Gohman Date: Fri, 22 Nov 2024 09:28:17 -0800 Subject: [PATCH 08/11] Fix test warnings. --- tests/cap-basics.rs | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/tests/cap-basics.rs b/tests/cap-basics.rs index eae9925d..b1f37f0a 100644 --- a/tests/cap-basics.rs +++ b/tests/cap-basics.rs @@ -217,10 +217,11 @@ fn symlink_loop_from_rename() { check!(tmpdir.open("link")); } -#[cfg(linux)] +#[cfg(target_os = "linux")] #[test] fn proc_self_fd() { - let fd = check!(File::open("/proc/self/fd")); + let fd = check!(std::fs::File::open("/proc/self/fd")); let dir = cap_std::fs::Dir::from_std_file(fd); - error!(dir.open("0"), "No such file"); + // This should fail with "too many levels of symbolic links". + dir.open("0").unwrap_err(); } From 35b150b48e869c4ee62c2291b74fa876e07f47ce Mon Sep 17 00:00:00 2001 From: Dan Gohman Date: Fri, 22 Nov 2024 09:35:52 -0800 Subject: [PATCH 09/11] Backport more warning fixes from main. --- cap-primitives/src/rustix/fs/metadata_ext.rs | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/cap-primitives/src/rustix/fs/metadata_ext.rs b/cap-primitives/src/rustix/fs/metadata_ext.rs index 7091017e..09acbf64 100644 --- a/cap-primitives/src/rustix/fs/metadata_ext.rs +++ b/cap-primitives/src/rustix/fs/metadata_ext.rs @@ -102,6 +102,9 @@ impl MetadataExt { #[inline] #[allow(unused_comparisons)] // NB: rust-lang/rust#115823 requires this here instead of on `st_dev` processing below pub(crate) fn from_rustix(stat: Stat) -> Metadata { + #[cfg(not(target_os = "wasi"))] + use rustix::fs::StatExt; + Metadata { file_type: ImplFileTypeExt::from_raw_mode(stat.st_mode as RawMode), len: u64::try_from(stat.st_size).unwrap(), @@ -112,12 +115,12 @@ impl MetadataExt { #[cfg(not(any(target_os = "netbsd", target_os = "wasi")))] modified: system_time_from_rustix( - stat.st_mtime.try_into().unwrap(), + stat.mtime().try_into().unwrap(), stat.st_mtime_nsec as _, ), #[cfg(not(any(target_os = "netbsd", target_os = "wasi")))] accessed: system_time_from_rustix( - stat.st_atime.try_into().unwrap(), + stat.atime().try_into().unwrap(), stat.st_atime_nsec as _, ), @@ -191,19 +194,19 @@ impl MetadataExt { rdev: u64::try_from(stat.st_rdev).unwrap(), size: u64::try_from(stat.st_size).unwrap(), #[cfg(not(target_os = "wasi"))] - atime: i64::try_from(stat.st_atime).unwrap(), + atime: i64::try_from(stat.atime()).unwrap(), #[cfg(not(any(target_os = "netbsd", target_os = "wasi")))] atime_nsec: stat.st_atime_nsec as _, #[cfg(target_os = "netbsd")] atime_nsec: stat.st_atimensec as _, #[cfg(not(target_os = "wasi"))] - mtime: i64::try_from(stat.st_mtime).unwrap(), + mtime: i64::try_from(stat.mtime()).unwrap(), #[cfg(not(any(target_os = "netbsd", target_os = "wasi")))] mtime_nsec: stat.st_mtime_nsec as _, #[cfg(target_os = "netbsd")] mtime_nsec: stat.st_mtimensec as _, #[cfg(not(target_os = "wasi"))] - ctime: i64::try_from(stat.st_ctime).unwrap(), + ctime: i64::try_from(stat.ctime()).unwrap(), #[cfg(not(any(target_os = "netbsd", target_os = "wasi")))] ctime_nsec: stat.st_ctime_nsec as _, #[cfg(target_os = "netbsd")] From 040a71cff51c7633f892a02ba4be725bd8c0b182 Mon Sep 17 00:00:00 2001 From: Dan Gohman Date: Fri, 22 Nov 2024 09:45:12 -0800 Subject: [PATCH 10/11] Implement `change_time` for windows' `MetadataExt`. --- cap-primitives/Cargo.toml | 2 +- cap-primitives/build.rs | 1 + cap-primitives/src/fs/metadata.rs | 5 +++ cap-primitives/src/lib.rs | 1 + cap-primitives/src/windows/fs/metadata_ext.rs | 37 +++++++++++++++++-- 5 files changed, 41 insertions(+), 5 deletions(-) diff --git a/cap-primitives/Cargo.toml b/cap-primitives/Cargo.toml index c871fcbf..2a596c27 100644 --- a/cap-primitives/Cargo.toml +++ b/cap-primitives/Cargo.toml @@ -25,7 +25,7 @@ io-lifetimes = { version = "2.0.0", default-features = false } cap-tempfile = { path = "../cap-tempfile" } [target.'cfg(not(windows))'.dependencies] -rustix = { version = "0.38.0", features = ["fs", "process", "procfs", "termios", "time"] } +rustix = { version = "0.38.31", features = ["fs", "process", "procfs", "termios", "time"] } [target.'cfg(windows)'.dependencies] winx = "0.36.0" diff --git a/cap-primitives/build.rs b/cap-primitives/build.rs index db78e77d..0515658a 100644 --- a/cap-primitives/build.rs +++ b/cap-primitives/build.rs @@ -5,6 +5,7 @@ fn main() { use_feature_or_nothing("windows_by_handle"); // https://github.com/rust-lang/rust/issues/63010 // https://doc.rust-lang.org/unstable-book/library-features/windows-file-type-ext.html use_feature_or_nothing("windows_file_type_ext"); + use_feature_or_nothing("windows_change_time"); use_feature_or_nothing("io_error_more"); // https://github.com/rust-lang/rust/issues/86442 use_feature_or_nothing("io_error_uncategorized"); diff --git a/cap-primitives/src/fs/metadata.rs b/cap-primitives/src/fs/metadata.rs index afcda83d..fd63d5c4 100644 --- a/cap-primitives/src/fs/metadata.rs +++ b/cap-primitives/src/fs/metadata.rs @@ -443,6 +443,11 @@ impl std::os::windows::fs::MetadataExt for Metadata { fn file_index(&self) -> Option { self.ext.file_index() } + + #[inline] + fn change_time(&self) -> Option { + self.ext.change_time() + } } /// Extension trait to allow `volume_serial_number` etc. to be exposed by diff --git a/cap-primitives/src/lib.rs b/cap-primitives/src/lib.rs index e9943917..03245996 100644 --- a/cap-primitives/src/lib.rs +++ b/cap-primitives/src/lib.rs @@ -5,6 +5,7 @@ #![allow(stable_features)] #![cfg_attr(target_os = "wasi", feature(wasi_ext))] #![cfg_attr(all(windows, windows_by_handle), feature(windows_by_handle))] +#![cfg_attr(all(windows, windows_change_time), feature(windows_change_time))] #![cfg_attr(all(windows, windows_file_type_ext), feature(windows_file_type_ext))] #![doc( html_logo_url = "https://raw.githubusercontent.com/bytecodealliance/cap-std/main/media/cap-std.svg" diff --git a/cap-primitives/src/windows/fs/metadata_ext.rs b/cap-primitives/src/windows/fs/metadata_ext.rs index 64797c77..38609ba3 100644 --- a/cap-primitives/src/windows/fs/metadata_ext.rs +++ b/cap-primitives/src/windows/fs/metadata_ext.rs @@ -16,6 +16,8 @@ pub(crate) struct MetadataExt { volume_serial_number: Option, number_of_links: Option, file_index: Option, + #[cfg(windows_change_time)] + change_time: Option, } impl MetadataExt { @@ -24,7 +26,8 @@ impl MetadataExt { #[inline] #[allow(unused_variables)] pub(crate) fn from(file: &fs::File, std: &fs::Metadata) -> io::Result { - let (mut volume_serial_number, mut number_of_links, mut file_index) = (None, None, None); + let (mut volume_serial_number, mut number_of_links, mut file_index, mut change_time) = + (None, None, None, None); #[cfg(windows_by_handle)] { @@ -38,10 +41,17 @@ impl MetadataExt { if let Some(some) = std.file_index() { file_index = Some(some); } + if let Some(some) = std.change_time() { + change_time = Some(some); + } } #[cfg(not(windows_by_handle))] - if volume_serial_number.is_none() || number_of_links.is_none() || file_index.is_none() { + if volume_serial_number.is_none() + || number_of_links.is_none() + || file_index.is_none() + || change_time.is_none() + { let fileinfo = winx::winapi_util::file::information(file)?; if volume_serial_number.is_none() { let t64: u64 = fileinfo.volume_serial_number(); @@ -63,6 +73,7 @@ impl MetadataExt { volume_serial_number, number_of_links, file_index, + change_time, )) } @@ -76,7 +87,8 @@ impl MetadataExt { #[inline] #[allow(unused_mut)] pub(crate) fn from_just_metadata(std: &fs::Metadata) -> Self { - let (mut volume_serial_number, mut number_of_links, mut file_index) = (None, None, None); + let (mut volume_serial_number, mut number_of_links, mut file_index, mut change_time) = + (None, None, None, None); #[cfg(windows_by_handle)] { @@ -90,9 +102,18 @@ impl MetadataExt { if let Some(some) = std.file_index() { file_index = Some(some); } + if let Some(some) = std.change_time() { + change_time = Some(some); + } } - Self::from_parts(std, volume_serial_number, number_of_links, file_index) + Self::from_parts( + std, + volume_serial_number, + number_of_links, + file_index, + change_time, + ) } #[inline] @@ -101,6 +122,7 @@ impl MetadataExt { volume_serial_number: Option, number_of_links: Option, file_index: Option, + change_time: Option, ) -> Self { use std::os::windows::fs::MetadataExt; Self { @@ -116,6 +138,8 @@ impl MetadataExt { volume_serial_number, number_of_links, file_index, + #[cfg(windows_change_time)] + change_time, } } @@ -188,6 +212,11 @@ impl std::os::windows::fs::MetadataExt for MetadataExt { fn file_index(&self) -> Option { self.file_index } + + #[inline] + fn change_time(&self) -> Option { + self.change_time + } } #[doc(hidden)] From 1afe1ca56bf109907a390f1bebe9dd0ceee22831 Mon Sep 17 00:00:00 2001 From: Dan Gohman Date: Fri, 22 Nov 2024 09:49:08 -0800 Subject: [PATCH 11/11] Fix warnings. --- cap-primitives/src/windows/fs/metadata_ext.rs | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/cap-primitives/src/windows/fs/metadata_ext.rs b/cap-primitives/src/windows/fs/metadata_ext.rs index 38609ba3..3b323546 100644 --- a/cap-primitives/src/windows/fs/metadata_ext.rs +++ b/cap-primitives/src/windows/fs/metadata_ext.rs @@ -66,6 +66,7 @@ impl MetadataExt { if file_index.is_none() { file_index = Some(fileinfo.file_index()); } + change_time = None; } Ok(Self::from_parts( @@ -125,6 +126,10 @@ impl MetadataExt { change_time: Option, ) -> Self { use std::os::windows::fs::MetadataExt; + + #[cfg(not(windows_change_time))] + let _ = change_time; + Self { file_attributes: std.file_attributes(), #[cfg(windows_by_handle)]