guix: Simplify and robustify lzread!.

* guix/lzlib.scm (lzread!): Do it.

Previously lzread! would fail if COUNT was bigger
than (lz-decompress-write-size).  This is possible if a previous call to
lzread! didn't empty the LZ_decompress input buffer (e.g. BV was too small to
fit all the data).
This commit is contained in:
Pierre Neidhardt 2019-05-07 18:40:40 +02:00
parent dbef9015db
commit ecfc54403e
No known key found for this signature in database
GPG Key ID: 9BDCF497A4BBCC7F
1 changed files with 6 additions and 14 deletions

View File

@ -491,27 +491,19 @@ perhaps not yet read."
;; High level functions.
(define %lz-decompress-input-buffer-size (* 64 1024))
(define* (lzread! decoder file-port bv
#:optional (start 0) (count (bytevector-length bv)))
"Read up to COUNT bytes from FILE-PORT into BV at offset START. Return the
number of uncompressed bytes actually read; it is zero if COUNT is zero or if
the end-of-stream has been reached."
;; WARNING: Because we don't alternate between lz-reads and lz-writes, we can't
;; process more than %lz-decompress-input-buffer-size from the file-port.
(when (> count %lz-decompress-input-buffer-size)
(set! count %lz-decompress-input-buffer-size))
(let* ((written 0)
(read 0)
(file-bv (get-bytevector-n file-port count)))
;; process more than lz-decompress-write-size from the file-port.
(when (> count (lz-decompress-write-size decoder))
(set! count (lz-decompress-write-size decoder)))
(let ((file-bv (get-bytevector-n file-port count)))
(unless (eof-object? file-bv)
(begin
(while (and (< 0 (lz-decompress-write-size decoder))
(< written (bytevector-length file-bv)))
(set! written (+ written
(lz-decompress-write decoder file-bv written
(- (bytevector-length file-bv) written)))))))
(lz-decompress-write decoder file-bv 0 (bytevector-length file-bv))))
(let ((read 0))
(let loop ((rd 0))
(if (< start (bytevector-length bv))
(begin