-
Notifications
You must be signed in to change notification settings - Fork 13
Various repository fixes #146
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
alexlarsson
wants to merge
3
commits into
containers:main
Choose a base branch
from
alexlarsson:repository-fixes
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,17 @@ | ||
use rand::{distr::Alphanumeric, Rng}; | ||
use std::{ | ||
io::{Error, ErrorKind, Read, Result}, | ||
os::fd::{AsFd, AsRawFd}, | ||
os::{ | ||
fd::{AsFd, AsRawFd, OwnedFd}, | ||
unix::ffi::OsStrExt, | ||
}, | ||
path::Path, | ||
}; | ||
|
||
use rustix::{ | ||
fs::{readlinkat, renameat, symlinkat, unlinkat, AtFlags}, | ||
io::{Errno, Result as ErrnoResult}, | ||
}; | ||
use tokio::io::{AsyncRead, AsyncReadExt}; | ||
|
||
/// Formats a string like "/proc/self/fd/3" for the given fd. This can be used to work with kernel | ||
|
@@ -97,6 +106,66 @@ pub fn parse_sha256(string: impl AsRef<str>) -> Result<Sha256Digest> { | |
Ok(value) | ||
} | ||
|
||
pub(crate) fn filter_errno<T>( | ||
result: rustix::io::Result<T>, | ||
ignored: Errno, | ||
) -> ErrnoResult<Option<T>> { | ||
match result { | ||
Ok(result) => Ok(Some(result)), | ||
Err(err) if err == ignored => Ok(None), | ||
Err(err) => Err(err), | ||
} | ||
} | ||
|
||
fn generate_tmpname(prefix: &str) -> String { | ||
let rand_string: String = rand::rng() | ||
.sample_iter(&Alphanumeric) | ||
.take(12) | ||
.map(char::from) | ||
.collect(); | ||
format!("{}{}", prefix, rand_string) | ||
} | ||
|
||
pub(crate) fn replace_symlinkat( | ||
target: impl AsRef<Path>, | ||
dirfd: &OwnedFd, | ||
name: impl AsRef<Path>, | ||
) -> ErrnoResult<()> { | ||
let name = name.as_ref(); | ||
let target = target.as_ref(); | ||
|
||
// Step 1: try to create the symlink | ||
if filter_errno(symlinkat(target, dirfd, name), Errno::EXIST)?.is_some() { | ||
return Ok(()); | ||
}; | ||
|
||
// Step 2: the symlink already exists. Maybe it already has the correct target? | ||
if let Some(current_target) = filter_errno(readlinkat(dirfd, name, []), Errno::NOENT)? { | ||
if current_target.into_bytes() == target.as_os_str().as_bytes() { | ||
return Ok(()); | ||
} | ||
} | ||
|
||
// Step 3: full atomic replace path | ||
for _ in 0..16 { | ||
let tmp_name = generate_tmpname(".symlink-"); | ||
if filter_errno(symlinkat(target, dirfd, &tmp_name), Errno::EXIST)?.is_none() { | ||
// This temporary filename already exists, try another | ||
continue; | ||
} | ||
|
||
match renameat(dirfd, &tmp_name, dirfd, name) { | ||
Ok(_) => return Ok(()), | ||
Err(e) => { | ||
let _ = unlinkat(dirfd, tmp_name, AtFlags::empty()); | ||
return Err(e); | ||
} | ||
} | ||
} | ||
|
||
Err(Errno::EXIST) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That could probably use some context... |
||
} | ||
|
||
#[cfg(test)] | ||
mod test { | ||
use similar_asserts::assert_eq; | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
BTW I maintain https://docs.rs/cap-std-ext/latest/cap_std_ext/dirext/trait.CapStdExtDirExt.html#tymethod.open_dir_optional
We had some previous debates about using cap-std here. I (obviously) like it a lot.
filter_errno
makes sense as is but it'd probably be good to have a higher levelopenat_optional
wrapper per above.