]> git.proxmox.com Git - pve-common.git/blobdiff - src/PVE/Tools.pm
tools: unbless errors in run_fork_with_timeout
[pve-common.git] / src / PVE / Tools.pm
index d5373a4246062887bd778f35f4d2ac9cf2bb8bd1..53ac83fd2b744366706d6354c4c4434aa183f614 100644 (file)
@@ -904,6 +904,13 @@ sub next_spice_port {
     return next_unused_port(61000, 61099, $family, $address);
 }
 
+sub must_stringify {
+       my ($value) = @_;
+       eval { $value = "$value" };
+       return "error turning value into a string: $@" if $@;
+       return $value;
+}
+
 # sigkill after $timeout  a $sub running in a fork if it can't write a pipe
 # the $sub has to return a single scalar
 sub run_fork_with_timeout {
@@ -935,7 +942,7 @@ sub run_fork_with_timeout {
            $pipe_out->flush();
        };
        if (my $err = $@) {
-           print {$pipe_out} encode_json({ error => $err });
+           print {$pipe_out} encode_json({ error => must_stringify($err) });
            $pipe_out->flush();
            POSIX::_exit(1);
        }
@@ -986,11 +993,35 @@ sub df {
     my $res = eval { run_fork_with_timeout($timeout, $df) } // {};
     warn $@ if $@;
 
+    # untaint the values
+    my ($blocks, $used, $bavail) = map { defined($_) ? (/^(\d+)$/) : 0 }
+       $res->@{qw(blocks used bavail)};
+
     return {
-       total => $res->{blocks} // 0,
-       used => $res->{used} // 0,
-       avail => $res->{bavail} // 0,
+       total => $blocks,
+       used => $used,
+       avail => $bavail,
+    };
+}
+
+sub du {
+    my ($path, $timeout) = @_;
+
+    my $size;
+
+    $timeout //= 10;
+
+    my $parser = sub {
+       my $line = shift;
+
+       if ($line =~ m/^(\d+)\s+total$/) {
+           $size = $1;
+       }
     };
+
+    run_command(['du', '-scb', $path], outfunc => $parser, timeout => $timeout);
+
+    return $size;
 }
 
 # UPID helper