Skip to content

Commit a442ac2

Browse files
committed
utils: Move open_dir_noxdev here
Prep for using it elsewhere. Signed-off-by: Colin Walters <[email protected]>
1 parent 161bc31 commit a442ac2

File tree

2 files changed

+33
-33
lines changed

2 files changed

+33
-33
lines changed

lib/src/lints.rs

Lines changed: 1 addition & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@ use cap_std_ext::cap_std;
1111
use cap_std_ext::dirext::CapStdExtDirExt as _;
1212
use fn_error_context::context;
1313

14-
use crate::utils::openat2_with_retry;
15-
1614
/// Reference to embedded default baseimage content that should exist.
1715
const BASEIMAGE_REF: &str = "usr/share/doc/bootc/baseimage/base";
1816

@@ -72,25 +70,6 @@ fn check_kernel(root: &Dir) -> Result<()> {
7270
Ok(())
7371
}
7472

75-
/// Open the target directory, but return Ok(None) if this would cross a mount point.
76-
fn open_dir_noxdev(
77-
parent: &Dir,
78-
path: impl AsRef<std::path::Path>,
79-
) -> std::io::Result<Option<Dir>> {
80-
use rustix::fs::{Mode, OFlags, ResolveFlags};
81-
match openat2_with_retry(
82-
parent,
83-
path,
84-
OFlags::CLOEXEC | OFlags::DIRECTORY | OFlags::NOFOLLOW,
85-
Mode::empty(),
86-
ResolveFlags::NO_XDEV | ResolveFlags::BENEATH,
87-
) {
88-
Ok(r) => Ok(Some(Dir::reopen_dir(&r)?)),
89-
Err(e) if e == rustix::io::Errno::XDEV => Ok(None),
90-
Err(e) => return Err(e.into()),
91-
}
92-
}
93-
9473
fn check_utf8(dir: &Dir) -> Result<()> {
9574
for entry in dir.entries()? {
9675
let entry = entry?;
@@ -109,7 +88,7 @@ fn check_utf8(dir: &Dir) -> Result<()> {
10988
"/{strname}: Found non-utf8 symlink target"
11089
);
11190
} else if ifmt.is_dir() {
112-
let Some(subdir) = open_dir_noxdev(dir, entry.file_name())? else {
91+
let Some(subdir) = crate::utils::open_dir_noxdev(dir, entry.file_name())? else {
11392
continue;
11493
};
11594
if let Err(err) = check_utf8(&subdir) {
@@ -181,17 +160,6 @@ mod tests {
181160
Ok(tempdir)
182161
}
183162

184-
#[test]
185-
fn test_open_noxdev() -> Result<()> {
186-
let root = Dir::open_ambient_dir("/", cap_std::ambient_authority())?;
187-
// This hard requires the host setup to have /usr/bin on the same filesystem as /
188-
let usr = Dir::open_ambient_dir("/usr", cap_std::ambient_authority())?;
189-
assert!(open_dir_noxdev(&usr, "bin").unwrap().is_some());
190-
// Requires a mounted /proc, but that also seems ane.
191-
assert!(open_dir_noxdev(&root, "proc").unwrap().is_none());
192-
Ok(())
193-
}
194-
195163
#[test]
196164
fn test_var_run() -> Result<()> {
197165
let root = &fixture()?;

lib/src/utils.rs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,25 @@ pub(crate) fn open_dir_remount_rw(root: &Dir, target: &Utf8Path) -> Result<Dir>
110110
root.open_dir(target).map_err(anyhow::Error::new)
111111
}
112112

113+
/// Open the target directory, but return Ok(None) if this would cross a mount point.
114+
pub fn open_dir_noxdev(
115+
parent: &Dir,
116+
path: impl AsRef<std::path::Path>,
117+
) -> std::io::Result<Option<Dir>> {
118+
use rustix::fs::{Mode, OFlags, ResolveFlags};
119+
match openat2_with_retry(
120+
parent,
121+
path,
122+
OFlags::CLOEXEC | OFlags::DIRECTORY | OFlags::NOFOLLOW,
123+
Mode::empty(),
124+
ResolveFlags::NO_XDEV | ResolveFlags::BENEATH,
125+
) {
126+
Ok(r) => Ok(Some(Dir::reopen_dir(&r)?)),
127+
Err(e) if e == rustix::io::Errno::XDEV => Ok(None),
128+
Err(e) => return Err(e.into()),
129+
}
130+
}
131+
113132
/// Given a target path, remove its immutability if present
114133
#[context("Removing immutable flag from {target}")]
115134
pub(crate) fn remove_immutability(root: &Dir, target: &Utf8Path) -> Result<()> {
@@ -223,6 +242,8 @@ pub(crate) fn digested_pullspec(image: &str, digest: &str) -> String {
223242

224243
#[cfg(test)]
225244
mod tests {
245+
use cap_std_ext::cap_std;
246+
226247
use super::*;
227248

228249
#[test]
@@ -269,4 +290,15 @@ mod tests {
269290
SignatureSource::ContainerPolicyAllowInsecure
270291
);
271292
}
293+
294+
#[test]
295+
fn test_open_noxdev() -> Result<()> {
296+
let root = Dir::open_ambient_dir("/", cap_std::ambient_authority())?;
297+
// This hard requires the host setup to have /usr/bin on the same filesystem as /
298+
let usr = Dir::open_ambient_dir("/usr", cap_std::ambient_authority())?;
299+
assert!(open_dir_noxdev(&usr, "bin").unwrap().is_some());
300+
// Requires a mounted /proc, but that also seems ane.
301+
assert!(open_dir_noxdev(&root, "proc").unwrap().is_none());
302+
Ok(())
303+
}
272304
}

0 commit comments

Comments
 (0)