[PATCH 06/12] util: change virDirClose to take a DIR* instead of DIR**.

Laine Stump laine at redhat.com
Wed Oct 28 01:35:52 UTC 2020


In order to make a usable g_autoptr(DIR), we need to have a close
function that is a NOP when the pointer is NULL, but takes a simple
DIR*. But virDirClose() (candidate to be the g_autoptr cleanup
function) currently takes a DIR**, not DIR*. It does this so that it
can clear the pointer, thus making it safe to call virDirClose on the
same DIR multiple times.

In the past the clearing of the DIR* was essential in a few places,
but those few places have now been changed, so we can modify
virDirClose() to take a DIR*, and remove the side effect of clearing
the DIR*. This will make it directly usable as the g_autoptr cleanup,
and will mean that this:

   {
   DIR *dirp = NULL;
   blah blah ...
   VIR_DIR_CLOSE(dirp)
   }

is functionally identical to

   {
   g_autoptr(DIR) dirp = NULL;
   blah blah ...
   }

which will make conversion to using g_autoptr mechanical and simple to review.

(Note that virDirClose() will still check for NULL before attempting
to close, so that it can always be safely called, as long as the DIR*
was initialized to NULL (another prerequisite of becoming a g_autoptr
cleanup function)

Signed-off-by: Laine Stump <laine at redhat.com>
---
 src/util/virfile.c | 7 +++----
 src/util/virfile.h | 4 ++--
 2 files changed, 5 insertions(+), 6 deletions(-)

diff --git a/src/util/virfile.c b/src/util/virfile.c
index 970d4bd234..442d2fab96 100644
--- a/src/util/virfile.c
+++ b/src/util/virfile.c
@@ -2927,13 +2927,12 @@ int virDirRead(DIR *dirp, struct dirent **ent, const char *name)
     return !!*ent;
 }
 
-void virDirClose(DIR **dirp)
+void virDirClose(DIR *dirp)
 {
-    if (!*dirp)
+    if (!dirp)
         return;
 
-    closedir(*dirp); /* exempt from syntax-check */
-    *dirp = NULL;
+    closedir(dirp); /* exempt from syntax-check */
 }
 
 
diff --git a/src/util/virfile.h b/src/util/virfile.h
index 09488398c5..6fde4f88ca 100644
--- a/src/util/virfile.h
+++ b/src/util/virfile.h
@@ -269,9 +269,9 @@ int virDirOpenQuiet(DIR **dirp, const char *dirname)
     ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2) G_GNUC_WARN_UNUSED_RESULT;
 int virDirRead(DIR *dirp, struct dirent **ent, const char *dirname)
     ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2) G_GNUC_WARN_UNUSED_RESULT;
-void virDirClose(DIR **dirp)
+void virDirClose(DIR *dirp)
     ATTRIBUTE_NONNULL(1);
-#define VIR_DIR_CLOSE(dir)  virDirClose(&(dir))
+#define VIR_DIR_CLOSE(dir)  virDirClose(dir)
 
 int virFileMakePath(const char *path) G_GNUC_WARN_UNUSED_RESULT;
 int virFileMakePathWithMode(const char *path,
-- 
2.26.2




More information about the libvir-list mailing list