From nobody Thu May 16 05:52:52 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of redhat.com designates 216.205.24.124 as permitted sender) client-ip=216.205.24.124; envelope-from=libvir-list-bounces@redhat.com; helo=us-smtp-delivery-124.mimecast.com; Authentication-Results: mx.zohomail.com; spf=pass (zohomail.com: domain of redhat.com designates 216.205.24.124 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=fail(p=none dis=none) header.from=gmail.com ARC-Seal: i=1; a=rsa-sha256; t=1622425740; cv=none; d=zohomail.com; s=zohoarc; b=J7Z8yDjy+Mc6RJ8R6UEu8+y4NOVbnq2a0ZkKPUc9cdHhhK4rvIL5A43PdTaFLbsXlvreh7L6AImMmSlI9iX7BOEWrEycH91oDUJ2WaDar8k1Xv0kb5DcThwO5z4Wr4hN82dTXIaIcegvIAP5ljCMBrqIz31Hk5MMYELpWlBHfWs= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1622425740; 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=1EBZjwFX9o6a0CDy0lclNb1pM3OXVIhjNebXzFIyYp4=; b=Gdjr9lZXCE6kixPiElwZQsgxLRWDRQtZ0hTCP9P5FZmIVqk7K/72wiKlOXnjVfVT8F2H/td7fb3KB1Ab0uEzrOJ6ES8ITNl9LBgeVWyISWgzApBycJn+DqUdNM+ztcoG85lU7egIQMWSpFf0r1utVjhieTORNGFImeYNH9FOuVg= ARC-Authentication-Results: i=1; mx.zohomail.com; spf=pass (zohomail.com: domain of redhat.com designates 216.205.24.124 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [216.205.24.124]) by mx.zohomail.com with SMTPS id 1622425740292125.92492688804123; Sun, 30 May 2021 18:49:00 -0700 (PDT) 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-581-F0ghxXivOcaKhFCq8IFWOg-1; Sun, 30 May 2021 21:48:56 -0400 Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.phx2.redhat.com [10.5.11.23]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id E5DB7107ACE8; Mon, 31 May 2021 01:48:50 +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 2B73E2BCCA; Mon, 31 May 2021 01:48:50 +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 77E731800FFC; Mon, 31 May 2021 01:48:46 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.rdu2.redhat.com [10.11.54.4]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id 14V1mfis007342 for ; Sun, 30 May 2021 21:48:41 -0400 Received: by smtp.corp.redhat.com (Postfix) id A3FE5200D8CC; Mon, 31 May 2021 01:48:41 +0000 (UTC) Received: from mimecast-mx02.redhat.com (mimecast03.extmail.prod.ext.rdu2.redhat.com [10.11.55.19]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 9F582200B69D for ; Mon, 31 May 2021 01:48:41 +0000 (UTC) Received: from us-smtp-1.mimecast.com (us-smtp-delivery-1.mimecast.com [207.211.31.120]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 8876780D0E4 for ; Mon, 31 May 2021 01:48:41 +0000 (UTC) Received: from mail-pf1-f174.google.com (mail-pf1-f174.google.com [209.85.210.174]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-151-RwHpU91XNQqi5axaezK3uw-1; Sun, 30 May 2021 21:48:38 -0400 Received: by mail-pf1-f174.google.com with SMTP id d78so7838037pfd.10 for ; Sun, 30 May 2021 18:48:38 -0700 (PDT) Received: from localhost ([103.138.53.238]) by smtp.gmail.com with ESMTPSA id t1sm4384742pfe.61.2021.05.30.18.48.36 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 30 May 2021 18:48:36 -0700 (PDT) X-MC-Unique: F0ghxXivOcaKhFCq8IFWOg-1 X-MC-Unique: RwHpU91XNQqi5axaezK3uw-1 X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=1EBZjwFX9o6a0CDy0lclNb1pM3OXVIhjNebXzFIyYp4=; b=qEPkXjIXuKMGubHOHcbKsnssnFIyezLmnHfMkGDsCGwfG1fFTSOhGyW1UTH8iruSrW KznS8kQ3598Pl/7FyMXup6N6/ksIEjCwXFfCq8hju7Roay1VFB7zsMbW7VGGm4/Y5Jbs T6IBNoRrMyESh5+oqS9Vw8fK1Y6GtKM8ZPazGoooBQ6QJ1Mzk7TBYp6pnS8GPCDGqsMc OrIXNTacsbqsqubEp5LbVC7vzk7d9WVjxjS/1ATax4TBjZr4hfsmgD5AD5uTP+ykv9SZ YGWTb2TYmf4t5XmMrjKcpqNJjSQNPpzGTfORtbA+pvtxmMpesGC11jNe0RDtDpKn++Yt USYQ== X-Gm-Message-State: AOAM530Z3KpZ6hxtqL60vvOEnVoa2sEkKO2BnUXl4vX3ipyQXPKXUYfi Gbj7s3vdfD5tbuwjvnmybbWmJlg04BBjANAm59c= X-Google-Smtp-Source: ABdhPJwj9T+oHkSuBZM3HNHnlfTc3tH8TUnolxUiPo92eu5euqLPWwrNU+Uvq0JGku5GRnrbo+8gBQ== X-Received: by 2002:a63:1316:: with SMTP id i22mr2794126pgl.150.1622425717255; Sun, 30 May 2021 18:48:37 -0700 (PDT) From: Luke Yue To: libvir-list@redhat.com Subject: [PATCH 1/3] virfile: Use g_build_filename() when building paths Date: Mon, 31 May 2021 09:48:22 +0800 Message-Id: <20210531014824.923662-2-lukedyue@gmail.com> In-Reply-To: <20210531014824.923662-1-lukedyue@gmail.com> References: <20210531014824.923662-1-lukedyue@gmail.com> MIME-Version: 1.0 X-Mimecast-Impersonation-Protect: Policy=CLT - Impersonation Protection Definition; Similar Internal Domain=false; Similar Monitored External Domain=false; Custom External Domain=false; Mimecast External Domain=false; Newly Observed Domain=false; Internal User Name=false; Custom Display Name List=false; Reply-to Address Mismatch=false; Targeted Threat Dictionary=false; Mimecast Threat Dictionary=false; Custom Threat Dictionary=false X-Scanned-By: MIMEDefang 2.78 on 10.11.54.4 X-loop: libvir-list@redhat.com 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.84 on 10.5.11.23 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=libvir-list-bounces@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" The g_build_filename() would decide which separator to use instead of hardcoding in g_strdup_printf(). Related issue: https://gitlab.com/libvirt/libvirt/-/issues/12 Signed-off-by: Luke Yue Reviewed-by: Martin Kletzander --- src/util/virfile.c | 25 ++++++++++++++----------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/src/util/virfile.c b/src/util/virfile.c index 03a7725dd3..dc2834fd1c 100644 --- a/src/util/virfile.c +++ b/src/util/virfile.c @@ -733,7 +733,7 @@ static int virFileLoopDeviceOpenSearch(char **dev_name) !g_ascii_isdigit(de->d_name[4])) continue; =20 - looppath =3D g_strdup_printf("/dev/%s", de->d_name); + looppath =3D g_build_filename("/dev", de->d_name, NULL); =20 VIR_DEBUG("Checking up on device %s", looppath); if ((fd =3D open(looppath, O_RDWR)) < 0) { @@ -860,7 +860,7 @@ virFileNBDDeviceIsBusy(const char *dev_name) { g_autofree char *path =3D NULL; =20 - path =3D g_strdup_printf(SYSFS_BLOCK_DIR "/%s/pid", dev_name); + path =3D g_build_filename(SYSFS_BLOCK_DIR, dev_name, "pid", NULL); =20 if (!virFileExists(path)) { if (errno =3D=3D ENOENT) @@ -893,7 +893,7 @@ virFileNBDDeviceFindUnused(void) return NULL; =20 if (rv =3D=3D 0) - return g_strdup_printf("/dev/%s", de->d_name); + return g_build_filename("/dev", de->d_name, NULL); } } if (direrr < 0) @@ -1028,7 +1028,7 @@ int virFileDeleteTree(const char *dir) g_autofree char *filepath =3D NULL; GStatBuf sb; =20 - filepath =3D g_strdup_printf("%s/%s", dir, de->d_name); + filepath =3D g_build_filename(dir, de->d_name, NULL); =20 if (g_lstat(filepath, &sb) < 0) { virReportSystemError(errno, _("Cannot access '%s'"), @@ -1568,7 +1568,7 @@ virFileRelLinkPointsTo(const char *directory, checkLink); return -1; } - candidate =3D g_strdup_printf("%s/%s", directory, checkLink); + candidate =3D g_build_filename(directory, checkLink, NULL); return virFileLinkPointsTo(candidate, checkDest); } =20 @@ -1705,7 +1705,7 @@ virFindFileInPath(const char *file) return NULL; =20 for (pathiter =3D paths; *pathiter; pathiter++) { - g_autofree char *fullpath =3D g_strdup_printf("%s/%s", *pathiter, = file); + g_autofree char *fullpath =3D g_build_filename(*pathiter, file, NU= LL); if (virFileIsExecutable(fullpath)) return g_steal_pointer(&fullpath); } @@ -1754,6 +1754,7 @@ virFileFindResourceFull(const char *filename, char *ret =3D NULL; const char *envval =3D envname ? getenv(envname) : NULL; const char *path; + g_autofree char *fullFilename =3D NULL; =20 if (!prefix) prefix =3D ""; @@ -1767,7 +1768,8 @@ virFileFindResourceFull(const char *filename, else path =3D installdir; =20 - ret =3D g_strdup_printf("%s/%s%s%s", path, prefix, filename, suffix); + fullFilename =3D g_strdup_printf("%s%s%s", prefix, filename, suffix); + ret =3D g_build_filename(path, fullFilename, NULL); =20 VIR_DEBUG("Resolved '%s' to '%s'", filename, ret); return ret; @@ -2986,7 +2988,7 @@ int virFileChownFiles(const char *name, while ((direrr =3D virDirRead(dir, &ent, name)) > 0) { g_autofree char *path =3D NULL; =20 - path =3D g_strdup_printf("%s/%s", name, ent->d_name); + path =3D g_build_filename(name, ent->d_name, NULL); =20 if (!virFileIsRegular(path)) continue; @@ -3048,9 +3050,10 @@ virFileBuildPath(const char *dir, const char *name, = const char *ext) char *path; =20 if (ext =3D=3D NULL) { - path =3D g_strdup_printf("%s/%s", dir, name); + path =3D g_build_filename(dir, name, NULL); } else { - path =3D g_strdup_printf("%s/%s%s", dir, name, ext); + g_autofree char *extName =3D g_strdup_printf("%s%s", name, ext); + path =3D g_build_filename(dir, extName, NULL); } =20 return path; @@ -3158,7 +3161,7 @@ virFileAbsPath(const char *path, char **abspath) } else { g_autofree char *buf =3D g_get_current_dir(); =20 - *abspath =3D g_strdup_printf("%s/%s", buf, path); + *abspath =3D g_build_filename(buf, path, NULL); } =20 return 0; --=20 2.31.1 From nobody Thu May 16 05:52:52 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of redhat.com designates 216.205.24.124 as permitted sender) client-ip=216.205.24.124; envelope-from=libvir-list-bounces@redhat.com; helo=us-smtp-delivery-124.mimecast.com; Authentication-Results: mx.zohomail.com; spf=pass (zohomail.com: domain of redhat.com designates 216.205.24.124 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=fail(p=none dis=none) header.from=gmail.com ARC-Seal: i=1; a=rsa-sha256; t=1622425757; cv=none; d=zohomail.com; s=zohoarc; b=L5qjIJ08xM+N/ESLm6YXFVgvAydbVVuIpzM8rMTM8cP/ZRNnUHEMV8rxlQ3NNjEd+UvKDitHa1yjZV8xkaGyZAFwBgxcvxkCxjjSpZp6CoQkjGkoEeJ0vtBSqPeXYBVx9aaexjx/HRvnsUI4rye5f/4S21U/VH84bs3M/jXBxxw= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1622425757; 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=J+kNVVJXPbKkjTUdSEP5uyyfWFf4DaXwsZ5U7F3NLxw=; b=G1EAYfaV5xUguUCJdbTsR8qAR5aimiEiioLYL2b4vMxqy3bLE30QnU9QZCxyCJevUg79eA2FWPO7BupSKPutOQYvycbCTUkiwNApKk2Sl/w3FCJa8gTjkCJPGgSkXBby8Qb+PW+q52hw0stG7dRHDqFNoSlw4jo/nq2kJtsXMbw= ARC-Authentication-Results: i=1; mx.zohomail.com; spf=pass (zohomail.com: domain of redhat.com designates 216.205.24.124 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [216.205.24.124]) by mx.zohomail.com with SMTPS id 162242575783257.765973872991935; Sun, 30 May 2021 18:49:17 -0700 (PDT) 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-245-AwClidT4MwijeP88us61-Q-1; Sun, 30 May 2021 21:49:14 -0400 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.phx2.redhat.com [10.5.11.13]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 6704C180FD61; Mon, 31 May 2021 01:49:08 +0000 (UTC) Received: from colo-mx.corp.redhat.com (colo-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.21]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 487B0687C7; Mon, 31 May 2021 01:49:08 +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 0D3095534B; Mon, 31 May 2021 01:49:08 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.rdu2.redhat.com [10.11.54.4]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id 14V1mh4C007357 for ; Sun, 30 May 2021 21:48:44 -0400 Received: by smtp.corp.redhat.com (Postfix) id D95F8200D8CE; Mon, 31 May 2021 01:48:43 +0000 (UTC) Received: from mimecast-mx02.redhat.com (mimecast06.extmail.prod.ext.rdu2.redhat.com [10.11.55.22]) by smtp.corp.redhat.com (Postfix) with ESMTPS id D4EB4200D8CD for ; Mon, 31 May 2021 01:48:43 +0000 (UTC) Received: from us-smtp-1.mimecast.com (us-smtp-2.mimecast.com [207.211.31.81]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id BC1B11857F28 for ; Mon, 31 May 2021 01:48:43 +0000 (UTC) Received: from mail-pj1-f52.google.com (mail-pj1-f52.google.com [209.85.216.52]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-575-LeMFpAquNDOwjez2rVlsIA-1; Sun, 30 May 2021 21:48:40 -0400 Received: by mail-pj1-f52.google.com with SMTP id g6-20020a17090adac6b029015d1a9a6f1aso9543097pjx.1 for ; Sun, 30 May 2021 18:48:40 -0700 (PDT) Received: from localhost ([103.138.53.238]) by smtp.gmail.com with ESMTPSA id z6sm10136471pgp.89.2021.05.30.18.48.39 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 30 May 2021 18:48:39 -0700 (PDT) X-MC-Unique: AwClidT4MwijeP88us61-Q-1 X-MC-Unique: LeMFpAquNDOwjez2rVlsIA-1 X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=J+kNVVJXPbKkjTUdSEP5uyyfWFf4DaXwsZ5U7F3NLxw=; b=fVeEiYIDhVsynk43dx90RqCA6ieflonSjEA/CdNT0FLmZJE6dWK7nEbJ3iO/oWDqSA faGhem4nrjrcSdNjPdwwZxsQFrDJMP4RmGrs9nVXDdsxguGSXRn09CiZSw0XHxdwTMm/ 1nD7FgcgbHyAVLwVPTuKlprW97X2YT95FkZd0HhyrBOlayBTM9QwbDHoPWq4txAZs4iJ TWHwMyLDU4FGu5369Kf66g4xfBQ11cSbGWK8ZOBBSRqaN5NUQIr9rK0HtMmePju2lL49 xHPSWOkYIVbhJvbvMBNIBpqZBNoNL4JtQ4PVArHwsvPOgjkCYMJadp1U3uf9F4SKtyu/ oucA== X-Gm-Message-State: AOAM532gRF5ugHT/yJOj3canhCz5MAozUeIVa3bxLR6Ceg/HHsfHJhVB V90JnFyno3Ntk0tN2/zoAFBhjTa7BuKZxE0u/cg= X-Google-Smtp-Source: ABdhPJxvyeNw+CxEwxrsULgc1EoDqw6mZYdCHnIy5nYGYI6i5vK0dcq6oEKwD6AcjR9zwjyrzzFRXg== X-Received: by 2002:a17:90a:5b0c:: with SMTP id o12mr9242394pji.108.1622425719694; Sun, 30 May 2021 18:48:39 -0700 (PDT) From: Luke Yue To: libvir-list@redhat.com Subject: [PATCH 2/3] virfile: Simplify virFindFileInPath() with g_find_program_in_path() Date: Mon, 31 May 2021 09:48:23 +0800 Message-Id: <20210531014824.923662-3-lukedyue@gmail.com> In-Reply-To: <20210531014824.923662-1-lukedyue@gmail.com> References: <20210531014824.923662-1-lukedyue@gmail.com> MIME-Version: 1.0 X-Mimecast-Impersonation-Protect: Policy=CLT - Impersonation Protection Definition; Similar Internal Domain=false; Similar Monitored External Domain=false; Custom External Domain=false; Mimecast External Domain=false; Newly Observed Domain=false; Internal User Name=false; Custom Display Name List=false; Reply-to Address Mismatch=false; Targeted Threat Dictionary=false; Mimecast Threat Dictionary=false; Custom Threat Dictionary=false X-Scanned-By: MIMEDefang 2.78 on 10.11.54.4 X-loop: libvir-list@redhat.com 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.13 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=libvir-list-bounces@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" The behavior is a little bit different when using g_find_program_in_path(), when the `file` contains a relative path, the original function would return a absolute path, but the g_find_program_in_path() would probably return a relative one. Other conditions would be fine, so just use g_find_program_in_path() to simplify the implementation. Note that when PATH is not set, g_find_program_in_path() will search in `/bin:/usr/bin:.`. Signed-off-by: Luke Yue --- src/util/virfile.c | 42 ++++++------------------------------------ 1 file changed, 6 insertions(+), 36 deletions(-) diff --git a/src/util/virfile.c b/src/util/virfile.c index dc2834fd1c..0d1c2ba518 100644 --- a/src/util/virfile.c +++ b/src/util/virfile.c @@ -1662,27 +1662,14 @@ virFileIsLink(const char *linkpath) char * virFindFileInPath(const char *file) { - const char *origpath =3D NULL; - g_auto(GStrv) paths =3D NULL; - char **pathiter; - if (file =3D=3D NULL) return NULL; =20 - /* if we are passed an absolute path (starting with /), return a - * copy of that path, after validating that it is executable - */ - if (g_path_is_absolute(file)) { - if (!virFileIsExecutable(file)) - return NULL; - - return g_strdup(file); - } - - /* If we are passed an anchored path (containing a /), then there - * is no path search - it must exist in the current directory + /* If we are passed an anchored path (containing a /), and it's not an + * absolute path then there is no path search - it must exist in the c= urrent + * directory */ - if (strchr(file, '/')) { + if (!g_path_is_absolute(file) && strchr(file, '/')) { char *abspath =3D NULL; =20 if (!virFileIsExecutable(file)) @@ -1692,25 +1679,8 @@ virFindFileInPath(const char *file) return abspath; } =20 - /* copy PATH env so we can tweak it */ - origpath =3D getenv("PATH"); - if (!origpath) - origpath =3D "/bin:/usr/bin"; - - /* for each path segment, append the file to search for and test for - * it. return it if found. - */ - - if (!(paths =3D g_strsplit(origpath, ":", 0))) - return NULL; - - for (pathiter =3D paths; *pathiter; pathiter++) { - g_autofree char *fullpath =3D g_build_filename(*pathiter, file, NU= LL); - if (virFileIsExecutable(fullpath)) - return g_steal_pointer(&fullpath); - } - - return NULL; + /* Otherwise, just use g_find_program_in_path() */ + return g_find_program_in_path(file); } =20 =20 --=20 2.31.1 From nobody Thu May 16 05:52:52 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of redhat.com designates 170.10.133.124 as permitted sender) client-ip=170.10.133.124; envelope-from=libvir-list-bounces@redhat.com; helo=us-smtp-delivery-124.mimecast.com; Authentication-Results: mx.zohomail.com; spf=pass (zohomail.com: domain of redhat.com designates 170.10.133.124 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=fail(p=none dis=none) header.from=gmail.com ARC-Seal: i=1; a=rsa-sha256; t=1622425740; cv=none; d=zohomail.com; s=zohoarc; b=I/DdwqGOYwzCcbbk1BJQtP9BIGsp7906W7wI2MPbQlzQBPkfmE68l4j4nsTdzXIDk3T1hgMSV+qhYF6S8Lqv8qF5izNZIGHvYqIZr25YHr/nr4umQexX8lXtkSXjDOwvES0YON9sCRrwjKTEUYXEqKmiejh4vEiopz/j2hHBLe4= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1622425740; 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=LoEX4OCcmLeZHruUwj2C+d//tcSZ/1rTW5qoX+WLMxU=; b=k/e35kpzHTioPmEu36QDYgW5UGMRdqUIVsrRWR7DB36efcGw5XRouwwBhDU8UnV5BJvt2ALvuz9z0b6+UkydYsrv2dNLGN0ZECYWp3Cts+l69QlBb92Y5+NNIN4cYzIeKQfRkYRkaBWGxOxFzFfWUIoTuVNBntyhakwoskYmIHY= ARC-Authentication-Results: i=1; mx.zohomail.com; spf=pass (zohomail.com: domain of redhat.com designates 170.10.133.124 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by mx.zohomail.com with SMTPS id 1622425740684844.5775356956883; Sun, 30 May 2021 18:49:00 -0700 (PDT) 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-202-7WM1wgeQPve8KuP2eCKGqA-1; Sun, 30 May 2021 21:48:57 -0400 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id C762D107ACE3; Mon, 31 May 2021 01:48:52 +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 7D7A15DEAD; Mon, 31 May 2021 01:48:52 +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 38BB6180B465; Mon, 31 May 2021 01:48:52 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.rdu2.redhat.com [10.11.54.3]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id 14V1moaV007384 for ; Sun, 30 May 2021 21:48:50 -0400 Received: by smtp.corp.redhat.com (Postfix) id A64CD1111A4E; Mon, 31 May 2021 01:48:50 +0000 (UTC) Received: from mimecast-mx02.redhat.com (mimecast04.extmail.prod.ext.rdu2.redhat.com [10.11.55.20]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 9FDC7100296D for ; Mon, 31 May 2021 01:48:45 +0000 (UTC) Received: from us-smtp-1.mimecast.com (us-smtp-delivery-1.mimecast.com [205.139.110.120]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id A6D05101D221 for ; Mon, 31 May 2021 01:48:45 +0000 (UTC) Received: from mail-pl1-f181.google.com (mail-pl1-f181.google.com [209.85.214.181]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-544-XSE-We0-Mxa2XiByZXb4mw-1; Sun, 30 May 2021 21:48:43 -0400 Received: by mail-pl1-f181.google.com with SMTP id q16so4422120pls.6 for ; Sun, 30 May 2021 18:48:43 -0700 (PDT) Received: from localhost ([103.138.53.238]) by smtp.gmail.com with ESMTPSA id s19sm9297689pfm.212.2021.05.30.18.48.41 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 30 May 2021 18:48:41 -0700 (PDT) X-MC-Unique: 7WM1wgeQPve8KuP2eCKGqA-1 X-MC-Unique: XSE-We0-Mxa2XiByZXb4mw-1 X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=LoEX4OCcmLeZHruUwj2C+d//tcSZ/1rTW5qoX+WLMxU=; b=ErPk3rFCsLcaHPWrsKDWszaCOqb5kiwdVQm8CWcA5ZRbynV0SpLMEvyPlPeejJlH5Y AhxxF/D2gISjmELseq0BfhGhJjINRJpuLW2rC9QhtXrBOfxwpPbDCzOx9OrAuduMeuCu MsW01zek6aGFxO0ecHBFFEPQbG8ABcqopJbEUPjGkQoWFn7cc2IbmOWsS4x78gD0B06J oz2y8UxE4cwEhiXotkIvBF5K2c4R7QcQ5+2GZicAu9P8jQHE4YvzY0Fu9/4+ExrJZJPu o5brBh0EirX9fXEG2cdHMJ4pX1EeJcyIKzvXgVJNMYaHMx4RJb/CGHio1mbJ487kaTvY T6Ug== X-Gm-Message-State: AOAM532KJlefyA+G1qILfUFtFdZU3vSzfHIKCTvvehdxRUg0FUMxAjuW AytUv7PjrByzuVkRjie7A5jlfEnDtDZ3/i5qtrc= X-Google-Smtp-Source: ABdhPJxKL1RJcTBeiwo+m5yauQnovv2Z/LDp1XADj5S7Mz6wPzRypF2flT20mVe4B979XiN0ILgkNQ== X-Received: by 2002:a17:90a:560b:: with SMTP id r11mr16454361pjf.224.1622425722020; Sun, 30 May 2021 18:48:42 -0700 (PDT) From: Luke Yue To: libvir-list@redhat.com Subject: [PATCH 3/3] virfile: Use g_canonicalize_file() to simplify virFileAbsPath() Date: Mon, 31 May 2021 09:48:24 +0800 Message-Id: <20210531014824.923662-4-lukedyue@gmail.com> In-Reply-To: <20210531014824.923662-1-lukedyue@gmail.com> References: <20210531014824.923662-1-lukedyue@gmail.com> MIME-Version: 1.0 X-Mimecast-Impersonation-Protect: Policy=CLT - Impersonation Protection Definition; Similar Internal Domain=false; Similar Monitored External Domain=false; Custom External Domain=false; Mimecast External Domain=false; Newly Observed Domain=false; Internal User Name=false; Custom Display Name List=false; Reply-to Address Mismatch=false; Targeted Threat Dictionary=false; Mimecast Threat Dictionary=false; Custom Threat Dictionary=false X-Scanned-By: MIMEDefang 2.78 on 10.11.54.3 X-loop: libvir-list@redhat.com 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.14 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=libvir-list-bounces@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Though the comment says that the function may return -1 on error, but it seems that now it will never return -1 now. So just use g_canonicalize_file= () to simplify the implementation. Signed-off-by: Luke Yue --- src/util/virfile.c | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/src/util/virfile.c b/src/util/virfile.c index 0d1c2ba518..bfff471194 100644 --- a/src/util/virfile.c +++ b/src/util/virfile.c @@ -3126,13 +3126,7 @@ virFileOpenTty(int *ttyprimary G_GNUC_UNUSED, int virFileAbsPath(const char *path, char **abspath) { - if (g_path_is_absolute(path)) { - *abspath =3D g_strdup(path); - } else { - g_autofree char *buf =3D g_get_current_dir(); - - *abspath =3D g_build_filename(buf, path, NULL); - } + *abspath =3D g_canonicalize_filename(path, NULL); =20 return 0; } --=20 2.31.1