From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mp0 ([2001:41d0:2:bcc0::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by ms0.migadu.com with LMTPS id JgxKMJSiGWEvKwEAgWs5BA (envelope-from ) for ; Mon, 16 Aug 2021 01:26:12 +0200 Received: from aspmx1.migadu.com ([2001:41d0:2:bcc0::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by mp0 with LMTPS id WPF7K5SiGWHbTAAA1q6Kng (envelope-from ) for ; Sun, 15 Aug 2021 23:26:12 +0000 Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by aspmx1.migadu.com (Postfix) with ESMTPS id C09F8B545 for ; Mon, 16 Aug 2021 01:26:11 +0200 (CEST) Received: from localhost ([::1]:57420 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1mFPW6-0008K3-Pq for larch@yhetil.org; Sun, 15 Aug 2021 19:26:10 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:37842) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1mFPVy-0008I6-Ad for guix-patches@gnu.org; Sun, 15 Aug 2021 19:26:02 -0400 Received: from debbugs.gnu.org ([209.51.188.43]:36402) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1mFPVy-0004xR-4c for guix-patches@gnu.org; Sun, 15 Aug 2021 19:26:02 -0400 Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1mFPVy-0001qY-0Y for guix-patches@gnu.org; Sun, 15 Aug 2021 19:26:02 -0400 X-Loop: help-debbugs@gnu.org Subject: [bug#50072] [PATCH WIP 1/4] guix hash: Extract file hashing procedures. Resent-From: Sarah Morgensen Original-Sender: "Debbugs-submit" Resent-CC: guix-patches@gnu.org Resent-Date: Sun, 15 Aug 2021 23:26:01 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 50072 X-GNU-PR-Package: guix-patches X-GNU-PR-Keywords: patch To: 50072@debbugs.gnu.org Received: via spool by 50072-submit@debbugs.gnu.org id=B50072.16290699327023 (code B ref 50072); Sun, 15 Aug 2021 23:26:01 +0000 Received: (at 50072) by debbugs.gnu.org; 15 Aug 2021 23:25:32 +0000 Received: from localhost ([127.0.0.1]:47939 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1mFPVU-0001pD-5j for submit@debbugs.gnu.org; Sun, 15 Aug 2021 19:25:32 -0400 Received: from out1.migadu.com ([91.121.223.63]:59878) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1mFPVS-0001p4-6V for 50072@debbugs.gnu.org; Sun, 15 Aug 2021 19:25:31 -0400 X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=mgsn.dev; s=key1; t=1629069929; 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=0WFMFSUuUYWVX08e2b+zqvIdj2OOvXmGMXdblJEeCyg=; b=Hj3SRdhXdJXsM1ywtCvt7jpZwMt76EonHF4eWORW+FRYmOWeiEWZQBd6KAMk8QvbO624cr i0Q2C+xoemwEMvhf/ANeBbHTBD890vCE9JIIBK158jkpXAQVk2KRGxmPesvesvd8iNZtXZ f+RLBB2nyPNtvWOKNs8LxQjiZe0sII0= From: Sarah Morgensen Date: Sun, 15 Aug 2021 16:25:24 -0700 Message-Id: <9ba0b798de2fdf859f94ca7b2f1ee052bceac63a.1629068119.git.iskarian@mgsn.dev> In-Reply-To: References: MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Migadu-Auth-User: iskarian@mgsn.dev 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: , Errors-To: guix-patches-bounces+larch=yhetil.org@gnu.org Sender: "Guix-patches" X-Migadu-Flow: FLOW_IN ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=yhetil.org; s=key1; t=1629069972; 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:resent-cc: resent-from:resent-sender:resent-message-id:in-reply-to:in-reply-to: references:references:list-id:list-help:list-unsubscribe: list-subscribe:list-post:dkim-signature; bh=0WFMFSUuUYWVX08e2b+zqvIdj2OOvXmGMXdblJEeCyg=; b=XoIOr+lm5X1X+epGunCzwMf9NC2AmvjozvJW/KmeHDfUbads4IBlmkGMVwTBohLjNjDZTD 8P3I6mYo90cJsl619GOdTIymDeV3+ATovb9lmAwnGETtC3Y9BuDXCz8CSWMv1OznBTb9/h gTQ/+f7JUqMOvQqicKoQyAKJrbqlDt7WdHaBRjpl1Ckf0WRM6yii1/Xne7ocvrUEGkIt3o 2/IJC67orq6rpU41Vu35FhKlU0AhiRiJUqP+20CCtibj/I7pYB9TyhNUTnKEvBZbrdLiC/ RRRETU/HvJ0jECWIaTkxeX1AX2v9MCcEx8olQMb3xxn7Hl4hAT1OCempmcoOlw== ARC-Seal: i=1; s=key1; d=yhetil.org; t=1629069972; a=rsa-sha256; cv=none; b=n2BiHxYHJcmCtIn9hpAIxmU7PdQIVgGpfUoCN9U7MgJUlgoPzxWqXYjoK/y+MVI5wt9K0e gtIW3iryH6v4VPrqVPG0/dud1/KGrDDrhIjTTxlvXkploFbr62hPCEaasEBWZwZElP2/Y3 w2tJqoB3Apmmk9ZHPTKZ2LB5P5/AAaxixPYChxznfOXKLllcrxwncXJrCPHSv/YGX3ar+x k26zH8w6KcgPL/OV4GMsdKYuiBwJH0kRUU2nrfPIF/khcJVIMDmklQjFpXzBs/QUg1NeA6 ltheD6rMev/EQcvY9M0KQWXK3DXX1DrEeVGm8BQoKrbSUwUhVaog9NgNBgGgIg== ARC-Authentication-Results: i=1; aspmx1.migadu.com; dkim=fail ("headers rsa verify failed") header.d=mgsn.dev header.s=key1 header.b=Hj3SRdhX; dmarc=fail reason="SPF not aligned (relaxed)" header.from=mgsn.dev (policy=none); spf=pass (aspmx1.migadu.com: domain of guix-patches-bounces@gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=guix-patches-bounces@gnu.org X-Migadu-Spam-Score: -0.31 Authentication-Results: aspmx1.migadu.com; dkim=fail ("headers rsa verify failed") header.d=mgsn.dev header.s=key1 header.b=Hj3SRdhX; dmarc=fail reason="SPF not aligned (relaxed)" header.from=mgsn.dev (policy=none); spf=pass (aspmx1.migadu.com: domain of guix-patches-bounces@gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=guix-patches-bounces@gnu.org X-Migadu-Queue-Id: C09F8B545 X-Spam-Score: -0.31 X-Migadu-Scanner: scn0.migadu.com X-TUID: QGSbeYgvSWxN * guix/scripts/hash.scm (guix-hash)[vcs-file?, file-hash]: Extract logic to... * guix/hash.scm: ...here. New file. --- guix/hash.scm | 51 +++++++++++++++++++++++++++++++++++++++++++ guix/scripts/hash.scm | 29 ++++++------------------ 2 files changed, 58 insertions(+), 22 deletions(-) create mode 100644 guix/hash.scm diff --git a/guix/hash.scm b/guix/hash.scm new file mode 100644 index 0000000000..8c2ab8187f --- /dev/null +++ b/guix/hash.scm @@ -0,0 +1,51 @@ +;;; GNU Guix --- Functional package management for GNU +;;; Copyright © 2021 Sarah Morgensen +;;; +;;; This file is part of GNU Guix. +;;; +;;; GNU Guix is free software; you can redistribute it and/or modify it +;;; under the terms of the GNU General Public License as published by +;;; the Free Software Foundation; either version 3 of the License, or (at +;;; your option) any later version. +;;; +;;; GNU Guix is distributed in the hope that it will be useful, but +;;; WITHOUT ANY WARRANTY; without even the implied warranty of +;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +;;; GNU General Public License for more details. +;;; +;;; You should have received a copy of the GNU General Public License +;;; along with GNU Guix. If not, see . + +(define-module (guix hash) + #:use-module (gcrypt hash) + #:use-module (guix serialization) + #:use-module (srfi srfi-1) + #:use-module (srfi srfi-11) + #:export (vcs-file? + file-hash*)) + +(define (vcs-file? file stat) + "Returns true if FILE is a version control system file." + (case (stat:type stat) + ((directory) + (member (basename file) '(".bzr" ".git" ".hg" ".svn" "CVS"))) + ((regular) + ;; Git sub-modules have a '.git' file that is a regular text file. + (string=? (basename file) ".git")) + (else + #f))) + +(define* (file-hash* file #:key + (algorithm (hash-algorithm sha256)) + (recursive? #t) + (select? (negate vcs-file?))) + "Compute the hash of FILE with ALGORITHM. If RECURSIVE? is true, recurse +into subdirectories of FILE, computing the combined hash of all files for +which (SELECT? FILE STAT) returns true." + (if recursive? + (let-values (((port get-hash) + (open-hash-port algorithm))) + (write-file file port #:select? select?) + (force-output port) + (get-hash)) + (file-hash algorithm file))) diff --git a/guix/scripts/hash.scm b/guix/scripts/hash.scm index b8622373cc..353ca30c2c 100644 --- a/guix/scripts/hash.scm +++ b/guix/scripts/hash.scm @@ -3,6 +3,7 @@ ;;; Copyright © 2013 Nikita Karetnikov ;;; Copyright © 2016 Jan Nieuwenhuizen ;;; Copyright © 2018 Tim Gesthuizen +;;; Copyright © 2021 Sarah Morgensen ;;; ;;; This file is part of GNU Guix. ;;; @@ -23,6 +24,7 @@ #:use-module (gcrypt hash) #:use-module (guix serialization) #:use-module (guix ui) + #:use-module (guix hash) #:use-module (guix scripts) #:use-module (guix base16) #:use-module (guix base32) @@ -125,16 +127,6 @@ and 'base16' ('hex' and 'hexadecimal' can be used as well).\n")) (parse-command-line args %options (list %default-options) #:build-options? #f)) - (define (vcs-file? file stat) - (case (stat:type stat) - ((directory) - (member (basename file) '(".bzr" ".git" ".hg" ".svn" "CVS"))) - ((regular) - ;; Git sub-modules have a '.git' file that is a regular text file. - (string=? (basename file) ".git")) - (else - #f))) - (let* ((opts (parse-options)) (args (filter-map (match-lambda (('argument . value) @@ -150,18 +142,11 @@ and 'base16' ('hex' and 'hexadecimal' can be used as well).\n")) ;; Compute the hash of FILE. ;; Catch and gracefully report possible '&nar-error' conditions. (with-error-handling - (if (assoc-ref opts 'recursive?) - (let-values (((port get-hash) - (open-hash-port (assoc-ref opts 'hash-algorithm)))) - (write-file file port #:select? select?) - (force-output port) - (get-hash)) - (match file - ("-" (port-hash (assoc-ref opts 'hash-algorithm) - (current-input-port))) - (_ (call-with-input-file file - (cute port-hash (assoc-ref opts 'hash-algorithm) - <>))))))) + (match file + ("-" (port-hash (assoc-ref opts 'hash-algorithm) + (current-input-port))) + (_ (file-hash* #:algorithm (assoc-ref opts 'hash-algorithm) + #:recursive? (assoc-ref opts 'recursive?)))))) (match args ((file) -- 2.31.1