You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

154 lines
5.2 KiB

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