qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
* [Qemu-devel] [PATCH v2] 9pfs-local: simplify/optimize local_mapped_attr_path()
@ 2015-03-12  6:52 Michael Tokarev
  2015-03-12  9:53 ` Aneesh Kumar K.V
  0 siblings, 1 reply; 2+ messages in thread
From: Michael Tokarev @ 2015-03-12  6:52 UTC (permalink / raw)
  To: qemu-devel; +Cc: qemu-trivial, Michael Tokarev, Aneesh Kumar K.V

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

Do not use basename(3) because there are 2 versions of
this function which differs when argument ends with
slash character, use strrchr() instead so we have
consistent result.  This also makes sure the function
will do the right thing in corner cases (eg, empty
pathname is given), when basename(3) return entirely
another string.

Signed-off-by: Michael Tokarev <mjt@tls.msk.ru>
---
v2: switch from basename(3) to strrchr(3)

 hw/9pfs/virtio-9p-local.c | 24 +++++++++++-------------
 1 file changed, 11 insertions(+), 13 deletions(-)

diff --git a/hw/9pfs/virtio-9p-local.c b/hw/9pfs/virtio-9p-local.c
index 0d79150..c9e5c28 100644
--- a/hw/9pfs/virtio-9p-local.c
+++ b/hw/9pfs/virtio-9p-local.c
@@ -45,19 +45,17 @@
 
 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;
+    int dirlen;
+    const char *name = strrchr(path, '/');
+    if (name) {
+        dirlen = name - path;
+        ++name;
+    } else {
+        name = path;
+        dirlen = 0;
+    }
+    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

^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [Qemu-devel] [PATCH v2] 9pfs-local: simplify/optimize local_mapped_attr_path()
  2015-03-12  6:52 [Qemu-devel] [PATCH v2] 9pfs-local: simplify/optimize local_mapped_attr_path() Michael Tokarev
@ 2015-03-12  9:53 ` Aneesh Kumar K.V
  0 siblings, 0 replies; 2+ messages in thread
From: Aneesh Kumar K.V @ 2015-03-12  9:53 UTC (permalink / raw)
  To: Michael Tokarev, qemu-devel; +Cc: qemu-trivial

Michael Tokarev <mjt@tls.msk.ru> writes:

> Omit one unnecessary memory allocation for components
> of the path and create the resulting path directly given
> lengths of the components.
>
> Do not use basename(3) because there are 2 versions of
> this function which differs when argument ends with
> slash character, use strrchr() instead so we have
> consistent result.  This also makes sure the function
> will do the right thing in corner cases (eg, empty
> pathname is given), when basename(3) return entirely
> another string.
>
> Signed-off-by: Michael Tokarev <mjt@tls.msk.ru>

Applied.

Thanks
-aneesh

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2015-03-12 19:18 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2015-03-12  6:52 [Qemu-devel] [PATCH v2] 9pfs-local: simplify/optimize local_mapped_attr_path() Michael Tokarev
2015-03-12  9:53 ` Aneesh Kumar K.V

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).