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