fs_sup.erl 1.1 KB

12345678910111213141516171819202122232425262728293031323334
  1. -module(fs_sup).
  2. -behaviour(supervisor).
  3. -export([start_link/0]).
  4. -export([init/1]).
  5. -define(CHILD(I, Type, Args), {I, {I, start_link, Args}, permanent, 5000, Type, [I]}).
  6. start_link() -> supervisor:start_link({local, ?MODULE}, ?MODULE, []).
  7. init([]) ->
  8. Backend = case os:type() of
  9. {unix, darwin} -> fsevents;
  10. {unix, linux} -> inotifywait;
  11. {unix, _} -> kqueue;
  12. {win32, nt} -> inotifywait_win32;
  13. _ -> undefined end,
  14. Children = case has_executable(Backend) of
  15. false -> [];
  16. true -> Path = fs:path(), [?CHILD(fs_server, worker, [Backend, Path, Path])] end,
  17. {ok, { {one_for_one, 5, 10},
  18. Children ++ [?CHILD(gen_event, worker, [{local, fs_events}])]} }.
  19. has_executable(undefined) ->
  20. os_not_supported(), false;
  21. has_executable(Backend) ->
  22. case Backend:find_executable() of
  23. false -> backend_port_not_found(Backend), false;
  24. _ -> true end.
  25. os_not_supported() ->
  26. error_logger:error_msg("fs does not support the current operating system~n",[]).
  27. backend_port_not_found(Backend) ->
  28. error_logger:error_msg("backend port not found: ~p~n",[Backend]).