From patchwork Thu Apr 28 01:10:53 2011 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: malahal@us.ibm.com X-Patchwork-Id: 93143 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [140.186.70.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (Client did not present a certificate) by ozlabs.org (Postfix) with ESMTPS id 11130B6F4A for ; Thu, 28 Apr 2011 11:11:51 +1000 (EST) Received: from localhost ([::1]:54985 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1QFFlk-0002Km-89 for incoming@patchwork.ozlabs.org; Wed, 27 Apr 2011 21:11:48 -0400 Received: from eggs.gnu.org ([140.186.70.92]:37714) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1QFFld-0002KS-3P for qemu-devel@nongnu.org; Wed, 27 Apr 2011 21:11:42 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1QFFlc-0007mg-89 for qemu-devel@nongnu.org; Wed, 27 Apr 2011 21:11:41 -0400 Received: from e31.co.us.ibm.com ([32.97.110.149]:51516) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1QFFlc-0007mD-0N for qemu-devel@nongnu.org; Wed, 27 Apr 2011 21:11:40 -0400 Received: from d03relay01.boulder.ibm.com (d03relay01.boulder.ibm.com [9.17.195.226]) by e31.co.us.ibm.com (8.14.4/8.13.1) with ESMTP id p3S0tXbO006826 for ; Wed, 27 Apr 2011 18:55:33 -0600 Received: from d03av01.boulder.ibm.com (d03av01.boulder.ibm.com [9.17.195.167]) by d03relay01.boulder.ibm.com (8.13.8/8.13.8/NCO v10.0) with ESMTP id p3S1BTmG145776 for ; Wed, 27 Apr 2011 19:11:30 -0600 Received: from d03av01.boulder.ibm.com (loopback [127.0.0.1]) by d03av01.boulder.ibm.com (8.14.4/8.13.1/NCO v10.0 AVout) with ESMTP id p3S1BTfQ003717 for ; Wed, 27 Apr 2011 19:11:29 -0600 Received: from malahal (malahal.beaverton.ibm.com [9.47.25.235]) by d03av01.boulder.ibm.com (8.14.4/8.13.1/NCO v10.0 AVin) with ESMTP id p3S1BS4E003695; Wed, 27 Apr 2011 19:11:29 -0600 Received: by malahal (Postfix, from userid 155450) id D737AC006C; Wed, 27 Apr 2011 18:11:27 -0700 (PDT) From: Malahal Naineni To: qemu-devel@nongnu.org Date: Wed, 27 Apr 2011 18:10:53 -0700 Message-Id: <1303953053-31185-1-git-send-email-malahal@us.ibm.com> X-Mailer: git-send-email 1.7.0.4 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.6, seldom 2.4 (older, 4) X-Received-From: 32.97.110.149 Cc: Malahal Naineni Subject: [Qemu-devel] [PATCH] Stop renaming files with similar name! X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org v9fs_complete_rename() mistakenly renames files with similar name as we don't check if the matched name is really an offspring. Signed-off-by: Malahal Naineni --- hw/9pfs/virtio-9p.c | 24 ++++++++++++++++++++---- 1 files changed, 20 insertions(+), 4 deletions(-) diff --git a/hw/9pfs/virtio-9p.c b/hw/9pfs/virtio-9p.c index 2227f7d..64b0e11 100644 --- a/hw/9pfs/virtio-9p.c +++ b/hw/9pfs/virtio-9p.c @@ -425,6 +425,22 @@ static void v9fs_string_copy(V9fsString *lhs, V9fsString *rhs) v9fs_string_sprintf(lhs, "%s", rhs->data); } +/* + * Return TRUE if s1 is an ancestor of s2. + * + * E.g. "a/b" is an ancestor of "a/b/c" but not of "a/bc/d". + * As a special case, We treat s1 as ancestor of s2 if they are same! + */ +static int v9fs_path_is_ancestor(V9fsString *s1, V9fsString *s2) +{ + if (!strncmp(s1->data, s2->data, s1->size)) { + if (s2->data[s1->size] == '\0' || s2->data[s1->size] == '/') { + return 1; + } + } + return 0; +} + static size_t v9fs_string_size(V9fsString *str) { return str->size; @@ -2807,13 +2823,13 @@ static int v9fs_complete_rename(V9fsState *s, V9fsRenameState *vs) for (fidp = s->fid_list; fidp; fidp = fidp->next) { if (vs->fidp == fidp) { /* - * we replace name of this fid towards the end - * so that our below strcmp will work + * we replace name of this fid towards the end so + * that our below v9fs_path_is_ancestor check will + * work */ continue; } - if (!strncmp(vs->fidp->path.data, fidp->path.data, - strlen(vs->fidp->path.data))) { + if (v9fs_path_is_ancestor(&vs->fidp->path, &fidp->path)) { /* replace the name */ v9fs_fix_path(&fidp->path, &vs->name, strlen(vs->fidp->path.data));