tools: df: handle a failing df
authorWolfgang Bumiller <w.bumiller@proxmox.com>
Mon, 11 Sep 2017 07:20:08 +0000 (09:20 +0200)
committerWolfgang Bumiller <w.bumiller@proxmox.com>
Tue, 12 Sep 2017 12:00:45 +0000 (14:00 +0200)
This function assumed df() will work or hang, but it can
also actually fail and return undef which results in
warnings - let's silence those.

src/PVE/Tools.pm

index 3080b3e..bd99914 100644 (file)
@@ -983,7 +983,8 @@ sub df {
        $pipe->writer();
        eval {
            my $df = Filesys::Df::df($path, 1);
-           print {$pipe} "$df->{blocks}\n$df->{used}\n$df->{bavail}\n";
+           print {$pipe} "$df->{blocks}\n$df->{used}\n$df->{bavail}\n"
+               if defined($df);
            $pipe->close();
        };
        if (my $err = $@) {
@@ -996,9 +997,9 @@ sub df {
     $pipe->reader();
 
     my $readvalues = sub {
-       $res->{total} = int((<$pipe> =~ /^(\d*)$/)[0]);
-       $res->{used}  = int((<$pipe> =~ /^(\d*)$/)[0]);
-       $res->{avail} = int((<$pipe> =~ /^(\d*)$/)[0]);
+       $res->{total} = int(((<$pipe> // 0) =~ /^(\d*)$/)[0]);
+       $res->{used}  = int(((<$pipe> // 0) =~ /^(\d*)$/)[0]);
+       $res->{avail} = int(((<$pipe> // 0) =~ /^(\d*)$/)[0]);
     };
     eval {
        run_with_timeout($timeout, $readvalues);