]> git.sesse.net Git - bcachefs-tools-debian/blob - rust-src/mount/src/lib.rs
New upstream snapshot
[bcachefs-tools-debian] / rust-src / mount / src / lib.rs
1 use anyhow::anyhow;
2 use structopt::StructOpt;
3
4 pub mod err {
5         pub enum GError {
6                 Unknown{
7                         message: std::borrow::Cow<'static, String>
8                  }
9         }
10         pub type GResult<T, E, OE> =::core::result::Result< ::core::result::Result<T, E>, OE>;
11         pub type Result<T, E> = GResult<T, E, GError>;
12 }
13
14 #[macro_export]
15 macro_rules! c_str {
16         ($lit:expr) => {
17                 unsafe {
18                         std::ffi::CStr::from_ptr(concat!($lit, "\0").as_ptr() as *const std::os::raw::c_char)
19                                 .to_bytes_with_nul()
20                                 .as_ptr() as *const std::os::raw::c_char
21                 }
22         };
23 }
24
25 #[derive(Debug)]
26 struct ErrnoError(errno::Errno);
27 impl std::fmt::Display for ErrnoError {
28         fn fmt(&self, f: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> {
29                 self.0.fmt(f)
30         }
31 }
32 impl std::error::Error for ErrnoError {}
33
34 #[derive(Debug)]
35 pub enum KeyLocation {
36         Fail,
37         Wait,
38         Ask,
39 }
40
41 #[derive(Debug)]
42 pub struct KeyLoc(pub Option<KeyLocation>);
43 impl std::ops::Deref for KeyLoc {
44         type Target = Option<KeyLocation>;
45         fn deref(&self) -> &Self::Target {
46                 &self.0
47         }
48 }
49 impl std::str::FromStr for KeyLoc {
50         type Err = anyhow::Error;
51         fn from_str(s: &str) -> anyhow::Result<Self> {
52                 // use anyhow::anyhow;
53                 match s {
54                         "" => Ok(KeyLoc(None)),
55                         "fail" => Ok(KeyLoc(Some(KeyLocation::Fail))),
56                         "wait" => Ok(KeyLoc(Some(KeyLocation::Wait))),
57                         "ask" => Ok(KeyLoc(Some(KeyLocation::Ask))),
58                         _ => Err(anyhow!("invalid password option")),
59                 }
60         }
61 }
62
63 #[derive(StructOpt, Debug)]
64 /// Mount a bcachefs filesystem by its UUID.
65 pub struct Options {
66         /// Where the password would be loaded from.
67         ///
68         /// Possible values are:
69         /// "fail" - don't ask for password, fail if filesystem is encrypted;
70         /// "wait" - wait for password to become available before mounting;
71         /// "ask" -  prompt the user for password;
72         #[structopt(short, long, default_value = "")]
73         pub key_location: KeyLoc,
74
75         /// External UUID of the bcachefs filesystem
76         pub uuid: uuid::Uuid,
77
78         /// Where the filesystem should be mounted. If not set, then the filesystem
79         /// won't actually be mounted. But all steps preceeding mounting the
80         /// filesystem (e.g. asking for passphrase) will still be performed.
81         pub mountpoint: Option<std::path::PathBuf>,
82
83         /// Mount options
84         #[structopt(short, default_value = "")]
85         pub options: String,
86 }
87
88 pub mod filesystem;
89 pub mod key;
90
91 // pub fn mnt_in_use()