2020-03-23 14:03:18 +00:00
|
|
|
use std::convert::TryFrom;
|
2019-11-12 08:25:39 +00:00
|
|
|
use std::ffi::{CStr, CString, OsStr};
|
2020-03-23 14:03:18 +00:00
|
|
|
use std::fmt;
|
2022-04-14 11:27:53 +00:00
|
|
|
use std::io::{Read, Seek, SeekFrom, Write};
|
2020-03-23 14:03:18 +00:00
|
|
|
use std::os::unix::ffi::OsStrExt;
|
2019-08-16 10:27:17 +00:00
|
|
|
|
2020-03-23 14:03:18 +00:00
|
|
|
use anyhow::{bail, format_err, Error};
|
2021-08-31 08:45:32 +00:00
|
|
|
use serde::{Deserialize, Serialize};
|
2019-08-16 10:27:17 +00:00
|
|
|
|
2020-03-23 14:03:18 +00:00
|
|
|
use pathpatterns::{MatchList, MatchType};
|
2021-10-08 09:19:37 +00:00
|
|
|
|
|
|
|
use proxmox_io::ReadExt;
|
|
|
|
use proxmox_schema::api;
|
2019-08-16 10:27:17 +00:00
|
|
|
|
2021-07-06 10:49:10 +00:00
|
|
|
use crate::file_formats::PROXMOX_CATALOG_FILE_MAGIC_1_0;
|
|
|
|
|
|
|
|
/// Trait for writing file list catalogs.
|
|
|
|
///
|
|
|
|
/// A file list catalog simply stores a directory tree. Such catalogs may be used as index to do a
|
|
|
|
/// fast search for files.
|
|
|
|
pub trait BackupCatalogWriter {
|
|
|
|
fn start_directory(&mut self, name: &CStr) -> Result<(), Error>;
|
|
|
|
fn end_directory(&mut self) -> Result<(), Error>;
|
|
|
|
fn add_file(&mut self, name: &CStr, size: u64, mtime: i64) -> Result<(), Error>;
|
|
|
|
fn add_symlink(&mut self, name: &CStr) -> Result<(), Error>;
|
|
|
|
fn add_hardlink(&mut self, name: &CStr) -> Result<(), Error>;
|
|
|
|
fn add_block_device(&mut self, name: &CStr) -> Result<(), Error>;
|
|
|
|
fn add_char_device(&mut self, name: &CStr) -> Result<(), Error>;
|
|
|
|
fn add_fifo(&mut self, name: &CStr) -> Result<(), Error>;
|
|
|
|
fn add_socket(&mut self, name: &CStr) -> Result<(), Error>;
|
|
|
|
}
|
2019-08-16 10:27:17 +00:00
|
|
|
|
2019-11-12 09:14:33 +00:00
|
|
|
#[repr(u8)]
|
2022-04-14 11:27:53 +00:00
|
|
|
#[derive(Copy, Clone, PartialEq)]
|
2021-07-06 10:49:10 +00:00
|
|
|
pub 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 {
|
2022-04-14 11:27:53 +00:00
|
|
|
type Error = Error;
|
2019-11-12 09:14:33 +00:00
|
|
|
|
|
|
|
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)),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-23 10:09:51 +00:00
|
|
|
impl From<&DirEntryAttribute> for CatalogEntryType {
|
|
|
|
fn from(value: &DirEntryAttribute) -> Self {
|
|
|
|
match value {
|
|
|
|
DirEntryAttribute::Directory { .. } => CatalogEntryType::Directory,
|
|
|
|
DirEntryAttribute::File { .. } => CatalogEntryType::File,
|
|
|
|
DirEntryAttribute::Symlink => CatalogEntryType::Symlink,
|
|
|
|
DirEntryAttribute::Hardlink => CatalogEntryType::Hardlink,
|
|
|
|
DirEntryAttribute::BlockDevice => CatalogEntryType::BlockDevice,
|
|
|
|
DirEntryAttribute::CharDevice => CatalogEntryType::CharDevice,
|
|
|
|
DirEntryAttribute::Fifo => CatalogEntryType::Fifo,
|
|
|
|
DirEntryAttribute::Socket => CatalogEntryType::Socket,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-12 09:14:33 +00:00
|
|
|
impl fmt::Display for CatalogEntryType {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
|
|
write!(f, "{}", char::from(*self as u8))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-12 12:23:40 +00:00
|
|
|
/// Represents a named directory entry
|
|
|
|
///
|
|
|
|
/// The ``attr`` property contain the exact type with type specific
|
|
|
|
/// attributes.
|
2020-02-27 16:12:55 +00:00
|
|
|
#[derive(Clone, PartialEq)]
|
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 12:23:40 +00:00
|
|
|
/// Used to specific additional attributes inside DirEntry
|
2020-03-23 14:03:18 +00:00
|
|
|
#[derive(Clone, Debug, PartialEq)]
|
2019-11-12 12:23:40 +00:00
|
|
|
pub enum DirEntryAttribute {
|
|
|
|
Directory { start: u64 },
|
2020-10-22 07:05:37 +00:00
|
|
|
File { size: u64, mtime: i64 },
|
2019-11-12 12:23:40 +00:00
|
|
|
Symlink,
|
|
|
|
Hardlink,
|
|
|
|
BlockDevice,
|
|
|
|
CharDevice,
|
|
|
|
Fifo,
|
|
|
|
Socket,
|
|
|
|
}
|
|
|
|
|
2019-11-12 10:15:05 +00:00
|
|
|
impl DirEntry {
|
2020-10-22 07:05:37 +00:00
|
|
|
fn new(etype: CatalogEntryType, name: Vec<u8>, start: u64, size: u64, mtime: i64) -> Self {
|
2019-11-12 10:15:05 +00:00
|
|
|
match etype {
|
2022-04-14 11:27:53 +00:00
|
|
|
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,
|
|
|
|
},
|
2019-11-12 10:15:05 +00:00
|
|
|
}
|
|
|
|
}
|
2019-11-14 16:42:17 +00:00
|
|
|
|
2020-03-23 14:03:18 +00:00
|
|
|
/// Get file mode bits for this entry to be used with the `MatchList` api.
|
|
|
|
pub fn get_file_mode(&self) -> Option<u32> {
|
2022-04-14 11:27:53 +00:00
|
|
|
Some(match self.attr {
|
|
|
|
DirEntryAttribute::Directory { .. } => pxar::mode::IFDIR,
|
|
|
|
DirEntryAttribute::File { .. } => pxar::mode::IFREG,
|
|
|
|
DirEntryAttribute::Symlink => pxar::mode::IFLNK,
|
|
|
|
DirEntryAttribute::Hardlink => return None,
|
|
|
|
DirEntryAttribute::BlockDevice => pxar::mode::IFBLK,
|
|
|
|
DirEntryAttribute::CharDevice => pxar::mode::IFCHR,
|
|
|
|
DirEntryAttribute::Fifo => pxar::mode::IFIFO,
|
|
|
|
DirEntryAttribute::Socket => pxar::mode::IFSOCK,
|
|
|
|
} as u32)
|
2020-03-23 14:03:18 +00:00
|
|
|
}
|
|
|
|
|
2020-02-27 16:12:57 +00:00
|
|
|
/// Check if DirEntry is a directory
|
2019-11-14 16:42:17 +00:00
|
|
|
pub fn is_directory(&self) -> bool {
|
2021-01-19 10:08:10 +00:00
|
|
|
matches!(self.attr, DirEntryAttribute::Directory { .. })
|
2019-11-14 16:42:17 +00:00
|
|
|
}
|
2020-02-27 16:12:56 +00:00
|
|
|
|
|
|
|
/// Check if DirEntry is a symlink
|
|
|
|
pub fn is_symlink(&self) -> bool {
|
2021-01-19 10:08:10 +00:00
|
|
|
matches!(self.attr, DirEntryAttribute::Symlink { .. })
|
2020-02-27 16:12:56 +00:00
|
|
|
}
|
2019-11-12 10:15:05 +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 {
|
2022-04-14 11:27:53 +00:00
|
|
|
DirInfo {
|
|
|
|
name,
|
|
|
|
entries: Vec::new(),
|
|
|
|
}
|
2019-11-08 09:35:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn new_rootdir() -> Self {
|
|
|
|
DirInfo::new(CString::new(b"/".to_vec()).unwrap())
|
|
|
|
}
|
|
|
|
|
2022-04-14 11:27:53 +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 {
|
2022-04-14 11:27:53 +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
|
|
|
}
|
2022-04-14 11:27:53 +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)?;
|
2020-10-22 07:05:37 +00:00
|
|
|
catalog_encode_i64(writer, *mtime)?;
|
2019-11-08 09:35:48 +00:00
|
|
|
}
|
2022-04-14 11:27:53 +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
|
|
|
}
|
2022-04-14 11:27:53 +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
|
|
|
}
|
2022-04-14 11:27:53 +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
|
|
|
}
|
2022-04-14 11:27:53 +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
|
|
|
}
|
2022-04-14 11:27:53 +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
|
|
|
}
|
2022-04-14 11:27:53 +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
|
|
|
|
2020-10-22 07:05:37 +00:00
|
|
|
fn parse<C: FnMut(CatalogEntryType, &[u8], u64, u64, i64) -> 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 {
|
2022-04-14 11:27:53 +00:00
|
|
|
let mut buf = [0u8];
|
2019-11-09 13:37:33 +00:00
|
|
|
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() {
|
2022-04-14 11:27:53 +00:00
|
|
|
bail!(
|
|
|
|
"directory entry name too long ({} >= {})",
|
|
|
|
name_len,
|
|
|
|
name_buf.len()
|
|
|
|
);
|
2019-11-12 08:25:39 +00:00
|
|
|
}
|
|
|
|
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)?;
|
2020-10-22 07:05:37 +00:00
|
|
|
let mtime = catalog_decode_i64(&mut cursor)?;
|
2019-11-12 10:21:19 +00:00
|
|
|
callback(etype, name, 0, size, mtime)?
|
2019-11-09 13:37:33 +00:00
|
|
|
}
|
2022-04-14 11:27:53 +00:00
|
|
|
_ => callback(etype, name, 0, 0, 0)?,
|
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
|
|
|
}
|
|
|
|
|
2019-11-12 12:23:40 +00:00
|
|
|
/// Write small catalog files
|
|
|
|
///
|
|
|
|
/// A Catalogs simply contains list of files and directories
|
|
|
|
/// (directory tree). They are use to find content without having to
|
|
|
|
/// search the real archive (which may be large). For files, they
|
|
|
|
/// include the last modification time and file size.
|
2019-11-08 09:35:48 +00:00
|
|
|
pub struct CatalogWriter<W> {
|
|
|
|
writer: W,
|
|
|
|
dirstack: Vec<DirInfo>,
|
|
|
|
pos: u64,
|
|
|
|
}
|
|
|
|
|
2022-04-14 11:27:53 +00:00
|
|
|
impl<W: Write> CatalogWriter<W> {
|
2019-11-12 12:23:40 +00:00
|
|
|
/// Create a new CatalogWriter instance
|
2019-11-08 09:35:48 +00:00
|
|
|
pub fn new(writer: W) -> Result<Self, Error> {
|
2022-04-14 11:27:53 +00:00
|
|
|
let mut me = Self {
|
|
|
|
writer,
|
|
|
|
dirstack: vec![DirInfo::new_rootdir()],
|
|
|
|
pos: 0,
|
|
|
|
};
|
2019-11-10 10:34:55 +00:00
|
|
|
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-12 12:23:40 +00:00
|
|
|
/// Finish writing, flush all data
|
|
|
|
///
|
|
|
|
/// This need to be called before drop.
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-14 11:27:53 +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");
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-04-14 11:27:53 +00:00
|
|
|
let current = self
|
|
|
|
.dirstack
|
|
|
|
.last_mut()
|
|
|
|
.ok_or_else(|| format_err!("outside root"))?;
|
2019-11-08 09:35:48 +00:00
|
|
|
let name = name.to_bytes().to_vec();
|
2022-04-14 11:27:53 +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(())
|
|
|
|
}
|
|
|
|
|
2020-10-22 07:05:37 +00:00
|
|
|
fn add_file(&mut self, name: &CStr, size: u64, mtime: i64) -> Result<(), Error> {
|
2022-04-14 11:27:53 +00:00
|
|
|
let dir = self
|
|
|
|
.dirstack
|
|
|
|
.last_mut()
|
|
|
|
.ok_or_else(|| format_err!("outside root"))?;
|
2019-11-08 09:35:48 +00:00
|
|
|
let name = name.to_bytes().to_vec();
|
2022-04-14 11:27:53 +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> {
|
2022-04-14 11:27:53 +00:00
|
|
|
let dir = self
|
|
|
|
.dirstack
|
|
|
|
.last_mut()
|
|
|
|
.ok_or_else(|| format_err!("outside root"))?;
|
2019-11-08 09:35:48 +00:00
|
|
|
let name = name.to_bytes().to_vec();
|
2022-04-14 11:27:53 +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> {
|
2022-04-14 11:27:53 +00:00
|
|
|
let dir = self
|
|
|
|
.dirstack
|
|
|
|
.last_mut()
|
|
|
|
.ok_or_else(|| format_err!("outside root"))?;
|
2019-11-08 09:35:48 +00:00
|
|
|
let name = name.to_bytes().to_vec();
|
2022-04-14 11:27:53 +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> {
|
2022-04-14 11:27:53 +00:00
|
|
|
let dir = self
|
|
|
|
.dirstack
|
|
|
|
.last_mut()
|
|
|
|
.ok_or_else(|| format_err!("outside root"))?;
|
2019-11-08 09:35:48 +00:00
|
|
|
let name = name.to_bytes().to_vec();
|
2022-04-14 11:27:53 +00:00
|
|
|
dir.entries.push(DirEntry {
|
|
|
|
name,
|
|
|
|
attr: DirEntryAttribute::BlockDevice,
|
|
|
|
});
|
2019-11-12 08:25:39 +00:00
|
|
|
Ok(())
|
2019-08-16 10:27:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn add_char_device(&mut self, name: &CStr) -> Result<(), Error> {
|
2022-04-14 11:27:53 +00:00
|
|
|
let dir = self
|
|
|
|
.dirstack
|
|
|
|
.last_mut()
|
|
|
|
.ok_or_else(|| format_err!("outside root"))?;
|
2019-11-08 09:35:48 +00:00
|
|
|
let name = name.to_bytes().to_vec();
|
2022-04-14 11:27:53 +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> {
|
2022-04-14 11:27:53 +00:00
|
|
|
let dir = self
|
|
|
|
.dirstack
|
|
|
|
.last_mut()
|
|
|
|
.ok_or_else(|| format_err!("outside root"))?;
|
2019-11-08 09:35:48 +00:00
|
|
|
let name = name.to_bytes().to_vec();
|
2022-04-14 11:27:53 +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> {
|
2022-04-14 11:27:53 +00:00
|
|
|
let dir = self
|
|
|
|
.dirstack
|
|
|
|
.last_mut()
|
|
|
|
.ok_or_else(|| format_err!("outside root"))?;
|
2019-11-08 09:35:48 +00:00
|
|
|
let name = name.to_bytes().to_vec();
|
2022-04-14 11:27:53 +00:00
|
|
|
dir.entries.push(DirEntry {
|
|
|
|
name,
|
|
|
|
attr: DirEntryAttribute::Socket,
|
|
|
|
});
|
2019-08-16 10:27:17 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-12 12:23:40 +00:00
|
|
|
/// Read Catalog files
|
2019-11-08 09:35:48 +00:00
|
|
|
pub struct CatalogReader<R> {
|
|
|
|
reader: R,
|
|
|
|
}
|
2019-08-16 10:27:17 +00:00
|
|
|
|
2022-04-14 11:27:53 +00:00
|
|
|
impl<R: Read + Seek> CatalogReader<R> {
|
2019-11-12 12:23:40 +00:00
|
|
|
/// Create a new CatalogReader instance
|
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 12:23:40 +00:00
|
|
|
/// Print whole catalog to stdout
|
2019-11-08 09:35:48 +00:00
|
|
|
pub fn dump(&mut self) -> Result<(), Error> {
|
2019-11-12 11:47:21 +00:00
|
|
|
let root = self.root()?;
|
|
|
|
match root {
|
2022-04-14 11:27:53 +00:00
|
|
|
DirEntry {
|
|
|
|
attr: DirEntryAttribute::Directory { start },
|
|
|
|
..
|
|
|
|
} => self.dump_dir(std::path::Path::new("./"), start),
|
2019-11-12 11:47:21 +00:00
|
|
|
_ => 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
|
2022-04-14 11:27:53 +00:00
|
|
|
pub fn root(&mut self) -> Result<DirEntry, Error> {
|
2019-11-12 10:15:05 +00:00
|
|
|
// Root dir is special
|
2019-11-12 11:54:06 +00:00
|
|
|
self.reader.seek(SeekFrom::Start(0))?;
|
2022-04-14 11:27:53 +00:00
|
|
|
let mut magic = [0u8; 8];
|
2019-11-12 11:54:06 +00:00
|
|
|
self.reader.read_exact(&mut magic)?;
|
|
|
|
if magic != PROXMOX_CATALOG_FILE_MAGIC_1_0 {
|
|
|
|
bail!("got unexpected magic number for catalog");
|
|
|
|
}
|
2019-11-12 10:15:05 +00:00
|
|
|
self.reader.seek(SeekFrom::End(-8))?;
|
|
|
|
let start = unsafe { self.reader.read_le_value::<u64>()? };
|
2022-04-14 11:27:53 +00:00
|
|
|
Ok(DirEntry {
|
|
|
|
name: b"".to_vec(),
|
|
|
|
attr: DirEntryAttribute::Directory { start },
|
|
|
|
})
|
2019-11-12 10:15:05 +00:00
|
|
|
}
|
2019-08-16 10:27:17 +00:00
|
|
|
|
2019-11-12 10:15:05 +00:00
|
|
|
/// Read all directory entries
|
2022-04-14 11:27:53 +00:00
|
|
|
pub fn read_dir(&mut self, parent: &DirEntry) -> Result<Vec<DirEntry>, Error> {
|
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| {
|
2019-11-12 15:34:33 +00:00
|
|
|
let entry = DirEntry::new(etype, name.to_vec(), start - offset, size, mtime);
|
2019-11-12 10:15:05 +00:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2021-02-16 17:06:52 +00:00
|
|
|
/// Lookup a DirEntry from an absolute path
|
2022-04-14 11:27:53 +00:00
|
|
|
pub fn lookup_recursive(&mut self, path: &[u8]) -> Result<DirEntry, Error> {
|
2021-02-16 17:06:52 +00:00
|
|
|
let mut current = self.root()?;
|
|
|
|
if path == b"/" {
|
|
|
|
return Ok(current);
|
|
|
|
}
|
|
|
|
|
|
|
|
let components = if !path.is_empty() && path[0] == b'/' {
|
|
|
|
&path[1..]
|
|
|
|
} else {
|
|
|
|
path
|
2022-04-14 11:27:53 +00:00
|
|
|
}
|
|
|
|
.split(|c| *c == b'/');
|
2021-02-16 17:06:52 +00:00
|
|
|
|
|
|
|
for comp in components {
|
|
|
|
if let Some(entry) = self.lookup(¤t, comp)? {
|
|
|
|
current = entry;
|
|
|
|
} else {
|
2022-04-14 11:27:53 +00:00
|
|
|
bail!(
|
|
|
|
"path {:?} not found in catalog",
|
|
|
|
String::from_utf8_lossy(path)
|
|
|
|
);
|
2021-02-16 17:06:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
Ok(current)
|
|
|
|
}
|
|
|
|
|
2019-11-12 10:15:05 +00:00
|
|
|
/// Lockup a DirEntry inside a parent directory
|
|
|
|
pub fn lookup(
|
|
|
|
&mut self,
|
|
|
|
parent: &DirEntry,
|
|
|
|
filename: &[u8],
|
2022-04-14 11:27:53 +00:00
|
|
|
) -> Result<Option<DirEntry>, Error> {
|
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"),
|
|
|
|
};
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2019-11-12 15:34:33 +00:00
|
|
|
let entry = DirEntry::new(etype, name.to_vec(), start - offset, size, mtime);
|
2019-11-12 10:15:05 +00:00
|
|
|
item = Some(entry);
|
2019-11-12 10:21:19 +00:00
|
|
|
Ok(false) // stop parsing
|
2019-11-12 10:15:05 +00:00
|
|
|
})?;
|
|
|
|
|
2020-03-23 14:03:18 +00:00
|
|
|
Ok(item)
|
2019-11-12 10:15:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Read the raw directory info block from current reader position.
|
2022-04-14 11:27:53 +00:00
|
|
|
fn read_raw_dirinfo_block(&mut self, start: u64) -> Result<Vec<u8>, Error> {
|
2019-11-12 10:15:05 +00:00
|
|
|
self.reader.seek(SeekFrom::Start(start))?;
|
|
|
|
let size = catalog_decode_u64(&mut self.reader)?;
|
2022-04-14 11:27:53 +00:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2019-11-12 12:23:40 +00:00
|
|
|
/// Print the content of a directory to stdout
|
2019-11-12 10:15:05 +00:00
|
|
|
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 => {
|
2020-09-12 13:10:47 +00:00
|
|
|
let mut mtime_string = mtime.to_string();
|
2021-10-08 09:19:37 +00:00
|
|
|
if let Ok(s) = proxmox_time::strftime_local("%FT%TZ", mtime as i64) {
|
2020-09-12 13:10:47 +00:00
|
|
|
mtime_string = s;
|
|
|
|
}
|
2019-11-08 09:35:48 +00:00
|
|
|
|
2022-04-14 11:27:53 +00:00
|
|
|
println!("{} {:?} {} {}", etype, path, size, mtime_string,);
|
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-12-16 11:13:47 +00:00
|
|
|
|
|
|
|
/// Finds all entries matching the given match patterns and calls the
|
|
|
|
/// provided callback on them.
|
|
|
|
pub fn find(
|
|
|
|
&mut self,
|
2020-03-23 14:03:18 +00:00
|
|
|
parent: &DirEntry,
|
|
|
|
file_path: &mut Vec<u8>,
|
|
|
|
match_list: &impl MatchList, //&[MatchEntry],
|
|
|
|
callback: &mut dyn FnMut(&[u8]) -> Result<(), Error>,
|
2019-12-16 11:13:47 +00:00
|
|
|
) -> Result<(), Error> {
|
2020-03-23 14:03:18 +00:00
|
|
|
let file_len = file_path.len();
|
2022-04-14 11:27:53 +00:00
|
|
|
for e in self.read_dir(parent)? {
|
2020-03-23 14:03:18 +00:00
|
|
|
let is_dir = e.is_directory();
|
|
|
|
file_path.truncate(file_len);
|
|
|
|
if !e.name.starts_with(b"/") {
|
|
|
|
file_path.reserve(e.name.len() + 1);
|
|
|
|
file_path.push(b'/');
|
|
|
|
}
|
|
|
|
file_path.extend(&e.name);
|
|
|
|
match match_list.matches(&file_path, e.get_file_mode()) {
|
|
|
|
Some(MatchType::Exclude) => continue,
|
2021-12-30 11:57:37 +00:00
|
|
|
Some(MatchType::Include) => callback(file_path)?,
|
2020-03-23 14:03:18 +00:00
|
|
|
None => (),
|
|
|
|
}
|
|
|
|
if is_dir {
|
|
|
|
self.find(&e, file_path, match_list, callback)?;
|
2019-12-16 11:13:47 +00:00
|
|
|
}
|
|
|
|
}
|
2020-03-23 14:03:18 +00:00
|
|
|
file_path.truncate(file_len);
|
2019-12-17 12:15:45 +00:00
|
|
|
|
2019-12-16 11:13:47 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
2021-08-31 08:56:41 +00:00
|
|
|
|
|
|
|
/// Returns the list of content of the given path
|
|
|
|
pub fn list_dir_contents(&mut self, path: &[u8]) -> Result<Vec<ArchiveEntry>, Error> {
|
|
|
|
let dir = self.lookup_recursive(path)?;
|
|
|
|
let mut res = vec![];
|
|
|
|
let mut path = path.to_vec();
|
|
|
|
if !path.is_empty() && path[0] == b'/' {
|
|
|
|
path.remove(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
for direntry in self.read_dir(&dir)? {
|
|
|
|
let mut components = path.clone();
|
|
|
|
components.push(b'/');
|
|
|
|
components.extend(&direntry.name);
|
|
|
|
let mut entry = ArchiveEntry::new(&components, Some(&direntry.attr));
|
|
|
|
if let DirEntryAttribute::File { size, mtime } = direntry.attr {
|
|
|
|
entry.size = size.into();
|
|
|
|
entry.mtime = mtime.into();
|
|
|
|
}
|
|
|
|
res.push(entry);
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(res)
|
|
|
|
}
|
2019-08-16 10:27:17 +00:00
|
|
|
}
|
2019-11-09 12:07:47 +00:00
|
|
|
|
2020-10-22 07:05:37 +00:00
|
|
|
/// Serialize i64 as short, variable length byte sequence
|
|
|
|
///
|
|
|
|
/// Stores 7 bits per byte, Bit 8 indicates the end of the sequence (when not set).
|
|
|
|
/// If the value is negative, we end with a zero byte (0x00).
|
2021-01-19 10:37:49 +00:00
|
|
|
#[allow(clippy::neg_multiply)]
|
2020-10-22 07:05:37 +00:00
|
|
|
pub fn catalog_encode_i64<W: Write>(writer: &mut W, v: i64) -> Result<(), Error> {
|
|
|
|
let mut enc = Vec::new();
|
|
|
|
|
|
|
|
let mut d = if v < 0 {
|
|
|
|
(-1 * (v + 1)) as u64 + 1 // also handles i64::MIN
|
|
|
|
} else {
|
|
|
|
v as u64
|
|
|
|
};
|
|
|
|
|
|
|
|
loop {
|
|
|
|
if d < 128 {
|
|
|
|
if v < 0 {
|
|
|
|
enc.push(128 | d as u8);
|
|
|
|
enc.push(0u8);
|
|
|
|
} else {
|
|
|
|
enc.push(d as u8);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
enc.push((128 | (d & 127)) as u8);
|
2021-01-19 10:12:01 +00:00
|
|
|
d >>= 7;
|
2020-10-22 07:05:37 +00:00
|
|
|
}
|
|
|
|
writer.write_all(&enc)?;
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Deserialize i64 from variable length byte sequence
|
|
|
|
///
|
|
|
|
/// We currently read maximal 11 bytes, which give a maximum of 70 bits + sign.
|
|
|
|
/// this method is compatible with catalog_encode_u64 iff the
|
|
|
|
/// value encoded is <= 2^63 (values > 2^63 cannot be represented in an i64)
|
2021-01-19 10:37:49 +00:00
|
|
|
#[allow(clippy::neg_multiply)]
|
2020-10-22 07:05:37 +00:00
|
|
|
pub fn catalog_decode_i64<R: Read>(reader: &mut R) -> Result<i64, Error> {
|
|
|
|
let mut v: u64 = 0;
|
|
|
|
let mut buf = [0u8];
|
|
|
|
|
2022-04-14 11:27:53 +00:00
|
|
|
for i in 0..11 {
|
|
|
|
// only allow 11 bytes (70 bits + sign marker)
|
2020-10-22 07:05:37 +00:00
|
|
|
if buf.is_empty() {
|
|
|
|
bail!("decode_i64 failed - unexpected EOB");
|
|
|
|
}
|
|
|
|
reader.read_exact(&mut buf)?;
|
|
|
|
|
|
|
|
let t = buf[0];
|
|
|
|
|
|
|
|
if t == 0 {
|
|
|
|
if v == 0 {
|
|
|
|
return Ok(0);
|
|
|
|
}
|
|
|
|
return Ok(((v - 1) as i64 * -1) - 1); // also handles i64::MIN
|
|
|
|
} else if t < 128 {
|
2022-04-14 11:27:53 +00:00
|
|
|
v |= (t as u64) << (i * 7);
|
2020-10-22 07:05:37 +00:00
|
|
|
return Ok(v as i64);
|
|
|
|
} else {
|
2022-04-14 11:27:53 +00:00
|
|
|
v |= ((t & 127) as u64) << (i * 7);
|
2020-10-22 07:05:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bail!("decode_i64 failed - missing end marker");
|
|
|
|
}
|
|
|
|
|
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).
|
|
|
|
pub fn catalog_encode_u64<W: Write>(writer: &mut W, v: u64) -> Result<(), Error> {
|
|
|
|
let mut enc = Vec::new();
|
|
|
|
|
|
|
|
let mut d = v;
|
|
|
|
loop {
|
|
|
|
if d < 128 {
|
|
|
|
enc.push(d as u8);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
enc.push((128 | (d & 127)) as u8);
|
2021-01-19 10:12:01 +00:00
|
|
|
d >>= 7;
|
2019-11-09 12:07:47 +00:00
|
|
|
}
|
|
|
|
writer.write_all(&enc)?;
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Deserialize u64 from variable length byte sequence
|
|
|
|
///
|
2020-10-22 07:05:37 +00:00
|
|
|
/// We currently read maximal 10 bytes, which give a maximum of 70 bits,
|
|
|
|
/// but we currently only encode up to 64 bits
|
2019-11-09 12:07:47 +00:00
|
|
|
pub fn catalog_decode_u64<R: Read>(reader: &mut R) -> Result<u64, Error> {
|
|
|
|
let mut v: u64 = 0;
|
|
|
|
let mut buf = [0u8];
|
|
|
|
|
2022-04-14 11:27:53 +00:00
|
|
|
for i in 0..10 {
|
|
|
|
// only allow 10 bytes (70 bits)
|
2019-11-09 12:07:47 +00:00
|
|
|
if buf.is_empty() {
|
|
|
|
bail!("decode_u64 failed - unexpected EOB");
|
|
|
|
}
|
|
|
|
reader.read_exact(&mut buf)?;
|
|
|
|
let t = buf[0];
|
|
|
|
if t < 128 {
|
2022-04-14 11:27:53 +00:00
|
|
|
v |= (t as u64) << (i * 7);
|
2019-11-09 12:07:47 +00:00
|
|
|
return Ok(v);
|
|
|
|
} else {
|
2022-04-14 11:27:53 +00:00
|
|
|
v |= ((t & 127) as u64) << (i * 7);
|
2019-11-09 12:07:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2020-10-22 07:05:37 +00:00
|
|
|
test_encode_decode(u64::MIN);
|
|
|
|
test_encode_decode(126);
|
2022-04-14 11:27:53 +00:00
|
|
|
test_encode_decode((1 << 12) - 1);
|
|
|
|
test_encode_decode((1 << 20) - 1);
|
|
|
|
test_encode_decode((1 << 50) - 1);
|
2020-10-22 07:05:37 +00:00
|
|
|
test_encode_decode(u64::MAX);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_catalog_i64_encoder() {
|
|
|
|
fn test_encode_decode(value: i64) {
|
|
|
|
let mut data = Vec::new();
|
|
|
|
catalog_encode_i64(&mut data, value).unwrap();
|
|
|
|
|
|
|
|
let slice = &mut &data[..];
|
|
|
|
let decoded = catalog_decode_i64(slice).unwrap();
|
|
|
|
|
|
|
|
assert!(decoded == value);
|
|
|
|
}
|
|
|
|
|
|
|
|
test_encode_decode(0);
|
|
|
|
test_encode_decode(-0);
|
|
|
|
test_encode_decode(126);
|
|
|
|
test_encode_decode(-126);
|
2022-04-14 11:27:53 +00:00
|
|
|
test_encode_decode((1 << 12) - 1);
|
|
|
|
test_encode_decode(-(1 << 12) - 1);
|
|
|
|
test_encode_decode((1 << 20) - 1);
|
|
|
|
test_encode_decode(-(1 << 20) - 1);
|
2020-10-22 07:05:37 +00:00
|
|
|
test_encode_decode(i64::MIN);
|
|
|
|
test_encode_decode(i64::MAX);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_catalog_i64_compatibility() {
|
|
|
|
fn test_encode_decode(value: u64) {
|
|
|
|
let mut data = Vec::new();
|
|
|
|
catalog_encode_u64(&mut data, value).unwrap();
|
|
|
|
|
|
|
|
let slice = &mut &data[..];
|
|
|
|
let decoded = catalog_decode_i64(slice).unwrap() as u64;
|
|
|
|
|
|
|
|
assert!(decoded == value);
|
|
|
|
}
|
|
|
|
|
|
|
|
test_encode_decode(u64::MIN);
|
2019-11-09 12:07:47 +00:00
|
|
|
test_encode_decode(126);
|
2022-04-14 11:27:53 +00:00
|
|
|
test_encode_decode((1 << 12) - 1);
|
|
|
|
test_encode_decode((1 << 20) - 1);
|
|
|
|
test_encode_decode((1 << 50) - 1);
|
2020-10-22 07:05:37 +00:00
|
|
|
test_encode_decode(u64::MAX);
|
2019-11-09 12:07:47 +00:00
|
|
|
}
|
2021-08-31 08:45:32 +00:00
|
|
|
|
|
|
|
/// An entry in a hierarchy of files for restore and listing.
|
|
|
|
#[api]
|
|
|
|
#[derive(Serialize, Deserialize)]
|
|
|
|
pub struct ArchiveEntry {
|
|
|
|
/// Base64-encoded full path to the file, including the filename
|
|
|
|
pub filepath: String,
|
|
|
|
/// Displayable filename text for UIs
|
|
|
|
pub text: String,
|
|
|
|
/// File or directory type of this entry
|
|
|
|
#[serde(rename = "type")]
|
|
|
|
pub entry_type: String,
|
|
|
|
/// Is this entry a leaf node, or does it have children (i.e. a directory)?
|
|
|
|
pub leaf: bool,
|
|
|
|
/// The file size, if entry_type is 'f' (file)
|
2022-04-14 11:27:53 +00:00
|
|
|
#[serde(skip_serializing_if = "Option::is_none")]
|
2021-08-31 08:45:32 +00:00
|
|
|
pub size: Option<u64>,
|
|
|
|
/// The file "last modified" time stamp, if entry_type is 'f' (file)
|
2022-04-14 11:27:53 +00:00
|
|
|
#[serde(skip_serializing_if = "Option::is_none")]
|
2021-08-31 08:45:32 +00:00
|
|
|
pub mtime: Option<i64>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ArchiveEntry {
|
|
|
|
pub fn new(filepath: &[u8], entry_type: Option<&DirEntryAttribute>) -> Self {
|
|
|
|
let size = match entry_type {
|
|
|
|
Some(DirEntryAttribute::File { size, .. }) => Some(*size),
|
|
|
|
_ => None,
|
|
|
|
};
|
|
|
|
Self::new_with_size(filepath, entry_type, size)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn new_with_size(
|
|
|
|
filepath: &[u8],
|
|
|
|
entry_type: Option<&DirEntryAttribute>,
|
|
|
|
size: Option<u64>,
|
|
|
|
) -> Self {
|
|
|
|
Self {
|
|
|
|
filepath: base64::encode(filepath),
|
|
|
|
text: String::from_utf8_lossy(filepath.split(|x| *x == b'/').last().unwrap())
|
|
|
|
.to_string(),
|
|
|
|
entry_type: match entry_type {
|
|
|
|
Some(entry_type) => CatalogEntryType::from(entry_type).to_string(),
|
|
|
|
None => "v".to_owned(),
|
|
|
|
},
|
|
|
|
leaf: !matches!(entry_type, None | Some(DirEntryAttribute::Directory { .. })),
|
|
|
|
size,
|
|
|
|
mtime: match entry_type {
|
|
|
|
Some(DirEntryAttribute::File { mtime, .. }) => Some(*mtime),
|
|
|
|
_ => None,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|