Class: HrrRbSftp::Protocol::Version1::Packet::SSH_FXP_READDIR

Inherits:
Object
  • Object
show all
Includes:
Common::Packetable
Defined in:
lib/hrr_rb_sftp/protocol/version1/packet/012_ssh_fxp_readdir.rb

Constant Summary collapse

TYPE =
12
FORMAT =
[
  [DataType::Byte,   :"type"      ],
  [DataType::Uint32, :"request-id"],
  [DataType::String, :"handle"    ],
]

Instance Attribute Summary

Attributes included from Loggable

#logger

Instance Method Summary collapse

Methods included from Common::Packetable

#decode, #encode, #initialize

Methods included from Loggable

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

Instance Method Details

#attrs(dir, entry) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
# File 'lib/hrr_rb_sftp/protocol/version1/packet/012_ssh_fxp_readdir.rb', line 77

def attrs dir, entry
  stat = ::File.lstat(::File.join(dir.path, entry))
  attrs = ::Hash.new
  attrs[:"size"]        = stat.size       if stat.size
  attrs[:"uid"]         = stat.uid        if stat.uid
  attrs[:"gid"]         = stat.gid        if stat.gid
  attrs[:"permissions"] = stat.mode       if stat.mode
  attrs[:"atime"]       = stat.atime.to_i if stat.atime
  attrs[:"mtime"]       = stat.mtime.to_i if stat.mtime
  attrs
end

#longname(dir, entry) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/hrr_rb_sftp/protocol/version1/packet/012_ssh_fxp_readdir.rb', line 64

def longname dir, entry
  stat = ::File.lstat(::File.join(dir.path, entry))
  [
    longname_permissions(stat),
    longname_nlink(stat),
    longname_user(stat),
    longname_group(stat),
    longname_size(stat),
    longname_mtime(stat),
    entry,
  ].join(" ")
end

#longname_group(stat) ⇒ Object



48
49
50
# File 'lib/hrr_rb_sftp/protocol/version1/packet/012_ssh_fxp_readdir.rb', line 48

def longname_group stat
  (::Etc.getgrgid(stat.gid).name rescue stat.gid).to_s.ljust(8, " ")
end

#longname_mtime(stat) ⇒ Object



56
57
58
59
60
61
62
# File 'lib/hrr_rb_sftp/protocol/version1/packet/012_ssh_fxp_readdir.rb', line 56

def longname_mtime stat
  if stat.mtime.year == ::Time.now.year
    stat.mtime.strftime "%b %e %H:%M"
  else
    stat.mtime.strftime "%b %e  %Y"
  end
end


40
41
42
# File 'lib/hrr_rb_sftp/protocol/version1/packet/012_ssh_fxp_readdir.rb', line 40

def longname_nlink stat
  stat.nlink.to_s.rjust(3, " ")
end

#longname_permissions(stat) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/hrr_rb_sftp/protocol/version1/packet/012_ssh_fxp_readdir.rb', line 16

def longname_permissions stat
  s = ::String.new
  s += case stat.mode & 0170000
       when 0140000 then "s"
       when 0120000 then "l"
       when 0100000 then "-"
       when 0060000 then "b"
       when 0040000 then "d"
       when 0020000 then "c"
       when 0010000 then "p"
       else "?"
       end
  s += (stat.mode & 0400) == 0400 ? "r" : "-"
  s += (stat.mode & 0200) == 0200 ? "w" : "-"
  s += (stat.mode & 0100) == 0100 ? "x" : "-"
  s += (stat.mode & 0040) == 0040 ? "r" : "-"
  s += (stat.mode & 0020) == 0020 ? "w" : "-"
  s += (stat.mode & 0010) == 0010 ? "x" : "-"
  s += (stat.mode & 0004) == 0004 ? "r" : "-"
  s += (stat.mode & 0002) == 0002 ? "w" : "-"
  s += (stat.mode & 0001) == 0001 ? "x" : "-"
  s
end

#longname_size(stat) ⇒ Object



52
53
54
# File 'lib/hrr_rb_sftp/protocol/version1/packet/012_ssh_fxp_readdir.rb', line 52

def longname_size stat
  stat.size.to_s.rjust(8, " ")
end

#longname_user(stat) ⇒ Object



44
45
46
# File 'lib/hrr_rb_sftp/protocol/version1/packet/012_ssh_fxp_readdir.rb', line 44

def longname_user stat
  (::Etc.getpwuid(stat.uid).name rescue stat.uid).to_s.ljust(8, " ")
end

#respond_to(request) ⇒ Object



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/hrr_rb_sftp/protocol/version1/packet/012_ssh_fxp_readdir.rb', line 89

def respond_to request
  begin
    raise "Specified handle does not exist" unless @handles.has_key?(request[:"handle"])
    dir = @handles[request[:"handle"]]
    raise "Specified handle is not directory" unless dir.instance_of?(::Dir)
    entries = ::Array.new
    while entry = dir.read
      entries.push entry
    end
    unless entries.empty?
      response = {
        :"type"       => SSH_FXP_NAME::TYPE,
        :"request-id" => request[:"request-id"],
        :"count"      => entries.size,
      }
      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
      {
        :"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