rust: replace tracing with logger

tracing framework is a overengineered for simple mount helper. Add a
few very barebone logging macros to allow configurable verbosity and
colorized output with a small footprint.

Signed-off-by: Alexander Fougner <fougner89@gmail.com>
This commit is contained in:
Alexander Fougner 2023-01-16 17:08:52 +01:00
parent 20aecb42d8
commit 8accfdc3c5
11 changed files with 156 additions and 346 deletions

View File

@ -191,9 +191,9 @@ checksum = "68354c5c6bd36d73ff3feceb05efa59b6acb7626617f4962be322a825e61f79a"
[[package]] [[package]]
name = "nom" name = "nom"
version = "7.1.2" version = "7.1.3"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e5507769c4919c998e69e49c839d9dc6e693ede4cc4290d6ad8b41d4f09c548c" checksum = "d273983c5a657a70a3e8f2a01329822f3b8c8172b73826411a55751e404a0a4a"
dependencies = [ dependencies = [
"memchr", "memchr",
"minimal-lexical", "minimal-lexical",

View File

@ -9,14 +9,14 @@ crate-type = ["lib"]
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies] [dependencies]
tracing = "0.1.26" chrono = "0.4"
colored = "2"
anyhow = "1.0" anyhow = "1.0"
udev = "0.7.0" udev = "0.7.0"
uuid = "1.2.2" uuid = "1.2.2"
bitfield = "0.14.0" bitfield = "0.14.0"
memoffset = "0.8.0" memoffset = "0.8.0"
byteorder = "1.3" byteorder = "1.3"
tracing-attributes = "0.1.15"
libc = "0.2.69" libc = "0.2.69"
gag = "1.0.0" gag = "1.0.0"

View File

@ -1,7 +1,7 @@
pub mod bcachefs; pub mod bcachefs;
pub mod keyutils; pub mod keyutils;
pub mod log;
pub mod rs; pub mod rs;
pub mod c { pub mod c {
pub use crate::bcachefs::*; pub use crate::bcachefs::*;
} }

View File

@ -0,0 +1,57 @@
use std::sync::atomic::{AtomicU8, Ordering};
pub const MUTE: u8 = 0;
pub const ERROR: u8 = 1;
pub const INFO: u8 = 2;
pub const DEBUG: u8 = 3;
// error level by default
pub static VERBOSE: AtomicU8 = AtomicU8::new(ERROR);
#[inline]
pub fn set_verbose_level(level: u8) {
VERBOSE.store(level, Ordering::SeqCst);
}
pub fn max_level() -> u8 {
VERBOSE.load(Ordering::SeqCst)
}
#[macro_export]
macro_rules! info {
($($arg:tt)*) => {
if 2 <= $crate::log::max_level() {
println!("{} {} {}",
" INFO".green(),
format!("{}:", module_path!()).bright_black(),
format_args!($($arg)*)
);
}
}
}
#[macro_export]
macro_rules! debug {
($($arg:tt)*) => {
if 3 <= $crate::log::max_level() {
println!("{} {} {}",
"DEBUG".bright_blue(),
format!("{}:", module_path!()).bright_black(),
format_args!($($arg)*)
);
}
}
}
#[macro_export]
macro_rules! error {
($($arg:tt)*) => {
if 1 <= $crate::log::max_level() {
println!("{} {} {}",
"ERROR".bright_red(),
format!("{}:", module_path!()).bright_black(),
format_args!($($arg)*)
);
}
}
}

View File

@ -1,4 +1,6 @@
use crate::bcachefs; use crate::bcachefs;
use crate::{error, info};
use colored::Colorize;
pub const SUPERBLOCK_MAGIC: uuid::Uuid = pub const SUPERBLOCK_MAGIC: uuid::Uuid =
uuid::Uuid::from_u128(0x_c68573f6_4e1a_45ca_8265_f57f48ba6d81); uuid::Uuid::from_u128(0x_c68573f6_4e1a_45ca_8265_f57f48ba6d81);
@ -13,27 +15,22 @@ pub enum ReadSuperErr {
type RResult<T> = std::io::Result<std::io::Result<T>>; type RResult<T> = std::io::Result<std::io::Result<T>>;
#[tracing_attributes::instrument(skip(opts))]
pub fn read_super_opts( pub fn read_super_opts(
path: &std::path::Path, path: &std::path::Path,
mut opts: bcachefs::bch_opts, mut opts: bcachefs::bch_opts,
) -> RResult<bcachefs::bch_sb_handle> { ) -> RResult<bcachefs::bch_sb_handle> {
// let devp = camino::Utf8Path::from_path(devp).unwrap();
use std::os::unix::ffi::OsStrExt; use std::os::unix::ffi::OsStrExt;
let path = std::ffi::CString::new(path.as_os_str().as_bytes())?; let path = std::ffi::CString::new(path.as_os_str().as_bytes())?;
let mut sb = std::mem::MaybeUninit::zeroed(); let mut sb = std::mem::MaybeUninit::zeroed();
// use gag::{BufferRedirect};
// // Stop libbcachefs from spamming the output
// let gag = BufferRedirect::stderr().unwrap();
// tracing::trace!("entering libbcachefs");
let ret = let ret =
unsafe { crate::bcachefs::bch2_read_super(path.as_ptr(), &mut opts, sb.as_mut_ptr()) }; unsafe { crate::bcachefs::bch2_read_super(path.as_ptr(), &mut opts, sb.as_mut_ptr()) };
tracing::trace!(%ret); println!("{}", ret);
info!("something");
error!("an error");
String::from("something").bright_black();
match -ret { match -ret {
libc::EACCES => Err(std::io::Error::new( libc::EACCES => Err(std::io::Error::new(
std::io::ErrorKind::PermissionDenied, std::io::ErrorKind::PermissionDenied,
@ -45,7 +42,7 @@ pub fn read_super_opts(
"Not a BCacheFS SuperBlock", "Not a BCacheFS SuperBlock",
))), ))),
code => { code => {
tracing::debug!(msg = "BCacheFS return error code", ?code); println!("BCacheFS return error code: {}", code);
Ok(Err(std::io::Error::new( Ok(Err(std::io::Error::new(
std::io::ErrorKind::Other, std::io::ErrorKind::Other,
"Failed to Read SuperBlock", "Failed to Read SuperBlock",
@ -54,7 +51,6 @@ pub fn read_super_opts(
} }
} }
#[tracing_attributes::instrument]
pub fn read_super(path: &std::path::Path) -> RResult<bcachefs::bch_sb_handle> { pub fn read_super(path: &std::path::Path) -> RResult<bcachefs::bch_sb_handle> {
let opts = bcachefs::bch_opts::default(); //unsafe {std::mem::MaybeUninit::zeroed().assume_init()}; let opts = bcachefs::bch_opts::default(); //unsafe {std::mem::MaybeUninit::zeroed().assume_init()};
read_super_opts(path, opts) read_super_opts(path, opts)

View File

@ -11,30 +11,23 @@ dependencies = [
"memchr", "memchr",
] ]
[[package]]
name = "android_system_properties"
version = "0.1.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "819e7219dbd41043ac279b19830f2efc897156490d7fd6ea916720117ee66311"
dependencies = [
"libc",
]
[[package]]
name = "ansi_term"
version = "0.12.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d52a9bb7ec0cf484c551830a7ce27bd20d67eac647e1befb56b0be4ee39a55d2"
dependencies = [
"winapi",
]
[[package]] [[package]]
name = "anyhow" name = "anyhow"
version = "1.0.68" version = "1.0.68"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2cb2f989d18dd141ab8ae82f64d1a8cdd37e0840f73a406896cf5e99502fab61" checksum = "2cb2f989d18dd141ab8ae82f64d1a8cdd37e0840f73a406896cf5e99502fab61"
[[package]]
name = "atty"
version = "0.2.14"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d9b39be18770d11421cdb1b9947a45dd3f37e93092cbf377614828a319d5fee8"
dependencies = [
"hermit-abi 0.1.19",
"libc",
"winapi",
]
[[package]] [[package]]
name = "autocfg" name = "autocfg"
version = "1.1.0" version = "1.1.0"
@ -46,6 +39,7 @@ name = "bcachefs-mount"
version = "0.3.1" version = "0.3.1"
dependencies = [ dependencies = [
"anyhow", "anyhow",
"atty",
"bch_bindgen", "bch_bindgen",
"byteorder", "byteorder",
"camino", "camino",
@ -116,12 +110,6 @@ version = "1.3.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a" checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a"
[[package]]
name = "bumpalo"
version = "3.11.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "572f695136211188308f16ad2ca5c851a712c464060ae6974944458eb83880ba"
[[package]] [[package]]
name = "byteorder" name = "byteorder"
version = "1.4.3" version = "1.4.3"
@ -155,18 +143,6 @@ version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
[[package]]
name = "chrono"
version = "0.4.23"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "16b0a3d9ed01224b22057780a37bb8c5dbfe1be8ba48678e7bf57ec4b385411f"
dependencies = [
"iana-time-zone",
"num-integer",
"num-traits",
"winapi",
]
[[package]] [[package]]
name = "clang-sys" name = "clang-sys"
version = "1.4.0" version = "1.4.0"
@ -215,66 +191,6 @@ dependencies = [
"os_str_bytes", "os_str_bytes",
] ]
[[package]]
name = "codespan-reporting"
version = "0.11.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3538270d33cc669650c4b093848450d380def10c331d38c768e34cac80576e6e"
dependencies = [
"termcolor",
"unicode-width",
]
[[package]]
name = "core-foundation-sys"
version = "0.8.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5827cebf4670468b8772dd191856768aedcb1b0278a04f989f7766351917b9dc"
[[package]]
name = "cxx"
version = "1.0.86"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "51d1075c37807dcf850c379432f0df05ba52cc30f279c5cfc43cc221ce7f8579"
dependencies = [
"cc",
"cxxbridge-flags",
"cxxbridge-macro",
"link-cplusplus",
]
[[package]]
name = "cxx-build"
version = "1.0.86"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5044281f61b27bc598f2f6647d480aed48d2bf52d6eb0b627d84c0361b17aa70"
dependencies = [
"cc",
"codespan-reporting",
"once_cell",
"proc-macro2",
"quote",
"scratch",
"syn",
]
[[package]]
name = "cxxbridge-flags"
version = "1.0.86"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "61b50bc93ba22c27b0d31128d2d130a0a6b3d267ae27ef7e4fae2167dfe8781c"
[[package]]
name = "cxxbridge-macro"
version = "1.0.86"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "39e61fda7e62115119469c7b3591fd913ecca96fb766cfd3f2e2502ab7bc87a5"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]] [[package]]
name = "either" name = "either"
version = "1.8.0" version = "1.8.0"
@ -358,35 +274,20 @@ checksum = "2540771e65fc8cb83cd6e8a237f70c319bd5c29f78ed1084ba5d50eeac86f7f9"
[[package]] [[package]]
name = "hermit-abi" name = "hermit-abi"
version = "0.2.6" version = "0.1.19"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ee512640fe35acbfb4bb779db6f0d80704c2cacfa2e39b601ef3e3f47d1ae4c7" checksum = "62b467343b94ba476dcb2500d242dadbb39557df889310ac77c5d99100aaac33"
dependencies = [ dependencies = [
"libc", "libc",
] ]
[[package]] [[package]]
name = "iana-time-zone" name = "hermit-abi"
version = "0.1.53" version = "0.2.6"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "64c122667b287044802d6ce17ee2ddf13207ed924c712de9a66a5814d5b64765" checksum = "ee512640fe35acbfb4bb779db6f0d80704c2cacfa2e39b601ef3e3f47d1ae4c7"
dependencies = [ dependencies = [
"android_system_properties", "libc",
"core-foundation-sys",
"iana-time-zone-haiku",
"js-sys",
"wasm-bindgen",
"winapi",
]
[[package]]
name = "iana-time-zone-haiku"
version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0703ae284fc167426161c2e3f1da3ea71d94b21bedbcc9494e92b28e334e3dca"
dependencies = [
"cxx",
"cxx-build",
] ]
[[package]] [[package]]
@ -414,7 +315,7 @@ version = "0.4.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "28dfb6c8100ccc63462345b67d1bbc3679177c75ee4bf59bf29c8b1d110b8189" checksum = "28dfb6c8100ccc63462345b67d1bbc3679177c75ee4bf59bf29c8b1d110b8189"
dependencies = [ dependencies = [
"hermit-abi", "hermit-abi 0.2.6",
"io-lifetimes", "io-lifetimes",
"rustix", "rustix",
"windows-sys", "windows-sys",
@ -429,21 +330,6 @@ dependencies = [
"either", "either",
] ]
[[package]]
name = "itoa"
version = "1.0.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fad582f4b9e86b6caa621cabeb0963332d92eea04729ab12892c2533951e6440"
[[package]]
name = "js-sys"
version = "0.3.60"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "49409df3e3bf0856b916e2ceaca09ee28e6871cf7d9ce97a692cacfdb2a25a47"
dependencies = [
"wasm-bindgen",
]
[[package]] [[package]]
name = "lazy_static" name = "lazy_static"
version = "1.4.0" version = "1.4.0"
@ -472,15 +358,6 @@ dependencies = [
"pkg-config", "pkg-config",
] ]
[[package]]
name = "link-cplusplus"
version = "1.0.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ecd207c9c713c34f95a097a5b029ac2ce6010530c7b49d7fea24d977dede04f5"
dependencies = [
"cc",
]
[[package]] [[package]]
name = "linux-raw-sys" name = "linux-raw-sys"
version = "0.1.4" version = "0.1.4"
@ -496,15 +373,6 @@ dependencies = [
"cfg-if", "cfg-if",
] ]
[[package]]
name = "matchers"
version = "0.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f099785f7595cc4b4553a174ce30dd7589ef93391ff414dbb67f62392b9e0ce1"
dependencies = [
"regex-automata",
]
[[package]] [[package]]
name = "memchr" name = "memchr"
version = "2.5.0" version = "2.5.0"
@ -528,31 +396,22 @@ checksum = "68354c5c6bd36d73ff3feceb05efa59b6acb7626617f4962be322a825e61f79a"
[[package]] [[package]]
name = "nom" name = "nom"
version = "7.1.2" version = "7.1.3"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e5507769c4919c998e69e49c839d9dc6e693ede4cc4290d6ad8b41d4f09c548c" checksum = "d273983c5a657a70a3e8f2a01329822f3b8c8172b73826411a55751e404a0a4a"
dependencies = [ dependencies = [
"memchr", "memchr",
"minimal-lexical", "minimal-lexical",
] ]
[[package]] [[package]]
name = "num-integer" name = "nu-ansi-term"
version = "0.1.45" version = "0.46.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "225d3389fb3509a24c93f5c29eb6bde2586b98d9f016636dff58d7c6f7569cd9" checksum = "77a8165726e8236064dbb45459242600304b42a5ea24ee2948e18e023bf7ba84"
dependencies = [ dependencies = [
"autocfg", "overload",
"num-traits", "winapi",
]
[[package]]
name = "num-traits"
version = "0.2.15"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "578ede34cf02f8924ab9447f50c28075b4d3e5b269972345e7e0372b38c6cdcd"
dependencies = [
"autocfg",
] ]
[[package]] [[package]]
@ -567,6 +426,12 @@ version = "6.4.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9b7820b9daea5457c9f21c69448905d723fbd21136ccf521748f23fd49e723ee" checksum = "9b7820b9daea5457c9f21c69448905d723fbd21136ccf521748f23fd49e723ee"
[[package]]
name = "overload"
version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b15813163c1d831bf4a13c3610c05c0d03b39feb07f7e09fa234dac9b15aaf39"
[[package]] [[package]]
name = "parse-display" name = "parse-display"
version = "0.1.2" version = "0.1.2"
@ -672,15 +537,6 @@ dependencies = [
"regex-syntax", "regex-syntax",
] ]
[[package]]
name = "regex-automata"
version = "0.1.10"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6c230d73fb8d8c1b9c0b3135c5142a8acee3a0558fb8db5cf1cb65f8d7862132"
dependencies = [
"regex-syntax",
]
[[package]] [[package]]
name = "regex-syntax" name = "regex-syntax"
version = "0.6.28" version = "0.6.28"
@ -726,35 +582,6 @@ dependencies = [
"windows-sys", "windows-sys",
] ]
[[package]]
name = "ryu"
version = "1.0.12"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7b4b9743ed687d4b4bcedf9ff5eaa7398495ae14e61cba0a295704edbc7decde"
[[package]]
name = "scratch"
version = "1.0.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ddccb15bcce173023b3fedd9436f882a0739b8dfb45e4f6b6002bee5929f61b2"
[[package]]
name = "serde"
version = "1.0.152"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bb7d1f0d3021d347a83e556fc4683dea2ea09d87bccdf88ff5c12545d89d5efb"
[[package]]
name = "serde_json"
version = "1.0.91"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "877c235533714907a8c2464236f5c4b2a17262ef1bd71f38f35ea592c8da6883"
dependencies = [
"itoa",
"ryu",
"serde",
]
[[package]] [[package]]
name = "sharded-slab" name = "sharded-slab"
version = "0.1.4" version = "0.1.4"
@ -899,36 +726,18 @@ dependencies = [
"tracing-core", "tracing-core",
] ]
[[package]]
name = "tracing-serde"
version = "0.1.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bc6b213177105856957181934e4920de57730fc69bf42c37ee5bb664d406d9e1"
dependencies = [
"serde",
"tracing-core",
]
[[package]] [[package]]
name = "tracing-subscriber" name = "tracing-subscriber"
version = "0.2.25" version = "0.3.16"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0e0d2eaa99c3c2e41547cfa109e910a68ea03823cccad4a0525dcbc9b01e8c71" checksum = "a6176eae26dd70d0c919749377897b54a9276bd7061339665dd68777926b5a70"
dependencies = [ dependencies = [
"ansi_term", "nu-ansi-term",
"chrono",
"lazy_static",
"matchers",
"regex",
"serde",
"serde_json",
"sharded-slab", "sharded-slab",
"smallvec", "smallvec",
"thread_local", "thread_local",
"tracing",
"tracing-core", "tracing-core",
"tracing-log", "tracing-log",
"tracing-serde",
] ]
[[package]] [[package]]
@ -948,12 +757,6 @@ version = "1.0.6"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "84a22b9f218b40614adcb3f4ff08b703773ad44fa9423e4e0d346d5db86e4ebc" checksum = "84a22b9f218b40614adcb3f4ff08b703773ad44fa9423e4e0d346d5db86e4ebc"
[[package]]
name = "unicode-width"
version = "0.1.10"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c0edd1e5b14653f783770bce4a4dabb4a5108a5370a5f5d8cfe8710c361f6c8b"
[[package]] [[package]]
name = "uuid" name = "uuid"
version = "1.2.2" version = "1.2.2"
@ -972,60 +775,6 @@ version = "0.9.4"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "49874b5167b65d7193b8aba1567f5c7d93d001cafc34600cee003eda787e483f" checksum = "49874b5167b65d7193b8aba1567f5c7d93d001cafc34600cee003eda787e483f"
[[package]]
name = "wasm-bindgen"
version = "0.2.83"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "eaf9f5aceeec8be17c128b2e93e031fb8a4d469bb9c4ae2d7dc1888b26887268"
dependencies = [
"cfg-if",
"wasm-bindgen-macro",
]
[[package]]
name = "wasm-bindgen-backend"
version = "0.2.83"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4c8ffb332579b0557b52d268b91feab8df3615f265d5270fec2a8c95b17c1142"
dependencies = [
"bumpalo",
"log",
"once_cell",
"proc-macro2",
"quote",
"syn",
"wasm-bindgen-shared",
]
[[package]]
name = "wasm-bindgen-macro"
version = "0.2.83"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "052be0f94026e6cbc75cdefc9bae13fd6052cdcaf532fa6c45e7ae33a1e6c810"
dependencies = [
"quote",
"wasm-bindgen-macro-support",
]
[[package]]
name = "wasm-bindgen-macro-support"
version = "0.2.83"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "07bc0c051dc5f23e307b13285f9d75df86bfdf816c5721e573dec1f9b8aa193c"
dependencies = [
"proc-macro2",
"quote",
"syn",
"wasm-bindgen-backend",
"wasm-bindgen-shared",
]
[[package]]
name = "wasm-bindgen-shared"
version = "0.2.83"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1c38c045535d93ec4f0b4defec448e4291638ee608530863b1e2ba115d4fff7f"
[[package]] [[package]]
name = "winapi" name = "winapi"
version = "0.3.9" version = "0.3.9"

View File

@ -7,10 +7,10 @@ edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies] [dependencies]
tracing = "0.1.26" atty = "0.2.14"
tracing-log = "0.1.2" log = "0.4"
tracing-subscriber = "0.2.20" chrono = "0.4"
tracing-attributes = "0.1.15" colored = "2"
clap = { version = "4.0.32", features = ["derive", "wrap_help"] } clap = { version = "4.0.32", features = ["derive", "wrap_help"] }
anyhow = "1.0" anyhow = "1.0"
libc = "0.2.69" libc = "0.2.69"
@ -23,6 +23,5 @@ parse-display = "0.1"
errno = "0.2" errno = "0.2"
either = "1.5" either = "1.5"
rpassword = "4" rpassword = "4"
camino = "1.0.5"
bch_bindgen = { path = "../bch_bindgen" } bch_bindgen = { path = "../bch_bindgen" }
byteorder = "1.3" byteorder = "1.3"

View File

@ -1,7 +1,8 @@
extern "C" { extern "C" {
pub static stdout: *mut libc::FILE; pub static stdout: *mut libc::FILE;
} }
use bch_bindgen::{debug, info};
use colored::Colorize;
use getset::{CopyGetters, Getters}; use getset::{CopyGetters, Getters};
use std::path::PathBuf; use std::path::PathBuf;
#[derive(Getters, CopyGetters)] #[derive(Getters, CopyGetters)]
@ -62,14 +63,14 @@ impl FileSystem {
target: impl AsRef<std::path::Path>, target: impl AsRef<std::path::Path>,
options: impl AsRef<str>, options: impl AsRef<str>,
) -> anyhow::Result<()> { ) -> anyhow::Result<()> {
tracing::info_span!("mount").in_scope(|| { let src = self.device_string();
let src = self.device_string(); let (data, mountflags) = parse_mount_options(options);
let (data, mountflags) = parse_mount_options(options);
// let fstype = c_str!("bcachefs");
tracing::info!(msg="mounting bcachefs filesystem", target=%target.as_ref().display()); info!(
mount_inner(src, target, "bcachefs", mountflags, data) "mounting bcachefs filesystem, {}",
}) target.as_ref().display()
);
mount_inner(src, target, "bcachefs", mountflags, data)
} }
} }
@ -100,8 +101,7 @@ fn mount_inner(
let fstype = fstype.as_c_str().to_bytes_with_nul().as_ptr() as *const c_char; let fstype = fstype.as_c_str().to_bytes_with_nul().as_ptr() as *const c_char;
let ret = { let ret = {
let _entered = tracing::info_span!("libc::mount").entered(); info!("mounting filesystem");
tracing::info!("mounting filesystem");
// REQUIRES: CAP_SYS_ADMIN // REQUIRES: CAP_SYS_ADMIN
unsafe { libc::mount(src, target, fstype, mountflags, data) } unsafe { libc::mount(src, target, fstype, mountflags, data) }
}; };
@ -113,10 +113,9 @@ fn mount_inner(
/// Parse a comma-separated mount options and split out mountflags and filesystem /// Parse a comma-separated mount options and split out mountflags and filesystem
/// specific options. /// specific options.
#[tracing_attributes::instrument(skip(options))]
fn parse_mount_options(options: impl AsRef<str>) -> (Option<String>, u64) { fn parse_mount_options(options: impl AsRef<str>) -> (Option<String>, u64) {
use either::Either::*; use either::Either::*;
tracing::debug!(msg="parsing mount options", options=?options.as_ref()); debug!("parsing mount options: {}", options.as_ref());
let (opts, flags) = options let (opts, flags) = options
.as_ref() .as_ref()
.split(",") .split(",")
@ -160,9 +159,8 @@ use bch_bindgen::bcachefs;
use std::collections::HashMap; use std::collections::HashMap;
use uuid::Uuid; use uuid::Uuid;
#[tracing_attributes::instrument]
pub fn probe_filesystems() -> anyhow::Result<HashMap<Uuid, FileSystem>> { pub fn probe_filesystems() -> anyhow::Result<HashMap<Uuid, FileSystem>> {
tracing::trace!("enumerating udev devices"); debug!("enumerating udev devices");
let mut udev = udev::Enumerator::new()?; let mut udev = udev::Enumerator::new()?;
udev.match_subsystem("block")?; // find kernel block devices udev.match_subsystem("block")?; // find kernel block devices
@ -177,7 +175,7 @@ pub fn probe_filesystems() -> anyhow::Result<HashMap<Uuid, FileSystem>> {
match get_super_block_uuid(&pathbuf)? { match get_super_block_uuid(&pathbuf)? {
Ok((uuid_key, superblock)) => { Ok((uuid_key, superblock)) => {
let fs = fs_map.entry(uuid_key).or_insert_with(|| { let fs = fs_map.entry(uuid_key).or_insert_with(|| {
tracing::info!(msg="found bcachefs pool", uuid=?uuid_key); info!("found bcachefs pool: {}", uuid_key);
FileSystem::new(superblock) FileSystem::new(superblock)
}); });
@ -185,12 +183,12 @@ pub fn probe_filesystems() -> anyhow::Result<HashMap<Uuid, FileSystem>> {
} }
Err(e) => { Err(e) => {
tracing::debug!(inner2_error=?e); debug!("{}", e);
} }
} }
} }
tracing::info!(msg = "found filesystems", count = fs_map.len()); info!("found {} filesystems", fs_map.len());
Ok(fs_map) Ok(fs_map)
} }
@ -212,7 +210,7 @@ fn get_super_block_uuid(
drop(gag); drop(gag);
let uuid = (&super_block).sb().uuid(); let uuid = (&super_block).sb().uuid();
tracing::debug!(found="bcachefs superblock", devnode=?path, ?uuid); debug!("bcachefs superblock path={} uuid={}", path.display(), uuid);
Ok(Ok((uuid, super_block))) Ok(Ok((uuid, super_block)))
} }

View File

@ -1,4 +1,5 @@
use tracing::info; use bch_bindgen::info;
use colored::Colorize;
fn check_for_key(key_name: &std::ffi::CStr) -> anyhow::Result<bool> { fn check_for_key(key_name: &std::ffi::CStr) -> anyhow::Result<bool> {
use bch_bindgen::keyutils::{self, keyctl_search}; use bch_bindgen::keyutils::{self, keyctl_search};
@ -7,7 +8,7 @@ fn check_for_key(key_name: &std::ffi::CStr) -> anyhow::Result<bool> {
let key_id = unsafe { keyctl_search(keyutils::KEY_SPEC_USER_KEYRING, key_type, key_name, 0) }; let key_id = unsafe { keyctl_search(keyutils::KEY_SPEC_USER_KEYRING, key_type, key_name, 0) };
if key_id > 0 { if key_id > 0 {
info!("Key has became avaiable"); info!("Key has became available");
Ok(true) Ok(true)
} else if errno::errno().0 != libc::ENOKEY { } else if errno::errno().0 != libc::ENOKEY {
Err(crate::ErrnoError(errno::errno()).into()) Err(crate::ErrnoError(errno::errno()).into())
@ -83,12 +84,11 @@ fn ask_for_key(fs: &FileSystem) -> anyhow::Result<()> {
} }
} }
#[tracing_attributes::instrument]
pub fn prepare_key(fs: &FileSystem, password: crate::KeyLocation) -> anyhow::Result<()> { pub fn prepare_key(fs: &FileSystem, password: crate::KeyLocation) -> anyhow::Result<()> {
use crate::KeyLocation::*; use crate::KeyLocation::*;
use anyhow::anyhow; use anyhow::anyhow;
tracing::info!(msg = "checking if key exists for filesystem"); info!("checking if key exists for filesystem {}", fs.uuid());
match password { match password {
Fail => Err(anyhow!("no key available")), Fail => Err(anyhow!("no key available")),
Wait => Ok(wait_for_key(fs.uuid())?), Wait => Ok(wait_for_key(fs.uuid())?),

View File

@ -1,4 +1,5 @@
use anyhow::anyhow; use anyhow::anyhow;
use atty::Stream;
use clap::Parser; use clap::Parser;
use uuid::Uuid; use uuid::Uuid;
@ -69,6 +70,14 @@ fn parse_fstab_uuid(uuid_raw: &str) -> Result<Uuid, uuid::Error> {
return Uuid::parse_str(&uuid); return Uuid::parse_str(&uuid);
} }
fn stdout_isatty() -> &'static str {
if atty::is(Stream::Stdout) {
"true"
} else {
"false"
}
}
/// Mount a bcachefs filesystem by its UUID. /// Mount a bcachefs filesystem by its UUID.
#[derive(Parser, Debug)] #[derive(Parser, Debug)]
#[command(author, version, about, long_about = None)] #[command(author, version, about, long_about = None)]
@ -85,7 +94,7 @@ pub struct Cli {
/// External UUID of the bcachefs filesystem /// External UUID of the bcachefs filesystem
/// ///
/// Accepts the UUID as is or as fstab style UUID=<UUID> /// Accepts the UUID as is or as fstab style UUID=<UUID>
#[arg(value_parser=parse_fstab_uuid)] #[arg(value_parser = parse_fstab_uuid)]
pub uuid: uuid::Uuid, pub uuid: uuid::Uuid,
/// Where the filesystem should be mounted. If not set, then the filesystem /// Where the filesystem should be mounted. If not set, then the filesystem
@ -96,11 +105,17 @@ pub struct Cli {
/// Mount options /// Mount options
#[arg(short, default_value = "")] #[arg(short, default_value = "")]
pub options: String, pub options: String,
/// Force color on/off. Default: autodetect tty
#[arg(short, long, action = clap::ArgAction::Set, default_value=stdout_isatty())]
pub colorize: bool,
#[arg(short = 'v', long, action = clap::ArgAction::Count)]
pub verbose: u8,
} }
pub mod filesystem; pub mod filesystem;
pub mod key; pub mod key;
// pub fn mnt_in_use() // pub fn mnt_in_use()
#[test] #[test]

View File

@ -1,34 +1,30 @@
use bcachefs_mount::Cli;
use bch_bindgen::{error, info};
use clap::Parser; use clap::Parser;
use colored::Colorize;
fn main() { fn main() {
// convert existing log statements to tracing events let opt = Cli::parse();
// tracing_log::LogTracer::init().expect("logtracer init failed!"); bch_bindgen::log::set_verbose_level(opt.verbose + bch_bindgen::log::ERROR);
// format tracing log data to env_logger like stdout colored::control::set_override(opt.colorize);
tracing_subscriber::fmt::init(); if let Err(e) = crate::main_inner(opt) {
error!("Fatal error: {}", e);
if let Err(e) = crate::main_inner() {
tracing::error!(fatal_error = ?e);
} }
} }
#[tracing_attributes::instrument("main")] pub fn main_inner(opt: Cli) -> anyhow::Result<()> {
pub fn main_inner() -> anyhow::Result<()> { use bcachefs_mount::{filesystem, key};
use bcachefs_mount::{filesystem, key, Cli};
unsafe { unsafe {
libc::setvbuf(filesystem::stdout, std::ptr::null_mut(), libc::_IONBF, 0); libc::setvbuf(filesystem::stdout, std::ptr::null_mut(), libc::_IONBF, 0);
// libc::fflush(filesystem::stdout); // libc::fflush(filesystem::stdout);
} }
let opt = Cli::parse();
tracing::trace!(?opt);
let fss = filesystem::probe_filesystems()?; let fss = filesystem::probe_filesystems()?;
let fs = fss let fs = fss
.get(&opt.uuid) .get(&opt.uuid)
.ok_or_else(|| anyhow::anyhow!("filesystem was not found"))?; .ok_or_else(|| anyhow::anyhow!("filesystem was not found"))?;
tracing::info!(msg="found filesystem", %fs); info!("found filesystem {}", fs);
if fs.encrypted() { if fs.encrypted() {
let key = opt let key = opt
.key_location .key_location