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
//! Replies to the kernel.

#![allow(clippy::needless_update)]

use crate::{
    common::{FileAttr, FileLock, StatFs},
    kernel::{
        fuse_attr_out, //
        fuse_bmap_out,
        fuse_entry_out,
        fuse_getxattr_out,
        fuse_lk_out,
        fuse_open_out,
        fuse_poll_out,
        fuse_statfs_out,
        fuse_write_out,
    },
};
use std::time::Duration;

/// Reply with the inode attributes.
#[derive(Debug)]
#[must_use]
pub struct ReplyAttr(fuse_attr_out);

impl AsRef<Self> for ReplyAttr {
    #[inline]
    fn as_ref(&self) -> &Self {
        self
    }
}

impl ReplyAttr {
    /// Create a new `ReplyAttr`.
    pub fn new(attr: FileAttr) -> Self {
        Self(fuse_attr_out {
            attr: attr.into_inner(),
            ..Default::default()
        })
    }

    /// Set the attribute value.
    pub fn attr(&mut self, attr: FileAttr) -> &mut Self {
        self.0.attr = attr.into_inner();
        self
    }

    /// Set the validity timeout for attributes.
    pub fn ttl_attr(&mut self, duration: Duration) -> &mut Self {
        self.0.attr_valid = duration.as_secs();
        self.0.attr_valid_nsec = duration.subsec_nanos();
        self
    }
}

/// Reply with entry params.
#[derive(Debug)]
#[must_use]
pub struct ReplyEntry(fuse_entry_out);

impl AsRef<Self> for ReplyEntry {
    #[inline]
    fn as_ref(&self) -> &Self {
        self
    }
}

impl Default for ReplyEntry {
    fn default() -> Self {
        Self(fuse_entry_out::default())
    }
}

impl ReplyEntry {
    #[doc(hidden)]
    #[deprecated(
        since = "0.3.1",
        note = "The assumption used here is incorrect. \
                See also https://github.com/ubnt-intrepid/polyfuse/issues/65."
    )]
    pub fn new(attr: FileAttr) -> Self {
        let attr = attr.into_inner();
        let nodeid = attr.ino;
        Self(fuse_entry_out {
            nodeid,
            attr,
            ..Default::default()
        })
    }

    /// Set the inode number of this entry.
    ///
    /// If this value is zero, it means that the entry is *negative*.
    /// Returning a negative entry is also possible with the `ENOENT` error,
    /// but the *zeroed* entries also have the ability to specify the lifetime
    /// of the entry cache by using the `ttl_entry` parameter.
    ///
    /// The default value is `0`.
    #[inline]
    pub fn ino(&mut self, ino: u64) -> &mut Self {
        self.0.nodeid = ino;
        self
    }

    /// Set the attribute value of this entry.
    pub fn attr(&mut self, attr: FileAttr) -> &mut Self {
        self.0.attr = attr.into_inner();
        self
    }

    /// Set the validity timeout for inode attributes.
    ///
    /// The operations should set this value to very large
    /// when the changes of inode attributes are caused
    /// only by FUSE requests.
    pub fn ttl_attr(&mut self, duration: Duration) -> &mut Self {
        self.0.attr_valid = duration.as_secs();
        self.0.attr_valid_nsec = duration.subsec_nanos();
        self
    }

    /// Set the validity timeout for the name.
    ///
    /// The operations should set this value to very large
    /// when the changes/deletions of directory entries are
    /// caused only by FUSE requests.
    pub fn ttl_entry(&mut self, duration: Duration) -> &mut Self {
        self.0.entry_valid = duration.as_secs();
        self.0.entry_valid_nsec = duration.subsec_nanos();
        self
    }

    /// Sets the generation of this entry.
    ///
    /// The parameter `generation` is used to distinguish the inode
    /// from the past one when the filesystem reuse inode numbers.
    /// That is, the operations must ensure that the pair of
    /// entry's inode number and `generation` are unique for
    /// the lifetime of the filesystem.
    pub fn generation(&mut self, generation: u64) -> &mut Self {
        self.0.generation = generation;
        self
    }
}

/// Reply with an opened file.
#[derive(Debug)]
#[must_use]
pub struct ReplyOpen(fuse_open_out);

impl AsRef<Self> for ReplyOpen {
    #[inline]
    fn as_ref(&self) -> &Self {
        self
    }
}

impl ReplyOpen {
    /// Create a new `ReplyOpen`.
    pub fn new(fh: u64) -> Self {
        Self(fuse_open_out {
            fh,
            ..Default::default()
        })
    }

    fn set_flag(&mut self, flag: u32, enabled: bool) {
        if enabled {
            self.0.open_flags |= flag;
        } else {
            self.0.open_flags &= !flag;
        }
    }

    /// Set the file handle.
    pub fn fh(&mut self, fh: u64) -> &mut Self {
        self.0.fh = fh;
        self
    }

    /// Indicates that the direct I/O is used on this file.
    pub fn direct_io(&mut self, enabled: bool) -> &mut Self {
        self.set_flag(crate::kernel::FOPEN_DIRECT_IO, enabled);
        self
    }

    /// Indicates that the currently cached file data in the kernel
    /// need not be invalidated.
    pub fn keep_cache(&mut self, enabled: bool) -> &mut Self {
        self.set_flag(crate::kernel::FOPEN_KEEP_CACHE, enabled);
        self
    }

    /// Indicates that the opened file is not seekable.
    pub fn nonseekable(&mut self, enabled: bool) -> &mut Self {
        self.set_flag(crate::kernel::FOPEN_NONSEEKABLE, enabled);
        self
    }

    /// Enable caching of entries returned by `readdir`.
    ///
    /// This flag is meaningful only for `opendir` operations.
    pub fn cache_dir(&mut self, enabled: bool) -> &mut Self {
        self.set_flag(crate::kernel::FOPEN_CACHE_DIR, enabled);
        self
    }
}

/// Reply with the information about written data.
#[derive(Debug)]
#[must_use]
pub struct ReplyWrite(fuse_write_out);

impl AsRef<Self> for ReplyWrite {
    #[inline]
    fn as_ref(&self) -> &Self {
        self
    }
}

impl ReplyWrite {
    /// Create a new `ReplyWrite`.
    pub fn new(size: u32) -> Self {
        Self(fuse_write_out {
            size,
            ..Default::default()
        })
    }

    /// Set the size of written bytes.
    pub fn size(&mut self, size: u32) -> &mut Self {
        self.0.size = size;
        self
    }
}

/// Reply to a request about extended attributes.
#[derive(Debug)]
#[must_use]
pub struct ReplyXattr(fuse_getxattr_out);

impl AsRef<Self> for ReplyXattr {
    #[inline]
    fn as_ref(&self) -> &Self {
        self
    }
}

impl ReplyXattr {
    /// Create a new `ReplyXattr`.
    pub fn new(size: u32) -> Self {
        Self(fuse_getxattr_out {
            size,
            ..Default::default()
        })
    }

    /// Set the actual size of attribute value.
    pub fn size(&mut self, size: u32) -> &mut Self {
        self.0.size = size;
        self
    }
}

/// Reply with the filesystem staticstics.
#[derive(Debug)]
#[must_use]
pub struct ReplyStatfs(fuse_statfs_out);

impl AsRef<Self> for ReplyStatfs {
    #[inline]
    fn as_ref(&self) -> &Self {
        self
    }
}

impl ReplyStatfs {
    /// Create a new `ReplyStatfs`.
    pub fn new(st: StatFs) -> Self {
        Self(fuse_statfs_out {
            st: st.into_inner(),
            ..Default::default()
        })
    }

    /// Set the value of filesystem statistics.
    pub fn stat(&mut self, st: StatFs) -> &mut Self {
        self.0.st = st.into_inner();
        self
    }
}

/// Reply with a file lock.
#[derive(Debug)]
#[must_use]
pub struct ReplyLk(fuse_lk_out);

impl AsRef<Self> for ReplyLk {
    #[inline]
    fn as_ref(&self) -> &Self {
        self
    }
}

impl ReplyLk {
    /// Create a new `ReplyLk`.
    pub fn new(lk: FileLock) -> Self {
        Self(fuse_lk_out {
            lk: lk.into_inner(),
            ..Default::default()
        })
    }

    /// Set the lock information.
    pub fn lock(&mut self, lk: FileLock) -> &mut Self {
        self.0.lk = lk.into_inner();
        self
    }
}

/// Reply with the mapped block index.
#[derive(Debug)]
#[must_use]
pub struct ReplyBmap(fuse_bmap_out);

impl AsRef<Self> for ReplyBmap {
    #[inline]
    fn as_ref(&self) -> &Self {
        self
    }
}

impl ReplyBmap {
    /// Create a new `ReplyBmap`.
    pub fn new(block: u64) -> Self {
        Self(fuse_bmap_out {
            block,
            ..Default::default()
        })
    }

    /// Set the index of mapped block.
    pub fn block(&mut self, block: u64) -> &mut Self {
        self.0.block = block;
        self
    }
}

/// Reply with the poll result.
#[derive(Debug)]
#[must_use]
pub struct ReplyPoll(fuse_poll_out);

impl AsRef<Self> for ReplyPoll {
    #[inline]
    fn as_ref(&self) -> &Self {
        self
    }
}

impl ReplyPoll {
    /// Create a new `ReplyPoll`.
    pub fn new(revents: u32) -> Self {
        Self(fuse_poll_out {
            revents,
            ..Default::default()
        })
    }

    /// Set the mask of ready events.
    pub fn revents(&mut self, revents: u32) -> &mut Self {
        self.0.revents = revents;
        self
    }
}