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
use std::{env, fs};
use std::fs::{File, OpenOptions};
use std::io::{BufReader, Read, Seek, SeekFrom, Write};
use std::path::Path;
use crate::date_time_ext::DateTimeExt;
use crate::symbol::SYMBOL;
#[cfg(test)]
mod tests;
mod date_time_ext;
mod symbol;
pub struct FileExt;
impl FileExt {
pub fn read_file_partially(filepath: &str, start: u64, end: u64) -> Result<Vec<u8>, String> {
let mut file_content = Vec::new();
let buff_length = (end - start) + 1;
let boxed_open = File::open(filepath);
if boxed_open.is_err() {
let error_msg = boxed_open.err().unwrap();
let error = format!("<p>Unable to open file: {}</p> <p>error: {}</p>", filepath, error_msg);
return Err(error)
}
let file = boxed_open.unwrap();
let mut reader = BufReader::new(file);
let boxed_seek = reader.seek(SeekFrom::Start(start));
if boxed_seek.is_ok() {
let boxed_read = reader.take(buff_length).read_to_end(&mut file_content);
if boxed_read.is_err() {
let error_msg = boxed_read.err().unwrap().to_string();
let error = format!("<p>Unable to read file: {}</p> <p>error: {}</p>", filepath, error_msg);
return Err(error)
}
} else {
let error_msg = boxed_seek.err().unwrap().to_string();
let error = format!("<p>Unable to seek file: {}</p> <p>error: {}</p>", filepath, error_msg);
return Err(error)
}
Ok(file_content)
}
pub fn read_file(filepath: &str) -> Result<Vec<u8>, String> {
let mut file_content = Vec::new();
let boxed_open = File::open(filepath);
if boxed_open.is_err() {
let error_msg = boxed_open.err().unwrap();
let error = format!("<p>Unable to open file: {}</p> <p>error: {}</p>", filepath, error_msg);
return Err(error)
} else {
let mut file = boxed_open.unwrap();
let boxed_read= file.read_to_end(&mut file_content);
if boxed_read.is_err() {
let error_msg = boxed_read.err().unwrap();
let error = format!("<p>Unable to read file: {}</p> <p>error: {}</p>", filepath, error_msg);
return Err(error)
}
}
Ok(file_content)
}
pub fn file_modified_utc(filepath: &str) -> Result<u128, String> {
let boxed_open = File::open(filepath);
if boxed_open.is_err() {
let error_msg = boxed_open.err().unwrap();
let error = format!("<p>Unable to open file: {}</p> <p>error: {}</p>", filepath, error_msg);
return Err(error)
}
let file : File = boxed_open.unwrap();
let boxed_metadata = file.metadata();
if boxed_metadata.is_err() {
let error_msg = boxed_metadata.err().unwrap();
let error = format!("<p>Unable to open file: {}</p> <p>error: {}</p>", filepath, error_msg);
return Err(error)
}
let metadata = boxed_metadata.unwrap();
let boxed_last_modified_time = metadata.modified();
if boxed_last_modified_time.is_err() {
let error_msg = boxed_last_modified_time.err().unwrap();
let error = format!("<p>Unable to open file: {}</p> <p>error: {}</p>", filepath, error_msg);
return Err(error)
}
let modified_time = boxed_last_modified_time.unwrap();
let nanos = DateTimeExt::_system_time_to_unix_nanos(modified_time);
Ok(nanos)
}
pub fn get_static_filepath(path: &str) -> Result<String, String> {
let boxed_dir = env::current_dir();
if boxed_dir.is_err() {
let error = boxed_dir.err().unwrap();
eprintln!("{}", error);
return Err(error.to_string());
}
let dir = boxed_dir.unwrap();
let boxed_working_directory = dir.as_path().to_str();
if boxed_working_directory.is_none() {
let error = "working directory is not set";
eprintln!("{}", error);
return Err(error.to_string());
}
let working_directory = boxed_working_directory.unwrap();
let absolute_path = [working_directory, path].join(SYMBOL.empty_string);
Ok(absolute_path)
}
pub fn read_or_create_and_write(path: &str, content: &[u8]) -> Result<Vec<u8>, String> {
let does_passphrase_exist = Self::does_file_exist(path);
return if does_passphrase_exist {
let boxed_read = Self::read_file(path);
if boxed_read.is_err() {
return Err(boxed_read.err().unwrap());
}
let passphrase = boxed_read.unwrap();
Ok(passphrase)
} else {
let boxed_create = Self::create_file(path);
if boxed_create.is_err() {
let message = boxed_create.err().unwrap();
return Err(message)
}
let boxed_write = Self::write_file(path, content);
if boxed_write.is_err() {
let message = boxed_write.err().unwrap();
return Err(message)
}
Ok(Vec::from(content))
}
}
pub fn create_file(path: &str) -> Result<File, String> {
let boxed_file = File::create(path);
if boxed_file.is_err() {
let message = format!("unable to create file: {}", boxed_file.err().unwrap());
return Err(message)
}
let file = boxed_file.unwrap();
Ok(file)
}
pub fn does_file_exist(path: &str) -> bool {
let file_exists = Path::new(path).is_file();
file_exists
}
pub fn write_file(path: &str, file_content: &[u8]) -> Result<(), String> {
let mut file = OpenOptions::new()
.read(false)
.write(true)
.create(false)
.truncate(false)
.open(path)
.unwrap();
let boxed_write = file.write_all(file_content);
if boxed_write.is_err() {
let message = format!("unable to write to file: {}", boxed_write.err().unwrap());
return Err(message)
}
Ok(())
}
pub fn is_symlink(path: &str) -> Result<bool, String> {
let boxed_symlink_metadata = fs::symlink_metadata(path);
if boxed_symlink_metadata.is_err() {
let msg = boxed_symlink_metadata.err().unwrap().to_string();
return Err(msg)
}
let symlink_metadata = boxed_symlink_metadata.unwrap();
Ok(symlink_metadata.file_type().is_symlink())
}
pub fn symlink_points_to(path: &str) -> Result<String, String> {
let boxed_path_buff = fs::read_link(path);
if boxed_path_buff.is_err() {
let msg = boxed_path_buff.err().unwrap().to_string();
return Err(msg)
}
let path_buff = boxed_path_buff.unwrap();
let boxed_points_to = path_buff.as_path().to_str();
if boxed_points_to.is_none() {
let msg = "unable to read link as path".to_string();
return Err(msg)
}
let points_to = boxed_points_to.unwrap();
Ok(points_to.to_string())
}
}