Class: Net::SFTP::Protocol::V01::Base

Inherits:
Base
  • Object
show all
Includes:
Constants::OpenFlags
Defined in:
lib/net/sftp/protocol/01/base.rb

Overview

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.

Direct Known Subclasses

Net::SFTP::Protocol::V02::Base

Constant Summary

Constants included from Constants::PacketTypes

Constants::PacketTypes::FXP_ATTRS, Constants::PacketTypes::FXP_BLOCK, Constants::PacketTypes::FXP_CLOSE, Constants::PacketTypes::FXP_DATA, Constants::PacketTypes::FXP_EXTENDED, Constants::PacketTypes::FXP_EXTENDED_REPLY, Constants::PacketTypes::FXP_FSETSTAT, Constants::PacketTypes::FXP_FSTAT, Constants::PacketTypes::FXP_HANDLE, Constants::PacketTypes::FXP_INIT, Constants::PacketTypes::FXP_LINK, Constants::PacketTypes::FXP_LSTAT, Constants::PacketTypes::FXP_MKDIR, Constants::PacketTypes::FXP_NAME, Constants::PacketTypes::FXP_OPEN, Constants::PacketTypes::FXP_OPENDIR, Constants::PacketTypes::FXP_READ, Constants::PacketTypes::FXP_READDIR, Constants::PacketTypes::FXP_READLINK, Constants::PacketTypes::FXP_REALPATH, Constants::PacketTypes::FXP_REMOVE, Constants::PacketTypes::FXP_RENAME, Constants::PacketTypes::FXP_RMDIR, Constants::PacketTypes::FXP_SETSTAT, Constants::PacketTypes::FXP_STAT, Constants::PacketTypes::FXP_STATUS, Constants::PacketTypes::FXP_SYMLINK, Constants::PacketTypes::FXP_UNBLOCK, Constants::PacketTypes::FXP_VERSION, Constants::PacketTypes::FXP_WRITE

Instance Attribute Summary

Attributes inherited from Base

#session

Instance Method Summary collapse

Methods inherited from Base

#initialize, #parse

Constructor Details

This class inherits a constructor from Net::SFTP::Protocol::Base

Instance Method Details

#block(handle, offset, length, mask) ⇒ Object

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



218
219
220
# File 'lib/net/sftp/protocol/01/base.rb', line 218

def block(handle, offset, length, mask)
  not_implemented! :block
end

#close(handle) ⇒ Object

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.



95
96
97
# File 'lib/net/sftp/protocol/01/base.rb', line 95

def close(handle)
  send_request(FXP_CLOSE, :string, handle)
end

#fsetstat(handle, attrs) ⇒ Object

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.



142
143
144
# File 'lib/net/sftp/protocol/01/base.rb', line 142

def fsetstat(handle, attrs)
  send_request(FXP_FSETSTAT, :string, handle, :raw, attribute_factory.new(attrs).to_s)
end

#fstat(handle, flags = nil) ⇒ Object

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.



127
128
129
# File 'lib/net/sftp/protocol/01/base.rb', line 127

def fstat(handle, flags=nil)
  send_request(FXP_FSTAT, :string, handle)
end

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



212
213
214
# File 'lib/net/sftp/protocol/01/base.rb', line 212

def link(*args)
  not_implemented! :link
end

#lstat(path, flags = nil) ⇒ Object

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.



119
120
121
# File 'lib/net/sftp/protocol/01/base.rb', line 119

def lstat(path, flags=nil)
  send_request(FXP_LSTAT, :string, path)
end

#mkdir(path, attrs) ⇒ Object

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.



168
169
170
# File 'lib/net/sftp/protocol/01/base.rb', line 168

def mkdir(path, attrs)
  send_request(FXP_MKDIR, :string, path, :raw, attribute_factory.new(attrs).to_s)
end

#open(path, flags, options) ⇒ Object

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.



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/net/sftp/protocol/01/base.rb', line 73

def open(path, flags, options)
  flags = normalize_open_flags(flags)

  if flags & (IO::WRONLY | IO::RDWR) != 0
    sftp_flags = FV1::WRITE
    sftp_flags |= FV1::READ if flags & IO::RDWR != 0
    sftp_flags |= FV1::APPEND if flags & IO::APPEND != 0
  else
    sftp_flags = FV1::READ
  end

  sftp_flags |= FV1::CREAT if flags & IO::CREAT != 0
  sftp_flags |= FV1::TRUNC if flags & IO::TRUNC != 0
  sftp_flags |= FV1::EXCL  if flags & IO::EXCL  != 0

  attributes = attribute_factory.new(options)

  send_request(FXP_OPEN, :string, path, :long, sftp_flags, :raw, attributes.to_s)
end

#opendir(path) ⇒ Object

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



148
149
150
# File 'lib/net/sftp/protocol/01/base.rb', line 148

def opendir(path)
  send_request(FXP_OPENDIR, :string, path)
end

#parse_attrs_packet(packet) ⇒ Object

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



48
49
50
# File 'lib/net/sftp/protocol/01/base.rb', line 48

def parse_attrs_packet(packet)
  { :attrs => attribute_factory.from_buffer(packet) }
end

#parse_data_packet(packet) ⇒ Object

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



42
43
44
# File 'lib/net/sftp/protocol/01/base.rb', line 42

def parse_data_packet(packet)
  { :data => packet.read_string }
end

#parse_handle_packet(packet) ⇒ Object

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



30
31
32
# File 'lib/net/sftp/protocol/01/base.rb', line 30

def parse_handle_packet(packet)
  { :handle => packet.read_string }
end

#parse_name_packet(packet) ⇒ Object

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



54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/net/sftp/protocol/01/base.rb', line 54

def parse_name_packet(packet)
  names = []

  packet.read_long.times do
    filename = packet.read_string
    longname = packet.read_string
    attrs    = attribute_factory.from_buffer(packet)
    names   << name_factory.new(filename, longname, attrs)
  end

  { :names => names }
end

#parse_status_packet(packet) ⇒ Object

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



36
37
38
# File 'lib/net/sftp/protocol/01/base.rb', line 36

def parse_status_packet(packet)
  { :code => packet.read_long }
end

#read(handle, offset, length) ⇒ Object

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.



103
104
105
# File 'lib/net/sftp/protocol/01/base.rb', line 103

def read(handle, offset, length)
  send_request(FXP_READ, :string, handle, :int64, offset, :long, length)
end

#readdir(handle) ⇒ Object

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).



155
156
157
# File 'lib/net/sftp/protocol/01/base.rb', line 155

def readdir(handle)
  send_request(FXP_READDIR, :string, handle)
end

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



200
201
202
# File 'lib/net/sftp/protocol/01/base.rb', line 200

def readlink(path)
  not_implemented! :readlink
end

#realpath(path) ⇒ Object

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



180
181
182
# File 'lib/net/sftp/protocol/01/base.rb', line 180

def realpath(path)
  send_request(FXP_REALPATH, :string, path)
end

#remove(filename) ⇒ Object

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



161
162
163
# File 'lib/net/sftp/protocol/01/base.rb', line 161

def remove(filename)
  send_request(FXP_REMOVE, :string, filename)
end

#rename(name, new_name, flags = nil) ⇒ Object

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



194
195
196
# File 'lib/net/sftp/protocol/01/base.rb', line 194

def rename(name, new_name, flags=nil)
  not_implemented! :rename
end

#rmdir(path) ⇒ Object

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



174
175
176
# File 'lib/net/sftp/protocol/01/base.rb', line 174

def rmdir(path)
  send_request(FXP_RMDIR, :string, path)
end

#setstat(path, attrs) ⇒ Object

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.



134
135
136
# File 'lib/net/sftp/protocol/01/base.rb', line 134

def setstat(path, attrs)
  send_request(FXP_SETSTAT, :string, path, :raw, attribute_factory.new(attrs).to_s)
end

#stat(path, flags = nil) ⇒ Object

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.



188
189
190
# File 'lib/net/sftp/protocol/01/base.rb', line 188

def stat(path, flags=nil)
  send_request(FXP_STAT, :string, path)
end

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



206
207
208
# File 'lib/net/sftp/protocol/01/base.rb', line 206

def symlink(path, target)
  not_implemented! :symlink
end

#unblock(handle, offset, length) ⇒ Object

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



224
225
226
# File 'lib/net/sftp/protocol/01/base.rb', line 224

def unblock(handle, offset, length)
  not_implemented! :unblock
end

#versionObject

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



24
25
26
# File 'lib/net/sftp/protocol/01/base.rb', line 24

def version
  1
end

#write(handle, offset, data) ⇒ Object

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.



111
112
113
# File 'lib/net/sftp/protocol/01/base.rb', line 111

def write(handle, offset, data)
  send_request(FXP_WRITE, :string, handle, :int64, offset, :string, data)
end