-
Notifications
You must be signed in to change notification settings - Fork 164
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a
rustix::fs::ABS
constant. (#1189)
Add a `rustix::fs::ABS` constant, which corresponds to the undocumented but commonly used convention of using `-EBADF` as the file descriptor in `openat` and similar calls. This makes them fail if the path is not absolute. Fixes #1187.
- Loading branch information
1 parent
c004964
commit ec78479
Showing
11 changed files
with
145 additions
and
40 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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 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 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,73 @@ | ||
//! The `CWD` and `ABS` constants, representing the current working directory | ||
//! and absolute-only paths, respectively. | ||
//! | ||
//! # Safety | ||
//! | ||
//! This file uses `AT_FDCWD`, which is a raw file descriptor, but which is | ||
//! always valid, and `-EBADF`, which is an undocumented by commonly used | ||
//! convention of passing a value which will always fail if the accompanying | ||
//! path isn't absolute. | ||
#![allow(unsafe_code)] | ||
|
||
use crate::backend; | ||
use backend::c; | ||
use backend::fd::{BorrowedFd, RawFd}; | ||
|
||
/// `AT_FDCWD`—A handle representing the current working directory. | ||
/// | ||
/// This is a file descriptor which refers to the process current directory | ||
/// which can be used as the directory argument in `*at` functions such as | ||
/// [`openat`]. | ||
/// | ||
/// # References | ||
/// - [POSIX] | ||
/// | ||
/// [`openat`]: crate::fs::openat | ||
/// [POSIX]: https://pubs.opengroup.org/onlinepubs/9799919799/basedefs/fcntl.h.html | ||
// SAFETY: `AT_FDCWD` is a reserved value that is never dynamically | ||
// allocated, so it'll remain valid for the duration of `'static`. | ||
#[doc(alias = "AT_FDCWD")] | ||
pub const CWD: BorrowedFd<'static> = | ||
unsafe { BorrowedFd::<'static>::borrow_raw(c::AT_FDCWD as RawFd) }; | ||
|
||
/// `-EBADF`—A handle that requires paths to be absolute. | ||
/// | ||
/// This is a file descriptor which refers to no directory, which can be used | ||
/// as the directory argument in `*at` functions such as [`openat`], which | ||
/// causes them to fail with [`BADF`] if the accompanying path is not absolute. | ||
/// | ||
/// This corresponds to the undocumented by commonly used convention of | ||
/// passing `-EBADF` as the `dirfd` argument, which is ignored if the path | ||
/// is absolute, and evokes an `EBADF` error otherwise. | ||
/// | ||
/// [`openat`]: crate::fs::openat | ||
/// [`BADF`]: crate::io::Errno::BADF | ||
// SAFETY: This `-EBADF` convention is commonly used, such as in lxc, so OS's | ||
// aren't going to break it. | ||
pub const ABS: BorrowedFd<'static> = | ||
unsafe { BorrowedFd::<'static>::borrow_raw(c::EBADF.wrapping_neg() as RawFd) }; | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
use crate::fd::AsRawFd; | ||
|
||
#[test] | ||
fn test_cwd() { | ||
assert!(CWD.as_raw_fd() != -1); | ||
#[cfg(linux_kernel)] | ||
#[cfg(feature = "io_uring")] | ||
assert!(CWD.as_raw_fd() != linux_raw_sys::io_uring::IORING_REGISTER_FILES_SKIP); | ||
} | ||
|
||
#[test] | ||
fn test_abs() { | ||
assert!(ABS.as_raw_fd() < 0); | ||
assert!(ABS.as_raw_fd() != -1); | ||
assert!(ABS.as_raw_fd() != c::AT_FDCWD); | ||
#[cfg(linux_kernel)] | ||
#[cfg(feature = "io_uring")] | ||
assert!(ABS.as_raw_fd() != linux_raw_sys::io_uring::IORING_REGISTER_FILES_SKIP); | ||
} | ||
} |
This file contains 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 was deleted.
Oops, something went wrong.
This file contains 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 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,57 @@ | ||
#[cfg(not(target_os = "wasi"))] | ||
#[test] | ||
fn test_special_fds() { | ||
use rustix::fs::{fstat, open, openat, Mode, OFlags, Stat, ABS, CWD}; | ||
use rustix::process::getcwd; | ||
use std::ffi::OsStr; | ||
use std::os::unix::ffi::OsStrExt; | ||
use std::path::PathBuf; | ||
|
||
let cwd_path = getcwd(Vec::new()).unwrap().into_bytes(); | ||
let cwd_path = OsStr::from_bytes(&cwd_path).to_owned(); | ||
let cwd_path = PathBuf::from(cwd_path); | ||
|
||
// Open the same file several ways using special constants and make sure | ||
// we get the same file. | ||
|
||
// Use plain `open`. | ||
let a = open("Cargo.toml", OFlags::RDONLY, Mode::empty()).unwrap(); | ||
|
||
// Use `CWD` with a relative path. | ||
let b = openat(CWD, "Cargo.toml", OFlags::RDONLY, Mode::empty()).unwrap(); | ||
|
||
// Use `CWD` with an absolute path. | ||
let c = openat( | ||
CWD, | ||
cwd_path.join("Cargo.toml"), | ||
OFlags::RDONLY, | ||
Mode::empty(), | ||
) | ||
.unwrap(); | ||
|
||
// Use `ABS` with an absolute path. | ||
let d = openat( | ||
ABS, | ||
cwd_path.join("Cargo.toml"), | ||
OFlags::RDONLY, | ||
Mode::empty(), | ||
) | ||
.unwrap(); | ||
|
||
// Test that opening a relative path with `ABS` fails. | ||
let err = openat(ABS, "Cargo.toml", OFlags::RDONLY, Mode::empty()).unwrap_err(); | ||
assert_eq!(err, rustix::io::Errno::BADF); | ||
|
||
let a_stat = fstat(a).unwrap(); | ||
let b_stat = fstat(b).unwrap(); | ||
let c_stat = fstat(c).unwrap(); | ||
let d_stat = fstat(d).unwrap(); | ||
|
||
assert!(same(&a_stat, &b_stat)); | ||
assert!(same(&b_stat, &c_stat)); | ||
assert!(same(&c_stat, &d_stat)); | ||
|
||
fn same(a: &Stat, b: &Stat) -> bool { | ||
a.st_ino == b.st_ino && a.st_dev == b.st_dev | ||
} | ||
} |