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
use {
crate::{IchwhError, IchwhResult},
async_std::{
fs::DirEntry,
path::{Path, PathBuf},
},
std::os::unix::fs::PermissionsExt,
};
pub fn split_path_env() -> IchwhResult<Vec<PathBuf>> {
let path_var = std::env::var_os("PATH").ok_or(IchwhError::PathNotDefined)?;
Ok(std::env::split_paths(&path_var).map(Into::into).collect())
}
pub async fn is_executable(file: &DirEntry) -> IchwhResult<bool> {
let metadata = file.metadata().await?;
let filetype = metadata.file_type();
let permissions = metadata.permissions();
Ok((filetype.is_file() || filetype.is_symlink()) && (permissions.mode() & 0o111 != 0))
}
pub async fn is_executable_path<P: AsRef<Path>>(path: P) -> IchwhResult<Option<PathBuf>> {
let metadata = path.as_ref().metadata().await?;
let filetype = metadata.file_type();
let permissions = metadata.permissions();
if (filetype.is_file() || filetype.is_symlink()) && (permissions.mode() & 0o111 != 0) {
Ok(Some(path.as_ref().canonicalize().await?))
} else {
Ok(None)
}
}