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 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 4b33cdd0 2015-11-30 stephen.d if err := e.encode(*v); err != nil {
185 4b33cdd0 2015-11-30 stephen.d return err
186 4b33cdd0 2015-11-30 stephen.d }
187 4b33cdd0 2015-11-30 stephen.d case Fcall:
188 4b33cdd0 2015-11-30 stephen.d if err := e.encode(v.Type, v.Tag, v.Message); err != nil {
189 4b33cdd0 2015-11-30 stephen.d return err
190 4b33cdd0 2015-11-30 stephen.d }
191 4b33cdd0 2015-11-30 stephen.d case *Fcall:
192 4b33cdd0 2015-11-30 stephen.d if err := e.encode(*v); err != nil {
193 4b33cdd0 2015-11-30 stephen.d return err
194 4b33cdd0 2015-11-30 stephen.d }
195 4b33cdd0 2015-11-30 stephen.d case Message:
196 4b33cdd0 2015-11-30 stephen.d elements, err := fields9p(v)
197 4b33cdd0 2015-11-30 stephen.d if 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
201 4b33cdd0 2015-11-30 stephen.d switch v.(type) {
202 4b33cdd0 2015-11-30 stephen.d case MessageRstat, *MessageRstat:
203 4b33cdd0 2015-11-30 stephen.d // NOTE(stevvooe): Prepend size preceeding Dir. See bugs in
204 4b33cdd0 2015-11-30 stephen.d // http://man.cat-v.org/plan_9/5/stat to make sense of this.
205 4b33cdd0 2015-11-30 stephen.d // The field has been included here but we need to make sure
206 4b33cdd0 2015-11-30 stephen.d // to double emit it for Rstat.
207 4b33cdd0 2015-11-30 stephen.d if err := e.encode(uint16(size9p(elements...))); err != nil {
208 4b33cdd0 2015-11-30 stephen.d return err
209 4b33cdd0 2015-11-30 stephen.d }
210 4b33cdd0 2015-11-30 stephen.d }
211 4b33cdd0 2015-11-30 stephen.d
212 4b33cdd0 2015-11-30 stephen.d if err := e.encode(elements...); err != nil {
213 4b33cdd0 2015-11-30 stephen.d return err
214 4b33cdd0 2015-11-30 stephen.d }
215 4b33cdd0 2015-11-30 stephen.d }
216 4b33cdd0 2015-11-30 stephen.d }
217 4b33cdd0 2015-11-30 stephen.d
218 4b33cdd0 2015-11-30 stephen.d return nil
219 4b33cdd0 2015-11-30 stephen.d }
220 4b33cdd0 2015-11-30 stephen.d
221 4b33cdd0 2015-11-30 stephen.d type decoder struct {
222 4b33cdd0 2015-11-30 stephen.d rd io.Reader
223 4b33cdd0 2015-11-30 stephen.d }
224 4b33cdd0 2015-11-30 stephen.d
225 4b33cdd0 2015-11-30 stephen.d // read9p extracts values from rd and unmarshals them to the targets of vs.
226 4b33cdd0 2015-11-30 stephen.d func (d *decoder) decode(vs ...interface{}) error {
227 4b33cdd0 2015-11-30 stephen.d for _, v := range vs {
228 4b33cdd0 2015-11-30 stephen.d switch v := v.(type) {
229 4b33cdd0 2015-11-30 stephen.d case *uint8, *uint16, *uint32, *uint64, *FcallType, *Tag, *QType, *Fid, *Flag:
230 4b33cdd0 2015-11-30 stephen.d if err := binary.Read(d.rd, binary.LittleEndian, v); err != nil {
231 4b33cdd0 2015-11-30 stephen.d return err
232 4b33cdd0 2015-11-30 stephen.d }
233 4b33cdd0 2015-11-30 stephen.d case *[]byte:
234 4b33cdd0 2015-11-30 stephen.d var ll uint32
235 4b33cdd0 2015-11-30 stephen.d
236 4b33cdd0 2015-11-30 stephen.d if err := d.decode(&ll); err != nil {
237 4b33cdd0 2015-11-30 stephen.d return err
238 4b33cdd0 2015-11-30 stephen.d }
239 4b33cdd0 2015-11-30 stephen.d
240 4b33cdd0 2015-11-30 stephen.d *v = make([]byte, int(ll))
241 4b33cdd0 2015-11-30 stephen.d
242 4b33cdd0 2015-11-30 stephen.d if err := binary.Read(d.rd, binary.LittleEndian, v); err != nil {
243 4b33cdd0 2015-11-30 stephen.d return err
244 4b33cdd0 2015-11-30 stephen.d }
245 4b33cdd0 2015-11-30 stephen.d case *string:
246 4b33cdd0 2015-11-30 stephen.d var ll uint16
247 4b33cdd0 2015-11-30 stephen.d
248 4b33cdd0 2015-11-30 stephen.d // implement string[s] encoding
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 4b33cdd0 2015-11-30 stephen.d b := make([]byte, ll)
254 4b33cdd0 2015-11-30 stephen.d
255 4b33cdd0 2015-11-30 stephen.d n, err := io.ReadFull(d.rd, b)
256 4b33cdd0 2015-11-30 stephen.d if 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
260 4b33cdd0 2015-11-30 stephen.d if n != int(ll) {
261 4b33cdd0 2015-11-30 stephen.d return fmt.Errorf("unexpected string length")
262 4b33cdd0 2015-11-30 stephen.d }
263 4b33cdd0 2015-11-30 stephen.d
264 4b33cdd0 2015-11-30 stephen.d *v = string(b)
265 4b33cdd0 2015-11-30 stephen.d case *[]string:
266 4b33cdd0 2015-11-30 stephen.d var ll uint16
267 4b33cdd0 2015-11-30 stephen.d
268 4b33cdd0 2015-11-30 stephen.d if err := d.decode(&ll); err != nil {
269 4b33cdd0 2015-11-30 stephen.d return err
270 4b33cdd0 2015-11-30 stephen.d }
271 4b33cdd0 2015-11-30 stephen.d
272 4b33cdd0 2015-11-30 stephen.d elements := make([]interface{}, int(ll))
273 4b33cdd0 2015-11-30 stephen.d *v = make([]string, int(ll))
274 4b33cdd0 2015-11-30 stephen.d for i := range elements {
275 4b33cdd0 2015-11-30 stephen.d elements[i] = &(*v)[i]
276 4b33cdd0 2015-11-30 stephen.d }
277 4b33cdd0 2015-11-30 stephen.d
278 4b33cdd0 2015-11-30 stephen.d if err := d.decode(elements...); err != nil {
279 4b33cdd0 2015-11-30 stephen.d return err
280 4b33cdd0 2015-11-30 stephen.d }
281 4b33cdd0 2015-11-30 stephen.d case *time.Time:
282 4b33cdd0 2015-11-30 stephen.d var epoch uint32
283 4b33cdd0 2015-11-30 stephen.d if err := d.decode(&epoch); 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 *v = time.Unix(int64(epoch), 0).UTC()
288 4b33cdd0 2015-11-30 stephen.d case *Qid:
289 4b33cdd0 2015-11-30 stephen.d if err := d.decode(&v.Type, &v.Version, &v.Path); err != nil {
290 4b33cdd0 2015-11-30 stephen.d return err
291 4b33cdd0 2015-11-30 stephen.d }
292 4b33cdd0 2015-11-30 stephen.d case *[]Qid:
293 4b33cdd0 2015-11-30 stephen.d var ll uint16
294 4b33cdd0 2015-11-30 stephen.d
295 4b33cdd0 2015-11-30 stephen.d if err := d.decode(&ll); err != nil {
296 4b33cdd0 2015-11-30 stephen.d return err
297 4b33cdd0 2015-11-30 stephen.d }
298 4b33cdd0 2015-11-30 stephen.d
299 4b33cdd0 2015-11-30 stephen.d elements := make([]interface{}, int(ll))
300 4b33cdd0 2015-11-30 stephen.d *v = make([]Qid, int(ll))
301 4b33cdd0 2015-11-30 stephen.d for i := range elements {
302 4b33cdd0 2015-11-30 stephen.d elements[i] = &(*v)[i]
303 4b33cdd0 2015-11-30 stephen.d }
304 4b33cdd0 2015-11-30 stephen.d
305 4b33cdd0 2015-11-30 stephen.d if err := d.decode(elements...); err != nil {
306 4b33cdd0 2015-11-30 stephen.d return err
307 4b33cdd0 2015-11-30 stephen.d }
308 4b33cdd0 2015-11-30 stephen.d case *Dir:
309 4b33cdd0 2015-11-30 stephen.d var ll uint16
310 4b33cdd0 2015-11-30 stephen.d
311 4b33cdd0 2015-11-30 stephen.d if err := d.decode(&ll); err != nil {
312 4b33cdd0 2015-11-30 stephen.d return err
313 4b33cdd0 2015-11-30 stephen.d }
314 4b33cdd0 2015-11-30 stephen.d
315 4b33cdd0 2015-11-30 stephen.d b := make([]byte, ll)
316 4b33cdd0 2015-11-30 stephen.d // must consume entire dir entry.
317 4b33cdd0 2015-11-30 stephen.d n, err := io.ReadFull(d.rd, b)
318 4b33cdd0 2015-11-30 stephen.d if err != nil {
319 4b33cdd0 2015-11-30 stephen.d log.Println("dir readfull failed:", err, ll, n)
320 4b33cdd0 2015-11-30 stephen.d return err
321 4b33cdd0 2015-11-30 stephen.d }
322 4b33cdd0 2015-11-30 stephen.d
323 4b33cdd0 2015-11-30 stephen.d elements, err := fields9p(v)
324 4b33cdd0 2015-11-30 stephen.d if err != nil {
325 4b33cdd0 2015-11-30 stephen.d return err
326 4b33cdd0 2015-11-30 stephen.d }
327 4b33cdd0 2015-11-30 stephen.d
328 4b33cdd0 2015-11-30 stephen.d dec := &decoder{bytes.NewReader(b)}
329 4b33cdd0 2015-11-30 stephen.d
330 4b33cdd0 2015-11-30 stephen.d if err := dec.decode(elements...); err != nil {
331 4b33cdd0 2015-11-30 stephen.d return err
332 4b33cdd0 2015-11-30 stephen.d }
333 4b33cdd0 2015-11-30 stephen.d case *Fcall:
334 4b33cdd0 2015-11-30 stephen.d if err := d.decode(&v.Type, &v.Tag); err != nil {
335 4b33cdd0 2015-11-30 stephen.d return err
336 4b33cdd0 2015-11-30 stephen.d }
337 4b33cdd0 2015-11-30 stephen.d
338 4b33cdd0 2015-11-30 stephen.d message, err := newMessage(v.Type)
339 4b33cdd0 2015-11-30 stephen.d if err != nil {
340 4b33cdd0 2015-11-30 stephen.d return err
341 4b33cdd0 2015-11-30 stephen.d }
342 4b33cdd0 2015-11-30 stephen.d
343 4b33cdd0 2015-11-30 stephen.d // NOTE(stevvooe): We do a little pointer dance to allocate the
344 4b33cdd0 2015-11-30 stephen.d // new type, write to it, then assign it back to the interface as
345 4b33cdd0 2015-11-30 stephen.d // a concrete type, avoiding a pointer (the interface) to a
346 4b33cdd0 2015-11-30 stephen.d // pointer.
347 4b33cdd0 2015-11-30 stephen.d rv := reflect.New(reflect.TypeOf(message))
348 4b33cdd0 2015-11-30 stephen.d if err := d.decode(rv.Interface()); err != nil {
349 4b33cdd0 2015-11-30 stephen.d return err
350 4b33cdd0 2015-11-30 stephen.d }
351 4b33cdd0 2015-11-30 stephen.d
352 4b33cdd0 2015-11-30 stephen.d v.Message = rv.Elem().Interface().(Message)
353 4b33cdd0 2015-11-30 stephen.d case Message:
354 4b33cdd0 2015-11-30 stephen.d elements, err := fields9p(v)
355 4b33cdd0 2015-11-30 stephen.d if err != nil {
356 4b33cdd0 2015-11-30 stephen.d return err
357 4b33cdd0 2015-11-30 stephen.d }
358 4b33cdd0 2015-11-30 stephen.d
359 4b33cdd0 2015-11-30 stephen.d switch v.(type) {
360 4b33cdd0 2015-11-30 stephen.d case *MessageRstat, MessageRstat:
361 4b33cdd0 2015-11-30 stephen.d // NOTE(stevvooe): Consume extra size preceeding Dir. See bugs
362 4b33cdd0 2015-11-30 stephen.d // in http://man.cat-v.org/plan_9/5/stat to make sense of
363 4b33cdd0 2015-11-30 stephen.d // this. The field has been included here but we need to make
364 4b33cdd0 2015-11-30 stephen.d // sure to double emit it for Rstat. decode extra size header
365 4b33cdd0 2015-11-30 stephen.d // for stat structure.
366 4b33cdd0 2015-11-30 stephen.d var ll uint16
367 4b33cdd0 2015-11-30 stephen.d if err := d.decode(&ll); err != nil {
368 4b33cdd0 2015-11-30 stephen.d return err
369 4b33cdd0 2015-11-30 stephen.d }
370 4b33cdd0 2015-11-30 stephen.d }
371 4b33cdd0 2015-11-30 stephen.d
372 4b33cdd0 2015-11-30 stephen.d if err := d.decode(elements...); err != nil {
373 4b33cdd0 2015-11-30 stephen.d return err
374 4b33cdd0 2015-11-30 stephen.d }
375 4b33cdd0 2015-11-30 stephen.d }
376 4b33cdd0 2015-11-30 stephen.d }
377 4b33cdd0 2015-11-30 stephen.d
378 4b33cdd0 2015-11-30 stephen.d return nil
379 4b33cdd0 2015-11-30 stephen.d }
380 4b33cdd0 2015-11-30 stephen.d
381 4b33cdd0 2015-11-30 stephen.d // size9p calculates the projected size of the values in vs when encoded into
382 4b33cdd0 2015-11-30 stephen.d // 9p binary protocol. If an element or elements are not valid for 9p encoded,
383 4b33cdd0 2015-11-30 stephen.d // the value 0 will be used for the size. The error will be detected when
384 4b33cdd0 2015-11-30 stephen.d // encoding.
385 4b33cdd0 2015-11-30 stephen.d func size9p(vs ...interface{}) uint32 {
386 4b33cdd0 2015-11-30 stephen.d var s uint32
387 4b33cdd0 2015-11-30 stephen.d for _, v := range vs {
388 4b33cdd0 2015-11-30 stephen.d if v == nil {
389 4b33cdd0 2015-11-30 stephen.d continue
390 4b33cdd0 2015-11-30 stephen.d }
391 4b33cdd0 2015-11-30 stephen.d
392 4b33cdd0 2015-11-30 stephen.d switch v := v.(type) {
393 4b33cdd0 2015-11-30 stephen.d case uint8, uint16, uint32, uint64, FcallType, Tag, QType, Fid, Flag,
394 4b33cdd0 2015-11-30 stephen.d *uint8, *uint16, *uint32, *uint64, *FcallType, *Tag, *QType, *Fid, *Flag:
395 4b33cdd0 2015-11-30 stephen.d s += uint32(binary.Size(v))
396 4b33cdd0 2015-11-30 stephen.d case []byte:
397 4b33cdd0 2015-11-30 stephen.d s += uint32(binary.Size(uint32(0)) + len(v))
398 4b33cdd0 2015-11-30 stephen.d case *[]byte:
399 4b33cdd0 2015-11-30 stephen.d s += size9p(uint32(0), *v)
400 4b33cdd0 2015-11-30 stephen.d case string:
401 4b33cdd0 2015-11-30 stephen.d s += uint32(binary.Size(uint16(0)) + len(v))
402 4b33cdd0 2015-11-30 stephen.d case *string:
403 4b33cdd0 2015-11-30 stephen.d s += size9p(*v)
404 4b33cdd0 2015-11-30 stephen.d case []string:
405 4b33cdd0 2015-11-30 stephen.d s += size9p(uint16(0))
406 4b33cdd0 2015-11-30 stephen.d
407 4b33cdd0 2015-11-30 stephen.d for _, sv := range v {
408 4b33cdd0 2015-11-30 stephen.d s += size9p(sv)
409 4b33cdd0 2015-11-30 stephen.d }
410 4b33cdd0 2015-11-30 stephen.d case *[]string:
411 4b33cdd0 2015-11-30 stephen.d s += size9p(*v)
412 4b33cdd0 2015-11-30 stephen.d case time.Time, *time.Time:
413 4b33cdd0 2015-11-30 stephen.d // BUG(stevvooe): Y2038 is coming.
414 4b33cdd0 2015-11-30 stephen.d s += size9p(uint32(0))
415 4b33cdd0 2015-11-30 stephen.d case Qid:
416 4b33cdd0 2015-11-30 stephen.d s += size9p(v.Type, v.Version, v.Path)
417 4b33cdd0 2015-11-30 stephen.d case *Qid:
418 4b33cdd0 2015-11-30 stephen.d s += size9p(*v)
419 4b33cdd0 2015-11-30 stephen.d case []Qid:
420 4b33cdd0 2015-11-30 stephen.d s += size9p(uint16(0))
421 4b33cdd0 2015-11-30 stephen.d elements := make([]interface{}, len(v))
422 4b33cdd0 2015-11-30 stephen.d for i := range elements {
423 4b33cdd0 2015-11-30 stephen.d elements[i] = &v[i]
424 4b33cdd0 2015-11-30 stephen.d }
425 4b33cdd0 2015-11-30 stephen.d s += size9p(elements...)
426 4b33cdd0 2015-11-30 stephen.d case *[]Qid:
427 4b33cdd0 2015-11-30 stephen.d s += size9p(*v)
428 4b33cdd0 2015-11-30 stephen.d
429 4b33cdd0 2015-11-30 stephen.d case Dir:
430 4b33cdd0 2015-11-30 stephen.d // walk the fields of the message to get the total size. we just
431 4b33cdd0 2015-11-30 stephen.d // use the field order from the message struct. We may add tag
432 4b33cdd0 2015-11-30 stephen.d // ignoring if needed.
433 4b33cdd0 2015-11-30 stephen.d elements, err := fields9p(v)
434 4b33cdd0 2015-11-30 stephen.d if err != nil {
435 4b33cdd0 2015-11-30 stephen.d // BUG(stevvooe): The options here are to return 0, panic or
436 4b33cdd0 2015-11-30 stephen.d // make this return an error. Ideally, we make it safe to
437 4b33cdd0 2015-11-30 stephen.d // return 0 and have the rest of the package do the right
438 4b33cdd0 2015-11-30 stephen.d // thing. For now, we do this, but may want to panic until
439 4b33cdd0 2015-11-30 stephen.d // things are stable.
440 4b33cdd0 2015-11-30 stephen.d panic(err)
441 4b33cdd0 2015-11-30 stephen.d }
442 4b33cdd0 2015-11-30 stephen.d
443 4b33cdd0 2015-11-30 stephen.d s += size9p(elements...) + size9p(uint16(0))
444 4b33cdd0 2015-11-30 stephen.d case *Dir:
445 4b33cdd0 2015-11-30 stephen.d s += size9p(*v)
446 4b33cdd0 2015-11-30 stephen.d case Fcall:
447 4b33cdd0 2015-11-30 stephen.d s += size9p(v.Type, v.Tag, v.Message)
448 4b33cdd0 2015-11-30 stephen.d case *Fcall:
449 4b33cdd0 2015-11-30 stephen.d s += size9p(*v)
450 4b33cdd0 2015-11-30 stephen.d case Message:
451 4b33cdd0 2015-11-30 stephen.d // special case twstat and rstat for size fields. See bugs in
452 4b33cdd0 2015-11-30 stephen.d // http://man.cat-v.org/plan_9/5/stat to make sense of this.
453 4b33cdd0 2015-11-30 stephen.d switch v.(type) {
454 4b33cdd0 2015-11-30 stephen.d case *MessageRstat, MessageRstat:
455 4b33cdd0 2015-11-30 stephen.d s += size9p(uint16(0)) // for extra size field before dir
456 4b33cdd0 2015-11-30 stephen.d }
457 4b33cdd0 2015-11-30 stephen.d
458 4b33cdd0 2015-11-30 stephen.d // walk the fields of the message to get the total size. we just
459 4b33cdd0 2015-11-30 stephen.d // use the field order from the message struct. We may add tag
460 4b33cdd0 2015-11-30 stephen.d // ignoring if needed.
461 4b33cdd0 2015-11-30 stephen.d elements, err := fields9p(v)
462 4b33cdd0 2015-11-30 stephen.d if err != nil {
463 4b33cdd0 2015-11-30 stephen.d // BUG(stevvooe): The options here are to return 0, panic or
464 4b33cdd0 2015-11-30 stephen.d // make this return an error. Ideally, we make it safe to
465 4b33cdd0 2015-11-30 stephen.d // return 0 and have the rest of the package do the right
466 4b33cdd0 2015-11-30 stephen.d // thing. For now, we do this, but may want to panic until
467 4b33cdd0 2015-11-30 stephen.d // things are stable.
468 4b33cdd0 2015-11-30 stephen.d panic(err)
469 4b33cdd0 2015-11-30 stephen.d }
470 4b33cdd0 2015-11-30 stephen.d
471 4b33cdd0 2015-11-30 stephen.d s += size9p(elements...)
472 4b33cdd0 2015-11-30 stephen.d }
473 4b33cdd0 2015-11-30 stephen.d }
474 4b33cdd0 2015-11-30 stephen.d
475 4b33cdd0 2015-11-30 stephen.d return s
476 4b33cdd0 2015-11-30 stephen.d }
477 4b33cdd0 2015-11-30 stephen.d
478 4b33cdd0 2015-11-30 stephen.d // fields9p lists the settable fields from a struct type for reading and
479 4b33cdd0 2015-11-30 stephen.d // writing. We are using a lot of reflection here for fairly static
480 4b33cdd0 2015-11-30 stephen.d // serialization but we can replace this in the future with generated code if
481 4b33cdd0 2015-11-30 stephen.d // performance is an issue.
482 4b33cdd0 2015-11-30 stephen.d func fields9p(v interface{}) ([]interface{}, error) {
483 4b33cdd0 2015-11-30 stephen.d rv := reflect.Indirect(reflect.ValueOf(v))
484 4b33cdd0 2015-11-30 stephen.d
485 4b33cdd0 2015-11-30 stephen.d if rv.Kind() != reflect.Struct {
486 4b33cdd0 2015-11-30 stephen.d return nil, fmt.Errorf("cannot extract fields from non-struct: %v", rv)
487 4b33cdd0 2015-11-30 stephen.d }
488 4b33cdd0 2015-11-30 stephen.d
489 4b33cdd0 2015-11-30 stephen.d var elements []interface{}
490 4b33cdd0 2015-11-30 stephen.d for i := 0; i < rv.NumField(); i++ {
491 4b33cdd0 2015-11-30 stephen.d f := rv.Field(i)
492 4b33cdd0 2015-11-30 stephen.d
493 4b33cdd0 2015-11-30 stephen.d if !f.CanInterface() {
494 4b33cdd0 2015-11-30 stephen.d // unexported field, skip it.
495 4b33cdd0 2015-11-30 stephen.d continue
496 4b33cdd0 2015-11-30 stephen.d }
497 4b33cdd0 2015-11-30 stephen.d
498 4b33cdd0 2015-11-30 stephen.d if f.CanAddr() {
499 4b33cdd0 2015-11-30 stephen.d f = f.Addr()
500 4b33cdd0 2015-11-30 stephen.d }
501 4b33cdd0 2015-11-30 stephen.d
502 4b33cdd0 2015-11-30 stephen.d elements = append(elements, f.Interface())
503 4b33cdd0 2015-11-30 stephen.d }
504 4b33cdd0 2015-11-30 stephen.d
505 4b33cdd0 2015-11-30 stephen.d return elements, nil
506 4b33cdd0 2015-11-30 stephen.d }
507 4b33cdd0 2015-11-30 stephen.d
508 4b33cdd0 2015-11-30 stephen.d func string9p(v interface{}) string {
509 4b33cdd0 2015-11-30 stephen.d if v == nil {
510 4b33cdd0 2015-11-30 stephen.d return "nil"
511 4b33cdd0 2015-11-30 stephen.d }
512 4b33cdd0 2015-11-30 stephen.d
513 4b33cdd0 2015-11-30 stephen.d rv := reflect.Indirect(reflect.ValueOf(v))
514 4b33cdd0 2015-11-30 stephen.d
515 4b33cdd0 2015-11-30 stephen.d if rv.Kind() != reflect.Struct {
516 4b33cdd0 2015-11-30 stephen.d panic("not a struct")
517 4b33cdd0 2015-11-30 stephen.d }
518 4b33cdd0 2015-11-30 stephen.d
519 4b33cdd0 2015-11-30 stephen.d var s string
520 4b33cdd0 2015-11-30 stephen.d
521 4b33cdd0 2015-11-30 stephen.d for i := 0; i < rv.NumField(); i++ {
522 4b33cdd0 2015-11-30 stephen.d f := rv.Field(i)
523 4b33cdd0 2015-11-30 stephen.d
524 4b33cdd0 2015-11-30 stephen.d s += fmt.Sprintf(" %v=%v", strings.ToLower(rv.Type().Field(i).Name), f.Interface())
525 4b33cdd0 2015-11-30 stephen.d }
526 4b33cdd0 2015-11-30 stephen.d
527 4b33cdd0 2015-11-30 stephen.d return s
528 4b33cdd0 2015-11-30 stephen.d }