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
use {
crate::{
util::{is_executable, is_executable_path, split_path_env},
IchwhResult,
},
async_std::{
fs::read_dir,
path::{Path, PathBuf},
},
cfg_if::cfg_if,
futures::{
future::join_all,
stream::{FuturesOrdered, StreamExt},
},
};
#[cfg(windows)]
use {
crate::util::{filename_matches, pathext},
std::collections::HashMap,
};
pub async fn which(bin: &str) -> IchwhResult<Option<PathBuf>> {
if let local_expanded @ Ok(Some(_)) = check_local_file(bin).await {
return local_expanded;
}
let dirs = split_path_env()?;
let mut read_stream = dirs
.into_iter()
.map(|dir| async move { which_in_dir(bin, &dir).await })
.collect::<FuturesOrdered<_>>();
while let Some(res) = read_stream.next().await {
if let res @ Ok(Some(_)) = res {
return res;
}
}
Ok(None)
}
pub async fn which_all(bin: &str) -> IchwhResult<Vec<PathBuf>> {
if let Some(local_expanded) = check_local_file(bin).await? {
return Ok(vec![local_expanded]);
}
cfg_if! {
if #[cfg(unix)] {
let read_dir = |dir| async move {
which_in_dir(bin, &dir).await
};
} else if #[cfg(windows)] {
let read_dir = |dir| async move {
which_all_in_dir(bin, &dir).await
};
}
}
let read_futures = split_path_env()?.into_iter().map(read_dir);
cfg_if! {
if #[cfg(unix)] {
let rtn = join_all(read_futures)
.await
.into_iter()
.filter_map(|res| match res {
Ok(res) => res,
Err(_) => None
})
.collect();
} else if #[cfg(windows)] {
let rtn = join_all(read_futures)
.await
.into_iter()
.filter_map(|res| res.ok())
.flatten()
.collect();
}
};
Ok(rtn)
}
pub async fn which_in_dir<P: AsRef<Path>>(bin: &str, path: P) -> IchwhResult<Option<PathBuf>> {
#[cfg(windows)]
{
let matching_entries = which_all_in_dir(bin, path).await?;
Ok(matching_entries.get(0).cloned())
}
#[cfg(unix)]
{
let mut entries = read_dir(path).await?;
while let Some(entry) = entries.next().await.transpose()? {
if is_executable(&entry).await? && entry.file_name() == bin {
return Ok(Some(entry.path()));
}
}
Ok(None)
}
}
#[cfg(windows)]
pub(crate) async fn which_all_in_dir<P: AsRef<Path>>(
bin: &str,
path: P,
) -> IchwhResult<Vec<PathBuf>> {
let mut matches = read_dir(path)
.await?
.filter_map(|entry| async {
if let Ok(entry) = entry {
let is_exec = is_executable(&entry).await;
if is_exec.is_ok() && is_exec.unwrap() && filename_matches(&bin, &entry) {
return Some(entry.path());
}
}
None
})
.collect::<Vec<_>>()
.await;
let exts = pathext()?
.into_iter()
.enumerate()
.map(|(a, b)| (b, a))
.collect::<HashMap<_, _>>();
matches.sort_by(|a, b| {
let a_ext = a
.extension()
.unwrap()
.to_string_lossy()
.to_ascii_uppercase();
let b_ext = b
.extension()
.unwrap()
.to_string_lossy()
.to_ascii_uppercase();
exts[&a_ext].cmp(&exts[&b_ext])
});
Ok(matches)
}
async fn check_local_file(bin: &str) -> IchwhResult<Option<PathBuf>> {
cfg_if! {
if #[cfg(unix)] {
let is_path = bin.contains('/');
} else if #[cfg(windows)] {
let is_path = true;
}
}
if is_path {
let path = Path::new(bin);
if let Some(actual_path) = is_executable_path(&path).await? {
return Ok(Some(actual_path));
}
}
Ok(None)
}
#[cfg(unix)]
pub async fn symlink_chain(bin: &str) -> IchwhResult<Vec<PathBuf>> {
if let Some(path) = which(bin).await? {
let rtn = follow_symlink_chain(&path).await?;
Ok(rtn)
} else {
Ok(vec![])
}
}
#[cfg(unix)]
pub async fn symlink_chain_in_dir<P: AsRef<Path>>(bin: &str, dir: P) -> IchwhResult<Vec<PathBuf>> {
if let Some(path) = which_in_dir(bin, dir).await? {
let rtn = follow_symlink_chain(&path).await?;
Ok(rtn)
} else {
Ok(vec![])
}
}
#[cfg(unix)]
async fn follow_symlink_chain<P: AsRef<Path>>(root: P) -> IchwhResult<Vec<PathBuf>> {
let mut path = root.as_ref().to_path_buf();
let mut rtn = vec![path.clone()];
while path.symlink_metadata().await?.file_type().is_symlink() {
let pointee = async_std::fs::read_link(&path).await?;
path = if pointee.is_relative() {
let mut full = path.parent().unwrap().to_path_buf();
full.push(pointee);
full
} else {
pointee
};
rtn.push(path.clone());
}
Ok(rtn)
}