Blame


1 4b33cdd0 2015-11-30 stephen.d package p9p
2 4b33cdd0 2015-11-30 stephen.d
3 4b33cdd0 2015-11-30 stephen.d import (
4 4b33cdd0 2015-11-30 stephen.d "bytes"
5 4b33cdd0 2015-11-30 stephen.d "encoding/binary"
6 4b33cdd0 2015-11-30 stephen.d "fmt"
7 4b33cdd0 2015-11-30 stephen.d "io"
8 4b33cdd0 2015-11-30 stephen.d "log"
9 4b33cdd0 2015-11-30 stephen.d "reflect"
10 4b33cdd0 2015-11-30 stephen.d "strings"
11 4b33cdd0 2015-11-30 stephen.d "time"
12 4b33cdd0 2015-11-30 stephen.d )
13 4b33cdd0 2015-11-30 stephen.d
14 4b33cdd0 2015-11-30 stephen.d // Codec defines the interface for encoding and decoding of 9p types.
15 4b33cdd0 2015-11-30 stephen.d // Unsupported types will throw an error.
16 4b33cdd0 2015-11-30 stephen.d type Codec interface {
17 4b33cdd0 2015-11-30 stephen.d // Unmarshal from data into the value pointed to by v.
18 4b33cdd0 2015-11-30 stephen.d Unmarshal(data []byte, v interface{}) error
19 4b33cdd0 2015-11-30 stephen.d
20 4b33cdd0 2015-11-30 stephen.d // Marshal the value v into a byte slice.
21 4b33cdd0 2015-11-30 stephen.d Marshal(v interface{}) ([]byte, error)
22 4b33cdd0 2015-11-30 stephen.d
23 4b33cdd0 2015-11-30 stephen.d // Size returns the encoded size for the target of v.
24 4b33cdd0 2015-11-30 stephen.d Size(v interface{}) int
25 4b33cdd0 2015-11-30 stephen.d }
26 4b33cdd0 2015-11-30 stephen.d
27 a0568195 2016-05-23 stevvooe // NewCodec returns a new, standard 9P2000 codec, ready for use.
28 4b33cdd0 2015-11-30 stephen.d func NewCodec() Codec {
29 4b33cdd0 2015-11-30 stephen.d return codec9p{}
30 4b33cdd0 2015-11-30 stephen.d }
31 4b33cdd0 2015-11-30 stephen.d
32 4b33cdd0 2015-11-30 stephen.d type codec9p struct{}
33 4b33cdd0 2015-11-30 stephen.d
34 4b33cdd0 2015-11-30 stephen.d func (c codec9p) Unmarshal(data []byte, v interface{}) error {
35 4b33cdd0 2015-11-30 stephen.d dec := &decoder{bytes.NewReader(data)}
36 4b33cdd0 2015-11-30 stephen.d return dec.decode(v)
37 4b33cdd0 2015-11-30 stephen.d }
38 4b33cdd0 2015-11-30 stephen.d
39 4b33cdd0 2015-11-30 stephen.d func (c codec9p) Marshal(v interface{}) ([]byte, error) {
40 4b33cdd0 2015-11-30 stephen.d var b bytes.Buffer
41 4b33cdd0 2015-11-30 stephen.d enc := &encoder{&b}
42 4b33cdd0 2015-11-30 stephen.d
43 4b33cdd0 2015-11-30 stephen.d if err := enc.encode(v); err != nil {
44 4b33cdd0 2015-11-30 stephen.d return nil, err
45 4b33cdd0 2015-11-30 stephen.d }
46 4b33cdd0 2015-11-30 stephen.d
47 4b33cdd0 2015-11-30 stephen.d return b.Bytes(), nil
48 4b33cdd0 2015-11-30 stephen.d }
49 4b33cdd0 2015-11-30 stephen.d
50 4b33cdd0 2015-11-30 stephen.d func (c codec9p) Size(v interface{}) int {
51 4b33cdd0 2015-11-30 stephen.d return int(size9p(v))
52 4b33cdd0 2015-11-30 stephen.d }
53 4b33cdd0 2015-11-30 stephen.d
54 4b33cdd0 2015-11-30 stephen.d // DecodeDir decodes a directory entry from rd using the provided codec.
55 4b33cdd0 2015-11-30 stephen.d func DecodeDir(codec Codec, rd io.Reader, d *Dir) error {
56 4b33cdd0 2015-11-30 stephen.d var ll uint16
57 4b33cdd0 2015-11-30 stephen.d
58 4b33cdd0 2015-11-30 stephen.d // pull the size off the wire
59 4b33cdd0 2015-11-30 stephen.d if err := binary.Read(rd, binary.LittleEndian, &ll); err != nil {
60 4b33cdd0 2015-11-30 stephen.d return err
61 4b33cdd0 2015-11-30 stephen.d }
62 4b33cdd0 2015-11-30 stephen.d
63 4b33cdd0 2015-11-30 stephen.d p := make([]byte, ll+2)
64 4b33cdd0 2015-11-30 stephen.d binary.LittleEndian.PutUint16(p, ll) // must have size at start
65 4b33cdd0 2015-11-30 stephen.d
66 4b33cdd0 2015-11-30 stephen.d // read out the rest of the record
67 4b33cdd0 2015-11-30 stephen.d if _, err := io.ReadFull(rd, p[2:]); err != nil {
68 4b33cdd0 2015-11-30 stephen.d return err
69 4b33cdd0 2015-11-30 stephen.d }
70 4b33cdd0 2015-11-30 stephen.d
71 4b33cdd0 2015-11-30 stephen.d return codec.Unmarshal(p, d)
72 4b33cdd0 2015-11-30 stephen.d }
73 4b33cdd0 2015-11-30 stephen.d
74 4b33cdd0 2015-11-30 stephen.d // EncodeDir writes the directory to wr.
75 4b33cdd0 2015-11-30 stephen.d func EncodeDir(codec Codec, wr io.Writer, d *Dir) error {
76 4b33cdd0 2015-11-30 stephen.d p, err := codec.Marshal(d)
77 4b33cdd0 2015-11-30 stephen.d if err != nil {
78 4b33cdd0 2015-11-30 stephen.d return err
79 4b33cdd0 2015-11-30 stephen.d }
80 4b33cdd0 2015-11-30 stephen.d
81 4b33cdd0 2015-11-30 stephen.d _, err = wr.Write(p)
82 4b33cdd0 2015-11-30 stephen.d return err
83 4b33cdd0 2015-11-30 stephen.d }
84 4b33cdd0 2015-11-30 stephen.d
85 4b33cdd0 2015-11-30 stephen.d type encoder struct {
86 4b33cdd0 2015-11-30 stephen.d wr io.Writer
87 4b33cdd0 2015-11-30 stephen.d }
88 4b33cdd0 2015-11-30 stephen.d
89 4b33cdd0 2015-11-30 stephen.d func (e *encoder) encode(vs ...interface{}) error {
90 4b33cdd0 2015-11-30 stephen.d for _, v := range vs {
91 4b33cdd0 2015-11-30 stephen.d switch v := v.(type) {
92 4b33cdd0 2015-11-30 stephen.d case uint8, uint16, uint32, uint64, FcallType, Tag, QType, Fid, Flag,
93 4b33cdd0 2015-11-30 stephen.d *uint8, *uint16, *uint32, *uint64, *FcallType, *Tag, *QType, *Fid, *Flag:
94 4b33cdd0 2015-11-30 stephen.d if err := binary.Write(e.wr, binary.LittleEndian, v); err != nil {
95 4b33cdd0 2015-11-30 stephen.d return err
96 4b33cdd0 2015-11-30 stephen.d }
97 4b33cdd0 2015-11-30 stephen.d case []byte:
98 4b33cdd0 2015-11-30 stephen.d if err := e.encode(uint32(len(v))); err != nil {
99 4b33cdd0 2015-11-30 stephen.d return err
100 4b33cdd0 2015-11-30 stephen.d }
101 4b33cdd0 2015-11-30 stephen.d
102 4b33cdd0 2015-11-30 stephen.d if err := binary.Write(e.wr, binary.LittleEndian, v); err != nil {
103 4b33cdd0 2015-11-30 stephen.d return err
104 4b33cdd0 2015-11-30 stephen.d }
105 4b33cdd0 2015-11-30 stephen.d
106 4b33cdd0 2015-11-30 stephen.d case *[]byte:
107 4b33cdd0 2015-11-30 stephen.d if err := e.encode(*v); err != nil {
108 4b33cdd0 2015-11-30 stephen.d return err
109 4b33cdd0 2015-11-30 stephen.d }
110 4b33cdd0 2015-11-30 stephen.d case string:
111 4b33cdd0 2015-11-30 stephen.d if err := binary.Write(e.wr, binary.LittleEndian, uint16(len(v))); err != nil {
112 4b33cdd0 2015-11-30 stephen.d return err
113 4b33cdd0 2015-11-30 stephen.d }
114 4b33cdd0 2015-11-30 stephen.d
115 4b33cdd0 2015-11-30 stephen.d _, err := io.WriteString(e.wr, v)
116 4b33cdd0 2015-11-30 stephen.d if err != nil {
117 4b33cdd0 2015-11-30 stephen.d return err
118 4b33cdd0 2015-11-30 stephen.d }
119 4b33cdd0 2015-11-30 stephen.d case *string:
120 4b33cdd0 2015-11-30 stephen.d if err := e.encode(*v); err != nil {
121 4b33cdd0 2015-11-30 stephen.d return err
122 4b33cdd0 2015-11-30 stephen.d }
123 4b33cdd0 2015-11-30 stephen.d
124 4b33cdd0 2015-11-30 stephen.d case []string:
125 4b33cdd0 2015-11-30 stephen.d if err := e.encode(uint16(len(v))); err != nil {
126 4b33cdd0 2015-11-30 stephen.d return err
127 4b33cdd0 2015-11-30 stephen.d }
128 4b33cdd0 2015-11-30 stephen.d
129 4b33cdd0 2015-11-30 stephen.d for _, m := range v {
130 4b33cdd0 2015-11-30 stephen.d if err := e.encode(m); err != nil {
131 4b33cdd0 2015-11-30 stephen.d return err
132 4b33cdd0 2015-11-30 stephen.d }
133 4b33cdd0 2015-11-30 stephen.d }
134 4b33cdd0 2015-11-30 stephen.d case *[]string:
135 4b33cdd0 2015-11-30 stephen.d if err := e.encode(*v); err != nil {
136 4b33cdd0 2015-11-30 stephen.d return err
137 4b33cdd0 2015-11-30 stephen.d }
138 4b33cdd0 2015-11-30 stephen.d case time.Time:
139 4b33cdd0 2015-11-30 stephen.d if err := e.encode(uint32(v.Unix())); err != nil {
140 4b33cdd0 2015-11-30 stephen.d return err
141 4b33cdd0 2015-11-30 stephen.d }
142 4b33cdd0 2015-11-30 stephen.d case *time.Time:
143 4b33cdd0 2015-11-30 stephen.d if err := e.encode(*v); err != nil {
144 4b33cdd0 2015-11-30 stephen.d return err
145 4b33cdd0 2015-11-30 stephen.d }
146 4b33cdd0 2015-11-30 stephen.d case Qid:
147 4b33cdd0 2015-11-30 stephen.d if err := e.encode(v.Type, v.Version, v.Path); err != nil {
148 4b33cdd0 2015-11-30 stephen.d return err
149 4b33cdd0 2015-11-30 stephen.d }
150 4b33cdd0 2015-11-30 stephen.d case *Qid:
151 4b33cdd0 2015-11-30 stephen.d if err := e.encode(*v); err != nil {
152 4b33cdd0 2015-11-30 stephen.d return err
153 4b33cdd0 2015-11-30 stephen.d }
154 4b33cdd0 2015-11-30 stephen.d case []Qid:
155 4b33cdd0 2015-11-30 stephen.d if err := e.encode(uint16(len(v))); err != nil {
156 4b33cdd0 2015-11-30 stephen.d return err
157 4b33cdd0 2015-11-30 stephen.d }
158 4b33cdd0 2015-11-30 stephen.d
159 4b33cdd0 2015-11-30 stephen.d elements := make([]interface{}, len(v))
160 4b33cdd0 2015-11-30 stephen.d for i := range v {
161 4b33cdd0 2015-11-30 stephen.d elements[i] = &v[i]
162 4b33cdd0 2015-11-30 stephen.d }
163 4b33cdd0 2015-11-30 stephen.d
164 4b33cdd0 2015-11-30 stephen.d if err := e.encode(elements...); err != nil {
165 4b33cdd0 2015-11-30 stephen.d return err
166 4b33cdd0 2015-11-30 stephen.d }
167 4b33cdd0 2015-11-30 stephen.d case *[]Qid:
168 4b33cdd0 2015-11-30 stephen.d if err := e.encode(*v); err != nil {
169 4b33cdd0 2015-11-30 stephen.d return err
170 4b33cdd0 2015-11-30 stephen.d }
171 4b33cdd0 2015-11-30 stephen.d case Dir:
172 4b33cdd0 2015-11-30 stephen.d elements, err := fields9p(v)
173 4b33cdd0 2015-11-30 stephen.d if err != nil {
174 4b33cdd0 2015-11-30 stephen.d return err
175 4b33cdd0 2015-11-30 stephen.d }
176 4b33cdd0 2015-11-30 stephen.d
177 4b33cdd0 2015-11-30 stephen.d if err := e.encode(uint16(size9p(elements...))); err != nil {
178 4b33cdd0 2015-11-30 stephen.d return err
179 4b33cdd0 2015-11-30 stephen.d }
180 4b33cdd0 2015-11-30 stephen.d
181 4b33cdd0 2015-11-30 stephen.d if err := e.encode(elements...); err != nil {
182 4b33cdd0 2015-11-30 stephen.d return err
183 4b33cdd0 2015-11-30 stephen.d }
184 4b33cdd0 2015-11-30 stephen.d case *Dir:
185 961194b3 2015-12-04 stevvooe if err := e.encode(*v); err != nil {
186 961194b3 2015-12-04 stevvooe return err
187 961194b3 2015-12-04 stevvooe }
188 961194b3 2015-12-04 stevvooe case []Dir:
189 961194b3 2015-12-04 stevvooe elements := make([]interface{}, len(v))
190 961194b3 2015-12-04 stevvooe for i := range v {
191 961194b3 2015-12-04 stevvooe elements[i] = &v[i]
192 961194b3 2015-12-04 stevvooe }
193 961194b3 2015-12-04 stevvooe
194 961194b3 2015-12-04 stevvooe if err := e.encode(elements...); err != nil {
195 961194b3 2015-12-04 stevvooe return err
196 961194b3 2015-12-04 stevvooe }
197 961194b3 2015-12-04 stevvooe case *[]Dir:
198 4b33cdd0 2015-11-30 stephen.d if err := e.encode(*v); err != nil {
199 4b33cdd0 2015-11-30 stephen.d return err
200 4b33cdd0 2015-11-30 stephen.d }
201 4b33cdd0 2015-11-30 stephen.d case Fcall:
202 4b33cdd0 2015-11-30 stephen.d if err := e.encode(v.Type, v.Tag, v.Message); err != nil {
203 4b33cdd0 2015-11-30 stephen.d return err
204 4b33cdd0 2015-11-30 stephen.d }
205 4b33cdd0 2015-11-30 stephen.d case *Fcall:
206 4b33cdd0 2015-11-30 stephen.d if err := e.encode(*v); err != nil {
207 4b33cdd0 2015-11-30 stephen.d return err
208 4b33cdd0 2015-11-30 stephen.d }
209 4b33cdd0 2015-11-30 stephen.d case Message:
210 4b33cdd0 2015-11-30 stephen.d elements, err := fields9p(v)
211 4b33cdd0 2015-11-30 stephen.d if err != nil {
212 4b33cdd0 2015-11-30 stephen.d return err
213 4b33cdd0 2015-11-30 stephen.d }
214 4b33cdd0 2015-11-30 stephen.d
215 4b33cdd0 2015-11-30 stephen.d switch v.(type) {
216 4b33cdd0 2015-11-30 stephen.d case MessageRstat, *MessageRstat:
217 4b33cdd0 2015-11-30 stephen.d // NOTE(stevvooe): Prepend size preceeding Dir. See bugs in
218 4b33cdd0 2015-11-30 stephen.d // http://man.cat-v.org/plan_9/5/stat to make sense of this.
219 4b33cdd0 2015-11-30 stephen.d // The field has been included here but we need to make sure
220 4b33cdd0 2015-11-30 stephen.d // to double emit it for Rstat.
221 4b33cdd0 2015-11-30 stephen.d if err := e.encode(uint16(size9p(elements...))); err != nil {
222 4b33cdd0 2015-11-30 stephen.d return err
223 4b33cdd0 2015-11-30 stephen.d }
224 4b33cdd0 2015-11-30 stephen.d }
225 4b33cdd0 2015-11-30 stephen.d
226 4b33cdd0 2015-11-30 stephen.d if err := e.encode(elements...); err != nil {
227 4b33cdd0 2015-11-30 stephen.d return err
228 4b33cdd0 2015-11-30 stephen.d }
229 4b33cdd0 2015-11-30 stephen.d }
230 4b33cdd0 2015-11-30 stephen.d }
231 4b33cdd0 2015-11-30 stephen.d
232 4b33cdd0 2015-11-30 stephen.d return nil
233 4b33cdd0 2015-11-30 stephen.d }
234 4b33cdd0 2015-11-30 stephen.d
235 4b33cdd0 2015-11-30 stephen.d type decoder struct {
236 4b33cdd0 2015-11-30 stephen.d rd io.Reader
237 4b33cdd0 2015-11-30 stephen.d }
238 4b33cdd0 2015-11-30 stephen.d
239 4b33cdd0 2015-11-30 stephen.d // read9p extracts values from rd and unmarshals them to the targets of vs.
240 4b33cdd0 2015-11-30 stephen.d func (d *decoder) decode(vs ...interface{}) error {
241 4b33cdd0 2015-11-30 stephen.d for _, v := range vs {
242 4b33cdd0 2015-11-30 stephen.d switch v := v.(type) {
243 4b33cdd0 2015-11-30 stephen.d case *uint8, *uint16, *uint32, *uint64, *FcallType, *Tag, *QType, *Fid, *Flag:
244 4b33cdd0 2015-11-30 stephen.d if err := binary.Read(d.rd, binary.LittleEndian, v); err != nil {
245 4b33cdd0 2015-11-30 stephen.d return err
246 4b33cdd0 2015-11-30 stephen.d }
247 4b33cdd0 2015-11-30 stephen.d case *[]byte:
248 4b33cdd0 2015-11-30 stephen.d var ll uint32
249 4b33cdd0 2015-11-30 stephen.d
250 4b33cdd0 2015-11-30 stephen.d if err := d.decode(&ll); err != nil {
251 4b33cdd0 2015-11-30 stephen.d return err
252 4b33cdd0 2015-11-30 stephen.d }
253 4b33cdd0 2015-11-30 stephen.d
254 4b33cdd0 2015-11-30 stephen.d *v = make([]byte, int(ll))
255 4b33cdd0 2015-11-30 stephen.d
256 4b33cdd0 2015-11-30 stephen.d if err := binary.Read(d.rd, binary.LittleEndian, v); err != nil {
257 4b33cdd0 2015-11-30 stephen.d return err
258 4b33cdd0 2015-11-30 stephen.d }
259 4b33cdd0 2015-11-30 stephen.d case *string:
260 4b33cdd0 2015-11-30 stephen.d var ll uint16
261 4b33cdd0 2015-11-30 stephen.d
262 4b33cdd0 2015-11-30 stephen.d // implement string[s] encoding
263 4b33cdd0 2015-11-30 stephen.d if err := d.decode(&ll); err != nil {
264 4b33cdd0 2015-11-30 stephen.d return err
265 4b33cdd0 2015-11-30 stephen.d }
266 4b33cdd0 2015-11-30 stephen.d
267 4b33cdd0 2015-11-30 stephen.d b := make([]byte, ll)
268 4b33cdd0 2015-11-30 stephen.d
269 4b33cdd0 2015-11-30 stephen.d n, err := io.ReadFull(d.rd, b)
270 4b33cdd0 2015-11-30 stephen.d if err != nil {
271 4b33cdd0 2015-11-30 stephen.d return err
272 4b33cdd0 2015-11-30 stephen.d }
273 4b33cdd0 2015-11-30 stephen.d
274 4b33cdd0 2015-11-30 stephen.d if n != int(ll) {
275 4b33cdd0 2015-11-30 stephen.d return fmt.Errorf("unexpected string length")
276 4b33cdd0 2015-11-30 stephen.d }
277 4b33cdd0 2015-11-30 stephen.d
278 4b33cdd0 2015-11-30 stephen.d *v = string(b)
279 4b33cdd0 2015-11-30 stephen.d case *[]string:
280 4b33cdd0 2015-11-30 stephen.d var ll uint16
281 4b33cdd0 2015-11-30 stephen.d
282 4b33cdd0 2015-11-30 stephen.d if err := d.decode(&ll); err != nil {
283 4b33cdd0 2015-11-30 stephen.d return err
284 4b33cdd0 2015-11-30 stephen.d }
285 4b33cdd0 2015-11-30 stephen.d
286 4b33cdd0 2015-11-30 stephen.d elements := make([]interface{}, int(ll))
287 4b33cdd0 2015-11-30 stephen.d *v = make([]string, int(ll))
288 4b33cdd0 2015-11-30 stephen.d for i := range elements {
289 4b33cdd0 2015-11-30 stephen.d elements[i] = &(*v)[i]
290 4b33cdd0 2015-11-30 stephen.d }
291 4b33cdd0 2015-11-30 stephen.d
292 4b33cdd0 2015-11-30 stephen.d if err := d.decode(elements...); err != nil {
293 4b33cdd0 2015-11-30 stephen.d return err
294 4b33cdd0 2015-11-30 stephen.d }
295 4b33cdd0 2015-11-30 stephen.d case *time.Time:
296 4b33cdd0 2015-11-30 stephen.d var epoch uint32
297 4b33cdd0 2015-11-30 stephen.d if err := d.decode(&epoch); err != nil {
298 4b33cdd0 2015-11-30 stephen.d return err
299 4b33cdd0 2015-11-30 stephen.d }
300 4b33cdd0 2015-11-30 stephen.d
301 4b33cdd0 2015-11-30 stephen.d *v = time.Unix(int64(epoch), 0).UTC()
302 4b33cdd0 2015-11-30 stephen.d case *Qid:
303 4b33cdd0 2015-11-30 stephen.d if err := d.decode(&v.Type, &v.Version, &v.Path); err != nil {
304 4b33cdd0 2015-11-30 stephen.d return err
305 4b33cdd0 2015-11-30 stephen.d }
306 4b33cdd0 2015-11-30 stephen.d case *[]Qid:
307 4b33cdd0 2015-11-30 stephen.d var ll uint16
308 4b33cdd0 2015-11-30 stephen.d
309 4b33cdd0 2015-11-30 stephen.d if err := d.decode(&ll); err != nil {
310 4b33cdd0 2015-11-30 stephen.d return err
311 4b33cdd0 2015-11-30 stephen.d }
312 4b33cdd0 2015-11-30 stephen.d
313 4b33cdd0 2015-11-30 stephen.d elements := make([]interface{}, int(ll))
314 4b33cdd0 2015-11-30 stephen.d *v = make([]Qid, int(ll))
315 4b33cdd0 2015-11-30 stephen.d for i := range elements {
316 4b33cdd0 2015-11-30 stephen.d elements[i] = &(*v)[i]
317 4b33cdd0 2015-11-30 stephen.d }
318 4b33cdd0 2015-11-30 stephen.d
319 4b33cdd0 2015-11-30 stephen.d if err := d.decode(elements...); err != nil {
320 4b33cdd0 2015-11-30 stephen.d return err
321 4b33cdd0 2015-11-30 stephen.d }
322 4b33cdd0 2015-11-30 stephen.d case *Dir:
323 4b33cdd0 2015-11-30 stephen.d var ll uint16
324 4b33cdd0 2015-11-30 stephen.d
325 4b33cdd0 2015-11-30 stephen.d if err := d.decode(&ll); err != nil {
326 4b33cdd0 2015-11-30 stephen.d return err
327 4b33cdd0 2015-11-30 stephen.d }
328 4b33cdd0 2015-11-30 stephen.d
329 4b33cdd0 2015-11-30 stephen.d b := make([]byte, ll)
330 4b33cdd0 2015-11-30 stephen.d // must consume entire dir entry.
331 4b33cdd0 2015-11-30 stephen.d n, err := io.ReadFull(d.rd, b)
332 4b33cdd0 2015-11-30 stephen.d if err != nil {
333 4b33cdd0 2015-11-30 stephen.d log.Println("dir readfull failed:", err, ll, n)
334 4b33cdd0 2015-11-30 stephen.d return err
335 4b33cdd0 2015-11-30 stephen.d }
336 4b33cdd0 2015-11-30 stephen.d
337 4b33cdd0 2015-11-30 stephen.d elements, err := fields9p(v)
338 4b33cdd0 2015-11-30 stephen.d if err != nil {
339 4b33cdd0 2015-11-30 stephen.d return err
340 4b33cdd0 2015-11-30 stephen.d }
341 4b33cdd0 2015-11-30 stephen.d
342 4b33cdd0 2015-11-30 stephen.d dec := &decoder{bytes.NewReader(b)}
343 4b33cdd0 2015-11-30 stephen.d
344 4b33cdd0 2015-11-30 stephen.d if err := dec.decode(elements...); err != nil {
345 4b33cdd0 2015-11-30 stephen.d return err
346 4b33cdd0 2015-11-30 stephen.d }
347 961194b3 2015-12-04 stevvooe case *[]Dir:
348 961194b3 2015-12-04 stevvooe *v = make([]Dir, 0)
349 961194b3 2015-12-04 stevvooe for {
350 961194b3 2015-12-04 stevvooe element := Dir{}
351 961194b3 2015-12-04 stevvooe if err := d.decode(&element); err != nil {
352 961194b3 2015-12-04 stevvooe if err == io.EOF {
353 961194b3 2015-12-04 stevvooe return nil
354 961194b3 2015-12-04 stevvooe }
355 961194b3 2015-12-04 stevvooe return err
356 961194b3 2015-12-04 stevvooe }
357 961194b3 2015-12-04 stevvooe *v = append(*v, element)
358 961194b3 2015-12-04 stevvooe }
359 4b33cdd0 2015-11-30 stephen.d case *Fcall:
360 4b33cdd0 2015-11-30 stephen.d if err := d.decode(&v.Type, &v.Tag); err != nil {
361 4b33cdd0 2015-11-30 stephen.d return err
362 4b33cdd0 2015-11-30 stephen.d }
363 4b33cdd0 2015-11-30 stephen.d
364 4b33cdd0 2015-11-30 stephen.d message, err := newMessage(v.Type)
365 4b33cdd0 2015-11-30 stephen.d if err != nil {
366 4b33cdd0 2015-11-30 stephen.d return err
367 4b33cdd0 2015-11-30 stephen.d }
368 4b33cdd0 2015-11-30 stephen.d
369 4b33cdd0 2015-11-30 stephen.d // NOTE(stevvooe): We do a little pointer dance to allocate the
370 4b33cdd0 2015-11-30 stephen.d // new type, write to it, then assign it back to the interface as
371 4b33cdd0 2015-11-30 stephen.d // a concrete type, avoiding a pointer (the interface) to a
372 4b33cdd0 2015-11-30 stephen.d // pointer.
373 4b33cdd0 2015-11-30 stephen.d rv := reflect.New(reflect.TypeOf(message))
374 4b33cdd0 2015-11-30 stephen.d if err := d.decode(rv.Interface()); err != nil {
375 4b33cdd0 2015-11-30 stephen.d return err
376 4b33cdd0 2015-11-30 stephen.d }
377 4b33cdd0 2015-11-30 stephen.d
378 4b33cdd0 2015-11-30 stephen.d v.Message = rv.Elem().Interface().(Message)
379 4b33cdd0 2015-11-30 stephen.d case Message:
380 4b33cdd0 2015-11-30 stephen.d elements, err := fields9p(v)
381 4b33cdd0 2015-11-30 stephen.d if err != nil {
382 4b33cdd0 2015-11-30 stephen.d return err
383 4b33cdd0 2015-11-30 stephen.d }
384 4b33cdd0 2015-11-30 stephen.d
385 4b33cdd0 2015-11-30 stephen.d switch v.(type) {
386 4b33cdd0 2015-11-30 stephen.d case *MessageRstat, MessageRstat:
387 4b33cdd0 2015-11-30 stephen.d // NOTE(stevvooe): Consume extra size preceeding Dir. See bugs
388 4b33cdd0 2015-11-30 stephen.d // in http://man.cat-v.org/plan_9/5/stat to make sense of
389 4b33cdd0 2015-11-30 stephen.d // this. The field has been included here but we need to make
390 4b33cdd0 2015-11-30 stephen.d // sure to double emit it for Rstat. decode extra size header
391 4b33cdd0 2015-11-30 stephen.d // for stat structure.
392 4b33cdd0 2015-11-30 stephen.d var ll uint16
393 4b33cdd0 2015-11-30 stephen.d if err := d.decode(&ll); err != nil {
394 4b33cdd0 2015-11-30 stephen.d return err
395 4b33cdd0 2015-11-30 stephen.d }
396 4b33cdd0 2015-11-30 stephen.d }
397 4b33cdd0 2015-11-30 stephen.d
398 4b33cdd0 2015-11-30 stephen.d if err := d.decode(elements...); err != nil {
399 4b33cdd0 2015-11-30 stephen.d return err
400 4b33cdd0 2015-11-30 stephen.d }
401 4b33cdd0 2015-11-30 stephen.d }
402 4b33cdd0 2015-11-30 stephen.d }
403 4b33cdd0 2015-11-30 stephen.d
404 4b33cdd0 2015-11-30 stephen.d return nil
405 4b33cdd0 2015-11-30 stephen.d }
406 4b33cdd0 2015-11-30 stephen.d
407 4b33cdd0 2015-11-30 stephen.d // size9p calculates the projected size of the values in vs when encoded into
408 4b33cdd0 2015-11-30 stephen.d // 9p binary protocol. If an element or elements are not valid for 9p encoded,
409 4b33cdd0 2015-11-30 stephen.d // the value 0 will be used for the size. The error will be detected when
410 4b33cdd0 2015-11-30 stephen.d // encoding.
411 4b33cdd0 2015-11-30 stephen.d func size9p(vs ...interface{}) uint32 {
412 4b33cdd0 2015-11-30 stephen.d var s uint32
413 4b33cdd0 2015-11-30 stephen.d for _, v := range vs {
414 4b33cdd0 2015-11-30 stephen.d if v == nil {
415 4b33cdd0 2015-11-30 stephen.d continue
416 4b33cdd0 2015-11-30 stephen.d }
417 4b33cdd0 2015-11-30 stephen.d
418 4b33cdd0 2015-11-30 stephen.d switch v := v.(type) {
419 4b33cdd0 2015-11-30 stephen.d case uint8, uint16, uint32, uint64, FcallType, Tag, QType, Fid, Flag,
420 4b33cdd0 2015-11-30 stephen.d *uint8, *uint16, *uint32, *uint64, *FcallType, *Tag, *QType, *Fid, *Flag:
421 4b33cdd0 2015-11-30 stephen.d s += uint32(binary.Size(v))
422 4b33cdd0 2015-11-30 stephen.d case []byte:
423 4b33cdd0 2015-11-30 stephen.d s += uint32(binary.Size(uint32(0)) + len(v))
424 4b33cdd0 2015-11-30 stephen.d case *[]byte:
425 4b33cdd0 2015-11-30 stephen.d s += size9p(uint32(0), *v)
426 4b33cdd0 2015-11-30 stephen.d case string:
427 4b33cdd0 2015-11-30 stephen.d s += uint32(binary.Size(uint16(0)) + len(v))
428 4b33cdd0 2015-11-30 stephen.d case *string:
429 4b33cdd0 2015-11-30 stephen.d s += size9p(*v)
430 4b33cdd0 2015-11-30 stephen.d case []string:
431 4b33cdd0 2015-11-30 stephen.d s += size9p(uint16(0))
432 4b33cdd0 2015-11-30 stephen.d
433 4b33cdd0 2015-11-30 stephen.d for _, sv := range v {
434 4b33cdd0 2015-11-30 stephen.d s += size9p(sv)
435 4b33cdd0 2015-11-30 stephen.d }
436 4b33cdd0 2015-11-30 stephen.d case *[]string:
437 4b33cdd0 2015-11-30 stephen.d s += size9p(*v)
438 4b33cdd0 2015-11-30 stephen.d case time.Time, *time.Time:
439 4b33cdd0 2015-11-30 stephen.d // BUG(stevvooe): Y2038 is coming.
440 4b33cdd0 2015-11-30 stephen.d s += size9p(uint32(0))
441 4b33cdd0 2015-11-30 stephen.d case Qid:
442 4b33cdd0 2015-11-30 stephen.d s += size9p(v.Type, v.Version, v.Path)
443 4b33cdd0 2015-11-30 stephen.d case *Qid:
444 4b33cdd0 2015-11-30 stephen.d s += size9p(*v)
445 4b33cdd0 2015-11-30 stephen.d case []Qid:
446 4b33cdd0 2015-11-30 stephen.d s += size9p(uint16(0))
447 4b33cdd0 2015-11-30 stephen.d elements := make([]interface{}, len(v))
448 4b33cdd0 2015-11-30 stephen.d for i := range elements {
449 4b33cdd0 2015-11-30 stephen.d elements[i] = &v[i]
450 4b33cdd0 2015-11-30 stephen.d }
451 4b33cdd0 2015-11-30 stephen.d s += size9p(elements...)
452 4b33cdd0 2015-11-30 stephen.d case *[]Qid:
453 4b33cdd0 2015-11-30 stephen.d s += size9p(*v)
454 4b33cdd0 2015-11-30 stephen.d
455 4b33cdd0 2015-11-30 stephen.d case Dir:
456 4b33cdd0 2015-11-30 stephen.d // walk the fields of the message to get the total size. we just
457 4b33cdd0 2015-11-30 stephen.d // use the field order from the message struct. We may add tag
458 4b33cdd0 2015-11-30 stephen.d // ignoring if needed.
459 4b33cdd0 2015-11-30 stephen.d elements, err := fields9p(v)
460 4b33cdd0 2015-11-30 stephen.d if err != nil {
461 4b33cdd0 2015-11-30 stephen.d // BUG(stevvooe): The options here are to return 0, panic or
462 4b33cdd0 2015-11-30 stephen.d // make this return an error. Ideally, we make it safe to
463 4b33cdd0 2015-11-30 stephen.d // return 0 and have the rest of the package do the right
464 4b33cdd0 2015-11-30 stephen.d // thing. For now, we do this, but may want to panic until
465 4b33cdd0 2015-11-30 stephen.d // things are stable.
466 4b33cdd0 2015-11-30 stephen.d panic(err)
467 4b33cdd0 2015-11-30 stephen.d }
468 4b33cdd0 2015-11-30 stephen.d
469 4b33cdd0 2015-11-30 stephen.d s += size9p(elements...) + size9p(uint16(0))
470 4b33cdd0 2015-11-30 stephen.d case *Dir:
471 4b33cdd0 2015-11-30 stephen.d s += size9p(*v)
472 961194b3 2015-12-04 stevvooe case []Dir:
473 961194b3 2015-12-04 stevvooe elements := make([]interface{}, len(v))
474 961194b3 2015-12-04 stevvooe for i := range elements {
475 961194b3 2015-12-04 stevvooe elements[i] = &v[i]
476 961194b3 2015-12-04 stevvooe }
477 961194b3 2015-12-04 stevvooe s += size9p(elements...)
478 961194b3 2015-12-04 stevvooe case *[]Dir:
479 961194b3 2015-12-04 stevvooe s += size9p(*v)
480 4b33cdd0 2015-11-30 stephen.d case Fcall:
481 4b33cdd0 2015-11-30 stephen.d s += size9p(v.Type, v.Tag, v.Message)
482 4b33cdd0 2015-11-30 stephen.d case *Fcall:
483 4b33cdd0 2015-11-30 stephen.d s += size9p(*v)
484 4b33cdd0 2015-11-30 stephen.d case Message:
485 4b33cdd0 2015-11-30 stephen.d // special case twstat and rstat for size fields. See bugs in
486 4b33cdd0 2015-11-30 stephen.d // http://man.cat-v.org/plan_9/5/stat to make sense of this.
487 4b33cdd0 2015-11-30 stephen.d switch v.(type) {
488 4b33cdd0 2015-11-30 stephen.d case *MessageRstat, MessageRstat:
489 4b33cdd0 2015-11-30 stephen.d s += size9p(uint16(0)) // for extra size field before dir
490 4b33cdd0 2015-11-30 stephen.d }
491 4b33cdd0 2015-11-30 stephen.d
492 4b33cdd0 2015-11-30 stephen.d // walk the fields of the message to get the total size. we just
493 4b33cdd0 2015-11-30 stephen.d // use the field order from the message struct. We may add tag
494 4b33cdd0 2015-11-30 stephen.d // ignoring if needed.
495 4b33cdd0 2015-11-30 stephen.d elements, err := fields9p(v)
496 4b33cdd0 2015-11-30 stephen.d if err != nil {
497 4b33cdd0 2015-11-30 stephen.d // BUG(stevvooe): The options here are to return 0, panic or
498 4b33cdd0 2015-11-30 stephen.d // make this return an error. Ideally, we make it safe to
499 4b33cdd0 2015-11-30 stephen.d // return 0 and have the rest of the package do the right
500 4b33cdd0 2015-11-30 stephen.d // thing. For now, we do this, but may want to panic until
501 4b33cdd0 2015-11-30 stephen.d // things are stable.
502 4b33cdd0 2015-11-30 stephen.d panic(err)
503 4b33cdd0 2015-11-30 stephen.d }
504 4b33cdd0 2015-11-30 stephen.d
505 4b33cdd0 2015-11-30 stephen.d s += size9p(elements...)
506 4b33cdd0 2015-11-30 stephen.d }
507 4b33cdd0 2015-11-30 stephen.d }
508 4b33cdd0 2015-11-30 stephen.d
509 4b33cdd0 2015-11-30 stephen.d return s
510 4b33cdd0 2015-11-30 stephen.d }
511 4b33cdd0 2015-11-30 stephen.d
512 4b33cdd0 2015-11-30 stephen.d // fields9p lists the settable fields from a struct type for reading and
513 4b33cdd0 2015-11-30 stephen.d // writing. We are using a lot of reflection here for fairly static
514 4b33cdd0 2015-11-30 stephen.d // serialization but we can replace this in the future with generated code if
515 4b33cdd0 2015-11-30 stephen.d // performance is an issue.
516 4b33cdd0 2015-11-30 stephen.d func fields9p(v interface{}) ([]interface{}, error) {
517 4b33cdd0 2015-11-30 stephen.d rv := reflect.Indirect(reflect.ValueOf(v))
518 4b33cdd0 2015-11-30 stephen.d
519 4b33cdd0 2015-11-30 stephen.d if rv.Kind() != reflect.Struct {
520 4b33cdd0 2015-11-30 stephen.d return nil, fmt.Errorf("cannot extract fields from non-struct: %v", rv)
521 4b33cdd0 2015-11-30 stephen.d }
522 4b33cdd0 2015-11-30 stephen.d
523 4b33cdd0 2015-11-30 stephen.d var elements []interface{}
524 4b33cdd0 2015-11-30 stephen.d for i := 0; i < rv.NumField(); i++ {
525 4b33cdd0 2015-11-30 stephen.d f := rv.Field(i)
526 4b33cdd0 2015-11-30 stephen.d
527 4b33cdd0 2015-11-30 stephen.d if !f.CanInterface() {
528 4b33cdd0 2015-11-30 stephen.d // unexported field, skip it.
529 4b33cdd0 2015-11-30 stephen.d continue
530 4b33cdd0 2015-11-30 stephen.d }
531 4b33cdd0 2015-11-30 stephen.d
532 4b33cdd0 2015-11-30 stephen.d if f.CanAddr() {
533 4b33cdd0 2015-11-30 stephen.d f = f.Addr()
534 4b33cdd0 2015-11-30 stephen.d }
535 4b33cdd0 2015-11-30 stephen.d
536 4b33cdd0 2015-11-30 stephen.d elements = append(elements, f.Interface())
537 4b33cdd0 2015-11-30 stephen.d }
538 4b33cdd0 2015-11-30 stephen.d
539 4b33cdd0 2015-11-30 stephen.d return elements, nil
540 4b33cdd0 2015-11-30 stephen.d }
541 4b33cdd0 2015-11-30 stephen.d
542 4b33cdd0 2015-11-30 stephen.d func string9p(v interface{}) string {
543 4b33cdd0 2015-11-30 stephen.d if v == nil {
544 4b33cdd0 2015-11-30 stephen.d return "nil"
545 4b33cdd0 2015-11-30 stephen.d }
546 4b33cdd0 2015-11-30 stephen.d
547 4b33cdd0 2015-11-30 stephen.d rv := reflect.Indirect(reflect.ValueOf(v))
548 4b33cdd0 2015-11-30 stephen.d
549 4b33cdd0 2015-11-30 stephen.d if rv.Kind() != reflect.Struct {
550 4b33cdd0 2015-11-30 stephen.d panic("not a struct")
551 4b33cdd0 2015-11-30 stephen.d }
552 4b33cdd0 2015-11-30 stephen.d
553 4b33cdd0 2015-11-30 stephen.d var s string
554 4b33cdd0 2015-11-30 stephen.d
555 4b33cdd0 2015-11-30 stephen.d for i := 0; i < rv.NumField(); i++ {
556 4b33cdd0 2015-11-30 stephen.d f := rv.Field(i)
557 4b33cdd0 2015-11-30 stephen.d
558 4b33cdd0 2015-11-30 stephen.d s += fmt.Sprintf(" %v=%v", strings.ToLower(rv.Type().Field(i).Name), f.Interface())
559 4b33cdd0 2015-11-30 stephen.d }
560 4b33cdd0 2015-11-30 stephen.d
561 4b33cdd0 2015-11-30 stephen.d return s
562 4b33cdd0 2015-11-30 stephen.d }