1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2018, 2020 Ludovic Courtès <ludo@gnu.org>
4 ;;; This file is part of GNU Guix.
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.
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.
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/>.
19 (define-module (test-store-deduplication)
20 #:use-module (guix tests)
21 #:use-module (guix store deduplication)
22 #:use-module (gcrypt hash)
23 #:use-module ((guix utils) #:select (call-with-temporary-directory))
24 #:use-module (guix build utils)
25 #:use-module (rnrs bytevectors)
26 #:use-module (ice-9 binary-ports)
27 #:use-module (srfi srfi-1)
28 #:use-module (srfi srfi-26)
29 #:use-module (srfi srfi-64))
31 (test-begin "store-deduplication")
33 (test-equal "deduplicate"
34 (cons* #t #f ;inode comparisons
35 2 (make-list 5 6)) ;'nlink' values
37 (call-with-temporary-directory
39 (let ((data (string->utf8 "Hello, world!"))
40 (identical (map (lambda (n)
41 (string-append store "/" (number->string n)
44 (unique (string-append store "/unique")))
45 (for-each (lambda (file)
46 (mkdir-p (dirname file))
47 (call-with-output-file file
49 (put-bytevector port data))))
51 ;; Make the parent of IDENTICAL read-only. This should not prevent
52 ;; deduplication from inserting its hard link.
53 (chmod (dirname (second identical)) #o544)
55 (call-with-output-file unique
57 (put-bytevector port (string->utf8 "This is unique."))))
59 (deduplicate store (nar-sha256 store) #:store store)
61 ;; (system (string-append "ls -lRia " store))
62 (cons* (apply = (map (compose stat:ino stat) identical))
63 (= (stat:ino (stat unique))
64 (stat:ino (stat (car identical))))
65 (stat:nlink (stat unique))
66 (map (compose stat:nlink stat) identical))))))
68 (test-equal "deduplicate, ENOSPC"
69 (cons* #f ;inode comparison
70 (append (make-list 3 4)
71 (make-list 7 1))) ;'nlink' values
73 ;; In this scenario the first 3 files are properly deduplicated and then we
74 ;; simulate a full '.links' directory where link(2) gets ENOSPC, thereby
75 ;; preventing deduplication of the subsequent files.
76 (call-with-temporary-directory
78 (let ((true-link link)
80 (data1 (string->utf8 "Hello, world!"))
81 (data2 (string->utf8 "Hi, world!"))
82 (identical (map (lambda (n)
83 (string-append store "/" (number->string n)
86 (populate (lambda (data)
88 (mkdir-p (dirname file))
89 (call-with-output-file file
91 (put-bytevector port data)))))))
92 (for-each (populate data1) (take identical 5))
93 (for-each (populate data2) (drop identical 5))
96 (set! link (lambda (old new)
97 (set! links (+ links 1))
100 (throw 'system-error "link" "~A" '("Whaaat?!")
103 (deduplicate store (nar-sha256 store) #:store store))
105 (set! link true-link)))
107 (cons (apply = (map (compose stat:ino stat) identical))
108 (map (compose stat:nlink stat) identical))))))
110 (test-assert "copy-file/deduplicate"
111 (call-with-temporary-directory
113 (let ((source (search-path %load-path "gnu/packages/emacs-xyz.scm")))
114 (for-each (lambda (target)
115 (copy-file/deduplicate source
116 (string-append store target)
119 (and (directory-exists? (string-append store "/.links"))
120 (file=? source (string-append store "/a"))
121 (apply = (map (compose stat:ino stat
122 (cut string-append store <>))
123 '("/a" "/b" "/c"))))))))
125 (test-end "store-deduplication")