Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support MDTM #12

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions README.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,9 @@ yield the appropriate value:
- an array of strings to join with the standard FTP line break and send to
the client

mtime(path, &block)
- time of last modification or nil if the file doesn't exist

The driver MUST have one of the following methods. Each method MUST accept a
block and yield the appropriate value:

Expand Down
16 changes: 16 additions & 0 deletions lib/em-ftpd/files.rb
Original file line number Diff line number Diff line change
Expand Up @@ -143,5 +143,21 @@ def cmd_stor_tempfile(target_path)
end
end

# return the file's mtime
def cmd_mdtm(param)
send_unauthorised and return unless logged_in?
send_param_required and return if param.nil?

path = build_path(param)

@driver.mtime(path) do |result|
if result.kind_of?(Time)
send_response result.strftime("213 %Y%m%d%H%M%S%L")
else
send_response "550 file not available"
end
end
end

end
end
2 changes: 1 addition & 1 deletion lib/em-ftpd/server.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class Server < EM::Connection

COMMANDS = %w[quit type user retr stor eprt port cdup cwd dele rmd pwd
list size syst mkd pass xcup xpwd xcwd xrmd rest allo nlst
pasv epsv help noop mode rnfr rnto stru feat]
pasv epsv help noop mode rnfr rnto stru feat mdtm]

attr_reader :root, :name_prefix
attr_accessor :datasocket
Expand Down
53 changes: 53 additions & 0 deletions spec/server_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -847,3 +847,56 @@
end

end

describe EM::FTPD::Server, "MDTM" do

before(:each) do
@c = EM::FTPD::Server.new(nil, TestDriver.new)
end

it "should always respond with 530 when called by a non logged in user" do
@c.reset_sent!
@c.receive_line("MDTM one.txt")
@c.sent_data.should match(/530.+/)
end

it "should always respond with 553 when called with no param" do
@c.receive_line("USER test")
@c.receive_line("PASS 1234")
@c.reset_sent!
@c.receive_line("MDTM")
@c.sent_data.should match(/553.+/)
end

it "should always respond with 213 when called with a directory param" do
@c.receive_line("USER test")
@c.receive_line("PASS 1234")
@c.reset_sent!
@c.receive_line("MDTM files")
@c.sent_data.should match(/^213 20130421110000/)
end

it "should always respond with 550 when called with a non-file param" do
@c.receive_line("USER test")
@c.receive_line("PASS 1234")
@c.reset_sent!
@c.receive_line("MDTM blah")
@c.sent_data.should match(/550.+/)
end

it "should always respond with 213 when called with a valid file param" do
@c.receive_line("USER test")
@c.receive_line("PASS 1234")
@c.reset_sent!
@c.receive_line("MDTM one.txt")
@c.sent_data.should match(/^213 20130421120000/)
end

it "should always respond with 213 when called with a valid file param" do
@c.receive_line("USER test")
@c.receive_line("PASS 1234")
@c.reset_sent!
@c.receive_line("MDTM files/two.txt")
@c.sent_data.should match(/^213 20130421130000/)
end
end
10 changes: 10 additions & 0 deletions spec/support/test_driver.rb
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,16 @@ def make_dir(path, &block)
yield path == "/four"
end

def mtime(path, &block)
yield case path
when "/files" then Time.utc(2013,4,21,11,0,0)
when "/one.txt" then Time.utc(2013,4,21,12,0,0)
when "/files/two.txt" then Time.utc(2013,4,21,13,0,0)
else
false
end
end

private

def dir_item(name)
Expand Down