]> git.ipfire.org Git - thirdparty/libvirt.git/commitdiff
Introduce virDirOpen
authorJán Tomko <jtomko@redhat.com>
Tue, 21 Jun 2016 14:33:36 +0000 (16:33 +0200)
committerJán Tomko <jtomko@redhat.com>
Fri, 24 Jun 2016 12:20:57 +0000 (14:20 +0200)
A helper that calls opendir and reports an error if it fails.

src/libvirt_private.syms
src/util/virfile.c
src/util/virfile.h

index 366a45b9d47ba0f3989f574a760a4e2d50b78734..e6c8df3af2d4e3f209c2f368a1e88f12c393c6d5 100644 (file)
@@ -1495,6 +1495,7 @@ safezero;
 virBuildPathInternal;
 virDirClose;
 virDirCreate;
+virDirOpen;
 virDirRead;
 virFileAbsPath;
 virFileAccessibleAs;
index 355255e2b4e6fa0b2cd92370565acd414276aef0..697ebc0f60d32d2a001f6e140aa1b9f481e7b3df 100644 (file)
@@ -2732,6 +2732,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
@@ -2740,13 +2765,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
index ab9eeba33027fa037ce5bb89b63b6e2ab0f444b2..c618842fc5928990f6e78f0cc376c09f0331243e 100644 (file)
@@ -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)