[bug#36976,1/1] download: Map file-name characters not allowed in store.
diff mbox series

Message ID 20190808144448.25147-1-h.goebel@crazy-compilers.com
State New
Headers show
Series
  • [bug#36976,1/1] download: Map file-name characters not allowed in store.
Related show

Commit Message

Hartmut Goebel Aug. 8, 2019, 2:44 p.m. UTC
In the file-name to be used for storing into the store, replace any
character not allowed in the store-file-name by an underscore.
This is only done when NAME is not given and thus defaults to
toe URL's basename. If NAME is given, this is used unchanged,
allowing for more control by other functions.

Fixes <http://issues.guix.gnu.org/issue/26175>

* guix/download.scm (safe-name): New function.
  (download-to-store): NAME defaults to the "safe" basename of URL.
---
 guix/download.scm | 25 ++++++++++++++++++++-----
 1 file changed, 20 insertions(+), 5 deletions(-)

Comments

Ludovic Courtès Aug. 23, 2019, 9:08 p.m. UTC | #1
Hello,

Hartmut Goebel <h.goebel@crazy-compilers.com> skribis:

> In the file-name to be used for storing into the store, replace any
> character not allowed in the store-file-name by an underscore.
> This is only done when NAME is not given and thus defaults to
> toe URL's basename. If NAME is given, this is used unchanged,
> allowing for more control by other functions.
>
> Fixes <http://issues.guix.gnu.org/issue/26175>
>
> * guix/download.scm (safe-name): New function.
>   (download-to-store): NAME defaults to the "safe" basename of URL.

What about moving this automatic renaming feature to (guix scripts
download)?  I’d rather not do it in the core APIs.

> +(define (safe-name name)
> +  "Replace any character not allowed in a stroe name by an underscore."
                                               ^^
Typo.

I’d call it ‘ensure-valid-store-file-name’ or similar, WDYT?

> +  (define valid-characters
> +    ;; according to nix/libstore/store-api.cc
> +    (string->list (string-append "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
> +                                 "abcdefghijklmnopqrstuvwxyz"
> +                                 "0123456789" "+-._?=")))
> +  (string-map (lambda (c)
> +                (if (member c valid-characters) c #\_))
> +              name))

Instead of a list, please use a SRFI-14 “character set”, like this:

  (define valid
    (string->char-set …))

  (string-map (lambda (c)
                (if (char-set-contains? valid c) …))
              name)

Thanks,
Ludo’.
Hartmut Goebel Aug. 27, 2019, 7:53 a.m. UTC | #2
Hi,

Thanks for the review and the coding suggestions..

Am 23.08.19 um 23:08 schrieb Ludovic Courtès:
>> * guix/download.scm (safe-name): New function.
>>   (download-to-store): NAME defaults to the "safe" basename of URL.
> What about moving this automatic renaming feature to (guix scripts
> download)?  I’d rather not do it in the core APIs.
`download-to-store store` was defined as:

   (define* (download-to-store store url #:optional (name (basename url)) …

When developing this patch, I decided to put it into the core since
users of this function would expect to be allowed to just pass any url
and don't need to take care about valid characters. If not doing the
automatic renaming here, users would need to perform the conversion
prior to calling this function in any case (except when 100% sure only
valid characters are used).
Ludovic Courtès Sept. 1, 2019, 7:39 p.m. UTC | #3
Hello,

Hartmut Goebel <h.goebel@crazy-compilers.com> skribis:

> Thanks for the review and the coding suggestions..
>
> Am 23.08.19 um 23:08 schrieb Ludovic Courtès:
>>> * guix/download.scm (safe-name): New function.
>>>   (download-to-store): NAME defaults to the "safe" basename of URL.
>> What about moving this automatic renaming feature to (guix scripts
>> download)?  I’d rather not do it in the core APIs.
> `download-to-store store` was defined as:
>
>    (define* (download-to-store store url #:optional (name (basename url)) …
>
> When developing this patch, I decided to put it into the core since
> users of this function would expect to be allowed to just pass any url
> and don't need to take care about valid characters. If not doing the
> automatic renaming here, users would need to perform the conversion
> prior to calling this function in any case (except when 100% sure only
> valid characters are used).

Yes, but that’s OK to me: IMO, procedures have to focused on one thing;
users can perform additional processing beforehand if they need it.

Conversely, commands have to do the right thing by default, which is why
I agree that ‘guix download’ should rename automatically when needed.

How does that sound?

Ludo’.
Hartmut Goebel Sept. 26, 2019, 3:50 p.m. UTC | #4
Committed as dec845606d2d184da31065fa26cd951b84b3ce2d

Thank for the review.

Patch
diff mbox series

diff --git a/guix/download.scm b/guix/download.scm
index b24aaa0a86..249f612237 100644
--- a/guix/download.scm
+++ b/guix/download.scm
@@ -6,6 +6,7 @@ 
 ;;; Copyright © 2016 David Craven <david@craven.ch>
 ;;; Copyright © 2017 Tobias Geerinckx-Rice <me@tobias.gr>
 ;;; Copyright © 2019 Guy Fleury Iteriteka <hoonandon@gmail.com>
+;;; Copyright © 2019 Hartmut Goeel <h.goebel@crazy-compilers.com>
 ;;;
 ;;; This file is part of GNU Guix.
 ;;;
@@ -563,13 +564,27 @@  own.  This helper makes it easier to deal with \"zip bombs\"."
                       #:graft? #f
                       #:local-build? #t)))
 
-(define* (download-to-store store url #:optional (name (basename url))
+(define (safe-name name)
+  "Replace any character not allowed in a stroe name by an underscore."
+
+  (define valid-characters
+    ;; according to nix/libstore/store-api.cc
+    (string->list (string-append "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
+                                 "abcdefghijklmnopqrstuvwxyz"
+                                 "0123456789" "+-._?=")))
+  (string-map (lambda (c)
+                (if (member c valid-characters) c #\_))
+              name))
+
+(define* (download-to-store store url
+                            #:optional (name (safe-name (basename url)))
                             #:key (log (current-error-port)) recursive?
                             (verify-certificate? #t))
-  "Download from URL to STORE, either under NAME or URL's basename if
-omitted.  Write progress reports to LOG.  RECURSIVE? has the same effect as
-the same-named parameter of 'add-to-store'.  VERIFY-CERTIFICATE? determines
-whether or not to validate HTTPS server certificates."
+  "Download from URL to STORE, either under NAME. If NAME is omitted, URL's
+basename with invalid characters replaced is used.  Write progress reports to
+LOG.  RECURSIVE? has the same effect as the same-named parameter of
+'add-to-store'.  VERIFY-CERTIFICATE? determines whether or not to validate
+HTTPS server certificates."
   (define uri
     (string->uri url))