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