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

extend LinkatFlags to allow passing AT_EMPTY_PATH #1895

Closed
wants to merge 2 commits into from
Closed
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
23 changes: 10 additions & 13 deletions src/unistd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1228,11 +1228,14 @@ pub fn isatty(fd: RawFd) -> Result<bool> {
}
}

/// Flags for `linkat` function.
#[derive(Clone, Copy, Debug)]
pub enum LinkatFlags {
SymlinkFollow,
NoSymlinkFollow,
libc_bitflags! {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This type itself should be #[cfg(not(target_os = "redox"))] as the file system on redox does not support symlink

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, I am not sure if we should define a new type or just use the AtFlags type.

It is safer to define a new type so that we can prevent users from passing invalid arguments, but this will add our maintainence burgen. We are already using AtFlags type for other functions like fstatat, cc @asomers, what do you think?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Link Add EmptyPath for FchownatFlags as these 2 PRs are similar

/// Flags for `linkat` function.
pub struct LinkatFlags: c_int {
#[cfg(not(target_os = "redox"))]
AT_SYMLINK_FOLLOW;
#[cfg(any(target_os = "android", target_os = "linux"))]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
#[cfg(any(target_os = "android", target_os = "linux"))]
#[cfg(linux_android)]

AT_EMPTY_PATH;
}
}

/// Link one file to another file
Expand All @@ -1254,15 +1257,9 @@ pub fn linkat<P: ?Sized + NixPath>(
oldpath: &P,
newdirfd: Option<RawFd>,
newpath: &P,
flag: LinkatFlags,
flags: LinkatFlags,
) -> Result<()> {

let atflag =
match flag {
LinkatFlags::SymlinkFollow => AtFlags::AT_SYMLINK_FOLLOW,
LinkatFlags::NoSymlinkFollow => AtFlags::empty(),
};

let res =
oldpath.with_nix_path(|oldcstr| {
newpath.with_nix_path(|newcstr| {
Expand All @@ -1272,7 +1269,7 @@ pub fn linkat<P: ?Sized + NixPath>(
oldcstr.as_ptr(),
at_rawfd(newdirfd),
newcstr.as_ptr(),
atflag.bits() as libc::c_int
flags.bits()
)
}
})
Expand Down
10 changes: 5 additions & 5 deletions test/test_unistd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -916,7 +916,7 @@ fn test_linkat_file() {
oldfilename,
Some(dirfd),
newfilename,
LinkatFlags::SymlinkFollow,
LinkatFlags::AT_SYMLINK_FOLLOW,
)
.unwrap();
assert!(newfilepath.exists());
Expand Down Expand Up @@ -953,7 +953,7 @@ fn test_linkat_olddirfd_none() {
oldfilename,
Some(dirfd),
newfilename,
LinkatFlags::SymlinkFollow,
LinkatFlags::AT_SYMLINK_FOLLOW,
)
.unwrap();
assert!(newfilepath.exists());
Expand Down Expand Up @@ -990,7 +990,7 @@ fn test_linkat_newdirfd_none() {
oldfilename,
None,
newfilename,
LinkatFlags::SymlinkFollow,
LinkatFlags::AT_SYMLINK_FOLLOW,
)
.unwrap();
assert!(newfilepath.exists());
Expand Down Expand Up @@ -1033,7 +1033,7 @@ fn test_linkat_no_follow_symlink() {
symoldfilename,
Some(dirfd),
newfilename,
LinkatFlags::NoSymlinkFollow,
LinkatFlags::empty(),
)
.unwrap();

Expand Down Expand Up @@ -1076,7 +1076,7 @@ fn test_linkat_follow_symlink() {
symoldfilename,
Some(dirfd),
newfilename,
LinkatFlags::SymlinkFollow,
LinkatFlags::AT_SYMLINK_FOLLOW,
)
.unwrap();

Expand Down