summaryrefslogtreecommitdiff
path: root/src/atomic.erl
blob: 580fd17ad86e06be0dc77524e6f5530338e95e6a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
%%% Copyright (c) 2014, NORDUnet A/S.
%%% See LICENSE for licensing information.

-module(atomic).
-export([replacefile/2, readfile/1, openfile/2, readline/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() | noentry.
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.

openfile(Path, Modes) ->
    file:open(Path, Modes).

readline(IoDevice) ->
    file:read_line(IoDevice).