drivers: use virDirRead API

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@redhat.com>
This commit is contained in:
Eric Blake 2014-04-25 22:11:05 -06:00
parent ac1d42ac72
commit ddcf4730ce
7 changed files with 26 additions and 22 deletions

View File

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

View File

@ -93,6 +93,7 @@ parallelsFindVolumes(virStoragePoolObjPtr pool)
struct dirent *ent; struct dirent *ent;
char *path = NULL; char *path = NULL;
int ret = -1; int ret = -1;
int direrr;
if (!(dir = opendir(pool->def->target.path))) { if (!(dir = opendir(pool->def->target.path))) {
virReportSystemError(errno, virReportSystemError(errno,
@ -101,7 +102,7 @@ parallelsFindVolumes(virStoragePoolObjPtr pool)
return -1; return -1;
} }
while ((ent = readdir(dir)) != NULL) { while ((direrr = virDirRead(dir, &ent, pool->def->target.path)) > 0) {
if (!virFileHasSuffix(ent->d_name, ".xml")) if (!virFileHasSuffix(ent->d_name, ".xml"))
continue; continue;
@ -113,6 +114,8 @@ parallelsFindVolumes(virStoragePoolObjPtr pool)
VIR_FREE(path); VIR_FREE(path);
} }
if (direrr < 0)
goto cleanup;
ret = 0; ret = 0;
cleanup: cleanup:
@ -331,6 +334,7 @@ static int parallelsFindVmVolumes(virStoragePoolObjPtr pool,
char *diskPath = NULL, *diskDescPath = NULL; char *diskPath = NULL, *diskDescPath = NULL;
struct stat sb; struct stat sb;
int ret = -1; int ret = -1;
int direrr;
if (!(dir = opendir(pdom->home))) { if (!(dir = opendir(pdom->home))) {
virReportSystemError(errno, virReportSystemError(errno,
@ -339,7 +343,7 @@ static int parallelsFindVmVolumes(virStoragePoolObjPtr pool,
goto cleanup; goto cleanup;
} }
while ((ent = readdir(dir)) != NULL) { while ((direrr = virDirRead(dir, &ent, pdom->home)) > 0) {
VIR_FREE(diskPath); VIR_FREE(diskPath);
VIR_FREE(diskDescPath); VIR_FREE(diskDescPath);
@ -368,8 +372,9 @@ static int parallelsFindVmVolumes(virStoragePoolObjPtr pool,
if (parallelsAddDiskVolume(pool, dom, ent->d_name, if (parallelsAddDiskVolume(pool, dom, ent->d_name,
diskPath, diskDescPath)) diskPath, diskDescPath))
goto cleanup; goto cleanup;
} }
if (direrr < 0)
goto cleanup;
ret = 0; ret = 0;
cleanup: cleanup:

View File

@ -417,6 +417,7 @@ qemuDomainSnapshotLoad(virDomainObjPtr vm,
VIR_DOMAIN_SNAPSHOT_PARSE_INTERNAL); VIR_DOMAIN_SNAPSHOT_PARSE_INTERNAL);
int ret = -1; int ret = -1;
virCapsPtr caps = NULL; virCapsPtr caps = NULL;
int direrr;
virObjectLock(vm); virObjectLock(vm);
if (virAsprintf(&snapDir, "%s/%s", baseDir, vm->def->name) < 0) { if (virAsprintf(&snapDir, "%s/%s", baseDir, vm->def->name) < 0) {
@ -439,7 +440,7 @@ qemuDomainSnapshotLoad(virDomainObjPtr vm,
goto cleanup; goto cleanup;
} }
while ((entry = readdir(dir))) { while ((direrr = virDirRead(dir, &entry, NULL)) > 0) {
if (entry->d_name[0] == '.') if (entry->d_name[0] == '.')
continue; continue;
@ -485,6 +486,8 @@ qemuDomainSnapshotLoad(virDomainObjPtr vm,
VIR_FREE(fullpath); VIR_FREE(fullpath);
VIR_FREE(xmlStr); VIR_FREE(xmlStr);
} }
if (direrr < 0)
VIR_ERROR(_("Failed to fully read directory %s"), snapDir);
if (vm->current_snapshot != current) { if (vm->current_snapshot != current) {
VIR_ERROR(_("Too many snapshots claiming to be current for domain %s"), VIR_ERROR(_("Too many snapshots claiming to be current for domain %s"),

View File

@ -1,7 +1,7 @@
/* /*
* qemu_hostdev.c: QEMU hostdev management * 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 * Copyright (C) 2006 Daniel P. Berrange
* *
* This library is free software; you can redistribute it and/or * This library is free software; you can redistribute it and/or
@ -90,12 +90,13 @@ qemuHostdevHostSupportsPassthroughVFIO(void)
DIR *iommuDir = NULL; DIR *iommuDir = NULL;
struct dirent *iommuGroup = NULL; struct dirent *iommuGroup = NULL;
bool ret = false; bool ret = false;
int direrr;
/* condition 1 - /sys/kernel/iommu_groups/ contains entries */ /* condition 1 - /sys/kernel/iommu_groups/ contains entries */
if (!(iommuDir = opendir("/sys/kernel/iommu_groups/"))) if (!(iommuDir = opendir("/sys/kernel/iommu_groups/")))
goto cleanup; goto cleanup;
while ((iommuGroup = readdir(iommuDir))) { while ((direrr = virDirRead(iommuDir, &iommuGroup, NULL)) > 0) {
/* skip ./ ../ */ /* skip ./ ../ */
if (STRPREFIX(iommuGroup->d_name, ".")) if (STRPREFIX(iommuGroup->d_name, "."))
continue; continue;
@ -104,7 +105,7 @@ qemuHostdevHostSupportsPassthroughVFIO(void)
break; break;
} }
if (!iommuGroup) if (direrr < 0 || !iommuGroup)
goto cleanup; goto cleanup;
/* okay, iommu is on and recognizes groups */ /* okay, iommu is on and recognizes groups */

View File

@ -1,7 +1,7 @@
/* /*
* secret_driver.c: local driver for secret manipulation API * 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 * This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public * modify it under the terms of the GNU Lesser General Public
@ -484,7 +484,7 @@ loadSecrets(virSecretDriverStatePtr driver,
driver->directory); driver->directory);
goto cleanup; goto cleanup;
} }
while ((de = readdir(dir)) != NULL) { while (virDirRead(dir, &de, NULL) > 0) {
virSecretEntryPtr secret; virSecretEntryPtr secret;
if (STREQ(de->d_name, ".") || STREQ(de->d_name, "..")) if (STREQ(de->d_name, ".") || STREQ(de->d_name, ".."))
@ -503,7 +503,7 @@ loadSecrets(virSecretDriverStatePtr driver,
} }
listInsert(&list, secret); 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. */ secrets we managed to find. */
while (list != NULL) { while (list != NULL) {

View File

@ -4,7 +4,7 @@
* /etc/xen * /etc/xen
* /var/lib/xend/domains * /var/lib/xend/domains
* *
* Copyright (C) 2010-2013 Red Hat, Inc. * Copyright (C) 2010-2014 Red Hat, Inc.
* Copyright (C) 2008 VirtualIron * Copyright (C) 2008 VirtualIron
* *
* This library is free software; you can redistribute it and/or * This library is free software; you can redistribute it and/or
@ -343,6 +343,7 @@ xenInotifyOpen(virConnectPtr conn,
struct dirent *ent; struct dirent *ent;
char *path; char *path;
xenUnifiedPrivatePtr priv = conn->privateData; xenUnifiedPrivatePtr priv = conn->privateData;
int direrr;
virCheckFlags(VIR_CONNECT_RO, -1); virCheckFlags(VIR_CONNECT_RO, -1);
@ -363,7 +364,7 @@ xenInotifyOpen(virConnectPtr conn,
priv->configDir); priv->configDir);
return -1; return -1;
} }
while ((ent = readdir(dh))) { while ((direrr = virDirRead(dh, &ent, priv->configDir)) > 0) {
if (STRPREFIX(ent->d_name, ".")) if (STRPREFIX(ent->d_name, "."))
continue; continue;
@ -384,6 +385,8 @@ xenInotifyOpen(virConnectPtr conn,
VIR_FREE(path); VIR_FREE(path);
} }
closedir(dh); closedir(dh);
if (direrr < 0)
return -1;
} }
if ((priv->inotifyFD = inotify_init()) < 0) { if ((priv->inotifyFD = inotify_init()) < 0) {

View File

@ -327,7 +327,7 @@ xenXMConfigCacheRefresh(virConnectPtr conn)
return -1; return -1;
} }
while ((ent = readdir(dh))) { while ((ret = virDirRead(dh, &ent, priv->configDir)) > 0) {
struct stat st; struct stat st;
char *path; char *path;
@ -386,7 +386,6 @@ xenXMConfigCacheRefresh(virConnectPtr conn)
args.now = now; args.now = now;
args.priv = priv; args.priv = priv;
virHashRemoveSet(priv->configCache, xenXMConfigReaper, &args); virHashRemoveSet(priv->configCache, xenXMConfigReaper, &args);
ret = 0;
closedir(dh); closedir(dh);