Skip to content

Commit 3554102

Browse files
authored
feat: AT_EMPTY_PATH for linkat() (#2284)
* feat: AT_EMPTY_PATH for linkat() * try fixing import * fix changelog name
1 parent f55dee9 commit 3554102

File tree

4 files changed

+30
-18
lines changed

4 files changed

+30
-18
lines changed

changelog/2284.added.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Enable the `AT_EMPTY_PATH` flag for the `linkat()` function

changelog/2284.removed.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
The `LinkatFlags` type has been deprecated, please use `AtFlags` instead.

src/unistd.rs

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1253,19 +1253,24 @@ pub fn isatty(fd: RawFd) -> Result<bool> {
12531253
}
12541254
}
12551255

1256-
/// Flags for `linkat` function.
1257-
#[derive(Clone, Copy, Debug)]
1258-
pub enum LinkatFlags {
1259-
SymlinkFollow,
1260-
NoSymlinkFollow,
1256+
#[cfg(not(target_os = "redox"))]
1257+
pub type LinkatFlags = AtFlags;
1258+
#[cfg(not(target_os = "redox"))]
1259+
impl LinkatFlags {
1260+
#[deprecated(since = "0.28.0", note = "The variant is deprecated, please use `AtFlags` instead")]
1261+
#[allow(non_upper_case_globals)]
1262+
pub const SymlinkFollow: LinkatFlags = LinkatFlags::AT_SYMLINK_FOLLOW;
1263+
#[deprecated(since = "0.28.0", note = "The variant is deprecated, please use `AtFlags` instead")]
1264+
#[allow(non_upper_case_globals)]
1265+
pub const NoSymlinkFollow: LinkatFlags = LinkatFlags::empty();
12611266
}
12621267

12631268
/// Link one file to another file
12641269
///
12651270
/// Creates a new link (directory entry) at `newpath` for the existing file at `oldpath`. In the
12661271
/// case of a relative `oldpath`, the path is interpreted relative to the directory associated
12671272
/// with file descriptor `olddirfd` instead of the current working directory and similiarly for
1268-
/// `newpath` and file descriptor `newdirfd`. In case `flag` is LinkatFlags::SymlinkFollow and
1273+
/// `newpath` and file descriptor `newdirfd`. In case `flag` is `AtFlags::AT_SYMLINK_FOLLOW` and
12691274
/// `oldpath` names a symoblic link, a new link for the target of the symbolic link is created.
12701275
/// If either `olddirfd` or `newdirfd` is `None`, `AT_FDCWD` is used respectively where `oldpath`
12711276
/// and/or `newpath` is then interpreted relative to the current working directory of the calling
@@ -1279,21 +1284,16 @@ pub fn linkat<P: ?Sized + NixPath>(
12791284
oldpath: &P,
12801285
newdirfd: Option<RawFd>,
12811286
newpath: &P,
1282-
flag: LinkatFlags,
1287+
flag: AtFlags,
12831288
) -> Result<()> {
1284-
let atflag = match flag {
1285-
LinkatFlags::SymlinkFollow => AtFlags::AT_SYMLINK_FOLLOW,
1286-
LinkatFlags::NoSymlinkFollow => AtFlags::empty(),
1287-
};
1288-
12891289
let res = oldpath.with_nix_path(|oldcstr| {
12901290
newpath.with_nix_path(|newcstr| unsafe {
12911291
libc::linkat(
12921292
at_rawfd(olddirfd),
12931293
oldcstr.as_ptr(),
12941294
at_rawfd(newdirfd),
12951295
newcstr.as_ptr(),
1296-
atflag.bits() as libc::c_int,
1296+
flag.bits(),
12971297
)
12981298
})
12991299
})??;

test/test_unistd.rs

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -897,6 +897,8 @@ fn test_symlinkat() {
897897
#[test]
898898
#[cfg(not(any(target_os = "redox", target_os = "haiku")))]
899899
fn test_linkat_file() {
900+
use nix::fcntl::AtFlags;
901+
900902
let tempdir = tempdir().unwrap();
901903
let oldfilename = "foo.txt";
902904
let oldfilepath = tempdir.path().join(oldfilename);
@@ -918,7 +920,7 @@ fn test_linkat_file() {
918920
oldfilename,
919921
Some(dirfd),
920922
newfilename,
921-
LinkatFlags::SymlinkFollow,
923+
AtFlags::AT_SYMLINK_FOLLOW,
922924
)
923925
.unwrap();
924926
assert!(newfilepath.exists());
@@ -927,6 +929,8 @@ fn test_linkat_file() {
927929
#[test]
928930
#[cfg(not(any(target_os = "redox", target_os = "haiku")))]
929931
fn test_linkat_olddirfd_none() {
932+
use nix::fcntl::AtFlags;
933+
930934
let _dr = crate::DirRestore::new();
931935

932936
let tempdir_oldfile = tempdir().unwrap();
@@ -955,7 +959,7 @@ fn test_linkat_olddirfd_none() {
955959
oldfilename,
956960
Some(dirfd),
957961
newfilename,
958-
LinkatFlags::SymlinkFollow,
962+
AtFlags::AT_SYMLINK_FOLLOW,
959963
)
960964
.unwrap();
961965
assert!(newfilepath.exists());
@@ -964,6 +968,8 @@ fn test_linkat_olddirfd_none() {
964968
#[test]
965969
#[cfg(not(any(target_os = "redox", target_os = "haiku")))]
966970
fn test_linkat_newdirfd_none() {
971+
use nix::fcntl::AtFlags;
972+
967973
let _dr = crate::DirRestore::new();
968974

969975
let tempdir_oldfile = tempdir().unwrap();
@@ -992,7 +998,7 @@ fn test_linkat_newdirfd_none() {
992998
oldfilename,
993999
None,
9941000
newfilename,
995-
LinkatFlags::SymlinkFollow,
1001+
AtFlags::AT_SYMLINK_FOLLOW,
9961002
)
9971003
.unwrap();
9981004
assert!(newfilepath.exists());
@@ -1001,6 +1007,8 @@ fn test_linkat_newdirfd_none() {
10011007
#[test]
10021008
#[cfg(not(any(apple_targets, target_os = "redox", target_os = "haiku")))]
10031009
fn test_linkat_no_follow_symlink() {
1010+
use nix::fcntl::AtFlags;
1011+
10041012
let _m = crate::CWD_LOCK.read();
10051013

10061014
let tempdir = tempdir().unwrap();
@@ -1030,7 +1038,7 @@ fn test_linkat_no_follow_symlink() {
10301038
symoldfilename,
10311039
Some(dirfd),
10321040
newfilename,
1033-
LinkatFlags::NoSymlinkFollow,
1041+
AtFlags::empty(),
10341042
)
10351043
.unwrap();
10361044

@@ -1044,6 +1052,8 @@ fn test_linkat_no_follow_symlink() {
10441052
#[test]
10451053
#[cfg(not(any(target_os = "redox", target_os = "haiku")))]
10461054
fn test_linkat_follow_symlink() {
1055+
use nix::fcntl::AtFlags;
1056+
10471057
let _m = crate::CWD_LOCK.read();
10481058

10491059
let tempdir = tempdir().unwrap();
@@ -1073,7 +1083,7 @@ fn test_linkat_follow_symlink() {
10731083
symoldfilename,
10741084
Some(dirfd),
10751085
newfilename,
1076-
LinkatFlags::SymlinkFollow,
1086+
AtFlags::AT_SYMLINK_FOLLOW,
10771087
)
10781088
.unwrap();
10791089

0 commit comments

Comments
 (0)