Skip to content

Chores: Edition 2018, quick-error -> thiserror, deps update #15

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 8 additions & 6 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ description = """
The type-safe wrapper around mount system call
"""
license = "MIT/Apache-2.0"
edition = "2018"
readme = "README.md"
keywords = ["linux", "container", "mount", "volume", "filesystem"]
homepage = "http://github.com/tailhook/libmount"
Expand All @@ -12,14 +13,15 @@ version = "0.1.15"
authors = ["[email protected]"]

[dependencies]
libc = "0.2.28"
nix = "0.14"
quick-error = "1.2.0"
libc = "^0.2.30"
nix = "0.16"
thiserror = "1.0"
fs-err = "*"

[dev-dependencies]
argparse = "0.2.1"
env_logger = "0.5.10"
log = "0.4.1"
argparse = "0.2"
env_logger = "0.8"
log = "0.4"

[lib]
name = "libmount"
Expand Down
8 changes: 4 additions & 4 deletions examples/overlay_readonly.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
extern crate libmount;
extern crate argparse;
extern crate env_logger;
#[macro_use] extern crate log;



use log::error;

use std::path::PathBuf;
use std::process::exit;
Expand Down
18 changes: 9 additions & 9 deletions src/bind.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ use std::path::Path;

use nix::mount::{MsFlags, mount};

use {OSError, Error};
use util::{path_to_cstring, as_path};
use explain::{Explainable, exists, user};
use remount::Remount;
use crate::{OSError, Error};
use crate::util::{path_to_cstring, as_path};
use crate::explain::{Explainable, exists, user};
use crate::remount::Remount;


/// A mount bind definition
Expand Down Expand Up @@ -60,7 +60,7 @@ impl BindMount {
pub fn bare_mount(self) -> Result<(), OSError> {
let mut flags = MsFlags::MS_BIND;
if self.recursive {
flags = flags | MsFlags::MS_REC;
flags |= MsFlags::MS_REC;
}
if let Err(err) = mount(
Some(&*self.source),
Expand All @@ -72,10 +72,10 @@ impl BindMount {
return Err(OSError::from_nix(err, Box::new(self)));
}
if self.readonly {
try!(Remount::new(OsStr::from_bytes(self.target.as_bytes()))
Remount::new(OsStr::from_bytes(self.target.as_bytes()))
.bind(true)
.readonly(true)
.bare_remount());
.bare_remount()?;
}
Ok(())
}
Expand All @@ -89,7 +89,7 @@ impl BindMount {
impl fmt::Display for BindMount {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
if self.recursive {
try!(write!(fmt, "recursive "));
write!(fmt, "recursive ")?;
}
write!(fmt, "bind mount {:?} -> {:?}",
as_path(&self.source), as_path(&self.target))
Expand All @@ -101,7 +101,7 @@ impl Explainable for BindMount {
[
format!("source: {}", exists(as_path(&self.source))),
format!("target: {}", exists(as_path(&self.target))),
format!("{}", user()),
user().to_string(),
].join(", ")
}
}
Expand Down
18 changes: 6 additions & 12 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,17 @@ use std::io;
use std::fmt;
use std::error::Error as StdError;

use {OSError, Error, MountError};
use remount::RemountError;
use crate::{OSError, Error, MountError};
use crate::remount::RemountError;

impl OSError {
/// Convert error to the one providing extra useful information
pub fn explain(self) -> Error {
let text = self.1.explain();
match self.0 {
MountError::Io(e) => Error(self.1, e, text),
MountError::Remount(RemountError::Io(msg, io_err)) => {
Error(self.1, io_err, format!("{}, {}", msg, text))
MountError::Remount(RemountError::Io(io_err)) => {
Error(self.1, io_err, text)
},
MountError::Remount(err) => {
let text = format!("{}, {}", &err, text);
Expand All @@ -32,12 +32,9 @@ impl fmt::Display for OSError {
}

impl StdError for OSError {
fn cause(&self) -> Option<&StdError> {
fn source(&self) -> Option<&(dyn StdError + 'static)> {
Some(&self.0)
}
fn description(&self) -> &str {
self.0.description()
}
}

impl fmt::Display for Error {
Expand All @@ -47,10 +44,7 @@ impl fmt::Display for Error {
}

impl StdError for Error {
fn cause(&self) -> Option<&StdError> {
fn source(&self) -> Option<&(dyn StdError + 'static)> {
Some(&self.1)
}
fn description(&self) -> &str {
self.1.description()
}
}
4 changes: 2 additions & 2 deletions src/explain.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
use std::io::Read;
use std::fs::File;
use fs_err::File;
use std::fmt::{Display, Debug};
use std::path::Path;

use nix::unistd::getuid;


pub trait Explainable: Display + Debug {
pub trait Explainable: Display + Debug + Send + Sync {
fn explain(&self) -> String;
}

Expand Down
39 changes: 13 additions & 26 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,6 @@
#![warn(missing_debug_implementations)]
#![warn(missing_docs)]

extern crate libc;
extern crate nix;
#[macro_use] extern crate quick_error;

mod util;
mod error;
mod explain;
Expand All @@ -38,26 +34,20 @@ pub mod mountinfo;

use std::io;

use explain::Explainable;
use remount::RemountError;
use crate::explain::Explainable;
pub use bind::BindMount;
pub use overlay::Overlay;
pub use tmpfs::Tmpfs;
pub use modify::Move;
pub use remount::Remount;
pub use crate::remount::{Remount,RemountError};

quick_error! {
#[derive(Debug)]
enum MountError {
Io(err: io::Error) {
cause(err)
from()
}
Remount(err: RemountError) {
cause(err)
from()
}
}
#[derive(Debug, thiserror::Error)]
#[allow(missing_docs)]
enum MountError {
#[error(transparent)]
Io(#[from] io::Error),
#[error(transparent)]
Remount(#[from] RemountError),
}

/// The raw os error
Expand All @@ -75,14 +65,14 @@ quick_error! {
/// path.
///
#[derive(Debug)]
pub struct OSError(MountError, Box<Explainable>);
pub struct OSError(MountError, Box<dyn Explainable + Send + Sync + 'static>);

impl OSError {
fn from_remount(err: RemountError, explain: Box<Explainable>) -> OSError {
fn from_remount(err: RemountError, explain: Box<dyn Explainable + Send + Sync + 'static>) -> OSError {
OSError(MountError::Remount(err), explain)
}

fn from_nix(err: nix::Error, explain: Box<Explainable>) -> OSError {
fn from_nix(err: nix::Error, explain: Box<dyn Explainable + Send + Sync + 'static>) -> OSError {
OSError(
MountError::Io(
err.as_errno().map_or_else(|| io::Error::new(io::ErrorKind::Other, err), io::Error::from),
Expand All @@ -94,8 +84,5 @@ impl OSError {

/// The error holder which contains as much information about why failure
/// happens as the library implementors could gain
///
/// This type only provides `Display` for now, but some programmatic interface
/// is expected in future.
#[derive(Debug)]
pub struct Error(Box<Explainable>, io::Error, String);
pub struct Error(Box<dyn Explainable + Send + Sync + 'static>, io::Error, String);
6 changes: 3 additions & 3 deletions src/modify.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ use std::path::Path;

use nix::mount::{MsFlags, mount};

use {OSError, Error};
use util::{path_to_cstring, as_path};
use explain::{Explainable, exists};
use crate::{OSError, Error};
use crate::util::{path_to_cstring, as_path};
use crate::explain::{Explainable, exists};

/// A move operation definition
///
Expand Down
Loading