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
use crate::errors::Result;
use crate::args::Fsck;

use std::fs::File;
use std::io;
use std::io::BufReader;
use std::io::BufWriter;
use std::io::prelude::*;
use std::str;


fn validate_file(path: &str, args: &Fsck) -> Result<()> {
    let f = File::open(path)?;
    let mut file = BufReader::new(&f);
    let mut out = BufWriter::new(io::stdout());

    let mut i = 0;
    let mut buf = Vec::new();
    const DELIM: u8 = b'\n';

    while 0 < file.read_until(DELIM, &mut buf)? {
        /*
        not removing the \n so we don't have to append it later
        if buf[buf.len() - 1] == DELIM {
            buf.pop();
        }
        */
        // TODO: remove empty lines?

        match str::from_utf8(&buf) {
            Ok(line) => {
                if !args.require_colon || buf.iter().any(|x| *x == b':') {
                    if !args.silent {
                        out.write_all(line.as_bytes())?;
                    }
                } else if !args.quiet {
                    eprintln!("Invalid(line {}): {:?}",
                        i,
                        line);
                }
            },
            Err(_) => {
                if !args.quiet {
                    eprintln!("Invalid(line {}): {:?} {:?}",
                        i,
                        String::from_utf8_lossy(&buf),
                        buf);
                }
            },
        };

        buf.clear();
        i += 1;
    }

    // Close the BufWriter to flush it
    let _ = out.into_inner()?;

    Ok(())
}

pub fn run_fsck(args: &Fsck) -> Result<()> {
    for path in &args.paths {
        validate_file(path, args)?;
    }
    Ok(())
}