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
#[cfg(all(feature = "mapped_io", not(target_arch = "wasm32")))]
mod merger;

#[cfg(all(feature = "mapped_io", not(target_arch = "wasm32")))]
mod reader;

#[cfg(all(feature = "mapped_io", not(target_arch = "wasm32")))]
mod track;

#[cfg(all(feature = "mapped_io", not(target_arch = "wasm32")))]
mod writer;

use std::{
    fs::File,
    io::{Read, Result, Seek},
    path::{Path, PathBuf},
};

use d4_framefile::{Directory, EntryKind};

#[cfg(all(feature = "mapped_io", not(target_arch = "wasm32")))]
mod mapped {
    use super::*;
    pub use merger::D4FileMerger;
    pub use reader::D4TrackReader;
    pub use track::{
        D4FilePartition, D4MatrixReader, DataScanner, MultiTrackPartitionReader, MultiTrackReader,
    };

    pub use writer::{D4FileBuilder, D4FileWriter, D4FileWriterExt};
}
#[cfg(all(feature = "mapped_io", not(target_arch = "wasm32")))]
pub use mapped::*;

/// The D4 magic number
pub const FILE_MAGIC_NUM: &[u8] = b"d4\xdd\xdd";

pub(crate) fn validate_header<R: Read>(mut reader: R) -> Result<()> {
    let mut signature = [0u8; 8];
    reader.read_exact(&mut signature[..])?;
    if signature[..4] != FILE_MAGIC_NUM[..] {
        return Err(std::io::Error::new(
            std::io::ErrorKind::Other,
            "Invalid D4 File magic number",
        ));
    }
    Ok(())
}

fn open_file_and_validate_header<P: AsRef<Path>>(path: P) -> Result<File> {
    let mut fp = File::open(path.as_ref())?;
    validate_header(&mut fp)?;
    Ok(fp)
}

pub fn find_tracks<Pat: FnMut(Option<&Path>) -> bool, R: Read + Seek>(
    mut input: R,
    mut pattern: Pat,
    buf: &mut Vec<PathBuf>,
) -> Result<()> {
    validate_header(&mut input)?;
    let file_root = Directory::open_root(input, 8)?;
    file_root.recurse(|path, kind| {
        if path.file_name().unwrap_or_default() == ".metadata"
            && kind == EntryKind::Stream
            && pattern(path.parent())
        {
            buf.push(path.parent().map(ToOwned::to_owned).unwrap_or_default());
            return false;
        }
        true
    });

    Ok(())
}

pub fn find_tracks_in_file<Pat: FnMut(Option<&Path>) -> bool, PathType: AsRef<Path>>(
    path: PathType,
    pattern: Pat,
    buf: &mut Vec<PathBuf>,
) -> Result<()> {
    let fp = File::open(path.as_ref())?;
    find_tracks(fp, pattern, buf)
}