rustix/mount/
mount_unmount.rs

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| {
23        target.into_with_c_str(|target| {
24            file_system_type.into_with_c_str(|file_system_type| {
25                data.into_with_c_str(|data| {
26                    backend::mount::syscalls::mount(
27                        Some(source),
28                        target,
29                        Some(file_system_type),
30                        MountFlagsArg(flags.bits()),
31                        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| {
54        data.into_with_c_str(|data| {
55            backend::mount::syscalls::mount(
56                None,
57                target,
58                None,
59                MountFlagsArg(InternalMountFlags::REMOUNT.bits() | flags.bits()),
60                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| {
80        target.into_with_c_str(|target| {
81            backend::mount::syscalls::mount(
82                Some(source),
83                target,
84                None,
85                MountFlagsArg(MountFlags::BIND.bits()),
86                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| {
106        target.into_with_c_str(|target| {
107            backend::mount::syscalls::mount(
108                Some(source),
109                target,
110                None,
111                MountFlagsArg(MountFlags::BIND.bits() | MountPropagationFlags::REC.bits()),
112                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| {
131        backend::mount::syscalls::mount(None, target, None, MountFlagsArg(flags.bits()), 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| {
153        target.into_with_c_str(|target| {
154            backend::mount::syscalls::mount(
155                Some(source),
156                target,
157                None,
158                MountFlagsArg(InternalMountFlags::MOVE.bits()),
159                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| backend::mount::syscalls::unmount(target, flags))
175}