bcachefs-ioctls 0.1.0

A more ergonomic wrapper around bcachefs' ioctl interface.
Documentation
// Copyright 2025 James Taliaferro
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//   http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

mod ioctls;

use anyhow::*;
use uuid::Uuid;

use std::{
    ffi::CString,
    fs,
    os::{fd::AsRawFd, unix::ffi::OsStrExt},
    path,
};

pub fn query_uuid(path: impl AsRef<path::Path>) -> Result<Uuid> {
    let mut query_result: ioctls::QueryUuidPayload = Default::default();
    unsafe { ioctls::query_uuid(fs::File::open(path)?.as_raw_fd(), &mut query_result)? };
    Ok(Uuid::from_bytes(query_result.uuid))
}

enum SubvolOperation {
    Create,
    Destroy,
    // Snapshot
}

fn do_subvolume_ioctl(path: impl AsRef<path::Path>, operation: SubvolOperation) -> Result<()> {
    let path = std::path::absolute(path.as_ref())?;

    let parent = path.parent().context("Path has no parent.")?;
    let parent_handle = fs::File::open(parent)?;
    let dst = CString::new(path.as_os_str().as_bytes())?;

    let payload = ioctls::SubvolumePayload {
        mode: 0o777,
        dst_ptr: dst.as_ptr() as u64,
        ..Default::default()
    };

    let parent_fd = parent_handle.as_raw_fd();

    match operation {
        SubvolOperation::Create => unsafe {
            ioctls::subvolume_create(parent_fd, &payload)?;
        },
        SubvolOperation::Destroy => unsafe {
            ioctls::subvolume_destroy(parent_fd, &payload)?;
        },
    }

    Ok(())
}

pub fn create_subvolume(path: impl AsRef<path::Path>) -> Result<()> {
    do_subvolume_ioctl(path, SubvolOperation::Create)
}

pub fn destroy_subvolume(path: impl AsRef<path::Path>) -> Result<()> {
    do_subvolume_ioctl(path, SubvolOperation::Destroy)
}