xref: /third_party/rust/crates/rustix/src/fs/mount.rs (revision b8a62b91)
1//! Linux `mount`.
2
3use crate::backend::fs::types::{
4    InternalMountFlags, MountFlags, MountFlagsArg, MountPropagationFlags,
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::fs::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]
46pub fn remount<Target: path::Arg, Data: path::Arg>(
47    target: Target,
48    flags: MountFlags,
49    data: Data,
50) -> io::Result<()> {
51    target.into_with_c_str(|target| {
52        data.into_with_c_str(|data| {
53            backend::fs::syscalls::mount(
54                None,
55                target,
56                None,
57                MountFlagsArg(InternalMountFlags::REMOUNT.bits() | flags.bits()),
58                Some(data),
59            )
60        })
61    })
62}
63
64/// `mount(source, target, null, MS_BIND, null)`
65///
66/// # References
67///  - [Linux]
68///
69/// [Linux]: https://man7.org/linux/man-pages/man2/mount.2.html
70#[inline]
71pub fn bind_mount<Source: path::Arg, Target: path::Arg>(
72    source: Source,
73    target: Target,
74) -> io::Result<()> {
75    source.into_with_c_str(|source| {
76        target.into_with_c_str(|target| {
77            backend::fs::syscalls::mount(
78                Some(source),
79                target,
80                None,
81                MountFlagsArg(MountFlags::BIND.bits()),
82                None,
83            )
84        })
85    })
86}
87
88/// `mount(source, target, null, MS_BIND | MS_REC, null)`
89///
90/// # References
91///  - [Linux]
92///
93/// [Linux]: https://man7.org/linux/man-pages/man2/mount.2.html
94#[inline]
95pub fn recursive_bind_mount<Source: path::Arg, Target: path::Arg>(
96    source: Source,
97    target: Target,
98) -> io::Result<()> {
99    source.into_with_c_str(|source| {
100        target.into_with_c_str(|target| {
101            backend::fs::syscalls::mount(
102                Some(source),
103                target,
104                None,
105                MountFlagsArg(MountFlags::BIND.bits() | MountPropagationFlags::REC.bits()),
106                None,
107            )
108        })
109    })
110}
111
112/// `mount(null, target, null, mountflags, null)`
113///
114/// # References
115///  - [Linux]
116///
117/// [Linux]: https://man7.org/linux/man-pages/man2/mount.2.html
118#[inline]
119pub fn change_mount<Target: path::Arg>(
120    target: Target,
121    flags: MountPropagationFlags,
122) -> io::Result<()> {
123    target.into_with_c_str(|target| {
124        backend::fs::syscalls::mount(None, target, None, MountFlagsArg(flags.bits()), None)
125    })
126}
127
128/// `mount(source, target, null, MS_MOVE, null)`
129///
130/// # References
131///  - [Linux]
132///
133/// [Linux]: https://man7.org/linux/man-pages/man2/mount.2.html
134#[inline]
135pub fn move_mount<Source: path::Arg, Target: path::Arg>(
136    source: Source,
137    target: Target,
138) -> io::Result<()> {
139    source.into_with_c_str(|source| {
140        target.into_with_c_str(|target| {
141            backend::fs::syscalls::mount(
142                Some(source),
143                target,
144                None,
145                MountFlagsArg(InternalMountFlags::MOVE.bits()),
146                None,
147            )
148        })
149    })
150}
151