ruby-changes:39710
From: shugo <ko1@a...>
Date: Tue, 8 Sep 2015 15:08:25 +0900 (JST)
Subject: [ruby-changes:39710] shugo:r51791 (trunk): * lib/net/ftp.rb (list): fetch all the lines before yielding a block
shugo 2015-09-08 15:08:20 +0900 (Tue, 08 Sep 2015) New Revision: 51791 http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=51791 Log: * lib/net/ftp.rb (list): fetch all the lines before yielding a block to allow other commands in the block. [Feature #11454] Patched by Srikanth Shreenivas. Modified files: trunk/ChangeLog trunk/lib/net/ftp.rb trunk/test/net/ftp/test_ftp.rb Index: ChangeLog =================================================================== --- ChangeLog (revision 51790) +++ ChangeLog (revision 51791) @@ -1,3 +1,9 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1 +Tue Sep 8 15:01:19 2015 Shugo Maeda <shugo@r...> + + * lib/net/ftp.rb (list): fetch all the lines before yielding a block + to allow other commands in the block. [Feature #11454] + Patched by Srikanth Shreenivas. + Tue Sep 8 12:05:00 2015 NAKAMURA Usaku <usa@r...> * win32/win32.c (rb_w32_read_reparse_point): return correct required Index: lib/net/ftp.rb =================================================================== --- lib/net/ftp.rb (revision 51790) +++ lib/net/ftp.rb (revision 51791) @@ -755,15 +755,14 @@ module Net https://github.com/ruby/ruby/blob/trunk/lib/net/ftp.rb#L755 args.each do |arg| cmd = "#{cmd} #{arg}" end + lines = [] + retrlines(cmd) do |line| + lines << line + end if block - retrlines(cmd, &block) - else - lines = [] - retrlines(cmd) do |line| - lines << line - end - return lines + lines.each(&block) end + return lines end alias ls list alias dir list Index: test/net/ftp/test_ftp.rb =================================================================== --- test/net/ftp/test_ftp.rb (revision 51790) +++ test/net/ftp/test_ftp.rb (revision 51791) @@ -741,6 +741,146 @@ class FTPTest < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/net/ftp/test_ftp.rb#L741 end end + def test_retrlines + commands = [] + text_data = <<EOF.gsub(/\n/, "\r\n") +foo +bar +baz +EOF + server = create_ftp_server { |sock| + sock.print("220 (test_ftp).\r\n") + commands.push(sock.gets) + sock.print("331 Please specify the password.\r\n") + commands.push(sock.gets) + sock.print("230 Login successful.\r\n") + commands.push(sock.gets) + sock.print("200 Switching to Binary mode.\r\n") + commands.push(sock.gets) + sock.print("200 Switching to ASCII mode.\r\n") + line = sock.gets + commands.push(line) + port_args = line.slice(/\APORT (.*)/, 1).split(/,/) + host = port_args[0, 4].join(".") + port = port_args[4, 2].map(&:to_i).inject {|x, y| (x << 8) + y} + sock.print("200 PORT command successful.\r\n") + commands.push(sock.gets) + sock.print("150 Opening TEXT mode data connection for foo (#{text_data.size} bytes)\r\n") + conn = TCPSocket.new(host, port) + text_data.each_line do |line| + conn.print(line) + end + conn.shutdown(Socket::SHUT_WR) + conn.read + conn.close + sock.print("226 Transfer complete.\r\n") + commands.push(sock.gets) + sock.print("200 Switching to Binary mode.\r\n") + } + begin + begin + ftp = Net::FTP.new + ftp.connect(SERVER_ADDR, server.port) + ftp.login + assert_match(/\AUSER /, commands.shift) + assert_match(/\APASS /, commands.shift) + assert_equal("TYPE I\r\n", commands.shift) + buf = "" + ftp.retrlines("RETR foo") do |line| + buf << line + "\r\n" + end + assert_equal(text_data.bytesize, buf.bytesize) + assert_equal(text_data, buf) + assert_equal("TYPE A\r\n", commands.shift) + assert_match(/\APORT /, commands.shift) + assert_equal("RETR foo\r\n", commands.shift) + assert_equal("TYPE I\r\n", commands.shift) + assert_equal(nil, commands.shift) + ensure + ftp.close if ftp + end + ensure + server.close + end + end + + def test_getbinaryfile_in_list + commands = [] + binary_data = (0..0xff).map {|i| i.chr}.join + list_lines = [ + "-rw-r--r-- 1 0 0 0 Mar 30 11:22 foo.txt", + "-rw-r--r-- 1 0 0 0 Mar 30 11:22 bar.txt", + "-rw-r--r-- 1 0 0 0 Mar 30 11:22 baz.bin" + ] + server = create_ftp_server { |sock| + sock.print("220 (test_ftp).\r\n") + commands.push(sock.gets) + sock.print("331 Please specify the password.\r\n") + commands.push(sock.gets) + sock.print("230 Login successful.\r\n") + commands.push(sock.gets) + sock.print("200 Switching to Binary mode.\r\n") + commands.push(sock.gets) + sock.print("200 Switching to ASCII mode.\r\n") + line = sock.gets + commands.push(line) + port_args = line.slice(/\APORT (.*)/, 1).split(/,/) + host = port_args[0, 4].join(".") + port = port_args[4, 2].map(&:to_i).inject {|x, y| (x << 8) + y} + sock.print("200 PORT command successful.\r\n") + commands.push(sock.gets) + sock.print("150 Here comes the directory listing.\r\n") + conn = TCPSocket.new(host, port) + list_lines.each_with_index do |l, i| + conn.print(l, "\r\n") + end + conn.close + sock.print("226 Directory send OK.\r\n") + commands.push(sock.gets) + sock.print("200 Switching to Binary mode.\r\n") + line = sock.gets + commands.push(line) + port_args = line.slice(/\APORT (.*)/, 1).split(/,/) + host = port_args[0, 4].join(".") + port = port_args[4, 2].map(&:to_i).inject {|x, y| (x << 8) + y} + sock.print("200 PORT command successful.\r\n") + commands.push(sock.gets) + sock.print("150 Opening BINARY mode data connection for foo (#{binary_data.size} bytes)\r\n") + conn = TCPSocket.new(host, port) + conn.print(binary_data) + conn.close + sock.print("226 Transfer complete.\r\n") + } + begin + begin + ftp = Net::FTP.new + ftp.connect(SERVER_ADDR, server.port) + ftp.login + assert_match(/\AUSER /, commands.shift) + assert_match(/\APASS /, commands.shift) + assert_equal("TYPE I\r\n", commands.shift) + ftp.list do |line| + file = line.slice(/(\S*\.bin)\z/) + if file + data = ftp.getbinaryfile(file, nil) + assert_equal(binary_data, data) + end + end + assert_equal("TYPE A\r\n", commands.shift) + assert_match(/\APORT /, commands.shift) + assert_equal("LIST\r\n", commands.shift) + assert_equal("TYPE I\r\n", commands.shift) + assert_match(/\APORT /, commands.shift) + assert_equal("RETR baz.bin\r\n", commands.shift) + assert_equal(nil, commands.shift) + ensure + ftp.close if ftp + end + ensure + server.close + end + end + def test_abort commands = [] server = create_ftp_server { |sock| -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/