[PATCH 1/1] Replace AbsPath judgement method with g_path_is_absolute()

Luke Yue lukedyue at gmail.com
Tue Apr 20 04:44:12 UTC 2021


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 <lukedyue at gmail.com>
---
 src/conf/backup_conf.c            | 2 +-
 src/conf/snapshot_conf.c          | 2 +-
 src/conf/storage_source_conf.c    | 2 +-
 src/lxc/lxc_native.c              | 2 +-
 src/qemu/qemu_block.c             | 2 +-
 src/storage_file/storage_source.c | 2 +-
 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 +-
 12 files changed, 12 insertions(+), 12 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/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..c815daf1e6 100644
--- a/src/qemu/qemu_block.c
+++ b/src/qemu/qemu_block.c
@@ -427,7 +427,7 @@ 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/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/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 <HardDisk> '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);
-- 
2.31.1




More information about the libvir-list mailing list