From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mp11.migadu.com ([2001:41d0:2:4a6f::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by ms0.migadu.com with LMTPS id kADWL8+11WHnQAAAgWs5BA (envelope-from ) for ; Wed, 05 Jan 2022 16:14:23 +0100 Received: from aspmx1.migadu.com ([2001:41d0:2:4a6f::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by mp11.migadu.com with LMTPS id mHkgLc+11WEWLAEA9RJhRA (envelope-from ) for ; Wed, 05 Jan 2022 16:14:23 +0100 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 57EAD28514 for ; Wed, 5 Jan 2022 16:14:23 +0100 (CET) Received: from localhost ([::1]:47588 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1n57za-0004g5-Ge for larch@yhetil.org; Wed, 05 Jan 2022 10:14:22 -0500 Received: from eggs.gnu.org ([209.51.188.92]:54486) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1n56xT-0007JN-MH for guix-patches@gnu.org; Wed, 05 Jan 2022 09:08:07 -0500 Received: from debbugs.gnu.org ([209.51.188.43]:56471) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1n56xO-0006be-GN for guix-patches@gnu.org; Wed, 05 Jan 2022 09:08:07 -0500 Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1n56xO-0004WZ-Cb for guix-patches@gnu.org; Wed, 05 Jan 2022 09:08:02 -0500 X-Loop: help-debbugs@gnu.org Subject: [bug#50072] [PATCH v5 1/4] guix hash: Extract file hashing procedures. References: In-Reply-To: Resent-From: Maxime Devos Original-Sender: "Debbugs-submit" Resent-CC: guix-patches@gnu.org Resent-Date: Wed, 05 Jan 2022 14:08: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.164139167917356 (code B ref 50072); Wed, 05 Jan 2022 14:08:02 +0000 Received: (at 50072) by debbugs.gnu.org; 5 Jan 2022 14:07:59 +0000 Received: from localhost ([127.0.0.1]:39777 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1n56xH-0004Vj-Hy for submit@debbugs.gnu.org; Wed, 05 Jan 2022 09:07:59 -0500 Received: from michel.telenet-ops.be ([195.130.137.88]:58248) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1n56xE-0004UX-Vt for 50072@debbugs.gnu.org; Wed, 05 Jan 2022 09:07:54 -0500 Received: from localhost.localdomain ([IPv6:2a02:1811:8c09:9d00:3c5f:2eff:feb0:ba5a]) by michel.telenet-ops.be with bizsmtp id f27s2600F4UW6Th0627sN9; Wed, 05 Jan 2022 15:07:52 +0100 From: Maxime Devos Date: Wed, 5 Jan 2022 14:07:47 +0000 Message-Id: <20220105140750.18214-1-maximedevos@telenet.be> X-Mailer: git-send-email 2.30.2 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=telenet.be; s=r22; t=1641391672; bh=NbrokZoVPvuKyRtEizbkjq1LE4lpGqaE5iJPt+N60jY=; h=From:To:Cc:Subject:Date; b=I8uIGPGF8KvuthIZQpGDtlV3tveyZEoPVpVo7uEAP8BFk1/GyvyNc7aRbMsMn+vLK Pt2iI/fET/TEq5IVbRRxMWogSaniKqeJO6D1tBVbmQXHmcBP0HlBrRLp6/Quua9sr4 sXhe1Bvu0kUFsvHZFIoqySZEFFaBqXd4gF2g+Vg3CcJoJ1SO7spyG6y+i/oE4vJh1R s4GDq2viNx3886+hp+PFv0tqC6PnR37nNLtpPrsRAULt8OILIC6l9DufLjFJe9bM5L YSoz6tcNqlL3y3keMOJByQv1k3CYqCiP3XDDGk9F87a1bP7rJDQJ1tDHhocckqokNk f7RfdXdwkveNw== 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 X-Migadu-Country: US ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=yhetil.org; s=key1; t=1641395663; h=from:from:sender:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc: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=NbrokZoVPvuKyRtEizbkjq1LE4lpGqaE5iJPt+N60jY=; b=lOU+9m7AKL8M9K81gF7hWDMXFcP8We58guW/DnSSK8NQaneyAJ52GO2qwZ+owLTdC1kqFj dA5mf8fkP9WiNS3uFXYWJYCPnq89YxmoxwUwXMjWjWJ1DiWyBr1fZCP6uowUF7miHnv4xH EOV28XKAdtZFqO3pcigZqHgg/R66dzgFobSpubEs8x5RxzG8ERkCwk++wQnIFU53JQmPM5 JmxHyiEJHR1GL6WvKREf5zdBq1lf+MO/0iiMr6OphSmeKqig2uTxEz4SdiqTNvA04YZzr/ Y3WLKD5pLEB18VDeITlFerHemoLvSungQWTGqr4CbYUq8Syr7wFm0WzHxgnAeQ== ARC-Seal: i=1; s=key1; d=yhetil.org; t=1641395663; a=rsa-sha256; cv=none; b=Az89AjUjS0XvA+/v+VFIf3w/Epm+B24oNYotzKVXwKfDK3vITH9/vbEoZJvaRPBiKGN/8n UxWHCFArZX4ps0zr0doGVAHMRF0Wbu3xcu6r9qnZbkWU1BfUtnNQrUj624xGWpOQ4GI0P+ iTgkFAr0guckoqclk3JnVowLuRn9+tle3Cz8TA19lpGMED217QvCAuEfn4QSVpKQPf+8Kd WUWJTJntgtIh9fNjY6w60Unr1wv1XCFcib50S16pek/w4q0PQ2nFy6zW+Zz/9NSFeGjd8T jHbSjzU2JlUX4puk+gMDuddiNOBj/W3hxqpRKvrDD8C/LpaGTnbjGsw7qGTMug== ARC-Authentication-Results: i=1; aspmx1.migadu.com; dkim=fail ("headers rsa verify failed") header.d=telenet.be header.s=r22 header.b=I8uIGPGF; dmarc=fail reason="SPF not aligned (relaxed)" header.from=telenet.be (policy=none); spf=pass (aspmx1.migadu.com: domain of "guix-patches-bounces+larch=yhetil.org@gnu.org" designates 209.51.188.17 as permitted sender) smtp.mailfrom="guix-patches-bounces+larch=yhetil.org@gnu.org" X-Migadu-Spam-Score: -1.10 Authentication-Results: aspmx1.migadu.com; dkim=fail ("headers rsa verify failed") header.d=telenet.be header.s=r22 header.b=I8uIGPGF; dmarc=fail reason="SPF not aligned (relaxed)" header.from=telenet.be (policy=none); spf=pass (aspmx1.migadu.com: domain of "guix-patches-bounces+larch=yhetil.org@gnu.org" designates 209.51.188.17 as permitted sender) smtp.mailfrom="guix-patches-bounces+larch=yhetil.org@gnu.org" X-Migadu-Queue-Id: 57EAD28514 X-Spam-Score: -1.10 X-Migadu-Scanner: scn1.migadu.com X-TUID: SqRhlNOfgYkf 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 | 73 +++++++++++++++++++++++++++++++++++++++++++ guix/scripts/hash.scm | 22 +++---------- 3 files changed, 78 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..3cb68e5c44 --- /dev/null +++ b/guix/hash.scm @@ -0,0 +1,73 @@ +;;; 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. + +Symbolic links are only dereferenced if RECURSIVE? is false. +Directories are only supported if RECURSIVE? is #true or 'auto'. +The executable bit is only recorded if RECURSIVE? is #true. +If FILE is a symbolic link, it is only followed if RECURSIVE? is false. + +For regular files, there are two different hashes when the executable +hash isn't recorded: the regular hash and the nar hash. In most situations, +the regular hash is desired and setting RECURSIVE? to 'auto' does the right +thing for both regular files and directories. + +This procedure must only be used under controlled circumstances; +the detection of symbolic links in FILE is racy. + +When FILE is a directory, the procedure SELECT? called as (SELECT? FILE STAT) +decides which files to include. By default, version control files are +excluded. To include everything, SELECT? can be set to (const #true)." + (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) base-commit: 9708681f1a9f221ae6cad64625ba8309b6742653 -- 2.30.2