store-deduplication.scm 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2018 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-64))
  28. (test-begin "store-deduplication")
  29. (test-equal "deduplicate"
  30. (cons* #t #f ;inode comparisons
  31. 2 (make-list 5 6)) ;'nlink' values
  32. (call-with-temporary-directory
  33. (lambda (store)
  34. (let ((data (string->utf8 "Hello, world!"))
  35. (identical (map (lambda (n)
  36. (string-append store "/" (number->string n)
  37. "/a/b/c"))
  38. (iota 5)))
  39. (unique (string-append store "/unique")))
  40. (for-each (lambda (file)
  41. (mkdir-p (dirname file))
  42. (call-with-output-file file
  43. (lambda (port)
  44. (put-bytevector port data))))
  45. identical)
  46. ;; Make the parent of IDENTICAL read-only. This should not prevent
  47. ;; deduplication from inserting its hard link.
  48. (chmod (dirname (second identical)) #o544)
  49. (call-with-output-file unique
  50. (lambda (port)
  51. (put-bytevector port (string->utf8 "This is unique."))))
  52. (deduplicate store (nar-sha256 store) #:store store)
  53. ;; (system (string-append "ls -lRia " store))
  54. (cons* (apply = (map (compose stat:ino stat) identical))
  55. (= (stat:ino (stat unique))
  56. (stat:ino (stat (car identical))))
  57. (stat:nlink (stat unique))
  58. (map (compose stat:nlink stat) identical))))))
  59. (test-equal "deduplicate, ENOSPC"
  60. (cons* #f ;inode comparison
  61. (append (make-list 3 4)
  62. (make-list 7 1))) ;'nlink' values
  63. ;; In this scenario the first 3 files are properly deduplicated and then we
  64. ;; simulate a full '.links' directory where link(2) gets ENOSPC, thereby
  65. ;; preventing deduplication of the subsequent files.
  66. (call-with-temporary-directory
  67. (lambda (store)
  68. (let ((true-link link)
  69. (links 0)
  70. (data1 (string->utf8 "Hello, world!"))
  71. (data2 (string->utf8 "Hi, world!"))
  72. (identical (map (lambda (n)
  73. (string-append store "/" (number->string n)
  74. "/a/b/c"))
  75. (iota 10)))
  76. (populate (lambda (data)
  77. (lambda (file)
  78. (mkdir-p (dirname file))
  79. (call-with-output-file file
  80. (lambda (port)
  81. (put-bytevector port data)))))))
  82. (for-each (populate data1) (take identical 5))
  83. (for-each (populate data2) (drop identical 5))
  84. (dynamic-wind
  85. (lambda ()
  86. (set! link (lambda (old new)
  87. (set! links (+ links 1))
  88. (if (<= links 3)
  89. (true-link old new)
  90. (throw 'system-error "link" "~A" '("Whaaat?!")
  91. (list ENOSPC))))))
  92. (lambda ()
  93. (deduplicate store (nar-sha256 store) #:store store))
  94. (lambda ()
  95. (set! link true-link)))
  96. (cons (apply = (map (compose stat:ino stat) identical))
  97. (map (compose stat:nlink stat) identical))))))
  98. (test-end "store-deduplication")