summaryrefslogtreecommitdiff
path: root/src/format.rs
blob: 3ba85b9813c47bb4d103352a63602c0bf826a3d7 (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
126
127
128
129
130
extern crate users;
extern crate hex;

use std::io;
use std::fs;
use std::os::unix::fs::PermissionsExt;
use std::os::linux::fs::MetadataExt;
use std::io::prelude::*;

use walkdir::DirEntry;
use chrono::prelude::DateTime;
use chrono::{Utc};
use crypto_hash::{Algorithm, Hasher};
use serde_yaml;


fn str_value(s: &str) -> serde_yaml::Value {
    serde_yaml::to_value(s).unwrap()
}


fn int_value(i: u64) -> serde_yaml::Value {
    serde_yaml::to_value(i).unwrap()
}


pub fn name(e: &DirEntry) -> serde_yaml::Value {
    str_value(&e.path().to_string_lossy())
}


pub fn mtime(e: &DirEntry) -> serde_yaml::Value {
    let meta = e.metadata().unwrap();
    let mtime = meta.modified().unwrap();
    let datetime = DateTime::<Utc>::from(mtime);
    let mtime = datetime.format("%Y-%m-%d %H:%M:%S.%f %z").to_string();
    str_value(&mtime)
}


pub fn mode(e: &DirEntry) -> serde_yaml::Value {
    let meta = e.metadata().unwrap();
    str_value(&format!("{:o}", meta.permissions().mode()))
}


pub fn inode(e: &DirEntry) -> serde_yaml::Value {
    let meta = e.metadata().unwrap();
    int_value(meta.st_ino())
}


pub fn dev(e: &DirEntry) -> serde_yaml::Value {
    let meta = e.metadata().unwrap();
    int_value(meta.st_dev())
}


pub fn nlink(e: &DirEntry) -> serde_yaml::Value {
    let meta = e.metadata().unwrap();
    int_value(meta.st_nlink())
}


pub fn size(e: &DirEntry) -> serde_yaml::Value {
    let meta = e.metadata().unwrap();
    int_value(meta.len())
}


pub fn uid(e: &DirEntry) -> serde_yaml::Value {
    let meta = e.metadata().unwrap();
    int_value(meta.st_uid() as u64)
}


pub fn gid(e: &DirEntry) -> serde_yaml::Value {
    let meta = e.metadata().unwrap();
    int_value(meta.st_gid() as u64)
}


pub fn username(e: &DirEntry) -> serde_yaml::Value {
    let meta = e.metadata().unwrap();
    if let Some(u) = users::get_user_by_uid(meta.st_uid()) {
        str_value(u.name())
    } else {
        str_value("unknown user")
    }
}


pub fn group(e: &DirEntry) -> serde_yaml::Value {
    let meta = e.metadata().unwrap();
    if let Some(g) = users::get_group_by_gid(meta.st_gid()) {
        str_value(g.name())
    } else {
        str_value("unknown group")
    }
}


pub fn sha256(e: &DirEntry) -> serde_yaml::Value {
    let mut ok = true;
    let mut hasher = Hasher::new(Algorithm::SHA256);
    if let Ok(file) = fs::File::open(e.path()) {
        let mut reader = io::BufReader::new(file);
        let mut buf = [0; 1024 * 1024];
        let mut done = false;
        while ok && !done{
            if let Ok(n) = reader.read(&mut buf) {
                if n == 0 {
                    done = true;
                } else if let Err(_) = hasher.write(&buf[0..n]) {
                    ok = false;
                }
            } else {
                ok = false;
            }
        }
    } else {
        ok = false;
    }
    if ok {
        let digest = hasher.finish();
        str_value(&hex::encode(digest))
    } else {
        str_value("can't read file")
    }
}