Use virDirOpenIfExists

Use it instead of opendir everywhere we need to check for ENOENT.
This commit is contained in:
Ján Tomko 2016-06-21 16:47:24 +02:00
parent 941ccbc174
commit 42b4a37d68
9 changed files with 35 additions and 83 deletions

View File

@ -3236,14 +3236,10 @@ virNetworkLoadAllState(virNetworkObjListPtr nets,
DIR *dir; DIR *dir;
struct dirent *entry; struct dirent *entry;
int ret = -1; int ret = -1;
int rc;
if (!(dir = opendir(stateDir))) { if ((rc = virDirOpenIfExists(&dir, stateDir)) <= 0)
if (errno == ENOENT) return rc;
return 0;
virReportSystemError(errno, _("Failed to open dir '%s'"), stateDir);
return -1;
}
while ((ret = virDirRead(dir, &entry, stateDir)) > 0) { while ((ret = virDirRead(dir, &entry, stateDir)) > 0) {
virNetworkObjPtr net; virNetworkObjPtr net;
@ -3267,15 +3263,10 @@ int virNetworkLoadAllConfigs(virNetworkObjListPtr nets,
DIR *dir; DIR *dir;
struct dirent *entry; struct dirent *entry;
int ret = -1; int ret = -1;
int rc;
if (!(dir = opendir(configDir))) { if ((rc = virDirOpenIfExists(&dir, configDir)) <= 0)
if (errno == ENOENT) return rc;
return 0;
virReportSystemError(errno,
_("Failed to open dir '%s'"),
configDir);
return -1;
}
while ((ret = virDirRead(dir, &entry, configDir)) > 0) { while ((ret = virDirRead(dir, &entry, configDir)) > 0) {
virNetworkObjPtr net; virNetworkObjPtr net;

View File

@ -3204,14 +3204,10 @@ virNWFilterLoadAllConfigs(virNWFilterObjListPtr nwfilters,
DIR *dir; DIR *dir;
struct dirent *entry; struct dirent *entry;
int ret = -1; int ret = -1;
int rc;
if (!(dir = opendir(configDir))) { if ((rc = virDirOpenIfExists(&dir, configDir)) <= 0)
if (errno == ENOENT) return rc;
return 0;
virReportSystemError(errno, _("Failed to open dir '%s'"),
configDir);
return -1;
}
while ((ret = virDirRead(dir, &entry, configDir)) > 0) { while ((ret = virDirRead(dir, &entry, configDir)) > 0) {
virNWFilterObjPtr nwfilter; virNWFilterObjPtr nwfilter;

View File

@ -1941,14 +1941,10 @@ virStoragePoolLoadAllState(virStoragePoolObjListPtr pools,
DIR *dir; DIR *dir;
struct dirent *entry; struct dirent *entry;
int ret = -1; int ret = -1;
int rc;
if (!(dir = opendir(stateDir))) { if ((rc = virDirOpenIfExists(&dir, stateDir)) <= 0)
if (errno == ENOENT) return rc;
return 0;
virReportSystemError(errno, _("Failed to open dir '%s'"), stateDir);
return -1;
}
while ((ret = virDirRead(dir, &entry, stateDir)) > 0) { while ((ret = virDirRead(dir, &entry, stateDir)) > 0) {
virStoragePoolObjPtr pool; virStoragePoolObjPtr pool;
@ -1974,14 +1970,10 @@ virStoragePoolLoadAllConfigs(virStoragePoolObjListPtr pools,
DIR *dir; DIR *dir;
struct dirent *entry; struct dirent *entry;
int ret; int ret;
int rc;
if (!(dir = opendir(configDir))) { if ((rc = virDirOpenIfExists(&dir, configDir)) <= 0)
if (errno == ENOENT) return rc;
return 0;
virReportSystemError(errno, _("Failed to open dir '%s'"),
configDir);
return -1;
}
while ((ret = virDirRead(dir, &entry, configDir)) > 0) { while ((ret = virDirRead(dir, &entry, configDir)) > 0) {
char *path; char *path;

View File

@ -566,17 +566,12 @@ virDomainObjListLoadAllConfigs(virDomainObjListPtr doms,
DIR *dir; DIR *dir;
struct dirent *entry; struct dirent *entry;
int ret = -1; int ret = -1;
int rc;
VIR_INFO("Scanning for configs in %s", configDir); VIR_INFO("Scanning for configs in %s", configDir);
if (!(dir = opendir(configDir))) { if ((rc = virDirOpenIfExists(&dir, configDir)) <= 0)
if (errno == ENOENT) return rc;
return 0;
virReportSystemError(errno,
_("Failed to open dir '%s'"),
configDir);
return -1;
}
virObjectLock(doms); virObjectLock(doms);

View File

@ -966,13 +966,10 @@ virSecretLoadAllConfigs(virSecretObjListPtr secrets,
{ {
DIR *dir = NULL; DIR *dir = NULL;
struct dirent *de; struct dirent *de;
int rc;
if (!(dir = opendir(configDir))) { if ((rc = virDirOpenIfExists(&dir, configDir)) <= 0)
if (errno == ENOENT) return rc;
return 0;
virReportSystemError(errno, _("cannot open '%s'"), configDir);
return -1;
}
/* Ignore errors reported by readdir or other calls within the /* Ignore errors reported by readdir or other calls within the
* loop (if any). It's better to keep the secrets we managed to find. */ * loop (if any). It's better to keep the secrets we managed to find. */

View File

@ -509,15 +509,10 @@ networkMigrateStateFiles(virNetworkDriverStatePtr driver)
struct dirent *entry; struct dirent *entry;
char *oldPath = NULL, *newPath = NULL; char *oldPath = NULL, *newPath = NULL;
char *contents = NULL; char *contents = NULL;
int rc;
if (!(dir = opendir(oldStateDir))) { if ((rc = virDirOpenIfExists(&dir, oldStateDir)) <= 0)
if (errno == ENOENT) return rc;
return 0;
virReportSystemError(errno, _("failed to open directory '%s'"),
oldStateDir);
return -1;
}
if (virFileMakePath(driver->stateDir) < 0) { if (virFileMakePath(driver->stateDir) < 0) {
virReportSystemError(errno, _("cannot create directory %s"), virReportSystemError(errno, _("cannot create directory %s"),

View File

@ -500,14 +500,8 @@ qemuDomainSnapshotLoad(virDomainObjPtr vm,
VIR_INFO("Scanning for snapshots for domain %s in %s", vm->def->name, VIR_INFO("Scanning for snapshots for domain %s in %s", vm->def->name,
snapDir); snapDir);
if (!(dir = opendir(snapDir))) { if (virDirOpenIfExists(&dir, snapDir) <= 0)
if (errno != ENOENT)
virReportSystemError(errno,
_("Failed to open snapshot directory %s "
"for domain %s"),
snapDir, vm->def->name);
goto cleanup; goto cleanup;
}
while ((direrr = virDirRead(dir, &entry, NULL)) > 0) { while ((direrr = virDirRead(dir, &entry, NULL)) > 0) {
/* NB: ignoring errors, so one malformed config doesn't /* NB: ignoring errors, so one malformed config doesn't

View File

@ -3624,15 +3624,13 @@ virCgroupKillRecursiveInternal(virCgroupPtr group,
killedAny = true; killedAny = true;
VIR_DEBUG("Iterate over children of %s (killedAny=%d)", keypath, killedAny); VIR_DEBUG("Iterate over children of %s (killedAny=%d)", keypath, killedAny);
if (!(dp = opendir(keypath))) { if ((rc = virDirOpenIfExists(&dp, keypath)) < 0)
if (errno == ENOENT) {
VIR_DEBUG("Path %s does not exist, assuming done", keypath);
killedAny = false;
goto done;
}
virReportSystemError(errno,
_("Cannot open %s"), keypath);
goto cleanup; 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) { while ((direrr = virDirRead(dp, &ent, keypath)) > 0) {

View File

@ -737,16 +737,10 @@ virNumaGetPages(int node,
if (virNumaGetHugePageInfoDir(&path, node) < 0) if (virNumaGetHugePageInfoDir(&path, node) < 0)
goto cleanup; goto cleanup;
if (!(dir = opendir(path))) { /* It's okay if the @path doesn't exist. Maybe we are running on
/* 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. */
* system without huge pages support where the path may not exist. */ if (virDirOpenIfExists(&dir, path) < 0)
if (errno != ENOENT) { goto cleanup;
virReportSystemError(errno,
_("unable to open path: %s"),
path);
goto cleanup;
}
}
while (dir && (direrr = virDirRead(dir, &entry, path)) > 0) { while (dir && (direrr = virDirRead(dir, &entry, path)) > 0) {
const char *page_name = entry->d_name; const char *page_name = entry->d_name;