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
#![doc(
html_logo_url =
"https://rawgit.com/aldarons-tech/adi_storage/master/res/icon.png",
html_favicon_url =
"https://rawgit.com/aldarons-tech/adi_storage/master/res/symbol.png",
html_root_url = "http://at.plopgrizzly.tech/utem/"
)]
extern crate whoami;
use std::fs;
use std::path::Path;
use std::io::{ Read, Write };
pub enum PathType {
Folder,
File,
}
pub struct Storage {
app_root: String,
user: String,
}
impl Storage {
pub fn new(app_name: &str, developer: &str) -> Storage {
let mut app_root = if cfg!(target_os = "linux") {
"/usr/local/share/at_root/"
} else if cfg!(target_os = "windows") {
"C:/Program Files/at_root/"
} else if cfg!(target_os = "aldarons_os") {
"/"
} else {
panic!("AA!")
}.to_string();
app_root.push_str(&app_name.to_lowercase());
app_root.push_str(".");
app_root.push_str(&developer);
app_root.push_str("/");
let mut user = String::new();
user.push_str(&whoami::username().to_lowercase());
user.push_str(".");
user.push_str(&whoami::computer());
Storage {
app_root: app_root,
user: user,
}
}
pub fn sync(&self) {
}
}
impl ::std::fmt::Display for Storage {
fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
write!(f, "{{ app_root: \"{}\", user: \"{}\" }}", self.app_root,
self.user)
}
}
#[macro_export] macro_rules! storage {
() => ( {
let app_name = env!("CARGO_PKG_NAME");
let developer = include!(concat!(env!("CARGO_MANIFEST_DIR"),
"/target/res/src/developer.rs"));
adi_storage::Storage::new(app_name, developer)
} )
}
#[macro_export] macro_rules! file_save {
(file_name) => ( {
let app_name = include!(concat!(env!("CARGO_MANIFEST_DIR"),
"/target/res/src/name.rs"));
let developer = include!(concat!(env!("CARGO_MANIFEST_DIR"),
"/target/res/src/developer.rs"));
let mut app_root = STORAGE_MAIN.to_string();
app_root.push_str(app_name);
app_root.push_str(".");
app_root.push_str(developer);
app_root.push_str("/");
let user = users::get_user_by_uid(users::get_current_uid());
app_root.push_str(user.unwrap());
app_root.push_str(".");
app_root.push_str(hostname::get_hostname().unwrap());
} )
}
pub fn save<P: AsRef<Path>, B: AsRef<[u8]>>(filename: P, data: B) -> () {
let path = filename.as_ref();
let parent = path.parent().unwrap();
if parent.exists() == false {
mkdir(parent);
}
fs::File::create(path).unwrap().write_all(data.as_ref()).unwrap();
}
pub fn load<P: AsRef<Path>>(filename: P) -> Vec<u8> {
let mut file = fs::File::open(filename).unwrap();
let mut contents = Vec::new();
file.read_to_end(&mut contents).unwrap();
contents
}
pub fn rm<P: AsRef<Path>>(path: P) {
fs::remove_file(path).unwrap();
}
pub fn rmdir<P: AsRef<Path>>(path: P) {
fs::remove_dir_all(path).unwrap();
}
pub fn mkdir<P: AsRef<Path>>(path: P) {
fs::create_dir_all(path).unwrap();
}
fn mv_ll<P: AsRef<Path>>(old: P, new: P) {
rmdir(&new);
mkdir(&new);
fs::rename(old, new).unwrap();
}
pub fn mv<P: AsRef<Path>>(old: P, new: P) {
mv_ll(old, new);
}
pub fn get_permissions<P: AsRef<Path>>(name: P) -> fs::Permissions {
let file = fs::File::open(name).unwrap();
file.metadata().unwrap().permissions()
}
pub fn set_permissions<P: AsRef<Path>>(name: P, permissions: fs::Permissions) -> () {
let file = fs::File::open(name).unwrap();
file.set_permissions(permissions).unwrap()
}
fn fnrm_first<P: AsRef<Path>>(input: P) -> String {
let input = input.as_ref().to_str().unwrap();
let index = input.find('/').unwrap();
let mut input = input.to_string();
let t: String = input.drain(index+1..).collect();
t
}
pub fn copy<P: AsRef<Path>>(src: P, dst: P) -> Result<(), String> {
let src = src.as_ref();
let dst = dst.as_ref();
if let Some(pt) = path_type(src) {
match pt {
PathType::File => {
let permissions = get_permissions(src);
let data = load(src);
save(dst, data.as_slice());
set_permissions(dst, permissions);
Ok(())
}
PathType::Folder => {
if let Ok(dir_iter) = fs::read_dir(src) {
for entry in dir_iter {
if let Ok(entry) = entry {
let path = entry.path();
let apnd = fnrm_first(&path);
let dest = dst.join(&apnd);
copy(path, dest)?;
} else {
return Err("intermitten\
t io".to_string())
}
}
Ok(())
} else {
Err(format!("Couldn't copy folder {:?} \
because it lacks read \
permission", src))
}
}
}
} else {
Err(format!("Couldn't copy {:?} because it doesn't exist.", src))
}
}
pub fn get_exists(filepath: &str) -> bool {
Path::new(filepath).exists()
}
pub fn path_type<P: AsRef<Path>>(path: P) -> Option<PathType> {
let path = path.as_ref();
if path.exists() == false {
None
} else if path.is_file() == true {
Some(PathType::File)
} else if path.is_dir() == true {
Some(PathType::Folder)
} else {
panic!("Filesystem contains mysterious entity (Not a file or a \
folder)!");
}
}