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

[Libguestfs] [PATCH nbdkit v3 3/4] common: Move a utility function to a common directory.



From: "Richard W.M. Jones" <rjones redhat com>

The shell_quote function is moved to a new common/utils directory.
Eventually more utility functions can be created here.

This change is pure refactoring.
---
 Makefile.am              |  1 +
 common/utils/Makefile.am | 41 +++++++++++++++++++++++++++
 common/utils/utils.c     | 73 ++++++++++++++++++++++++++++++++++++++++++++++++
 common/utils/utils.h     | 39 ++++++++++++++++++++++++++
 configure.ac             |  1 +
 plugins/iso/Makefile.am  |  3 ++
 plugins/iso/iso.c        | 36 ++----------------------
 7 files changed, 160 insertions(+), 34 deletions(-)
 create mode 100644 common/utils/Makefile.am
 create mode 100644 common/utils/utils.c
 create mode 100644 common/utils/utils.h

diff --git a/Makefile.am b/Makefile.am
index 653956f..99f0852 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -74,6 +74,7 @@ SUBDIRS += \
 	common/gpt \
 	common/regions \
 	common/sparse \
+	common/utils \
 	plugins \
 	filters
 endif
diff --git a/common/utils/Makefile.am b/common/utils/Makefile.am
new file mode 100644
index 0000000..e002586
--- /dev/null
+++ b/common/utils/Makefile.am
@@ -0,0 +1,41 @@
+# nbdkit
+# Copyright (C) 2019 Red Hat Inc.
+# All rights reserved.
+#
+# Redistribution and use in source and binary forms, with or without
+# modification, are permitted provided that the following conditions are
+# met:
+#
+# * Redistributions of source code must retain the above copyright
+# notice, this list of conditions and the following disclaimer.
+#
+# * Redistributions in binary form must reproduce the above copyright
+# notice, this list of conditions and the following disclaimer in the
+# documentation and/or other materials provided with the distribution.
+#
+# * Neither the name of Red Hat nor the names of its contributors may be
+# used to endorse or promote products derived from this software without
+# specific prior written permission.
+#
+# THIS SOFTWARE IS PROVIDED BY RED HAT AND CONTRIBUTORS ''AS IS'' AND
+# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
+# THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
+# PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL RED HAT OR
+# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
+# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
+# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
+# OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+# SUCH DAMAGE.
+
+include $(top_srcdir)/common-rules.mk
+
+noinst_LTLIBRARIES = libutils.la
+
+libutils_la_SOURCES = \
+        utils.c \
+	utils.h
+libutils_la_CFLAGS = \
+        $(WARNINGS_CFLAGS)
diff --git a/common/utils/utils.c b/common/utils/utils.c
new file mode 100644
index 0000000..1f2b57d
--- /dev/null
+++ b/common/utils/utils.c
@@ -0,0 +1,73 @@
+/* nbdkit
+ * Copyright (C) 2018-2019 Red Hat Inc.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are
+ * met:
+ *
+ * * Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ *
+ * * Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * * Neither the name of Red Hat nor the names of its contributors may be
+ * used to endorse or promote products derived from this software without
+ * specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY RED HAT AND CONTRIBUTORS ''AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
+ * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
+ * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL RED HAT OR
+ * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
+ * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
+ * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+ * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
+ * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+#include <config.h>
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+/* Print str to fp, shell quoting if necessary.  This comes from
+ * libguestfs, but was written by me so I'm relicensing it to a BSD
+ * license for nbdkit.
+ */
+void
+shell_quote (const char *str, FILE *fp)
+{
+  /* Note possible bug in this list (XXX):
+   * https://www.redhat.com/archives/libguestfs/2019-February/msg00036.html
+   */
+  const char *safe_chars =
+    "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789.-_=,:/";
+  size_t i, len;
+
+  /* If the string consists only of safe characters, output it as-is. */
+  len = strlen (str);
+  if (len == strspn (str, safe_chars)) {
+    fputs (str, fp);
+    return;
+  }
+
+  /* Double-quote the string. */
+  fputc ('"', fp);
+  for (i = 0; i < len; ++i) {
+    switch (str[i]) {
+    case '$': case '`': case '\\': case '"':
+      fputc ('\\', fp);
+      /*FALLTHROUGH*/
+    default:
+      fputc (str[i], fp);
+    }
+  }
+  fputc ('"', fp);
+}
diff --git a/common/utils/utils.h b/common/utils/utils.h
new file mode 100644
index 0000000..a012183
--- /dev/null
+++ b/common/utils/utils.h
@@ -0,0 +1,39 @@
+/* nbdkit
+ * Copyright (C) 2018-2019 Red Hat Inc.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are
+ * met:
+ *
+ * * Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ *
+ * * Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * * Neither the name of Red Hat nor the names of its contributors may be
+ * used to endorse or promote products derived from this software without
+ * specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY RED HAT AND CONTRIBUTORS ''AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
+ * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
+ * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL RED HAT OR
+ * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
+ * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
+ * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+ * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
+ * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+#ifndef NBDKIT_UTILS_H
+#define NBDKIT_UTILS_H
+
+extern void shell_quote (const char *str, FILE *fp);
+
+#endif /* NBDKIT_UTILS_H */
diff --git a/configure.ac b/configure.ac
index bca6134..229e38c 100644
--- a/configure.ac
+++ b/configure.ac
@@ -837,6 +837,7 @@ AC_CONFIG_FILES([Makefile
                  common/include/Makefile
                  common/regions/Makefile
                  common/sparse/Makefile
+                 common/utils/Makefile
                  docs/Makefile
                  include/Makefile
                  plugins/Makefile
diff --git a/plugins/iso/Makefile.am b/plugins/iso/Makefile.am
index 061c161..673d1ee 100644
--- a/plugins/iso/Makefile.am
+++ b/plugins/iso/Makefile.am
@@ -43,6 +43,7 @@ nbdkit_iso_plugin_la_SOURCES = \
 	$(top_srcdir)/include/nbdkit-plugin.h
 
 nbdkit_iso_plugin_la_CPPFLAGS = \
+	-I$(top_srcdir)/common/utils \
 	-I$(top_srcdir)/include \
 	-I.
 nbdkit_iso_plugin_la_CFLAGS = \
@@ -50,6 +51,8 @@ nbdkit_iso_plugin_la_CFLAGS = \
 nbdkit_iso_plugin_la_LDFLAGS = \
 	-module -avoid-version -shared \
 	-Wl,--version-script=$(top_srcdir)/plugins/plugins.syms
+nbdkit_iso_plugin_la_LIBADD = \
+	$(top_builddir)/common/utils/libutils.la
 
 if HAVE_POD
 
diff --git a/plugins/iso/iso.c b/plugins/iso/iso.c
index 7431b48..53fccf0 100644
--- a/plugins/iso/iso.c
+++ b/plugins/iso/iso.c
@@ -43,6 +43,8 @@
 
 #include <nbdkit-plugin.h>
 
+#include "utils.h"
+
 /* List of directories parsed from the command line. */
 static char **dirs = NULL;
 static size_t nr_dirs = 0;
@@ -59,8 +61,6 @@ static const char *params = NULL;
 static int fd = -1;
 
 /* Construct the temporary ISO. */
-static void shell_quote (const char *str, FILE *fp);
-
 static int
 make_iso (void)
 {
@@ -136,38 +136,6 @@ make_iso (void)
   return 0;
 }
 
-/* Print str to fp, shell quoting if necessary.  This comes from
- * libguestfs, but was written by me so I'm relicensing it to a BSD
- * license for nbdkit.
- */
-static void
-shell_quote (const char *str, FILE *fp)
-{
-  const char *safe_chars =
-    "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789.-_=,:/";
-  size_t i, len;
-
-  /* If the string consists only of safe characters, output it as-is. */
-  len = strlen (str);
-  if (len == strspn (str, safe_chars)) {
-    fputs (str, fp);
-    return;
-  }
-
-  /* Double-quote the string. */
-  fputc ('"', fp);
-  for (i = 0; i < len; ++i) {
-    switch (str[i]) {
-    case '$': case '`': case '\\': case '"':
-      fputc ('\\', fp);
-      /*FALLTHROUGH*/
-    default:
-      fputc (str[i], fp);
-    }
-  }
-  fputc ('"', fp);
-}
-
 static void
 iso_unload (void)
 {
-- 
1.8.3.1


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