summaryrefslogtreecommitdiff
path: root/vendor/github.com/dtylman/scp/msg.go
blob: 6dfc535356bb9ebfdc3f4f715c1f2b1d96f1a884 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
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
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
package scp

import (
	"errors"
	"fmt"
	"io"
	"io/ioutil"
	"strconv"
	"strings"
)

const (
	//CopyMessage Copy Message Opcode
	CopyMessage = 'C'
	//ErrorMessage Error OpCode
	ErrorMessage = 0x1
	//WarnMessage Warning Opcode
	WarnMessage = 0x2
)

//Message is scp control message
type Message struct {
	Type     byte
	Error    error
	Mode     string
	Size     int64
	FileName string
}

func (m *Message) readByte(reader io.Reader) (byte, error) {
	buff := make([]byte, 1)
	_, err := io.ReadFull(reader, buff)
	if err != nil {
		return 0, err
	}
	return buff[0], nil

}

func (m *Message) readOpCode(reader io.Reader) error {
	var err error
	m.Type, err = m.readByte(reader)
	return err
}

//ReadError reads an error message
func (m *Message) ReadError(reader io.Reader) error {
	msg, err := ioutil.ReadAll(reader)
	if err != nil {
		return err
	}
	m.Error = errors.New(strings.TrimSpace(string(msg)))
	return nil
}

func (m *Message) readLine(reader io.Reader) (string, error) {
	line := ""
	b, err := m.readByte(reader)
	if err != nil {
		return "", err
	}
	for b != 10 {
		line += string(b)
		b, err = m.readByte(reader)
		if err != nil {
			return "", err
		}
	}
	return line, nil
}

func (m *Message) readCopy(reader io.Reader) error {
	line, err := m.readLine(reader)
	if err != nil {
		return err
	}
	parts := strings.Split(line, " ")
	if len(parts) < 2 {
		return errors.New("Invalid copy line: " + line)
	}
	m.Mode = parts[0]
	m.Size, err = strconv.ParseInt(parts[1], 10, 0)
	if err != nil {
		return err
	}
	m.FileName = parts[2]
	return nil
}

//ReadFrom reads message from reader
func (m *Message) ReadFrom(reader io.Reader) (int64, error) {
	err := m.readOpCode(reader)
	if err != nil {
		return 0, err
	}
	switch m.Type {
	case CopyMessage:
		err = m.readCopy(reader)
		if err != nil {
			return 0, err
		}
	case ErrorMessage, WarnMessage:
		err = m.ReadError(reader)
		if err != nil {
			return 0, err
		}
	default:
		return 0, fmt.Errorf("Unsupported opcode: %v", m.Type)
	}
	return m.Size, nil
}

//NewMessageFromReader constructs a new message from a data in reader
func NewMessageFromReader(reader io.Reader) (*Message, error) {
	m := new(Message)
	_, err := m.ReadFrom(reader)
	if err != nil {
		return nil, err
	}
	return m, nil
}