Skip to content

Commit 9b459ad

Browse files
committed
added better mutex handling
1 parent fccc22c commit 9b459ad

File tree

8 files changed

+63
-44
lines changed

8 files changed

+63
-44
lines changed

.gitignore

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
target
2424
testfiles
2525
testfiles_orig
26-
*.flac
27-
*.wav
28-
*.tmp
29-
*.db
26+
./*.flac
27+
./*.wav
28+
./*.tmp
29+
./*.db

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "flac-reencoder"
3-
version = "0.2.2"
3+
version = "0.2.3"
44
edition = "2024"
55
repository = "https://github.com/justjakka/reencoder/"
66
license = "BSD-3-Clause"

samples/16bit.flac

942 KB
Binary file not shown.

samples/24bit.flac

1.28 MB
Binary file not shown.

samples/32bit.flac

1.74 MB
Binary file not shown.

src/files.rs

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -149,21 +149,24 @@ pub fn reencode_files(conn: Connection, handler: Arc<AtomicBool>) -> Result<()>
149149

150150
files.par_iter().for_each(|file| {
151151
if handler.load(Ordering::SeqCst) {
152-
let conn = match lock.lock() {
153-
Ok(conn) => conn,
154-
Err(_) => {
155-
eprintln!("Lock poisoned on file:\t{}", file.to_string_lossy());
156-
return;
157-
}
158-
};
159-
if let Err(error) = handle_encode(file) {
160-
eprintln!("{}", FileError::new(file, error));
161-
} else {
162-
if let Err(error) = conn.update_file(file) {
163-
eprintln!("{}", FileError::new(file, error));
152+
let newhandler = handler.clone();
153+
match handle_encode(file, newhandler) {
154+
Err(error) => eprintln!("{}", FileError::new(file, error)),
155+
Ok(false) => {
156+
let conn = match lock.lock() {
157+
Ok(conn) => conn,
158+
Err(_) => {
159+
eprintln!("Lock poisoned on file:\t{}", file.to_string_lossy());
160+
return;
161+
}
162+
};
163+
if let Err(error) = conn.update_file(file) {
164+
eprintln!("{}", FileError::new(file, error));
165+
}
166+
#[cfg(not(test))]
167+
bar.inc(1)
164168
}
165-
#[cfg(not(test))]
166-
bar.inc(1)
169+
Ok(true) => {}
167170
}
168171
}
169172
});

src/flac.rs

Lines changed: 40 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,13 @@ use claxon::{FlacReader, FlacReaderOptions};
33
use flac_bound::FlacEncoder;
44
use md5::{Digest, Md5};
55
use metaflac::{Block, Tag};
6-
use std::path::Path;
6+
use std::{
7+
path::Path,
8+
sync::{
9+
Arc,
10+
atomic::{AtomicBool, Ordering},
11+
},
12+
};
713

814
pub const CURRENT_VENDOR: &str = "reference libFLAC 1.5.0 20250211";
915

@@ -35,7 +41,7 @@ fn write_tags(filename: impl AsRef<Path>, hash: Vec<u8>) -> Result<()> {
3541
Ok(())
3642
}
3743

38-
fn encode_file(filename: impl AsRef<Path>) -> Result<()> {
44+
fn encode_file(filename: impl AsRef<Path>, handler: Arc<AtomicBool>) -> Result<bool> {
3945
let temp_name = filename.as_ref().with_extension("tmp");
4046
if temp_name.exists() {
4147
std::fs::remove_file(&temp_name)?;
@@ -55,7 +61,7 @@ fn encode_file(filename: impl AsRef<Path>) -> Result<()> {
5561

5662
let mut hasher = Md5::new();
5763

58-
let samples = decoder
64+
for samples in decoder
5965
.samples()
6066
.map(|res| {
6167
let sample = res.unwrap();
@@ -73,11 +79,17 @@ fn encode_file(filename: impl AsRef<Path>) -> Result<()> {
7379
}
7480
sample
7581
})
76-
.collect::<Vec<i32>>();
77-
78-
encoder
79-
.process_interleaved(&samples, samples.len() as u32 / streaminfo.channels)
80-
.unwrap();
82+
.collect::<Vec<i32>>()
83+
.chunks(streaminfo.channels as usize)
84+
{
85+
if handler.load(Ordering::SeqCst) {
86+
let _ =
87+
encoder.process_interleaved(samples, samples.len() as u32 / streaminfo.channels);
88+
} else {
89+
let _ = std::fs::remove_file(temp_name);
90+
return Ok(true);
91+
}
92+
}
8193

8294
if let Err(enc) = encoder.finish() {
8395
return Err(anyhow!("Encoding failed:\t{:?}", enc.state()));
@@ -86,15 +98,16 @@ fn encode_file(filename: impl AsRef<Path>) -> Result<()> {
8698
let hash = hasher.finalize().to_vec();
8799
write_tags(&filename, hash)?;
88100
std::fs::rename(temp_name, filename)?;
89-
Ok(())
101+
Ok(false)
90102
}
91103

92-
pub fn handle_encode(filename: impl AsRef<Path>) -> Result<()> {
93-
if let Err(error) = encode_file(&filename) {
94-
let _ = std::fs::remove_file(filename.as_ref().with_extension("tmp"));
95-
Err(error)
96-
} else {
97-
Ok(())
104+
pub fn handle_encode(filename: impl AsRef<Path>, handler: Arc<AtomicBool>) -> Result<bool> {
105+
match encode_file(&filename, handler) {
106+
Err(error) => {
107+
let _ = std::fs::remove_file(filename.as_ref().with_extension("tmp"));
108+
Err(error)
109+
}
110+
Ok(res) => Ok(res),
98111
}
99112
}
100113

@@ -120,10 +133,11 @@ mod tests {
120133

121134
#[test]
122135
fn bit16() {
123-
let name = "16bit.flac";
124-
let tempname = "16bit.flac.temp";
136+
let name = "./samples/16bit.flac";
137+
let tempname = "./samples/16bit.flac.temp";
125138
std::fs::copy(name, tempname).unwrap();
126-
encode_file(name).unwrap();
139+
let handler = Arc::new(AtomicBool::new(true));
140+
encode_file(name, handler).unwrap();
127141
let target_md5 = FlacReader::open(tempname).unwrap().streaminfo().md5sum;
128142
let temp_md5 = FlacReader::open(name).unwrap().streaminfo().md5sum;
129143

@@ -133,10 +147,11 @@ mod tests {
133147

134148
#[test]
135149
fn bit24() {
136-
let name = "24bit.flac";
137-
let tempname = "24bit.flac.temp";
150+
let name = "./samples/24bit.flac";
151+
let tempname = "./samples/24bit.flac.temp";
138152
std::fs::copy(name, tempname).unwrap();
139-
encode_file(name).unwrap();
153+
let handler = Arc::new(AtomicBool::new(true));
154+
encode_file(name, handler).unwrap();
140155
let target_md5 = FlacReader::open(tempname).unwrap().streaminfo().md5sum;
141156
let temp_md5 = FlacReader::open(name).unwrap().streaminfo().md5sum;
142157

@@ -146,10 +161,11 @@ mod tests {
146161

147162
#[test]
148163
fn bit32() {
149-
let name = "32bit.flac";
150-
let tempname = "32bit.flac.temp";
164+
let name = "./samples/32bit.flac";
165+
let tempname = "./samples/32bit.flac.temp";
151166
std::fs::copy(name, tempname).unwrap();
152-
encode_file(name).unwrap();
167+
let handler = Arc::new(AtomicBool::new(true));
168+
encode_file(name, handler).unwrap();
153169
let target_md5 = FlacReader::open(tempname).unwrap().streaminfo().md5sum;
154170
let temp_md5 = FlacReader::open(name).unwrap().streaminfo().md5sum;
155171

0 commit comments

Comments
 (0)