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 <laine@redhat.com>
Reviewed-by: Daniel Henrique Barboza <danielhb413@gmail.com>
This commit is contained in:
Laine Stump 2020-10-13 09:58:57 -04:00
parent 7f42bdf5c0
commit 24d8968cd0
2 changed files with 5 additions and 6 deletions

View File

@ -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 */
}

View File

@ -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,