summaryrefslogtreecommitdiff
path: root/src/main.rs
blob: fd23eb874eda97e3ff71af7c93abf5ddaea13e4b (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
use anyhow::Context;
use serde::Serialize;
use std::fs::symlink_metadata;
use std::fs::Metadata;
use std::os::linux::fs::MetadataExt;
use std::path::{Path, PathBuf};
use structopt::StructOpt;

fn main() -> anyhow::Result<()> {
    let mut opt = Opt::from_args();
    opt.pathnames[..].sort();
    for pathname in opt.pathnames {
        report(&pathname).with_context(|| format!("{}", pathname.display()))?
    }
    Ok(())
}

#[derive(StructOpt, Debug)]
struct Opt {
    #[structopt(parse(from_os_str))]
    pathnames: Vec<PathBuf>,
}

#[derive(Serialize, Debug)]
struct Entry {
    path: PathBuf,
    atime: i64,
    atime_nsec: i64,
    #[serde(with = "mode")]
    mode: u32,
    mtime: i64,
    mtime_nsec: i64,
    nlink: u64,
    size: Option<u64>,
}

impl Entry {
    fn new(path: &Path, m: Metadata) -> Self {
        Self {
            path: path.to_path_buf(),
            atime: m.st_atime(),
            atime_nsec: m.st_atime_nsec(),
            mode: m.st_mode(),
            mtime: m.st_mtime(),
            mtime_nsec: m.st_mtime_nsec(),
            nlink: m.st_nlink(),
            size: if m.is_dir() { None } else { Some(m.st_size()) },
        }
    }
}

fn report(pathname: &Path) -> anyhow::Result<()> {
    let m = symlink_metadata(pathname)?;
    let e = Entry::new(pathname, m);
    println!("{}", serde_yaml::to_string(&e)?);
    Ok(())
}

mod mode {
    use serde::{self, Serializer};

    pub fn serialize<S>(mode: &u32, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        let s = unix_mode::to_string(*mode);
        serializer.serialize_str(&s)
    }
}