Skip to content

Net::FTP support pathnames #828

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

Closed
wants to merge 3 commits into from
Closed
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
30 changes: 15 additions & 15 deletions lib/net/ftp.rb
Original file line number Diff line number Diff line change
Expand Up @@ -618,7 +618,7 @@ def getbinaryfile(remotefile, localfile = File.basename(remotefile),
end
begin
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
yield(data) if block_given?
result.concat(data) if result
Expand All @@ -644,7 +644,7 @@ def gettextfile(remotefile, localfile = File.basename(remotefile)) # :yield: lin
result = ""
end
begin
retrlines("RETR " + remotefile) do |line, newline|
retrlines("RETR #{remotefile}") do |line, newline|
l = newline ? line + "\n" : line
f.print(l) if localfile
yield(line, newline) if block_given?
Expand Down Expand Up @@ -689,9 +689,9 @@ def putbinaryfile(localfile, remotefile = File.basename(localfile),
begin
f.binmode
if rest_offset
storbinary("APPE " + remotefile, f, blocksize, rest_offset, &block)
storbinary("APPE #{remotefile}", f, blocksize, rest_offset, &block)
else
storbinary("STOR " + remotefile, f, blocksize, rest_offset, &block)
storbinary("STOR #{remotefile}", f, blocksize, rest_offset, &block)
end
ensure
f.close
Expand All @@ -706,7 +706,7 @@ def putbinaryfile(localfile, remotefile = File.basename(localfile),
def puttextfile(localfile, remotefile = File.basename(localfile), &block) # :yield: line
f = open(localfile)
begin
storlines("STOR " + remotefile, f, &block)
storlines("STOR #{remotefile}", f, &block)
ensure
f.close
end
Expand Down Expand Up @@ -742,7 +742,7 @@ def acct(account)
def nlst(dir = nil)
cmd = "NLST"
if dir
cmd = cmd + " " + dir
cmd = "#{cmd} #{dir}"
end
files = []
retrlines(cmd) do |line|
Expand All @@ -758,7 +758,7 @@ def nlst(dir = nil)
def list(*args, &block) # :yield: line
cmd = "LIST"
args.each do |arg|
cmd = cmd + " " + arg.to_s
cmd = "#{cmd} #{arg}"
end
if block
retrlines(cmd, &block)
Expand All @@ -777,18 +777,18 @@ def list(*args, &block) # :yield: line
# Renames a file on the server.
#
def rename(fromname, toname)
resp = sendcmd("RNFR " + fromname)
resp = sendcmd("RNFR #{fromname}")
if resp[0] != ?3
raise FTPReplyError, resp
end
voidcmd("RNTO " + toname)
voidcmd("RNTO #{toname}")
end

#
# Deletes a file on the server.
#
def delete(filename)
resp = sendcmd("DELE " + filename)
resp = sendcmd("DELE #{filename}")
if resp[0, 3] == "250"
return
elsif resp[0] == ?5
Expand All @@ -812,7 +812,7 @@ def chdir(dirname)
end
end
end
cmd = "CWD " + dirname
cmd = "CWD #{dirname}"
voidcmd(cmd)
end

Expand All @@ -821,7 +821,7 @@ def chdir(dirname)
#
def size(filename)
with_binary(true) do
resp = sendcmd("SIZE " + filename)
resp = sendcmd("SIZE #{filename}")
if resp[0, 3] != "213"
raise FTPReplyError, resp
end
Expand All @@ -845,15 +845,15 @@ def mtime(filename, local = false)
# Creates a remote directory.
#
def mkdir(dirname)
resp = sendcmd("MKD " + dirname)
resp = sendcmd("MKD #{dirname}")
return parse257(resp)
end

#
# Removes a remote directory.
#
def rmdir(dirname)
voidcmd("RMD " + dirname)
voidcmd("RMD #{dirname}")
end

#
Expand Down Expand Up @@ -907,7 +907,7 @@ def status
# Use +mtime+ if you want a parsed Time instance.
#
def mdtm(filename)
resp = sendcmd("MDTM " + filename)
resp = sendcmd("MDTM #{filename}")
if resp[0, 3] == "213"
return resp[3 .. -1].strip
end
Expand Down
78 changes: 78 additions & 0 deletions test/net/ftp/test_ftp.rb
Original file line number Diff line number Diff line change
Expand Up @@ -767,6 +767,84 @@ def test_status
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


Expand Down