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