]> git.ipfire.org Git - thirdparty/libvirt.git/commitdiff
src: replace last_component() with g_path_get_basename()
authorDaniel P. Berrangé <berrange@redhat.com>
Mon, 23 Dec 2019 15:47:21 +0000 (15:47 +0000)
committerDaniel P. Berrangé <berrange@redhat.com>
Fri, 3 Jan 2020 15:42:13 +0000 (15:42 +0000)
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>
src/conf/node_device_conf.c
src/node_device/node_device_udev.c
src/rpc/virnetsocket.c
src/storage/storage_backend_disk.c
src/storage/storage_util.c
src/util/virmdev.c
src/util/virnetdev.c
src/util/virpci.c
tests/testutils.c
tests/testutils.h
tests/virpcimock.c

index a02c8fe306712a566775d8e67fd33cea4a69845b..9b206abadc6e1785fc7fdf479aec6c03e6e5fb8b 100644 (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;
index eedcd123a3021d588350a398eac574e359bd7705..4b33dc25d831534de04c06bada7d60b1712c1063 100644 (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)
index f072afe857a29c29d0ac9d68c87086234df11ed3..9ad7c2cc28edab6d2116824273ef3bc563e11f35 100644 (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) {
index 45d1257f3d9598a277407fa21a74b24c14bc08bf..00e8b1aa135e3caadbb21537fdc26e9bf32eb0d9 100644 (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);
index 58225c09f5d04c49af6345fc1d827a4326d71465..ebc262278d7b7fab725472c4f64e55ba53bbe990 100644 (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) {
index cd52a91ffd97ff76c109e53a1affdc21dd4c72b8..c2499c0a20262c1f4f4c893ba2bcf9aa2e700911 100644 (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);
index 43d2fb06cfa69c9689ffe59e19519605911c8d35..03e276a97241cb91cb04392b0d4a2eb36c57c089 100644 (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:
index 99a80027436e50694b3fd43024cbcbd9a9c0947b..06d855a95be1a03ed2a668c0d2706e8184749750 100644 (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;
 
index f3f66169675534aed43b41bbe5e53d9a424e71ce..6f0ef2b2f19b339a5890368a9785f379d4457e2f 100644 (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;
 
index f62d435555cb833c6f386efe72146d809be2a684..8a7ea38f439b79dab0fdcd15819eeb8dd19a93bb 100644 (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
index 6048118d5cff27c40781436bbe88bef1727979dd..92b6f810d888bd43c5adbe34e3e8034d23a98a16 100644 (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);