1//! Linux `mount`.
2
3use crate::backend::mount::types::{
4 InternalMountFlags, MountFlags, MountFlagsArg, MountPropagationFlags, UnmountFlags,
5};
6use crate::{backend, io, path};
7
8/// `mount(source, target, filesystemtype, mountflags, data)`
9///
10/// # References
11/// - [Linux]
12///
13/// [Linux]: https://man7.org/linux/man-pages/man2/mount.2.html
14#[inline]
15pub fn mount<Source: path::Arg, Target: path::Arg, Fs: path::Arg, Data: path::Arg>(
16 source: Source,
17 target: Target,
18 file_system_type: Fs,
19 flags: MountFlags,
20 data: Data,
21) -> io::Result<()> {
22 source.into_with_c_str(|source: &CStr| {
23 target.into_with_c_str(|target: &CStr| {
24 file_system_type.into_with_c_str(|file_system_type: &CStr| {
25 data.into_with_c_str(|data: &CStr| {
26 backend::mount::syscalls::mount(
27 source:Some(source),
28 target,
29 file_system_type:Some(file_system_type),
30 flags:MountFlagsArg(flags.bits()),
31 data:Some(data),
32 )
33 })
34 })
35 })
36 })
37}
38
39/// `mount(NULL, target, NULL, MS_REMOUNT | mountflags, data)`
40///
41/// # References
42/// - [Linux]
43///
44/// [Linux]: https://man7.org/linux/man-pages/man2/mount.2.html
45#[inline]
46#[doc(alias = "mount")]
47#[doc(alias = "MS_REMOUNT")]
48pub fn mount_remount<Target: path::Arg, Data: path::Arg>(
49 target: Target,
50 flags: MountFlags,
51 data: Data,
52) -> io::Result<()> {
53 target.into_with_c_str(|target: &CStr| {
54 data.into_with_c_str(|data: &CStr| {
55 backend::mount::syscalls::mount(
56 source:None,
57 target,
58 file_system_type:None,
59 flags:MountFlagsArg(InternalMountFlags::REMOUNT.bits() | flags.bits()),
60 data:Some(data),
61 )
62 })
63 })
64}
65
66/// `mount(source, target, NULL, MS_BIND, NULL)`
67///
68/// # References
69/// - [Linux]
70///
71/// [Linux]: https://man7.org/linux/man-pages/man2/mount.2.html
72#[inline]
73#[doc(alias = "mount")]
74#[doc(alias = "MS_BIND")]
75pub fn mount_bind<Source: path::Arg, Target: path::Arg>(
76 source: Source,
77 target: Target,
78) -> io::Result<()> {
79 source.into_with_c_str(|source: &CStr| {
80 target.into_with_c_str(|target: &CStr| {
81 backend::mount::syscalls::mount(
82 source:Some(source),
83 target,
84 file_system_type:None,
85 flags:MountFlagsArg(MountFlags::BIND.bits()),
86 data:None,
87 )
88 })
89 })
90}
91
92/// `mount(source, target, NULL, MS_BIND | MS_REC, NULL)`
93///
94/// # References
95/// - [Linux]
96///
97/// [Linux]: https://man7.org/linux/man-pages/man2/mount.2.html
98#[inline]
99#[doc(alias = "mount")]
100#[doc(alias = "MS_REC")]
101pub fn mount_recursive_bind<Source: path::Arg, Target: path::Arg>(
102 source: Source,
103 target: Target,
104) -> io::Result<()> {
105 source.into_with_c_str(|source: &CStr| {
106 target.into_with_c_str(|target: &CStr| {
107 backend::mount::syscalls::mount(
108 source:Some(source),
109 target,
110 file_system_type:None,
111 flags:MountFlagsArg(MountFlags::BIND.bits() | MountPropagationFlags::REC.bits()),
112 data:None,
113 )
114 })
115 })
116}
117
118/// `mount(NULL, target, NULL, mountflags, NULL)`
119///
120/// # References
121/// - [Linux]
122///
123/// [Linux]: https://man7.org/linux/man-pages/man2/mount.2.html
124#[inline]
125#[doc(alias = "mount")]
126pub fn mount_change<Target: path::Arg>(
127 target: Target,
128 flags: MountPropagationFlags,
129) -> io::Result<()> {
130 target.into_with_c_str(|target: &CStr| {
131 backend::mount::syscalls::mount(source:None, target, file_system_type:None, flags:MountFlagsArg(flags.bits()), data:None)
132 })
133}
134
135/// `mount(source, target, NULL, MS_MOVE, NULL)`
136///
137/// This is not the same as the `move_mount` syscall. If you want to use that,
138/// use [`move_mount`] instead.
139///
140/// # References
141/// - [Linux]
142///
143/// [`move_mount`]: crate::mount::move_mount
144/// [Linux]: https://man7.org/linux/man-pages/man2/mount.2.html
145#[inline]
146#[doc(alias = "mount")]
147#[doc(alias = "MS_MOVE")]
148pub fn mount_move<Source: path::Arg, Target: path::Arg>(
149 source: Source,
150 target: Target,
151) -> io::Result<()> {
152 source.into_with_c_str(|source: &CStr| {
153 target.into_with_c_str(|target: &CStr| {
154 backend::mount::syscalls::mount(
155 source:Some(source),
156 target,
157 file_system_type:None,
158 flags:MountFlagsArg(InternalMountFlags::MOVE.bits()),
159 data:None,
160 )
161 })
162 })
163}
164
165/// `umount2(target, flags)`
166///
167/// # References
168/// - [Linux]
169///
170/// [Linux]: https://man7.org/linux/man-pages/man2/umount.2.html
171#[inline]
172#[doc(alias = "umount", alias = "umount2")]
173pub fn unmount<Target: path::Arg>(target: Target, flags: UnmountFlags) -> io::Result<()> {
174 target.into_with_c_str(|target: &CStr| backend::mount::syscalls::unmount(target, flags))
175}
176