hash.scm 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2012-2014, 2016-2017, 2020-2022 Ludovic Courtès <ludo@gnu.org>
  3. ;;; Copyright © 2013 Nikita Karetnikov <nikita@karetnikov.org>
  4. ;;; Copyright © 2016 Jan Nieuwenhuizen <janneke@gnu.org>
  5. ;;; Copyright © 2018 Tim Gesthuizen <tim.gesthuizen@yahoo.de>
  6. ;;; Copyright © 2021 Simon Tournier <zimon.toutoune@gmail.com>
  7. ;;; Copyright © 2021 Sarah Morgensen <iskarian@mgsn.dev>
  8. ;;;
  9. ;;; This file is part of GNU Guix.
  10. ;;;
  11. ;;; GNU Guix is free software; you can redistribute it and/or modify it
  12. ;;; under the terms of the GNU General Public License as published by
  13. ;;; the Free Software Foundation; either version 3 of the License, or (at
  14. ;;; your option) any later version.
  15. ;;;
  16. ;;; GNU Guix is distributed in the hope that it will be useful, but
  17. ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
  18. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. ;;; GNU General Public License for more details.
  20. ;;;
  21. ;;; You should have received a copy of the GNU General Public License
  22. ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
  23. (define-module (guix scripts hash)
  24. #:use-module (gcrypt hash)
  25. #:use-module (guix ui)
  26. #:use-module (guix hash)
  27. #:use-module (guix scripts)
  28. #:use-module (guix base16)
  29. #:use-module (guix base32)
  30. #:autoload (guix base64) (base64-encode)
  31. #:use-module (ice-9 match)
  32. #:use-module (srfi srfi-1)
  33. #:use-module (srfi srfi-26)
  34. #:use-module (srfi srfi-37)
  35. #:autoload (disarchive git-hash) (git-hash-file git-hash-directory)
  36. #:export (guix-hash))
  37. ;;;
  38. ;;; Serializers
  39. ;;;
  40. (define* (nar-hash file #:optional
  41. (algorithm (assoc-ref %default-options 'hash-algorithm))
  42. select?)
  43. (file-hash* file #:algorithm algorithm #:select? select? #:recursive? #true))
  44. (define* (default-hash file #:optional
  45. (algorithm (assoc-ref %default-options 'hash-algorithm))
  46. select?)
  47. (match file
  48. ("-" (port-hash algorithm (current-input-port)))
  49. (_ (file-hash* file #:algorithm algorithm #:recursive? #false))))
  50. (define* (git-hash file #:optional
  51. (algorithm (assoc-ref %default-options 'hash-algorithm))
  52. select?)
  53. (define directory?
  54. (case (stat:type (stat file))
  55. ((directory) #t)
  56. (else #f)))
  57. (if directory?
  58. (git-hash-directory file algorithm #:select? select?)
  59. (git-hash-file file algorithm)))
  60. ;;;
  61. ;;; Command-line options.
  62. ;;;
  63. (define %default-options
  64. ;; Alist of default option values.
  65. `((format . ,bytevector->nix-base32-string)
  66. (hash-algorithm . ,(hash-algorithm sha256))
  67. (serializer . ,default-hash)))
  68. (define (show-help)
  69. (display (G_ "Usage: guix hash [OPTION] FILE
  70. Return the cryptographic hash of FILE.\n"))
  71. (newline)
  72. (display (G_ "\
  73. Supported formats: 'base64', 'nix-base32' (default), 'base32',
  74. and 'base16' ('hex' and 'hexadecimal' can be used as well).\n"))
  75. (format #t (G_ "
  76. -x, --exclude-vcs exclude version control directories"))
  77. (format #t (G_ "
  78. -H, --hash=ALGORITHM use the given hash ALGORITHM"))
  79. (format #t (G_ "
  80. -f, --format=FMT write the hash in the given format"))
  81. (format #t (G_ "
  82. -S, --serializer=TYPE compute the hash on FILE according to TYPE serialization"))
  83. (newline)
  84. (display (G_ "
  85. -h, --help display this help and exit"))
  86. (display (G_ "
  87. -V, --version display version information and exit"))
  88. (newline)
  89. (show-bug-report-information))
  90. (define %options
  91. ;; Specification of the command-line options.
  92. (list (option '(#\x "exclude-vcs") #f #f
  93. (lambda (opt name arg result)
  94. (alist-cons 'exclude-vcs? #t result)))
  95. (option '(#\H "hash") #t #f
  96. (lambda (opt name arg result)
  97. (match (lookup-hash-algorithm (string->symbol arg))
  98. (#f
  99. (leave (G_ "~a: unknown hash algorithm~%") arg))
  100. (algo
  101. (alist-cons 'hash-algorithm algo result)))))
  102. (option '(#\f "format") #t #f
  103. (lambda (opt name arg result)
  104. (define fmt-proc
  105. (match arg
  106. ("base64"
  107. base64-encode)
  108. ("nix-base32"
  109. bytevector->nix-base32-string)
  110. ("base32"
  111. bytevector->base32-string)
  112. ((or "base16" "hex" "hexadecimal")
  113. bytevector->base16-string)
  114. (x
  115. (leave (G_ "unsupported hash format: ~a~%")
  116. arg))))
  117. (alist-cons 'format fmt-proc
  118. (alist-delete 'format result))))
  119. (option '(#\r "recursive") #f #f
  120. (lambda (opt name arg result)
  121. (unless (eqv? name #\r)
  122. (warning (G_ "'--recursive' is deprecated, \
  123. use '--serializer=nar' instead~%")))
  124. (alist-cons 'serializer nar-hash
  125. (alist-delete 'serializer result))))
  126. (option '(#\S "serializer") #t #f
  127. (lambda (opt name arg result)
  128. (define serializer-proc
  129. (match arg
  130. ("none"
  131. default-hash)
  132. ("nar"
  133. nar-hash)
  134. ("git"
  135. git-hash)
  136. (x
  137. (leave (G_ "unsupported serializer type: ~a~%")
  138. arg))))
  139. (alist-cons 'serializer serializer-proc
  140. (alist-delete 'serializer result))))
  141. (option '(#\h "help") #f #f
  142. (lambda args
  143. (show-help)
  144. (exit 0)))
  145. (option '(#\V "version") #f #f
  146. (lambda args
  147. (show-version-and-exit "guix hash")))))
  148. ;;;
  149. ;;; Entry point.
  150. ;;;
  151. (define-command (guix-hash . args)
  152. (category packaging)
  153. (synopsis "compute the cryptographic hash of a file")
  154. (define (parse-options)
  155. ;; Return the alist of option values.
  156. (parse-command-line args %options (list %default-options)
  157. #:build-options? #f))
  158. (let* ((opts (parse-options))
  159. (args (filter-map (match-lambda
  160. (('argument . value)
  161. value)
  162. (_ #f))
  163. (reverse opts)))
  164. (fmt (assq-ref opts 'format))
  165. (select? (if (assq-ref opts 'exclude-vcs?)
  166. (negate vcs-file?)
  167. (const #t)))
  168. (algorithm (assoc-ref opts 'hash-algorithm))
  169. (serializer (assoc-ref opts 'serializer)))
  170. (define (file-hash file)
  171. ;; Compute the hash of FILE.
  172. ;; Catch and gracefully report possible error
  173. (catch 'system-error
  174. (lambda _
  175. (with-error-handling
  176. (serializer file algorithm select?)))
  177. (lambda args
  178. (leave (G_ "~a ~a~%")
  179. file
  180. (strerror (system-error-errno args))))))
  181. (define (formatted-hash thing)
  182. (fmt (file-hash thing)))
  183. (match args
  184. (()
  185. (leave (G_ "no arguments specified~%")))
  186. (_
  187. (for-each
  188. (compose (cute format #t "~a~%" <>) formatted-hash)
  189. args)))))