Skip to content

Commit 99458d8

Browse files
feat(redox): renameat and renameat_with (#1586)
* misc(Cargo.toml): bump `libc` to 0.2.182 Signed-off-by: Anhad Singh <andypython@protonmail.com> * feat(redox): `renameat` Signed-off-by: Anhad Singh <andypython@protonmail.com> * feat(redox): `renameat2` and `renameat_with` Signed-off-by: Anhad Singh <andypython@protonmail.com> --------- Signed-off-by: Anhad Singh <andypython@protonmail.com>
1 parent a9c8dcb commit 99458d8

File tree

6 files changed

+74
-26
lines changed

6 files changed

+74
-26
lines changed

Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,15 +35,15 @@ rustc-std-workspace-alloc = { version = "1.0.0", optional = true } # not aliased
3535
[target.'cfg(all(not(rustix_use_libc), not(miri), target_os = "linux", any(target_endian = "little", any(target_arch = "s390x", target_arch = "powerpc")), any(target_arch = "arm", all(target_arch = "aarch64", target_pointer_width = "64"), target_arch = "riscv64", all(rustix_use_experimental_asm, target_arch = "powerpc"), all(rustix_use_experimental_asm, target_arch = "powerpc64"), all(rustix_use_experimental_asm, target_arch = "s390x"), all(rustix_use_experimental_asm, target_arch = "mips"), all(rustix_use_experimental_asm, target_arch = "mips32r6"), all(rustix_use_experimental_asm, target_arch = "mips64"), all(rustix_use_experimental_asm, target_arch = "mips64r6"), target_arch = "x86", all(target_arch = "x86_64", target_pointer_width = "64"))))'.dependencies]
3636
linux-raw-sys = { version = "0.11.0", default-features = false, features = ["auxvec", "general", "errno", "ioctl", "no_std", "elf"] }
3737
libc_errno = { package = "errno", version = "0.3.10", default-features = false, optional = true }
38-
libc = { version = "0.2.181", default-features = false, optional = true }
38+
libc = { version = "0.2.182", default-features = false, optional = true }
3939

4040
# Dependencies for platforms where only libc is supported:
4141
#
4242
# On all other Unix-family platforms, and under Miri, we always use the libc
4343
# backend, so enable its dependencies unconditionally.
4444
[target.'cfg(all(not(windows), any(rustix_use_libc, miri, not(all(target_os = "linux", any(target_endian = "little", any(target_arch = "s390x", target_arch = "powerpc")), any(target_arch = "arm", all(target_arch = "aarch64", target_pointer_width = "64"), target_arch = "riscv64", all(rustix_use_experimental_asm, target_arch = "powerpc"), all(rustix_use_experimental_asm, target_arch = "powerpc64"), all(rustix_use_experimental_asm, target_arch = "s390x"), all(rustix_use_experimental_asm, target_arch = "mips"), all(rustix_use_experimental_asm, target_arch = "mips32r6"), all(rustix_use_experimental_asm, target_arch = "mips64"), all(rustix_use_experimental_asm, target_arch = "mips64r6"), target_arch = "x86", all(target_arch = "x86_64", target_pointer_width = "64")))))))'.dependencies]
4545
libc_errno = { package = "errno", version = "0.3.10", default-features = false }
46-
libc = { version = "0.2.181", default-features = false }
46+
libc = { version = "0.2.182", default-features = false }
4747

4848
# Additional dependencies for Linux and Android with the libc backend:
4949
#

src/backend/libc/fs/syscalls.rs

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ use crate::fs::FallocateFlags;
3333
use crate::fs::FlockOperation;
3434
#[cfg(any(linux_kernel, target_os = "freebsd"))]
3535
use crate::fs::MemfdFlags;
36-
#[cfg(any(linux_kernel, apple))]
36+
#[cfg(any(linux_kernel, apple, target_os = "redox"))]
3737
use crate::fs::RenameFlags;
3838
#[cfg(any(linux_kernel, target_os = "freebsd", target_os = "fuchsia"))]
3939
use crate::fs::SealFlags;
@@ -448,7 +448,6 @@ pub(crate) fn rename(old_path: &CStr, new_path: &CStr) -> io::Result<()> {
448448
unsafe { ret(c::rename(c_str(old_path), c_str(new_path))) }
449449
}
450450

451-
#[cfg(not(target_os = "redox"))]
452451
pub(crate) fn renameat(
453452
old_dirfd: BorrowedFd<'_>,
454453
old_path: &CStr,
@@ -493,6 +492,25 @@ pub(crate) fn renameat(
493492
}
494493
}
495494

495+
#[cfg(target_os = "redox")]
496+
pub(crate) fn renameat2(
497+
old_dirfd: BorrowedFd<'_>,
498+
old_path: &CStr,
499+
new_dirfd: BorrowedFd<'_>,
500+
new_path: &CStr,
501+
flags: RenameFlags,
502+
) -> io::Result<()> {
503+
unsafe {
504+
ret(c::renameat2(
505+
borrowed_fd(old_dirfd),
506+
c_str(old_path),
507+
borrowed_fd(new_dirfd),
508+
c_str(new_path),
509+
flags.bits(),
510+
))
511+
}
512+
}
513+
496514
#[cfg(all(target_os = "linux", target_env = "gnu"))]
497515
pub(crate) fn renameat2(
498516
old_dirfd: BorrowedFd<'_>,

src/backend/libc/fs/types.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -508,6 +508,22 @@ bitflags! {
508508
}
509509
}
510510

511+
#[cfg(target_os = "redox")]
512+
bitflags! {
513+
/// `RENAME_*` constants for use with [`renameat_with`].
514+
///
515+
/// [`renameat_with`]: crate::fs::renameat_with
516+
#[repr(transparent)]
517+
#[derive(Copy, Clone, Eq, PartialEq, Hash, Debug)]
518+
pub struct RenameFlags: ffi::c_uint {
519+
/// `RENAME_NOREPLACE`
520+
const NOREPLACE = bitcast!(c::RENAME_NOREPLACE);
521+
522+
/// <https://docs.rs/bitflags/*/bitflags/#externally-defined-flags>
523+
const _ = !0;
524+
}
525+
}
526+
511527
#[cfg(apple)]
512528
bitflags! {
513529
/// `RENAME_*` constants for use with [`renameat_with`].

src/fs/at.rs

Lines changed: 30 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,11 @@ use crate::buffer::Buffer;
1212
use crate::fd::OwnedFd;
1313
#[cfg(not(any(target_os = "espidf", target_os = "horizon", target_os = "vita")))]
1414
use crate::fs::Access;
15-
#[cfg(not(target_os = "espidf"))]
15+
#[cfg(not(any(target_os = "espidf", target_os = "redox")))]
1616
use crate::fs::AtFlags;
1717
#[cfg(apple)]
1818
use crate::fs::CloneFlags;
19-
#[cfg(any(linux_kernel, apple))]
19+
#[cfg(any(linux_kernel, apple, target_os = "redox"))]
2020
use crate::fs::RenameFlags;
2121
#[cfg(not(target_os = "espidf"))]
2222
use crate::fs::Stat;
@@ -73,6 +73,7 @@ pub const UTIME_OMIT: Nsecs = backend::c::UTIME_OMIT as Nsecs;
7373
///
7474
/// [POSIX]: https://pubs.opengroup.org/onlinepubs/9799919799/functions/openat.html
7575
/// [Linux]: https://man7.org/linux/man-pages/man2/openat.2.html
76+
#[cfg(not(target_os = "redox"))]
7677
#[inline]
7778
pub fn openat<P: path::Arg, Fd: AsFd>(
7879
dirfd: Fd,
@@ -95,7 +96,7 @@ pub fn openat<P: path::Arg, Fd: AsFd>(
9596
///
9697
/// [POSIX]: https://pubs.opengroup.org/onlinepubs/9799919799/functions/readlinkat.html
9798
/// [Linux]: https://man7.org/linux/man-pages/man2/readlinkat.2.html
98-
#[cfg(feature = "alloc")]
99+
#[cfg(all(feature = "alloc", not(target_os = "redox")))]
99100
#[cfg_attr(docsrs, doc(cfg(feature = "alloc")))]
100101
#[inline]
101102
pub fn readlinkat<P: path::Arg, Fd: AsFd, B: Into<Vec<u8>>>(
@@ -106,7 +107,7 @@ pub fn readlinkat<P: path::Arg, Fd: AsFd, B: Into<Vec<u8>>>(
106107
path.into_with_c_str(|path| _readlinkat(dirfd.as_fd(), path, reuse.into()))
107108
}
108109

109-
#[cfg(feature = "alloc")]
110+
#[cfg(all(feature = "alloc", not(target_os = "redox")))]
110111
#[allow(unsafe_code)]
111112
fn _readlinkat(dirfd: BorrowedFd<'_>, path: &CStr, mut buffer: Vec<u8>) -> io::Result<CString> {
112113
buffer.clear();
@@ -170,6 +171,7 @@ fn _readlinkat(dirfd: BorrowedFd<'_>, path: &CStr, mut buffer: Vec<u8>) -> io::R
170171
///
171172
/// [POSIX]: https://pubs.opengroup.org/onlinepubs/9799919799/functions/readlinkat.html
172173
/// [Linux]: https://man7.org/linux/man-pages/man2/readlinkat.2.html
174+
#[cfg(not(target_os = "redox"))]
173175
#[inline]
174176
pub fn readlinkat_raw<P: path::Arg, Fd: AsFd, Buf: Buffer<u8>>(
175177
dirfd: Fd,
@@ -192,6 +194,7 @@ pub fn readlinkat_raw<P: path::Arg, Fd: AsFd, Buf: Buffer<u8>>(
192194
///
193195
/// [POSIX]: https://pubs.opengroup.org/onlinepubs/9799919799/functions/mkdirat.html
194196
/// [Linux]: https://man7.org/linux/man-pages/man2/mkdirat.2.html
197+
#[cfg(not(target_os = "redox"))]
195198
#[inline]
196199
pub fn mkdirat<P: path::Arg, Fd: AsFd>(dirfd: Fd, path: P, mode: Mode) -> io::Result<()> {
197200
path.into_with_c_str(|path| backend::fs::syscalls::mkdirat(dirfd.as_fd(), path, mode))
@@ -206,7 +209,7 @@ pub fn mkdirat<P: path::Arg, Fd: AsFd>(dirfd: Fd, path: P, mode: Mode) -> io::Re
206209
///
207210
/// [POSIX]: https://pubs.opengroup.org/onlinepubs/9799919799/functions/linkat.html
208211
/// [Linux]: https://man7.org/linux/man-pages/man2/linkat.2.html
209-
#[cfg(not(target_os = "espidf"))]
212+
#[cfg(not(any(target_os = "espidf", target_os = "redox")))]
210213
#[inline]
211214
pub fn linkat<P: path::Arg, Q: path::Arg, PFd: AsFd, QFd: AsFd>(
212215
old_dirfd: PFd,
@@ -240,7 +243,7 @@ pub fn linkat<P: path::Arg, Q: path::Arg, PFd: AsFd, QFd: AsFd>(
240243
/// [`REMOVEDIR`]: AtFlags::REMOVEDIR
241244
/// [POSIX]: https://pubs.opengroup.org/onlinepubs/9799919799/functions/unlinkat.html
242245
/// [Linux]: https://man7.org/linux/man-pages/man2/unlinkat.2.html
243-
#[cfg(not(target_os = "espidf"))]
246+
#[cfg(not(any(target_os = "espidf", target_os = "redox")))]
244247
#[inline]
245248
pub fn unlinkat<P: path::Arg, Fd: AsFd>(dirfd: Fd, path: P, flags: AtFlags) -> io::Result<()> {
246249
path.into_with_c_str(|path| backend::fs::syscalls::unlinkat(dirfd.as_fd(), path, flags))
@@ -286,7 +289,7 @@ pub fn renameat<P: path::Arg, Q: path::Arg, PFd: AsFd, QFd: AsFd>(
286289
/// - [Linux]
287290
///
288291
/// [Linux]: https://man7.org/linux/man-pages/man2/renameat2.2.html
289-
#[cfg(any(apple, linux_kernel))]
292+
#[cfg(any(apple, linux_kernel, target_os = "redox"))]
290293
#[inline]
291294
#[doc(alias = "renameat2")]
292295
#[doc(alias = "renameatx_np")]
@@ -318,6 +321,7 @@ pub fn renameat_with<P: path::Arg, Q: path::Arg, PFd: AsFd, QFd: AsFd>(
318321
///
319322
/// [POSIX]: https://pubs.opengroup.org/onlinepubs/9799919799/functions/symlinkat.html
320323
/// [Linux]: https://man7.org/linux/man-pages/man2/symlinkat.2.html
324+
#[cfg(not(target_os = "redox"))]
321325
#[inline]
322326
pub fn symlinkat<P: path::Arg, Q: path::Arg, Fd: AsFd>(
323327
old_path: P,
@@ -344,7 +348,7 @@ pub fn symlinkat<P: path::Arg, Q: path::Arg, Fd: AsFd>(
344348
/// [Linux]: https://man7.org/linux/man-pages/man2/fstatat.2.html
345349
/// [`Mode::from_raw_mode`]: crate::fs::Mode::from_raw_mode
346350
/// [`FileType::from_raw_mode`]: crate::fs::FileType::from_raw_mode
347-
#[cfg(not(target_os = "espidf"))]
351+
#[cfg(not(any(target_os = "espidf", target_os = "redox")))]
348352
#[inline]
349353
#[doc(alias = "fstatat")]
350354
pub fn statat<P: path::Arg, Fd: AsFd>(dirfd: Fd, path: P, flags: AtFlags) -> io::Result<Stat> {
@@ -367,7 +371,12 @@ pub fn statat<P: path::Arg, Fd: AsFd>(dirfd: Fd, path: P, flags: AtFlags) -> io:
367371
///
368372
/// [POSIX]: https://pubs.opengroup.org/onlinepubs/9799919799/functions/faccessat.html
369373
/// [Linux]: https://man7.org/linux/man-pages/man2/faccessat.2.html
370-
#[cfg(not(any(target_os = "espidf", target_os = "horizon", target_os = "vita")))]
374+
#[cfg(not(any(
375+
target_os = "espidf",
376+
target_os = "horizon",
377+
target_os = "vita",
378+
target_os = "redox"
379+
)))]
371380
#[inline]
372381
#[doc(alias = "faccessat")]
373382
pub fn accessat<P: path::Arg, Fd: AsFd>(
@@ -387,7 +396,12 @@ pub fn accessat<P: path::Arg, Fd: AsFd>(
387396
///
388397
/// [POSIX]: https://pubs.opengroup.org/onlinepubs/9799919799/functions/utimensat.html
389398
/// [Linux]: https://man7.org/linux/man-pages/man2/utimensat.2.html
390-
#[cfg(not(any(target_os = "espidf", target_os = "horizon", target_os = "vita")))]
399+
#[cfg(not(any(
400+
target_os = "espidf",
401+
target_os = "horizon",
402+
target_os = "vita",
403+
target_os = "redox"
404+
)))]
391405
#[inline]
392406
pub fn utimensat<P: path::Arg, Fd: AsFd>(
393407
dirfd: Fd,
@@ -410,7 +424,7 @@ pub fn utimensat<P: path::Arg, Fd: AsFd>(
410424
///
411425
/// [POSIX]: https://pubs.opengroup.org/onlinepubs/9799919799/functions/fchmodat.html
412426
/// [Linux]: https://man7.org/linux/man-pages/man2/fchmodat.2.html
413-
#[cfg(not(any(target_os = "espidf", target_os = "wasi")))]
427+
#[cfg(not(any(target_os = "espidf", target_os = "wasi", target_os = "redox")))]
414428
#[inline]
415429
#[doc(alias = "fchmodat")]
416430
pub fn chmodat<P: path::Arg, Fd: AsFd>(
@@ -454,7 +468,8 @@ pub fn fclonefileat<Fd: AsFd, DstFd: AsFd, P: path::Arg>(
454468
target_os = "espidf",
455469
target_os = "horizon",
456470
target_os = "vita",
457-
target_os = "wasi"
471+
target_os = "wasi",
472+
target_os = "redox",
458473
)))]
459474
#[inline]
460475
pub fn mknodat<P: path::Arg, Fd: AsFd>(
@@ -480,7 +495,8 @@ pub fn mknodat<P: path::Arg, Fd: AsFd>(
480495
target_os = "espidf",
481496
target_os = "horizon",
482497
target_os = "vita",
483-
target_os = "wasi"
498+
target_os = "wasi",
499+
target_os = "redox",
484500
)))]
485501
#[inline]
486502
pub fn mkfifoat<P: path::Arg, Fd: AsFd>(dirfd: Fd, path: P, mode: Mode) -> io::Result<()> {
@@ -496,7 +512,7 @@ pub fn mkfifoat<P: path::Arg, Fd: AsFd>(dirfd: Fd, path: P, mode: Mode) -> io::R
496512
///
497513
/// [POSIX]: https://pubs.opengroup.org/onlinepubs/9799919799/functions/fchownat.html
498514
/// [Linux]: https://man7.org/linux/man-pages/man2/fchownat.2.html
499-
#[cfg(not(any(target_os = "espidf", target_os = "wasi")))]
515+
#[cfg(not(any(target_os = "espidf", target_os = "wasi", target_os = "redox")))]
500516
#[inline]
501517
#[doc(alias = "fchownat")]
502518
pub fn chownat<P: path::Arg, Fd: AsFd>(

src/fs/mod.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
//! Filesystem operations.
22
33
mod abs;
4-
#[cfg(not(target_os = "redox"))]
54
mod at;
65
mod constants;
76
#[cfg(linux_kernel)]
@@ -51,7 +50,7 @@ mod raw_dir;
5150
mod seek_from;
5251
#[cfg(target_os = "linux")]
5352
mod sendfile;
54-
#[cfg(not(any(target_os = "espidf", target_os = "redox")))]
53+
#[cfg(not(target_os = "espidf"))]
5554
mod special;
5655
#[cfg(linux_kernel)]
5756
mod statx;
@@ -67,7 +66,6 @@ mod sync;
6766
mod xattr;
6867

6968
pub use abs::*;
70-
#[cfg(not(target_os = "redox"))]
7169
pub use at::*;
7270
pub use constants::*;
7371
#[cfg(linux_kernel)]
@@ -115,7 +113,7 @@ pub use raw_dir::{RawDir, RawDirEntry};
115113
pub use seek_from::SeekFrom;
116114
#[cfg(target_os = "linux")]
117115
pub use sendfile::sendfile;
118-
#[cfg(not(any(target_os = "espidf", target_os = "redox")))]
116+
#[cfg(not(target_os = "espidf"))]
119117
pub use special::*;
120118
#[cfg(linux_kernel)]
121119
pub use statx::*;

tests/fs/renameat.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ fn same(a: &Stat, b: &Stat) -> bool {
44
a.st_ino == b.st_ino && a.st_dev == b.st_dev
55
}
66

7-
#[cfg(linux_kernel)]
7+
#[cfg(any(linux_kernel, target_os = "redox"))]
88
const DIR_OPEN_FLAGS: OFlags = OFlags::RDONLY.union(OFlags::PATH);
99
#[cfg(apple)]
1010
const DIR_OPEN_FLAGS: OFlags = OFlags::RDONLY;
@@ -34,7 +34,7 @@ fn test_rename() {
3434
access(tmp.path().join("bar"), Access::EXISTS).unwrap();
3535
}
3636

37-
#[cfg(any(linux_kernel, apple))]
37+
#[cfg(any(linux_kernel, apple, target_os = "redox"))]
3838
#[test]
3939
fn test_renameat() {
4040
use rustix::fs::{accessat, openat, renameat, statat, Access, AtFlags, Mode, CWD};
@@ -58,7 +58,7 @@ fn test_renameat() {
5858

5959
/// Like `test_renameat` but the file already exists, so `renameat`
6060
/// overwrites it.
61-
#[cfg(any(linux_kernel, apple))]
61+
#[cfg(any(linux_kernel, apple, target_os = "redox"))]
6262
#[test]
6363
fn test_renameat_overwrite() {
6464
use rustix::fs::{openat, renameat, statat, AtFlags, Mode, CWD};
@@ -74,7 +74,7 @@ fn test_renameat_overwrite() {
7474
assert!(same(&before, &renamed));
7575
}
7676

77-
#[cfg(any(linux_kernel, apple))]
77+
#[cfg(any(linux_kernel, apple, target_os = "redox"))]
7878
#[test]
7979
fn test_renameat_with() {
8080
use rustix::fs::{openat, renameat_with, statat, AtFlags, Mode, RenameFlags, CWD};

0 commit comments

Comments
 (0)