store-deduplication.scm 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2018, 2020 Ludovic Courtès <ludo@gnu.org>
  3. ;;;
  4. ;;; This file is part of GNU Guix.
  5. ;;;
  6. ;;; GNU Guix is free software; you can redistribute it and/or modify it
  7. ;;; under the terms of the GNU General Public License as published by
  8. ;;; the Free Software Foundation; either version 3 of the License, or (at
  9. ;;; your option) any later version.
  10. ;;;
  11. ;;; GNU Guix is distributed in the hope that it will be useful, but
  12. ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
  13. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. ;;; GNU General Public License for more details.
  15. ;;;
  16. ;;; You should have received a copy of the GNU General Public License
  17. ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
  18. (define-module (test-store-deduplication)
  19. #:use-module (guix tests)
  20. #:use-module (guix store deduplication)
  21. #:use-module (gcrypt hash)
  22. #:use-module ((guix utils) #:select (call-with-temporary-directory))
  23. #:use-module (guix build utils)
  24. #:use-module (rnrs bytevectors)
  25. #:use-module (ice-9 binary-ports)
  26. #:use-module (srfi srfi-1)
  27. #:use-module (srfi srfi-26)
  28. #:use-module (srfi srfi-64))
  29. (test-begin "store-deduplication")
  30. (test-equal "deduplicate"
  31. (cons* #t #f ;inode comparisons
  32. 2 (make-list 5 6)) ;'nlink' values
  33. (call-with-temporary-directory
  34. (lambda (store)
  35. (let ((data (string->utf8 "Hello, world!"))
  36. (identical (map (lambda (n)
  37. (string-append store "/" (number->string n)
  38. "/a/b/c"))
  39. (iota 5)))
  40. (unique (string-append store "/unique")))
  41. (for-each (lambda (file)
  42. (mkdir-p (dirname file))
  43. (call-with-output-file file
  44. (lambda (port)
  45. (put-bytevector port data))))
  46. identical)
  47. ;; Make the parent of IDENTICAL read-only. This should not prevent
  48. ;; deduplication from inserting its hard link.
  49. (chmod (dirname (second identical)) #o544)
  50. (call-with-output-file unique
  51. (lambda (port)
  52. (put-bytevector port (string->utf8 "This is unique."))))
  53. (deduplicate store (nar-sha256 store) #:store store)
  54. ;; (system (string-append "ls -lRia " store))
  55. (cons* (apply = (map (compose stat:ino stat) identical))
  56. (= (stat:ino (stat unique))
  57. (stat:ino (stat (car identical))))
  58. (stat:nlink (stat unique))
  59. (map (compose stat:nlink stat) identical))))))
  60. (test-equal "deduplicate, ENOSPC"
  61. (cons* #f ;inode comparison
  62. (append (make-list 3 4)
  63. (make-list 7 1))) ;'nlink' values
  64. ;; In this scenario the first 3 files are properly deduplicated and then we
  65. ;; simulate a full '.links' directory where link(2) gets ENOSPC, thereby
  66. ;; preventing deduplication of the subsequent files.
  67. (call-with-temporary-directory
  68. (lambda (store)
  69. (let ((true-link link)
  70. (links 0)
  71. (data1 (string->utf8 "Hello, world!"))
  72. (data2 (string->utf8 "Hi, world!"))
  73. (identical (map (lambda (n)
  74. (string-append store "/" (number->string n)
  75. "/a/b/c"))
  76. (iota 10)))
  77. (populate (lambda (data)
  78. (lambda (file)
  79. (mkdir-p (dirname file))
  80. (call-with-output-file file
  81. (lambda (port)
  82. (put-bytevector port data)))))))
  83. (for-each (populate data1) (take identical 5))
  84. (for-each (populate data2) (drop identical 5))
  85. (dynamic-wind
  86. (lambda ()
  87. (set! link (lambda (old new)
  88. (set! links (+ links 1))
  89. (if (<= links 4)
  90. (true-link old new)
  91. (throw 'system-error "link" "~A" '("Whaaat?!")
  92. (list ENOSPC))))))
  93. (lambda ()
  94. (deduplicate store (nar-sha256 store) #:store store))
  95. (lambda ()
  96. (set! link true-link)))
  97. (cons (apply = (map (compose stat:ino stat) identical))
  98. (map (compose stat:nlink stat) identical))))))
  99. (test-assert "copy-file/deduplicate"
  100. (call-with-temporary-directory
  101. (lambda (store)
  102. (let ((source (search-path %load-path "gnu/packages/emacs-xyz.scm")))
  103. (for-each (lambda (target)
  104. (copy-file/deduplicate source
  105. (string-append store target)
  106. #:store store))
  107. '("/a" "/b" "/c"))
  108. (and (directory-exists? (string-append store "/.links"))
  109. (file=? source (string-append store "/a"))
  110. (apply = (map (compose stat:ino stat
  111. (cut string-append store <>))
  112. '("/a" "/b" "/c"))))))))
  113. (test-end "store-deduplication")