Skip to content

Commit c70a51a

Browse files
committed
always write to tmp file and move
http://arclanguage.org/item?id=16109 tmp file is dynamically chosen so (mostly) thread-safe
1 parent d0d7cc2 commit c70a51a

File tree

1 file changed

+29
-18
lines changed

1 file changed

+29
-18
lines changed

arc.arc

Lines changed: 29 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -898,11 +898,37 @@
898898
(mac reading (var port . body)
899899
`(ifread ,var ,port (do ,@body)))
900900

901-
; inconsistency between names of readfile[1] and writefile
901+
(mac fromfile (f . body)
902+
(w/uniq gf
903+
`(w/infile ,gf ,f
904+
(w/stdin ,gf
905+
,@body))))
906+
907+
(mac tofile (f . body)
908+
(w/uniq (gf gs)
909+
`(let ,gs (+ ,f "." rand-string.6)
910+
(w/outfile ,gf ,gs
911+
(w/stdout ,gf
912+
,@body))
913+
(mvfile ,gs ,f))))
914+
915+
(mac ontofile (f . body)
916+
(w/uniq gf
917+
`(w/appendfile ,gf ,f
918+
(w/stdout ,gf
919+
,@body))))
902920

903-
(def readfile (name) (w/infile s name (drain:read s)))
921+
(def readfile (name)
922+
(fromfile name
923+
(drain:read)))
904924

905-
(def readfile1 (name) (w/infile s name (read s)))
925+
(def readfile1 (name)
926+
(fromfile name
927+
(read)))
928+
929+
(def writefile (val name)
930+
(tofile name
931+
(write val)))
906932

907933
(def readall (src (o eof nil))
908934
((afn (i)
@@ -919,12 +945,6 @@
919945
(def filechars (name)
920946
(w/infile s name (allchars s)))
921947

922-
(def writefile (val file)
923-
(let tmpfile (+ file ".tmp")
924-
(w/outfile o tmpfile (write val o))
925-
(mvfile tmpfile file))
926-
val)
927-
928948
(= ac-denil ($ ac-denil))
929949
(= ac-global-name ($ ac-global-name))
930950
(= ac-niltree ($ ac-niltree))
@@ -1925,15 +1945,6 @@
19251945
,within)
19261946
,@body))))
19271947

1928-
(mac tofile (name . body)
1929-
(w/uniq str `(w/outfile ,str ,name (w/stdout ,str ,@body))))
1930-
1931-
(mac ontofile (name . body)
1932-
(w/uniq str `(w/appendfile ,str ,name (w/stdout ,str ,@body))))
1933-
1934-
(mac fromfile (name . body)
1935-
(w/uniq str `(w/infile ,str ,name (w/stdin ,str ,@body))))
1936-
19371948
(def cars (xs) (map car xs))
19381949
(def cdrs (xs) (map cdr xs))
19391950

0 commit comments

Comments
 (0)