Introduce VIR_DIR_CLOSE

Introduce a helper that only calls closedir if DIR* is non-NULL
and sets it to NULL afterwards.
This commit is contained in:
Ján Tomko 2016-06-21 12:40:29 +02:00
parent 44f5b3f869
commit a4e6f1eb9c
31 changed files with 66 additions and 77 deletions

4
cfg.mk
View File

@ -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:

View File

@ -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;
}

View File

@ -3227,7 +3227,7 @@ virNWFilterLoadAllConfigs(virNWFilterObjListPtr nwfilters,
virNWFilterObjUnlock(nwfilter);
}
closedir(dir);
VIR_DIR_CLOSE(dir);
return ret;
}

View File

@ -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;
}

View File

@ -616,7 +616,7 @@ virDomainObjListLoadAllConfigs(virDomainObjListPtr doms,
}
}
closedir(dir);
VIR_DIR_CLOSE(dir);
virObjectUnlock(doms);
return ret;
}

View File

@ -1000,6 +1000,6 @@ virSecretLoadAllConfigs(virSecretObjListPtr secrets,
virSecretObjEndAPI(&secret);
}
closedir(dir);
VIR_DIR_CLOSE(dir);
return 0;
}

View File

@ -1493,6 +1493,7 @@ saferead;
safewrite;
safezero;
virBuildPathInternal;
virDirClose;
virDirCreate;
virDirRead;
virFileAbsPath;

View File

@ -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);

View File

@ -1072,7 +1072,7 @@ static int openvzAssignUUIDs(void)
openvzSetUUID(vpsid);
}
closedir(dp);
VIR_DIR_CLOSE(dp);
VIR_FREE(conf_dir);
return ret;
}

View File

@ -587,8 +587,7 @@ qemuDomainSnapshotLoad(virDomainObjPtr vm,
ret = 0;
cleanup:
if (dir)
closedir(dir);
VIR_DIR_CLOSE(dir);
VIR_FREE(snapDir);
virObjectUnref(caps);
virObjectUnlock(vm);

View File

@ -134,9 +134,7 @@ qemuHostdevHostSupportsPassthroughVFIO(void)
ret = true;
cleanup:
if (iommuDir)
closedir(iommuDir);
VIR_DIR_CLOSE(iommuDir);
return ret;
}

View File

@ -1946,12 +1946,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;
}
@ -1963,7 +1963,7 @@ virStorageBackendStablePath(virStoragePoolObjPtr pool,
goto retry;
}
closedir(dh);
VIR_DIR_CLOSE(dh);
ret_strdup:
/* Couldn't find any matching stable link so give back

View File

@ -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);

View File

@ -122,7 +122,7 @@ virStorageBackendISCSIGetHostNumber(const char *sysfs_path,
if (direrr < 0)
retval = -1;
closedir(sysdir);
VIR_DIR_CLOSE(sysdir);
out:
return retval;
}

View File

@ -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;

View File

@ -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;

View File

@ -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)
{

View File

@ -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,

View File

@ -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);

View File

@ -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);

View File

@ -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;

View File

@ -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;
}

View File

@ -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);

View File

@ -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);

View File

@ -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;
}

View File

@ -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;

View File

@ -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;

View File

@ -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;
}

View File

@ -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;
}

View File

@ -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;
}

View File

@ -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;
}