From nobody Mon Feb 9 00:56:17 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of redhat.com designates 209.132.183.25 as permitted sender) client-ip=209.132.183.25; envelope-from=libvir-list-bounces@redhat.com; helo=mx4-phx2.redhat.com; Authentication-Results: mx.zoho.com; spf=pass (zoho.com: domain of redhat.com designates 209.132.183.25 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; Return-Path: Received: from mx4-phx2.redhat.com (mx4-phx2.redhat.com [209.132.183.25]) by mx.zohomail.com with SMTPS id 1486477281109863.9762508598386; Tue, 7 Feb 2017 06:21:21 -0800 (PST) Received: from lists01.pubmisc.prod.ext.phx2.redhat.com (lists01.pubmisc.prod.ext.phx2.redhat.com [10.5.19.33]) by mx4-phx2.redhat.com (8.13.8/8.13.8) with ESMTP id v17EHOa2031300; Tue, 7 Feb 2017 09:17:24 -0500 Received: from int-mx14.intmail.prod.int.phx2.redhat.com (int-mx14.intmail.prod.int.phx2.redhat.com [10.5.11.27]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id v17EGl18016010 for ; Tue, 7 Feb 2017 09:16:47 -0500 Received: from beluga.usersys.redhat.com (dhcp129-94.brq.redhat.com [10.34.129.94]) by int-mx14.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id v17EGixS010056; Tue, 7 Feb 2017 09:16:46 -0500 From: Erik Skultety To: libvir-list@redhat.com Date: Tue, 7 Feb 2017 15:16:37 +0100 Message-Id: <6844fc9312d8fa1c5419448509673477a46bb01f.1486476642.git.eskultet@redhat.com> In-Reply-To: References: In-Reply-To: References: X-Scanned-By: MIMEDefang 2.68 on 10.5.11.27 X-loop: libvir-list@redhat.com Cc: Erik Skultety Subject: [libvirt] [PATCH 2/3] util: Introduce virFileComparePaths X-BeenThere: libvir-list@redhat.com X-Mailman-Version: 2.1.12 Precedence: junk List-Id: Development discussions about the libvirt library & tools List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Sender: libvir-list-bounces@redhat.com Errors-To: libvir-list-bounces@redhat.com X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Type: text/plain; charset="utf-8" So rather than comparing 2 paths (strings) as they are, which can very easily lead to unnecessary errors (e.g. in storage driver) that the paths are not the same when in fact they'd be e.g. just symlinks to the same location, we should put our best effort into resolving any symlinks and canonicalizing the path and only then compare the 2 paths. Signed-off-by: Erik Skultety --- src/libvirt_private.syms | 1 + src/util/virfile.c | 45 +++++++++++++++++++++++++++++++++++++++++++++ src/util/virfile.h | 2 ++ 3 files changed, 48 insertions(+) diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms index 8e994c7..06f3737 100644 --- a/src/libvirt_private.syms +++ b/src/libvirt_private.syms @@ -1575,6 +1575,7 @@ virFileActivateDirOverride; virFileBindMountDevice; virFileBuildPath; virFileClose; +virFileComparePaths; virFileCopyACLs; virFileDeleteTree; virFileDirectFdFlag; diff --git a/src/util/virfile.c b/src/util/virfile.c index bf8099e..b261632 100644 --- a/src/util/virfile.c +++ b/src/util/virfile.c @@ -3737,3 +3737,48 @@ virFileCopyACLs(const char *src, virFileFreeACLs(&acl); return ret; } + +/* + * virFileComparePaths: + * @p1: source path 1 + * @p2: source path 2 + * + * Compares two paths for equality. To do so, it first canonicalizes both = paths + * to resolve all symlinks and discard relative path components. If symlin= ks + * resolution or path canonicalization fails, plain string equality of @p1 + * and @p2 is performed. + * + * Returns 0 if the paths are equal, 1 if they're not or -1 in case of an + * error. + */ +int +virFileComparePaths(const char *p1, const char *p2) +{ + int ret =3D -1; + char *res1, *res2; + + res1 =3D res2 =3D NULL; + + /* Assume p1 and p2 are symlinks, so try to resolve and canonicalize t= hem. + * Canonicalization fails for example on file systems names like 'proc= ' or + * 'sysfs', since they're no real paths so fallback to plain string + * comparison. + */ + ignore_value(virFileResolveLink(p1, &res1)); + if (!res1 && VIR_STRDUP(res1, p1) < 0) + goto cleanup; + + ignore_value(virFileResolveLink(p2, &res2)); + if (!res2 && VIR_STRDUP(res2, p2) < 0) + goto cleanup; + + if (STREQ_NULLABLE(res1, res2)) + ret =3D 0; + else + ret =3D 1; + + cleanup: + VIR_FREE(res1); + VIR_FREE(res2); + return ret; +} diff --git a/src/util/virfile.h b/src/util/virfile.h index 0343acd..5822b29 100644 --- a/src/util/virfile.h +++ b/src/util/virfile.h @@ -331,4 +331,6 @@ void virFileFreeACLs(void **acl); =20 int virFileCopyACLs(const char *src, const char *dst); + +int virFileComparePaths(const char *p1, const char *p2); #endif /* __VIR_FILE_H */ --=20 2.10.2 -- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list