From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([2001:4830:134:3::10]:38197) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1YXRzX-0002pT-9v for qemu-devel@nongnu.org; Mon, 16 Mar 2015 06:11:28 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1YXRzR-0005a6-GH for qemu-devel@nongnu.org; Mon, 16 Mar 2015 06:11:23 -0400 Received: from e28smtp07.in.ibm.com ([122.248.162.7]:35102) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1YXRzQ-0005ZP-SC for qemu-devel@nongnu.org; Mon, 16 Mar 2015 06:11:17 -0400 Received: from /spool/local by e28smtp07.in.ibm.com with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted for from ; Mon, 16 Mar 2015 15:41:13 +0530 Received: from d28relay01.in.ibm.com (d28relay01.in.ibm.com [9.184.220.58]) by d28dlp02.in.ibm.com (Postfix) with ESMTP id ECDA7394005E for ; Mon, 16 Mar 2015 15:41:09 +0530 (IST) Received: from d28av01.in.ibm.com (d28av01.in.ibm.com [9.184.220.63]) by d28relay01.in.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id t2GAB3Ol33226970 for ; Mon, 16 Mar 2015 15:41:04 +0530 Received: from d28av01.in.ibm.com (localhost [127.0.0.1]) by d28av01.in.ibm.com (8.14.4/8.14.4/NCO v10.0 AVout) with ESMTP id t2GAB2VT024590 for ; Mon, 16 Mar 2015 15:41:03 +0530 From: "Aneesh Kumar K.V" Date: Mon, 16 Mar 2015 15:39:38 +0530 Message-Id: <1426500583-15712-2-git-send-email-aneesh.kumar@linux.vnet.ibm.com> In-Reply-To: <1426500583-15712-1-git-send-email-aneesh.kumar@linux.vnet.ibm.com> References: <1426500583-15712-1-git-send-email-aneesh.kumar@linux.vnet.ibm.com> Subject: [Qemu-devel] [PATCH 1/6] 9pfs-local: simplify/optimize local_mapped_attr_path() List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: anthony@codemonkey.ws, peter.maydell@linaro.org Cc: Michael Tokarev , qemu-devel@nongnu.org, "Aneesh Kumar K.V" From: Michael Tokarev 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 Signed-off-by: Aneesh Kumar K.V --- 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 d05c91779f2c..84efb31cfec4 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.0