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
use std::borrow::Cow;
use std::ffi::OsStr;
use std::fs::{self, File, OpenOptions};
use std::io::{Read, Write};
use std::path::{Path, PathBuf};
use std::sync::Arc;
#[cfg(unix)]
use std::os::unix::fs::symlink as symlink_file;
#[cfg(windows)]
use std::os::windows::fs::symlink_file;
use super::common::log_info;
use super::settings::SceneSettings;
#[allow(dead_code)]
pub struct AtPath<'at> {
pub subdir: Cow<'at, Path>,
settings: Option<Arc<SceneSettings>>
}
impl<'at> AtPath<'at> {
pub fn from_osstr(subdir: &'at OsStr) -> AtPath<'at> {
AtPath { subdir: Cow::Borrowed(Path::new(subdir)), settings: None }
}
pub fn from_path(subdir: &'at Path) -> AtPath<'at> {
AtPath { subdir: Cow::Borrowed(subdir), settings: None }
}
pub fn from_path_owned(subdir: PathBuf) -> AtPath<'at> {
AtPath { subdir : Cow::Owned(subdir), settings: None }
}
pub fn from_scene_settings(settings: Arc<SceneSettings>) -> AtPath<'at> {
AtPath { subdir : Cow::Owned(PathBuf::from(settings.as_ref().tmpd.path())), settings: Some(settings)}
}
pub fn as_string(&self) -> String {
self.subdir.to_str().unwrap().to_owned()
}
pub fn plus(&self, name: &str) -> PathBuf {
let mut pathbuf = PathBuf::from(self.subdir.as_ref());
pathbuf.push(name);
pathbuf
}
pub fn plus_as_string(&self, name: &str) -> String {
String::from(self.plus(name).to_str().unwrap())
}
fn minus(&self, name: &str) -> PathBuf {
let prefixed = PathBuf::from(name);
if prefixed.starts_with(&self.subdir) {
let mut unprefixed = PathBuf::new();
for component in prefixed.components()
.skip(self.subdir.components().count()) {
unprefixed.push(component.as_ref().to_str().unwrap());
}
unprefixed
} else {
prefixed
}
}
pub fn minus_as_string(&self, name: &str) -> String {
String::from(self.minus(name).to_str().unwrap())
}
pub fn open(&self, name: &str) -> File {
log_info("open", self.plus_as_string(name));
File::open(self.plus(name)).unwrap()
}
pub fn read(&self, name: &str) -> String {
let mut f = self.open(name);
let mut contents = String::new();
let _ = f.read_to_string(&mut contents);
contents
}
pub fn write(&self, name: &str, contents: &str) {
let mut f = self.open(name);
let _ = f.write(contents.as_bytes());
}
pub fn append(&self, name: &str, contents: &str) {
log_info("open(append)", self.plus_as_string(name));
let mut f = OpenOptions::new().write(true).append(true).open(self.plus(name)).unwrap();
let _ = f.write(contents.as_bytes());
}
pub fn mkdir(&self, dir: &str) {
log_info("mkdir", self.plus_as_string(dir));
fs::create_dir(&self.plus(dir)).unwrap();
}
pub fn mkdir_all(&self, dir: &str) {
log_info("mkdir_all", self.plus_as_string(dir));
fs::create_dir_all(self.plus(dir)).unwrap();
}
pub fn make_file(&self, name: &str) -> File {
match File::create(&self.plus(name)) {
Ok(f) => f,
Err(e) => panic!("{}", e),
}
}
pub fn touch(&self, file: &str) {
log_info("touch", self.plus_as_string(file));
File::create(&self.plus(file)).unwrap();
}
pub fn symlink(&self, src: &str, dst: &str) {
log_info("symlink",
&format!("{},{}", self.plus_as_string(src), self.plus_as_string(dst)));
symlink_file(&self.plus(src), &self.plus(dst)).unwrap();
}
pub fn is_symlink(&self, path: &str) -> bool {
log_info("is_symlink", self.plus_as_string(path));
match fs::symlink_metadata(&self.plus(path)) {
Ok(m) => m.file_type().is_symlink(),
Err(_) => false,
}
}
pub fn resolve_link(&self, path: &str) -> String {
log_info("resolve_link", self.plus_as_string(path));
match fs::read_link(&self.plus(path)) {
Ok(p) => {
self.minus_as_string(p.to_str().unwrap())
}
Err(_) => "".to_string(),
}
}
pub fn metadata(&self, path: &str) -> fs::Metadata {
match fs::metadata(&self.plus(path)) {
Ok(m) => m,
Err(e) => panic!("{}", e),
}
}
pub fn file_exists(&self, path: &str) -> bool {
match fs::metadata(&self.plus(path)) {
Ok(m) => m.is_file(),
Err(_) => false,
}
}
pub fn dir_exists(&self, path: &str) -> bool {
match fs::metadata(&self.plus(path)) {
Ok(m) => m.is_dir(),
Err(_) => false,
}
}
pub fn cleanup(&self, path: &'static str) {
let p = &self.plus(path);
match fs::metadata(p) {
Ok(m) => if m.is_file() {
fs::remove_file(&p).unwrap();
} else {
fs::remove_dir(&p).unwrap();
},
Err(_) => {}
}
}
pub fn root_dir(&self) -> String {
log_info("current_directory", "");
self.subdir.to_str().unwrap().to_owned()
}
pub fn root_dir_resolved(&self) -> Option<String> {
log_info("current_directory_resolved", "");
let c = self.subdir.canonicalize();
match c {
Result::Err(_) => None,
Result::Ok(x) => {
match x.to_str() {
None => None,
Some(sb) => Some({
let s = sb.to_owned();
let prefix = "\\\\?\\";
if s.starts_with(prefix) {
String::from(&s[prefix.len()..])
} else {
s
}
})
}
}
}
}
}