Debian Codegraph @

src/bind.rs

raw symbols SCIP
1 use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::fmtrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc fmt/;
2 use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::ffirust-analyzer cargo std https://github.com/rust-lang/rust/library/std ffi/::{CStrrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ffi/c_str/CStr#, CStringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc ffi/c_str/CString#, OsStrrust-analyzer cargo std https://github.com/rust-lang/rust/library/std ffi/os_str/OsStr#};
3 use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::osrust-analyzer cargo std https://github.com/rust-lang/rust/library/std os/::unixrust-analyzer cargo std https://github.com/rust-lang/rust/library/std os/unix/::ffirust-analyzer cargo std https://github.com/rust-lang/rust/library/std os/unix/ffi/::OsStrExtrust-analyzer cargo std https://github.com/rust-lang/rust/library/std os/unix/ffi/os_str/OsStrExt#;
4 use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::pathrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/::Pathrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/Path#;
5
6 use nixrust-analyzer cargo nix 0.31.3 crate/::mountrust-analyzer cargo nix 0.31.3 mount/::{MsFlagsrust-analyzer cargo nix 0.31.3 mount/linux/MsFlags#, mountrust-analyzer cargo nix 0.31.3 mount/linux/mount().};
7
8 use {OSErrorrust-analyzer cargo libmount 0.1.15 OSError#, Errorrust-analyzer cargo libmount 0.1.15 Error#};
9 use util::{path_to_cstring, as_path};
10 use explain::{Explainable, exists, user};
11 use remount::Remount;
12
13
14 /// A mount bind definition
15 ///
16 /// By default bind mount is recursive (it's what you want most of the time).
17 ///
18 /// Also recursive mounts can be used in user namespaces.
19 #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/macros/Debug:, Clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core clone/Clone:)]
20 pub struct BindMountrust-analyzer cargo libmount 0.1.15 bind/BindMount# {
21 sourcerust-analyzer cargo libmount 0.1.15 bind/BindMount#source.: CStringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc ffi/c_str/CString#,
22 targetrust-analyzer cargo libmount 0.1.15 bind/BindMount#target.: CStringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc ffi/c_str/CString#,
23 recursiverust-analyzer cargo libmount 0.1.15 bind/BindMount#recursive.: bool,
24 readonlyrust-analyzer cargo libmount 0.1.15 bind/BindMount#readonly.: bool,
25 }
26
27 impl BindMountrust-analyzer cargo libmount 0.1.15 bind/BindMount# {
28 /// Create a new, recursive bind mount
29 ///
30 /// You can disable recursion with a `non_recursive()` method
31 pub fn newrust-analyzer cargo libmount 0.1.15 bind/impl#[BindMount]new().<A: AsRefrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/AsRef#<Pathrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/Path#>, B: AsRefrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/AsRef#<Pathrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/Path#>>(source: A, target: B)
32 -> BindMountrust-analyzer cargo libmount 0.1.15 bind/BindMount#
33 {
34 BindMountrust-analyzer cargo libmount 0.1.15 bind/BindMount# {
35 sourcerust-analyzer cargo libmount 0.1.15 bind/BindMount#source.: path_to_cstringrust-analyzer cargo libmount 0.1.15 util/path_to_cstring().(source.as_refrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/AsRef#as_ref().()),
36 targetrust-analyzer cargo libmount 0.1.15 bind/BindMount#target.: path_to_cstringrust-analyzer cargo libmount 0.1.15 util/path_to_cstring().(target.as_refrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/AsRef#as_ref().()),
37 recursiverust-analyzer cargo libmount 0.1.15 bind/BindMount#recursive.: true,
38 readonlyrust-analyzer cargo libmount 0.1.15 bind/BindMount#readonly.: false,
39 }
40 }
41 /// Toggle recursion
42 pub fn recursiverust-analyzer cargo libmount 0.1.15 bind/impl#[BindMount]recursive().(mut self, flag: bool) -> BindMountrust-analyzer cargo libmount 0.1.15 bind/BindMount# {
43 self.recursiverust-analyzer cargo libmount 0.1.15 bind/BindMount#recursive. = flag;
44 self
45 }
46 /// If set to `true` makes bind-mount readonly
47 ///
48 /// Few notes:
49 ///
50 /// 1. This makes additional `mount` call (`Remount().readonly()`)
51 /// 2. If remount fails mount bind is left on the filesystem, no cleanup
52 /// is done
53 /// 3. If set to `false` is option is no-op (does **not** remount `rw`)
54 pub fn readonlyrust-analyzer cargo libmount 0.1.15 bind/impl#[BindMount]readonly().(mut self, flag: bool) -> BindMountrust-analyzer cargo libmount 0.1.15 bind/BindMount# {
55 self.readonlyrust-analyzer cargo libmount 0.1.15 bind/BindMount#readonly. = flag;
56 self
57 }
58
59 /// Execute a bind mount
60 pub fn bare_mountrust-analyzer cargo libmount 0.1.15 bind/impl#[BindMount]bare_mount().(self) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<(), OSErrorrust-analyzer cargo libmount 0.1.15 OSError#> {
61 let mut flags = MsFlagsrust-analyzer cargo nix 0.31.3 mount/linux/MsFlags#::MS_BINDrust-analyzer cargo nix 0.31.3 mount/linux/impl#[MsFlags]MS_BIND.;
62 if self.recursiverust-analyzer cargo libmount 0.1.15 bind/BindMount#recursive. {
63 flags = flags rust-analyzer cargo nix 0.31.3 mount/linux/impl#[MsFlags][`BitOr<Self>`]bitor().|rust-analyzer cargo nix 0.31.3 mount/linux/impl#[MsFlags][`BitOr<Self>`]bitor(). rust-analyzer cargo nix 0.31.3 mount/linux/impl#[MsFlags][`BitOr<Self>`]bitor().MsFlagsrust-analyzer cargo nix 0.31.3 mount/linux/MsFlags#::MS_RECrust-analyzer cargo nix 0.31.3 mount/linux/impl#[MsFlags]MS_REC.;
64 }
65 if let Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(err) = mountrust-analyzer cargo nix 0.31.3 mount/linux/mount().(
66 Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(&*rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc ffi/c_str/impl#[CString][Deref]deref().self.sourcerust-analyzer cargo libmount 0.1.15 bind/BindMount#source.),
67 &*rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc ffi/c_str/impl#[CString][Deref]deref().self.targetrust-analyzer cargo libmount 0.1.15 bind/BindMount#target.,
68 Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#::<&CStrrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ffi/c_str/CStr#>,
69 flags,
70 Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#::<&CStrrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ffi/c_str/CStr#>,
71 ) {
72 return Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(OSErrorrust-analyzer cargo libmount 0.1.15 OSError#::from_nixrust-analyzer cargo libmount 0.1.15 impl#[OSError]from_nix().(err, Boxrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc boxed/Box#::newrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc boxed/impl#[`Box<T>`]new().(self)));
73 }
74 if self.readonlyrust-analyzer cargo libmount 0.1.15 bind/BindMount#readonly. {
75 tryrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/`r#try`!!(Remountrust-analyzer cargo libmount 0.1.15 remount/Remount#::newrust-analyzer cargo libmount 0.1.15 remount/impl#[Remount]new().(OsStrrust-analyzer cargo std https://github.com/rust-lang/rust/library/std ffi/os_str/OsStr#::from_bytesrust-analyzer cargo std https://github.com/rust-lang/rust/library/std os/unix/ffi/os_str/impl#[OsStr][OsStrExt]from_bytes().(self.targetrust-analyzer cargo libmount 0.1.15 bind/BindMount#target..as_bytesrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc ffi/c_str/impl#[CString]as_bytes().()))
76 .bindrust-analyzer cargo libmount 0.1.15 remount/impl#[Remount]bind().(true)
77 .readonlyrust-analyzer cargo libmount 0.1.15 remount/impl#[Remount]readonly().(true)
78 .bare_remountrust-analyzer cargo libmount 0.1.15 remount/impl#[Remount]bare_remount().());
79 }
80 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(())
81 }
82
83 /// Execute a bind mount and explain the error immediately
84 pub fn mountrust-analyzer cargo libmount 0.1.15 bind/impl#[BindMount]mount().(self) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<(), Errorrust-analyzer cargo libmount 0.1.15 Error#> {
85 self.bare_mountrust-analyzer cargo libmount 0.1.15 bind/impl#[BindMount]bare_mount().().map_errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]map_err().(OSErrorrust-analyzer cargo libmount 0.1.15 OSError#::explainrust-analyzer cargo libmount 0.1.15 error/impl#[OSError]explain().)
86 }
87 }
88
89 impl fmtrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc fmt/::Displayrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Display# for BindMountrust-analyzer cargo libmount 0.1.15 bind/BindMount# {
90 fn fmtrust-analyzer cargo libmount 0.1.15 bind/impl#[BindMount][Display]fmt().(&self, fmt: &mut fmtrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc fmt/::Formatterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Formatter#) -> fmtrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc fmt/::Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Result# {
91 if self.recursiverust-analyzer cargo libmount 0.1.15 bind/BindMount#recursive. {
92 tryrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/`r#try`!!(writerust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/write!!(fmt, "recursive "));
93 }
94 writerust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/write!!(fmt, "bind mount {:?} -> {:?}",
95 as_pathrust-analyzer cargo libmount 0.1.15 util/as_path().(&self.sourcerust-analyzer cargo libmount 0.1.15 bind/BindMount#source.), as_pathrust-analyzer cargo libmount 0.1.15 util/as_path().(&self.targetrust-analyzer cargo libmount 0.1.15 bind/BindMount#target.))
96 }
97 }
98
99 impl Explainablerust-analyzer cargo libmount 0.1.15 explain/Explainable# for BindMountrust-analyzer cargo libmount 0.1.15 bind/BindMount# {
100 fn explainrust-analyzer cargo libmount 0.1.15 bind/impl#[BindMount][Explainable]explain().(&self) -> Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String# {
101 [
102 formatrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/format!!("source: {}", existsrust-analyzer cargo libmount 0.1.15 explain/exists().(as_pathrust-analyzer cargo libmount 0.1.15 util/as_path().(&self.sourcerust-analyzer cargo libmount 0.1.15 bind/BindMount#source.))),
103 formatrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/format!!("target: {}", existsrust-analyzer cargo libmount 0.1.15 explain/exists().(as_pathrust-analyzer cargo libmount 0.1.15 util/as_path().(&self.targetrust-analyzer cargo libmount 0.1.15 bind/BindMount#target.))),
104 formatrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/format!!("{}", userrust-analyzer cargo libmount 0.1.15 explain/user().()),
105 ].joinrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc slice/impl#[`[T]`]join().(", ")
106 }
107 }
108