hash.scm 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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 serialization)
  26. #:use-module (guix ui)
  27. #:use-module (guix hash)
  28. #:use-module (guix scripts)
  29. #:use-module (guix base16)
  30. #:use-module (guix base32)
  31. #:autoload (guix base64) (base64-encode)
  32. #:use-module (ice-9 binary-ports)
  33. #:use-module (rnrs files)
  34. #:use-module (ice-9 match)
  35. #:use-module (srfi srfi-1)
  36. #:use-module (srfi srfi-11)
  37. #:use-module (srfi srfi-26)
  38. #:use-module (srfi srfi-37)
  39. #:autoload (disarchive git-hash) (git-hash-file git-hash-directory)
  40. #:export (guix-hash))
  41. ;;;
  42. ;;; Serializers
  43. ;;;
  44. (define* (nar-hash file #:optional
  45. (algorithm (assoc-ref %default-options 'hash-algorithm))
  46. select?)
  47. (file-hash* file #:algorithm algorithm #:select? select? #:recursive? #true))
  48. (define* (default-hash file #:optional
  49. (algorithm (assoc-ref %default-options 'hash-algorithm))
  50. select?)
  51. (match file
  52. ("-" (port-hash algorithm (current-input-port)))
  53. (_ (file-hash* file #:algorithm algorithm #:recursive? #false))))
  54. (define* (git-hash file #:optional
  55. (algorithm (assoc-ref %default-options 'hash-algorithm))
  56. select?)
  57. (define directory?
  58. (case (stat:type (stat file))
  59. ((directory) #t)
  60. (else #f)))
  61. (if directory?
  62. (git-hash-directory file algorithm #:select? select?)
  63. (git-hash-file file algorithm)))
  64. ;;;
  65. ;;; Command-line options.
  66. ;;;
  67. (define %default-options
  68. ;; Alist of default option values.
  69. `((format . ,bytevector->nix-base32-string)
  70. (hash-algorithm . ,(hash-algorithm sha256))
  71. (serializer . ,default-hash)))
  72. (define (show-help)
  73. (display (G_ "Usage: guix hash [OPTION] FILE
  74. Return the cryptographic hash of FILE.\n"))
  75. (newline)
  76. (display (G_ "\
  77. Supported formats: 'base64', 'nix-base32' (default), 'base32',
  78. and 'base16' ('hex' and 'hexadecimal' can be used as well).\n"))
  79. (format #t (G_ "
  80. -x, --exclude-vcs exclude version control directories"))
  81. (format #t (G_ "
  82. -H, --hash=ALGORITHM use the given hash ALGORITHM"))
  83. (format #t (G_ "
  84. -f, --format=FMT write the hash in the given format"))
  85. (format #t (G_ "
  86. -S, --serializer=TYPE compute the hash on FILE according to TYPE serialization"))
  87. (newline)
  88. (display (G_ "
  89. -h, --help display this help and exit"))
  90. (display (G_ "
  91. -V, --version display version information and exit"))
  92. (newline)
  93. (show-bug-report-information))
  94. (define %options
  95. ;; Specification of the command-line options.
  96. (list (option '(#\x "exclude-vcs") #f #f
  97. (lambda (opt name arg result)
  98. (alist-cons 'exclude-vcs? #t result)))
  99. (option '(#\H "hash") #t #f
  100. (lambda (opt name arg result)
  101. (match (lookup-hash-algorithm (string->symbol arg))
  102. (#f
  103. (leave (G_ "~a: unknown hash algorithm~%") arg))
  104. (algo
  105. (alist-cons 'hash-algorithm algo result)))))
  106. (option '(#\f "format") #t #f
  107. (lambda (opt name arg result)
  108. (define fmt-proc
  109. (match arg
  110. ("base64"
  111. base64-encode)
  112. ("nix-base32"
  113. bytevector->nix-base32-string)
  114. ("base32"
  115. bytevector->base32-string)
  116. ((or "base16" "hex" "hexadecimal")
  117. bytevector->base16-string)
  118. (x
  119. (leave (G_ "unsupported hash format: ~a~%")
  120. arg))))
  121. (alist-cons 'format fmt-proc
  122. (alist-delete 'format result))))
  123. (option '(#\r "recursive") #f #f
  124. (lambda (opt name arg result)
  125. (unless (eqv? name #\r)
  126. (warning (G_ "'--recursive' is deprecated, \
  127. use '--serializer=nar' instead~%")))
  128. (alist-cons 'serializer nar-hash
  129. (alist-delete 'serializer result))))
  130. (option '(#\S "serializer") #t #f
  131. (lambda (opt name arg result)
  132. (define serializer-proc
  133. (match arg
  134. ("none"
  135. default-hash)
  136. ("nar"
  137. nar-hash)
  138. ("git"
  139. git-hash)
  140. (x
  141. (leave (G_ "unsupported serializer type: ~a~%")
  142. arg))))
  143. (alist-cons 'serializer serializer-proc
  144. (alist-delete 'serializer result))))
  145. (option '(#\h "help") #f #f
  146. (lambda args
  147. (show-help)
  148. (exit 0)))
  149. (option '(#\V "version") #f #f
  150. (lambda args
  151. (show-version-and-exit "guix hash")))))
  152. ;;;
  153. ;;; Entry point.
  154. ;;;
  155. (define-command (guix-hash . args)
  156. (category packaging)
  157. (synopsis "compute the cryptographic hash of a file")
  158. (define (parse-options)
  159. ;; Return the alist of option values.
  160. (parse-command-line args %options (list %default-options)
  161. #:build-options? #f))
  162. (let* ((opts (parse-options))
  163. (args (filter-map (match-lambda
  164. (('argument . value)
  165. value)
  166. (_ #f))
  167. (reverse opts)))
  168. (fmt (assq-ref opts 'format))
  169. (select? (if (assq-ref opts 'exclude-vcs?)
  170. (negate vcs-file?)
  171. (const #t)))
  172. (algorithm (assoc-ref opts 'hash-algorithm))
  173. (serializer (assoc-ref opts 'serializer)))
  174. (define (file-hash file)
  175. ;; Compute the hash of FILE.
  176. ;; Catch and gracefully report possible error
  177. (catch 'system-error
  178. (lambda _
  179. (with-error-handling
  180. (serializer file algorithm select?)))
  181. (lambda args
  182. (leave (G_ "~a ~a~%")
  183. file
  184. (strerror (system-error-errno args))))))
  185. (define (formatted-hash thing)
  186. (fmt (file-hash thing)))
  187. (match args
  188. (()
  189. (leave (G_ "no arguments specified~%")))
  190. (_
  191. (for-each
  192. (compose (cute format #t "~a~%" <>) formatted-hash)
  193. args)))))