|
@@ -1,41 +1,58 @@
|
|
|
-module(fs).
|
|
|
-include_lib("kernel/include/file.hrl").
|
|
|
--export([subscribe/0, known_events/0, start_looper/0, path/0, find_executable/2]).
|
|
|
+-export([start_link/1, start_link/2, subscribe/0, subscribe/1, known_events/0, known_events/1,
|
|
|
+ start_looper/0, start_looper/1, find_executable/2, path/0]).
|
|
|
|
|
|
% sample subscriber
|
|
|
|
|
|
-subscribe() -> gen_event:add_sup_handler(fs_events, {fs_event_bridge, self()}, [self()]).
|
|
|
-known_events() -> gen_server:call(fs_server, known_events).
|
|
|
-start_looper() -> spawn(fun() -> subscribe(), loop() end).
|
|
|
+start_link(Name) -> start_link(Name, path()).
|
|
|
+start_link(Name, Path) ->
|
|
|
+ SupName = name(Name, "sup"),
|
|
|
+ FileHandler = name(Name, "file"),
|
|
|
+ fs_sup:start_link(SupName, Name, FileHandler, Path).
|
|
|
+
|
|
|
+subscribe() -> subscribe(default_fs).
|
|
|
+subscribe(Name) -> gen_event:add_sup_handler(Name, {fs_event_bridge, self()}, [self()]).
|
|
|
|
|
|
path() ->
|
|
|
- case application:get_env(fs, path) of
|
|
|
- {ok, P} -> filename:absname(P);
|
|
|
- undefined -> filename:absname("") end.
|
|
|
+ case application:get_env(fs, path) of
|
|
|
+ {ok, P} -> filename:absname(P);
|
|
|
+ undefined -> filename:absname("")
|
|
|
+ end.
|
|
|
+
|
|
|
+known_events() -> known_events(default_fs).
|
|
|
+known_events(Name) -> gen_server:call(name(Name, "file"), known_events).
|
|
|
+
|
|
|
+start_looper() -> start_looper(default_fs).
|
|
|
+start_looper(Name) -> spawn(fun() -> subscribe(Name), loop() end).
|
|
|
|
|
|
loop() ->
|
|
|
- receive
|
|
|
- {_Pid, {fs, file_event}, {Path, Flags}} -> error_logger:info_msg("file_event: ~p ~p", [Path, Flags]);
|
|
|
- _ -> ignore end,
|
|
|
- loop().
|
|
|
+ receive
|
|
|
+ {_Pid, {fs, file_event}, {Path, Flags}} -> error_logger:info_msg("file_event: ~p ~p", [Path, Flags]);
|
|
|
+ _ -> ignore
|
|
|
+ end,
|
|
|
+ loop().
|
|
|
|
|
|
find_executable(Cmd, DepsPath) ->
|
|
|
- case priv_file(Cmd) of
|
|
|
+ case priv_file(Cmd) of
|
|
|
false -> mad_file(DepsPath);
|
|
|
- Priv -> Priv end.
|
|
|
+ Priv -> Priv
|
|
|
+ end.
|
|
|
|
|
|
mad_file(DepsPath) ->
|
|
|
- case filelib:is_regular(DepsPath) of
|
|
|
- true -> DepsPath;
|
|
|
+ case filelib:is_regular(DepsPath) of
|
|
|
+ true -> path() ++ "/" ++ DepsPath;
|
|
|
false ->
|
|
|
- case mad_repl:load_file(DepsPath) of
|
|
|
- {error,_} ->
|
|
|
- %% This path has been already checked in find_executable/2
|
|
|
- false;
|
|
|
- {ok,ETSFile} ->
|
|
|
- filelib:ensure_dir(DepsPath),
|
|
|
- file:write_file(DepsPath, ETSFile),
|
|
|
- file:write_file_info(DepsPath, #file_info{mode=8#00555}) end end.
|
|
|
+ case load_file(DepsPath) of
|
|
|
+ {error, _} ->
|
|
|
+ %% This path has been already checked in find_executable/2
|
|
|
+ false;
|
|
|
+ {ok, ETSFile} ->
|
|
|
+ filelib:ensure_dir(DepsPath),
|
|
|
+ file:write_file(DepsPath, ETSFile),
|
|
|
+ file:write_file_info(DepsPath, #file_info{mode=8#00555})
|
|
|
+ end
|
|
|
+ end.
|
|
|
|
|
|
priv_file(Cmd) ->
|
|
|
case code:priv_dir(fs) of
|
|
@@ -46,3 +63,24 @@ priv_file(Cmd) ->
|
|
|
false -> false end;
|
|
|
_ ->
|
|
|
false end.
|
|
|
+
|
|
|
+
|
|
|
+name(Name, Prefix) ->
|
|
|
+ NameList = erlang:atom_to_list(Name),
|
|
|
+ list_to_atom(NameList ++ Prefix).
|
|
|
+
|
|
|
+
|
|
|
+ets_created() ->
|
|
|
+ case ets:info(filesystem) of
|
|
|
+ undefined -> ets:new(filesystem, [set, named_table, {keypos, 1}, public]);
|
|
|
+ _ -> skip
|
|
|
+ end.
|
|
|
+
|
|
|
+
|
|
|
+load_file(Name) ->
|
|
|
+ ets_created(),
|
|
|
+ case ets:lookup(filesystem, Name) of
|
|
|
+ [{Name, Bin}] -> {ok, Bin};
|
|
|
+ _ -> {error, etsfs}
|
|
|
+ end.
|
|
|
+
|