summaryrefslogtreecommitdiff
path: root/src/atomic.erl
diff options
context:
space:
mode:
authorMagnus Ahltorp <map@kth.se>2014-09-27 23:34:58 +0200
committerMagnus Ahltorp <map@kth.se>2014-09-27 23:34:58 +0200
commit54ce1162dc56e02f2e086e7bd34b97b57604e7a4 (patch)
tree3be1b442e896a39f894d639528bd494048061d6b /src/atomic.erl
parentc3111912df87159d1c3bccac351f614a25a3c553 (diff)
parent9c0d01c3059a7f82ac9acd5574755feed2f351a8 (diff)
Merge branch 'fsync3' into fsync4
Diffstat (limited to 'src/atomic.erl')
-rw-r--r--src/atomic.erl24
1 files changed, 24 insertions, 0 deletions
diff --git a/src/atomic.erl b/src/atomic.erl
new file mode 100644
index 0000000..5bf5670
--- /dev/null
+++ b/src/atomic.erl
@@ -0,0 +1,24 @@
+%%
+%% Copyright (c) 2014 Kungliga Tekniska Högskolan
+%% (KTH Royal Institute of Technology, Stockholm, Sweden).
+%%
+
+-module(atomic).
+-export([replacefile/2, readfile/1]).
+
+-spec replacefile(string(), binary()) -> ok.
+replacefile(Path, Content) ->
+ TempName = util:tempfilename(Path),
+ util:write_tempfile_and_rename(Path, TempName, Content),
+ util:fsync([Path, filename:dirname(Path)]).
+
+-spec readfile(string()) -> binary().
+readfile(Path) ->
+ case file:read_file(Path) of
+ {ok, Contents} ->
+ Contents;
+ {error, enoent} ->
+ noentry;
+ {error, Error} ->
+ util:exit_with_error(readfile, Error, "Error reading file")
+ end.