fixed ordering of dir bars / overall bar; fixed overall offset when resuming

This commit is contained in:
epi
2023-02-27 19:25:17 -06:00
parent 339189ff13
commit 4b7a25c13b
5 changed files with 41 additions and 21 deletions

View File

@@ -24,7 +24,9 @@ pub enum Command {
AddStatus(StatusCode),
/// Create the progress bar (`BarType::Total`) that is updated from the stats thread
CreateBar,
///
/// the u64 value is the offset at which to start the progress bar (can be 0)
CreateBar(u64),
/// Add to a `Stats` field that corresponds to the given `StatField` by the given `usize` value
AddToUsizeField(StatField, usize),

View File

@@ -115,8 +115,9 @@ impl StatsHandler {
}
}
Command::AddToF64Field(field, value) => self.stats.update_f64_field(field, value),
Command::CreateBar => {
Command::CreateBar(offset) => {
self.bar = add_bar("", self.stats.total_expected() as u64, BarType::Total);
self.bar.set_position(offset);
}
Command::LoadStats(filename) => {
self.stats.merge_from(&filename)?;

View File

@@ -102,8 +102,20 @@ async fn scan(targets: Vec<String>, handles: Arc<Handles>) -> Result<()> {
// having been set, makes it so the progress bar doesn't flash as full before anything has
// even happened
if matches!(handles.config.output_level, OutputLevel::Default) {
let mut total_offset = 0;
if let Ok(guard) = handles.scans.read() {
if let Some(handle) = &*guard {
if let Ok(scans) = handle.data.scans.read() {
for scan in scans.iter() {
total_offset += scan.requests_made_so_far();
}
}
}
}
// only create the bar if no --silent|--quiet
handles.stats.send(CreateBar)?;
handles.stats.send(CreateBar(total_offset))?;
// blocks until the bar is created / avoids race condition in first two bars
handles.stats.sync().await?;

View File

@@ -44,6 +44,12 @@ pub struct FeroxScan {
/// Number of requests to populate the progress bar with
pub(super) num_requests: u64,
/// Number of requests made so far, only used during deserialization
///
/// serialization: saves self.requests() to this field
/// deserialization: sets self.requests_made_so_far to this field
pub(super) requests_made_so_far: u64,
/// Status of this scan
pub status: Mutex<ScanStatus>,
@@ -80,6 +86,7 @@ impl Default for FeroxScan {
task: sync::Mutex::new(None), // tokio mutex
status: Mutex::new(ScanStatus::default()),
num_requests: 0,
requests_made_so_far: 0,
scan_order: ScanOrder::Latest,
url: String::new(),
normalized_url: String::new(),
@@ -122,6 +129,11 @@ impl FeroxScan {
&self.url
}
/// getter for number of requests made during previously saved scans (i.e. --resume-from used)
pub fn requests_made_so_far(&self) -> u64 {
self.requests_made_so_far
}
/// small wrapper to set the JoinHandle
pub async fn set_task(&self, task: JoinHandle<()>) -> Result<()> {
let mut guard = self.task.lock().await;
@@ -162,6 +174,8 @@ impl FeroxScan {
let pb = add_bar(&self.url, self.num_requests, bar_type);
pb.reset_elapsed();
pb.set_position(self.requests_made_so_far);
let _ = std::mem::replace(&mut *guard, Some(pb.clone()));
pb
@@ -368,7 +382,6 @@ impl<'de> Deserialize<'de> for FeroxScan {
D: Deserializer<'de>,
{
let mut scan = Self::default();
let mut progress_bar_position = 0;
let map: HashMap<String, Value> = HashMap::deserialize(deserializer)?;
@@ -416,15 +429,13 @@ impl<'de> Deserialize<'de> for FeroxScan {
}
"requests_made_so_far" => {
if let Some(requests_made_so_far) = value.as_u64() {
progress_bar_position = requests_made_so_far;
scan.requests_made_so_far = requests_made_so_far;
}
}
_ => {}
}
}
scan.progress_bar().set_position(progress_bar_position);
Ok(scan)
}
}
@@ -513,6 +524,7 @@ mod tests {
scan_type: ScanType::Directory,
scan_order: ScanOrder::Initial,
num_requests: 0,
requests_made_so_far: 0,
status: Mutex::new(ScanStatus::Running),
task: Default::default(),
progress_bar: Mutex::new(None),

View File

@@ -247,21 +247,12 @@ fn ferox_scan_deserialize() {
}
match fs.progress_bar.lock() {
Ok(guard) => {
match guard.as_ref() {
Some(pb) => {
// position based on the requests made so far
//
// note: when this goes through the actual deserialize function, the
// progress bar will be set to the total requests made so far, -10%
// (i.e. 450 in this case)
assert_eq!(pb.position(), 500);
}
None => {
panic!();
}
Ok(guard) => match guard.as_ref() {
Some(_) => {
panic!();
}
}
None => {}
},
Err(_) => {
panic!();
}
@@ -573,6 +564,7 @@ fn feroxscan_display() {
scan_order: ScanOrder::Latest,
scan_type: Default::default(),
num_requests: 0,
requests_made_so_far: 0,
start_time: Instant::now(),
output_level: OutputLevel::Default,
status_403s: Default::default(),
@@ -618,6 +610,7 @@ async fn ferox_scan_abort() {
scan_order: ScanOrder::Latest,
scan_type: Default::default(),
num_requests: 0,
requests_made_so_far: 0,
start_time: Instant::now(),
output_level: OutputLevel::Default,
status_403s: Default::default(),