From patchwork Tue Jan 4 20:06:40 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: M X-Patchwork-Id: 35944 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 A301F27BBE9; Tue, 4 Jan 2022 20:07:23 +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=-2.7 required=5.0 tests=BAYES_00,DKIM_INVALID, DKIM_SIGNED,FREEMAIL_FROM,MAILING_LIST_MULTI,RCVD_IN_MSPIKE_H3, RCVD_IN_MSPIKE_WL,SPF_HELO_PASS,URIBL_BLOCKED autolearn=unavailable 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 DD52227BBEA for ; Tue, 4 Jan 2022 20:07:22 +0000 (GMT) Received: from localhost ([::1]:53982 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1n4q5Z-0005gi-W3 for patchwork@mira.cbaines.net; Tue, 04 Jan 2022 15:07:22 -0500 Received: from eggs.gnu.org ([209.51.188.92]:39886) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1n4q5G-0005g1-T7 for guix-patches@gnu.org; Tue, 04 Jan 2022 15:07:03 -0500 Received: from debbugs.gnu.org ([209.51.188.43]:55495) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1n4q5G-00035u-K8 for guix-patches@gnu.org; Tue, 04 Jan 2022 15:07:02 -0500 Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1n4q5G-000852-GB for guix-patches@gnu.org; Tue, 04 Jan 2022 15:07:02 -0500 X-Loop: help-debbugs@gnu.org Subject: [bug#50072] [PATCH v4 1/4] guix hash: Extract file hashing procedures. Resent-From: Maxime Devos Original-Sender: "Debbugs-submit" Resent-CC: guix-patches@gnu.org Resent-Date: Tue, 04 Jan 2022 20:07:02 +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 Cc: Sarah Morgensen , Maxime Devos Received: via spool by 50072-submit@debbugs.gnu.org id=B50072.164132682031032 (code B ref 50072); Tue, 04 Jan 2022 20:07:02 +0000 Received: (at 50072) by debbugs.gnu.org; 4 Jan 2022 20:07:00 +0000 Received: from localhost ([127.0.0.1]:38801 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1n4q5D-00084K-IX for submit@debbugs.gnu.org; Tue, 04 Jan 2022 15:07:00 -0500 Received: from michel.telenet-ops.be ([195.130.137.88]:47928) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1n4q56-00083e-3H for 50072@debbugs.gnu.org; Tue, 04 Jan 2022 15:06:53 -0500 Received: from localhost.localdomain ([IPv6:2a02:1811:8c09:9d00:3c5f:2eff:feb0:ba5a]) by michel.telenet-ops.be with bizsmtp id ek6q260014UW6Th06k6q5m; Tue, 04 Jan 2022 21:06:50 +0100 From: Maxime Devos Date: Tue, 4 Jan 2022 20:06:40 +0000 Message-Id: <20220104200643.43374-2-maximedevos@telenet.be> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20220104200643.43374-1-maximedevos@telenet.be> References: <20220104200643.43374-1-maximedevos@telenet.be> MIME-Version: 1.0 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=telenet.be; s=r22; t=1641326810; bh=i9yb7nh1nBUy1tiYhhqR0CWJ3JVd+uJc5NmKjxoYf0I=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=EEZbBeebeOrVtRmFroCCtV65TOKzdaJuFoTHeofNlnoztvJBtrgo62n6e7vWLQupP IJukbu6q/a+LRNN9edSSgqTbwQhc8DddKySk/YgWBERRpORmCU/ayAKv2139KSd1Jc 75UiY64PMDyInvsgcC2A5sKIaTunHpWR4Mi+SVXMb0bHW3vA6hV0Udo712vS/OiHRn zacAYtYZcbQXlh7Pm+qIRWDJqgVHtetBf1YSnFY9op6o2MdeMb51RXgK2omeGe1L5R v1tAEHQbxWyFN6ZZ/H8Ged7dAOkarQK9d/ERjRVTPYwi+18WPg8PUBLk+t2uMf6Lf3 mZTqYTtnE9euQ== 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+patchwork=mira.cbaines.net@gnu.org Sender: "Guix-patches" X-getmail-retrieved-from-mailbox: Patches From: Sarah Morgensen * guix/scripts/hash.scm (guix-hash)[vcs-file?] (nar-hash, default-hash): Extract hashing logic to... * guix/hash.scm (vcs-file?, file-hash*): ... these new procedures in this new file. Modified-by: Maxime Devos --- Makefile.am | 1 + guix/hash.scm | 68 +++++++++++++++++++++++++++++++++++++++++++ guix/scripts/hash.scm | 22 +++----------- 3 files changed, 73 insertions(+), 18 deletions(-) create mode 100644 guix/hash.scm diff --git a/Makefile.am b/Makefile.am index 8c5682a1c6..bc3d0087d0 100644 --- a/Makefile.am +++ b/Makefile.am @@ -99,6 +99,7 @@ MODULES = \ guix/extracting-download.scm \ guix/git-download.scm \ guix/hg-download.scm \ + guix/hash.scm \ guix/swh.scm \ guix/monads.scm \ guix/monad-repl.scm \ diff --git a/guix/hash.scm b/guix/hash.scm new file mode 100644 index 0000000000..19cbc41ad1 --- /dev/null +++ b/guix/hash.scm @@ -0,0 +1,68 @@ +;;; GNU Guix --- Functional package management for GNU +;;; Copyright © 2021 Sarah Morgensen +;;; Copyright © 2022 Maxime Devos +;;; +;;; 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? 'auto) + (select? (negate vcs-file?))) + "Compute the hash of FILE with ALGORITHM. If RECURSIVE? is #true or 'auto', +recurse into subdirectories of FILE, computing the combined hash (nar hash) of +all files for which (SELECT? FILE STAT) returns true. + +Symbolic links are not dereferenced unless RECURSIVE? is false. + +This procedure must only be used under controlled circumstances; +the detection of symbolic links in FILE is racy. + +Keep in mind that the hash of a regular file depends on RECURSIVE?: +if the recursive hash is desired, it must be set to #true. Otherwise, it must +be set to #false or 'auto'. In most situations, the non-recursive hash is desired +for regular files." + (if (or (eq? recursive? #true) + (and (eq? recursive? 'auto) + ;; Don't change this to (eq? 'directory ...), because otherwise + ;; if 'file' denotes a symbolic link, the 'file-hash' below + ;; would dereference it -- dereferencing symbolic links would + ;; open an avoidable can of potential worms. + (not (eq? 'regular (stat:type (lstat file)))))) + (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 d73e3d13dd..28d587b944 100644 --- a/guix/scripts/hash.scm +++ b/guix/scripts/hash.scm @@ -4,6 +4,7 @@ ;;; Copyright © 2016 Jan Nieuwenhuizen ;;; Copyright © 2018 Tim Gesthuizen ;;; Copyright © 2021 Simon Tournier +;;; Copyright © 2021 Sarah Morgensen ;;; ;;; This file is part of GNU Guix. ;;; @@ -24,6 +25,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) @@ -46,20 +48,14 @@ (define* (nar-hash file #:optional (algorithm (assoc-ref %default-options 'hash-algorithm)) select?) - (let-values (((port get-hash) - (open-hash-port algorithm))) - (write-file file port #:select? select?) - (force-output port) - (get-hash))) + (file-hash* file #:algorithm algorithm #:select? select? #:recursive? #true)) (define* (default-hash file #:optional (algorithm (assoc-ref %default-options 'hash-algorithm)) select?) (match file ("-" (port-hash algorithm (current-input-port))) - (_ - (call-with-input-file file - (cute port-hash algorithm <>))))) + (_ (file-hash* file #:algorithm algorithm #:recursive? #false)))) (define* (git-hash file #:optional (algorithm (assoc-ref %default-options 'hash-algorithm)) @@ -181,16 +177,6 @@ use '--serializer' instead~%")) (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)