Skip to content

Commit cd29e5f

Browse files
committed
* lib/net/ftp.rb (chdir, delete, gettextfile, mdtm, mkdir, nlst,
putbinaryfile, puttextfile, rename, rmdir, size): support Pathname. Patch by Joe Rafaniello. [fix rubyGH-828] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@49552 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
1 parent ab8dbd1 commit cd29e5f

File tree

3 files changed

+99
-15
lines changed

3 files changed

+99
-15
lines changed

ChangeLog

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
Tue Feb 10 11:19:11 2015 Shugo Maeda <shugo@ruby-lang.org>
2+
3+
* lib/net/ftp.rb (chdir, delete, gettextfile, mdtm, mkdir, nlst,
4+
putbinaryfile, puttextfile, rename, rmdir, size): support
5+
Pathname. Patch by Joe Rafaniello. [fix GH-828]
6+
17
Mon Feb 9 16:36:12 2015 Nobuyoshi Nakada <nobu@ruby-lang.org>
28

39
* tool/make-snapshot (package): get rid of loading unbundled and

lib/net/ftp.rb

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -618,7 +618,7 @@ def getbinaryfile(remotefile, localfile = File.basename(remotefile),
618618
end
619619
begin
620620
f.binmode if localfile
621-
retrbinary("RETR " + remotefile.to_s, blocksize, rest_offset) do |data|
621+
retrbinary("RETR #{remotefile}", blocksize, rest_offset) do |data|
622622
f.write(data) if localfile
623623
yield(data) if block_given?
624624
result.concat(data) if result
@@ -644,7 +644,7 @@ def gettextfile(remotefile, localfile = File.basename(remotefile)) # :yield: lin
644644
result = ""
645645
end
646646
begin
647-
retrlines("RETR " + remotefile) do |line, newline|
647+
retrlines("RETR #{remotefile}") do |line, newline|
648648
l = newline ? line + "\n" : line
649649
f.print(l) if localfile
650650
yield(line, newline) if block_given?
@@ -689,9 +689,9 @@ def putbinaryfile(localfile, remotefile = File.basename(localfile),
689689
begin
690690
f.binmode
691691
if rest_offset
692-
storbinary("APPE " + remotefile, f, blocksize, rest_offset, &block)
692+
storbinary("APPE #{remotefile}", f, blocksize, rest_offset, &block)
693693
else
694-
storbinary("STOR " + remotefile, f, blocksize, rest_offset, &block)
694+
storbinary("STOR #{remotefile}", f, blocksize, rest_offset, &block)
695695
end
696696
ensure
697697
f.close
@@ -706,7 +706,7 @@ def putbinaryfile(localfile, remotefile = File.basename(localfile),
706706
def puttextfile(localfile, remotefile = File.basename(localfile), &block) # :yield: line
707707
f = open(localfile)
708708
begin
709-
storlines("STOR " + remotefile, f, &block)
709+
storlines("STOR #{remotefile}", f, &block)
710710
ensure
711711
f.close
712712
end
@@ -742,7 +742,7 @@ def acct(account)
742742
def nlst(dir = nil)
743743
cmd = "NLST"
744744
if dir
745-
cmd = cmd + " " + dir
745+
cmd = "#{cmd} #{dir}"
746746
end
747747
files = []
748748
retrlines(cmd) do |line|
@@ -758,7 +758,7 @@ def nlst(dir = nil)
758758
def list(*args, &block) # :yield: line
759759
cmd = "LIST"
760760
args.each do |arg|
761-
cmd = cmd + " " + arg.to_s
761+
cmd = "#{cmd} #{arg}"
762762
end
763763
if block
764764
retrlines(cmd, &block)
@@ -777,18 +777,18 @@ def list(*args, &block) # :yield: line
777777
# Renames a file on the server.
778778
#
779779
def rename(fromname, toname)
780-
resp = sendcmd("RNFR " + fromname)
780+
resp = sendcmd("RNFR #{fromname}")
781781
if resp[0] != ?3
782782
raise FTPReplyError, resp
783783
end
784-
voidcmd("RNTO " + toname)
784+
voidcmd("RNTO #{toname}")
785785
end
786786

787787
#
788788
# Deletes a file on the server.
789789
#
790790
def delete(filename)
791-
resp = sendcmd("DELE " + filename)
791+
resp = sendcmd("DELE #{filename}")
792792
if resp[0, 3] == "250"
793793
return
794794
elsif resp[0] == ?5
@@ -812,7 +812,7 @@ def chdir(dirname)
812812
end
813813
end
814814
end
815-
cmd = "CWD " + dirname
815+
cmd = "CWD #{dirname}"
816816
voidcmd(cmd)
817817
end
818818

@@ -821,7 +821,7 @@ def chdir(dirname)
821821
#
822822
def size(filename)
823823
with_binary(true) do
824-
resp = sendcmd("SIZE " + filename)
824+
resp = sendcmd("SIZE #{filename}")
825825
if resp[0, 3] != "213"
826826
raise FTPReplyError, resp
827827
end
@@ -845,15 +845,15 @@ def mtime(filename, local = false)
845845
# Creates a remote directory.
846846
#
847847
def mkdir(dirname)
848-
resp = sendcmd("MKD " + dirname)
848+
resp = sendcmd("MKD #{dirname}")
849849
return parse257(resp)
850850
end
851851

852852
#
853853
# Removes a remote directory.
854854
#
855855
def rmdir(dirname)
856-
voidcmd("RMD " + dirname)
856+
voidcmd("RMD #{dirname}")
857857
end
858858

859859
#
@@ -907,7 +907,7 @@ def status
907907
# Use +mtime+ if you want a parsed Time instance.
908908
#
909909
def mdtm(filename)
910-
resp = sendcmd("MDTM " + filename)
910+
resp = sendcmd("MDTM #{filename}")
911911
if resp[0, 3] == "213"
912912
return resp[3 .. -1].strip
913913
end

test/net/ftp/test_ftp.rb

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -767,6 +767,84 @@ def test_status
767767
end
768768
end
769769

770+
def test_pathnames
771+
require 'pathname'
772+
773+
commands = []
774+
server = create_ftp_server(0.2) { |sock|
775+
sock.print("220 (test_ftp).\r\n")
776+
commands.push(sock.gets)
777+
sock.print("331 Please specify the password.\r\n")
778+
commands.push(sock.gets)
779+
sock.print("230 Login successful.\r\n")
780+
commands.push(sock.gets)
781+
sock.print("200 Switching to Binary mode.\r\n")
782+
commands.push(sock.gets)
783+
sock.print("257 'foo' directory created.\r\n")
784+
commands.push(sock.gets)
785+
sock.print("250 CWD command successful.\r\n")
786+
commands.push(sock.gets)
787+
sock.print("250 CWD command successful.\r\n")
788+
commands.push(sock.gets)
789+
sock.print("250 RMD command successful.\r\n")
790+
commands.push(sock.gets)
791+
sock.print("213 test.txt Fri, 11 Jan 2013 11:20:41 -0500.\r\n")
792+
commands.push(sock.gets)
793+
sock.print("213 test.txt 16.\r\n")
794+
commands.push(sock.gets)
795+
sock.print("350 File exists, ready for destination name\r\n")
796+
commands.push(sock.gets)
797+
sock.print("250 RNTO command successful.\r\n")
798+
commands.push(sock.gets)
799+
sock.print("250 DELE command successful.\r\n")
800+
}
801+
802+
begin
803+
begin
804+
dir = Pathname.new("foo")
805+
file = Pathname.new("test.txt")
806+
file2 = Pathname.new("test2.txt")
807+
ftp = Net::FTP.new
808+
ftp.connect(SERVER_ADDR, server.port)
809+
ftp.login
810+
ftp.mkdir(dir)
811+
ftp.chdir(dir)
812+
ftp.chdir("..")
813+
ftp.rmdir(dir)
814+
ftp.mdtm(file)
815+
ftp.size(file)
816+
ftp.rename(file, file2)
817+
ftp.delete(file)
818+
819+
# TODO: These commented tests below expose the error but don't test anything:
820+
# TypeError: no implicit conversion of Pathname into String
821+
# ftp.nlst(dir)
822+
# ftp.putbinaryfile(Pathname.new("/etc/hosts"), file2)
823+
# ftp.puttextfile(Pathname.new("/etc/hosts"), file2)
824+
# ftp.gettextfile(Pathname.new("/etc/hosts"), file2)
825+
# ftp.getbinaryfile(Pathname.new("/etc/hosts"), file2)
826+
# ftp.list(dir, dir, dir)
827+
828+
assert_match(/\AUSER /, commands.shift)
829+
assert_match(/\APASS /, commands.shift)
830+
assert_match(/\ATYPE /, commands.shift)
831+
assert_match(/\AMKD /, commands.shift)
832+
assert_match(/\ACWD /, commands.shift)
833+
assert_match(/\ACDUP/, commands.shift)
834+
assert_match(/\ARMD /, commands.shift)
835+
assert_match(/\AMDTM /, commands.shift)
836+
assert_match(/\ASIZE /, commands.shift)
837+
assert_match(/\ARNFR /, commands.shift)
838+
assert_match(/\ARNTO /, commands.shift)
839+
assert_match(/\ADELE /, commands.shift)
840+
ensure
841+
ftp.close if ftp
842+
end
843+
ensure
844+
server.close
845+
end
846+
end
847+
770848
private
771849

772850

0 commit comments

Comments
 (0)