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