]> xenbits.xensource.com Git - libvirt.git/commitdiff
Use virDirOpenIfExists
authorJán Tomko <jtomko@redhat.com>
Tue, 21 Jun 2016 14:47:24 +0000 (16:47 +0200)
committerJán Tomko <jtomko@redhat.com>
Fri, 24 Jun 2016 12:20:57 +0000 (14:20 +0200)
Use it instead of opendir everywhere we need to check for ENOENT.

src/conf/network_conf.c
src/conf/nwfilter_conf.c
src/conf/storage_conf.c
src/conf/virdomainobjlist.c
src/conf/virsecretobj.c
src/network/bridge_driver.c
src/qemu/qemu_driver.c
src/util/vircgroup.c
src/util/virnuma.c

index e9f2f79321d2992da53015679c98d551ec13df1d..dfa851b9d6c471b8fce8d0811e304641fae05956 100644 (file)
@@ -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;
@@ -3267,15 +3263,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;
index db92f7cd493898171fc46657be377b8026e5bc9b..2cdcfa74eb235b5bcbf6ad77f08b69ee8afd0d3c 100644 (file)
@@ -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;
index 3c1fd02e92e17c00290bbf23b885bf975d4d3697..05a1a49cee4fd0ac57cdf5e015f20a7edbd1d322 100644 (file)
@@ -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;
@@ -1974,14 +1970,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;
index 46763f91bc8ab39372b07db713a78829ff2c33ae..41c9910dc40c9f65e9a78b802f7ef181dbe38ddd 100644 (file)
@@ -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);
 
index f4529ccd4fcacca236d32eaae8a64b67d87df16f..30a5e80f6e40f8e5147a85fc805877894996990b 100644 (file)
@@ -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. */
index 728baf3e4d70f1e3b5ed9708a846a2718a394da6..58ceaf252982acb44d9b77644b3d7512fcdd573b 100644 (file)
@@ -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"),
index 9973392ca8e9a819ee73c07d49accff614010ab1..713a1cd4b5e76298512193a3cf689dcd277cd70b 100644 (file)
@@ -500,14 +500,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) {
         /* NB: ignoring errors, so one malformed config doesn't
index e90cceee222b9cdbf384a61e5909aa3f990bdd30..4c6abd96ce461c39ef9537bed00b5141929f2980 100644 (file)
@@ -3624,15 +3624,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) {
index b756f7f24278e5b2a9ce6601f160ebcbdec078fd..fc25051b4690c5772c3a34cd917f732931d43ce3 100644 (file)
@@ -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;