hash.scm 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2021 Sarah Morgensen <iskarian@mgsn.dev>
  3. ;;; Copyright © 2022 Maxime Devos <maximedevos@telenet.be>
  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 hash)
  20. #:use-module (gcrypt hash)
  21. #:use-module (guix serialization)
  22. #:use-module (srfi srfi-1)
  23. #:use-module (srfi srfi-11)
  24. #:export (vcs-file?
  25. file-hash*))
  26. (define (vcs-file? file stat)
  27. "Returns true if FILE is a version control system file."
  28. (case (stat:type stat)
  29. ((directory)
  30. (member (basename file) '(".bzr" ".git" ".hg" ".svn" "CVS")))
  31. ((regular)
  32. ;; Git sub-modules have a '.git' file that is a regular text file.
  33. (string=? (basename file) ".git"))
  34. (else
  35. #f)))
  36. (define* (file-hash* file #:key
  37. (algorithm (hash-algorithm sha256))
  38. (recursive? 'auto)
  39. (select? (negate vcs-file?)))
  40. "Compute the hash of FILE with ALGORITHM.
  41. Symbolic links are only dereferenced if RECURSIVE? is false.
  42. Directories are only supported if RECURSIVE? is #true or 'auto'.
  43. The executable bit is only recorded if RECURSIVE? is #true.
  44. If FILE is a symbolic link, it is only followed if RECURSIVE? is false.
  45. For regular files, there are two different hashes when the executable
  46. hash isn't recorded: the regular hash and the nar hash. In most situations,
  47. the regular hash is desired and setting RECURSIVE? to 'auto' does the right
  48. thing for both regular files and directories.
  49. This procedure must only be used under controlled circumstances;
  50. the detection of symbolic links in FILE is racy.
  51. When FILE is a directory, the procedure SELECT? called as (SELECT? FILE STAT)
  52. decides which files to include. By default, version control files are
  53. excluded. To include everything, SELECT? can be set to (const #true)."
  54. (if (or (eq? recursive? #true)
  55. (and (eq? recursive? 'auto)
  56. ;; Don't change this to (eq? 'directory ...), because otherwise
  57. ;; if 'file' denotes a symbolic link, the 'file-hash' below
  58. ;; would dereference it -- dereferencing symbolic links would
  59. ;; open an avoidable can of potential worms.
  60. (not (eq? 'regular (stat:type (lstat file))))))
  61. (let-values (((port get-hash)
  62. (open-hash-port algorithm)))
  63. (write-file file port #:select? select?)
  64. (force-output port)
  65. (get-hash))
  66. (file-hash algorithm file)))