libmount/lib.rs
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
25extern crate libc;
26extern crate nix;
27#[macro_use] extern crate quick_error;
28
29mod util;
30mod error;
31mod explain;
32mod bind;
33mod overlay;
34mod tmpfs;
35mod modify;
36mod remount;
37pub mod mountinfo;
38
39use std::io;
40
41use explain::Explainable;
42use remount::RemountError;
43pub use bind::BindMount;
44pub use overlay::Overlay;
45pub use tmpfs::Tmpfs;
46pub use modify::Move;
47pub use remount::Remount;
48
49quick_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)]
78pub struct OSError(MountError, Box<Explainable>);
79
80impl OSError {
81 fn from_remount(err: RemountError, explain: Box<Explainable>) -> OSError {
82 OSError(MountError::Remount(err), explain)
83 }
84
85 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)]
101pub struct Error(Box<Explainable>, io::Error, String);