Coverage Report

Created: 2024-04-26 06:25

/rust/registry/src/index.crates.io-6f17d22bba15001f/nix-0.26.2/src/mount/linux.rs
Line
Count
Source (jump to first uncovered line)
1
#![allow(missing_docs)]
2
use crate::errno::Errno;
3
use crate::{NixPath, Result};
4
use libc::{self, c_int, c_ulong};
5
6
libc_bitflags!(
7
    pub struct MsFlags: c_ulong {
8
        /// Mount read-only
9
        MS_RDONLY;
10
        /// Ignore suid and sgid bits
11
        MS_NOSUID;
12
        /// Disallow access to device special files
13
        MS_NODEV;
14
        /// Disallow program execution
15
        MS_NOEXEC;
16
        /// Writes are synced at once
17
        MS_SYNCHRONOUS;
18
        /// Alter flags of a mounted FS
19
        MS_REMOUNT;
20
        /// Allow mandatory locks on a FS
21
        MS_MANDLOCK;
22
        /// Directory modifications are synchronous
23
        MS_DIRSYNC;
24
        /// Do not update access times
25
        MS_NOATIME;
26
        /// Do not update directory access times
27
        MS_NODIRATIME;
28
        /// Linux 2.4.0 - Bind directory at different place
29
        MS_BIND;
30
        MS_MOVE;
31
        MS_REC;
32
        MS_SILENT;
33
        MS_POSIXACL;
34
        MS_UNBINDABLE;
35
        MS_PRIVATE;
36
        MS_SLAVE;
37
        MS_SHARED;
38
        MS_RELATIME;
39
        MS_KERNMOUNT;
40
        MS_I_VERSION;
41
        MS_STRICTATIME;
42
        MS_LAZYTIME;
43
        MS_ACTIVE;
44
        MS_NOUSER;
45
        MS_RMT_MASK;
46
        MS_MGC_VAL;
47
        MS_MGC_MSK;
48
    }
49
);
50
51
libc_bitflags!(
52
    pub struct MntFlags: c_int {
53
        MNT_FORCE;
54
        MNT_DETACH;
55
        MNT_EXPIRE;
56
        UMOUNT_NOFOLLOW;
57
    }
58
);
59
60
0
pub fn mount<
61
0
    P1: ?Sized + NixPath,
62
0
    P2: ?Sized + NixPath,
63
0
    P3: ?Sized + NixPath,
64
0
    P4: ?Sized + NixPath,
65
0
>(
66
0
    source: Option<&P1>,
67
0
    target: &P2,
68
0
    fstype: Option<&P3>,
69
0
    flags: MsFlags,
70
0
    data: Option<&P4>,
71
0
) -> Result<()> {
72
0
    fn with_opt_nix_path<P, T, F>(p: Option<&P>, f: F) -> Result<T>
73
0
    where
74
0
        P: ?Sized + NixPath,
75
0
        F: FnOnce(*const libc::c_char) -> T,
76
0
    {
77
0
        match p {
78
0
            Some(path) => path.with_nix_path(|p_str| f(p_str.as_ptr())),
79
0
            None => Ok(f(std::ptr::null())),
80
        }
81
0
    }
82
83
0
    let res = with_opt_nix_path(source, |s| {
84
0
        target.with_nix_path(|t| {
85
0
            with_opt_nix_path(fstype, |ty| {
86
0
                with_opt_nix_path(data, |d| unsafe {
87
0
                    libc::mount(
88
0
                        s,
89
0
                        t.as_ptr(),
90
0
                        ty,
91
0
                        flags.bits,
92
0
                        d as *const libc::c_void,
93
0
                    )
94
0
                })
95
0
            })
96
0
        })
97
0
    })????;
98
99
0
    Errno::result(res).map(drop)
100
0
}
101
102
0
pub fn umount<P: ?Sized + NixPath>(target: &P) -> Result<()> {
103
0
    let res =
104
0
        target.with_nix_path(|cstr| unsafe { libc::umount(cstr.as_ptr()) })?;
105
106
0
    Errno::result(res).map(drop)
107
0
}
108
109
0
pub fn umount2<P: ?Sized + NixPath>(target: &P, flags: MntFlags) -> Result<()> {
110
0
    let res = target.with_nix_path(|cstr| unsafe {
111
0
        libc::umount2(cstr.as_ptr(), flags.bits)
112
0
    })?;
113
114
0
    Errno::result(res).map(drop)
115
0
}