[Libguestfs] [PATCH nbdkit 6/9] lib: Use replacement strategy for get_current_dir_name.

Richard W.M. Jones
2020-08-18T10:50:10Z

This is really a replacement for a missing platform function, so use
the same LIBOBJS mechanism to replace it.
---
 configure.ac                                  |  2 +-
 common/include/Makefile.am                    |  6 ---
 common/replacements/Makefile.am               | 10 ++++
 plugins/floppy/Makefile.am                    |  1 +
 common/replacements/get_current_dir_name.h    | 49 +++++++++++++++++++
 server/public.c                               |  2 +-
 .../get_current_dir_name.c}                   | 10 ++--
 .../test-current-dir-name.c                   |  2 +-
 plugins/floppy/virtual-floppy.c               |  2 +-
 .gitignore                                    |  2 +-
 10 files changed, 69 insertions(+), 17 deletions(-)

diff --git a/configure.ac b/configure.ac
index aa586997..8c0398fe 100644
--- a/configure.ac
+++ b/configure.ac
@@ -327,7 +327,6 @@ AC_CHECK_FUNCS([\
 	fdatasync \
 	flockfile \
 	funlockfile \
-	get_current_dir_name \
 	mkostemp \
 	mlock \
 	mlockall \
@@ -340,6 +339,7 @@ AC_CHECK_FUNCS([\
 dnl Replacement functions that we provide for some platforms.
 AC_CONFIG_LIBOBJ_DIR([common/replacements])
 AC_REPLACE_FUNCS([\
+	get_current_dir_name \
 	getdelim \
 	getline \
 	openlog \
diff --git a/common/include/Makefile.am b/common/include/Makefile.am
index 151c2ae4..a7d0d026 100644
--- a/common/include/Makefile.am
+++ b/common/include/Makefile.am
@@ -38,7 +38,6 @@ EXTRA_DIST = \
 	ascii-string.h \
 	byte-swapping.h \
 	exit-with-parent.h \
-	get-current-dir-name.h \
 	isaligned.h \
 	ispowerof2.h \
 	iszero.h \
@@ -56,7 +55,6 @@ TESTS = \
 	test-ascii-ctype \
 	test-ascii-string \
 	test-byte-swapping \
-	test-current-dir-name \
 	test-isaligned \
 	test-ispowerof2 \
 	test-iszero \
@@ -79,10 +77,6 @@ test_byte_swapping_SOURCES = test-byte-swapping.c
byte-swapping.h
 test_byte_swapping_CPPFLAGS = -I$(srcdir)
 test_byte_swapping_CFLAGS = $(WARNINGS_CFLAGS)

-test_current_dir_name_SOURCES = test-current-dir-name.c get-current-dir-name.h
-test_current_dir_name_CPPFLAGS = -I$(srcdir)
-test_current_dir_name_CFLAGS = $(WARNINGS_CFLAGS)
-
 test_isaligned_SOURCES = test-isaligned.c isaligned.h
 test_isaligned_CPPFLAGS = -I$(srcdir)
 test_isaligned_CFLAGS = $(WARNINGS_CFLAGS)
diff --git a/common/replacements/Makefile.am b/common/replacements/Makefile.am
index 5051099a..5d73ac35 100644
--- a/common/replacements/Makefile.am
+++ b/common/replacements/Makefile.am
@@ -40,6 +40,8 @@ libcompat_la_SOURCES  libcompat_la_LIBADD = $(LTLIBOBJS)

 EXTRA_DIST = \
+	get_current_dir_name.c \
+	get_current_dir_name.h \
 	getdelim.c \
 	getline.c \
 	getline.h \
@@ -51,3 +53,11 @@ EXTRA_DIST = \
 	syslog.c \
 	syslog.h \
 	vsyslog.c
+
+TESTS = \
+	test-current-dir-name
+check_PROGRAMS = $(TESTS)
+
+test_current_dir_name_SOURCES = test-current-dir-name.c get_current_dir_name.h
+test_current_dir_name_CPPFLAGS = -I$(srcdir)
+test_current_dir_name_CFLAGS = $(WARNINGS_CFLAGS)
diff --git a/plugins/floppy/Makefile.am b/plugins/floppy/Makefile.am
index 6636caf2..84cedd2b 100644
--- a/plugins/floppy/Makefile.am
+++ b/plugins/floppy/Makefile.am
@@ -49,6 +49,7 @@ nbdkit_floppy_plugin_la_CPPFLAGS = \
 	-I$(top_srcdir)/include \
 	-I$(top_srcdir)/common/include \
 	-I$(top_srcdir)/common/regions \
+	-I$(top_srcdir)/common/replacements \
 	-I$(top_srcdir)/common/utils \
 	-I. \
 	$(NULL)
diff --git a/common/replacements/get_current_dir_name.h
b/common/replacements/get_current_dir_name.h
new file mode 100644
index 00000000..95d4d473
--- /dev/null
+++ b/common/replacements/get_current_dir_name.h
@@ -0,0 +1,49 @@
+/* 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.
+ */
+
+#ifndef NBDKIT_GET_CURRENT_DIR_NAME_H
+#define NBDKIT_GET_CURRENT_DIR_NAME_H
+
+#include <config.h>
+
+#ifdef HAVE_GET_CURRENT_DIR_NAME
+
+#include <unistd.h>
+
+#else
+
+extern char *get_current_dir_name (void);
+
+#endif
+
+#endif /* NBDKIT_GET_CURRENT_DIR_NAME_H */
diff --git a/server/public.c b/server/public.c
index 1f7e1af0..fce16989 100644
--- a/server/public.c
+++ b/server/public.c
@@ -53,7 +53,7 @@

 #include "ascii-ctype.h"
 #include "ascii-string.h"
-#include "get-current-dir-name.h"
+#include "get_current_dir_name.h"
 #include "getline.h"
 #include "realpath.h"

diff --git a/common/include/get-current-dir-name.h
b/common/replacements/get_current_dir_name.c
similarity index 92%
rename from common/include/get-current-dir-name.h
rename to common/replacements/get_current_dir_name.c
index 09c6a0fd..72ca45f0 100644
--- a/common/include/get-current-dir-name.h
+++ b/common/replacements/get_current_dir_name.c
@@ -32,9 +32,6 @@

 /* Implement get_current_dir_name(3) on platforms which don't have it. */

-#ifndef NBDKIT_GET_CURRENT_DIR_NAME_H
-#define NBDKIT_GET_CURRENT_DIR_NAME_H
-
 #include <config.h>

 #ifndef HAVE_GET_CURRENT_DIR_NAME
@@ -43,7 +40,9 @@
 #include <unistd.h>
 #include <limits.h>

-static inline char *
+#include "get_current_dir_name.h"
+
+char *
 get_current_dir_name (void)
 {
   char *ret;
@@ -56,6 +55,5 @@ get_current_dir_name (void)
     return NULL;
   return realloc (ret, strlen (ret) + 1);
 }
-#endif

-#endif /* NBDKIT_GET_CURRENT_DIR_NAME_H */
+#endif /* !HAVE_GET_CURRENT_DIR_NAME */
diff --git a/common/include/test-current-dir-name.c
b/common/replacements/test-current-dir-name.c
similarity index 98%
rename from common/include/test-current-dir-name.c
rename to common/replacements/test-current-dir-name.c
index c3ca52ad..a9bb8de0 100644
--- a/common/include/test-current-dir-name.c
+++ b/common/replacements/test-current-dir-name.c
@@ -40,7 +40,7 @@
 #undef NDEBUG /* Keep test strong even for nbdkit built without assertions */
 #include <assert.h>

-#include "get-current-dir-name.h"
+#include "get_current_dir_name.h"

 int
 main (void)
diff --git a/plugins/floppy/virtual-floppy.c b/plugins/floppy/virtual-floppy.c
index 18fd4b01..60916fc5 100644
--- a/plugins/floppy/virtual-floppy.c
+++ b/plugins/floppy/virtual-floppy.c
@@ -49,7 +49,7 @@

 #include "byte-swapping.h"
 #include "cleanup.h"
-#include "get-current-dir-name.h"
+#include "get_current_dir_name.h"
 #include "regions.h"
 #include "rounding.h"

diff --git a/.gitignore b/.gitignore
index ca36d9c2..792b73c6 100644
--- a/.gitignore
+++ b/.gitignore
@@ -36,7 +36,6 @@ plugins/*/*.3
 /common/include/test-ascii-ctype
 /common/include/test-ascii-string
 /common/include/test-byte-swapping
-/common/include/test-current-dir-name
 /common/include/test-isaligned
 /common/include/test-ispowerof2
 /common/include/test-iszero
@@ -47,6 +46,7 @@ plugins/*/*.3
 /common/protocol/generate-protostrings.sh
 /common/protocol/protostrings.c
 /common/replacements/libcompat.a
+/common/replacements/test-current-dir-name
 /common/replacements/win32/MSG00001.bin
 /common/replacements/win32/nbdkit-cat.h
 /common/replacements/win32/nbdkit-cat.rc
--
2.27.0