Introduce a helper that only calls closedir if DIR* is non-NULL
and sets it to NULL afterwards.
---
cfg.mk | 4 ++--
src/conf/network_conf.c | 4 ++--
src/conf/nwfilter_conf.c | 2 +-
src/conf/storage_conf.c | 4 ++--
src/conf/virdomainobjlist.c | 2 +-
src/conf/virsecretobj.c | 2 +-
src/libvirt_private.syms | 1 +
src/network/bridge_driver.c | 2 +-
src/openvz/openvz_conf.c | 2 +-
src/qemu/qemu_driver.c | 3 +--
src/qemu/qemu_hostdev.c | 4 +---
src/storage/storage_backend.c | 6 +++---
src/storage/storage_backend_fs.c | 6 ++----
src/storage/storage_backend_iscsi.c | 2 +-
src/storage/storage_backend_scsi.c | 8 +++-----
src/util/vircgroup.c | 12 ++++--------
src/util/virfile.c | 16 ++++++++++++----
src/util/virfile.h | 3 +++
src/util/virhostcpu.c | 6 ++----
src/util/virnetdev.c | 2 +-
src/util/virnetdevtap.c | 2 +-
src/util/virnuma.c | 3 +--
src/util/virpci.c | 10 ++++------
src/util/virprocess.c | 3 +--
src/util/virscsi.c | 6 ++----
src/util/virusb.c | 4 +---
src/util/virutil.c | 6 +++---
src/xen/xen_inotify.c | 6 +++---
src/xen/xm_internal.c | 4 ++--
tests/virschematest.c | 2 +-
tools/nss/libvirt_nss.c | 6 ++----
31 files changed, 66 insertions(+), 77 deletions(-)
diff --git a/cfg.mk b/cfg.mk
index d82070d..a2576d1 100644
--- a/cfg.mk
+++ b/cfg.mk
@@ -421,9 +421,9 @@ sc_prohibit_gethostname:
$(_sc_search_regexp)
sc_prohibit_readdir:
- @prohibit='\breaddir *\(' \
+ @prohibit='\b(read|close)dir *\(' \
exclude='exempt from syntax-check' \
- halt='use virDirRead, not readdir' \
+ halt='use virDirRead and VIR_DIR_CLOSE' \
$(_sc_search_regexp)
sc_prohibit_gettext_noop:
diff --git a/src/conf/network_conf.c b/src/conf/network_conf.c
index 02b8cd7..1e4b719 100644
--- a/src/conf/network_conf.c
+++ b/src/conf/network_conf.c
@@ -3258,7 +3258,7 @@ virNetworkLoadAllState(virNetworkObjListPtr nets,
virNetworkObjEndAPI(&net);
}
- closedir(dir);
+ VIR_DIR_CLOSE(dir);
return ret;
}
@@ -3298,7 +3298,7 @@ int virNetworkLoadAllConfigs(virNetworkObjListPtr nets,
virNetworkObjEndAPI(&net);
}
- closedir(dir);
+ VIR_DIR_CLOSE(dir);
return ret;
}
diff --git a/src/conf/nwfilter_conf.c b/src/conf/nwfilter_conf.c
index 3f90f65..56f8b86 100644
--- a/src/conf/nwfilter_conf.c
+++ b/src/conf/nwfilter_conf.c
@@ -3227,7 +3227,7 @@ virNWFilterLoadAllConfigs(virNWFilterObjListPtr nwfilters,
virNWFilterObjUnlock(nwfilter);
}
- closedir(dir);
+ VIR_DIR_CLOSE(dir);
return ret;
}
diff --git a/src/conf/storage_conf.c b/src/conf/storage_conf.c
index 6932195..5c044d2 100644
--- a/src/conf/storage_conf.c
+++ b/src/conf/storage_conf.c
@@ -1964,7 +1964,7 @@ virStoragePoolLoadAllState(virStoragePoolObjListPtr pools,
virStoragePoolObjUnlock(pool);
}
- closedir(dir);
+ VIR_DIR_CLOSE(dir);
return ret;
}
@@ -2015,7 +2015,7 @@ virStoragePoolLoadAllConfigs(virStoragePoolObjListPtr pools,
VIR_FREE(autostartLink);
}
- closedir(dir);
+ VIR_DIR_CLOSE(dir);
return ret;
}
diff --git a/src/conf/virdomainobjlist.c b/src/conf/virdomainobjlist.c
index 485671e..4f7756d 100644
--- a/src/conf/virdomainobjlist.c
+++ b/src/conf/virdomainobjlist.c
@@ -616,7 +616,7 @@ virDomainObjListLoadAllConfigs(virDomainObjListPtr doms,
}
}
- closedir(dir);
+ VIR_DIR_CLOSE(dir);
virObjectUnlock(doms);
return ret;
}
diff --git a/src/conf/virsecretobj.c b/src/conf/virsecretobj.c
index c46d22c..46042eb 100644
--- a/src/conf/virsecretobj.c
+++ b/src/conf/virsecretobj.c
@@ -1000,6 +1000,6 @@ virSecretLoadAllConfigs(virSecretObjListPtr secrets,
virSecretObjEndAPI(&secret);
}
- closedir(dir);
+ VIR_DIR_CLOSE(dir);
return 0;
}
diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms
index 501c23e..f4dc88d 100644
--- a/src/libvirt_private.syms
+++ b/src/libvirt_private.syms
@@ -1493,6 +1493,7 @@ saferead;
safewrite;
safezero;
virBuildPathInternal;
+virDirClose;
virDirCreate;
virDirRead;
virFileAbsPath;
diff --git a/src/network/bridge_driver.c b/src/network/bridge_driver.c
index 7c8d2cc..7b021d8 100644
--- a/src/network/bridge_driver.c
+++ b/src/network/bridge_driver.c
@@ -577,7 +577,7 @@ networkMigrateStateFiles(virNetworkDriverStatePtr driver)
ret = 0;
cleanup:
- closedir(dir);
+ VIR_DIR_CLOSE(dir);
VIR_FREE(oldPath);
VIR_FREE(newPath);
VIR_FREE(contents);
diff --git a/src/openvz/openvz_conf.c b/src/openvz/openvz_conf.c
index 820dc22..ff5e5b8 100644
--- a/src/openvz/openvz_conf.c
+++ b/src/openvz/openvz_conf.c
@@ -1072,7 +1072,7 @@ static int openvzAssignUUIDs(void)
openvzSetUUID(vpsid);
}
- closedir(dp);
+ VIR_DIR_CLOSE(dp);
VIR_FREE(conf_dir);
return ret;
}
diff --git a/src/qemu/qemu_driver.c b/src/qemu/qemu_driver.c
index d065e45..76ee3c1 100644
--- a/src/qemu/qemu_driver.c
+++ b/src/qemu/qemu_driver.c
@@ -585,8 +585,7 @@ qemuDomainSnapshotLoad(virDomainObjPtr vm,
ret = 0;
cleanup:
- if (dir)
- closedir(dir);
+ VIR_DIR_CLOSE(dir);
VIR_FREE(snapDir);
virObjectUnref(caps);
virObjectUnlock(vm);
diff --git a/src/qemu/qemu_hostdev.c b/src/qemu/qemu_hostdev.c
index e16d5fd..84f3615 100644
--- a/src/qemu/qemu_hostdev.c
+++ b/src/qemu/qemu_hostdev.c
@@ -134,9 +134,7 @@ qemuHostdevHostSupportsPassthroughVFIO(void)
ret = true;
cleanup:
- if (iommuDir)
- closedir(iommuDir);
-
+ VIR_DIR_CLOSE(iommuDir);
return ret;
}
diff --git a/src/storage/storage_backend.c b/src/storage/storage_backend.c
index 8f03a6e..955d983 100644
--- a/src/storage/storage_backend.c
+++ b/src/storage/storage_backend.c
@@ -1957,12 +1957,12 @@ virStorageBackendStablePath(virStoragePoolObjPtr pool,
if (virAsprintf(&stablepath, "%s/%s",
pool->def->target.path,
dent->d_name) == -1) {
- closedir(dh);
+ VIR_DIR_CLOSE(dh);
return NULL;
}
if (virFileLinkPointsTo(stablepath, devpath)) {
- closedir(dh);
+ VIR_DIR_CLOSE(dh);
return stablepath;
}
@@ -1974,7 +1974,7 @@ virStorageBackendStablePath(virStoragePoolObjPtr pool,
goto retry;
}
- closedir(dh);
+ VIR_DIR_CLOSE(dh);
ret_strdup:
/* Couldn't find any matching stable link so give back
diff --git a/src/storage/storage_backend_fs.c b/src/storage/storage_backend_fs.c
index a11df36..152f9f3 100644
--- a/src/storage/storage_backend_fs.c
+++ b/src/storage/storage_backend_fs.c
@@ -972,8 +972,7 @@ virStorageBackendFileSystemRefresh(virConnectPtr conn
ATTRIBUTE_UNUSED,
}
if (direrr < 0)
goto cleanup;
- closedir(dir);
- dir = NULL;
+ VIR_DIR_CLOSE(dir);
vol = NULL;
if (VIR_ALLOC(target))
@@ -1019,8 +1018,7 @@ virStorageBackendFileSystemRefresh(virConnectPtr conn
ATTRIBUTE_UNUSED,
ret = 0;
cleanup:
- if (dir)
- closedir(dir);
+ VIR_DIR_CLOSE(dir);
VIR_FORCE_CLOSE(fd);
virStorageVolDefFree(vol);
virStorageSourceFree(target);
diff --git a/src/storage/storage_backend_iscsi.c b/src/storage/storage_backend_iscsi.c
index 876c14c..e50158f 100644
--- a/src/storage/storage_backend_iscsi.c
+++ b/src/storage/storage_backend_iscsi.c
@@ -129,7 +129,7 @@ virStorageBackendISCSIGetHostNumber(const char *sysfs_path,
}
cleanup:
- closedir(sysdir);
+ VIR_DIR_CLOSE(sysdir);
return ret;
}
diff --git a/src/storage/storage_backend_scsi.c b/src/storage/storage_backend_scsi.c
index be993f1..b08d960 100644
--- a/src/storage/storage_backend_scsi.c
+++ b/src/storage/storage_backend_scsi.c
@@ -290,8 +290,7 @@ getNewStyleBlockDevice(const char *lun_path,
retval = 0;
cleanup:
- if (block_dir)
- closedir(block_dir);
+ VIR_DIR_CLOSE(block_dir);
VIR_FREE(block_path);
return retval;
}
@@ -387,8 +386,7 @@ getBlockDevice(uint32_t host,
retval = 0;
cleanup:
- if (lun_dir)
- closedir(lun_dir);
+ VIR_DIR_CLOSE(lun_dir);
VIR_FREE(lun_path);
return retval;
}
@@ -501,7 +499,7 @@ virStorageBackendSCSIFindLUs(virStoragePoolObjPtr pool,
found++;
}
- closedir(devicedir);
+ VIR_DIR_CLOSE(devicedir);
if (retval < 0)
return -1;
diff --git a/src/util/vircgroup.c b/src/util/vircgroup.c
index 7bab086..c76c94f 100644
--- a/src/util/vircgroup.c
+++ b/src/util/vircgroup.c
@@ -3409,7 +3409,7 @@ virCgroupRemoveRecursively(char *grppath)
VIR_ERROR(_("Failed to readdir for %s (%d)"), grppath, errno);
}
- closedir(grpdir);
+ VIR_DIR_CLOSE(grpdir);
VIR_DEBUG("Removing cgroup %s", grppath);
if (rmdir(grppath) != 0 && errno != ENOENT) {
@@ -3669,9 +3669,7 @@ virCgroupKillRecursiveInternal(virCgroupPtr group,
cleanup:
virCgroupFree(&subgroup);
VIR_FREE(keypath);
- if (dp)
- closedir(dp);
-
+ VIR_DIR_CLOSE(dp);
return ret;
}
@@ -3993,15 +3991,13 @@ int virCgroupSetOwner(virCgroupPtr cgroup,
}
VIR_FREE(base);
- closedir(dh);
- dh = NULL;
+ VIR_DIR_CLOSE(dh);
}
ret = 0;
cleanup:
- if (dh)
- closedir(dh);
+ VIR_DIR_CLOSE(dh);
VIR_FREE(entry);
VIR_FREE(base);
return ret;
diff --git a/src/util/virfile.c b/src/util/virfile.c
index f47bf39..ce8f7fd 100644
--- a/src/util/virfile.c
+++ b/src/util/virfile.c
@@ -667,8 +667,7 @@ static int virFileLoopDeviceOpenSearch(char **dev_name)
VIR_DEBUG("No free loop devices available");
VIR_FREE(looppath);
}
- if (dh)
- closedir(dh);
+ VIR_DIR_CLOSE(dh);
return fd;
}
@@ -807,7 +806,7 @@ virFileNBDDeviceFindUnused(void)
_("No free NBD devices"));
cleanup:
- closedir(dh);
+ VIR_DIR_CLOSE(dh);
return ret;
}
@@ -994,7 +993,7 @@ int virFileDeleteTree(const char *dir)
cleanup:
VIR_FREE(filepath);
- closedir(dh);
+ VIR_DIR_CLOSE(dh);
return ret;
}
@@ -2770,6 +2769,15 @@ int virDirRead(DIR *dirp, struct dirent **ent, const char *name)
return !!*ent;
}
+void virDirClose(DIR **dirp)
+{
+ if (!*dirp)
+ return;
+
+ closedir(*dirp); /* exempt from syntax-check */
+ *dirp = NULL;
+}
+
static int
virFileMakePathHelper(char *path, mode_t mode)
{
diff --git a/src/util/virfile.h b/src/util/virfile.h
index dae234e..ab9eeba 100644
--- a/src/util/virfile.h
+++ b/src/util/virfile.h
@@ -232,6 +232,9 @@ int virDirCreate(const char *path, mode_t mode, uid_t uid, gid_t gid,
unsigned int flags) ATTRIBUTE_RETURN_CHECK;
int virDirRead(DIR *dirp, struct dirent **ent, const char *dirname)
ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2) ATTRIBUTE_RETURN_CHECK;
+void virDirClose(DIR **dirp)
+ ATTRIBUTE_NONNULL(1);
+# define VIR_DIR_CLOSE(dir) virDirClose(&(dir))
int virFileMakePath(const char *path) ATTRIBUTE_RETURN_CHECK;
int virFileMakePathWithMode(const char *path,
diff --git a/src/util/virhostcpu.c b/src/util/virhostcpu.c
index 0cdba0a..6883466 100644
--- a/src/util/virhostcpu.c
+++ b/src/util/virhostcpu.c
@@ -477,8 +477,7 @@ virHostCPUParseNode(const char *node,
ret = processors;
cleanup:
- if (cpudir)
- closedir(cpudir);
+ VIR_DIR_CLOSE(cpudir);
if (cores_maps)
for (i = 0; i < sock_max; i++)
virBitmapFree(cores_maps[i]);
@@ -774,8 +773,7 @@ virHostCPUGetInfoPopulateLinux(FILE *cpuinfo,
ret = 0;
cleanup:
- if (nodedir)
- closedir(nodedir);
+ VIR_DIR_CLOSE(nodedir);
virBitmapFree(present_cpus_map);
virBitmapFree(online_cpus_map);
VIR_FREE(sysfs_nodedir);
diff --git a/src/util/virnetdev.c b/src/util/virnetdev.c
index 5a4ccc6..75ec484 100644
--- a/src/util/virnetdev.c
+++ b/src/util/virnetdev.c
@@ -3188,7 +3188,7 @@ virNetDevRDMAFeature(const char *ifname,
ret = 0;
cleanup:
- closedir(dirp);
+ VIR_DIR_CLOSE(dirp);
VIR_FREE(eth_devpath);
VIR_FREE(ib_devpath);
VIR_FREE(eth_res_buf);
diff --git a/src/util/virnetdevtap.c b/src/util/virnetdevtap.c
index b34cbb7..eec7614 100644
--- a/src/util/virnetdevtap.c
+++ b/src/util/virnetdevtap.c
@@ -144,7 +144,7 @@ virNetDevTapGetRealDeviceName(char *ifname ATTRIBUTE_UNUSED)
cleanup:
VIR_FREE(devpath);
VIR_FORCE_CLOSE(fd);
- closedir(dirp);
+ VIR_DIR_CLOSE(dirp);
return ret;
#else
return NULL;
diff --git a/src/util/virnuma.c b/src/util/virnuma.c
index 23064ff..b756f7f 100644
--- a/src/util/virnuma.c
+++ b/src/util/virnuma.c
@@ -833,8 +833,7 @@ virNumaGetPages(int node,
VIR_FREE(tmp_free);
VIR_FREE(tmp_avail);
VIR_FREE(tmp_size);
- if (dir)
- closedir(dir);
+ VIR_DIR_CLOSE(dir);
VIR_FREE(path);
return ret;
}
diff --git a/src/util/virpci.c b/src/util/virpci.c
index 095d706..5cb5d3a 100644
--- a/src/util/virpci.c
+++ b/src/util/virpci.c
@@ -509,7 +509,7 @@ virPCIDeviceIterDevices(virPCIDeviceIterPredicate predicate,
virPCIDeviceFree(check);
}
- closedir(dir);
+ VIR_DIR_CLOSE(dir);
return ret;
}
@@ -1993,8 +1993,7 @@ int virPCIDeviceFileIterate(virPCIDevicePtr dev,
ret = 0;
cleanup:
- if (dir)
- closedir(dir);
+ VIR_DIR_CLOSE(dir);
VIR_FREE(file);
VIR_FREE(pcidir);
return ret;
@@ -2051,8 +2050,7 @@ virPCIDeviceAddressIOMMUGroupIterate(virPCIDeviceAddressPtr orig,
cleanup:
VIR_FREE(groupPath);
- if (groupDir)
- closedir(groupDir);
+ VIR_DIR_CLOSE(groupDir);
return ret;
}
@@ -2713,7 +2711,7 @@ virPCIGetNetName(char *device_link_sysfs_path, char **netname)
break;
}
- closedir(dir);
+ VIR_DIR_CLOSE(dir);
out:
VIR_FREE(pcidev_sysfs_net_path);
diff --git a/src/util/virprocess.c b/src/util/virprocess.c
index bf6a6df..b0ca1ce 100644
--- a/src/util/virprocess.c
+++ b/src/util/virprocess.c
@@ -637,8 +637,7 @@ int virProcessGetPids(pid_t pid, size_t *npids, pid_t **pids)
ret = 0;
cleanup:
- if (dir)
- closedir(dir);
+ VIR_DIR_CLOSE(dir);
VIR_FREE(taskPath);
if (ret < 0)
VIR_FREE(*pids);
diff --git a/src/util/virscsi.c b/src/util/virscsi.c
index 66b9017..72a5661 100644
--- a/src/util/virscsi.c
+++ b/src/util/virscsi.c
@@ -143,8 +143,7 @@ virSCSIDeviceGetSgName(const char *sysfs_prefix,
}
cleanup:
- if (dir)
- closedir(dir);
+ VIR_DIR_CLOSE(dir);
VIR_FREE(path);
return sg;
}
@@ -189,8 +188,7 @@ virSCSIDeviceGetDevName(const char *sysfs_prefix,
}
cleanup:
- if (dir)
- closedir(dir);
+ VIR_DIR_CLOSE(dir);
VIR_FREE(path);
return name;
}
diff --git a/src/util/virusb.c b/src/util/virusb.c
index 5c39667..33b188e 100644
--- a/src/util/virusb.c
+++ b/src/util/virusb.c
@@ -202,9 +202,7 @@ virUSBDeviceSearch(unsigned int vendor,
ret = list;
cleanup:
- if (dir)
- closedir(dir);
-
+ VIR_DIR_CLOSE(dir);
if (!ret)
virObjectUnref(list);
return ret;
diff --git a/src/util/virutil.c b/src/util/virutil.c
index 60da17b..a6c1273 100644
--- a/src/util/virutil.c
+++ b/src/util/virutil.c
@@ -1915,7 +1915,7 @@ virFindSCSIHostByPCI(const char *sysfs_prefix,
}
cleanup:
- closedir(dir);
+ VIR_DIR_CLOSE(dir);
VIR_FREE(unique_path);
VIR_FREE(host_link);
VIR_FREE(host_path);
@@ -2265,7 +2265,7 @@ virGetFCHostNameByWWN(const char *sysfs_prefix,
cleanup:
# undef READ_WWN
- closedir(dir);
+ VIR_DIR_CLOSE(dir);
VIR_FREE(wwnn_path);
VIR_FREE(wwpn_path);
VIR_FREE(wwnn_buf);
@@ -2354,7 +2354,7 @@ virFindFCHostCapableVport(const char *sysfs_prefix)
}
cleanup:
- closedir(dir);
+ VIR_DIR_CLOSE(dir);
VIR_FREE(max_vports);
VIR_FREE(vports);
return ret;
diff --git a/src/xen/xen_inotify.c b/src/xen/xen_inotify.c
index d81a35d..cd169e0 100644
--- a/src/xen/xen_inotify.c
+++ b/src/xen/xen_inotify.c
@@ -372,21 +372,21 @@ xenInotifyOpen(virConnectPtr conn,
/* Build the full file path */
if (!(path = virFileBuildPath(priv->configDir, ent->d_name, NULL))) {
- closedir(dh);
+ VIR_DIR_CLOSE(dh);
return -1;
}
if (xenInotifyAddDomainConfigInfo(conn, path, now) < 0) {
virReportError(VIR_ERR_INTERNAL_ERROR,
"%s", _("Error adding file to config
list"));
- closedir(dh);
+ VIR_DIR_CLOSE(dh);
VIR_FREE(path);
return -1;
}
VIR_FREE(path);
}
- closedir(dh);
+ VIR_DIR_CLOSE(dh);
if (direrr < 0)
return -1;
}
diff --git a/src/xen/xm_internal.c b/src/xen/xm_internal.c
index f7486b5..e7ac57e 100644
--- a/src/xen/xm_internal.c
+++ b/src/xen/xm_internal.c
@@ -358,7 +358,7 @@ xenXMConfigCacheRefresh(virConnectPtr conn)
/* Build the full file path */
if (!(path = virFileBuildPath(priv->configDir, ent->d_name, NULL))) {
- closedir(dh);
+ VIR_DIR_CLOSE(dh);
return -1;
}
@@ -386,7 +386,7 @@ xenXMConfigCacheRefresh(virConnectPtr conn)
args.priv = priv;
virHashRemoveSet(priv->configCache, xenXMConfigReaper, &args);
- closedir(dh);
+ VIR_DIR_CLOSE(dh);
return ret;
}
diff --git a/tests/virschematest.c b/tests/virschematest.c
index 638fd0f..14a9e20 100644
--- a/tests/virschematest.c
+++ b/tests/virschematest.c
@@ -112,7 +112,7 @@ testSchemaDir(const char *schema,
cleanup:
VIR_FREE(test_name);
VIR_FREE(xml_path);
- closedir(dir);
+ VIR_DIR_CLOSE(dir);
return ret;
}
diff --git a/tools/nss/libvirt_nss.c b/tools/nss/libvirt_nss.c
index de34baf..d179514 100644
--- a/tools/nss/libvirt_nss.c
+++ b/tools/nss/libvirt_nss.c
@@ -159,8 +159,7 @@ findLease(const char *name,
VIR_FREE(path);
}
- closedir(dir);
- dir = NULL;
+ VIR_DIR_CLOSE(dir);
nleases = virJSONValueArraySize(leases_array);
DEBUG("Read %zd leases", nleases);
@@ -231,8 +230,7 @@ findLease(const char *name,
*errnop = errno;
VIR_FREE(tmpAddress);
virJSONValueFree(leases_array);
- if (dir)
- closedir(dir);
+ VIR_DIR_CLOSE(dir);
return ret;
}
--
2.7.3