Skip to content

Commit b854ed5

Browse files
alexlarssonallisonkarlitskaya
authored andcommitted
utils: Make filter_errno a method on the result instead of a function
I.e. instead of `filter(open(), ENOENT)`, you do `open().filter(ENOENT)`. Signed-off-by: Alexander Larsson <[email protected]>
1 parent 9154c64 commit b854ed5

File tree

2 files changed

+31
-25
lines changed

2 files changed

+31
-25
lines changed

crates/composefs/src/repository.rs

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ use crate::{
2626
},
2727
mount::{composefs_fsmount, mount_at},
2828
splitstream::{DigestMap, SplitStreamReader, SplitStreamWriter},
29-
util::{filter_errno, proc_self_fd, replace_symlinkat, Sha256Digest},
29+
util::{proc_self_fd, replace_symlinkat, ErrnoFilter, Sha256Digest},
3030
};
3131

3232
/// Call openat() on the named subdirectory of "dirfd", possibly creating it first.
@@ -550,24 +550,21 @@ impl<ObjectID: FsVerityHashValue> Repository<ObjectID> {
550550
fn gc_category(&self, category: &str) -> Result<HashSet<ObjectID>> {
551551
let mut objects = HashSet::new();
552552

553-
let Some(category_fd) = filter_errno(
554-
self.openat(category, OFlags::RDONLY | OFlags::DIRECTORY),
555-
Errno::NOENT,
556-
)
557-
.context("Opening {category} dir in repository")?
553+
let Some(category_fd) = self
554+
.openat(category, OFlags::RDONLY | OFlags::DIRECTORY)
555+
.filter_errno(Errno::NOENT)
556+
.context("Opening {category} dir in repository")?
558557
else {
559558
return Ok(objects);
560559
};
561560

562-
if let Some(refs) = filter_errno(
563-
openat(
564-
&category_fd,
565-
"refs",
566-
OFlags::RDONLY | OFlags::DIRECTORY,
567-
Mode::empty(),
568-
),
569-
Errno::NOENT,
561+
if let Some(refs) = openat(
562+
&category_fd,
563+
"refs",
564+
OFlags::RDONLY | OFlags::DIRECTORY,
565+
Mode::empty(),
570566
)
567+
.filter_errno(Errno::NOENT)
571568
.context("Opening {category}/refs dir in repository")?
572569
{
573570
Self::walk_symlinkdir(refs, &mut objects)?;

crates/composefs/src/util.rs

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -106,14 +106,17 @@ pub fn parse_sha256(string: impl AsRef<str>) -> Result<Sha256Digest> {
106106
Ok(value)
107107
}
108108

109-
pub(crate) fn filter_errno<T>(
110-
result: rustix::io::Result<T>,
111-
ignored: Errno,
112-
) -> ErrnoResult<Option<T>> {
113-
match result {
114-
Ok(result) => Ok(Some(result)),
115-
Err(err) if err == ignored => Ok(None),
116-
Err(err) => Err(err),
109+
pub(crate) trait ErrnoFilter<T> {
110+
fn filter_errno(self, ignored: Errno) -> ErrnoResult<Option<T>>;
111+
}
112+
113+
impl<T> ErrnoFilter<T> for ErrnoResult<T> {
114+
fn filter_errno(self, ignored: Errno) -> ErrnoResult<Option<T>> {
115+
match self {
116+
Ok(result) => Ok(Some(result)),
117+
Err(err) if err == ignored => Ok(None),
118+
Err(err) => Err(err),
119+
}
117120
}
118121
}
119122

@@ -135,12 +138,15 @@ pub(crate) fn replace_symlinkat(
135138
let target = target.as_ref();
136139

137140
// Step 1: try to create the symlink
138-
if filter_errno(symlinkat(target, dirfd, name), Errno::EXIST)?.is_some() {
141+
if symlinkat(target, dirfd, name)
142+
.filter_errno(Errno::EXIST)?
143+
.is_some()
144+
{
139145
return Ok(());
140146
};
141147

142148
// Step 2: the symlink already exists. Maybe it already has the correct target?
143-
if let Some(current_target) = filter_errno(readlinkat(dirfd, name, []), Errno::NOENT)? {
149+
if let Some(current_target) = readlinkat(dirfd, name, []).filter_errno(Errno::NOENT)? {
144150
if current_target.into_bytes() == target.as_os_str().as_bytes() {
145151
return Ok(());
146152
}
@@ -149,7 +155,10 @@ pub(crate) fn replace_symlinkat(
149155
// Step 3: full atomic replace path
150156
for _ in 0..16 {
151157
let tmp_name = generate_tmpname(".symlink-");
152-
if filter_errno(symlinkat(target, dirfd, &tmp_name), Errno::EXIST)?.is_none() {
158+
if symlinkat(target, dirfd, &tmp_name)
159+
.filter_errno(Errno::EXIST)?
160+
.is_none()
161+
{
153162
// This temporary filename already exists, try another
154163
continue;
155164
}

0 commit comments

Comments
 (0)