guix-patches
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[bug#30708] [PATCH] utils: Add helper method to list subdirectories.


From: Ludovic Courtès
Subject: [bug#30708] [PATCH] utils: Add helper method to list subdirectories.
Date: Mon, 05 Mar 2018 18:12:40 +0100
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/25.3 (gnu/linux)

Hi Maxim,

Maxim Cournoyer <address@hidden> skribis:

> From b4b607800d770c4cf77f92c247276c368357e94f Mon Sep 17 00:00:00 2001
> From: Maxim Cournoyer <address@hidden>
> Date: Sun, 25 Feb 2018 17:49:06 -0500
> Subject: [PATCH] utils: Add helper method to list subdirectories.
>
> * guix/build/utils.scm (find-subdirectories): New procedure.
> * tests/build-utils.scm: Rename module so that it can be used with Geiser.
> (%test-dir-hierarchy): New variable.
> (make-test-dir-hierarchy): New test procedure.
> ("find-subdirectories"): New test.

[...]

> +(define* (find-subdirectories dir #:key fail-on-error?)
> +  "Return the list of the immediate subdirectories of DIR."
> +  ;; Strip the trailing '/' DIR is '/'.
> +  (let ((dir (if (and (> 1 (string-length dir))
> +                      (eq? (string-take-right dir 1) #\/))
> +                 (string-drop-right dir 1)
> +                 dir)))
> +    (define (pred filename stat)
> +      (and (eq? (stat:type stat) 'directory)
> +           (string-match (string-append dir "/[^/]*$") filename)))
> +    (find-files dir pred
> +                #:directories? #t
> +                #:fail-on-error? fail-on-error?)))

‘find-files’ recurses in subdirectories, so the above implementation is
not as efficient as it could be.

I would instead suggest using ‘scandir’ (or ‘file-system-fold’) from
Guile’s (ice-9 ftw) module.

That said… is this a common enough operation?

Thanks,
Ludo’.





reply via email to

[Prev in Thread] Current Thread [Next in Thread]