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)
37 ;;; Build a directory that is the union of a set of directories, using
42 (define (files-in-directory dirname)
43 (let ((dir (opendir dirname)))
44 (let loop ((files '()))
50 (sort files string<?))
52 (loop (cons file files)))))))
54 (define (file-is-directory? file)
56 (#f #f) ;maybe a dangling symlink
57 (st (eq? 'directory (stat:type st)))))
59 (define (file=? file1 file2)
60 "Return #t if FILE1 and FILE2 are regular files and their contents are
61 identical, #f otherwise."
62 (let ((st1 (stat file1 #f))
63 (st2 (stat file2 #f)))
64 ;; When deduplication is enabled, identical files share the same inode.
66 (or (= (stat:ino st1) (stat:ino st2))
67 (and (eq? (stat:type st1) 'regular)
68 (eq? (stat:type st2) 'regular)
69 (= (stat:size st1) (stat:size st2))
70 (call-with-input-file file1
72 (call-with-input-file file2
75 (define buf1 (make-bytevector len))
76 (define buf2 (make-bytevector len))
78 (let ((n1 (get-bytevector-n! port1 buf1 0 len))
79 (n2 (get-bytevector-n! port2 buf2 0 len)))
84 (define %harmless-collisions
85 ;; This is a list of files that are known to collide, but for which emitting
86 ;; a warning doesn't make sense. For example, "icon-theme.cache" is
87 ;; regenerated by a profile hook which shadows the file provided by
88 ;; individual packages, and "gschemas.compiled" is made available to
89 ;; applications via 'glib-or-gtk-build-system'.
90 '("icon-theme.cache" "gschemas.compiled"))
92 (define (warn-about-collision files)
93 "Handle the collision among FILES by emitting a warning and choosing the
95 (let ((file (first files)))
96 (unless (member (basename file) %harmless-collisions)
97 (format (current-error-port)
98 "~%warning: collision encountered:~%~{ ~a~%~}"
100 (format (current-error-port) "warning: choosing ~a~%" file))
103 (define* (union-build output inputs
104 #:key (log-port (current-error-port))
105 (create-all-directories? #f)
107 (resolve-collision warn-about-collision))
108 "Build in the OUTPUT directory a symlink tree that is the union of all the
109 INPUTS, using SYMLINK to create symlinks. As a special case, if
110 CREATE-ALL-DIRECTORIES?, creates the subdirectories in the output directory to
111 make sure the caller can modify them later.
113 When two or more regular files collide, call RESOLVE-COLLISION with the list
114 of colliding files and use the one that it returns; or, if RESOLVE-COLLISION
115 returns #f, skip the faulty file altogether."
117 (define (symlink* input output)
118 (format log-port "`~a' ~~> `~a'~%" input output)
119 (symlink input output))
121 (define (resolve-collisions output dirs files)
123 ;; The inputs are all files.
124 (match (resolve-collision files)
127 (symlink* file output))))
130 ;; The inputs are a mixture of files and directories
131 (error "union-build: collision between file and directories"
132 `((files ,files) (dirs ,dirs))))))
134 (define (union output inputs)
137 ;; There's only one input, so just make a link unless
138 ;; create-all-directories?.
139 (if (and create-all-directories? (file-is-directory? input))
140 (union-of-directories output inputs)
141 (symlink* input output)))
143 (call-with-values (lambda () (partition file-is-directory? inputs))
146 ;; All inputs are directories.
147 (union-of-directories output dirs))
149 ((() (file (? (cut file=? <> file)) ...))
150 ;; There are no directories, and all files have the same contents,
151 ;; so there's no conflict.
152 (symlink* file output))
155 (resolve-collisions output dirs files)))))))
157 (define (union-of-directories output dirs)
158 ;; Create a new directory where we will merge the input directories.
161 ;; Build a hash table mapping each file to a list of input
162 ;; directories containing that file.
163 (let ((table (make-hash-table)))
165 (define (add-to-table! file dir)
166 (hash-set! table file (cons dir (hash-ref table file '()))))
168 ;; Populate the table.
169 (for-each (lambda (dir)
170 (for-each (cut add-to-table! <> dir)
171 (files-in-directory dir)))
174 ;; Now iterate over the table and recursively
175 ;; perform a union for each entry.
176 (hash-for-each (lambda (file dirs-with-file)
177 (union (string-append output "/" file)
178 (map (cut string-append <> "/" file)
179 (reverse dirs-with-file))))
182 (setvbuf (current-output-port) _IOLBF)
183 (setvbuf (current-error-port) _IOLBF)
184 (when (file-port? log-port)
185 (setvbuf log-port _IOLBF))
187 (union-of-directories output (delete-duplicates inputs)))
191 ;;; Relative symlinks.
195 (char-set-complement (char-set #\/)))
197 (define (relative-file-name reference file)
198 "Given REFERENCE and FILE, both of which are absolute file names, return the
199 file name of FILE relative to REFERENCE.
201 (relative-file-name \"/gnu/store/foo\" \"/gnu/store/bin/bar\")
204 Note that this is from a purely lexical standpoint; conversely, \"..\" is
205 *not* resolved lexically on POSIX in the presence of symlinks."
206 (if (and (string-prefix? "/" file) (string-prefix? "/" reference))
207 (let loop ((reference (string-tokenize reference %not-slash))
208 (file (string-tokenize file %not-slash)))
210 (string-join (append (make-list (length reference) "..") file)
221 (if (string=? head head*)
226 (define (symlink-relative old new)
227 "Assuming both OLD and NEW are absolute file names, make NEW a symlink to
228 OLD, but using a relative file name."
229 (symlink (relative-file-name (dirname new) old)
232 ;;; union.scm ends here