diff options
author | Magnus Ahltorp <map@kth.se> | 2014-09-25 08:35:07 +0200 |
---|---|---|
committer | Magnus Ahltorp <map@kth.se> | 2014-09-25 17:52:50 +0200 |
commit | eb95f6951e7a4abd2b7685b2de07de90b90ee0d2 (patch) | |
tree | 55f3168b14b8e7da0511c60c4a2d0a9160b24e7a | |
parent | 29ac49eabca61c4a9e0c3a0d8f9ba57ab516ebae (diff) |
perm: Don't crash if file content is different, tell caller instead. Better error handling.
-rw-r--r-- | src/perm.erl | 54 |
1 files changed, 35 insertions, 19 deletions
diff --git a/src/perm.erl b/src/perm.erl index 2ce5b46..34f431c 100644 --- a/src/perm.erl +++ b/src/perm.erl @@ -6,17 +6,25 @@ -module(perm). -export([ensurefile/3]). -fsync(Name) -> - fsyncport:fsync(Name). +fsync([]) -> + ok; +fsync([Name | Rest]) -> + case fsyncport:fsync(Name) of + ok -> + fsync(Rest); + {error, Error} -> + {error, Error} + end. readfile_and_verify(Name, Content) -> case file:read_file(Name) of {ok, ContentsReadBinary} -> ContentsRead = binary_to_list(ContentsReadBinary), - if Content == ContentsRead -> + if + Content == ContentsRead -> ok; - true -> - {error, "File contents differ"} + true -> + differ end; {error, Error} -> {error, Error} @@ -72,24 +80,32 @@ tempfilename(Base) -> MegaSecs * 1000000 + Secs, MicroSecs]), Filename. +exit_with_error(Error, Message) -> + io:format("~s: ~w~n", [Message, Error]), + exit({perm, fileerror, Message, Error}). + +check_error(ReturnValue, ErrorMessage) -> + case ReturnValue of + ok -> + ok; + {error, Error} -> + exit_with_error(Error, ErrorMessage) + end. + ensurefile(Rootdir, Key, Content) -> {Dirs, Path} = path_for_key(Rootdir, Key), case readfile_and_verify(Path, Content) of ok -> - lists:foreach(fun (Dir) -> fsync(Dir) end, [Path, Rootdir | Dirs]); + check_error(fsync([Path, Rootdir | Dirs]), "Error in fsync"); + differ -> + differ; {error, enoent} -> - case make_dirs([Rootdir, Rootdir ++ "nursery/"] ++ Dirs) of - ok -> - NurseryName = Rootdir ++ "nursery/" ++ - tempfilename(hex:bin_to_hexstr(Key)), - _Result = writefile(Path, NurseryName, Content), - lists:foreach(fun (Dir) -> - fsync(Dir) - end, - [Path, Rootdir | Dirs]); %% XXX check results - {error, Error} -> - io:format("Error creating directory: ~w~n", [Error]) - end; + check_error(make_dirs([Rootdir, Rootdir ++ "nursery/"] ++ Dirs), + "Error creating directory"), + NurseryName = Rootdir ++ "nursery/" ++ + tempfilename(hex:bin_to_hexstr(Key)), + _Result = writefile(Path, NurseryName, Content), + check_error(fsync([Path, Rootdir | Dirs]), "Error in fsync"); {error, Error} -> - exit({perm, fileerror, "Error reading file", Error}) + exit_with_error(Error, "Error reading file") end. |