aboutsummaryrefslogtreecommitdiffstats
path: root/src/sys/unix.rs
blob: 0f433b3228fbc80eab099311744982ee53c9421b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
use users;
use std;

use std::os::unix::fs::MetadataExt;
use std::os::unix::fs::PermissionsExt;

pub fn path_writable(path: &std::path::Path) -> super::PathWritability {
    std::fs::metadata(path)
        .map(|stat| {
            // XXX there really has to be a better option here
            let euid = users::get_effective_uid();
            let egid = users::get_effective_gid();
            let file_uid = stat.uid();
            let file_gid = stat.gid();
            let file_mode = stat.permissions().mode();

            if euid == 0 {
                super::PathWritability::Writable
            }
            else if (file_uid == euid) && (file_mode & 0o200 != 0) {
                super::PathWritability::Writable
            }
            else if (file_gid == egid) && (file_mode & 0o020 != 0) {
                super::PathWritability::Writable
            }
            else if file_mode & 0o002 != 0 {
                super::PathWritability::Writable
            }
            else {
                super::PathWritability::NotWritable
            }
        })
        .unwrap_or(super::PathWritability::NotExist)
}