path_dedot/
lib.rs

1/*!
2# Path Dedot
3
4This is a library for extending `Path` and `PathBuf` in order to parse the path which contains dots.
5
6Please read the following examples to know the parsing rules.
7
8## Examples
9
10If a path starts with a single dot, the dot means your program's **current working directory** (CWD).
11
12```rust
13use std::path::Path;
14use std::env;
15
16use path_dedot::*;
17
18let p = Path::new("./path/to/123/456");
19# if cfg!(unix) {
20# #[cfg(feature = "unsafe_cache")]
21# {
22#     unsafe {
23#         update_cwd();
24#     }
25# }
26assert_eq!(Path::join(env::current_dir().unwrap().as_path(), Path::new("path/to/123/456")).to_str().unwrap(), p.parse_dot().unwrap().to_str().unwrap());
27# }
28```
29
30If a path starts with a pair of dots, the dots means the parent of the CWD. If the CWD is **root**, the parent is still **root**.
31
32```rust
33use std::path::Path;
34use std::env;
35
36use path_dedot::*;
37
38let p = Path::new("../path/to/123/456");
39
40let cwd = env::current_dir().unwrap();
41
42let cwd_parent = cwd.parent();
43
44# if cfg!(unix) {
45# #[cfg(feature = "unsafe_cache")]
46# {
47#     unsafe {
48#         update_cwd();
49#     }
50# }
51match cwd_parent {
52   Some(cwd_parent) => {
53      assert_eq!(Path::join(&cwd_parent, Path::new("path/to/123/456")).to_str().unwrap(), p.parse_dot().unwrap().to_str().unwrap());
54   }
55   None => {
56      assert_eq!(Path::join(Path::new("/"), Path::new("path/to/123/456")).to_str().unwrap(), p.parse_dot().unwrap().to_str().unwrap());
57   }
58}
59# }
60```
61
62In addition to starting with, the **Single Dot** and **Double Dots** can also be placed to other positions. **Single Dot** means noting and will be ignored. **Double Dots** means the parent.
63
64```rust
65use std::path::Path;
66
67use path_dedot::*;
68
69let p = Path::new("/path/to/../123/456/./777");
70
71# if cfg!(unix) {
72# #[cfg(feature = "unsafe_cache")]
73# {
74#     unsafe {
75#         update_cwd();
76#     }
77# }
78assert_eq!("/path/123/456/777", p.parse_dot().unwrap().to_str().unwrap());
79# }
80```
81
82```rust
83use std::path::Path;
84
85use path_dedot::*;
86
87let p = Path::new("/path/to/../123/456/./777/..");
88
89# if cfg!(unix) {
90# #[cfg(feature = "unsafe_cache")]
91# {
92#     unsafe {
93#         update_cwd();
94#     }
95# }
96assert_eq!("/path/123/456", p.parse_dot().unwrap().to_str().unwrap());
97# }
98```
99
100You should notice that `parse_dot` method does **not** aim to get an **absolute path**. A path which does not start with a `MAIN_SEPARATOR`, **Single Dot** and **Double Dots**, will not have each of them after the `parse_dot` method is used.
101
102```rust
103use std::path::Path;
104
105use path_dedot::*;
106
107let p = Path::new("path/to/../123/456/./777/..");
108
109# if cfg!(unix) {
110# #[cfg(feature = "unsafe_cache")]
111# {
112#     unsafe {
113#         update_cwd();
114#     }
115# }
116assert_eq!("path/123/456", p.parse_dot().unwrap().to_str().unwrap());
117# }
118```
119
120**Double Dots** which is not placed at the start cannot get the parent beyond the original path. Why not? With this constraint, you can insert an absolute path to the start as a virtual root in order to protect your file system from being exposed.
121
122```rust
123use std::path::Path;
124
125use path_dedot::*;
126
127let p = Path::new("path/to/../../../../123/456/./777/..");
128
129# if cfg!(unix) {
130# #[cfg(feature = "unsafe_cache")]
131# {
132#     unsafe {
133#         update_cwd();
134#     }
135# }
136assert_eq!("123/456", p.parse_dot().unwrap().to_str().unwrap());
137# }
138```
139
140```rust
141use std::path::Path;
142
143use path_dedot::*;
144
145let p = Path::new("/path/to/../../../../123/456/./777/..");
146
147# if cfg!(unix) {
148# #[cfg(feature = "unsafe_cache")]
149# {
150#     unsafe {
151#         update_cwd();
152#     }
153# }
154assert_eq!("/123/456", p.parse_dot().unwrap().to_str().unwrap());
155# }
156```
157
158### Starting from a given current working directory
159
160With the `parse_dot_from` function, you can provide the current working directory that the relative paths should be resolved from.
161
162```rust
163use std::env;
164use std::path::Path;
165
166use path_dedot::*;
167
168let p = Path::new("../path/to/123/456");
169let cwd = env::current_dir().unwrap();
170
171println!("{}", p.parse_dot_from(cwd).unwrap().to_str().unwrap());
172```
173
174## Caching
175
176By default, the `parse_dot` method creates a new `PathBuf` instance of the CWD every time in its operation. The overhead is obvious. Although it allows us to safely change the CWD at runtime by the program itself (e.g. using the `std::env::set_current_dir` function) or outside controls (e.g. using gdb to call `chdir`), we don't need that in most cases.
177
178In order to parse paths with better performance, this crate provides three ways to cache the CWD.
179
180### once_cell_cache
181
182Enabling the `once_cell_cache` feature can let this crate use `once_cell` to cache the CWD. It's thread-safe and does not need to modify any code, but once the CWD is cached, it cannot be changed anymore at runtime.
183
184```toml
185[dependencies.path-dedot]
186version = "*"
187features = ["once_cell_cache"]
188```
189
190### lazy_static_cache
191
192Enabling the `lazy_static_cache` feature can let this crate use `lazy_static` to cache the CWD. It's thread-safe and does not need to modify any code, but once the CWD is cached, it cannot be changed anymore at runtime.
193
194```toml
195[dependencies.path-dedot]
196version = "*"
197features = ["lazy_static_cache"]
198```
199
200### unsafe_cache
201
202Enabling the `unsafe_cache` feature can let this crate use a mutable static variable to cache the CWD. It allows the program to change the CWD at runtime by the program itself, but it's not thread-safe.
203
204You need to use the `update_cwd` function to initialize the CWD first. The function should also be used to update the CWD after the CWD is changed.
205
206```toml
207[dependencies.path-dedot]
208version = "*"
209features = ["unsafe_cache"]
210```
211
212```rust
213use std::path::Path;
214
215use path_dedot::*;
216
217# #[cfg(feature = "unsafe_cache")]
218unsafe {
219    update_cwd();
220}
221
222let p = Path::new("./path/to/123/456");
223
224println!("{}", p.parse_dot().unwrap().to_str().unwrap());
225
226std::env::set_current_dir("/").unwrap();
227
228# #[cfg(feature = "unsafe_cache")]
229unsafe {
230    update_cwd();
231}
232
233println!("{}", p.parse_dot().unwrap().to_str().unwrap());
234```
235
236## Benchmark
237
238#### No-cache
239
240```bash
241cargo bench
242```
243
244#### once_cell_cache
245
246```bash
247cargo bench --features once_cell_cache
248```
249
250#### lazy_static_cache
251
252```bash
253cargo bench --features lazy_static_cache
254```
255
256#### unsafe_cache
257
258```bash
259cargo bench --features unsafe_cache
260```
261
262*/
263
264#[cfg(any(
265    all(feature = "lazy_static_cache", feature = "unsafe_cache"),
266    all(feature = "once_cell_cache", feature = "unsafe_cache"),
267    all(feature = "lazy_static_cache", feature = "once_cell_cache")
268))]
269compile_error!("You can only enable at most one caching mechanism for `path-dedot`.");
270
271#[cfg(feature = "lazy_static_cache")]
272#[macro_use]
273extern crate lazy_static;
274
275#[cfg(not(feature = "lazy_static_cache"))]
276extern crate once_cell;
277
278use std::{
279    borrow::Cow,
280    ffi::OsString,
281    io,
282    path::{self, Path, PathBuf},
283};
284
285mod parse_dot;
286
287#[macro_use]
288mod macros;
289
290#[cfg(any(unix, all(target_family = "wasm", feature = "use_unix_paths_on_wasm")))]
291mod unix;
292
293#[cfg(windows)]
294mod windows;
295
296#[cfg(feature = "unsafe_cache")]
297mod unsafe_cwd;
298
299#[cfg(not(feature = "lazy_static_cache"))]
300use once_cell::sync::Lazy;
301pub use parse_dot::*;
302#[cfg(windows)]
303pub use windows::ParsePrefix;
304
305#[cfg(not(feature = "lazy_static_cache"))]
306/// The main separator for the target OS.
307pub static MAIN_SEPARATOR: Lazy<OsString> =
308    Lazy::new(|| OsString::from(path::MAIN_SEPARATOR.to_string()));
309
310#[cfg(feature = "lazy_static_cache")]
311lazy_static! {
312    /// Current working directory.
313    pub static ref MAIN_SEPARATOR: OsString = OsString::from(path::MAIN_SEPARATOR.to_string());
314}
315
316impl ParseDot for PathBuf {
317    #[inline]
318    fn parse_dot(&self) -> io::Result<Cow<Path>> {
319        self.as_path().parse_dot()
320    }
321
322    #[inline]
323    fn parse_dot_from(&self, cwd: impl AsRef<Path>) -> io::Result<Cow<Path>> {
324        self.as_path().parse_dot_from(cwd)
325    }
326}
327
328#[cfg(feature = "once_cell_cache")]
329/// Current working directory.
330pub static CWD: Lazy<PathBuf> = Lazy::new(|| std::env::current_dir().unwrap());
331
332#[cfg(feature = "lazy_static_cache")]
333lazy_static! {
334    /// Current working directory.
335    pub static ref CWD: PathBuf = std::env::current_dir().unwrap();
336}
337
338#[cfg(feature = "unsafe_cache")]
339/// Current working directory.
340pub static mut CWD: unsafe_cwd::UnsafeCWD = unsafe_cwd::UnsafeCWD::new();
341
342#[cfg(feature = "unsafe_cache")]
343/// Initialize or update the CWD cached in the `path-dedot` crate after using the `std::env::set_current_dir` function. It is not a safe operation. Make sure there is no `parse_dot` method running at this moment.
344#[allow(clippy::missing_safety_doc)]
345pub unsafe fn update_cwd() {
346    CWD.update();
347}