2020-05-04 16:28:38 +03:00
|
|
|
use anyhow::anyhow;
|
2023-01-12 22:35:23 +03:00
|
|
|
use clap::Parser;
|
|
|
|
use uuid::Uuid;
|
2021-10-18 20:27:51 +03:00
|
|
|
|
|
|
|
pub mod err {
|
|
|
|
pub enum GError {
|
|
|
|
Unknown{
|
|
|
|
message: std::borrow::Cow<'static, String>
|
|
|
|
}
|
|
|
|
}
|
|
|
|
pub type GResult<T, E, OE> =::core::result::Result< ::core::result::Result<T, E>, OE>;
|
|
|
|
pub type Result<T, E> = GResult<T, E, GError>;
|
|
|
|
}
|
2020-05-04 16:28:38 +03:00
|
|
|
|
|
|
|
#[macro_export]
|
|
|
|
macro_rules! c_str {
|
|
|
|
($lit:expr) => {
|
2021-10-18 20:27:51 +03:00
|
|
|
unsafe {
|
|
|
|
std::ffi::CStr::from_ptr(concat!($lit, "\0").as_ptr() as *const std::os::raw::c_char)
|
|
|
|
.to_bytes_with_nul()
|
|
|
|
.as_ptr() as *const std::os::raw::c_char
|
|
|
|
}
|
2020-05-04 16:28:38 +03:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
struct ErrnoError(errno::Errno);
|
|
|
|
impl std::fmt::Display for ErrnoError {
|
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> {
|
|
|
|
self.0.fmt(f)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
impl std::error::Error for ErrnoError {}
|
|
|
|
|
2023-01-12 22:35:23 +03:00
|
|
|
#[derive(Clone, Debug)]
|
2021-10-18 20:27:51 +03:00
|
|
|
pub enum KeyLocation {
|
2020-05-04 16:28:38 +03:00
|
|
|
Fail,
|
|
|
|
Wait,
|
|
|
|
Ask,
|
|
|
|
}
|
|
|
|
|
2023-01-12 22:35:23 +03:00
|
|
|
#[derive(Clone, Debug)]
|
2021-10-18 20:27:51 +03:00
|
|
|
pub struct KeyLoc(pub Option<KeyLocation>);
|
|
|
|
impl std::ops::Deref for KeyLoc {
|
|
|
|
type Target = Option<KeyLocation>;
|
|
|
|
fn deref(&self) -> &Self::Target {
|
|
|
|
&self.0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
impl std::str::FromStr for KeyLoc {
|
2020-05-04 16:28:38 +03:00
|
|
|
type Err = anyhow::Error;
|
|
|
|
fn from_str(s: &str) -> anyhow::Result<Self> {
|
2021-10-18 20:27:51 +03:00
|
|
|
// use anyhow::anyhow;
|
2020-05-04 16:28:38 +03:00
|
|
|
match s {
|
2021-10-18 20:27:51 +03:00
|
|
|
"" => Ok(KeyLoc(None)),
|
|
|
|
"fail" => Ok(KeyLoc(Some(KeyLocation::Fail))),
|
|
|
|
"wait" => Ok(KeyLoc(Some(KeyLocation::Wait))),
|
|
|
|
"ask" => Ok(KeyLoc(Some(KeyLocation::Ask))),
|
|
|
|
_ => Err(anyhow!("invalid password option")),
|
2020-05-04 16:28:38 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-12 22:35:23 +03:00
|
|
|
fn parse_fstab_uuid(uuid_raw: &str) -> Result<Uuid, uuid::Error> {
|
|
|
|
let mut uuid = String::from(uuid_raw);
|
|
|
|
if uuid.starts_with("UUID=") {
|
|
|
|
uuid = uuid.replacen("UUID=", "", 1);
|
|
|
|
}
|
|
|
|
return Uuid::parse_str(&uuid);
|
|
|
|
}
|
|
|
|
|
2020-05-04 16:28:38 +03:00
|
|
|
/// Mount a bcachefs filesystem by its UUID.
|
2023-01-12 22:35:23 +03:00
|
|
|
#[derive(Parser, Debug)]
|
|
|
|
#[command(author, version, about, long_about = None)]
|
|
|
|
pub struct Cli {
|
2020-05-04 16:28:38 +03:00
|
|
|
/// Where the password would be loaded from.
|
|
|
|
///
|
|
|
|
/// Possible values are:
|
|
|
|
/// "fail" - don't ask for password, fail if filesystem is encrypted;
|
|
|
|
/// "wait" - wait for password to become available before mounting;
|
|
|
|
/// "ask" - prompt the user for password;
|
2023-01-12 22:35:23 +03:00
|
|
|
#[arg(short, long, default_value = "", verbatim_doc_comment)]
|
2021-10-18 20:27:51 +03:00
|
|
|
pub key_location: KeyLoc,
|
2020-05-04 16:28:38 +03:00
|
|
|
|
|
|
|
/// External UUID of the bcachefs filesystem
|
2023-01-12 22:35:23 +03:00
|
|
|
///
|
|
|
|
/// Accepts the UUID as is or as fstab style UUID=<UUID>
|
|
|
|
#[arg(value_parser=parse_fstab_uuid)]
|
2021-10-18 20:27:51 +03:00
|
|
|
pub uuid: uuid::Uuid,
|
2020-05-04 16:28:38 +03:00
|
|
|
|
|
|
|
/// Where the filesystem should be mounted. If not set, then the filesystem
|
|
|
|
/// won't actually be mounted. But all steps preceeding mounting the
|
|
|
|
/// filesystem (e.g. asking for passphrase) will still be performed.
|
2021-10-18 20:27:51 +03:00
|
|
|
pub mountpoint: Option<std::path::PathBuf>,
|
2020-05-04 16:28:38 +03:00
|
|
|
|
|
|
|
/// Mount options
|
2023-01-12 22:35:23 +03:00
|
|
|
#[arg(short, default_value = "")]
|
2021-10-18 20:27:51 +03:00
|
|
|
pub options: String,
|
2020-05-04 16:28:38 +03:00
|
|
|
}
|
|
|
|
|
2021-10-18 20:27:51 +03:00
|
|
|
pub mod filesystem;
|
|
|
|
pub mod key;
|
2020-05-04 16:28:38 +03:00
|
|
|
|
2021-10-18 20:27:51 +03:00
|
|
|
// pub fn mnt_in_use()
|
2023-01-12 22:35:23 +03:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn verify_cli() {
|
|
|
|
use clap::CommandFactory;
|
|
|
|
Cli::command().debug_assert()
|
|
|
|
}
|