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

[Libguestfs] [PATCH v2 3/3] inspect: make netpbm and icoutils really optional runtime tools



Check at runtime for the existency of all the tools needed to manipulate
the icons for CirrOS and Windows guests, gracefully handle their lack.
This also drops the requirement of looking for them and hardcoding
their full paths at configure time, since we are directly running them
as available in $PATH.

As result, the code extracting the icons of CirrOS and Windows guests
can be always compiled.
---
 lib/inspect-icon.c  | 54 +++++++++++++++++++++++++++++------------------------
 m4/guestfs_progs.m4 | 24 ------------------------
 2 files changed, 30 insertions(+), 48 deletions(-)

diff --git a/lib/inspect-icon.c b/lib/inspect-icon.c
index 84d4e4a..5d7301f 100644
--- a/lib/inspect-icon.c
+++ b/lib/inspect-icon.c
@@ -28,14 +28,24 @@
 #include "guestfs-internal.h"
 #include "guestfs-internal-actions.h"
 
-/* External tools are required for some icon types.  Check we have them. */
-#if defined(PBMTEXT) && defined (PNMTOPNG)
-#define CAN_DO_CIRROS 1
-#endif
-#if defined(WRESTOOL) && defined(BMPTOPNM) && defined(PNMTOPNG) &&	\
-  defined(PAMCUT)
-#define CAN_DO_WINDOWS 1
-#endif
+/* External tools are required for some icon types.  List their names
+ * here, so in case we need to change their names and/or set the right
+ * names at configure time, then it's easier to do.
+ */
+#define BMPTOPNM "bmptopnm"
+#define PAMCUT "pamcut"
+#define PBMTEXT "pbmtext"
+#define PNMTOPNG "pnmtopng"
+#define WRESTOOL "wrestool"
+
+#define check_cmd(_cmd_) \
+  do { \
+    const int cmd_res_ = guestfs_int_find_cmd (g, _cmd_, NULL); \
+    if (cmd_res_ < 0) \
+      return NULL; /* a real error */ \
+    if (cmd_res_ == 0) \
+      return NOT_FOUND; \
+  } while (0)
 
 /* All these icon_*() functions return the same way.  One of:
  *
@@ -58,14 +68,10 @@ static char *icon_debian (guestfs_h *g, struct inspect_fs *fs, size_t *size_r);
 static char *icon_ubuntu (guestfs_h *g, struct inspect_fs *fs, size_t *size_r);
 static char *icon_mageia (guestfs_h *g, struct inspect_fs *fs, size_t *size_r);
 static char *icon_opensuse (guestfs_h *g, struct inspect_fs *fs, size_t *size_r);
-#if CAN_DO_CIRROS
 static char *icon_cirros (guestfs_h *g, struct inspect_fs *fs, size_t *size_r);
-#endif
 static char *icon_voidlinux (guestfs_h *g, struct inspect_fs *fs, size_t *size_r);
 static char *icon_altlinux (guestfs_h *g, struct inspect_fs *fs, size_t *size_r);
-#if CAN_DO_WINDOWS
 static char *icon_windows (guestfs_h *g, struct inspect_fs *fs, size_t *size_r);
-#endif
 
 /* Dummy static object. */
 static char *NOT_FOUND = (char *) "not_found";
@@ -156,9 +162,7 @@ guestfs_impl_inspect_get_icon (guestfs_h *g, const char *root, size_t *size_r,
       break;
 
     case OS_DISTRO_CIRROS:
-#if CAN_DO_CIRROS
       r = icon_cirros (g, fs, &size);
-#endif
       break;
 
     case OS_DISTRO_VOID_LINUX:
@@ -194,13 +198,11 @@ guestfs_impl_inspect_get_icon (guestfs_h *g, const char *root, size_t *size_r,
     break;
 
   case OS_TYPE_WINDOWS:
-#if CAN_DO_WINDOWS
     /* We don't know how to get high quality icons from a Windows guest,
      * so disable this if high quality was specified.
      */
     if (!highquality)
       r = icon_windows (g, fs, &size);
-#endif
     break;
 
   case OS_TYPE_FREEBSD:
@@ -391,8 +393,6 @@ icon_opensuse (guestfs_h *g, struct inspect_fs *fs, size_t *size_r)
   return get_png (g, fs, OPENSUSE_ICON, size_r, 2048);
 }
 
-#if CAN_DO_CIRROS
-
 /* Cirros's logo is a text file! */
 #define CIRROS_LOGO "/usr/share/cirros/logo"
 
@@ -406,6 +406,9 @@ icon_cirros (guestfs_h *g, struct inspect_fs *fs, size_t *size_r)
   CLEANUP_CMD_CLOSE struct command *cmd = guestfs_int_new_command (g);
   int r;
 
+  check_cmd (PBMTEXT);
+  check_cmd (PNMTOPNG);
+
   r = guestfs_is_file_opts (g, CIRROS_LOGO,
                             GUESTFS_IS_FILE_OPTS_FOLLOWSYMLINKS, 1, -1);
   if (r == -1)
@@ -445,8 +448,6 @@ icon_cirros (guestfs_h *g, struct inspect_fs *fs, size_t *size_r)
   return ret;
 }
 
-#endif /* CAN_DO_CIRROS */
-
 #define VOIDLINUX_ICON "/usr/share/void-artwork/void-logo.png"
 
 static char *
@@ -463,8 +464,6 @@ icon_altlinux (guestfs_h *g, struct inspect_fs *fs, size_t *size_r)
   return get_png (g, fs, ALTLINUX_ICON, size_r, 20480);
 }
 
-#if CAN_DO_WINDOWS
-
 /* Windows, as usual, has to be much more complicated and stupid than
  * anything else.
  *
@@ -491,6 +490,10 @@ icon_windows_xp (guestfs_h *g, struct inspect_fs *fs, size_t *size_r)
   int r;
   char *ret;
 
+  check_cmd (WRESTOOL);
+  check_cmd (BMPTOPNM);
+  check_cmd (PNMTOPNG);
+
   /* Download %systemroot%\explorer.exe */
   filename = safe_asprintf (g, "%s/explorer.exe", fs->windows_systemroot);
   filename_case = guestfs_case_sensitive_path (g, filename);
@@ -553,6 +556,11 @@ icon_windows_7 (guestfs_h *g, struct inspect_fs *fs, size_t *size_r)
   int r;
   char *ret;
 
+  check_cmd (WRESTOOL);
+  check_cmd (BMPTOPNM);
+  check_cmd (PAMCUT);
+  check_cmd (PNMTOPNG);
+
   for (i = 0; win7_explorer[i] != NULL; ++i) {
     CLEANUP_FREE char *filename = NULL;
 
@@ -661,5 +669,3 @@ icon_windows (guestfs_h *g, struct inspect_fs *fs, size_t *size_r)
   /* Not (yet) a supported version of Windows. */
   else return NOT_FOUND;
 }
-
-#endif /* CAN_DO_WINDOWS */
diff --git a/m4/guestfs_progs.m4 b/m4/guestfs_progs.m4
index c63e62f..74780eb 100644
--- a/m4/guestfs_progs.m4
+++ b/m4/guestfs_progs.m4
@@ -73,30 +73,6 @@ if test "x$DB_LOAD" != "xno"; then
     AC_DEFINE_UNQUOTED([DB_LOAD],["$DB_LOAD"],[Name of db_load program.])
 fi
 
-dnl Check for netpbm programs (optional).
-AC_PATH_PROGS([PBMTEXT],[pbmtext],[no])
-AC_PATH_PROGS([PNMTOPNG],[pnmtopng],[no])
-AC_PATH_PROGS([BMPTOPNM],[bmptopnm],[no])
-AC_PATH_PROGS([PAMCUT],[pamcut],[no])
-if test "x$PBMTEXT" != "xno"; then
-    AC_DEFINE_UNQUOTED([PBMTEXT],["$PBMTEXT"],[Name of pbmtext program.])
-fi
-if test "x$PNMTOPNG" != "xno"; then
-    AC_DEFINE_UNQUOTED([PNMTOPNG],["$PNMTOPNG"],[Name of pnmtopng program.])
-fi
-if test "x$BMPTOPNM" != "xno"; then
-    AC_DEFINE_UNQUOTED([BMPTOPNM],["$BMPTOPNM"],[Name of bmptopnm program.])
-fi
-if test "x$PAMCUT" != "xno"; then
-    AC_DEFINE_UNQUOTED([PAMCUT],["$PAMCUT"],[Name of pamcut program.])
-fi
-
-dnl Check for icoutils (optional).
-AC_PATH_PROGS([WRESTOOL],[wrestool],[no])
-if test "x$WRESTOOL" != "xno"; then
-    AC_DEFINE_UNQUOTED([WRESTOOL],["$WRESTOOL"],[Name of wrestool program.])
-fi
-
 dnl Check for xzcat (required).
 AC_PATH_PROGS([XZCAT],[xzcat],[no])
 test "x$XZCAT" = "xno" && AC_MSG_ERROR([xzcat must be installed])
-- 
2.9.3


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