From eb95f6951e7a4abd2b7685b2de07de90b90ee0d2 Mon Sep 17 00:00:00 2001 From: Magnus Ahltorp Date: Thu, 25 Sep 2014 08:35:07 +0200 Subject: perm: Don't crash if file content is different, tell caller instead. Better error handling. --- src/perm.erl | 54 +++++++++++++++++++++++++++++++++++------------------- 1 file changed, 35 insertions(+), 19 deletions(-) (limited to 'src/perm.erl') 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. -- cgit v1.1