hash.scm 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2012, 2013, 2014, 2016, 2017, 2020, 2021 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. ;;;
  7. ;;; This file is part of GNU Guix.
  8. ;;;
  9. ;;; GNU Guix is free software; you can redistribute it and/or modify it
  10. ;;; under the terms of the GNU General Public License as published by
  11. ;;; the Free Software Foundation; either version 3 of the License, or (at
  12. ;;; your option) any later version.
  13. ;;;
  14. ;;; GNU Guix is distributed in the hope that it will be useful, but
  15. ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
  16. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. ;;; GNU General Public License for more details.
  18. ;;;
  19. ;;; You should have received a copy of the GNU General Public License
  20. ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
  21. (define-module (guix scripts hash)
  22. #:use-module (gcrypt hash)
  23. #:use-module (guix serialization)
  24. #:use-module (guix ui)
  25. #:use-module (guix scripts)
  26. #:use-module (guix base16)
  27. #:use-module (guix base32)
  28. #:autoload (guix base64) (base64-encode)
  29. #:use-module (ice-9 binary-ports)
  30. #:use-module (rnrs files)
  31. #:use-module (ice-9 match)
  32. #:use-module (srfi srfi-1)
  33. #:use-module (srfi srfi-11)
  34. #:use-module (srfi srfi-26)
  35. #:use-module (srfi srfi-37)
  36. #:export (guix-hash))
  37. ;;;
  38. ;;; Command-line options.
  39. ;;;
  40. (define %default-options
  41. ;; Alist of default option values.
  42. `((format . ,bytevector->nix-base32-string)
  43. (hash-algorithm . ,(hash-algorithm sha256))))
  44. (define (show-help)
  45. (display (G_ "Usage: guix hash [OPTION] FILE
  46. Return the cryptographic hash of FILE.\n"))
  47. (newline)
  48. (display (G_ "\
  49. Supported formats: 'base64', 'nix-base32' (default), 'base32',
  50. and 'base16' ('hex' and 'hexadecimal' can be used as well).\n"))
  51. (format #t (G_ "
  52. -x, --exclude-vcs exclude version control directories"))
  53. (format #t (G_ "
  54. -H, --hash=ALGORITHM use the given hash ALGORITHM"))
  55. (format #t (G_ "
  56. -f, --format=FMT write the hash in the given format"))
  57. (format #t (G_ "
  58. -r, --recursive compute the hash on FILE recursively"))
  59. (newline)
  60. (display (G_ "
  61. -h, --help display this help and exit"))
  62. (display (G_ "
  63. -V, --version display version information and exit"))
  64. (newline)
  65. (show-bug-report-information))
  66. (define %options
  67. ;; Specification of the command-line options.
  68. (list (option '(#\x "exclude-vcs") #f #f
  69. (lambda (opt name arg result)
  70. (alist-cons 'exclude-vcs? #t result)))
  71. (option '(#\H "hash") #t #f
  72. (lambda (opt name arg result)
  73. (match (lookup-hash-algorithm (string->symbol arg))
  74. (#f
  75. (leave (G_ "~a: unknown hash algorithm~%") arg))
  76. (algo
  77. (alist-cons 'hash-algorithm algo result)))))
  78. (option '(#\f "format") #t #f
  79. (lambda (opt name arg result)
  80. (define fmt-proc
  81. (match arg
  82. ("base64"
  83. base64-encode)
  84. ("nix-base32"
  85. bytevector->nix-base32-string)
  86. ("base32"
  87. bytevector->base32-string)
  88. ((or "base16" "hex" "hexadecimal")
  89. bytevector->base16-string)
  90. (x
  91. (leave (G_ "unsupported hash format: ~a~%")
  92. arg))))
  93. (alist-cons 'format fmt-proc
  94. (alist-delete 'format result))))
  95. (option '(#\r "recursive") #f #f
  96. (lambda (opt name arg result)
  97. (alist-cons 'recursive? #t result)))
  98. (option '(#\h "help") #f #f
  99. (lambda args
  100. (show-help)
  101. (exit 0)))
  102. (option '(#\V "version") #f #f
  103. (lambda args
  104. (show-version-and-exit "guix hash")))))
  105. ;;;
  106. ;;; Entry point.
  107. ;;;
  108. (define-command (guix-hash . args)
  109. (category packaging)
  110. (synopsis "compute the cryptographic hash of a file")
  111. (define (parse-options)
  112. ;; Return the alist of option values.
  113. (parse-command-line args %options (list %default-options)
  114. #:build-options? #f))
  115. (define (vcs-file? file stat)
  116. (case (stat:type stat)
  117. ((directory)
  118. (member (basename file) '(".bzr" ".git" ".hg" ".svn" "CVS")))
  119. ((regular)
  120. ;; Git sub-modules have a '.git' file that is a regular text file.
  121. (string=? (basename file) ".git"))
  122. (else
  123. #f)))
  124. (let* ((opts (parse-options))
  125. (args (filter-map (match-lambda
  126. (('argument . value)
  127. value)
  128. (_ #f))
  129. (reverse opts)))
  130. (fmt (assq-ref opts 'format))
  131. (select? (if (assq-ref opts 'exclude-vcs?)
  132. (negate vcs-file?)
  133. (const #t))))
  134. (define (file-hash file)
  135. ;; Compute the hash of FILE.
  136. ;; Catch and gracefully report possible '&nar-error' conditions.
  137. (with-error-handling
  138. (if (assoc-ref opts 'recursive?)
  139. (let-values (((port get-hash)
  140. (open-hash-port (assoc-ref opts 'hash-algorithm))))
  141. (write-file file port #:select? select?)
  142. (force-output port)
  143. (get-hash))
  144. (match file
  145. ("-" (port-hash (assoc-ref opts 'hash-algorithm)
  146. (current-input-port)))
  147. (_ (call-with-input-file file
  148. (cute port-hash (assoc-ref opts 'hash-algorithm)
  149. <>)))))))
  150. (match args
  151. ((file)
  152. (catch 'system-error
  153. (lambda ()
  154. (format #t "~a~%" (fmt (file-hash file))))
  155. (lambda args
  156. (leave (G_ "~a~%")
  157. (strerror (system-error-errno args))))))
  158. (x
  159. (leave (G_ "wrong number of arguments~%"))))))