Blame SOURCES/0130-filearch-move-libmagic-code-in-an-own-function.patch

ffd6ed
From 45209466afa76eb93f5c27028c9b5c5e6ea4f572 Mon Sep 17 00:00:00 2001
ffd6ed
From: Pino Toscano <ptoscano@redhat.com>
ffd6ed
Date: Mon, 13 Apr 2015 11:18:46 +0200
ffd6ed
Subject: [PATCH] filearch: move libmagic code in an own function
ffd6ed
ffd6ed
Also use a cleanup attribue to ease the close of the magic_t handle.
ffd6ed
ffd6ed
This is mostly code motion, hopefully with no actual behaviour changes.
ffd6ed
ffd6ed
(cherry picked from commit 20acc1f124a3f3af365c27b7654bead5beb1ef4c)
ffd6ed
---
ffd6ed
 src/filearch.c | 100 +++++++++++++++++++++++++++++++++++++++------------------
ffd6ed
 1 file changed, 68 insertions(+), 32 deletions(-)
ffd6ed
ffd6ed
diff --git a/src/filearch.c b/src/filearch.c
ffd6ed
index c0380d9..e851279 100644
ffd6ed
--- a/src/filearch.c
ffd6ed
+++ b/src/filearch.c
ffd6ed
@@ -74,6 +74,22 @@ free_regexps (void)
ffd6ed
   pcre_free (re_elf_ppc64);
ffd6ed
 }
ffd6ed
 
ffd6ed
+# ifdef HAVE_ATTRIBUTE_CLEANUP
ffd6ed
+# define CLEANUP_MAGIC_T_FREE __attribute__((cleanup(cleanup_magic_t_free)))
ffd6ed
+
ffd6ed
+static void
ffd6ed
+cleanup_magic_t_free (void *ptr)
ffd6ed
+{
ffd6ed
+  magic_t m = *(magic_t *) ptr;
ffd6ed
+
ffd6ed
+  if (m)
ffd6ed
+    magic_close (m);
ffd6ed
+}
ffd6ed
+
ffd6ed
+# else
ffd6ed
+# define CLEANUP_MAGIC_T_FREE
ffd6ed
+# endif
ffd6ed
+
ffd6ed
 /* Convert output from 'file' command on ELF files to the canonical
ffd6ed
  * architecture string.  Caller must free the result.
ffd6ed
  */
ffd6ed
@@ -120,6 +136,55 @@ is_regular_file (const char *filename)
ffd6ed
   return lstat (filename, &statbuf) == 0 && S_ISREG (statbuf.st_mode);
ffd6ed
 }
ffd6ed
 
ffd6ed
+static char *
ffd6ed
+magic_for_file (guestfs_h *g, const char *filename, bool *loading_ok,
ffd6ed
+                bool *matched)
ffd6ed
+{
ffd6ed
+  int flags;
ffd6ed
+  CLEANUP_MAGIC_T_FREE magic_t m = NULL;
ffd6ed
+  const char *line;
ffd6ed
+  char *elf_arch;
ffd6ed
+
ffd6ed
+  flags = g->verbose ? MAGIC_DEBUG : 0;
ffd6ed
+  flags |= MAGIC_ERROR | MAGIC_RAW;
ffd6ed
+
ffd6ed
+  if (loading_ok)
ffd6ed
+    *loading_ok = false;
ffd6ed
+  if (matched)
ffd6ed
+    *matched = false;
ffd6ed
+
ffd6ed
+  m = magic_open (flags);
ffd6ed
+  if (m == NULL) {
ffd6ed
+    perrorf (g, "magic_open");
ffd6ed
+    return NULL;
ffd6ed
+  }
ffd6ed
+
ffd6ed
+  if (magic_load (m, NULL) == -1) {
ffd6ed
+    perrorf (g, "magic_load: default magic database file");
ffd6ed
+    return NULL;
ffd6ed
+  }
ffd6ed
+
ffd6ed
+  line = magic_file (m, filename);
ffd6ed
+  if (line == NULL) {
ffd6ed
+    perrorf (g, "magic_file: %s", filename);
ffd6ed
+    return NULL;
ffd6ed
+  }
ffd6ed
+
ffd6ed
+  if (loading_ok)
ffd6ed
+    *loading_ok = true;
ffd6ed
+
ffd6ed
+  elf_arch = match1 (g, line, re_file_elf);
ffd6ed
+  if (elf_arch == NULL) {
ffd6ed
+    error (g, "no re_file_elf match in '%s'", line);
ffd6ed
+    return NULL;
ffd6ed
+  }
ffd6ed
+
ffd6ed
+  if (matched)
ffd6ed
+    *matched = true;
ffd6ed
+
ffd6ed
+  return canonical_elf_arch (g, elf_arch);
ffd6ed
+}
ffd6ed
+
ffd6ed
 /* Download and uncompress the cpio file to find binaries within. */
ffd6ed
 static const char *initrd_binaries[] = {
ffd6ed
   "bin/ls",
ffd6ed
@@ -198,40 +263,11 @@ cpio_arch (guestfs_h *g, const char *file, const char *path)
ffd6ed
       safe_asprintf (g, "%s/%s", dir, initrd_binaries[i]);
ffd6ed
 
ffd6ed
     if (is_regular_file (bin)) {
ffd6ed
-      int flags;
ffd6ed
-      magic_t m;
ffd6ed
-      const char *line;
ffd6ed
-      CLEANUP_FREE char *elf_arch = NULL;
ffd6ed
+      bool loading_ok, matched;
ffd6ed
 
ffd6ed
-      flags = g->verbose ? MAGIC_DEBUG : 0;
ffd6ed
-      flags |= MAGIC_ERROR | MAGIC_RAW;
ffd6ed
-
ffd6ed
-      m = magic_open (flags);
ffd6ed
-      if (m == NULL) {
ffd6ed
-        perrorf (g, "magic_open");
ffd6ed
-        goto out;
ffd6ed
-      }
ffd6ed
-
ffd6ed
-      if (magic_load (m, NULL) == -1) {
ffd6ed
-        perrorf (g, "magic_load: default magic database file");
ffd6ed
-        magic_close (m);
ffd6ed
-        goto out;
ffd6ed
-      }
ffd6ed
-
ffd6ed
-      line = magic_file (m, bin);
ffd6ed
-      if (line == NULL) {
ffd6ed
-        perrorf (g, "magic_file: %s", bin);
ffd6ed
-        magic_close (m);
ffd6ed
-        goto out;
ffd6ed
-      }
ffd6ed
-
ffd6ed
-      elf_arch = match1 (g, line, re_file_elf);
ffd6ed
-      if (elf_arch != NULL) {
ffd6ed
-        ret = canonical_elf_arch (g, elf_arch);
ffd6ed
-        magic_close (m);
ffd6ed
+      ret = magic_for_file (g, bin, &loading_ok, &matched);
ffd6ed
+      if (!loading_ok || matched)
ffd6ed
         goto out;
ffd6ed
-      }
ffd6ed
-      magic_close (m);
ffd6ed
     }
ffd6ed
   }
ffd6ed
   error (g, "file_architecture: could not determine architecture of cpio archive");
ffd6ed
-- 
ffd6ed
1.8.3.1
ffd6ed