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

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