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
use std::{env, fs};
use std::fs::{DirBuilder, remove_file};
use std::path::{Path, PathBuf};
use json::{JsonValue, object};
use log::error;
pub fn root_path(path: &str) -> &'static str {
if PathBuf::from(path) != env::current_dir().unwrap() {
let mut dir = env::current_exe().unwrap();
dir.pop();
env::set_current_dir(dir).unwrap();
}
let root_path = env::current_dir().unwrap();
Box::leak(root_path.to_str().unwrap().to_string().into_boxed_str())
}
pub fn create_dir(mut path: &str) -> bool {
if path.contains(".") {
let data = Path::new(path);
let data = data.parent().unwrap();
path = data.to_str().unwrap();
}
match DirBuilder::new().recursive(true).create(path) {
Ok(_) => {
return true;
}
Err(_e) => {
return false;
}
}
}
pub fn dir_files(path: &str) -> Vec<String> {
let mut files = vec![];
for entry in walkdir::WalkDir::new(path) {
match entry {
Ok(e) => {
if e.path().is_dir() {
continue;
}
files.push(e.path().display().to_string())
}
_ => {}
}
}
return files;
}
pub fn get_file_list(path: &str) -> Vec<String> {
let mut files = vec![];
let paths = fs::read_dir(path).unwrap();
for path in paths {
match path {
Ok(e) => {
if e.path().is_file() {
files.push(e.file_name().to_str().unwrap().to_string().clone());
}
}
Err(_) => {}
}
}
return files;
}
pub fn get_dir_list(path: &str) -> Vec<String> {
let mut dirs = vec![];
let paths = fs::read_dir(path).unwrap();
for path in paths {
match path {
Ok(e) => {
if e.path().is_dir() {
dirs.push(e.file_name().to_str().unwrap().to_string().clone());
}
}
Err(_) => {}
}
}
return dirs;
}
pub fn file_content_get(path: &str) -> String {
let data = fs::read_to_string(path);
match data {
Ok(content) => {
return content;
}
Err(e) => {
error!("{}", e);
return "".to_string();
}
}
}
pub fn file_content_get_stream(path: &str) -> String {
let file = fs::read(path).unwrap();
let contents = unsafe { String::from_utf8_unchecked(file) };
contents
}
pub fn file_content_get_json(path: &str) -> JsonValue {
if !is_file(path) {
return object! {};
}
let data = file_content_get(path);
if data == "" {
return object! {};
}
json::parse(data.as_str()).unwrap()
}
pub fn file_content_put(path: &str, data: &str) -> bool {
create_dir(path);
let data = fs::write(path, data);
match data {
Ok(_) => {
return true;
}
Err(e) => {
error!("{}", e);
return false;
}
}
}
pub fn file_content_put_u8(path: &str, data: Vec<u8>) -> bool {
create_dir(path);
let data = fs::write(path, data);
match data {
Ok(_) => {
return true;
}
Err(e) => {
error!("{}", e);
return false;
}
}
}
pub fn is_file(file: &str) -> bool {
let o = Path::new(file);
return o.is_file();
}
pub fn is_dir(path: &str) -> bool {
let o = Path::new(path);
return o.is_dir();
}
pub fn remove(path: &str) -> bool {
let res = remove_file(path);
match res {
Ok(_) => {
true
}
Err(e) => {
error!("remove file error:{}", e);
false
}
}
}