From fd9befa6bb8b13d703c4e65c3d409156f6b26db6 Mon Sep 17 00:00:00 2001 From: David Barksdale Date: Wed, 18 Jul 2012 23:46:57 -0500 Subject: Fix race condition in local-write-block. --- src/clojure/foofs/localbackend.clj | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/src/clojure/foofs/localbackend.clj b/src/clojure/foofs/localbackend.clj index d15712b..a4ce223 100644 --- a/src/clojure/foofs/localbackend.clj +++ b/src/clojure/foofs/localbackend.clj @@ -98,11 +98,15 @@ dir-name (String. (into-array Character/TYPE (take 2 hash-chars))) file-name (String. (into-array Character/TYPE (drop 2 hash-chars))) dir-path (str "/tmp/foofs/" dir-name) - path (str dir-path "/" file-name)] + path (str dir-path "/" file-name) + file (File. path)] (try - (.mkdirs (File. dir-path)) - (.write (FileOutputStream. path) (to-byte-array e-block)) - true + (if (.isFile file) + true + (let [dir-file (doto (File. dir-path) (.mkdirs)) + temp-file (File/createTempFile "foofs" nil dir-file)] + (.write (FileOutputStream. temp-file) (to-byte-array e-block)) + (.renameTo temp-file file))) (catch Exception _ false)))) -- cgit v1.2.3-18-g5258