• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 use crate::{Errno, NixPath, Result};
2 use libc::c_int;
3 
4 libc_bitflags!(
5     /// Used with [`mount()`] and [`unmount()`].
6     pub struct MntFlags: c_int {
7         /// Do not interpret special files on the filesystem.
8         MNT_NODEV;
9         /// Enable data protection on the filesystem if the filesystem is configured for it.
10         MNT_CPROTECT;
11         /// file system is quarantined
12         MNT_QUARANTINE;
13         /// filesystem is stored locally
14         MNT_LOCAL;
15         /// quotas are enabled on filesystem
16         MNT_QUOTA;
17         /// identifies the root filesystem
18         MNT_ROOTFS;
19         /// file system is not appropriate path to user data
20         MNT_DONTBROWSE;
21         /// VFS will ignore ownership information on filesystem objects
22         MNT_IGNORE_OWNERSHIP;
23         /// filesystem was mounted by automounter
24         MNT_AUTOMOUNTED;
25         /// filesystem is journaled
26         MNT_JOURNALED;
27         /// Don't allow user extended attributes
28         MNT_NOUSERXATTR;
29         /// filesystem should defer writes
30         MNT_DEFWRITE;
31         /// don't block unmount if not responding
32         MNT_NOBLOCK;
33         /// file system is exported
34         MNT_EXPORTED;
35         /// file system written asynchronously
36         MNT_ASYNC;
37         /// Force a read-write mount even if the file system appears to be
38         /// unclean.
39         MNT_FORCE;
40         /// MAC support for objects.
41         MNT_MULTILABEL;
42         /// Do not update access times.
43         MNT_NOATIME;
44         /// Disallow program execution.
45         MNT_NOEXEC;
46         /// Do not honor setuid or setgid bits on files when executing them.
47         MNT_NOSUID;
48         /// Mount read-only.
49         MNT_RDONLY;
50         /// Causes the vfs subsystem to update its data structures pertaining to
51         /// the specified already mounted file system.
52         MNT_RELOAD;
53         /// Create a snapshot of the file system.
54         MNT_SNAPSHOT;
55         /// All I/O to the file system should be done synchronously.
56         MNT_SYNCHRONOUS;
57         /// Union with underlying fs.
58         MNT_UNION;
59         /// Indicates that the mount command is being applied to an already
60         /// mounted file system.
61         MNT_UPDATE;
62     }
63 );
64 
65 /// Mount a file system.
66 ///
67 /// # Arguments
68 /// - `source`  -   Specifies the file system.  e.g. `/dev/sd0`.
69 /// - `target` -    Specifies the destination.  e.g. `/mnt`.
70 /// - `flags` -     Optional flags controlling the mount.
71 /// - `data` -      Optional file system specific data.
72 ///
73 /// # see also
74 /// [`mount`](https://developer.apple.com/library/archive/documentation/System/Conceptual/ManPages_iPhoneOS/man2/mount.2.html)
mount< P1: ?Sized + NixPath, P2: ?Sized + NixPath, P3: ?Sized + NixPath, >( source: &P1, target: &P2, flags: MntFlags, data: Option<&P3>, ) -> Result<()>75 pub fn mount<
76     P1: ?Sized + NixPath,
77     P2: ?Sized + NixPath,
78     P3: ?Sized + NixPath,
79 >(
80     source: &P1,
81     target: &P2,
82     flags: MntFlags,
83     data: Option<&P3>,
84 ) -> Result<()> {
85     let res = source.with_nix_path(|s| {
86         target.with_nix_path(|t| {
87             crate::with_opt_nix_path(data, |d| unsafe {
88                 libc::mount(
89                     s.as_ptr(),
90                     t.as_ptr(),
91                     flags.bits(),
92                     d.cast_mut().cast(),
93                 )
94             })
95         })
96     })???;
97 
98     Errno::result(res).map(drop)
99 }
100 
101 /// Umount the file system mounted at `target`.
unmount<P>(target: &P, flags: MntFlags) -> Result<()> where P: ?Sized + NixPath,102 pub fn unmount<P>(target: &P, flags: MntFlags) -> Result<()>
103 where
104     P: ?Sized + NixPath,
105 {
106     let res = target.with_nix_path(|cstr| unsafe {
107         libc::unmount(cstr.as_ptr(), flags.bits())
108     })?;
109 
110     Errno::result(res).map(drop)
111 }
112