From ce464810d5d97fd60043b2e4214a2021532dab4e Mon Sep 17 00:00:00 2001 From: Matthew Flatt Date: Sat, 18 Oct 2014 06:43:58 -0500 Subject: [PATCH] file/tar: add `#:exists-ok?` argument The new argument is needed to reliably write to a tmp file, for example, where the existence of the tmp file prevents other processes from using the same name. --- .../racket-doc/file/scribblings/tar.scrbl | 12 ++++++++++-- racket/collects/file/tar.rkt | 4 ++++ 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/pkgs/racket-pkgs/racket-doc/file/scribblings/tar.scrbl b/pkgs/racket-pkgs/racket-doc/file/scribblings/tar.scrbl index 82ee7ab22b..3f2232f65a 100644 --- a/pkgs/racket-pkgs/racket-doc/file/scribblings/tar.scrbl +++ b/pkgs/racket-pkgs/racket-doc/file/scribblings/tar.scrbl @@ -17,6 +17,7 @@ in a link must be less than 100 bytes.} @defproc[(tar [tar-file path-string?] [path path-string?] ... + [#:exists-ok? exists-ok? any/c #f] [#:path-prefix path-prefix (or/c #f path-string?) #f] [#:get-timestamp get-timestamp (path? . -> . exact-integer?) @@ -33,13 +34,18 @@ to the current directory). If a nested path is provided as a resulting tar file, up to the current directory (using @racket[pathlist-closure]). +If @racket[exists-ok?] is @racket[#f], then an exception is raised if +@racket[tar-file] exists already. If @racket[exists-ok?] is true, then +@racket[tar-file] is truncated or replaced if it exists already. + If @racket[path-prefix] is not @racket[#f], then it is prefixed to each path in the archive. The @racket[get-timestamp] function is used to obtain the modification date to record in the archive for each file or directory. -@history[#:changed "6.0.0.3" @elem{Added the @racket[#:get-timestamp] argument.}]} +@history[#:changed "6.0.0.3" @elem{Added the @racket[#:get-timestamp] argument.} + #:changed "6.1.1.1" @elem{Added the @racket[#:exists-ok?] argument.}]} @defproc[(tar->output [paths (listof path?)] @@ -63,6 +69,7 @@ without parent directories. @defproc[(tar-gzip [tar-file path-string?] [paths path-string?] ... + [#:exists-ok? exists-ok? any/c #f] [#:path-prefix path-prefix (or/c #f path-string?) #f] [#:get-timestamp get-timestamp (path? . -> . exact-integer?) @@ -73,4 +80,5 @@ without parent directories. Like @racket[tar], but compresses the resulting file with @racket[gzip]. -@history[#:changed "6.0.0.3" @elem{Added the @racket[#:get-timestamp] argument.}]} +@history[#:changed "6.0.0.3" @elem{Added the @racket[#:get-timestamp] argument.} + #:changed "6.1.1.1" @elem{Added the @racket[#:exists-ok?] argument.}]} diff --git a/racket/collects/file/tar.rkt b/racket/collects/file/tar.rkt index f052a0e574..14b9c7e79b 100644 --- a/racket/collects/file/tar.rkt +++ b/racket/collects/file/tar.rkt @@ -149,11 +149,13 @@ ;; tar : output-file paths -> (provide tar) (define (tar tar-file + #:exists-ok? [exists-ok? #f] #:path-prefix [prefix #f] #:get-timestamp [get-timestamp file-or-directory-modify-seconds] . paths) (when (null? paths) (error 'tar "no paths specified")) (with-output-to-file tar-file + #:exists (if exists-ok? 'truncate/replace 'error) (lambda () (tar->output (pathlist-closure paths #:follow-links? #f) #:get-timestamp get-timestamp #:path-prefix prefix)))) @@ -161,11 +163,13 @@ ;; tar-gzip : output-file paths -> (provide tar-gzip) (define (tar-gzip tgz-file + #:exists-ok? [exists-ok? #f] #:path-prefix [prefix #f] #:get-timestamp [get-timestamp file-or-directory-modify-seconds] . paths) (when (null? paths) (error 'tar-gzip "no paths specified")) (with-output-to-file tgz-file + #:exists (if exists-ok? 'truncate/replace 'error) (lambda () (let-values ([(i o) (make-pipe (* 1024 1024 32))]) (thread (lambda ()