From nobody Tue Feb 10 01:00:02 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zoho.com; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1496152188694379.93028923934685; Tue, 30 May 2017 06:49:48 -0700 (PDT) Received: from localhost ([::1]:53840 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dFhWt-0005nZ-9D for importer@patchew.org; Tue, 30 May 2017 09:49:47 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:40439) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dFhOi-0007Jp-J9 for qemu-devel@nongnu.org; Tue, 30 May 2017 09:41:24 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1dFhOe-00024a-IE for qemu-devel@nongnu.org; Tue, 30 May 2017 09:41:20 -0400 Received: from mx0b-001b2d01.pphosted.com ([148.163.158.5]:44942 helo=mx0a-001b2d01.pphosted.com) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1dFhOe-00024L-Bk for qemu-devel@nongnu.org; Tue, 30 May 2017 09:41:16 -0400 Received: from pps.filterd (m0098420.ppops.net [127.0.0.1]) by mx0b-001b2d01.pphosted.com (8.16.0.20/8.16.0.20) with SMTP id v4UDciKT136549 for ; Tue, 30 May 2017 09:41:15 -0400 Received: from e06smtp13.uk.ibm.com (e06smtp13.uk.ibm.com [195.75.94.109]) by mx0b-001b2d01.pphosted.com with ESMTP id 2as8gdkrqa-1 (version=TLSv1.2 cipher=AES256-SHA bits=256 verify=NOT) for ; Tue, 30 May 2017 09:41:15 -0400 Received: from localhost by e06smtp13.uk.ibm.com with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted for from ; Tue, 30 May 2017 14:41:13 +0100 Received: from b06cxnps4075.portsmouth.uk.ibm.com (9.149.109.197) by e06smtp13.uk.ibm.com (192.168.101.143) with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted; Tue, 30 May 2017 14:41:10 +0100 Received: from d06av26.portsmouth.uk.ibm.com (d06av26.portsmouth.uk.ibm.com [9.149.105.62]) by b06cxnps4075.portsmouth.uk.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id v4UDf91a39190716; Tue, 30 May 2017 13:41:09 GMT Received: from d06av26.portsmouth.uk.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 6C21BAE063; Tue, 30 May 2017 14:38:52 +0100 (BST) Received: from d06av26.portsmouth.uk.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 4C6A0AE04D; Tue, 30 May 2017 14:38:52 +0100 (BST) Received: from smtp.lab.toulouse-stg.fr.ibm.com (unknown [9.101.4.1]) by d06av26.portsmouth.uk.ibm.com (Postfix) with ESMTP; Tue, 30 May 2017 14:38:52 +0100 (BST) Received: from bahia.lan (icon-9-164-152-88.megacenter.de.ibm.com [9.164.152.88]) by smtp.lab.toulouse-stg.fr.ibm.com (Postfix) with ESMTP id C635F220178; Tue, 30 May 2017 15:41:08 +0200 (CEST) From: Greg Kurz To: qemu-devel@nongnu.org Date: Tue, 30 May 2017 15:40:39 +0200 X-Mailer: git-send-email 2.7.5 In-Reply-To: <1496151640-29919-1-git-send-email-groug@kaod.org> References: <1496151640-29919-1-git-send-email-groug@kaod.org> X-TM-AS-GCONF: 00 x-cbid: 17053013-0012-0000-0000-0000053DC536 X-IBM-AV-DETECTION: SAVI=unused REMOTE=unused XFE=unused x-cbparentid: 17053013-0013-0000-0000-000018A7750D Message-Id: <1496151640-29919-11-git-send-email-groug@kaod.org> X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10432:, , definitions=2017-05-30_09:, , signatures=0 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 spamscore=0 suspectscore=13 malwarescore=0 phishscore=0 adultscore=0 bulkscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.0.1-1703280000 definitions=main-1705300256 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 3.x [generic] [fuzzy] X-Received-From: 148.163.158.5 Subject: [Qemu-devel] [PULL v2 10/11] 9pfs: local: simplify file opening X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Peter Maydell , Greg Kurz , Stefan Hajnoczi Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" The logic to open a path currently sits between local_open_nofollow() and the relative_openat_nofollow() helper, which has no other user. For the sake of clarity, this patch moves all the code of the helper into its unique caller. While here we also: - drop the code to skip leading "/" because the backend isn't supposed to pass anything but relative paths without consecutive slashes. The assert() is kept because we really don't want a buggy backend to pass an absolute path to openat(). - use strchrnul() to get a simpler code. This is ok since virtfs is for linux+glibc hosts only. - don't dup() the initial directory and add an assert() to ensure we don't return the global mountfd to the caller. BTW, this would mean that the caller passed an empty path, which isn't supposed to happen either. Signed-off-by: Greg Kurz Reviewed-by: Eric Blake [groug: fixed typos in changelog] --- hw/9pfs/9p-local.c | 34 +++++++++++++++++++++++++++++----- hw/9pfs/9p-util.c | 43 ------------------------------------------- hw/9pfs/9p-util.h | 2 -- 3 files changed, 29 insertions(+), 50 deletions(-) diff --git a/hw/9pfs/9p-local.c b/hw/9pfs/9p-local.c index 68e92652ed73..ddc5038cff39 100644 --- a/hw/9pfs/9p-local.c +++ b/hw/9pfs/9p-local.c @@ -53,13 +53,37 @@ int local_open_nofollow(FsContext *fs_ctx, const char *= path, int flags, mode_t mode) { LocalData *data =3D fs_ctx->private; - - /* All paths are relative to the path data->mountfd points to */ - while (*path =3D=3D '/') { - path++; + int fd =3D data->mountfd; + + while (*path && fd !=3D -1) { + const char *c; + int next_fd; + char *head; + + /* Only relative paths without consecutive slashes */ + assert(*path !=3D '/'); + + head =3D g_strdup(path); + c =3D strchrnul(path, '/'); + if (*c) { + /* Intermediate path element */ + head[c - path] =3D 0; + path =3D c + 1; + next_fd =3D openat_dir(fd, head); + } else { + /* Rightmost path element */ + next_fd =3D openat_file(fd, head, flags, mode); + path =3D c; + } + g_free(head); + if (fd !=3D data->mountfd) { + close_preserve_errno(fd); + } + fd =3D next_fd; } =20 - return relative_openat_nofollow(data->mountfd, path, flags, mode); + assert(fd !=3D data->mountfd); + return fd; } =20 int local_opendir_nofollow(FsContext *fs_ctx, const char *path) diff --git a/hw/9pfs/9p-util.c b/hw/9pfs/9p-util.c index fdb4d5737635..f709c27a1fbd 100644 --- a/hw/9pfs/9p-util.c +++ b/hw/9pfs/9p-util.c @@ -14,49 +14,6 @@ #include "qemu/xattr.h" #include "9p-util.h" =20 -int relative_openat_nofollow(int dirfd, const char *path, int flags, - mode_t mode) -{ - int fd; - - fd =3D dup(dirfd); - if (fd =3D=3D -1) { - return -1; - } - - while (*path) { - const char *c; - int next_fd; - char *head; - - /* Only relative paths without consecutive slashes */ - assert(path[0] !=3D '/'); - - head =3D g_strdup(path); - c =3D strchr(path, '/'); - if (c) { - head[c - path] =3D 0; - next_fd =3D openat_dir(fd, head); - } else { - next_fd =3D openat_file(fd, head, flags, mode); - } - g_free(head); - if (next_fd =3D=3D -1) { - close_preserve_errno(fd); - return -1; - } - close(fd); - fd =3D next_fd; - - if (!c) { - break; - } - path =3D c + 1; - } - - return fd; -} - ssize_t fgetxattrat_nofollow(int dirfd, const char *filename, const char *= name, void *value, size_t size) { diff --git a/hw/9pfs/9p-util.h b/hw/9pfs/9p-util.h index 517027c52032..91299a24b8af 100644 --- a/hw/9pfs/9p-util.h +++ b/hw/9pfs/9p-util.h @@ -50,8 +50,6 @@ static inline int openat_file(int dirfd, const char *name= , int flags, return fd; } =20 -int relative_openat_nofollow(int dirfd, const char *path, int flags, - mode_t mode); ssize_t fgetxattrat_nofollow(int dirfd, const char *path, const char *name, void *value, size_t size); int fsetxattrat_nofollow(int dirfd, const char *path, const char *name, --=20 2.7.5