fs.erl 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. -module(fs).
  2. -include_lib("kernel/include/file.hrl").
  3. -export([subscribe/0, known_events/0, start_looper/0, path/0, find_executable/2]).
  4. % sample subscriber
  5. subscribe() -> gen_event:add_sup_handler(fs_events, {fs_event_bridge, self()}, [self()]).
  6. known_events() -> gen_server:call(fs_server, known_events).
  7. start_looper() -> spawn(fun() -> subscribe(), loop() end).
  8. path() ->
  9. case application:get_env(fs, path) of
  10. {ok, P} -> filename:absname(P);
  11. undefined -> filename:absname("") end.
  12. loop() ->
  13. receive
  14. {_Pid, {fs, file_event}, {Path, Flags}} -> error_logger:info_msg("file_event: ~p ~p", [Path, Flags]);
  15. _ -> ignore end,
  16. loop().
  17. find_executable(Cmd, DepsPath) ->
  18. case priv_file(Cmd) of
  19. false -> mad_file(DepsPath);
  20. Priv -> Priv end.
  21. mad_file(DepsPath) ->
  22. case filelib:is_regular(DepsPath) of
  23. true -> DepsPath;
  24. false ->
  25. case mad_repl:load_file(DepsPath) of
  26. {error,_} ->
  27. %% This path has been already checked in find_executable/2
  28. false;
  29. {ok,ETSFile} ->
  30. filelib:ensure_dir(DepsPath),
  31. file:write_file(DepsPath, ETSFile),
  32. file:write_file_info(DepsPath, #file_info{mode=8#00555}) end end.
  33. priv_file(Cmd) ->
  34. case code:priv_dir(fs) of
  35. Priv when is_list(Priv) ->
  36. Path = filename:join(Priv, Cmd),
  37. case filelib:is_regular(Path) of
  38. true -> Path;
  39. false -> false end;
  40. _ ->
  41. false end.