[libvirt] [PATCHv2 5/7] drivers: use virDirRead API

Eric Blake eblake at redhat.com
Sat Apr 26 13:18:18 UTC 2014


Convert all remaining clients of readdir to use the new
interface, so that we can ensure (unlikely) errors while
reading a directory are reported.

* src/openvz/openvz_conf.c (openvzAssignUUIDs): Use new
interface.
* src/parallels/parallels_storage.c (parallelsFindVolumes)
(parallelsFindVmVolumes): Report readdir failures.
* src/qemu/qemu_driver.c (qemuDomainSnapshotLoad): Ignore readdir
failures.
* src/secret/secret_driver.c (loadSecrets): Likewise.
* src/qemu/qemu_hostdev.c
(qemuHostdevHostSupportsPassthroughVFIO): Report readdir failures.
* src/xen/xen_inotify.c (xenInotifyOpen): Likewise.
* src/xen/xm_internal.c (xenXMConfigCacheRefresh): Likewise.

Signed-off-by: Eric Blake <eblake at redhat.com>
---
 src/openvz/openvz_conf.c          |  9 +--------
 src/parallels/parallels_storage.c | 11 ++++++++---
 src/qemu/qemu_driver.c            |  5 ++++-
 src/qemu/qemu_hostdev.c           |  7 ++++---
 src/secret/secret_driver.c        |  6 +++---
 src/xen/xen_inotify.c             |  7 +++++--
 src/xen/xm_internal.c             |  3 +--
 7 files changed, 26 insertions(+), 22 deletions(-)

diff --git a/src/openvz/openvz_conf.c b/src/openvz/openvz_conf.c
index a7aff2a..dc84b29 100644
--- a/src/openvz/openvz_conf.c
+++ b/src/openvz/openvz_conf.c
@@ -1105,20 +1105,13 @@ static int openvzAssignUUIDs(void)
         return 0;
     }

-    errno = 0;
-    while ((dent = readdir(dp))) {
+    while ((ret = virDirRead(dp, &dent, conf_dir)) > 0) {
         if (virStrToLong_i(dent->d_name, &ext, 10, &vpsid) < 0 ||
             *ext++ != '.' ||
             STRNEQ(ext, "conf"))
             continue;
         if (vpsid > 0) /* '0.conf' belongs to the host, ignore it */
             openvzSetUUID(vpsid);
-        errno = 0;
-    }
-    if (errno) {
-        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
-                       _("Failed to scan configuration directory"));
-        ret = -1;
     }

     closedir(dp);
diff --git a/src/parallels/parallels_storage.c b/src/parallels/parallels_storage.c
index 736d060..4dbaed1 100644
--- a/src/parallels/parallels_storage.c
+++ b/src/parallels/parallels_storage.c
@@ -93,6 +93,7 @@ parallelsFindVolumes(virStoragePoolObjPtr pool)
     struct dirent *ent;
     char *path = NULL;
     int ret = -1;
+    int direrr;

     if (!(dir = opendir(pool->def->target.path))) {
         virReportSystemError(errno,
@@ -101,7 +102,7 @@ parallelsFindVolumes(virStoragePoolObjPtr pool)
         return -1;
     }

-    while ((ent = readdir(dir)) != NULL) {
+    while ((direrr = virDirRead(dir, &ent, pool->def->target.path)) > 0) {
         if (!virFileHasSuffix(ent->d_name, ".xml"))
             continue;

@@ -113,6 +114,8 @@ parallelsFindVolumes(virStoragePoolObjPtr pool)

         VIR_FREE(path);
     }
+    if (direrr < 0)
+        goto cleanup;

     ret = 0;
  cleanup:
@@ -331,6 +334,7 @@ static int parallelsFindVmVolumes(virStoragePoolObjPtr pool,
     char *diskPath = NULL, *diskDescPath = NULL;
     struct stat sb;
     int ret = -1;
+    int direrr;

     if (!(dir = opendir(pdom->home))) {
         virReportSystemError(errno,
@@ -339,7 +343,7 @@ static int parallelsFindVmVolumes(virStoragePoolObjPtr pool,
         goto cleanup;
     }

-    while ((ent = readdir(dir)) != NULL) {
+    while ((direrr = virDirRead(dir, &ent, pdom->home)) > 0) {
         VIR_FREE(diskPath);
         VIR_FREE(diskDescPath);

@@ -368,8 +372,9 @@ static int parallelsFindVmVolumes(virStoragePoolObjPtr pool,
         if (parallelsAddDiskVolume(pool, dom, ent->d_name,
                                    diskPath, diskDescPath))
             goto cleanup;
-
     }
+    if (direrr < 0)
+        goto cleanup;

     ret = 0;
  cleanup:
diff --git a/src/qemu/qemu_driver.c b/src/qemu/qemu_driver.c
index 69a7053..f742dae 100644
--- a/src/qemu/qemu_driver.c
+++ b/src/qemu/qemu_driver.c
@@ -417,6 +417,7 @@ qemuDomainSnapshotLoad(virDomainObjPtr vm,
                           VIR_DOMAIN_SNAPSHOT_PARSE_INTERNAL);
     int ret = -1;
     virCapsPtr caps = NULL;
+    int direrr;

     virObjectLock(vm);
     if (virAsprintf(&snapDir, "%s/%s", baseDir, vm->def->name) < 0) {
@@ -439,7 +440,7 @@ qemuDomainSnapshotLoad(virDomainObjPtr vm,
         goto cleanup;
     }

-    while ((entry = readdir(dir))) {
+    while ((direrr = virDirRead(dir, &entry, NULL)) > 0) {
         if (entry->d_name[0] == '.')
             continue;

@@ -485,6 +486,8 @@ qemuDomainSnapshotLoad(virDomainObjPtr vm,
         VIR_FREE(fullpath);
         VIR_FREE(xmlStr);
     }
+    if (direrr < 0)
+        VIR_ERROR(_("Failed to fully read directory %s"), snapDir);

     if (vm->current_snapshot != current) {
         VIR_ERROR(_("Too many snapshots claiming to be current for domain %s"),
diff --git a/src/qemu/qemu_hostdev.c b/src/qemu/qemu_hostdev.c
index e9879c4..706db0c 100644
--- a/src/qemu/qemu_hostdev.c
+++ b/src/qemu/qemu_hostdev.c
@@ -1,7 +1,7 @@
 /*
  * qemu_hostdev.c: QEMU hostdev management
  *
- * Copyright (C) 2006-2007, 2009-2013 Red Hat, Inc.
+ * Copyright (C) 2006-2007, 2009-2014 Red Hat, Inc.
  * Copyright (C) 2006 Daniel P. Berrange
  *
  * This library is free software; you can redistribute it and/or
@@ -90,12 +90,13 @@ qemuHostdevHostSupportsPassthroughVFIO(void)
     DIR *iommuDir = NULL;
     struct dirent *iommuGroup = NULL;
     bool ret = false;
+    int direrr;

     /* condition 1 - /sys/kernel/iommu_groups/ contains entries */
     if (!(iommuDir = opendir("/sys/kernel/iommu_groups/")))
         goto cleanup;

-    while ((iommuGroup = readdir(iommuDir))) {
+    while ((direrr = virDirRead(iommuDir, &iommuGroup, NULL)) > 0) {
         /* skip ./ ../ */
         if (STRPREFIX(iommuGroup->d_name, "."))
             continue;
@@ -104,7 +105,7 @@ qemuHostdevHostSupportsPassthroughVFIO(void)
         break;
     }

-    if (!iommuGroup)
+    if (direrr < 0 || !iommuGroup)
         goto cleanup;
     /* okay, iommu is on and recognizes groups */

diff --git a/src/secret/secret_driver.c b/src/secret/secret_driver.c
index a7dfdf0..cd04b20 100644
--- a/src/secret/secret_driver.c
+++ b/src/secret/secret_driver.c
@@ -1,7 +1,7 @@
 /*
  * secret_driver.c: local driver for secret manipulation API
  *
- * Copyright (C) 2009-2012, 2014 Red Hat, Inc.
+ * Copyright (C) 2009-2014 Red Hat, Inc.
  *
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public
@@ -484,7 +484,7 @@ loadSecrets(virSecretDriverStatePtr driver,
                              driver->directory);
         goto cleanup;
     }
-    while ((de = readdir(dir)) != NULL) {
+    while (virDirRead(dir, &de, NULL) > 0) {
         virSecretEntryPtr secret;

         if (STREQ(de->d_name, ".") || STREQ(de->d_name, ".."))
@@ -503,7 +503,7 @@ loadSecrets(virSecretDriverStatePtr driver,
         }
         listInsert(&list, secret);
     }
-    /* Ignore error reported by readdir(), if any.  It's better to keep the
+    /* Ignore error reported by readdir, if any.  It's better to keep the
        secrets we managed to find. */

     while (list != NULL) {
diff --git a/src/xen/xen_inotify.c b/src/xen/xen_inotify.c
index 7c2d133..cd1e2df 100644
--- a/src/xen/xen_inotify.c
+++ b/src/xen/xen_inotify.c
@@ -4,7 +4,7 @@
  *                /etc/xen
  *                /var/lib/xend/domains
  *
- * Copyright (C) 2010-2013 Red Hat, Inc.
+ * Copyright (C) 2010-2014 Red Hat, Inc.
  * Copyright (C) 2008 VirtualIron
  *
  * This library is free software; you can redistribute it and/or
@@ -343,6 +343,7 @@ xenInotifyOpen(virConnectPtr conn,
     struct dirent *ent;
     char *path;
     xenUnifiedPrivatePtr priv = conn->privateData;
+    int direrr;

     virCheckFlags(VIR_CONNECT_RO, -1);

@@ -363,7 +364,7 @@ xenInotifyOpen(virConnectPtr conn,
                                  priv->configDir);
             return -1;
         }
-        while ((ent = readdir(dh))) {
+        while ((direrr = virDirRead(dh, &ent, priv->configDir)) > 0) {
             if (STRPREFIX(ent->d_name, "."))
                 continue;

@@ -384,6 +385,8 @@ xenInotifyOpen(virConnectPtr conn,
             VIR_FREE(path);
         }
         closedir(dh);
+        if (direrr < 0)
+            return -1;
     }

     if ((priv->inotifyFD = inotify_init()) < 0) {
diff --git a/src/xen/xm_internal.c b/src/xen/xm_internal.c
index f25a7df..30985d8 100644
--- a/src/xen/xm_internal.c
+++ b/src/xen/xm_internal.c
@@ -327,7 +327,7 @@ xenXMConfigCacheRefresh(virConnectPtr conn)
         return -1;
     }

-    while ((ent = readdir(dh))) {
+    while ((ret = virDirRead(dh, &ent, priv->configDir)) > 0) {
         struct stat st;
         char *path;

@@ -386,7 +386,6 @@ xenXMConfigCacheRefresh(virConnectPtr conn)
     args.now = now;
     args.priv = priv;
     virHashRemoveSet(priv->configCache, xenXMConfigReaper, &args);
-    ret = 0;

     closedir(dh);

-- 
1.9.0




More information about the libvir-list mailing list