btrfs_cli/qgroup/
create.rs1use crate::{Format, Runnable, util::parse_qgroupid};
2use anyhow::{Context, Result};
3use clap::Parser;
4use nix::errno::Errno;
5use std::{fs::File, os::unix::io::AsFd, path::PathBuf};
6
7#[derive(Parser, Debug)]
9pub struct QgroupCreateCommand {
10 pub qgroupid: String,
12
13 pub path: PathBuf,
15}
16
17impl Runnable for QgroupCreateCommand {
18 fn run(&self, _format: Format, _dry_run: bool) -> Result<()> {
19 let qgroupid = parse_qgroupid(&self.qgroupid)?;
20
21 let file = File::open(&self.path).with_context(|| {
22 format!("failed to open '{}'", self.path.display())
23 })?;
24
25 match btrfs_uapi::quota::qgroup_create(file.as_fd(), qgroupid) {
26 Ok(()) => {
27 println!("qgroup {} created", self.qgroupid);
28 Ok(())
29 }
30 Err(Errno::ENOTCONN) => {
31 anyhow::bail!("quota not enabled on '{}'", self.path.display())
32 }
33 Err(e) => Err(e).with_context(|| {
34 format!(
35 "failed to create qgroup '{}' on '{}'",
36 self.qgroupid,
37 self.path.display()
38 )
39 }),
40 }
41 }
42}