From patchwork Sun Feb 23 01:18:58 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Andrew Wong X-Patchwork-Id: 38987 Return-Path: X-Original-To: patchwork@mira.cbaines.net Delivered-To: patchwork@mira.cbaines.net Received: by mira.cbaines.net (Postfix, from userid 113) id 1E80527BBEA; Sun, 23 Feb 2025 01:24:35 +0000 (GMT) X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on mira.cbaines.net X-Spam-Level: X-Spam-Status: No, score=-6.4 required=5.0 tests=BAYES_00,DKIM_INVALID, DKIM_SIGNED,FREEMAIL_FROM,MAILING_LIST_MULTI,RCVD_IN_DNSWL_BLOCKED, RCVD_IN_VALIDITY_CERTIFIED,RCVD_IN_VALIDITY_RPBL,RCVD_IN_VALIDITY_SAFE, SPF_HELO_PASS,URIBL_BLOCKED autolearn=ham autolearn_force=no version=3.4.6 Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mira.cbaines.net (Postfix) with ESMTPS id 61C2327BBE2 for ; Sun, 23 Feb 2025 01:24:33 +0000 (GMT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1tm0j2-00089N-9f; Sat, 22 Feb 2025 20:24:08 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1tm0j0-00089D-J1 for guix-patches@gnu.org; Sat, 22 Feb 2025 20:24:06 -0500 Received: from debbugs.gnu.org ([2001:470:142:5::43]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1tm0iy-0000ds-JW; Sat, 22 Feb 2025 20:24:04 -0500 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=debbugs.gnu.org; s=debbugs-gnu-org; h=MIME-Version:Date:From:To:Subject; bh=poAOmTqI56tzS8O0yn364Gvtq1XSZbraNKAohoqtspE=; b=XSEg4jPQYLb0SpA7Ru8reKTn8BzSbRkmWgPhxqbFb07K3zPQ1lVO1WbxliYRM+xnjg5G581XObsBxyZjAHSHzUIHUM7kpcepoEni1kC7xi3nivQXjQkzNiFZ/36Ukb4hqehbShNXDYi3tMt6YRFx2BbmvQsXp/sQCdci5N4SZDR9QNQn0Rk3wx+CauDb9ZB3KlHfaVcZPR3aIZw4ps5H/WAGBJFSuRjg4SeP1zl2ibgDYRBulJLD9bXekwsmICLdrhCI7Ku/M7Alflwh6TSfkNGdEgRwOnqcX951HjjyvD3HKBXP2NW0YELDxdgj2KpWzMJi+XYmcXy/nhSY1/y0ZQ==; Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1tm0iw-00086z-E5; Sat, 22 Feb 2025 20:24:02 -0500 X-Loop: help-debbugs@gnu.org Subject: [bug#76492] [PATCH] build/utils: Add delete-all-but Resent-From: Andrew Wong Original-Sender: "Debbugs-submit" Resent-CC: andreas@enge.fr, janneke@gnu.org, ludo@gnu.org, z572@z572.online, guix-patches@gnu.org Resent-Date: Sun, 23 Feb 2025 01:24:02 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: report 76492 X-GNU-PR-Package: guix-patches X-GNU-PR-Keywords: patch To: 76492@debbugs.gnu.org Cc: Andrew Wong , Andreas Enge , Janneke Nieuwenhuizen , Ludovic =?utf-8?q?Court=C3=A8s?= , Zheng Junjie X-Debbugs-Original-To: guix-patches@gnu.org X-Debbugs-Original-Xcc: Andreas Enge , Janneke Nieuwenhuizen , Ludovic =?utf-8?q?Court=C3=A8s?= , Zheng Junjie Received: via spool by submit@debbugs.gnu.org id=B.174027381531132 (code B ref -1); Sun, 23 Feb 2025 01:24:02 +0000 Received: (at submit) by debbugs.gnu.org; 23 Feb 2025 01:23:35 +0000 Received: from localhost ([127.0.0.1]:58151 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1tm0iU-000864-Ra for submit@debbugs.gnu.org; Sat, 22 Feb 2025 20:23:35 -0500 Received: from lists.gnu.org ([2001:470:142::17]:39304) by debbugs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1tm0iR-00085p-Of for submit@debbugs.gnu.org; Sat, 22 Feb 2025 20:23:33 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1tm0iM-000876-9b for guix-patches@gnu.org; Sat, 22 Feb 2025 20:23:26 -0500 Received: from qs51p00im-qukt01080301.me.com ([17.57.155.20]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1tm0iK-0000ad-9Q for guix-patches@gnu.org; Sat, 22 Feb 2025 20:23:25 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=icloud.com; s=1a1hai; bh=poAOmTqI56tzS8O0yn364Gvtq1XSZbraNKAohoqtspE=; h=From:To:Subject:Date:Message-ID:MIME-Version:Content-Type:x-icloud-hme; b=A1dkf8cQifqW/JXAhI/uB+x0c3r3+F22is0gyMW0TQRh01WrezhiSxrPLq6SXs8wF 50SbtLPKRlUimt8gmmuEjOKOuwSlsj+N5iQnOeVaArVze9SxzZBTv6Mi7cQHa2O8MJ E/c+trgPBhFN0wpIzjJthNaQpK6Mbp/9W6xTYxJE38XV9VZXlupgXtG+d+wH/IASH4 8EyXJJhSNDjSQmao0TIjB+D9uNUg/SwQL9HArtDg8JyMP7rjcr73kzk7Y5Dwo9J13W 4usTGS0YpBw1buU4jsRsASVbIMp3F//fv6aW72BcV8pM1OrTuYtfHnZKRazdufyEjM 3RpavqJtP3hRQ== Received: from wongworks.lan (qs51p00im-dlb-asmtp-mailmevip.me.com [17.57.155.28]) by qs51p00im-qukt01080301.me.com (Postfix) with ESMTPSA id D86925F00173; Sun, 23 Feb 2025 01:23:18 +0000 (UTC) Date: Sat, 22 Feb 2025 20:18:58 -0500 Message-ID: <0b8d4eb4212609048cf81f2e3e6e3da83c1caf46.1740273538.git.wongandj@icloud.com> X-Mailer: git-send-email 2.48.1 MIME-Version: 1.0 X-Proofpoint-GUID: hoxzyO-ZrR8HdaAzRcC-78DA2kzndnw4 X-Proofpoint-ORIG-GUID: hoxzyO-ZrR8HdaAzRcC-78DA2kzndnw4 X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.293,Aquarius:18.0.1057,Hydra:6.0.680,FMLib:17.12.68.34 definitions=2025-02-22_11,2025-02-20_02,2024-11-22_01 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 bulkscore=0 mlxscore=0 phishscore=0 malwarescore=0 clxscore=1015 adultscore=0 suspectscore=0 spamscore=0 mlxlogscore=671 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.19.0-2308100000 definitions=main-2502230008 X-Apple-Remote-Links: v=1;h=KCk=;charset=UTF-8 Received-SPF: pass client-ip=17.57.155.20; envelope-from=wongandj@icloud.com; helo=qs51p00im-qukt01080301.me.com X-Spam_score_int: -27 X-Spam_score: -2.8 X-Spam_bar: -- X-Spam_report: (-2.8 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_LOW=-0.7, RCVD_IN_MSPIKE_H5=0.001, RCVD_IN_MSPIKE_WL=0.001, RCVD_IN_VALIDITY_CERTIFIED_BLOCKED=0.001, RCVD_IN_VALIDITY_RPBL_BLOCKED=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-BeenThere: guix-patches@gnu.org List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-to: Andrew Wong X-ACL-Warn: , Andrew Wong via Guix-patches X-Patchwork-Original-From: Andrew Wong via Guix-patches via From: Andrew Wong Errors-To: guix-patches-bounces+patchwork=mira.cbaines.net@gnu.org Sender: guix-patches-bounces+patchwork=mira.cbaines.net@gnu.org X-getmail-retrieved-from-mailbox: Patches * guix/build/utils.scm (delete-all-but): New procedure. Change-Id: I82db4b63c45d88ef0529adb760182495c28dae9e --- delete-all-but is defined and used within package definitions a total of 11 times according to a simple grep; it makes sense to move it to a common location. However, changing (guix build utils) is a heavy change, so perhaps there is a better branch to merge this to than master. guix/build/utils.scm | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) base-commit: cd20619cfcb32c2631fb602729512740bc510550 diff --git a/guix/build/utils.scm b/guix/build/utils.scm index 94714bf397..b74c1ad7d8 100644 --- a/guix/build/utils.scm +++ b/guix/build/utils.scm @@ -10,6 +10,7 @@ ;;; Copyright © 2021, 2022 Maxime Devos ;;; Copyright © 2021 Brendan Tildesley ;;; Copyright © 2023 Carlo Zancanaro +;;; Copyright © 2025 Andrew Wong ;;; ;;; This file is part of GNU Guix. ;;; @@ -77,6 +78,7 @@ (define-module (guix build utils) make-file-writable copy-recursively delete-file-recursively + delete-all-but file-name-predicate find-files false-if-file-not-found @@ -522,6 +524,29 @@ (define* (delete-file-recursively dir ;; Don't follow symlinks. lstat))) +(define (delete-all-but paths-kept) + "Delete all paths in DIR except for those listed in PATHS-KEPT, without +following symblinks. Report but don't ignore errors." + (let ((paths-kept (map (cut canonicalize-path <>) + paths-kept))) + (file-system-fold + (lambda (current-path current-stat result) ;enter? + (not (member (canonicalize-path current-path) paths-kept string=?))) + (lambda (file file-stat result) ;leaf + (if (not (any (cut string-prefix? (canonicalize-path file) <>) + paths-kept)) + (warn-on-error (delete-file file) file))) + (const #t) ;down + (lambda (dir dir-stat result) ;up + (if (not (any (cut string-prefix? (canonicalize-path dir) <>) + paths-kept)) + (warn-on-error (rmdir dir) dir))) + (const #t) ;skip + (lambda (file stat errno result) ;error + (format (current-error-port) + "warning: failed to delete ~a: ~a~%" file (strerror errno))) + #t (getcwd) lstat))) + (define (file-name-predicate regexp) "Return a predicate that returns true when passed a file name whose base name matches REGEXP."