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: Fabiano Fidêncio <fidencio@redhat.com>
Signed-off-by: Daniel P. Berrangé <berrange@redhat.com>
This commit is contained in:
Daniel P. Berrangé 2019-12-23 15:47:21 +00:00
parent f5e9bdb87f
commit 12d17dcc26
11 changed files with 35 additions and 48 deletions

View File

@ -2448,7 +2448,7 @@ virNodeDeviceGetSCSITargetCaps(const char *sysfsPath,
if (!(dir = mdir_name(sysfsPath)))
return -1;
rport = g_strdup(last_component(dir));
rport = g_path_get_basename(dir);
if (!virFCIsCapableRport(rport))
goto cleanup;

View File

@ -23,7 +23,6 @@
#include <pciaccess.h>
#include <scsi/scsi.h>
#include "dirname.h"
#include "node_device_conf.h"
#include "node_device_event.h"
#include "node_device_driver.h"
@ -602,10 +601,10 @@ udevProcessSCSIHost(struct udev_device *device G_GNUC_UNUSED,
virNodeDeviceDefPtr def)
{
virNodeDevCapSCSIHostPtr scsi_host = &def->caps->data.scsi_host;
char *filename = NULL;
g_autofree char *filename = NULL;
char *str;
filename = last_component(def->sysfs_path);
filename = g_path_get_basename(def->sysfs_path);
if (!(str = STRSKIP(filename, "host")) ||
virStrToLong_ui(str, NULL, 0, &scsi_host->host) < 0) {
@ -711,9 +710,10 @@ udevProcessSCSIDevice(struct udev_device *device G_GNUC_UNUSED,
int ret = -1;
unsigned int tmp = 0;
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 ||
virStrToLong_ui(p + 1, &p, 10, &scsi->bus) < 0 || p == NULL ||
@ -1038,7 +1038,7 @@ udevProcessMediatedDevice(struct udev_device *dev,
goto cleanup;
}
data->type = g_strdup(last_component(canonicalpath));
data->type = g_path_get_basename(canonicalpath);
uuidstr = udev_device_get_sysname(dev);
if ((iommugrp = virMediatedDeviceGetIOMMUGroupNum(uuidstr)) < 0)

View File

@ -55,7 +55,6 @@
#include "virprobe.h"
#include "virprocess.h"
#include "virstring.h"
#include "dirname.h"
#include "passfd.h"
#if WITH_SSH2
@ -668,7 +667,7 @@ int virNetSocketNewConnectUNIX(const char *path,
remoteAddr.len = sizeof(remoteAddr.data.un);
if (spawnDaemon) {
const char *binname;
g_autofree char *binname = NULL;
if (spawnDaemon && !binary) {
virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
@ -677,13 +676,7 @@ int virNetSocketNewConnectUNIX(const char *path,
goto cleanup;
}
if (!(binname = last_component(binary)) || binname[0] == '\0') {
virReportError(VIR_ERR_INTERNAL_ERROR,
_("Cannot determine basename for binary '%s'"),
binary);
goto cleanup;
}
binname = g_path_get_basename(binary);
rundir = virGetUserRuntimeDirectory();
if (virFileMakePathWithMode(rundir, 0700) < 0) {

View File

@ -22,7 +22,6 @@
#include <config.h>
#include <unistd.h>
#include "dirname.h"
#include "virerror.h"
#include "virlog.h"
#include "storage_backend_disk.h"
@ -777,10 +776,10 @@ virStorageBackendDiskDeleteVol(virStoragePoolObjPtr pool,
unsigned int flags)
{
char *part_num = NULL;
char *dev_name;
g_autofree char *dev_name = NULL;
virStoragePoolDefPtr def = virStoragePoolObjGetDef(pool);
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;
g_autofree char *devpath = NULL;
g_autoptr(virCommand) cmd = NULL;
@ -800,7 +799,7 @@ virStorageBackendDiskDeleteVol(virStoragePoolObjPtr pool,
* in both places */
isDevMapperDevice = virIsDevMapperDevice(vol->target.path);
if (isDevMapperDevice) {
dev_name = last_component(vol->target.path);
dev_name = g_path_get_basename(vol->target.path);
} else {
if (virFileResolveLink(vol->target.path, &devpath) < 0) {
virReportSystemError(errno,
@ -808,7 +807,7 @@ virStorageBackendDiskDeleteVol(virStoragePoolObjPtr pool,
vol->target.path);
return -1;
}
dev_name = last_component(devpath);
dev_name = g_path_get_basename(devpath);
}
VIR_DEBUG("dev_name=%s, srcname=%s", dev_name, srcname);

View File

@ -26,7 +26,6 @@
#include <sys/statvfs.h>
#include <sys/param.h>
#include <dirent.h>
#include "dirname.h"
#ifdef __linux__
# include <sys/ioctl.h>
# include <linux/fs.h>
@ -1519,7 +1518,7 @@ virStorageBackendVolOpen(const char *path, struct stat *sb,
unsigned int flags)
{
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);
if (g_lstat(path, sb) < 0) {

View File

@ -18,7 +18,6 @@
#include <config.h>
#include "dirname.h"
#include "virmdev.h"
#include "virlog.h"
#include "virerror.h"
@ -207,6 +206,7 @@ char *
virMediatedDeviceGetIOMMUGroupDev(const char *uuidstr)
{
g_autofree char *result_path = NULL;
g_autofree char *result_file = NULL;
g_autofree char *iommu_path = NULL;
g_autofree char *dev_path = virMediatedDeviceGetSysfsPath(uuidstr);
char *vfio_path = NULL;
@ -226,7 +226,9 @@ virMediatedDeviceGetIOMMUGroupDev(const char *uuidstr)
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;
}
@ -236,13 +238,13 @@ int
virMediatedDeviceGetIOMMUGroupNum(const char *uuidstr)
{
g_autofree char *vfio_path = NULL;
char *group_num_str = NULL;
g_autofree char *group_num_str = NULL;
unsigned int group_num = -1;
if (!(vfio_path = virMediatedDeviceGetIOMMUGroupDev(uuidstr)))
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));
return group_num;
@ -501,7 +503,7 @@ virMediatedDeviceTypeReadAttrs(const char *sysfspath,
if (VIR_ALLOC(tmp) < 0)
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 */
MDEV_GET_SYSFS_ATTR("name", &tmp->name, virFileReadValueString, true);

View File

@ -18,7 +18,6 @@
#include <config.h>
#include "dirname.h"
#include "virnetdev.h"
#include "viralloc.h"
#include "virnetlink.h"
@ -1096,7 +1095,7 @@ virNetDevIsPCIDevice(const char *devpath)
{
char *subsys_link = NULL;
char *abs_path = NULL;
char *subsys = NULL;
g_autofree char *subsys = NULL;
bool ret = false;
subsys_link = g_strdup_printf("%s/subsystem", devpath);
@ -1111,7 +1110,7 @@ virNetDevIsPCIDevice(const char *devpath)
goto cleanup;
}
subsys = last_component(abs_path);
subsys = g_path_get_basename(abs_path);
ret = STRPREFIX(subsys, "pci");
cleanup:

View File

@ -30,7 +30,6 @@
#include <sys/stat.h>
#include <unistd.h>
#include "dirname.h"
#include "virlog.h"
#include "vircommand.h"
#include "virerror.h"
@ -265,7 +264,7 @@ virPCIDeviceGetDriverPathAndName(virPCIDevicePtr dev, char **path, char **name)
}
/* path = "/sys/bus/pci/drivers/${drivername}" */
*name = g_strdup(last_component(*path));
*name = g_path_get_basename(*path);
/* name = "${drivername}" */
ret = 0;
@ -1926,7 +1925,7 @@ virPCIDeviceAddressGetIOMMUGroupNum(virPCIDeviceAddressPtr addr)
g_autofree char *devName = NULL;
g_autofree char *devPath = NULL;
g_autofree char *groupPath = NULL;
const char *groupNumStr;
g_autofree char *groupNumStr = NULL;
unsigned int groupNum;
devName = g_strdup_printf(VIR_PCI_DEVICE_ADDRESS_FMT, addr->domain, addr->bus,
@ -1943,7 +1942,7 @@ virPCIDeviceAddressGetIOMMUGroupNum(virPCIDeviceAddressPtr addr)
return -1;
}
groupNumStr = last_component(groupPath);
groupNumStr = g_path_get_basename(groupPath);
if (virStrToLong_ui(groupNumStr, NULL, 10, &groupNum) < 0) {
virReportError(VIR_ERR_INTERNAL_ERROR,
_("device %s iommu_group symlink %s has "
@ -1979,7 +1978,7 @@ virPCIDeviceGetIOMMUGroupDev(virPCIDevicePtr dev)
{
g_autofree char *devPath = NULL;
g_autofree char *groupPath = NULL;
char *groupDev = NULL;
g_autofree char *groupFile = NULL;
if (!(devPath = virPCIFile(dev->name, "iommu_group")))
return NULL;
@ -1995,9 +1994,9 @@ virPCIDeviceGetIOMMUGroupDev(virPCIDevicePtr dev)
dev->name, devPath);
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
@ -2207,7 +2206,7 @@ virPCIDeviceAddressPtr
virPCIGetDeviceAddressFromSysfsLink(const char *device_link)
{
virPCIDeviceAddressPtr bdf = NULL;
char *config_address = NULL;
g_autofree char *config_address = NULL;
g_autofree char *device_path = NULL;
if (!virFileExists(device_link)) {
@ -2223,7 +2222,7 @@ virPCIGetDeviceAddressFromSysfsLink(const char *device_link)
return NULL;
}
config_address = last_component(device_path);
config_address = g_path_get_basename(device_path);
if (VIR_ALLOC(bdf) < 0)
return NULL;

View File

@ -37,7 +37,6 @@
#include "virlog.h"
#include "vircommand.h"
#include "virrandom.h"
#include "dirname.h"
#include "virprocess.h"
#include "virstring.h"
@ -57,8 +56,6 @@ static unsigned int testRegenerate = -1;
static size_t testCounter;
static virBitmapPtr testBitmap;
char *progname;
virArch virTestHostArch = VIR_ARCH_X86_64;
virArch
@ -857,6 +854,8 @@ int virTestMain(int argc,
size_t noutputs = 0;
virLogOutputPtr output = NULL;
virLogOutputPtr *outputs = NULL;
g_autofree char *baseprogname = NULL;
const char *progname;
if (getenv("VIR_TEST_FILE_ACCESS"))
VIR_TEST_PRELOAD(VIR_TEST_MOCK("virtest"));
@ -866,7 +865,7 @@ int virTestMain(int argc,
VIR_TEST_PRELOAD(lib);
va_end(ap);
progname = last_component(argv[0]);
progname = baseprogname = g_path_get_basename(argv[0]);
if (STRPREFIX(progname, "lt-"))
progname += 3;

View File

@ -36,8 +36,6 @@
# define fprintf virFilePrintf
#endif
extern char *progname;
/* Makefile.am provides these two definitions */
#if !defined(abs_srcdir) || !defined(abs_builddir)
# error Fix Makefile.am

View File

@ -28,7 +28,6 @@
# include "viralloc.h"
# include "virstring.h"
# include "virfile.h"
# include "dirname.h"
static int (*real_access)(const char *path, int mode);
static int (*real_open)(const char *path, int flags, ...);
@ -884,7 +883,7 @@ static int
pci_driver_handle_change(int fd G_GNUC_UNUSED, const char *path)
{
int ret;
const char *file = last_component(path);
g_autofree char *file = g_path_get_basename(path);
if (STREQ(file, "bind"))
ret = pci_driver_handle_bind(path);