qemu-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Qemu-devel] [PATCH] 9pfs-local: simplify/optimize local_mapped_attr_pat


From: Michael Tokarev
Subject: [Qemu-devel] [PATCH] 9pfs-local: simplify/optimize local_mapped_attr_path()
Date: Thu, 5 Mar 2015 00:03:56 +0300

Omit one unnecessary memory allocation for components of the path
and create the resulting path directly given lengths of the components.

This uses (char*) cast because basename() accepts a char* without const,
for unknown reason.  Maybe it is better to use strrchr(), but I'm not
sure for various forms of directory component delimiter.

And according to basename(3) manpage, it might return different strings
for various corner cases, for example for empty argument it returns a
string "." which is obviously at some other address, so both the old
code and new code will do a bad thing here.  So maybe it is actually
better to use strrchr() after all.

Signed-off-by: Michael Tokarev <address@hidden>
---
 hw/9pfs/virtio-9p-local.c | 17 ++++-------------
 1 file changed, 4 insertions(+), 13 deletions(-)

diff --git a/hw/9pfs/virtio-9p-local.c b/hw/9pfs/virtio-9p-local.c
index d05c917..fddc242 100644
--- a/hw/9pfs/virtio-9p-local.c
+++ b/hw/9pfs/virtio-9p-local.c
@@ -45,19 +45,10 @@
 
 static char *local_mapped_attr_path(FsContext *ctx, const char *path)
 {
-    char *dir_name;
-    char *tmp_path = g_strdup(path);
-    char *base_name = basename(tmp_path);
-    char *buffer;
-
-    /* NULL terminate the directory */
-    dir_name = tmp_path;
-    *(base_name - 1) = '\0';
-
-    buffer = g_strdup_printf("%s/%s/%s/%s",
-             ctx->fs_root, dir_name, VIRTFS_META_DIR, base_name);
-    g_free(tmp_path);
-    return buffer;
+    const char *name = basename((char*)path);
+    int dirlen = name - path - 1;
+    return g_strdup_printf("%s/%.*s/%s/%s", ctx->fs_root,
+                           dirlen, path, VIRTFS_META_DIR, name);
 }
 
 static FILE *local_fopen(const char *path, const char *mode)
-- 
2.1.4




reply via email to

[Prev in Thread] Current Thread [Next in Thread]