summaryrefslogtreecommitdiff
path: root/src/files.rs
diff options
context:
space:
mode:
authorjakka <jakka@jakka.su>2025-10-08 14:27:11 +0300
committerjakka <jakka@jakka.su>2025-10-08 14:27:11 +0300
commitd9871c81dd945ea188d9d7a31993b7bb1735d257 (patch)
tree51067d0c1a99b82e5f1efb76cac12c010c0ffcd1 /src/files.rs
parent7f9aa7d6c650b8d32dc7eca02975c3e876c72c9d (diff)
better file scanning logic - stopped walking down the filetree two times, now files are sent thru channels from a walker thread
Diffstat (limited to 'src/files.rs')
-rw-r--r--src/files.rs66
1 files changed, 38 insertions, 28 deletions
diff --git a/src/files.rs b/src/files.rs
index 3b00de1..995e938 100644
--- a/src/files.rs
+++ b/src/files.rs
@@ -11,6 +11,7 @@ use std::{
sync::{
Arc, Mutex,
atomic::{AtomicBool, AtomicUsize, Ordering},
+ mpsc,
},
thread::{self, sleep},
time::{Duration, UNIX_EPOCH},
@@ -83,40 +84,49 @@ pub(crate) fn index_files_recursively(
let bar = ProgressBar::with_draw_target(Some(0), ProgressDrawTarget::stdout_with_hz(60))
.with_style(ProgressStyle::with_template(BAR_TEMPLATE)?.progress_chars("#>-"))
.with_message("Indexing");
+ thread::scope(|s| {
+ let (filesend, filerecv) = mpsc::channel();
- for entry in WalkDir::new(&abspath) {
- if handler.load(Ordering::SeqCst) {
- let path = entry?.into_path();
- if !path.is_file() {
- continue;
- }
- if path.extension().is_some_and(|x| x == "flac") {
- #[cfg(not(test))]
- bar.inc_length(1);
- }
- } else {
- break;
- }
- }
+ #[cfg(not(test))]
+ let newbar = bar.clone();
- for entry in WalkDir::new(abspath) {
- if handler.load(Ordering::SeqCst) {
- let path = entry.unwrap().into_path();
- if !path.is_file() {
- continue;
- }
- if path.extension().is_some_and(|x| x == "flac") {
- if let Err(error) = handle_file(&path, conn) {
- eprintln!("{}", FileError::new(&path, error));
+ let newhandler = handler.clone();
+
+ s.spawn(move || {
+ for entry in WalkDir::new(&abspath) {
+ if newhandler.load(Ordering::SeqCst) {
+ if let Err(error) = entry {
+ #[cfg(not(test))]
+ newbar.println(format!("{}", error));
+ } else {
+ let path = entry.unwrap().into_path();
+ if !path.is_file() {
+ continue;
+ }
+ if path.extension().is_some_and(|x| x == "flac") {
+ let _ = filesend.send(path.to_owned());
+ #[cfg(not(test))]
+ newbar.inc_length(1);
+ }
+ }
} else {
- #[cfg(not(test))]
- bar.inc(1);
+ break;
}
}
- } else {
- break;
+ });
+
+ while let Ok(path) = filerecv.recv()
+ && handler.load(Ordering::SeqCst)
+ {
+ if let Err(error) = handle_file(&path, conn) {
+ #[cfg(not(test))]
+ bar.println(format!("{}", FileError::new(&path, error)));
+ } else {
+ #[cfg(not(test))]
+ bar.inc(1);
+ }
}
- }
+ });
#[cfg(not(test))]
{