Skip to main content

async_fuser/ll/reply_async/
readdir.rs

1//! Response data implementation of [`crate::AsyncFilesystem::readdir`] operation to
2//! send to the kernel
3
4use std::ffi::OsStr;
5use std::io::IoSlice;
6
7use crate::FileType;
8use crate::INodeNo;
9
10use crate::ll::ioslice_concat::IosliceConcat;
11use crate::ll::reply::DirEntList;
12use crate::ll::reply::DirEntOffset;
13use crate::ll::reply::DirEntry;
14use crate::ll::reply::Response;
15
16/// Response data from [`crate::AsyncFilesystem::readdir`] operation
17#[derive(Debug)]
18pub struct DirectoryResponse {
19    data: DirEntList,
20}
21
22impl DirectoryResponse {
23    /// Creates a new [`DirectoryResponse`] with a specified buffer size.
24    pub fn new(size: usize) -> DirectoryResponse {
25        DirectoryResponse {
26            data: DirEntList::new(size),
27        }
28    }
29
30    /// Add an entry to the directory reply buffer. Returns true if the buffer is full.
31    /// A transparent offset value can be provided for each entry. The kernel uses these
32    /// value to request the next entries in further readdir calls
33    #[must_use]
34    pub fn add<T: AsRef<OsStr>>(
35        &mut self,
36        ino: INodeNo,
37        offset: u64,
38        kind: FileType,
39        name: T,
40    ) -> bool {
41        let name = name.as_ref();
42        self.data
43            .push(&DirEntry::new(ino, DirEntOffset(offset), kind, name))
44    }
45}
46
47impl Response for DirectoryResponse {
48    fn payload(&self) -> impl IosliceConcat {
49        [IoSlice::new(self.data.as_bytes())]
50    }
51}