Class: HrrRbSftp::Protocol::Version1::Packets::SSH_FXP_OPEN

Inherits:
Packet show all
Defined in:
lib/hrr_rb_sftp/protocol/version1/packets/003_ssh_fxp_open.rb

Overview

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

Constant Summary collapse

TYPE =

Represents SSH_FXP_OPEN packet type.

3
FORMAT =

Represents SSH_FXP_OPEN packet format.

[
  [DataTypes::Byte,   :"type"      ],
  [DataTypes::Uint32, :"request-id"],
  [DataTypes::String, :"filename"  ],
  [DataTypes::Uint32, :"pflags"    ],
  [DataTypes::Attrs,  :"attrs"     ],
]
SSH_FXF_READ =

Represents SSH_FXF_READ flag.

0x00000001
SSH_FXF_WRITE =

Represents SSH_FXF_WRITE flag.

0x00000002
SSH_FXF_APPEND =

Represents SSH_FXF_APPEND flag.

0x00000004
SSH_FXF_CREAT =

Represents SSH_FXF_CREAT flag.

0x00000008
SSH_FXF_TRUNC =

Represents SSH_FXF_TRUNC flag.

0x00000010
SSH_FXF_EXCL =

Represents SSH_FXF_EXCL flag.

0x00000020

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_OPEN request.

Parameters:

  • request (Hash{Symbol=>Object})

    SSH_FXP_OPEN request represented in Hash. Only permissions attribute is taken care of. When attrs field contains attributes other than permissions are ignored and they are expected to be taken care of by subsequent SSH_FXP_SETSTAT and/or SSH_FXP_FSETSTAT requests.

Returns:

  • (Hash{Symbol=>Object})

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



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
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
# File 'lib/hrr_rb_sftp/protocol/version1/packets/003_ssh_fxp_open.rb', line 66

def respond_to request
  begin
    flags = convert_pflags_to_flags request[:"pflags"]
    args = [request[:"filename"], flags]
    if (flags & ::File::CREAT == ::File::CREAT) && request[:"attrs"].has_key?(:"permissions")
      args.push request[:"attrs"][:"permissions"]
    end
    log_debug { "file = File.open(#{args.map(&:inspect).join(", ")})" }
    file = ::File.open(*args)
    log_debug { "handle = #{file.object_id.to_s(16).inspect}" }
    handle = file.object_id.to_s(16)
    log_debug { "handles[#{handle.inspect}] = file" }
    handles[handle] = file
    {
      :"type"       => SSH_FXP_HANDLE::TYPE,
      :"request-id" => request[:"request-id"],
      :"handle"     => handle,
    }
  rescue Error => e
    log_debug { e.message }
    {
      :"type"          => SSH_FXP_STATUS::TYPE,
      :"request-id"    => request[:"request-id"],
      :"code"          => SSH_FXP_STATUS::SSH_FX_FAILURE,
      :"error message" => e.message,
      :"language tag"  => "",
    }
  rescue Errno::ENOENT => e
    log_debug { e.message }
    {
      :"type"          => SSH_FXP_STATUS::TYPE,
      :"request-id"    => request[:"request-id"],
      :"code"          => SSH_FXP_STATUS::SSH_FX_NO_SUCH_FILE,
      :"error message" => "No such file or directory",
      :"language tag"  => "",
    }
  rescue Errno::EACCES => e
    log_debug { e.message }
    {
      :"type"          => SSH_FXP_STATUS::TYPE,
      :"request-id"    => request[:"request-id"],
      :"code"          => SSH_FXP_STATUS::SSH_FX_PERMISSION_DENIED,
      :"error message" => "Permission denied",
      :"language tag"  => "",
    }
  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