1 //! # libmount 2 //! 3 //! [Documentation](https://docs.rs/libmount) | 4 //! [Github](https://github.com/tailhook/libmount) | 5 //! [Crate](https://crates.io/crates/libmount) 6 //! 7 //! This library has two major goals: 8 //! 9 //! 1. Add type-safe interface for mount() system call 10 //! 2. Add very good explanation of what's wrong when the call fails 11 //! 12 //! So we have two error types: 13 //! 14 //! 1. `OSError` holds mount info and errno 15 //! 2. `Error` is returned by `OSError::explain()` 16 //! 17 //! The first one is returned by `bare_mount()` the second by `mount()`, and 18 //! using latter is preffered for most situations. Unless performance is 19 //! too critical (i.e. you are doing thousands of *failing* mounts per second). 20 //! On the success path there is no overhead. 21 //! 22 #![warn(missing_debug_implementations)] 23 #![warn(missing_docs)] 24 25 extern crate libc; 26 extern crate nix; 27 #[macro_use] extern crate quick_error; 28 29 mod util; 30 mod error; 31 mod explain; 32 mod bind; 33 mod overlay; 34 mod tmpfs; 35 mod modify; 36 mod remount; 37 pub mod mountinfo; 38 39 use std::io; 40 41 use explain::Explainable; 42 use remount::RemountError; 43 pub use bind::BindMount; 44 pub use overlay::Overlay; 45 pub use tmpfs::Tmpfs; 46 pub use modify::Move; 47 pub use remount::Remount; 48 49 quick_error! { 50 #[derive(Debug)] 51 enum MountError { 52 Io(err: io::Error) { 53 cause(err) 54 from() 55 } 56 Remount(err: RemountError) { 57 cause(err) 58 from() 59 } 60 } 61 } 62 63 /// The raw os error 64 /// 65 /// This is a wrapper around `io::Error` providing `explain()` method 66 /// 67 /// Note: you need to explain as fast as possible, because during explain 68 /// library makes some probes for different things in filesystem, and if 69 /// anything changes it may give incorrect results. 70 /// 71 /// You should always `explain()` the errors, unless you are trying lots of 72 /// mounts for bruteforcing or other similar thing and you are concerned of 73 /// performance. Usually library does `stat()` and similar things which are 74 /// much faster than mount anyway. Also explaining is zero-cost in the success 75 /// path. 76 /// 77 #[derive(Debug)] 78 pub struct OSError(MountError, Box<Explainable>); 79 80 impl OSError { from_remount(err: RemountError, explain: Box<Explainable>) -> OSError81 fn from_remount(err: RemountError, explain: Box<Explainable>) -> OSError { 82 OSError(MountError::Remount(err), explain) 83 } 84 from_nix(err: nix::Error, explain: Box<Explainable>) -> OSError85 fn from_nix(err: nix::Error, explain: Box<Explainable>) -> OSError { 86 OSError( 87 MountError::Io( 88 err.as_errno().map_or_else(|| io::Error::new(io::ErrorKind::Other, err), io::Error::from), 89 ), 90 explain, 91 ) 92 } 93 } 94 95 /// The error holder which contains as much information about why failure 96 /// happens as the library implementors could gain 97 /// 98 /// This type only provides `Display` for now, but some programmatic interface 99 /// is expected in future. 100 #[derive(Debug)] 101 pub struct Error(Box<Explainable>, io::Error, String); 102