2019-08-16 10:27:17 +00:00
|
|
|
use failure::*;
|
2019-11-12 09:14:33 +00:00
|
|
|
use std::fmt;
|
2019-11-12 08:25:39 +00:00
|
|
|
use std::ffi::{CStr, CString, OsStr};
|
|
|
|
use std::os::unix::ffi::OsStrExt;
|
2019-11-08 09:35:48 +00:00
|
|
|
use std::io::{Read, Write, Seek, SeekFrom};
|
2019-08-16 10:27:17 +00:00
|
|
|
use std::convert::TryFrom;
|
|
|
|
|
|
|
|
use chrono::offset::{TimeZone, Local};
|
|
|
|
|
|
|
|
use proxmox::tools::io::ReadExt;
|
|
|
|
|
2019-11-12 09:14:33 +00:00
|
|
|
use crate::pxar::catalog::BackupCatalogWriter;
|
2019-11-10 10:34:55 +00:00
|
|
|
use crate::backup::file_formats::PROXMOX_CATALOG_FILE_MAGIC_1_0;
|
2019-08-16 10:27:17 +00:00
|
|
|
|
2019-11-12 09:14:33 +00:00
|
|
|
#[repr(u8)]
|
|
|
|
#[derive(Copy,Clone,PartialEq)]
|
2019-11-12 10:15:05 +00:00
|
|
|
enum CatalogEntryType {
|
2019-11-12 09:14:33 +00:00
|
|
|
Directory = b'd',
|
|
|
|
File = b'f',
|
|
|
|
Symlink = b'l',
|
|
|
|
Hardlink = b'h',
|
|
|
|
BlockDevice = b'b',
|
|
|
|
CharDevice = b'c',
|
|
|
|
Fifo = b'p', // Fifo,Pipe
|
|
|
|
Socket = b's',
|
|
|
|
}
|
|
|
|
|
|
|
|
impl TryFrom<u8> for CatalogEntryType {
|
|
|
|
type Error=Error;
|
|
|
|
|
|
|
|
fn try_from(value: u8) -> Result<Self, Error> {
|
|
|
|
Ok(match value {
|
|
|
|
b'd' => CatalogEntryType::Directory,
|
|
|
|
b'f' => CatalogEntryType::File,
|
|
|
|
b'l' => CatalogEntryType::Symlink,
|
|
|
|
b'h' => CatalogEntryType::Hardlink,
|
|
|
|
b'b' => CatalogEntryType::BlockDevice,
|
|
|
|
b'c' => CatalogEntryType::CharDevice,
|
|
|
|
b'p' => CatalogEntryType::Fifo,
|
|
|
|
b's' => CatalogEntryType::Socket,
|
|
|
|
_ => bail!("invalid CatalogEntryType value '{}'", char::from(value)),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl fmt::Display for CatalogEntryType {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
|
|
write!(f, "{}", char::from(*self as u8))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-12 10:15:05 +00:00
|
|
|
pub struct DirEntry {
|
|
|
|
pub name: Vec<u8>,
|
|
|
|
pub attr: DirEntryAttribute,
|
2019-11-12 08:25:39 +00:00
|
|
|
}
|
|
|
|
|
2019-11-12 10:15:05 +00:00
|
|
|
impl DirEntry {
|
|
|
|
|
|
|
|
fn new(etype: CatalogEntryType, name: Vec<u8>, start: u64, size: u64, mtime:u64) -> Self {
|
|
|
|
match etype {
|
|
|
|
CatalogEntryType::Directory => {
|
|
|
|
DirEntry { name, attr: DirEntryAttribute::Directory { start } }
|
|
|
|
}
|
|
|
|
CatalogEntryType::File => {
|
|
|
|
DirEntry { name, attr: DirEntryAttribute::File { size, mtime } }
|
|
|
|
}
|
|
|
|
CatalogEntryType::Symlink => {
|
|
|
|
DirEntry { name, attr: DirEntryAttribute::Symlink }
|
|
|
|
}
|
|
|
|
CatalogEntryType::Hardlink => {
|
|
|
|
DirEntry { name, attr: DirEntryAttribute::Hardlink }
|
|
|
|
}
|
|
|
|
CatalogEntryType::BlockDevice => {
|
|
|
|
DirEntry { name, attr: DirEntryAttribute::BlockDevice }
|
|
|
|
}
|
|
|
|
CatalogEntryType::CharDevice => {
|
|
|
|
DirEntry { name, attr: DirEntryAttribute::CharDevice }
|
|
|
|
}
|
|
|
|
CatalogEntryType::Fifo => {
|
|
|
|
DirEntry { name, attr: DirEntryAttribute::Fifo }
|
|
|
|
}
|
|
|
|
CatalogEntryType::Socket => {
|
|
|
|
DirEntry { name, attr: DirEntryAttribute::Socket }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub enum DirEntryAttribute {
|
2019-11-12 08:25:39 +00:00
|
|
|
Directory { start: u64 },
|
|
|
|
File { size: u64, mtime: u64 },
|
|
|
|
Symlink,
|
|
|
|
Hardlink,
|
|
|
|
BlockDevice,
|
|
|
|
CharDevice,
|
|
|
|
Fifo,
|
|
|
|
Socket,
|
2019-11-08 09:35:48 +00:00
|
|
|
}
|
2019-08-16 10:27:17 +00:00
|
|
|
|
2019-11-08 09:35:48 +00:00
|
|
|
struct DirInfo {
|
|
|
|
name: CString,
|
|
|
|
entries: Vec<DirEntry>,
|
2019-08-16 10:27:17 +00:00
|
|
|
}
|
|
|
|
|
2019-11-08 09:35:48 +00:00
|
|
|
impl DirInfo {
|
|
|
|
|
|
|
|
fn new(name: CString) -> Self {
|
|
|
|
DirInfo { name, entries: Vec::new() }
|
|
|
|
}
|
|
|
|
|
|
|
|
fn new_rootdir() -> Self {
|
|
|
|
DirInfo::new(CString::new(b"/".to_vec()).unwrap())
|
|
|
|
}
|
|
|
|
|
2019-11-09 12:07:47 +00:00
|
|
|
fn encode_entry<W: Write>(
|
|
|
|
writer: &mut W,
|
|
|
|
entry: &DirEntry,
|
|
|
|
pos: u64,
|
|
|
|
) -> Result<(), Error> {
|
2019-11-08 09:35:48 +00:00
|
|
|
match entry {
|
2019-11-12 08:25:39 +00:00
|
|
|
DirEntry { name, attr: DirEntryAttribute::Directory { start } } => {
|
2019-11-09 12:07:47 +00:00
|
|
|
writer.write_all(&[CatalogEntryType::Directory as u8])?;
|
|
|
|
catalog_encode_u64(writer, name.len() as u64)?;
|
|
|
|
writer.write_all(name)?;
|
|
|
|
catalog_encode_u64(writer, pos - start)?;
|
2019-11-08 09:35:48 +00:00
|
|
|
}
|
2019-11-12 08:25:39 +00:00
|
|
|
DirEntry { name, attr: DirEntryAttribute::File { size, mtime } } => {
|
2019-11-09 12:07:47 +00:00
|
|
|
writer.write_all(&[CatalogEntryType::File as u8])?;
|
|
|
|
catalog_encode_u64(writer, name.len() as u64)?;
|
|
|
|
writer.write_all(name)?;
|
|
|
|
catalog_encode_u64(writer, *size)?;
|
|
|
|
catalog_encode_u64(writer, *mtime)?;
|
2019-11-08 09:35:48 +00:00
|
|
|
}
|
2019-11-12 08:25:39 +00:00
|
|
|
DirEntry { name, attr: DirEntryAttribute::Symlink } => {
|
2019-11-09 12:07:47 +00:00
|
|
|
writer.write_all(&[CatalogEntryType::Symlink as u8])?;
|
|
|
|
catalog_encode_u64(writer, name.len() as u64)?;
|
|
|
|
writer.write_all(name)?;
|
2019-11-08 09:35:48 +00:00
|
|
|
}
|
2019-11-12 08:25:39 +00:00
|
|
|
DirEntry { name, attr: DirEntryAttribute::Hardlink } => {
|
2019-11-09 12:07:47 +00:00
|
|
|
writer.write_all(&[CatalogEntryType::Hardlink as u8])?;
|
|
|
|
catalog_encode_u64(writer, name.len() as u64)?;
|
|
|
|
writer.write_all(name)?;
|
2019-11-08 09:35:48 +00:00
|
|
|
}
|
2019-11-12 08:25:39 +00:00
|
|
|
DirEntry { name, attr: DirEntryAttribute::BlockDevice } => {
|
2019-11-09 12:07:47 +00:00
|
|
|
writer.write_all(&[CatalogEntryType::BlockDevice as u8])?;
|
|
|
|
catalog_encode_u64(writer, name.len() as u64)?;
|
|
|
|
writer.write_all(name)?;
|
2019-11-08 09:35:48 +00:00
|
|
|
}
|
2019-11-12 08:25:39 +00:00
|
|
|
DirEntry { name, attr: DirEntryAttribute::CharDevice } => {
|
2019-11-09 12:07:47 +00:00
|
|
|
writer.write_all(&[CatalogEntryType::CharDevice as u8])?;
|
|
|
|
catalog_encode_u64(writer, name.len() as u64)?;
|
|
|
|
writer.write_all(name)?;
|
2019-11-08 09:35:48 +00:00
|
|
|
}
|
2019-11-12 08:25:39 +00:00
|
|
|
DirEntry { name, attr: DirEntryAttribute::Fifo } => {
|
2019-11-09 12:07:47 +00:00
|
|
|
writer.write_all(&[CatalogEntryType::Fifo as u8])?;
|
|
|
|
catalog_encode_u64(writer, name.len() as u64)?;
|
|
|
|
writer.write_all(name)?;
|
2019-11-08 09:35:48 +00:00
|
|
|
}
|
2019-11-12 08:25:39 +00:00
|
|
|
DirEntry { name, attr: DirEntryAttribute::Socket } => {
|
2019-11-09 12:07:47 +00:00
|
|
|
writer.write_all(&[CatalogEntryType::Socket as u8])?;
|
|
|
|
catalog_encode_u64(writer, name.len() as u64)?;
|
|
|
|
writer.write_all(name)?;
|
2019-11-08 09:35:48 +00:00
|
|
|
}
|
|
|
|
}
|
2019-11-09 12:07:47 +00:00
|
|
|
Ok(())
|
2019-08-16 10:27:17 +00:00
|
|
|
}
|
2019-11-08 09:35:48 +00:00
|
|
|
|
|
|
|
fn encode(self, start: u64) -> Result<(CString, Vec<u8>), Error> {
|
|
|
|
let mut table = Vec::new();
|
2019-11-09 12:07:47 +00:00
|
|
|
catalog_encode_u64(&mut table, self.entries.len() as u64)?;
|
2019-11-08 09:35:48 +00:00
|
|
|
for entry in self.entries {
|
2019-11-09 12:07:47 +00:00
|
|
|
Self::encode_entry(&mut table, &entry, start)?;
|
2019-11-08 09:35:48 +00:00
|
|
|
}
|
|
|
|
|
2019-11-09 12:07:47 +00:00
|
|
|
let mut data = Vec::new();
|
|
|
|
catalog_encode_u64(&mut data, table.len() as u64)?;
|
|
|
|
data.extend_from_slice(&table);
|
|
|
|
|
|
|
|
Ok((self.name, data))
|
2019-08-16 10:27:17 +00:00
|
|
|
}
|
2019-11-09 13:37:33 +00:00
|
|
|
|
2019-11-12 10:21:19 +00:00
|
|
|
fn parse<C: FnMut(CatalogEntryType, &[u8], u64, u64, u64) -> Result<bool, Error>>(
|
2019-11-09 13:37:33 +00:00
|
|
|
data: &[u8],
|
|
|
|
mut callback: C,
|
|
|
|
) -> Result<(), Error> {
|
|
|
|
|
|
|
|
let mut cursor = data;
|
|
|
|
|
|
|
|
let entries = catalog_decode_u64(&mut cursor)?;
|
|
|
|
|
2019-11-12 08:25:39 +00:00
|
|
|
let mut name_buf = vec![0u8; 4096];
|
|
|
|
|
2019-11-09 13:37:33 +00:00
|
|
|
for _ in 0..entries {
|
|
|
|
|
|
|
|
let mut buf = [ 0u8 ];
|
|
|
|
cursor.read_exact(&mut buf)?;
|
|
|
|
let etype = CatalogEntryType::try_from(buf[0])?;
|
|
|
|
|
2019-11-12 08:25:39 +00:00
|
|
|
let name_len = catalog_decode_u64(&mut cursor)? as usize;
|
|
|
|
if name_len >= name_buf.len() {
|
|
|
|
bail!("directory entry name too long ({} >= {})", name_len, name_buf.len());
|
|
|
|
}
|
|
|
|
let name = &mut name_buf[0..name_len];
|
|
|
|
cursor.read_exact(name)?;
|
2019-11-09 13:37:33 +00:00
|
|
|
|
2019-11-12 10:21:19 +00:00
|
|
|
let cont = match etype {
|
2019-11-09 13:37:33 +00:00
|
|
|
CatalogEntryType::Directory => {
|
|
|
|
let offset = catalog_decode_u64(&mut cursor)?;
|
2019-11-12 10:21:19 +00:00
|
|
|
callback(etype, name, offset, 0, 0)?
|
2019-11-09 13:37:33 +00:00
|
|
|
}
|
|
|
|
CatalogEntryType::File => {
|
|
|
|
let size = catalog_decode_u64(&mut cursor)?;
|
|
|
|
let mtime = catalog_decode_u64(&mut cursor)?;
|
2019-11-12 10:21:19 +00:00
|
|
|
callback(etype, name, 0, size, mtime)?
|
2019-11-09 13:37:33 +00:00
|
|
|
}
|
|
|
|
_ => {
|
2019-11-12 10:21:19 +00:00
|
|
|
callback(etype, name, 0, 0, 0)?
|
2019-11-09 13:37:33 +00:00
|
|
|
}
|
2019-11-12 10:21:19 +00:00
|
|
|
};
|
|
|
|
if !cont {
|
|
|
|
return Ok(());
|
2019-11-09 13:37:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if !cursor.is_empty() {
|
|
|
|
bail!("unable to parse whole catalog data block");
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
2019-11-08 09:35:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub struct CatalogWriter<W> {
|
|
|
|
writer: W,
|
|
|
|
dirstack: Vec<DirInfo>,
|
|
|
|
pos: u64,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl <W: Write> CatalogWriter<W> {
|
|
|
|
|
|
|
|
pub fn new(writer: W) -> Result<Self, Error> {
|
2019-11-10 10:34:55 +00:00
|
|
|
let mut me = Self { writer, dirstack: vec![ DirInfo::new_rootdir() ], pos: 0 };
|
|
|
|
me.write_all(&PROXMOX_CATALOG_FILE_MAGIC_1_0)?;
|
|
|
|
Ok(me)
|
2019-08-16 10:27:17 +00:00
|
|
|
}
|
2019-11-08 09:35:48 +00:00
|
|
|
|
2019-11-10 10:38:31 +00:00
|
|
|
fn write_all(&mut self, data: &[u8]) -> Result<(), Error> {
|
|
|
|
self.writer.write_all(data)?;
|
|
|
|
self.pos += u64::try_from(data.len())?;
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2019-11-08 09:35:48 +00:00
|
|
|
pub fn finish(&mut self) -> Result<(), Error> {
|
|
|
|
if self.dirstack.len() != 1 {
|
|
|
|
bail!("unable to finish catalog at level {}", self.dirstack.len());
|
|
|
|
}
|
|
|
|
|
|
|
|
let dir = self.dirstack.pop().unwrap();
|
|
|
|
|
|
|
|
let start = self.pos;
|
|
|
|
let (_, data) = dir.encode(start)?;
|
|
|
|
self.write_all(&data)?;
|
|
|
|
|
|
|
|
self.write_all(&start.to_le_bytes())?;
|
|
|
|
|
|
|
|
self.writer.flush()?;
|
|
|
|
|
|
|
|
Ok(())
|
2019-08-16 10:27:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-08 09:35:48 +00:00
|
|
|
impl <W: Write> BackupCatalogWriter for CatalogWriter<W> {
|
2019-08-16 10:27:17 +00:00
|
|
|
|
|
|
|
fn start_directory(&mut self, name: &CStr) -> Result<(), Error> {
|
2019-11-08 09:35:48 +00:00
|
|
|
let new = DirInfo::new(name.to_owned());
|
|
|
|
self.dirstack.push(new);
|
2019-08-16 10:27:17 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn end_directory(&mut self) -> Result<(), Error> {
|
2019-11-08 09:35:48 +00:00
|
|
|
let (start, name) = match self.dirstack.pop() {
|
|
|
|
Some(dir) => {
|
|
|
|
let start = self.pos;
|
|
|
|
let (name, data) = dir.encode(start)?;
|
|
|
|
self.write_all(&data)?;
|
|
|
|
(start, name)
|
|
|
|
}
|
|
|
|
None => {
|
|
|
|
bail!("got unexpected end_directory level 0");
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
let current = self.dirstack.last_mut().ok_or_else(|| format_err!("outside root"))?;
|
|
|
|
let name = name.to_bytes().to_vec();
|
2019-11-12 08:25:39 +00:00
|
|
|
current.entries.push(DirEntry { name, attr: DirEntryAttribute::Directory { start } });
|
2019-11-08 09:35:48 +00:00
|
|
|
|
2019-08-16 10:27:17 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn add_file(&mut self, name: &CStr, size: u64, mtime: u64) -> Result<(), Error> {
|
2019-11-08 09:35:48 +00:00
|
|
|
let dir = self.dirstack.last_mut().ok_or_else(|| format_err!("outside root"))?;
|
|
|
|
let name = name.to_bytes().to_vec();
|
2019-11-12 08:25:39 +00:00
|
|
|
dir.entries.push(DirEntry { name, attr: DirEntryAttribute::File { size, mtime } });
|
2019-08-16 10:27:17 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn add_symlink(&mut self, name: &CStr) -> Result<(), Error> {
|
2019-11-08 09:35:48 +00:00
|
|
|
let dir = self.dirstack.last_mut().ok_or_else(|| format_err!("outside root"))?;
|
|
|
|
let name = name.to_bytes().to_vec();
|
2019-11-12 08:25:39 +00:00
|
|
|
dir.entries.push(DirEntry { name, attr: DirEntryAttribute::Symlink });
|
2019-08-16 10:27:17 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn add_hardlink(&mut self, name: &CStr) -> Result<(), Error> {
|
2019-11-08 09:35:48 +00:00
|
|
|
let dir = self.dirstack.last_mut().ok_or_else(|| format_err!("outside root"))?;
|
|
|
|
let name = name.to_bytes().to_vec();
|
2019-11-12 08:25:39 +00:00
|
|
|
dir.entries.push(DirEntry { name, attr: DirEntryAttribute::Hardlink });
|
2019-08-16 10:27:17 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn add_block_device(&mut self, name: &CStr) -> Result<(), Error> {
|
2019-11-08 09:35:48 +00:00
|
|
|
let dir = self.dirstack.last_mut().ok_or_else(|| format_err!("outside root"))?;
|
|
|
|
let name = name.to_bytes().to_vec();
|
2019-11-12 08:25:39 +00:00
|
|
|
dir.entries.push(DirEntry { name, attr: DirEntryAttribute::BlockDevice });
|
|
|
|
Ok(())
|
2019-08-16 10:27:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn add_char_device(&mut self, name: &CStr) -> Result<(), Error> {
|
2019-11-08 09:35:48 +00:00
|
|
|
let dir = self.dirstack.last_mut().ok_or_else(|| format_err!("outside root"))?;
|
|
|
|
let name = name.to_bytes().to_vec();
|
2019-11-12 08:25:39 +00:00
|
|
|
dir.entries.push(DirEntry { name, attr: DirEntryAttribute::CharDevice });
|
2019-08-16 10:27:17 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn add_fifo(&mut self, name: &CStr) -> Result<(), Error> {
|
2019-11-08 09:35:48 +00:00
|
|
|
let dir = self.dirstack.last_mut().ok_or_else(|| format_err!("outside root"))?;
|
|
|
|
let name = name.to_bytes().to_vec();
|
2019-11-12 08:25:39 +00:00
|
|
|
dir.entries.push(DirEntry { name, attr: DirEntryAttribute::Fifo });
|
2019-08-16 10:27:17 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn add_socket(&mut self, name: &CStr) -> Result<(), Error> {
|
2019-11-08 09:35:48 +00:00
|
|
|
let dir = self.dirstack.last_mut().ok_or_else(|| format_err!("outside root"))?;
|
|
|
|
let name = name.to_bytes().to_vec();
|
2019-11-12 08:25:39 +00:00
|
|
|
dir.entries.push(DirEntry { name, attr: DirEntryAttribute::Socket });
|
2019-08-16 10:27:17 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-08 09:35:48 +00:00
|
|
|
// fixme: move to somehere else?
|
|
|
|
/// Implement Write to tokio mpsc channel Sender
|
|
|
|
pub struct SenderWriter(tokio::sync::mpsc::Sender<Result<Vec<u8>, Error>>);
|
|
|
|
|
|
|
|
impl SenderWriter {
|
|
|
|
pub fn new(sender: tokio::sync::mpsc::Sender<Result<Vec<u8>, Error>>) -> Self {
|
|
|
|
Self(sender)
|
|
|
|
}
|
2019-08-16 10:27:17 +00:00
|
|
|
}
|
|
|
|
|
2019-11-08 09:35:48 +00:00
|
|
|
impl Write for SenderWriter {
|
|
|
|
fn write(&mut self, buf: &[u8]) -> Result<usize, std::io::Error> {
|
|
|
|
futures::executor::block_on(async move {
|
|
|
|
self.0.send(Ok(buf.to_vec())).await
|
|
|
|
.map_err(|err| std::io::Error::new(std::io::ErrorKind::Other, err.to_string()))?;
|
|
|
|
Ok(buf.len())
|
|
|
|
})
|
|
|
|
}
|
2019-08-16 10:27:17 +00:00
|
|
|
|
2019-11-08 09:35:48 +00:00
|
|
|
fn flush(&mut self) -> Result<(), std::io::Error> {
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
2019-08-16 10:27:17 +00:00
|
|
|
|
2019-11-08 09:35:48 +00:00
|
|
|
pub struct CatalogReader<R> {
|
|
|
|
reader: R,
|
|
|
|
}
|
2019-08-16 10:27:17 +00:00
|
|
|
|
2019-11-08 09:35:48 +00:00
|
|
|
impl <R: Read + Seek> CatalogReader<R> {
|
2019-08-16 10:27:17 +00:00
|
|
|
|
2019-11-08 09:35:48 +00:00
|
|
|
pub fn new(reader: R) -> Self {
|
|
|
|
Self { reader }
|
2019-08-16 10:27:17 +00:00
|
|
|
}
|
|
|
|
|
2019-11-12 11:47:21 +00:00
|
|
|
/// Print catalog to stdout
|
2019-11-08 09:35:48 +00:00
|
|
|
pub fn dump(&mut self) -> Result<(), Error> {
|
2019-08-16 10:27:17 +00:00
|
|
|
|
2019-11-12 11:47:21 +00:00
|
|
|
let root = self.root()?;
|
|
|
|
match root {
|
|
|
|
DirEntry { attr: DirEntryAttribute::Directory { start }, .. }=> {
|
|
|
|
self.dump_dir(std::path::Path::new("./"), start)
|
|
|
|
}
|
|
|
|
_ => unreachable!(),
|
|
|
|
}
|
2019-11-08 09:35:48 +00:00
|
|
|
}
|
2019-08-16 10:27:17 +00:00
|
|
|
|
2019-11-12 10:15:05 +00:00
|
|
|
/// Get the root DirEntry
|
|
|
|
pub fn root(&mut self) -> Result<DirEntry, Error> {
|
|
|
|
// Root dir is special
|
|
|
|
// mixme: verify magic
|
|
|
|
self.reader.seek(SeekFrom::End(-8))?;
|
|
|
|
let start = unsafe { self.reader.read_le_value::<u64>()? };
|
|
|
|
Ok(DirEntry { name: b"".to_vec(), attr: DirEntryAttribute::Directory { start } })
|
|
|
|
}
|
2019-08-16 10:27:17 +00:00
|
|
|
|
2019-11-12 10:15:05 +00:00
|
|
|
/// Read all directory entries
|
|
|
|
pub fn read_dir(
|
|
|
|
&mut self,
|
|
|
|
parent: &DirEntry,
|
|
|
|
) -> Result<Vec<DirEntry>, Error> {
|
2019-08-16 10:27:17 +00:00
|
|
|
|
2019-11-12 10:15:05 +00:00
|
|
|
let start = match parent.attr {
|
|
|
|
DirEntryAttribute::Directory { start } => start,
|
|
|
|
_ => bail!("parent is not a directory - internal error"),
|
|
|
|
};
|
2019-08-16 10:27:17 +00:00
|
|
|
|
2019-11-12 10:15:05 +00:00
|
|
|
let data = self.read_raw_dirinfo_block(start)?;
|
2019-08-16 10:27:17 +00:00
|
|
|
|
2019-11-12 10:15:05 +00:00
|
|
|
let mut entry_list = Vec::new();
|
|
|
|
|
|
|
|
DirInfo::parse(&data, |etype, name, offset, size, mtime| {
|
|
|
|
let entry = DirEntry::new(etype, name.to_vec(), offset, size, mtime);
|
|
|
|
entry_list.push(entry);
|
2019-11-12 10:21:19 +00:00
|
|
|
Ok(true)
|
2019-11-12 10:15:05 +00:00
|
|
|
})?;
|
|
|
|
|
|
|
|
Ok(entry_list)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Lockup a DirEntry inside a parent directory
|
|
|
|
pub fn lookup(
|
|
|
|
&mut self,
|
|
|
|
parent: &DirEntry,
|
|
|
|
filename: &[u8],
|
|
|
|
) -> Result<DirEntry, Error> {
|
|
|
|
|
|
|
|
let start = match parent.attr {
|
|
|
|
DirEntryAttribute::Directory { start } => start,
|
|
|
|
_ => bail!("parent is not a directory - internal error"),
|
|
|
|
};
|
|
|
|
|
|
|
|
let data = self.read_raw_dirinfo_block(start)?;
|
|
|
|
|
|
|
|
let mut item = None;
|
|
|
|
DirInfo::parse(&data, |etype, name, offset, size, mtime| {
|
|
|
|
if name != filename {
|
2019-11-12 10:21:19 +00:00
|
|
|
return Ok(true);
|
2019-11-12 10:15:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
let entry = DirEntry::new(etype, name.to_vec(), offset, size, mtime);
|
|
|
|
item = Some(entry);
|
2019-11-12 10:21:19 +00:00
|
|
|
Ok(false) // stop parsing
|
2019-11-12 10:15:05 +00:00
|
|
|
})?;
|
|
|
|
|
|
|
|
match item {
|
|
|
|
None => bail!("no such file"),
|
|
|
|
Some(entry) => Ok(entry),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Read the raw directory info block from current reader position.
|
|
|
|
fn read_raw_dirinfo_block(&mut self, start: u64) -> Result<Vec<u8>, Error> {
|
|
|
|
self.reader.seek(SeekFrom::Start(start))?;
|
|
|
|
let size = catalog_decode_u64(&mut self.reader)?;
|
|
|
|
if size < 1 { bail!("got small directory size {}", size) };
|
2019-11-09 12:07:47 +00:00
|
|
|
let data = self.reader.read_exact_allocated(size as usize)?;
|
2019-11-12 10:15:05 +00:00
|
|
|
Ok(data)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn dump_dir(&mut self, prefix: &std::path::Path, start: u64) -> Result<(), Error> {
|
|
|
|
|
|
|
|
let data = self.read_raw_dirinfo_block(start)?;
|
2019-08-16 10:27:17 +00:00
|
|
|
|
2019-11-09 13:37:33 +00:00
|
|
|
DirInfo::parse(&data, |etype, name, offset, size, mtime| {
|
2019-11-08 09:35:48 +00:00
|
|
|
|
|
|
|
let mut path = std::path::PathBuf::from(prefix);
|
2019-11-12 08:25:39 +00:00
|
|
|
let name: &OsStr = OsStrExt::from_bytes(name);
|
|
|
|
path.push(name);
|
2019-08-16 10:27:17 +00:00
|
|
|
|
|
|
|
match etype {
|
|
|
|
CatalogEntryType::Directory => {
|
2019-11-11 13:19:54 +00:00
|
|
|
println!("{} {:?}", etype, path);
|
2019-11-08 09:35:48 +00:00
|
|
|
if offset > start {
|
|
|
|
bail!("got wrong directory offset ({} > {})", offset, start);
|
|
|
|
}
|
|
|
|
let pos = start - offset;
|
|
|
|
self.dump_dir(&path, pos)?;
|
2019-08-16 10:27:17 +00:00
|
|
|
}
|
|
|
|
CatalogEntryType::File => {
|
2019-11-08 09:35:48 +00:00
|
|
|
let dt = Local.timestamp(mtime as i64, 0);
|
|
|
|
|
2019-11-09 13:37:33 +00:00
|
|
|
println!(
|
|
|
|
"{} {:?} {} {}",
|
2019-11-11 13:19:54 +00:00
|
|
|
etype,
|
2019-11-09 13:37:33 +00:00
|
|
|
path,
|
|
|
|
size,
|
|
|
|
dt.to_rfc3339_opts(chrono::SecondsFormat::Secs, false),
|
2019-11-08 09:35:48 +00:00
|
|
|
);
|
2019-08-16 10:27:17 +00:00
|
|
|
}
|
2019-11-08 09:35:48 +00:00
|
|
|
_ => {
|
2019-11-11 13:19:54 +00:00
|
|
|
println!("{} {:?}", etype, path);
|
2019-08-16 10:27:17 +00:00
|
|
|
}
|
|
|
|
}
|
2019-11-09 12:21:20 +00:00
|
|
|
|
2019-11-12 10:21:19 +00:00
|
|
|
Ok(true)
|
2019-11-09 13:37:33 +00:00
|
|
|
})
|
2019-08-16 10:27:17 +00:00
|
|
|
}
|
|
|
|
}
|
2019-11-09 12:07:47 +00:00
|
|
|
|
|
|
|
/// Serialize u64 as short, variable length byte sequence
|
|
|
|
///
|
|
|
|
/// Stores 7 bits per byte, Bit 8 indicates the end of the sequence (when not set).
|
|
|
|
/// We limit values to a maximum of 2^63.
|
|
|
|
pub fn catalog_encode_u64<W: Write>(writer: &mut W, v: u64) -> Result<(), Error> {
|
|
|
|
let mut enc = Vec::new();
|
|
|
|
|
|
|
|
if (v & (1<<63)) != 0 { bail!("catalog_encode_u64 failed - value >= 2^63"); }
|
|
|
|
let mut d = v;
|
|
|
|
loop {
|
|
|
|
if d < 128 {
|
|
|
|
enc.push(d as u8);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
enc.push((128 | (d & 127)) as u8);
|
|
|
|
d = d >> 7;
|
|
|
|
}
|
|
|
|
writer.write_all(&enc)?;
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Deserialize u64 from variable length byte sequence
|
|
|
|
///
|
|
|
|
/// We currently read maximal 9 bytes, which give a maximum of 63 bits.
|
|
|
|
pub fn catalog_decode_u64<R: Read>(reader: &mut R) -> Result<u64, Error> {
|
|
|
|
|
|
|
|
let mut v: u64 = 0;
|
|
|
|
let mut buf = [0u8];
|
|
|
|
|
|
|
|
for i in 0..9 { // only allow 9 bytes (63 bits)
|
|
|
|
if buf.is_empty() {
|
|
|
|
bail!("decode_u64 failed - unexpected EOB");
|
|
|
|
}
|
|
|
|
reader.read_exact(&mut buf)?;
|
|
|
|
let t = buf[0];
|
|
|
|
if t < 128 {
|
|
|
|
v |= (t as u64) << (i*7);
|
|
|
|
return Ok(v);
|
|
|
|
} else {
|
|
|
|
v |= ((t & 127) as u64) << (i*7);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bail!("decode_u64 failed - missing end marker");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_catalog_u64_encoder() {
|
|
|
|
|
|
|
|
fn test_encode_decode(value: u64) {
|
|
|
|
|
|
|
|
let mut data = Vec::new();
|
|
|
|
catalog_encode_u64(&mut data, value).unwrap();
|
|
|
|
|
|
|
|
//println!("ENCODE {} {:?}", value, data);
|
|
|
|
|
|
|
|
let slice = &mut &data[..];
|
|
|
|
let decoded = catalog_decode_u64(slice).unwrap();
|
|
|
|
|
|
|
|
//println!("DECODE {}", decoded);
|
|
|
|
|
|
|
|
assert!(decoded == value);
|
|
|
|
}
|
|
|
|
|
|
|
|
test_encode_decode(126);
|
|
|
|
test_encode_decode((1<<12)-1);
|
|
|
|
test_encode_decode((1<<20)-1);
|
|
|
|
test_encode_decode((1<<50)-1);
|
|
|
|
test_encode_decode((1<<63)-1);
|
|
|
|
}
|