Class: Net::FTP
Overview
This class implements the File Transfer Protocol. If you have used a command-line FTP program, and are familiar with the commands, you will be able to use this class easily. Some extra features are included to take advantage of Ruby’s style and strengths.
Example
require 'net/ftp'
Example 1
ftp = Net::FTP.new('example.com')
ftp.login
files = ftp.chdir('pub/lang/ruby/contrib')
files = ftp.list('n*')
ftp.getbinaryfile('nif.rb-0.91.gz', 'nif.gz', 1024)
ftp.close
Example 2
Net::FTP.open('example.com') do |ftp|
ftp.login
files = ftp.chdir('pub/lang/ruby/contrib')
files = ftp.list('n*')
ftp.getbinaryfile('nif.rb-0.91.gz', 'nif.gz', 1024)
end
Major Methods
The following are the methods most likely to be useful to users:
-
FTP.open
-
#getbinaryfile
-
#gettextfile
-
#putbinaryfile
-
#puttextfile
-
#chdir
-
#nlst
-
#size
-
#rename
-
#delete
Defined Under Namespace
Classes: BufferedSocket, MLSxEntry, NullSocket
Constant Summary collapse
- FTP_PORT =
:stopdoc:
21
- CRLF =
"\r\n"
- DEFAULT_BLOCKSIZE =
BufferedIO::BUFSIZE
- CASE_DEPENDENT_PARSER =
->(value) { value }
- CASE_INDEPENDENT_PARSER =
->(value) { value.downcase }
- DECIMAL_PARSER =
->(value) { value.to_i }
- OCTAL_PARSER =
->(value) { value.to_i(8) }
- TIME_PARSER =
->(value, local = false) { unless /\A(?<year>\d{4})(?<month>\d{2})(?<day>\d{2}) (?<hour>\d{2})(?<min>\d{2})(?<sec>\d{2}) (\.(?<fractions>\d+))?/x =~ value raise FTPProtoError, "invalid time-val: #{value}" end usec = fractions.to_i * 10 ** (6 - fractions.to_s.size) Time.send(local ? :local : :utc, year, month, day, hour, min, sec, usec) }
- FACT_PARSERS =
Hash.new(CASE_DEPENDENT_PARSER)
- @@default_passive =
true
Instance Attribute Summary collapse
-
#binary ⇒ Object
When
true
, transfers are performed in binary mode. -
#debug_mode ⇒ Object
When
true
, all traffic to and from the server is written to $stdout. -
#last_response ⇒ Object
readonly
The server’s last response.
-
#last_response_code ⇒ Object
(also: #lastresp)
readonly
The server’s last response code.
-
#open_timeout ⇒ Object
Number of seconds to wait for the connection to open.
-
#passive ⇒ Object
When
true
, the connection is in passive mode. -
#read_timeout ⇒ Object
Number of seconds to wait for one block to be read (via one read(2) call).
-
#resume ⇒ Object
Sets or retrieves the
resume
status, which decides whether incomplete transfers are resumed or restarted. -
#welcome ⇒ Object
readonly
The server’s welcome message.
Class Method Summary collapse
-
.default_passive ⇒ Object
When
true
, connections are in passive mode per default. -
.default_passive=(value) ⇒ Object
When
true
, connections are in passive mode per default. -
.open(host, user = nil, passwd = nil, acct = nil) ⇒ Object
A synonym for
FTP.new
, but with a mandatory host parameter.
Instance Method Summary collapse
-
#abort ⇒ Object
Aborts the previous command (ABOR command).
-
#acct(account) ⇒ Object
Sends the ACCT command.
-
#chdir(dirname) ⇒ Object
Changes the (remote) directory.
-
#close ⇒ Object
Closes the connection.
-
#closed? ⇒ Boolean
Returns
true
iff the connection is closed. -
#connect(host, port = FTP_PORT) ⇒ Object
Establishes an FTP connection to host, optionally overriding the default port.
-
#delete(filename) ⇒ Object
Deletes a file on the server.
-
#get(remotefile, localfile = File.basename(remotefile), blocksize = DEFAULT_BLOCKSIZE, &block) ⇒ Object
Retrieves
remotefile
in whatever mode the session is set (text or binary). -
#getbinaryfile(remotefile, localfile = File.basename(remotefile), blocksize = DEFAULT_BLOCKSIZE, &block) ⇒ Object
Retrieves
remotefile
in binary mode, storing the result inlocalfile
. -
#gettextfile(remotefile, localfile = File.basename(remotefile), &block) ⇒ Object
Retrieves
remotefile
in ASCII (text) mode, storing the result inlocalfile
. -
#help(arg = nil) ⇒ Object
Issues the HELP command.
-
#initialize(host = nil, user = nil, passwd = nil, acct = nil) ⇒ FTP
constructor
Creates and returns a new
FTP
object. -
#list(*args, &block) ⇒ Object
(also: #ls, #dir)
Returns an array of file information in the directory (the output is like ‘ls -l`).
-
#login(user = "anonymous", passwd = nil, acct = nil) ⇒ Object
Logs in to the remote host.
-
#mdtm(filename) ⇒ Object
Returns the raw last modification time of the (remote) file in the format “YYYYMMDDhhmmss” (MDTM command).
-
#mkdir(dirname) ⇒ Object
Creates a remote directory.
-
#mlsd(pathname = nil, &block) ⇒ Object
Returns an array of the entries of the directory specified by
pathname
. -
#mlst(pathname = nil) ⇒ Object
Returns data (e.g., size, last modification time, entry type, etc.) about the file or directory specified by
pathname
. -
#mtime(filename, local = false) ⇒ Object
Returns the last modification time of the (remote) file.
-
#nlst(dir = nil) ⇒ Object
Returns an array of filenames in the remote directory.
-
#noop ⇒ Object
Issues a NOOP command.
-
#put(localfile, remotefile = File.basename(localfile), blocksize = DEFAULT_BLOCKSIZE, &block) ⇒ Object
Transfers
localfile
to the server in whatever mode the session is set (text or binary). -
#putbinaryfile(localfile, remotefile = File.basename(localfile), blocksize = DEFAULT_BLOCKSIZE, &block) ⇒ Object
Transfers
localfile
to the server in binary mode, storing the result inremotefile
. -
#puttextfile(localfile, remotefile = File.basename(localfile), &block) ⇒ Object
Transfers
localfile
to the server in ASCII (text) mode, storing the result inremotefile
. -
#pwd ⇒ Object
(also: #getdir)
Returns the current remote directory.
-
#quit ⇒ Object
Exits the FTP session.
-
#rename(fromname, toname) ⇒ Object
Renames a file on the server.
-
#retrbinary(cmd, blocksize, rest_offset = nil) ⇒ Object
Puts the connection into binary (image) mode, issues the given command, and fetches the data returned, passing it to the associated block in chunks of
blocksize
characters. -
#retrlines(cmd) ⇒ Object
Puts the connection into ASCII (text) mode, issues the given command, and passes the resulting data, one line at a time, to the associated block.
-
#return_code ⇒ Object
Obsolete.
-
#return_code=(s) ⇒ Object
Obsolete.
-
#rmdir(dirname) ⇒ Object
Removes a remote directory.
-
#sendcmd(cmd) ⇒ Object
Sends a command and returns the response.
-
#set_socket(sock, get_greeting = true) ⇒ Object
Set the socket used to connect to the FTP server.
-
#site(arg) ⇒ Object
Issues a SITE command.
-
#size(filename) ⇒ Object
Returns the size of the given (remote) filename.
-
#status ⇒ Object
Returns the status (STAT command).
-
#storbinary(cmd, file, blocksize, rest_offset = nil) ⇒ Object
Puts the connection into binary (image) mode, issues the given server-side command (such as “STOR myfile”), and sends the contents of the file named
file
to the server. -
#storlines(cmd, file) ⇒ Object
Puts the connection into ASCII (text) mode, issues the given server-side command (such as “STOR myfile”), and sends the contents of the file named
file
to the server, one line at a time. -
#system ⇒ Object
Returns system information.
-
#voidcmd(cmd) ⇒ Object
Sends a command and expect a response beginning with ‘2’.
Constructor Details
#initialize(host = nil, user = nil, passwd = nil, acct = nil) ⇒ FTP
Creates and returns a new FTP
object. If a host
is given, a connection is made. Additionally, if the user
is given, the given user name, password, and (optionally) account are used to log in. See #login.
164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 |
# File 'lib/net/ftp.rb', line 164 def initialize(host = nil, user = nil, passwd = nil, acct = nil) super() @binary = true @passive = @@default_passive @debug_mode = false @resume = false @sock = NullSocket.new @logged_in = false @open_timeout = nil @read_timeout = 60 if host connect(host) if user login(user, passwd, acct) end end end |
Instance Attribute Details
#binary ⇒ Object
When true
, transfers are performed in binary mode. Default: true
.
87 88 89 |
# File 'lib/net/ftp.rb', line 87 def binary @binary end |
#debug_mode ⇒ Object
When true
, all traffic to and from the server is written to $stdout. Default: false
.
94 95 96 |
# File 'lib/net/ftp.rb', line 94 def debug_mode @debug_mode end |
#last_response ⇒ Object (readonly)
The server’s last response.
126 127 128 |
# File 'lib/net/ftp.rb', line 126 def last_response @last_response end |
#last_response_code ⇒ Object (readonly) Also known as: lastresp
The server’s last response code.
122 123 124 |
# File 'lib/net/ftp.rb', line 122 def last_response_code @last_response_code end |
#open_timeout ⇒ Object
Number of seconds to wait for the connection to open. Any number may be used, including Floats for fractional seconds. If the FTP object cannot open a connection in this many seconds, it raises a Net::OpenTimeout exception. The default value is nil
.
104 105 106 |
# File 'lib/net/ftp.rb', line 104 def open_timeout @open_timeout end |
#passive ⇒ Object
When true
, the connection is in passive mode. Default: true
.
90 91 92 |
# File 'lib/net/ftp.rb', line 90 def passive @passive end |
#read_timeout ⇒ Object
Number of seconds to wait for one block to be read (via one read(2) call). Any number may be used, including Floats for fractional seconds. If the FTP object cannot read data in this many seconds, it raises a Timeout::Error exception. The default value is 60 seconds.
110 111 112 |
# File 'lib/net/ftp.rb', line 110 def read_timeout @read_timeout end |
#resume ⇒ Object
Sets or retrieves the resume
status, which decides whether incomplete transfers are resumed or restarted. Default: false
.
98 99 100 |
# File 'lib/net/ftp.rb', line 98 def resume @resume end |
#welcome ⇒ Object (readonly)
The server’s welcome message.
119 120 121 |
# File 'lib/net/ftp.rb', line 119 def welcome @welcome end |
Class Method Details
.default_passive ⇒ Object
When true
, connections are in passive mode per default. Default: true
.
136 137 138 |
# File 'lib/net/ftp.rb', line 136 def self.default_passive @@default_passive end |
.default_passive=(value) ⇒ Object
When true
, connections are in passive mode per default. Default: true
.
130 131 132 |
# File 'lib/net/ftp.rb', line 130 def self.default_passive=(value) @@default_passive = value end |
.open(host, user = nil, passwd = nil, acct = nil) ⇒ Object
A synonym for FTP.new
, but with a mandatory host parameter.
If a block is given, it is passed the FTP
object, which will be closed when the block finishes, or when an exception is raised.
146 147 148 149 150 151 152 153 154 155 156 157 |
# File 'lib/net/ftp.rb', line 146 def FTP.open(host, user = nil, passwd = nil, acct = nil) if block_given? ftp = new(host, user, passwd, acct) begin yield ftp ensure ftp.close end else new(host, user, passwd, acct) end end |
Instance Method Details
#abort ⇒ Object
Aborts the previous command (ABOR command).
1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 |
# File 'lib/net/ftp.rb', line 1094 def abort line = "ABOR" + CRLF print "put: ABOR\n" if @debug_mode @sock.send(line, Socket::MSG_OOB) resp = getmultiline unless ["426", "226", "225"].include?(resp[0, 3]) raise FTPProtoError, resp end return resp end |
#acct(account) ⇒ Object
Sends the ACCT command.
This is a less common FTP command, to send account information if the destination host requires it.
747 748 749 750 |
# File 'lib/net/ftp.rb', line 747 def acct(account) cmd = "ACCT " + account voidcmd(cmd) end |
#chdir(dirname) ⇒ Object
Changes the (remote) directory.
1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 |
# File 'lib/net/ftp.rb', line 1015 def chdir(dirname) if dirname == ".." begin voidcmd("CDUP") return rescue FTPPermError => e if e.[0, 3] != "500" raise e end end end cmd = "CWD #{dirname}" voidcmd(cmd) end |
#close ⇒ Object
Closes the connection. Further operations are impossible until you open a new connection with #connect.
1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 |
# File 'lib/net/ftp.rb', line 1167 def close if @sock and not @sock.closed? begin @sock.shutdown(Socket::SHUT_WR) rescue nil orig, self.read_timeout = self.read_timeout, 3 @sock.read rescue nil ensure @sock.close self.read_timeout = orig end end end |
#closed? ⇒ Boolean
Returns true
iff the connection is closed.
1183 1184 1185 |
# File 'lib/net/ftp.rb', line 1183 def closed? @sock == nil or @sock.closed? end |
#connect(host, port = FTP_PORT) ⇒ Object
Establishes an FTP connection to host, optionally overriding the default port. If the environment variable SOCKS_SERVER
is set, sets up the connection through a SOCKS proxy. Raises an exception (typically Errno::ECONNREFUSED
) if the connection cannot be established.
258 259 260 261 262 263 264 265 266 |
# File 'lib/net/ftp.rb', line 258 def connect(host, port = FTP_PORT) if @debug_mode print "connect: ", host, ", ", port, "\n" end synchronize do @sock = open_socket(host, port) voidresp end end |
#delete(filename) ⇒ Object
Deletes a file on the server.
1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 |
# File 'lib/net/ftp.rb', line 1001 def delete(filename) resp = sendcmd("DELE #{filename}") if resp.start_with?("250") return elsif resp.start_with?("5") raise FTPPermError, resp else raise FTPReplyError, resp end end |
#get(remotefile, localfile = File.basename(remotefile), blocksize = DEFAULT_BLOCKSIZE, &block) ⇒ Object
Retrieves remotefile
in whatever mode the session is set (text or binary). See #gettextfile and #getbinaryfile.
676 677 678 679 680 681 682 683 |
# File 'lib/net/ftp.rb', line 676 def get(remotefile, localfile = File.basename(remotefile), blocksize = DEFAULT_BLOCKSIZE, &block) # :yield: data if @binary getbinaryfile(remotefile, localfile, blocksize, &block) else gettextfile(remotefile, localfile, &block) end end |
#getbinaryfile(remotefile, localfile = File.basename(remotefile), blocksize = DEFAULT_BLOCKSIZE, &block) ⇒ Object
Retrieves remotefile
in binary mode, storing the result in localfile
. If localfile
is nil, returns retrieved data. If a block is supplied, it is passed the retrieved data in blocksize
chunks.
615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 |
# File 'lib/net/ftp.rb', line 615 def getbinaryfile(remotefile, localfile = File.basename(remotefile), blocksize = DEFAULT_BLOCKSIZE, &block) # :yield: data f = nil result = nil if localfile if @resume rest_offset = File.size?(localfile) f = open(localfile, "a") else rest_offset = nil f = open(localfile, "w") end elsif !block_given? result = String.new end begin f&.binmode retrbinary("RETR #{remotefile}", blocksize, rest_offset) do |data| f&.write(data) block&.(data) result&.concat(data) end return result ensure f&.close end end |
#gettextfile(remotefile, localfile = File.basename(remotefile), &block) ⇒ Object
Retrieves remotefile
in ASCII (text) mode, storing the result in localfile
. If localfile
is nil, returns retrieved data. If a block is supplied, it is passed the retrieved data one line at a time.
650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 |
# File 'lib/net/ftp.rb', line 650 def gettextfile(remotefile, localfile = File.basename(remotefile), &block) # :yield: line f = nil result = nil if localfile f = open(localfile, "w") elsif !block_given? result = String.new end begin retrlines("RETR #{remotefile}") do |line, newline| l = newline ? line + "\n" : line f&.print(l) block&.(line, newline) result&.concat(l) end return result ensure f&.close end end |
#help(arg = nil) ⇒ Object
Issues the HELP command.
1131 1132 1133 1134 1135 1136 1137 |
# File 'lib/net/ftp.rb', line 1131 def help(arg = nil) cmd = "HELP" if arg cmd = cmd + " " + arg end sendcmd(cmd) end |
#list(*args, &block) ⇒ Object Also known as: ls, dir
Returns an array of file information in the directory (the output is like ‘ls -l`). If a block is given, it iterates through the listing.
771 772 773 774 775 776 777 778 779 780 781 782 783 784 |
# File 'lib/net/ftp.rb', line 771 def list(*args, &block) # :yield: line cmd = "LIST" args.each do |arg| cmd = "#{cmd} #{arg}" end lines = [] retrlines(cmd) do |line| lines << line end if block lines.each(&block) end return lines end |
#login(user = "anonymous", passwd = nil, acct = nil) ⇒ Object
Logs in to the remote host. The session must have been previously connected. If user
is the string “anonymous” and the password
is nil
, “anonymous@” is used as a password. If the acct
parameter is not nil
, an FTP ACCT command is sent following the successful login. Raises an exception on error (typically Net::FTPPermError
).
466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 |
# File 'lib/net/ftp.rb', line 466 def login(user = "anonymous", passwd = nil, acct = nil) if user == "anonymous" and passwd == nil passwd = "anonymous@" end resp = "" synchronize do resp = sendcmd('USER ' + user) if resp.start_with?("3") raise FTPReplyError, resp if passwd.nil? resp = sendcmd('PASS ' + passwd) end if resp.start_with?("3") raise FTPReplyError, resp if acct.nil? resp = sendcmd('ACCT ' + acct) end end if !resp.start_with?("2") raise FTPReplyError, resp end @welcome = resp send_type_command @logged_in = true end |
#mdtm(filename) ⇒ Object
Returns the raw last modification time of the (remote) file in the format “YYYYMMDDhhmmss” (MDTM command).
Use mtime
if you want a parsed Time instance.
1121 1122 1123 1124 1125 1126 |
# File 'lib/net/ftp.rb', line 1121 def mdtm(filename) resp = sendcmd("MDTM #{filename}") if resp.start_with?("213") return get_body(resp) end end |
#mkdir(dirname) ⇒ Object
Creates a remote directory.
1059 1060 1061 1062 |
# File 'lib/net/ftp.rb', line 1059 def mkdir(dirname) resp = sendcmd("MKD #{dirname}") return parse257(resp) end |
#mlsd(pathname = nil, &block) ⇒ Object
Returns an array of the entries of the directory specified by pathname
. Each entry has the facts (e.g., size, last modification time, etc.) and the pathname. If a block is given, it iterates through the listing. If pathname
is omitted, the current directory is assumed.
975 976 977 978 979 980 981 982 983 984 985 |
# File 'lib/net/ftp.rb', line 975 def mlsd(pathname = nil, &block) # :yield: entry cmd = pathname ? "MLSD #{pathname}" : "MLSD" entries = [] retrlines(cmd) do |line| entries << parse_mlsx_entry(line) end if block entries.each(&block) end return entries end |
#mlst(pathname = nil) ⇒ Object
Returns data (e.g., size, last modification time, entry type, etc.) about the file or directory specified by pathname
. If pathname
is omitted, the current directory is assumed.
953 954 955 956 957 958 959 960 961 962 963 964 965 |
# File 'lib/net/ftp.rb', line 953 def mlst(pathname = nil) cmd = pathname ? "MLST #{pathname}" : "MLST" resp = sendcmd(cmd) if !resp.start_with?("250") raise FTPReplyError, resp end line = resp.lines[1] unless line raise FTPProtoError, resp end entry = line.sub(/\A(250-| *)/, "") return parse_mlsx_entry(entry) end |
#mtime(filename, local = false) ⇒ Object
Returns the last modification time of the (remote) file. If local
is true
, it is returned as a local time, otherwise it’s a UTC time.
1052 1053 1054 |
# File 'lib/net/ftp.rb', line 1052 def mtime(filename, local = false) return TIME_PARSER.(mdtm(filename), local) end |
#nlst(dir = nil) ⇒ Object
Returns an array of filenames in the remote directory.
755 756 757 758 759 760 761 762 763 764 765 |
# File 'lib/net/ftp.rb', line 755 def nlst(dir = nil) cmd = "NLST" if dir cmd = "#{cmd} #{dir}" end files = [] retrlines(cmd) do |line| files.push(line) end return files end |
#noop ⇒ Object
Issues a NOOP command.
Does nothing except return a response.
1151 1152 1153 |
# File 'lib/net/ftp.rb', line 1151 def noop voidcmd("NOOP") end |
#put(localfile, remotefile = File.basename(localfile), blocksize = DEFAULT_BLOCKSIZE, &block) ⇒ Object
Transfers localfile
to the server in whatever mode the session is set (text or binary). See #puttextfile and #putbinaryfile.
732 733 734 735 736 737 738 739 |
# File 'lib/net/ftp.rb', line 732 def put(localfile, remotefile = File.basename(localfile), blocksize = DEFAULT_BLOCKSIZE, &block) if @binary putbinaryfile(localfile, remotefile, blocksize, &block) else puttextfile(localfile, remotefile, &block) end end |
#putbinaryfile(localfile, remotefile = File.basename(localfile), blocksize = DEFAULT_BLOCKSIZE, &block) ⇒ Object
Transfers localfile
to the server in binary mode, storing the result in remotefile
. If a block is supplied, calls it, passing in the transmitted data in blocksize
chunks.
690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 |
# File 'lib/net/ftp.rb', line 690 def putbinaryfile(localfile, remotefile = File.basename(localfile), blocksize = DEFAULT_BLOCKSIZE, &block) # :yield: data if @resume begin rest_offset = size(remotefile) rescue Net::FTPPermError rest_offset = nil end else rest_offset = nil end f = open(localfile) begin f.binmode if rest_offset storbinary("APPE #{remotefile}", f, blocksize, rest_offset, &block) else storbinary("STOR #{remotefile}", f, blocksize, rest_offset, &block) end ensure f.close end end |
#puttextfile(localfile, remotefile = File.basename(localfile), &block) ⇒ Object
Transfers localfile
to the server in ASCII (text) mode, storing the result in remotefile
. If callback or an associated block is supplied, calls it, passing in the transmitted data one line at a time.
719 720 721 722 723 724 725 726 |
# File 'lib/net/ftp.rb', line 719 def puttextfile(localfile, remotefile = File.basename(localfile), &block) # :yield: line f = open(localfile) begin storlines("STOR #{remotefile}", f, &block) ensure f.close end end |
#pwd ⇒ Object Also known as: getdir
Returns the current remote directory.
1074 1075 1076 1077 |
# File 'lib/net/ftp.rb', line 1074 def pwd resp = sendcmd("PWD") return parse257(resp) end |
#quit ⇒ Object
Exits the FTP session.
1142 1143 1144 |
# File 'lib/net/ftp.rb', line 1142 def quit voidcmd("QUIT") end |
#rename(fromname, toname) ⇒ Object
Renames a file on the server.
990 991 992 993 994 995 996 |
# File 'lib/net/ftp.rb', line 990 def rename(fromname, toname) resp = sendcmd("RNFR #{fromname}") if !resp.start_with?("3") raise FTPReplyError, resp end voidcmd("RNTO #{toname}") end |
#retrbinary(cmd, blocksize, rest_offset = nil) ⇒ Object
Puts the connection into binary (image) mode, issues the given command, and fetches the data returned, passing it to the associated block in chunks of blocksize
characters. Note that cmd
is a server command (such as “RETR myfile”).
497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 |
# File 'lib/net/ftp.rb', line 497 def retrbinary(cmd, blocksize, rest_offset = nil) # :yield: data synchronize do with_binary(true) do begin conn = transfercmd(cmd, rest_offset) loop do data = conn.read(blocksize) break if data == nil yield(data) end conn.shutdown(Socket::SHUT_WR) conn.read_timeout = 1 conn.read ensure conn.close if conn end voidresp end end end |
#retrlines(cmd) ⇒ Object
Puts the connection into ASCII (text) mode, issues the given command, and passes the resulting data, one line at a time, to the associated block. If no block is given, prints the lines. Note that cmd
is a server command (such as “RETR myfile”).
524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 |
# File 'lib/net/ftp.rb', line 524 def retrlines(cmd) # :yield: line synchronize do with_binary(false) do begin conn = transfercmd(cmd) loop do line = conn.gets break if line == nil yield(line.sub(/\r?\n\z/, ""), !line.match(/\n\z/).nil?) end conn.shutdown(Socket::SHUT_WR) conn.read_timeout = 1 conn.read ensure conn.close if conn end voidresp end end end |
#return_code ⇒ Object
Obsolete
222 223 224 225 |
# File 'lib/net/ftp.rb', line 222 def return_code # :nodoc: $stderr.puts("warning: Net::FTP#return_code is obsolete and do nothing") return "\n" end |
#return_code=(s) ⇒ Object
Obsolete
228 229 230 |
# File 'lib/net/ftp.rb', line 228 def return_code=(s) # :nodoc: $stderr.puts("warning: Net::FTP#return_code= is obsolete and do nothing") end |
#rmdir(dirname) ⇒ Object
Removes a remote directory.
1067 1068 1069 |
# File 'lib/net/ftp.rb', line 1067 def rmdir(dirname) voidcmd("RMD #{dirname}") end |
#sendcmd(cmd) ⇒ Object
Sends a command and returns the response.
364 365 366 367 368 369 |
# File 'lib/net/ftp.rb', line 364 def sendcmd(cmd) synchronize do putline(cmd) return getresp end end |
#set_socket(sock, get_greeting = true) ⇒ Object
Set the socket used to connect to the FTP server.
May raise FTPReplyError if get_greeting
is false.
272 273 274 275 276 277 278 279 |
# File 'lib/net/ftp.rb', line 272 def set_socket(sock, get_greeting = true) synchronize do @sock = sock if get_greeting voidresp end end end |
#site(arg) ⇒ Object
Issues a SITE command.
1158 1159 1160 1161 |
# File 'lib/net/ftp.rb', line 1158 def site(arg) cmd = "SITE " + arg voidcmd(cmd) end |
#size(filename) ⇒ Object
Returns the size of the given (remote) filename.
1038 1039 1040 1041 1042 1043 1044 1045 1046 |
# File 'lib/net/ftp.rb', line 1038 def size(filename) with_binary(true) do resp = sendcmd("SIZE #{filename}") if !resp.start_with?("213") raise FTPReplyError, resp end return get_body(resp).to_i end end |
#status ⇒ Object
Returns the status (STAT command).
1108 1109 1110 1111 1112 1113 |
# File 'lib/net/ftp.rb', line 1108 def status line = "STAT" + CRLF print "put: STAT\n" if @debug_mode @sock.send(line, Socket::MSG_OOB) return getresp end |
#storbinary(cmd, file, blocksize, rest_offset = nil) ⇒ Object
Puts the connection into binary (image) mode, issues the given server-side command (such as “STOR myfile”), and sends the contents of the file named file
to the server. If the optional block is given, it also passes it the data, in chunks of blocksize
characters.
551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 |
# File 'lib/net/ftp.rb', line 551 def storbinary(cmd, file, blocksize, rest_offset = nil) # :yield: data if rest_offset file.seek(rest_offset, IO::SEEK_SET) end synchronize do with_binary(true) do conn = transfercmd(cmd) loop do buf = file.read(blocksize) break if buf == nil conn.write(buf) yield(buf) if block_given? end conn.close voidresp end end rescue Errno::EPIPE # EPIPE, in this case, means that the data connection was unexpectedly # terminated. Rather than just raising EPIPE to the caller, check the # response on the control connection. If getresp doesn't raise a more # appropriate exception, re-raise the original exception. getresp raise end |
#storlines(cmd, file) ⇒ Object
Puts the connection into ASCII (text) mode, issues the given server-side command (such as “STOR myfile”), and sends the contents of the file named file
to the server, one line at a time. If the optional block is given, it also passes it the lines.
583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 |
# File 'lib/net/ftp.rb', line 583 def storlines(cmd, file) # :yield: line synchronize do with_binary(false) do conn = transfercmd(cmd) loop do buf = file.gets break if buf == nil if buf[-2, 2] != CRLF buf = buf.chomp + CRLF end conn.write(buf) yield(buf) if block_given? end conn.close voidresp end end rescue Errno::EPIPE # EPIPE, in this case, means that the data connection was unexpectedly # terminated. Rather than just raising EPIPE to the caller, check the # response on the control connection. If getresp doesn't raise a more # appropriate exception, re-raise the original exception. getresp raise end |
#system ⇒ Object
Returns system information.
1083 1084 1085 1086 1087 1088 1089 |
# File 'lib/net/ftp.rb', line 1083 def system resp = sendcmd("SYST") if !resp.start_with?("215") raise FTPReplyError, resp end return get_body(resp) end |
#voidcmd(cmd) ⇒ Object
Sends a command and expect a response beginning with ‘2’.
374 375 376 377 378 379 |
# File 'lib/net/ftp.rb', line 374 def voidcmd(cmd) synchronize do putline(cmd) voidresp end end |