diff options
| author | Magnus Ahltorp <map@kth.se> | 2014-09-25 15:18:39 +0200 |
|---|---|---|
| committer | Magnus Ahltorp <map@kth.se> | 2014-09-25 15:18:39 +0200 |
| commit | 48f0cff6aaed918cedc8a1eadfd2bf08c58c810e (patch) | |
| tree | e26cdc04daad156c62fef7d733f1465340166ea6 /src/atomic.erl | |
| parent | 47bc655b471c86b47090ea91d526d3961dc6aaf6 (diff) | |
Added atomic modulefsync2
Diffstat (limited to 'src/atomic.erl')
| -rw-r--r-- | src/atomic.erl | 24 |
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. |
