[Libguestfs] [PATCH nbdkit 2/4] common: regions: Split out code used by partitioning plugin into a library.

Richard W.M. Jones
2018-10-28T10:13:26Z

Just simple code refactoring, allowing this code to be reused by
nbdkit-floppy-plugin.
---
 Makefile.am                                   |  1 +
 common/regions/Makefile.am                    | 44 +++++++++++++++++++
 .../partitioning => common/regions}/regions.c |  0
 .../partitioning => common/regions}/regions.h |  6 +--
 configure.ac                                  |  1 +
 plugins/partitioning/Makefile.am              |  5 ++-
 6 files changed, 52 insertions(+), 5 deletions(-)

diff --git a/Makefile.am b/Makefile.am
index 4881cf3..bce927d 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -55,6 +55,7 @@ SUBDIRS = \

 if HAVE_PLUGINS
 SUBDIRS += \
+	common/regions \
 	common/sparse \
 	plugins \
 	filters
diff --git a/common/regions/Makefile.am b/common/regions/Makefile.am
new file mode 100644
index 0000000..35399e5
--- /dev/null
+++ b/common/regions/Makefile.am
@@ -0,0 +1,44 @@
+# nbdkit
+# Copyright (C) 2018 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 = libregions.la
+
+libregions_la_SOURCES = \
+        regions.c \
+        regions.h
+libregions_la_CPPFLAGS = \
+	-I$(top_srcdir)/include \
+	-I$(top_srcdir)/common/include
+libregions_la_CFLAGS = \
+        $(WARNINGS_CFLAGS)
diff --git a/plugins/partitioning/regions.c b/common/regions/regions.c
similarity index 100%
rename from plugins/partitioning/regions.c
rename to common/regions/regions.c
diff --git a/plugins/partitioning/regions.h b/common/regions/regions.h
similarity index 94%
rename from plugins/partitioning/regions.h
rename to common/regions/regions.h
index d6c47f1..2036767 100644
--- a/plugins/partitioning/regions.h
+++ b/common/regions/regions.h
@@ -38,7 +38,7 @@
 #include <assert.h>

 /* This defines a very simple structure used to define the virtual
- * disk in the partitioning plugin.
+ * disk in the partitioning and floppy plugins.
  *
  * We split the virtual disk into non-overlapping, contiguous regions.
  * These are stored in an array, ordered by address.
@@ -50,7 +50,7 @@
 /* Region type. */
 enum region_type {
   region_file,        /* contents of the i'th file */
-  region_data,        /* pointer to data (used for partition table) */
+  region_data,        /* pointer to in-memory data */
   region_zero,        /* padding */
 };

@@ -60,7 +60,7 @@ struct region {
   enum region_type type;
   union {
     size_t i;                  /* region_file: i'th file */
-    const unsigned char *data; /* region_data: data (partition table) */
+    const unsigned char *data; /* region_data: data */
   } u;
 };

diff --git a/configure.ac b/configure.ac
index b8651b3..b66a7e2 100644
--- a/configure.ac
+++ b/configure.ac
@@ -644,6 +644,7 @@ AC_CONFIG_FILES([podwrapper.pl],
 AC_CONFIG_FILES([Makefile
                  bash/Makefile
                  common/include/Makefile
+                 common/regions/Makefile
                  common/sparse/Makefile
                  docs/Makefile
                  include/Makefile
diff --git a/plugins/partitioning/Makefile.am b/plugins/partitioning/Makefile.am
index 8d516d5..bc768be 100644
--- a/plugins/partitioning/Makefile.am
+++ b/plugins/partitioning/Makefile.am
@@ -39,8 +39,6 @@ plugin_LTLIBRARIES = nbdkit-partitioning-plugin.la
 nbdkit_partitioning_plugin_la_SOURCES = \
 	crc32.c \
 	crc32.h \
-	regions.c \
-	regions.h \
 	partitioning.c \
 	partition-gpt.c \
 	partition-mbr.c \
@@ -51,11 +49,14 @@ nbdkit_partitioning_plugin_la_SOURCES = \
 nbdkit_partitioning_plugin_la_CPPFLAGS = \
 	-I$(top_srcdir)/include \
 	-I$(top_srcdir)/common/include \
+	-I$(top_srcdir)/common/regions \
 	-I.
 nbdkit_partitioning_plugin_la_CFLAGS = \
 	$(WARNINGS_CFLAGS)
 nbdkit_partitioning_plugin_la_LDFLAGS = \
 	-module -avoid-version -shared
+nbdkit_partitioning_plugin_la_LIBADD = \
+	$(top_builddir)/common/regions/libregions.la

 if HAVE_POD

--
2.19.0.rc0