~rbn/neinp

ref: 55b6eee1e78f053b260d845551ee71f4777448c6 neinp/message/stat.go -rw-r--r-- 1.8 KiB
55b6eee1Ruben Schuller move to sr.ht and go modulize 1 year, 10 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
package message

import (
	"git.sr.ht/~rbn/neinp/basic"
	"git.sr.ht/~rbn/neinp/fid"
	"git.sr.ht/~rbn/neinp/stat"
	"bytes"
	"io"
)

/*TStat requests information about file.

The file for which information is requested is identified by Fid.

See also: http://man.cat-v.org/inferno/5/stat
*/
type TStat struct {
	Fid fid.Fid
}

func (m *TStat) encode(w io.Writer) (int64, error) {
	n, err := basic.Uint32Encode(w, uint32(m.Fid))
	if err != nil {
		return n, err
	}

	return n, nil
}

func (m *TStat) decode(r io.Reader) (int64, error) {
	f, n, err := basic.Uint32Decode(r)
	if err != nil {
		return n, err
	}

	m.Fid = fid.Fid(f)
	return n, nil
}

/*RStat contains a machine-independent directory entry, Stat.

See also: http://man.cat-v.org/inferno/5/stat
*/
type RStat struct {
	Stat stat.Stat
}

func (m *RStat) encode(w io.Writer) (int64, error) {
	var buf bytes.Buffer

	_, err := m.Stat.Encode(&buf)
	if err != nil {
		return 0, err
	}

	/* From plan9 manual:
	BUGS
	To make the contents of a directory, such as returned by
	read(9P), easy to parse, each directory entry begins with a
	size field.  For consistency, the entries in Twstat and
	Rstat messages also contain their size, which means the size
	appears twice.  For example, the Rstat message is formatted
	as ``(4+1+2+2+n)[4] Rstat tag[2] n[2] (n-2)[2] type[2]
	dev[4]...,'' where n is the value returned by convD2M.
	*/

	size := uint16(buf.Len())
	n1, err := basic.Uint16Encode(w, size)
	if err != nil {
		return n1, err
	}

	n2, err := buf.WriteTo(w)
	if err != nil {
		return n1 + n2, err
	}

	return n1 + n2, nil
}

func (m *RStat) decode(r io.Reader) (int64, error) {
	// read the size but don't store it. see Rstat.encode
	_, n1, err := basic.Uint16Decode(r)
	if err != nil {
		return n1, err
	}

	n2, err := m.Stat.Decode(r)
	if err != nil {
		return n1 + n2, err
	}

	return n1 + n2, nil
}