[PATCH 05/18] Introduce virDirOpen

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



A helper that calls opendir and reports an error if it fails.
---
 src/libvirt_private.syms |  1 +
 src/util/virfile.c       | 33 +++++++++++++++++++++++++++++----
 src/util/virfile.h       |  2 ++
 3 files changed, 32 insertions(+), 4 deletions(-)

diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms
index f4dc88d..457fe19 100644
--- a/src/libvirt_private.syms
+++ b/src/libvirt_private.syms
@@ -1495,6 +1495,7 @@ safezero;
 virBuildPathInternal;
 virDirClose;
 virDirCreate;
+virDirOpen;
 virDirRead;
 virFileAbsPath;
 virFileAccessibleAs;
diff --git a/src/util/virfile.c b/src/util/virfile.c
index ce8f7fd..aac0324 100644
--- a/src/util/virfile.c
+++ b/src/util/virfile.c
@@ -2736,6 +2736,31 @@ virFileRemove(const char *path,
 }
 #endif /* WIN32 */
 
+static int
+virDirOpenInternal(DIR **dirp, const char *name)
+{
+    *dirp = opendir(name);
+    if (!*dirp) {
+        virReportSystemError(errno, _("cannot open directory '%s'"), name);
+        return -1;
+    }
+    return 1;
+}
+
+/**
+ * virDirOpen
+ * @dirp: directory stream
+ * @name: path of the directory
+ *
+ * Returns 1 on success.
+ * On failure, -1 is returned and an error is reported.
+ */
+int
+virDirOpen(DIR **dirp, const char *name)
+{
+    return virDirOpenInternal(dirp, name);
+}
+
 /**
  * virDirRead:
  * @dirp: directory to read
@@ -2744,13 +2769,13 @@ virFileRemove(const char *path,
  *
  * Wrapper around readdir. Typical usage:
  *   struct dirent ent;
- *   int value;
+ *   int rc;
  *   DIR *dir;
- *   if (!(dir = opendir(name)))
+ *   if (virDirOpen(&dir, name) < 0)
  *       goto error;
- *   while ((value = virDirRead(dir, &ent, name)) > 0)
+ *   while ((rc = virDirRead(dir, &ent, name)) > 0)
  *       process ent;
- *   if (value < 0)
+ *   if (rc < 0)
  *       goto error;
  *
  * Returns -1 on error, with error already reported if @name was
diff --git a/src/util/virfile.h b/src/util/virfile.h
index ab9eeba..c618842 100644
--- a/src/util/virfile.h
+++ b/src/util/virfile.h
@@ -230,6 +230,8 @@ enum {
 };
 int virDirCreate(const char *path, mode_t mode, uid_t uid, gid_t gid,
                  unsigned int flags) ATTRIBUTE_RETURN_CHECK;
+int virDirOpen(DIR **dirp, const char *dirname)
+    ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2) ATTRIBUTE_RETURN_CHECK;
 int virDirRead(DIR *dirp, struct dirent **ent, const char *dirname)
     ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2) ATTRIBUTE_RETURN_CHECK;
 void virDirClose(DIR **dirp)
-- 
2.7.3

--
libvir-list mailing list
libvir-list@xxxxxxxxxx
https://www.redhat.com/mailman/listinfo/libvir-list



[Index of Archives]     [Virt Tools]     [Libvirt Users]     [Lib OS Info]     [Fedora Users]     [Fedora Desktop]     [Fedora SELinux]     [Big List of Linux Books]     [Yosemite News]     [KDE Users]     [Fedora Tools]