[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[bug#49169] [PATCH v2 07/16] utils: 'edit-expression' no longer leaks fi
From: |
Ludovic Courtès |
Subject: |
[bug#49169] [PATCH v2 07/16] utils: 'edit-expression' no longer leaks file ports. |
Date: |
Wed, 30 Jun 2021 22:48:23 +0200 |
* guix/utils.scm (edit-expression): Use 'call-with-input-file' to make
sure IN gets closed.
---
guix/utils.scm | 64 ++++++++++++++++++++++++++------------------------
1 file changed, 33 insertions(+), 31 deletions(-)
diff --git a/guix/utils.scm b/guix/utils.scm
index 19990ceb8a..a13b13c4fa 100644
--- a/guix/utils.scm
+++ b/guix/utils.scm
@@ -342,38 +342,40 @@ a list of command-line arguments passed to the
compression program."
be a procedure that takes the original expression in string and returns a new
one. ENCODING will be used to interpret all port I/O, it default to UTF-8.
This procedure returns #t on success."
+ (define file (assq-ref source-properties 'filename))
+ (define line (assq-ref source-properties 'line))
+ (define column (assq-ref source-properties 'column))
+
(with-fluids ((%default-port-encoding encoding))
- (let* ((file (assq-ref source-properties 'filename))
- (line (assq-ref source-properties 'line))
- (column (assq-ref source-properties 'column))
- (in (open-input-file file))
- ;; The start byte position of the expression.
- (start (begin (while (not (and (= line (port-line in))
- (= column (port-column in))))
- (when (eof-object? (read-char in))
- (error (format #f "~a: end of file~%" in))))
- (ftell in)))
- ;; The end byte position of the expression.
- (end (begin (read in) (ftell in))))
- (seek in 0 SEEK_SET) ; read from the beginning of the file.
- (let* ((pre-bv (get-bytevector-n in start))
- ;; The expression in string form.
- (str (iconv:bytevector->string
- (get-bytevector-n in (- end start))
- (port-encoding in)))
- (post-bv (get-bytevector-all in))
- (str* (proc str)))
- ;; Verify the edited expression is still a scheme expression.
- (call-with-input-string str* read)
- ;; Update the file with edited expression.
- (with-atomic-file-output file
- (lambda (out)
- (put-bytevector out pre-bv)
- (display str* out)
- ;; post-bv maybe the end-of-file object.
- (when (not (eof-object? post-bv))
- (put-bytevector out post-bv))
- #t))))))
+ (call-with-input-file file
+ (lambda (in)
+ (let* ( ;; The start byte position of the expression.
+ (start (begin (while (not (and (= line (port-line in))
+ (= column (port-column in))))
+ (when (eof-object? (read-char in))
+ (error (format #f "~a: end of file~%" in))))
+ (ftell in)))
+ ;; The end byte position of the expression.
+ (end (begin (read in) (ftell in))))
+ (seek in 0 SEEK_SET) ; read from the beginning of the file.
+ (let* ((pre-bv (get-bytevector-n in start))
+ ;; The expression in string form.
+ (str (iconv:bytevector->string
+ (get-bytevector-n in (- end start))
+ (port-encoding in)))
+ (post-bv (get-bytevector-all in))
+ (str* (proc str)))
+ ;; Verify the edited expression is still a scheme expression.
+ (call-with-input-string str* read)
+ ;; Update the file with edited expression.
+ (with-atomic-file-output file
+ (lambda (out)
+ (put-bytevector out pre-bv)
+ (display str* out)
+ ;; post-bv maybe the end-of-file object.
+ (when (not (eof-object? post-bv))
+ (put-bytevector out post-bv))
+ #t))))))))
;;;
--
2.32.0
- [bug#49169] [PATCH 00/11] Removing input labels from package definitions, (continued)
- [bug#49169] [PATCH 00/11] Removing input labels from package definitions, Ludovic Courtès, 2021/06/22
- [bug#49169] [PATCH 00/11] Removing input labels from package definitions, Ludovic Courtès, 2021/06/27
- [bug#49169] [PATCH v2 00/16] Removing input labels from package definitions, Ludovic Courtès, 2021/06/30
- [bug#49169] [PATCH v2 01/16] records: Support field sanitizers., Ludovic Courtès, 2021/06/30
- [bug#49169] [PATCH v2 03/16] lint: Add 'input-labels' checker., Ludovic Courtès, 2021/06/30
- [bug#49169] [PATCH v2 02/16] packages: Allow inputs to be plain package lists., Ludovic Courtès, 2021/06/30
- [bug#49169] [PATCH v2 04/16] packages: Add 'lookup-package-input' & co., Ludovic Courtès, 2021/06/30
- [bug#49169] [PATCH v2 05/16] packages: Add 'modify-inputs'., Ludovic Courtès, 2021/06/30
- [bug#49169] [PATCH v2 07/16] utils: 'edit-expression' no longer leaks file ports.,
Ludovic Courtès <=
- [bug#49169] [PATCH v2 06/16] gnu: Change inputs of core packages to plain lists., Ludovic Courtès, 2021/06/30
- [bug#49169] [PATCH v2 10/16] utils: 'edit-expression' copies part of the original source map., Ludovic Courtès, 2021/06/30
- [bug#49169] [PATCH v2 12/16] packages: 'hidden-package' inherits the original package location., Ludovic Courtès, 2021/06/30
- [bug#49169] [PATCH v2 13/16] import: pypi: Emit new-style package inputs., Ludovic Courtès, 2021/06/30
- [bug#49169] [PATCH v2 14/16] import: cran: Emit new-style package inputs., Ludovic Courtès, 2021/06/30
- [bug#49169] [PATCH v2 15/16] import: print: Emit new-style package inputs when possible., Ludovic Courtès, 2021/06/30
- [bug#49169] [PATCH v2 08/16] utils: Add 'go-to-location' with source location caching., Ludovic Courtès, 2021/06/30
- [bug#49169] [PATCH v2 09/16] utils: 'edit-expression' modifies the file only if necessary., Ludovic Courtès, 2021/06/30
- [bug#49169] [PATCH v2 11/16] Add 'guix style'., Ludovic Courtès, 2021/06/30
- [bug#49169] [PATCH v2 16/16] import: elpa: Emit new-style package inputs., Ludovic Courtès, 2021/06/30