store-deduplication.scm 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2018, 2020-2022 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, below %deduplication-minimum-size"
  31. (list #t (make-list 5 1))
  32. (call-with-temporary-directory
  33. (lambda (store)
  34. ;; Note: DATA must be longer than %DEDUPLICATION-MINIMUM-SIZE.
  35. (let ((data "Hello, world!")
  36. (identical (map (lambda (n)
  37. (string-append store "/" (number->string n)
  38. "/a/b/c"))
  39. (iota 5))))
  40. (for-each (lambda (file)
  41. (mkdir-p (dirname file))
  42. (call-with-output-file file
  43. (lambda (port)
  44. (put-bytevector port (string->utf8 data)))))
  45. identical)
  46. (deduplicate store (nar-sha256 store) #:store store)
  47. ;; (system (string-append "ls -lRia " store))
  48. (list (= (length (delete-duplicates
  49. (map (compose stat:ino stat) identical)))
  50. (length identical))
  51. (map (compose stat:nlink stat) identical))))))
  52. (test-equal "deduplicate"
  53. (cons* #t #f ;inode comparisons
  54. 2 (make-list 5 6)) ;'nlink' values
  55. (call-with-temporary-directory
  56. (lambda (store)
  57. ;; Note: DATA must be longer than %DEDUPLICATION-MINIMUM-SIZE.
  58. (let ((data (string-concatenate (make-list 1000 "Hello, world!")))
  59. (identical (map (lambda (n)
  60. (string-append store "/" (number->string n)
  61. "/a/b/c"))
  62. (iota 5)))
  63. (unique (string-append store "/unique")))
  64. (for-each (lambda (file)
  65. (mkdir-p (dirname file))
  66. (call-with-output-file file
  67. (lambda (port)
  68. (put-bytevector port (string->utf8 data)))))
  69. identical)
  70. ;; Make the parent of IDENTICAL read-only. This should not prevent
  71. ;; deduplication from inserting its hard link.
  72. (chmod (dirname (second identical)) #o544)
  73. (call-with-output-file unique
  74. (lambda (port)
  75. (put-bytevector port (string->utf8 (string-reverse data)))))
  76. (deduplicate store (nar-sha256 store) #:store store)
  77. ;; (system (string-append "ls -lRia " store))
  78. (cons* (apply = (map (compose stat:ino stat) identical))
  79. (= (stat:ino (stat unique))
  80. (stat:ino (stat (car identical))))
  81. (stat:nlink (stat unique))
  82. (map (compose stat:nlink stat) identical))))))
  83. (test-equal "deduplicate, ENOSPC"
  84. (cons* #f ;inode comparison
  85. (append (make-list 3 4)
  86. (make-list 7 1))) ;'nlink' values
  87. ;; In this scenario the first 3 files are properly deduplicated and then we
  88. ;; simulate a full '.links' directory where link(2) gets ENOSPC, thereby
  89. ;; preventing deduplication of the subsequent files.
  90. (call-with-temporary-directory
  91. (lambda (store)
  92. (let ((true-link link)
  93. (links 0)
  94. (data1 (string->utf8
  95. (string-concatenate (make-list 1000 "Hello, world!"))))
  96. (data2 (string->utf8
  97. (string-concatenate (make-list 1000 "Hi, world!"))))
  98. (identical (map (lambda (n)
  99. (string-append store "/" (number->string n)
  100. "/a/b/c"))
  101. (iota 10)))
  102. (populate (lambda (data)
  103. (lambda (file)
  104. (mkdir-p (dirname file))
  105. (call-with-output-file file
  106. (lambda (port)
  107. (put-bytevector port data)))))))
  108. (for-each (populate data1) (take identical 5))
  109. (for-each (populate data2) (drop identical 5))
  110. (dynamic-wind
  111. (lambda ()
  112. (set! link (lambda (old new)
  113. (set! links (+ links 1))
  114. (if (<= links 4)
  115. (true-link old new)
  116. (throw 'system-error "link" "~A" '("Whaaat?!")
  117. (list ENOSPC))))))
  118. (lambda ()
  119. (deduplicate store (nar-sha256 store) #:store store))
  120. (lambda ()
  121. (set! link true-link)))
  122. (cons (apply = (map (compose stat:ino stat) identical))
  123. (map (compose stat:nlink stat) identical))))))
  124. (test-assert "copy-file/deduplicate, below %deduplication-minimum-size"
  125. (call-with-temporary-directory
  126. (lambda (store)
  127. (let ((source (string-append store "/input")))
  128. (call-with-output-file source
  129. (lambda (port)
  130. (display "Hello!\n" port)))
  131. (copy-file/deduplicate source
  132. (string-append store "/a")
  133. #:store store)
  134. (and (not (directory-exists? (string-append store "/.links")))
  135. (file=? source (string-append store "/a"))
  136. (not (= (stat:ino (stat (string-append store "/a")))
  137. (stat:ino (stat source)))))))))
  138. (test-assert "copy-file/deduplicate"
  139. (call-with-temporary-directory
  140. (lambda (store)
  141. (let ((source (search-path %load-path "gnu/packages/emacs-xyz.scm")))
  142. (for-each (lambda (target)
  143. (copy-file/deduplicate source
  144. (string-append store target)
  145. #:store store))
  146. '("/a" "/b" "/c"))
  147. (and (directory-exists? (string-append store "/.links"))
  148. (file=? source (string-append store "/a"))
  149. (apply = (map (compose stat:ino stat
  150. (cut string-append store <>))
  151. '("/a" "/b" "/c"))))))))
  152. (test-end "store-deduplication")