Class: HrrRbSftp::Protocol::Version1::Packets::SSH_FXP_READDIR

Inherits:
Packet show all
Defined in:
lib/hrr_rb_sftp/protocol/version1/packets/012_ssh_fxp_readdir.rb

Overview

This class implements SFTP protocol version 1 SSH_FXP_READDIR packet type, format, and responder.

Constant Summary collapse

TYPE =

Represents SSH_FXP_READDIR packet type.

12
FORMAT =

Represents SSH_FXP_READDIR packet format.

[
  [DataTypes::Byte,   :"type"      ],
  [DataTypes::Uint32, :"request-id"],
  [DataTypes::String, :"handle"    ],
]

Instance Attribute Summary

Attributes included from Loggable

#logger

Instance Method Summary collapse

Methods inherited from Packet

#context, #handles, #initialize, #version

Methods inherited from Common::Packets::Packet

#decode, #encode, #initialize

Methods included from Loggable

#log_debug, #log_error, #log_fatal, #log_info, #log_warn

Constructor Details

This class inherits a constructor from HrrRbSftp::Protocol::Version1::Packets::Packet

Instance Method Details

#respond_to(request) ⇒ Hash{Symbol=>Object}

Responds to SSH_FXP_READDIR request.

Parameters:

  • request (Hash{Symbol=>Object})

    SSH_FXP_READDIR request represented in Hash.

Returns:

  • (Hash{Symbol=>Object})

    Response represented in Hash. In case of success, its type is SSH_FXP_NAME. In other cases, its type is SSH_FXP_STATUS.



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/hrr_rb_sftp/protocol/version1/packets/012_ssh_fxp_readdir.rb', line 31

def respond_to request
  begin
    raise "Specified handle does not exist" unless handles.has_key?(request[:"handle"])
    log_debug { "dir = handles[#{request[:"handle"].inspect}]" }
    dir = handles[request[:"handle"]]
    raise "Specified handle is not directory" unless dir.instance_of?(::Dir)
    entries = ::Array.new
    while entry = dir.read
      log_debug { "#{entry.inspect} = dir.read" }
      log_debug { "entries.push #{entry.inspect}" }
      entries.push entry
    end
    unless entries.empty?
      log_debug { "entries is not empty" }
      log_debug { "count = #{entries.size.inspect}" }
      count = entries.size
      response = {
        :"type"       => SSH_FXP_NAME::TYPE,
        :"request-id" => request[:"request-id"],
        :"count"      => count,
      }
      entries.each.with_index do |entry, idx|
        response[:"filename[#{idx}]"] = entry
        response[:"longname[#{idx}]"] = longname(dir, entry)
        response[:"attrs[#{idx}]"]    = attrs(dir, entry)
      end
      response
    else
      log_debug { "entries is empty" }
      {
        :"type"          => SSH_FXP_STATUS::TYPE,
        :"request-id"    => request[:"request-id"],
        :"code"          => SSH_FXP_STATUS::SSH_FX_EOF,
        :"error message" => "End of file",
        :"language tag"  => "",
      }
    end
  rescue => e
    log_error { [e.backtrace[0], ": ", e.message, " (", e.class.to_s, ")\n\t", e.backtrace[1..-1].join("\n\t")].join }
    {
      :"type"          => SSH_FXP_STATUS::TYPE,
      :"request-id"    => request[:"request-id"],
      :"code"          => SSH_FXP_STATUS::SSH_FX_FAILURE,
      :"error message" => e.message,
      :"language tag"  => "",
    }
  end
end