utils: find-files: Add DIRECTORIES? and FAIL-ON-ERROR? arguments.

* guix/build/utils.scm (find-files): Add DIRECTORIES? and FAIL-ON-ERROR?
  keyword arguments.
This commit is contained in:
Mark H Weaver 2015-09-06 12:24:08 -04:00
parent f5c0b04bfe
commit f4ae827e26
1 changed files with 12 additions and 3 deletions

View File

@ -2,6 +2,7 @@
;;; Copyright © 2012, 2013, 2014, 2015 Ludovic Courtès <ludo@gnu.org> ;;; Copyright © 2012, 2013, 2014, 2015 Ludovic Courtès <ludo@gnu.org>
;;; Copyright © 2013 Andreas Enge <andreas@enge.fr> ;;; Copyright © 2013 Andreas Enge <andreas@enge.fr>
;;; Copyright © 2013 Nikita Karetnikov <nikita@karetnikov.org> ;;; Copyright © 2013 Nikita Karetnikov <nikita@karetnikov.org>
;;; Copyright © 2015 Mark H Weaver <mhw@netris.org>
;;; ;;;
;;; This file is part of GNU Guix. ;;; This file is part of GNU Guix.
;;; ;;;
@ -316,13 +317,16 @@ name matches REGEXP."
(regexp-exec file-rx (basename file))))) (regexp-exec file-rx (basename file)))))
(define* (find-files dir #:optional (pred (const #t)) (define* (find-files dir #:optional (pred (const #t))
#:key (stat lstat)) #:key (stat lstat)
directories?
fail-on-error?)
"Return the lexicographically sorted list of files under DIR for which PRED "Return the lexicographically sorted list of files under DIR for which PRED
returns true. PRED is passed two arguments: the absolute file name, and its returns true. PRED is passed two arguments: the absolute file name, and its
stat buffer; the default predicate always returns true. PRED can also be a stat buffer; the default predicate always returns true. PRED can also be a
regular expression, in which case it is equivalent to (file-name-predicate regular expression, in which case it is equivalent to (file-name-predicate
PRED). STAT is used to obtain file information; using 'lstat' means that PRED). STAT is used to obtain file information; using 'lstat' means that
symlinks are not followed." symlinks are not followed. If DIRECTORIES? is true, then directories will
also be included. If FAIL-ON-ERROR? is true, raise an exception upon error."
(let ((pred (if (procedure? pred) (let ((pred (if (procedure? pred)
pred pred
(file-name-predicate pred)))) (file-name-predicate pred))))
@ -333,7 +337,10 @@ symlinks are not followed."
(cons file result) (cons file result)
result)) result))
(lambda (dir stat result) ; down (lambda (dir stat result) ; down
result) (if (and directories?
(pred dir stat))
(cons dir result)
result))
(lambda (dir stat result) ; up (lambda (dir stat result) ; up
result) result)
(lambda (file stat result) ; skip (lambda (file stat result) ; skip
@ -341,6 +348,8 @@ symlinks are not followed."
(lambda (file stat errno result) (lambda (file stat errno result)
(format (current-error-port) "find-files: ~a: ~a~%" (format (current-error-port) "find-files: ~a: ~a~%"
file (strerror errno)) file (strerror errno))
(when fail-on-error?
(error "find-files failed"))
result) result)
'() '()
dir dir