1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
#![deny(
warnings,
missing_docs,
trivial_numeric_casts,
unused_import_braces,
unused_qualifications
)]
#![doc(html_root_url = "https://docs.rs/canonical-path/2.0.2")]
use std::{
borrow::Borrow,
env,
ffi::{OsStr, OsString},
fs::{Metadata, ReadDir},
io::{Error, ErrorKind, Result},
path::{Components, Display, Iter, Path, PathBuf},
};
macro_rules! impl_path {
() => {
#[inline]
pub fn as_path(&self) -> &Path {
self.0.as_ref()
}
#[inline]
pub fn as_os_str(&self) -> &OsStr {
self.0.as_os_str()
}
#[inline]
pub fn to_str(&self) -> Option<&str> {
self.0.to_str()
}
pub fn parent(&self) -> Result<CanonicalPathBuf> {
CanonicalPathBuf::new(&self.0
.parent()
.ok_or_else(|| Error::new(ErrorKind::InvalidInput, "can't get parent of '/'"))?)
}
#[inline]
pub fn file_name(&self) -> Option<&OsStr> {
self.0.file_name()
}
#[inline]
pub fn starts_with<P: AsRef<Path>>(&self, base: P) -> bool {
self.0.starts_with(base)
}
#[inline]
pub fn ends_with<P: AsRef<Path>>(&self, child: P) -> bool {
self.0.ends_with(child)
}
#[inline]
pub fn file_stem(&self) -> Option<&OsStr> {
self.0.file_stem()
}
#[inline]
pub fn extension(&self) -> Option<&OsStr> {
self.0.extension()
}
#[inline]
pub fn with_file_name<S: AsRef<OsStr>>(&self, file_name: S) -> Result<CanonicalPathBuf> {
CanonicalPathBuf::new(&self.0.with_file_name(file_name))
}
#[inline]
pub fn with_extension<S: AsRef<OsStr>>(&self, extension: S) -> Result<CanonicalPathBuf> {
CanonicalPathBuf::new(&self.0.with_extension(extension))
}
#[inline]
pub fn components(&self) -> Components {
self.0.components()
}
#[inline]
pub fn iter(&self) -> Iter {
self.0.iter()
}
#[inline]
pub fn display(&self) -> Display {
self.0.display()
}
#[inline]
pub fn metadata(&self) -> Result<Metadata> {
self.0.symlink_metadata()
}
#[inline]
pub fn join<P: AsRef<Path>>(&self, path: P) -> Result<CanonicalPathBuf> {
CanonicalPathBuf::new(&self.0.join(path))
}
#[inline]
pub fn read_dir(&self) -> Result<ReadDir> {
self.0.read_dir()
}
#[inline]
pub fn exists(&self) -> bool {
self.0.exists()
}
#[inline]
pub fn is_file(&self) -> bool {
self.0.is_file()
}
#[inline]
pub fn is_dir(&self) -> bool {
self.0.is_file()
}
}
}
#[derive(Clone, Debug, PartialOrd, Ord, PartialEq, Eq, Hash)]
pub struct CanonicalPathBuf(PathBuf);
impl CanonicalPathBuf {
pub fn canonicalize<P>(path: P) -> Result<Self>
where
P: AsRef<Path>,
{
Ok(CanonicalPathBuf(path.as_ref().canonicalize()?))
}
#[allow(clippy::new_ret_no_self)]
pub fn new<P>(path: P) -> Result<Self>
where
P: AsRef<Path>,
{
let p = path.as_ref();
let canonical_path_buf = Self::canonicalize(p)?;
if canonical_path_buf.as_path() != p {
return Err(Error::new(
ErrorKind::InvalidInput,
format!("non-canonical input path: {}", p.display()),
));
}
Ok(canonical_path_buf)
}
#[inline]
pub fn as_canonical_path(&self) -> &CanonicalPath {
unsafe { CanonicalPath::from_path_unchecked(&self.0) }
}
pub fn set_file_name<S: AsRef<OsStr>>(&mut self, file_name: S) {
self.0.set_file_name(file_name);
}
pub fn set_extension<S: AsRef<OsStr>>(&mut self, extension: S) -> bool {
self.0.set_extension(extension)
}
pub fn into_path_buf(self) -> PathBuf {
self.0
}
pub fn into_os_string(self) -> OsString {
self.0.into_os_string()
}
impl_path!();
}
impl AsRef<Path> for CanonicalPathBuf {
fn as_ref(&self) -> &Path {
self.as_path()
}
}
impl AsRef<CanonicalPath> for CanonicalPathBuf {
fn as_ref(&self) -> &CanonicalPath {
self.as_canonical_path()
}
}
impl AsRef<OsStr> for CanonicalPathBuf {
fn as_ref(&self) -> &OsStr {
self.as_os_str()
}
}
impl Borrow<CanonicalPath> for CanonicalPathBuf {
fn borrow(&self) -> &CanonicalPath {
self.as_canonical_path()
}
}
#[derive(Debug, PartialOrd, Ord, PartialEq, Eq, Hash)]
pub struct CanonicalPath(Path);
impl CanonicalPath {
pub fn new<P>(path: &P) -> Result<&Self>
where
P: AsRef<Path> + ?Sized,
{
let p = path.as_ref();
if p != p.canonicalize()? {
return Err(Error::new(
ErrorKind::InvalidInput,
format!("non-canonical input path: {}", p.display()),
));
}
Ok(unsafe { Self::from_path_unchecked(p) })
}
pub unsafe fn from_path_unchecked<P>(path: &P) -> &Self
where
P: AsRef<Path> + ?Sized,
{
&*(path.as_ref() as *const Path as *const CanonicalPath)
}
pub fn to_canonical_path_buf(&self) -> CanonicalPathBuf {
CanonicalPathBuf(self.0.to_owned())
}
impl_path!();
}
impl AsRef<Path> for CanonicalPath {
fn as_ref(&self) -> &Path {
&self.0
}
}
impl ToOwned for CanonicalPath {
type Owned = CanonicalPathBuf;
fn to_owned(&self) -> CanonicalPathBuf {
self.to_canonical_path_buf()
}
}
pub fn current_exe() -> Result<CanonicalPathBuf> {
let p = env::current_exe()?;
Ok(CanonicalPathBuf::canonicalize(p)?)
}
#[cfg(all(test, not(windows)))]
mod tests {
use std::fs::File;
use std::os::unix::fs;
use std::path::PathBuf;
use super::{CanonicalPath, CanonicalPathBuf};
use tempfile::TempDir;
const CANONICAL_FILENAME: &str = "canonical-file";
const NON_CANONICAL_FILENAME: &str = "non-canonical-file";
struct TestFixtureDir {
pub tempdir: TempDir,
pub base_path: PathBuf,
pub canonical_path: PathBuf,
pub symlink_path: PathBuf,
}
impl TestFixtureDir {
pub fn new() -> Self {
let tempdir = TempDir::new().unwrap();
let base_path = tempdir.path().canonicalize().unwrap();
let canonical_path = base_path.join(CANONICAL_FILENAME);
File::create(&canonical_path).unwrap();
let symlink_path = base_path.join(NON_CANONICAL_FILENAME);
fs::symlink(&canonical_path, &symlink_path).unwrap();
Self {
tempdir,
base_path,
canonical_path,
symlink_path,
}
}
}
#[test]
fn create_canonical_path() {
let test_fixtures = TestFixtureDir::new();
let canonical_path = CanonicalPath::new(&test_fixtures.canonical_path).unwrap();
assert_eq!(
canonical_path.as_path(),
test_fixtures.canonical_path.as_path()
);
}
#[test]
fn create_canonical_path_buf() {
let test_fixtures = TestFixtureDir::new();
let canonical_path_buf = CanonicalPathBuf::new(&test_fixtures.canonical_path).unwrap();
assert_eq!(
canonical_path_buf.as_path(),
test_fixtures.canonical_path.as_path()
);
}
#[test]
fn reject_canonical_path_symlinks() {
let test_fixtures = TestFixtureDir::new();
let result = CanonicalPath::new(&test_fixtures.symlink_path);
assert!(result.is_err(), "symlinks aren't canonical paths!");
}
#[test]
fn reject_canonical_path_buf_symlinks() {
let test_fixtures = TestFixtureDir::new();
let result = CanonicalPathBuf::new(&test_fixtures.symlink_path);
assert!(result.is_err(), "symlinks aren't canonical paths!");
}
}