src/pxar/fuse.rs: style fixups
Signed-off-by: Christian Ebner <c.ebner@proxmox.com> Signed-off-by: Wolfgang Bumiller <w.bumiller@proxmox.com>
This commit is contained in:
parent
d792dc3cd2
commit
b69169e7be
@ -2,16 +2,16 @@
|
|||||||
//!
|
//!
|
||||||
//! Allows to mount the archive as read-only filesystem to inspect its contents.
|
//! Allows to mount the archive as read-only filesystem to inspect its contents.
|
||||||
|
|
||||||
use std::ffi::{OsStr, CString};
|
use std::ffi::{CString, OsStr};
|
||||||
use std::fs::File;
|
use std::fs::File;
|
||||||
use std::io::BufReader;
|
use std::io::BufReader;
|
||||||
use std::os::unix::ffi::OsStrExt;
|
use std::os::unix::ffi::OsStrExt;
|
||||||
use std::path::Path;
|
use std::path::Path;
|
||||||
use std::sync::Mutex;
|
use std::sync::Mutex;
|
||||||
|
|
||||||
|
use failure::{bail, format_err, Error};
|
||||||
|
use libc::{c_char, c_int, c_void, size_t};
|
||||||
use libc;
|
use libc;
|
||||||
use libc::{c_int, c_void, c_char, size_t};
|
|
||||||
use failure::*;
|
|
||||||
|
|
||||||
use super::decoder::Decoder;
|
use super::decoder::Decoder;
|
||||||
|
|
||||||
@ -34,7 +34,12 @@ type StrPtr = *const c_char;
|
|||||||
|
|
||||||
#[link(name = "fuse3")]
|
#[link(name = "fuse3")]
|
||||||
extern "C" {
|
extern "C" {
|
||||||
fn fuse_session_new(args: *const FuseArgs, oprs: *const Operations, size: size_t, op: ConstPtr) -> MutPtr;
|
fn fuse_session_new(
|
||||||
|
args: *const FuseArgs,
|
||||||
|
oprs: *const Operations,
|
||||||
|
size: size_t,
|
||||||
|
op: ConstPtr,
|
||||||
|
) -> MutPtr;
|
||||||
fn fuse_set_signal_handlers(session: ConstPtr) -> c_int;
|
fn fuse_set_signal_handlers(session: ConstPtr) -> c_int;
|
||||||
fn fuse_remove_signal_handlers(session: ConstPtr);
|
fn fuse_remove_signal_handlers(session: ConstPtr);
|
||||||
fn fuse_daemonize(foreground: c_int) -> c_int;
|
fn fuse_daemonize(foreground: c_int) -> c_int;
|
||||||
@ -42,7 +47,7 @@ extern "C" {
|
|||||||
fn fuse_session_unmount(session: ConstPtr);
|
fn fuse_session_unmount(session: ConstPtr);
|
||||||
fn fuse_session_loop(session: ConstPtr) -> c_int;
|
fn fuse_session_loop(session: ConstPtr) -> c_int;
|
||||||
fn fuse_session_destroy(session: ConstPtr);
|
fn fuse_session_destroy(session: ConstPtr);
|
||||||
// fn fuse_reply_attr(req: Request, attr: *const libc::stat, timeout: f64) -> c_int;
|
// fn fuse_reply_attr(req: Request, attr: *const libc::stat, timeout: f64) -> c_int;
|
||||||
fn fuse_reply_err(req: Request, errno: c_int) -> c_int;
|
fn fuse_reply_err(req: Request, errno: c_int) -> c_int;
|
||||||
fn fuse_req_userdata(req: Request) -> MutPtr;
|
fn fuse_req_userdata(req: Request) -> MutPtr;
|
||||||
}
|
}
|
||||||
@ -66,6 +71,7 @@ pub struct Session {
|
|||||||
/// `Operations` defines the callback function table of supported operations.
|
/// `Operations` defines the callback function table of supported operations.
|
||||||
#[repr(C)]
|
#[repr(C)]
|
||||||
#[derive(Default)]
|
#[derive(Default)]
|
||||||
|
#[rustfmt::skip]
|
||||||
struct Operations {
|
struct Operations {
|
||||||
// The order in which the functions are listed matters, as the offset in the
|
// The order in which the functions are listed matters, as the offset in the
|
||||||
// struct defines what function the fuse driver uses.
|
// struct defines what function the fuse driver uses.
|
||||||
@ -122,7 +128,7 @@ impl Session {
|
|||||||
/// default signal handlers.
|
/// default signal handlers.
|
||||||
/// Options have to be provided as comma separated OsStr, e.g.
|
/// Options have to be provided as comma separated OsStr, e.g.
|
||||||
/// ("ro,default_permissions").
|
/// ("ro,default_permissions").
|
||||||
pub fn new(archive_path: &Path, options: &OsStr, verbose: bool)-> Result<Self, Error> {
|
pub fn new(archive_path: &Path, options: &OsStr, verbose: bool) -> Result<Self, Error> {
|
||||||
let file = File::open(archive_path)?;
|
let file = File::open(archive_path)?;
|
||||||
// First argument should be the executable name
|
// First argument should be the executable name
|
||||||
let mut arguments = vec![
|
let mut arguments = vec![
|
||||||
@ -142,31 +148,33 @@ impl Session {
|
|||||||
};
|
};
|
||||||
|
|
||||||
// Register the callback funcitons for the session
|
// Register the callback funcitons for the session
|
||||||
let mut oprs = Operations::default();
|
let mut oprs = Operations::default();
|
||||||
oprs.init = Some(init);
|
oprs.init = Some(init);
|
||||||
oprs.destroy = Some(destroy);
|
oprs.destroy = Some(destroy);
|
||||||
oprs.lookup = Some(lookup);
|
oprs.lookup = Some(lookup);
|
||||||
oprs.getattr = Some(getattr);
|
oprs.getattr = Some(getattr);
|
||||||
oprs.open = Some(open);
|
oprs.open = Some(open);
|
||||||
oprs.read = Some(read);
|
oprs.read = Some(read);
|
||||||
oprs.opendir = Some(opendir);
|
oprs.opendir = Some(opendir);
|
||||||
oprs.readdir = Some(readdir);
|
oprs.readdir = Some(readdir);
|
||||||
|
|
||||||
// By storing the decoder as userdata of the session, each request may
|
// By storing the decoder as userdata of the session, each request may
|
||||||
// access it.
|
// access it.
|
||||||
let reader = BufReader::new(file);
|
let reader = BufReader::new(file);
|
||||||
let decoder = Decoder::new(reader, decoder_callback as fn(&Path) -> Result<(), Error>)?;
|
let decoder = Decoder::new(reader, decoder_callback as fn(&Path) -> Result<(), Error>)?;
|
||||||
let session_decoder = Box::new(Mutex::new(decoder));
|
let session_decoder = Box::new(Mutex::new(decoder));
|
||||||
let session_ptr = unsafe { fuse_session_new(
|
let session_ptr = unsafe {
|
||||||
&args as *const FuseArgs,
|
fuse_session_new(
|
||||||
&oprs as *const Operations,
|
&args as *const FuseArgs,
|
||||||
std::mem::size_of::<Operations>(),
|
&oprs as *const Operations,
|
||||||
// Ownership of session_decoder is passed to the session here.
|
std::mem::size_of::<Operations>(),
|
||||||
// It has to be reclaimed before dropping the session to free
|
// Ownership of session_decoder is passed to the session here.
|
||||||
// the decoder and close the underlying file. This is done inside
|
// It has to be reclaimed before dropping the session to free
|
||||||
// the destroy callback function.
|
// the decoder and close the underlying file. This is done inside
|
||||||
Box::into_raw(session_decoder) as ConstPtr
|
// the destroy callback function.
|
||||||
)};
|
Box::into_raw(session_decoder) as ConstPtr,
|
||||||
|
)
|
||||||
|
};
|
||||||
|
|
||||||
if session_ptr.is_null() {
|
if session_ptr.is_null() {
|
||||||
bail!("error while creating new fuse session");
|
bail!("error while creating new fuse session");
|
||||||
@ -236,22 +244,30 @@ impl Drop for Session {
|
|||||||
/// exclusive handle by running the code inside this context.
|
/// exclusive handle by running the code inside this context.
|
||||||
/// Responses with error code can easily be generated by returning with the
|
/// Responses with error code can easily be generated by returning with the
|
||||||
/// error code.
|
/// error code.
|
||||||
fn run_in_context<F: FnOnce(&mut Decoder<BufReader<File>, fn(&Path) -> Result<(), Error>>) -> Result<(), i32>>(req: Request, code: F) {
|
fn run_in_context<F>(req: Request, code: F)
|
||||||
let ptr = unsafe { fuse_req_userdata(req) as *mut Mutex<Decoder<BufReader<File>, fn(&Path) -> Result<(), Error>>> };
|
where
|
||||||
|
F: FnOnce(&mut Decoder<BufReader<File>, fn(&Path) -> Result<(), Error>>) -> Result<(), i32>,
|
||||||
|
{
|
||||||
|
let ptr = unsafe {
|
||||||
|
fuse_req_userdata(req)
|
||||||
|
as *mut Mutex<Decoder<BufReader<File>, fn(&Path) -> Result<(), Error>>>
|
||||||
|
};
|
||||||
let boxed_decoder = unsafe { Box::from_raw(ptr) };
|
let boxed_decoder = unsafe { Box::from_raw(ptr) };
|
||||||
let result = boxed_decoder.lock()
|
let result = boxed_decoder
|
||||||
|
.lock()
|
||||||
.map(|mut decoder| code(&mut decoder))
|
.map(|mut decoder| code(&mut decoder))
|
||||||
.unwrap_or(Err(libc::ENOENT));
|
.unwrap_or(Err(libc::ENOENT));
|
||||||
|
|
||||||
if let Err(err) = result {
|
if let Err(err) = result {
|
||||||
unsafe { let _res = fuse_reply_err(req, err); }
|
unsafe {
|
||||||
|
let _res = fuse_reply_err(req, err);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Release ownership of boxed decoder, do not drop it.
|
// Release ownership of boxed decoder, do not drop it.
|
||||||
let _ = Box::into_raw(boxed_decoder);
|
let _ = Box::into_raw(boxed_decoder);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/// Callback functions for fuse kernel driver.
|
/// Callback functions for fuse kernel driver.
|
||||||
extern "C" fn init(_decoder: MutPtr) {
|
extern "C" fn init(_decoder: MutPtr) {
|
||||||
// Notting to do here for now
|
// Notting to do here for now
|
||||||
@ -260,7 +276,9 @@ extern "C" fn init(_decoder: MutPtr) {
|
|||||||
/// Cleanup the userdata created while creating the session, which is the decoder
|
/// Cleanup the userdata created while creating the session, which is the decoder
|
||||||
extern "C" fn destroy(decoder: MutPtr) {
|
extern "C" fn destroy(decoder: MutPtr) {
|
||||||
// Get ownership of the decoder and drop it when Box goes out of scope.
|
// Get ownership of the decoder and drop it when Box goes out of scope.
|
||||||
unsafe { Box::from_raw(decoder); }
|
unsafe {
|
||||||
|
Box::from_raw(decoder);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
extern "C" fn lookup(req: Request, _parent: u64, _name: StrPtr) {
|
extern "C" fn lookup(req: Request, _parent: u64, _name: StrPtr) {
|
||||||
@ -310,4 +328,3 @@ extern "C" fn readdir(req: Request, _inode: u64, _size: size_t, _offset: c_int,
|
|||||||
Err(libc::ENOENT)
|
Err(libc::ENOENT)
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user