Skip to main content

fuser/
reply.rs

1//! Filesystem operation reply
2//!
3//! A reply is passed to filesystem operation implementations and must be used to send back the
4//! result of an operation. The reply can optionally be sent to another thread to asynchronously
5//! work on an operation and provide the result later. Also it allows replying with a block of
6//! data without cloning the data. A reply *must always* be used (by calling either ok() or
7//! error() exactly once).
8
9use crate::ll::{
10    self,
11    reply::{DirEntPlusList, DirEntryPlus},
12    Generation,
13};
14use crate::ll::{
15    reply::{DirEntList, DirEntOffset, DirEntry},
16    INodeNo,
17};
18use libc::c_int;
19use log::{error, warn};
20use std::convert::AsRef;
21use std::ffi::OsStr;
22use std::fmt;
23use std::io::IoSlice;
24use std::time::Duration;
25
26#[cfg(target_os = "macos")]
27use std::time::SystemTime;
28
29use crate::{FileAttr, FileType};
30
31/// Generic reply callback to send data
32pub trait ReplySender: Send + Sync + Unpin + 'static {
33    /// Send data.
34    fn send(&self, data: &[IoSlice<'_>]) -> std::io::Result<()>;
35}
36
37impl fmt::Debug for Box<dyn ReplySender> {
38    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> {
39        write!(f, "Box<ReplySender>")
40    }
41}
42
43/// Generic reply trait
44pub trait Reply {
45    /// Create a new reply for the given request
46    fn new<S: ReplySender>(unique: u64, sender: S) -> Self;
47}
48
49///
50/// Raw reply
51///
52#[derive(Debug)]
53pub(crate) struct ReplyRaw {
54    /// Unique id of the request to reply to
55    unique: ll::RequestId,
56    /// Closure to call for sending the reply
57    sender: Option<Box<dyn ReplySender>>,
58}
59
60impl Reply for ReplyRaw {
61    fn new<S: ReplySender>(unique: u64, sender: S) -> ReplyRaw {
62        let sender = Box::new(sender);
63        ReplyRaw {
64            unique: ll::RequestId(unique),
65            sender: Some(sender),
66        }
67    }
68}
69
70impl ReplyRaw {
71    /// Reply to a request with the given error code and data. Must be called
72    /// only once (the `ok` and `error` methods ensure this by consuming `self`)
73    fn send_ll_mut(&mut self, response: &ll::Response<'_>) {
74        assert!(self.sender.is_some());
75        let sender = self.sender.take().unwrap();
76        let res = response.with_iovec(self.unique, |iov| sender.send(iov));
77        if let Err(err) = res {
78            error!("Failed to send FUSE reply: {}", err);
79        }
80    }
81    fn send_ll(mut self, response: &ll::Response<'_>) {
82        self.send_ll_mut(response)
83    }
84
85    /// Reply to a request with the given error code
86    pub fn error(self, err: c_int) {
87        assert_ne!(err, 0);
88        self.send_ll(&ll::Response::new_error(ll::Errno::from_i32(err)));
89    }
90}
91
92impl Drop for ReplyRaw {
93    fn drop(&mut self) {
94        if self.sender.is_some() {
95            warn!(
96                "Reply not sent for operation {}, replying with I/O error",
97                self.unique.0
98            );
99            self.send_ll_mut(&ll::Response::new_error(ll::Errno::EIO));
100        }
101    }
102}
103
104///
105/// Empty reply
106///
107#[derive(Debug)]
108pub struct ReplyEmpty {
109    reply: ReplyRaw,
110}
111
112impl Reply for ReplyEmpty {
113    fn new<S: ReplySender>(unique: u64, sender: S) -> ReplyEmpty {
114        ReplyEmpty {
115            reply: Reply::new(unique, sender),
116        }
117    }
118}
119
120impl ReplyEmpty {
121    /// Reply to a request with nothing
122    pub fn ok(self) {
123        self.reply.send_ll(&ll::Response::new_empty());
124    }
125
126    /// Reply to a request with the given error code
127    pub fn error(self, err: c_int) {
128        self.reply.error(err);
129    }
130}
131
132///
133/// Data reply
134///
135#[derive(Debug)]
136pub struct ReplyData {
137    reply: ReplyRaw,
138}
139
140impl Reply for ReplyData {
141    fn new<S: ReplySender>(unique: u64, sender: S) -> ReplyData {
142        ReplyData {
143            reply: Reply::new(unique, sender),
144        }
145    }
146}
147
148impl ReplyData {
149    /// Reply to a request with the given data
150    pub fn data(self, data: &[u8]) {
151        self.reply.send_ll(&ll::Response::new_slice(data));
152    }
153
154    /// Reply to a request with the given error code
155    pub fn error(self, err: c_int) {
156        self.reply.error(err);
157    }
158}
159
160///
161/// Entry reply
162///
163#[derive(Debug)]
164pub struct ReplyEntry {
165    reply: ReplyRaw,
166}
167
168impl Reply for ReplyEntry {
169    fn new<S: ReplySender>(unique: u64, sender: S) -> ReplyEntry {
170        ReplyEntry {
171            reply: Reply::new(unique, sender),
172        }
173    }
174}
175
176impl ReplyEntry {
177    /// Reply to a request with the given entry
178    pub fn entry(self, ttl: &Duration, attr: &FileAttr, generation: u64) {
179        self.reply.send_ll(&ll::Response::new_entry(
180            ll::INodeNo(attr.ino),
181            ll::Generation(generation),
182            &attr.into(),
183            *ttl,
184            *ttl,
185        ));
186    }
187
188    /// Reply to a request with the given error code
189    pub fn error(self, err: c_int) {
190        self.reply.error(err);
191    }
192}
193
194///
195/// Attribute Reply
196///
197#[derive(Debug)]
198pub struct ReplyAttr {
199    reply: ReplyRaw,
200}
201
202impl Reply for ReplyAttr {
203    fn new<S: ReplySender>(unique: u64, sender: S) -> ReplyAttr {
204        ReplyAttr {
205            reply: Reply::new(unique, sender),
206        }
207    }
208}
209
210impl ReplyAttr {
211    /// Reply to a request with the given attribute
212    pub fn attr(self, ttl: &Duration, attr: &FileAttr) {
213        self.reply
214            .send_ll(&ll::Response::new_attr(ttl, &attr.into()));
215    }
216
217    /// Reply to a request with the given error code
218    pub fn error(self, err: c_int) {
219        self.reply.error(err);
220    }
221}
222
223///
224/// XTimes Reply
225///
226#[cfg(target_os = "macos")]
227#[derive(Debug)]
228pub struct ReplyXTimes {
229    reply: ReplyRaw,
230}
231
232#[cfg(target_os = "macos")]
233impl Reply for ReplyXTimes {
234    fn new<S: ReplySender>(unique: u64, sender: S) -> ReplyXTimes {
235        ReplyXTimes {
236            reply: Reply::new(unique, sender),
237        }
238    }
239}
240
241#[cfg(target_os = "macos")]
242impl ReplyXTimes {
243    /// Reply to a request with the given xtimes
244    pub fn xtimes(self, bkuptime: SystemTime, crtime: SystemTime) {
245        self.reply
246            .send_ll(&ll::Response::new_xtimes(bkuptime, crtime))
247    }
248
249    /// Reply to a request with the given error code
250    pub fn error(self, err: c_int) {
251        self.reply.error(err);
252    }
253}
254
255///
256/// Open Reply
257///
258#[derive(Debug)]
259pub struct ReplyOpen {
260    reply: ReplyRaw,
261}
262
263impl Reply for ReplyOpen {
264    fn new<S: ReplySender>(unique: u64, sender: S) -> ReplyOpen {
265        ReplyOpen {
266            reply: Reply::new(unique, sender),
267        }
268    }
269}
270
271impl ReplyOpen {
272    /// Reply to a request with the given open result
273    pub fn opened(self, fh: u64, flags: u32) {
274        self.reply
275            .send_ll(&ll::Response::new_open(ll::FileHandle(fh), flags))
276    }
277
278    /// Reply to a request with the given error code
279    pub fn error(self, err: c_int) {
280        self.reply.error(err);
281    }
282}
283
284///
285/// Write Reply
286///
287#[derive(Debug)]
288pub struct ReplyWrite {
289    reply: ReplyRaw,
290}
291
292impl Reply for ReplyWrite {
293    fn new<S: ReplySender>(unique: u64, sender: S) -> ReplyWrite {
294        ReplyWrite {
295            reply: Reply::new(unique, sender),
296        }
297    }
298}
299
300impl ReplyWrite {
301    /// Reply to a request with the given open result
302    pub fn written(self, size: u32) {
303        self.reply.send_ll(&ll::Response::new_write(size))
304    }
305
306    /// Reply to a request with the given error code
307    pub fn error(self, err: c_int) {
308        self.reply.error(err);
309    }
310}
311
312///
313/// Statfs Reply
314///
315#[derive(Debug)]
316pub struct ReplyStatfs {
317    reply: ReplyRaw,
318}
319
320impl Reply for ReplyStatfs {
321    fn new<S: ReplySender>(unique: u64, sender: S) -> ReplyStatfs {
322        ReplyStatfs {
323            reply: Reply::new(unique, sender),
324        }
325    }
326}
327
328impl ReplyStatfs {
329    /// Reply to a request with the given open result
330    #[allow(clippy::too_many_arguments)]
331    pub fn statfs(
332        self,
333        blocks: u64,
334        bfree: u64,
335        bavail: u64,
336        files: u64,
337        ffree: u64,
338        bsize: u32,
339        namelen: u32,
340        frsize: u32,
341    ) {
342        self.reply.send_ll(&ll::Response::new_statfs(
343            blocks, bfree, bavail, files, ffree, bsize, namelen, frsize,
344        ))
345    }
346
347    /// Reply to a request with the given error code
348    pub fn error(self, err: c_int) {
349        self.reply.error(err);
350    }
351}
352
353///
354/// Create reply
355///
356#[derive(Debug)]
357pub struct ReplyCreate {
358    reply: ReplyRaw,
359}
360
361impl Reply for ReplyCreate {
362    fn new<S: ReplySender>(unique: u64, sender: S) -> ReplyCreate {
363        ReplyCreate {
364            reply: Reply::new(unique, sender),
365        }
366    }
367}
368
369impl ReplyCreate {
370    /// Reply to a request with the given entry
371    pub fn created(self, ttl: &Duration, attr: &FileAttr, generation: u64, fh: u64, flags: u32) {
372        self.reply.send_ll(&ll::Response::new_create(
373            ttl,
374            &attr.into(),
375            ll::Generation(generation),
376            ll::FileHandle(fh),
377            flags,
378        ))
379    }
380
381    /// Reply to a request with the given error code
382    pub fn error(self, err: c_int) {
383        self.reply.error(err);
384    }
385}
386
387///
388/// Lock Reply
389///
390#[derive(Debug)]
391pub struct ReplyLock {
392    reply: ReplyRaw,
393}
394
395impl Reply for ReplyLock {
396    fn new<S: ReplySender>(unique: u64, sender: S) -> ReplyLock {
397        ReplyLock {
398            reply: Reply::new(unique, sender),
399        }
400    }
401}
402
403impl ReplyLock {
404    /// Reply to a request with the given open result
405    pub fn locked(self, start: u64, end: u64, typ: i32, pid: u32) {
406        self.reply.send_ll(&ll::Response::new_lock(&ll::Lock {
407            range: (start, end),
408            typ,
409            pid,
410        }))
411    }
412
413    /// Reply to a request with the given error code
414    pub fn error(self, err: c_int) {
415        self.reply.error(err);
416    }
417}
418
419///
420/// Bmap Reply
421///
422#[derive(Debug)]
423pub struct ReplyBmap {
424    reply: ReplyRaw,
425}
426
427impl Reply for ReplyBmap {
428    fn new<S: ReplySender>(unique: u64, sender: S) -> ReplyBmap {
429        ReplyBmap {
430            reply: Reply::new(unique, sender),
431        }
432    }
433}
434
435impl ReplyBmap {
436    /// Reply to a request with the given open result
437    pub fn bmap(self, block: u64) {
438        self.reply.send_ll(&ll::Response::new_bmap(block))
439    }
440
441    /// Reply to a request with the given error code
442    pub fn error(self, err: c_int) {
443        self.reply.error(err);
444    }
445}
446
447///
448/// Ioctl Reply
449///
450#[derive(Debug)]
451pub struct ReplyIoctl {
452    reply: ReplyRaw,
453}
454
455impl Reply for ReplyIoctl {
456    fn new<S: ReplySender>(unique: u64, sender: S) -> ReplyIoctl {
457        ReplyIoctl {
458            reply: Reply::new(unique, sender),
459        }
460    }
461}
462
463impl ReplyIoctl {
464    /// Reply to a request with the given open result
465    pub fn ioctl(self, result: i32, data: &[u8]) {
466        self.reply
467            .send_ll(&ll::Response::new_ioctl(result, &[IoSlice::new(data)]));
468    }
469
470    /// Reply to a request with the given error code
471    pub fn error(self, err: c_int) {
472        self.reply.error(err);
473    }
474}
475
476///
477/// Poll Reply
478///
479#[derive(Debug)]
480#[cfg(feature = "abi-7-11")]
481pub struct ReplyPoll {
482    reply: ReplyRaw,
483}
484
485#[cfg(feature = "abi-7-11")]
486impl Reply for ReplyPoll {
487    fn new<S: ReplySender>(unique: u64, sender: S) -> ReplyPoll {
488        ReplyPoll {
489            reply: Reply::new(unique, sender),
490        }
491    }
492}
493
494#[cfg(feature = "abi-7-11")]
495impl ReplyPoll {
496    /// Reply to a request with the given poll result
497    pub fn poll(self, revents: u32) {
498        self.reply.send_ll(&ll::Response::new_poll(revents))
499    }
500
501    /// Reply to a request with the given error code
502    pub fn error(self, err: c_int) {
503        self.reply.error(err);
504    }
505}
506
507///
508/// Directory reply
509///
510#[derive(Debug)]
511pub struct ReplyDirectory {
512    reply: ReplyRaw,
513    data: DirEntList,
514}
515
516impl ReplyDirectory {
517    /// Creates a new ReplyDirectory with a specified buffer size.
518    pub fn new<S: ReplySender>(unique: u64, sender: S, size: usize) -> ReplyDirectory {
519        ReplyDirectory {
520            reply: Reply::new(unique, sender),
521            data: DirEntList::new(size),
522        }
523    }
524
525    /// Add an entry to the directory reply buffer. Returns true if the buffer is full.
526    /// A transparent offset value can be provided for each entry. The kernel uses these
527    /// value to request the next entries in further readdir calls
528    #[must_use]
529    pub fn add<T: AsRef<OsStr>>(&mut self, ino: u64, offset: i64, kind: FileType, name: T) -> bool {
530        let name = name.as_ref();
531        self.data.push(&DirEntry::new(
532            INodeNo(ino),
533            DirEntOffset(offset),
534            kind,
535            name,
536        ))
537    }
538
539    /// Reply to a request with the filled directory buffer
540    pub fn ok(self) {
541        self.reply.send_ll(&self.data.into());
542    }
543
544    /// Reply to a request with the given error code
545    pub fn error(self, err: c_int) {
546        self.reply.error(err);
547    }
548}
549
550///
551/// DirectoryPlus reply
552///
553#[derive(Debug)]
554pub struct ReplyDirectoryPlus {
555    reply: ReplyRaw,
556    buf: DirEntPlusList,
557}
558
559impl ReplyDirectoryPlus {
560    /// Creates a new ReplyDirectory with a specified buffer size.
561    pub fn new<S: ReplySender>(unique: u64, sender: S, size: usize) -> ReplyDirectoryPlus {
562        ReplyDirectoryPlus {
563            reply: Reply::new(unique, sender),
564            buf: DirEntPlusList::new(size),
565        }
566    }
567
568    /// Add an entry to the directory reply buffer. Returns true if the buffer is full.
569    /// A transparent offset value can be provided for each entry. The kernel uses these
570    /// value to request the next entries in further readdir calls
571    pub fn add<T: AsRef<OsStr>>(
572        &mut self,
573        ino: u64,
574        offset: i64,
575        name: T,
576        ttl: &Duration,
577        attr: &FileAttr,
578        generation: u64,
579    ) -> bool {
580        let name = name.as_ref();
581        self.buf.push(&DirEntryPlus::new(
582            INodeNo(ino),
583            Generation(generation),
584            DirEntOffset(offset),
585            name,
586            *ttl,
587            attr.into(),
588            *ttl,
589        ))
590    }
591
592    /// Reply to a request with the filled directory buffer
593    pub fn ok(self) {
594        self.reply.send_ll(&self.buf.into());
595    }
596
597    /// Reply to a request with the given error code
598    pub fn error(self, err: c_int) {
599        self.reply.error(err);
600    }
601}
602
603///
604/// Xattr reply
605///
606#[derive(Debug)]
607pub struct ReplyXattr {
608    reply: ReplyRaw,
609}
610
611impl Reply for ReplyXattr {
612    fn new<S: ReplySender>(unique: u64, sender: S) -> ReplyXattr {
613        ReplyXattr {
614            reply: Reply::new(unique, sender),
615        }
616    }
617}
618
619impl ReplyXattr {
620    /// Reply to a request with the size of the xattr.
621    pub fn size(self, size: u32) {
622        self.reply.send_ll(&ll::Response::new_xattr_size(size))
623    }
624
625    /// Reply to a request with the data in the xattr.
626    pub fn data(self, data: &[u8]) {
627        self.reply.send_ll(&ll::Response::new_data(data))
628    }
629
630    /// Reply to a request with the given error code.
631    pub fn error(self, err: c_int) {
632        self.reply.error(err);
633    }
634}
635
636///
637/// Lseek Reply
638///
639#[derive(Debug)]
640pub struct ReplyLseek {
641    reply: ReplyRaw,
642}
643
644impl Reply for ReplyLseek {
645    fn new<S: ReplySender>(unique: u64, sender: S) -> ReplyLseek {
646        ReplyLseek {
647            reply: Reply::new(unique, sender),
648        }
649    }
650}
651
652impl ReplyLseek {
653    /// Reply to a request with seeked offset
654    pub fn offset(self, offset: i64) {
655        self.reply.send_ll(&ll::Response::new_lseek(offset))
656    }
657
658    /// Reply to a request with the given error code
659    pub fn error(self, err: c_int) {
660        self.reply.error(err);
661    }
662}
663
664#[cfg(test)]
665mod test {
666    use super::*;
667    use crate::{FileAttr, FileType};
668    use std::io::IoSlice;
669    use std::sync::mpsc::{sync_channel, SyncSender};
670    use std::thread;
671    use std::time::{Duration, UNIX_EPOCH};
672    use zerocopy::AsBytes;
673
674    #[derive(Debug, AsBytes)]
675    #[repr(C)]
676    struct Data {
677        a: u8,
678        b: u8,
679        c: u16,
680    }
681
682    #[test]
683    fn serialize_empty() {
684        assert!(().as_bytes().is_empty());
685    }
686
687    #[test]
688    fn serialize_slice() {
689        let data: [u8; 4] = [0x12, 0x34, 0x56, 0x78];
690        assert_eq!(data.as_bytes(), [0x12, 0x34, 0x56, 0x78]);
691    }
692
693    #[test]
694    fn serialize_struct() {
695        let data = Data {
696            a: 0x12,
697            b: 0x34,
698            c: 0x5678,
699        };
700        assert_eq!(data.as_bytes(), [0x12, 0x34, 0x78, 0x56]);
701    }
702
703    struct AssertSender {
704        expected: Vec<u8>,
705    }
706
707    impl super::ReplySender for AssertSender {
708        fn send(&self, data: &[IoSlice<'_>]) -> std::io::Result<()> {
709            let mut v = vec![];
710            for x in data {
711                v.extend_from_slice(x)
712            }
713            assert_eq!(self.expected, v);
714            Ok(())
715        }
716    }
717
718    #[test]
719    fn reply_raw() {
720        let data = Data {
721            a: 0x12,
722            b: 0x34,
723            c: 0x5678,
724        };
725        let sender = AssertSender {
726            expected: vec![
727                0x14, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xef, 0xbe, 0xad, 0xde, 0x00, 0x00,
728                0x00, 0x00, 0x12, 0x34, 0x78, 0x56,
729            ],
730        };
731        let reply: ReplyRaw = Reply::new(0xdeadbeef, sender);
732        reply.send_ll(&ll::Response::new_data(data.as_bytes()));
733    }
734
735    #[test]
736    fn reply_error() {
737        let sender = AssertSender {
738            expected: vec![
739                0x10, 0x00, 0x00, 0x00, 0xbe, 0xff, 0xff, 0xff, 0xef, 0xbe, 0xad, 0xde, 0x00, 0x00,
740                0x00, 0x00,
741            ],
742        };
743        let reply: ReplyRaw = Reply::new(0xdeadbeef, sender);
744        reply.error(66);
745    }
746
747    #[test]
748    fn reply_empty() {
749        let sender = AssertSender {
750            expected: vec![
751                0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xef, 0xbe, 0xad, 0xde, 0x00, 0x00,
752                0x00, 0x00,
753            ],
754        };
755        let reply: ReplyEmpty = Reply::new(0xdeadbeef, sender);
756        reply.ok();
757    }
758
759    #[test]
760    fn reply_data() {
761        let sender = AssertSender {
762            expected: vec![
763                0x14, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xef, 0xbe, 0xad, 0xde, 0x00, 0x00,
764                0x00, 0x00, 0xde, 0xad, 0xbe, 0xef,
765            ],
766        };
767        let reply: ReplyData = Reply::new(0xdeadbeef, sender);
768        reply.data(&[0xde, 0xad, 0xbe, 0xef]);
769    }
770
771    #[test]
772    fn reply_entry() {
773        let mut expected = if cfg!(target_os = "macos") {
774            vec![
775                0x98, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xef, 0xbe, 0xad, 0xde, 0x00, 0x00,
776                0x00, 0x00, 0x11, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xaa, 0x00, 0x00, 0x00,
777                0x00, 0x00, 0x00, 0x00, 0x65, 0x87, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x65, 0x87,
778                0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x21, 0x43, 0x00, 0x00, 0x21, 0x43, 0x00, 0x00,
779                0x11, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x22, 0x00, 0x00, 0x00, 0x00, 0x00,
780                0x00, 0x00, 0x33, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x34, 0x12, 0x00, 0x00,
781                0x00, 0x00, 0x00, 0x00, 0x34, 0x12, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x34, 0x12,
782                0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x34, 0x12, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
783                0x78, 0x56, 0x00, 0x00, 0x78, 0x56, 0x00, 0x00, 0x78, 0x56, 0x00, 0x00, 0x78, 0x56,
784                0x00, 0x00, 0xa4, 0x81, 0x00, 0x00, 0x55, 0x00, 0x00, 0x00, 0x66, 0x00, 0x00, 0x00,
785                0x77, 0x00, 0x00, 0x00, 0x88, 0x00, 0x00, 0x00, 0x99, 0x00, 0x00, 0x00,
786            ]
787        } else {
788            vec![
789                0x88, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xef, 0xbe, 0xad, 0xde, 0x00, 0x00,
790                0x00, 0x00, 0x11, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xaa, 0x00, 0x00, 0x00,
791                0x00, 0x00, 0x00, 0x00, 0x65, 0x87, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x65, 0x87,
792                0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x21, 0x43, 0x00, 0x00, 0x21, 0x43, 0x00, 0x00,
793                0x11, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x22, 0x00, 0x00, 0x00, 0x00, 0x00,
794                0x00, 0x00, 0x33, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x34, 0x12, 0x00, 0x00,
795                0x00, 0x00, 0x00, 0x00, 0x34, 0x12, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x34, 0x12,
796                0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x78, 0x56, 0x00, 0x00, 0x78, 0x56, 0x00, 0x00,
797                0x78, 0x56, 0x00, 0x00, 0xa4, 0x81, 0x00, 0x00, 0x55, 0x00, 0x00, 0x00, 0x66, 0x00,
798                0x00, 0x00, 0x77, 0x00, 0x00, 0x00, 0x88, 0x00, 0x00, 0x00,
799            ]
800        };
801
802        if cfg!(feature = "abi-7-9") {
803            expected.extend(vec![0xbb, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]);
804        }
805        expected[0] = (expected.len()) as u8;
806
807        let sender = AssertSender { expected };
808        let reply: ReplyEntry = Reply::new(0xdeadbeef, sender);
809        let time = UNIX_EPOCH + Duration::new(0x1234, 0x5678);
810        let ttl = Duration::new(0x8765, 0x4321);
811        let attr = FileAttr {
812            ino: 0x11,
813            size: 0x22,
814            blocks: 0x33,
815            atime: time,
816            mtime: time,
817            ctime: time,
818            crtime: time,
819            kind: FileType::RegularFile,
820            perm: 0o644,
821            nlink: 0x55,
822            uid: 0x66,
823            gid: 0x77,
824            rdev: 0x88,
825            flags: 0x99,
826            blksize: 0xbb,
827        };
828        reply.entry(&ttl, &attr, 0xaa);
829    }
830
831    #[test]
832    fn reply_attr() {
833        let mut expected = if cfg!(target_os = "macos") {
834            vec![
835                0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xef, 0xbe, 0xad, 0xde, 0x00, 0x00,
836                0x00, 0x00, 0x65, 0x87, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x21, 0x43, 0x00, 0x00,
837                0x00, 0x00, 0x00, 0x00, 0x11, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x22, 0x00,
838                0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x33, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
839                0x34, 0x12, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x34, 0x12, 0x00, 0x00, 0x00, 0x00,
840                0x00, 0x00, 0x34, 0x12, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x34, 0x12, 0x00, 0x00,
841                0x00, 0x00, 0x00, 0x00, 0x78, 0x56, 0x00, 0x00, 0x78, 0x56, 0x00, 0x00, 0x78, 0x56,
842                0x00, 0x00, 0x78, 0x56, 0x00, 0x00, 0xa4, 0x81, 0x00, 0x00, 0x55, 0x00, 0x00, 0x00,
843                0x66, 0x00, 0x00, 0x00, 0x77, 0x00, 0x00, 0x00, 0x88, 0x00, 0x00, 0x00, 0x99, 0x00,
844                0x00, 0x00,
845            ]
846        } else {
847            vec![
848                0x70, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xef, 0xbe, 0xad, 0xde, 0x00, 0x00,
849                0x00, 0x00, 0x65, 0x87, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x21, 0x43, 0x00, 0x00,
850                0x00, 0x00, 0x00, 0x00, 0x11, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x22, 0x00,
851                0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x33, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
852                0x34, 0x12, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x34, 0x12, 0x00, 0x00, 0x00, 0x00,
853                0x00, 0x00, 0x34, 0x12, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x78, 0x56, 0x00, 0x00,
854                0x78, 0x56, 0x00, 0x00, 0x78, 0x56, 0x00, 0x00, 0xa4, 0x81, 0x00, 0x00, 0x55, 0x00,
855                0x00, 0x00, 0x66, 0x00, 0x00, 0x00, 0x77, 0x00, 0x00, 0x00, 0x88, 0x00, 0x00, 0x00,
856            ]
857        };
858
859        if cfg!(feature = "abi-7-9") {
860            expected.extend_from_slice(&[0xbb, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]);
861        }
862        expected[0] = expected.len() as u8;
863
864        let sender = AssertSender { expected };
865        let reply: ReplyAttr = Reply::new(0xdeadbeef, sender);
866        let time = UNIX_EPOCH + Duration::new(0x1234, 0x5678);
867        let ttl = Duration::new(0x8765, 0x4321);
868        let attr = FileAttr {
869            ino: 0x11,
870            size: 0x22,
871            blocks: 0x33,
872            atime: time,
873            mtime: time,
874            ctime: time,
875            crtime: time,
876            kind: FileType::RegularFile,
877            perm: 0o644,
878            nlink: 0x55,
879            uid: 0x66,
880            gid: 0x77,
881            rdev: 0x88,
882            flags: 0x99,
883            blksize: 0xbb,
884        };
885        reply.attr(&ttl, &attr);
886    }
887
888    #[test]
889    #[cfg(target_os = "macos")]
890    fn reply_xtimes() {
891        let sender = AssertSender {
892            expected: vec![
893                0x28, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xef, 0xbe, 0xad, 0xde, 0x00, 0x00,
894                0x00, 0x00, 0x34, 0x12, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x34, 0x12, 0x00, 0x00,
895                0x00, 0x00, 0x00, 0x00, 0x78, 0x56, 0x00, 0x00, 0x78, 0x56, 0x00, 0x00,
896            ],
897        };
898        let reply: ReplyXTimes = Reply::new(0xdeadbeef, sender);
899        let time = UNIX_EPOCH + Duration::new(0x1234, 0x5678);
900        reply.xtimes(time, time);
901    }
902
903    #[test]
904    fn reply_open() {
905        let sender = AssertSender {
906            expected: vec![
907                0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xef, 0xbe, 0xad, 0xde, 0x00, 0x00,
908                0x00, 0x00, 0x22, 0x11, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x33, 0x00, 0x00, 0x00,
909                0x00, 0x00, 0x00, 0x00,
910            ],
911        };
912        let reply: ReplyOpen = Reply::new(0xdeadbeef, sender);
913        reply.opened(0x1122, 0x33);
914    }
915
916    #[test]
917    fn reply_write() {
918        let sender = AssertSender {
919            expected: vec![
920                0x18, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xef, 0xbe, 0xad, 0xde, 0x00, 0x00,
921                0x00, 0x00, 0x22, 0x11, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
922            ],
923        };
924        let reply: ReplyWrite = Reply::new(0xdeadbeef, sender);
925        reply.written(0x1122);
926    }
927
928    #[test]
929    fn reply_statfs() {
930        let sender = AssertSender {
931            expected: vec![
932                0x60, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xef, 0xbe, 0xad, 0xde, 0x00, 0x00,
933                0x00, 0x00, 0x11, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x22, 0x00, 0x00, 0x00,
934                0x00, 0x00, 0x00, 0x00, 0x33, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x44, 0x00,
935                0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x55, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
936                0x66, 0x00, 0x00, 0x00, 0x77, 0x00, 0x00, 0x00, 0x88, 0x00, 0x00, 0x00, 0x00, 0x00,
937                0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
938                0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
939            ],
940        };
941        let reply: ReplyStatfs = Reply::new(0xdeadbeef, sender);
942        reply.statfs(0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88);
943    }
944
945    #[test]
946    fn reply_create() {
947        let mut expected = if cfg!(target_os = "macos") {
948            vec![
949                0xa8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xef, 0xbe, 0xad, 0xde, 0x00, 0x00,
950                0x00, 0x00, 0x11, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xaa, 0x00, 0x00, 0x00,
951                0x00, 0x00, 0x00, 0x00, 0x65, 0x87, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x65, 0x87,
952                0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x21, 0x43, 0x00, 0x00, 0x21, 0x43, 0x00, 0x00,
953                0x11, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x22, 0x00, 0x00, 0x00, 0x00, 0x00,
954                0x00, 0x00, 0x33, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x34, 0x12, 0x00, 0x00,
955                0x00, 0x00, 0x00, 0x00, 0x34, 0x12, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x34, 0x12,
956                0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x34, 0x12, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
957                0x78, 0x56, 0x00, 0x00, 0x78, 0x56, 0x00, 0x00, 0x78, 0x56, 0x00, 0x00, 0x78, 0x56,
958                0x00, 0x00, 0xa4, 0x81, 0x00, 0x00, 0x55, 0x00, 0x00, 0x00, 0x66, 0x00, 0x00, 0x00,
959                0x77, 0x00, 0x00, 0x00, 0x88, 0x00, 0x00, 0x00, 0x99, 0x00, 0x00, 0x00, 0xbb, 0x00,
960                0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xcc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
961            ]
962        } else {
963            vec![
964                0x98, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xef, 0xbe, 0xad, 0xde, 0x00, 0x00,
965                0x00, 0x00, 0x11, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xaa, 0x00, 0x00, 0x00,
966                0x00, 0x00, 0x00, 0x00, 0x65, 0x87, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x65, 0x87,
967                0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x21, 0x43, 0x00, 0x00, 0x21, 0x43, 0x00, 0x00,
968                0x11, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x22, 0x00, 0x00, 0x00, 0x00, 0x00,
969                0x00, 0x00, 0x33, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x34, 0x12, 0x00, 0x00,
970                0x00, 0x00, 0x00, 0x00, 0x34, 0x12, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x34, 0x12,
971                0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x78, 0x56, 0x00, 0x00, 0x78, 0x56, 0x00, 0x00,
972                0x78, 0x56, 0x00, 0x00, 0xa4, 0x81, 0x00, 0x00, 0x55, 0x00, 0x00, 0x00, 0x66, 0x00,
973                0x00, 0x00, 0x77, 0x00, 0x00, 0x00, 0x88, 0x00, 0x00, 0x00, 0xbb, 0x00, 0x00, 0x00,
974                0x00, 0x00, 0x00, 0x00, 0xcc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
975            ]
976        };
977
978        if cfg!(feature = "abi-7-9") {
979            let insert_at = expected.len() - 16;
980            expected.splice(
981                insert_at..insert_at,
982                vec![0xdd, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00],
983            );
984        }
985        expected[0] = (expected.len()) as u8;
986
987        let sender = AssertSender { expected };
988        let reply: ReplyCreate = Reply::new(0xdeadbeef, sender);
989        let time = UNIX_EPOCH + Duration::new(0x1234, 0x5678);
990        let ttl = Duration::new(0x8765, 0x4321);
991        let attr = FileAttr {
992            ino: 0x11,
993            size: 0x22,
994            blocks: 0x33,
995            atime: time,
996            mtime: time,
997            ctime: time,
998            crtime: time,
999            kind: FileType::RegularFile,
1000            perm: 0o644,
1001            nlink: 0x55,
1002            uid: 0x66,
1003            gid: 0x77,
1004            rdev: 0x88,
1005            flags: 0x99,
1006            blksize: 0xdd,
1007        };
1008        reply.created(&ttl, &attr, 0xaa, 0xbb, 0xcc);
1009    }
1010
1011    #[test]
1012    fn reply_lock() {
1013        let sender = AssertSender {
1014            expected: vec![
1015                0x28, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xef, 0xbe, 0xad, 0xde, 0x00, 0x00,
1016                0x00, 0x00, 0x11, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x22, 0x00, 0x00, 0x00,
1017                0x00, 0x00, 0x00, 0x00, 0x33, 0x00, 0x00, 0x00, 0x44, 0x00, 0x00, 0x00,
1018            ],
1019        };
1020        let reply: ReplyLock = Reply::new(0xdeadbeef, sender);
1021        reply.locked(0x11, 0x22, 0x33, 0x44);
1022    }
1023
1024    #[test]
1025    fn reply_bmap() {
1026        let sender = AssertSender {
1027            expected: vec![
1028                0x18, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xef, 0xbe, 0xad, 0xde, 0x00, 0x00,
1029                0x00, 0x00, 0x34, 0x12, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1030            ],
1031        };
1032        let reply: ReplyBmap = Reply::new(0xdeadbeef, sender);
1033        reply.bmap(0x1234);
1034    }
1035
1036    #[test]
1037    fn reply_directory() {
1038        let sender = AssertSender {
1039            expected: vec![
1040                0x50, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xef, 0xbe, 0xad, 0xde, 0x00, 0x00,
1041                0x00, 0x00, 0xbb, 0xaa, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
1042                0x00, 0x00, 0x00, 0x00, 0x05, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x68, 0x65,
1043                0x6c, 0x6c, 0x6f, 0x00, 0x00, 0x00, 0xdd, 0xcc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1044                0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x08, 0x00,
1045                0x00, 0x00, 0x77, 0x6f, 0x72, 0x6c, 0x64, 0x2e, 0x72, 0x73,
1046            ],
1047        };
1048        let mut reply = ReplyDirectory::new(0xdeadbeef, sender, 4096);
1049        assert!(!reply.add(0xaabb, 1, FileType::Directory, "hello"));
1050        assert!(!reply.add(0xccdd, 2, FileType::RegularFile, "world.rs"));
1051        reply.ok();
1052    }
1053
1054    #[test]
1055    fn reply_xattr_size() {
1056        let sender = AssertSender {
1057            expected: vec![
1058                0x18, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xEF, 0xBE, 0xAD, 0xDE, 0x00, 0x00,
1059                0x00, 0x00, 0x78, 0x56, 0x34, 0x12, 0x00, 0x00, 0x00, 0x00,
1060            ],
1061        };
1062        let reply = ReplyXattr::new(0xdeadbeef, sender);
1063        reply.size(0x12345678);
1064    }
1065
1066    #[test]
1067    fn reply_xattr_data() {
1068        let sender = AssertSender {
1069            expected: vec![
1070                0x14, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xEF, 0xBE, 0xAD, 0xDE, 0x00, 0x00,
1071                0x00, 0x00, 0x11, 0x22, 0x33, 0x44,
1072            ],
1073        };
1074        let reply = ReplyXattr::new(0xdeadbeef, sender);
1075        reply.data(&[0x11, 0x22, 0x33, 0x44]);
1076    }
1077
1078    impl super::ReplySender for SyncSender<()> {
1079        fn send(&self, _: &[IoSlice<'_>]) -> std::io::Result<()> {
1080            self.send(()).unwrap();
1081            Ok(())
1082        }
1083    }
1084
1085    #[test]
1086    fn async_reply() {
1087        let (tx, rx) = sync_channel::<()>(1);
1088        let reply: ReplyEmpty = Reply::new(0xdeadbeef, tx);
1089        thread::spawn(move || {
1090            reply.ok();
1091        });
1092        rx.recv().unwrap();
1093    }
1094}