~kris/9p

llm9p

ref: 35c20f3e6e008970ccdc9ea460682c578bd6d35a llm9p/internal/protocol/message.go -rw-r--r-- 8.2 KiB
35c20f3e — pdfinn fix(client): replace empty text content block with placeholder 6 months ago
                                                                                
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
package protocol

import (
	"encoding/binary"
	"fmt"
)

// Message is the interface implemented by all 9P messages
type Message interface {
	Type() uint8
	Encode(buf []byte) int
}

// Request messages (T-messages)

// TversionMsg negotiates protocol version
type TversionMsg struct {
	Msize   uint32 // maximum message size
	Version string // protocol version string
}

func (m *TversionMsg) Type() uint8 { return Tversion }

func (m *TversionMsg) Encode(buf []byte) int {
	binary.LittleEndian.PutUint32(buf[0:4], m.Msize)
	return 4 + EncodeString(buf[4:], m.Version)
}

func DecodeTversion(buf []byte) (*TversionMsg, error) {
	if len(buf) < 6 {
		return nil, fmt.Errorf("Tversion too short")
	}
	m := &TversionMsg{
		Msize: binary.LittleEndian.Uint32(buf[0:4]),
	}
	m.Version, _ = DecodeString(buf[4:])
	return m, nil
}

// RversionMsg is the response to Tversion
type RversionMsg struct {
	Msize   uint32
	Version string
}

func (m *RversionMsg) Type() uint8 { return Rversion }

func (m *RversionMsg) Encode(buf []byte) int {
	binary.LittleEndian.PutUint32(buf[0:4], m.Msize)
	return 4 + EncodeString(buf[4:], m.Version)
}

// TattachMsg attaches to a filesystem
type TattachMsg struct {
	Fid   uint32 // fid to use for this connection
	Afid  uint32 // auth fid (NoFid if no auth)
	Uname string // user name
	Aname string // attach name (filesystem to attach)
}

func (m *TattachMsg) Type() uint8 { return Tattach }

func (m *TattachMsg) Encode(buf []byte) int {
	binary.LittleEndian.PutUint32(buf[0:4], m.Fid)
	binary.LittleEndian.PutUint32(buf[4:8], m.Afid)
	n := 8
	n += EncodeString(buf[n:], m.Uname)
	n += EncodeString(buf[n:], m.Aname)
	return n
}

func DecodeTattach(buf []byte) (*TattachMsg, error) {
	if len(buf) < 12 {
		return nil, fmt.Errorf("Tattach too short")
	}
	m := &TattachMsg{
		Fid:  binary.LittleEndian.Uint32(buf[0:4]),
		Afid: binary.LittleEndian.Uint32(buf[4:8]),
	}
	n := 8
	var sn int
	m.Uname, sn = DecodeString(buf[n:])
	n += sn
	m.Aname, _ = DecodeString(buf[n:])
	return m, nil
}

// RattachMsg is the response to Tattach
type RattachMsg struct {
	Qid Qid
}

func (m *RattachMsg) Type() uint8 { return Rattach }

func (m *RattachMsg) Encode(buf []byte) int {
	return m.Qid.Encode(buf)
}

// TwalkMsg walks a path
type TwalkMsg struct {
	Fid    uint32   // starting fid
	Newfid uint32   // fid for the result
	Names  []string // path components to walk
}

func (m *TwalkMsg) Type() uint8 { return Twalk }

func (m *TwalkMsg) Encode(buf []byte) int {
	binary.LittleEndian.PutUint32(buf[0:4], m.Fid)
	binary.LittleEndian.PutUint32(buf[4:8], m.Newfid)
	binary.LittleEndian.PutUint16(buf[8:10], uint16(len(m.Names)))
	n := 10
	for _, name := range m.Names {
		n += EncodeString(buf[n:], name)
	}
	return n
}

func DecodeTwalk(buf []byte) (*TwalkMsg, error) {
	if len(buf) < 10 {
		return nil, fmt.Errorf("Twalk too short")
	}
	m := &TwalkMsg{
		Fid:    binary.LittleEndian.Uint32(buf[0:4]),
		Newfid: binary.LittleEndian.Uint32(buf[4:8]),
	}
	nwname := binary.LittleEndian.Uint16(buf[8:10])
	m.Names = make([]string, nwname)
	n := 10
	for i := range m.Names {
		var sn int
		m.Names[i], sn = DecodeString(buf[n:])
		n += sn
	}
	return m, nil
}

// RwalkMsg is the response to Twalk
type RwalkMsg struct {
	Qids []Qid // qids for each successfully walked element
}

func (m *RwalkMsg) Type() uint8 { return Rwalk }

func (m *RwalkMsg) Encode(buf []byte) int {
	binary.LittleEndian.PutUint16(buf[0:2], uint16(len(m.Qids)))
	n := 2
	for i := range m.Qids {
		n += m.Qids[i].Encode(buf[n:])
	}
	return n
}

// TopenMsg opens a file
type TopenMsg struct {
	Fid  uint32
	Mode uint8
}

func (m *TopenMsg) Type() uint8 { return Topen }

func (m *TopenMsg) Encode(buf []byte) int {
	binary.LittleEndian.PutUint32(buf[0:4], m.Fid)
	buf[4] = m.Mode
	return 5
}

func DecodeTopen(buf []byte) (*TopenMsg, error) {
	if len(buf) < 5 {
		return nil, fmt.Errorf("Topen too short")
	}
	return &TopenMsg{
		Fid:  binary.LittleEndian.Uint32(buf[0:4]),
		Mode: buf[4],
	}, nil
}

// RopenMsg is the response to Topen
type RopenMsg struct {
	Qid    Qid
	Iounit uint32
}

func (m *RopenMsg) Type() uint8 { return Ropen }

func (m *RopenMsg) Encode(buf []byte) int {
	n := m.Qid.Encode(buf)
	binary.LittleEndian.PutUint32(buf[n:n+4], m.Iounit)
	return n + 4
}

// TreadMsg reads from a file
type TreadMsg struct {
	Fid    uint32
	Offset uint64
	Count  uint32
}

func (m *TreadMsg) Type() uint8 { return Tread }

func (m *TreadMsg) Encode(buf []byte) int {
	binary.LittleEndian.PutUint32(buf[0:4], m.Fid)
	binary.LittleEndian.PutUint64(buf[4:12], m.Offset)
	binary.LittleEndian.PutUint32(buf[12:16], m.Count)
	return 16
}

func DecodeTread(buf []byte) (*TreadMsg, error) {
	if len(buf) < 16 {
		return nil, fmt.Errorf("Tread too short")
	}
	return &TreadMsg{
		Fid:    binary.LittleEndian.Uint32(buf[0:4]),
		Offset: binary.LittleEndian.Uint64(buf[4:12]),
		Count:  binary.LittleEndian.Uint32(buf[12:16]),
	}, nil
}

// RreadMsg is the response to Tread
type RreadMsg struct {
	Data []byte
}

func (m *RreadMsg) Type() uint8 { return Rread }

func (m *RreadMsg) Encode(buf []byte) int {
	binary.LittleEndian.PutUint32(buf[0:4], uint32(len(m.Data)))
	copy(buf[4:], m.Data)
	return 4 + len(m.Data)
}

// TwriteMsg writes to a file
type TwriteMsg struct {
	Fid    uint32
	Offset uint64
	Data   []byte
}

func (m *TwriteMsg) Type() uint8 { return Twrite }

func (m *TwriteMsg) Encode(buf []byte) int {
	binary.LittleEndian.PutUint32(buf[0:4], m.Fid)
	binary.LittleEndian.PutUint64(buf[4:12], m.Offset)
	binary.LittleEndian.PutUint32(buf[12:16], uint32(len(m.Data)))
	copy(buf[16:], m.Data)
	return 16 + len(m.Data)
}

func DecodeTwrite(buf []byte) (*TwriteMsg, error) {
	if len(buf) < 16 {
		return nil, fmt.Errorf("Twrite too short")
	}
	count := binary.LittleEndian.Uint32(buf[12:16])
	if len(buf) < int(16+count) {
		return nil, fmt.Errorf("Twrite data truncated")
	}
	return &TwriteMsg{
		Fid:    binary.LittleEndian.Uint32(buf[0:4]),
		Offset: binary.LittleEndian.Uint64(buf[4:12]),
		Data:   buf[16 : 16+count],
	}, nil
}

// RwriteMsg is the response to Twrite
type RwriteMsg struct {
	Count uint32
}

func (m *RwriteMsg) Type() uint8 { return Rwrite }

func (m *RwriteMsg) Encode(buf []byte) int {
	binary.LittleEndian.PutUint32(buf[0:4], m.Count)
	return 4
}

// TclunkMsg closes a fid
type TclunkMsg struct {
	Fid uint32
}

func (m *TclunkMsg) Type() uint8 { return Tclunk }

func (m *TclunkMsg) Encode(buf []byte) int {
	binary.LittleEndian.PutUint32(buf[0:4], m.Fid)
	return 4
}

func DecodeTclunk(buf []byte) (*TclunkMsg, error) {
	if len(buf) < 4 {
		return nil, fmt.Errorf("Tclunk too short")
	}
	return &TclunkMsg{
		Fid: binary.LittleEndian.Uint32(buf[0:4]),
	}, nil
}

// RclunkMsg is the response to Tclunk
type RclunkMsg struct{}

func (m *RclunkMsg) Type() uint8 { return Rclunk }

func (m *RclunkMsg) Encode(buf []byte) int {
	return 0
}

// TstatMsg requests file stats
type TstatMsg struct {
	Fid uint32
}

func (m *TstatMsg) Type() uint8 { return Tstat }

func (m *TstatMsg) Encode(buf []byte) int {
	binary.LittleEndian.PutUint32(buf[0:4], m.Fid)
	return 4
}

func DecodeTstat(buf []byte) (*TstatMsg, error) {
	if len(buf) < 4 {
		return nil, fmt.Errorf("Tstat too short")
	}
	return &TstatMsg{
		Fid: binary.LittleEndian.Uint32(buf[0:4]),
	}, nil
}

// RstatMsg is the response to Tstat
type RstatMsg struct {
	Stat Stat
}

func (m *RstatMsg) Type() uint8 { return Rstat }

func (m *RstatMsg) Encode(buf []byte) int {
	// Rstat has an extra 2-byte length prefix for the stat
	statBuf := buf[2:]
	n := m.Stat.Encode(statBuf)
	binary.LittleEndian.PutUint16(buf[0:2], uint16(n))
	return 2 + n
}

// RerrorMsg indicates an error
type RerrorMsg struct {
	Ename string
}

func (m *RerrorMsg) Type() uint8 { return Rerror }

func (m *RerrorMsg) Encode(buf []byte) int {
	return EncodeString(buf, m.Ename)
}

// TflushMsg cancels a pending request
type TflushMsg struct {
	Oldtag uint16
}

func (m *TflushMsg) Type() uint8 { return Tflush }

func (m *TflushMsg) Encode(buf []byte) int {
	binary.LittleEndian.PutUint16(buf[0:2], m.Oldtag)
	return 2
}

func DecodeTflush(buf []byte) (*TflushMsg, error) {
	if len(buf) < 2 {
		return nil, fmt.Errorf("Tflush too short")
	}
	return &TflushMsg{
		Oldtag: binary.LittleEndian.Uint16(buf[0:2]),
	}, nil
}

// RflushMsg is the response to Tflush
type RflushMsg struct{}

func (m *RflushMsg) Type() uint8 { return Rflush }

func (m *RflushMsg) Encode(buf []byte) int {
	return 0
}