From 11f8efc7fb27935761c38cf32f41836193ae97f4 Mon Sep 17 00:00:00 2001 From: Magnus Ahltorp Date: Thu, 25 Sep 2014 09:04:48 +0200 Subject: perm: Added readfile function. --- src/perm.erl | 25 ++++++++++++++++--------- 1 file changed, 16 insertions(+), 9 deletions(-) diff --git a/src/perm.erl b/src/perm.erl index 34f431c..5cd2889 100644 --- a/src/perm.erl +++ b/src/perm.erl @@ -4,7 +4,7 @@ %% -module(perm). --export([ensurefile/3]). +-export([ensurefile/3, readfile/2]). fsync([]) -> ok; @@ -18,14 +18,10 @@ fsync([Name | Rest]) -> readfile_and_verify(Name, Content) -> case file:read_file(Name) of - {ok, ContentsReadBinary} -> - ContentsRead = binary_to_list(ContentsReadBinary), - if - Content == ContentsRead -> - ok; - true -> - differ - end; + {ok, ContentsRead} when Content == ContentsRead -> + ok; + {ok, _ContentsRead} -> + differ; {error, Error} -> {error, Error} end. @@ -109,3 +105,14 @@ ensurefile(Rootdir, Key, Content) -> {error, Error} -> exit_with_error(Error, "Error reading file") end. + +readfile(Rootdir, Key) -> + {_Dirs, Path} = path_for_key(Rootdir, Key), + case file:read_file(Path) of + {ok, Contents} -> + Contents; + {error, enoent} -> + noentry; + {error, Error} -> + exit_with_error(Error, "Error reading file") + end. -- cgit v1.1