9pfs-local: simplify/optimize local_mapped_attr_path()

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>
Signed-off-by: Aneesh Kumar K.V <aneesh.kumar@linux.vnet.ibm.com>
This commit is contained in:
Michael Tokarev 2015-03-12 09:52:30 +03:00 committed by Aneesh Kumar K.V
parent ee74801035
commit 1b6f85e2cb
1 changed files with 11 additions and 13 deletions

View File

@ -45,19 +45,17 @@
static char *local_mapped_attr_path(FsContext *ctx, const char *path) static char *local_mapped_attr_path(FsContext *ctx, const char *path)
{ {
char *dir_name; int dirlen;
char *tmp_path = g_strdup(path); const char *name = strrchr(path, '/');
char *base_name = basename(tmp_path); if (name) {
char *buffer; dirlen = name - path;
++name;
/* NULL terminate the directory */ } else {
dir_name = tmp_path; name = path;
*(base_name - 1) = '\0'; dirlen = 0;
}
buffer = g_strdup_printf("%s/%s/%s/%s", return g_strdup_printf("%s/%.*s/%s/%s", ctx->fs_root,
ctx->fs_root, dir_name, VIRTFS_META_DIR, base_name); dirlen, path, VIRTFS_META_DIR, name);
g_free(tmp_path);
return buffer;
} }
static FILE *local_fopen(const char *path, const char *mode) static FILE *local_fopen(const char *path, const char *mode)