From 024de6cbea644fac2a0df25f4de906d37572f9df Mon Sep 17 00:00:00 2001 From: Bernhard Schuster Date: Wed, 18 Nov 2020 13:42:03 +0100 Subject: [PATCH 1/8] migrate from quick_error! to thiserror, use fs_err for better io::Error descriptions --- Cargo.toml | 7 ++++--- src/error.rs | 4 ++-- src/explain.rs | 2 +- src/lib.rs | 24 ++++++++---------------- src/overlay.rs | 2 +- src/remount.rs | 38 ++++++++++++++------------------------ 6 files changed, 30 insertions(+), 47 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 4e6da30ff..917d2e598 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -12,9 +12,10 @@ version = "0.1.15" authors = ["paul@colomiets.name"] [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" diff --git a/src/error.rs b/src/error.rs index 95a086004..2ca59ed99 100644 --- a/src/error.rs +++ b/src/error.rs @@ -11,8 +11,8 @@ impl OSError { 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); diff --git a/src/explain.rs b/src/explain.rs index 3be2a9e0d..dce8ef630 100644 --- a/src/explain.rs +++ b/src/explain.rs @@ -1,5 +1,5 @@ use std::io::Read; -use std::fs::File; +use fs_err::File; use std::fmt::{Display, Debug}; use std::path::Path; diff --git a/src/lib.rs b/src/lib.rs index e53c3f6bd..5f49103c7 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -24,7 +24,7 @@ extern crate libc; extern crate nix; -#[macro_use] extern crate quick_error; +extern crate fs_err; mod util; mod error; @@ -46,18 +46,13 @@ pub use tmpfs::Tmpfs; pub use modify::Move; pub use remount::Remount; -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 @@ -94,8 +89,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, io::Error, String); diff --git a/src/overlay.rs b/src/overlay.rs index bccd8676c..d67a3688b 100644 --- a/src/overlay.rs +++ b/src/overlay.rs @@ -1,6 +1,6 @@ use std::fmt; use std::path::{Path, PathBuf}; -use std::fs::metadata; +use fs_err::metadata; use std::ffi::{CStr, CString}; use std::os::unix::fs::MetadataExt; use std::os::unix::ffi::OsStrExt; diff --git a/src/remount.rs b/src/remount.rs index 807626f86..87e24cade 100644 --- a/src/remount.rs +++ b/src/remount.rs @@ -1,7 +1,7 @@ use std::io; use std::fmt; use std::ffi::CStr; -use std::fs::File; +use fs_err::File; use std::io::Read; use std::path::{Path, PathBuf}; use std::env::current_dir; @@ -67,23 +67,17 @@ fn apply_flag(flags: MsFlags, flag: MsFlags, set: Option) -> MsFlags { } } -quick_error! { - #[derive(Debug)] - pub enum RemountError { - Io(msg: String, err: io::Error) { - cause(err) - display("{}: {}", msg, err) - description(err.description()) - from(err: io::Error) -> (String::new(), err) - } - ParseMountInfo(err: String) { - display("{}", err) - from() - } - UnknownMountPoint(path: PathBuf) { - display("Cannot find mount point: {:?}", path) - } - } +#[derive(Debug, thiserror::Error)] +#[allow(missing_docs)] +pub enum RemountError { + #[error(transparent)] + Io(#[from] io::Error), + + #[error("{0}")] + ParseMountInfo(String), + + #[error("Cannot find mount point: {0:?}")] + UnknownMountPoint(PathBuf), } impl Remount { @@ -266,12 +260,8 @@ fn get_mountpoint_flags(path: &Path) -> Result { }; let mut mountinfo_content = Vec::with_capacity(4 * 1024); let mountinfo_path = Path::new("/proc/self/mountinfo"); - let mut mountinfo_file = try!(File::open(mountinfo_path) - .map_err(|e| RemountError::Io( - format!("Cannot open file: {:?}", mountinfo_path), e))); - try!(mountinfo_file.read_to_end(&mut mountinfo_content) - .map_err(|e| RemountError::Io( - format!("Cannot read file: {:?}", mountinfo_path), e))); + let mut mountinfo_file = File::open(mountinfo_path)?; + mountinfo_file.read_to_end(&mut mountinfo_content)?; match get_mountpoint_flags_from(&mountinfo_content, &mount_path) { Ok(Some(flags)) => Ok(flags), Ok(None) => Err(RemountError::UnknownMountPoint(mount_path)), From 722e0f37fa27438aec833d3acc94608fd13e66fc Mon Sep 17 00:00:00 2001 From: Bernhard Schuster Date: Wed, 18 Nov 2020 13:43:25 +0100 Subject: [PATCH 2/8] bump other deps --- Cargo.toml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 917d2e598..fb81ba88d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -18,9 +18,9 @@ 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" From e1e24775073971a07d7b52087ca922c97f75b85c Mon Sep 17 00:00:00 2001 From: Bernhard Schuster Date: Wed, 18 Nov 2020 13:43:49 +0100 Subject: [PATCH 3/8] start migration to edition 2018 --- examples/overlay_readonly.rs | 8 ++++---- src/error.rs | 2 +- src/lib.rs | 7 +------ 3 files changed, 6 insertions(+), 11 deletions(-) diff --git a/examples/overlay_readonly.rs b/examples/overlay_readonly.rs index 546d611dc..6b149dd08 100644 --- a/examples/overlay_readonly.rs +++ b/examples/overlay_readonly.rs @@ -1,7 +1,7 @@ -extern crate libmount; -extern crate argparse; -extern crate env_logger; -#[macro_use] extern crate log; +use libmount; +use argparse; +use env_logger; +use log::error; use std::path::PathBuf; use std::process::exit; diff --git a/src/error.rs b/src/error.rs index 2ca59ed99..df20b19d3 100644 --- a/src/error.rs +++ b/src/error.rs @@ -2,7 +2,7 @@ use std::io; use std::fmt; use std::error::Error as StdError; -use {OSError, Error, MountError}; +use crate::{OSError, Error, MountError}; use remount::RemountError; impl OSError { diff --git a/src/lib.rs b/src/lib.rs index 5f49103c7..aae938973 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -22,10 +22,6 @@ #![warn(missing_debug_implementations)] #![warn(missing_docs)] -extern crate libc; -extern crate nix; -extern crate fs_err; - mod util; mod error; mod explain; @@ -39,12 +35,11 @@ pub mod mountinfo; use std::io; use explain::Explainable; -use remount::RemountError; pub use bind::BindMount; pub use overlay::Overlay; pub use tmpfs::Tmpfs; pub use modify::Move; -pub use remount::Remount; +pub use remount::{Remount,RemountError}; #[derive(Debug, thiserror::Error)] #[allow(missing_docs)] From 74af3cf13d9f2dfee518402b7e30564f37c1db2c Mon Sep 17 00:00:00 2001 From: Bernhard Schuster Date: Wed, 18 Nov 2020 13:48:19 +0100 Subject: [PATCH 4/8] edition 2018 --- Cargo.toml | 1 + src/bind.rs | 14 +++++++------- src/error.rs | 2 +- src/lib.rs | 4 ++-- src/modify.rs | 6 +++--- src/mountinfo.rs | 42 +++++++++++++++++++++--------------------- src/overlay.rs | 6 +++--- src/remount.rs | 36 ++++++++++++++++++------------------ src/tmpfs.rs | 6 +++--- 9 files changed, 59 insertions(+), 58 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index fb81ba88d..e5c4b9560 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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" diff --git a/src/bind.rs b/src/bind.rs index f69b046b4..ab6ca4c23 100644 --- a/src/bind.rs +++ b/src/bind.rs @@ -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 @@ -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(()) } @@ -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)) diff --git a/src/error.rs b/src/error.rs index df20b19d3..65468395b 100644 --- a/src/error.rs +++ b/src/error.rs @@ -3,7 +3,7 @@ use std::fmt; use std::error::Error as StdError; use crate::{OSError, Error, MountError}; -use remount::RemountError; +use crate::remount::RemountError; impl OSError { /// Convert error to the one providing extra useful information diff --git a/src/lib.rs b/src/lib.rs index aae938973..32de6a6f9 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -34,12 +34,12 @@ pub mod mountinfo; use std::io; -use explain::Explainable; +use crate::explain::Explainable; pub use bind::BindMount; pub use overlay::Overlay; pub use tmpfs::Tmpfs; pub use modify::Move; -pub use remount::{Remount,RemountError}; +pub use crate::remount::{Remount,RemountError}; #[derive(Debug, thiserror::Error)] #[allow(missing_docs)] diff --git a/src/modify.rs b/src/modify.rs index 15187a6b6..69141c497 100644 --- a/src/modify.rs +++ b/src/modify.rs @@ -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 /// diff --git a/src/mountinfo.rs b/src/mountinfo.rs index 58e9113fe..9fee699de 100644 --- a/src/mountinfo.rs +++ b/src/mountinfo.rs @@ -172,16 +172,16 @@ pub(crate) fn parse_mount_point<'a>(row: &'a [u8]) return Ok(None); } - let (mount_id, row) = try!(parse_int(row)); - let (parent_id, row) = try!(parse_int(row)); - let (major, minor, row) = try!(parse_major_minor(row)); - let (root, row) = try!(parse_os_str(row)); - let (mount_point, row) = try!(parse_os_str(row)); - let (mount_options, row) = try!(parse_os_str(row)); - let (optional_fields, row) = try!(parse_optional(row)); - let (fstype, row) = try!(parse_os_str(row)); - let (mount_source, row) = try!(parse_os_str(row)); - let (super_options, _) = try!(parse_os_str(row)); + let (mount_id, row) = parse_int(row)?; + let (parent_id, row) = parse_int(row)?; + let (major, minor, row) = parse_major_minor(row)?; + let (root, row) = parse_os_str(row)?; + let (mount_point, row) = parse_os_str(row)?; + let (mount_options, row) = parse_os_str(row)?; + let (optional_fields, row) = parse_optional(row)?; + let (fstype, row) = parse_os_str(row)?; + let (mount_source, row) = parse_os_str(row)?; + let (super_options, _) = parse_os_str(row)?; // TODO: should we ignore extra fields? Ok(Some(MountPoint { mount_id: mount_id, @@ -235,38 +235,38 @@ fn parse_field<'a>(data: &'a [u8], delimit: &'a [u8]) fn parse_os_str<'a>(data: &'a [u8]) -> Result<(Cow<'a, OsStr>, &'a [u8]), ParseRowError> { - let (field, tail) = try!(parse_field(data, b" ")); + let (field, tail) = parse_field(data, b" ")?; Ok((unescape_octals(OsStr::from_bytes(field)), tail)) } fn parse_int(data: &[u8]) -> Result<(c_ulong, &[u8]), ParseRowError> { - let (field, tail) = try!(parse_field(data, b" ")); - let v = try!(std::str::from_utf8(field).map_err(|e| { + let (field, tail) = parse_field(data, b" ")?; + let v = std::str::from_utf8(field).map_err(|e| { ParseRowError(format!("Cannot parse integer {:?}: {}", - String::from_utf8_lossy(field).into_owned(), e))})); + String::from_utf8_lossy(field).into_owned(), e))})?; - let v = try!(c_ulong::from_str_radix(v, 10).map_err(|e| { + let v = c_ulong::from_str_radix(v, 10).map_err(|e| { ParseRowError(format!("Cannot parse integer {:?}: {}", - String::from_utf8_lossy(field).into_owned(), e))})); + String::from_utf8_lossy(field).into_owned(), e))})?; Ok((v, tail)) } fn parse_major_minor(data: &[u8]) -> Result<(c_ulong, c_ulong, &[u8]), ParseRowError> { - let (major_field, data) = try!(parse_field(data, b":")); - let (minor_field, tail) = try!(parse_field(data, b" ")); - let (major, _) = try!(parse_int(major_field)); - let (minor, _) = try!(parse_int(minor_field)); + let (major_field, data) = parse_field(data, b":")?; + let (minor_field, tail) = parse_field(data, b" ")?; + let (major, _) = parse_int(major_field)?; + let (minor, _) = parse_int(minor_field)?; Ok((major, minor, tail)) } fn parse_optional<'a>(data: &'a [u8]) -> Result<(Cow<'a, OsStr>, &'a [u8]), ParseRowError> { - let (field, tail) = try!(parse_field(data, b"- ")); + let (field, tail) = parse_field(data, b"- ")?; let field = rstrip_whitespaces(field); Ok((unescape_octals(OsStr::from_bytes(field)), tail)) } diff --git a/src/overlay.rs b/src/overlay.rs index d67a3688b..865f2c0a8 100644 --- a/src/overlay.rs +++ b/src/overlay.rs @@ -7,9 +7,9 @@ use std::os::unix::ffi::OsStrExt; use nix::mount::{MsFlags, mount}; -use util::{path_to_cstring, as_path}; -use {OSError, Error}; -use explain::{Explainable, exists, user}; +use crate::util::{path_to_cstring, as_path}; +use crate::{OSError, Error}; +use crate::explain::{Explainable, exists, user}; /// An overlay mount point diff --git a/src/remount.rs b/src/remount.rs index 87e24cade..4da08ddc1 100644 --- a/src/remount.rs +++ b/src/remount.rs @@ -9,10 +9,10 @@ use std::default::Default; use nix::mount::{MsFlags, mount}; -use {OSError, Error}; -use util::path_to_cstring; -use explain::{Explainable, exists, user}; -use mountinfo::{parse_mount_point}; +use crate::{OSError, Error}; +use crate::util::path_to_cstring; +use crate::explain::{Explainable, exists, user}; +use crate::mountinfo::{parse_mount_point}; /// A remount definition /// @@ -182,51 +182,51 @@ impl fmt::Display for MountFlags { fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { let mut prefix = ""; if let Some(true) = self.bind { - try!(write!(fmt, "{}bind", prefix)); + write!(fmt, "{}bind", prefix)?; prefix = ","; } if let Some(true) = self.readonly { - try!(write!(fmt, "{}ro", prefix)); + write!(fmt, "{}ro", prefix)?; prefix = ","; } if let Some(true) = self.nodev { - try!(write!(fmt, "{}nodev", prefix)); + write!(fmt, "{}nodev", prefix)?; prefix = ","; } if let Some(true) = self.noexec { - try!(write!(fmt, "{}noexec", prefix)); + write!(fmt, "{}noexec", prefix)?; prefix = ","; } if let Some(true) = self.nosuid { - try!(write!(fmt, "{}nosuid", prefix)); + write!(fmt, "{}nosuid", prefix)?; prefix = ","; } if let Some(true) = self.noatime { - try!(write!(fmt, "{}noatime", prefix)); + write!(fmt, "{}noatime", prefix)?; prefix = ","; } if let Some(true) = self.nodiratime { - try!(write!(fmt, "{}nodiratime", prefix)); + write!(fmt, "{}nodiratime", prefix)?; prefix = ","; } if let Some(true) = self.relatime { - try!(write!(fmt, "{}relatime", prefix)); + write!(fmt, "{}relatime", prefix)?; prefix = ","; } if let Some(true) = self.strictatime { - try!(write!(fmt, "{}strictatime", prefix)); + write!(fmt, "{}strictatime", prefix)?; prefix = ","; } if let Some(true) = self.dirsync { - try!(write!(fmt, "{}dirsync", prefix)); + write!(fmt, "{}dirsync", prefix)?; prefix = ","; } if let Some(true) = self.synchronous { - try!(write!(fmt, "{}sync", prefix)); + write!(fmt, "{}sync", prefix)?; prefix = ","; } if let Some(true) = self.mandlock { - try!(write!(fmt, "{}mand", prefix)); + write!(fmt, "{}mand", prefix)?; } Ok(()) } @@ -235,7 +235,7 @@ impl fmt::Display for MountFlags { impl fmt::Display for Remount { fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { if !self.flags.apply_to_flags(MsFlags::empty()).is_empty() { - try!(write!(fmt, "{} ", self.flags)); + write!(fmt, "{} ", self.flags)?; } write!(fmt, "remount {:?}", &self.path) } @@ -254,7 +254,7 @@ fn get_mountpoint_flags(path: &Path) -> Result { let mount_path = if path.is_absolute() { path.to_path_buf() } else { - let mut mpath = try!(current_dir()); + let mut mpath = current_dir()?; mpath.push(path); mpath }; diff --git a/src/tmpfs.rs b/src/tmpfs.rs index 5c9f3e7a0..fc0309a79 100644 --- a/src/tmpfs.rs +++ b/src/tmpfs.rs @@ -7,9 +7,9 @@ use std::path::Path; use libc::{uid_t, gid_t, mode_t}; use nix::mount::{MsFlags, mount}; -use {OSError, Error}; -use util::{path_to_cstring, as_path}; -use explain::{Explainable, exists, user}; +use crate::{OSError, Error}; +use crate::util::{path_to_cstring, as_path}; +use crate::explain::{Explainable, exists, user}; #[derive(Debug, Clone, Copy)] From fc0bbd2cfa835ac30677c027fc7a2cd83812613c Mon Sep 17 00:00:00 2001 From: Bernhard Schuster Date: Wed, 18 Nov 2020 14:02:48 +0100 Subject: [PATCH 5/8] cargo clippy run --- examples/overlay_readonly.rs | 6 ++--- src/bind.rs | 4 +-- src/error.rs | 4 +-- src/lib.rs | 8 +++--- src/mountinfo.rs | 48 ++++++++++++++++++------------------ src/overlay.rs | 6 ++--- src/remount.rs | 2 +- src/tmpfs.rs | 6 ++--- 8 files changed, 42 insertions(+), 42 deletions(-) diff --git a/examples/overlay_readonly.rs b/examples/overlay_readonly.rs index 6b149dd08..fa909be3a 100644 --- a/examples/overlay_readonly.rs +++ b/examples/overlay_readonly.rs @@ -1,6 +1,6 @@ -use libmount; -use argparse; -use env_logger; + + + use log::error; use std::path::PathBuf; diff --git a/src/bind.rs b/src/bind.rs index ab6ca4c23..3f4e6d052 100644 --- a/src/bind.rs +++ b/src/bind.rs @@ -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), @@ -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(", ") } } diff --git a/src/error.rs b/src/error.rs index 65468395b..94cb4dc84 100644 --- a/src/error.rs +++ b/src/error.rs @@ -32,7 +32,7 @@ impl fmt::Display for OSError { } impl StdError for OSError { - fn cause(&self) -> Option<&StdError> { + fn cause(&self) -> Option<&dyn StdError> { Some(&self.0) } fn description(&self) -> &str { @@ -47,7 +47,7 @@ impl fmt::Display for Error { } impl StdError for Error { - fn cause(&self) -> Option<&StdError> { + fn cause(&self) -> Option<&dyn StdError> { Some(&self.1) } fn description(&self) -> &str { diff --git a/src/lib.rs b/src/lib.rs index 32de6a6f9..289a78686 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -65,14 +65,14 @@ enum MountError { /// path. /// #[derive(Debug)] -pub struct OSError(MountError, Box); +pub struct OSError(MountError, Box); impl OSError { - fn from_remount(err: RemountError, explain: Box) -> OSError { + fn from_remount(err: RemountError, explain: Box) -> OSError { OSError(MountError::Remount(err), explain) } - fn from_nix(err: nix::Error, explain: Box) -> OSError { + fn from_nix(err: nix::Error, explain: Box) -> OSError { OSError( MountError::Io( err.as_errno().map_or_else(|| io::Error::new(io::ErrorKind::Other, err), io::Error::from), @@ -85,4 +85,4 @@ impl OSError { /// The error holder which contains as much information about why failure /// happens as the library implementors could gain #[derive(Debug)] -pub struct Error(Box, io::Error, String); +pub struct Error(Box, io::Error, String); diff --git a/src/mountinfo.rs b/src/mountinfo.rs index 9fee699de..4001ded7e 100644 --- a/src/mountinfo.rs +++ b/src/mountinfo.rs @@ -1,6 +1,6 @@ //! This module contains parser for /proc/PID/mountinfo //! -use std; + use std::fmt; use std::ffi::{OsStr, OsString}; use std::os::unix::ffi::{OsStrExt, OsStringExt}; @@ -23,7 +23,7 @@ impl fmt::Display for ParseRowError { impl Error for ParseRowError { fn description(&self) -> &str { - return &self.0; + &self.0 } } @@ -38,9 +38,9 @@ pub struct ParseError { impl ParseError { fn new(msg: String, row_num: usize, row: String) -> ParseError { ParseError { - msg: msg, - row_num: row_num, - row: row, + msg, + row_num, + row, } } } @@ -54,7 +54,7 @@ impl fmt::Display for ParseError { impl Error for ParseError { fn description(&self) -> &str { - return &self.msg; + &self.msg } } @@ -73,7 +73,7 @@ impl<'a> Parser<'a> { /// `data` should contain whole contents of `mountinfo` file of any process pub fn new(data: &'a [u8]) -> Parser<'a> { Parser { - data: data, + data, row_num: 0, exhausted: false, } @@ -184,17 +184,17 @@ pub(crate) fn parse_mount_point<'a>(row: &'a [u8]) let (super_options, _) = parse_os_str(row)?; // TODO: should we ignore extra fields? Ok(Some(MountPoint { - mount_id: mount_id, - parent_id: parent_id, - major: major, - minor: minor, - root: root, - mount_point: mount_point, - mount_options: mount_options, - optional_fields: optional_fields, - fstype: fstype, - mount_source: mount_source, - super_options: super_options, + mount_id, + parent_id, + major, + minor, + root, + mount_point, + mount_options, + optional_fields, + fstype, + mount_source, + super_options, })) } @@ -211,7 +211,7 @@ fn is_comment_line(row: &[u8]) -> bool { } return false; } - return false; + false } fn rstrip_cr(row: &[u8]) -> &[u8] { @@ -226,7 +226,7 @@ fn parse_field<'a>(data: &'a [u8], delimit: &'a [u8]) -> Result<(&'a [u8], &'a [u8]), ParseRowError> { if data.is_empty() { - return Err(ParseRowError(format!("Expected more fields"))); + return Err(ParseRowError("Expected more fields".to_string())); } let data = lstrip_whitespaces(data); Ok(split_by(data, delimit)) @@ -277,7 +277,7 @@ fn lstrip_whitespaces(v: &[u8]) -> &[u8] { return &v[i..]; } } - return &v[0..0]; + &v[0..0] } fn rstrip_whitespaces(v: &[u8]) -> &[u8] { @@ -286,7 +286,7 @@ fn rstrip_whitespaces(v: &[u8]) -> &[u8] { return &v[..i + 1]; } } - return &v[0..0]; + &v[0..0] } fn split_by<'a, 'b>(v: &'a [u8], needle: &'b [u8]) -> (&'a [u8], &'a [u8]) { @@ -301,7 +301,7 @@ fn split_by<'a, 'b>(v: &'a [u8], needle: &'b [u8]) -> (&'a [u8], &'a [u8]) { } i += 1; } - return (&v[0..], &v[0..0]); + (&v[0..], &v[0..0]) } fn unescape_octals(s: &OsStr) -> Cow { @@ -342,7 +342,7 @@ fn is_octal_encoding(v: &[u8]) -> bool { } fn is_oct(c: u8) -> bool { - c >= b'0' && c <= b'7' + (b'0'..=b'7').contains(&c) } fn parse_octal(v: &[u8]) -> u8 { diff --git a/src/overlay.rs b/src/overlay.rs index 865f2c0a8..0766dc00b 100644 --- a/src/overlay.rs +++ b/src/overlay.rs @@ -147,8 +147,8 @@ impl Explainable for Overlay { info.push(format!("workdir: {}", exists(&wdir))); if let (Some(u), Some(w)) = (umeta, wmeta) { - info.push(format!("{}", if u.dev() == w.dev() - { "same-fs" } else { "different-fs" })); + info.push(if u.dev() == w.dev() + { "same-fs" } else { "different-fs" }.to_string()); } if udir.starts_with(wdir) { info.push("upperdir-prefix-of-workdir".to_string()); @@ -157,7 +157,7 @@ impl Explainable for Overlay { } info.push(format!("target: {}", exists(as_path(&self.target)))); } - if self.lowerdirs.len() < 1 { + if self.lowerdirs.is_empty() { info.push("no-lowerdirs".to_string()); } else if self.upperdir.is_none() && self.lowerdirs.len() < 2 { info.push("single-lowerdir".to_string()); diff --git a/src/remount.rs b/src/remount.rs index 4da08ddc1..0d60a5783 100644 --- a/src/remount.rs +++ b/src/remount.rs @@ -245,7 +245,7 @@ impl Explainable for Remount { fn explain(&self) -> String { [ format!("path: {}", exists(&self.path)), - format!("{}", user()), + user().to_string(), ].join(", ") } } diff --git a/src/tmpfs.rs b/src/tmpfs.rs index fc0309a79..f347455d5 100644 --- a/src/tmpfs.rs +++ b/src/tmpfs.rs @@ -108,12 +108,12 @@ impl Tmpfs { } write!(cur, "gid={}", gid).unwrap(); } - return cur.into_inner(); + cur.into_inner() } /// Mount the tmpfs pub fn bare_mount(self) -> Result<(), OSError> { - let mut options = self.format_options(); + let options = self.format_options(); mount( Some(CStr::from_bytes_with_nul(b"tmpfs\0").unwrap()), &*self.target, @@ -141,7 +141,7 @@ impl Explainable for Tmpfs { fn explain(&self) -> String { [ format!("target: {}", exists(as_path(&self.target))), - format!("{}", user()), + user().to_string(), ].join(", ") } } From 3fe7e9b617ea37c911c438d4e0f036d5ab7daeff Mon Sep 17 00:00:00 2001 From: Bernhard Schuster Date: Wed, 18 Nov 2020 14:05:04 +0100 Subject: [PATCH 6/8] move away from deprecate error::Error trait fns --- src/error.rs | 10 ++-------- src/mountinfo.rs | 2 +- 2 files changed, 3 insertions(+), 9 deletions(-) diff --git a/src/error.rs b/src/error.rs index 94cb4dc84..e889dd418 100644 --- a/src/error.rs +++ b/src/error.rs @@ -32,12 +32,9 @@ impl fmt::Display for OSError { } impl StdError for OSError { - fn cause(&self) -> Option<&dyn StdError> { + fn source(&self) -> Option<&(dyn StdError + 'static)> { Some(&self.0) } - fn description(&self) -> &str { - self.0.description() - } } impl fmt::Display for Error { @@ -47,10 +44,7 @@ impl fmt::Display for Error { } impl StdError for Error { - fn cause(&self) -> Option<&dyn StdError> { + fn source(&self) -> Option<&(dyn StdError + 'static)> { Some(&self.1) } - fn description(&self) -> &str { - self.1.description() - } } diff --git a/src/mountinfo.rs b/src/mountinfo.rs index 4001ded7e..b0e46b9db 100644 --- a/src/mountinfo.rs +++ b/src/mountinfo.rs @@ -48,7 +48,7 @@ impl ParseError { impl fmt::Display for ParseError { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "Parse error at line {}: {}\n{}", - self.row_num, self.description(), self.row) + self.row_num, self, self.row) } } From 43c86f1d1c80e9a66c3191561df39909faf35863 Mon Sep 17 00:00:00 2001 From: Bernhard Schuster Date: Wed, 18 Nov 2020 14:21:43 +0100 Subject: [PATCH 7/8] make the users happy by providing Send + Sync + 'static bounds Required by error frameworks such as `thiserror`, `anyhow`, and `eyre`. --- src/explain.rs | 2 +- src/lib.rs | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/explain.rs b/src/explain.rs index dce8ef630..3fcb4a3b3 100644 --- a/src/explain.rs +++ b/src/explain.rs @@ -6,7 +6,7 @@ use std::path::Path; use nix::unistd::getuid; -pub trait Explainable: Display + Debug { +pub trait Explainable: Display + Debug + Send + Sync { fn explain(&self) -> String; } diff --git a/src/lib.rs b/src/lib.rs index 289a78686..94c8d3022 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -65,14 +65,14 @@ enum MountError { /// path. /// #[derive(Debug)] -pub struct OSError(MountError, Box); +pub struct OSError(MountError, Box); impl OSError { - fn from_remount(err: RemountError, explain: Box) -> OSError { + fn from_remount(err: RemountError, explain: Box) -> OSError { OSError(MountError::Remount(err), explain) } - fn from_nix(err: nix::Error, explain: Box) -> OSError { + fn from_nix(err: nix::Error, explain: Box) -> OSError { OSError( MountError::Io( err.as_errno().map_or_else(|| io::Error::new(io::ErrorKind::Other, err), io::Error::from), @@ -85,4 +85,4 @@ impl OSError { /// The error holder which contains as much information about why failure /// happens as the library implementors could gain #[derive(Debug)] -pub struct Error(Box, io::Error, String); +pub struct Error(Box, io::Error, String); From 86d8784b0dcfc8894f87994cc58cc5f61b292a13 Mon Sep 17 00:00:00 2001 From: Bernhard Schuster Date: Thu, 19 Nov 2020 15:39:42 +0100 Subject: [PATCH 8/8] fix/test: missing editon 2018 conversion --- src/remount.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/remount.rs b/src/remount.rs index 0d60a5783..26a3a5c9a 100644 --- a/src/remount.rs +++ b/src/remount.rs @@ -293,7 +293,7 @@ mod test { use nix::mount::MsFlags; - use Error; + use crate::Error; use super::{Remount, RemountError, MountFlags}; use super::{get_mountpoint_flags, get_mountpoint_flags_from};