Class: Net::SFTP::Protocol::V01::Attributes

### Home Classes Methods

### In Files

- lib/net/sftp/protocol/01/attributes.rb

### Parent

Object

### Methods

- ::from_buffer
- ::new
- #directory?
- #file?
- #gid
- #group
- #owner
- #symbolic_type
- #symlink?
- #to_s
- #type
- #uid

### Files

- README.rdoc

### Class/Module Index

Quicksearch

- Net
- Net::SFTP
- Net::SFTP::Constants
- Net::SFTP::Constants::ACE
- Net::SFTP::Constants::ACE::Flag
- Net::SFTP::Constants::ACE::Mask
- Net::SFTP::Constants::ACE::Type
- Net::SFTP::Constants::LockTypes
- Net::SFTP::Constants::OpenFlags
- Net::SFTP::Constants::OpenFlags::FV1
- Net::SFTP::Constants::OpenFlags::FV5
- Net::SFTP::Constants::OpenFlags::FV6
- Net::SFTP::Constants::PacketTypes
- Net::SFTP::Constants::RenameFlags
- Net::SFTP::Constants::StatusCodes
- Net::SFTP::Exception
- Net::SFTP::Operations
- Net::SFTP::Operations::Dir
- Net::SFTP::Operations::Download
- Net::SFTP::Operations::File
- Net::SFTP::Operations::FileFactory
- Net::SFTP::Operations::Upload
- Net::SFTP::Packet
- Net::SFTP::Protocol
- Net::SFTP::Protocol::Base
- Net::SFTP::Protocol::V01
- Net::SFTP::Protocol::V01::Attributes
- Net::SFTP::Protocol::V01::Base
- Net::SFTP::Protocol::V01::Name
- Net::SFTP::Protocol::V02
- Net::SFTP::Protocol::V02::Base
- Net::SFTP::Protocol::V03
- Net::SFTP::Protocol::V03::Base
- Net::SFTP::Protocol::V04
- Net::SFTP::Protocol::V04::Attributes
- Net::SFTP::Protocol::V04::Base
- Net::SFTP::Protocol::V04::Name
- Net::SFTP::Protocol::V05
- Net::SFTP::Protocol::V05::Base
- Net::SFTP::Protocol::V06
- Net::SFTP::Protocol::V06::Attributes
- Net::SFTP::Protocol::V06::Base
- Net::SFTP::Request
- Net::SFTP::Response
- Net::SFTP::Session
- Net::SFTP::StatusException
- Net::SFTP::Version
- Net::SSH
- Net::SSH::Connection
- Net::SSH::Connection::Session

No matching classes.

# Net::SFTP::Protocol::V01::Attributes

A class representing the attributes of a file or directory on the server. It may be used to specify new attributes, or to query existing attributes.

To specify new attributes, just pass a hash as the argument to the constructor. The following keys are supported:

- :size | the size of the file

- :uid | the user-id that owns the file (integer)

- :gid | the group-id that owns the file (integer)

- :owner | the name of the user that owns the file (string)

- :group | the name of the group that owns the file (string)

- :permissions | the permissions on the file (integer, e.g. 0755)

- :atime | the access time of the file (integer, seconds since epoch)

- :mtime | the modification time of the file (integer, seconds since epoch)

- :extended | a hash of name/value pairs identifying extended info

Likewise, when the server sends an Attributes object, all of the above attributes are exposed as methods (though not all will be set with non-nil values from the server).

### Constants

F_ACMODTIME
F_EXTENDED
F_PERMISSIONS
F_SIZE
F_UIDGID
T_BLOCK_DEVICE
T_CHAR_DEVICE
T_DIRECTORY
T_FIFO
T_REGULAR
T_SOCKET
T_SPECIAL
T_SYMLINK
T_UNKNOWN

### Attributes

atime[RW]

The last access time of the file

attributes[R]

The hash of name/value pairs that backs this Attributes instance

extended[RW]

The hash of name/value pairs identifying extended information about the file

gid[W]

The group-id of the user that owns the file

mtime[RW]

The modification time of the file

permissions[RW]

The permissions on the file

size[RW]

The size of the file.

uid[W]

The user-id of the user that owns the file

### Public Class Methods

from_buffer(buffer) click to toggle source

Parses the given buffer and returns an Attributes object compsed from the data extracted from it.

    # File lib/net/sftp/protocol/01/attributes.rb, line 59
    def from_buffer(buffer)
      flags = buffer.read_long
      data = {}

      elements.each do |name, type, condition|
        if flags & condition == condition
          if type == :special
            data[name] = send("parse_#{name}", buffer)
          else
            data[name] = buffer.send("read_#{type}")
          end
        end
      end

      new(data)
    end

new(attributes={}) click to toggle source

Create a new Attributes instance with the given attributes. The following keys are supported:

- :size | the size of the file

- :uid | the user-id that owns the file (integer)

- :gid | the group-id that owns the file (integer)

- :owner | the name of the user that owns the file (string)

- :group | the name of the group that owns the file (string)

- :permissions | the permissions on the file (integer, e.g. 0755)

- :atime | the access time of the file (integer, seconds since epoch)

- :mtime | the modification time of the file (integer, seconds since epoch)

- :extended | a hash of name/value pairs identifying extended info

    # File lib/net/sftp/protocol/01/attributes.rb, line 152
    def initialize(attributes={})
      @attributes = attributes
    end

### Public Instance Methods

directory?() click to toggle source

Returns true if these attributes appear to describe a directory.

    # File lib/net/sftp/protocol/01/attributes.rb, line 244
    def directory?
      case type
      when T_DIRECTORY then true
      when T_UNKNOWN   then nil
      else false
      end
    end

file?() click to toggle source

Returns true if these attributes appear to describe a regular file.

    # File lib/net/sftp/protocol/01/attributes.rb, line 262
    def file?
      case type
      when T_REGULAR then true
      when T_UNKNOWN then nil
      else false
      end
    end

gid() click to toggle source

Returns the group-id of the group that owns the file, or `nil` if that information is not available. If a :group key exists, but not a :gid key, the Etc module will be used to reverse lookup the id from the name. This might fail on some systems (e.g., Windows).

    # File lib/net/sftp/protocol/01/attributes.rb, line 172
    def gid
      if attributes[:group] && !attributes.key?(:gid)
        require 'etc'
        attributes[:gid] = Etc.getgrnam(attributes[:group]).gid
      end
      attributes[:gid]
    end

group() click to toggle source

Returns the group name of the group that owns the file, or `nil` if that information is not available. If the :gid is given, but not the :group, the Etc module will be used to lookup the name from the id. This might fail on some systems (e.g. Windows).

    # File lib/net/sftp/protocol/01/attributes.rb, line 196
    def group
      if attributes[:gid] && !attributes[:group]
        require 'etc'
        attributes[:group] = Etc.getgrgid(attributes[:gid].to_i).name
      end
      attributes[:group]
    end

owner() click to toggle source

Returns the username of the user that owns the file, or `nil` if that information is not available. If the :uid is given, but not the :owner, the Etc module will be used to lookup the name from the id. This might fail on some systems (e.g. Windows).

    # File lib/net/sftp/protocol/01/attributes.rb, line 184
    def owner
      if attributes[:uid] && !attributes[:owner]
        require 'etc'
        attributes[:owner] = Etc.getpwuid(attributes[:uid].to_i).name
      end
      attributes[:owner]
    end

symbolic_type() click to toggle source

Returns the type as a symbol, rather than an integer, for easier use in Ruby programs.

    # File lib/net/sftp/protocol/01/attributes.rb, line 228
    def symbolic_type
      case type
      when T_SOCKET       then :socket
      when T_SYMLINK      then :symlink
      when T_REGULAR      then :regular
      when T_BLOCK_DEVICE then :block_device
      when T_DIRECTORY    then :directory
      when T_CHAR_DEVICE  then :char_device
      when T_FIFO         then :fifo
      when T_SPECIAL      then :special
      when T_UNKNOWN      then :unknown
      else raise NotImplementedError, "unknown file type #{type} (bug?)"
      end
    end

symlink?() click to toggle source

Returns true if these attributes appear to describe a symlink.

    # File lib/net/sftp/protocol/01/attributes.rb, line 253
    def symlink?
      case type
      when T_SYMLINK then true
      when T_UNKNOWN then nil
      else false
      end
    end

to_s() click to toggle source

Convert the object to a string suitable for passing in an SFTP packet. This is the raw representation of the attribute packet payload, and is not intended to be human readable.

    # File lib/net/sftp/protocol/01/attributes.rb, line 273
    def to_s
      prepare_serialization!

      flags = 0

      self.class.elements.each do |name, type, condition|
        flags |= condition if attributes[name]
      end

      buffer = Net::SSH::Buffer.from(:long, flags)
      self.class.elements.each do |name, type, condition|
        if flags & condition == condition
          if type == :special
            send("encode_#{name}", buffer)
          else
            buffer.send("write_#{type}", attributes[name])
          end
        end
      end

      buffer.to_s
    end

type() click to toggle source

Inspects the permissions bits to determine what type of entity this attributes object represents. If will return one of the T_ constants.

    # File lib/net/sftp/protocol/01/attributes.rb, line 206
    def type
      if    permissions & 0140000 == 0140000 then
        T_SOCKET
      elsif permissions & 0120000 == 0120000 then
        T_SYMLINK
      elsif permissions & 0100000 == 0100000 then
        T_REGULAR
      elsif permissions &  060000 ==  060000 then
        T_BLOCK_DEVICE
      elsif permissions &  040000 ==  040000 then
        T_DIRECTORY
      elsif permissions &  020000 ==  020000 then
        T_CHAR_DEVICE
      elsif permissions &  010000 ==  010000 then
        T_FIFO
      else
        T_UNKNOWN
      end
    end

uid() click to toggle source

Returns the user-id of the user that owns the file, or `nil` if that information is not available. If an :owner key exists, but not a :uid key, the Etc module will be used to reverse lookup the id from the name. This might fail on some systems (e.g., Windows).

    # File lib/net/sftp/protocol/01/attributes.rb, line 160
    def uid
      if attributes[:owner] && !attributes.key?(:uid)
        require 'etc'
        attributes[:uid] = Etc.getpwnam(attributes[:owner]).uid
      end
      attributes[:uid]
    end

[Validate]

Generated with the Darkfish Rdoc Generator 2.