Skip to content
This repository has been archived by the owner on Oct 22, 2021. It is now read-only.

Commit

Permalink
move responsibility for creation and deletion into monic_file (and cl…
Browse files Browse the repository at this point in the history
…ose the file descriptors when we delete)
  • Loading branch information
Robert Newson committed Mar 19, 2011
1 parent 997c0f6 commit cfdbec0
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
4 changes: 2 additions & 2 deletions src/monic_container_resource.erl
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ content_types_provided(ReqData, Context) ->
{[{?CONTAINER_MIME_TYPE, get_container}], ReqData, Context}.

delete_resource(ReqData, Context) ->
{file:delete(monic_utils:path(ReqData, Context)) == ok, ReqData, Context}.
{monic_file:delete(monic_utils:path(ReqData, Context)) == ok, ReqData, Context}.

is_conflict(ReqData, Context) ->
{monic_utils:exists(ReqData, Context), ReqData, Context}.
Expand All @@ -54,4 +54,4 @@ get_container(ReqData, Context) ->
{"{\"ok\": true}", ReqData, Context}.

put_container(ReqData, Context) ->
{file:write_file(monic_utils:path(ReqData, Context), <<>>, [exclusive]) == ok, ReqData, Context}.
{monic_file:create(monic_utils:path(ReqData, Context)), ReqData, Context}.
18 changes: 17 additions & 1 deletion src/monic_file.erl
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
-include("monic.hrl").

%% public API
-export([open/1, open_new/1, close/1, add/5, delete/3, info/3, read/3]).
-export([create/1, delete/1, open/1, open_new/1, close/1, add/5, delete/3, info/3, read/3]).

%% gen_server API
-export([init/1, terminate/2, code_change/3,handle_call/3, handle_cast/2, handle_info/2]).
Expand Down Expand Up @@ -55,6 +55,22 @@ open_new(Path) ->
close(Pid) ->
gen_server:call(Pid, close, infinity).

create(Path) ->
file:write_file(Path, <<>>, [exclusive]) == ok.

delete(Path) ->
case file:delete(Path ++ ".idx") == ok andalso file:delete(Path) == ok of
true ->
case open(Path) of
{ok, Pid} ->
close(Pid); %% ensure fds are released.
Else ->
Else
end;
Else ->
Else
end.

-spec add(pid(), binary(), integer(), integer(), streambody()) -> ok | {error, term()}.
add(Pid, Key, Cookie, Size, StreamBody) ->
case gen_server:call(Pid, {start_writing, Key, Cookie, Size}) of
Expand Down

0 comments on commit cfdbec0

Please sign in to comment.