correct calculation of free space
[BackupPC.git] / bin / BackupPC_burnArchiveCLI
index b17f5e0..f8845d4 100755 (executable)
@@ -11,6 +11,7 @@ use POSIX qw/strftime/;
 use Term::Menus;
 use File::Which;
 use File::Path;
+use Filesys::Df;
 
 use Data::Dumper;
 
@@ -193,23 +194,29 @@ order by date asc
 
 $sth->execute();
 
+sub fmt_mb($) {
+       my $s = shift;
+       die "missing size" unless defined($s);
+       $s /= (1024*1024);
+       return sprintf("%.2f Mb", $s);
+}
+
 sub fmt_archive($) {
        my $row = shift || die;
 
        $row->{'date'} =~ s/\.\d+$//;
        $row->{'copies'} =~ s/^\s*0+\s*$/no/;
-       $row->{'total_size'} /= (1024*1024);
 
        my $copies = 'copies';
        $copies = 'copy' if ($row->{'copies'} == 1);
 
        return
-               sprintf("%d by %s on %s, %s %s [%.2f Mb]",
+               sprintf("%d by %s on %s, %s %s [%s]",
                        $row->{'dvd_nr'},
                        $row->{'username'},
                        $row->{'date'},
                        $row->{'copies'}, $copies,
-                       $row->{'total_size'},
+                       fmt_mb($row->{'total_size'}),
                );
 }
 
@@ -382,6 +389,8 @@ foreach my $copy_nr ( 1 .. $copies ) {
                                $v = {};
                        }
                        $v->{size} += $row->{part_size};
+                       $v_size += $row->{part_size};
+
                        # this part
                        my $p = {
                                filename => BackupPC::SearchLib::getGzipName($row->{'host'}, $row->{'share'}, $row->{'num'}),
@@ -393,7 +402,16 @@ foreach my $copy_nr ( 1 .. $copies ) {
                }
                push @volumes, $v if ($v);
 
-               #warn "# volumes: ",Dumper(\@volumes),"\n";
+               #warn "# volumes: ",Dumper(\@volumes)," total size: ", fmt_mb($v_size), "\n";
+
+               # check available disk space
+
+               my $df = df($iso_dir)->{bavail} || die "can't get free space on $iso_dir";
+               $df *= 1024;    # calulate space in bytes
+               if ($df < $v_size) {
+                       warn "ABORTED: not enough disk space to create ISO ! [need ", fmt_mb($v_size), " have ", fmt_mb( $df ), " on $iso_dir]\n";
+                       next;
+               }
 
                my $volumes = $#volumes + 1;
                my $volume_nr = 1;
@@ -412,7 +430,7 @@ foreach my $copy_nr ( 1 .. $copies ) {
                                $disk_name .= ' ' . $volume_nr . '/' . $volumes;
                        }
 
-                       print "Processing DVD #$dvd_nr, volume $volume_nr/$volumes\n";
+                       print "Processing DVD #$dvd_nr, volume $volume_nr/$volumes [", fmt_mb($v->{size}), "]\n";
 
                        my $iso_file = my $xml_file = my $stage =
                                "${iso_dir}/${dvd_nr}";
@@ -511,7 +529,16 @@ foreach my $copy_nr ( 1 .. $copies ) {
                        $sth_archive_burned->execute($dvd_nr, $iso_size, $volume_nr, $copy_nr);
 
                        print "Media burn for $disk_name copy $copy_nr recorded\n";
-               
+
+                       if ($copy_nr >= $copies) {
+                               print STDERR "erasing temporary files, have $copy_nr copies (> $copies)\n";
+                               foreach my $f (( $xml_file, $iso_file )) {
+                                       print STDERR "\t$f ";
+                                       unlink $f || die "can't remove $f: $!";
+                                       print STDERR "removed\n";
+                               }
+                       }
+
                        $volume_nr++;
                }