From 6e91cbfdad88cbc85053002e548cde747d21e15c Mon Sep 17 00:00:00 2001 From: Luke Yue Date: Tue, 20 Apr 2021 12:44:12 +0800 Subject: [PATCH] Replace AbsPath judgement method with g_path_is_absolute() The g_path_is_absolute() considers more situations than just a simply "path[0] == '/'". Related issue: https://gitlab.com/libvirt/libvirt/-/issues/12 Signed-off-by: Luke Yue Signed-off-by: Michal Privoznik Reviewed-by: Michal Privoznik --- src/conf/backup_conf.c | 2 +- src/conf/snapshot_conf.c | 2 +- src/conf/storage_source_conf.c | 2 +- src/libxl/xen_common.c | 2 +- src/lxc/lxc_cgroup.c | 2 +- src/lxc/lxc_native.c | 2 +- src/qemu/qemu_block.c | 3 ++- src/qemu/qemu_cgroup.c | 2 +- src/qemu/qemu_command.c | 2 +- src/storage_file/storage_source.c | 2 +- src/storage_file/storage_source_backingstore.c | 2 +- src/util/vircgroup.c | 4 ++-- src/util/vircommand.c | 2 +- src/vbox/vbox_snapshot_conf.c | 2 +- src/vmware/vmware_conf.c | 2 +- src/vmware/vmware_driver.c | 2 +- tests/virtestmock.c | 2 +- tools/virt-login-shell-helper.c | 2 +- 18 files changed, 20 insertions(+), 19 deletions(-) diff --git a/src/conf/backup_conf.c b/src/conf/backup_conf.c index 2de77a59c0..7f54a25ff6 100644 --- a/src/conf/backup_conf.c +++ b/src/conf/backup_conf.c @@ -262,7 +262,7 @@ virDomainBackupDefParse(xmlXPathContextPtr ctxt, } if (def->server->transport == VIR_STORAGE_NET_HOST_TRANS_UNIX && - def->server->socket[0] != '/') { + !g_path_is_absolute(def->server->socket)) { virReportError(VIR_ERR_XML_ERROR, _("backup socket path '%s' must be absolute"), def->server->socket); diff --git a/src/conf/snapshot_conf.c b/src/conf/snapshot_conf.c index 07c9ea7af7..df3f2a4c63 100644 --- a/src/conf/snapshot_conf.c +++ b/src/conf/snapshot_conf.c @@ -363,7 +363,7 @@ virDomainSnapshotDefParse(xmlXPathContextPtr ctxt, def->file = g_steal_pointer(&memoryFile); /* verify that memory path is absolute */ - if (def->file && def->file[0] != '/') { + if (def->file && !g_path_is_absolute(def->file)) { virReportError(VIR_ERR_XML_ERROR, _("memory snapshot file path (%s) must be absolute"), def->file); diff --git a/src/conf/storage_source_conf.c b/src/conf/storage_source_conf.c index 05939181d6..5ca06fa30a 100644 --- a/src/conf/storage_source_conf.c +++ b/src/conf/storage_source_conf.c @@ -1213,7 +1213,7 @@ virStorageSourceIsRelative(virStorageSource *src) case VIR_STORAGE_TYPE_FILE: case VIR_STORAGE_TYPE_BLOCK: case VIR_STORAGE_TYPE_DIR: - return src->path[0] != '/'; + return !g_path_is_absolute(src->path); case VIR_STORAGE_TYPE_NETWORK: case VIR_STORAGE_TYPE_VOLUME: diff --git a/src/libxl/xen_common.c b/src/libxl/xen_common.c index 6fa69fbdf0..4e09c6b1a2 100644 --- a/src/libxl/xen_common.c +++ b/src/libxl/xen_common.c @@ -818,7 +818,7 @@ xenParseSxprChar(const char *value, prefix = value; - if (value[0] == '/') { + if (g_path_is_absolute(value)) { def->source->type = VIR_DOMAIN_CHR_TYPE_DEV; def->source->data.file.path = g_strdup(value); } else { diff --git a/src/lxc/lxc_cgroup.c b/src/lxc/lxc_cgroup.c index 1861ccf4db..736b2000ff 100644 --- a/src/lxc/lxc_cgroup.c +++ b/src/lxc/lxc_cgroup.c @@ -383,7 +383,7 @@ virCgroup *virLXCCgroupCreate(virDomainDef *def, if (!machineName) return NULL; - if (def->resource->partition[0] != '/') { + if (!g_path_is_absolute(def->resource->partition)) { virReportError(VIR_ERR_CONFIG_UNSUPPORTED, _("Resource partition '%s' must start with '/'"), def->resource->partition); diff --git a/src/lxc/lxc_native.c b/src/lxc/lxc_native.c index 083fb50af7..4bdd960e23 100644 --- a/src/lxc/lxc_native.c +++ b/src/lxc/lxc_native.c @@ -261,7 +261,7 @@ lxcAddFstabLine(virDomainDef *def, lxcFstab *fstab) if (!options) return -1; - if (fstab->dst[0] != '/') { + if (!g_path_is_absolute(fstab->dst)) { dst = g_strdup_printf("/%s", fstab->dst); } else { dst = g_strdup(fstab->dst); diff --git a/src/qemu/qemu_block.c b/src/qemu/qemu_block.c index a972e1e368..89f20eb1d2 100644 --- a/src/qemu/qemu_block.c +++ b/src/qemu/qemu_block.c @@ -427,7 +427,8 @@ qemuBlockStorageSourceGetURI(virStorageSource *src) if (src->volume) { uri->path = g_strdup_printf("/%s/%s", src->volume, src->path); } else { - uri->path = g_strdup_printf("%s%s", src->path[0] == '/' ? "" : "/", + uri->path = g_strdup_printf("%s%s", + g_path_is_absolute(src->path) ? "" : "/", src->path); } } diff --git a/src/qemu/qemu_cgroup.c b/src/qemu/qemu_cgroup.c index 717ce32bf2..0e8835fb86 100644 --- a/src/qemu/qemu_cgroup.c +++ b/src/qemu/qemu_cgroup.c @@ -927,7 +927,7 @@ qemuInitCgroup(virDomainObj *vm, vm->def->resource = res; } - if (vm->def->resource->partition[0] != '/') { + if (!g_path_is_absolute(vm->def->resource->partition)) { virReportError(VIR_ERR_CONFIG_UNSUPPORTED, _("Resource partition '%s' must start with '/'"), vm->def->resource->partition); diff --git a/src/qemu/qemu_command.c b/src/qemu/qemu_command.c index 2f69a79bc0..2ceff15512 100644 --- a/src/qemu/qemu_command.c +++ b/src/qemu/qemu_command.c @@ -1009,7 +1009,7 @@ qemuBuildNetworkDriveStr(virStorageSource *src, !src->hosts->name) || (src->hosts->transport == VIR_STORAGE_NET_HOST_TRANS_UNIX && src->hosts->socket && - src->hosts->socket[0] != '/'))) { + !g_path_is_absolute(src->hosts->socket)))) { virBufferAddLit(&buf, "nbd:"); diff --git a/src/storage_file/storage_source.c b/src/storage_file/storage_source.c index 4246ede04b..c2bdc39f25 100644 --- a/src/storage_file/storage_source.c +++ b/src/storage_file/storage_source.c @@ -68,7 +68,7 @@ virStorageSourceBackinStoreStringIsFile(const char *backing) static bool virStorageSourceBackinStoreStringIsRelative(const char *backing) { - if (backing[0] == '/') + if (g_path_is_absolute(backing)) return false; if (!virStorageSourceBackinStoreStringIsFile(backing)) diff --git a/src/storage_file/storage_source_backingstore.c b/src/storage_file/storage_source_backingstore.c index 9021b9da0c..e48ae725ab 100644 --- a/src/storage_file/storage_source_backingstore.c +++ b/src/storage_file/storage_source_backingstore.c @@ -92,7 +92,7 @@ virStorageSourceParseBackingURI(virStorageSource *src, path = ""; /* possibly skip the leading slash */ - if (path[0] == '/') + if (g_path_is_absolute(path)) path++; /* NBD allows empty export name (path) */ diff --git a/src/util/vircgroup.c b/src/util/vircgroup.c index 37dde2a5ed..65a81e8d6f 100644 --- a/src/util/vircgroup.c +++ b/src/util/vircgroup.c @@ -953,7 +953,7 @@ virCgroupNewPartition(const char *path, *group = NULL; - if (path[0] != '/') { + if (!g_path_is_absolute(path)) { virReportError(VIR_ERR_INTERNAL_ERROR, _("Partition path '%s' must start with '/'"), path); @@ -1307,7 +1307,7 @@ virCgroupNewMachineSystemd(const char *name, } } - if (!path || STREQ(path, "/") || path[0] != '/') { + if (!path || STREQ(path, "/") || !g_path_is_absolute(path)) { VIR_DEBUG("Systemd didn't setup its controller, path=%s", NULLSTR(path)); return -2; diff --git a/src/util/vircommand.c b/src/util/vircommand.c index 8117190076..7abb2e288f 100644 --- a/src/util/vircommand.c +++ b/src/util/vircommand.c @@ -629,7 +629,7 @@ virExec(virCommand *cmd) g_autofree gid_t *groups = NULL; int ngroups; - if (cmd->args[0][0] != '/') { + if (!g_path_is_absolute(cmd->args[0])) { if (!(binary = binarystr = virFindFileInPath(cmd->args[0]))) { virReportSystemError(ENOENT, _("Cannot find '%s' in path"), diff --git a/src/vbox/vbox_snapshot_conf.c b/src/vbox/vbox_snapshot_conf.c index 743ee72211..f7423f60fc 100644 --- a/src/vbox/vbox_snapshot_conf.c +++ b/src/vbox/vbox_snapshot_conf.c @@ -82,7 +82,7 @@ virVBoxSnapshotConfCreateVBoxSnapshotConfHardDiskPtr(xmlNodePtr diskNode, _("Cannot parse 'location' attribute")); goto cleanup; } - if (location[0] != '/') { + if (!g_path_is_absolute(location)) { /* The location is a relative path, so we must change it into an absolute one. */ tmp = g_strdup_printf("%s%s", machineLocation, location); hardDisk->location = g_strdup(tmp); diff --git a/src/vmware/vmware_conf.c b/src/vmware/vmware_conf.c index 6b0d3f9610..af4461f2cb 100644 --- a/src/vmware/vmware_conf.c +++ b/src/vmware/vmware_conf.c @@ -154,7 +154,7 @@ vmwareLoadDomains(struct vmware_driver *driver) for (str = outbuf; (vmxPath = strtok_r(str, "\n", &saveptr)) != NULL; str = NULL) { - if (vmxPath[0] != '/') + if (!g_path_is_absolute(vmxPath)) continue; if (virFileReadAll(vmxPath, 10000, &vmx) < 0) diff --git a/src/vmware/vmware_driver.c b/src/vmware/vmware_driver.c index 62f653e86f..3bd6d4d440 100644 --- a/src/vmware/vmware_driver.c +++ b/src/vmware/vmware_driver.c @@ -309,7 +309,7 @@ vmwareUpdateVMStatus(struct vmware_driver *driver, virDomainObj *vm) for (str = outbuf; (parsedVmxPath = strtok_r(str, "\n", &saveptr)) != NULL; str = NULL) { - if (parsedVmxPath[0] != '/') + if (!g_path_is_absolute(parsedVmxPath)) continue; if (STREQ(parsedVmxPath, vmxAbsolutePath)) { diff --git a/tests/virtestmock.c b/tests/virtestmock.c index 776493f0c5..0073677b4d 100644 --- a/tests/virtestmock.c +++ b/tests/virtestmock.c @@ -104,7 +104,7 @@ checkPath(const char *path, char *relPath = NULL; char *crippledPath = NULL; - if (path[0] != '/') + if (!g_path_is_absolute(path)) relPath = g_strdup_printf("./%s", path); /* Le sigh. virFileCanonicalizePath() expects @path to exist, otherwise diff --git a/tools/virt-login-shell-helper.c b/tools/virt-login-shell-helper.c index 78e351867f..5c6e007b09 100644 --- a/tools/virt-login-shell-helper.c +++ b/tools/virt-login-shell-helper.c @@ -346,7 +346,7 @@ main(int argc, char **argv) * a leading '-' to indicate it is a login shell */ shcmd = shargv[0]; - if (shcmd[0] != '/') { + if (!g_path_is_absolute(shcmd)) { virReportSystemError(errno, _("Shell '%s' should have absolute path"), shcmd);