summaryrefslogtreecommitdiff
path: root/src/commands
diff options
context:
space:
mode:
authorKent Overstreet <kent.overstreet@linux.dev>2024-05-26 20:38:08 -0400
committerKent Overstreet <kent.overstreet@linux.dev>2024-05-26 20:38:08 -0400
commitaa985e6a44a58fa0d508cda7fdaa63acf872ced8 (patch)
treef15b7a1207f024abb12c6fc8d12c9e7adba778e1 /src/commands
parent5b216318b887283d1b22fda055bb7cf381e6ae10 (diff)
Format with rustfmt
Note that we're using struct/enum align options, which require nightly. Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
Diffstat (limited to 'src/commands')
-rw-r--r--src/commands/list.rs118
-rw-r--r--src/commands/mod.rs8
-rw-r--r--src/commands/mount.rs78
-rw-r--r--src/commands/subvolume.rs42
4 files changed, 148 insertions, 98 deletions
diff --git a/src/commands/list.rs b/src/commands/list.rs
index fd61a516..082c9dc4 100644
--- a/src/commands/list.rs
+++ b/src/commands/list.rs
@@ -1,20 +1,23 @@
-use log::{error};
use bch_bindgen::bcachefs;
-use bch_bindgen::opt_set;
-use bch_bindgen::fs::Fs;
use bch_bindgen::bkey::BkeySC;
-use bch_bindgen::btree::BtreeTrans;
use bch_bindgen::btree::BtreeIter;
-use bch_bindgen::btree::BtreeNodeIter;
use bch_bindgen::btree::BtreeIterFlags;
-use clap::{Parser};
+use bch_bindgen::btree::BtreeNodeIter;
+use bch_bindgen::btree::BtreeTrans;
+use bch_bindgen::fs::Fs;
+use bch_bindgen::opt_set;
+use clap::Parser;
+use log::error;
use std::io::{stdout, IsTerminal};
fn list_keys(fs: &Fs, opt: Cli) -> anyhow::Result<()> {
let trans = BtreeTrans::new(fs);
- let mut iter = BtreeIter::new(&trans, opt.btree, opt.start,
- BtreeIterFlags::ALL_SNAPSHOTS|
- BtreeIterFlags::PREFETCH);
+ let mut iter = BtreeIter::new(
+ &trans,
+ opt.btree,
+ opt.start,
+ BtreeIterFlags::ALL_SNAPSHOTS | BtreeIterFlags::PREFETCH,
+ );
while let Some(k) = iter.peek_and_restart()? {
if k.k.p > opt.end {
@@ -37,9 +40,14 @@ fn list_keys(fs: &Fs, opt: Cli) -> anyhow::Result<()> {
fn list_btree_formats(fs: &Fs, opt: Cli) -> anyhow::Result<()> {
let trans = BtreeTrans::new(fs);
- let mut iter = BtreeNodeIter::new(&trans, opt.btree, opt.start,
- 0, opt.level,
- BtreeIterFlags::PREFETCH);
+ let mut iter = BtreeNodeIter::new(
+ &trans,
+ opt.btree,
+ opt.start,
+ 0,
+ opt.level,
+ BtreeIterFlags::PREFETCH,
+ );
while let Some(b) = iter.peek_and_restart()? {
if b.key.k.p > opt.end {
@@ -55,9 +63,14 @@ fn list_btree_formats(fs: &Fs, opt: Cli) -> anyhow::Result<()> {
fn list_btree_nodes(fs: &Fs, opt: Cli) -> anyhow::Result<()> {
let trans = BtreeTrans::new(fs);
- let mut iter = BtreeNodeIter::new(&trans, opt.btree, opt.start,
- 0, opt.level,
- BtreeIterFlags::PREFETCH);
+ let mut iter = BtreeNodeIter::new(
+ &trans,
+ opt.btree,
+ opt.start,
+ 0,
+ opt.level,
+ BtreeIterFlags::PREFETCH,
+ );
while let Some(b) = iter.peek_and_restart()? {
if b.key.k.p > opt.end {
@@ -73,9 +86,14 @@ fn list_btree_nodes(fs: &Fs, opt: Cli) -> anyhow::Result<()> {
fn list_nodes_ondisk(fs: &Fs, opt: Cli) -> anyhow::Result<()> {
let trans = BtreeTrans::new(fs);
- let mut iter = BtreeNodeIter::new(&trans, opt.btree, opt.start,
- 0, opt.level,
- BtreeIterFlags::PREFETCH);
+ let mut iter = BtreeNodeIter::new(
+ &trans,
+ opt.btree,
+ opt.start,
+ 0,
+ opt.level,
+ BtreeIterFlags::PREFETCH,
+ );
while let Some(b) = iter.peek_and_restart()? {
if b.key.k.p > opt.end {
@@ -102,69 +120,77 @@ enum Mode {
pub struct Cli {
/// Btree to list from
#[arg(short, long, default_value_t=bcachefs::btree_id::BTREE_ID_extents)]
- btree: bcachefs::btree_id,
+ btree: bcachefs::btree_id,
/// Bkey type to list
- #[arg(short='k', long)]
- bkey_type: Option<bcachefs::bch_bkey_type>,
+ #[arg(short = 'k', long)]
+ bkey_type: Option<bcachefs::bch_bkey_type>,
/// Btree depth to descend to (0 == leaves)
- #[arg(short, long, default_value_t=0)]
- level: u32,
+ #[arg(short, long, default_value_t = 0)]
+ level: u32,
/// Start position to list from
- #[arg(short, long, default_value="POS_MIN")]
- start: bcachefs::bpos,
+ #[arg(short, long, default_value = "POS_MIN")]
+ start: bcachefs::bpos,
/// End position
- #[arg(short, long, default_value="SPOS_MAX")]
- end: bcachefs::bpos,
+ #[arg(short, long, default_value = "SPOS_MAX")]
+ end: bcachefs::bpos,
- #[arg(short, long, default_value="keys")]
- mode: Mode,
+ #[arg(short, long, default_value = "keys")]
+ mode: Mode,
/// Check (fsck) the filesystem first
#[arg(short, long)]
- fsck: bool,
+ fsck: bool,
/// Force color on/off. Default: autodetect tty
#[arg(short, long, action = clap::ArgAction::Set, default_value_t=stdout().is_terminal())]
- colorize: bool,
+ colorize: bool,
/// Verbose mode
#[arg(short, long)]
- verbose: bool,
+ verbose: bool,
#[arg(required(true))]
- devices: Vec<std::path::PathBuf>,
+ devices: Vec<std::path::PathBuf>,
}
fn cmd_list_inner(opt: Cli) -> anyhow::Result<()> {
let mut fs_opts: bcachefs::bch_opts = Default::default();
- opt_set!(fs_opts, nochanges, 1);
- opt_set!(fs_opts, read_only, 1);
- opt_set!(fs_opts, norecovery, 1);
- opt_set!(fs_opts, degraded, 1);
- opt_set!(fs_opts, very_degraded, 1);
- opt_set!(fs_opts, errors, bcachefs::bch_error_actions::BCH_ON_ERROR_continue as u8);
+ opt_set!(fs_opts, nochanges, 1);
+ opt_set!(fs_opts, read_only, 1);
+ opt_set!(fs_opts, norecovery, 1);
+ opt_set!(fs_opts, degraded, 1);
+ opt_set!(fs_opts, very_degraded, 1);
+ opt_set!(
+ fs_opts,
+ errors,
+ bcachefs::bch_error_actions::BCH_ON_ERROR_continue as u8
+ );
if opt.fsck {
- opt_set!(fs_opts, fix_errors, bcachefs::fsck_err_opts::FSCK_FIX_yes as u8);
- opt_set!(fs_opts, norecovery, 0);
+ opt_set!(
+ fs_opts,
+ fix_errors,
+ bcachefs::fsck_err_opts::FSCK_FIX_yes as u8
+ );
+ opt_set!(fs_opts, norecovery, 0);
}
if opt.verbose {
- opt_set!(fs_opts, verbose, 1);
+ opt_set!(fs_opts, verbose, 1);
}
let fs = Fs::open(&opt.devices, fs_opts)?;
match opt.mode {
- Mode::Keys => list_keys(&fs, opt),
- Mode::Formats => list_btree_formats(&fs, opt),
- Mode::Nodes => list_btree_nodes(&fs, opt),
- Mode::NodesOndisk => list_nodes_ondisk(&fs, opt),
+ Mode::Keys => list_keys(&fs, opt),
+ Mode::Formats => list_btree_formats(&fs, opt),
+ Mode::Nodes => list_btree_nodes(&fs, opt),
+ Mode::NodesOndisk => list_nodes_ondisk(&fs, opt),
}
}
diff --git a/src/commands/mod.rs b/src/commands/mod.rs
index c7645926..07c25966 100644
--- a/src/commands/mod.rs
+++ b/src/commands/mod.rs
@@ -1,14 +1,14 @@
use clap::Subcommand;
+pub mod completions;
+pub mod list;
pub mod logger;
pub mod mount;
-pub mod list;
-pub mod completions;
pub mod subvolume;
-pub use mount::mount;
-pub use list::list;
pub use completions::completions;
+pub use list::list;
+pub use mount::mount;
pub use subvolume::subvolume;
#[derive(clap::Parser, Debug)]
diff --git a/src/commands/mount.rs b/src/commands/mount.rs
index 79667cca..f8edf9bc 100644
--- a/src/commands/mount.rs
+++ b/src/commands/mount.rs
@@ -1,14 +1,14 @@
-use bch_bindgen::{path_to_cstr, bcachefs, bcachefs::bch_sb_handle, opt_set};
-use log::{info, debug, error, LevelFilter};
-use std::collections::HashMap;
+use crate::key;
+use crate::key::UnlockPolicy;
+use bch_bindgen::{bcachefs, bcachefs::bch_sb_handle, opt_set, path_to_cstr};
use clap::Parser;
-use uuid::Uuid;
+use log::{debug, error, info, LevelFilter};
+use std::collections::HashMap;
+use std::ffi::{c_char, c_void, CString};
use std::io::{stdout, IsTerminal};
use std::path::{Path, PathBuf};
-use std::{fs, str, env};
-use crate::key;
-use crate::key::UnlockPolicy;
-use std::ffi::{CString, c_char, c_void};
+use std::{env, fs, str};
+use uuid::Uuid;
fn mount_inner(
src: String,
@@ -17,7 +17,6 @@ fn mount_inner(
mountflags: libc::c_ulong,
data: Option<String>,
) -> anyhow::Result<()> {
-
// bind the CStrings to keep them alive
let src = CString::new(src)?;
let target = path_to_cstr(target);
@@ -52,22 +51,22 @@ fn parse_mount_options(options: impl AsRef<str>) -> (Option<String>, libc::c_ulo
.as_ref()
.split(',')
.map(|o| match o {
- "dirsync" => Left(libc::MS_DIRSYNC),
- "lazytime" => Left(1 << 25), // MS_LAZYTIME
- "mand" => Left(libc::MS_MANDLOCK),
- "noatime" => Left(libc::MS_NOATIME),
- "nodev" => Left(libc::MS_NODEV),
- "nodiratime" => Left(libc::MS_NODIRATIME),
- "noexec" => Left(libc::MS_NOEXEC),
- "nosuid" => Left(libc::MS_NOSUID),
- "relatime" => Left(libc::MS_RELATIME),
- "remount" => Left(libc::MS_REMOUNT),
- "ro" => Left(libc::MS_RDONLY),
- "rw" => Left(0),
- "strictatime" => Left(libc::MS_STRICTATIME),
- "sync" => Left(libc::MS_SYNCHRONOUS),
- "" => Left(0),
- o => Right(o),
+ "dirsync" => Left(libc::MS_DIRSYNC),
+ "lazytime" => Left(1 << 25), // MS_LAZYTIME
+ "mand" => Left(libc::MS_MANDLOCK),
+ "noatime" => Left(libc::MS_NOATIME),
+ "nodev" => Left(libc::MS_NODEV),
+ "nodiratime" => Left(libc::MS_NODIRATIME),
+ "noexec" => Left(libc::MS_NOEXEC),
+ "nosuid" => Left(libc::MS_NOSUID),
+ "relatime" => Left(libc::MS_RELATIME),
+ "remount" => Left(libc::MS_REMOUNT),
+ "ro" => Left(libc::MS_RDONLY),
+ "rw" => Left(0),
+ "strictatime" => Left(libc::MS_STRICTATIME),
+ "sync" => Left(libc::MS_SYNCHRONOUS),
+ "" => Left(0),
+ o => Right(o),
})
.fold((Vec::new(), 0), |(mut opts, flags), next| match next {
Left(f) => (opts, flags | f),
@@ -235,7 +234,7 @@ pub struct Cli {
/// by the specified passphrase file; it is decrypted. (i.e. Regardless
/// if "fail" is specified for key_location/unlock_policy.)
#[arg(short = 'f', long)]
- passphrase_file: Option<PathBuf>,
+ passphrase_file: Option<PathBuf>,
/// Password policy to use in case of encrypted filesystem.
///
@@ -243,28 +242,33 @@ pub struct Cli {
/// "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;
- #[arg(short = 'k', long = "key_location", default_value = "ask", verbatim_doc_comment)]
- unlock_policy: UnlockPolicy,
+ #[arg(
+ short = 'k',
+ long = "key_location",
+ default_value = "ask",
+ verbatim_doc_comment
+ )]
+ unlock_policy: UnlockPolicy,
/// Device, or UUID=\<UUID\>
- dev: String,
+ dev: String,
/// 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.
- mountpoint: Option<PathBuf>,
+ mountpoint: Option<PathBuf>,
/// Mount options
#[arg(short, default_value = "")]
- options: String,
+ options: String,
/// Force color on/off. Autodetect tty is used to define default:
#[arg(short, long, action = clap::ArgAction::Set, default_value_t=stdout().is_terminal())]
- colorize: bool,
+ colorize: bool,
/// Verbose mode
#[arg(short, long, action = clap::ArgAction::Count)]
- verbose: u8,
+ verbose: u8,
}
fn devs_str_sbs_from_uuid(
@@ -358,7 +362,10 @@ fn cmd_mount_inner(opt: Cli) -> anyhow::Result<()> {
{
// First by password_file, if available
let fallback_to_unlock_policy = if let Some(passphrase_file) = &opt.passphrase_file {
- match key::read_from_passphrase_file(&block_devices_to_mount[0], passphrase_file.as_path()) {
+ match key::read_from_passphrase_file(
+ &block_devices_to_mount[0],
+ passphrase_file.as_path(),
+ ) {
Ok(()) => {
// Decryption succeeded
false
@@ -391,8 +398,7 @@ fn cmd_mount_inner(opt: Cli) -> anyhow::Result<()> {
} else {
info!(
"would mount with params: device: {}, options: {}",
- devices,
- &opt.options
+ devices, &opt.options
);
}
diff --git a/src/commands/subvolume.rs b/src/commands/subvolume.rs
index 5f7cdc76..0691e34d 100644
--- a/src/commands/subvolume.rs
+++ b/src/commands/subvolume.rs
@@ -17,13 +17,13 @@ enum Subcommands {
#[command(visible_aliases = ["new"])]
Create {
/// Paths
- targets: Vec<PathBuf>
+ targets: Vec<PathBuf>,
},
#[command(visible_aliases = ["del"])]
Delete {
/// Path
- target: PathBuf
+ target: PathBuf,
},
#[command(allow_missing_positional = true, visible_aliases = ["snap"])]
@@ -31,9 +31,9 @@ enum Subcommands {
/// Make snapshot read only
#[arg(long, short)]
read_only: bool,
- source: Option<PathBuf>,
- dest: PathBuf
- }
+ source: Option<PathBuf>,
+ dest: PathBuf,
+ },
}
pub fn subvolume(argv: Vec<String>) -> i32 {
@@ -44,24 +44,42 @@ pub fn subvolume(argv: Vec<String>) -> i32 {
for target in targets {
if let Some(dirname) = target.parent() {
let fs = unsafe { BcachefsHandle::open(dirname) };
- fs.create_subvolume(target).expect("Failed to create the subvolume");
+ fs.create_subvolume(target)
+ .expect("Failed to create the subvolume");
}
}
}
- ,
Subcommands::Delete { target } => {
if let Some(dirname) = target.parent() {
let fs = unsafe { BcachefsHandle::open(dirname) };
- fs.delete_subvolume(target).expect("Failed to delete the subvolume");
+ fs.delete_subvolume(target)
+ .expect("Failed to delete the subvolume");
}
- },
- Subcommands::Snapshot { read_only, source, dest } => {
+ }
+ Subcommands::Snapshot {
+ read_only,
+ source,
+ dest,
+ } => {
if let Some(dirname) = dest.parent() {
let dot = PathBuf::from(".");
- let dir = if dirname.as_os_str().is_empty() { &dot } else { dirname };
+ let dir = if dirname.as_os_str().is_empty() {
+ &dot
+ } else {
+ dirname
+ };
let fs = unsafe { BcachefsHandle::open(dir) };
- fs.snapshot_subvolume(if read_only { BCH_SUBVOL_SNAPSHOT_RO } else { 0x0 }, source, dest).expect("Failed to snapshot the subvolume");
+ fs.snapshot_subvolume(
+ if read_only {
+ BCH_SUBVOL_SNAPSHOT_RO
+ } else {
+ 0x0
+ },
+ source,
+ dest,
+ )
+ .expect("Failed to snapshot the subvolume");
}
}
}