summaryrefslogtreecommitdiff
path: root/guix
diff options
context:
space:
mode:
authorLudovic Courtès <ludo@gnu.org>2014-03-24 22:15:29 +0100
committerLudovic Courtès <ludo@gnu.org>2014-03-24 22:15:29 +0100
commit01ac19dca4318d577cf3bef53cfe6af590f0e5f8 (patch)
tree413b19db595ab5138a74a635419bd82aac5d3c24 /guix
parent80dea563a3dad98bda60385188509ca79a3651f8 (diff)
utils: Add 'call-with-decompressed-port' and 'call-with-compressed-output-port'.
* guix/utils.scm (call-with-decompressed-port, call-with-compressed-output-port): New procedures. * tests/utils.scm ("compressed-output-port + decompressed-port"): Rewrite to use them.
Diffstat (limited to 'guix')
-rw-r--r--guix/utils.scm37
1 files changed, 36 insertions, 1 deletions
diff --git a/guix/utils.scm b/guix/utils.scm
index 44060c46b5..7306c6011d 100644
--- a/guix/utils.scm
+++ b/guix/utils.scm
@@ -21,6 +21,7 @@
#:use-module (guix config)
#:use-module (srfi srfi-1)
#:use-module (srfi srfi-9)
+ #:use-module (srfi srfi-11)
#:use-module (srfi srfi-26)
#:use-module (srfi srfi-39)
#:use-module (srfi srfi-60)
@@ -74,7 +75,9 @@
filtered-port
compressed-port
decompressed-port
- compressed-output-port))
+ call-with-decompressed-port
+ compressed-output-port
+ call-with-compressed-output-port))
;;;
@@ -224,6 +227,22 @@ a symbol such as 'xz."
('gzip (filtered-port `(,%gzip "-c") input))
(else (error "unsupported compression scheme" compression))))
+(define (call-with-decompressed-port compression port proc)
+ "Call PROC with a wrapper around PORT, a file port, that decompresses data
+read from PORT according to COMPRESSION, a symbol such as 'xz. PORT is closed
+as soon as PROC's dynamic extent is entered."
+ (let-values (((decompressed pids)
+ (decompressed-port compression port)))
+ (dynamic-wind
+ (const #f)
+ (lambda ()
+ (close-port port)
+ (proc decompressed))
+ (lambda ()
+ (close-port decompressed)
+ (unless (every (compose zero? cdr waitpid) pids)
+ (error "decompressed-port failure" pids))))))
+
(define (filtered-output-port command output)
"Return an output port. Data written to that port is filtered through
COMMAND and written to OUTPUT, an output file port. In addition, return a
@@ -265,6 +284,22 @@ of PIDs to wait for."
('gzip (filtered-output-port `(,%gzip "-c") output))
(else (error "unsupported compression scheme" compression))))
+(define (call-with-compressed-output-port compression port proc)
+ "Call PROC with a wrapper around PORT, a file port, that compresses data
+that goes to PORT according to COMPRESSION, a symbol such as 'xz. PORT is
+closed as soon as PROC's dynamic extent is entered."
+ (let-values (((compressed pids)
+ (compressed-output-port compression port)))
+ (dynamic-wind
+ (const #f)
+ (lambda ()
+ (close-port port)
+ (proc compressed))
+ (lambda ()
+ (close-port compressed)
+ (unless (every (compose zero? cdr waitpid) pids)
+ (error "compressed-output-port failure" pids))))))
+
;;;
;;; Nixpkgs.