~rbn/neinp

ref: 280ca11dc4f314d4bb506de2f219eb70dd959227 neinp/qid/qid.go -rw-r--r-- 1.7 KiB
280ca11dRuben Schuller update go.mod 1 year, 2 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
//Package qid provides the qid type and supporting functionality.
package qid

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

/*Qid represents the server's unique identification for a file.

Two files on the same server hierarchy are
the same if and only if their Qids are the same. (The
client may have multiple Fids pointing to a single file on a
server and hence having a single Qid.) The Qid fields hold a type,
specifying whether the file is a directory, append-only file, etc.,
the Qid Version and the Qid Path. The Path is an integer unique
among all files in the hierarchy.  If a file is deleted and recreated with
the same name in the same directory, the old and new path
components of the Qids should be different. The Version is
a version number for a file; typically, it is incremented
every time the file is modified.
*/
type Qid struct {
	Type    Type
	Version uint32
	Path    uint64
}

//Encode writes the 9p representation of the qid to an io.Writer.
func (q *Qid) Encode(w io.Writer) (int64, error) {
	n1, err := basic.Uint8Encode(w, uint8(q.Type))
	if err != nil {
		return n1, err
	}

	n2, err := basic.Uint32Encode(w, q.Version)
	if err != nil {
		return n1 + n2, err
	}

	n3, err := basic.Uint64Encode(w, q.Path)
	if err != nil {
		return n1 + n2 + n3, err
	}

	return n1 + n2 + n3, nil
}

//Decode reads the Qid data from an io.Reader.
func (q *Qid) Decode(r io.Reader) (int64, error) {
	typ, n1, err := basic.Uint8Decode(r)
	if err != nil {
		return n1, err
	}

	version, n2, err := basic.Uint32Decode(r)
	if err != nil {
		return n1 + n2, err
	}

	path, n3, err := basic.Uint64Decode(r)
	if err != nil {
		return n1 + n2 + n3, err
	}

	q.Type = Type(typ)
	q.Version = version
	q.Path = path

	return n1 + n2 + n3, err
}