sftp/sftp.go

178 lines
4.1 KiB
Go
Raw Normal View History

2013-11-05 08:25:17 +08:00
// Package sftp implements the SSH File Transfer Protocol as described in
// https://filezilla-project.org/specs/draft-ietf-secsh-filexfer-02.txt
package sftp
import (
2013-11-05 11:36:38 +08:00
"fmt"
2013-11-05 08:25:17 +08:00
)
const (
2013-11-06 16:10:28 +08:00
ssh_FXP_INIT = 1
ssh_FXP_VERSION = 2
ssh_FXP_OPEN = 3
ssh_FXP_CLOSE = 4
ssh_FXP_READ = 5
ssh_FXP_WRITE = 6
ssh_FXP_LSTAT = 7
ssh_FXP_FSTAT = 8
ssh_FXP_SETSTAT = 9
ssh_FXP_FSETSTAT = 10
ssh_FXP_OPENDIR = 11
ssh_FXP_READDIR = 12
ssh_FXP_REMOVE = 13
ssh_FXP_MKDIR = 14
ssh_FXP_RMDIR = 15
ssh_FXP_REALPATH = 16
ssh_FXP_STAT = 17
ssh_FXP_RENAME = 18
ssh_FXP_READLINK = 19
ssh_FXP_SYMLINK = 20
ssh_FXP_STATUS = 101
ssh_FXP_HANDLE = 102
ssh_FXP_DATA = 103
ssh_FXP_NAME = 104
ssh_FXP_ATTRS = 105
ssh_FXP_EXTENDED = 200
ssh_FXP_EXTENDED_REPLY = 201
2013-11-06 08:04:26 +08:00
)
2013-11-05 12:32:04 +08:00
2013-11-06 08:04:26 +08:00
const (
2013-11-06 16:10:28 +08:00
ssh_FX_OK = 0
ssh_FX_EOF = 1
ssh_FX_NO_SUCH_FILE = 2
ssh_FX_PERMISSION_DENIED = 3
ssh_FX_FAILURE = 4
ssh_FX_BAD_MESSAGE = 5
ssh_FX_NO_CONNECTION = 6
ssh_FX_CONNECTION_LOST = 7
ssh_FX_OP_UNSUPPORTED = 8
2013-11-05 08:25:17 +08:00
)
2013-11-06 08:04:26 +08:00
const (
2013-11-06 16:10:28 +08:00
ssh_FXF_READ = 0x00000001
ssh_FXF_WRITE = 0x00000002
ssh_FXF_APPEND = 0x00000004
ssh_FXF_CREAT = 0x00000008
ssh_FXF_TRUNC = 0x00000010
ssh_FXF_EXCL = 0x00000020
2013-11-06 08:04:26 +08:00
)
2013-11-05 12:32:04 +08:00
type fxp uint8
func (f fxp) String() string {
switch f {
2013-11-06 16:10:28 +08:00
case ssh_FXP_INIT:
2013-11-05 12:32:04 +08:00
return "SSH_FXP_INIT"
2013-11-06 16:10:28 +08:00
case ssh_FXP_VERSION:
2013-11-05 12:32:04 +08:00
return "SSH_FXP_VERSION"
2013-11-06 16:10:28 +08:00
case ssh_FXP_OPEN:
2013-11-05 12:32:04 +08:00
return "SSH_FXP_OPEN"
2013-11-06 16:10:28 +08:00
case ssh_FXP_CLOSE:
2013-11-05 12:32:04 +08:00
return "SSH_FXP_CLOSE"
2013-11-06 16:10:28 +08:00
case ssh_FXP_READ:
2013-11-05 12:32:04 +08:00
return "SSH_FXP_READ"
2013-11-06 16:10:28 +08:00
case ssh_FXP_WRITE:
2013-11-05 12:32:04 +08:00
return "SSH_FXP_WRITE"
2013-11-06 16:10:28 +08:00
case ssh_FXP_LSTAT:
2013-11-05 12:32:04 +08:00
return "SSH_FXP_LSTAT"
2013-11-06 16:10:28 +08:00
case ssh_FXP_FSTAT:
2013-11-05 12:32:04 +08:00
return "SSH_FXP_FSTAT"
2013-11-06 16:10:28 +08:00
case ssh_FXP_SETSTAT:
2013-11-05 12:32:04 +08:00
return "SSH_FXP_SETSTAT"
2013-11-06 16:10:28 +08:00
case ssh_FXP_FSETSTAT:
2013-11-05 12:32:04 +08:00
return "SSH_FXP_FSETSTAT"
2013-11-06 16:10:28 +08:00
case ssh_FXP_OPENDIR:
2013-11-05 12:32:04 +08:00
return "SSH_FXP_OPENDIR"
2013-11-06 16:10:28 +08:00
case ssh_FXP_READDIR:
2013-11-05 12:32:04 +08:00
return "SSH_FXP_READDIR"
2013-11-06 16:10:28 +08:00
case ssh_FXP_REMOVE:
2013-11-05 12:32:04 +08:00
return "SSH_FXP_REMOVE"
2013-11-06 16:10:28 +08:00
case ssh_FXP_MKDIR:
2013-11-05 12:32:04 +08:00
return "SSH_FXP_MKDIR"
2013-11-06 16:10:28 +08:00
case ssh_FXP_RMDIR:
2013-11-05 12:32:04 +08:00
return "SSH_FXP_RMDIR"
2013-11-06 16:10:28 +08:00
case ssh_FXP_REALPATH:
2013-11-05 12:32:04 +08:00
return "SSH_FXP_REALPATH"
2013-11-06 16:10:28 +08:00
case ssh_FXP_STAT:
2013-11-05 12:32:04 +08:00
return "SSH_FXP_STAT"
2013-11-06 16:10:28 +08:00
case ssh_FXP_RENAME:
2013-11-05 12:32:04 +08:00
return "SSH_FXP_RENAME"
2013-11-06 16:10:28 +08:00
case ssh_FXP_READLINK:
2013-11-05 12:32:04 +08:00
return "SSH_FXP_READLINK"
2013-11-06 16:10:28 +08:00
case ssh_FXP_SYMLINK:
2013-11-05 12:32:04 +08:00
return "SSH_FXP_SYMLINK"
2013-11-06 16:10:28 +08:00
case ssh_FXP_STATUS:
2013-11-05 12:32:04 +08:00
return "SSH_FXP_STATUS"
2013-11-06 16:10:28 +08:00
case ssh_FXP_HANDLE:
2013-11-05 12:32:04 +08:00
return "SSH_FXP_HANDLE"
2013-11-06 16:10:28 +08:00
case ssh_FXP_DATA:
2013-11-05 12:32:04 +08:00
return "SSH_FXP_DATA"
2013-11-06 16:10:28 +08:00
case ssh_FXP_NAME:
2013-11-05 12:32:04 +08:00
return "SSH_FXP_NAME"
2013-11-06 16:10:28 +08:00
case ssh_FXP_ATTRS:
2013-11-05 12:32:04 +08:00
return "SSH_FXP_ATTRS"
2013-11-06 16:10:28 +08:00
case ssh_FXP_EXTENDED:
2013-11-05 12:32:04 +08:00
return "SSH_FXP_EXTENDED"
2013-11-06 16:10:28 +08:00
case ssh_FXP_EXTENDED_REPLY:
2013-11-05 12:32:04 +08:00
return "SSH_FXP_EXTENDED_REPLY"
default:
return "unknown"
}
}
type fx uint8
func (f fx) String() string {
switch f {
2013-11-06 16:10:28 +08:00
case ssh_FX_OK:
2013-11-05 12:32:04 +08:00
return "SSH_FX_OK"
2013-11-06 16:10:28 +08:00
case ssh_FX_EOF:
2013-11-05 12:32:04 +08:00
return "SSH_FX_EOF"
2013-11-06 16:10:28 +08:00
case ssh_FX_NO_SUCH_FILE:
2013-11-05 12:32:04 +08:00
return "SSH_FX_NO_SUCH_FILE"
2013-11-06 16:10:28 +08:00
case ssh_FX_PERMISSION_DENIED:
2013-11-05 12:32:04 +08:00
return "SSH_FX_PERMISSION_DENIED"
2013-11-06 16:10:28 +08:00
case ssh_FX_FAILURE:
2013-11-05 12:32:04 +08:00
return "SSH_FX_FAILURE"
2013-11-06 16:10:28 +08:00
case ssh_FX_BAD_MESSAGE:
2013-11-05 12:32:04 +08:00
return "SSH_FX_BAD_MESSAGE"
2013-11-06 16:10:28 +08:00
case ssh_FX_NO_CONNECTION:
2013-11-05 12:32:04 +08:00
return "SSH_FX_NO_CONNECTION"
2013-11-06 16:10:28 +08:00
case ssh_FX_CONNECTION_LOST:
2013-11-05 12:32:04 +08:00
return "SSH_FX_CONNECTION_LOST"
2013-11-06 16:10:28 +08:00
case ssh_FX_OP_UNSUPPORTED:
2013-11-05 12:32:04 +08:00
return "SSH_FX_OP_UNSUPPORTED"
default:
return "unknown"
}
}
2013-11-05 11:36:38 +08:00
type unexpectedPacketErr struct {
want, got uint8
}
func (u *unexpectedPacketErr) Error() string {
2013-11-05 12:32:04 +08:00
return fmt.Sprintf("sftp: unexpected packet: want %v, got %v", fxp(u.want), fxp(u.got))
}
2013-11-14 12:32:21 +08:00
func unimplementedPacketErr(u uint8) error {
return fmt.Errorf("sftp: unimplemented packet type: got %v", fxp(u))
2013-11-05 12:32:04 +08:00
}
type unexpectedIdErr struct{ want, got uint32 }
func (u *unexpectedIdErr) Error() string {
return fmt.Sprintf("sftp: unexpected id: want %v, got %v", u.want, u.got)
2013-11-05 11:36:38 +08:00
}
func unimplementedSeekWhence(whence int) error {
return fmt.Errorf("sftp: unimplemented seek whence %v", whence)
}
2013-11-05 12:32:04 +08:00
type StatusError struct {
Code uint32
msg, lang string
}
func (s *StatusError) Error() string { return fmt.Sprintf("sftp: %q (%v)", s.msg, fx(s.Code)) }