-
Notifications
You must be signed in to change notification settings - Fork 18
/
serialize.rs
662 lines (584 loc) · 26.3 KB
/
serialize.rs
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
//! Serialize/deserialize 9P messages into/from binary.
extern crate num;
extern crate byteorder;
use fcall::*;
use std::mem;
use std::ops::{Shl, Shr, Carrier};
use std::io::{Read, Cursor, Result};
use self::num::FromPrimitive;
use self::byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt};
macro_rules! decode {
($decoder:expr) => { Decodable::decode(&mut $decoder)? };
($typ:ident, $buf:expr) => { $typ::from_bits_truncate(decode!($buf)) };
}
// Create an unintialized buffer
// Safe to use only for writing data to it
fn create_buffer(size: usize) -> Vec<u8> {
let mut buffer = Vec::with_capacity(size);
unsafe { buffer.set_len(size); }
buffer
}
fn read_exact<R: Read + ?Sized>(r: &mut R, size: usize) -> Result<Vec<u8>> {
let mut buf = create_buffer(size);
r.read_exact(&mut buf[..]).and(Ok(buf))
}
/// A serializing specific result to overload operators on `Result`
///
/// # Overloaded operators
/// <<, >>, ?
pub struct SResult<T>(::std::io::Result<T>);
impl<U> Carrier for SResult<U> {
type Success = U;
type Error = ::std::io::Error;
fn from_success(u: Self::Success) -> SResult<U> {
SResult(Ok(u))
}
fn from_error(e: Self::Error) -> SResult<U> {
SResult(Err(e))
}
fn translate<T>(self) -> T
where T: Carrier<Success=U, Error=Self::Error>
{
match self.0 {
Ok(u) => T::from_success(u),
Err(e) => T::from_error(e),
}
}
}
macro_rules! stry {
($res:expr) => {
match $res {
Ok(v) => v,
Err(e) => return ::serialize::SResult(Err(From::from(e))),
}
}
}
/// A wrapper class of WriteBytesExt to provide operator overloads
/// for serializing
///
/// Operator '<<' serializes the right hand side argument into
/// the left hand side encoder
#[derive(Clone, Debug)]
pub struct Encoder<W> {
writer: W,
bytes: usize
}
impl<W: WriteBytesExt> Encoder<W> {
pub fn new(writer: W) -> Encoder<W> {
Encoder { writer: writer, bytes: 0 }
}
/// Return total bytes written
pub fn bytes_written(&self) -> usize { self.bytes }
/// Encode data, equivalent to: decoder << data
pub fn encode<T: Encodable,>(&mut self, data: &T) -> Result<usize> {
let bytes = data.encode(&mut self.writer)?;
self.bytes += bytes;
Ok(bytes)
}
/// Get inner writer
pub fn into_inner(self) -> W { self.writer }
}
impl<'a, T: Encodable, W: WriteBytesExt> Shl<&'a T> for Encoder<W> {
type Output = SResult<Encoder<W>>;
fn shl(mut self, rhs: &'a T) -> Self::Output {
stry!(self.encode(rhs));
SResult(Ok(self))
}
}
impl<'a, T: Encodable, W: WriteBytesExt> Shl<&'a T> for SResult<Encoder<W>> {
type Output = Self;
fn shl(self, rhs: &'a T) -> Self::Output {
let mut encoder = stry!(self.0);
stry!(encoder.encode(rhs));
SResult(Ok(encoder))
}
}
/// A wrapper class of ReadBytesExt to provide operator overloads
/// for deserializing
#[derive(Clone, Debug)]
pub struct Decoder<R> {
reader: R,
}
impl<R: ReadBytesExt> Decoder<R> {
pub fn new(reader: R) -> Decoder<R> { Decoder { reader: reader } }
pub fn decode<T: Decodable,>(&mut self) -> Result<T> {
Decodable::decode(&mut self.reader)
}
/// Get inner reader
pub fn into_inner(self) -> R { self.reader }
}
impl<'a, T: Decodable, R: ReadBytesExt> Shr<&'a mut T> for Decoder<R> {
type Output = SResult<Decoder<R>>;
fn shr(mut self, rhs: &'a mut T) -> Self::Output {
*rhs = stry!(self.decode());
SResult(Ok(self))
}
}
impl<'a, T: Decodable, R: ReadBytesExt> Shr<&'a mut T> for SResult<Decoder<R>> {
type Output = Self;
fn shr(self, rhs: &'a mut T) -> Self::Output {
let mut decoder = stry!(self.0);
*rhs = stry!(decoder.decode());
SResult(Ok(decoder))
}
}
/// Trait representing a type which can be serialized into binary
pub trait Encodable {
/// Encode self to w and returns the number of bytes encoded
fn encode<W: WriteBytesExt>(&self, w: &mut W) -> Result<usize>;
}
impl Encodable for u8 {
fn encode<W: WriteBytesExt>(&self, w: &mut W) -> Result<usize> {
w.write_u8(*self).and(Ok(mem::size_of::<Self>()))
}
}
impl Encodable for u16 {
fn encode<W: WriteBytesExt>(&self, w: &mut W) -> Result<usize> {
w.write_u16::<LittleEndian>(*self).and(Ok(mem::size_of::<Self>()))
}
}
impl Encodable for u32 {
fn encode<W: WriteBytesExt>(&self, w: &mut W) -> Result<usize> {
w.write_u32::<LittleEndian>(*self).and(Ok(mem::size_of::<Self>()))
}
}
impl Encodable for u64 {
fn encode<W: WriteBytesExt>(&self, w: &mut W) -> Result<usize> {
w.write_u64::<LittleEndian>(*self).and(Ok(mem::size_of::<Self>()))
}
}
impl Encodable for String {
fn encode<W: WriteBytesExt>(&self, w: &mut W) -> Result<usize> {
let mut bytes = (self.len() as u16).encode(w)?;
bytes += w.write_all(self.as_bytes()).and(Ok(self.len()))?;
Ok(bytes)
}
}
impl Encodable for Qid {
fn encode<W: WriteBytesExt>(&self, w: &mut W) -> Result<usize> {
Ok((Encoder::new(w) << &self.typ.bits() << &self.version << &self.path)?.bytes_written())
}
}
impl Encodable for Statfs {
fn encode<W: WriteBytesExt>(&self, w: &mut W) -> Result<usize> {
Ok((Encoder::new(w)
<< &self.typ << &self.bsize << &self.blocks
<< &self.bfree << &self.bavail << &self.files
<< &self.ffree << &self.fsid << &self.namelen
)?.bytes_written())
}
}
impl Encodable for Time {
fn encode<W: WriteBytesExt>(&self, w: &mut W) -> Result<usize> {
Ok((Encoder::new(w) << &self.sec << &self.nsec)?.bytes_written())
}
}
impl Encodable for Stat {
fn encode<W: WriteBytesExt>(&self, w: &mut W) -> Result<usize> {
Ok((Encoder::new(w)
<< &self.mode << &self.uid << &self.gid
<< &self.nlink << &self.rdev << &self.size
<< &self.blksize << &self.blocks << &self.atime
<< &self.mtime << &self.ctime
)?.bytes_written())
}
}
impl Encodable for SetAttr {
fn encode<W: WriteBytesExt>(&self, w: &mut W) -> Result<usize> {
Ok((Encoder::new(w)
<< &self.mode << &self.uid << &self.gid
<< &self.size << &self.atime << &self.mtime
)?.bytes_written())
}
}
impl Encodable for DirEntry {
fn encode<W: WriteBytesExt>(&self, w: &mut W) -> Result<usize> {
Ok((Encoder::new(w)
<< &self.qid << &self.offset << &self.typ << &self.name
)?.bytes_written())
}
}
impl Encodable for DirEntryData {
fn encode<W: WriteBytesExt>(&self, w: &mut W) -> Result<usize> {
Ok((self.data().iter()
.fold(Encoder::new(w) << &self.size(), |acc, e| acc << e)
)?.bytes_written())
}
}
impl Encodable for Data {
fn encode<W: WriteBytesExt>(&self, w: &mut W) -> Result<usize> {
let size = self.0.len();
let bytes = (size as u32).encode(w)? + size;
w.write_all(&self.0)?;
Ok(bytes)
}
}
impl Encodable for Flock {
fn encode<W: WriteBytesExt>(&self, w: &mut W) -> Result<usize> {
Ok((Encoder::new(w)
<< &self.typ.bits() << &self.flags.bits() << &self.start
<< &self.length << &self.proc_id << &self.client_id
)?.bytes_written())
}
}
impl Encodable for Getlock {
fn encode<W: WriteBytesExt>(&self, w: &mut W) -> Result<usize> {
Ok((Encoder::new(w)
<< &self.typ.bits() << &self.start << &self.length
<< &self.proc_id << &self.client_id
)?.bytes_written())
}
}
impl<T: Encodable> Encodable for Vec<T> {
fn encode<W: WriteBytesExt>(&self, w: &mut W) -> Result<usize> {
Ok((self.iter()
.fold(Encoder::new(w) << &(self.len() as u16), |acc, s| acc << s)
)?.bytes_written())
}
}
impl Encodable for Msg {
fn encode<W: WriteBytesExt>(&self, w: &mut W) -> Result<usize> {
use Fcall::*;
let typ = MsgType::from(&self.body);
let buf = Encoder::new(Vec::with_capacity(8196)) << &(0u32 /* size */) << &(typ as u8) << &self.tag;
let buf = (match self.body {
// 9P2000.L
Rlerror { ref ecode } => { buf << ecode },
Tstatfs { ref fid } => { buf << fid },
Rstatfs { ref statfs } => { buf << statfs },
Tlopen { ref fid, ref flags } => { buf << fid << flags },
Rlopen { ref qid, ref iounit } => { buf << qid << iounit },
Tlcreate { ref fid, ref name, ref flags, ref mode, ref gid } => { buf << fid << name << flags << mode << gid },
Rlcreate { ref qid, ref iounit } => { buf << qid << iounit },
Tsymlink { ref fid, ref name, ref symtgt, ref gid } => { buf << fid << name << symtgt << gid },
Rsymlink { ref qid } => { buf << qid },
Tmknod { ref dfid, ref name, ref mode, ref major, ref minor, ref gid } => { buf << dfid << name << mode << major << minor << gid },
Rmknod { ref qid } => { buf << qid },
Trename { ref fid, ref dfid, ref name } => { buf << fid << dfid << name },
Rrename => { buf },
Treadlink { ref fid } => { buf << fid },
Rreadlink { ref target } => { buf << target },
Tgetattr { ref fid, ref req_mask } => { buf << fid << &req_mask.bits() },
Rgetattr { ref valid, ref qid, ref stat } => { buf << &valid.bits() << qid << stat << &0u64 << &0u64 << &0u64 << &0u64 },
Tsetattr { ref fid, ref valid, ref stat } => { buf << fid << &valid.bits() << stat },
Rsetattr => { buf },
Txattrwalk { ref fid, ref newfid, ref name } => { buf << fid << newfid << name },
Rxattrwalk { ref size } => { buf << size },
Txattrcreate { ref fid, ref name, ref attr_size, ref flags } => { buf << fid << name << attr_size << flags },
Rxattrcreate => { buf },
Treaddir { ref fid, ref offset, ref count } => { buf << fid << offset << count },
Rreaddir { ref data } => { buf << data },
Tfsync { ref fid } => { buf << fid },
Rfsync => { buf },
Tlock { ref fid, ref flock } => { buf << fid << flock },
Rlock { ref status } => { buf << &status.bits() },
Tgetlock { ref fid, ref flock } => { buf << fid << flock },
Rgetlock { ref flock } => { buf << flock },
Tlink { ref dfid, ref fid, ref name } => { buf << dfid << fid << name },
Rlink => { buf },
Tmkdir { ref dfid, ref name, ref mode, ref gid } => { buf << dfid << name << mode << gid },
Rmkdir { ref qid } => { buf << qid },
Trenameat { ref olddirfid, ref oldname, ref newdirfid, ref newname } => { buf << olddirfid << oldname << newdirfid << newname },
Rrenameat => { buf },
Tunlinkat { ref dirfd, ref name, ref flags } => { buf << dirfd << name << flags },
Runlinkat => { buf },
// 9P2000.u
Tauth { ref afid, ref uname, ref aname, ref n_uname } => { buf << afid << uname << aname << n_uname },
Rauth { ref aqid } => { buf << aqid },
Tattach { ref fid, ref afid, ref uname, ref aname, ref n_uname } => { buf << fid << afid << uname << aname << n_uname },
Rattach { ref qid } => { buf << qid },
// 9P2000
Tversion { ref msize, ref version } => { buf << msize << version },
Rversion { ref msize, ref version } => { buf << msize << version },
Tflush { ref oldtag } => { buf << oldtag },
Rflush => { buf },
Twalk { ref fid, ref newfid, ref wnames } => { buf << fid << newfid << wnames },
Rwalk { ref wqids } => { buf << wqids },
Tread { ref fid, ref offset, ref count } => { buf << fid << offset << count },
Rread { ref data } => { buf << data },
Twrite { ref fid, ref offset, ref data } => { buf << fid << offset << data },
Rwrite { ref count } => { buf << count },
Tclunk { ref fid } => { buf << fid },
Rclunk => { buf },
Tremove { ref fid } => { buf << fid },
Rremove => { buf },
})?;
let mut vec_buffer = buf.into_inner();
let buf_size = vec_buffer.len();
unsafe { *(vec_buffer.as_mut_ptr() as *mut u32) = buf_size as u32 };
w.write_all(&vec_buffer).and(Ok(buf_size))
}
}
/// Trait representing a type which can be deserialized from binary
pub trait Decodable: Sized {
fn decode<R: ReadBytesExt>(r: &mut R) -> Result<Self>;
}
impl Decodable for u8 {
fn decode<R: ReadBytesExt>(r: &mut R) -> Result<Self> {
r.read_u8()
}
}
impl Decodable for u16 {
fn decode<R: ReadBytesExt>(r: &mut R) -> Result<Self> {
r.read_u16::<LittleEndian>()
}
}
impl Decodable for u32 {
fn decode<R: ReadBytesExt>(r: &mut R) -> Result<Self> {
r.read_u32::<LittleEndian>()
}
}
impl Decodable for u64 {
fn decode<R: ReadBytesExt>(r: &mut R) -> Result<Self> {
r.read_u64::<LittleEndian>()
}
}
impl Decodable for String {
fn decode<R: ReadBytesExt>(r: &mut R) -> Result<Self> {
let len: u16 = Decodable::decode(r)?;
String::from_utf8(read_exact(r, len as usize)?)
.or(res!(io_err!(Other, "Invalid UTF-8 sequence")))
}
}
impl Decodable for Qid {
fn decode<R: ReadBytesExt>(r: &mut R) -> Result<Self> {
Ok(Qid {
typ: decode!(QidType, *r),
version: Decodable::decode(r)?,
path: Decodable::decode(r)?
})
}
}
impl Decodable for Statfs {
fn decode<R: ReadBytesExt>(r: &mut R) -> Result<Self> {
Ok(Statfs {
typ: Decodable::decode(r)?,
bsize: Decodable::decode(r)?,
blocks: Decodable::decode(r)?,
bfree: Decodable::decode(r)?,
bavail: Decodable::decode(r)?,
files: Decodable::decode(r)?,
ffree: Decodable::decode(r)?,
fsid: Decodable::decode(r)?,
namelen: Decodable::decode(r)?,
})
}
}
impl Decodable for Time {
fn decode<R: ReadBytesExt>(r: &mut R) -> Result<Self> {
Ok(Time {
sec: Decodable::decode(r)?,
nsec: Decodable::decode(r)?,
})
}
}
impl Decodable for Stat {
fn decode<R: ReadBytesExt>(r: &mut R) -> Result<Self> {
Ok(Stat {
mode: Decodable::decode(r)?,
uid: Decodable::decode(r)?,
gid: Decodable::decode(r)?,
nlink: Decodable::decode(r)?,
rdev: Decodable::decode(r)?,
size: Decodable::decode(r)?,
blksize: Decodable::decode(r)?,
blocks: Decodable::decode(r)?,
atime: Decodable::decode(r)?,
mtime: Decodable::decode(r)?,
ctime: Decodable::decode(r)?,
})
}
}
impl Decodable for SetAttr {
fn decode<R: ReadBytesExt>(r: &mut R) -> Result<Self> {
Ok(SetAttr {
mode: Decodable::decode(r)?,
uid: Decodable::decode(r)?,
gid: Decodable::decode(r)?,
size: Decodable::decode(r)?,
atime: Decodable::decode(r)?,
mtime: Decodable::decode(r)?,
})
}
}
impl Decodable for DirEntry {
fn decode<R: ReadBytesExt>(r: &mut R) -> Result<Self> {
Ok(DirEntry {
qid: Decodable::decode(r)?,
offset: Decodable::decode(r)?,
typ: Decodable::decode(r)?,
name: Decodable::decode(r)?,
})
}
}
impl Decodable for DirEntryData {
fn decode<R: ReadBytesExt>(r: &mut R) -> Result<Self> {
let count: u32 = Decodable::decode(r)?;
let mut data: Vec<DirEntry> = Vec::with_capacity(count as usize);
for _ in 0..count {
data.push(Decodable::decode(r)?);
}
Ok(DirEntryData::with(data))
}
}
impl Decodable for Data {
fn decode<R: ReadBytesExt>(r: &mut R) -> Result<Self> {
let len: u32 = Decodable::decode(r)?;
Ok(Data(read_exact(r, len as usize)?))
}
}
impl Decodable for Flock {
fn decode<R: ReadBytesExt>(r: &mut R) -> Result<Self> {
Ok(Flock {
typ: decode!(LockType, *r),
flags: decode!(LockFlag, *r),
start: Decodable::decode(r)?,
length: Decodable::decode(r)?,
proc_id: Decodable::decode(r)?,
client_id: Decodable::decode(r)?,
})
}
}
impl Decodable for Getlock {
fn decode<R: ReadBytesExt>(r: &mut R) -> Result<Self> {
Ok(Getlock {
typ: decode!(LockType, *r),
start: Decodable::decode(r)?,
length: Decodable::decode(r)?,
proc_id: Decodable::decode(r)?,
client_id: Decodable::decode(r)?,
})
}
}
impl<T: Decodable> Decodable for Vec<T> {
fn decode<R: ReadBytesExt>(r: &mut R) -> Result<Self> {
let len: u16 = Decodable::decode(r)?;
let mut buf = Vec::new();
for _ in 0..len {
buf.push(Decodable::decode(r)?);
}
Ok(buf)
}
}
impl Decodable for Msg {
fn decode<R: ReadBytesExt>(r: &mut R) -> Result<Self> {
use MsgType::*;
let size = r.read_u32::<LittleEndian>()? - 4;
let mut buf = Cursor::new(read_exact(r, size as usize)?);
let msg_type = MsgType::from_u8(decode!(buf));
let tag = decode!(buf);
let body = match msg_type {
// 9P2000.L
Some(Rlerror) => Fcall::Rlerror { ecode: decode!(buf) },
Some(Tstatfs) => Fcall::Tstatfs { fid: decode!(buf) },
Some(Rstatfs) => Fcall::Rstatfs { statfs: decode!(buf) },
Some(Tlopen) => Fcall::Tlopen { fid: decode!(buf), flags: decode!(buf) },
Some(Rlopen) => Fcall::Rlopen { qid: decode!(buf), iounit: decode!(buf) },
Some(Tlcreate) => Fcall::Tlcreate { fid: decode!(buf), name: decode!(buf), flags: decode!(buf), mode: decode!(buf), gid: decode!(buf) },
Some(Rlcreate) => Fcall::Rlcreate { qid: decode!(buf), iounit: decode!(buf) },
Some(Tsymlink) => Fcall::Tsymlink { fid: decode!(buf), name: decode!(buf), symtgt: decode!(buf), gid: decode!(buf) },
Some(Rsymlink) => Fcall::Rsymlink { qid: decode!(buf) },
Some(Tmknod) => Fcall::Tmknod { dfid: decode!(buf), name: decode!(buf), mode: decode!(buf), major: decode!(buf), minor: decode!(buf), gid: decode!(buf) },
Some(Rmknod) => Fcall::Rmknod { qid: decode!(buf) },
Some(Trename) => Fcall::Trename { fid: decode!(buf), dfid: decode!(buf), name: decode!(buf) },
Some(Rrename) => Fcall::Rrename,
Some(Treadlink) => Fcall::Treadlink { fid: decode!(buf) },
Some(Rreadlink) => Fcall::Rreadlink { target: decode!(buf) },
Some(Tgetattr) => Fcall::Tgetattr { fid: decode!(buf), req_mask: decode!(GetattrMask, buf) },
Some(Rgetattr) => {
let r = Fcall::Rgetattr { valid: decode!(GetattrMask, buf), qid: decode!(buf), stat: decode!(buf) };
let (_btime, _gen, _ver): (Time, u64, u64) = (decode!(buf), decode!(buf), decode!(buf));
r
},
Some(Tsetattr) => Fcall::Tsetattr { fid: decode!(buf), valid: decode!(SetattrMask, buf), stat: decode!(buf) },
Some(Rsetattr) => Fcall::Rsetattr,
Some(Txattrwalk) => Fcall::Txattrwalk { fid: decode!(buf), newfid: decode!(buf), name: decode!(buf) },
Some(Rxattrwalk) => Fcall::Rxattrwalk { size: decode!(buf) },
Some(Txattrcreate) => Fcall::Txattrcreate { fid: decode!(buf), name: decode!(buf), attr_size: decode!(buf), flags: decode!(buf) },
Some(Rxattrcreate) => Fcall::Rxattrcreate,
Some(Treaddir) => Fcall::Treaddir { fid: decode!(buf), offset: decode!(buf), count: decode!(buf) },
Some(Rreaddir) => Fcall::Rreaddir { data: decode!(buf) },
Some(Tfsync) => Fcall::Tfsync { fid: decode!(buf) },
Some(Rfsync) => Fcall::Rfsync,
Some(Tlock) => Fcall::Tlock { fid: decode!(buf), flock: decode!(buf) },
Some(Rlock) => Fcall::Rlock { status: decode!(LockStatus, buf) },
Some(Tgetlock) => Fcall::Tgetlock { fid: decode!(buf), flock: decode!(buf) },
Some(Rgetlock) => Fcall::Rgetlock { flock: decode!(buf) },
Some(Tlink) => Fcall::Tlink { dfid: decode!(buf), fid: decode!(buf), name: decode!(buf) },
Some(Rlink) => Fcall::Rlink,
Some(Tmkdir) => Fcall::Tmkdir { dfid: decode!(buf), name: decode!(buf), mode: decode!(buf), gid: decode!(buf) },
Some(Rmkdir) => Fcall::Rmkdir { qid: decode!(buf) },
Some(Trenameat) => Fcall::Trenameat { olddirfid: decode!(buf), oldname: decode!(buf), newdirfid: decode!(buf), newname: decode!(buf) },
Some(Rrenameat) => Fcall::Rrenameat,
Some(Tunlinkat) => Fcall::Tunlinkat { dirfd: decode!(buf), name: decode!(buf), flags: decode!(buf) },
Some(Runlinkat) => Fcall::Runlinkat,
// 9P2000.u
Some(Tauth) => Fcall::Tauth { afid: decode!(buf), uname: decode!(buf), aname: decode!(buf), n_uname: decode!(buf) },
Some(Rauth) => Fcall::Rauth { aqid: decode!(buf) },
Some(Tattach) => Fcall::Tattach { fid: decode!(buf), afid: decode!(buf), uname: decode!(buf), aname: decode!(buf), n_uname: decode!(buf) },
Some(Rattach) => Fcall::Rattach { qid: decode!(buf) },
// 9P2000
Some(Tversion) => Fcall::Tversion { msize: decode!(buf), version: decode!(buf) },
Some(Rversion) => Fcall::Rversion { msize: decode!(buf), version: decode!(buf) },
Some(Tflush) => Fcall::Tflush { oldtag: decode!(buf) },
Some(Rflush) => Fcall::Rflush,
Some(Twalk) => Fcall::Twalk { fid: decode!(buf), newfid: decode!(buf), wnames: decode!(buf) },
Some(Rwalk) => Fcall::Rwalk { wqids: decode!(buf) },
Some(Tread) => Fcall::Tread { fid: decode!(buf), offset: decode!(buf), count: decode!(buf) },
Some(Rread) => Fcall::Rread { data: decode!(buf) },
Some(Twrite) => Fcall::Twrite { fid: decode!(buf), offset: decode!(buf), data: decode!(buf) },
Some(Rwrite) => Fcall::Rwrite { count: decode!(buf) },
Some(Tclunk) => Fcall::Tclunk { fid: decode!(buf) },
Some(Rclunk) => Fcall::Rclunk,
Some(Tremove) => Fcall::Tremove { fid: decode!(buf) },
Some(Rremove) => Fcall::Rremove,
Some(Tlerror) | None => return res!(io_err!(Other, "Invalid message type"))
};
Ok(Msg { tag: tag, body: body })
}
}
/// Helper function to read a 9P message from a byte-oriented stream
pub fn read_msg<R: ReadBytesExt>(r: &mut R) -> Result<Msg> {
Decodable::decode(r)
}
/// Helper function to write a 9P message into a byte-oriented stream
pub fn write_msg<W: WriteBytesExt>(w: &mut W, msg: &Msg) -> Result<usize> {
msg.encode(w)
}
#[test]
fn encoder_test1() {
let expected: Vec<u8> = (0..10).collect();
let mut encoder = Vec::new();
for i in 0..10 {
(&(i as u8)).encode(&mut encoder).unwrap();
}
assert_eq!(expected, encoder);
}
#[test]
fn decoder_test1() {
let expected: Vec<u8> = (0..10).collect();
let mut decoder = Cursor::new(expected.clone());
let mut actual: Vec<u8> = Vec::new();
loop {
match Decodable::decode(&mut decoder) {
Ok(i) => actual.push(i),
Err(_) => break
}
}
assert_eq!(expected, actual);
}
#[test]
fn msg_encode_decode1() {
let expected = Msg {
tag: 0xdead,
body: Fcall::Rversion {
msize: 40,
version: P92000L.to_owned()
}
};
let mut buf = Vec::new();
let _ = expected.encode(&mut buf);
let mut readbuf = Cursor::new(buf);
let actual = Decodable::decode(&mut readbuf);
assert_eq!(expected, actual.unwrap());
}