Repository navigation
Minimald scaffolding, including sessions actor, session actor, session store + generics, and RPC tests #253
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
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
37fbadc
crates/minimald: move ssh RPC handler out of russh::Handler impl
twitchyliquid64 0290f6c
crates/sessions: implement session store/loader types
twitchyliquid64 e5fbeba
crates/{common,lcache}: move atomic rename code to common crate
twitchyliquid64 a226768
crates/{minimald,sessions}: Sessions+Session actors, lifecycle and RP…
twitchyliquid64 6eebe24
refactor(minimald): address code-review comments
twitchyliquid64 809c719
chore: address review feedback, create Responder type for encapsulati…
twitchyliquid64 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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 |
|---|---|---|
| @@ -0,0 +1,113 @@ | ||
| //! Direct wrapper around the Linux `renameat2(2)` syscall. | ||
| //! | ||
| //! Unlike `rename(2)`, `renameat2` accepts flags such as `RENAME_EXCHANGE` | ||
| //! that atomically swap two paths — useful for cache finalization where the | ||
| //! existing entry must be replaced without ever leaving the destination empty. | ||
|
|
||
| use std::ffi::CString; | ||
| use std::io; | ||
| use std::os::unix::ffi::OsStrExt; | ||
| use std::path::Path; | ||
|
|
||
| #[cfg(target_arch = "x86_64")] | ||
| const SYS_RENAMEAT2: i64 = 316; | ||
|
|
||
| #[cfg(target_arch = "aarch64")] | ||
| 8000 const SYS_RENAMEAT2: i64 = 276; | ||
|
|
||
| /// Special value used as a directory fd to indicate the current working directory. | ||
| pub const AT_FDCWD: i32 = -100; | ||
|
|
||
| /// Flag for `renameat2`: don't overwrite the target. | ||
| pub const RENAME_NOREPLACE: u32 = 1 << 0; | ||
| /// Flag for `renameat2`: atomically exchange the two paths. | ||
| pub const RENAME_EXCHANGE: u32 = 1 << 1; | ||
| /// Flag for `renameat2`: create a whiteout object at the source. | ||
| pub const RENAME_WHITEOUT: u32 = 1 << 2; | ||
|
|
||
| /// Invokes the `renameat2(2)` syscall directly. | ||
| pub fn renameat2<P: AsRef<Path>>( | ||
| olddirfd: i32, | ||
| oldpath: P, | ||
| newdirfd: i32, | ||
| newpath: P, | ||
| flags: u32, | ||
| ) -> io::Result<()> { | ||
| let oldpath_c = path_to_cstring(oldpath.as_ref())?; | ||
| let newpath_c = path_to_cstring(newpath.as_ref())?; | ||
|
|
||
| // SAFETY: `oldpath_c` and `newpath_c` are owned `CString`s that remain live | ||
| // for the duration of the call, so their `.as_ptr()` values are valid, | ||
| // NUL-terminated C strings. `olddirfd` and `newdirfd` are either real file | ||
| // descriptors supplied by the caller or `AT_FDCWD`, both of which the | ||
| // kernel accepts. `flags` is a `u32` bitmask of the `RENAME_*` constants | ||
| // documented in `renameat2(2)`; unknown bits are rejected by the kernel | ||
| // with `EINVAL` rather than causing UB. The syscall returns `-1` on error | ||
| // (with errno set) or a non-negative value on success, which we inspect | ||
| // immediately below. | ||
| let ret = unsafe { | ||
| libc::syscall( | ||
| SYS_RENAMEAT2, | ||
| olddirfd, | ||
| oldpath_c.as_ptr(), | ||
| newdirfd, | ||
| newpath_c.as_ptr(), | ||
| flags, | ||
| ) | ||
| }; | ||
|
|
||
| if ret == 0 { | ||
| Ok(()) | ||
| } else { | ||
| Err(io::Error::last_os_error()) | ||
| } | ||
| } | ||
|
|
||
| /// Convenience wrapper around [`renameat2`] for the common case where both | ||
| /// paths are resolved relative to the current working directory. | ||
| pub fn renameat2_cwd<P: AsRef<Path>>(oldpath: P, newpath: P, flags: u32) -> io::Result<()> { | ||
| renameat2(AT_FDCWD, oldpath, AT_FDCWD, newpath, flags) | ||
| } | ||
|
|
||
| fn path_to_cstring(path: &Path) -> io::Result<CString> { | ||
| CString::new(path.as_os_str().as_bytes()) | ||
| .map_err(|_| io::Error::new(io::ErrorKind::InvalidInput, "path contained null byte")) | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use super::*; | ||
| use std::fs; | ||
| use tempfile::TempDir; | ||
|
|
||
| #[test] | ||
| fn test_rename_noreplace() { | ||
| let tmp = TempDir::new().unwrap(); | ||
| let old = tmp.path().join("old"); | ||
| let new = tmp.path().join("new"); | ||
|
|
||
| fs::write(&old, "source").unwrap(); | ||
| fs::write(&new, "destination").unwrap(); | ||
|
|
||
| let err = renameat2_cwd(&old, &new, RENAME_NOREPLACE).unwrap_err(); | ||
| assert_eq!(err.raw_os_error(), Some(libc::EEXIST)); | ||
|
|
||
| assert_eq!(fs::read_to_string(&new).unwrap(), "destination"); | ||
| assert_eq!(fs::read_to_string(&old).unwrap(), "source"); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_rename_exchange() { | ||
| let tmp = TempDir::new().unwrap(); | ||
| let path1 = tmp.path().join("a"); | ||
| let path2 = tmp.path().join("b"); | ||
|
|
||
| fs::write(&path1, "content1").unwrap(); | ||
| fs::write(&path2, "content2").unwrap(); | ||
|
|
||
| renameat2_cwd(&path1, &path2, RENAME_EXCHANGE).unwrap(); | ||
|
|
||
| assert_eq!(fs::read_to_string(&path1).unwrap(), "content2"); | ||
| assert_eq!(fs::read_to_string(&path2).unwrap(), "content1"); | ||
| } | ||
| } | ||
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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.