Blame


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