From patchwork Wed Oct 20 16:54:34 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Simon Tournier X-Patchwork-Id: 33964 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 AB9F927BBE3; Wed, 20 Oct 2021 17:55:39 +0100 (BST) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on mira.cbaines.net X-Spam-Level: X-Spam-Status: No, score=-2.8 required=5.0 tests=BAYES_00,DKIM_ADSP_CUSTOM_MED, DKIM_SIGNED,FREEMAIL_FROM,MAILING_LIST_MULTI,RCVD_IN_MSPIKE_H2, SPF_HELO_PASS,T_DKIM_INVALID,URIBL_BLOCKED autolearn=unavailable autolearn_force=no version=3.4.2 Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mira.cbaines.net (Postfix) with ESMTPS id 29D1527BBE1 for ; Wed, 20 Oct 2021 17:55:39 +0100 (BST) Received: from localhost ([::1]:47114 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1mdEsM-000194-7E for patchwork@mira.cbaines.net; Wed, 20 Oct 2021 12:55:38 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:55976) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1mdErn-00017o-Lr for guix-patches@gnu.org; Wed, 20 Oct 2021 12:55:03 -0400 Received: from debbugs.gnu.org ([209.51.188.43]:43813) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1mdErm-0000rL-Ax for guix-patches@gnu.org; Wed, 20 Oct 2021 12:55:03 -0400 Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1mdErm-0007Sv-99 for guix-patches@gnu.org; Wed, 20 Oct 2021 12:55:02 -0400 X-Loop: help-debbugs@gnu.org Subject: [bug#51307] [PATCH 1/2] scripts: hash: Improve error handling. References: <20211020165020.3358311-1-zimon.toutoune@gmail.com> In-Reply-To: <20211020165020.3358311-1-zimon.toutoune@gmail.com> Resent-From: zimoun Original-Sender: "Debbugs-submit" Resent-CC: guix-patches@gnu.org Resent-Date: Wed, 20 Oct 2021 16:55:02 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 51307 X-GNU-PR-Package: guix-patches X-GNU-PR-Keywords: patch To: 51307@debbugs.gnu.org Cc: zimoun Received: via spool by 51307-submit@debbugs.gnu.org id=B51307.163474889128672 (code B ref 51307); Wed, 20 Oct 2021 16:55:02 +0000 Received: (at 51307) by debbugs.gnu.org; 20 Oct 2021 16:54:51 +0000 Received: from localhost ([127.0.0.1]:55357 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1mdErb-0007SN-0p for submit@debbugs.gnu.org; Wed, 20 Oct 2021 12:54:51 -0400 Received: from mail-wr1-f50.google.com ([209.85.221.50]:45632) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1mdErY-0007S2-MJ for 51307@debbugs.gnu.org; Wed, 20 Oct 2021 12:54:49 -0400 Received: by mail-wr1-f50.google.com with SMTP id r10so48265346wra.12 for <51307@debbugs.gnu.org>; Wed, 20 Oct 2021 09:54:48 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=OCiq+B8eY3dy24gREyN0p0VuTvCUjlqt45JBpT64X1g=; b=iiZDbFfXtEJuoUFA5jn2VuLREdMKQv5qEclE5TvRv9yA4v/EH/O+Yy5kbpQzIFyyjh /jsSEJQsNCGVFop2/SNYOajwd0sMEiynuJNeKaTWo6RaHS3+ecHAAV4xNG5GTOPJkc+A 0zD6EKwLAy2TcMXyOgrjLVRCyoovOxRdciPWWEFlBez2AN+Cz3uBOSA9T/vQ4FvpD+PZ 22ZF/v/BmkuMzkpApf/aV9C5YgFbSOZmeKWDab+XD2XQiVtF9ttX/QHu3gejsVllZmtA ECsmDcIO4tlmBzLCe2jHMY6kaWkQxtHqamulAaWw/hIBcpbVRKqvZBhDCW2hQIExopNK baSw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=OCiq+B8eY3dy24gREyN0p0VuTvCUjlqt45JBpT64X1g=; b=h3kCHEqOxYE7b9hnQkD4kizwqxyshmH3okR5KbhHr5hKYTPw0AZDEDf8qFyM/S0/Nn 0OzOE80Yevyrw64d0dYdvGiIN8HTgCbo+20mN/NHQXazSfEARQIJqiZEcz6IQNNpq2vV NRnCYv0xRedUh4EAuVbkk9PKHOCJUIpeN8HUIy2kijvLW/JRY5w3hA+3GikzwCpWr9Vb ITWi7dhOFJkgy8DpErRFOENvhQs1ttkUxU4KQLrBhFGcnVE+4FrbvP6+/hDt5euNTUZW C+woVyPk1KOXj5xcYE8/cAEpA8nFj3PbCeBfPjzc6cKEFVdC9eTnai9P6eHAHO6U/j52 iRPg== X-Gm-Message-State: AOAM531SqonA7N09H4ur0xz72sU//zfbh5UiMk+ttKLEBWacQC4TBww8 BOQllvtUbDhZwkhROnmPt+bT43APUh0= X-Google-Smtp-Source: ABdhPJzEeEPHMwjPY8fy3pK763EZ5pD3BaG0siPnvRRUbHiZzSgP3tRkL/PeaAjbwMWmA1JH8nWnKg== X-Received: by 2002:a5d:47cb:: with SMTP id o11mr487718wrc.184.1634748882763; Wed, 20 Oct 2021 09:54:42 -0700 (PDT) Received: from localhost.localdomain ([193.48.40.117]) by smtp.gmail.com with ESMTPSA id o11sm3639977wry.0.2021.10.20.09.54.42 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 20 Oct 2021 09:54:42 -0700 (PDT) From: zimoun Date: Wed, 20 Oct 2021 18:54:34 +0200 Message-Id: <20211020165435.3358398-1-zimon.toutoune@gmail.com> X-Mailer: git-send-email 2.32.0 MIME-Version: 1.0 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 * guix/scripts/hash.scm (guix-hash): Allow several files. [directory?]: New procedure. [file-hash]: Catch system-error. [hash-to-display]: New procedure. --- guix/scripts/hash.scm | 56 +++++++++++++++++++++++++++---------------- 1 file changed, 36 insertions(+), 20 deletions(-) diff --git a/guix/scripts/hash.scm b/guix/scripts/hash.scm index b8622373cc..f3363549d3 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 Simon Tournier ;;; ;;; This file is part of GNU Guix. ;;; @@ -135,6 +136,11 @@ (define (vcs-file? file stat) (else #f))) + (define (directory? file) + (case (stat:type (stat file)) + ((directory) #t) + (else #f))) + (let* ((opts (parse-options)) (args (filter-map (match-lambda (('argument . value) @@ -149,27 +155,37 @@ (define (vcs-file? file stat) (define (file-hash file) ;; Compute the hash of FILE. ;; Catch and gracefully report possible '&nar-error' conditions. - (with-error-handling - (if (assoc-ref opts 'recursive?) + (if (and (assoc-ref opts 'recursive?) + (directory? file)) + (with-error-handling (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 args - ((file) - (catch 'system-error - (lambda () - (format #t "~a~%" (fmt (file-hash file)))) - (lambda args - (leave (G_ "~a~%") - (strerror (system-error-errno args)))))) - (x - (leave (G_ "wrong number of arguments~%")))))) + (get-hash))) + (catch 'system-error + (lambda _ + (call-with-input-file file + (cute port-hash (assoc-ref opts 'hash-algorithm) + <>))) + (lambda args + (when (directory? file) + (display-hint (G_ "Try @option{--recursive}."))) + (leave (G_ "~a ~a~%") + file + (strerror (system-error-errno args))))))) + + (define (hash-to-display thing) + (match thing + ((? file-exists? file) + (fmt (file-hash file))) + ("-" (with-error-handling + (fmt (port-hash (assoc-ref opts 'hash-algorithm) + (current-input-port))))) + (x + (leave (G_ "wrong argument~%"))))) + + (for-each + (lambda (arg) + (format #t "~a~%" (hash-to-display arg))) + args)))