eliminate deinstalled packages
authorAndreas Steffen <andreas.steffen@strongswan.org>
Mon, 5 Nov 2012 20:40:19 +0000 (21:40 +0100)
committerAndreas Steffen <andreas.steffen@strongswan.org>
Mon, 5 Nov 2012 20:40:19 +0000 (21:40 +0100)
src/libimcv/os_info/os_info.c

index 4f2a15d..110c60e 100644 (file)
@@ -220,7 +220,7 @@ typedef struct {
        /**
         * line buffer
         */
-       char line[512];
+       u_char line[512];
 
 } package_enumerator_t;
 
@@ -239,36 +239,53 @@ static void package_enumerator_destroy(package_enumerator_t *this)
 static bool package_enumerator_enumerate(package_enumerator_t *this, ...)
 {
        chunk_t *name, *version;
-       char *pos;
+       u_char *pos;
        va_list args;
 
-       if (!fgets(this->line, sizeof(this->line), this->file))
+       while (TRUE)
        {
-               return FALSE;
-       }
-       va_start(args, this);
+               if (!fgets(this->line, sizeof(this->line), this->file))
+               {
+                       return FALSE;
+               }
 
-       name = va_arg(args, chunk_t*);
-       name->ptr = this->line;
-       pos = strchr(this->line, '\t');
-       if (!pos)
-       {
-               return FALSE;
-       }
-       name->len = pos++ - this->line;
+               pos = strchr(this->line, '\t');
+               if (!pos)
+               {
+                       return FALSE;
+               }
+               *pos++ = '\0';
 
-       version = va_arg(args, chunk_t*);
-       version->ptr = pos;
-       version->len = strlen(pos) - 1;
+               if (!streq(this->line, "install ok installed"))
+               {
+                       continue;
+               }
+               va_start(args, this);
 
-       va_end(args);
-       return TRUE;
+               name = va_arg(args, chunk_t*);
+               name->ptr = pos;
+               pos = strchr(pos, '\t');
+               if (!pos)
+               {
+                       return FALSE;
+               }
+               name->len = pos++ - name->ptr;
+
+               version = va_arg(args, chunk_t*);
+               version->ptr = pos;
+               version->len = strlen(pos) - 1;
+
+               va_end(args);
+               return TRUE;
+       }
 }
 
 METHOD(os_info_t, create_package_enumerator, enumerator_t*,
        private_os_info_t *this)
 {
        FILE *file;
+       const char command[] = "dpkg-query --show --showformat="
+                                                               "'${Status}\t${PackageSpec}\t${Version}\n'";
        package_enumerator_t *enumerator;
 
        /* Only Debian and Ubuntu package enumeration is currently supported */
@@ -278,7 +295,7 @@ METHOD(os_info_t, create_package_enumerator, enumerator_t*,
        }
 
        /* Open a pipe stream for reading the output of the dpkg-query commmand */
-       file = popen("/usr/bin/dpkg-query --show", "r");
+       file = popen(command, "r");
        if (!file)
        {
                DBG1(DBG_IMC, "failed to run dpkg command");