mirror of
https://github.com/koverstreet/bcachefs-tools.git
synced 2025-02-23 00:00:02 +03:00
path_to_cstr()
Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
This commit is contained in:
parent
f4f87d9f76
commit
6a41851118
@ -70,6 +70,11 @@ impl fmt::Display for c::btree_id {
|
|||||||
|
|
||||||
use std::str::FromStr;
|
use std::str::FromStr;
|
||||||
use std::ffi::CString;
|
use std::ffi::CString;
|
||||||
|
use std::{path::Path,os::unix::ffi::OsStrExt};
|
||||||
|
|
||||||
|
pub fn path_to_cstr<P: AsRef<Path>>(p: P) -> CString {
|
||||||
|
CString::new(p.as_ref().as_os_str().as_bytes()).unwrap()
|
||||||
|
}
|
||||||
|
|
||||||
use std::error::Error;
|
use std::error::Error;
|
||||||
|
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
use anyhow::anyhow;
|
use anyhow::anyhow;
|
||||||
|
use crate::path_to_cstr;
|
||||||
use crate::bcachefs;
|
use crate::bcachefs;
|
||||||
use crate::bcachefs::*;
|
use crate::bcachefs::*;
|
||||||
use crate::errcode::bch_errcode;
|
use crate::errcode::bch_errcode;
|
||||||
@ -7,13 +8,10 @@ pub fn read_super_opts(
|
|||||||
path: &std::path::Path,
|
path: &std::path::Path,
|
||||||
mut opts: bch_opts,
|
mut opts: bch_opts,
|
||||||
) -> anyhow::Result<bch_sb_handle> {
|
) -> anyhow::Result<bch_sb_handle> {
|
||||||
use std::os::unix::ffi::OsStrExt;
|
let path = path_to_cstr(path);
|
||||||
let path = std::ffi::CString::new(path.as_os_str().as_bytes()).unwrap();
|
|
||||||
|
|
||||||
let mut sb = std::mem::MaybeUninit::zeroed();
|
let mut sb = std::mem::MaybeUninit::zeroed();
|
||||||
|
|
||||||
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()) };
|
|
||||||
|
|
||||||
if ret != 0 {
|
if ret != 0 {
|
||||||
let err: bch_errcode = unsafe { ::std::mem::transmute(ret) };
|
let err: bch_errcode = unsafe { ::std::mem::transmute(ret) };
|
||||||
@ -32,13 +30,10 @@ pub fn read_super_silent(
|
|||||||
path: &std::path::Path,
|
path: &std::path::Path,
|
||||||
mut opts: bch_opts,
|
mut opts: bch_opts,
|
||||||
) -> anyhow::Result<bch_sb_handle> {
|
) -> anyhow::Result<bch_sb_handle> {
|
||||||
use std::os::unix::ffi::OsStrExt;
|
let path = path_to_cstr(path);
|
||||||
let path = std::ffi::CString::new(path.as_os_str().as_bytes()).unwrap();
|
|
||||||
|
|
||||||
let mut sb = std::mem::MaybeUninit::zeroed();
|
let mut sb = std::mem::MaybeUninit::zeroed();
|
||||||
|
|
||||||
let ret =
|
let ret = unsafe { crate::bcachefs::bch2_read_super_silent(path.as_ptr(), &mut opts, sb.as_mut_ptr()) };
|
||||||
unsafe { crate::bcachefs::bch2_read_super_silent(path.as_ptr(), &mut opts, sb.as_mut_ptr()) };
|
|
||||||
|
|
||||||
if ret != 0 {
|
if ret != 0 {
|
||||||
let err: bch_errcode = unsafe { ::std::mem::transmute(ret) };
|
let err: bch_errcode = unsafe { ::std::mem::transmute(ret) };
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
use bch_bindgen::{bcachefs, bcachefs::bch_sb_handle, opt_set};
|
use bch_bindgen::{path_to_cstr, bcachefs, bcachefs::bch_sb_handle, opt_set};
|
||||||
use log::{info, debug, error, LevelFilter};
|
use log::{info, debug, error, LevelFilter};
|
||||||
use clap::Parser;
|
use clap::Parser;
|
||||||
use uuid::Uuid;
|
use uuid::Uuid;
|
||||||
@ -7,7 +7,6 @@ use std::path::PathBuf;
|
|||||||
use crate::key;
|
use crate::key;
|
||||||
use crate::key::UnlockPolicy;
|
use crate::key::UnlockPolicy;
|
||||||
use std::ffi::{CString, c_char, c_void};
|
use std::ffi::{CString, c_char, c_void};
|
||||||
use std::os::unix::ffi::OsStrExt;
|
|
||||||
|
|
||||||
fn mount_inner(
|
fn mount_inner(
|
||||||
src: String,
|
src: String,
|
||||||
@ -19,7 +18,7 @@ fn mount_inner(
|
|||||||
|
|
||||||
// bind the CStrings to keep them alive
|
// bind the CStrings to keep them alive
|
||||||
let src = CString::new(src)?;
|
let src = CString::new(src)?;
|
||||||
let target = CString::new(target.as_ref().as_os_str().as_bytes())?;
|
let target = path_to_cstr(target);
|
||||||
let data = data.map(CString::new).transpose()?;
|
let data = data.map(CString::new).transpose()?;
|
||||||
let fstype = CString::new(fstype)?;
|
let fstype = CString::new(fstype)?;
|
||||||
|
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
use std::{path::Path, os::unix::ffi::OsStrExt, ffi::CString};
|
use std::path::Path;
|
||||||
|
|
||||||
use bch_bindgen::c::{bchfs_handle, BCH_IOCTL_SUBVOLUME_CREATE, BCH_IOCTL_SUBVOLUME_DESTROY, bch_ioctl_subvolume, bcache_fs_open, BCH_SUBVOL_SNAPSHOT_CREATE, bcache_fs_close};
|
use bch_bindgen::c::{bchfs_handle, BCH_IOCTL_SUBVOLUME_CREATE, BCH_IOCTL_SUBVOLUME_DESTROY, bch_ioctl_subvolume, bcache_fs_open, BCH_SUBVOL_SNAPSHOT_CREATE, bcache_fs_close};
|
||||||
|
use bch_bindgen::path_to_cstr;
|
||||||
use errno::Errno;
|
use errno::Errno;
|
||||||
|
|
||||||
/// A handle to a bcachefs filesystem
|
/// A handle to a bcachefs filesystem
|
||||||
@ -13,7 +14,7 @@ impl BcachefsHandle {
|
|||||||
/// Opens a bcachefs filesystem and returns its handle
|
/// Opens a bcachefs filesystem and returns its handle
|
||||||
/// TODO(raitobezarius): how can this not be faillible?
|
/// TODO(raitobezarius): how can this not be faillible?
|
||||||
pub(crate) unsafe fn open<P: AsRef<Path>>(path: P) -> Self {
|
pub(crate) unsafe fn open<P: AsRef<Path>>(path: P) -> Self {
|
||||||
let path = CString::new(path.as_ref().as_os_str().as_bytes()).expect("Failed to cast path into a C-style string");
|
let path = path_to_cstr(path);
|
||||||
Self {
|
Self {
|
||||||
inner: bcache_fs_open(path.as_ptr())
|
inner: bcache_fs_open(path.as_ptr())
|
||||||
}
|
}
|
||||||
@ -59,7 +60,7 @@ impl BcachefsHandle {
|
|||||||
/// Create a subvolume for this bcachefs filesystem
|
/// Create a subvolume for this bcachefs filesystem
|
||||||
/// at the given path
|
/// at the given path
|
||||||
pub fn create_subvolume<P: AsRef<Path>>(&self, dst: P) -> Result<(), Errno> {
|
pub fn create_subvolume<P: AsRef<Path>>(&self, dst: P) -> Result<(), Errno> {
|
||||||
let dst = CString::new(dst.as_ref().as_os_str().as_bytes()).expect("Failed to cast destination path for subvolume in a C-style string");
|
let dst = path_to_cstr(dst);
|
||||||
self.ioctl(BcachefsIoctl::SubvolumeCreate, &BcachefsIoctlPayload::Subvolume(bch_ioctl_subvolume {
|
self.ioctl(BcachefsIoctl::SubvolumeCreate, &BcachefsIoctlPayload::Subvolume(bch_ioctl_subvolume {
|
||||||
dirfd: libc::AT_FDCWD as u32,
|
dirfd: libc::AT_FDCWD as u32,
|
||||||
mode: 0o777,
|
mode: 0o777,
|
||||||
@ -71,7 +72,7 @@ impl BcachefsHandle {
|
|||||||
/// Delete the subvolume at the given path
|
/// Delete the subvolume at the given path
|
||||||
/// for this bcachefs filesystem
|
/// for this bcachefs filesystem
|
||||||
pub fn delete_subvolume<P: AsRef<Path>>(&self, dst: P) -> Result<(), Errno> {
|
pub fn delete_subvolume<P: AsRef<Path>>(&self, dst: P) -> Result<(), Errno> {
|
||||||
let dst = CString::new(dst.as_ref().as_os_str().as_bytes()).expect("Failed to cast destination path for subvolume in a C-style string");
|
let dst = path_to_cstr(dst);
|
||||||
self.ioctl(BcachefsIoctl::SubvolumeDestroy, &BcachefsIoctlPayload::Subvolume(bch_ioctl_subvolume {
|
self.ioctl(BcachefsIoctl::SubvolumeDestroy, &BcachefsIoctlPayload::Subvolume(bch_ioctl_subvolume {
|
||||||
dirfd: libc::AT_FDCWD as u32,
|
dirfd: libc::AT_FDCWD as u32,
|
||||||
mode: 0o777,
|
mode: 0o777,
|
||||||
@ -83,8 +84,8 @@ impl BcachefsHandle {
|
|||||||
/// Snapshot a subvolume for this bcachefs filesystem
|
/// Snapshot a subvolume for this bcachefs filesystem
|
||||||
/// at the given path
|
/// at the given path
|
||||||
pub fn snapshot_subvolume<P: AsRef<Path>>(&self, extra_flags: u32, src: Option<P>, dst: P) -> Result<(), Errno> {
|
pub fn snapshot_subvolume<P: AsRef<Path>>(&self, extra_flags: u32, src: Option<P>, dst: P) -> Result<(), Errno> {
|
||||||
let src = src.map(|src| CString::new(src.as_ref().as_os_str().as_bytes()).expect("Failed to cast source path for subvolume in a C-style string"));
|
let src = src.map(|src| path_to_cstr(src));
|
||||||
let dst = CString::new(dst.as_ref().as_os_str().as_bytes()).expect("Failed to cast destination path for subvolume in a C-style string");
|
let dst = path_to_cstr(dst);
|
||||||
|
|
||||||
let res = self.ioctl(BcachefsIoctl::SubvolumeCreate, &BcachefsIoctlPayload::Subvolume(bch_ioctl_subvolume {
|
let res = self.ioctl(BcachefsIoctl::SubvolumeCreate, &BcachefsIoctlPayload::Subvolume(bch_ioctl_subvolume {
|
||||||
flags: BCH_SUBVOL_SNAPSHOT_CREATE | extra_flags,
|
flags: BCH_SUBVOL_SNAPSHOT_CREATE | extra_flags,
|
||||||
|
Loading…
Reference in New Issue
Block a user