From nobody Tue Feb 10 20:48:43 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 1493997663516733.4916494561584; Fri, 5 May 2017 08:21:03 -0700 (PDT) Received: from localhost ([::1]:47481 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1d6f2S-0001Rn-5q for importer@patchew.org; Fri, 05 May 2017 11:21:00 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:33534) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1d6f1G-0000mH-2A for qemu-devel@nongnu.org; Fri, 05 May 2017 11:19:47 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1d6f1C-00008x-QU for qemu-devel@nongnu.org; Fri, 05 May 2017 11:19:46 -0400 Received: from 17.mo5.mail-out.ovh.net ([46.105.56.132]:51145) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1d6f1C-00007H-JH for qemu-devel@nongnu.org; Fri, 05 May 2017 11:19:42 -0400 Received: from player786.ha.ovh.net (b6.ovh.net [213.186.33.56]) by mo5.mail-out.ovh.net (Postfix) with ESMTP id 17A90EB6DE for ; Fri, 5 May 2017 17:19:41 +0200 (CEST) Received: from bahia.lan (gar31-1-82-66-74-139.fbx.proxad.net [82.66.74.139]) (Authenticated sender: groug@kaod.org) by player786.ha.ovh.net (Postfix) with ESMTPA id 934CB80857; Fri, 5 May 2017 16:37:21 +0200 (CEST) From: Greg Kurz To: qemu-devel@nongnu.org Date: Fri, 05 May 2017 16:37:21 +0200 Message-ID: <149399504137.29022.4985100698645275511.stgit@bahia.lan> In-Reply-To: <149399500677.29022.12340124231191204194.stgit@bahia.lan> References: <149399500677.29022.12340124231191204194.stgit@bahia.lan> User-Agent: StGit/0.17.1-20-gc0b1b-dirty MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-Ovh-Tracer-Id: 10242311453152614728 X-VR-SPAMSTATE: OK X-VR-SPAMSCORE: -100 X-VR-SPAMCAUSE: gggruggvucftvghtrhhoucdtuddrfeeliedrieeggdekjecutefuodetggdotefrodftvfcurfhrohhfihhlvgemucfqggfjpdevjffgvefmvefgnecuuegrihhlohhuthemuceftddtnecusecvtfgvtghiphhivghnthhsucdlqddutddtmd X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 46.105.56.132 Subject: [Qemu-devel] [PATCH 3/5] 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: =?utf-8?q?L=C3=A9o?= Gaspard , Greg Kurz Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RSF_0 Z_629925259 SPT_0 All paths in the virtfs directory now start with "./" (except the virtfs root itself which is exactly "."). We hence don't need to skip leading '/' characters anymore, nor to handle the empty path case. Also, since virtfs will only ever be supported on linux+glibc hosts, we can use strchrnul() and come up with a much simplier code to walk through the path elements. And we don't need to dup() the passed directory fd. Signed-off-by: Greg Kurz --- hw/9pfs/9p-local.c | 5 ----- hw/9pfs/9p-util.c | 26 ++++++++++---------------- 2 files changed, 10 insertions(+), 21 deletions(-) diff --git a/hw/9pfs/9p-local.c b/hw/9pfs/9p-local.c index 92262f3c3e37..bb6e296df317 100644 --- a/hw/9pfs/9p-local.c +++ b/hw/9pfs/9p-local.c @@ -54,11 +54,6 @@ int local_open_nofollow(FsContext *fs_ctx, const char *p= ath, int flags, { LocalData *data =3D fs_ctx->private; =20 - /* All paths are relative to the path data->mountfd points to */ - while (*path =3D=3D '/') { - path++; - } - return relative_openat_nofollow(data->mountfd, path, flags, mode); } =20 diff --git a/hw/9pfs/9p-util.c b/hw/9pfs/9p-util.c index fdb4d5737635..e46399a9119d 100644 --- a/hw/9pfs/9p-util.c +++ b/hw/9pfs/9p-util.c @@ -17,14 +17,11 @@ int relative_openat_nofollow(int dirfd, const char *path, int flags, mode_t mode) { - int fd; + int fd =3D dirfd; =20 - fd =3D dup(dirfd); - if (fd =3D=3D -1) { - return -1; - } + assert(*path); =20 - while (*path) { + while (*path && fd !=3D -1) { const char *c; int next_fd; char *head; @@ -33,25 +30,22 @@ int relative_openat_nofollow(int dirfd, const char *pat= h, int flags, assert(path[0] !=3D '/'); =20 head =3D g_strdup(path); - c =3D strchr(path, '/'); - if (c) { + 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 (next_fd =3D=3D -1) { + if (dirfd !=3D fd) { close_preserve_errno(fd); - return -1; } - close(fd); fd =3D next_fd; - - if (!c) { - break; - } - path =3D c + 1; } =20 return fd;