From 24d8968cd0a718af4badbbc858b1b449fea7205a Mon Sep 17 00:00:00 2001 From: Laine Stump Date: Tue, 13 Oct 2020 09:58:57 -0400 Subject: [PATCH] util: change virDirClose to take a DIR* instead of DIR**. 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 Reviewed-by: Daniel Henrique Barboza --- 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,