[libvirt] [PATCH 1/2] util: introduce virDirRead wrapper for readdir

Natanael Copa ncopa at alpinelinux.org
Sun Apr 20 11:53:45 UTC 2014


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 <ncopa at alpinelinux.org>
---
 src/util/virfile.c | 14 ++++++++++++++
 src/util/virfile.h |  3 +++
 2 files changed, 17 insertions(+)

diff --git a/src/util/virfile.c b/src/util/virfile.c
index 3eb2703..b54b9fd 100644
--- a/src/util/virfile.c
+++ b/src/util/virfile.c
@@ -2295,6 +2295,20 @@ virDirCreate(const char *path ATTRIBUTE_UNUSED,
 }
 #endif /* WIN32 */
 
+/* return 0 = success, 1 = end-of-dir and -1 = error */
+int virDirRead(DIR *dirp, struct dirent **ent, const char *dirname)
+{
+    errno = 0;
+    *ent = readdir(dirp);
+    if (!*ent && errno) {
+        if (dirname)
+            virReportSystemError(errno, _("Unable to read directory '%s'"),
+                                 dirname);
+        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 46ef781..622a81b 100644
--- a/src/util/virfile.h
+++ b/src/util/virfile.h
@@ -27,6 +27,7 @@
 # define __VIR_FILE_H_
 
 # include <stdio.h>
+# include <dirent.h>
 
 # include "internal.h"
 # include "virstoragefile.h"
@@ -211,6 +212,8 @@ 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);
+
 int virFileMakePath(const char *path) ATTRIBUTE_RETURN_CHECK;
 int virFileMakePathWithMode(const char *path,
                             mode_t mode) ATTRIBUTE_RETURN_CHECK;
-- 
1.9.2




More information about the libvir-list mailing list