2019-03-14 09:54:09 +00:00
|
|
|
//! *pxar* format decoder.
|
2019-01-06 16:27:57 +00:00
|
|
|
//!
|
2019-03-14 09:54:09 +00:00
|
|
|
//! This module contain the code to decode *pxar* archive files.
|
2018-12-30 16:32:52 +00:00
|
|
|
|
2019-01-06 16:27:57 +00:00
|
|
|
use failure::*;
|
2019-01-07 18:07:03 +00:00
|
|
|
use endian_trait::Endian;
|
2018-12-30 16:32:52 +00:00
|
|
|
|
2019-08-02 13:19:33 +00:00
|
|
|
use super::flags;
|
2019-01-06 16:27:57 +00:00
|
|
|
use super::format_definition::*;
|
2019-08-02 15:02:24 +00:00
|
|
|
use super::match_pattern::*;
|
2019-08-02 13:19:36 +00:00
|
|
|
use super::dir_stack::*;
|
2019-01-06 16:27:57 +00:00
|
|
|
|
2019-03-08 15:55:54 +00:00
|
|
|
use std::io::{Read, Write};
|
2019-01-07 18:07:03 +00:00
|
|
|
use std::path::{Path, PathBuf};
|
2019-01-06 16:27:57 +00:00
|
|
|
|
2019-01-08 16:17:55 +00:00
|
|
|
use std::os::unix::io::AsRawFd;
|
|
|
|
use std::os::unix::io::RawFd;
|
|
|
|
use std::os::unix::io::FromRawFd;
|
2019-07-16 16:19:42 +00:00
|
|
|
use std::os::unix::ffi::{OsStrExt, OsStringExt};
|
|
|
|
use std::ffi::CString;
|
2019-01-06 16:27:57 +00:00
|
|
|
use std::ffi::{OsStr, OsString};
|
|
|
|
|
2019-01-08 16:17:55 +00:00
|
|
|
use nix::fcntl::OFlag;
|
|
|
|
use nix::sys::stat::Mode;
|
|
|
|
use nix::errno::Errno;
|
|
|
|
use nix::NixPath;
|
2019-05-22 13:02:16 +00:00
|
|
|
|
2019-07-01 08:44:03 +00:00
|
|
|
use proxmox::tools::io::ReadExt;
|
2019-07-01 09:03:25 +00:00
|
|
|
use proxmox::tools::vec;
|
2019-07-01 08:44:03 +00:00
|
|
|
|
2019-05-29 12:34:05 +00:00
|
|
|
use crate::tools::fs;
|
2019-05-23 16:09:15 +00:00
|
|
|
use crate::tools::acl;
|
2019-05-17 12:23:35 +00:00
|
|
|
use crate::tools::xattr;
|
2019-01-08 16:17:55 +00:00
|
|
|
|
2019-03-08 15:55:54 +00:00
|
|
|
// This one need Read, but works without Seek
|
2019-08-13 12:50:13 +00:00
|
|
|
pub struct SequentialDecoder<R: Read, F: Fn(&Path) -> Result<(), Error>> {
|
|
|
|
reader: R,
|
2019-05-22 15:50:40 +00:00
|
|
|
feature_flags: u64,
|
2019-07-29 12:01:45 +00:00
|
|
|
allow_existing_dirs: bool,
|
2019-03-08 15:55:54 +00:00
|
|
|
skip_buffer: Vec<u8>,
|
2019-07-04 12:03:20 +00:00
|
|
|
callback: F,
|
2019-01-06 16:27:57 +00:00
|
|
|
}
|
|
|
|
|
2019-08-02 13:19:34 +00:00
|
|
|
const HEADER_SIZE: u64 = std::mem::size_of::<PxarHeader>() as u64;
|
2019-01-06 16:27:57 +00:00
|
|
|
|
2019-08-13 12:50:13 +00:00
|
|
|
impl <R: Read, F: Fn(&Path) -> Result<(), Error>> SequentialDecoder<R, F> {
|
2019-01-06 16:27:57 +00:00
|
|
|
|
2019-08-13 12:50:13 +00:00
|
|
|
pub fn new(reader: R, feature_flags: u64, callback: F) -> Self {
|
2019-05-22 13:02:16 +00:00
|
|
|
let skip_buffer = vec::undefined(64*1024);
|
2019-05-22 15:50:40 +00:00
|
|
|
|
|
|
|
Self {
|
|
|
|
reader,
|
|
|
|
feature_flags,
|
2019-07-29 12:01:45 +00:00
|
|
|
allow_existing_dirs: false,
|
2019-07-04 12:03:20 +00:00
|
|
|
skip_buffer,
|
|
|
|
callback,
|
2019-05-22 15:50:40 +00:00
|
|
|
}
|
2019-01-06 16:27:57 +00:00
|
|
|
}
|
|
|
|
|
2019-07-29 12:01:45 +00:00
|
|
|
pub fn set_allow_existing_dirs(&mut self, allow: bool) {
|
|
|
|
self.allow_existing_dirs = allow;
|
|
|
|
}
|
|
|
|
|
2019-08-13 12:50:13 +00:00
|
|
|
pub (crate) fn get_reader_mut(&mut self) -> &mut R {
|
|
|
|
&mut self.reader
|
2019-03-15 08:36:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub (crate) fn read_item<T: Endian>(&mut self) -> Result<T, Error> {
|
2019-01-06 16:27:57 +00:00
|
|
|
|
2019-01-07 18:07:03 +00:00
|
|
|
let mut result: T = unsafe { std::mem::uninitialized() };
|
2019-01-06 16:27:57 +00:00
|
|
|
|
2019-01-07 18:07:03 +00:00
|
|
|
let buffer = unsafe { std::slice::from_raw_parts_mut(
|
|
|
|
&mut result as *mut T as *mut u8,
|
|
|
|
std::mem::size_of::<T>()
|
|
|
|
)};
|
|
|
|
|
|
|
|
self.reader.read_exact(buffer)?;
|
|
|
|
|
|
|
|
Ok(result.from_le())
|
|
|
|
}
|
|
|
|
|
2019-03-16 10:02:12 +00:00
|
|
|
fn read_link(&mut self, size: u64) -> Result<PathBuf, Error> {
|
2019-01-07 18:07:03 +00:00
|
|
|
if size < (HEADER_SIZE + 2) {
|
2019-03-16 10:02:12 +00:00
|
|
|
bail!("dectected short link target.");
|
2019-01-06 16:27:57 +00:00
|
|
|
}
|
2019-01-07 18:07:03 +00:00
|
|
|
let target_len = size - HEADER_SIZE;
|
2019-01-06 16:27:57 +00:00
|
|
|
|
2019-01-07 18:07:03 +00:00
|
|
|
if target_len > (libc::PATH_MAX as u64) {
|
2019-03-16 10:02:12 +00:00
|
|
|
bail!("link target too long ({}).", target_len);
|
2019-01-07 18:07:03 +00:00
|
|
|
}
|
2019-01-06 16:27:57 +00:00
|
|
|
|
2019-05-22 13:02:16 +00:00
|
|
|
let mut buffer = self.reader.read_exact_allocated(target_len as usize)?;
|
2019-01-06 16:27:57 +00:00
|
|
|
|
2019-01-07 18:07:03 +00:00
|
|
|
let last_byte = buffer.pop().unwrap();
|
|
|
|
if last_byte != 0u8 {
|
2019-03-16 10:02:12 +00:00
|
|
|
bail!("link target not nul terminated.");
|
2019-01-06 16:27:57 +00:00
|
|
|
}
|
2019-01-07 18:07:03 +00:00
|
|
|
|
|
|
|
Ok(PathBuf::from(std::ffi::OsString::from_vec(buffer)))
|
|
|
|
}
|
|
|
|
|
2019-08-20 11:40:19 +00:00
|
|
|
pub (crate) fn read_hardlink(&mut self, size: u64) -> Result<(PathBuf, u64), Error> {
|
2019-03-16 10:02:12 +00:00
|
|
|
if size < (HEADER_SIZE + 8 + 2) {
|
|
|
|
bail!("dectected short hardlink header.");
|
|
|
|
}
|
|
|
|
let offset: u64 = self.read_item()?;
|
|
|
|
let target = self.read_link(size - 8)?;
|
|
|
|
|
|
|
|
for c in target.components() {
|
|
|
|
match c {
|
|
|
|
std::path::Component::Normal(_) => { /* OK */ },
|
|
|
|
_ => {
|
|
|
|
bail!("hardlink target contains invalid component {:?}", c);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok((target, offset))
|
|
|
|
}
|
|
|
|
|
2019-03-15 08:36:05 +00:00
|
|
|
pub (crate) fn read_filename(&mut self, size: u64) -> Result<OsString, Error> {
|
2019-01-07 18:07:03 +00:00
|
|
|
if size < (HEADER_SIZE + 2) {
|
|
|
|
bail!("dectected short filename");
|
|
|
|
}
|
|
|
|
let name_len = size - HEADER_SIZE;
|
2019-01-06 16:27:57 +00:00
|
|
|
|
|
|
|
if name_len > ((libc::FILENAME_MAX as u64) + 1) {
|
2019-01-07 18:07:03 +00:00
|
|
|
bail!("filename too long ({}).", name_len);
|
2019-01-06 16:27:57 +00:00
|
|
|
}
|
|
|
|
|
2019-05-22 13:02:16 +00:00
|
|
|
let mut buffer = self.reader.read_exact_allocated(name_len as usize)?;
|
2019-01-06 16:27:57 +00:00
|
|
|
|
|
|
|
let last_byte = buffer.pop().unwrap();
|
|
|
|
if last_byte != 0u8 {
|
2019-01-07 18:07:03 +00:00
|
|
|
bail!("filename entry not nul terminated.");
|
|
|
|
}
|
2019-01-06 16:27:57 +00:00
|
|
|
|
2019-07-01 15:03:49 +00:00
|
|
|
if buffer == b"." || buffer == b".." {
|
|
|
|
bail!("found invalid filename '.' or '..'.");
|
2019-03-11 13:31:01 +00:00
|
|
|
}
|
|
|
|
|
2019-07-16 16:19:41 +00:00
|
|
|
if buffer.iter().find(|b| (**b == b'/' || **b == b'\0')).is_some() {
|
|
|
|
bail!("found invalid filename with slashes or nul bytes.");
|
2019-01-09 11:17:23 +00:00
|
|
|
}
|
2019-01-08 16:17:55 +00:00
|
|
|
|
2019-03-11 13:31:01 +00:00
|
|
|
let name = std::ffi::OsString::from_vec(buffer);
|
|
|
|
if name.is_empty() {
|
|
|
|
bail!("found empty filename.");
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(name)
|
2019-01-07 18:07:03 +00:00
|
|
|
}
|
|
|
|
|
2019-05-22 15:50:40 +00:00
|
|
|
fn has_features(&self, feature_flags: u64) -> bool {
|
|
|
|
(self.feature_flags & feature_flags) == feature_flags
|
|
|
|
}
|
|
|
|
|
2019-08-02 13:19:34 +00:00
|
|
|
fn read_xattr(&mut self, size: usize) -> Result<PxarXAttr, Error> {
|
2019-05-22 13:02:16 +00:00
|
|
|
let buffer = self.reader.read_exact_allocated(size)?;
|
2019-05-17 12:23:35 +00:00
|
|
|
|
2019-05-23 09:58:40 +00:00
|
|
|
let separator = buffer.iter().position(|c| *c == b'\0')
|
|
|
|
.ok_or_else(|| format_err!("no value found in xattr"))?;
|
|
|
|
|
|
|
|
let (name, value) = buffer.split_at(separator);
|
|
|
|
if !xattr::is_valid_xattr_name(name) ||
|
|
|
|
xattr::is_security_capability(name)
|
|
|
|
{
|
|
|
|
bail!("incorrect xattr name - {}.", String::from_utf8_lossy(name));
|
2019-05-17 12:23:35 +00:00
|
|
|
}
|
2019-05-23 09:58:40 +00:00
|
|
|
|
2019-08-02 13:19:34 +00:00
|
|
|
Ok(PxarXAttr {
|
2019-05-23 09:58:40 +00:00
|
|
|
name: name.to_vec(),
|
|
|
|
value: value[1..].to_vec(),
|
|
|
|
})
|
2019-05-17 12:23:35 +00:00
|
|
|
}
|
|
|
|
|
2019-08-02 13:19:34 +00:00
|
|
|
fn read_fcaps(&mut self, size: usize) -> Result<PxarFCaps, Error> {
|
2019-05-22 13:02:16 +00:00
|
|
|
let buffer = self.reader.read_exact_allocated(size)?;
|
2019-05-17 12:23:35 +00:00
|
|
|
|
2019-08-02 13:19:34 +00:00
|
|
|
Ok(PxarFCaps { data: buffer })
|
2019-05-17 12:23:35 +00:00
|
|
|
}
|
|
|
|
|
2019-08-02 13:19:34 +00:00
|
|
|
fn read_attributes(&mut self) -> Result<(PxarHeader, PxarAttributes), Error> {
|
2019-08-02 05:16:10 +00:00
|
|
|
let mut attr = PxarAttributes::default();
|
2019-08-02 13:19:34 +00:00
|
|
|
let mut head: PxarHeader = self.read_item()?;
|
2019-05-17 12:23:35 +00:00
|
|
|
let mut size = (head.size - HEADER_SIZE) as usize;
|
2019-01-09 13:44:00 +00:00
|
|
|
loop {
|
|
|
|
match head.htype {
|
2019-08-02 13:19:34 +00:00
|
|
|
PXAR_XATTR => {
|
2019-08-02 13:19:33 +00:00
|
|
|
if self.has_features(flags::WITH_XATTRS) {
|
2019-08-01 11:08:08 +00:00
|
|
|
attr.xattrs.push(self.read_xattr(size)?);
|
2019-05-22 15:50:40 +00:00
|
|
|
} else {
|
|
|
|
self.skip_bytes(size)?;
|
|
|
|
}
|
|
|
|
},
|
2019-08-02 13:19:34 +00:00
|
|
|
PXAR_FCAPS => {
|
2019-08-02 13:19:33 +00:00
|
|
|
if self.has_features(flags::WITH_FCAPS) {
|
2019-08-01 11:08:08 +00:00
|
|
|
attr.fcaps = Some(self.read_fcaps(size)?);
|
2019-05-22 15:50:40 +00:00
|
|
|
} else {
|
|
|
|
self.skip_bytes(size)?;
|
|
|
|
}
|
|
|
|
},
|
2019-08-02 13:19:34 +00:00
|
|
|
PXAR_ACL_USER => {
|
2019-08-02 13:19:33 +00:00
|
|
|
if self.has_features(flags::WITH_ACL) {
|
2019-08-02 13:19:34 +00:00
|
|
|
attr.acl_user.push(self.read_item::<PxarACLUser>()?);
|
2019-05-23 16:09:15 +00:00
|
|
|
} else {
|
|
|
|
self.skip_bytes(size)?;
|
|
|
|
}
|
|
|
|
},
|
2019-08-02 13:19:34 +00:00
|
|
|
PXAR_ACL_GROUP => {
|
2019-08-02 13:19:33 +00:00
|
|
|
if self.has_features(flags::WITH_ACL) {
|
2019-08-02 13:19:34 +00:00
|
|
|
attr.acl_group.push(self.read_item::<PxarACLGroup>()?);
|
2019-05-23 16:09:15 +00:00
|
|
|
} else {
|
|
|
|
self.skip_bytes(size)?;
|
|
|
|
}
|
|
|
|
},
|
2019-08-02 13:19:34 +00:00
|
|
|
PXAR_ACL_GROUP_OBJ => {
|
2019-08-02 13:19:33 +00:00
|
|
|
if self.has_features(flags::WITH_ACL) {
|
2019-08-02 13:19:34 +00:00
|
|
|
attr.acl_group_obj = Some(self.read_item::<PxarACLGroupObj>()?);
|
2019-05-23 16:09:15 +00:00
|
|
|
} else {
|
|
|
|
self.skip_bytes(size)?;
|
|
|
|
}
|
|
|
|
},
|
2019-08-02 13:19:34 +00:00
|
|
|
PXAR_ACL_DEFAULT => {
|
2019-08-02 13:19:33 +00:00
|
|
|
if self.has_features(flags::WITH_ACL) {
|
2019-08-02 13:19:34 +00:00
|
|
|
attr.acl_default = Some(self.read_item::<PxarACLDefault>()?);
|
2019-05-23 16:09:15 +00:00
|
|
|
} else {
|
|
|
|
self.skip_bytes(size)?;
|
|
|
|
}
|
|
|
|
},
|
2019-08-02 13:19:34 +00:00
|
|
|
PXAR_ACL_DEFAULT_USER => {
|
2019-08-02 13:19:33 +00:00
|
|
|
if self.has_features(flags::WITH_ACL) {
|
2019-08-02 13:19:34 +00:00
|
|
|
attr.acl_default_user.push(self.read_item::<PxarACLUser>()?);
|
2019-05-23 16:09:15 +00:00
|
|
|
} else {
|
|
|
|
self.skip_bytes(size)?;
|
|
|
|
}
|
|
|
|
},
|
2019-08-02 13:19:34 +00:00
|
|
|
PXAR_ACL_DEFAULT_GROUP => {
|
2019-08-02 13:19:33 +00:00
|
|
|
if self.has_features(flags::WITH_ACL) {
|
2019-08-02 13:19:34 +00:00
|
|
|
attr.acl_default_group.push(self.read_item::<PxarACLGroup>()?);
|
2019-05-23 16:09:15 +00:00
|
|
|
} else {
|
|
|
|
self.skip_bytes(size)?;
|
|
|
|
}
|
|
|
|
},
|
2019-08-02 13:19:34 +00:00
|
|
|
PXAR_QUOTA_PROJID => {
|
2019-08-02 13:19:33 +00:00
|
|
|
if self.has_features(flags::WITH_QUOTA_PROJID) {
|
2019-08-02 13:19:34 +00:00
|
|
|
attr.quota_projid = Some(self.read_item::<PxarQuotaProjID>()?);
|
2019-05-29 12:34:05 +00:00
|
|
|
} else {
|
|
|
|
self.skip_bytes(size)?;
|
|
|
|
}
|
|
|
|
},
|
2019-05-17 12:23:35 +00:00
|
|
|
_ => break,
|
2019-01-09 13:44:00 +00:00
|
|
|
}
|
2019-05-17 12:23:35 +00:00
|
|
|
head = self.read_item()?;
|
|
|
|
size = (head.size - HEADER_SIZE) as usize;
|
2019-01-09 13:44:00 +00:00
|
|
|
}
|
2019-05-23 16:09:15 +00:00
|
|
|
|
2019-08-01 11:08:08 +00:00
|
|
|
Ok((head, attr))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn restore_attributes(
|
|
|
|
&mut self,
|
|
|
|
fd: RawFd,
|
|
|
|
attr: &PxarAttributes,
|
2019-08-02 13:19:34 +00:00
|
|
|
entry: &PxarEntry,
|
2019-08-01 11:08:08 +00:00
|
|
|
) -> Result<(), Error> {
|
|
|
|
self.restore_xattrs_fcaps_fd(fd, &attr.xattrs, &attr.fcaps)?;
|
|
|
|
|
|
|
|
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::ACL_OTHER, None, mode_other_to_acl_permissions(entry.mode))?;
|
|
|
|
match &attr.acl_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_GROUP_OBJ, None, group_obj.permissions)?;
|
|
|
|
},
|
|
|
|
None => {
|
|
|
|
acl.add_entry_full(acl::ACL_GROUP_OBJ, None, mode_group_to_acl_permissions(entry.mode))?;
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for user in &attr.acl_user {
|
|
|
|
acl.add_entry_full(acl::ACL_USER, Some(user.uid), user.permissions)?;
|
|
|
|
}
|
|
|
|
for group in &attr.acl_group {
|
|
|
|
acl.add_entry_full(acl::ACL_GROUP, Some(group.gid), group.permissions)?;
|
|
|
|
}
|
|
|
|
let proc_path = Path::new("/proc/self/fd/").join(fd.to_string());
|
|
|
|
if !acl.is_valid() {
|
|
|
|
bail!("Error while restoring ACL - ACL invalid");
|
|
|
|
}
|
|
|
|
acl.set_file(&proc_path, acl::ACL_TYPE_ACCESS)?;
|
|
|
|
|
|
|
|
if let Some(default) = &attr.acl_default {
|
2019-05-23 16:09:15 +00:00
|
|
|
let mut acl = acl::ACL::init(5)?;
|
2019-08-01 11:08:08 +00:00
|
|
|
acl.add_entry_full(acl::ACL_USER_OBJ, None, default.user_obj_permissions)?;
|
|
|
|
acl.add_entry_full(acl::ACL_GROUP_OBJ, None, default.group_obj_permissions)?;
|
|
|
|
acl.add_entry_full(acl::ACL_OTHER, None, default.other_permissions)?;
|
|
|
|
if default.mask_permissions != std::u64::MAX {
|
|
|
|
acl.add_entry_full(acl::ACL_MASK, None, default.mask_permissions)?;
|
2019-05-23 16:09:15 +00:00
|
|
|
}
|
2019-08-01 11:08:08 +00:00
|
|
|
for user in &attr.acl_default_user {
|
2019-05-23 16:09:15 +00:00
|
|
|
acl.add_entry_full(acl::ACL_USER, Some(user.uid), user.permissions)?;
|
|
|
|
}
|
2019-08-01 11:08:08 +00:00
|
|
|
for group in &attr.acl_default_group {
|
2019-05-23 16:09:15 +00:00
|
|
|
acl.add_entry_full(acl::ACL_GROUP, Some(group.gid), group.permissions)?;
|
|
|
|
}
|
|
|
|
if !acl.is_valid() {
|
|
|
|
bail!("Error while restoring ACL - ACL invalid");
|
|
|
|
}
|
2019-08-01 11:08:08 +00:00
|
|
|
acl.set_file(&proc_path, acl::ACL_TYPE_DEFAULT)?;
|
2019-05-23 16:09:15 +00:00
|
|
|
}
|
2019-08-01 11:08:08 +00:00
|
|
|
self.restore_quota_projid(fd, &attr.quota_projid)?;
|
2019-05-23 16:09:15 +00:00
|
|
|
|
2019-08-01 11:08:08 +00:00
|
|
|
Ok(())
|
2019-05-17 12:23:35 +00:00
|
|
|
}
|
|
|
|
|
2019-07-16 16:19:43 +00:00
|
|
|
// Restore xattrs and fcaps to the given RawFd.
|
|
|
|
fn restore_xattrs_fcaps_fd(
|
|
|
|
&mut self,
|
|
|
|
fd: RawFd,
|
2019-08-02 13:19:34 +00:00
|
|
|
xattrs: &Vec<PxarXAttr>,
|
|
|
|
fcaps: &Option<PxarFCaps>
|
2019-07-16 16:19:43 +00:00
|
|
|
) -> Result<(), Error> {
|
2019-05-17 12:23:35 +00:00
|
|
|
for xattr in xattrs {
|
2019-08-01 10:13:07 +00:00
|
|
|
if let Err(err) = xattr::fsetxattr(fd, &xattr) {
|
2019-05-17 12:23:35 +00:00
|
|
|
bail!("fsetxattr failed with error: {}", err);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if let Some(fcaps) = fcaps {
|
2019-08-01 10:13:07 +00:00
|
|
|
if let Err(err) = xattr::fsetxattr_fcaps(fd, &fcaps) {
|
2019-05-17 12:23:35 +00:00
|
|
|
bail!("fsetxattr_fcaps failed with error: {}", err);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
2019-01-09 13:44:00 +00:00
|
|
|
}
|
|
|
|
|
2019-07-16 16:19:43 +00:00
|
|
|
fn restore_quota_projid(
|
|
|
|
&mut self,
|
|
|
|
fd: RawFd,
|
2019-08-02 13:19:34 +00:00
|
|
|
projid: &Option<PxarQuotaProjID>
|
2019-07-16 16:19:43 +00:00
|
|
|
) -> Result<(), Error> {
|
2019-05-29 12:34:05 +00:00
|
|
|
if let Some(projid) = projid {
|
|
|
|
let mut fsxattr = fs::FSXAttr::default();
|
|
|
|
unsafe {
|
|
|
|
fs::fs_ioc_fsgetxattr(fd, &mut fsxattr)
|
|
|
|
.map_err(|err| format_err!("error while getting fsxattr to restore quota project id - {}", err))?;
|
|
|
|
}
|
|
|
|
fsxattr.fsx_projid = projid.projid as u32;
|
|
|
|
unsafe {
|
|
|
|
fs::fs_ioc_fssetxattr(fd, &fsxattr)
|
|
|
|
.map_err(|err| format_err!("error while setting fsxattr to restore quota project id - {}", err))?;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2019-08-02 13:19:34 +00:00
|
|
|
fn restore_mode(&mut self, entry: &PxarEntry, fd: RawFd) -> Result<(), Error> {
|
2019-01-09 13:44:00 +00:00
|
|
|
|
|
|
|
let mode = Mode::from_bits_truncate((entry.mode as u32) & 0o7777);
|
|
|
|
|
|
|
|
nix::sys::stat::fchmod(fd, mode)?;
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2019-08-02 13:19:34 +00:00
|
|
|
fn restore_mode_at(&mut self, entry: &PxarEntry, dirfd: RawFd, filename: &OsStr) -> Result<(), Error> {
|
2019-01-09 13:44:00 +00:00
|
|
|
|
|
|
|
let mode = Mode::from_bits_truncate((entry.mode as u32) & 0o7777);
|
|
|
|
|
2019-01-11 11:22:00 +00:00
|
|
|
// 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.
|
|
|
|
nix::sys::stat::fchmodat(Some(dirfd), filename, mode, nix::sys::stat::FchmodatFlags::FollowSymlink)?;
|
2019-01-09 13:44:00 +00:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2019-08-02 13:19:34 +00:00
|
|
|
fn restore_ugid(&mut self, entry: &PxarEntry, fd: RawFd) -> Result<(), Error> {
|
2019-01-09 13:44:00 +00:00
|
|
|
|
2019-08-02 13:19:35 +00:00
|
|
|
let uid = entry.uid;
|
|
|
|
let gid = entry.gid;
|
2019-01-09 13:44:00 +00:00
|
|
|
|
|
|
|
let res = unsafe { libc::fchown(fd, uid, gid) };
|
|
|
|
Errno::result(res)?;
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2019-08-02 13:19:34 +00:00
|
|
|
fn restore_ugid_at(&mut self, entry: &PxarEntry, dirfd: RawFd, filename: &OsStr) -> Result<(), Error> {
|
2019-01-09 13:44:00 +00:00
|
|
|
|
2019-08-02 13:19:34 +00:00
|
|
|
let uid = entry.uid;
|
|
|
|
let gid = entry.gid;
|
2019-01-09 13:44:00 +00:00
|
|
|
|
|
|
|
let res = filename.with_nix_path(|cstr| unsafe {
|
|
|
|
libc::fchownat(dirfd, cstr.as_ptr(), uid, gid, libc::AT_SYMLINK_NOFOLLOW)
|
|
|
|
})?;
|
|
|
|
Errno::result(res)?;
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2019-08-02 13:19:34 +00:00
|
|
|
fn restore_mtime(&mut self, entry: &PxarEntry, fd: RawFd) -> Result<(), Error> {
|
2019-01-09 13:44:00 +00:00
|
|
|
|
|
|
|
let times = nsec_to_update_timespec(entry.mtime);
|
|
|
|
|
|
|
|
let res = unsafe { libc::futimens(fd, ×[0]) };
|
|
|
|
Errno::result(res)?;
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2019-08-02 13:19:34 +00:00
|
|
|
fn restore_mtime_at(&mut self, entry: &PxarEntry, dirfd: RawFd, filename: &OsStr) -> Result<(), Error> {
|
2019-01-09 13:44:00 +00:00
|
|
|
|
|
|
|
let times = nsec_to_update_timespec(entry.mtime);
|
|
|
|
|
|
|
|
let res = filename.with_nix_path(|cstr| unsafe {
|
|
|
|
libc::utimensat(dirfd, cstr.as_ptr(), ×[0], libc::AT_SYMLINK_NOFOLLOW)
|
|
|
|
})?;
|
|
|
|
Errno::result(res)?;
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2019-08-02 13:19:34 +00:00
|
|
|
fn restore_device_at(&mut self, entry: &PxarEntry, dirfd: RawFd, filename: &OsStr, device: &PxarDevice) -> Result<(), Error> {
|
2019-01-11 11:22:00 +00:00
|
|
|
|
|
|
|
let rdev = nix::sys::stat::makedev(device.major, device.minor);
|
2019-01-11 12:12:55 +00:00
|
|
|
let mode = ((entry.mode as u32) & libc::S_IFMT) | 0o0600;
|
2019-01-11 11:22:00 +00:00
|
|
|
let res = filename.with_nix_path(|cstr| unsafe {
|
2019-01-11 12:12:55 +00:00
|
|
|
libc::mknodat(dirfd, cstr.as_ptr(), mode, rdev)
|
2019-01-11 11:22:00 +00:00
|
|
|
})?;
|
|
|
|
Errno::result(res)?;
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2019-01-11 12:26:05 +00:00
|
|
|
fn restore_socket_at(&mut self, dirfd: RawFd, filename: &OsStr) -> Result<(), Error> {
|
|
|
|
|
|
|
|
let mode = libc::S_IFSOCK | 0o0600;
|
|
|
|
let res = filename.with_nix_path(|cstr| unsafe {
|
|
|
|
libc::mknodat(dirfd, cstr.as_ptr(), mode, 0)
|
|
|
|
})?;
|
|
|
|
Errno::result(res)?;
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn restore_fifo_at(&mut self, dirfd: RawFd, filename: &OsStr) -> Result<(), Error> {
|
|
|
|
|
|
|
|
let mode = libc::S_IFIFO | 0o0600;
|
|
|
|
let res = filename.with_nix_path(|cstr| unsafe {
|
|
|
|
libc::mkfifoat(dirfd, cstr.as_ptr(), mode)
|
|
|
|
})?;
|
|
|
|
Errno::result(res)?;
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2019-03-14 12:10:27 +00:00
|
|
|
fn skip_bytes(&mut self, count: usize) -> Result<(), Error> {
|
|
|
|
let mut done = 0;
|
|
|
|
while done < count {
|
|
|
|
let todo = count - done;
|
|
|
|
let n = if todo > self.skip_buffer.len() { self.skip_buffer.len() } else { todo };
|
|
|
|
let data = &mut self.skip_buffer[..n];
|
|
|
|
self.reader.read_exact(data)?;
|
|
|
|
done += n;
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2019-07-04 14:15:54 +00:00
|
|
|
fn restore_symlink(
|
|
|
|
&mut self,
|
2019-07-16 16:19:43 +00:00
|
|
|
parent_fd: Option<RawFd>,
|
2019-07-04 14:15:54 +00:00
|
|
|
full_path: &PathBuf,
|
2019-08-02 13:19:34 +00:00
|
|
|
entry: &PxarEntry,
|
2019-07-04 14:15:54 +00:00
|
|
|
filename: &OsStr
|
|
|
|
) -> Result<(), Error> {
|
|
|
|
//fixme: create symlink
|
|
|
|
//fixme: restore permission, acls, xattr, ...
|
|
|
|
|
2019-08-02 13:19:34 +00:00
|
|
|
let head: PxarHeader = self.read_item()?;
|
2019-07-04 14:15:54 +00:00
|
|
|
match head.htype {
|
2019-08-02 13:19:34 +00:00
|
|
|
PXAR_SYMLINK => {
|
2019-07-04 14:15:54 +00:00
|
|
|
let target = self.read_link(head.size)?;
|
|
|
|
//println!("TARGET: {:?}", target);
|
2019-07-16 16:19:43 +00:00
|
|
|
if let Some(fd) = parent_fd {
|
|
|
|
if let Err(err) = symlinkat(&target, fd, filename) {
|
|
|
|
bail!("create symlink {:?} failed - {}", full_path, err);
|
|
|
|
}
|
2019-07-04 14:15:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => {
|
|
|
|
bail!("got unknown header type inside symlink entry {:016x}", head.htype);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-16 16:19:43 +00:00
|
|
|
if let Some(fd) = parent_fd {
|
|
|
|
// self.restore_mode_at(&entry, fd, filename)?; //not supported on symlinks
|
|
|
|
self.restore_ugid_at(&entry, fd, filename)?;
|
|
|
|
self.restore_mtime_at(&entry, fd, filename)?;
|
|
|
|
}
|
2019-07-04 14:15:54 +00:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn restore_socket(
|
|
|
|
&mut self,
|
2019-07-16 16:19:43 +00:00
|
|
|
parent_fd: Option<RawFd>,
|
2019-08-02 13:19:34 +00:00
|
|
|
entry: &PxarEntry,
|
2019-07-04 14:15:54 +00:00
|
|
|
filename: &OsStr
|
|
|
|
) -> Result<(), Error> {
|
2019-08-02 13:19:33 +00:00
|
|
|
if !self.has_features(flags::WITH_SOCKETS) {
|
2019-08-01 15:51:59 +00:00
|
|
|
return Ok(());
|
|
|
|
}
|
2019-07-16 16:19:43 +00:00
|
|
|
if let Some(fd) = parent_fd {
|
|
|
|
self.restore_socket_at(fd, filename)?;
|
|
|
|
self.restore_mode_at(&entry, fd, filename)?;
|
|
|
|
self.restore_ugid_at(&entry, fd, filename)?;
|
|
|
|
self.restore_mtime_at(&entry, fd, filename)?;
|
|
|
|
}
|
2019-07-04 14:15:54 +00:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn restore_fifo(
|
|
|
|
&mut self,
|
2019-07-16 16:19:43 +00:00
|
|
|
parent_fd: Option<RawFd>,
|
2019-08-02 13:19:34 +00:00
|
|
|
entry: &PxarEntry,
|
2019-07-04 14:15:54 +00:00
|
|
|
filename: &OsStr
|
|
|
|
) -> Result<(), Error> {
|
2019-08-02 13:19:33 +00:00
|
|
|
if !self.has_features(flags::WITH_FIFOS) {
|
2019-08-01 15:51:59 +00:00
|
|
|
return Ok(());
|
|
|
|
}
|
2019-07-16 16:19:43 +00:00
|
|
|
if let Some(fd) = parent_fd {
|
|
|
|
self.restore_fifo_at(fd, filename)?;
|
|
|
|
self.restore_mode_at(&entry, fd, filename)?;
|
|
|
|
self.restore_ugid_at(&entry, fd, filename)?;
|
|
|
|
self.restore_mtime_at(&entry, fd, filename)?;
|
|
|
|
}
|
2019-07-04 14:15:54 +00:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn restore_device(
|
|
|
|
&mut self,
|
2019-07-16 16:19:43 +00:00
|
|
|
parent_fd: Option<RawFd>,
|
2019-08-02 13:19:34 +00:00
|
|
|
entry: &PxarEntry,
|
2019-07-04 14:15:54 +00:00
|
|
|
filename: &OsStr
|
|
|
|
) -> Result<(), Error> {
|
2019-08-02 13:19:34 +00:00
|
|
|
let head: PxarHeader = self.read_item()?;
|
|
|
|
if head.htype != PXAR_DEVICE {
|
2019-07-04 14:15:54 +00:00
|
|
|
bail!("got unknown header type inside device entry {:016x}", head.htype);
|
|
|
|
}
|
2019-08-02 13:19:34 +00:00
|
|
|
let device: PxarDevice = self.read_item()?;
|
2019-08-02 13:19:33 +00:00
|
|
|
if !self.has_features(flags::WITH_DEVICE_NODES) {
|
2019-08-01 15:51:59 +00:00
|
|
|
return Ok(());
|
|
|
|
}
|
2019-07-16 16:19:43 +00:00
|
|
|
if let Some(fd) = parent_fd {
|
|
|
|
self.restore_device_at(&entry, fd, filename, &device)?;
|
|
|
|
self.restore_mode_at(&entry, fd, filename)?;
|
|
|
|
self.restore_ugid_at(&entry, fd, filename)?;
|
|
|
|
self.restore_mtime_at(&entry, fd, filename)?;
|
|
|
|
}
|
2019-07-04 14:15:54 +00:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2019-07-16 16:19:43 +00:00
|
|
|
/// Restores a regular file with its content and associated attributes to the
|
|
|
|
/// folder provided by the raw filedescriptor.
|
|
|
|
/// If None is passed instead of a filedescriptor, the file is not restored but
|
|
|
|
/// the archive reader is skipping over it instead.
|
2019-07-04 14:15:54 +00:00
|
|
|
fn restore_regular_file(
|
|
|
|
&mut self,
|
2019-07-16 16:19:43 +00:00
|
|
|
parent_fd: Option<RawFd>,
|
2019-07-04 14:15:54 +00:00
|
|
|
full_path: &PathBuf,
|
2019-08-02 13:19:34 +00:00
|
|
|
entry: &PxarEntry,
|
2019-07-04 14:15:54 +00:00
|
|
|
filename: &OsStr
|
|
|
|
) -> Result<(), Error> {
|
|
|
|
let mut read_buffer: [u8; 64*1024] = unsafe { std::mem::uninitialized() };
|
2019-08-01 11:08:08 +00:00
|
|
|
let (head, attr) = self.read_attributes()
|
|
|
|
.map_err(|err| format_err!("Reading of file attributes failed - {}", err))?;
|
2019-07-04 14:15:54 +00:00
|
|
|
|
2019-07-16 16:19:43 +00:00
|
|
|
if let Some(fd) = parent_fd {
|
|
|
|
let flags = OFlag::O_CREAT|OFlag::O_WRONLY|OFlag::O_EXCL;
|
|
|
|
let open_mode = Mode::from_bits_truncate(0o0600 | entry.mode as u32); //fixme: upper 32bits of entry.mode?
|
|
|
|
let mut file = file_openat(fd, filename, flags, open_mode)
|
|
|
|
.map_err(|err| format_err!("open file {:?} failed - {}", full_path, err))?;
|
2019-07-04 14:15:54 +00:00
|
|
|
|
2019-08-02 13:19:34 +00:00
|
|
|
if head.htype != PXAR_PAYLOAD {
|
2019-07-16 16:19:43 +00:00
|
|
|
bail!("got unknown header type for file entry {:016x}", head.htype);
|
|
|
|
}
|
2019-07-04 14:15:54 +00:00
|
|
|
|
2019-07-16 16:19:43 +00:00
|
|
|
if head.size < HEADER_SIZE {
|
|
|
|
bail!("detected short payload");
|
|
|
|
}
|
|
|
|
let need = (head.size - HEADER_SIZE) as usize;
|
|
|
|
|
|
|
|
let mut done = 0;
|
|
|
|
while done < need {
|
|
|
|
let todo = need - done;
|
|
|
|
let n = if todo > read_buffer.len() { read_buffer.len() } else { todo };
|
|
|
|
let data = &mut read_buffer[..n];
|
|
|
|
self.reader.read_exact(data)?;
|
|
|
|
file.write_all(data)?;
|
|
|
|
done += n;
|
|
|
|
}
|
2019-07-04 14:15:54 +00:00
|
|
|
|
2019-08-01 11:08:08 +00:00
|
|
|
self.restore_ugid(&entry, file.as_raw_fd())?;
|
|
|
|
// fcaps have to be restored after restore_ugid as chown clears security.capability xattr, see CVE-2015-1350
|
|
|
|
self.restore_attributes(file.as_raw_fd(), &attr, &entry)?;
|
2019-07-16 16:19:43 +00:00
|
|
|
self.restore_mode(&entry, file.as_raw_fd())?;
|
|
|
|
self.restore_mtime(&entry, file.as_raw_fd())?;
|
|
|
|
} else {
|
2019-08-02 13:19:34 +00:00
|
|
|
if head.htype != PXAR_PAYLOAD {
|
2019-07-16 16:19:43 +00:00
|
|
|
bail!("got unknown header type for file entry {:016x}", head.htype);
|
|
|
|
}
|
|
|
|
if head.size < HEADER_SIZE {
|
|
|
|
bail!("detected short payload");
|
|
|
|
}
|
|
|
|
self.skip_bytes((head.size - HEADER_SIZE) as usize)?;
|
2019-07-04 14:15:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2019-08-01 14:23:48 +00:00
|
|
|
fn restore_dir(
|
2019-07-04 14:15:54 +00:00
|
|
|
&mut self,
|
|
|
|
base_path: &Path,
|
2019-08-02 13:19:36 +00:00
|
|
|
dirs: &mut PxarDirStack,
|
2019-08-02 13:19:34 +00:00
|
|
|
entry: PxarEntry,
|
2019-07-04 14:15:54 +00:00
|
|
|
filename: &OsStr,
|
2019-08-01 14:23:48 +00:00
|
|
|
matched: MatchType,
|
2019-08-02 15:02:24 +00:00
|
|
|
match_pattern: &Vec<MatchPattern>,
|
2019-07-04 14:15:54 +00:00
|
|
|
) -> Result<(), Error> {
|
2019-08-01 11:08:08 +00:00
|
|
|
let (mut head, attr) = self.read_attributes()
|
|
|
|
.map_err(|err| format_err!("Reading of directory attributes failed - {}", err))?;
|
|
|
|
|
2019-08-01 14:23:48 +00:00
|
|
|
let dir = PxarDir::new(filename, entry, attr);
|
|
|
|
dirs.push(dir);
|
2019-08-02 15:02:24 +00:00
|
|
|
if matched == MatchType::Positive {
|
2019-08-01 14:23:48 +00:00
|
|
|
dirs.create_all_dirs(!self.allow_existing_dirs)?;
|
|
|
|
}
|
2019-07-04 14:15:54 +00:00
|
|
|
|
2019-08-02 13:19:34 +00:00
|
|
|
while head.htype == PXAR_FILENAME {
|
2019-07-04 14:15:54 +00:00
|
|
|
let name = self.read_filename(head.size)?;
|
2019-08-01 14:23:48 +00:00
|
|
|
self.restore_dir_entry(base_path, dirs, &name, matched, match_pattern)?;
|
2019-07-04 14:15:54 +00:00
|
|
|
head = self.read_item()?;
|
|
|
|
}
|
|
|
|
|
2019-08-02 13:19:34 +00:00
|
|
|
if head.htype != PXAR_GOODBYE {
|
2019-07-04 14:15:54 +00:00
|
|
|
bail!("got unknown header type inside directory entry {:016x}", head.htype);
|
|
|
|
}
|
|
|
|
|
|
|
|
if head.size < HEADER_SIZE { bail!("detected short goodbye table"); }
|
|
|
|
self.skip_bytes((head.size - HEADER_SIZE) as usize)?;
|
2019-07-16 16:19:43 +00:00
|
|
|
|
2019-08-01 14:23:48 +00:00
|
|
|
let last = dirs.pop()
|
|
|
|
.ok_or_else(|| format_err!("Tried to pop beyond dir root - this should not happen!"))?;
|
|
|
|
if let Some(d) = last.dir {
|
|
|
|
let fd = d.as_raw_fd();
|
|
|
|
self.restore_ugid(&last.entry, fd)?;
|
2019-08-01 11:08:08 +00:00
|
|
|
// fcaps have to be restored after restore_ugid as chown clears security.capability xattr, see CVE-2015-1350
|
2019-08-01 14:23:48 +00:00
|
|
|
self.restore_attributes(fd, &last.attr, &last.entry)?;
|
|
|
|
self.restore_mode(&last.entry, fd)?;
|
|
|
|
self.restore_mtime(&last.entry, fd)?;
|
2019-07-16 16:19:43 +00:00
|
|
|
}
|
2019-07-04 14:15:54 +00:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2019-03-15 06:12:11 +00:00
|
|
|
/// Restore an archive into the specified directory.
|
|
|
|
///
|
|
|
|
/// The directory is created if it does not exist.
|
2019-08-01 14:23:48 +00:00
|
|
|
pub fn restore(
|
|
|
|
&mut self,
|
|
|
|
path: &Path,
|
2019-08-02 15:02:24 +00:00
|
|
|
match_pattern: &Vec<MatchPattern>
|
2019-08-01 14:23:48 +00:00
|
|
|
) -> Result<(), Error> {
|
2019-03-11 13:31:01 +00:00
|
|
|
|
|
|
|
let _ = std::fs::create_dir(path);
|
|
|
|
|
2019-07-01 15:24:23 +00:00
|
|
|
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))?;
|
2019-08-01 14:23:48 +00:00
|
|
|
let fd = dir.as_raw_fd();
|
2019-08-02 13:19:36 +00:00
|
|
|
let mut dirs = PxarDirStack::new(fd);
|
2019-08-01 14:23:48 +00:00
|
|
|
// An empty match pattern list indicates to restore the full archive.
|
|
|
|
let matched = if match_pattern.len() == 0 {
|
2019-08-02 15:02:24 +00:00
|
|
|
MatchType::Positive
|
2019-08-01 14:23:48 +00:00
|
|
|
} else {
|
|
|
|
MatchType::None
|
|
|
|
};
|
|
|
|
|
2019-08-02 13:19:34 +00:00
|
|
|
let header: PxarHeader = self.read_item()?;
|
|
|
|
check_ca_header::<PxarEntry>(&header, PXAR_ENTRY)?;
|
|
|
|
let entry: PxarEntry = self.read_item()?;
|
2019-08-01 14:23:48 +00:00
|
|
|
|
|
|
|
let (mut head, attr) = self.read_attributes()
|
|
|
|
.map_err(|err| format_err!("Reading of directory attributes failed - {}", err))?;
|
2019-03-11 13:31:01 +00:00
|
|
|
|
2019-08-02 13:19:34 +00:00
|
|
|
while head.htype == PXAR_FILENAME {
|
2019-08-01 14:23:48 +00:00
|
|
|
let name = self.read_filename(head.size)?;
|
|
|
|
self.restore_dir_entry(path, &mut dirs, &name, matched, match_pattern)?;
|
|
|
|
head = self.read_item()?;
|
|
|
|
}
|
|
|
|
|
2019-08-02 13:19:34 +00:00
|
|
|
if head.htype != PXAR_GOODBYE {
|
2019-08-01 14:23:48 +00:00
|
|
|
bail!("got unknown header type inside directory entry {:016x}", head.htype);
|
|
|
|
}
|
|
|
|
|
|
|
|
if head.size < HEADER_SIZE { bail!("detected short goodbye table"); }
|
|
|
|
self.skip_bytes((head.size - HEADER_SIZE) as usize)?;
|
|
|
|
|
|
|
|
self.restore_ugid(&entry, fd)?;
|
|
|
|
// fcaps have to be restored after restore_ugid as chown clears security.capability xattr, see CVE-2015-1350
|
|
|
|
self.restore_attributes(fd, &attr, &entry)?;
|
|
|
|
self.restore_mode(&entry, fd)?;
|
|
|
|
self.restore_mtime(&entry, fd)?;
|
|
|
|
|
|
|
|
Ok(())
|
2019-03-11 13:31:01 +00:00
|
|
|
}
|
|
|
|
|
2019-08-01 14:23:48 +00:00
|
|
|
fn restore_dir_entry(
|
2019-01-07 18:07:03 +00:00
|
|
|
&mut self,
|
2019-03-16 10:02:12 +00:00
|
|
|
base_path: &Path,
|
2019-08-02 13:19:36 +00:00
|
|
|
dirs: &mut PxarDirStack,
|
2019-08-01 14:23:48 +00:00
|
|
|
filename: &OsStr,
|
|
|
|
parent_matched: MatchType,
|
2019-08-02 15:02:24 +00:00
|
|
|
match_pattern: &Vec<MatchPattern>,
|
2019-07-04 12:03:20 +00:00
|
|
|
) -> Result<(), Error> {
|
2019-08-01 14:23:48 +00:00
|
|
|
let relative_path = dirs.as_path_buf();
|
|
|
|
let full_path = base_path.join(&relative_path).join(filename);
|
2019-03-16 10:02:12 +00:00
|
|
|
|
2019-08-02 13:19:34 +00:00
|
|
|
let head: PxarHeader = self.read_item()?;
|
2019-03-16 10:02:12 +00:00
|
|
|
if head.htype == PXAR_FORMAT_HARDLINK {
|
|
|
|
let (target, _offset) = self.read_hardlink(head.size)?;
|
|
|
|
let target_path = base_path.join(&target);
|
2019-08-01 14:23:48 +00:00
|
|
|
if dirs.last_dir_fd().is_some() {
|
2019-07-19 08:55:28 +00:00
|
|
|
(self.callback)(&full_path)?;
|
2019-07-16 16:19:43 +00:00
|
|
|
hardlink(&target_path, &full_path)?;
|
|
|
|
}
|
2019-03-16 10:02:12 +00:00
|
|
|
return Ok(());
|
|
|
|
}
|
|
|
|
|
2019-08-02 13:19:34 +00:00
|
|
|
check_ca_header::<PxarEntry>(&head, PXAR_ENTRY)?;
|
|
|
|
let entry: PxarEntry = self.read_item()?;
|
2019-07-16 16:19:43 +00:00
|
|
|
|
|
|
|
let mut child_pattern = Vec::new();
|
2019-08-01 14:23:48 +00:00
|
|
|
// If parent was a match, then children should be assumed to match too
|
|
|
|
// This is especially the case when the full archive is restored and
|
|
|
|
// there are no match pattern.
|
|
|
|
let mut matched = parent_matched;
|
2019-07-16 16:19:43 +00:00
|
|
|
if match_pattern.len() > 0 {
|
2019-08-05 12:28:03 +00:00
|
|
|
match match_filename(filename, entry.mode as u32 & libc::S_IFMT == libc::S_IFDIR, match_pattern)? {
|
2019-08-01 14:23:48 +00:00
|
|
|
(MatchType::None, _) => matched = MatchType::None,
|
2019-08-02 15:02:24 +00:00
|
|
|
(MatchType::Negative, _) => matched = MatchType::Negative,
|
|
|
|
(match_type, pattern) => {
|
|
|
|
matched = match_type;
|
2019-08-01 14:23:48 +00:00
|
|
|
child_pattern = pattern;
|
|
|
|
},
|
2019-07-16 16:19:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-02 15:02:24 +00:00
|
|
|
let fd = if matched == MatchType::Positive {
|
2019-08-01 14:23:48 +00:00
|
|
|
Some(dirs.create_all_dirs(!self.allow_existing_dirs)?)
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
};
|
|
|
|
|
2019-07-19 08:55:28 +00:00
|
|
|
if fd.is_some() {
|
|
|
|
(self.callback)(&full_path)?;
|
|
|
|
}
|
|
|
|
|
2019-07-16 11:19:50 +00:00
|
|
|
match entry.mode as u32 & libc::S_IFMT {
|
2019-08-01 14:23:48 +00:00
|
|
|
libc::S_IFDIR => self.restore_dir(base_path, dirs, entry, &filename, matched, &child_pattern),
|
2019-07-16 16:19:43 +00:00
|
|
|
libc::S_IFLNK => self.restore_symlink(fd, &full_path, &entry, &filename),
|
|
|
|
libc::S_IFSOCK => self.restore_socket(fd, &entry, &filename),
|
|
|
|
libc::S_IFIFO => self.restore_fifo(fd, &entry, &filename),
|
|
|
|
libc::S_IFBLK | libc::S_IFCHR => self.restore_device(fd, &entry, &filename),
|
|
|
|
libc::S_IFREG => self.restore_regular_file(fd, &full_path, &entry, &filename),
|
2019-07-04 14:15:54 +00:00
|
|
|
_ => Ok(()),
|
2019-01-06 16:27:57 +00:00
|
|
|
}
|
2019-01-07 18:07:03 +00:00
|
|
|
}
|
2019-03-14 12:10:27 +00:00
|
|
|
|
2019-03-14 16:43:11 +00:00
|
|
|
/// List/Dump archive content.
|
|
|
|
///
|
|
|
|
/// Simply print the list of contained files. This dumps archive
|
|
|
|
/// format details when the verbose flag is set (useful for debug).
|
|
|
|
pub fn dump_entry<W: std::io::Write>(
|
2019-03-14 12:10:27 +00:00
|
|
|
&mut self,
|
2019-03-14 16:43:11 +00:00
|
|
|
path: &mut PathBuf,
|
|
|
|
verbose: bool,
|
2019-03-14 12:10:27 +00:00
|
|
|
output: &mut W,
|
|
|
|
) -> Result<(), Error> {
|
|
|
|
|
2019-08-02 13:19:34 +00:00
|
|
|
let print_head = |head: &PxarHeader| {
|
2019-03-14 16:43:11 +00:00
|
|
|
println!("Type: {:016x}", head.htype);
|
|
|
|
println!("Size: {}", head.size);
|
|
|
|
};
|
2019-03-14 12:10:27 +00:00
|
|
|
|
2019-08-02 13:19:34 +00:00
|
|
|
let head: PxarHeader = self.read_item()?;
|
2019-03-14 16:43:11 +00:00
|
|
|
if verbose {
|
|
|
|
println!("Path: {:?}", path);
|
|
|
|
print_head(&head);
|
|
|
|
} else {
|
|
|
|
println!("{:?}", path);
|
|
|
|
}
|
|
|
|
|
2019-03-16 10:02:12 +00:00
|
|
|
if head.htype == PXAR_FORMAT_HARDLINK {
|
|
|
|
let (target, offset) = self.read_hardlink(head.size)?;
|
|
|
|
if verbose {
|
|
|
|
println!("Hardlink: {} {:?}", offset, target);
|
|
|
|
}
|
|
|
|
return Ok(());
|
|
|
|
}
|
|
|
|
|
2019-08-02 13:19:34 +00:00
|
|
|
check_ca_header::<PxarEntry>(&head, PXAR_ENTRY)?;
|
|
|
|
let entry: PxarEntry = self.read_item()?;
|
2019-03-14 12:10:27 +00:00
|
|
|
|
2019-03-14 16:43:11 +00:00
|
|
|
if verbose {
|
|
|
|
println!("Mode: {:08x} {:08x}", entry.mode, (entry.mode as u32) & libc::S_IFDIR);
|
|
|
|
}
|
2019-03-14 12:10:27 +00:00
|
|
|
|
2019-03-14 16:43:11 +00:00
|
|
|
let ifmt = (entry.mode as u32) & libc::S_IFMT;
|
2019-03-14 12:10:27 +00:00
|
|
|
|
2019-03-14 16:43:11 +00:00
|
|
|
if ifmt == libc::S_IFDIR {
|
2019-03-14 12:10:27 +00:00
|
|
|
|
2019-03-14 16:43:11 +00:00
|
|
|
let mut entry_count = 0;
|
|
|
|
|
|
|
|
loop {
|
2019-08-02 13:19:34 +00:00
|
|
|
let head: PxarHeader = self.read_item()?;
|
2019-03-14 16:43:11 +00:00
|
|
|
if verbose {
|
|
|
|
print_head(&head);
|
2019-03-14 12:10:27 +00:00
|
|
|
}
|
2019-05-23 16:09:15 +00:00
|
|
|
|
|
|
|
// This call covers all the cases of the match statement
|
|
|
|
// regarding extended attributes. These calls will never
|
|
|
|
// break on the loop and can therefore be handled separately.
|
|
|
|
// If the header was matched, true is returned and we can continue
|
|
|
|
if self.dump_if_attribute(&head, verbose)? {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2019-03-14 16:43:11 +00:00
|
|
|
match head.htype {
|
2019-08-02 13:19:34 +00:00
|
|
|
PXAR_FILENAME => {
|
2019-03-14 16:43:11 +00:00
|
|
|
let name = self.read_filename(head.size)?;
|
|
|
|
if verbose { println!("Name: {:?}", name); }
|
|
|
|
entry_count += 1;
|
|
|
|
path.push(&name);
|
|
|
|
self.dump_entry(path, verbose, output)?;
|
|
|
|
path.pop();
|
2019-05-23 16:09:15 +00:00
|
|
|
},
|
2019-08-02 13:19:34 +00:00
|
|
|
PXAR_GOODBYE => {
|
2019-03-14 16:43:11 +00:00
|
|
|
let table_size = (head.size - HEADER_SIZE) as usize;
|
|
|
|
if verbose {
|
|
|
|
println!("Goodbye: {:?}", path);
|
|
|
|
self.dump_goodby_entries(entry_count, table_size)?;
|
|
|
|
} else {
|
2019-03-14 16:45:47 +00:00
|
|
|
self.skip_bytes(table_size)?;
|
2019-03-14 16:43:11 +00:00
|
|
|
}
|
2019-03-14 12:10:27 +00:00
|
|
|
break;
|
2019-05-23 16:09:15 +00:00
|
|
|
},
|
|
|
|
_ => panic!("got unexpected header type inside directory"),
|
2019-03-14 12:10:27 +00:00
|
|
|
}
|
2019-03-14 16:43:11 +00:00
|
|
|
}
|
2019-03-15 11:15:38 +00:00
|
|
|
} else if (ifmt == libc::S_IFBLK) || (ifmt == libc::S_IFCHR) ||
|
|
|
|
(ifmt == libc::S_IFLNK) || (ifmt == libc::S_IFREG)
|
|
|
|
{
|
2019-05-17 12:23:35 +00:00
|
|
|
loop {
|
2019-08-02 13:19:34 +00:00
|
|
|
let head: PxarHeader = self.read_item()?;
|
2019-05-17 12:23:35 +00:00
|
|
|
if verbose {
|
|
|
|
print_head(&head);
|
|
|
|
}
|
2019-03-14 16:43:11 +00:00
|
|
|
|
2019-05-23 16:09:15 +00:00
|
|
|
// This call covers all the cases of the match statement
|
|
|
|
// regarding extended attributes. These calls will never
|
|
|
|
// break on the loop and can therefore be handled separately.
|
|
|
|
// If the header was matched, true is returned and we can continue
|
|
|
|
if self.dump_if_attribute(&head, verbose)? {
|
|
|
|
continue;
|
|
|
|
}
|
2019-03-14 16:43:11 +00:00
|
|
|
|
2019-05-23 16:09:15 +00:00
|
|
|
match head.htype {
|
2019-08-02 13:19:34 +00:00
|
|
|
PXAR_SYMLINK => {
|
2019-05-17 12:23:35 +00:00
|
|
|
let target = self.read_link(head.size)?;
|
|
|
|
if verbose {
|
|
|
|
println!("Symlink: {:?}", target);
|
|
|
|
}
|
|
|
|
break;
|
2019-05-23 16:09:15 +00:00
|
|
|
},
|
2019-08-02 13:19:34 +00:00
|
|
|
PXAR_DEVICE => {
|
|
|
|
let device: PxarDevice = self.read_item()?;
|
2019-05-17 12:23:35 +00:00
|
|
|
if verbose {
|
|
|
|
println!("Device: {}, {}", device.major, device.minor);
|
|
|
|
}
|
|
|
|
break;
|
2019-05-23 16:09:15 +00:00
|
|
|
},
|
2019-08-02 13:19:34 +00:00
|
|
|
PXAR_PAYLOAD => {
|
2019-05-17 12:23:35 +00:00
|
|
|
let payload_size = (head.size - HEADER_SIZE) as usize;
|
|
|
|
if verbose {
|
|
|
|
println!("Payload: {}", payload_size);
|
|
|
|
}
|
|
|
|
self.skip_bytes(payload_size)?;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
_ => {
|
|
|
|
panic!("got unexpected header type inside non-directory");
|
2019-03-14 16:43:11 +00:00
|
|
|
}
|
2019-03-14 12:10:27 +00:00
|
|
|
}
|
|
|
|
}
|
2019-03-15 11:15:38 +00:00
|
|
|
} else if ifmt == libc::S_IFIFO {
|
|
|
|
if verbose {
|
|
|
|
println!("Fifo:");
|
|
|
|
}
|
|
|
|
} else if ifmt == libc::S_IFSOCK {
|
|
|
|
if verbose {
|
|
|
|
println!("Socket:");
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
panic!("unknown st_mode");
|
2019-03-14 12:10:27 +00:00
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
2019-03-14 16:43:11 +00:00
|
|
|
|
2019-08-02 13:19:34 +00:00
|
|
|
fn dump_if_attribute(&mut self, header: &PxarHeader, verbose: bool) -> Result<bool, Error> {
|
2019-05-28 09:16:21 +00:00
|
|
|
match header.htype {
|
2019-08-02 13:19:34 +00:00
|
|
|
PXAR_XATTR => {
|
2019-05-28 09:16:21 +00:00
|
|
|
let xattr = self.read_xattr((header.size - HEADER_SIZE) as usize)?;
|
2019-08-02 13:19:33 +00:00
|
|
|
if verbose && self.has_features(flags::WITH_XATTRS) {
|
2019-05-28 09:16:21 +00:00
|
|
|
println!("XAttr: {:?}", xattr);
|
|
|
|
}
|
|
|
|
},
|
2019-08-02 13:19:34 +00:00
|
|
|
PXAR_FCAPS => {
|
2019-05-28 09:16:21 +00:00
|
|
|
let fcaps = self.read_fcaps((header.size - HEADER_SIZE) as usize)?;
|
2019-08-02 13:19:33 +00:00
|
|
|
if verbose && self.has_features(flags::WITH_FCAPS) {
|
2019-05-28 09:16:21 +00:00
|
|
|
println!("FCaps: {:?}", fcaps);
|
|
|
|
}
|
|
|
|
},
|
2019-08-02 13:19:34 +00:00
|
|
|
PXAR_ACL_USER => {
|
|
|
|
let user = self.read_item::<PxarACLUser>()?;
|
2019-08-02 13:19:33 +00:00
|
|
|
if verbose && self.has_features(flags::WITH_ACL) {
|
2019-05-28 09:16:21 +00:00
|
|
|
println!("ACLUser: {:?}", user);
|
|
|
|
}
|
|
|
|
},
|
2019-08-02 13:19:34 +00:00
|
|
|
PXAR_ACL_GROUP => {
|
|
|
|
let group = self.read_item::<PxarACLGroup>()?;
|
2019-08-02 13:19:33 +00:00
|
|
|
if verbose && self.has_features(flags::WITH_ACL) {
|
2019-05-28 09:16:21 +00:00
|
|
|
println!("ACLGroup: {:?}", group);
|
|
|
|
}
|
|
|
|
},
|
2019-08-02 13:19:34 +00:00
|
|
|
PXAR_ACL_GROUP_OBJ => {
|
|
|
|
let group_obj = self.read_item::<PxarACLGroupObj>()?;
|
2019-08-02 13:19:33 +00:00
|
|
|
if verbose && self.has_features(flags::WITH_ACL) {
|
2019-05-28 09:16:21 +00:00
|
|
|
println!("ACLGroupObj: {:?}", group_obj);
|
|
|
|
}
|
|
|
|
},
|
2019-08-02 13:19:34 +00:00
|
|
|
PXAR_ACL_DEFAULT => {
|
|
|
|
let default = self.read_item::<PxarACLDefault>()?;
|
2019-08-02 13:19:33 +00:00
|
|
|
if verbose && self.has_features(flags::WITH_ACL) {
|
2019-05-28 09:16:21 +00:00
|
|
|
println!("ACLDefault: {:?}", default);
|
|
|
|
}
|
|
|
|
},
|
2019-08-02 13:19:34 +00:00
|
|
|
PXAR_ACL_DEFAULT_USER => {
|
|
|
|
let default_user = self.read_item::<PxarACLUser>()?;
|
2019-08-02 13:19:33 +00:00
|
|
|
if verbose && self.has_features(flags::WITH_ACL) {
|
2019-05-28 09:16:21 +00:00
|
|
|
println!("ACLDefaultUser: {:?}", default_user);
|
|
|
|
}
|
|
|
|
},
|
2019-08-02 13:19:34 +00:00
|
|
|
PXAR_ACL_DEFAULT_GROUP => {
|
|
|
|
let default_group = self.read_item::<PxarACLGroup>()?;
|
2019-08-02 13:19:33 +00:00
|
|
|
if verbose && self.has_features(flags::WITH_ACL) {
|
2019-05-28 09:16:21 +00:00
|
|
|
println!("ACLDefaultGroup: {:?}", default_group);
|
|
|
|
}
|
|
|
|
},
|
2019-08-02 13:19:34 +00:00
|
|
|
PXAR_QUOTA_PROJID => {
|
|
|
|
let quota_projid = self.read_item::<PxarQuotaProjID>()?;
|
2019-08-02 13:19:33 +00:00
|
|
|
if verbose && self.has_features(flags::WITH_QUOTA_PROJID) {
|
2019-05-29 12:34:05 +00:00
|
|
|
println!("Quota project id: {:?}", quota_projid);
|
|
|
|
}
|
|
|
|
},
|
2019-05-23 16:09:15 +00:00
|
|
|
_ => return Ok(false),
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(true)
|
|
|
|
}
|
|
|
|
|
2019-03-14 16:43:11 +00:00
|
|
|
fn dump_goodby_entries(
|
|
|
|
&mut self,
|
|
|
|
entry_count: usize,
|
|
|
|
table_size: usize,
|
|
|
|
) -> Result<(), Error> {
|
|
|
|
|
2019-08-02 13:19:34 +00:00
|
|
|
const GOODBYE_ITEM_SIZE: usize = std::mem::size_of::<PxarGoodbyeItem>();
|
2019-03-15 09:18:28 +00:00
|
|
|
|
|
|
|
if table_size < GOODBYE_ITEM_SIZE {
|
|
|
|
bail!("Goodbye table to small ({} < {})", table_size, GOODBYE_ITEM_SIZE);
|
2019-03-14 16:43:11 +00:00
|
|
|
}
|
2019-03-15 09:18:28 +00:00
|
|
|
if (table_size % GOODBYE_ITEM_SIZE) != 0 {
|
2019-03-14 16:43:11 +00:00
|
|
|
bail!("Goodbye table with strange size ({})", table_size);
|
|
|
|
}
|
|
|
|
|
2019-03-15 09:18:28 +00:00
|
|
|
let entries = table_size / GOODBYE_ITEM_SIZE;
|
2019-03-14 16:43:11 +00:00
|
|
|
|
|
|
|
if entry_count != (entries - 1) {
|
|
|
|
bail!("Goodbye table with wrong entry count ({} != {})", entry_count, entries - 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut count = 0;
|
|
|
|
|
|
|
|
loop {
|
2019-08-02 13:19:34 +00:00
|
|
|
let item: PxarGoodbyeItem = self.read_item()?;
|
2019-03-14 16:43:11 +00:00
|
|
|
count += 1;
|
2019-08-02 13:19:34 +00:00
|
|
|
if item.hash == PXAR_GOODBYE_TAIL_MARKER {
|
2019-03-14 16:43:11 +00:00
|
|
|
if count != entries {
|
|
|
|
bail!("unexpected goodbye tail marker");
|
|
|
|
}
|
|
|
|
println!("Goodby tail mark.");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
println!("Goodby item: offset {}, size {}, hash {:016x}", item.offset, item.size, item.hash);
|
2019-03-15 09:18:28 +00:00
|
|
|
if count >= entries {
|
2019-03-14 16:43:11 +00:00
|
|
|
bail!("too many goodbye items (no tail marker)");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
2019-01-06 16:27:57 +00:00
|
|
|
}
|
2019-01-08 16:17:55 +00:00
|
|
|
|
2019-07-16 16:19:42 +00:00
|
|
|
fn match_filename(
|
|
|
|
filename: &OsStr,
|
|
|
|
is_dir: bool,
|
2019-08-02 15:02:24 +00:00
|
|
|
match_pattern: &Vec<MatchPattern>
|
2019-08-05 12:28:03 +00:00
|
|
|
) -> Result<(MatchType, Vec<MatchPattern>), Error> {
|
2019-07-16 16:19:42 +00:00
|
|
|
let mut child_pattern = Vec::new();
|
|
|
|
let mut match_state = MatchType::None;
|
|
|
|
// read_filename() checks for nul bytes, so it is save to unwrap here
|
|
|
|
let name = CString::new(filename.as_bytes()).unwrap();
|
|
|
|
|
|
|
|
for pattern in match_pattern {
|
2019-08-05 12:28:03 +00:00
|
|
|
match pattern.matches_filename(&name, is_dir)? {
|
2019-07-16 16:19:42 +00:00
|
|
|
MatchType::None => {},
|
2019-08-02 15:02:24 +00:00
|
|
|
MatchType::Positive => {
|
|
|
|
match_state = MatchType::Positive;
|
|
|
|
let incl_pattern = MatchPattern::from_line(b"**/*").unwrap().unwrap();
|
2019-07-16 16:19:42 +00:00
|
|
|
child_pattern.push(incl_pattern.get_rest_pattern());
|
|
|
|
},
|
2019-08-02 15:02:24 +00:00
|
|
|
MatchType::Negative => match_state = MatchType::Negative,
|
|
|
|
MatchType::PartialPositive => {
|
|
|
|
if match_state != MatchType::Negative && match_state != MatchType::Positive {
|
|
|
|
match_state = MatchType::PartialPositive;
|
2019-07-16 16:19:42 +00:00
|
|
|
}
|
|
|
|
child_pattern.push(pattern.get_rest_pattern());
|
|
|
|
},
|
2019-08-02 15:02:24 +00:00
|
|
|
MatchType::PartialNegative => {
|
|
|
|
if match_state == MatchType::PartialPositive {
|
|
|
|
match_state = MatchType::PartialNegative;
|
2019-07-16 16:19:42 +00:00
|
|
|
}
|
|
|
|
child_pattern.push(pattern.get_rest_pattern());
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-05 12:28:03 +00:00
|
|
|
Ok((match_state, child_pattern))
|
2019-07-16 16:19:42 +00:00
|
|
|
}
|
|
|
|
|
2019-01-08 16:17:55 +00:00
|
|
|
fn file_openat(parent: RawFd, filename: &OsStr, flags: OFlag, mode: Mode) -> Result<std::fs::File, Error> {
|
|
|
|
|
2019-01-11 07:41:33 +00:00
|
|
|
let fd = filename.with_nix_path(|cstr| {
|
2019-01-08 16:17:55 +00:00
|
|
|
nix::fcntl::openat(parent, cstr.as_ref(), flags, mode)
|
|
|
|
})??;
|
|
|
|
|
|
|
|
let file = unsafe { std::fs::File::from_raw_fd(fd) };
|
|
|
|
|
|
|
|
Ok(file)
|
|
|
|
}
|
|
|
|
|
2019-03-16 10:02:12 +00:00
|
|
|
fn hardlink(oldpath: &Path, newpath: &Path) -> Result<(), Error> {
|
|
|
|
oldpath.with_nix_path(|oldpath| {
|
|
|
|
newpath.with_nix_path(|newpath| {
|
|
|
|
let res = unsafe { libc::link(oldpath.as_ptr(), newpath.as_ptr()) };
|
|
|
|
Errno::result(res)?;
|
|
|
|
Ok(())
|
|
|
|
})?
|
|
|
|
})?
|
|
|
|
}
|
|
|
|
|
2019-01-08 16:17:55 +00:00
|
|
|
fn symlinkat(target: &Path, parent: RawFd, linkname: &OsStr) -> Result<(), Error> {
|
|
|
|
|
|
|
|
target.with_nix_path(|target| {
|
|
|
|
linkname.with_nix_path(|linkname| {
|
|
|
|
let res = unsafe { libc::symlinkat(target.as_ptr(), parent, linkname.as_ptr()) };
|
|
|
|
Errno::result(res)?;
|
|
|
|
Ok(())
|
|
|
|
})?
|
|
|
|
})?
|
|
|
|
}
|
2019-01-09 13:44:00 +00:00
|
|
|
|
|
|
|
fn nsec_to_update_timespec(mtime_nsec: u64) -> [libc::timespec; 2] {
|
|
|
|
|
|
|
|
// restore mtime
|
|
|
|
const UTIME_OMIT: i64 = ((1 << 30) - 2);
|
|
|
|
const NANOS_PER_SEC: i64 = 1_000_000_000;
|
|
|
|
|
|
|
|
let sec = (mtime_nsec as i64) / NANOS_PER_SEC;
|
|
|
|
let nsec = (mtime_nsec as i64) % NANOS_PER_SEC;
|
|
|
|
|
|
|
|
let times: [libc::timespec; 2] = [
|
|
|
|
libc::timespec { tv_sec: 0, tv_nsec: UTIME_OMIT },
|
|
|
|
libc::timespec { tv_sec: sec, tv_nsec: nsec },
|
|
|
|
];
|
|
|
|
|
|
|
|
times
|
|
|
|
}
|
2019-05-23 16:09:15 +00:00
|
|
|
|
|
|
|
fn mode_user_to_acl_permissions(mode: u64) -> u64 {
|
|
|
|
return (mode >> 6) & 7;
|
|
|
|
}
|
|
|
|
|
|
|
|
fn mode_group_to_acl_permissions(mode: u64) -> u64 {
|
|
|
|
return (mode >> 3) & 7;
|
|
|
|
}
|
|
|
|
|
|
|
|
fn mode_other_to_acl_permissions(mode: u64) -> u64 {
|
|
|
|
return mode & 7;
|
|
|
|
}
|