]> git.proxmox.com Git - pve-common.git/commitdiff
Tools::run_command: array of arrays special case
authorWolfgang Bumiller <w.bumiller@proxmox.com>
Tue, 15 Sep 2015 08:01:33 +0000 (10:01 +0200)
committerDietmar Maurer <dietmar@proxmox.com>
Wed, 16 Sep 2015 07:08:53 +0000 (09:08 +0200)
Passing an array of arrays to run_command will cause each
array to be treated like a command piped to the following
command. Each argument is shell-quoted unless its passed by
reference.

src/PVE/Tools.pm

index 4c35abc54b74a19aa999682c0b7b9c63303aa721..26f008059df0104c04331482d70830cc5c03c29d 100644 (file)
@@ -254,13 +254,39 @@ sub safe_read_from {
 #  -) an array of arguments (strings)
 #    Will be executed without interference from a shell. (Parameters are passed
 #    as is, no escape sequences of strings will be touched.)
+#  -) an array of arrays
+#    Each array represents a command, and each command's output is piped into
+#    the following command's standard input.
+#    For this a shell command string is created with pipe symbols between each
+#    command.
+#    Each command is a list of strings meant to end up in the final command
+#    unchanged. In order to achieve this, every argument is shell-quoted.
+#    Quoting can be disabled for a particular argument by turning it into a
+#    reference, this allows inserting arbitrary shell options.
+#    For instance: the $cmd [ [ 'echo', 'hello', \'>/dev/null' ] ] will not
+#    produce any output, while the $cmd [ [ 'echo', 'hello', '>/dev/null' ] ]
+#    will literally print: hello >/dev/null
 sub run_command {
     my ($cmd, %param) = @_;
 
     my $old_umask;
     my $cmdstr;
 
-    if (!ref($cmd)) {
+    if (my $ref = ref($cmd)) {
+       if (ref($cmd->[0])) {
+           $cmdstr = 'set -o pipefail && ';
+           my $pipe = '';
+           foreach my $command (@$cmd) {
+               # concatenate quoted parameters
+               # strings which are passed by reference are NOT shell quoted
+               $cmdstr .= $pipe .  join(' ', map { ref($_) ? $$_ : shellquote($_) } @$command);
+               $pipe = ' | ';
+           }
+           $cmd = [ '/bin/bash', '-c', "set -o pipefail && $cmdstr" ];
+       } else {
+           $cmdstr = cmd2string($cmd);
+       }
+    } else {
        $cmdstr = $cmd;
        if ($cmd =~ m/\|/) {
            # see 'man bash' for option pipefail
@@ -268,8 +294,6 @@ sub run_command {
        } else {
            $cmd = [ $cmd ];
        }
-    } else {
-       $cmdstr = cmd2string($cmd);
     }
 
     my $errmsg;