cap_primitives/fs/
mod.rs

1//! Filesystem utilities.
2
3#[cfg(racy_asserts)]
4#[macro_use]
5pub(crate) mod assert_same_file;
6
7mod access;
8mod canonicalize;
9mod copy;
10mod create_dir;
11mod dir_builder;
12mod dir_entry;
13mod dir_options;
14mod file;
15#[cfg(not(any(target_os = "android", target_os = "linux", windows)))]
16mod file_path_by_searching;
17mod file_type;
18mod follow_symlinks;
19mod hard_link;
20mod is_file_read_write;
21mod maybe_owned_file;
22mod metadata;
23mod open;
24mod open_ambient;
25mod open_dir;
26mod open_options;
27mod open_unchecked_error;
28mod permissions;
29mod read_dir;
30mod read_link;
31mod remove_dir;
32mod remove_dir_all;
33mod remove_file;
34mod remove_open_dir;
35mod rename;
36mod reopen;
37#[cfg(not(target_os = "wasi"))]
38mod set_permissions;
39mod set_times;
40mod stat;
41mod symlink;
42mod system_time_spec;
43
44pub(crate) mod errors;
45pub(crate) mod manually;
46pub(crate) mod via_parent;
47
48use maybe_owned_file::MaybeOwnedFile;
49
50#[cfg(not(any(target_os = "android", target_os = "linux", windows)))]
51pub(crate) use file_path_by_searching::file_path_by_searching;
52pub(crate) use open_unchecked_error::*;
53
54#[cfg(not(windows))]
55pub(crate) use super::rustix::fs::*;
56#[cfg(windows)]
57pub(crate) use super::windows::fs::*;
58
59#[cfg(not(windows))]
60pub(crate) use read_dir::{read_dir_nofollow, read_dir_unchecked};
61
62pub use access::{access, AccessModes, AccessType};
63pub use canonicalize::canonicalize;
64pub use copy::copy;
65pub use create_dir::create_dir;
66pub use dir_builder::*;
67pub use dir_entry::DirEntry;
68#[cfg(windows)]
69pub use dir_entry::_WindowsDirEntryExt;
70pub use dir_options::DirOptions;
71pub use file::FileExt;
72pub use file_type::FileType;
73#[cfg(any(unix, target_os = "vxworks", all(windows, windows_file_type_ext)))]
74pub use file_type::FileTypeExt;
75#[cfg(windows)]
76pub use file_type::_WindowsFileTypeExt;
77pub use follow_symlinks::FollowSymlinks;
78pub use hard_link::hard_link;
79pub use is_file_read_write::is_file_read_write;
80#[cfg(windows)]
81pub use metadata::_WindowsByHandle;
82pub use metadata::{Metadata, MetadataExt};
83pub use open::open;
84pub use open_ambient::open_ambient;
85pub use open_dir::*;
86pub use open_options::*;
87pub use permissions::Permissions;
88#[cfg(unix)]
89pub use permissions::PermissionsExt;
90pub use read_dir::{read_base_dir, read_dir, ReadDir};
91pub use read_link::{read_link, read_link_contents};
92pub use remove_dir::remove_dir;
93pub use remove_dir_all::remove_dir_all;
94pub use remove_file::remove_file;
95pub use remove_open_dir::{remove_open_dir, remove_open_dir_all};
96pub use rename::rename;
97pub use reopen::reopen;
98#[cfg(not(target_os = "wasi"))]
99pub use set_permissions::{set_permissions, set_symlink_permissions};
100pub use set_times::{set_times, set_times_nofollow};
101pub use stat::stat;
102#[cfg(not(windows))]
103pub use symlink::{symlink, symlink_contents};
104#[cfg(windows)]
105pub use symlink::{symlink_dir, symlink_file};
106pub use system_time_spec::SystemTimeSpec;
107
108#[cfg(racy_asserts)]
109fn map_result<T: Clone>(result: &std::io::Result<T>) -> Result<T, (std::io::ErrorKind, String)> {
110    match result {
111        Ok(t) => Ok(t.clone()),
112        Err(e) => Err((e.kind(), e.to_string())),
113    }
114}
115
116/// Test that `file_path` works on a few miscellaneous directory paths.
117#[test]
118fn dir_paths() {
119    use crate::ambient_authority;
120
121    for path in [std::env::current_dir().unwrap(), std::env::temp_dir()] {
122        let dir = open_ambient_dir(&path, ambient_authority()).unwrap();
123        assert_eq!(
124            file_path(&dir)
125                .as_ref()
126                .map(std::fs::canonicalize)
127                .map(Result::unwrap),
128            Some(std::fs::canonicalize(path).unwrap())
129        );
130    }
131}