Browse files

Merge pull request #25 from kcdragon/feature-nlst-mask

Add Support for Wildcard argument in NLST command
  • Loading branch information...
2 parents 609962f + 6981b06 commit 1382f849b3af1cf946e144a14b290d4a0cd87140 @meatballhat meatballhat committed Feb 15, 2016
Showing with 48 additions and 15 deletions.
  1. +30 −13 lib/fake_ftp/server.rb
  2. +18 −2 spec/functional/server_spec.rb
View
43 lib/fake_ftp/server.rb
@@ -146,23 +146,14 @@ def _cdup(*args)
end
def _list(*args)
- wildcards = []
- args.each do |arg|
- next unless arg.include? '*'
- wildcards << arg.gsub('*', '.*')
- end
-
respond_with('425 Ain\'t no data port!') && return if active? && @active_connection.nil?
respond_with('150 Listing status ok, about to open data connection')
data_client = active? ? @active_connection : @data_server.accept
- files = @files
- if not wildcards.empty?
- files = files.select do |f|
- wildcards.any? { |wildcard| f.name =~ /#{wildcard}/ }
- end
- end
+ wildcards = build_wildcards(args)
+ files = matching_files(@files, wildcards)
+
files = files.map do |f|
"-rw-r--r--\t1\towner\tgroup\t#{f.bytes}\t#{f.created.strftime('%b %d %H:%M')}\t#{f.name}\n"
end
@@ -187,7 +178,14 @@ def _nlst(*args)
respond_with('150 Listing status ok, about to open data connection')
data_client = active? ? @active_connection : @data_server.accept
- data_client.write(files.join("\n"))
+ wildcards = build_wildcards(args)
+ files = matching_files(@files, wildcards)
+
+ files = files.map do |file|
+ "#{file.name}\n"
+ end
+
+ data_client.write(files.join)
data_client.close
@active_connection = nil
@@ -336,5 +334,24 @@ def port_is_open?(port)
return false
end
+
+ def build_wildcards(args)
+ wildcards = []
+ args.each do |arg|
+ next unless arg.include? '*'
+ wildcards << arg.gsub('*', '.*')
+ end
+ wildcards
+ end
+
+ def matching_files(files, wildcards)
+ if not wildcards.empty?
+ files.select do |f|
+ wildcards.any? { |wildcard| f.name =~ /#{wildcard}/ }
+ end
+ else
+ files
+ end
+ end
end
end
View
20 spec/functional/server_spec.rb
@@ -317,7 +317,23 @@
expect(client.gets).to eql("150 Listing status ok, about to open data connection\r\n")
data = data_client.read(1024)
data_client.close
- expect(data).to eql("some_file\nanother_file")
+ expect(data).to eql("some_file\nanother_file\n")
+ expect(client.gets).to eql("226 List information transferred\r\n")
+ end
+
+ it "accepts an NLST command with wildcard arguments" do
+ files = ['test.jpg', 'test.txt', 'test2.jpg']
+ files.each do |file|
+ server.add_file(file, '1234567890')
+ end
+
+ client.puts "NLST *.jpg"
+
+ expect(client.gets).to eql("150 Listing status ok, about to open data connection\r\n")
+ data = data_client.read(1024)
+ data_client.close
+
+ expect(data).to eql("test.jpg\ntest2.jpg\n")
expect(client.gets).to eql("226 List information transferred\r\n")
end
@@ -459,7 +475,7 @@
data = @server_client.read(1024)
@server_client.close
- expect(data).to eql("some_file\nanother_file")
+ expect(data).to eql("some_file\nanother_file\n")
expect(client.gets).to eql("226 List information transferred\r\n")
end
end

0 comments on commit 1382f84

Please sign in to comment.