[Libguestfs] [PATCH v6 07/41] daemon: Use common/cleanups to implement most cleanups.

Richard W.M. Jones
Thu, 15 Jun 2017 17:05:57 -0000

Remove duplicate reimplementation of (most) cleanup functions in the
daemon.  Use the code from common/cleanups instead.
---
 daemon/Makefile.am  |  7 +++++--
 daemon/cleanups.c   | 53 ++++++-----------------------------------------------
 daemon/cleanups.h   | 51 ---------------------------------------------------
 daemon/daemon.h     | 25 +++++++++++++++++++++++--
 docs/C_SOURCE_FILES |  1 -
 5 files changed, 34 insertions(+), 103 deletions(-)

diff --git a/daemon/Makefile.am b/daemon/Makefile.am
index 0d3dde516..9695500bf 100644
--- a/daemon/Makefile.am
+++ b/daemon/Makefile.am
@@ -49,6 +49,7 @@ endif
 guestfsd_SOURCES = \
 	../common/errnostring/errnostring.h \
 	../common/protocol/guestfs_protocol.h \
+	../common/cleanups/cleanups.h \
 	9p.c \
 	acl.c \
 	actions.h \
@@ -62,7 +63,6 @@ guestfsd_SOURCES = \
 	cap.c \
 	checksum.c \
 	cleanups.c \
-	cleanups.h \
 	cmp.c \
 	command.c \
 	command.h \
@@ -178,6 +178,7 @@ guestfsd_SOURCES = \
 guestfsd_LDADD = \
 	../common/errnostring/liberrnostring.la \
 	../common/protocol/libprotocol.la \
+	../common/cleanups/libcleanups.la \
 	$(ACL_LIBS) \
 	$(CAP_LIBS) \
 	$(YAJL_LIBS) \
@@ -206,7 +207,9 @@ guestfsd_CPPFLAGS = \
 	-I$(top_srcdir)/common/errnostring \
 	-I$(top_builddir)/common/errnostring \
 	-I$(top_srcdir)/common/protocol \
-	-I$(top_builddir)/common/protocol
+	-I$(top_builddir)/common/protocol \
+	-I$(top_srcdir)/common/cleanups \
+	-I$(top_builddir)/common/cleanups
 guestfsd_CFLAGS = \
 	$(WARN_CFLAGS) $(WERROR_CFLAGS) \
 	$(RPC_CFLAGS) \
diff --git a/daemon/cleanups.c b/daemon/cleanups.c
index 3102cf94b..c73b9e492 100644
--- a/daemon/cleanups.c
+++ b/daemon/cleanups.c
@@ -24,51 +24,7 @@

 #include <augeas.h>

-#include "cleanups.h"
-
-/* Use by the CLEANUP_* macros.  Do not call these directly. */
-void
-cleanup_free (void *ptr)
-{
-  free (* (void **) ptr);
-}
-
-extern void free_strings (char **argv);
-
-void
-cleanup_free_string_list (void *ptr)
-{
-  free_strings (* (char ***) ptr);
-}
-
-void
-cleanup_unlink_free (void *ptr)
-{
-  char *filename = * (char **) ptr;
-
-  if (filename) {
-    unlink (filename);
-    free (filename);
-  }
-}
-
-void
-cleanup_close (void *ptr)
-{
-  const int fd = * (int *) ptr;
-
-  if (fd >= 0)
-    close (fd);
-}
-
-void
-cleanup_fclose (void *ptr)
-{
-  FILE *f = * (FILE **) ptr;
-
-  if (f)
-    fclose (f);
-}
+#include "daemon.h"

 void
 cleanup_aug_close (void *ptr)
@@ -79,8 +35,11 @@ cleanup_aug_close (void *ptr)
     aug_close (aug);
 }

-struct stringsbuf;
-extern void free_stringsbuf (struct stringsbuf *sb);
+void
+cleanup_free_string_list (void *ptr)
+{
+  free_strings (* (char ***) ptr);
+}

 void
 cleanup_free_stringsbuf (void *ptr)
diff --git a/daemon/cleanups.h b/daemon/cleanups.h
deleted file mode 100644
index a791244cb..000000000
--- a/daemon/cleanups.h
+++ /dev/null
@@ -1,51 +0,0 @@
-/* libguestfs - the guestfsd daemon
- * Copyright (C) 2009-2015 Red Hat Inc.
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License along
- * with this program; if not, write to the Free Software Foundation, Inc.,
- * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
- */
-
-#ifndef GUESTFSD_CLEANUPS_H
-#define GUESTFSD_CLEANUPS_H
-
-/* These functions are used internally by the CLEANUP_* macros.
- * Don't call them directly.
- */
-extern void cleanup_free (void *ptr);
-extern void cleanup_free_string_list (void *ptr);
-extern void cleanup_unlink_free (void *ptr);
-extern void cleanup_close (void *ptr);
-extern void cleanup_fclose (void *ptr);
-extern void cleanup_aug_close (void *ptr);
-extern void cleanup_free_stringsbuf (void *ptr);
-
-#ifdef HAVE_ATTRIBUTE_CLEANUP
-#define CLEANUP_FREE __attribute__((cleanup(cleanup_free)))
-#define CLEANUP_FREE_STRING_LIST                        \
-    __attribute__((cleanup(cleanup_free_string_list)))
-#define CLEANUP_UNLINK_FREE __attribute__((cleanup(cleanup_unlink_free)))
-#define CLEANUP_CLOSE __attribute__((cleanup(cleanup_close)))
-#define CLEANUP_FCLOSE __attribute__((cleanup(cleanup_fclose)))
-#define CLEANUP_AUG_CLOSE __attribute__((cleanup(cleanup_aug_close)))
-#define CLEANUP_FREE_STRINGSBUF
__attribute__((cleanup(cleanup_free_stringsbuf)))
-#else
-#define CLEANUP_FREE
-#define CLEANUP_FREE_STRING_LIST
-#define CLEANUP_UNLINK_FREE
-#define CLEANUP_CLOSE
-#define CLEANUP_AUG_CLOSE
-#define CLEANUP_FREE_STRINGSBUF
-#endif
-
-#endif /* GUESTFSD_CLEANUPS_H */
diff --git a/daemon/daemon.h b/daemon/daemon.h
index 400116514..746af22b9 100644
--- a/daemon/daemon.h
+++ b/daemon/daemon.h
@@ -30,9 +30,10 @@

 #include "guestfs_protocol.h"

-#include "guestfs-internal-all.h"
-
 #include "cleanups.h"
+
+#include "guestfs-internal-all.h"
+
 #include "structs-cleanups.h"
 #include "command.h"

@@ -76,6 +77,26 @@ extern int xread (int sock, void *buf, size_t len)

 extern char *mountable_to_string (const mountable_t *mountable);

+/*-- in cleanups.c --*/
+
+/* These functions are used internally by the CLEANUP_* macros.
+ * Don't call them directly.
+ */
+extern void cleanup_aug_close (void *ptr);
+extern void cleanup_free_string_list (void *ptr);
+extern void cleanup_free_stringsbuf (void *ptr);
+
+#ifdef HAVE_ATTRIBUTE_CLEANUP
+#define CLEANUP_AUG_CLOSE __attribute__((cleanup(cleanup_aug_close)))
+#define CLEANUP_FREE_STRING_LIST                        \
+  __attribute__((cleanup(cleanup_free_string_list)))
+#define CLEANUP_FREE_STRINGSBUF
__attribute__((cleanup(cleanup_free_stringsbuf)))
+#else
+#define CLEANUP_AUG_CLOSE
+#define CLEANUP_FREE_STRING_LIST
+#define CLEANUP_FREE_STRINGSBUF
+#endif
+
 /*-- in mount.c --*/

 extern int mount_vfs_nochroot (const char *options, const char *vfstype,
diff --git a/docs/C_SOURCE_FILES b/docs/C_SOURCE_FILES
index 3382fce56..ceed9581d 100644
--- a/docs/C_SOURCE_FILES
+++ b/docs/C_SOURCE_FILES
@@ -73,7 +73,6 @@ daemon/btrfs.c
 daemon/cap.c
 daemon/checksum.c
 daemon/cleanups.c
-daemon/cleanups.h
 daemon/cmp.c
 daemon/command.c
 daemon/command.h
--
2.13.0