[system-tools-backends-clone] Improve error reporting when running external commands



commit f64cef21aca94518e0af681c89cd2eb658492094
Author: Milan Bouchet-Valat <nalimilan club fr>
Date:   Wed Nov 18 16:26:20 2009 +0100

    Improve error reporting when running external commands
    
    exec() does not allow checking the returned error code. Use system() instead, even if we still need to fork to set stdin, stdout and LC_ALL. Add a new report when launching a command failed.

 Utils/File.pm   |   19 +++++++++++++------
 Utils/Report.pm |    1 +
 2 files changed, 14 insertions(+), 6 deletions(-)
---
diff --git a/Utils/File.pm b/Utils/File.pm
index 0bace55..f226ed6 100644
--- a/Utils/File.pm
+++ b/Utils/File.pm
@@ -770,11 +770,10 @@ sub run_full
 
   $tool_path = &locate_tool ($cmd);
   return -1 if ($tool_path eq "");
-  return -1 if $command == -1;
+  return -1 if $cmd == -1;
 
   $command = join (" ", ($tool_path, @arguments));
   &Utils::Report::do_report ("file_run_full", $command);
-  &Utils::Report::leave ();
 
   my $pid = fork();
 
@@ -785,8 +784,10 @@ sub run_full
     $ENV{"LC_ALL"} = "C";
     open (STDOUT, "/dev/null");
     open (STDERR, "/dev/null");
-    exec ($tool_path, @arguments);
-    exit (0);
+    system ($tool_path, @arguments);
+
+    # As documented in perlfunc, divide by 256.
+    exit ($? / 256);
   }
 
   # If no error has occurred so far, assume success,
@@ -795,8 +796,14 @@ sub run_full
 
   waitpid ($pid, 0);
 
-  # As documented in perlfunc, divide by 256.
-  return ($? / 256);
+  if ($? != 0)
+  {
+    &Utils::Report::do_report ("file_run_full_failed", $command);
+  }
+
+  &Utils::Report::leave ();
+
+  return ($?);
 }
 
 # Simple wrappers calling &run_full() with the right background parameter
diff --git a/Utils/Report.pm b/Utils/Report.pm
index 1bf47d5..dfdb8d3 100644
--- a/Utils/Report.pm
+++ b/Utils/Report.pm
@@ -189,6 +189,7 @@ sub add
      "file_run_pipe_failed"     => ["warn",  "Failed to pipe command [%s] for reading."],
      "file_run_pipe_success"    => ["info",  "Piping command [%s] for reading."],
      "file_run_full"            => ["info",  "Running command [%s]."],
+     "file_run_full_failed"     => ["warn",  "Command [%s] failed."],
      "file_create_path"         => ["info",  "Directory [%s] created."],
      "file_backup_rotate"       => ["info",  "Backup directory [%s] was rotated."],
      "file_backup_success"      => ["info",  "Saved backup for [%s]."],



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]