2019-03-14 09:54:09 +00:00
|
|
|
//! *pxar* binary format definition
|
2018-12-30 12:47:27 +00:00
|
|
|
//!
|
|
|
|
//! Please note the all values are stored in little endian ordering.
|
|
|
|
//!
|
|
|
|
//! The Archive contains a list of items. Each item starts with a
|
2019-08-02 13:19:34 +00:00
|
|
|
//! `PxarHeader`, followed by the item data.
|
2019-05-15 13:27:33 +00:00
|
|
|
use std::cmp::Ordering;
|
2018-12-27 08:22:23 +00:00
|
|
|
|
2019-08-22 12:46:21 +00:00
|
|
|
use endian_trait::Endian;
|
|
|
|
use failure::{bail, Error};
|
2018-12-30 16:32:52 +00:00
|
|
|
use siphasher::sip::SipHasher24;
|
|
|
|
|
2019-10-25 16:44:51 +00:00
|
|
|
|
2019-08-02 13:19:34 +00:00
|
|
|
/// Header types identifying items stored in the archive
|
2019-08-22 12:46:21 +00:00
|
|
|
pub const PXAR_ENTRY: u64 = 0x1396fabcea5bbb51;
|
|
|
|
pub const PXAR_FILENAME: u64 = 0x6dbb6ebcb3161f0b;
|
|
|
|
pub const PXAR_SYMLINK: u64 = 0x664a6fb6830e0d6c;
|
|
|
|
pub const PXAR_DEVICE: u64 = 0xac3dace369dfe643;
|
|
|
|
pub const PXAR_XATTR: u64 = 0xb8157091f80bc486;
|
|
|
|
pub const PXAR_ACL_USER: u64 = 0x297dc88b2ef12faf;
|
|
|
|
pub const PXAR_ACL_GROUP: u64 = 0x36f2acb56cb3dd0b;
|
|
|
|
pub const PXAR_ACL_GROUP_OBJ: u64 = 0x23047110441f38f3;
|
|
|
|
pub const PXAR_ACL_DEFAULT: u64 = 0xfe3eeda6823c8cd0;
|
|
|
|
pub const PXAR_ACL_DEFAULT_USER: u64 = 0xbdf03df9bd010a91;
|
|
|
|
pub const PXAR_ACL_DEFAULT_GROUP: u64 = 0xa0cb1168782d1f51;
|
|
|
|
pub const PXAR_FCAPS: u64 = 0xf7267db0afed0629;
|
|
|
|
pub const PXAR_QUOTA_PROJID: u64 = 0x161baf2d8772a72b;
|
2019-08-02 13:19:34 +00:00
|
|
|
|
|
|
|
/// Marks item as hardlink
|
|
|
|
/// compute_goodbye_hash(b"__PROXMOX_FORMAT_HARDLINK__");
|
2019-08-22 12:46:21 +00:00
|
|
|
pub const PXAR_FORMAT_HARDLINK: u64 = 0x2c5e06f634f65b86;
|
2019-08-02 13:19:34 +00:00
|
|
|
/// Marks the beginnig of the payload (actual content) of regular files
|
2019-08-22 12:46:21 +00:00
|
|
|
pub const PXAR_PAYLOAD: u64 = 0x8b9e1d93d6dcffc9;
|
2019-08-02 13:19:34 +00:00
|
|
|
/// Marks item as entry of goodbye table
|
2019-08-22 12:46:21 +00:00
|
|
|
pub const PXAR_GOODBYE: u64 = 0xdfd35c5e8327c403;
|
2019-08-02 13:19:34 +00:00
|
|
|
/// The end marker used in the GOODBYE object
|
|
|
|
pub const PXAR_GOODBYE_TAIL_MARKER: u64 = 0x57446fa533702943;
|
2019-01-11 08:20:10 +00:00
|
|
|
|
2019-08-22 12:46:21 +00:00
|
|
|
#[derive(Debug, Endian)]
|
2018-12-27 08:22:23 +00:00
|
|
|
#[repr(C)]
|
2019-08-02 13:19:34 +00:00
|
|
|
pub struct PxarHeader {
|
|
|
|
/// The item type (see `PXAR_` constants).
|
2018-12-28 08:55:26 +00:00
|
|
|
pub htype: u64,
|
2019-09-03 12:14:31 +00:00
|
|
|
/// The size of the item, including the size of `PxarHeader`.
|
|
|
|
pub size: u64,
|
2018-12-27 08:22:23 +00:00
|
|
|
}
|
|
|
|
|
2019-01-11 09:01:51 +00:00
|
|
|
#[derive(Endian)]
|
2018-12-27 08:22:23 +00:00
|
|
|
#[repr(C)]
|
2019-08-02 13:19:34 +00:00
|
|
|
pub struct PxarEntry {
|
2018-12-28 08:55:26 +00:00
|
|
|
pub mode: u64,
|
|
|
|
pub flags: u64,
|
2019-08-02 13:19:35 +00:00
|
|
|
pub uid: u32,
|
|
|
|
pub gid: u32,
|
2018-12-28 08:55:26 +00:00
|
|
|
pub mtime: u64,
|
2018-12-27 08:22:23 +00:00
|
|
|
}
|
|
|
|
|
2019-01-11 11:22:00 +00:00
|
|
|
#[derive(Endian)]
|
|
|
|
#[repr(C)]
|
2019-08-02 13:19:34 +00:00
|
|
|
pub struct PxarDevice {
|
2019-01-11 11:22:00 +00:00
|
|
|
pub major: u64,
|
|
|
|
pub minor: u64,
|
|
|
|
}
|
|
|
|
|
2019-01-07 18:07:03 +00:00
|
|
|
#[derive(Endian)]
|
2018-12-27 08:22:23 +00:00
|
|
|
#[repr(C)]
|
2019-08-02 13:19:34 +00:00
|
|
|
pub struct PxarGoodbyeItem {
|
2019-09-03 12:14:31 +00:00
|
|
|
/// SipHash24 of the directory item name. The last GOODBYE item
|
|
|
|
/// uses the special hash value `PXAR_GOODBYE_TAIL_MARKER`.
|
|
|
|
pub hash: u64,
|
2018-12-30 12:47:27 +00:00
|
|
|
/// The offset from the start of the GOODBYE object to the start
|
|
|
|
/// of the matching directory item (point to a FILENAME). The last
|
|
|
|
/// GOODBYE item points to the start of the matching ENTRY
|
2019-01-06 16:27:22 +00:00
|
|
|
/// object.
|
2018-12-28 08:55:26 +00:00
|
|
|
pub offset: u64,
|
2018-12-30 12:47:27 +00:00
|
|
|
/// The overall size of the directory item. The last GOODBYE item
|
|
|
|
/// repeats the size of the GOODBYE item.
|
2018-12-28 08:55:26 +00:00
|
|
|
pub size: u64,
|
2018-12-27 08:22:23 +00:00
|
|
|
}
|
|
|
|
|
2018-12-30 16:32:52 +00:00
|
|
|
/// Helper function to extract file names from binary archive.
|
|
|
|
pub fn read_os_string(buffer: &[u8]) -> std::ffi::OsString {
|
2018-12-27 08:22:23 +00:00
|
|
|
let len = buffer.len();
|
|
|
|
|
|
|
|
use std::os::unix::ffi::OsStrExt;
|
|
|
|
|
2019-08-22 12:46:21 +00:00
|
|
|
let name = if len > 0 && buffer[len - 1] == 0 {
|
|
|
|
std::ffi::OsStr::from_bytes(&buffer[0..len - 1])
|
2018-12-27 08:22:23 +00:00
|
|
|
} else {
|
|
|
|
std::ffi::OsStr::from_bytes(&buffer)
|
|
|
|
};
|
|
|
|
|
|
|
|
name.into()
|
|
|
|
}
|
2018-12-30 16:32:52 +00:00
|
|
|
|
2019-05-15 13:27:33 +00:00
|
|
|
#[derive(Debug, Eq)]
|
|
|
|
#[repr(C)]
|
2019-08-02 13:19:34 +00:00
|
|
|
pub struct PxarXAttr {
|
2019-05-15 13:27:33 +00:00
|
|
|
pub name: Vec<u8>,
|
|
|
|
pub value: Vec<u8>,
|
|
|
|
}
|
|
|
|
|
2019-08-02 13:19:34 +00:00
|
|
|
impl Ord for PxarXAttr {
|
|
|
|
fn cmp(&self, other: &PxarXAttr) -> Ordering {
|
2019-05-15 13:27:33 +00:00
|
|
|
self.name.cmp(&other.name)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-02 13:19:34 +00:00
|
|
|
impl PartialOrd for PxarXAttr {
|
|
|
|
fn partial_cmp(&self, other: &PxarXAttr) -> Option<Ordering> {
|
2019-05-15 13:27:33 +00:00
|
|
|
Some(self.cmp(other))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-02 13:19:34 +00:00
|
|
|
impl PartialEq for PxarXAttr {
|
|
|
|
fn eq(&self, other: &PxarXAttr) -> bool {
|
2019-05-15 13:27:33 +00:00
|
|
|
self.name == other.name
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
#[repr(C)]
|
2019-08-02 13:19:34 +00:00
|
|
|
pub struct PxarFCaps {
|
2019-05-15 13:27:33 +00:00
|
|
|
pub data: Vec<u8>,
|
|
|
|
}
|
|
|
|
|
2019-05-23 16:08:47 +00:00
|
|
|
#[derive(Debug, Endian, Eq)]
|
|
|
|
#[repr(C)]
|
2019-08-02 13:19:34 +00:00
|
|
|
pub struct PxarACLUser {
|
2019-05-23 16:08:47 +00:00
|
|
|
pub uid: u64,
|
|
|
|
pub permissions: u64,
|
|
|
|
//pub name: Vec<u64>, not impl for now
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO if also name is impl, sort by uid, then by name and last by permissions
|
2019-08-02 13:19:34 +00:00
|
|
|
impl Ord for PxarACLUser {
|
|
|
|
fn cmp(&self, other: &PxarACLUser) -> Ordering {
|
2019-05-23 16:08:47 +00:00
|
|
|
match self.uid.cmp(&other.uid) {
|
|
|
|
// uids are equal, entries ordered by permissions
|
|
|
|
Ordering::Equal => self.permissions.cmp(&other.permissions),
|
|
|
|
// uids are different, entries ordered by uid
|
|
|
|
uid_order => uid_order,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-02 13:19:34 +00:00
|
|
|
impl PartialOrd for PxarACLUser {
|
|
|
|
fn partial_cmp(&self, other: &PxarACLUser) -> Option<Ordering> {
|
2019-05-23 16:08:47 +00:00
|
|
|
Some(self.cmp(other))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-02 13:19:34 +00:00
|
|
|
impl PartialEq for PxarACLUser {
|
|
|
|
fn eq(&self, other: &PxarACLUser) -> bool {
|
2019-05-23 16:08:47 +00:00
|
|
|
self.uid == other.uid && self.permissions == other.permissions
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Endian, Eq)]
|
|
|
|
#[repr(C)]
|
2019-08-02 13:19:34 +00:00
|
|
|
pub struct PxarACLGroup {
|
2019-05-23 16:08:47 +00:00
|
|
|
pub gid: u64,
|
|
|
|
pub permissions: u64,
|
|
|
|
//pub name: Vec<u64>, not impl for now
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO if also name is impl, sort by gid, then by name and last by permissions
|
2019-08-02 13:19:34 +00:00
|
|
|
impl Ord for PxarACLGroup {
|
|
|
|
fn cmp(&self, other: &PxarACLGroup) -> Ordering {
|
2019-05-23 16:08:47 +00:00
|
|
|
match self.gid.cmp(&other.gid) {
|
|
|
|
// gids are equal, entries are ordered by permissions
|
|
|
|
Ordering::Equal => self.permissions.cmp(&other.permissions),
|
|
|
|
// gids are different, entries ordered by gid
|
|
|
|
gid_ordering => gid_ordering,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-02 13:19:34 +00:00
|
|
|
impl PartialOrd for PxarACLGroup {
|
|
|
|
fn partial_cmp(&self, other: &PxarACLGroup) -> Option<Ordering> {
|
2019-05-23 16:08:47 +00:00
|
|
|
Some(self.cmp(other))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-02 13:19:34 +00:00
|
|
|
impl PartialEq for PxarACLGroup {
|
|
|
|
fn eq(&self, other: &PxarACLGroup) -> bool {
|
2019-05-23 16:08:47 +00:00
|
|
|
self.gid == other.gid && self.permissions == other.permissions
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Endian)]
|
|
|
|
#[repr(C)]
|
2019-08-02 13:19:34 +00:00
|
|
|
pub struct PxarACLGroupObj {
|
2019-05-23 16:08:47 +00:00
|
|
|
pub permissions: u64,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Endian)]
|
|
|
|
#[repr(C)]
|
2019-08-02 13:19:34 +00:00
|
|
|
pub struct PxarACLDefault {
|
2019-05-23 16:08:47 +00:00
|
|
|
pub user_obj_permissions: u64,
|
|
|
|
pub group_obj_permissions: u64,
|
|
|
|
pub other_permissions: u64,
|
|
|
|
pub mask_permissions: u64,
|
|
|
|
}
|
|
|
|
|
2019-08-22 12:46:21 +00:00
|
|
|
pub(crate) struct PxarACL {
|
2019-08-02 13:19:34 +00:00
|
|
|
pub users: Vec<PxarACLUser>,
|
|
|
|
pub groups: Vec<PxarACLGroup>,
|
|
|
|
pub group_obj: Option<PxarACLGroupObj>,
|
|
|
|
pub default: Option<PxarACLDefault>,
|
2019-05-23 16:08:47 +00:00
|
|
|
}
|
|
|
|
|
2019-08-02 13:19:34 +00:00
|
|
|
pub const PXAR_ACL_PERMISSION_READ: u64 = 4;
|
|
|
|
pub const PXAR_ACL_PERMISSION_WRITE: u64 = 2;
|
|
|
|
pub const PXAR_ACL_PERMISSION_EXECUTE: u64 = 1;
|
2019-05-23 16:08:47 +00:00
|
|
|
|
2019-05-29 12:34:04 +00:00
|
|
|
#[derive(Debug, Endian)]
|
|
|
|
#[repr(C)]
|
2019-08-02 13:19:34 +00:00
|
|
|
pub struct PxarQuotaProjID {
|
2019-05-29 12:34:04 +00:00
|
|
|
pub projid: u64,
|
|
|
|
}
|
|
|
|
|
2019-08-02 05:16:10 +00:00
|
|
|
#[derive(Debug, Default)]
|
2019-08-01 11:08:07 +00:00
|
|
|
pub struct PxarAttributes {
|
2019-08-02 13:19:34 +00:00
|
|
|
pub xattrs: Vec<PxarXAttr>,
|
|
|
|
pub fcaps: Option<PxarFCaps>,
|
|
|
|
pub quota_projid: Option<PxarQuotaProjID>,
|
|
|
|
pub acl_user: Vec<PxarACLUser>,
|
|
|
|
pub acl_group: Vec<PxarACLGroup>,
|
|
|
|
pub acl_group_obj: Option<PxarACLGroupObj>,
|
|
|
|
pub acl_default: Option<PxarACLDefault>,
|
|
|
|
pub acl_default_user: Vec<PxarACLUser>,
|
|
|
|
pub acl_default_group: Vec<PxarACLGroup>,
|
2019-08-01 11:08:07 +00:00
|
|
|
}
|
|
|
|
|
2018-12-30 16:32:52 +00:00
|
|
|
/// Create SipHash values for goodby tables.
|
|
|
|
//pub fn compute_goodbye_hash(name: &std::ffi::CStr) -> u64 {
|
|
|
|
pub fn compute_goodbye_hash(name: &[u8]) -> u64 {
|
|
|
|
use std::hash::Hasher;
|
|
|
|
let mut hasher = SipHasher24::new_with_keys(0x8574442b0f1d84b3, 0x2736ed30d1c22ec1);
|
|
|
|
hasher.write(name);
|
|
|
|
hasher.finish()
|
|
|
|
}
|
2019-01-07 18:07:03 +00:00
|
|
|
|
2019-08-02 13:19:34 +00:00
|
|
|
pub fn check_ca_header<T>(head: &PxarHeader, htype: u64) -> Result<(), Error> {
|
2019-01-07 18:07:03 +00:00
|
|
|
if head.htype != htype {
|
2019-08-22 12:46:21 +00:00
|
|
|
bail!(
|
|
|
|
"got wrong header type ({:016x} != {:016x})",
|
|
|
|
head.htype,
|
|
|
|
htype
|
|
|
|
);
|
2019-01-07 18:07:03 +00:00
|
|
|
}
|
2019-08-02 13:19:34 +00:00
|
|
|
if head.size != (std::mem::size_of::<T>() + std::mem::size_of::<PxarHeader>()) as u64 {
|
2019-01-07 18:07:03 +00:00
|
|
|
bail!("got wrong header size for type {:016x}", htype);
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|