From nobody Sun Feb 8 10:48:48 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of redhat.com designates 207.211.31.81 as permitted sender) client-ip=207.211.31.81; envelope-from=libvir-list-bounces@redhat.com; helo=us-smtp-delivery-1.mimecast.com; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 207.211.31.81 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1577977085; cv=none; d=zohomail.com; s=zohoarc; b=QoXLQN7Jout9l3I/jbveQhY9I3OkwnSb5q/j6nwx3e72ZU36jIjYc+F2pXr/cMf0fCuTQRl8Ln91BSyAMJS8xh6yG7iAY8HWzEru8Bnxh3nq7gMQhRwp6p+SvdAUDhqRdlVn6N4s88eHV3yxRTOvQ+4YtJT2WFOn58zcOejY1xo= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1577977085; h=Content-Type:Content-Transfer-Encoding:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=n6nL737U5o75ZeV2Y0NazI/a2iWYjb3LcdU7uhst1fY=; b=amwvRuCU517gEa2SdqBSUzDFVmDniw8LQqOO0Z+nrAAIDbsxTe8cYBOaO2fPrJX1nKVOBdP3Mvr4Ju0xRy9hREeacv/N6c48SzXJnw+YfAX/015iSu/HMmL7+GwKnGPtNZ1XDr5hfxm8Hx8gEVBm01OOKTyTYD81OZ1y+nnIxKU= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 207.211.31.81 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass header.from= (p=none dis=none) header.from= Return-Path: Received: from us-smtp-delivery-1.mimecast.com (us-smtp-2.mimecast.com [207.211.31.81]) by mx.zohomail.com with SMTPS id 1577977085661988.4446563069516; Thu, 2 Jan 2020 06:58:05 -0800 (PST) Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-61-IAtOkjiZOCCHrrQ4npr8Qg-1; Thu, 02 Jan 2020 09:57:24 -0500 Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id B0461800D4C; Thu, 2 Jan 2020 14:57:18 +0000 (UTC) Received: from colo-mx.corp.redhat.com (colo-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.20]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 8AA0F7DB2E; Thu, 2 Jan 2020 14:57:18 +0000 (UTC) Received: from lists01.pubmisc.prod.ext.phx2.redhat.com (lists01.pubmisc.prod.ext.phx2.redhat.com [10.5.19.33]) by colo-mx.corp.redhat.com (Postfix) with ESMTP id 4999D18034EE; Thu, 2 Jan 2020 14:57:18 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id 002EvHMU005049 for ; Thu, 2 Jan 2020 09:57:17 -0500 Received: by smtp.corp.redhat.com (Postfix) id 3FF7F95304; Thu, 2 Jan 2020 14:57:17 +0000 (UTC) Received: from catbus.gsslab.fab.redhat.com (dhcp-43.gsslab.fab.redhat.com [10.33.9.43]) by smtp.corp.redhat.com (Postfix) with ESMTP id DD93360BF4; Thu, 2 Jan 2020 14:57:09 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1577977084; h=from:from:sender:sender: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:list-id:list-help: list-unsubscribe:list-subscribe:list-post; bh=n6nL737U5o75ZeV2Y0NazI/a2iWYjb3LcdU7uhst1fY=; b=OmE7d9Jo5vTnSXXd4FTUBJEtYOJYTrtrFvn1W+zdbR2jzz4hMrEkbXaE7l3bsa9zNRJ2XW Dcb2X5+nG/7+xkBJVdBPKyUFgXnnkNHOy/7UesmlSh1U/PDTd/k+KedRRn1P5kP489dRLG /KjEd6B1jwuJn2BIbNgg9lqcvz7Q060= From: =?UTF-8?q?Daniel=20P=2E=20Berrang=C3=A9?= To: libvir-list@redhat.com Date: Thu, 2 Jan 2020 14:53:52 +0000 Message-Id: <20200102145357.6724-19-berrange@redhat.com> In-Reply-To: <20200102145357.6724-1-berrange@redhat.com> References: <20200102145357.6724-1-berrange@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.12 X-loop: libvir-list@redhat.com Subject: [libvirt] [PATCH 18/23] util: use realpath/g_canonicalize_filename 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: , Sender: libvir-list-bounces@redhat.com Errors-To: libvir-list-bounces@redhat.com X-Scanned-By: MIMEDefang 2.79 on 10.5.11.11 X-MC-Unique: IAtOkjiZOCCHrrQ4npr8Qg-1 X-Mimecast-Spam-Score: 0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @redhat.com) The canonicalize_file_name(path) is equivalent to calling realpath(path, NULL). Passing NULL for the second arg of realpath is not standardized behaviour, however, Linux, FreeBSD > 6.4 and macOS > 10.5 all support this critical extension. This leaves Windows which doesn't provide realpath at all. The g_canonicalize_filename() function doesn't expand symlinks, so is not strictly equivalent to realpath() but is close enough for our Windows portability needs right now. Signed-off-by: Daniel P. Berrang=C3=A9 --- src/util/virfile.c | 9 ++++++++- tests/virfilemock.c | 12 +++++++----- 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/src/util/virfile.c b/src/util/virfile.c index 9787086f3d..4fc872ef1d 100644 --- a/src/util/virfile.c +++ b/src/util/virfile.c @@ -3256,7 +3256,14 @@ virFileSanitizePath(const char *path) char * virFileCanonicalizePath(const char *path) { - return canonicalize_file_name(path); /* exempt from syntax-check */ +#ifdef WIN32 + /* Does not resolve symlinks, only expands . & .. & repeated /. + * It will never fail, so sanitize errno to indicate success */ + errno =3D 0; + return g_canonicalize_filename(path, NULL); +#else + return realpath(path, NULL); /* exempt from syntax-check */ +#endif } =20 /** diff --git a/tests/virfilemock.c b/tests/virfilemock.c index 65685c6d26..802ee9b5f9 100644 --- a/tests/virfilemock.c +++ b/tests/virfilemock.c @@ -24,6 +24,7 @@ #if HAVE_LINUX_MAGIC_H # include #endif +#include =20 #include "virmock.h" #include "virstring.h" @@ -33,7 +34,7 @@ =20 static FILE *(*real_setmntent)(const char *filename, const char *type); static int (*real_statfs)(const char *path, struct statfs *buf); -static char *(*real_canonicalize_file_name)(const char *path); +static char *(*real_realpath)(const char *path, char *resolved); =20 =20 static void @@ -44,7 +45,7 @@ init_syms(void) =20 VIR_MOCK_REAL_INIT(setmntent); VIR_MOCK_REAL_INIT(statfs); - VIR_MOCK_REAL_INIT(canonicalize_file_name); + VIR_MOCK_REAL_INIT(realpath); } =20 =20 @@ -116,7 +117,7 @@ statfs_mock(const char *mtab, /* We don't need to do this in callers because real statfs(2) * does that for us. However, in mocked implementation we * need to do this. */ - if (!(canonPath =3D canonicalize_file_name(path))) + if (!(canonPath =3D realpath(path, NULL))) return -1; =20 while (getmntent_r(f, &mb, mntbuf, sizeof(mntbuf))) { @@ -178,7 +179,7 @@ statfs(const char *path, struct statfs *buf) =20 =20 char * -canonicalize_file_name(const char *path) +realpath(const char *path, char *resolved) { =20 init_syms(); @@ -187,6 +188,7 @@ canonicalize_file_name(const char *path) const char *p; char *ret; =20 + assert(resolved =3D=3D NULL); if ((p =3D STRSKIP(path, "/some/symlink"))) ret =3D g_strdup_printf("/gluster%s", p); else @@ -195,5 +197,5 @@ canonicalize_file_name(const char *path) return ret; } =20 - return real_canonicalize_file_name(path); + return real_realpath(path, resolved); } --=20 2.24.1 -- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list