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
use super::{
ChangedContentVisitOutcome, CompactScanReport, ContentVisitControl, ContentVisitEvent,
ContentVisitMode, ContentVisitReport, FinishedContentVisit, Result, Scanner,
apply_total_bytes_limit, discover_compact, finish_content_report, run_workers,
visit_changed_content_plan, visit_content_direct,
};
use crate::watch::WatchPlan;
impl Scanner {
/// Visits selected file bytes once with bounded parallelism.
///
/// Ignore rules, file limits, path safety, binary detection, content
/// hashing, and revision evidence use the same scanner configuration. A
/// worker-local visitor is created by `factory`; callback order is
/// intentionally concurrent. Every event carries a monotonic work
/// sequence plus its root and normalized relative path; use the paths for
/// deterministic cross-run ordering.
///
/// `ContentVisitControl::SkipFile` stops delivering chunks for the current
/// file. The scanner still finishes reading when hashing or binary
/// detection requires complete evidence. `ContentVisitControl::Quit`
/// cooperatively cancels every worker.
///
/// # Errors
///
/// Returns root, traversal, content I/O, or worker-submission failures
/// according to the configured error policy.
///
/// # Panics
///
/// Propagates a panic from the factory or visitor after active workers
/// observe cancellation.
pub fn visit_content<Factory, Visitor>(self, factory: Factory) -> Result<ContentVisitReport>
where
Factory: Fn(usize) -> Visitor + Send + Sync + 'static,
Visitor:
for<'event> FnMut(ContentVisitEvent<'event>) -> ContentVisitControl + Send + 'static,
{
self.visit_content_with_root(0, factory)
}
/// Visits selected file bytes once and returns the exact compact manifest
/// produced by those same verified reads.
///
/// This additive API lets parsers consume bytes and retain incremental scan
/// evidence without changing the long-standing [`ContentVisitReport`]
/// construction contract used by existing scanner consumers.
///
/// # Errors
///
/// Returns the same errors as [`Self::visit_content`].
///
/// # Panics
///
/// Propagates callback panics like [`Self::visit_content`].
pub fn visit_content_manifest<Factory, Visitor>(
self,
factory: Factory,
) -> Result<CompactScanReport>
where
Factory: Fn(usize) -> Visitor + Send + Sync + 'static,
Visitor:
for<'event> FnMut(ContentVisitEvent<'event>) -> ContentVisitControl + Send + 'static,
{
self.visit_content_with_root_mode_finished(0, ContentVisitMode::Revision, factory)
.map(FinishedContentVisit::into_manifest)
}
/// Visits selected bytes without retaining a selected-file manifest or
/// computing a revision.
///
/// Typed skip evidence is still retained when `EvidenceMode::Complete` is
/// configured. Use `selected_files_only()` as well for constant-memory
/// summary reporting.
///
/// # Errors
///
/// Returns the same errors as [`Self::visit_content`].
///
/// # Panics
///
/// Propagates callback panics like [`Self::visit_content`].
pub fn visit_content_streaming<Factory, Visitor>(
self,
factory: Factory,
) -> Result<ContentVisitReport>
where
Factory: Fn(usize) -> Visitor + Send + Sync + 'static,
Visitor:
for<'event> FnMut(ContentVisitEvent<'event>) -> ContentVisitControl + Send + 'static,
{
self.visit_content_with_root_mode(0, ContentVisitMode::Streaming, factory)
}
/// Visits only safe changed-file paths from a watcher plan.
///
/// No directory traversal occurs. Plans that can affect directory
/// structure or file-selection rules return
/// [`ChangedContentVisitOutcome::FullRescanRequired`] before invoking the
/// factory. Removed paths are returned separately.
///
/// # Errors
///
/// Returns root, matcher, content I/O, or worker-submission failures
/// according to the configured error policy.
///
/// # Panics
///
/// Propagates a panic from the factory or visitor after active workers
/// observe cancellation.
pub fn visit_changed_content<Factory, Visitor>(
self,
plan: &WatchPlan,
factory: Factory,
) -> Result<ChangedContentVisitOutcome>
where
Factory: Fn(usize) -> Visitor + Send + Sync + 'static,
Visitor:
for<'event> FnMut(ContentVisitEvent<'event>) -> ContentVisitControl + Send + 'static,
{
visit_changed_content_plan(self, plan, ContentVisitMode::Revision, factory)
}
/// Visits only changed-file bytes without retaining their compact manifest
/// or computing a subset revision.
///
/// # Errors
///
/// Returns the same errors as [`Self::visit_changed_content`].
pub fn visit_changed_content_streaming<Factory, Visitor>(
self,
plan: &WatchPlan,
factory: Factory,
) -> Result<ChangedContentVisitOutcome>
where
Factory: Fn(usize) -> Visitor + Send + Sync + 'static,
Visitor:
for<'event> FnMut(ContentVisitEvent<'event>) -> ContentVisitControl + Send + 'static,
{
visit_changed_content_plan(self, plan, ContentVisitMode::Streaming, factory)
}
pub(crate) fn visit_content_with_root<Factory, Visitor>(
self,
root_index: usize,
factory: Factory,
) -> Result<ContentVisitReport>
where
Factory: Fn(usize) -> Visitor + Send + Sync + 'static,
Visitor:
for<'event> FnMut(ContentVisitEvent<'event>) -> ContentVisitControl + Send + 'static,
{
self.visit_content_with_root_mode(root_index, ContentVisitMode::Revision, factory)
}
pub(crate) fn visit_content_with_root_mode<Factory, Visitor>(
self,
root_index: usize,
mode: ContentVisitMode,
factory: Factory,
) -> Result<ContentVisitReport>
where
Factory: Fn(usize) -> Visitor + Send + Sync + 'static,
Visitor:
for<'event> FnMut(ContentVisitEvent<'event>) -> ContentVisitControl + Send + 'static,
{
self.visit_content_with_root_mode_finished(root_index, mode, factory)
.map(|finished| finished.report)
}
fn visit_content_with_root_mode_finished<Factory, Visitor>(
self,
root_index: usize,
mode: ContentVisitMode,
factory: Factory,
) -> Result<FinishedContentVisit>
where
Factory: Fn(usize) -> Visitor + Send + Sync + 'static,
Visitor:
for<'event> FnMut(ContentVisitEvent<'event>) -> ContentVisitControl + Send + 'static,
{
if self.options.limits.max_total_bytes.is_none()
&& self.options.content_discovery == crate::ContentDiscoveryMode::Streaming
&& !self.runtime.is_worker_thread()
{
return visit_content_direct(self, root_index, mode, factory);
}
let mut discovery_options = self.options.clone();
discovery_options.detect_binary_files = true;
let (mut evidence, mut files, scan_runtime) =
discover_compact(&self.root, &discovery_options, &self.runtime)?;
files.sort_unstable_by(|left, right| left.relative.cmp(&right.relative));
apply_total_bytes_limit(&mut evidence, &mut files, &self.options);
let discovered = u64::try_from(files.len()).unwrap_or(u64::MAX);
let cancellation = self.options.cancellation.clone().unwrap_or_default();
let mut visit_options = self.options.clone();
visit_options.cancellation = Some(cancellation.clone());
let workers = visit_options
.content_visit_worker_count(files.len())
.min(self.runtime.parallelism())
.max(1);
let worker_reports = run_workers(
evidence.root.clone(),
files,
visit_options,
&scan_runtime,
&self.runtime,
workers,
root_index,
mode,
factory,
)?;
Ok(finish_content_report(
evidence,
discovered,
worker_reports,
mode,
))
}
}