summaryrefslogtreecommitdiff
path: root/src/lib.rs
blob: f811ba1f6fd6eac68869993c878a4124d5b47544 (plain)
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
//! An asynchronous and high-level implementation of the Filesystem in Userspace protocol.
//!
//! `blown-fuse`

#![forbid(unsafe_code)]
#![feature(try_trait_v2)]

#[cfg(not(target_os = "linux"))]
compile_error!("Unsupported OS");

use std::{
    marker::PhantomData,
    time::{SystemTime, UNIX_EPOCH},
};

pub use nix;
pub use nix::errno::Errno;
pub use util::{FuseError, FuseResult};

pub mod io;
pub mod mount;
pub mod ops;
pub mod session;

mod proto;
mod util;

pub trait Operation<'o>: private_trait::Sealed + Sized {
    type RequestBody: crate::proto::Structured<'o>;
    type ReplyTail;
}

pub type Op<'o, O = ops::Any> = (Request<'o, O>, Reply<'o, O>);

pub struct Request<'o, O: Operation<'o>> {
    header: proto::InHeader,
    body: O::RequestBody,
}

#[must_use]
pub struct Reply<'o, O: Operation<'o>> {
    session: &'o session::Session,
    unique: u64,
    tail: O::ReplyTail,
}

/// Inode number.
///
/// This is a public newtype. Users are expected to inspect the underlying `u64` and construct
/// arbitrary `Ino` objects.
#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug)]
pub struct Ino(pub u64);

#[must_use]
pub struct Done<'o>(PhantomData<&'o mut &'o ()>);

#[derive(Copy, Clone, Eq, PartialEq)]
pub struct Ttl {
    seconds: u64,
    nanoseconds: u32,
}

#[derive(Copy, Clone, Default, Eq, PartialEq)]
pub struct Timestamp {
    seconds: i64,
    nanoseconds: u32,
}

impl Done<'_> {
    fn new() -> Self {
        Done(PhantomData)
    }

    fn consume(self) {
        drop(self);
    }
}

impl Ino {
    /// The invalid inode number, mostly useful for internal aspects of the FUSE protocol.
    pub const NULL: Self = Ino(0);

    /// The inode number of the root inode as observed by a FUSE client. Other libraries refer to
    /// this as `FUSE_ROOT_ID`.
    ///
    /// Note that a mounted session will remember the inode number given by `Inode::ino()` for the
    /// root inode at initialization and transparently swap between it and `Ino::ROOT`. During
    /// dispatch, requests targeted at `Ino::ROOT` will have this value replaced by the stored root
    /// number, while replies involving the root inode will always report `Ino::ROOT` to the FUSE
    /// client. Therefore, filesystems do not have to be aware of `Ino::ROOT` in most cases.
    pub const ROOT: Self = Ino(proto::ROOT_ID);

    /// Extracts the raw inode number.
    pub fn as_raw(self) -> u64 {
        self.0
    }
}

impl std::fmt::Display for Ino {
    fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(formatter, "{}", self.0)
    }
}

impl Ttl {
    pub const NULL: Self = Ttl {
        seconds: 0,
        nanoseconds: 0,
    };

    pub const MAX: Self = Ttl {
        seconds: u64::MAX,
        nanoseconds: u32::MAX,
    };

    pub fn new(seconds: u64, nanoseconds: u32) -> Ttl {
        assert!(nanoseconds < 1_000_000_000);
        Ttl {
            seconds,
            nanoseconds,
        }
    }

    pub fn seconds(&self) -> u64 {
        self.seconds
    }

    pub fn nanoseconds(&self) -> u32 {
        self.nanoseconds
    }
}

impl Timestamp {
    pub fn new(seconds: i64, nanoseconds: u32) -> Self {
        Timestamp {
            seconds,
            nanoseconds,
        }
    }
}

impl From<SystemTime> for Timestamp {
    fn from(time: SystemTime) -> Self {
        let (seconds, nanoseconds) = match time.duration_since(UNIX_EPOCH) {
            Ok(duration) => {
                let secs = duration.as_secs().try_into().unwrap();
                (secs, duration.subsec_nanos())
            }

            Err(before_epoch) => {
                let duration = before_epoch.duration();
                let secs = -i64::try_from(duration.as_secs()).unwrap();
                (secs, duration.subsec_nanos())
            }
        };

        Timestamp {
            seconds,
            nanoseconds,
        }
    }
}

mod private_trait {
    pub trait Sealed {}
}