diff options
| author | Arun Isaac | 2026-04-12 04:02:47 +0100 |
|---|---|---|
| committer | Arun Isaac | 2026-04-12 04:06:05 +0100 |
| commit | a14b3740a45931f27a04b4947b5db6972981e72c (patch) | |
| tree | 74718d3631bc5e382b504f05fab056bdff7417f5 | |
| parent | 5bdc331408505d8264c6d5d31ba1b3fb29c0e69b (diff) | |
| download | kaagum-a14b3740a45931f27a04b4947b5db6972981e72c.tar.gz kaagum-a14b3740a45931f27a04b4947b5db6972981e72c.tar.lz kaagum-a14b3740a45931f27a04b4947b5db6972981e72c.zip | |
Error out when reading binary files.
| -rw-r--r-- | kaakaa/tools/base.scm | 20 |
1 files changed, 20 insertions, 0 deletions
diff --git a/kaakaa/tools/base.scm b/kaakaa/tools/base.scm index e726c14..a8d7b00 100644 --- a/kaakaa/tools/base.scm +++ b/kaakaa/tools/base.scm @@ -17,6 +17,7 @@ ;;; along with kaakaa. If not, see <https://www.gnu.org/licenses/>. (define-module (kaakaa tools base) + #:use-module (rnrs exceptions) #:use-module (rnrs io ports) #:use-module (guix build utils) #:use-module (srfi srfi-171) @@ -24,6 +25,20 @@ #:export (%list-files %base-tools)) +(define (binary-file? file) + "Return @code{#t} if @var{file} is a binary file. Else, return @code{#f}." + ;; We use the following heuristic: If there are character decoding errors in + ;; the first 10K characters, we assume that this is a binary file. + (guard (c ((or (i/o-decoding-error? c) + (eq? (exception-kind c) + 'decoding-error)) + #t)) + (call-with-input-file file + (lambda (port) + (set-port-conversion-strategy! port 'error) + (get-string-n port (* 10 1024)))) + #f)) + (define %read (tool #:description "Read whole text file, or optionally a subset of its lines. @@ -45,6 +60,11 @@ Line numbers start from 1. Output is the raw file contents without line numbers. "Error: File ~a does not exist~%" path) (exit #f)) + ((binary-file? path) + (format (current-output-port) + "Error: File ~a is binary, not text~%" + path) + (exit #f)) (else (call-with-input-file path (cut port-transduce |
