[Libguestfs] [PATCH] GuestOS: Fix _is_installed to return 0 if the package isn't installed

Matthew Booth mbooth at redhat.com
Fri Feb 12 14:22:43 UTC 2010


_is_installed was only checking if an existing package was newer than the
target. If the package wasn't installed at all it was returning an error.
---
 lib/Sys/VirtV2V/GuestOS/RedHat.pm |   29 +++++++++++++++++++++++++++--
 1 files changed, 27 insertions(+), 2 deletions(-)

diff --git a/lib/Sys/VirtV2V/GuestOS/RedHat.pm b/lib/Sys/VirtV2V/GuestOS/RedHat.pm
index 9561338..8410ca2 100644
--- a/lib/Sys/VirtV2V/GuestOS/RedHat.pm
+++ b/lib/Sys/VirtV2V/GuestOS/RedHat.pm
@@ -710,8 +710,33 @@ sub _is_installed
 
     # Search installed rpms matching <name>.<arch>
     my $found = 0;
-    foreach my $installed ($g->command_lines(['rpm', '-q', '--qf',
-                           '%{EPOCH} %{VERSION} %{RELEASE}', "$name.$arch"])) {
+
+    my $rpmcmd = ['rpm', '-q', '--qf', '%{EPOCH} %{VERSION} %{RELEASE}\n',
+                  "$name.$arch"];
+    my @output;
+    eval {
+        @output = $g->command_lines($rpmcmd);
+    };
+
+    if ($@) {
+        # RPM command returned non-zero. This might be because there was
+        # actually an error, or might just be because the package isn't
+        # installed.
+        # Unfortunately, rpm sent its error to stdout instead of stderr, and
+        # command_lines only gives us stderr in $@. To get round this we'll
+        # execute the command again, sending all output to stdout and ignoring
+        # failure. If the output contains 'not installed', we'll assume it's not
+        # a real error.
+        my $error = $g->sh(join(' ', @$rpmcmd).' 2>&1 ||:');
+
+        return 0 if ($error =~ /not installed/);
+
+        die(user_message(__x("Error running {command}: {error}",
+                             command => join(' ', @$rpmcmd),
+                             error => $error)));
+    }
+
+    foreach my $installed (@output) {
         $installed =~ /^(\S+)\s+(\S+)\s+(\S+)$/
             or die("Unexpected return from rpm command: $installed");
         my ($iepoch, $iversion, $irelease) = ($1, $2, $3);
-- 
1.6.6




More information about the Libguestfs mailing list