Skip to content

Commit

Permalink
support {module, function} tuple as event handler
Browse files Browse the repository at this point in the history
  • Loading branch information
jalpedersen committed Jul 9, 2019
1 parent 1a9e937 commit a4f5b38
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 25 deletions.
7 changes: 7 additions & 0 deletions lib/events.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
defmodule Exsftpd.Events do
require Logger

def log_event(event) do
Logger.debug("#{inspect(event)}")
end
end
54 changes: 29 additions & 25 deletions lib/sftpd_file_handler.ex
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
defmodule Exsftpd.SftpFileHandler do

defp user_path(path, state) do
Path.join(state[:root_path], path)
end

defp on_event({event_name, meta}, state) do
case state[:event_handler] do
nil -> nil
{module, fun} -> apply(module, fun, [{event_name, state[:user], meta}])
handler -> handler.({event_name, state[:user], meta})
end
end
Expand All @@ -16,7 +16,6 @@ defmodule Exsftpd.SftpFileHandler do
result
end


defp get_file_info(io_device) do
case :file.pid2name(io_device) do
{:ok, filename} -> {io_device, filename}
Expand All @@ -25,18 +24,15 @@ defmodule Exsftpd.SftpFileHandler do
end

def close(io_device, state) do
after_event({:close, get_file_info(io_device)}, state,
{:file.close(io_device), state})
after_event({:close, get_file_info(io_device)}, state, {:file.close(io_device), state})
end

def delete(path, state) do
after_event({:delete, path}, state,
{:file.delete(user_path(path, state)), state})
after_event({:delete, path}, state, {:file.delete(user_path(path, state)), state})
end

def del_dir(path, state) do
after_event({:del_dir, path}, state,
{:file.del_dir(user_path(path, state)), state})
after_event({:del_dir, path}, state, {:file.del_dir(user_path(path, state)), state})
end

def get_cwd(state) do
Expand All @@ -52,31 +48,34 @@ defmodule Exsftpd.SftpFileHandler do
end

def make_dir(dir, state) do
after_event({:make_dir, dir}, state,
{:file.make_dir(user_path(dir, state)), state})
after_event({:make_dir, dir}, state, {:file.make_dir(user_path(dir, state)), state})
end

def make_symlink(path2, path, state) do
after_event({:make_symlink, {path2, path}}, state,
{:file.make_symlink(user_path(path2, state), user_path(path, state)), state})
after_event(
{:make_symlink, {path2, path}},
state,
{:file.make_symlink(user_path(path2, state), user_path(path, state)), state}
)
end

def open(path, flags, state) do
{case :file.open(user_path(path, state), flags) do
{:ok, pid} ->
on_event({:open, {get_file_info(pid), path, flags}}, state)
{:ok, pid}
other -> other
end, state}
{:ok, pid} ->
on_event({:open, {get_file_info(pid), path, flags}}, state)
{:ok, pid}

other ->
other
end, state}
end

def position(io_device, offs, state) do
{:file.position(io_device, offs), state}
end

def read(io_device, len, state) do
after_event({:read, get_file_info(io_device)}, state,
{:file.read(io_device, len), state})
after_event({:read, get_file_info(io_device)}, state, {:file.read(io_device, len), state})
end

def read_link(path, state) do
Expand All @@ -92,17 +91,22 @@ defmodule Exsftpd.SftpFileHandler do
end

def rename(path, path2, state) do
after_event({:rename, {path, path2}}, state,
{:file.rename(user_path(path, state), user_path(path2, state)), state})
after_event(
{:rename, {path, path2}},
state,
{:file.rename(user_path(path, state), user_path(path2, state)), state}
)
end

def write(io_device, data, state) do
after_event({:write, get_file_info(io_device)}, state,
{:file.write(io_device, data), state})
after_event({:write, get_file_info(io_device)}, state, {:file.write(io_device, data), state})
end

def write_file_info(path, info, state) do
after_event({:write_file_info, {path, info}}, state,
{:file.write_file_info(user_path(path, state), info), state})
after_event(
{:write_file_info, {path, info}},
state,
{:file.write_file_info(user_path(path, state), info), state}
)
end
end

0 comments on commit a4f5b38

Please sign in to comment.