-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
absolute_system_path.rs
176 lines (145 loc) · 4.86 KB
/
absolute_system_path.rs
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
#[cfg(not(windows))]
use std::os::unix::fs::symlink as symlink_file;
#[cfg(not(windows))]
use std::os::unix::fs::symlink as symlink_dir;
#[cfg(windows)]
use std::os::windows::fs::{symlink_dir, symlink_file};
use std::{
borrow::Cow,
fmt, fs,
fs::Metadata,
io,
path::{Path, PathBuf},
};
use path_slash::PathExt;
use crate::{
AbsoluteSystemPathBuf, AnchoredSystemPathBuf, IntoSystem, PathError, PathValidationError,
RelativeSystemPathBuf, RelativeUnixPath,
};
pub struct AbsoluteSystemPath(Path);
impl ToOwned for AbsoluteSystemPath {
type Owned = AbsoluteSystemPathBuf;
fn to_owned(&self) -> Self::Owned {
AbsoluteSystemPathBuf(self.0.to_owned())
}
}
impl AsRef<AbsoluteSystemPath> for AbsoluteSystemPath {
fn as_ref(&self) -> &AbsoluteSystemPath {
self
}
}
impl fmt::Display for AbsoluteSystemPath {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.0.display().fmt(f)
}
}
impl AsRef<Path> for AbsoluteSystemPath {
fn as_ref(&self) -> &Path {
&self.0
}
}
impl AbsoluteSystemPath {
pub fn new<P: AsRef<Path> + ?Sized>(value: &P) -> Result<Cow<Self>, PathError> {
let path = value.as_ref();
if path.is_relative() {
return Err(PathValidationError::NotAbsolute(path.to_owned()).into());
}
let system_path = path.to_slash().ok_or_else(|| {
PathError::PathValidationError(PathValidationError::InvalidUnicode(path.to_owned()))
})?;
// copied from stdlib path.rs: relies on the representation of
// AbsoluteSystemPath being just a Path, the same way Path relies on
// just being an OsStr
match system_path {
Cow::Owned(path) => Ok(Cow::Owned(AbsoluteSystemPathBuf::new(path)?)),
Cow::Borrowed(path) => {
let path = Path::new(path);
let absolute_system_path = unsafe { &*(path as *const Path as *const Self) };
Ok(Cow::Borrowed(absolute_system_path))
}
}
}
pub fn as_path(&self) -> &Path {
&self.0
}
pub fn join_relative(&self, path: &RelativeSystemPathBuf) -> AbsoluteSystemPathBuf {
let path = self.0.join(path.as_path());
AbsoluteSystemPathBuf(path)
}
pub fn join_literal(&self, segment: &str) -> AbsoluteSystemPathBuf {
AbsoluteSystemPathBuf(self.0.join(segment))
}
pub fn join_unix_path(
&self,
unix_path: &RelativeUnixPath,
) -> Result<AbsoluteSystemPathBuf, PathError> {
let tail = unix_path.to_system_path()?;
Ok(AbsoluteSystemPathBuf(self.0.join(tail.as_path())))
}
pub fn anchor(&self, path: &AbsoluteSystemPath) -> Result<AnchoredSystemPathBuf, PathError> {
AnchoredSystemPathBuf::new(self, path)
}
pub fn ensure_dir(&self) -> Result<(), io::Error> {
if let Some(parent) = self.0.parent() {
fs::create_dir_all(parent)
} else {
Ok(())
}
}
pub fn symlink_to_file<P: AsRef<Path>>(&self, to: P) -> Result<(), PathError> {
let system_path = to.as_ref();
let system_path = system_path.into_system()?;
symlink_file(system_path, &self.0)?;
Ok(())
}
pub fn symlink_to_dir<P: AsRef<Path>>(&self, to: P) -> Result<(), PathError> {
let system_path = to.as_ref();
let system_path = system_path.into_system()?;
symlink_dir(&system_path, &self.0)?;
Ok(())
}
pub fn resolve(&self, path: &AnchoredSystemPathBuf) -> AbsoluteSystemPathBuf {
let path = self.0.join(path.as_path());
AbsoluteSystemPathBuf(path)
}
// note that this is *not* lstat. If this is a symlink, it
// will return metadata for the target.
pub fn stat(&self) -> Result<Metadata, PathError> {
Ok(fs::metadata(&self.0)?)
}
pub fn symlink_metadata(&self) -> Result<Metadata, PathError> {
Ok(fs::symlink_metadata(&self.0)?)
}
pub fn read_link(&self) -> Result<PathBuf, io::Error> {
fs::read_link(&self.0)
}
pub fn remove_file(&self) -> Result<(), io::Error> {
fs::remove_file(&self.0)
}
}
#[cfg(test)]
mod tests {
use anyhow::Result;
use super::*;
#[test]
fn test_create_absolute_path() -> Result<()> {
let absolute_path = AbsoluteSystemPath::new("/foo/bar")?;
#[cfg(unix)]
{
assert_eq!(absolute_path.to_string(), "/foo/bar");
assert!(absolute_path.is_borrowed());
}
#[cfg(windows)]
{
assert_eq!(absolute_path.to_string(), r"\foo\bar");
assert!(absolute_path.is_owned());
}
#[cfg(windows)]
{
let absolute_path = AbsoluteSystemPath::new(r"\foo\bar")?;
assert_eq!(absolute_path.to_string(), r"\foo\bar");
assert!(absolute_path.is_borrowed());
}
Ok(())
}
}