[ale] Burning quick hack

Christopher Fowler cfowler at outpostsentinel.com
Wed Jun 15 20:02:44 EDT 2005


I go tired of seeing all the output of cdrecord so I hacked out this
quick program.  Maybe you guys can play around with it and make it
better.

---------------------------------------------------------------------------------
#!/usr/bin/perl

use POSIX ":sys_wait_h";
use Getopt::Std;
use IO::Handle;
use IO::Select;
use vars qw/$opt_s $opt_d $optind/;
use strict;

# Globals
$opt_s = 4;
$opt_d = undef;
my $selector = new IO::Select;
my $cdrecord = "/usr/bin/cdrecord";

sub get_dev {
  my $dev = undef;
  open DEV, "cdrecord --scanbus -dev=ATA 2>&1 |";
  while(<DEV>) {
    next unless m/DRU-530/;
    next unless m/^\s+(\d,\d,\d)/;
    $dev = $1;
  }

  return $dev;
}
sub main {
  getopts("s:d");
  die "dvdburn.pl <image file>\n" unless $ARGV[$optind];
  my $dev = get_dev() or die "Did not find DVD-RW device\n";
  print "Device:     ATA:$dev\n";
  print "Speed :     $opt_s\n";
  print "Image File: $ARGV[$optind]\n";

  pipe RP,WP or die "Pipe: $!\n";
  pipe ERP, EWP or die "Pipe $!\n";

  my $pid = fork();

  # Child Code
  if($pid == 0) {
    open STDOUT, ">&WP" or die "PIPE Redirection: $!\n";
    open STDERR, ">&EWP";
    close RP;
    close ERP;

    STDOUT->autoflush();
    STDERR->autoflush();
    RP->autoflush();

    my @args = ();
    push @args, $cdrecord;
    push @args, "-v";
    push @args, "--dummy" if $opt_d;
    push @args, "--dev=ATA:$dev";
    push @args, "--speed";
    push @args, "$opt_s";
    push @args, "-dao";
    push @args, "-eject";
    push @args, $ARGV[$optind];
    exec @args or die "$!\n";
  }
  # Parent
  close WP;
  close EWP;
  $selector->add(*RP);
  $selector->add(*ERP);
  my $data = "";
  my $data_stderr = "";
  my $percent = 0;

  while(1) {
    my ($rr, undef, $er) = IO::Select->select($selector, undef,
$selector, undef) ;
    foreach my $fh (@$rr) {
      if($fh == *RP) {
        my $byte = "";
        if(sysread($fh, $byte, 1) == 0) {
          close RP;
          while(waitpid($pid, WNOHANG) != $pid) { }
          if($? !=0 ) {
            print "\n";
            print "CDRECORD STDERR: \n";
            print "$data_stderr\n";
          }
          print "\n";
          return 0;
        }
        $data .= $byte;
        if ($data =~ m/Track 01:\s+(\d+)\sof\s(\d+)\sMB/) {
          my $ps = int($2 / 50);
          #print "\r$1:$2:$ps:(".($1 % $ps)."";
          if(($1 % $ps) == 0 && ($1 != 0))  {
            $percent++;
            $percent++;
          }
          print "\r";
          my $bar = "";

          for (my $i = 0; $i < $percent; $i++) {
            $bar .= "=";
            $i++;
          }

          $bar .= ">";
          printf "Complete: [%-50.50s] %s\%\r", $bar, $percent;
          STDOUT->flush();
          $data = "";
        }
      } elsif($fh == *EWP) {
        my $byte = "";
        sysread $fh, $byte, 1;
        $data_stderr .= $byte;
      }
    }
  }
}

exit main;
---------------------------------------------------------------------------------

I did not say unreadable.  I said better :)



More information about the Ale mailing list