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
//! Fast, `.gitignore`-aware discovery of everything we can triplify.
use {
crate::{
location::Location,
s3::{S3Store, S3Url},
source::SourceKind,
},
std::{
path::PathBuf,
sync::{
Arc,
atomic::{AtomicU64, Ordering},
},
},
tokio::sync::mpsc,
tracing::{error, warn},
};
/// A source worth triplifying.
#[derive(Debug, Clone)]
pub struct Candidate {
pub location: Location,
pub kind: SourceKind,
}
/// Walks local roots on a blocking thread and lists S3 prefixes, streaming
/// candidates as they are found.
#[derive(Debug, Clone)]
pub struct Walker {
roots: Vec<PathBuf>,
s3: Vec<S3Url>,
}
impl Walker {
pub fn new(roots: Vec<Location>) -> Self {
let mut walker = Self { roots: Vec::new(), s3: Vec::new() };
for root in roots {
match root {
Location::Local(p) => walker.roots.push(p),
Location::S3(u) => walker.s3.push(u),
}
}
walker
}
/// Start walking. `failed` counts roots that could not be listed.
pub fn spawn(
self,
capacity: usize,
store: Arc<S3Store>,
failed: Arc<AtomicU64>,
) -> mpsc::Receiver<Candidate> {
let (tx, rx) = mpsc::channel(capacity);
for url in self.s3.clone() {
let (tx, store, failed) = (
tx.clone(),
Arc::clone(&store),
Arc::clone(&failed),
);
tokio::spawn(async move {
match store.list(&url).await {
Ok(objects) => {
for object in objects {
let Some(kind) =
SourceKind::from_name(object.file_name())
else {
continue;
};
let location = Location::S3(object);
if tx
.send(Candidate { location, kind })
.await
.is_err()
{
return;
}
}
},
Err(e) => {
error!("{url}: {e:#}");
failed.fetch_add(1, Ordering::Relaxed);
},
}
});
}
if !self.roots.is_empty() {
tokio::task::spawn_blocking(move || self.walk(&tx));
}
rx
}
fn walk(self, tx: &mpsc::Sender<Candidate>) {
let Some((first, rest)) = self.roots.split_first() else {
return
};
let mut builder = ignore::WalkBuilder::new(first);
for root in rest {
builder.add(root);
}
builder
.hidden(true)
.git_ignore(true)
.git_exclude(true)
.git_global(true)
.parents(true);
builder.build_parallel().run(|| {
let tx = tx.clone();
Box::new(move |entry| {
use ignore::WalkState::{Continue, Quit};
let entry = match entry {
Ok(e) => e,
Err(e) => {
warn!("{e}");
return Continue;
},
};
if !entry.file_type().is_some_and(|t| t.is_file()) {
return Continue;
}
let path = entry.into_path();
// Skip Excel lock files like `~$Book.xlsx`.
if path
.file_name()
.and_then(|n| n.to_str())
.is_some_and(|n| n.starts_with("~$"))
{
return Continue;
}
let Some(kind) = SourceKind::from_path(&path) else {
return Continue
};
if tx
.blocking_send(Candidate {
location: Location::Local(path),
kind,
})
.is_err()
{
Quit
} else {
Continue
}
})
});
}
}