From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mp2 ([2001:41d0:2:bcc0::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by ms0.migadu.com with LMTPS id 60smCwlKcGEcwgAAgWs5BA (envelope-from ) for ; Wed, 20 Oct 2021 18:55:37 +0200 Received: from aspmx1.migadu.com ([2001:41d0:2:bcc0::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by mp2 with LMTPS id OIBQBglKcGFvbQAAB5/wlQ (envelope-from ) for ; Wed, 20 Oct 2021 16:55:37 +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 943ED14831 for ; Wed, 20 Oct 2021 18:55:36 +0200 (CEST) Received: from localhost ([::1]:47114 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1mdEsJ-000194-OF for larch@yhetil.org; Wed, 20 Oct 2021 12:55:35 -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 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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=1634748936; 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=OCiq+B8eY3dy24gREyN0p0VuTvCUjlqt45JBpT64X1g=; b=OUoliLxnTa1txdxgecR9psAYW5lSsJ+nyn1hpVMFnPQp78WUWCM36vmTstBZ6PgqcLxKxH UuMl0XfndsHlaDWZXAvesfN3EmSdlezCpeBeA3WwGPdTAON3nd3OQZ1FtFJMnJNGKrHN+s J4AjKIbktVdNoPFN71Y/pejySluhavfpF64sFubuweMhr5NEANQRoQdbfVBd1jgfdX++Qm 1afsnmuc57dVGImComu7BOrk6wMtXMT6i8X+k/xjJQV8PZ/K2f3aja1D64pe0MhULkesHY fjTWHcoApQJwdQJwsDNSoCOGzhkWf7JvhOCtQAjNS+gNsAEl7ilPnY9GxcW1VA== ARC-Seal: i=1; s=key1; d=yhetil.org; t=1634748936; a=rsa-sha256; cv=none; b=lr9iUrgvZ1zP1PPVzfan6FbR4tCogK1C5aMM1ZQb7p/qaUKAwok4O73tbUv9y33pMP7sjZ 4CVjswxgDzft4GheoGYuIO1hgtzK2Q9JC4o2+mn9E8P6GA1NzUl+68yQpDN+6wG2Tkfn8f bPskEHafTLs4BxqiyjUAUP6SlZnf2DjEShnZHi3JOUcdEfezAr/vbeCcj+heH2KHarUDdn MHLPfKSCrUxu1enbLdHZUMEh4Na7Imu3som9o6gXzq6Ii/pOcSaHCnLCjhgRA3X61HSp9i wf+2inScDwk04P1kDVAdjYKsmV9jyMTw0mRRFFFP2hoP/qK4R3w4q+xMgJaeig== ARC-Authentication-Results: i=1; aspmx1.migadu.com; dkim=fail ("headers rsa verify failed") header.d=gmail.com header.s=20210112 header.b=iiZDbFfX; dmarc=fail reason="SPF not aligned (relaxed)" header.from=gmail.com (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: -1.73 Authentication-Results: aspmx1.migadu.com; dkim=fail ("headers rsa verify failed") header.d=gmail.com header.s=20210112 header.b=iiZDbFfX; dmarc=fail reason="SPF not aligned (relaxed)" header.from=gmail.com (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: 943ED14831 X-Spam-Score: -1.73 X-Migadu-Scanner: scn1.migadu.com X-TUID: 0NPTCOR8QAp6 * 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))) -- 2.32.0