=cut
+package Jobs;
+
+use strict;
+use warnings;
+
+use Errno;
+use POSIX ':sys_wait_h';
+
+sub newjob {
+ my $f = shift;
+ my $jobs = shift;
+ my $debug = shift;
+ my $pid;
+
+ if (not $debug) {
+ $pid = fork();
+ if (not defined($pid)) {
+ die("fork: $!");
+ }
+ }
+
+ if ($debug or $pid == 0) {
+ exit($f->());
+ }
+
+ if ($pid == 0) {
+ exit($f->());
+ }
+
+ push(@$jobs, $pid);
+
+ return $pid;
+}
+
+sub deljob {
+ my $pid = shift;
+ my $status = shift;
+ my $jobs = shift;
+
+ for (my $i = 0; $i <= $#$jobs; $i++) {
+ if ($pid == $jobs->[$i]) {
+ splice(@$jobs, $i, 1);
+ last;
+ }
+ }
+
+ return ($pid, $status);
+}
+
+sub run {
+ my %o = @_;
+ my $maxjobs = $o{'max-jobs'};
+ my $get_job = $o{'get-job'};
+ my $notify_start = $o{'notify-start'};
+ my $notify_finish = $o{'notify-finish'};
+ my @jobs;
+ my $pid;
+
+ # Call notifier function if given.
+ sub call {
+ my $f = shift or return;
+ ref($f) eq 'CODE' or return;
+ $f->(@_);
+ }
+
+ while (1) {
+ if (@jobs <= $maxjobs) {
+ my $job;
+ while (defined($job = $get_job->())) {
+ $pid = newjob($job, \@jobs, $o{'debug'});
+ call($notify_start, $pid, @jobs);
+ @jobs < $maxjobs or last;
+ }
+
+ # No jobs running and get-job returned undef; we're finished.
+ if (@jobs == 0 and not defined($job)) {
+ return;
+ }
+ }
+
+ # Now running as many jobs as we can, block waiting for one to die.
+ do {
+ $pid = waitpid(-1, 0);
+ } while ($pid == 0
+ or ($pid == -1 and ($!{ECHILD} or $!{EINTR})));
+ $pid == -1 and die("waitpid(-1): $!");
+
+ # Before starting more, see if any others have finished.
+ do {
+ call($notify_finish, deljob($pid, $?, \@jobs), @jobs);
+ } while (($pid = waitpid(-1, WNOHANG)) > 0);
+ if ($pid == -1) {
+ $!{ECHILD} or $!{EINTR} or die("waitpid(-1): $!");
+ }
+ }
+}
+
+\f
+################################################################################
+package main;
+
use strict;
use warnings;
my $debug;
my $verbose;
-my @jobs;
-my @finished;
sub verbose {
$verbose and print(STDERR $_) for @_;
return ($artist, $album, $discnum, @tags);
}
-sub bork_tags {
+sub track_tags {
my $h = shift;
my @result;
$outlog = "../$artist/$outfile.log";
@files = ("$artist/$outfile.flac");
} else {
- my %things;
- my @artist;
- my @bork;
+ # Go over @tags, store all [n] tags in a list keyed by n in
+ # %tracks_to_tags, store all ARTIST (not ARTIST[n]) tags in
+ # @disc_artist, and leave the rest in @tags.
+ my %tracks_to_tags;
+ my @disc_artist;
+ my @tmp;
for my $tag (@tags) {
if ($tag =~ /^([^[]+)\[(\d+)]=(.*)/) {
- push(@{$things{$2}->{$1}}, $3);
+ push(@{$tracks_to_tags{$2}->{$1}}, $3);
} elsif ($tag =~ /^ARTIST=/) {
- push(@artist, $tag);
+ push(@disc_artist, $tag);
} else {
- push(@bork, $tag);
+ push(@tmp, $tag);
}
}
- @tags = @bork;
+ @tags = @tmp;
- for my $tracknum (sort(map(int, keys(%things)))) {
- my $title = join(' ', map(split, @{$things{$tracknum}->{'TITLE'}}));
+ for my $tracknum (sort(map(int, keys(%tracks_to_tags)))) {
+ my $title = join(' ', map(split, @{$tracks_to_tags{$tracknum}->{'TITLE'}}));
$title =~ s|/|_|g;
- $outfile = join(' ',
- (defined($discnum)
- ? sprintf('%02d', $discnum)
- : ()),
- sprintf('%02d', $tracknum),
- $title);
- push(@files, "$outdir/$outfile.flac");
- $outfile = "../$outdir/$outfile";
-
- my @lartist;
- if (exists($things{$tracknum}->{'ARTIST'})) {
- @lartist = ();
+ $outfile = join('/',
+ $outdir,
+ join(' ',
+ (defined($discnum)
+ ? sprintf('%02d', $discnum)
+ : ()),
+ sprintf('%02d', $tracknum),
+ $title));
+
+ # If we have ARTIST[n] tags for this track, set
+ # @track_artist to the empty list; they will go in along
+ # with the other [n] tags.
+ my @track_artist;
+ if (exists($tracks_to_tags{$tracknum}->{'ARTIST'})) {
+ @track_artist = ();
} else {
- @lartist = @artist;
+ @track_artist = @disc_artist;
}
- run_flac(sprintf('track%02d.cdda.wav', $tracknum), undef, $outfile,
- @lartist,
- grep({ $_ !~ /^ARTIST=/ } @tags),
- bork_tags($things{$tracknum}));
- $outlog = "../$outdir/log";
+ run_flac(sprintf('track%02d.cdda.wav', $tracknum), undef,
+ "../$outfile",
+ @track_artist,
+ @tags,
+ "TRACKNUMBER=$tracknum",
+ track_tags($tracks_to_tags{$tracknum}));
+ push(@files, "$outfile.flac");
}
+ $outlog = "../$outdir/log";
}
verbose("Cleaning up $dir\n");
chdir('..') or die("chdir(..): $!");
if (-x "$dir/post-processor") {
- verbose(join(' ', 'Running', @files), "\n");
+ verbose(join(' ', "Running ./$dir/post-processor", @files), "\n");
system("./$dir/post-processor", @files);
unlink("$dir/post-processor") or die("unlink($dir/post-processor): $!");
}
return 0;
}
-sub reaper {
- my $pid;
-
- while (($pid = waitpid(-1, WNOHANG)) > 0) {
- push(@finished, [$pid, $?]);
- }
-
- $SIG{CHLD} = \&reaper;
-}
-
-sub newjob {
- my $dir = shift;
- my $pid;
-
- if (not $debug) {
- $pid = fork();
- if (not defined($pid)) {
- die("fork: $!");
- }
- }
-
- if ($debug or $pid == 0) {
- $SIG{CHLD} = 'DEFAULT';
- open(STDERR, ">$dir/log") or die("open(STDERR, >$dir/log): $!");
- exit(flac($dir));
- }
-
- verbose("new job $pid for $dir\n");
- return $pid;
-}
-
-sub deljob {
- my $i = shift;
- my $j;
- my $pid;
- my $status;
-
- $pid = $finished[$i][0];
- $status = $finished[$i][1];
-
- verbose("$pid finished (");
- if (WIFEXITED($status)) {
- verbose('exited with status ', WEXITSTATUS($status));
- } elsif (WIFSIGNALED($status)) {
- verbose('killed with signal ', WTERMSIG($status));
- } elsif (WIFSTOPPED($status)) {
- verbose('stopped with signal ', WSTOPSIG($status));
- }
- verbose(")\n");
-
- for ($j = 0; $j <= $#jobs; $j++) {
- $pid == $jobs[$j] and splice(@jobs, $j, 1) and last;
- }
-
- splice(@finished, $i, 1);
-}
-
sub flacloop {
my $MAXJOBS = shift;
- my $i;
- my $j;
-
+ my @jobs;
+ my $dir;
- $SIG{CHLD} = \&reaper;
while (1) {
- if (scalar(@jobs) <= $MAXJOBS) {
- foreach $i (glob('*/tags')) {
- push(@jobs, newjob(dirname($i))) <= $MAXJOBS or last;
- }
- }
-
- for ($i = 0; $i <= $#finished; $i++) {
- deljob($i);
+ if (scalar(@jobs = glob('*/tags')) == 0) {
+ sleep(5);
+ next;
}
- verbose(scalar(@jobs), " jobs\n");
- sleep(5);
+ Jobs::run('max-jobs'=>$MAXJOBS,
+ 'debug'=>$debug,
+ 'get-job'=>sub {
+ my $bork = shift(@jobs) or return;
+ $dir = dirname($bork);
+ return sub {
+ open(STDERR, ">$dir/log")
+ or die("open(STDERR, >$dir/log): $!");
+ return flac($dir);
+ }
+ },
+
+ 'notify-start'=>sub {
+ my $pid = shift;
+ verbose("new job $pid for $dir\n");
+ verbose(scalar(@_), " jobs\n");
+ },
+
+ 'notify-finish'=>sub {
+ my $pid = shift;
+ my $status = shift;
+ verbose("$pid finished (");
+ if (WIFEXITED($status)) {
+ verbose('exited with status ', WEXITSTATUS($status));
+ } elsif (WIFSIGNALED($status)) {
+ verbose('killed with signal ', WTERMSIG($status));
+ } elsif (WIFSTOPPED($status)) {
+ verbose('stopped with signal ', WSTOPSIG($status));
+ }
+ verbose(")\n");
+ });
}
}