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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
/*
 *  _  ___     _    _                     _     _
 * | |/ / |   | |  | |     /\            | |   (_)
 * | ' /| |   | |  | |    /  \   _ __ ___| |__  ___   _____
 * |  < | |   | |  | |   / /\ \ | '__/ __| '_ \| \ \ / / _ \
 * | . \| |___| |__| |  / ____ \| | | (__| | | | |\ V /  __/
 * |_|\_\______\____/__/_/____\_\_|__\___|_|_|_|_|_\_/_\___|
 * |______|______|______|______|______|______|______|______|
 *         \ \        / /  __ \|_   _|__   __|  ____|
 *          \ \  /\  / /| |__) | | |    | |  | |__
 *           \ \/  \/ / |  _  /  | |    | |  |  __|
 *            \  /\  /  | | \ \ _| |_   | |  | |____
 *             \/  \/   |_|  \_\_____|  |_|  |______|
 */

/*
 * Archive:
 *      0x00 - 0x03: b"KLU "
 *      0x04 - 0x0B: headersize (u64)
 *      0x0C - 0x13: file size  (u64)
 *      0x13 - 0x13 + headersize: File Registrations;
 *      0x14 + headersize - EOF : File Data
 *  File Registrations:
 *                                  0b_______*
 *      0x0: 7b => Filename length; 1b: dir flag (0=dir;1=file)
 *      0x1 - 0x08 => File Size
 *      0x09 + 0x09 + Filename length => Filename
 *  File:
 *      Is a dir:
 *          0x00 - 0x07: Headersize
 *          0x08 - 0x08 + headersize: Dir Header
 *          0x09 - 0x09 + dir size: files data
 *      Is a file:
 *          0x0 - 0x0 + filesize : raw bytes
 */
use std::path::{PathBuf,Path};
use std::io::prelude::*;
mod utils;
#[derive(Debug)]
pub struct Archive {
    headersize: u64,
    filesize: u64,
    file: File,
}

pub type WriteResult<T> = Result<T,WriteError>;

#[derive(Debug)]
pub enum WriteError {
    IoError(std::io::Error),
    InvalidInput(Filename)
}

#[derive(Debug)]
pub enum Filename {
    NotUTF8(String),
    TooLong(String),
    Inexistant(String)
}

impl std::convert::From<std::io::Error> for WriteError {
    fn from(err: std::io::Error) -> Self{
        Self::IoError(err)
    }
}
impl std::fmt::Display for WriteError {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> {
        write!(
            f,
            "{}",
            match self {
                Self::IoError(e) => e.to_string(),
                Self::InvalidInput(e) => format!("{}",match e {
                    Filename::NotUTF8(s) => s,
                    Filename::TooLong(s) => s,
                    Filename::Inexistant(s) => s,
                })
            }
        )
    }
}

impl Archive {
    /// The 4 bytes at the start of any archive
    const ID: [u8; 4] = *b"KLU\x00";
    /// Create an archive from the path
    pub fn from_path<P:AsRef<Path>>(path: P) -> WriteResult<Self> {
        let file = File::from_path(path)?;
        let filesize =  Self::ID.len() as u64 + 
                        8 /* headersize */ + 
                        8 /* filesize */ + 
                        file.header_len() as u64 + file.filesize;
        Ok(Archive {
            headersize: file.header_len() as u64,
            filesize: filesize,
            file: file,
        })
    }
    ///Write archive to file at given path. Will create a new file or truncate it if allready
    ///existing
    pub fn write_to_path<P:AsRef<Path>>(&self, path : P) -> WriteResult<()> {
        let out_file = std::fs::File::create(path)?;
        let mut buffer = std::io::BufWriter::new(out_file);
        buffer.write(&Self::ID)?;
        buffer.write(&utils::u64_to_slice(self.headersize))?;
        buffer.write(&utils::u64_to_slice(self.filesize))?;
        buffer.write(&self.file.header())?;
        self.file.write_to_buf(&mut buffer)?;
        Ok(())
    } 
}

#[derive(Debug)]
/// Represent a file on the archive
pub struct File {
    filesize: u64,
    is_file: bool,
    filename: String,
    path: PathBuf,
    childs: Vec<File>,
}

impl File {
    ///Get the file header
    pub fn header(&self) -> Box<[u8]> {
        let mut header = vec![0x00_u8; 1];
        header[0x00] = (self.filename.len() << 1) as u8 | self.is_file as u8;
        header = [&*header, &*utils::u64_to_slice(self.filesize)].concat();
        header = [&*header, self.filename.as_bytes()].concat();
        header.into_boxed_slice()
    }
    /// Return the file's header length
    pub fn header_len(&self) -> usize {
        return 1 /*filename length + dir bit*/ + 8 /*filesize (u64)*/ + self.filename.len();
    }
    /// Write file to given buffer, needs to be a mutable reference because it 
    /// will be given to file's children an so on;
    pub fn write_to_buf<W:Write>(&self, buffer: &mut std::io::BufWriter<W>) -> WriteResult<()>{
        if self.is_file {
            let mut reader = std::io::BufReader::new(std::fs::File::open(&self.path)?);
            std::io::copy(&mut reader,buffer)?;
            buffer.flush()?;
        } else {
            let mut headersize = 0;
            for c in &self.childs {
                headersize += c.header_len() as u64;
            }
            buffer.write(&utils::u64_to_slice(headersize))?;
            for c in &self.childs {   
                buffer.write(&c.header())?;
            }
            for c in &self.childs {
                c.write_to_buf(buffer)?;
            }
        }
        Ok(())
    }

    /// Create a [File] from a [PathBuf], will populate childs if needed
    pub fn from_path<P:AsRef<Path>>(path: P) -> WriteResult<Self> {
        let path = path.as_ref()
            .canonicalize()?;
        let md = path
            .metadata()?;
        let mut filesize = if md.is_file() { md.len() } else { 8 };
        if let Some(fname) = path.file_name() {
            if None == fname.to_str() {
                return Err(WriteError::InvalidInput(Filename::NotUTF8(
                            format!("Filename `{}` isn't valid UTF-8", path.display()))));
            }
            if fname.len() > 127 {
                return Err(WriteError::InvalidInput(
                        Filename::TooLong(
                            format!("Filename `{:?}` is longer than 127 chars", fname)))); 
            }
        } else {
            return Err(WriteError::InvalidInput(Filename::Inexistant(
                        format!("Filename `{}` doesn't exist", path.display()))));
        }
        let mut childs = Vec::new();
        if md.is_dir() {
            for dir in path.read_dir()? {
                if let Ok(child) = dir {
                    let child_file = Self::from_path(child.path())?;
                    filesize += child_file.header_len() as u64 + child_file.filesize;
                    childs.push(child_file);
                }
            }
        }
        Ok(File {
            filesize: filesize,
            is_file: md.is_file(),
            filename: path.file_name().unwrap().to_str().unwrap().to_owned(),
            path: path,
            childs: childs,
        })
    }
}