summaryrefslogtreecommitdiff
path: root/src/protocol/messages.rs
blob: 106f1365ab3ba48a1e34b15b2f70b829872fde2f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
// Copyright 2018 The ChromiumOS Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

use std::io;
use std::io::ErrorKind;
use std::io::Read;
use std::io::Write;
use std::mem;
use std::string::String;
use std::vec::Vec;

use crate::protocol::wire_format::Data;
use crate::protocol::wire_format::WireFormat;

// Message type constants.  Taken from "include/net/9p/9p.h" in the linux kernel
// tree.  The protocol specifies each R* message to be the corresponding T*
// message plus one.
const TLERROR: u8 = 6;
const RLERROR: u8 = TLERROR + 1;
const TSTATFS: u8 = 8;
const RSTATFS: u8 = TSTATFS + 1;
const TLOPEN: u8 = 12;
const RLOPEN: u8 = TLOPEN + 1;
const TLCREATE: u8 = 14;
const RLCREATE: u8 = TLCREATE + 1;
const TSYMLINK: u8 = 16;
const RSYMLINK: u8 = TSYMLINK + 1;
const TMKNOD: u8 = 18;
const RMKNOD: u8 = TMKNOD + 1;
const TRENAME: u8 = 20;
const RRENAME: u8 = TRENAME + 1;
const TREADLINK: u8 = 22;
const RREADLINK: u8 = TREADLINK + 1;
const TGETATTR: u8 = 24;
const RGETATTR: u8 = TGETATTR + 1;
const TSETATTR: u8 = 26;
const RSETATTR: u8 = TSETATTR + 1;
const TXATTRWALK: u8 = 30;
const RXATTRWALK: u8 = TXATTRWALK + 1;
const TXATTRCREATE: u8 = 32;
const RXATTRCREATE: u8 = TXATTRCREATE + 1;
const TREADDIR: u8 = 40;
const RREADDIR: u8 = TREADDIR + 1;
const TFSYNC: u8 = 50;
const RFSYNC: u8 = TFSYNC + 1;
const TLOCK: u8 = 52;
const RLOCK: u8 = TLOCK + 1;
const TGETLOCK: u8 = 54;
const RGETLOCK: u8 = TGETLOCK + 1;
const TLINK: u8 = 70;
const RLINK: u8 = TLINK + 1;
const TMKDIR: u8 = 72;
const RMKDIR: u8 = TMKDIR + 1;
const TRENAMEAT: u8 = 74;
const RRENAMEAT: u8 = TRENAMEAT + 1;
const TUNLINKAT: u8 = 76;
const RUNLINKAT: u8 = TUNLINKAT + 1;
const TVERSION: u8 = 100;
const RVERSION: u8 = TVERSION + 1;
const TAUTH: u8 = 102;
const RAUTH: u8 = TAUTH + 1;
const TATTACH: u8 = 104;
const RATTACH: u8 = TATTACH + 1;
const _TERROR: u8 = 106;
const _RERROR: u8 = _TERROR + 1;
const TFLUSH: u8 = 108;
const RFLUSH: u8 = TFLUSH + 1;
const TWALK: u8 = 110;
const RWALK: u8 = TWALK + 1;
const _TOPEN: u8 = 112;
const _ROPEN: u8 = _TOPEN + 1;
const _TCREATE: u8 = 114;
const _RCREATE: u8 = _TCREATE + 1;
const TREAD: u8 = 116;
const RREAD: u8 = TREAD + 1;
const TWRITE: u8 = 118;
const RWRITE: u8 = TWRITE + 1;
const TCLUNK: u8 = 120;
const RCLUNK: u8 = TCLUNK + 1;
const TREMOVE: u8 = 122;
const RREMOVE: u8 = TREMOVE + 1;
const _TSTAT: u8 = 124;
const _RSTAT: u8 = _TSTAT + 1;
const _TWSTAT: u8 = 126;
const _RWSTAT: u8 = _TWSTAT + 1;

/// A message sent from a 9P client to a 9P server.
#[derive(Debug)]
pub enum Tmessage {
    Version(Tversion),
    Flush(Tflush),
    Walk(Twalk),
    Read(Tread),
    Write(Twrite),
    Clunk(Tclunk),
    Remove(Tremove),
    Attach(Tattach),
    Auth(Tauth),
    Statfs(Tstatfs),
    Lopen(Tlopen),
    Lcreate(Tlcreate),
    Symlink(Tsymlink),
    Mknod(Tmknod),
    Rename(Trename),
    Readlink(Treadlink),
    GetAttr(Tgetattr),
    SetAttr(Tsetattr),
    XattrWalk(Txattrwalk),
    XattrCreate(Txattrcreate),
    Readdir(Treaddir),
    Fsync(Tfsync),
    Lock(Tlock),
    GetLock(Tgetlock),
    Link(Tlink),
    Mkdir(Tmkdir),
    RenameAt(Trenameat),
    UnlinkAt(Tunlinkat),
}

#[derive(Debug)]
pub struct Tframe {
    pub tag: u16,
    pub msg: io::Result<Tmessage>,
}

impl WireFormat for Tframe {
    fn byte_size(&self) -> u32 {
        let msg = self
            .msg
            .as_ref()
            .expect("tried to encode Tframe with invalid msg");
        let msg_size = match msg {
            Tmessage::Version(ref version) => version.byte_size(),
            Tmessage::Flush(ref flush) => flush.byte_size(),
            Tmessage::Walk(ref walk) => walk.byte_size(),
            Tmessage::Read(ref read) => read.byte_size(),
            Tmessage::Write(ref write) => write.byte_size(),
            Tmessage::Clunk(ref clunk) => clunk.byte_size(),
            Tmessage::Remove(ref remove) => remove.byte_size(),
            Tmessage::Attach(ref attach) => attach.byte_size(),
            Tmessage::Auth(ref auth) => auth.byte_size(),
            Tmessage::Statfs(ref statfs) => statfs.byte_size(),
            Tmessage::Lopen(ref lopen) => lopen.byte_size(),
            Tmessage::Lcreate(ref lcreate) => lcreate.byte_size(),
            Tmessage::Symlink(ref symlink) => symlink.byte_size(),
            Tmessage::Mknod(ref mknod) => mknod.byte_size(),
            Tmessage::Rename(ref rename) => rename.byte_size(),
            Tmessage::Readlink(ref readlink) => readlink.byte_size(),
            Tmessage::GetAttr(ref getattr) => getattr.byte_size(),
            Tmessage::SetAttr(ref setattr) => setattr.byte_size(),
            Tmessage::XattrWalk(ref xattrwalk) => xattrwalk.byte_size(),
            Tmessage::XattrCreate(ref xattrcreate) => xattrcreate.byte_size(),
            Tmessage::Readdir(ref readdir) => readdir.byte_size(),
            Tmessage::Fsync(ref fsync) => fsync.byte_size(),
            Tmessage::Lock(ref lock) => lock.byte_size(),
            Tmessage::GetLock(ref getlock) => getlock.byte_size(),
            Tmessage::Link(ref link) => link.byte_size(),
            Tmessage::Mkdir(ref mkdir) => mkdir.byte_size(),
            Tmessage::RenameAt(ref renameat) => renameat.byte_size(),
            Tmessage::UnlinkAt(ref unlinkat) => unlinkat.byte_size(),
        };

        // size + type + tag + message size
        (mem::size_of::<u32>() + mem::size_of::<u8>() + mem::size_of::<u16>()) as u32 + msg_size
    }

    fn encode<W: Write>(&self, writer: &mut W) -> io::Result<()> {
        let msg = match self.msg.as_ref() {
            Ok(msg) => msg,
            Err(_) => {
                return Err(io::Error::new(
                    io::ErrorKind::InvalidData,
                    "tried to encode Tframe with invalid msg",
                ))
            }
        };

        self.byte_size().encode(writer)?;

        let ty = match msg {
            Tmessage::Version(_) => TVERSION,
            Tmessage::Flush(_) => TFLUSH,
            Tmessage::Walk(_) => TWALK,
            Tmessage::Read(_) => TREAD,
            Tmessage::Write(_) => TWRITE,
            Tmessage::Clunk(_) => TCLUNK,
            Tmessage::Remove(_) => TREMOVE,
            Tmessage::Attach(_) => TATTACH,
            Tmessage::Auth(_) => TAUTH,
            Tmessage::Statfs(_) => TSTATFS,
            Tmessage::Lopen(_) => TLOPEN,
            Tmessage::Lcreate(_) => TLCREATE,
            Tmessage::Symlink(_) => TSYMLINK,
            Tmessage::Mknod(_) => TMKNOD,
            Tmessage::Rename(_) => TRENAME,
            Tmessage::Readlink(_) => TREADLINK,
            Tmessage::GetAttr(_) => TGETATTR,
            Tmessage::SetAttr(_) => TSETATTR,
            Tmessage::XattrWalk(_) => TXATTRWALK,
            Tmessage::XattrCreate(_) => TXATTRCREATE,
            Tmessage::Readdir(_) => TREADDIR,
            Tmessage::Fsync(_) => TFSYNC,
            Tmessage::Lock(_) => TLOCK,
            Tmessage::GetLock(_) => TGETLOCK,
            Tmessage::Link(_) => TLINK,
            Tmessage::Mkdir(_) => TMKDIR,
            Tmessage::RenameAt(_) => TRENAMEAT,
            Tmessage::UnlinkAt(_) => TUNLINKAT,
        };

        ty.encode(writer)?;
        self.tag.encode(writer)?;

        match msg {
            Tmessage::Version(ref version) => version.encode(writer),
            Tmessage::Flush(ref flush) => flush.encode(writer),
            Tmessage::Walk(ref walk) => walk.encode(writer),
            Tmessage::Read(ref read) => read.encode(writer),
            Tmessage::Write(ref write) => write.encode(writer),
            Tmessage::Clunk(ref clunk) => clunk.encode(writer),
            Tmessage::Remove(ref remove) => remove.encode(writer),
            Tmessage::Attach(ref attach) => attach.encode(writer),
            Tmessage::Auth(ref auth) => auth.encode(writer),
            Tmessage::Statfs(ref statfs) => statfs.encode(writer),
            Tmessage::Lopen(ref lopen) => lopen.encode(writer),
            Tmessage::Lcreate(ref lcreate) => lcreate.encode(writer),
            Tmessage::Symlink(ref symlink) => symlink.encode(writer),
            Tmessage::Mknod(ref mknod) => mknod.encode(writer),
            Tmessage::Rename(ref rename) => rename.encode(writer),
            Tmessage::Readlink(ref readlink) => readlink.encode(writer),
            Tmessage::GetAttr(ref getattr) => getattr.encode(writer),
            Tmessage::SetAttr(ref setattr) => setattr.encode(writer),
            Tmessage::XattrWalk(ref xattrwalk) => xattrwalk.encode(writer),
            Tmessage::XattrCreate(ref xattrcreate) => xattrcreate.encode(writer),
            Tmessage::Readdir(ref readdir) => readdir.encode(writer),
            Tmessage::Fsync(ref fsync) => fsync.encode(writer),
            Tmessage::Lock(ref lock) => lock.encode(writer),
            Tmessage::GetLock(ref getlock) => getlock.encode(writer),
            Tmessage::Link(ref link) => link.encode(writer),
            Tmessage::Mkdir(ref mkdir) => mkdir.encode(writer),
            Tmessage::RenameAt(ref renameat) => renameat.encode(writer),
            Tmessage::UnlinkAt(ref unlinkat) => unlinkat.encode(writer),
        }
    }

    fn decode<R: Read>(reader: &mut R) -> io::Result<Self> {
        let byte_size: u32 = WireFormat::decode(reader)?;

        // byte_size includes the size of byte_size so remove that from the
        // expected length of the message.  Also make sure that byte_size is at least
        // that long to begin with.
        if byte_size < mem::size_of::<u32>() as u32 {
            return Err(io::Error::new(
                ErrorKind::InvalidData,
                format!("byte_size(= {}) is less than 4 bytes", byte_size),
            ));
        }

        let reader = &mut reader.take((byte_size - mem::size_of::<u32>() as u32) as u64);

        let mut ty = [0u8];
        reader.read_exact(&mut ty)?;

        let tag: u16 = WireFormat::decode(reader)?;
        let msg = Self::decode_message(reader, ty[0]);

        Ok(Tframe { tag, msg })
    }
}

impl Tframe {
    fn decode_message<R: Read>(reader: &mut R, ty: u8) -> io::Result<Tmessage> {
        match ty {
            TVERSION => Ok(Tmessage::Version(WireFormat::decode(reader)?)),
            TFLUSH => Ok(Tmessage::Flush(WireFormat::decode(reader)?)),
            TWALK => Ok(Tmessage::Walk(WireFormat::decode(reader)?)),
            TREAD => Ok(Tmessage::Read(WireFormat::decode(reader)?)),
            TWRITE => Ok(Tmessage::Write(WireFormat::decode(reader)?)),
            TCLUNK => Ok(Tmessage::Clunk(WireFormat::decode(reader)?)),
            TREMOVE => Ok(Tmessage::Remove(WireFormat::decode(reader)?)),
            TATTACH => Ok(Tmessage::Attach(WireFormat::decode(reader)?)),
            TAUTH => Ok(Tmessage::Auth(WireFormat::decode(reader)?)),
            TSTATFS => Ok(Tmessage::Statfs(WireFormat::decode(reader)?)),
            TLOPEN => Ok(Tmessage::Lopen(WireFormat::decode(reader)?)),
            TLCREATE => Ok(Tmessage::Lcreate(WireFormat::decode(reader)?)),
            TSYMLINK => Ok(Tmessage::Symlink(WireFormat::decode(reader)?)),
            TMKNOD => Ok(Tmessage::Mknod(WireFormat::decode(reader)?)),
            TRENAME => Ok(Tmessage::Rename(WireFormat::decode(reader)?)),
            TREADLINK => Ok(Tmessage::Readlink(WireFormat::decode(reader)?)),
            TGETATTR => Ok(Tmessage::GetAttr(WireFormat::decode(reader)?)),
            TSETATTR => Ok(Tmessage::SetAttr(WireFormat::decode(reader)?)),
            TXATTRWALK => Ok(Tmessage::XattrWalk(WireFormat::decode(reader)?)),
            TXATTRCREATE => Ok(Tmessage::XattrCreate(WireFormat::decode(reader)?)),
            TREADDIR => Ok(Tmessage::Readdir(WireFormat::decode(reader)?)),
            TFSYNC => Ok(Tmessage::Fsync(WireFormat::decode(reader)?)),
            TLOCK => Ok(Tmessage::Lock(WireFormat::decode(reader)?)),
            TGETLOCK => Ok(Tmessage::GetLock(WireFormat::decode(reader)?)),
            TLINK => Ok(Tmessage::Link(WireFormat::decode(reader)?)),
            TMKDIR => Ok(Tmessage::Mkdir(WireFormat::decode(reader)?)),
            TRENAMEAT => Ok(Tmessage::RenameAt(WireFormat::decode(reader)?)),
            TUNLINKAT => Ok(Tmessage::UnlinkAt(WireFormat::decode(reader)?)),
            err => Err(io::Error::new(
                ErrorKind::InvalidData,
                format!("unknown message type {}", err),
            )),
        }
    }
}

#[derive(Debug, P9WireFormat)]
pub struct Tversion {
    pub msize: u32,
    pub version: String,
}

#[derive(Debug, P9WireFormat)]
pub struct Tflush {
    pub oldtag: u16,
}

#[derive(Debug, P9WireFormat)]
pub struct Twalk {
    pub fid: u32,
    pub newfid: u32,
    pub wnames: Vec<String>,
}

#[derive(Debug, P9WireFormat)]
pub struct Tread {
    pub fid: u32,
    pub offset: u64,
    pub count: u32,
}

#[derive(Debug, P9WireFormat)]
pub struct Twrite {
    pub fid: u32,
    pub offset: u64,
    pub data: Data,
}

#[derive(Debug, P9WireFormat)]
pub struct Tclunk {
    pub fid: u32,
}

#[derive(Debug, P9WireFormat)]
pub struct Tremove {
    pub fid: u32,
}

#[derive(Debug, P9WireFormat)]
pub struct Tauth {
    pub afid: u32,
    pub uname: String,
    pub aname: String,
    pub n_uname: u32,
}

#[derive(Debug, P9WireFormat)]
pub struct Tattach {
    pub fid: u32,
    pub afid: u32,
    pub uname: String,
    pub aname: String,
    pub n_uname: u32,
}

#[derive(Debug, P9WireFormat)]
pub struct Tstatfs {
    pub fid: u32,
}

#[derive(Debug, P9WireFormat)]
pub struct Tlopen {
    pub fid: u32,
    pub flags: u32,
}

#[derive(Debug, P9WireFormat)]
pub struct Tlcreate {
    pub fid: u32,
    pub name: String,
    pub flags: u32,
    pub mode: u32,
    pub gid: u32,
}

#[derive(Debug, P9WireFormat)]
pub struct Tsymlink {
    pub fid: u32,
    pub name: String,
    pub symtgt: String,
    pub gid: u32,
}

#[derive(Debug, P9WireFormat)]
pub struct Tmknod {
    pub dfid: u32,
    pub name: String,
    pub mode: u32,
    pub major: u32,
    pub minor: u32,
    pub gid: u32,
}

#[derive(Debug, P9WireFormat)]
pub struct Trename {
    pub fid: u32,
    pub dfid: u32,
    pub name: String,
}

#[derive(Debug, P9WireFormat)]
pub struct Treadlink {
    pub fid: u32,
}

#[derive(Debug, P9WireFormat)]
pub struct Tgetattr {
    pub fid: u32,
    pub request_mask: u64,
}

#[derive(Debug, P9WireFormat)]
pub struct Tsetattr {
    pub fid: u32,
    pub valid: u32,
    pub mode: u32,
    pub uid: u32,
    pub gid: u32,
    pub size: u64,
    pub atime_sec: u64,
    pub atime_nsec: u64,
    pub mtime_sec: u64,
    pub mtime_nsec: u64,
}

#[derive(Debug, P9WireFormat)]
pub struct Txattrwalk {
    pub fid: u32,
    pub newfid: u32,
    pub name: String,
}

#[derive(Debug, P9WireFormat)]
pub struct Txattrcreate {
    pub fid: u32,
    pub name: String,
    pub attr_size: u64,
    pub flags: u32,
}

#[derive(Debug, P9WireFormat)]
pub struct Treaddir {
    pub fid: u32,
    pub offset: u64,
    pub count: u32,
}

#[derive(Debug, P9WireFormat)]
pub struct Tfsync {
    pub fid: u32,
    pub datasync: u32,
}

#[derive(Debug, P9WireFormat)]
pub struct Tlock {
    pub fid: u32,
    pub type_: u8,
    pub flags: u32,
    pub start: u64,
    pub length: u64,
    pub proc_id: u32,
    pub client_id: String,
}

#[derive(Debug, P9WireFormat)]
pub struct Tgetlock {
    pub fid: u32,
    pub type_: u8,
    pub start: u64,
    pub length: u64,
    pub proc_id: u32,
    pub client_id: String,
}

#[derive(Debug, P9WireFormat)]
pub struct Tlink {
    pub dfid: u32,
    pub fid: u32,
    pub name: String,
}

#[derive(Debug, P9WireFormat)]
pub struct Tmkdir {
    pub dfid: u32,
    pub name: String,
    pub mode: u32,
    pub gid: u32,
}

#[derive(Debug, P9WireFormat)]
pub struct Trenameat {
    pub olddirfid: u32,
    pub oldname: String,
    pub newdirfid: u32,
    pub newname: String,
}

#[derive(Debug, P9WireFormat)]
pub struct Tunlinkat {
    pub dirfd: u32,
    pub name: String,
    pub flags: u32,
}

/// A message sent from a 9P server to a 9P client in response to a request from
/// that client.  Encapsulates a full frame.
#[derive(Debug)]
pub enum Rmessage {
    Version(Rversion),
    Flush,
    Walk(Rwalk),
    Read(Rread),
    Write(Rwrite),
    Clunk,
    Remove,
    Attach(Rattach),
    Auth(Rauth),
    Statfs(Rstatfs),
    Lopen(Rlopen),
    Lcreate(Rlcreate),
    Symlink(Rsymlink),
    Mknod(Rmknod),
    Rename,
    Readlink(Rreadlink),
    GetAttr(Rgetattr),
    SetAttr,
    XattrWalk(Rxattrwalk),
    XattrCreate,
    Readdir(Rreaddir),
    Fsync,
    Lock(Rlock),
    GetLock(Rgetlock),
    Link,
    Mkdir(Rmkdir),
    RenameAt,
    UnlinkAt,
    Lerror(Rlerror),
}

#[derive(Debug)]
pub struct Rframe {
    pub tag: u16,
    pub msg: Rmessage,
}

impl WireFormat for Rframe {
    fn byte_size(&self) -> u32 {
        let msg_size = match self.msg {
            Rmessage::Version(ref version) => version.byte_size(),
            Rmessage::Flush => 0,
            Rmessage::Walk(ref walk) => walk.byte_size(),
            Rmessage::Read(ref read) => read.byte_size(),
            Rmessage::Write(ref write) => write.byte_size(),
            Rmessage::Clunk => 0,
            Rmessage::Remove => 0,
            Rmessage::Attach(ref attach) => attach.byte_size(),
            Rmessage::Auth(ref auth) => auth.byte_size(),
            Rmessage::Statfs(ref statfs) => statfs.byte_size(),
            Rmessage::Lopen(ref lopen) => lopen.byte_size(),
            Rmessage::Lcreate(ref lcreate) => lcreate.byte_size(),
            Rmessage::Symlink(ref symlink) => symlink.byte_size(),
            Rmessage::Mknod(ref mknod) => mknod.byte_size(),
            Rmessage::Rename => 0,
            Rmessage::Readlink(ref readlink) => readlink.byte_size(),
            Rmessage::GetAttr(ref getattr) => getattr.byte_size(),
            Rmessage::SetAttr => 0,
            Rmessage::XattrWalk(ref xattrwalk) => xattrwalk.byte_size(),
            Rmessage::XattrCreate => 0,
            Rmessage::Readdir(ref readdir) => readdir.byte_size(),
            Rmessage::Fsync => 0,
            Rmessage::Lock(ref lock) => lock.byte_size(),
            Rmessage::GetLock(ref getlock) => getlock.byte_size(),
            Rmessage::Link => 0,
            Rmessage::Mkdir(ref mkdir) => mkdir.byte_size(),
            Rmessage::RenameAt => 0,
            Rmessage::UnlinkAt => 0,
            Rmessage::Lerror(ref lerror) => lerror.byte_size(),
        };

        // size + type + tag + message size
        (mem::size_of::<u32>() + mem::size_of::<u8>() + mem::size_of::<u16>()) as u32 + msg_size
    }

    fn encode<W: Write>(&self, writer: &mut W) -> io::Result<()> {
        self.byte_size().encode(writer)?;

        let ty = match self.msg {
            Rmessage::Version(_) => RVERSION,
            Rmessage::Flush => RFLUSH,
            Rmessage::Walk(_) => RWALK,
            Rmessage::Read(_) => RREAD,
            Rmessage::Write(_) => RWRITE,
            Rmessage::Clunk => RCLUNK,
            Rmessage::Remove => RREMOVE,
            Rmessage::Attach(_) => RATTACH,
            Rmessage::Auth(_) => RAUTH,
            Rmessage::Statfs(_) => RSTATFS,
            Rmessage::Lopen(_) => RLOPEN,
            Rmessage::Lcreate(_) => RLCREATE,
            Rmessage::Symlink(_) => RSYMLINK,
            Rmessage::Mknod(_) => RMKNOD,
            Rmessage::Rename => RRENAME,
            Rmessage::Readlink(_) => RREADLINK,
            Rmessage::GetAttr(_) => RGETATTR,
            Rmessage::SetAttr => RSETATTR,
            Rmessage::XattrWalk(_) => RXATTRWALK,
            Rmessage::XattrCreate => RXATTRCREATE,
            Rmessage::Readdir(_) => RREADDIR,
            Rmessage::Fsync => RFSYNC,
            Rmessage::Lock(_) => RLOCK,
            Rmessage::GetLock(_) => RGETLOCK,
            Rmessage::Link => RLINK,
            Rmessage::Mkdir(_) => RMKDIR,
            Rmessage::RenameAt => RRENAMEAT,
            Rmessage::UnlinkAt => RUNLINKAT,
            Rmessage::Lerror(_) => RLERROR,
        };

        ty.encode(writer)?;
        self.tag.encode(writer)?;

        match self.msg {
            Rmessage::Version(ref version) => version.encode(writer),
            Rmessage::Flush => Ok(()),
            Rmessage::Walk(ref walk) => walk.encode(writer),
            Rmessage::Read(ref read) => read.encode(writer),
            Rmessage::Write(ref write) => write.encode(writer),
            Rmessage::Clunk => Ok(()),
            Rmessage::Remove => Ok(()),
            Rmessage::Attach(ref attach) => attach.encode(writer),
            Rmessage::Auth(ref auth) => auth.encode(writer),
            Rmessage::Statfs(ref statfs) => statfs.encode(writer),
            Rmessage::Lopen(ref lopen) => lopen.encode(writer),
            Rmessage::Lcreate(ref lcreate) => lcreate.encode(writer),
            Rmessage::Symlink(ref symlink) => symlink.encode(writer),
            Rmessage::Mknod(ref mknod) => mknod.encode(writer),
            Rmessage::Rename => Ok(()),
            Rmessage::Readlink(ref readlink) => readlink.encode(writer),
            Rmessage::GetAttr(ref getattr) => getattr.encode(writer),
            Rmessage::SetAttr => Ok(()),
            Rmessage::XattrWalk(ref xattrwalk) => xattrwalk.encode(writer),
            Rmessage::XattrCreate => Ok(()),
            Rmessage::Readdir(ref readdir) => readdir.encode(writer),
            Rmessage::Fsync => Ok(()),
            Rmessage::Lock(ref lock) => lock.encode(writer),
            Rmessage::GetLock(ref getlock) => getlock.encode(writer),
            Rmessage::Link => Ok(()),
            Rmessage::Mkdir(ref mkdir) => mkdir.encode(writer),
            Rmessage::RenameAt => Ok(()),
            Rmessage::UnlinkAt => Ok(()),
            Rmessage::Lerror(ref lerror) => lerror.encode(writer),
        }
    }

    fn decode<R: Read>(reader: &mut R) -> io::Result<Self> {
        let byte_size: u32 = WireFormat::decode(reader)?;

        // byte_size includes the size of byte_size so remove that from the
        // expected length of the message.
        let reader = &mut reader.take((byte_size - mem::size_of::<u32>() as u32) as u64);

        let mut ty = [0u8];
        reader.read_exact(&mut ty)?;

        let tag: u16 = WireFormat::decode(reader)?;

        let msg = match ty[0] {
            RVERSION => Ok(Rmessage::Version(WireFormat::decode(reader)?)),
            RFLUSH => Ok(Rmessage::Flush),
            RWALK => Ok(Rmessage::Walk(WireFormat::decode(reader)?)),
            RREAD => Ok(Rmessage::Read(WireFormat::decode(reader)?)),
            RWRITE => Ok(Rmessage::Write(WireFormat::decode(reader)?)),
            RCLUNK => Ok(Rmessage::Clunk),
            RREMOVE => Ok(Rmessage::Remove),
            RATTACH => Ok(Rmessage::Attach(WireFormat::decode(reader)?)),
            RAUTH => Ok(Rmessage::Auth(WireFormat::decode(reader)?)),
            RSTATFS => Ok(Rmessage::Statfs(WireFormat::decode(reader)?)),
            RLOPEN => Ok(Rmessage::Lopen(WireFormat::decode(reader)?)),
            RLCREATE => Ok(Rmessage::Lcreate(WireFormat::decode(reader)?)),
            RSYMLINK => Ok(Rmessage::Symlink(WireFormat::decode(reader)?)),
            RMKNOD => Ok(Rmessage::Mknod(WireFormat::decode(reader)?)),
            RRENAME => Ok(Rmessage::Rename),
            RREADLINK => Ok(Rmessage::Readlink(WireFormat::decode(reader)?)),
            RGETATTR => Ok(Rmessage::GetAttr(WireFormat::decode(reader)?)),
            RSETATTR => Ok(Rmessage::SetAttr),
            RXATTRWALK => Ok(Rmessage::XattrWalk(WireFormat::decode(reader)?)),
            RXATTRCREATE => Ok(Rmessage::XattrCreate),
            RREADDIR => Ok(Rmessage::Readdir(WireFormat::decode(reader)?)),
            RFSYNC => Ok(Rmessage::Fsync),
            RLOCK => Ok(Rmessage::Lock(WireFormat::decode(reader)?)),
            RGETLOCK => Ok(Rmessage::GetLock(WireFormat::decode(reader)?)),
            RLINK => Ok(Rmessage::Link),
            RMKDIR => Ok(Rmessage::Mkdir(WireFormat::decode(reader)?)),
            RRENAMEAT => Ok(Rmessage::RenameAt),
            RUNLINKAT => Ok(Rmessage::UnlinkAt),
            RLERROR => Ok(Rmessage::Lerror(WireFormat::decode(reader)?)),
            err => Err(io::Error::new(
                ErrorKind::InvalidData,
                format!("unknown message type {}", err),
            )),
        }?;

        Ok(Rframe { tag, msg })
    }
}

#[derive(Debug, Copy, Clone, P9WireFormat)]
pub struct Qid {
    pub ty: u8,
    pub version: u32,
    pub path: u64,
}

#[derive(Debug, P9WireFormat)]
pub struct Dirent {
    pub qid: Qid,
    pub offset: u64,
    pub ty: u8,
    pub name: String,
}

#[derive(Debug, P9WireFormat)]
pub struct Rversion {
    pub msize: u32,
    pub version: String,
}

#[derive(Debug, P9WireFormat)]
pub struct Rwalk {
    pub wqids: Vec<Qid>,
}

#[derive(Debug, P9WireFormat)]
pub struct Rread {
    pub data: Data,
}

#[derive(Debug, P9WireFormat)]
pub struct Rwrite {
    pub count: u32,
}

#[derive(Debug, P9WireFormat)]
pub struct Rauth {
    pub aqid: Qid,
}

#[derive(Debug, P9WireFormat)]
pub struct Rattach {
    pub qid: Qid,
}

#[derive(Debug, P9WireFormat)]
pub struct Rlerror {
    pub ecode: u32,
}

#[derive(Debug, P9WireFormat)]
pub struct Rstatfs {
    pub ty: u32,
    pub bsize: u32,
    pub blocks: u64,
    pub bfree: u64,
    pub bavail: u64,
    pub files: u64,
    pub ffree: u64,
    pub fsid: u64,
    pub namelen: u32,
}

#[derive(Debug, P9WireFormat)]
pub struct Rlopen {
    pub qid: Qid,
    pub iounit: u32,
}

#[derive(Debug, P9WireFormat)]
pub struct Rlcreate {
    pub qid: Qid,
    pub iounit: u32,
}

#[derive(Debug, P9WireFormat)]
pub struct Rsymlink {
    pub qid: Qid,
}

#[derive(Debug, P9WireFormat)]
pub struct Rmknod {
    pub qid: Qid,
}

#[derive(Debug, P9WireFormat)]
pub struct Rreadlink {
    pub target: String,
}

#[derive(Debug, P9WireFormat)]
pub struct Rgetattr {
    pub valid: u64,
    pub qid: Qid,
    pub mode: u32,
    pub uid: u32,
    pub gid: u32,
    pub nlink: u64,
    pub rdev: u64,
    pub size: u64,
    pub blksize: u64,
    pub blocks: u64,
    pub atime_sec: u64,
    pub atime_nsec: u64,
    pub mtime_sec: u64,
    pub mtime_nsec: u64,
    pub ctime_sec: u64,
    pub ctime_nsec: u64,
    pub btime_sec: u64,
    pub btime_nsec: u64,
    pub gen: u64,
    pub data_version: u64,
}

#[derive(Debug, P9WireFormat)]
pub struct Rxattrwalk {
    pub size: u64,
}

#[derive(Debug, P9WireFormat)]
pub struct Rreaddir {
    pub data: Data,
}

#[derive(Debug, P9WireFormat)]
pub struct Rlock {
    pub status: u8,
}

#[derive(Debug, P9WireFormat)]
pub struct Rgetlock {
    pub type_: u8,
    pub start: u64,
    pub length: u64,
    pub proc_id: u32,
    pub client_id: String,
}

#[derive(Debug, P9WireFormat)]
pub struct Rmkdir {
    pub qid: Qid,
}