| 2 | #[cfg(not(any(target_os = "redox", target_os = "wasi")))] |
| 3 | #[test] |
| 4 | fn test_special_fds() { |
| 5 | use rustix::fs::{fstat, open, openat, Mode, OFlags, Stat, ABS, CWD}; |
| 6 | use rustix::process::getcwd; |
| 7 | use std::ffi::OsStr; |
| 8 | use std::os::unix::ffi::OsStrExt as _; |
| 9 | use std::path::PathBuf; |
| 10 | |
| 11 | let cwd_path = getcwd(Vec::new()).unwrap().into_bytes(); |
| 12 | let cwd_path = OsStr::from_bytes(&cwd_path).to_owned(); |
| 13 | let cwd_path = PathBuf::from(cwd_path); |
| 14 | |
| 15 | // Open the same file several ways using special constants and make sure we |
| 16 | // get the same file. |
| 17 | |
| 18 | // Use plain `open`. |
| 19 | let a = open("Cargo.toml", OFlags::RDONLY, Mode::empty()).unwrap(); |
| 20 | |
| 21 | // Use `CWD` with a relative path. |
| 22 | let b = openat(CWD, "Cargo.toml", OFlags::RDONLY, Mode::empty()).unwrap(); |
| 23 | |
| 24 | // Use `CWD` with an absolute path. |
| 25 | let c = openat( |
| 26 | CWD, |
| 27 | cwd_path.join("Cargo.toml"), |
| 28 | OFlags::RDONLY, |
| 29 | Mode::empty(), |
| 30 | ) |
| 31 | .unwrap(); |
| 32 | |
| 33 | // Use `ABS` with an absolute path. |
| 34 | let d = openat( |
| 35 | ABS, |
| 36 | cwd_path.join("Cargo.toml"), |
| 37 | OFlags::RDONLY, |
| 38 | Mode::empty(), |
| 39 | ) |
| 40 | .unwrap(); |
| 41 | |
| 42 | // Test that opening a relative path with `ABS` fails. |
| 43 | let err = openat(ABS, "Cargo.toml", OFlags::RDONLY, Mode::empty()).unwrap_err(); |
| 44 | assert_eq!(err, rustix::io::Errno::BADF); |
| 45 | |
| 46 | let a_stat = fstat(a).unwrap(); |
| 47 | let b_stat = fstat(b).unwrap(); |
| 48 | let c_stat = fstat(c).unwrap(); |
| 49 | let d_stat = fstat(d).unwrap(); |
| 50 | |
| 51 | assert!(same(&a_stat, &b_stat)); |
| 52 | assert!(same(&b_stat, &c_stat)); |
| 53 | assert!(same(&c_stat, &d_stat)); |
| 54 | |
| 55 | fn same(a: &Stat, b: &Stat) -> bool { |
| 56 | a.st_ino == b.st_ino && a.st_dev == b.st_dev |
| 57 | } |
| 58 | } |