1 // Copyright 2012 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
5 // Plan 9 directory marshalling. See intro(5).
12 ErrShortStat = errors.New("stat buffer too short")
13 ErrBadStat = errors.New("malformed stat buffer")
16 // A Qid represents a 9P server's unique identification for a file.
18 Path uint64 // the file server's unique identification for the file
19 Vers uint32 // version number for given Path
20 Type uint8 // the type of the file (syscall.QTDIR for example)
23 // A Dir contains the metadata for a file.
25 // system-modified data
26 Type uint16 // server type
27 Dev uint32 // server subtype
30 Qid Qid // unique id from server
31 Mode uint32 // permissions
32 Atime uint32 // last read time
33 Mtime uint32 // last write time
34 Length int64 // file length
35 Name string // last element of path
36 Uid string // owner name
37 Gid string // group name
38 Muid string // last modifier name
55 // Null assigns special "don't touch" values to members of d to
56 // avoid modifiying them during syscall.Wstat.
57 func (d *Dir) Null() { *d = nullDir }
59 // Marshal encodes a 9P stat message corresponding to d into b
61 // If there isn't enough space in b for a stat message, ErrShortStat is returned.
62 func (d *Dir) Marshal(b []byte) (n int, err error) {
63 n = STATFIXLEN + len(d.Name) + len(d.Uid) + len(d.Gid) + len(d.Muid)
65 return n, ErrShortStat
68 b = pbit16(b, uint16(n)-2)
71 b = pbit8(b, d.Qid.Type)
72 b = pbit32(b, d.Qid.Vers)
73 b = pbit64(b, d.Qid.Path)
75 b = pbit32(b, d.Atime)
76 b = pbit32(b, d.Mtime)
77 b = pbit64(b, uint64(d.Length))
78 b = pstring(b, d.Name)
81 b = pstring(b, d.Muid)
86 // UnmarshalDir decodes a single 9P stat message from b and returns the resulting Dir.
88 // If b is too small to hold a valid stat message, ErrShortStat is returned.
90 // If the stat message itself is invalid, ErrBadStat is returned.
91 func UnmarshalDir(b []byte) (*Dir, error) {
92 if len(b) < STATFIXLEN {
93 return nil, ErrShortStat
95 size, buf := gbit16(b)
96 if len(b) != int(size)+2 {
97 return nil, ErrBadStat
102 d.Type, b = gbit16(b)
104 d.Qid.Type, b = gbit8(b)
105 d.Qid.Vers, b = gbit32(b)
106 d.Qid.Path, b = gbit64(b)
107 d.Mode, b = gbit32(b)
108 d.Atime, b = gbit32(b)
109 d.Mtime, b = gbit32(b)
115 if d.Name, b, ok = gstring(b); !ok {
116 return nil, ErrBadStat
118 if d.Uid, b, ok = gstring(b); !ok {
119 return nil, ErrBadStat
121 if d.Gid, b, ok = gstring(b); !ok {
122 return nil, ErrBadStat
124 if d.Muid, b, ok = gstring(b); !ok {
125 return nil, ErrBadStat
131 // pbit8 copies the 8-bit number v to b and returns the remaining slice of b.
132 func pbit8(b []byte, v uint8) []byte {
137 // pbit16 copies the 16-bit number v to b in little-endian order and returns the remaining slice of b.
138 func pbit16(b []byte, v uint16) []byte {
144 // pbit32 copies the 32-bit number v to b in little-endian order and returns the remaining slice of b.
145 func pbit32(b []byte, v uint32) []byte {
153 // pbit64 copies the 64-bit number v to b in little-endian order and returns the remaining slice of b.
154 func pbit64(b []byte, v uint64) []byte {
166 // pstring copies the string s to b, prepending it with a 16-bit length in little-endian order, and
167 // returning the remaining slice of b..
168 func pstring(b []byte, s string) []byte {
169 b = pbit16(b, uint16(len(s)))
174 // gbit8 reads an 8-bit number from b and returns it with the remaining slice of b.
175 func gbit8(b []byte) (uint8, []byte) {
176 return uint8(b[0]), b[1:]
179 // gbit16 reads a 16-bit number in little-endian order from b and returns it with the remaining slice of b.
180 func gbit16(b []byte) (uint16, []byte) {
181 return uint16(b[0]) | uint16(b[1])<<8, b[2:]
184 // gbit32 reads a 32-bit number in little-endian order from b and returns it with the remaining slice of b.
185 func gbit32(b []byte) (uint32, []byte) {
186 return uint32(b[0]) | uint32(b[1])<<8 | uint32(b[2])<<16 | uint32(b[3])<<24, b[4:]
189 // gbit64 reads a 64-bit number in little-endian order from b and returns it with the remaining slice of b.
190 func gbit64(b []byte) (uint64, []byte) {
191 lo := uint32(b[0]) | uint32(b[1])<<8 | uint32(b[2])<<16 | uint32(b[3])<<24
192 hi := uint32(b[4]) | uint32(b[5])<<8 | uint32(b[6])<<16 | uint32(b[7])<<24
193 return uint64(lo) | uint64(hi)<<32, b[8:]
196 // gstring reads a string from b, prefixed with a 16-bit length in little-endian order.
197 // It returns the string with the remaining slice of b and a boolean. If the length is
198 // greater than the number of bytes in b, the boolean will be false.
199 func gstring(b []byte) (string, []byte, bool) {
204 return string(b[:n]), b[n:], true