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
use std::fs;
use minijinja::Value;
use std::error::Error;
use serde_derive::Serialize;
use std::path::{Path, PathBuf};
use crate::debug::time_string;
#[derive(Serialize)]
struct File {
accessed: String,
created: String,
modified: String,
is_dir: bool,
is_file: bool,
is_symlink: bool,
name: String,
len: u64
}
#[derive(Clone)]
pub struct IO {
dir: PathBuf
}
impl IO {
pub fn new(dir: PathBuf) -> Result<IO, Box<dyn Error>> {
let p = dir.as_path();
if p.is_dir() {
Ok(IO {dir})
} else {
Err(format!("Data must be a directory: {}", p.display()).into())
}
}
fn get_path(&self, path: &str) -> Option<PathBuf> {
let dir = self.dir.as_path();
let mut path = Path::new(path);
if let Ok(p) = path.strip_prefix("/") {
path = p
}
let path = dir.join(path);
if path.starts_with(dir) {
Some(path)
} else {
None
}
}
pub fn remove(&self, entry: &str) -> Option<String> {
let path = match self.get_path(entry) {
Some(path) => path,
None => {
return Some(format!("Unable to resolve path: {}", entry))
}
};
let path = path.as_path();
if path.is_dir() {
match fs::remove_dir_all(path) {
Ok(_) => None,
Err(err) => Some(format!(
"Unable to remove dir: {}\n{:#}", entry, err
))
}
} else {
match fs::remove_file(path) {
Ok(_) => None,
Err(err) => Some(format!(
"Unable to remove file: {}\n{:#}", entry, err
))
}
}
}
pub fn read (&self, entry: &str) -> Option<Value> {
let path = match self.get_path(entry) {
Some(path) => path,
None => {
return None
}
};
let path = path.as_path();
if path.try_exists().unwrap_or(false) {
if path.is_dir() {
let mut files: Vec<File> = Vec::new();
match fs::read_dir(path) {
Ok(entries) => {
for entry in entries {
if let Ok(entry) = entry {
let p = entry.path();
let mut fname = String::new();
if let Some(name) = p.file_name() {
if let Some(name) = name.to_str() {
fname = name.to_string();
}
}
let mut len: u64 = 0;
let mut accessed = String::new();
let mut created = String::new();
let mut modified = String::new();
if let Ok(meta) = p.metadata() {
len = meta.len();
if let Ok(time) = meta.accessed() {
accessed = time_string(time.into());
}
if let Ok(time) = meta.created() {
created = time_string(time.into());
}
if let Ok(time) = meta.modified() {
modified = time_string(time.into());
}
}
files.push(File {
accessed,
created,
modified,
is_dir: p.is_dir(),
is_file: p.is_file(),
is_symlink: p.is_symlink(),
name: fname,
len
});
}
}
Some(Value::from_serialize(files))
},
Err(_) => None
}
} else {
match fs::read(path) {
Ok(data) => Some(data.into()),
Err(_) => None
}
}
} else {
None
}
}
pub fn write (&self, file: &str, data: &Vec<u8>) -> Option<String> {
let path = match self.get_path(file) {
Some(path) => path,
None => {
return Some(format!("Unable to resolve file: {}", file))
}
};
let path = path.as_path();
if let Some(dir) = path.parent() {
if !dir.exists() {
if let Err(err) = fs::create_dir_all(dir) {
return Some(format!("Unable to create dir: {}\n{:#}",
dir.display(), err
));
}
}
}
match fs::write(path, data) {
Ok(_) => None,
Err(err) => Some(format!("Unable to write file: {}\n{:#}",
file, err
))
}
}
}