nix/mount/
linux.rs

1#![allow(missing_docs)]
2use libc::{self, c_ulong, c_int};
3use crate::{Result, NixPath};
4use crate::errno::Errno;
5
6libc_bitflags!(
7    pub struct MsFlags: c_ulong {
8        /// Mount read-only
9        MS_RDONLY;
10        /// Ignore suid and sgid bits
11        MS_NOSUID;
12        /// Disallow access to device special files
13        MS_NODEV;
14        /// Disallow program execution
15        MS_NOEXEC;
16        /// Writes are synced at once
17        MS_SYNCHRONOUS;
18        /// Alter flags of a mounted FS
19        MS_REMOUNT;
20        /// Allow mandatory locks on a FS
21        MS_MANDLOCK;
22        /// Directory modifications are synchronous
23        MS_DIRSYNC;
24        /// Do not update access times
25        MS_NOATIME;
26        /// Do not update directory access times
27        MS_NODIRATIME;
28        /// Linux 2.4.0 - Bind directory at different place
29        MS_BIND;
30        MS_MOVE;
31        MS_REC;
32        MS_SILENT;
33        MS_POSIXACL;
34        MS_UNBINDABLE;
35        MS_PRIVATE;
36        MS_SLAVE;
37        MS_SHARED;
38        MS_RELATIME;
39        MS_KERNMOUNT;
40        MS_I_VERSION;
41        MS_STRICTATIME;
42        MS_LAZYTIME;
43        MS_ACTIVE;
44        MS_NOUSER;
45        MS_RMT_MASK;
46        MS_MGC_VAL;
47        MS_MGC_MSK;
48    }
49);
50
51libc_bitflags!(
52    pub struct MntFlags: c_int {
53        MNT_FORCE;
54        MNT_DETACH;
55        MNT_EXPIRE;
56        UMOUNT_NOFOLLOW;
57    }
58);
59
60pub fn mount<P1: ?Sized + NixPath, P2: ?Sized + NixPath, P3: ?Sized + NixPath, P4: ?Sized + NixPath>(
61        source: Option<&P1>,
62        target: &P2,
63        fstype: Option<&P3>,
64        flags: MsFlags,
65        data: Option<&P4>) -> Result<()> {
66
67    fn with_opt_nix_path<P, T, F>(p: Option<&P>, f: F) -> Result<T>
68        where P: ?Sized + NixPath,
69              F: FnOnce(*const libc::c_char) -> T
70    {
71        match p {
72            Some(path) => path.with_nix_path(|p_str| f(p_str.as_ptr())),
73            None => Ok(f(std::ptr::null()))
74        }
75    }
76
77    let res = with_opt_nix_path(source, |s| {
78        target.with_nix_path(|t| {
79            with_opt_nix_path(fstype, |ty| {
80                with_opt_nix_path(data, |d| {
81                    unsafe {
82                        libc::mount(
83                            s,
84                            t.as_ptr(),
85                            ty,
86                            flags.bits,
87                            d as *const libc::c_void
88                        )
89                    }
90                })
91            })
92        })
93    })????;
94
95    Errno::result(res).map(drop)
96}
97
98pub fn umount<P: ?Sized + NixPath>(target: &P) -> Result<()> {
99    let res = target.with_nix_path(|cstr| {
100        unsafe { libc::umount(cstr.as_ptr()) }
101    })?;
102
103    Errno::result(res).map(drop)
104}
105
106pub fn umount2<P: ?Sized + NixPath>(target: &P, flags: MntFlags) -> Result<()> {
107    let res = target.with_nix_path(|cstr| {
108        unsafe { libc::umount2(cstr.as_ptr(), flags.bits) }
109    })?;
110
111    Errno::result(res).map(drop)
112}