Class Net::SFTP::Protocol::V01::Base
In: lib/net/sftp/protocol/01/base.rb
Parent: Protocol::Base

Wraps the low-level SFTP calls for version 1 of the SFTP protocol. Also implements the packet parsing as defined by version 1 of the protocol.

None of these protocol methods block—all of them return immediately, requiring the SSH event loop to be run while the server response is pending.

You will almost certainly never need to use this driver directly. Please see Net::SFTP::Session for the recommended interface.

Methods

Included Modules

Net::SFTP::Constants::OpenFlags

Public Instance methods

Not implemented in version 1 of the SFTP protocol. Raises a NotImplementedError if called.

[Source]

     # File lib/net/sftp/protocol/01/base.rb, line 218
218:     def block(handle, offset, length, mask)
219:       not_implemented! :block
220:     end

Sends a FXP_CLOSE packet to the server for the given handle (such as would be returned via a FXP_HANDLE packet). Returns the new packet id.

[Source]

    # File lib/net/sftp/protocol/01/base.rb, line 95
95:     def close(handle)
96:       send_request(FXP_CLOSE, :string, handle)
97:     end

Sends a FXP_FSETSTAT packet to the server, to update the attributes for the file represented by the given handle (which must have been obtained from a FXP_HANDLE packet). The attrs parameter is a hash that defines the attributes to set.

[Source]

     # File lib/net/sftp/protocol/01/base.rb, line 142
142:     def fsetstat(handle, attrs)
143:       send_request(FXP_FSETSTAT, :string, handle, :raw, attribute_factory.new(attrs).to_s)
144:     end

Sends a FXP_FSTAT packet to the server, requesting a FXP_ATTR response for the file represented by the given handle (which must have been obtained from a FXP_HANDLE packet). The flags parameter is ignored in this version of the protocol.

[Source]

     # File lib/net/sftp/protocol/01/base.rb, line 127
127:     def fstat(handle, flags=nil)
128:       send_request(FXP_FSTAT, :string, handle)
129:     end

Not implemented in version 1 of the SFTP protocol. Raises a NotImplementedError if called.

[Source]

     # File lib/net/sftp/protocol/01/base.rb, line 212
212:     def link(*args)
213:       not_implemented! :link
214:     end

Sends a FXP_LSTAT packet to the server, requesting a FXP_ATTR response for the file at the given remote path (a string). The flags parameter is ignored in this version of the protocol. lstat will not follow symbolic links; see stat for a version that will.

[Source]

     # File lib/net/sftp/protocol/01/base.rb, line 119
119:     def lstat(path, flags=nil)
120:       send_request(FXP_LSTAT, :string, path)
121:     end

Sends a FXP_MKDIR packet to the server, to request that a new directory at path on the remote server be created, and with attrs (a hash) describing the attributes of the new directory.

[Source]

     # File lib/net/sftp/protocol/01/base.rb, line 168
168:     def mkdir(path, attrs)
169:       send_request(FXP_MKDIR, :string, path, :raw, attribute_factory.new(attrs).to_s)
170:     end

Sends a FXP_OPEN packet to the server and returns the packet identifier. The flags parameter is either an integer (in which case it must be a combination of the IO constants) or a string (in which case it must be one of the mode strings that IO::open accepts). The options parameter is a hash that is used to construct a new Attribute object, to pass as part of the FXP_OPEN request.

[Source]

    # File lib/net/sftp/protocol/01/base.rb, line 73
73:     def open(path, flags, options)
74:       flags = normalize_open_flags(flags)
75: 
76:       if flags & (IO::WRONLY | IO::RDWR) != 0
77:         sftp_flags = FV1::WRITE
78:         sftp_flags |= FV1::READ if flags & IO::RDWR != 0
79:         sftp_flags |= FV1::APPEND if flags & IO::APPEND != 0
80:       else
81:         sftp_flags = FV1::READ
82:       end
83: 
84:       sftp_flags |= FV1::CREAT if flags & IO::CREAT != 0
85:       sftp_flags |= FV1::TRUNC if flags & IO::TRUNC != 0
86:       sftp_flags |= FV1::EXCL  if flags & IO::EXCL  != 0
87: 
88:       attributes = attribute_factory.new(options)
89: 
90:       send_request(FXP_OPEN, :string, path, :long, sftp_flags, :raw, attributes.to_s)
91:     end

Sends a FXP_OPENDIR packet to the server, to request a handle for manipulating the directory at the given remote path.

[Source]

     # File lib/net/sftp/protocol/01/base.rb, line 148
148:     def opendir(path)
149:       send_request(FXP_OPENDIR, :string, path)
150:     end

Parses the given FXP_ATTRS packet and returns a hash with one key, :attrs, which references an Attributes object.

[Source]

    # File lib/net/sftp/protocol/01/base.rb, line 48
48:     def parse_attrs_packet(packet)
49:       { :attrs => attribute_factory.from_buffer(packet) }
50:     end

Parses the given FXP_DATA packet and returns a hash with one key, :data, which references the data returned in the packet.

[Source]

    # File lib/net/sftp/protocol/01/base.rb, line 42
42:     def parse_data_packet(packet)
43:       { :data => packet.read_string }
44:     end

Parses the given FXP_HANDLE packet and returns a hash with one key, :handle, which references the handle.

[Source]

    # File lib/net/sftp/protocol/01/base.rb, line 30
30:     def parse_handle_packet(packet)
31:       { :handle => packet.read_string }
32:     end

Parses the given FXP_NAME packet and returns a hash with one key, :names, which references an array of Name objects.

[Source]

    # File lib/net/sftp/protocol/01/base.rb, line 54
54:     def parse_name_packet(packet)
55:       names = []
56: 
57:       packet.read_long.times do
58:         filename = packet.read_string
59:         longname = packet.read_string
60:         attrs    = attribute_factory.from_buffer(packet)
61:         names   << name_factory.new(filename, longname, attrs)
62:       end
63: 
64:       { :names => names }
65:     end

Parses the given FXP_STATUS packet and returns a hash with one key, :code, which references the status code returned by the server.

[Source]

    # File lib/net/sftp/protocol/01/base.rb, line 36
36:     def parse_status_packet(packet)
37:       { :code => packet.read_long }
38:     end

Sends a FXP_READ packet to the server, requesting that length bytes be read from the file identified by handle, starting at offset bytes within the file. The handle must be one that was returned via a FXP_HANDLE packet. Returns the new packet id.

[Source]

     # File lib/net/sftp/protocol/01/base.rb, line 103
103:     def read(handle, offset, length)
104:       send_request(FXP_READ, :string, handle, :int64, offset, :long, length)
105:     end

Sends a FXP_READDIR packet to the server, to request a batch of directory name entries in the directory identified by handle (which must have been obtained via a FXP_OPENDIR request).

[Source]

     # File lib/net/sftp/protocol/01/base.rb, line 155
155:     def readdir(handle)
156:       send_request(FXP_READDIR, :string, handle)
157:     end

Not implemented in version 1 of the SFTP protocol. Raises a NotImplementedError if called.

[Source]

     # File lib/net/sftp/protocol/01/base.rb, line 200
200:     def readlink(path)
201:       not_implemented! :readlink
202:     end

Sends a FXP_REALPATH packet to the server, to request that the given path be canonicalized, taking into account path segments like "..".

[Source]

     # File lib/net/sftp/protocol/01/base.rb, line 180
180:     def realpath(path)
181:       send_request(FXP_REALPATH, :string, path)
182:     end

Sends a FXP_REMOTE packet to the server, to request that the given file be deleted from the remote server.

[Source]

     # File lib/net/sftp/protocol/01/base.rb, line 161
161:     def remove(filename)
162:       send_request(FXP_REMOVE, :string, filename)
163:     end

Not implemented in version 1 of the SFTP protocol. Raises a NotImplementedError if called.

[Source]

     # File lib/net/sftp/protocol/01/base.rb, line 194
194:     def rename(name, new_name, flags=nil)
195:       not_implemented! :rename
196:     end

Sends a FXP_RMDIR packet to the server, to request that the directory at path on the remote server be deleted.

[Source]

     # File lib/net/sftp/protocol/01/base.rb, line 174
174:     def rmdir(path)
175:       send_request(FXP_RMDIR, :string, path)
176:     end

Sends a FXP_SETSTAT packet to the server, to update the attributes for the file at the given remote path (a string). The attrs parameter is a hash that defines the attributes to set.

[Source]

     # File lib/net/sftp/protocol/01/base.rb, line 134
134:     def setstat(path, attrs)
135:       send_request(FXP_SETSTAT, :string, path, :raw, attribute_factory.new(attrs).to_s)
136:     end

Sends a FXP_STAT packet to the server, requesting a FXP_ATTR response for the file at the given remote path (a string). The flags parameter is ignored in this version of the protocol. stat will follow symbolic links; see lstat for a version that will not.

[Source]

     # File lib/net/sftp/protocol/01/base.rb, line 188
188:     def stat(path, flags=nil)
189:       send_request(FXP_STAT, :string, path)
190:     end

Not implemented in version 1 of the SFTP protocol. Raises a NotImplementedError if called.

[Source]

     # File lib/net/sftp/protocol/01/base.rb, line 206
206:     def symlink(path, target)
207:       not_implemented! :symlink
208:     end

Not implemented in version 1 of the SFTP protocol. Raises a NotImplementedError if called.

[Source]

     # File lib/net/sftp/protocol/01/base.rb, line 224
224:     def unblock(handle, offset, length)
225:       not_implemented! :unblock
226:     end

Returns the protocol version implemented by this driver. (1, in this case)

[Source]

    # File lib/net/sftp/protocol/01/base.rb, line 24
24:     def version
25:       1
26:     end

Sends a FXP_WRITE packet to the server, requesting that data (a string), be written to the file identified by handle, starting at offset bytes from the beginning of the file. The handle must be one that was returned via a FXP_HANDLE packet. Returns the new packet id.

[Source]

     # File lib/net/sftp/protocol/01/base.rb, line 111
111:     def write(handle, offset, data)
112:       send_request(FXP_WRITE, :string, handle, :int64, offset, :string, data)
113:     end

Protected Instance methods

Returns the Attributes class used by this version of the protocol (Net::SFTP::Protocol::V01::Attributes, in this case)

[Source]

     # File lib/net/sftp/protocol/01/base.rb, line 257
257:       def attribute_factory
258:         V01::Attributes
259:       end

Returns the Name class used by this version of the protocol (Net::SFTP::Protocol::V01::Name, in this case)

[Source]

     # File lib/net/sftp/protocol/01/base.rb, line 263
263:       def name_factory
264:         V01::Name
265:       end

Normalizes the given flags parameter, converting it into a combination of IO constants.

[Source]

     # File lib/net/sftp/protocol/01/base.rb, line 239
239:       def normalize_open_flags(flags)
240:         if String === flags
241:           case flags.tr("b", "")
242:           when "r"  then IO::RDONLY
243:           when "r+" then IO::RDWR
244:           when "w"  then IO::WRONLY | IO::TRUNC | IO::CREAT
245:           when "w+" then IO::RDWR | IO::TRUNC | IO::CREAT
246:           when "a"  then IO::APPEND | IO::CREAT | IO::WRONLY
247:           when "a+" then IO::APPEND | IO::CREAT | IO::RDWR
248:           else raise ArgumentError, "unsupported flags: #{flags.inspect}"
249:           end
250:         else
251:           flags.to_i
252:         end
253:       end

A helper method for implementing wrappers for operations that are not implemented by the current SFTP protocol version. Simply raises NotImplementedError with a message based on the given operation name.

[Source]

     # File lib/net/sftp/protocol/01/base.rb, line 233
233:       def not_implemented!(operation)
234:         raise NotImplementedError, "the #{operation} operation is not available in the version of the SFTP protocol supported by your server"
235:       end

[Validate]