summaryrefslogtreecommitdiffstats
path: root/src/fs/metadata.rs
blob: 9a3fb605d8fc7f32d969116b7896adc4eb5bca52 (plain)
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
use std::{fs, path, process, time};

#[derive(Clone, Debug)]
pub enum FileType {
    Directory,
    Symlink(String),
    File,
}

impl FileType {
    pub fn is_dir(&self) -> bool {
        match *self {
            Self::Directory => true,
            _ => false,
        }
    }

    pub fn is_symlink(&self) -> bool {
        match *self {
            Self::Symlink(_) => true,
            _ => false,
        }
    }

    pub fn is_file(&self) -> bool {
        match *self {
            Self::File => true,
            _ => false,
        }
    }
}

#[derive(Clone, Debug)]
pub struct JoshutoMetadata {
    pub len: u64,
    pub modified: time::SystemTime,
    pub permissions: fs::Permissions,
    pub file_type: FileType,
    pub mimetype: Option<String>,
    #[cfg(unix)]
    pub uid: u32,
    #[cfg(unix)]
    pub gid: u32,
    #[cfg(unix)]
    pub mode: u32,
}

impl JoshutoMetadata {
    pub fn from(path: &path::Path) -> std::io::Result<Self> {
        #[cfg(unix)]
        use std::os::unix::fs::MetadataExt;

        let metadata = fs::symlink_metadata(path)?;

        let len = metadata.len();
        let modified = metadata.modified()?;
        let permissions = metadata.permissions();
        let file_type = metadata.file_type();

        let file_type = if file_type.is_dir() {
            FileType::Directory
        } else if file_type.is_symlink() {
            let mut link = "".to_string();

            if let Ok(path) = fs::read_link(path) {
                if let Some(s) = path.to_str() {
                    link = s.to_string();
                }
            }
            FileType::Symlink(link)
        } else {
            FileType::File
        };

        let mut mimetype = None;
        if let FileType::File = file_type {
            #[cfg(feature = "file_mimetype")]
            {
                mimetype = file_mimetype(path)
            }
        }

        #[cfg(unix)]
        let uid = metadata.uid();
        #[cfg(unix)]
        let gid = metadata.gid();
        #[cfg(unix)]
        let mode = metadata.mode();

        Ok(Self {
            len,
            modified,
            permissions,
            file_type,
            mimetype,
            #[cfg(unix)]
            uid,
            #[cfg(unix)]
            gid,
            #[cfg(unix)]
            mode,
        })
    }
}

fn file_mimetype(path: &path::Path) -> Option<String> {
    let output = process::Command::new("file")
        .args(&["-Lb", "--mime-type"])
        .arg(path)
        .output();

    match output {
        Ok(s) => {
            if s.status.success() {
                match String::from_utf8(s.stdout) {
                    Ok(s) => Some(s),
                    Err(_) => None,
                }
            } else {
                None
            }
        }
        Err(_) => None,
    }
}