src/pxar/sequential_decoder.rs: style fixups

Signed-off-by: Christian Ebner <c.ebner@proxmox.com>
Signed-off-by: Wolfgang Bumiller <w.bumiller@proxmox.com>
This commit is contained in:
Christian Ebner 2019-08-22 15:36:25 +02:00 committed by Wolfgang Bumiller
parent dac88033ba
commit 9cb9e1b7db
1 changed files with 254 additions and 169 deletions

View File

@ -1,35 +1,32 @@
//! *pxar* format decoder. //! *pxar* format decoder.
//! //!
//! This module contain the code to decode *pxar* archive files. //! This module contain the code to decode *pxar* archive files.
use failure::*;
use endian_trait::Endian;
use super::flags;
use super::format_definition::*;
use super::match_pattern::*;
use super::dir_stack::*;
use std::io::{Read, Write};
use std::path::{Path, PathBuf};
use std::os::unix::io::AsRawFd;
use std::os::unix::io::RawFd;
use std::os::unix::io::FromRawFd;
use std::os::unix::ffi::{OsStrExt, OsStringExt};
use std::ffi::CString; use std::ffi::CString;
use std::ffi::{OsStr, OsString}; use std::ffi::{OsStr, OsString};
use std::io::{Read, Write};
use std::os::unix::ffi::{OsStrExt, OsStringExt};
use std::os::unix::io::AsRawFd;
use std::os::unix::io::FromRawFd;
use std::os::unix::io::RawFd;
use std::path::{Path, PathBuf};
use endian_trait::Endian;
use failure::{bail, format_err, Error};
use nix::errno::Errno;
use nix::fcntl::OFlag; use nix::fcntl::OFlag;
use nix::sys::stat::Mode; use nix::sys::stat::Mode;
use nix::errno::Errno;
use nix::NixPath; use nix::NixPath;
use proxmox::tools::io::ReadExt; use proxmox::tools::io::ReadExt;
use proxmox::tools::vec; use proxmox::tools::vec;
use crate::tools::fs; use super::dir_stack::{PxarDir, PxarDirStack};
use super::flags;
use super::format_definition::*;
use super::match_pattern::{MatchPattern, MatchType};
use crate::tools::acl; use crate::tools::acl;
use crate::tools::fs;
use crate::tools::xattr; use crate::tools::xattr;
// This one need Read, but works without Seek // This one need Read, but works without Seek
@ -44,7 +41,6 @@ pub struct SequentialDecoder<R: Read, F: Fn(&Path) -> Result<(), Error>> {
const HEADER_SIZE: u64 = std::mem::size_of::<PxarHeader>() as u64; const HEADER_SIZE: u64 = std::mem::size_of::<PxarHeader>() as u64;
impl<R: Read, F: Fn(&Path) -> Result<(), Error>> SequentialDecoder<R, F> { impl<R: Read, F: Fn(&Path) -> Result<(), Error>> SequentialDecoder<R, F> {
pub fn new(reader: R, feature_flags: u64, callback: F) -> Self { pub fn new(reader: R, feature_flags: u64, callback: F) -> Self {
let skip_buffer = vec::undefined(64 * 1024); let skip_buffer = vec::undefined(64 * 1024);
@ -66,13 +62,11 @@ impl <R: Read, F: Fn(&Path) -> Result<(), Error>> SequentialDecoder<R, F> {
} }
pub(crate) fn read_item<T: Endian>(&mut self) -> Result<T, Error> { pub(crate) fn read_item<T: Endian>(&mut self) -> Result<T, Error> {
let mut result = std::mem::MaybeUninit::<T>::uninit(); let mut result = std::mem::MaybeUninit::<T>::uninit();
let buffer = unsafe { std::slice::from_raw_parts_mut( let buffer = unsafe {
result.as_mut_ptr() as *mut u8, std::slice::from_raw_parts_mut(result.as_mut_ptr() as *mut u8, std::mem::size_of::<T>())
std::mem::size_of::<T>() };
)};
self.reader.read_exact(buffer)?; self.reader.read_exact(buffer)?;
let result = unsafe { result.assume_init() }; let result = unsafe { result.assume_init() };
@ -109,10 +103,8 @@ impl <R: Read, F: Fn(&Path) -> Result<(), Error>> SequentialDecoder<R, F> {
for c in target.components() { for c in target.components() {
match c { match c {
std::path::Component::Normal(_) => { /* OK */ }, std::path::Component::Normal(_) => { /* OK */ }
_ => { _ => bail!("hardlink target contains invalid component {:?}", c),
bail!("hardlink target contains invalid component {:?}", c);
}
} }
} }
@ -140,7 +132,11 @@ impl <R: Read, F: Fn(&Path) -> Result<(), Error>> SequentialDecoder<R, F> {
bail!("found invalid filename '.' or '..'."); bail!("found invalid filename '.' or '..'.");
} }
if buffer.iter().find(|b| (**b == b'/' || **b == b'\0')).is_some() { if buffer
.iter()
.find(|b| (**b == b'/' || **b == b'\0'))
.is_some()
{
bail!("found invalid filename with slashes or nul bytes."); bail!("found invalid filename with slashes or nul bytes.");
} }
@ -159,13 +155,13 @@ impl <R: Read, F: Fn(&Path) -> Result<(), Error>> SequentialDecoder<R, F> {
fn read_xattr(&mut self, size: usize) -> Result<PxarXAttr, Error> { fn read_xattr(&mut self, size: usize) -> Result<PxarXAttr, Error> {
let buffer = self.reader.read_exact_allocated(size)?; let buffer = self.reader.read_exact_allocated(size)?;
let separator = buffer.iter().position(|c| *c == b'\0') let separator = buffer
.iter()
.position(|c| *c == b'\0')
.ok_or_else(|| format_err!("no value found in xattr"))?; .ok_or_else(|| format_err!("no value found in xattr"))?;
let (name, value) = buffer.split_at(separator); let (name, value) = buffer.split_at(separator);
if !xattr::is_valid_xattr_name(name) || if !xattr::is_valid_xattr_name(name) || xattr::is_security_capability(name) {
xattr::is_security_capability(name)
{
bail!("incorrect xattr name - {}.", String::from_utf8_lossy(name)); bail!("incorrect xattr name - {}.", String::from_utf8_lossy(name));
} }
@ -193,63 +189,64 @@ impl <R: Read, F: Fn(&Path) -> Result<(), Error>> SequentialDecoder<R, F> {
} else { } else {
self.skip_bytes(size)?; self.skip_bytes(size)?;
} }
}, }
PXAR_FCAPS => { PXAR_FCAPS => {
if self.has_features(flags::WITH_FCAPS) { if self.has_features(flags::WITH_FCAPS) {
attr.fcaps = Some(self.read_fcaps(size)?); attr.fcaps = Some(self.read_fcaps(size)?);
} else { } else {
self.skip_bytes(size)?; self.skip_bytes(size)?;
} }
}, }
PXAR_ACL_USER => { PXAR_ACL_USER => {
if self.has_features(flags::WITH_ACL) { if self.has_features(flags::WITH_ACL) {
attr.acl_user.push(self.read_item::<PxarACLUser>()?); attr.acl_user.push(self.read_item::<PxarACLUser>()?);
} else { } else {
self.skip_bytes(size)?; self.skip_bytes(size)?;
} }
}, }
PXAR_ACL_GROUP => { PXAR_ACL_GROUP => {
if self.has_features(flags::WITH_ACL) { if self.has_features(flags::WITH_ACL) {
attr.acl_group.push(self.read_item::<PxarACLGroup>()?); attr.acl_group.push(self.read_item::<PxarACLGroup>()?);
} else { } else {
self.skip_bytes(size)?; self.skip_bytes(size)?;
} }
}, }
PXAR_ACL_GROUP_OBJ => { PXAR_ACL_GROUP_OBJ => {
if self.has_features(flags::WITH_ACL) { if self.has_features(flags::WITH_ACL) {
attr.acl_group_obj = Some(self.read_item::<PxarACLGroupObj>()?); attr.acl_group_obj = Some(self.read_item::<PxarACLGroupObj>()?);
} else { } else {
self.skip_bytes(size)?; self.skip_bytes(size)?;
} }
}, }
PXAR_ACL_DEFAULT => { PXAR_ACL_DEFAULT => {
if self.has_features(flags::WITH_ACL) { if self.has_features(flags::WITH_ACL) {
attr.acl_default = Some(self.read_item::<PxarACLDefault>()?); attr.acl_default = Some(self.read_item::<PxarACLDefault>()?);
} else { } else {
self.skip_bytes(size)?; self.skip_bytes(size)?;
} }
}, }
PXAR_ACL_DEFAULT_USER => { PXAR_ACL_DEFAULT_USER => {
if self.has_features(flags::WITH_ACL) { if self.has_features(flags::WITH_ACL) {
attr.acl_default_user.push(self.read_item::<PxarACLUser>()?); attr.acl_default_user.push(self.read_item::<PxarACLUser>()?);
} else { } else {
self.skip_bytes(size)?; self.skip_bytes(size)?;
} }
}, }
PXAR_ACL_DEFAULT_GROUP => { PXAR_ACL_DEFAULT_GROUP => {
if self.has_features(flags::WITH_ACL) { if self.has_features(flags::WITH_ACL) {
attr.acl_default_group.push(self.read_item::<PxarACLGroup>()?); attr.acl_default_group
.push(self.read_item::<PxarACLGroup>()?);
} else { } else {
self.skip_bytes(size)?; self.skip_bytes(size)?;
} }
}, }
PXAR_QUOTA_PROJID => { PXAR_QUOTA_PROJID => {
if self.has_features(flags::WITH_QUOTA_PROJID) { if self.has_features(flags::WITH_QUOTA_PROJID) {
attr.quota_projid = Some(self.read_item::<PxarQuotaProjID>()?); attr.quota_projid = Some(self.read_item::<PxarQuotaProjID>()?);
} else { } else {
self.skip_bytes(size)?; self.skip_bytes(size)?;
} }
}, }
_ => break, _ => break,
} }
head = self.read_item()?; head = self.read_item()?;
@ -268,16 +265,32 @@ impl <R: Read, F: Fn(&Path) -> Result<(), Error>> SequentialDecoder<R, F> {
self.restore_xattrs_fcaps_fd(fd, &attr.xattrs, &attr.fcaps)?; self.restore_xattrs_fcaps_fd(fd, &attr.xattrs, &attr.fcaps)?;
let mut acl = acl::ACL::init(5)?; let mut acl = acl::ACL::init(5)?;
acl.add_entry_full(acl::ACL_USER_OBJ, None, mode_user_to_acl_permissions(entry.mode))?; acl.add_entry_full(
acl.add_entry_full(acl::ACL_OTHER, None, mode_other_to_acl_permissions(entry.mode))?; acl::ACL_USER_OBJ,
None,
mode_user_to_acl_permissions(entry.mode),
)?;
acl.add_entry_full(
acl::ACL_OTHER,
None,
mode_other_to_acl_permissions(entry.mode),
)?;
match &attr.acl_group_obj { match &attr.acl_group_obj {
Some(group_obj) => { Some(group_obj) => {
acl.add_entry_full(acl::ACL_MASK, None, mode_group_to_acl_permissions(entry.mode))?; acl.add_entry_full(
acl::ACL_MASK,
None,
mode_group_to_acl_permissions(entry.mode),
)?;
acl.add_entry_full(acl::ACL_GROUP_OBJ, None, group_obj.permissions)?; acl.add_entry_full(acl::ACL_GROUP_OBJ, None, group_obj.permissions)?;
}, }
None => { None => {
acl.add_entry_full(acl::ACL_GROUP_OBJ, None, mode_group_to_acl_permissions(entry.mode))?; acl.add_entry_full(
}, acl::ACL_GROUP_OBJ,
None,
mode_group_to_acl_permissions(entry.mode),
)?;
}
} }
for user in &attr.acl_user { for user in &attr.acl_user {
acl.add_entry_full(acl::ACL_USER, Some(user.uid), user.permissions)?; acl.add_entry_full(acl::ACL_USER, Some(user.uid), user.permissions)?;
@ -320,7 +333,7 @@ impl <R: Read, F: Fn(&Path) -> Result<(), Error>> SequentialDecoder<R, F> {
&mut self, &mut self,
fd: RawFd, fd: RawFd,
xattrs: &Vec<PxarXAttr>, xattrs: &Vec<PxarXAttr>,
fcaps: &Option<PxarFCaps> fcaps: &Option<PxarFCaps>,
) -> Result<(), Error> { ) -> Result<(), Error> {
for xattr in xattrs { for xattr in xattrs {
if let Err(err) = xattr::fsetxattr(fd, &xattr) { if let Err(err) = xattr::fsetxattr(fd, &xattr) {
@ -339,18 +352,26 @@ impl <R: Read, F: Fn(&Path) -> Result<(), Error>> SequentialDecoder<R, F> {
fn restore_quota_projid( fn restore_quota_projid(
&mut self, &mut self,
fd: RawFd, fd: RawFd,
projid: &Option<PxarQuotaProjID> projid: &Option<PxarQuotaProjID>,
) -> Result<(), Error> { ) -> Result<(), Error> {
if let Some(projid) = projid { if let Some(projid) = projid {
let mut fsxattr = fs::FSXAttr::default(); let mut fsxattr = fs::FSXAttr::default();
unsafe { unsafe {
fs::fs_ioc_fsgetxattr(fd, &mut fsxattr) fs::fs_ioc_fsgetxattr(fd, &mut fsxattr).map_err(|err| {
.map_err(|err| format_err!("error while getting fsxattr to restore quota project id - {}", err))?; format_err!(
"error while getting fsxattr to restore quota project id - {}",
err
)
})?;
} }
fsxattr.fsx_projid = projid.projid as u32; fsxattr.fsx_projid = projid.projid as u32;
unsafe { unsafe {
fs::fs_ioc_fssetxattr(fd, &fsxattr) fs::fs_ioc_fssetxattr(fd, &fsxattr).map_err(|err| {
.map_err(|err| format_err!("error while setting fsxattr to restore quota project id - {}", err))?; format_err!(
"error while setting fsxattr to restore quota project id - {}",
err
)
})?;
} }
} }
@ -358,7 +379,6 @@ impl <R: Read, F: Fn(&Path) -> Result<(), Error>> SequentialDecoder<R, F> {
} }
fn restore_mode(&mut self, entry: &PxarEntry, fd: RawFd) -> Result<(), Error> { fn restore_mode(&mut self, entry: &PxarEntry, fd: RawFd) -> Result<(), Error> {
let mode = Mode::from_bits_truncate((entry.mode as u32) & 0o7777); let mode = Mode::from_bits_truncate((entry.mode as u32) & 0o7777);
nix::sys::stat::fchmod(fd, mode)?; nix::sys::stat::fchmod(fd, mode)?;
@ -366,19 +386,27 @@ impl <R: Read, F: Fn(&Path) -> Result<(), Error>> SequentialDecoder<R, F> {
Ok(()) Ok(())
} }
fn restore_mode_at(&mut self, entry: &PxarEntry, dirfd: RawFd, filename: &OsStr) -> Result<(), Error> { fn restore_mode_at(
&mut self,
entry: &PxarEntry,
dirfd: RawFd,
filename: &OsStr,
) -> Result<(), Error> {
let mode = Mode::from_bits_truncate((entry.mode as u32) & 0o7777); let mode = Mode::from_bits_truncate((entry.mode as u32) & 0o7777);
// NOTE: we want :FchmodatFlags::NoFollowSymlink, but fchmodat does not support that // NOTE: we want :FchmodatFlags::NoFollowSymlink, but fchmodat does not support that
// on linux (see man fchmodat). Fortunately, we can simply avoid calling this on symlinks. // on linux (see man fchmodat). Fortunately, we can simply avoid calling this on symlinks.
nix::sys::stat::fchmodat(Some(dirfd), filename, mode, nix::sys::stat::FchmodatFlags::FollowSymlink)?; nix::sys::stat::fchmodat(
Some(dirfd),
filename,
mode,
nix::sys::stat::FchmodatFlags::FollowSymlink,
)?;
Ok(()) Ok(())
} }
fn restore_ugid(&mut self, entry: &PxarEntry, fd: RawFd) -> Result<(), Error> { fn restore_ugid(&mut self, entry: &PxarEntry, fd: RawFd) -> Result<(), Error> {
let uid = entry.uid; let uid = entry.uid;
let gid = entry.gid; let gid = entry.gid;
@ -388,8 +416,12 @@ impl <R: Read, F: Fn(&Path) -> Result<(), Error>> SequentialDecoder<R, F> {
Ok(()) Ok(())
} }
fn restore_ugid_at(&mut self, entry: &PxarEntry, dirfd: RawFd, filename: &OsStr) -> Result<(), Error> { fn restore_ugid_at(
&mut self,
entry: &PxarEntry,
dirfd: RawFd,
filename: &OsStr,
) -> Result<(), Error> {
let uid = entry.uid; let uid = entry.uid;
let gid = entry.gid; let gid = entry.gid;
@ -402,7 +434,6 @@ impl <R: Read, F: Fn(&Path) -> Result<(), Error>> SequentialDecoder<R, F> {
} }
fn restore_mtime(&mut self, entry: &PxarEntry, fd: RawFd) -> Result<(), Error> { fn restore_mtime(&mut self, entry: &PxarEntry, fd: RawFd) -> Result<(), Error> {
let times = nsec_to_update_timespec(entry.mtime); let times = nsec_to_update_timespec(entry.mtime);
let res = unsafe { libc::futimens(fd, &times[0]) }; let res = unsafe { libc::futimens(fd, &times[0]) };
@ -411,8 +442,12 @@ impl <R: Read, F: Fn(&Path) -> Result<(), Error>> SequentialDecoder<R, F> {
Ok(()) Ok(())
} }
fn restore_mtime_at(&mut self, entry: &PxarEntry, dirfd: RawFd, filename: &OsStr) -> Result<(), Error> { fn restore_mtime_at(
&mut self,
entry: &PxarEntry,
dirfd: RawFd,
filename: &OsStr,
) -> Result<(), Error> {
let times = nsec_to_update_timespec(entry.mtime); let times = nsec_to_update_timespec(entry.mtime);
let res = filename.with_nix_path(|cstr| unsafe { let res = filename.with_nix_path(|cstr| unsafe {
@ -423,35 +458,35 @@ impl <R: Read, F: Fn(&Path) -> Result<(), Error>> SequentialDecoder<R, F> {
Ok(()) Ok(())
} }
fn restore_device_at(&mut self, entry: &PxarEntry, dirfd: RawFd, filename: &OsStr, device: &PxarDevice) -> Result<(), Error> { fn restore_device_at(
&mut self,
entry: &PxarEntry,
dirfd: RawFd,
filename: &OsStr,
device: &PxarDevice,
) -> Result<(), Error> {
let rdev = nix::sys::stat::makedev(device.major, device.minor); let rdev = nix::sys::stat::makedev(device.major, device.minor);
let mode = ((entry.mode as u32) & libc::S_IFMT) | 0o0600; let mode = ((entry.mode as u32) & libc::S_IFMT) | 0o0600;
let res = filename.with_nix_path(|cstr| unsafe { let res = filename
libc::mknodat(dirfd, cstr.as_ptr(), mode, rdev) .with_nix_path(|cstr| unsafe { libc::mknodat(dirfd, cstr.as_ptr(), mode, rdev) })?;
})?;
Errno::result(res)?; Errno::result(res)?;
Ok(()) Ok(())
} }
fn restore_socket_at(&mut self, dirfd: RawFd, filename: &OsStr) -> Result<(), Error> { fn restore_socket_at(&mut self, dirfd: RawFd, filename: &OsStr) -> Result<(), Error> {
let mode = libc::S_IFSOCK | 0o0600; let mode = libc::S_IFSOCK | 0o0600;
let res = filename.with_nix_path(|cstr| unsafe { let res = filename
libc::mknodat(dirfd, cstr.as_ptr(), mode, 0) .with_nix_path(|cstr| unsafe { libc::mknodat(dirfd, cstr.as_ptr(), mode, 0) })?;
})?;
Errno::result(res)?; Errno::result(res)?;
Ok(()) Ok(())
} }
fn restore_fifo_at(&mut self, dirfd: RawFd, filename: &OsStr) -> Result<(), Error> { fn restore_fifo_at(&mut self, dirfd: RawFd, filename: &OsStr) -> Result<(), Error> {
let mode = libc::S_IFIFO | 0o0600; let mode = libc::S_IFIFO | 0o0600;
let res = filename.with_nix_path(|cstr| unsafe { let res =
libc::mkfifoat(dirfd, cstr.as_ptr(), mode) filename.with_nix_path(|cstr| unsafe { libc::mkfifoat(dirfd, cstr.as_ptr(), mode) })?;
})?;
Errno::result(res)?; Errno::result(res)?;
Ok(()) Ok(())
@ -461,7 +496,11 @@ impl <R: Read, F: Fn(&Path) -> Result<(), Error>> SequentialDecoder<R, F> {
let mut done = 0; let mut done = 0;
while done < count { while done < count {
let todo = count - done; let todo = count - done;
let n = if todo > self.skip_buffer.len() { self.skip_buffer.len() } else { todo }; let n = if todo > self.skip_buffer.len() {
self.skip_buffer.len()
} else {
todo
};
let data = &mut self.skip_buffer[..n]; let data = &mut self.skip_buffer[..n];
self.reader.read_exact(data)?; self.reader.read_exact(data)?;
done += n; done += n;
@ -474,7 +513,7 @@ impl <R: Read, F: Fn(&Path) -> Result<(), Error>> SequentialDecoder<R, F> {
parent_fd: Option<RawFd>, parent_fd: Option<RawFd>,
full_path: &PathBuf, full_path: &PathBuf,
entry: &PxarEntry, entry: &PxarEntry,
filename: &OsStr filename: &OsStr,
) -> Result<(), Error> { ) -> Result<(), Error> {
//fixme: create symlink //fixme: create symlink
//fixme: restore permission, acls, xattr, ... //fixme: restore permission, acls, xattr, ...
@ -490,9 +529,10 @@ impl <R: Read, F: Fn(&Path) -> Result<(), Error>> SequentialDecoder<R, F> {
} }
} }
} }
_ => { _ => bail!(
bail!("got unknown header type inside symlink entry {:016x}", head.htype); "got unknown header type inside symlink entry {:016x}",
} head.htype
),
} }
if let Some(fd) = parent_fd { if let Some(fd) = parent_fd {
@ -508,7 +548,7 @@ impl <R: Read, F: Fn(&Path) -> Result<(), Error>> SequentialDecoder<R, F> {
&mut self, &mut self,
parent_fd: Option<RawFd>, parent_fd: Option<RawFd>,
entry: &PxarEntry, entry: &PxarEntry,
filename: &OsStr filename: &OsStr,
) -> Result<(), Error> { ) -> Result<(), Error> {
if !self.has_features(flags::WITH_SOCKETS) { if !self.has_features(flags::WITH_SOCKETS) {
return Ok(()); return Ok(());
@ -527,7 +567,7 @@ impl <R: Read, F: Fn(&Path) -> Result<(), Error>> SequentialDecoder<R, F> {
&mut self, &mut self,
parent_fd: Option<RawFd>, parent_fd: Option<RawFd>,
entry: &PxarEntry, entry: &PxarEntry,
filename: &OsStr filename: &OsStr,
) -> Result<(), Error> { ) -> Result<(), Error> {
if !self.has_features(flags::WITH_FIFOS) { if !self.has_features(flags::WITH_FIFOS) {
return Ok(()); return Ok(());
@ -546,11 +586,14 @@ impl <R: Read, F: Fn(&Path) -> Result<(), Error>> SequentialDecoder<R, F> {
&mut self, &mut self,
parent_fd: Option<RawFd>, parent_fd: Option<RawFd>,
entry: &PxarEntry, entry: &PxarEntry,
filename: &OsStr filename: &OsStr,
) -> Result<(), Error> { ) -> Result<(), Error> {
let head: PxarHeader = self.read_item()?; let head: PxarHeader = self.read_item()?;
if head.htype != PXAR_DEVICE { if head.htype != PXAR_DEVICE {
bail!("got unknown header type inside device entry {:016x}", head.htype); bail!(
"got unknown header type inside device entry {:016x}",
head.htype
);
} }
let device: PxarDevice = self.read_item()?; let device: PxarDevice = self.read_item()?;
if !self.has_features(flags::WITH_DEVICE_NODES) { if !self.has_features(flags::WITH_DEVICE_NODES) {
@ -575,9 +618,10 @@ impl <R: Read, F: Fn(&Path) -> Result<(), Error>> SequentialDecoder<R, F> {
parent_fd: Option<RawFd>, parent_fd: Option<RawFd>,
full_path: &PathBuf, full_path: &PathBuf,
entry: &PxarEntry, entry: &PxarEntry,
filename: &OsStr filename: &OsStr,
) -> Result<(), Error> { ) -> Result<(), Error> {
let (head, attr) = self.read_attributes() let (head, attr) = self
.read_attributes()
.map_err(|err| format_err!("Reading of file attributes failed - {}", err))?; .map_err(|err| format_err!("Reading of file attributes failed - {}", err))?;
if let Some(fd) = parent_fd { if let Some(fd) = parent_fd {
@ -599,7 +643,11 @@ impl <R: Read, F: Fn(&Path) -> Result<(), Error>> SequentialDecoder<R, F> {
let mut done = 0; let mut done = 0;
while done < need { while done < need {
let todo = need - done; let todo = need - done;
let n = if todo > read_buffer.len() { read_buffer.len() } else { todo }; let n = if todo > read_buffer.len() {
read_buffer.len()
} else {
todo
};
let data = &mut read_buffer[..n]; let data = &mut read_buffer[..n];
self.reader.read_exact(data)?; self.reader.read_exact(data)?;
file.write_all(data)?; file.write_all(data)?;
@ -633,7 +681,8 @@ impl <R: Read, F: Fn(&Path) -> Result<(), Error>> SequentialDecoder<R, F> {
matched: MatchType, matched: MatchType,
match_pattern: &Vec<MatchPattern>, match_pattern: &Vec<MatchPattern>,
) -> Result<(), Error> { ) -> Result<(), Error> {
let (mut head, attr) = self.read_attributes() let (mut head, attr) = self
.read_attributes()
.map_err(|err| format_err!("Reading of directory attributes failed - {}", err))?; .map_err(|err| format_err!("Reading of directory attributes failed - {}", err))?;
let dir = PxarDir::new(filename, entry, attr); let dir = PxarDir::new(filename, entry, attr);
@ -649,13 +698,19 @@ impl <R: Read, F: Fn(&Path) -> Result<(), Error>> SequentialDecoder<R, F> {
} }
if head.htype != PXAR_GOODBYE { if head.htype != PXAR_GOODBYE {
bail!("got unknown header type inside directory entry {:016x}", head.htype); bail!(
"got unknown header type inside directory entry {:016x}",
head.htype
);
} }
if head.size < HEADER_SIZE { bail!("detected short goodbye table"); } if head.size < HEADER_SIZE {
bail!("detected short goodbye table");
}
self.skip_bytes((head.size - HEADER_SIZE) as usize)?; self.skip_bytes((head.size - HEADER_SIZE) as usize)?;
let last = dirs.pop() let last = dirs
.pop()
.ok_or_else(|| format_err!("Tried to pop beyond dir root - this should not happen!"))?; .ok_or_else(|| format_err!("Tried to pop beyond dir root - this should not happen!"))?;
if let Some(d) = last.dir { if let Some(d) = last.dir {
let fd = d.as_raw_fd(); let fd = d.as_raw_fd();
@ -672,15 +727,14 @@ impl <R: Read, F: Fn(&Path) -> Result<(), Error>> SequentialDecoder<R, F> {
/// Restore an archive into the specified directory. /// Restore an archive into the specified directory.
/// ///
/// The directory is created if it does not exist. /// The directory is created if it does not exist.
pub fn restore( pub fn restore(&mut self, path: &Path, match_pattern: &Vec<MatchPattern>) -> Result<(), Error> {
&mut self,
path: &Path,
match_pattern: &Vec<MatchPattern>
) -> Result<(), Error> {
let _ = std::fs::create_dir(path); let _ = std::fs::create_dir(path);
let dir = nix::dir::Dir::open(path, nix::fcntl::OFlag::O_DIRECTORY, nix::sys::stat::Mode::empty()) let dir = nix::dir::Dir::open(
path,
nix::fcntl::OFlag::O_DIRECTORY,
nix::sys::stat::Mode::empty(),
)
.map_err(|err| format_err!("unable to open target directory {:?} - {}", path, err))?; .map_err(|err| format_err!("unable to open target directory {:?} - {}", path, err))?;
let fd = dir.as_raw_fd(); let fd = dir.as_raw_fd();
let mut dirs = PxarDirStack::new(fd); let mut dirs = PxarDirStack::new(fd);
@ -695,7 +749,8 @@ impl <R: Read, F: Fn(&Path) -> Result<(), Error>> SequentialDecoder<R, F> {
check_ca_header::<PxarEntry>(&header, PXAR_ENTRY)?; check_ca_header::<PxarEntry>(&header, PXAR_ENTRY)?;
let entry: PxarEntry = self.read_item()?; let entry: PxarEntry = self.read_item()?;
let (mut head, attr) = self.read_attributes() let (mut head, attr) = self
.read_attributes()
.map_err(|err| format_err!("Reading of directory attributes failed - {}", err))?; .map_err(|err| format_err!("Reading of directory attributes failed - {}", err))?;
while head.htype == PXAR_FILENAME { while head.htype == PXAR_FILENAME {
@ -705,10 +760,15 @@ impl <R: Read, F: Fn(&Path) -> Result<(), Error>> SequentialDecoder<R, F> {
} }
if head.htype != PXAR_GOODBYE { if head.htype != PXAR_GOODBYE {
bail!("got unknown header type inside directory entry {:016x}", head.htype); bail!(
"got unknown header type inside directory entry {:016x}",
head.htype
);
} }
if head.size < HEADER_SIZE { bail!("detected short goodbye table"); } if head.size < HEADER_SIZE {
bail!("detected short goodbye table");
}
self.skip_bytes((head.size - HEADER_SIZE) as usize)?; self.skip_bytes((head.size - HEADER_SIZE) as usize)?;
self.restore_ugid(&entry, fd)?; self.restore_ugid(&entry, fd)?;
@ -751,13 +811,17 @@ impl <R: Read, F: Fn(&Path) -> Result<(), Error>> SequentialDecoder<R, F> {
// there are no match pattern. // there are no match pattern.
let mut matched = parent_matched; let mut matched = parent_matched;
if match_pattern.len() > 0 { if match_pattern.len() > 0 {
match match_filename(filename, entry.mode as u32 & libc::S_IFMT == libc::S_IFDIR, match_pattern)? { match match_filename(
filename,
entry.mode as u32 & libc::S_IFMT == libc::S_IFDIR,
match_pattern,
)? {
(MatchType::None, _) => matched = MatchType::None, (MatchType::None, _) => matched = MatchType::None,
(MatchType::Negative, _) => matched = MatchType::Negative, (MatchType::Negative, _) => matched = MatchType::Negative,
(match_type, pattern) => { (match_type, pattern) => {
matched = match_type; matched = match_type;
child_pattern = pattern; child_pattern = pattern;
}, }
} }
} }
@ -772,7 +836,9 @@ impl <R: Read, F: Fn(&Path) -> Result<(), Error>> SequentialDecoder<R, F> {
} }
match entry.mode as u32 & libc::S_IFMT { match entry.mode as u32 & libc::S_IFMT {
libc::S_IFDIR => self.restore_dir(base_path, dirs, entry, &filename, matched, &child_pattern), libc::S_IFDIR => {
self.restore_dir(base_path, dirs, entry, &filename, matched, &child_pattern)
}
libc::S_IFLNK => self.restore_symlink(fd, &full_path, &entry, &filename), libc::S_IFLNK => self.restore_symlink(fd, &full_path, &entry, &filename),
libc::S_IFSOCK => self.restore_socket(fd, &entry, &filename), libc::S_IFSOCK => self.restore_socket(fd, &entry, &filename),
libc::S_IFIFO => self.restore_fifo(fd, &entry, &filename), libc::S_IFIFO => self.restore_fifo(fd, &entry, &filename),
@ -792,7 +858,6 @@ impl <R: Read, F: Fn(&Path) -> Result<(), Error>> SequentialDecoder<R, F> {
verbose: bool, verbose: bool,
output: &mut W, output: &mut W,
) -> Result<(), Error> { ) -> Result<(), Error> {
let print_head = |head: &PxarHeader| { let print_head = |head: &PxarHeader| {
println!("Type: {:016x}", head.htype); println!("Type: {:016x}", head.htype);
println!("Size: {}", head.size); println!("Size: {}", head.size);
@ -818,13 +883,16 @@ impl <R: Read, F: Fn(&Path) -> Result<(), Error>> SequentialDecoder<R, F> {
let entry: PxarEntry = self.read_item()?; let entry: PxarEntry = self.read_item()?;
if verbose { if verbose {
println!("Mode: {:08x} {:08x}", entry.mode, (entry.mode as u32) & libc::S_IFDIR); println!(
"Mode: {:08x} {:08x}",
entry.mode,
(entry.mode as u32) & libc::S_IFDIR
);
} }
let ifmt = (entry.mode as u32) & libc::S_IFMT; let ifmt = (entry.mode as u32) & libc::S_IFMT;
if ifmt == libc::S_IFDIR { if ifmt == libc::S_IFDIR {
let mut entry_count = 0; let mut entry_count = 0;
loop { loop {
@ -844,12 +912,14 @@ impl <R: Read, F: Fn(&Path) -> Result<(), Error>> SequentialDecoder<R, F> {
match head.htype { match head.htype {
PXAR_FILENAME => { PXAR_FILENAME => {
let name = self.read_filename(head.size)?; let name = self.read_filename(head.size)?;
if verbose { println!("Name: {:?}", name); } if verbose {
println!("Name: {:?}", name);
}
entry_count += 1; entry_count += 1;
path.push(&name); path.push(&name);
self.dump_entry(path, verbose, output)?; self.dump_entry(path, verbose, output)?;
path.pop(); path.pop();
}, }
PXAR_GOODBYE => { PXAR_GOODBYE => {
let table_size = (head.size - HEADER_SIZE) as usize; let table_size = (head.size - HEADER_SIZE) as usize;
if verbose { if verbose {
@ -859,12 +929,14 @@ impl <R: Read, F: Fn(&Path) -> Result<(), Error>> SequentialDecoder<R, F> {
self.skip_bytes(table_size)?; self.skip_bytes(table_size)?;
} }
break; break;
}, }
_ => panic!("got unexpected header type inside directory"), _ => panic!("got unexpected header type inside directory"),
} }
} }
} else if (ifmt == libc::S_IFBLK) || (ifmt == libc::S_IFCHR) || } else if (ifmt == libc::S_IFBLK)
(ifmt == libc::S_IFLNK) || (ifmt == libc::S_IFREG) || (ifmt == libc::S_IFCHR)
|| (ifmt == libc::S_IFLNK)
|| (ifmt == libc::S_IFREG)
{ {
loop { loop {
let head: PxarHeader = self.read_item()?; let head: PxarHeader = self.read_item()?;
@ -887,14 +959,14 @@ impl <R: Read, F: Fn(&Path) -> Result<(), Error>> SequentialDecoder<R, F> {
println!("Symlink: {:?}", target); println!("Symlink: {:?}", target);
} }
break; break;
}, }
PXAR_DEVICE => { PXAR_DEVICE => {
let device: PxarDevice = self.read_item()?; let device: PxarDevice = self.read_item()?;
if verbose { if verbose {
println!("Device: {}, {}", device.major, device.minor); println!("Device: {}, {}", device.major, device.minor);
} }
break; break;
}, }
PXAR_PAYLOAD => { PXAR_PAYLOAD => {
let payload_size = (head.size - HEADER_SIZE) as usize; let payload_size = (head.size - HEADER_SIZE) as usize;
if verbose { if verbose {
@ -929,71 +1001,70 @@ impl <R: Read, F: Fn(&Path) -> Result<(), Error>> SequentialDecoder<R, F> {
if verbose && self.has_features(flags::WITH_XATTRS) { if verbose && self.has_features(flags::WITH_XATTRS) {
println!("XAttr: {:?}", xattr); println!("XAttr: {:?}", xattr);
} }
}, }
PXAR_FCAPS => { PXAR_FCAPS => {
let fcaps = self.read_fcaps((header.size - HEADER_SIZE) as usize)?; let fcaps = self.read_fcaps((header.size - HEADER_SIZE) as usize)?;
if verbose && self.has_features(flags::WITH_FCAPS) { if verbose && self.has_features(flags::WITH_FCAPS) {
println!("FCaps: {:?}", fcaps); println!("FCaps: {:?}", fcaps);
} }
}, }
PXAR_ACL_USER => { PXAR_ACL_USER => {
let user = self.read_item::<PxarACLUser>()?; let user = self.read_item::<PxarACLUser>()?;
if verbose && self.has_features(flags::WITH_ACL) { if verbose && self.has_features(flags::WITH_ACL) {
println!("ACLUser: {:?}", user); println!("ACLUser: {:?}", user);
} }
}, }
PXAR_ACL_GROUP => { PXAR_ACL_GROUP => {
let group = self.read_item::<PxarACLGroup>()?; let group = self.read_item::<PxarACLGroup>()?;
if verbose && self.has_features(flags::WITH_ACL) { if verbose && self.has_features(flags::WITH_ACL) {
println!("ACLGroup: {:?}", group); println!("ACLGroup: {:?}", group);
} }
}, }
PXAR_ACL_GROUP_OBJ => { PXAR_ACL_GROUP_OBJ => {
let group_obj = self.read_item::<PxarACLGroupObj>()?; let group_obj = self.read_item::<PxarACLGroupObj>()?;
if verbose && self.has_features(flags::WITH_ACL) { if verbose && self.has_features(flags::WITH_ACL) {
println!("ACLGroupObj: {:?}", group_obj); println!("ACLGroupObj: {:?}", group_obj);
} }
}, }
PXAR_ACL_DEFAULT => { PXAR_ACL_DEFAULT => {
let default = self.read_item::<PxarACLDefault>()?; let default = self.read_item::<PxarACLDefault>()?;
if verbose && self.has_features(flags::WITH_ACL) { if verbose && self.has_features(flags::WITH_ACL) {
println!("ACLDefault: {:?}", default); println!("ACLDefault: {:?}", default);
} }
}, }
PXAR_ACL_DEFAULT_USER => { PXAR_ACL_DEFAULT_USER => {
let default_user = self.read_item::<PxarACLUser>()?; let default_user = self.read_item::<PxarACLUser>()?;
if verbose && self.has_features(flags::WITH_ACL) { if verbose && self.has_features(flags::WITH_ACL) {
println!("ACLDefaultUser: {:?}", default_user); println!("ACLDefaultUser: {:?}", default_user);
} }
}, }
PXAR_ACL_DEFAULT_GROUP => { PXAR_ACL_DEFAULT_GROUP => {
let default_group = self.read_item::<PxarACLGroup>()?; let default_group = self.read_item::<PxarACLGroup>()?;
if verbose && self.has_features(flags::WITH_ACL) { if verbose && self.has_features(flags::WITH_ACL) {
println!("ACLDefaultGroup: {:?}", default_group); println!("ACLDefaultGroup: {:?}", default_group);
} }
}, }
PXAR_QUOTA_PROJID => { PXAR_QUOTA_PROJID => {
let quota_projid = self.read_item::<PxarQuotaProjID>()?; let quota_projid = self.read_item::<PxarQuotaProjID>()?;
if verbose && self.has_features(flags::WITH_QUOTA_PROJID) { if verbose && self.has_features(flags::WITH_QUOTA_PROJID) {
println!("Quota project id: {:?}", quota_projid); println!("Quota project id: {:?}", quota_projid);
} }
}, }
_ => return Ok(false), _ => return Ok(false),
} }
Ok(true) Ok(true)
} }
fn dump_goodby_entries( fn dump_goodby_entries(&mut self, entry_count: usize, table_size: usize) -> Result<(), Error> {
&mut self,
entry_count: usize,
table_size: usize,
) -> Result<(), Error> {
const GOODBYE_ITEM_SIZE: usize = std::mem::size_of::<PxarGoodbyeItem>(); const GOODBYE_ITEM_SIZE: usize = std::mem::size_of::<PxarGoodbyeItem>();
if table_size < GOODBYE_ITEM_SIZE { if table_size < GOODBYE_ITEM_SIZE {
bail!("Goodbye table to small ({} < {})", table_size, GOODBYE_ITEM_SIZE); bail!(
"Goodbye table to small ({} < {})",
table_size,
GOODBYE_ITEM_SIZE
);
} }
if (table_size % GOODBYE_ITEM_SIZE) != 0 { if (table_size % GOODBYE_ITEM_SIZE) != 0 {
bail!("Goodbye table with strange size ({})", table_size); bail!("Goodbye table with strange size ({})", table_size);
@ -1002,7 +1073,11 @@ impl <R: Read, F: Fn(&Path) -> Result<(), Error>> SequentialDecoder<R, F> {
let entries = table_size / GOODBYE_ITEM_SIZE; let entries = table_size / GOODBYE_ITEM_SIZE;
if entry_count != (entries - 1) { if entry_count != (entries - 1) {
bail!("Goodbye table with wrong entry count ({} != {})", entry_count, entries - 1); bail!(
"Goodbye table with wrong entry count ({} != {})",
entry_count,
entries - 1
);
} }
let mut count = 0; let mut count = 0;
@ -1017,7 +1092,10 @@ impl <R: Read, F: Fn(&Path) -> Result<(), Error>> SequentialDecoder<R, F> {
println!("Goodby tail mark."); println!("Goodby tail mark.");
break; break;
} }
println!("Goodby item: offset {}, size {}, hash {:016x}", item.offset, item.size, item.hash); println!(
"Goodby item: offset {}, size {}, hash {:016x}",
item.offset, item.size, item.hash
);
if count >= entries { if count >= entries {
bail!("too many goodbye items (no tail marker)"); bail!("too many goodbye items (no tail marker)");
} }
@ -1030,7 +1108,7 @@ impl <R: Read, F: Fn(&Path) -> Result<(), Error>> SequentialDecoder<R, F> {
fn match_filename( fn match_filename(
filename: &OsStr, filename: &OsStr,
is_dir: bool, is_dir: bool,
match_pattern: &Vec<MatchPattern> match_pattern: &Vec<MatchPattern>,
) -> Result<(MatchType, Vec<MatchPattern>), Error> { ) -> Result<(MatchType, Vec<MatchPattern>), Error> {
let mut child_pattern = Vec::new(); let mut child_pattern = Vec::new();
let mut match_state = MatchType::None; let mut match_state = MatchType::None;
@ -1039,36 +1117,39 @@ fn match_filename(
for pattern in match_pattern { for pattern in match_pattern {
match pattern.matches_filename(&name, is_dir)? { match pattern.matches_filename(&name, is_dir)? {
MatchType::None => {}, MatchType::None => {}
MatchType::Positive => { MatchType::Positive => {
match_state = MatchType::Positive; match_state = MatchType::Positive;
let incl_pattern = MatchPattern::from_line(b"**/*").unwrap().unwrap(); let incl_pattern = MatchPattern::from_line(b"**/*").unwrap().unwrap();
child_pattern.push(incl_pattern.get_rest_pattern()); child_pattern.push(incl_pattern.get_rest_pattern());
}, }
MatchType::Negative => match_state = MatchType::Negative, MatchType::Negative => match_state = MatchType::Negative,
MatchType::PartialPositive => { MatchType::PartialPositive => {
if match_state != MatchType::Negative && match_state != MatchType::Positive { if match_state != MatchType::Negative && match_state != MatchType::Positive {
match_state = MatchType::PartialPositive; match_state = MatchType::PartialPositive;
} }
child_pattern.push(pattern.get_rest_pattern()); child_pattern.push(pattern.get_rest_pattern());
}, }
MatchType::PartialNegative => { MatchType::PartialNegative => {
if match_state == MatchType::PartialPositive { if match_state == MatchType::PartialPositive {
match_state = MatchType::PartialNegative; match_state = MatchType::PartialNegative;
} }
child_pattern.push(pattern.get_rest_pattern()); child_pattern.push(pattern.get_rest_pattern());
}, }
} }
} }
Ok((match_state, child_pattern)) Ok((match_state, child_pattern))
} }
fn file_openat(parent: RawFd, filename: &OsStr, flags: OFlag, mode: Mode) -> Result<std::fs::File, Error> { fn file_openat(
parent: RawFd,
let fd = filename.with_nix_path(|cstr| { filename: &OsStr,
nix::fcntl::openat(parent, cstr.as_ref(), flags, mode) flags: OFlag,
})??; mode: Mode,
) -> Result<std::fs::File, Error> {
let fd =
filename.with_nix_path(|cstr| nix::fcntl::openat(parent, cstr.as_ref(), flags, mode))??;
let file = unsafe { std::fs::File::from_raw_fd(fd) }; let file = unsafe { std::fs::File::from_raw_fd(fd) };
@ -1086,7 +1167,6 @@ fn hardlink(oldpath: &Path, newpath: &Path) -> Result<(), Error> {
} }
fn symlinkat(target: &Path, parent: RawFd, linkname: &OsStr) -> Result<(), Error> { fn symlinkat(target: &Path, parent: RawFd, linkname: &OsStr) -> Result<(), Error> {
target.with_nix_path(|target| { target.with_nix_path(|target| {
linkname.with_nix_path(|linkname| { linkname.with_nix_path(|linkname| {
let res = unsafe { libc::symlinkat(target.as_ptr(), parent, linkname.as_ptr()) }; let res = unsafe { libc::symlinkat(target.as_ptr(), parent, linkname.as_ptr()) };
@ -1097,7 +1177,6 @@ fn symlinkat(target: &Path, parent: RawFd, linkname: &OsStr) -> Result<(), Error
} }
fn nsec_to_update_timespec(mtime_nsec: u64) -> [libc::timespec; 2] { fn nsec_to_update_timespec(mtime_nsec: u64) -> [libc::timespec; 2] {
// restore mtime // restore mtime
const UTIME_OMIT: i64 = ((1 << 30) - 2); const UTIME_OMIT: i64 = ((1 << 30) - 2);
const NANOS_PER_SEC: i64 = 1_000_000_000; const NANOS_PER_SEC: i64 = 1_000_000_000;
@ -1106,8 +1185,14 @@ fn nsec_to_update_timespec(mtime_nsec: u64) -> [libc::timespec; 2] {
let nsec = (mtime_nsec as i64) % NANOS_PER_SEC; let nsec = (mtime_nsec as i64) % NANOS_PER_SEC;
let times: [libc::timespec; 2] = [ let times: [libc::timespec; 2] = [
libc::timespec { tv_sec: 0, tv_nsec: UTIME_OMIT }, libc::timespec {
libc::timespec { tv_sec: sec, tv_nsec: nsec }, tv_sec: 0,
tv_nsec: UTIME_OMIT,
},
libc::timespec {
tv_sec: sec,
tv_nsec: nsec,
},
]; ];
times times