diff options
author | David Barksdale <amatus.amongus@gmail.com> | 2012-07-18 23:46:57 -0500 |
---|---|---|
committer | David Barksdale <amatus.amongus@gmail.com> | 2012-07-18 23:46:57 -0500 |
commit | fd9befa6bb8b13d703c4e65c3d409156f6b26db6 (patch) | |
tree | 05d87bbf12611cfaf076e2f6a0a92cf888c0d53f | |
parent | 5654c9bf58211e1fcec5f7553fa751066d7e6824 (diff) |
Fix race condition in local-write-block.
-rw-r--r-- | src/clojure/foofs/localbackend.clj | 12 |
1 files 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)))) |