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.

137 lines
4.5 KiB

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