Blame SOURCES/libvirt-util-file-introduce-VIR_AUTOCLOSE-macro-to-close-fd-of-the-file-automatically.patch

Pablo Greco 40546a
From 3d08d15b8fb214233e6b426bffe8f8b89969529a Mon Sep 17 00:00:00 2001
Pablo Greco 40546a
Message-Id: <3d08d15b8fb214233e6b426bffe8f8b89969529a@dist-git>
Pablo Greco 40546a
From: Shi Lei <shi_lei@massclouds.com>
Pablo Greco 40546a
Date: Fri, 21 Jun 2019 09:25:42 +0200
Pablo Greco 40546a
Subject: [PATCH] util: file: introduce VIR_AUTOCLOSE macro to close fd of the
Pablo Greco 40546a
 file automatically
Pablo Greco 40546a
MIME-Version: 1.0
Pablo Greco 40546a
Content-Type: text/plain; charset=UTF-8
Pablo Greco 40546a
Content-Transfer-Encoding: 8bit
Pablo Greco 40546a
Pablo Greco 40546a
Signed-off-by: Shi Lei <shi_lei@massclouds.com>
Pablo Greco 40546a
(cherry picked from commit 09d35afd2c3058688290d5d818343d0f6aa2dd6e)
Pablo Greco 40546a
Pablo Greco 40546a
https://bugzilla.redhat.com/show_bug.cgi?id=1697627
Pablo Greco 40546a
Pablo Greco 40546a
Signed-off-by: Jiri Denemark <jdenemar@redhat.com>
Pablo Greco 40546a
Message-Id: <93cb7516e852b96a82eac8dd71acdccc81ef13f9.1561068591.git.jdenemar@redhat.com>
Pablo Greco 40546a
Reviewed-by: Ján Tomko <jtomko@redhat.com>
Pablo Greco 40546a
---
Pablo Greco 40546a
 src/util/virfile.h | 18 ++++++++++++++++--
Pablo Greco 40546a
 1 file changed, 16 insertions(+), 2 deletions(-)
Pablo Greco 40546a
Pablo Greco 40546a
diff --git a/src/util/virfile.h b/src/util/virfile.h
Pablo Greco 40546a
index 51c221e069..fa03269289 100644
Pablo Greco 40546a
--- a/src/util/virfile.h
Pablo Greco 40546a
+++ b/src/util/virfile.h
Pablo Greco 40546a
@@ -53,6 +53,11 @@ int virFileClose(int *fdptr, virFileCloseFlags flags)
Pablo Greco 40546a
 int virFileFclose(FILE **file, bool preserve_errno) ATTRIBUTE_RETURN_CHECK;
Pablo Greco 40546a
 FILE *virFileFdopen(int *fdptr, const char *mode) ATTRIBUTE_RETURN_CHECK;
Pablo Greco 40546a
 
Pablo Greco 40546a
+static inline void virForceCloseHelper(int *fd)
Pablo Greco 40546a
+{
Pablo Greco 40546a
+    ignore_value(virFileClose(fd, VIR_FILE_CLOSE_PRESERVE_ERRNO));
Pablo Greco 40546a
+}
Pablo Greco 40546a
+
Pablo Greco 40546a
 /* For use on normal paths; caller must check return value,
Pablo Greco 40546a
    and failure sets errno per close. */
Pablo Greco 40546a
 # define VIR_CLOSE(FD) virFileClose(&(FD), 0)
Pablo Greco 40546a
@@ -63,8 +68,7 @@ FILE *virFileFdopen(int *fdptr, const char *mode) ATTRIBUTE_RETURN_CHECK;
Pablo Greco 40546a
 
Pablo Greco 40546a
 /* For use on cleanup paths; errno is unaffected by close,
Pablo Greco 40546a
    and no return value to worry about. */
Pablo Greco 40546a
-# define VIR_FORCE_CLOSE(FD) \
Pablo Greco 40546a
-    ignore_value(virFileClose(&(FD), VIR_FILE_CLOSE_PRESERVE_ERRNO))
Pablo Greco 40546a
+# define VIR_FORCE_CLOSE(FD) virForceCloseHelper(&(FD))
Pablo Greco 40546a
 # define VIR_FORCE_FCLOSE(FILE) ignore_value(virFileFclose(&(FILE), true))
Pablo Greco 40546a
 
Pablo Greco 40546a
 /* Similar VIR_FORCE_CLOSE() but ignores EBADF errors since they are expected
Pablo Greco 40546a
@@ -79,6 +83,16 @@ FILE *virFileFdopen(int *fdptr, const char *mode) ATTRIBUTE_RETURN_CHECK;
Pablo Greco 40546a
                  VIR_FILE_CLOSE_PRESERVE_ERRNO | \
Pablo Greco 40546a
                  VIR_FILE_CLOSE_DONT_LOG))
Pablo Greco 40546a
 
Pablo Greco 40546a
+/**
Pablo Greco 40546a
+ * VIR_AUTOCLOSE:
Pablo Greco 40546a
+ *
Pablo Greco 40546a
+ * Macro to automatically force close the fd by calling virForceCloseHelper
Pablo Greco 40546a
+ * when the fd goes out of scope. It's used to eliminate VIR_FORCE_CLOSE
Pablo Greco 40546a
+ * in cleanup sections.
Pablo Greco 40546a
+ */
Pablo Greco 40546a
+# define VIR_AUTOCLOSE __attribute__((cleanup(virForceCloseHelper))) int
Pablo Greco 40546a
+
Pablo Greco 40546a
+
Pablo Greco 40546a
 /* Opaque type for managing a wrapper around a fd.  */
Pablo Greco 40546a
 struct _virFileWrapperFd;
Pablo Greco 40546a
 
Pablo Greco 40546a
-- 
Pablo Greco 40546a
2.22.0
Pablo Greco 40546a