From nobody Tue Sep 9 03:13:57 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of lists.libvirt.org designates 8.43.85.245 as permitted sender) client-ip=8.43.85.245; envelope-from=devel-bounces@lists.libvirt.org; helo=lists.libvirt.org; Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of lists.libvirt.org designates 8.43.85.245 as permitted sender) smtp.mailfrom=devel-bounces@lists.libvirt.org; dmarc=pass(p=reject dis=none) header.from=lists.libvirt.org ARC-Seal: i=1; a=rsa-sha256; t=1748519667; cv=none; d=zohomail.com; s=zohoarc; b=Z85pQjKerLrn2odl9+oRoDsVQ/l1Dbpn+/BOSjRaPqfSeJCCkqI7YKPhwPMqb2Qr3/vX0g323kG+ExSkMnATK5kvZ4Nz6k01owU2a479vFdjr/hbT6caBti8tKsjmmFYqdYivORfRopAFXrcUCrVUz3S1RrwKV+oWtaL4xqqgek= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1748519667; h=Content-Type:Content-Transfer-Encoding:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Reply-To:Reply-To:References:Subject:Subject:To:To:Message-Id:Cc; bh=CCcSPF1u4zSsFZrcWJQxCXJQeG+q2OnMHPe1TGp3huU=; b=HvTGAV+XFe4haBhAog4ByM7MLQ4A8pUKxFlN4ZG0tSaziD6E2dM7azWri3Yvgl8eoG41fb9urW2gv1ywpGRZx5NBRuwGgkSyESbyVrqj34nCu91CNwrFNxm1YkhhceLDXAig+uUgbxP3drL0GhLfNOblCN2Rsnt41FSfytnsE+0= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of lists.libvirt.org designates 8.43.85.245 as permitted sender) smtp.mailfrom=devel-bounces@lists.libvirt.org; dmarc=pass header.from= (p=reject dis=none) Return-Path: Received: from lists.libvirt.org (lists.libvirt.org [8.43.85.245]) by mx.zohomail.com with SMTPS id 1748519667075560.9728712413132; Thu, 29 May 2025 04:54:27 -0700 (PDT) Received: by lists.libvirt.org (Postfix, from userid 996) id 2D82C17CE; Thu, 29 May 2025 07:54:26 -0400 (EDT) Received: from lists.libvirt.org (localhost [IPv6:::1]) by lists.libvirt.org (Postfix) with ESMTP id B3385155E; Thu, 29 May 2025 07:53:32 -0400 (EDT) Received: by lists.libvirt.org (Postfix, from userid 996) id DD92015CC; Thu, 29 May 2025 07:53:28 -0400 (EDT) Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by lists.libvirt.org (Postfix) with ESMTPS id 2034913F5 for ; Thu, 29 May 2025 07:53:28 -0400 (EDT) Received: from mx-prod-mc-02.mail-002.prod.us-west-2.aws.redhat.com (ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-408-HRS9YnBZMBG71exLkeYOqQ-1; Thu, 29 May 2025 07:53:26 -0400 Received: from mx-prod-int-02.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-02.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.15]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-02.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 9827A1956089 for ; Thu, 29 May 2025 11:53:25 +0000 (UTC) Received: from orkuz (unknown [10.43.3.115]) by mx-prod-int-02.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 244DE1955D82 for ; Thu, 29 May 2025 11:53:24 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on lists.libvirt.org X-Spam-Level: X-Spam-Status: No, score=-0.8 required=5.0 tests=DKIM_INVALID,DKIM_SIGNED, MAILING_LIST_MULTI,RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H5, RCVD_IN_MSPIKE_WL,RCVD_IN_VALIDITY_RPBL_BLOCKED, RCVD_IN_VALIDITY_SAFE_BLOCKED,SPF_HELO_NONE autolearn=unavailable autolearn_force=no version=3.4.4 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1748519607; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=Wrphp3wMhjWE9gdbsVXhZ7UGAXgWBj5hUBCMAM6RKpY=; b=db79MVV6ODDP2rc4lcs0AMaZe1oWHt8YmvCMyCmN95sgRfpTg/k2+e0qQYUxLkkQtwxVeW x++RxYuEPRLGqLU9hWa2Nm55WvcRL+toEp5is0++pHMD0IFLiw/4e1u/9owBB0gyrOpXH3 Gr8zHKzy+JAdC4qzmSS6TUzvzkIOLgo= X-MC-Unique: HRS9YnBZMBG71exLkeYOqQ-1 X-Mimecast-MFC-AGG-ID: HRS9YnBZMBG71exLkeYOqQ_1748519605 To: devel@lists.libvirt.org Subject: [PATCH 2/3] util: Introduce virFileGetExistingParent helper Date: Thu, 29 May 2025 13:53:17 +0200 Message-ID: In-Reply-To: References: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.0 on 10.30.177.15 X-Mimecast-Spam-Score: 0 X-Mimecast-MFC-PROC-ID: gbBLPLQ_CglQaOxwLKYzS45fz0S2RxKqfxoFlG_D7jk_1748519605 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable Message-ID-Hash: TWEPC3ZZ2QAUFNFU2LMKPMTZTYNJMNDZ X-Message-ID-Hash: TWEPC3ZZ2QAUFNFU2LMKPMTZTYNJMNDZ X-MailFrom: jdenemar@redhat.com X-Mailman-Rule-Misses: dmarc-mitigation; no-senders; approved; emergency; loop; banned-address; member-moderation; header-match-config-1; header-match-config-2; header-match-config-3; header-match-devel.lists.libvirt.org-0; nonmember-moderation; administrivia; implicit-dest; max-recipients; max-size; news-moderation; no-subject; suspicious-header X-Mailman-Version: 3.2.2 Precedence: list List-Id: Development discussions about the libvirt library & tools Archived-At: List-Archive: List-Help: List-Post: List-Subscribe: List-Unsubscribe: From: Jiri Denemark via Devel Reply-To: Jiri Denemark X-ZohoMail-DKIM: fail (Header signature does not verify) X-ZM-MESSAGEID: 1748519668895116600 Content-Type: text/plain; charset="utf-8" From: Jiri Denemark The code from virFileIsSharedFSType which finds the longest existing path for a given input is separated into a new helper so that it can be reused elsewhere. Signed-off-by: Jiri Denemark --- Notes: If you wonder why the code is almost the same as the original one, but not exactly the same, it's because I rewrote the code using stat(), but than realized that mocking it for our tests would be a nightmare. So I replaced stat() with statfs() :-) src/util/virfile.c | 60 ++++++++++++++++++++++++---------------------- 1 file changed, 31 insertions(+), 29 deletions(-) diff --git a/src/util/virfile.c b/src/util/virfile.c index 242aa7ff88..ef1bc0bbf3 100644 --- a/src/util/virfile.c +++ b/src/util/virfile.c @@ -3557,6 +3557,34 @@ virFileIsSharedFsFUSE(const char *path, } =20 =20 +static char * +virFileGetExistingParent(const char *path) +{ + g_autofree char *dirpath =3D g_strdup(path); + struct statfs sb; + char *p =3D NULL; + + /* Try less and less of the path until we get to a directory we can st= at. + * Even if we don't have 'x' permission on any directory in the path o= n the + * NFS server (assuming it's NFS), we will be able to stat the mount p= oint. + */ + while (statfs(dirpath, &sb) < 0 && p !=3D dirpath) { + if (!(p =3D strrchr(dirpath, '/'))) { + virReportSystemError(EINVAL, + _("Invalid relative path '%1$s'"), path); + return NULL; + } + + if (p =3D=3D dirpath) + *(p + 1) =3D '\0'; + else + *p =3D '\0'; + } + + return g_steal_pointer(&dirpath); +} + + static const struct virFileSharedFsData virFileSharedFs[] =3D { { .fstype =3D VIR_FILE_SHFS_NFS, .magic =3D NFS_SUPER_MAGIC }, { .fstype =3D VIR_FILE_SHFS_GFS2, .magic =3D GFS2_MAGIC }, @@ -3576,40 +3604,14 @@ virFileIsSharedFSType(const char *path, unsigned int fstypes) { g_autofree char *dirpath =3D NULL; - char *p =3D NULL; struct statfs sb; - int statfs_ret; long long f_type =3D 0; size_t i; =20 - dirpath =3D g_strdup(path); + if (!(dirpath =3D virFileGetExistingParent(path))) + return -1; =20 - statfs_ret =3D statfs(dirpath, &sb); - - while ((statfs_ret < 0) && (p !=3D dirpath)) { - /* Try less and less of the path until we get to a - * directory we can stat. Even if we don't have 'x' - * permission on any directory in the path on the NFS - * server (assuming it's NFS), we will be able to stat the - * mount point, and that will properly tell us if the - * fstype is NFS. - */ - - if ((p =3D strrchr(dirpath, '/')) =3D=3D NULL) { - virReportSystemError(EINVAL, - _("Invalid relative path '%1$s'"), path); - return -1; - } - - if (p =3D=3D dirpath) - *(p+1) =3D '\0'; - else - *p =3D '\0'; - - statfs_ret =3D statfs(dirpath, &sb); - } - - if (statfs_ret < 0) { + if (statfs(dirpath, &sb) < 0) { virReportSystemError(errno, _("cannot determine filesystem for '%1$s'"), path); --=20 2.49.0