• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #![allow(missing_docs)]
2 use libc::{self, c_ulong, c_int};
3 use crate::{Result, NixPath};
4 use crate::errno::Errno;
5 
6 libc_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 
51 libc_bitflags!(
52     pub struct MntFlags: c_int {
53         MNT_FORCE;
54         MNT_DETACH;
55         MNT_EXPIRE;
56     }
57 );
58 
mount<P1: ?Sized + NixPath, P2: ?Sized + NixPath, P3: ?Sized + NixPath, P4: ?Sized + NixPath>( source: Option<&P1>, target: &P2, fstype: Option<&P3>, flags: MsFlags, data: Option<&P4>) -> Result<()>59 pub fn mount<P1: ?Sized + NixPath, P2: ?Sized + NixPath, P3: ?Sized + NixPath, P4: ?Sized + NixPath>(
60         source: Option<&P1>,
61         target: &P2,
62         fstype: Option<&P3>,
63         flags: MsFlags,
64         data: Option<&P4>) -> Result<()> {
65 
66     fn with_opt_nix_path<P, T, F>(p: Option<&P>, f: F) -> Result<T>
67         where P: ?Sized + NixPath,
68               F: FnOnce(*const libc::c_char) -> T
69     {
70         match p {
71             Some(path) => path.with_nix_path(|p_str| f(p_str.as_ptr())),
72             None => Ok(f(std::ptr::null()))
73         }
74     }
75 
76     let res = with_opt_nix_path(source, |s| {
77         target.with_nix_path(|t| {
78             with_opt_nix_path(fstype, |ty| {
79                 with_opt_nix_path(data, |d| {
80                     unsafe {
81                         libc::mount(
82                             s,
83                             t.as_ptr(),
84                             ty,
85                             flags.bits,
86                             d as *const libc::c_void
87                         )
88                     }
89                 })
90             })
91         })
92     })????;
93 
94     Errno::result(res).map(drop)
95 }
96 
umount<P: ?Sized + NixPath>(target: &P) -> Result<()>97 pub fn umount<P: ?Sized + NixPath>(target: &P) -> Result<()> {
98     let res = target.with_nix_path(|cstr| {
99         unsafe { libc::umount(cstr.as_ptr()) }
100     })?;
101 
102     Errno::result(res).map(drop)
103 }
104 
umount2<P: ?Sized + NixPath>(target: &P, flags: MntFlags) -> Result<()>105 pub fn umount2<P: ?Sized + NixPath>(target: &P, flags: MntFlags) -> Result<()> {
106     let res = target.with_nix_path(|cstr| {
107         unsafe { libc::umount2(cstr.as_ptr(), flags.bits) }
108     })?;
109 
110     Errno::result(res).map(drop)
111 }
112