1use std::ffi::CStr;
30use std::io;
31use std::time::Duration;
32
33use fuse_backend_rs::abi::fuse_abi::{stat64, statvfs64};
34use fuse_backend_rs::api::filesystem::{
35 Context, DirEntry, Entry, FileSystem, FsOptions, GetxattrReply, Layer, ListxattrReply,
36 OpenOptions, SetattrValid, ZeroCopyReader, ZeroCopyWriter,
37};
38
39pub struct ZeroOpenLayer<L>
42where
43 L: Layer<Inode = u64, Handle = u64> + Send + Sync,
44{
45 inner: L,
46}
47
48impl<L> ZeroOpenLayer<L>
49where
50 L: Layer<Inode = u64, Handle = u64> + Send + Sync,
51{
52 pub fn new(inner: L) -> Self {
55 Self { inner }
56 }
57}
58
59impl<L> FileSystem for ZeroOpenLayer<L>
60where
61 L: Layer<Inode = u64, Handle = u64> + Send + Sync,
62{
63 type Inode = u64;
64 type Handle = u64;
65
66 fn init(&self, capable: FsOptions) -> io::Result<FsOptions> {
67 self.inner.init(capable)
68 }
69
70 fn destroy(&self) {
71 self.inner.destroy()
72 }
73
74 fn lookup(&self, ctx: &Context, parent: u64, name: &CStr) -> io::Result<Entry> {
75 self.inner.lookup(ctx, parent, name)
76 }
77
78 fn forget(&self, ctx: &Context, inode: u64, count: u64) {
79 self.inner.forget(ctx, inode, count)
80 }
81
82 fn batch_forget(&self, ctx: &Context, requests: Vec<(u64, u64)>) {
83 self.inner.batch_forget(ctx, requests)
84 }
85
86 fn getattr(
87 &self,
88 ctx: &Context,
89 inode: u64,
90 handle: Option<u64>,
91 ) -> io::Result<(stat64, Duration)> {
92 self.inner.getattr(ctx, inode, handle)
93 }
94
95 fn setattr(
96 &self,
97 ctx: &Context,
98 inode: u64,
99 attr: stat64,
100 handle: Option<u64>,
101 valid: SetattrValid,
102 ) -> io::Result<(stat64, Duration)> {
103 self.inner.setattr(ctx, inode, attr, handle, valid)
104 }
105
106 fn readlink(&self, ctx: &Context, inode: u64) -> io::Result<Vec<u8>> {
107 self.inner.readlink(ctx, inode)
108 }
109
110 fn unlink(&self, ctx: &Context, parent: u64, name: &CStr) -> io::Result<()> {
111 self.inner.unlink(ctx, parent, name)
112 }
113
114 fn rmdir(&self, ctx: &Context, parent: u64, name: &CStr) -> io::Result<()> {
115 self.inner.rmdir(ctx, parent, name)
116 }
117
118 fn open(
120 &self,
121 ctx: &Context,
122 inode: u64,
123 flags: u32,
124 fuse_flags: u32,
125 ) -> io::Result<(Option<u64>, OpenOptions, Option<u32>)> {
126 let (handle, opts, passthrough) = self.inner.open(ctx, inode, flags, fuse_flags)?;
127 Ok((Some(handle.unwrap_or(0)), opts, passthrough))
128 }
129
130 #[allow(clippy::too_many_arguments)]
131 fn read(
132 &self,
133 ctx: &Context,
134 inode: u64,
135 handle: u64,
136 w: &mut dyn ZeroCopyWriter,
137 size: u32,
138 offset: u64,
139 lock_owner: Option<u64>,
140 flags: u32,
141 ) -> io::Result<usize> {
142 self.inner
143 .read(ctx, inode, handle, w, size, offset, lock_owner, flags)
144 }
145
146 #[allow(clippy::too_many_arguments)]
147 fn write(
148 &self,
149 ctx: &Context,
150 inode: u64,
151 handle: u64,
152 r: &mut dyn ZeroCopyReader,
153 size: u32,
154 offset: u64,
155 lock_owner: Option<u64>,
156 delayed_write: bool,
157 flags: u32,
158 fuse_flags: u32,
159 ) -> io::Result<usize> {
160 self.inner.write(
161 ctx,
162 inode,
163 handle,
164 r,
165 size,
166 offset,
167 lock_owner,
168 delayed_write,
169 flags,
170 fuse_flags,
171 )
172 }
173
174 fn release(
175 &self,
176 ctx: &Context,
177 inode: u64,
178 flags: u32,
179 handle: u64,
180 flush: bool,
181 flock_release: bool,
182 lock_owner: Option<u64>,
183 ) -> io::Result<()> {
184 self.inner
185 .release(ctx, inode, flags, handle, flush, flock_release, lock_owner)
186 }
187
188 fn statfs(&self, ctx: &Context, inode: u64) -> io::Result<statvfs64> {
189 self.inner.statfs(ctx, inode)
190 }
191
192 fn getxattr(
193 &self,
194 ctx: &Context,
195 inode: u64,
196 name: &CStr,
197 size: u32,
198 ) -> io::Result<GetxattrReply> {
199 self.inner.getxattr(ctx, inode, name, size)
200 }
201
202 fn listxattr(&self, ctx: &Context, inode: u64, size: u32) -> io::Result<ListxattrReply> {
203 self.inner.listxattr(ctx, inode, size)
204 }
205
206 fn opendir(
208 &self,
209 ctx: &Context,
210 inode: u64,
211 flags: u32,
212 ) -> io::Result<(Option<u64>, OpenOptions)> {
213 let (handle, opts) = self.inner.opendir(ctx, inode, flags)?;
214 Ok((Some(handle.unwrap_or(0)), opts))
215 }
216
217 fn readdir(
218 &self,
219 ctx: &Context,
220 inode: u64,
221 handle: u64,
222 size: u32,
223 offset: u64,
224 add_entry: &mut dyn FnMut(DirEntry) -> io::Result<usize>,
225 ) -> io::Result<()> {
226 self.inner
227 .readdir(ctx, inode, handle, size, offset, add_entry)
228 }
229
230 fn readdirplus(
231 &self,
232 ctx: &Context,
233 inode: u64,
234 handle: u64,
235 size: u32,
236 offset: u64,
237 add_entry: &mut dyn FnMut(DirEntry, Entry) -> io::Result<usize>,
238 ) -> io::Result<()> {
239 self.inner
240 .readdirplus(ctx, inode, handle, size, offset, add_entry)
241 }
242
243 fn releasedir(&self, ctx: &Context, inode: u64, flags: u32, handle: u64) -> io::Result<()> {
244 self.inner.releasedir(ctx, inode, flags, handle)
245 }
246
247 fn access(&self, ctx: &Context, inode: u64, mask: u32) -> io::Result<()> {
248 self.inner.access(ctx, inode, mask)
249 }
250}
251
252impl<L> Layer for ZeroOpenLayer<L>
253where
254 L: Layer<Inode = u64, Handle = u64> + Send + Sync,
255{
256 fn root_inode(&self) -> u64 {
257 self.inner.root_inode()
258 }
259}