gnu: ikiwiki: Add missing input.
[jackhill/guix/guix.git] / guix / utils.scm
1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019, 2020 Ludovic Courtès <ludo@gnu.org>
3 ;;; Copyright © 2013, 2014, 2015 Mark H Weaver <mhw@netris.org>
4 ;;; Copyright © 2014 Eric Bavier <bavier@member.fsf.org>
5 ;;; Copyright © 2014 Ian Denhardt <ian@zenhack.net>
6 ;;; Copyright © 2016 Mathieu Lirzin <mthl@gnu.org>
7 ;;; Copyright © 2015 David Thompson <davet@gnu.org>
8 ;;; Copyright © 2017 Mathieu Othacehe <m.othacehe@gmail.com>
9 ;;; Copyright © 2018 Marius Bakke <mbakke@fastmail.com>
10 ;;;
11 ;;; This file is part of GNU Guix.
12 ;;;
13 ;;; GNU Guix is free software; you can redistribute it and/or modify it
14 ;;; under the terms of the GNU General Public License as published by
15 ;;; the Free Software Foundation; either version 3 of the License, or (at
16 ;;; your option) any later version.
17 ;;;
18 ;;; GNU Guix is distributed in the hope that it will be useful, but
19 ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 ;;; GNU General Public License for more details.
22 ;;;
23 ;;; You should have received a copy of the GNU General Public License
24 ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
25
26 (define-module (guix utils)
27 #:use-module (guix config)
28 #:use-module (srfi srfi-1)
29 #:use-module (srfi srfi-9)
30 #:use-module (srfi srfi-11)
31 #:use-module (srfi srfi-26)
32 #:use-module (srfi srfi-35)
33 #:use-module (srfi srfi-39)
34 #:use-module (ice-9 ftw)
35 #:use-module (rnrs io ports) ;need 'port-position' etc.
36 #:use-module ((rnrs bytevectors) #:select (bytevector-u8-set!))
37 #:use-module (guix memoization)
38 #:use-module ((guix build utils) #:select (dump-port mkdir-p delete-file-recursively))
39 #:use-module ((guix build syscalls) #:select (mkdtemp! fdatasync))
40 #:use-module (ice-9 format)
41 #:use-module (ice-9 regex)
42 #:use-module (ice-9 match)
43 #:use-module (ice-9 format)
44 #:use-module ((ice-9 iconv) #:prefix iconv:)
45 #:use-module (system foreign)
46 #:re-export (memoize) ; for backwards compatibility
47 #:export (strip-keyword-arguments
48 default-keyword-arguments
49 substitute-keyword-arguments
50 ensure-keyword-arguments
51
52 current-source-directory
53
54 <location>
55 location
56 location?
57 location-file
58 location-line
59 location-column
60 source-properties->location
61 location->source-properties
62
63 &error-location
64 error-location?
65 error-location
66
67 &fix-hint
68 fix-hint?
69 condition-fix-hint
70
71 nix-system->gnu-triplet
72 gnu-triplet->nix-system
73 %current-system
74 %current-target-system
75 package-name->name+version
76 target-mingw?
77 target-arm32?
78 target-aarch64?
79 target-arm?
80 target-64bit?
81 version-compare
82 version>?
83 version>=?
84 version-prefix
85 version-major+minor
86 version-major
87 guile-version>?
88 version-prefix?
89 string-replace-substring
90 arguments-from-environment-variable
91 file-extension
92 file-sans-extension
93 tarball-sans-extension
94 compressed-file?
95 switch-symlinks
96 call-with-temporary-output-file
97 call-with-temporary-directory
98 with-atomic-file-output
99
100 config-directory
101 cache-directory
102
103 readlink*
104 edit-expression
105
106 filtered-port
107 compressed-port
108 decompressed-port
109 call-with-decompressed-port
110 compressed-output-port
111 call-with-compressed-output-port
112 canonical-newline-port))
113
114 \f
115 ;;;
116 ;;; Filtering & pipes.
117 ;;;
118
119 (define (filtered-port command input)
120 "Return an input port where data drained from INPUT is filtered through
121 COMMAND (a list). In addition, return a list of PIDs that the caller must
122 wait. When INPUT is a file port, it must be unbuffered; otherwise, any
123 buffered data is lost."
124 (let loop ((input input)
125 (pids '()))
126 (if (file-port? input)
127 (match (pipe)
128 ((in . out)
129 (match (primitive-fork)
130 (0
131 (dynamic-wind
132 (const #f)
133 (lambda ()
134 (close-port in)
135 (close-port (current-input-port))
136 (dup2 (fileno input) 0)
137 (close-port (current-output-port))
138 (dup2 (fileno out) 1)
139 (catch 'system-error
140 (lambda ()
141 (apply execl (car command) command))
142 (lambda args
143 (format (current-error-port)
144 "filtered-port: failed to execute '~{~a ~}': ~a~%"
145 command (strerror (system-error-errno args))))))
146 (lambda ()
147 (primitive-_exit 1))))
148 (child
149 (close-port out)
150 (values in (cons child pids))))))
151
152 ;; INPUT is not a file port, so fork just for the sake of tunneling it
153 ;; through a file port.
154 (match (pipe)
155 ((in . out)
156 (match (primitive-fork)
157 (0
158 (dynamic-wind
159 (const #t)
160 (lambda ()
161 (close-port in)
162 (dump-port input out))
163 (lambda ()
164 (close-port input)
165 (false-if-exception (close out))
166 (primitive-_exit 0))))
167 (child
168 (close-port input)
169 (close-port out)
170 (loop in (cons child pids)))))))))
171
172 (define (lzip-port proc port . args)
173 "Return the lzip port produced by calling PROC (a symbol) on PORT and ARGS.
174 Raise an error if lzlib support is missing."
175 (let* ((lzlib (false-if-exception (resolve-interface '(guix lzlib))))
176 (supported? (and lzlib
177 ((module-ref lzlib 'lzlib-available?)))))
178 (if supported?
179 (let ((make-port (module-ref lzlib proc)))
180 (values (make-port port) '()))
181 (error "lzip compression not supported" lzlib))))
182
183 (define (decompressed-port compression input)
184 "Return an input port where INPUT is decompressed according to COMPRESSION,
185 a symbol such as 'xz."
186 (match compression
187 ((or #f 'none) (values input '()))
188 ('bzip2 (filtered-port `(,%bzip2 "-dc") input))
189 ('xz (filtered-port `(,%xz "-dc") input))
190 ('gzip (filtered-port `(,%gzip "-dc") input))
191 ('lzip (values (lzip-port 'make-lzip-input-port input)
192 '()))
193 (_ (error "unsupported compression scheme" compression))))
194
195 (define (compressed-port compression input)
196 "Return an input port where INPUT is compressed according to COMPRESSION,
197 a symbol such as 'xz."
198 (match compression
199 ((or #f 'none) (values input '()))
200 ('bzip2 (filtered-port `(,%bzip2 "-c") input))
201 ('xz (filtered-port `(,%xz "-c") input))
202 ('gzip (filtered-port `(,%gzip "-c") input))
203 ('lzip (values (lzip-port 'make-lzip-input-port/compressed input)
204 '()))
205 (_ (error "unsupported compression scheme" compression))))
206
207 (define (call-with-decompressed-port compression port proc)
208 "Call PROC with a wrapper around PORT, a file port, that decompresses data
209 read from PORT according to COMPRESSION, a symbol such as 'xz."
210 (let-values (((decompressed pids)
211 (decompressed-port compression port)))
212 (dynamic-wind
213 (const #f)
214 (lambda ()
215 (proc decompressed))
216 (lambda ()
217 (close-port decompressed)
218 (unless (every (compose zero? cdr waitpid) pids)
219 (error "decompressed-port failure" pids))))))
220
221 (define (filtered-output-port command output)
222 "Return an output port. Data written to that port is filtered through
223 COMMAND and written to OUTPUT, an output file port. In addition, return a
224 list of PIDs to wait for. OUTPUT must be unbuffered; otherwise, any buffered
225 data is lost."
226 (match (pipe)
227 ((in . out)
228 (match (primitive-fork)
229 (0
230 (dynamic-wind
231 (const #f)
232 (lambda ()
233 (close-port out)
234 (close-port (current-input-port))
235 (dup2 (fileno in) 0)
236 (close-port (current-output-port))
237 (dup2 (fileno output) 1)
238 (catch 'system-error
239 (lambda ()
240 (apply execl (car command) command))
241 (lambda args
242 (format (current-error-port)
243 "filtered-output-port: failed to execute '~{~a ~}': ~a~%"
244 command (strerror (system-error-errno args))))))
245 (lambda ()
246 (primitive-_exit 1))))
247 (child
248 (close-port in)
249 (values out (list child)))))))
250
251 (define* (compressed-output-port compression output
252 #:key (options '()))
253 "Return an output port whose input is compressed according to COMPRESSION,
254 a symbol such as 'xz, and then written to OUTPUT. In addition return a list
255 of PIDs to wait for. OPTIONS is a list of strings passed to the compression
256 program--e.g., '(\"--fast\")."
257 (match compression
258 ((or #f 'none) (values output '()))
259 ('bzip2 (filtered-output-port `(,%bzip2 "-c" ,@options) output))
260 ('xz (filtered-output-port `(,%xz "-c" ,@options) output))
261 ('gzip (filtered-output-port `(,%gzip "-c" ,@options) output))
262 ('lzip (values (lzip-port 'make-lzip-output-port output)
263 '()))
264 (_ (error "unsupported compression scheme" compression))))
265
266 (define* (call-with-compressed-output-port compression port proc
267 #:key (options '()))
268 "Call PROC with a wrapper around PORT, a file port, that compresses data
269 that goes to PORT according to COMPRESSION, a symbol such as 'xz. OPTIONS is
270 a list of command-line arguments passed to the compression program."
271 (let-values (((compressed pids)
272 (compressed-output-port compression port
273 #:options options)))
274 (dynamic-wind
275 (const #f)
276 (lambda ()
277 (proc compressed))
278 (lambda ()
279 (close-port compressed)
280 (unless (every (compose zero? cdr waitpid) pids)
281 (error "compressed-output-port failure" pids))))))
282
283 (define* (edit-expression source-properties proc #:key (encoding "UTF-8"))
284 "Edit the expression specified by SOURCE-PROPERTIES using PROC, which should
285 be a procedure that takes the original expression in string and returns a new
286 one. ENCODING will be used to interpret all port I/O, it default to UTF-8.
287 This procedure returns #t on success."
288 (with-fluids ((%default-port-encoding encoding))
289 (let* ((file (assq-ref source-properties 'filename))
290 (line (assq-ref source-properties 'line))
291 (column (assq-ref source-properties 'column))
292 (in (open-input-file file))
293 ;; The start byte position of the expression.
294 (start (begin (while (not (and (= line (port-line in))
295 (= column (port-column in))))
296 (when (eof-object? (read-char in))
297 (error (format #f "~a: end of file~%" in))))
298 (ftell in)))
299 ;; The end byte position of the expression.
300 (end (begin (read in) (ftell in))))
301 (seek in 0 SEEK_SET) ; read from the beginning of the file.
302 (let* ((pre-bv (get-bytevector-n in start))
303 ;; The expression in string form.
304 (str (iconv:bytevector->string
305 (get-bytevector-n in (- end start))
306 (port-encoding in)))
307 (post-bv (get-bytevector-all in))
308 (str* (proc str)))
309 ;; Verify the edited expression is still a scheme expression.
310 (call-with-input-string str* read)
311 ;; Update the file with edited expression.
312 (with-atomic-file-output file
313 (lambda (out)
314 (put-bytevector out pre-bv)
315 (display str* out)
316 ;; post-bv maybe the end-of-file object.
317 (when (not (eof-object? post-bv))
318 (put-bytevector out post-bv))
319 #t))))))
320
321 \f
322 ;;;
323 ;;; Keyword arguments.
324 ;;;
325
326 (define (strip-keyword-arguments keywords args)
327 "Remove all of the keyword arguments listed in KEYWORDS from ARGS."
328 (let loop ((args args)
329 (result '()))
330 (match args
331 (()
332 (reverse result))
333 (((? keyword? kw) arg . rest)
334 (loop rest
335 (if (memq kw keywords)
336 result
337 (cons* arg kw result))))
338 ((head . tail)
339 (loop tail (cons head result))))))
340
341 (define (default-keyword-arguments args defaults)
342 "Return ARGS augmented with any keyword/value from DEFAULTS for
343 keywords not already present in ARGS."
344 (let loop ((defaults defaults)
345 (args args))
346 (match defaults
347 ((kw value rest ...)
348 (loop rest
349 (if (memq kw args)
350 args
351 (cons* kw value args))))
352 (()
353 args))))
354
355 (define-syntax collect-default-args
356 (syntax-rules ()
357 ((_)
358 '())
359 ((_ (_ _) rest ...)
360 (collect-default-args rest ...))
361 ((_ (kw _ dflt) rest ...)
362 (cons* kw dflt (collect-default-args rest ...)))))
363
364 (define-syntax substitute-keyword-arguments
365 (syntax-rules ()
366 "Return a new list of arguments where the value for keyword arg KW is
367 replaced by EXP. EXP is evaluated in a context where VAR is bound to the
368 previous value of the keyword argument, or DFLT if given."
369 ((_ original-args ((kw var dflt ...) exp) ...)
370 (let loop ((args (default-keyword-arguments
371 original-args
372 (collect-default-args (kw var dflt ...) ...)))
373 (before '()))
374 (match args
375 ((kw var rest (... ...))
376 (loop rest (cons* exp kw before)))
377 ...
378 ((x rest (... ...))
379 (loop rest (cons x before)))
380 (()
381 (reverse before)))))))
382
383 (define (delkw kw lst)
384 "Remove KW and its associated value from LST, a keyword/value list such
385 as '(#:foo 1 #:bar 2)."
386 (let loop ((lst lst)
387 (result '()))
388 (match lst
389 (()
390 (reverse result))
391 ((kw? value rest ...)
392 (if (eq? kw? kw)
393 (append (reverse result) rest)
394 (loop rest (cons* value kw? result)))))))
395
396 (define (ensure-keyword-arguments args kw/values)
397 "Force the keywords arguments KW/VALUES in the keyword argument list ARGS.
398 For instance:
399
400 (ensure-keyword-arguments '(#:foo 2) '(#:foo 2))
401 => (#:foo 2)
402
403 (ensure-keyword-arguments '(#:foo 2) '(#:bar 3))
404 => (#:foo 2 #:bar 3)
405
406 (ensure-keyword-arguments '(#:foo 2) '(#:bar 3 #:foo 42))
407 => (#:foo 42 #:bar 3)
408 "
409 (let loop ((args args)
410 (kw/values kw/values)
411 (result '()))
412 (match args
413 (()
414 (append (reverse result) kw/values))
415 ((kw value rest ...)
416 (match (memq kw kw/values)
417 ((_ value . _)
418 (loop rest (delkw kw kw/values) (cons* value kw result)))
419 (#f
420 (loop rest kw/values (cons* value kw result))))))))
421
422 \f
423 ;;;
424 ;;; System strings.
425 ;;;
426
427 (define* (nix-system->gnu-triplet
428 #:optional (system (%current-system)) (vendor "unknown"))
429 "Return a guess of the GNU triplet corresponding to Nix system
430 identifier SYSTEM."
431 (match system
432 ("armhf-linux"
433 (string-append "arm-" vendor "-linux-gnueabihf"))
434 (_
435 (let* ((dash (string-index system #\-))
436 (arch (substring system 0 dash))
437 (os (substring system (+ 1 dash))))
438 (string-append arch
439 "-" vendor "-"
440 (if (string=? os "linux")
441 "linux-gnu"
442 os))))))
443
444 (define (gnu-triplet->nix-system triplet)
445 "Return the Nix system type corresponding to TRIPLET, a GNU triplet as
446 returned by `config.guess'."
447 (let ((triplet (cond ((string-match "^i[345]86-(.*)$" triplet)
448 =>
449 (lambda (m)
450 (string-append "i686-" (match:substring m 1))))
451 (else triplet))))
452 (cond ((string-match "^arm[^-]*-([^-]+-)?linux-gnueabihf" triplet)
453 "armhf-linux")
454 ((string-match "^([^-]+)-([^-]+-)?linux-gnu.*" triplet)
455 =>
456 (lambda (m)
457 ;; Nix omits `-gnu' for GNU/Linux.
458 (string-append (match:substring m 1) "-linux")))
459 ((string-match "^([^-]+)-([^-]+-)?([[:alpha:]]+)([0-9]+\\.?)*$" triplet)
460 =>
461 (lambda (m)
462 ;; Nix strip the version number from names such as `gnu0.3',
463 ;; `darwin10.2.0', etc., and always strips the vendor part.
464 (string-append (match:substring m 1) "-"
465 (match:substring m 3))))
466 (else triplet))))
467
468 (define %current-system
469 ;; System type as expected by Nix, usually ARCHITECTURE-KERNEL.
470 ;; By default, this is equal to (gnu-triplet->nix-system %host-type).
471 (make-parameter %system))
472
473 (define %current-target-system
474 ;; Either #f or a GNU triplet representing the target system we are
475 ;; cross-building to.
476 (make-parameter #f))
477
478 (define* (package-name->name+version spec
479 #:optional (delimiter #\@))
480 "Given SPEC, a package name like \"foo@0.9.1b\", return two values: \"foo\"
481 and \"0.9.1b\". When the version part is unavailable, SPEC and #f are
482 returned. Both parts must not contain any '@'. Optionally, DELIMITER can be
483 a character other than '@'."
484 (match (string-rindex spec delimiter)
485 (#f (values spec #f))
486 (idx (values (substring spec 0 idx)
487 (substring spec (1+ idx))))))
488
489 (define* (target-mingw? #:optional (target (%current-target-system)))
490 (and target
491 (string-suffix? "-mingw32" target)))
492
493 (define* (target-arm32? #:optional (target (or (%current-target-system)
494 (%current-system))))
495 (string-prefix? "arm" target))
496
497 (define* (target-aarch64? #:optional (target (or (%current-target-system)
498 (%current-system))))
499 (string-prefix? "aarch64" target))
500
501 (define* (target-arm? #:optional (target (or (%current-target-system)
502 (%current-system))))
503 (or (target-arm32? target) (target-aarch64? target)))
504
505 (define* (target-64bit? #:optional (system (or (%current-target-system)
506 (%current-system))))
507 (any (cut string-prefix? <> system) '("x86_64" "aarch64" "mips64" "ppc64")))
508
509 (define version-compare
510 (let ((strverscmp
511 (let ((sym (or (dynamic-func "strverscmp" (dynamic-link))
512 (error "could not find `strverscmp' (from GNU libc)"))))
513 (pointer->procedure int sym (list '* '*)))))
514 (lambda (a b)
515 "Return '> when A denotes a newer version than B,
516 '< when A denotes a older version than B,
517 or '= when they denote equal versions."
518 (let ((result (strverscmp (string->pointer a) (string->pointer b))))
519 (cond ((positive? result) '>)
520 ((negative? result) '<)
521 (else '=))))))
522
523 (define (version-prefix version-string num-parts)
524 "Truncate version-string to the first num-parts components of the version.
525 For example, (version-prefix \"2.1.47.4.23\" 3) returns \"2.1.47\""
526 (string-join (take (string-split version-string #\.) num-parts) "."))
527
528
529 (define (version-major+minor version-string)
530 "Return \"<major>.<minor>\", where major and minor are the major and
531 minor version numbers from version-string."
532 (version-prefix version-string 2))
533
534 (define (version-major version-string)
535 "Return the major version number as string from the version-string."
536 (version-prefix version-string 1))
537
538 (define (version>? a b)
539 "Return #t when A denotes a version strictly newer than B."
540 (eq? '> (version-compare a b)))
541
542 (define (version>=? a b)
543 "Return #t when A denotes a version newer or equal to B."
544 (case (version-compare a b)
545 ((> =) #t)
546 (else #f)))
547
548 (define (guile-version>? str)
549 "Return #t if the running Guile version is greater than STR."
550 ;; Note: Using (version>? (version) "2.0.5") or similar doesn't work,
551 ;; because the result of (version) can have a prefix, like "2.0.5-deb1".
552 (version>? (string-append (major-version) "."
553 (minor-version) "."
554 (micro-version))
555 str))
556
557 (define version-prefix?
558 (let ((not-dot (char-set-complement (char-set #\.))))
559 (lambda (v1 v2)
560 "Return true if V1 is a version prefix of V2:
561
562 (version-prefix? \"4.1\" \"4.16.2\") => #f
563 (version-prefix? \"4.1\" \"4.1.2\") => #t
564 "
565 (define (list-prefix? lst1 lst2)
566 (match lst1
567 (() #t)
568 ((head1 tail1 ...)
569 (match lst2
570 (() #f)
571 ((head2 tail2 ...)
572 (and (equal? head1 head2)
573 (list-prefix? tail1 tail2)))))))
574
575 (list-prefix? (string-tokenize v1 not-dot)
576 (string-tokenize v2 not-dot)))))
577
578 (define (file-extension file)
579 "Return the extension of FILE or #f if there is none."
580 (let ((dot (string-rindex file #\.)))
581 (and dot (substring file (+ 1 dot) (string-length file)))))
582
583 (define (file-sans-extension file)
584 "Return the substring of FILE without its extension, if any."
585 (let ((dot (string-rindex file #\.)))
586 (if dot
587 (substring file 0 dot)
588 file)))
589
590 (define (tarball-sans-extension tarball)
591 "Return TARBALL without its .tar.* or .zip extension."
592 (let ((end (or (string-contains tarball ".tar")
593 (string-contains tarball ".zip"))))
594 (substring tarball 0 end)))
595
596 (define (compressed-file? file)
597 "Return true if FILE denotes a compressed file."
598 (->bool (member (file-extension file)
599 '("gz" "bz2" "xz" "lz" "lzma" "tgz" "tbz2" "zip"))))
600
601 (define (switch-symlinks link target)
602 "Atomically switch LINK, a symbolic link, to point to TARGET. Works
603 both when LINK already exists and when it does not."
604 (let ((pivot (string-append link ".new")))
605 (symlink target pivot)
606 (rename-file pivot link)))
607
608 (define* (string-replace-substring str substr replacement
609 #:optional
610 (start 0)
611 (end (string-length str)))
612 "Replace all occurrences of SUBSTR in the START--END range of STR by
613 REPLACEMENT."
614 (match (string-length substr)
615 (0
616 (error "string-replace-substring: empty substring"))
617 (substr-length
618 (let loop ((start start)
619 (pieces (list (substring str 0 start))))
620 (match (string-contains str substr start end)
621 (#f
622 (string-concatenate-reverse
623 (cons (substring str start) pieces)))
624 (index
625 (loop (+ index substr-length)
626 (cons* replacement
627 (substring str start index)
628 pieces))))))))
629
630 (define (arguments-from-environment-variable variable)
631 "Retrieve value of environment variable denoted by string VARIABLE in the
632 form of a list of strings (`char-set:graphic' tokens) suitable for consumption
633 by `args-fold', if VARIABLE is defined, otherwise return an empty list."
634 (let ((env (getenv variable)))
635 (if env
636 (string-tokenize env char-set:graphic)
637 '())))
638
639 (define (call-with-temporary-output-file proc)
640 "Call PROC with a name of a temporary file and open output port to that
641 file; close the file and delete it when leaving the dynamic extent of this
642 call."
643 (let* ((directory (or (getenv "TMPDIR") "/tmp"))
644 (template (string-append directory "/guix-file.XXXXXX"))
645 (out (mkstemp! template)))
646 (dynamic-wind
647 (lambda ()
648 #t)
649 (lambda ()
650 (proc template out))
651 (lambda ()
652 (false-if-exception (close out))
653 (false-if-exception (delete-file template))))))
654
655 (define (call-with-temporary-directory proc)
656 "Call PROC with a name of a temporary directory; close the directory and
657 delete it when leaving the dynamic extent of this call."
658 (let* ((directory (or (getenv "TMPDIR") "/tmp"))
659 (template (string-append directory "/guix-directory.XXXXXX"))
660 (tmp-dir (mkdtemp! template)))
661 (dynamic-wind
662 (const #t)
663 (lambda ()
664 (proc tmp-dir))
665 (lambda ()
666 (false-if-exception (delete-file-recursively tmp-dir))))))
667
668 (define (with-atomic-file-output file proc)
669 "Call PROC with an output port for the file that is going to replace FILE.
670 Upon success, FILE is atomically replaced by what has been written to the
671 output port, and PROC's result is returned."
672 (let* ((template (string-append file ".XXXXXX"))
673 (out (mkstemp! template)))
674 (with-throw-handler #t
675 (lambda ()
676 (let ((result (proc out)))
677 (fdatasync out)
678 (close-port out)
679 (rename-file template file)
680 result))
681 (lambda (key . args)
682 (false-if-exception (delete-file template))
683 (close-port out)))))
684
685 (define* (xdg-directory variable suffix #:key (ensure? #t))
686 "Return the name of the XDG directory that matches VARIABLE and SUFFIX,
687 after making sure that it exists if ENSURE? is true. VARIABLE is an
688 environment variable name like \"XDG_CONFIG_HOME\"; SUFFIX is a suffix like
689 \"/.config\". Honor the XDG specs,
690 <http://standards.freedesktop.org/basedir-spec/basedir-spec-latest.html>."
691 (let ((dir (and=> (or (getenv variable)
692 (and=> (or (getenv "HOME")
693 (passwd:dir (getpwuid (getuid))))
694 (cut string-append <> suffix)))
695 (cut string-append <> "/guix"))))
696 (when ensure?
697 (mkdir-p dir))
698 dir))
699
700 (define config-directory
701 (cut xdg-directory "XDG_CONFIG_HOME" "/.config" <...>))
702
703 (define cache-directory
704 (cut xdg-directory "XDG_CACHE_HOME" "/.cache" <...>))
705
706 (define (readlink* file)
707 "Call 'readlink' until the result is not a symlink."
708 (define %max-symlink-depth 50)
709
710 (let loop ((file file)
711 (depth 0))
712 (define (absolute target)
713 (if (absolute-file-name? target)
714 target
715 (string-append (dirname file) "/" target)))
716
717 (if (>= depth %max-symlink-depth)
718 file
719 (call-with-values
720 (lambda ()
721 (catch 'system-error
722 (lambda ()
723 (values #t (readlink file)))
724 (lambda args
725 (let ((errno (system-error-errno args)))
726 (if (or (= errno EINVAL))
727 (values #f file)
728 (apply throw args))))))
729 (lambda (success? target)
730 (if success?
731 (loop (absolute target) (+ depth 1))
732 file))))))
733
734 (define (canonical-newline-port port)
735 "Return an input port that wraps PORT such that all newlines consist
736 of a single linefeed."
737 (define (get-position)
738 (if (port-has-port-position? port) (port-position port) #f))
739 (define (set-position! position)
740 (if (port-has-set-port-position!? port)
741 (set-port-position! position port)
742 #f))
743 (define (close) (close-port port))
744 (define (read! bv start n)
745 (let loop ((count 0)
746 (byte (get-u8 port)))
747 (cond ((eof-object? byte) count)
748 ;; XXX: consume all CRs even if not followed by LF.
749 ((eqv? byte (char->integer #\return)) (loop count (get-u8 port)))
750 ((= count (- n 1))
751 (bytevector-u8-set! bv (+ start count) byte)
752 n)
753 (else
754 (bytevector-u8-set! bv (+ start count) byte)
755 (loop (+ count 1) (get-u8 port))))))
756 (make-custom-binary-input-port "canonical-newline-port"
757 read!
758 get-position
759 set-position!
760 close))
761 \f
762 ;;;
763 ;;; Source location.
764 ;;;
765
766 (define absolute-dirname
767 ;; Memoize to avoid repeated 'stat' storms from 'search-path'.
768 (mlambda (file)
769 "Return the absolute name of the directory containing FILE, or #f upon
770 failure."
771 (match (search-path %load-path file)
772 (#f #f)
773 ((? string? file)
774 ;; If there are relative names in %LOAD-PATH, FILE can be relative and
775 ;; needs to be canonicalized.
776 (if (string-prefix? "/" file)
777 (dirname file)
778 (canonicalize-path (dirname file)))))))
779
780 (define-syntax current-source-directory
781 (lambda (s)
782 "Return the absolute name of the current directory, or #f if it could not
783 be determined."
784 (syntax-case s ()
785 ((_)
786 (match (assq 'filename (or (syntax-source s) '()))
787 (('filename . (? string? file-name))
788 ;; If %FILE-PORT-NAME-CANONICALIZATION is 'relative, then FILE-NAME
789 ;; can be relative. In that case, we try to find out at run time
790 ;; the absolute file name by looking at %LOAD-PATH; doing this at
791 ;; run time rather than expansion time is necessary to allow files
792 ;; to be moved on the file system.
793 (if (string-prefix? "/" file-name)
794 (dirname file-name)
795 #`(absolute-dirname #,file-name)))
796 ((or ('filename . #f) #f)
797 ;; raising an error would upset Geiser users
798 #f))))))
799
800 ;; A source location.
801 (define-record-type <location>
802 (make-location file line column)
803 location?
804 (file location-file) ; file name
805 (line location-line) ; 1-indexed line
806 (column location-column)) ; 0-indexed column
807
808 (define (location file line column)
809 "Return the <location> object for the given FILE, LINE, and COLUMN."
810 (and line column file
811 (make-location file line column)))
812
813 (define (source-properties->location loc)
814 "Return a location object based on the info in LOC, an alist as returned
815 by Guile's `source-properties', `frame-source', `current-source-location',
816 etc."
817 ;; In accordance with the GCS, start line and column numbers at 1. Note
818 ;; that unlike LINE and `port-column', COL is actually 1-indexed here...
819 (match loc
820 ((('line . line) ('column . col) ('filename . file)) ;common case
821 (and file line col
822 (make-location file (+ line 1) col)))
823 (#f
824 #f)
825 (_
826 (let ((file (assq-ref loc 'filename))
827 (line (assq-ref loc 'line))
828 (col (assq-ref loc 'column)))
829 (location file (and line (+ line 1)) col)))))
830
831 (define (location->source-properties loc)
832 "Return the source property association list based on the info in LOC,
833 a location object."
834 `((line . ,(and=> (location-line loc) 1-))
835 (column . ,(location-column loc))
836 (filename . ,(location-file loc))))
837
838 (define-condition-type &error-location &error
839 error-location?
840 (location error-location)) ;<location>
841
842 (define-condition-type &fix-hint &condition
843 fix-hint?
844 (hint condition-fix-hint)) ;string
845
846 ;;; Local Variables:
847 ;;; eval: (put 'call-with-progress-reporter 'scheme-indent-function 1)
848 ;;; End: