[libvirt] [PATCH 08/18] Use virDirOpenIfExists

Ján Tomko jtomko at redhat.com
Tue Jun 21 16:05:31 UTC 2016


Use it instead of opendir everywhere we need to check for ENOENT.
---
 src/conf/network_conf.c     | 21 ++++++---------------
 src/conf/nwfilter_conf.c    | 10 +++-------
 src/conf/storage_conf.c     | 20 ++++++--------------
 src/conf/virdomainobjlist.c | 11 +++--------
 src/conf/virsecretobj.c     |  9 +++------
 src/network/bridge_driver.c | 11 +++--------
 src/qemu/qemu_driver.c      |  8 +-------
 src/util/vircgroup.c        | 14 ++++++--------
 src/util/virnuma.c          | 14 ++++----------
 9 files changed, 35 insertions(+), 83 deletions(-)

diff --git a/src/conf/network_conf.c b/src/conf/network_conf.c
index 1e4b719..2aa7242 100644
--- a/src/conf/network_conf.c
+++ b/src/conf/network_conf.c
@@ -3236,14 +3236,10 @@ virNetworkLoadAllState(virNetworkObjListPtr nets,
     DIR *dir;
     struct dirent *entry;
     int ret = -1;
+    int rc;
 
-    if (!(dir = opendir(stateDir))) {
-        if (errno == ENOENT)
-            return 0;
-
-        virReportSystemError(errno, _("Failed to open dir '%s'"), stateDir);
-        return -1;
-    }
+    if ((rc = virDirOpenIfExists(&dir, stateDir)) <= 0)
+        return rc;
 
     while ((ret = virDirRead(dir, &entry, stateDir)) > 0) {
         virNetworkObjPtr net;
@@ -3270,15 +3266,10 @@ int virNetworkLoadAllConfigs(virNetworkObjListPtr nets,
     DIR *dir;
     struct dirent *entry;
     int ret = -1;
+    int rc;
 
-    if (!(dir = opendir(configDir))) {
-        if (errno == ENOENT)
-            return 0;
-        virReportSystemError(errno,
-                             _("Failed to open dir '%s'"),
-                             configDir);
-        return -1;
-    }
+    if ((rc = virDirOpenIfExists(&dir, configDir)) <= 0)
+        return rc;
 
     while ((ret = virDirRead(dir, &entry, configDir)) > 0) {
         virNetworkObjPtr net;
diff --git a/src/conf/nwfilter_conf.c b/src/conf/nwfilter_conf.c
index 56f8b86..74f2a14 100644
--- a/src/conf/nwfilter_conf.c
+++ b/src/conf/nwfilter_conf.c
@@ -3204,14 +3204,10 @@ virNWFilterLoadAllConfigs(virNWFilterObjListPtr nwfilters,
     DIR *dir;
     struct dirent *entry;
     int ret = -1;
+    int rc;
 
-    if (!(dir = opendir(configDir))) {
-        if (errno == ENOENT)
-            return 0;
-        virReportSystemError(errno, _("Failed to open dir '%s'"),
-                             configDir);
-        return -1;
-    }
+    if ((rc = virDirOpenIfExists(&dir, configDir)) <= 0)
+        return rc;
 
     while ((ret = virDirRead(dir, &entry, configDir)) > 0) {
         virNWFilterObjPtr nwfilter;
diff --git a/src/conf/storage_conf.c b/src/conf/storage_conf.c
index 5c044d2..d1ca08b 100644
--- a/src/conf/storage_conf.c
+++ b/src/conf/storage_conf.c
@@ -1941,14 +1941,10 @@ virStoragePoolLoadAllState(virStoragePoolObjListPtr pools,
     DIR *dir;
     struct dirent *entry;
     int ret = -1;
+    int rc;
 
-    if (!(dir = opendir(stateDir))) {
-        if (errno == ENOENT)
-            return 0;
-
-        virReportSystemError(errno, _("Failed to open dir '%s'"), stateDir);
-        return -1;
-    }
+    if ((rc = virDirOpenIfExists(&dir, stateDir)) <= 0)
+        return rc;
 
     while ((ret = virDirRead(dir, &entry, stateDir)) > 0) {
         virStoragePoolObjPtr pool;
@@ -1977,14 +1973,10 @@ virStoragePoolLoadAllConfigs(virStoragePoolObjListPtr pools,
     DIR *dir;
     struct dirent *entry;
     int ret;
+    int rc;
 
-    if (!(dir = opendir(configDir))) {
-        if (errno == ENOENT)
-            return 0;
-        virReportSystemError(errno, _("Failed to open dir '%s'"),
-                             configDir);
-        return -1;
-    }
+    if ((rc = virDirOpenIfExists(&dir, configDir)) <= 0)
+        return rc;
 
     while ((ret = virDirRead(dir, &entry, configDir)) > 0) {
         char *path;
diff --git a/src/conf/virdomainobjlist.c b/src/conf/virdomainobjlist.c
index 4f7756d..51753fe 100644
--- a/src/conf/virdomainobjlist.c
+++ b/src/conf/virdomainobjlist.c
@@ -566,17 +566,12 @@ virDomainObjListLoadAllConfigs(virDomainObjListPtr doms,
     DIR *dir;
     struct dirent *entry;
     int ret = -1;
+    int rc;
 
     VIR_INFO("Scanning for configs in %s", configDir);
 
-    if (!(dir = opendir(configDir))) {
-        if (errno == ENOENT)
-            return 0;
-        virReportSystemError(errno,
-                             _("Failed to open dir '%s'"),
-                             configDir);
-        return -1;
-    }
+    if ((rc = virDirOpenIfExists(&dir, configDir)) <= 0)
+        return rc;
 
     virObjectLock(doms);
 
diff --git a/src/conf/virsecretobj.c b/src/conf/virsecretobj.c
index 46042eb..a093258 100644
--- a/src/conf/virsecretobj.c
+++ b/src/conf/virsecretobj.c
@@ -966,13 +966,10 @@ virSecretLoadAllConfigs(virSecretObjListPtr secrets,
 {
     DIR *dir = NULL;
     struct dirent *de;
+    int rc;
 
-    if (!(dir = opendir(configDir))) {
-        if (errno == ENOENT)
-            return 0;
-        virReportSystemError(errno, _("cannot open '%s'"), configDir);
-        return -1;
-    }
+    if ((rc = virDirOpenIfExists(&dir, configDir)) <= 0)
+        return rc;
 
     /* Ignore errors reported by readdir or other calls within the
      * loop (if any).  It's better to keep the secrets we managed to find. */
diff --git a/src/network/bridge_driver.c b/src/network/bridge_driver.c
index 7b021d8..b108152 100644
--- a/src/network/bridge_driver.c
+++ b/src/network/bridge_driver.c
@@ -509,15 +509,10 @@ networkMigrateStateFiles(virNetworkDriverStatePtr driver)
     struct dirent *entry;
     char *oldPath = NULL, *newPath = NULL;
     char *contents = NULL;
+    int rc;
 
-    if (!(dir = opendir(oldStateDir))) {
-        if (errno == ENOENT)
-            return 0;
-
-        virReportSystemError(errno, _("failed to open directory '%s'"),
-                             oldStateDir);
-        return -1;
-    }
+    if ((rc = virDirOpenIfExists(&dir, oldStateDir)) <= 0)
+        return rc;
 
     if (virFileMakePath(driver->stateDir) < 0) {
         virReportSystemError(errno, _("cannot create directory %s"),
diff --git a/src/qemu/qemu_driver.c b/src/qemu/qemu_driver.c
index 76ee3c1..7769272 100644
--- a/src/qemu/qemu_driver.c
+++ b/src/qemu/qemu_driver.c
@@ -498,14 +498,8 @@ qemuDomainSnapshotLoad(virDomainObjPtr vm,
     VIR_INFO("Scanning for snapshots for domain %s in %s", vm->def->name,
              snapDir);
 
-    if (!(dir = opendir(snapDir))) {
-        if (errno != ENOENT)
-            virReportSystemError(errno,
-                                 _("Failed to open snapshot directory %s "
-                                 "for domain %s"),
-                                 snapDir, vm->def->name);
+    if (virDirOpenIfExists(&dir, snapDir) <= 0)
         goto cleanup;
-    }
 
     while ((direrr = virDirRead(dir, &entry, NULL)) > 0) {
         if (entry->d_name[0] == '.')
diff --git a/src/util/vircgroup.c b/src/util/vircgroup.c
index ce9b942..634f659 100644
--- a/src/util/vircgroup.c
+++ b/src/util/vircgroup.c
@@ -3625,15 +3625,13 @@ virCgroupKillRecursiveInternal(virCgroupPtr group,
         killedAny = true;
 
     VIR_DEBUG("Iterate over children of %s (killedAny=%d)", keypath, killedAny);
-    if (!(dp = opendir(keypath))) {
-        if (errno == ENOENT) {
-            VIR_DEBUG("Path %s does not exist, assuming done", keypath);
-            killedAny = false;
-            goto done;
-        }
-        virReportSystemError(errno,
-                             _("Cannot open %s"), keypath);
+    if ((rc = virDirOpenIfExists(&dp, keypath)) < 0)
         goto cleanup;
+
+    if (rc == 0) {
+        VIR_DEBUG("Path %s does not exist, assuming done", keypath);
+        killedAny = false;
+        goto done;
     }
 
     while ((direrr = virDirRead(dp, &ent, keypath)) > 0) {
diff --git a/src/util/virnuma.c b/src/util/virnuma.c
index b756f7f..fc25051 100644
--- a/src/util/virnuma.c
+++ b/src/util/virnuma.c
@@ -737,16 +737,10 @@ virNumaGetPages(int node,
     if (virNumaGetHugePageInfoDir(&path, node) < 0)
         goto cleanup;
 
-    if (!(dir = opendir(path))) {
-        /* It's okay if the @path doesn't exist. Maybe we are running on
-         * system without huge pages support where the path may not exist. */
-        if (errno != ENOENT) {
-            virReportSystemError(errno,
-                                 _("unable to open path: %s"),
-                                 path);
-            goto cleanup;
-        }
-    }
+    /* It's okay if the @path doesn't exist. Maybe we are running on
+     * system without huge pages support where the path may not exist. */
+    if (virDirOpenIfExists(&dir, path) < 0)
+        goto cleanup;
 
     while (dir && (direrr = virDirRead(dir, &entry, path)) > 0) {
         const char *page_name = entry->d_name;
-- 
2.7.3




More information about the libvir-list mailing list