提交 12d17dcc 编写于 作者: D Daniel P. Berrangé

src: replace last_component() with g_path_get_basename()

The last_component() method is a GNULIB custom function
that returns a pointer to the base name in the path.
This is similar to g_path_get_basename() but without the
malloc. The extra malloc is no trouble for libvirt's
needs so we can use g_path_get_basename().
Reviewed-by: NFabiano Fidêncio <fidencio@redhat.com>
Signed-off-by: NDaniel P. Berrangé <berrange@redhat.com>
上级 f5e9bdb8
...@@ -2448,7 +2448,7 @@ virNodeDeviceGetSCSITargetCaps(const char *sysfsPath, ...@@ -2448,7 +2448,7 @@ virNodeDeviceGetSCSITargetCaps(const char *sysfsPath,
if (!(dir = mdir_name(sysfsPath))) if (!(dir = mdir_name(sysfsPath)))
return -1; return -1;
rport = g_strdup(last_component(dir)); rport = g_path_get_basename(dir);
if (!virFCIsCapableRport(rport)) if (!virFCIsCapableRport(rport))
goto cleanup; goto cleanup;
......
...@@ -23,7 +23,6 @@ ...@@ -23,7 +23,6 @@
#include <pciaccess.h> #include <pciaccess.h>
#include <scsi/scsi.h> #include <scsi/scsi.h>
#include "dirname.h"
#include "node_device_conf.h" #include "node_device_conf.h"
#include "node_device_event.h" #include "node_device_event.h"
#include "node_device_driver.h" #include "node_device_driver.h"
...@@ -602,10 +601,10 @@ udevProcessSCSIHost(struct udev_device *device G_GNUC_UNUSED, ...@@ -602,10 +601,10 @@ udevProcessSCSIHost(struct udev_device *device G_GNUC_UNUSED,
virNodeDeviceDefPtr def) virNodeDeviceDefPtr def)
{ {
virNodeDevCapSCSIHostPtr scsi_host = &def->caps->data.scsi_host; virNodeDevCapSCSIHostPtr scsi_host = &def->caps->data.scsi_host;
char *filename = NULL; g_autofree char *filename = NULL;
char *str; char *str;
filename = last_component(def->sysfs_path); filename = g_path_get_basename(def->sysfs_path);
if (!(str = STRSKIP(filename, "host")) || if (!(str = STRSKIP(filename, "host")) ||
virStrToLong_ui(str, NULL, 0, &scsi_host->host) < 0) { virStrToLong_ui(str, NULL, 0, &scsi_host->host) < 0) {
...@@ -711,9 +710,10 @@ udevProcessSCSIDevice(struct udev_device *device G_GNUC_UNUSED, ...@@ -711,9 +710,10 @@ udevProcessSCSIDevice(struct udev_device *device G_GNUC_UNUSED,
int ret = -1; int ret = -1;
unsigned int tmp = 0; unsigned int tmp = 0;
virNodeDevCapSCSIPtr scsi = &def->caps->data.scsi; virNodeDevCapSCSIPtr scsi = &def->caps->data.scsi;
char *filename = NULL, *p = NULL; g_autofree char *filename = NULL;
char *p = NULL;
filename = last_component(def->sysfs_path); filename = g_path_get_basename(def->sysfs_path);
if (virStrToLong_ui(filename, &p, 10, &scsi->host) < 0 || p == NULL || if (virStrToLong_ui(filename, &p, 10, &scsi->host) < 0 || p == NULL ||
virStrToLong_ui(p + 1, &p, 10, &scsi->bus) < 0 || p == NULL || virStrToLong_ui(p + 1, &p, 10, &scsi->bus) < 0 || p == NULL ||
...@@ -1038,7 +1038,7 @@ udevProcessMediatedDevice(struct udev_device *dev, ...@@ -1038,7 +1038,7 @@ udevProcessMediatedDevice(struct udev_device *dev,
goto cleanup; goto cleanup;
} }
data->type = g_strdup(last_component(canonicalpath)); data->type = g_path_get_basename(canonicalpath);
uuidstr = udev_device_get_sysname(dev); uuidstr = udev_device_get_sysname(dev);
if ((iommugrp = virMediatedDeviceGetIOMMUGroupNum(uuidstr)) < 0) if ((iommugrp = virMediatedDeviceGetIOMMUGroupNum(uuidstr)) < 0)
......
...@@ -55,7 +55,6 @@ ...@@ -55,7 +55,6 @@
#include "virprobe.h" #include "virprobe.h"
#include "virprocess.h" #include "virprocess.h"
#include "virstring.h" #include "virstring.h"
#include "dirname.h"
#include "passfd.h" #include "passfd.h"
#if WITH_SSH2 #if WITH_SSH2
...@@ -668,7 +667,7 @@ int virNetSocketNewConnectUNIX(const char *path, ...@@ -668,7 +667,7 @@ int virNetSocketNewConnectUNIX(const char *path,
remoteAddr.len = sizeof(remoteAddr.data.un); remoteAddr.len = sizeof(remoteAddr.data.un);
if (spawnDaemon) { if (spawnDaemon) {
const char *binname; g_autofree char *binname = NULL;
if (spawnDaemon && !binary) { if (spawnDaemon && !binary) {
virReportError(VIR_ERR_INTERNAL_ERROR, "%s", virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
...@@ -677,13 +676,7 @@ int virNetSocketNewConnectUNIX(const char *path, ...@@ -677,13 +676,7 @@ int virNetSocketNewConnectUNIX(const char *path,
goto cleanup; goto cleanup;
} }
if (!(binname = last_component(binary)) || binname[0] == '\0') { binname = g_path_get_basename(binary);
virReportError(VIR_ERR_INTERNAL_ERROR,
_("Cannot determine basename for binary '%s'"),
binary);
goto cleanup;
}
rundir = virGetUserRuntimeDirectory(); rundir = virGetUserRuntimeDirectory();
if (virFileMakePathWithMode(rundir, 0700) < 0) { if (virFileMakePathWithMode(rundir, 0700) < 0) {
......
...@@ -22,7 +22,6 @@ ...@@ -22,7 +22,6 @@
#include <config.h> #include <config.h>
#include <unistd.h> #include <unistd.h>
#include "dirname.h"
#include "virerror.h" #include "virerror.h"
#include "virlog.h" #include "virlog.h"
#include "storage_backend_disk.h" #include "storage_backend_disk.h"
...@@ -777,10 +776,10 @@ virStorageBackendDiskDeleteVol(virStoragePoolObjPtr pool, ...@@ -777,10 +776,10 @@ virStorageBackendDiskDeleteVol(virStoragePoolObjPtr pool,
unsigned int flags) unsigned int flags)
{ {
char *part_num = NULL; char *part_num = NULL;
char *dev_name; g_autofree char *dev_name = NULL;
virStoragePoolDefPtr def = virStoragePoolObjGetDef(pool); virStoragePoolDefPtr def = virStoragePoolObjGetDef(pool);
char *src_path = def->source.devices[0].path; char *src_path = def->source.devices[0].path;
char *srcname = last_component(src_path); g_autofree char *srcname = g_path_get_basename(src_path);
bool isDevMapperDevice; bool isDevMapperDevice;
g_autofree char *devpath = NULL; g_autofree char *devpath = NULL;
g_autoptr(virCommand) cmd = NULL; g_autoptr(virCommand) cmd = NULL;
...@@ -800,7 +799,7 @@ virStorageBackendDiskDeleteVol(virStoragePoolObjPtr pool, ...@@ -800,7 +799,7 @@ virStorageBackendDiskDeleteVol(virStoragePoolObjPtr pool,
* in both places */ * in both places */
isDevMapperDevice = virIsDevMapperDevice(vol->target.path); isDevMapperDevice = virIsDevMapperDevice(vol->target.path);
if (isDevMapperDevice) { if (isDevMapperDevice) {
dev_name = last_component(vol->target.path); dev_name = g_path_get_basename(vol->target.path);
} else { } else {
if (virFileResolveLink(vol->target.path, &devpath) < 0) { if (virFileResolveLink(vol->target.path, &devpath) < 0) {
virReportSystemError(errno, virReportSystemError(errno,
...@@ -808,7 +807,7 @@ virStorageBackendDiskDeleteVol(virStoragePoolObjPtr pool, ...@@ -808,7 +807,7 @@ virStorageBackendDiskDeleteVol(virStoragePoolObjPtr pool,
vol->target.path); vol->target.path);
return -1; return -1;
} }
dev_name = last_component(devpath); dev_name = g_path_get_basename(devpath);
} }
VIR_DEBUG("dev_name=%s, srcname=%s", dev_name, srcname); VIR_DEBUG("dev_name=%s, srcname=%s", dev_name, srcname);
......
...@@ -26,7 +26,6 @@ ...@@ -26,7 +26,6 @@
#include <sys/statvfs.h> #include <sys/statvfs.h>
#include <sys/param.h> #include <sys/param.h>
#include <dirent.h> #include <dirent.h>
#include "dirname.h"
#ifdef __linux__ #ifdef __linux__
# include <sys/ioctl.h> # include <sys/ioctl.h>
# include <linux/fs.h> # include <linux/fs.h>
...@@ -1519,7 +1518,7 @@ virStorageBackendVolOpen(const char *path, struct stat *sb, ...@@ -1519,7 +1518,7 @@ virStorageBackendVolOpen(const char *path, struct stat *sb,
unsigned int flags) unsigned int flags)
{ {
int fd, mode = 0; int fd, mode = 0;
char *base = last_component(path); g_autofree char *base = g_path_get_basename(path);
bool noerror = (flags & VIR_STORAGE_VOL_OPEN_NOERROR); bool noerror = (flags & VIR_STORAGE_VOL_OPEN_NOERROR);
if (g_lstat(path, sb) < 0) { if (g_lstat(path, sb) < 0) {
......
...@@ -18,7 +18,6 @@ ...@@ -18,7 +18,6 @@
#include <config.h> #include <config.h>
#include "dirname.h"
#include "virmdev.h" #include "virmdev.h"
#include "virlog.h" #include "virlog.h"
#include "virerror.h" #include "virerror.h"
...@@ -207,6 +206,7 @@ char * ...@@ -207,6 +206,7 @@ char *
virMediatedDeviceGetIOMMUGroupDev(const char *uuidstr) virMediatedDeviceGetIOMMUGroupDev(const char *uuidstr)
{ {
g_autofree char *result_path = NULL; g_autofree char *result_path = NULL;
g_autofree char *result_file = NULL;
g_autofree char *iommu_path = NULL; g_autofree char *iommu_path = NULL;
g_autofree char *dev_path = virMediatedDeviceGetSysfsPath(uuidstr); g_autofree char *dev_path = virMediatedDeviceGetSysfsPath(uuidstr);
char *vfio_path = NULL; char *vfio_path = NULL;
...@@ -226,7 +226,9 @@ virMediatedDeviceGetIOMMUGroupDev(const char *uuidstr) ...@@ -226,7 +226,9 @@ virMediatedDeviceGetIOMMUGroupDev(const char *uuidstr)
return NULL; return NULL;
} }
vfio_path = g_strdup_printf("/dev/vfio/%s", last_component(result_path)); result_file = g_path_get_basename(result_path);
vfio_path = g_strdup_printf("/dev/vfio/%s", result_file);
return vfio_path; return vfio_path;
} }
...@@ -236,13 +238,13 @@ int ...@@ -236,13 +238,13 @@ int
virMediatedDeviceGetIOMMUGroupNum(const char *uuidstr) virMediatedDeviceGetIOMMUGroupNum(const char *uuidstr)
{ {
g_autofree char *vfio_path = NULL; g_autofree char *vfio_path = NULL;
char *group_num_str = NULL; g_autofree char *group_num_str = NULL;
unsigned int group_num = -1; unsigned int group_num = -1;
if (!(vfio_path = virMediatedDeviceGetIOMMUGroupDev(uuidstr))) if (!(vfio_path = virMediatedDeviceGetIOMMUGroupDev(uuidstr)))
return -1; return -1;
group_num_str = last_component(vfio_path); group_num_str = g_path_get_basename(vfio_path);
ignore_value(virStrToLong_ui(group_num_str, NULL, 10, &group_num)); ignore_value(virStrToLong_ui(group_num_str, NULL, 10, &group_num));
return group_num; return group_num;
...@@ -501,7 +503,7 @@ virMediatedDeviceTypeReadAttrs(const char *sysfspath, ...@@ -501,7 +503,7 @@ virMediatedDeviceTypeReadAttrs(const char *sysfspath,
if (VIR_ALLOC(tmp) < 0) if (VIR_ALLOC(tmp) < 0)
return -1; return -1;
tmp->id = g_strdup(last_component(sysfspath)); tmp->id = g_path_get_basename(sysfspath);
/* @name sysfs attribute is optional, so getting ENOENT is fine */ /* @name sysfs attribute is optional, so getting ENOENT is fine */
MDEV_GET_SYSFS_ATTR("name", &tmp->name, virFileReadValueString, true); MDEV_GET_SYSFS_ATTR("name", &tmp->name, virFileReadValueString, true);
......
...@@ -18,7 +18,6 @@ ...@@ -18,7 +18,6 @@
#include <config.h> #include <config.h>
#include "dirname.h"
#include "virnetdev.h" #include "virnetdev.h"
#include "viralloc.h" #include "viralloc.h"
#include "virnetlink.h" #include "virnetlink.h"
...@@ -1096,7 +1095,7 @@ virNetDevIsPCIDevice(const char *devpath) ...@@ -1096,7 +1095,7 @@ virNetDevIsPCIDevice(const char *devpath)
{ {
char *subsys_link = NULL; char *subsys_link = NULL;
char *abs_path = NULL; char *abs_path = NULL;
char *subsys = NULL; g_autofree char *subsys = NULL;
bool ret = false; bool ret = false;
subsys_link = g_strdup_printf("%s/subsystem", devpath); subsys_link = g_strdup_printf("%s/subsystem", devpath);
...@@ -1111,7 +1110,7 @@ virNetDevIsPCIDevice(const char *devpath) ...@@ -1111,7 +1110,7 @@ virNetDevIsPCIDevice(const char *devpath)
goto cleanup; goto cleanup;
} }
subsys = last_component(abs_path); subsys = g_path_get_basename(abs_path);
ret = STRPREFIX(subsys, "pci"); ret = STRPREFIX(subsys, "pci");
cleanup: cleanup:
......
...@@ -30,7 +30,6 @@ ...@@ -30,7 +30,6 @@
#include <sys/stat.h> #include <sys/stat.h>
#include <unistd.h> #include <unistd.h>
#include "dirname.h"
#include "virlog.h" #include "virlog.h"
#include "vircommand.h" #include "vircommand.h"
#include "virerror.h" #include "virerror.h"
...@@ -265,7 +264,7 @@ virPCIDeviceGetDriverPathAndName(virPCIDevicePtr dev, char **path, char **name) ...@@ -265,7 +264,7 @@ virPCIDeviceGetDriverPathAndName(virPCIDevicePtr dev, char **path, char **name)
} }
/* path = "/sys/bus/pci/drivers/${drivername}" */ /* path = "/sys/bus/pci/drivers/${drivername}" */
*name = g_strdup(last_component(*path)); *name = g_path_get_basename(*path);
/* name = "${drivername}" */ /* name = "${drivername}" */
ret = 0; ret = 0;
...@@ -1926,7 +1925,7 @@ virPCIDeviceAddressGetIOMMUGroupNum(virPCIDeviceAddressPtr addr) ...@@ -1926,7 +1925,7 @@ virPCIDeviceAddressGetIOMMUGroupNum(virPCIDeviceAddressPtr addr)
g_autofree char *devName = NULL; g_autofree char *devName = NULL;
g_autofree char *devPath = NULL; g_autofree char *devPath = NULL;
g_autofree char *groupPath = NULL; g_autofree char *groupPath = NULL;
const char *groupNumStr; g_autofree char *groupNumStr = NULL;
unsigned int groupNum; unsigned int groupNum;
devName = g_strdup_printf(VIR_PCI_DEVICE_ADDRESS_FMT, addr->domain, addr->bus, devName = g_strdup_printf(VIR_PCI_DEVICE_ADDRESS_FMT, addr->domain, addr->bus,
...@@ -1943,7 +1942,7 @@ virPCIDeviceAddressGetIOMMUGroupNum(virPCIDeviceAddressPtr addr) ...@@ -1943,7 +1942,7 @@ virPCIDeviceAddressGetIOMMUGroupNum(virPCIDeviceAddressPtr addr)
return -1; return -1;
} }
groupNumStr = last_component(groupPath); groupNumStr = g_path_get_basename(groupPath);
if (virStrToLong_ui(groupNumStr, NULL, 10, &groupNum) < 0) { if (virStrToLong_ui(groupNumStr, NULL, 10, &groupNum) < 0) {
virReportError(VIR_ERR_INTERNAL_ERROR, virReportError(VIR_ERR_INTERNAL_ERROR,
_("device %s iommu_group symlink %s has " _("device %s iommu_group symlink %s has "
...@@ -1979,7 +1978,7 @@ virPCIDeviceGetIOMMUGroupDev(virPCIDevicePtr dev) ...@@ -1979,7 +1978,7 @@ virPCIDeviceGetIOMMUGroupDev(virPCIDevicePtr dev)
{ {
g_autofree char *devPath = NULL; g_autofree char *devPath = NULL;
g_autofree char *groupPath = NULL; g_autofree char *groupPath = NULL;
char *groupDev = NULL; g_autofree char *groupFile = NULL;
if (!(devPath = virPCIFile(dev->name, "iommu_group"))) if (!(devPath = virPCIFile(dev->name, "iommu_group")))
return NULL; return NULL;
...@@ -1995,9 +1994,9 @@ virPCIDeviceGetIOMMUGroupDev(virPCIDevicePtr dev) ...@@ -1995,9 +1994,9 @@ virPCIDeviceGetIOMMUGroupDev(virPCIDevicePtr dev)
dev->name, devPath); dev->name, devPath);
return NULL; return NULL;
} }
groupDev = g_strdup_printf("/dev/vfio/%s", last_component(groupPath)); groupFile = g_path_get_basename(groupPath);
return groupDev; return g_strdup_printf("/dev/vfio/%s", groupFile);
} }
static int static int
...@@ -2207,7 +2206,7 @@ virPCIDeviceAddressPtr ...@@ -2207,7 +2206,7 @@ virPCIDeviceAddressPtr
virPCIGetDeviceAddressFromSysfsLink(const char *device_link) virPCIGetDeviceAddressFromSysfsLink(const char *device_link)
{ {
virPCIDeviceAddressPtr bdf = NULL; virPCIDeviceAddressPtr bdf = NULL;
char *config_address = NULL; g_autofree char *config_address = NULL;
g_autofree char *device_path = NULL; g_autofree char *device_path = NULL;
if (!virFileExists(device_link)) { if (!virFileExists(device_link)) {
...@@ -2223,7 +2222,7 @@ virPCIGetDeviceAddressFromSysfsLink(const char *device_link) ...@@ -2223,7 +2222,7 @@ virPCIGetDeviceAddressFromSysfsLink(const char *device_link)
return NULL; return NULL;
} }
config_address = last_component(device_path); config_address = g_path_get_basename(device_path);
if (VIR_ALLOC(bdf) < 0) if (VIR_ALLOC(bdf) < 0)
return NULL; return NULL;
......
...@@ -37,7 +37,6 @@ ...@@ -37,7 +37,6 @@
#include "virlog.h" #include "virlog.h"
#include "vircommand.h" #include "vircommand.h"
#include "virrandom.h" #include "virrandom.h"
#include "dirname.h"
#include "virprocess.h" #include "virprocess.h"
#include "virstring.h" #include "virstring.h"
...@@ -57,8 +56,6 @@ static unsigned int testRegenerate = -1; ...@@ -57,8 +56,6 @@ static unsigned int testRegenerate = -1;
static size_t testCounter; static size_t testCounter;
static virBitmapPtr testBitmap; static virBitmapPtr testBitmap;
char *progname;
virArch virTestHostArch = VIR_ARCH_X86_64; virArch virTestHostArch = VIR_ARCH_X86_64;
virArch virArch
...@@ -857,6 +854,8 @@ int virTestMain(int argc, ...@@ -857,6 +854,8 @@ int virTestMain(int argc,
size_t noutputs = 0; size_t noutputs = 0;
virLogOutputPtr output = NULL; virLogOutputPtr output = NULL;
virLogOutputPtr *outputs = NULL; virLogOutputPtr *outputs = NULL;
g_autofree char *baseprogname = NULL;
const char *progname;
if (getenv("VIR_TEST_FILE_ACCESS")) if (getenv("VIR_TEST_FILE_ACCESS"))
VIR_TEST_PRELOAD(VIR_TEST_MOCK("virtest")); VIR_TEST_PRELOAD(VIR_TEST_MOCK("virtest"));
...@@ -866,7 +865,7 @@ int virTestMain(int argc, ...@@ -866,7 +865,7 @@ int virTestMain(int argc,
VIR_TEST_PRELOAD(lib); VIR_TEST_PRELOAD(lib);
va_end(ap); va_end(ap);
progname = last_component(argv[0]); progname = baseprogname = g_path_get_basename(argv[0]);
if (STRPREFIX(progname, "lt-")) if (STRPREFIX(progname, "lt-"))
progname += 3; progname += 3;
......
...@@ -36,8 +36,6 @@ ...@@ -36,8 +36,6 @@
# define fprintf virFilePrintf # define fprintf virFilePrintf
#endif #endif
extern char *progname;
/* Makefile.am provides these two definitions */ /* Makefile.am provides these two definitions */
#if !defined(abs_srcdir) || !defined(abs_builddir) #if !defined(abs_srcdir) || !defined(abs_builddir)
# error Fix Makefile.am # error Fix Makefile.am
......
...@@ -28,7 +28,6 @@ ...@@ -28,7 +28,6 @@
# include "viralloc.h" # include "viralloc.h"
# include "virstring.h" # include "virstring.h"
# include "virfile.h" # include "virfile.h"
# include "dirname.h"
static int (*real_access)(const char *path, int mode); static int (*real_access)(const char *path, int mode);
static int (*real_open)(const char *path, int flags, ...); static int (*real_open)(const char *path, int flags, ...);
...@@ -884,7 +883,7 @@ static int ...@@ -884,7 +883,7 @@ static int
pci_driver_handle_change(int fd G_GNUC_UNUSED, const char *path) pci_driver_handle_change(int fd G_GNUC_UNUSED, const char *path)
{ {
int ret; int ret;
const char *file = last_component(path); g_autofree char *file = g_path_get_basename(path);
if (STREQ(file, "bind")) if (STREQ(file, "bind"))
ret = pci_driver_handle_bind(path); ret = pci_driver_handle_bind(path);
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册