From 1ce2f1a4349654c9311f00d8b745ed21e26a5ee7 Mon Sep 17 00:00:00 2001 From: Natanael Copa Date: Sun, 20 Apr 2014 13:53:45 +0200 Subject: [PATCH] util: introduce virDirRead wrapper for readdir Introduce a wrapper for readdir. This helps us make sure that we always set errno before calling readdir and it will make sure errors are properly logged. Signed-off-by: Natanael Copa Signed-off-by: Eric Blake --- src/libvirt_private.syms | 1 + src/util/virfile.c | 33 +++++++++++++++++++++++++++++++++ src/util/virfile.h | 4 ++++ 3 files changed, 38 insertions(+) diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms index 5788468721..8bbe6e76de 100644 --- a/src/libvirt_private.syms +++ b/src/libvirt_private.syms @@ -1222,6 +1222,7 @@ safewrite; safezero; virBuildPathInternal; virDirCreate; +virDirRead; virFileAbsPath; virFileAccessibleAs; virFileActivateDirOverride; diff --git a/src/util/virfile.c b/src/util/virfile.c index da4e0a0c51..34811243c8 100644 --- a/src/util/virfile.c +++ b/src/util/virfile.c @@ -2391,6 +2391,39 @@ virDirCreate(const char *path ATTRIBUTE_UNUSED, } #endif /* WIN32 */ +/** + * virDirRead: + * @dirp: directory to read + * @end: output one entry + * @name: if non-NULL, the name related to @dirp for use in error reporting + * + * Wrapper around readdir. Typical usage: + * struct dirent ent; + * int value; + * DIR *dir; + * if (!(dir = opendir(name))) + * goto error; + * while ((value = virDirRead(dir, &ent, name)) > 0) + * process ent; + * if (value < 0) + * goto error; + * + * Returns -1 on error, with error already reported if @name was + * supplied. On success, returns 1 for entry read, 0 for end-of-dir. + */ +int virDirRead(DIR *dirp, struct dirent **ent, const char *name) +{ + errno = 0; + *ent = readdir(dirp); + if (!*ent && errno) { + if (name) + virReportSystemError(errno, _("Unable to read directory '%s'"), + name); + return -1; + } + return !!*ent; +} + static int virFileMakePathHelper(char *path, mode_t mode) { diff --git a/src/util/virfile.h b/src/util/virfile.h index 5d0d6995d2..12bcb227bf 100644 --- a/src/util/virfile.h +++ b/src/util/virfile.h @@ -27,6 +27,7 @@ # define __VIR_FILE_H_ # include +# include # include "internal.h" # include "virstoragefile.h" @@ -225,6 +226,9 @@ enum { }; int virDirCreate(const char *path, mode_t mode, uid_t uid, gid_t gid, unsigned int flags) ATTRIBUTE_RETURN_CHECK; +int virDirRead(DIR *dirp, struct dirent **ent, const char *dirname) + ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2) ATTRIBUTE_RETURN_CHECK; + int virFileMakePath(const char *path) ATTRIBUTE_RETURN_CHECK; int virFileMakePathWithMode(const char *path, mode_t mode) ATTRIBUTE_RETURN_CHECK;