1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

* lib/net/ftp.rb (chdir, delete, gettextfile, mdtm, mkdir, nlst,

putbinaryfile, puttextfile, rename, rmdir, size): support
  Pathname. Patch by Joe Rafaniello. [fix GH-828]

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@49552 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
shugo 2015-02-10 02:26:06 +00:00
parent ab8dbd1082
commit cd29e5fcef
3 changed files with 99 additions and 15 deletions

View file

@ -1,3 +1,9 @@
Tue Feb 10 11:19:11 2015 Shugo Maeda <shugo@ruby-lang.org>
* lib/net/ftp.rb (chdir, delete, gettextfile, mdtm, mkdir, nlst,
putbinaryfile, puttextfile, rename, rmdir, size): support
Pathname. Patch by Joe Rafaniello. [fix GH-828]
Mon Feb 9 16:36:12 2015 Nobuyoshi Nakada <nobu@ruby-lang.org> Mon Feb 9 16:36:12 2015 Nobuyoshi Nakada <nobu@ruby-lang.org>
* tool/make-snapshot (package): get rid of loading unbundled and * tool/make-snapshot (package): get rid of loading unbundled and

View file

@ -618,7 +618,7 @@ module Net
end end
begin begin
f.binmode if localfile f.binmode if localfile
retrbinary("RETR " + remotefile.to_s, blocksize, rest_offset) do |data| retrbinary("RETR #{remotefile}", blocksize, rest_offset) do |data|
f.write(data) if localfile f.write(data) if localfile
yield(data) if block_given? yield(data) if block_given?
result.concat(data) if result result.concat(data) if result
@ -644,7 +644,7 @@ module Net
result = "" result = ""
end end
begin begin
retrlines("RETR " + remotefile) do |line, newline| retrlines("RETR #{remotefile}") do |line, newline|
l = newline ? line + "\n" : line l = newline ? line + "\n" : line
f.print(l) if localfile f.print(l) if localfile
yield(line, newline) if block_given? yield(line, newline) if block_given?
@ -689,9 +689,9 @@ module Net
begin begin
f.binmode f.binmode
if rest_offset if rest_offset
storbinary("APPE " + remotefile, f, blocksize, rest_offset, &block) storbinary("APPE #{remotefile}", f, blocksize, rest_offset, &block)
else else
storbinary("STOR " + remotefile, f, blocksize, rest_offset, &block) storbinary("STOR #{remotefile}", f, blocksize, rest_offset, &block)
end end
ensure ensure
f.close f.close
@ -706,7 +706,7 @@ module Net
def puttextfile(localfile, remotefile = File.basename(localfile), &block) # :yield: line def puttextfile(localfile, remotefile = File.basename(localfile), &block) # :yield: line
f = open(localfile) f = open(localfile)
begin begin
storlines("STOR " + remotefile, f, &block) storlines("STOR #{remotefile}", f, &block)
ensure ensure
f.close f.close
end end
@ -742,7 +742,7 @@ module Net
def nlst(dir = nil) def nlst(dir = nil)
cmd = "NLST" cmd = "NLST"
if dir if dir
cmd = cmd + " " + dir cmd = "#{cmd} #{dir}"
end end
files = [] files = []
retrlines(cmd) do |line| retrlines(cmd) do |line|
@ -758,7 +758,7 @@ module Net
def list(*args, &block) # :yield: line def list(*args, &block) # :yield: line
cmd = "LIST" cmd = "LIST"
args.each do |arg| args.each do |arg|
cmd = cmd + " " + arg.to_s cmd = "#{cmd} #{arg}"
end end
if block if block
retrlines(cmd, &block) retrlines(cmd, &block)
@ -777,18 +777,18 @@ module Net
# Renames a file on the server. # Renames a file on the server.
# #
def rename(fromname, toname) def rename(fromname, toname)
resp = sendcmd("RNFR " + fromname) resp = sendcmd("RNFR #{fromname}")
if resp[0] != ?3 if resp[0] != ?3
raise FTPReplyError, resp raise FTPReplyError, resp
end end
voidcmd("RNTO " + toname) voidcmd("RNTO #{toname}")
end end
# #
# Deletes a file on the server. # Deletes a file on the server.
# #
def delete(filename) def delete(filename)
resp = sendcmd("DELE " + filename) resp = sendcmd("DELE #{filename}")
if resp[0, 3] == "250" if resp[0, 3] == "250"
return return
elsif resp[0] == ?5 elsif resp[0] == ?5
@ -812,7 +812,7 @@ module Net
end end
end end
end end
cmd = "CWD " + dirname cmd = "CWD #{dirname}"
voidcmd(cmd) voidcmd(cmd)
end end
@ -821,7 +821,7 @@ module Net
# #
def size(filename) def size(filename)
with_binary(true) do with_binary(true) do
resp = sendcmd("SIZE " + filename) resp = sendcmd("SIZE #{filename}")
if resp[0, 3] != "213" if resp[0, 3] != "213"
raise FTPReplyError, resp raise FTPReplyError, resp
end end
@ -845,7 +845,7 @@ module Net
# Creates a remote directory. # Creates a remote directory.
# #
def mkdir(dirname) def mkdir(dirname)
resp = sendcmd("MKD " + dirname) resp = sendcmd("MKD #{dirname}")
return parse257(resp) return parse257(resp)
end end
@ -853,7 +853,7 @@ module Net
# Removes a remote directory. # Removes a remote directory.
# #
def rmdir(dirname) def rmdir(dirname)
voidcmd("RMD " + dirname) voidcmd("RMD #{dirname}")
end end
# #
@ -907,7 +907,7 @@ module Net
# Use +mtime+ if you want a parsed Time instance. # Use +mtime+ if you want a parsed Time instance.
# #
def mdtm(filename) def mdtm(filename)
resp = sendcmd("MDTM " + filename) resp = sendcmd("MDTM #{filename}")
if resp[0, 3] == "213" if resp[0, 3] == "213"
return resp[3 .. -1].strip return resp[3 .. -1].strip
end end

View file

@ -767,6 +767,84 @@ class FTPTest < Test::Unit::TestCase
end end
end end
def test_pathnames
require 'pathname'
commands = []
server = create_ftp_server(0.2) { |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("257 'foo' directory created.\r\n")
commands.push(sock.gets)
sock.print("250 CWD command successful.\r\n")
commands.push(sock.gets)
sock.print("250 CWD command successful.\r\n")
commands.push(sock.gets)
sock.print("250 RMD command successful.\r\n")
commands.push(sock.gets)
sock.print("213 test.txt Fri, 11 Jan 2013 11:20:41 -0500.\r\n")
commands.push(sock.gets)
sock.print("213 test.txt 16.\r\n")
commands.push(sock.gets)
sock.print("350 File exists, ready for destination name\r\n")
commands.push(sock.gets)
sock.print("250 RNTO command successful.\r\n")
commands.push(sock.gets)
sock.print("250 DELE command successful.\r\n")
}
begin
begin
dir = Pathname.new("foo")
file = Pathname.new("test.txt")
file2 = Pathname.new("test2.txt")
ftp = Net::FTP.new
ftp.connect(SERVER_ADDR, server.port)
ftp.login
ftp.mkdir(dir)
ftp.chdir(dir)
ftp.chdir("..")
ftp.rmdir(dir)
ftp.mdtm(file)
ftp.size(file)
ftp.rename(file, file2)
ftp.delete(file)
# TODO: These commented tests below expose the error but don't test anything:
# TypeError: no implicit conversion of Pathname into String
# ftp.nlst(dir)
# ftp.putbinaryfile(Pathname.new("/etc/hosts"), file2)
# ftp.puttextfile(Pathname.new("/etc/hosts"), file2)
# ftp.gettextfile(Pathname.new("/etc/hosts"), file2)
# ftp.getbinaryfile(Pathname.new("/etc/hosts"), file2)
# ftp.list(dir, dir, dir)
assert_match(/\AUSER /, commands.shift)
assert_match(/\APASS /, commands.shift)
assert_match(/\ATYPE /, commands.shift)
assert_match(/\AMKD /, commands.shift)
assert_match(/\ACWD /, commands.shift)
assert_match(/\ACDUP/, commands.shift)
assert_match(/\ARMD /, commands.shift)
assert_match(/\AMDTM /, commands.shift)
assert_match(/\ASIZE /, commands.shift)
assert_match(/\ARNFR /, commands.shift)
assert_match(/\ARNTO /, commands.shift)
assert_match(/\ADELE /, commands.shift)
ensure
ftp.close if ftp
end
ensure
server.close
end
end
private private