Skip to content

Commit 6024c04

Browse files
authored
Merge pull request #75 from korrat/korrat/push-wvxqwumulwuz
Properly feature gate rustix-based functions
2 parents 677efca + 75c5d31 commit 6024c04

File tree

3 files changed

+147
-80
lines changed

3 files changed

+147
-80
lines changed

.github/workflows/rust.yml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,3 +117,19 @@ jobs:
117117
run: cargo build --all-targets
118118
- name: cargo test
119119
run: cargo test --all-targets
120+
tests-windows:
121+
name: Tests, Windows
122+
runs-on: windows-latest
123+
steps:
124+
- name: Check out repository
125+
uses: actions/checkout@v4
126+
- name: Install toolchain
127+
uses: dtolnay/rust-toolchain@v1
128+
with:
129+
toolchain: stable
130+
- name: Cache build artifacts
131+
uses: Swatinem/rust-cache@v2
132+
- name: cargo build
133+
run: cargo build --all-targets
134+
- name: cargo test
135+
run: cargo test --all-targets

src/dirext.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,14 @@ use cap_primitives::fs::FileType;
1212
use cap_std::fs::{Dir, File, Metadata};
1313
use cap_tempfile::cap_std;
1414
use cap_tempfile::cap_std::fs::DirEntry;
15+
#[cfg(any(target_os = "android", target_os = "linux"))]
1516
use rustix::path::Arg;
1617
use std::cmp::Ordering;
1718
use std::ffi::OsStr;
1819
use std::io::Result;
1920
use std::io::{self, Write};
2021
use std::ops::Deref;
22+
#[cfg(unix)]
2123
use std::os::fd::OwnedFd;
2224
use std::path::{Path, PathBuf};
2325

@@ -197,6 +199,7 @@ pub trait CapStdExtDirExt {
197199
/// ```
198200
///
199201
/// Any existing file will be replaced.
202+
#[cfg(not(windows))]
200203
fn atomic_replace_with<F, T, E>(
201204
&self,
202205
destname: impl AsRef<Path>,
@@ -207,9 +210,11 @@ pub trait CapStdExtDirExt {
207210
E: From<std::io::Error>;
208211

209212
/// Atomically write the provided contents to a file.
213+
#[cfg(not(windows))]
210214
fn atomic_write(&self, destname: impl AsRef<Path>, contents: impl AsRef<[u8]>) -> Result<()>;
211215

212216
/// Atomically write the provided contents to a file, using specified permissions.
217+
#[cfg(not(windows))]
213218
fn atomic_write_with_perms(
214219
&self,
215220
destname: impl AsRef<Path>,
@@ -447,6 +452,7 @@ fn subdir_of<'d, 'p>(d: &'d Dir, p: &'p Path) -> io::Result<(DirOwnedOrBorrowed<
447452
Ok((r, name))
448453
}
449454

455+
#[cfg(any(target_os = "android", target_os = "linux"))]
450456
/// A thin wrapper for [`openat2`] but that retries on interruption.
451457
fn openat2_with_retry(
452458
dirfd: impl std::os::fd::AsFd,
@@ -474,6 +480,7 @@ fn openat2_with_retry(
474480
})
475481
}
476482

483+
#[cfg(any(target_os = "android", target_os = "linux"))]
477484
fn is_mountpoint_impl_statx(root: &Dir, path: &Path) -> Result<Option<bool>> {
478485
// https://github.com/systemd/systemd/blob/8fbf0a214e2fe474655b17a4b663122943b55db0/src/basic/mountpoint-util.c#L176
479486
use rustix::fs::StatxAttributes;
@@ -577,6 +584,7 @@ where
577584
debug_assert!(matches!(flow, std::ops::ControlFlow::Continue(())));
578585
// Open the child directory, using the noxdev API if
579586
// we're configured not to cross devices,
587+
#[cfg(any(target_os = "android", target_os = "linux"))]
580588
let d = {
581589
if !config.noxdev {
582590
entry.open_dir()?
@@ -587,6 +595,10 @@ where
587595
continue;
588596
}
589597
};
598+
599+
#[cfg(not(any(target_os = "android", target_os = "linux")))]
600+
let d = entry.open_dir()?;
601+
590602
// Recurse into the target directory
591603
walk_inner(&d, path, callback, config)?;
592604
path.pop();
@@ -707,6 +719,7 @@ impl CapStdExtDirExt for Dir {
707719
Ok(())
708720
}
709721

722+
#[cfg(not(windows))]
710723
fn atomic_replace_with<F, T, E>(
711724
&self,
712725
destname: impl AsRef<Path>,
@@ -744,10 +757,12 @@ impl CapStdExtDirExt for Dir {
744757
Ok(r)
745758
}
746759

760+
#[cfg(not(windows))]
747761
fn atomic_write(&self, destname: impl AsRef<Path>, contents: impl AsRef<[u8]>) -> Result<()> {
748762
self.atomic_replace_with(destname, |f| f.write_all(contents.as_ref()))
749763
}
750764

765+
#[cfg(not(windows))]
751766
fn atomic_write_with_perms(
752767
&self,
753768
destname: impl AsRef<Path>,
@@ -784,6 +799,7 @@ impl CapStdExtDirExt for Dir {
784799
.map_err(Into::into)
785800
}
786801

802+
#[cfg(any(target_os = "android", target_os = "linux"))]
787803
fn is_mountpoint(&self, path: impl AsRef<Path>) -> Result<Option<bool>> {
788804
is_mountpoint_impl_statx(self, path.as_ref()).map_err(Into::into)
789805
}
@@ -931,7 +947,10 @@ mod tests {
931947
#[test]
932948
fn test_validate_relpath_no_uplinks() {
933949
let ok_cases = ["foo", "foo/bar", "foo/bar/"];
950+
#[cfg(unix)]
934951
let err_cases = ["/foo", "/", "../foo", "foo/../bar"];
952+
#[cfg(windows)]
953+
let err_cases = ["C:\\foo", "../foo", "foo/../bar"];
935954

936955
for case in ok_cases {
937956
assert!(validate_relpath_no_uplinks(Path::new(case)).is_ok());

0 commit comments

Comments
 (0)