diff --git a/gash/guix-build-utils.scm b/gash/guix-build-utils.scm index c010543..340bec6 100644 --- a/gash/guix-build-utils.scm +++ b/gash/guix-build-utils.scm @@ -44,6 +44,7 @@ grep-match-line grep-match-column grep-match-end-column + mkdir-p directory-exists? executable-file? @@ -212,3 +213,28 @@ transferred and the continuation of the transfer as a thunk." (lambda (in) (grep* pattern #:port in #:file-name file)))) (else (grep* pattern)))) + +(define (mkdir-p dir) + "Create directory DIR and all its ancestors." + (define absolute? + (string-prefix? "/" dir)) + + (define not-slash + (char-set-complement (char-set #\/))) + + (let loop ((components (string-tokenize dir not-slash)) + (root (if absolute? + "" + "."))) + (match components + ((head tail ...) + (let ((path (string-append root "/" head))) + (catch 'system-error + (lambda () + (mkdir path) + (loop tail path)) + (lambda args + (if (= EEXIST (system-error-errno args)) + (loop tail path) + (apply throw args)))))) + (() #t))))