cap_primitives/rustix/fs/
mod.rs

1mod access_unchecked;
2mod copy_impl;
3mod create_dir_unchecked;
4mod dir_entry_inner;
5#[cfg(not(target_os = "wasi"))]
6mod dir_options_ext;
7mod dir_utils;
8#[cfg(not(any(target_os = "android", target_os = "linux")))]
9mod file_path;
10mod file_type_ext;
11mod hard_link_unchecked;
12mod is_file_read_write_impl;
13mod is_root_dir;
14mod is_same_file;
15mod metadata_ext;
16mod oflags;
17mod open_options_ext;
18mod open_unchecked;
19mod permissions_ext;
20mod read_dir_inner;
21mod read_link_unchecked;
22mod remove_dir_all_impl;
23mod remove_dir_unchecked;
24mod remove_file_unchecked;
25mod remove_open_dir_by_searching;
26mod rename_unchecked;
27mod reopen_impl;
28#[cfg(not(any(target_os = "android", target_os = "linux", target_os = "wasi")))]
29mod set_permissions_impl;
30#[cfg(not(target_os = "wasi"))]
31mod set_symlink_permissions_unchecked;
32#[cfg(not(any(target_os = "android", target_os = "linux")))]
33mod set_times_impl;
34mod stat_unchecked;
35mod symlink_unchecked;
36mod times;
37
38pub(crate) mod errors;
39
40// On Linux, use optimized implementations based on
41// `openat2` and `O_PATH` when available.
42//
43// On FreeBSD, use optimized implementations based on
44// `O_RESOLVE_BENEATH`/`AT_RESOLVE_BENEATH` and `O_PATH` when available.
45#[cfg(any(
46    target_os = "macos",
47    target_os = "ios",
48    target_os = "tvos",
49    target_os = "watchos",
50    target_os = "visionos",
51))]
52pub(crate) use crate::rustix::darwin::fs::*;
53#[cfg(target_os = "freebsd")]
54pub(crate) use crate::rustix::freebsd::fs::*;
55#[cfg(any(target_os = "android", target_os = "linux"))]
56pub(crate) use crate::rustix::linux::fs::*;
57#[cfg(not(any(target_os = "android", target_os = "linux", target_os = "freebsd")))]
58#[rustfmt::skip]
59pub(crate) use crate::fs::{
60    manually::open_entry as open_entry_impl,
61    manually::open as open_impl,
62    manually::stat as stat_impl,
63    manually::canonicalize as canonicalize_impl,
64    via_parent::set_times_nofollow as set_times_nofollow_impl,
65};
66#[cfg(any(
67    target_os = "macos",
68    target_os = "ios",
69    target_os = "tvos",
70    target_os = "watchos",
71    target_os = "visionos",
72))]
73pub(super) use file_path::file_path_by_ttyname_or_seaching;
74#[cfg(not(any(
75    target_os = "android",
76    target_os = "linux",
77    target_os = "macos",
78    target_os = "ios",
79    target_os = "tvos",
80    target_os = "watchos",
81    target_os = "visionos",
82)))]
83pub(crate) use file_path::file_path_by_ttyname_or_seaching as file_path;
84#[cfg(not(any(
85    target_os = "android",
86    target_os = "linux",
87    target_os = "freebsd",
88    target_os = "wasi"
89)))]
90pub(crate) use set_permissions_impl::set_permissions_impl;
91#[cfg(target_os = "freebsd")]
92pub(crate) use set_permissions_impl::set_permissions_impl as set_permissions_manually;
93#[cfg(not(target_os = "wasi"))]
94pub(crate) use set_symlink_permissions_unchecked::set_symlink_permissions_unchecked;
95#[cfg(not(any(target_os = "android", target_os = "linux", target_os = "freebsd")))]
96pub(crate) use set_times_impl::set_times_impl;
97#[cfg(target_os = "freebsd")]
98pub(crate) use set_times_impl::set_times_impl as set_times_manually;
99#[rustfmt::skip]
100pub(crate) use crate::fs::{
101    via_parent::access as access_impl,
102    via_parent::hard_link as hard_link_impl,
103    via_parent::create_dir as create_dir_impl,
104    via_parent::read_link as read_link_impl,
105    via_parent::rename as rename_impl,
106    via_parent::symlink as symlink_impl,
107    remove_open_dir_by_searching as remove_open_dir_impl,
108};
109#[cfg(not(target_os = "wasi"))]
110pub(crate) use crate::fs::via_parent::set_symlink_permissions as set_symlink_permissions_impl;
111#[cfg(not(target_os = "freebsd"))]
112#[rustfmt::skip]
113pub(crate) use crate::fs::{
114    via_parent::remove_dir as remove_dir_impl,
115    via_parent::remove_file as remove_file_impl,
116};
117
118pub(crate) use access_unchecked::access_unchecked;
119pub(crate) use copy_impl::copy_impl;
120pub(crate) use create_dir_unchecked::create_dir_unchecked;
121pub(crate) use dir_entry_inner::DirEntryInner;
122#[cfg(not(target_os = "wasi"))]
123pub(crate) use dir_options_ext::DirOptionsExt;
124pub(crate) use dir_utils::*;
125pub(crate) use file_type_ext::ImplFileTypeExt;
126pub(crate) use hard_link_unchecked::hard_link_unchecked;
127pub(crate) use is_file_read_write_impl::is_file_read_write_impl;
128pub(crate) use is_root_dir::is_root_dir;
129#[allow(unused_imports)]
130pub(crate) use is_same_file::{is_different_file, is_different_file_metadata, is_same_file};
131pub(crate) use metadata_ext::ImplMetadataExt;
132pub(crate) use open_options_ext::ImplOpenOptionsExt;
133pub(crate) use open_unchecked::{open_ambient_impl, open_unchecked};
134pub(crate) use permissions_ext::ImplPermissionsExt;
135pub(crate) use read_dir_inner::ReadDirInner;
136pub(crate) use read_link_unchecked::read_link_unchecked;
137pub(crate) use remove_dir_all_impl::{remove_dir_all_impl, remove_open_dir_all_impl};
138pub(crate) use remove_dir_unchecked::remove_dir_unchecked;
139pub(crate) use remove_file_unchecked::remove_file_unchecked;
140pub(crate) use remove_open_dir_by_searching::remove_open_dir_by_searching;
141pub(crate) use rename_unchecked::rename_unchecked;
142pub(crate) use reopen_impl::reopen_impl;
143pub(crate) use stat_unchecked::stat_unchecked;
144pub(crate) use symlink_unchecked::symlink_unchecked;
145#[allow(unused_imports)]
146pub(crate) use times::{set_times_follow_unchecked, set_times_nofollow_unchecked, to_timespec};
147
148// On Linux, there is a limit of 40 symlink expansions.
149// Source: <https://man7.org/linux/man-pages/man7/path_resolution.7.html>
150pub(crate) const MAX_SYMLINK_EXPANSIONS: u8 = 40;
151
152pub(super) use oflags::*;
153
154/// Test that `file_path` works on a tty path.
155#[test]
156fn tty_path() {
157    #[cfg(unix)]
158    use std::os::unix::fs::FileTypeExt;
159
160    let paths: &[&str] = if cfg!(target_os = "freebsd") {
161        // On FreeBSD, /dev/{tty,stdin,stdout,stderr} are aliases to different
162        // real devices.
163        &["/dev/ttyv0", "/dev/pts/0"]
164    } else if cfg!(target_os = "illumos") {
165        // On illumos, /dev/std{in,out,err} only exist if they're open.
166        &["/dev/tty", "/dev/pts/0"]
167    } else {
168        &["/dev/tty", "/dev/stdin", "/dev/stdout", "/dev/stderr"]
169    };
170
171    for path in paths {
172        // Not all host configurations have these, so only test them if we can
173        // open and canonicalize them, and if they're not FIFOs, which some
174        // OS's use for stdin/stdout/stderr.
175        if let Ok(file) = std::fs::File::open(path) {
176            if !file.metadata().unwrap().file_type().is_fifo() {
177                if let Ok(canonical) = std::fs::canonicalize(path) {
178                    assert_eq!(
179                        file_path(&file)
180                            .as_ref()
181                            .map(std::fs::canonicalize)
182                            .map(Result::unwrap),
183                        Some(canonical),
184                        "for path {path}, file_path matches canonicalized path"
185                    );
186                }
187            }
188        }
189    }
190}