Skip to content
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

unistd: allow linkat to take multiple path types #2582

Merged
merged 1 commit into from
Jan 14, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog/2582.changed.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
linkat: allow distinct types for path arguments
6 changes: 3 additions & 3 deletions src/unistd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1577,11 +1577,11 @@ impl LinkatFlags {
/// # References
/// See also [linkat(2)](https://pubs.opengroup.org/onlinepubs/9699919799/functions/linkat.html)
#[cfg(not(target_os = "redox"))] // Redox does not have this yet
pub fn linkat<Fd1: std::os::fd::AsFd, Fd2: std::os::fd::AsFd, P: ?Sized + NixPath>(
pub fn linkat<Fd1: std::os::fd::AsFd, Fd2: std::os::fd::AsFd, P1: ?Sized + NixPath, P2: ?Sized + NixPath>(
olddirfd: Fd1,
oldpath: &P,
oldpath: &P1,
newdirfd: Fd2,
newpath: &P,
newpath: &P2,
flag: AtFlags,
) -> Result<()> {
use std::os::fd::AsRawFd;
Expand Down
34 changes: 34 additions & 0 deletions test/test_unistd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -927,6 +927,40 @@ fn test_linkat_file() {
assert!(newfilepath.exists());
}

#[test]
#[cfg(not(any(target_os = "redox", target_os = "haiku")))]
/// This test is the same as [test_linkat_file], but ensures that two different types can be used
/// as the path arguments.
fn test_linkat_pathtypes() {
use nix::fcntl::AtFlags;

let tempdir = tempdir().unwrap();
let oldfilename = "foo.txt";
let oldfilepath = tempdir.path().join(oldfilename);

let newfilename = "bar.txt";
let newfilepath = tempdir.path().join(newfilename);

// Create file
File::create(oldfilepath).unwrap();

// Get file descriptor for base directory
let dirfd =
fcntl::open(tempdir.path(), fcntl::OFlag::empty(), stat::Mode::empty())
.unwrap();

// Attempt hard link file at relative path
linkat(
&dirfd,
PathBuf::from(oldfilename).as_path(),
&dirfd,
newfilename,
AtFlags::AT_SYMLINK_FOLLOW,
)
.unwrap();
assert!(newfilepath.exists());
}

#[test]
#[cfg(not(any(target_os = "redox", target_os = "haiku")))]
fn test_linkat_olddirfd_none() {
Expand Down
Loading