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
//! Try to guess the types of files on disk.

use std::fs;
use std::io::Read;
use std::path::Path;

use errors::*;

/// Internal helper function which looks for "magic" bytes at the start of
/// a file.
fn has_magic(path: &Path, magic: &[u8]) -> Result<bool> {
    let mkerr = || ErrorKind::ReadFile(path.to_owned());
    let mut f = fs::File::open(path).chain_err(&mkerr)?;
    let mut bytes = vec![0; magic.len()];
    f.read_exact(&mut bytes).chain_err(&mkerr)?;
    Ok(magic == &bytes[..])
}

/// Does the specified path appear to point to an `*.idx` file?
pub fn is_idx_file<P: AsRef<Path>>(path: P) -> Result<bool> {
    has_magic(path.as_ref(),
              b"# VobSub index file")
}

#[test]
fn probe_idx_files() {
    assert!(is_idx_file("../fixtures/tiny.idx").unwrap());
    assert!(!is_idx_file("../fixtures/tiny.sub").unwrap());
}

/// Does the specified path appear to point to a `*.sub` file?
///
/// Note that this may (or may not) return false positives for certain
/// MPEG-2 related formats.
pub fn is_sub_file<P: AsRef<Path>>(path: P) -> Result<bool> {
    has_magic(path.as_ref(),
              &[0x00, 0x00, 0x01, 0xba])
}

#[test]
fn probe_sub_files() {
    assert!(is_sub_file("../fixtures/tiny.sub").unwrap());
    assert!(!is_sub_file("../fixtures/tiny.idx").unwrap());
}