Use it instead of opendir everywhere we need to check for ENOENT.
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;
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;
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;
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;
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;
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);
{
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. */
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"),
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
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) {
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;