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
use clap::ArgMatches;
use errors::Result;
use filetime::set_file_times;
use filetime::FileTime;
use std::fs;
use std::path::Path;
use std::path::PathBuf;
pub fn arg_as_string_vec(matches: &ArgMatches, option: &str) -> Vec<String> {
matches
.values_of(option)
.map(|vs| vs.map(|s| s.to_string()).collect())
.unwrap_or(vec![])
}
pub fn copy_and_sync_file<P: AsRef<Path>, Q: AsRef<Path>>(from: P, to: Q) -> Result<()> {
let from = &from.as_ref();
let to = &to.as_ref();
if to.exists() {
let mut permissions = fs::metadata(&to)?.permissions();
if permissions.readonly() {
permissions.set_readonly(false);
fs::set_permissions(&to, permissions)?;
}
}
trace!("copy {:?} to {:?}", from, to);
fs::copy(&from, &to)?;
let from_metadata = from.metadata()?;
let atime = FileTime::from_last_access_time(&from_metadata);
let mtime = FileTime::from_last_modification_time(&from_metadata);
set_file_times(&to, atime, mtime)?;
Ok(())
}
pub fn path_to_str<'a>(path: &'a Path) -> Result<&'a str> {
Ok(path
.to_str()
.ok_or(format!("Path is invalid '{}'", path.display()))?)
}
pub fn normalize_path(path: &Path) -> PathBuf {
PathBuf::from(path.to_string_lossy().replace("\\", "/"))
}
pub fn contains_file_with_ext(dir_path: &Path, ext: &str) -> bool {
if !dir_path.is_dir() {
return false;
};
if let Ok(path) = dir_path.read_dir() {
for file in path {
if let Ok(file) = file {
if file.file_name().to_string_lossy().ends_with(ext) {
return true;
}
}
}
}
false
}
pub fn destructure_path<P: AsRef<Path>>(path: P) -> Option<(PathBuf, String)> {
let path = path.as_ref();
path.file_name()
.and_then(|it| it.to_str())
.map(|name| (path.to_path_buf(), name.to_string()))
}
pub fn file_has_ext(file_path: &Path, ext: &str) -> bool {
file_path.is_file()
&& file_path
.file_name()
.and_then(|it| it.to_str())
.map(|it| it.ends_with(ext))
.unwrap_or(false)
}
pub fn is_library(file_path: &Path) -> bool {
file_path.is_file()
&& file_path
.file_name()
.and_then(|it| it.to_str())
.map(|it| {
it.ends_with(".so")
|| it.contains(".so.")
|| it.ends_with(".dylib")
|| it.ends_with(".a")
})
.unwrap_or(false)
}
pub fn lib_name_from(file_path: &Path) -> Result<String> {
let file_name = file_path
.file_name()
.and_then(|it| it.to_str())
.ok_or(format!(
"'{}' doesn't point to a valid lib name",
file_path.display()
))?;
let (start_index, end_index) = file_name
.find(".so")
.map(|end_index| (if file_name.starts_with("lib") { 3 } else { 0 }, end_index))
.unwrap_or((0, file_name.len()));
if start_index == end_index {
bail!(
"'{}' doesn't point to a valid lib name",
file_path.display()
);
} else {
Ok(file_name[start_index..end_index].to_string())
}
}
pub fn file_name_as_str(file_path: &Path) -> Result<&str> {
Ok(file_path
.file_name()
.and_then(|it| it.to_str())
.ok_or(format!(
"'{}' is not a valid file name",
file_path.display()
))?)
}