utils: 'dump-port' has an optional 'len' parameter.

* guix/build/utils.scm (dump-port): Add optional 'len' parameter and
honor it.
This commit is contained in:
Ludovic Courtès 2020-08-27 22:52:26 +02:00
parent 0eb093b0ee
commit 24ce6e27bf
No known key found for this signature in database
GPG Key ID: 090B11993D9AEBB5

View File

@ -1,5 +1,5 @@
;;; GNU Guix --- Functional package management for GNU ;;; GNU Guix --- Functional package management for GNU
;;; Copyright © 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019 Ludovic Courtès <ludo@gnu.org> ;;; Copyright © 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019, 2020 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, 2018 Mark H Weaver <mhw@netris.org> ;;; Copyright © 2015, 2018 Mark H Weaver <mhw@netris.org>
@ -854,29 +854,38 @@ match the terminating newline of a line."
;;; ;;;
(define* (dump-port in out (define* (dump-port in out
#:optional len
#:key (buffer-size 16384) #:key (buffer-size 16384)
(progress (lambda (t k) (k)))) (progress (lambda (t k) (k))))
"Read as much data as possible from IN and write it to OUT, using chunks of "Read LEN bytes from IN or as much data as possible if LEN is #f, and write
BUFFER-SIZE bytes. Call PROGRESS at the beginning and after each successful it to OUT, using chunks of BUFFER-SIZE bytes. Call PROGRESS at the beginning
transfer of BUFFER-SIZE bytes or less, passing it the total number of bytes and after each successful transfer of BUFFER-SIZE bytes or less, passing it
transferred and the continuation of the transfer as a thunk." the total number of bytes transferred and the continuation of the transfer as
a thunk."
(define buffer (define buffer
(make-bytevector buffer-size)) (make-bytevector buffer-size))
(define (loop total bytes) (define (loop total bytes)
(or (eof-object? bytes) (or (eof-object? bytes)
(and len (= total len))
(let ((total (+ total bytes))) (let ((total (+ total bytes)))
(put-bytevector out buffer 0 bytes) (put-bytevector out buffer 0 bytes)
(progress total (progress total
(lambda () (lambda ()
(loop total (loop total
(get-bytevector-n! in buffer 0 buffer-size))))))) (get-bytevector-n! in buffer 0
(if len
(min (- len total) buffer-size)
buffer-size))))))))
;; Make sure PROGRESS is called when we start so that it can measure ;; Make sure PROGRESS is called when we start so that it can measure
;; throughput. ;; throughput.
(progress 0 (progress 0
(lambda () (lambda ()
(loop 0 (get-bytevector-n! in buffer 0 buffer-size))))) (loop 0 (get-bytevector-n! in buffer 0
(if len
(min len buffer-size)
buffer-size))))))
(define (set-file-time file stat) (define (set-file-time file stat)
"Set the atime/mtime of FILE to that specified by STAT." "Set the atime/mtime of FILE to that specified by STAT."