summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/files.rs70
-rw-r--r--src/flac.rs4
2 files changed, 43 insertions, 31 deletions
diff --git a/src/files.rs b/src/files.rs
index 12794ab..e3a31ca 100644
--- a/src/files.rs
+++ b/src/files.rs
@@ -10,6 +10,7 @@ use std::{
sync::{
Arc,
atomic::{AtomicBool, AtomicUsize, Ordering},
+ mpsc,
},
thread::{self, sleep},
time::{Duration, UNIX_EPOCH},
@@ -69,7 +70,7 @@ async fn handle_file(file: &Path, conn: &Connection) -> Result<()> {
Ok(())
}
-pub fn index_files_recursively(
+pub(crate) async fn index_files_recursively(
path: &Path,
conn: &Connection,
handler: Arc<AtomicBool>,
@@ -83,40 +84,51 @@ pub 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) = smol::block_on(async { handle_file(&path, conn).await }) {
- eprintln!("{}", FileError::new(&path, error));
+ let newhandler = handler.clone();
+
+ let newhandler = handler.clone();
+
+ #[allow(unused_variables)]
+ 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)
+ {
+ #[allow(unused_variables)]
+ if let Err(error) = smol::block_on( async {handle_file(&path, conn)}).await {
+ #[cfg(not(test))]
+ bar.println(format!("{}", FileError::new(&path, error)));
+ } else {
+ #[cfg(not(test))]
+ bar.inc(1);
+ }
}
- }
+ });
#[cfg(not(test))]
{
diff --git a/src/flac.rs b/src/flac.rs
index e882cd7..eb603d7 100644
--- a/src/flac.rs
+++ b/src/flac.rs
@@ -141,7 +141,7 @@ fn encode_file(filename: &Path, handler: Arc<AtomicBool>) -> Result<bool> {
Ok(false)
}
-pub fn handle_encode(filename: &Path, handler: Arc<AtomicBool>) -> Result<bool> {
+pub(crate) fn handle_encode(filename: &Path, handler: Arc<AtomicBool>) -> Result<bool> {
match encode_file(filename, handler) {
Err(error) => {
let _ = std::fs::remove_file(filename.with_extension("tmp"));
@@ -151,7 +151,7 @@ pub fn handle_encode(filename: &Path, handler: Arc<AtomicBool>) -> Result<bool>
}
}
-pub fn get_vendor(file: &Path) -> Result<String> {
+pub(crate) fn get_vendor(file: &Path) -> Result<String> {
let blocklist = metadata::BlockList::open(file)?;
if let Some(data) = blocklist.get::<metadata::VorbisComment>() {
Ok(data.vendor_string.to_owned())