From 03fc5c928ab6f1cec77a7a6efb6e400a6923d1ca Mon Sep 17 00:00:00 2001 From: Jan Nieuwenhuizen Date: Fri, 26 Oct 2018 22:41:01 +0200 Subject: [PATCH] Add mkdir-p. * gash/guix-build-utils.scm (mkdir-p): New function, import from Guix. --- gash/guix-build-utils.scm | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) 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))))