summaryrefslogtreecommitdiff
path: root/textproto/io.go
blob: 9e8f5320086ebdabc7d66d2dd6cfe9e90fbb60d9 (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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
package textproto

import (
	"bufio"
	"fmt"
	"io"
	"strconv"
	"strings"
)

type FIReader struct {
	r *bufio.Reader
}

func NewFIReader(r io.Reader) *FIReader {
	return &FIReader{
		r: bufio.NewReader(r),
	}
}

func (fir *FIReader) ReadLine() (line string, err error) {
	for len(line) <= 1 {
		line, err = fir.r.ReadString('\n')
		if err != nil {
			return
		}
	}

	if strings.HasPrefix(line, "data ") {
		if line[5:7] == "<<" {
			// Delimited format
			delim := line[7 : len(line)-1]
			suffix := "\n" + delim + "\n"

			for !strings.HasSuffix(line, suffix) {
				var _line string
				_line, err = fir.r.ReadString('\n')
				line += _line
				if err != nil {
					return
				}
			}
		} else {
			// Exact byte count format
			var size int
			size, err = strconv.Atoi(line[5 : len(line)-1])
			if err != nil {
				return
			}
			data := make([]byte, size)
			_, err = io.ReadFull(fir.r, data)
			line += string(data)
		}
	}
	return
}

type FIWriter struct {
	w io.Writer
}

func NewFIWriter(w io.Writer) *FIWriter {
	return &FIWriter{
		w: w,
	}
}

func (fiw *FIWriter) WriteLine(a ...interface{}) error {
	_, err := fmt.Fprintln(fiw.w, a...)
	return err
}

func (fiw *FIWriter) WriteData(data []byte) error {
	err := fiw.WriteLine("data", len(data))
	if err != nil {
		return err
	}
	_, err = fiw.w.Write(data)
	return err
}

type CatBlobReader struct {
	r *bufio.Reader
}

func NewCatBlobReader(r io.Reader) *CatBlobReader {
	return &CatBlobReader{
		r: bufio.NewReader(r),
	}
}

func (cbr *CatBlobReader) ReadLine() (line string, err error) {
	for len(line) <= 1 {
		line, err = cbr.r.ReadString('\n')
		if err != nil {
			return
		}
	}

	// get-mark : <sha1> LF
	// cat-blob : <sha1> SP 'blob' SP <size> LF <data> LF
	// ls       : <mode> SP ('blob' | 'tree' | 'commit') SP <dataref> HT <path> LF
	// ls       : 'missing' SP <path> LF

	// decide if we have a cat-blob result
	if len(line) <= 46 || line[40:46] != " blob " {
		return
	}
	for _, b := range line[:40] {
		if !(('0' <= b && b <= '9') || ('a' <= b && b <= 'f')) {
			return
		}
	}
	// we have a cat-blob result
	var size int
	size, err = strconv.Atoi(line[46 : len(line)-1])
	if err != nil {
		return
	}
	data := make([]byte, size+1)
	_, err = io.ReadFull(cbr.r, data)
	line += string(data[:size])
	return
}

type CatBlobWriter struct {
	w io.Writer
}

func NewCatBlobWriter(w io.Writer) *CatBlobWriter {
	return &CatBlobWriter {
		w: w,
	}
}

func (cbw *CatBlobWriter) WriteLine(a ...interface{}) error {
	_, err := fmt.Fprintln(cbw.w, a...)
	return err
}

func (cbw *CatBlobWriter) WriteBlob(sha1 string, data []byte) error {
	err := cbw.WriteLine(sha1, "blob", len(data))
	if err != nil {
		return err
	}
	_, err = cbw.w.Write(data)
	return err
}