/
qid.go
59 lines (49 loc) · 1.9 KB
/
qid.go
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
package styxproto
import (
"fmt"
"io"
)
// A Qid represents the server's unique identification for the file
// being accessed: two files on the same server hierarchy are the same
// if and only if their qids are the same.
type Qid []byte
// NewQid writes the 9P representation of a Qid to buf. If buf is
// not long enough to hold a Qid (13 bytes), io.ErrShortBuffer is
// returned. NewQid returns any remaining space in buf after
// the Qid has been written.
func NewQid(buf []byte, qtype uint8, version uint32, path uint64) (Qid, []byte, error) {
if len(buf) < 13 {
return nil, buf, io.ErrShortBuffer
}
buf[0] = qtype
buint32(buf[1:5], version)
buint64(buf[5:13], path)
return Qid(buf[:13]), buf[13:], nil
}
// Type returns the type of a file (directory, etc)
func (q Qid) Type() uint8 { return q[0] }
// Version is a version number for a file; typically, it is incremented
// every time a file is modified. By convention, synthetic files usually
// have a verison number of 0. Traditional files have a version number
// that is a hash of their modification time.
func (q Qid) Version() uint32 { return guint32(q[1:5]) }
// 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.
func (q Qid) Path() uint64 { return guint64(q[5:13]) }
func (q Qid) String() string {
return fmt.Sprintf("type=%d ver=%d path=%x", q.Type(), q.Version(), q.Path())
}
// A Qid's type field represents the type of a file (directory, etc.), represented
// as a bit vector corresponding to the high 8 bits of the file's mode
// word.
const (
QTDIR = 0x80 // directories
QTAPPEND = 0x40 // append only files
QTEXCL = 0x20 // exclusive use files
QTMOUNT = 0x10 // mounted channel
QTAUTH = 0x08 // authentication file (afid)
QTTMP = 0x04 // non-backed-up file
QTFILE = 0x00
)