1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2012, 2013, 2014, 2016, 2017, 2018 Ludovic Courtès <ludo@gnu.org>
3 ;;; Copyright © 2014 Mark H Weaver <mhw@netris.org>
4 ;;; Copyright © 2017 Huang Ying <huang.ying.caritas@gmail.com>
6 ;;; This file is part of GNU Guix.
8 ;;; GNU Guix is free software; you can redistribute it and/or modify it
9 ;;; under the terms of the GNU General Public License as published by
10 ;;; the Free Software Foundation; either version 3 of the License, or (at
11 ;;; your option) any later version.
13 ;;; GNU Guix is distributed in the hope that it will be useful, but
14 ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
15 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 ;;; GNU General Public License for more details.
18 ;;; You should have received a copy of the GNU General Public License
19 ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
21 (define-module (guix build union)
22 #:use-module (ice-9 match)
23 #:use-module (ice-9 format)
24 #:use-module (srfi srfi-1)
25 #:use-module (srfi srfi-26)
26 #:use-module (rnrs bytevectors)
27 #:use-module (rnrs io ports)
36 ;;; Build a directory that is the union of a set of directories, using
41 (define (files-in-directory dirname)
42 (let ((dir (opendir dirname)))
43 (let loop ((files '()))
49 (sort files string<?))
51 (loop (cons file files)))))))
53 (define (file-is-directory? file)
55 (#f #f) ;maybe a dangling symlink
56 (st (eq? 'directory (stat:type st)))))
58 (define (file=? file1 file2)
59 "Return #t if FILE1 and FILE2 are regular files and their contents are
60 identical, #f otherwise."
61 (let ((st1 (stat file1 #f))
62 (st2 (stat file2 #f)))
63 ;; When deduplication is enabled, identical files share the same inode.
65 (or (= (stat:ino st1) (stat:ino st2))
66 (and (eq? (stat:type st1) 'regular)
67 (eq? (stat:type st2) 'regular)
68 (= (stat:size st1) (stat:size st2))
69 (call-with-input-file file1
71 (call-with-input-file file2
74 (define buf1 (make-bytevector len))
75 (define buf2 (make-bytevector len))
77 (let ((n1 (get-bytevector-n! port1 buf1 0 len))
78 (n2 (get-bytevector-n! port2 buf2 0 len)))
83 (define (warn-about-collision files)
84 "Handle the collision among FILES by emitting a warning and choosing the
86 (format (current-error-port)
87 "~%warning: collision encountered:~%~{ ~a~%~}"
89 (let ((file (first files)))
90 (format (current-error-port) "warning: choosing ~a~%" file)
93 (define* (union-build output inputs
94 #:key (log-port (current-error-port))
95 (create-all-directories? #f)
97 (resolve-collision warn-about-collision))
98 "Build in the OUTPUT directory a symlink tree that is the union of all the
99 INPUTS, using SYMLINK to create symlinks. As a special case, if
100 CREATE-ALL-DIRECTORIES?, creates the subdirectories in the output directory to
101 make sure the caller can modify them later.
103 When two or more regular files collide, call RESOLVE-COLLISION with the list
104 of colliding files and use the one that it returns; or, if RESOLVE-COLLISION
105 returns #f, skip the faulty file altogether."
107 (define (symlink* input output)
108 (format log-port "`~a' ~~> `~a'~%" input output)
109 (symlink input output))
111 (define (resolve-collisions output dirs files)
113 ;; The inputs are all files.
114 (match (resolve-collision files)
117 (symlink* file output))))
120 ;; The inputs are a mixture of files and directories
121 (error "union-build: collision between file and directories"
122 `((files ,files) (dirs ,dirs))))))
124 (define (union output inputs)
127 ;; There's only one input, so just make a link unless
128 ;; create-all-directories?.
129 (if (and create-all-directories? (file-is-directory? input))
130 (union-of-directories output inputs)
131 (symlink* input output)))
133 (call-with-values (lambda () (partition file-is-directory? inputs))
136 ;; All inputs are directories.
137 (union-of-directories output dirs))
139 ((() (file (? (cut file=? <> file)) ...))
140 ;; There are no directories, and all files have the same contents,
141 ;; so there's no conflict.
142 (symlink* file output))
145 (resolve-collisions output dirs files)))))))
147 (define (union-of-directories output dirs)
148 ;; Create a new directory where we will merge the input directories.
151 ;; Build a hash table mapping each file to a list of input
152 ;; directories containing that file.
153 (let ((table (make-hash-table)))
155 (define (add-to-table! file dir)
156 (hash-set! table file (cons dir (hash-ref table file '()))))
158 ;; Populate the table.
159 (for-each (lambda (dir)
160 (for-each (cut add-to-table! <> dir)
161 (files-in-directory dir)))
164 ;; Now iterate over the table and recursively
165 ;; perform a union for each entry.
166 (hash-for-each (lambda (file dirs-with-file)
167 (union (string-append output "/" file)
168 (map (cut string-append <> "/" file)
169 (reverse dirs-with-file))))
172 (setvbuf (current-output-port) _IOLBF)
173 (setvbuf (current-error-port) _IOLBF)
174 (when (file-port? log-port)
175 (setvbuf log-port _IOLBF))
177 (union-of-directories output (delete-duplicates inputs)))
181 ;;; Relative symlinks.
185 (char-set-complement (char-set #\/)))
187 (define (relative-file-name reference file)
188 "Given REFERENCE and FILE, both of which are absolute file names, return the
189 file name of FILE relative to REFERENCE.
191 (relative-file-name \"/gnu/store/foo\" \"/gnu/store/bin/bar\")
194 Note that this is from a purely lexical standpoint; conversely, \"..\" is
195 *not* resolved lexically on POSIX in the presence of symlinks."
196 (if (and (string-prefix? "/" file) (string-prefix? "/" reference))
197 (let loop ((reference (string-tokenize reference %not-slash))
198 (file (string-tokenize file %not-slash)))
200 (string-join (append (make-list (length reference) "..") file)
211 (if (string=? head head*)
216 ;;; union.scm ends here