From 8e4cecf2151d704d9c38d81d4d37371e431243dc Mon Sep 17 00:00:00 2001 From: Matthew Flatt Date: Mon, 12 Nov 2018 09:58:12 -0700 Subject: [PATCH] io: flush before performing file truncate For consistency with traditional Racket and currently matters on Windows. The Windows implementation of file-truncate should probably not move the file position as it does, though. --- racket/src/io/port/fd-port.rkt | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/racket/src/io/port/fd-port.rkt b/racket/src/io/port/fd-port.rkt index 6eb4bf29ef..ae263d4bf6 100644 --- a/racket/src/io/port/fd-port.rkt +++ b/racket/src/io/port/fd-port.rkt @@ -26,16 +26,19 @@ (struct fd-data (fd extra input?) #:property prop:file-stream (lambda (fdd) (fd-data-fd fdd)) + #:property prop:data-place-message (lambda (port) + (lambda () + (fd-port->place-message port)))) + +(struct fd-output-data fd-data (flush) #:property prop:file-truncate (case-lambda [(fdd pos) + ((fd-output-data-flush fdd)) (check-rktio-error* (rktio_set_file_size rktio (fd-data-fd fdd) pos) - "error setting file size")]) - #:property prop:data-place-message (lambda (port) - (lambda () - (fd-port->place-message port)))) + "error setting file size")])) (define (maybe-fd-data-extra data) (and (fd-data? data) @@ -172,7 +175,11 @@ (define port (make-core-output-port #:name name - #:data (fd-data fd extra-data #f) + #:data (fd-output-data fd extra-data #f + ;; Flush function needed for `file-truncate`: + (lambda () + (atomically + (flush-buffer-fully #f)))) #:evt evt