2019-03-14 09:54:09 +00:00
|
|
|
//! *pxar* format encoder.
|
2018-12-30 12:47:27 +00:00
|
|
|
//!
|
2019-03-14 09:54:09 +00:00
|
|
|
//! This module contain the code to generate *pxar* archive files.
|
2019-08-22 10:49:07 +00:00
|
|
|
use std::collections::{HashMap, HashSet};
|
2019-10-22 13:07:51 +00:00
|
|
|
use std::ffi::{CStr, CString};
|
2018-12-27 12:15:10 +00:00
|
|
|
use std::io::Write;
|
|
|
|
use std::os::unix::ffi::OsStrExt;
|
2019-08-22 10:49:07 +00:00
|
|
|
use std::os::unix::io::AsRawFd;
|
2018-12-27 13:24:31 +00:00
|
|
|
use std::os::unix::io::RawFd;
|
2018-12-28 06:45:15 +00:00
|
|
|
use std::path::{Path, PathBuf};
|
2018-12-27 13:24:31 +00:00
|
|
|
|
2019-08-22 10:49:07 +00:00
|
|
|
use endian_trait::Endian;
|
2020-04-17 12:11:25 +00:00
|
|
|
use anyhow::{bail, format_err, Error};
|
2018-12-28 06:45:15 +00:00
|
|
|
use nix::errno::Errno;
|
2019-08-22 10:49:07 +00:00
|
|
|
use nix::fcntl::OFlag;
|
2018-12-28 08:55:26 +00:00
|
|
|
use nix::sys::stat::FileStat;
|
2019-08-22 10:49:07 +00:00
|
|
|
use nix::sys::stat::Mode;
|
|
|
|
use nix::NixPath;
|
2018-12-27 12:15:10 +00:00
|
|
|
|
2019-07-01 09:03:25 +00:00
|
|
|
use proxmox::tools::vec;
|
2019-05-22 13:02:16 +00:00
|
|
|
|
2019-08-22 10:49:07 +00:00
|
|
|
use super::binary_search_tree::*;
|
|
|
|
use super::catalog::BackupCatalogWriter;
|
|
|
|
use super::flags;
|
|
|
|
use super::format_definition::*;
|
|
|
|
use super::helper::*;
|
2019-12-16 11:13:44 +00:00
|
|
|
use super::match_pattern::{MatchPattern, MatchPatternSlice, MatchType};
|
2019-08-22 10:49:07 +00:00
|
|
|
use crate::tools::acl;
|
|
|
|
use crate::tools::fs;
|
|
|
|
use crate::tools::xattr;
|
|
|
|
|
2019-03-15 11:19:51 +00:00
|
|
|
#[derive(Eq, PartialEq, Hash)]
|
|
|
|
struct HardLinkInfo {
|
|
|
|
st_dev: u64,
|
|
|
|
st_ino: u64,
|
|
|
|
}
|
|
|
|
|
2019-08-09 07:46:49 +00:00
|
|
|
pub struct Encoder<'a, W: Write, C: BackupCatalogWriter> {
|
2019-03-15 11:19:51 +00:00
|
|
|
base_path: PathBuf,
|
|
|
|
relative_path: PathBuf,
|
2019-01-02 10:02:56 +00:00
|
|
|
writer: &'a mut W,
|
2018-12-28 10:48:47 +00:00
|
|
|
writer_pos: usize,
|
2019-08-09 07:46:49 +00:00
|
|
|
catalog: Option<&'a mut C>,
|
2019-01-30 17:25:37 +00:00
|
|
|
_size: usize,
|
2018-12-28 10:48:47 +00:00
|
|
|
file_copy_buffer: Vec<u8>,
|
2019-07-24 05:48:59 +00:00
|
|
|
device_set: Option<HashSet<u64>>,
|
2019-03-04 07:01:09 +00:00
|
|
|
verbose: bool,
|
2019-06-06 11:37:07 +00:00
|
|
|
// Flags set by the user
|
2019-05-22 15:50:38 +00:00
|
|
|
feature_flags: u64,
|
2019-06-06 11:37:07 +00:00
|
|
|
// Flags signaling features supported by the filesystem
|
|
|
|
fs_feature_flags: u64,
|
2019-03-16 10:02:12 +00:00
|
|
|
hardlinks: HashMap<HardLinkInfo, (PathBuf, u64)>,
|
2020-01-10 11:50:06 +00:00
|
|
|
entry_counter: usize,
|
|
|
|
entry_max: usize,
|
2018-12-27 12:15:10 +00:00
|
|
|
}
|
|
|
|
|
2019-08-22 10:49:07 +00:00
|
|
|
impl<'a, W: Write, C: BackupCatalogWriter> Encoder<'a, W, C> {
|
2019-03-15 11:19:51 +00:00
|
|
|
// used for error reporting
|
2019-08-22 10:49:07 +00:00
|
|
|
fn full_path(&self) -> PathBuf {
|
2019-03-15 11:19:51 +00:00
|
|
|
self.base_path.join(&self.relative_path)
|
|
|
|
}
|
|
|
|
|
2019-07-24 05:48:59 +00:00
|
|
|
/// Create archive, write result data to ``writer``.
|
|
|
|
///
|
|
|
|
/// The ``device_set`` can be use used to limit included mount points.
|
|
|
|
///
|
|
|
|
/// - ``None``: include all mount points
|
|
|
|
/// - ``Some(set)``: only include devices listed in this set (the
|
|
|
|
/// root path device is automathically added to this list, so
|
|
|
|
/// you can pass an empty set if you want to archive a single
|
|
|
|
/// mount point.)
|
2019-01-12 14:43:20 +00:00
|
|
|
pub fn encode(
|
|
|
|
path: PathBuf,
|
|
|
|
dir: &mut nix::dir::Dir,
|
2019-03-04 07:01:09 +00:00
|
|
|
writer: &'a mut W,
|
2019-08-09 07:46:49 +00:00
|
|
|
catalog: Option<&'a mut C>,
|
2019-07-24 05:48:59 +00:00
|
|
|
device_set: Option<HashSet<u64>>,
|
2019-03-04 07:01:09 +00:00
|
|
|
verbose: bool,
|
2019-07-24 10:21:25 +00:00
|
|
|
skip_lost_and_found: bool, // fixme: should be a feature flag ??
|
2019-05-23 11:10:20 +00:00
|
|
|
feature_flags: u64,
|
2019-10-22 13:07:51 +00:00
|
|
|
mut excludes: Vec<MatchPattern>,
|
2020-01-10 11:50:06 +00:00
|
|
|
entry_max: usize,
|
2019-01-12 14:43:20 +00:00
|
|
|
) -> Result<(), Error> {
|
2019-08-22 10:49:07 +00:00
|
|
|
const FILE_COPY_BUFFER_SIZE: usize = 1024 * 1024;
|
2018-12-30 13:09:59 +00:00
|
|
|
|
2018-12-28 10:48:47 +00:00
|
|
|
let mut file_copy_buffer = Vec::with_capacity(FILE_COPY_BUFFER_SIZE);
|
2019-08-22 10:49:07 +00:00
|
|
|
unsafe {
|
|
|
|
file_copy_buffer.set_len(FILE_COPY_BUFFER_SIZE);
|
|
|
|
}
|
2018-12-27 12:15:10 +00:00
|
|
|
|
|
|
|
// todo: use scandirat??
|
2019-01-11 08:20:10 +00:00
|
|
|
|
2019-01-12 09:20:08 +00:00
|
|
|
let dir_fd = dir.as_raw_fd();
|
2019-07-01 15:36:08 +00:00
|
|
|
let stat = nix::sys::stat::fstat(dir_fd)
|
|
|
|
.map_err(|err| format_err!("fstat {:?} failed - {}", path, err))?;
|
2019-01-11 08:20:10 +00:00
|
|
|
|
2019-05-27 11:59:17 +00:00
|
|
|
if !is_directory(&stat) {
|
2019-03-08 07:14:26 +00:00
|
|
|
bail!("got unexpected file type {:?} (not a directory)", path);
|
2019-01-11 08:20:10 +00:00
|
|
|
}
|
|
|
|
|
2019-07-24 05:48:59 +00:00
|
|
|
let mut device_set = device_set.clone();
|
|
|
|
if let Some(ref mut set) = device_set {
|
|
|
|
set.insert(stat.st_dev);
|
|
|
|
}
|
|
|
|
|
2019-01-12 09:20:08 +00:00
|
|
|
let magic = detect_fs_type(dir_fd)?;
|
2019-01-12 07:51:04 +00:00
|
|
|
|
2019-01-12 10:56:53 +00:00
|
|
|
if is_virtual_file_system(magic) {
|
|
|
|
bail!("backup virtual file systems is disabled!");
|
|
|
|
}
|
|
|
|
|
2019-08-02 13:19:33 +00:00
|
|
|
let fs_feature_flags = flags::feature_flags_from_magic(magic);
|
2019-06-06 11:37:07 +00:00
|
|
|
|
2019-03-08 07:14:26 +00:00
|
|
|
let mut me = Self {
|
2019-03-15 11:19:51 +00:00
|
|
|
base_path: path,
|
|
|
|
relative_path: PathBuf::new(),
|
2019-09-11 10:06:59 +00:00
|
|
|
writer,
|
2019-03-08 07:14:26 +00:00
|
|
|
writer_pos: 0,
|
2019-08-09 07:46:49 +00:00
|
|
|
catalog,
|
2019-03-08 07:14:26 +00:00
|
|
|
_size: 0,
|
|
|
|
file_copy_buffer,
|
2019-07-24 05:48:59 +00:00
|
|
|
device_set,
|
2019-03-08 07:14:26 +00:00
|
|
|
verbose,
|
2019-05-22 15:50:38 +00:00
|
|
|
feature_flags,
|
2019-06-06 11:37:07 +00:00
|
|
|
fs_feature_flags,
|
2019-03-15 11:19:51 +00:00
|
|
|
hardlinks: HashMap::new(),
|
2020-01-10 11:50:06 +00:00
|
|
|
entry_counter: 0,
|
|
|
|
entry_max,
|
2019-03-08 07:14:26 +00:00
|
|
|
};
|
2019-01-12 14:43:20 +00:00
|
|
|
|
2019-08-22 10:49:07 +00:00
|
|
|
if verbose {
|
|
|
|
println!("{:?}", me.full_path());
|
|
|
|
}
|
2019-03-04 07:01:09 +00:00
|
|
|
|
2019-07-24 10:21:25 +00:00
|
|
|
if skip_lost_and_found {
|
2019-08-02 15:02:24 +00:00
|
|
|
excludes.push(MatchPattern::from_line(b"**/lost+found").unwrap().unwrap());
|
2019-07-24 10:21:25 +00:00
|
|
|
}
|
2019-12-16 11:13:44 +00:00
|
|
|
let mut exclude_slices = Vec::new();
|
|
|
|
for excl in &excludes {
|
|
|
|
exclude_slices.push(excl.as_slice());
|
|
|
|
}
|
2019-10-22 13:07:51 +00:00
|
|
|
|
2019-12-16 11:13:44 +00:00
|
|
|
me.encode_dir(dir, &stat, magic, exclude_slices)?;
|
2018-12-27 12:15:10 +00:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2019-08-22 10:49:07 +00:00
|
|
|
fn write(&mut self, buf: &[u8]) -> Result<(), Error> {
|
2019-01-03 14:47:32 +00:00
|
|
|
self.writer.write_all(buf)?;
|
2018-12-28 10:48:47 +00:00
|
|
|
self.writer_pos += buf.len();
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2019-08-22 10:49:07 +00:00
|
|
|
fn write_item<T: Endian>(&mut self, item: T) -> Result<(), Error> {
|
2019-01-11 09:01:51 +00:00
|
|
|
let data = item.to_le();
|
2019-01-11 08:20:10 +00:00
|
|
|
|
2019-08-22 10:49:07 +00:00
|
|
|
let buffer = unsafe {
|
|
|
|
std::slice::from_raw_parts(&data as *const T as *const u8, std::mem::size_of::<T>())
|
|
|
|
};
|
2019-01-11 08:20:10 +00:00
|
|
|
|
|
|
|
self.write(buffer)?;
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2018-12-28 10:48:47 +00:00
|
|
|
fn flush_copy_buffer(&mut self, size: usize) -> Result<(), Error> {
|
2019-01-03 14:47:32 +00:00
|
|
|
self.writer.write_all(&self.file_copy_buffer[..size])?;
|
2018-12-28 10:48:47 +00:00
|
|
|
self.writer_pos += size;
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2018-12-28 08:55:26 +00:00
|
|
|
fn write_header(&mut self, htype: u64, size: u64) -> Result<(), Error> {
|
2019-08-02 13:19:34 +00:00
|
|
|
let size = size + (std::mem::size_of::<PxarHeader>() as u64);
|
|
|
|
self.write_item(PxarHeader { size, htype })?;
|
2018-12-28 10:48:47 +00:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn write_filename(&mut self, name: &CStr) -> Result<(), Error> {
|
|
|
|
let buffer = name.to_bytes_with_nul();
|
2019-08-02 13:19:34 +00:00
|
|
|
self.write_header(PXAR_FILENAME, buffer.len() as u64)?;
|
2018-12-28 10:48:47 +00:00
|
|
|
self.write(buffer)?;
|
2018-12-28 08:55:26 +00:00
|
|
|
|
|
|
|
Ok(())
|
2018-12-28 10:48:47 +00:00
|
|
|
}
|
2018-12-28 08:55:26 +00:00
|
|
|
|
2019-08-02 13:19:34 +00:00
|
|
|
fn create_entry(&self, stat: &FileStat) -> Result<PxarEntry, Error> {
|
2019-05-27 11:59:17 +00:00
|
|
|
let mode = if is_symlink(&stat) {
|
2019-01-11 08:20:10 +00:00
|
|
|
(libc::S_IFLNK | 0o777) as u64
|
|
|
|
} else {
|
|
|
|
(stat.st_mode & (libc::S_IFMT | 0o7777)) as u64
|
|
|
|
};
|
2018-12-28 08:55:26 +00:00
|
|
|
|
2019-01-11 08:20:10 +00:00
|
|
|
let mtime = stat.st_mtime * 1_000_000_000 + stat.st_mtime_nsec;
|
|
|
|
if mtime < 0 {
|
2019-03-15 11:19:51 +00:00
|
|
|
bail!("got strange mtime ({}) from fstat for {:?}.", mtime, self.full_path());
|
2019-01-11 08:20:10 +00:00
|
|
|
}
|
2018-12-28 08:55:26 +00:00
|
|
|
|
2019-08-02 13:19:34 +00:00
|
|
|
let entry = PxarEntry {
|
2019-09-11 10:06:59 +00:00
|
|
|
mode,
|
2019-01-11 08:20:10 +00:00
|
|
|
flags: 0,
|
2019-08-02 13:19:35 +00:00
|
|
|
uid: stat.st_uid,
|
|
|
|
gid: stat.st_gid,
|
2019-01-11 08:20:10 +00:00
|
|
|
mtime: mtime as u64,
|
|
|
|
};
|
2018-12-28 08:55:26 +00:00
|
|
|
|
2019-01-11 08:20:10 +00:00
|
|
|
Ok(entry)
|
|
|
|
}
|
2018-12-28 08:55:26 +00:00
|
|
|
|
2019-08-02 13:19:34 +00:00
|
|
|
fn read_chattr(&self, fd: RawFd, entry: &mut PxarEntry) -> Result<(), Error> {
|
2019-01-11 09:18:22 +00:00
|
|
|
let mut attr: usize = 0;
|
|
|
|
|
2019-08-22 10:49:07 +00:00
|
|
|
let res = unsafe { fs::read_attr_fd(fd, &mut attr) };
|
2019-01-11 09:18:22 +00:00
|
|
|
if let Err(err) = res {
|
|
|
|
if let nix::Error::Sys(errno) = err {
|
2019-08-22 10:49:07 +00:00
|
|
|
if errno_is_unsupported(errno) {
|
|
|
|
return Ok(());
|
|
|
|
};
|
2019-01-11 09:18:22 +00:00
|
|
|
}
|
2019-03-15 11:19:51 +00:00
|
|
|
bail!("read_attr_fd failed for {:?} - {}", self.full_path(), err);
|
2019-01-11 09:18:22 +00:00
|
|
|
}
|
|
|
|
|
2019-08-02 13:19:33 +00:00
|
|
|
let flags = flags::feature_flags_from_chattr(attr as u32);
|
2019-10-26 09:36:01 +00:00
|
|
|
entry.flags |= flags;
|
2019-01-11 09:18:22 +00:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2019-08-02 13:19:34 +00:00
|
|
|
fn read_fat_attr(&self, fd: RawFd, magic: i64, entry: &mut PxarEntry) -> Result<(), Error> {
|
2019-08-03 07:28:57 +00:00
|
|
|
use proxmox::sys::linux::magic::*;
|
2019-01-12 09:28:26 +00:00
|
|
|
|
2019-08-02 13:19:32 +00:00
|
|
|
if magic != MSDOS_SUPER_MAGIC && magic != FUSE_SUPER_MAGIC {
|
|
|
|
return Ok(());
|
|
|
|
}
|
2019-01-11 09:18:22 +00:00
|
|
|
|
|
|
|
let mut attr: u32 = 0;
|
|
|
|
|
2019-08-22 10:49:07 +00:00
|
|
|
let res = unsafe { fs::read_fat_attr_fd(fd, &mut attr) };
|
2019-01-11 09:18:22 +00:00
|
|
|
if let Err(err) = res {
|
|
|
|
if let nix::Error::Sys(errno) = err {
|
2019-08-22 10:49:07 +00:00
|
|
|
if errno_is_unsupported(errno) {
|
|
|
|
return Ok(());
|
|
|
|
};
|
2019-01-11 09:18:22 +00:00
|
|
|
}
|
2019-03-15 11:19:51 +00:00
|
|
|
bail!("read_fat_attr_fd failed for {:?} - {}", self.full_path(), err);
|
2019-01-11 08:20:10 +00:00
|
|
|
}
|
|
|
|
|
2019-08-02 13:19:33 +00:00
|
|
|
let flags = flags::feature_flags_from_fat_attr(attr);
|
2019-10-26 09:36:01 +00:00
|
|
|
entry.flags |= flags;
|
2019-01-11 09:18:22 +00:00
|
|
|
|
2019-01-11 08:20:10 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
2018-12-28 08:55:26 +00:00
|
|
|
|
2019-05-28 09:16:21 +00:00
|
|
|
/// True if all of the given feature flags are set in the Encoder, false otherwise
|
2019-05-22 15:50:38 +00:00
|
|
|
fn has_features(&self, feature_flags: u64) -> bool {
|
2019-06-06 11:37:07 +00:00
|
|
|
(self.feature_flags & self.fs_feature_flags & feature_flags) == feature_flags
|
2019-05-22 15:50:38 +00:00
|
|
|
}
|
|
|
|
|
2019-05-28 09:16:21 +00:00
|
|
|
/// True if at least one of the given feature flags is set in the Encoder, false otherwise
|
|
|
|
fn has_some_features(&self, feature_flags: u64) -> bool {
|
2019-06-06 11:37:07 +00:00
|
|
|
(self.feature_flags & self.fs_feature_flags & feature_flags) != 0
|
2019-05-28 09:16:21 +00:00
|
|
|
}
|
|
|
|
|
2019-08-22 10:49:07 +00:00
|
|
|
fn read_xattrs(
|
|
|
|
&self,
|
|
|
|
fd: RawFd,
|
|
|
|
stat: &FileStat,
|
|
|
|
) -> Result<(Vec<PxarXAttr>, Option<PxarFCaps>), Error> {
|
2019-05-17 12:23:34 +00:00
|
|
|
let mut xattrs = Vec::new();
|
|
|
|
let mut fcaps = None;
|
|
|
|
|
2019-08-02 13:19:33 +00:00
|
|
|
let flags = flags::WITH_XATTRS | flags::WITH_FCAPS;
|
2019-05-28 09:16:21 +00:00
|
|
|
if !self.has_some_features(flags) {
|
2019-05-23 09:58:40 +00:00
|
|
|
return Ok((xattrs, fcaps));
|
|
|
|
}
|
2019-05-17 12:23:34 +00:00
|
|
|
// Should never be called on symlinks, just in case check anyway
|
2019-05-27 11:59:17 +00:00
|
|
|
if is_symlink(&stat) {
|
2019-05-23 09:58:40 +00:00
|
|
|
return Ok((xattrs, fcaps));
|
|
|
|
}
|
2019-05-17 12:23:34 +00:00
|
|
|
|
|
|
|
let xattr_names = match xattr::flistxattr(fd) {
|
|
|
|
Ok(names) => names,
|
2019-05-27 11:01:38 +00:00
|
|
|
// Do not bail if the underlying endpoint does not supports xattrs
|
|
|
|
Err(Errno::EOPNOTSUPP) => return Ok((xattrs, fcaps)),
|
|
|
|
// Do not bail if the endpoint cannot carry xattrs (such as symlinks)
|
|
|
|
Err(Errno::EBADF) => return Ok((xattrs, fcaps)),
|
2019-05-17 12:23:34 +00:00
|
|
|
Err(err) => bail!("read_xattrs failed for {:?} - {}", self.full_path(), err),
|
|
|
|
};
|
|
|
|
|
2020-04-24 08:48:28 +00:00
|
|
|
for name in &xattr_names {
|
2019-05-17 12:23:34 +00:00
|
|
|
// Only extract the relevant extended attributes
|
2019-05-23 09:58:40 +00:00
|
|
|
if !xattr::is_valid_xattr_name(&name) {
|
|
|
|
continue;
|
|
|
|
}
|
2019-05-17 12:23:34 +00:00
|
|
|
|
|
|
|
let value = match xattr::fgetxattr(fd, name) {
|
|
|
|
Ok(value) => value,
|
|
|
|
// Vanished between flistattr and getxattr, this is ok, silently ignore
|
|
|
|
Err(Errno::ENODATA) => continue,
|
|
|
|
Err(err) => bail!("read_xattrs failed for {:?} - {}", self.full_path(), err),
|
|
|
|
};
|
|
|
|
|
2019-05-23 09:58:40 +00:00
|
|
|
if xattr::is_security_capability(&name) {
|
2019-08-02 13:19:33 +00:00
|
|
|
if self.has_features(flags::WITH_FCAPS) {
|
2019-05-28 09:16:21 +00:00
|
|
|
// fcaps are stored in own format within the archive
|
2019-08-22 10:49:07 +00:00
|
|
|
fcaps = Some(PxarFCaps { data: value });
|
2019-05-28 09:16:21 +00:00
|
|
|
}
|
2019-08-02 13:19:33 +00:00
|
|
|
} else if self.has_features(flags::WITH_XATTRS) {
|
2019-08-02 13:19:34 +00:00
|
|
|
xattrs.push(PxarXAttr {
|
2020-04-24 08:48:28 +00:00
|
|
|
name: name.to_bytes().to_vec(),
|
2019-09-11 10:06:59 +00:00
|
|
|
value,
|
2019-05-17 12:23:34 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
xattrs.sort();
|
|
|
|
|
|
|
|
Ok((xattrs, fcaps))
|
|
|
|
}
|
|
|
|
|
2019-08-22 10:49:07 +00:00
|
|
|
fn read_acl(
|
|
|
|
&self,
|
|
|
|
fd: RawFd,
|
|
|
|
stat: &FileStat,
|
|
|
|
acl_type: acl::ACLType,
|
|
|
|
) -> Result<PxarACL, Error> {
|
2019-05-23 16:08:56 +00:00
|
|
|
let ret = PxarACL {
|
|
|
|
users: Vec::new(),
|
|
|
|
groups: Vec::new(),
|
|
|
|
group_obj: None,
|
|
|
|
default: None,
|
|
|
|
};
|
|
|
|
|
2019-08-02 13:19:33 +00:00
|
|
|
if !self.has_features(flags::WITH_ACL) {
|
2019-05-23 16:08:56 +00:00
|
|
|
return Ok(ret);
|
|
|
|
}
|
2019-05-27 11:59:17 +00:00
|
|
|
if is_symlink(&stat) {
|
2019-05-23 16:08:56 +00:00
|
|
|
return Ok(ret);
|
|
|
|
}
|
2019-05-27 11:59:17 +00:00
|
|
|
if acl_type == acl::ACL_TYPE_DEFAULT && !is_directory(&stat) {
|
2019-05-23 16:08:56 +00:00
|
|
|
bail!("ACL_TYPE_DEFAULT only defined for directories.");
|
|
|
|
}
|
|
|
|
|
|
|
|
// In order to be able to get ACLs with type ACL_TYPE_DEFAULT, we have
|
|
|
|
// to create a path for acl_get_file(). acl_get_fd() only allows to get
|
|
|
|
// ACL_TYPE_ACCESS attributes.
|
|
|
|
let proc_path = Path::new("/proc/self/fd/").join(fd.to_string());
|
2019-05-27 11:01:38 +00:00
|
|
|
let acl = match acl::ACL::get_file(&proc_path, acl_type) {
|
|
|
|
Ok(acl) => acl,
|
|
|
|
// Don't bail if underlying endpoint does not support acls
|
|
|
|
Err(Errno::EOPNOTSUPP) => return Ok(ret),
|
|
|
|
// Don't bail if the endpoint cannot carry acls
|
|
|
|
Err(Errno::EBADF) => return Ok(ret),
|
|
|
|
// Don't bail if there is no data
|
|
|
|
Err(Errno::ENODATA) => return Ok(ret),
|
|
|
|
Err(err) => bail!("error while reading ACL - {}", err),
|
|
|
|
};
|
2019-05-23 16:08:56 +00:00
|
|
|
|
|
|
|
self.process_acl(acl, acl_type)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn process_acl(&self, acl: acl::ACL, acl_type: acl::ACLType) -> Result<PxarACL, Error> {
|
|
|
|
let mut acl_user = Vec::new();
|
|
|
|
let mut acl_group = Vec::new();
|
|
|
|
let mut acl_group_obj = None;
|
|
|
|
let mut acl_default = None;
|
|
|
|
let mut user_obj_permissions = None;
|
|
|
|
let mut group_obj_permissions = None;
|
|
|
|
let mut other_permissions = None;
|
|
|
|
let mut mask_permissions = None;
|
|
|
|
|
|
|
|
for entry in &mut acl.entries() {
|
|
|
|
let tag = entry.get_tag_type()?;
|
|
|
|
let permissions = entry.get_permissions()?;
|
|
|
|
match tag {
|
|
|
|
acl::ACL_USER_OBJ => user_obj_permissions = Some(permissions),
|
|
|
|
acl::ACL_GROUP_OBJ => group_obj_permissions = Some(permissions),
|
|
|
|
acl::ACL_OTHER => other_permissions = Some(permissions),
|
|
|
|
acl::ACL_MASK => mask_permissions = Some(permissions),
|
|
|
|
acl::ACL_USER => {
|
2019-08-02 13:19:34 +00:00
|
|
|
acl_user.push(PxarACLUser {
|
2019-05-23 16:08:56 +00:00
|
|
|
uid: entry.get_qualifier()?,
|
2019-09-11 10:06:59 +00:00
|
|
|
permissions,
|
2019-05-23 16:08:56 +00:00
|
|
|
});
|
2019-08-22 10:49:07 +00:00
|
|
|
}
|
2019-05-23 16:08:56 +00:00
|
|
|
acl::ACL_GROUP => {
|
2019-08-02 13:19:34 +00:00
|
|
|
acl_group.push(PxarACLGroup {
|
2019-05-23 16:08:56 +00:00
|
|
|
gid: entry.get_qualifier()?,
|
2019-09-11 10:06:59 +00:00
|
|
|
permissions,
|
2019-05-23 16:08:56 +00:00
|
|
|
});
|
2019-08-22 10:49:07 +00:00
|
|
|
}
|
2019-05-23 16:08:56 +00:00
|
|
|
_ => bail!("Unexpected ACL tag encountered!"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
acl_user.sort();
|
|
|
|
acl_group.sort();
|
|
|
|
|
|
|
|
match acl_type {
|
|
|
|
acl::ACL_TYPE_ACCESS => {
|
|
|
|
// The mask permissions are mapped to the stat group permissions
|
|
|
|
// in case that the ACL group permissions were set.
|
|
|
|
// Only in that case we need to store the group permissions,
|
|
|
|
// in the other cases they are identical to the stat group permissions.
|
|
|
|
if let (Some(gop), Some(_)) = (group_obj_permissions, mask_permissions) {
|
2019-08-22 10:49:07 +00:00
|
|
|
acl_group_obj = Some(PxarACLGroupObj { permissions: gop });
|
2019-05-23 16:08:56 +00:00
|
|
|
}
|
2019-08-22 10:49:07 +00:00
|
|
|
}
|
2019-05-23 16:08:56 +00:00
|
|
|
acl::ACL_TYPE_DEFAULT => {
|
2019-08-22 10:49:07 +00:00
|
|
|
if user_obj_permissions != None
|
|
|
|
|| group_obj_permissions != None
|
|
|
|
|| other_permissions != None
|
|
|
|
|| mask_permissions != None
|
2019-05-23 16:08:56 +00:00
|
|
|
{
|
2019-08-02 13:19:34 +00:00
|
|
|
acl_default = Some(PxarACLDefault {
|
2019-05-23 16:08:56 +00:00
|
|
|
// The value is set to UINT64_MAX as placeholder if one
|
|
|
|
// of the permissions is not set
|
|
|
|
user_obj_permissions: user_obj_permissions.unwrap_or(std::u64::MAX),
|
|
|
|
group_obj_permissions: group_obj_permissions.unwrap_or(std::u64::MAX),
|
|
|
|
other_permissions: other_permissions.unwrap_or(std::u64::MAX),
|
|
|
|
mask_permissions: mask_permissions.unwrap_or(std::u64::MAX),
|
|
|
|
});
|
|
|
|
}
|
2019-08-22 10:49:07 +00:00
|
|
|
}
|
2019-05-23 16:08:56 +00:00
|
|
|
_ => bail!("Unexpected ACL type encountered"),
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(PxarACL {
|
|
|
|
users: acl_user,
|
|
|
|
groups: acl_group,
|
|
|
|
group_obj: acl_group_obj,
|
|
|
|
default: acl_default,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-07-29 15:21:35 +00:00
|
|
|
/// Read the quota project id for an inode, supported on ext4/XFS/FUSE/ZFS filesystems
|
2019-08-22 10:49:07 +00:00
|
|
|
fn read_quota_project_id(
|
|
|
|
&self,
|
|
|
|
fd: RawFd,
|
|
|
|
magic: i64,
|
|
|
|
stat: &FileStat,
|
|
|
|
) -> Result<Option<PxarQuotaProjID>, Error> {
|
2019-05-29 12:34:05 +00:00
|
|
|
if !(is_directory(&stat) || is_reg_file(&stat)) {
|
|
|
|
return Ok(None);
|
|
|
|
}
|
2019-08-02 13:19:33 +00:00
|
|
|
if !self.has_features(flags::WITH_QUOTA_PROJID) {
|
2019-05-29 12:34:05 +00:00
|
|
|
return Ok(None);
|
|
|
|
}
|
2019-06-05 12:26:16 +00:00
|
|
|
|
2019-08-03 07:28:57 +00:00
|
|
|
use proxmox::sys::linux::magic::*;
|
|
|
|
|
2019-06-05 12:26:16 +00:00
|
|
|
match magic {
|
2019-08-22 10:49:07 +00:00
|
|
|
EXT4_SUPER_MAGIC | XFS_SUPER_MAGIC | FUSE_SUPER_MAGIC | ZFS_SUPER_MAGIC => {
|
2019-05-29 12:34:05 +00:00
|
|
|
let mut fsxattr = fs::FSXAttr::default();
|
2019-08-22 10:49:07 +00:00
|
|
|
let res = unsafe { fs::fs_ioc_fsgetxattr(fd, &mut fsxattr) };
|
2019-06-05 12:26:16 +00:00
|
|
|
|
|
|
|
// On some FUSE filesystems it can happen that ioctl is not supported.
|
|
|
|
// For these cases projid is set to 0 while the error is ignored.
|
|
|
|
if let Err(err) = res {
|
|
|
|
let errno = err.as_errno().ok_or_else(|| {
|
2019-08-22 10:49:07 +00:00
|
|
|
format_err!(
|
|
|
|
"error while reading quota project id for {:#?}",
|
|
|
|
self.full_path()
|
|
|
|
)
|
2019-06-05 12:26:16 +00:00
|
|
|
})?;
|
|
|
|
if errno_is_unsupported(errno) {
|
|
|
|
return Ok(None);
|
|
|
|
} else {
|
2019-08-22 10:49:07 +00:00
|
|
|
bail!(
|
|
|
|
"error while reading quota project id for {:#?} - {}",
|
|
|
|
self.full_path(),
|
|
|
|
errno
|
|
|
|
);
|
2019-06-05 12:26:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let projid = fsxattr.fsx_projid as u64;
|
|
|
|
if projid == 0 {
|
2019-10-25 16:44:51 +00:00
|
|
|
Ok(None)
|
2019-06-05 12:26:16 +00:00
|
|
|
} else {
|
2019-10-25 16:44:51 +00:00
|
|
|
Ok(Some(PxarQuotaProjID { projid }))
|
2019-05-29 12:34:05 +00:00
|
|
|
}
|
2019-08-22 10:49:07 +00:00
|
|
|
}
|
2019-10-25 16:44:51 +00:00
|
|
|
_ => Ok(None),
|
2019-05-29 12:34:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-02 13:19:34 +00:00
|
|
|
fn write_entry(&mut self, entry: PxarEntry) -> Result<(), Error> {
|
|
|
|
self.write_header(PXAR_ENTRY, std::mem::size_of::<PxarEntry>() as u64)?;
|
2019-01-11 08:20:10 +00:00
|
|
|
self.write_item(entry)?;
|
2018-12-28 08:55:26 +00:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
2018-12-28 06:45:15 +00:00
|
|
|
|
2019-08-02 13:19:34 +00:00
|
|
|
fn write_xattr(&mut self, xattr: PxarXAttr) -> Result<(), Error> {
|
2019-05-17 12:23:34 +00:00
|
|
|
let size = xattr.name.len() + xattr.value.len() + 1; // +1 for '\0' separating name and value
|
2019-08-02 13:19:34 +00:00
|
|
|
self.write_header(PXAR_XATTR, size as u64)?;
|
2019-05-17 12:23:34 +00:00
|
|
|
self.write(xattr.name.as_slice())?;
|
|
|
|
self.write(&[0])?;
|
|
|
|
self.write(xattr.value.as_slice())?;
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2019-08-02 13:19:34 +00:00
|
|
|
fn write_fcaps(&mut self, fcaps: Option<PxarFCaps>) -> Result<(), Error> {
|
2019-05-17 12:23:34 +00:00
|
|
|
if let Some(fcaps) = fcaps {
|
|
|
|
let size = fcaps.data.len();
|
2019-08-02 13:19:34 +00:00
|
|
|
self.write_header(PXAR_FCAPS, size as u64)?;
|
2019-05-17 12:23:34 +00:00
|
|
|
self.write(fcaps.data.as_slice())?;
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2019-08-02 13:19:34 +00:00
|
|
|
fn write_acl_user(&mut self, acl_user: PxarACLUser) -> Result<(), Error> {
|
2019-08-22 10:49:07 +00:00
|
|
|
self.write_header(PXAR_ACL_USER, std::mem::size_of::<PxarACLUser>() as u64)?;
|
2019-05-23 16:08:56 +00:00
|
|
|
self.write_item(acl_user)?;
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2019-08-02 13:19:34 +00:00
|
|
|
fn write_acl_group(&mut self, acl_group: PxarACLGroup) -> Result<(), Error> {
|
2019-08-22 10:49:07 +00:00
|
|
|
self.write_header(PXAR_ACL_GROUP, std::mem::size_of::<PxarACLGroup>() as u64)?;
|
2019-05-23 16:08:56 +00:00
|
|
|
self.write_item(acl_group)?;
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2019-08-02 13:19:34 +00:00
|
|
|
fn write_acl_group_obj(&mut self, acl_group_obj: PxarACLGroupObj) -> Result<(), Error> {
|
2019-08-22 10:49:07 +00:00
|
|
|
self.write_header(
|
|
|
|
PXAR_ACL_GROUP_OBJ,
|
|
|
|
std::mem::size_of::<PxarACLGroupObj>() as u64,
|
|
|
|
)?;
|
2019-05-23 16:08:56 +00:00
|
|
|
self.write_item(acl_group_obj)?;
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2019-08-02 13:19:34 +00:00
|
|
|
fn write_acl_default(&mut self, acl_default: PxarACLDefault) -> Result<(), Error> {
|
2019-08-22 10:49:07 +00:00
|
|
|
self.write_header(
|
|
|
|
PXAR_ACL_DEFAULT,
|
|
|
|
std::mem::size_of::<PxarACLDefault>() as u64,
|
|
|
|
)?;
|
2019-05-23 16:08:56 +00:00
|
|
|
self.write_item(acl_default)?;
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2019-08-02 13:19:34 +00:00
|
|
|
fn write_acl_default_user(&mut self, acl_default_user: PxarACLUser) -> Result<(), Error> {
|
2019-08-22 10:49:07 +00:00
|
|
|
self.write_header(
|
|
|
|
PXAR_ACL_DEFAULT_USER,
|
|
|
|
std::mem::size_of::<PxarACLUser>() as u64,
|
|
|
|
)?;
|
2019-05-23 16:08:56 +00:00
|
|
|
self.write_item(acl_default_user)?;
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2019-08-02 13:19:34 +00:00
|
|
|
fn write_acl_default_group(&mut self, acl_default_group: PxarACLGroup) -> Result<(), Error> {
|
2019-08-22 10:49:07 +00:00
|
|
|
self.write_header(
|
|
|
|
PXAR_ACL_DEFAULT_GROUP,
|
|
|
|
std::mem::size_of::<PxarACLGroup>() as u64,
|
|
|
|
)?;
|
2019-05-23 16:08:56 +00:00
|
|
|
self.write_item(acl_default_group)?;
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2019-08-02 13:19:34 +00:00
|
|
|
fn write_quota_project_id(&mut self, projid: PxarQuotaProjID) -> Result<(), Error> {
|
2019-08-22 10:49:07 +00:00
|
|
|
self.write_header(
|
|
|
|
PXAR_QUOTA_PROJID,
|
|
|
|
std::mem::size_of::<PxarQuotaProjID>() as u64,
|
|
|
|
)?;
|
2019-05-29 12:34:05 +00:00
|
|
|
self.write_item(projid)?;
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2019-08-22 10:49:07 +00:00
|
|
|
fn write_goodbye_table(
|
|
|
|
&mut self,
|
|
|
|
goodbye_offset: usize,
|
|
|
|
goodbye_items: &mut [PxarGoodbyeItem],
|
|
|
|
) -> Result<(), Error> {
|
2018-12-31 09:11:28 +00:00
|
|
|
goodbye_items.sort_unstable_by(|a, b| a.hash.cmp(&b.hash));
|
2018-12-29 16:26:32 +00:00
|
|
|
|
|
|
|
let item_count = goodbye_items.len();
|
|
|
|
|
2019-08-22 10:49:07 +00:00
|
|
|
let goodbye_table_size = (item_count + 1) * std::mem::size_of::<PxarGoodbyeItem>();
|
2018-12-29 16:26:32 +00:00
|
|
|
|
2019-08-02 13:19:34 +00:00
|
|
|
self.write_header(PXAR_GOODBYE, goodbye_table_size as u64)?;
|
2018-12-29 16:26:32 +00:00
|
|
|
|
2019-01-04 16:23:01 +00:00
|
|
|
if self.file_copy_buffer.len() < goodbye_table_size {
|
|
|
|
let need = goodbye_table_size - self.file_copy_buffer.len();
|
2018-12-30 13:09:59 +00:00
|
|
|
self.file_copy_buffer.reserve(need);
|
2019-08-22 10:49:07 +00:00
|
|
|
unsafe {
|
|
|
|
self.file_copy_buffer
|
|
|
|
.set_len(self.file_copy_buffer.capacity());
|
|
|
|
}
|
2018-12-29 16:26:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
let buffer = &mut self.file_copy_buffer;
|
|
|
|
|
|
|
|
copy_binary_search_tree(item_count, |s, d| {
|
|
|
|
let item = &goodbye_items[s];
|
2019-08-22 10:49:07 +00:00
|
|
|
let offset = d * std::mem::size_of::<PxarGoodbyeItem>();
|
|
|
|
let dest =
|
|
|
|
crate::tools::map_struct_mut::<PxarGoodbyeItem>(&mut buffer[offset..]).unwrap();
|
2018-12-29 16:26:32 +00:00
|
|
|
dest.offset = u64::to_le(item.offset);
|
|
|
|
dest.size = u64::to_le(item.size);
|
|
|
|
dest.hash = u64::to_le(item.hash);
|
|
|
|
});
|
|
|
|
|
2019-08-02 13:19:34 +00:00
|
|
|
// append PxarGoodbyeTail as last item
|
2019-08-22 10:49:07 +00:00
|
|
|
let offset = item_count * std::mem::size_of::<PxarGoodbyeItem>();
|
2019-08-02 13:19:34 +00:00
|
|
|
let dest = crate::tools::map_struct_mut::<PxarGoodbyeItem>(&mut buffer[offset..]).unwrap();
|
2018-12-29 16:26:32 +00:00
|
|
|
dest.offset = u64::to_le(goodbye_offset as u64);
|
2019-08-02 13:19:34 +00:00
|
|
|
dest.size = u64::to_le((goodbye_table_size + std::mem::size_of::<PxarHeader>()) as u64);
|
|
|
|
dest.hash = u64::to_le(PXAR_GOODBYE_TAIL_MARKER);
|
2018-12-29 16:26:32 +00:00
|
|
|
|
|
|
|
self.flush_copy_buffer(goodbye_table_size)?;
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2019-08-02 15:02:24 +00:00
|
|
|
fn encode_dir(
|
|
|
|
&mut self,
|
|
|
|
dir: &mut nix::dir::Dir,
|
|
|
|
dir_stat: &FileStat,
|
|
|
|
magic: i64,
|
2019-12-16 11:13:44 +00:00
|
|
|
match_pattern: Vec<MatchPatternSlice>,
|
2019-08-02 15:02:24 +00:00
|
|
|
) -> Result<(), Error> {
|
2019-03-15 11:19:51 +00:00
|
|
|
//println!("encode_dir: {:?} start {}", self.full_path(), self.writer_pos);
|
2018-12-27 12:15:10 +00:00
|
|
|
|
2020-01-08 09:46:56 +00:00
|
|
|
let mut name_list = Vec::new();
|
2018-12-27 12:15:10 +00:00
|
|
|
|
|
|
|
let rawfd = dir.as_raw_fd();
|
|
|
|
|
2019-01-11 08:20:10 +00:00
|
|
|
let dir_start_pos = self.writer_pos;
|
2018-12-27 12:15:10 +00:00
|
|
|
|
2019-10-22 13:07:51 +00:00
|
|
|
let is_root = dir_start_pos == 0;
|
|
|
|
|
2019-01-11 08:20:10 +00:00
|
|
|
let mut dir_entry = self.create_entry(&dir_stat)?;
|
2018-12-28 09:44:12 +00:00
|
|
|
|
2019-01-11 08:20:10 +00:00
|
|
|
self.read_chattr(rawfd, &mut dir_entry)?;
|
2019-01-12 09:28:26 +00:00
|
|
|
self.read_fat_attr(rawfd, magic, &mut dir_entry)?;
|
2019-06-06 11:37:07 +00:00
|
|
|
|
|
|
|
// for each node in the directory tree, the filesystem features are
|
|
|
|
// checked based on the fs magic number.
|
2019-08-02 13:19:33 +00:00
|
|
|
self.fs_feature_flags = flags::feature_flags_from_magic(magic);
|
2019-06-06 11:37:07 +00:00
|
|
|
|
2019-05-23 09:58:40 +00:00
|
|
|
let (xattrs, fcaps) = self.read_xattrs(rawfd, &dir_stat)?;
|
2019-05-23 16:08:56 +00:00
|
|
|
let acl_access = self.read_acl(rawfd, &dir_stat, acl::ACL_TYPE_ACCESS)?;
|
|
|
|
let acl_default = self.read_acl(rawfd, &dir_stat, acl::ACL_TYPE_DEFAULT)?;
|
2019-05-29 12:34:05 +00:00
|
|
|
let projid = self.read_quota_project_id(rawfd, magic, &dir_stat)?;
|
2018-12-28 08:55:26 +00:00
|
|
|
|
2019-01-11 09:01:51 +00:00
|
|
|
self.write_entry(dir_entry)?;
|
2019-05-23 09:58:40 +00:00
|
|
|
for xattr in xattrs {
|
|
|
|
self.write_xattr(xattr)?;
|
|
|
|
}
|
2019-05-17 12:23:34 +00:00
|
|
|
self.write_fcaps(fcaps)?;
|
2018-12-28 10:48:47 +00:00
|
|
|
|
2019-05-23 16:08:56 +00:00
|
|
|
for user in acl_access.users {
|
|
|
|
self.write_acl_user(user)?;
|
|
|
|
}
|
|
|
|
for group in acl_access.groups {
|
|
|
|
self.write_acl_group(group)?;
|
|
|
|
}
|
|
|
|
if let Some(group_obj) = acl_access.group_obj {
|
|
|
|
self.write_acl_group_obj(group_obj)?;
|
|
|
|
}
|
|
|
|
|
|
|
|
for default_user in acl_default.users {
|
|
|
|
self.write_acl_default_user(default_user)?;
|
|
|
|
}
|
|
|
|
for default_group in acl_default.groups {
|
|
|
|
self.write_acl_default_group(default_group)?;
|
|
|
|
}
|
|
|
|
if let Some(default) = acl_default.default {
|
|
|
|
self.write_acl_default(default)?;
|
|
|
|
}
|
2019-05-29 12:34:05 +00:00
|
|
|
if let Some(projid) = projid {
|
|
|
|
self.write_quota_project_id(projid)?;
|
|
|
|
}
|
2019-05-23 16:08:56 +00:00
|
|
|
|
2019-03-08 07:14:26 +00:00
|
|
|
let include_children;
|
2019-01-12 14:43:20 +00:00
|
|
|
if is_virtual_file_system(magic) {
|
|
|
|
include_children = false;
|
2019-10-25 16:44:51 +00:00
|
|
|
} else if let Some(set) = &self.device_set {
|
|
|
|
include_children = set.contains(&dir_stat.st_dev);
|
2019-01-12 14:43:20 +00:00
|
|
|
} else {
|
2019-10-25 16:44:51 +00:00
|
|
|
include_children = true;
|
2019-01-12 14:43:20 +00:00
|
|
|
}
|
|
|
|
|
2019-06-21 16:15:01 +00:00
|
|
|
// Expand the exclude match pattern inherited from the parent by local entries, if present
|
|
|
|
let mut local_match_pattern = match_pattern.clone();
|
2019-12-16 11:13:44 +00:00
|
|
|
let (pxar_exclude, excludes) = match MatchPattern::from_file(rawfd, ".pxarexclude") {
|
|
|
|
Ok(Some((excludes, buffer, stat))) => {
|
|
|
|
(Some((buffer, stat)), excludes)
|
2019-08-22 10:49:07 +00:00
|
|
|
}
|
2019-12-16 11:13:44 +00:00
|
|
|
Ok(None) => (None, Vec::new()),
|
2020-01-08 14:14:44 +00:00
|
|
|
Err(nix::Error::Sys(Errno::EACCES)) => {
|
|
|
|
// No permission to read .pxarexclude, ignore its contents.
|
|
|
|
eprintln!(
|
|
|
|
"ignoring match patterns in {:?}: open file failed - EACCES",
|
|
|
|
self.full_path().join(".pxarexclude"),
|
|
|
|
);
|
|
|
|
(None, Vec::new())
|
|
|
|
}
|
2019-06-21 16:15:01 +00:00
|
|
|
Err(err) => bail!("error while reading exclude file - {}", err),
|
|
|
|
};
|
2019-12-16 11:13:44 +00:00
|
|
|
for excl in &excludes {
|
|
|
|
local_match_pattern.push(excl.as_slice());
|
|
|
|
}
|
2019-06-21 16:15:01 +00:00
|
|
|
|
2019-01-12 14:43:20 +00:00
|
|
|
if include_children {
|
2019-10-23 05:55:56 +00:00
|
|
|
// Exclude patterns passed via the CLI are stored as '.pxarexclude-cli'
|
|
|
|
// in the root directory of the archive.
|
2019-10-26 09:36:01 +00:00
|
|
|
if is_root && !match_pattern.is_empty() {
|
2019-10-23 05:55:56 +00:00
|
|
|
let filename = CString::new(".pxarexclude-cli")?;
|
2019-10-26 09:36:01 +00:00
|
|
|
name_list.push((filename, *dir_stat, match_pattern.clone()));
|
2019-10-23 05:55:56 +00:00
|
|
|
}
|
|
|
|
|
2019-01-12 09:20:08 +00:00
|
|
|
for entry in dir.iter() {
|
2019-08-22 10:49:07 +00:00
|
|
|
let entry = entry
|
|
|
|
.map_err(|err| format_err!("readir {:?} failed - {}", self.full_path(), err))?;
|
2019-01-12 09:20:08 +00:00
|
|
|
let filename = entry.file_name().to_owned();
|
2018-12-27 12:15:10 +00:00
|
|
|
|
2019-01-12 09:20:08 +00:00
|
|
|
let name = filename.to_bytes_with_nul();
|
2019-06-05 12:37:25 +00:00
|
|
|
if name == b".\0" || name == b"..\0" {
|
|
|
|
continue;
|
|
|
|
}
|
2019-10-22 13:07:51 +00:00
|
|
|
// Do not store a ".pxarexclude-cli" file found in the archive root,
|
|
|
|
// as this would confilict with new cli passed exclude patterns,
|
|
|
|
// if present.
|
|
|
|
if is_root && name == b".pxarexclude-cli\0" {
|
|
|
|
eprintln!("skip existing '.pxarexclude-cli' in archive root.");
|
|
|
|
continue;
|
|
|
|
}
|
2018-12-27 12:15:10 +00:00
|
|
|
|
2019-08-22 10:49:07 +00:00
|
|
|
let stat = match nix::sys::stat::fstatat(
|
|
|
|
rawfd,
|
|
|
|
filename.as_ref(),
|
|
|
|
nix::fcntl::AtFlags::AT_SYMLINK_NOFOLLOW,
|
|
|
|
) {
|
2019-06-21 16:15:01 +00:00
|
|
|
Ok(stat) => stat,
|
|
|
|
Err(nix::Error::Sys(Errno::ENOENT)) => {
|
|
|
|
let filename_osstr = std::ffi::OsStr::from_bytes(filename.to_bytes());
|
|
|
|
self.report_vanished_file(&self.full_path().join(filename_osstr))?;
|
|
|
|
continue;
|
2019-08-22 10:49:07 +00:00
|
|
|
}
|
2019-06-21 16:15:01 +00:00
|
|
|
Err(err) => bail!("fstat {:?} failed - {}", self.full_path(), err),
|
|
|
|
};
|
|
|
|
|
2019-12-16 11:13:44 +00:00
|
|
|
match MatchPatternSlice::match_filename_exclude(
|
2019-12-10 12:01:39 +00:00
|
|
|
&filename,
|
|
|
|
is_directory(&stat),
|
|
|
|
&local_match_pattern,
|
|
|
|
)? {
|
2019-08-02 15:02:24 +00:00
|
|
|
(MatchType::Positive, _) => {
|
2019-06-21 16:15:01 +00:00
|
|
|
let filename_osstr = std::ffi::OsStr::from_bytes(filename.to_bytes());
|
2019-08-22 10:49:07 +00:00
|
|
|
eprintln!(
|
2020-02-20 13:29:13 +00:00
|
|
|
"matched by exclude pattern - skipping: {:?}",
|
2019-08-22 10:49:07 +00:00
|
|
|
self.full_path().join(filename_osstr)
|
|
|
|
);
|
|
|
|
}
|
2020-01-10 11:50:06 +00:00
|
|
|
(_, child_pattern) => {
|
|
|
|
self.entry_counter += 1;
|
|
|
|
name_list.push((filename, stat, child_pattern));
|
|
|
|
}
|
2019-06-21 16:15:01 +00:00
|
|
|
}
|
|
|
|
|
2020-01-10 11:50:06 +00:00
|
|
|
if self.entry_counter > self.entry_max {
|
2019-08-22 10:49:07 +00:00
|
|
|
bail!(
|
2020-01-10 11:50:06 +00:00
|
|
|
"exceeded max number of entries (> {})",
|
|
|
|
self.entry_max
|
2019-10-22 13:07:51 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2019-03-08 08:25:00 +00:00
|
|
|
} else {
|
2019-03-15 11:19:51 +00:00
|
|
|
eprintln!("skip mount point: {:?}", self.full_path());
|
2018-12-27 12:15:10 +00:00
|
|
|
}
|
|
|
|
|
2019-06-21 16:15:01 +00:00
|
|
|
name_list.sort_unstable_by(|a, b| a.0.cmp(&b.0));
|
2020-01-10 11:50:06 +00:00
|
|
|
let num_entries = name_list.len();
|
2018-12-27 12:15:10 +00:00
|
|
|
|
2020-01-10 11:50:06 +00:00
|
|
|
let mut goodbye_items = Vec::with_capacity(num_entries);
|
2018-12-28 13:26:05 +00:00
|
|
|
|
2019-06-21 16:15:01 +00:00
|
|
|
for (filename, stat, exclude_list) in name_list {
|
2019-07-04 12:24:13 +00:00
|
|
|
let start_pos = self.writer_pos;
|
|
|
|
|
2019-06-21 16:15:01 +00:00
|
|
|
if filename.as_bytes() == b".pxarexclude" {
|
2020-01-08 14:14:44 +00:00
|
|
|
// pxar_exclude is none in case of error EACCES.
|
2019-06-21 16:15:01 +00:00
|
|
|
if let Some((ref content, ref stat)) = pxar_exclude {
|
2019-08-22 10:49:07 +00:00
|
|
|
let filefd = match nix::fcntl::openat(
|
|
|
|
rawfd,
|
|
|
|
filename.as_ref(),
|
|
|
|
OFlag::O_NOFOLLOW,
|
|
|
|
Mode::empty(),
|
|
|
|
) {
|
2019-06-21 16:15:01 +00:00
|
|
|
Ok(filefd) => filefd,
|
|
|
|
Err(nix::Error::Sys(Errno::ENOENT)) => {
|
|
|
|
self.report_vanished_file(&self.full_path())?;
|
|
|
|
continue;
|
2019-08-22 10:49:07 +00:00
|
|
|
}
|
2020-01-08 14:14:44 +00:00
|
|
|
Err(nix::Error::Sys(Errno::EACCES)) => {
|
|
|
|
let filename_osstr = std::ffi::OsStr::from_bytes(filename.to_bytes());
|
|
|
|
eprintln!(
|
|
|
|
"skipping {:?}: open file failed - EACCES",
|
|
|
|
self.full_path().join(filename_osstr),
|
|
|
|
);
|
|
|
|
continue;
|
|
|
|
}
|
2019-06-21 16:15:01 +00:00
|
|
|
Err(err) => {
|
|
|
|
let filename_osstr = std::ffi::OsStr::from_bytes(filename.to_bytes());
|
2019-08-22 10:49:07 +00:00
|
|
|
bail!(
|
|
|
|
"open file {:?} failed - {}",
|
|
|
|
self.full_path().join(filename_osstr),
|
|
|
|
err
|
|
|
|
);
|
|
|
|
}
|
2019-06-21 16:15:01 +00:00
|
|
|
};
|
2018-12-27 12:15:10 +00:00
|
|
|
|
2019-06-21 16:15:01 +00:00
|
|
|
let child_magic = if dir_stat.st_dev != stat.st_dev {
|
|
|
|
detect_fs_type(filefd)?
|
|
|
|
} else {
|
|
|
|
magic
|
|
|
|
};
|
2019-03-04 07:01:09 +00:00
|
|
|
|
2019-06-21 16:15:01 +00:00
|
|
|
self.write_filename(&filename)?;
|
2019-10-16 16:06:05 +00:00
|
|
|
if let Some(ref mut catalog) = self.catalog {
|
|
|
|
catalog.add_file(&filename, stat.st_size as u64, stat.st_mtime as u64)?;
|
|
|
|
}
|
2019-06-21 16:15:01 +00:00
|
|
|
self.encode_pxar_exclude(filefd, stat, child_magic, content)?;
|
2019-01-11 08:20:10 +00:00
|
|
|
}
|
2020-01-08 14:14:44 +00:00
|
|
|
continue;
|
2019-06-21 16:15:01 +00:00
|
|
|
}
|
|
|
|
|
2019-10-22 13:07:51 +00:00
|
|
|
if is_root && filename.as_bytes() == b".pxarexclude-cli" {
|
|
|
|
// '.pxarexclude-cli' is used to store the exclude MatchPatterns
|
|
|
|
// passed via the cli in the root directory of the archive.
|
|
|
|
self.write_filename(&filename)?;
|
2019-12-16 11:13:44 +00:00
|
|
|
let content = MatchPatternSlice::to_bytes(&exclude_list);
|
2019-10-22 13:07:51 +00:00
|
|
|
if let Some(ref mut catalog) = self.catalog {
|
|
|
|
catalog.add_file(&filename, content.len() as u64, 0)?;
|
|
|
|
}
|
|
|
|
self.encode_pxar_exclude_cli(stat.st_uid, stat.st_gid, 0, &content)?;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2019-08-22 10:49:07 +00:00
|
|
|
self.relative_path
|
|
|
|
.push(std::ffi::OsStr::from_bytes(filename.as_bytes()));
|
2019-06-21 16:15:01 +00:00
|
|
|
|
2019-08-22 10:49:07 +00:00
|
|
|
if self.verbose {
|
|
|
|
println!("{:?}", self.full_path());
|
|
|
|
}
|
2019-01-11 08:20:10 +00:00
|
|
|
|
2019-05-27 11:59:17 +00:00
|
|
|
if is_directory(&stat) {
|
2019-08-22 10:49:07 +00:00
|
|
|
let mut dir = match nix::dir::Dir::openat(
|
|
|
|
rawfd,
|
|
|
|
filename.as_ref(),
|
|
|
|
OFlag::O_DIRECTORY | OFlag::O_NOFOLLOW,
|
|
|
|
Mode::empty(),
|
|
|
|
) {
|
2019-01-12 07:51:04 +00:00
|
|
|
Ok(dir) => dir,
|
|
|
|
Err(nix::Error::Sys(Errno::ENOENT)) => {
|
2019-03-15 11:19:51 +00:00
|
|
|
self.report_vanished_file(&self.full_path())?;
|
2020-01-08 14:14:43 +00:00
|
|
|
self.relative_path.pop();
|
|
|
|
continue;
|
2019-08-22 10:49:07 +00:00
|
|
|
}
|
2020-01-08 14:14:44 +00:00
|
|
|
Err(nix::Error::Sys(Errno::EACCES)) => {
|
|
|
|
eprintln!(
|
|
|
|
"skipping {:?}: open dir failed - EACCES",
|
|
|
|
self.full_path(),
|
|
|
|
);
|
|
|
|
self.relative_path.pop();
|
|
|
|
continue;
|
|
|
|
}
|
2019-03-15 11:19:51 +00:00
|
|
|
Err(err) => bail!("open dir {:?} failed - {}", self.full_path(), err),
|
2019-01-12 07:51:04 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
let child_magic = if dir_stat.st_dev != stat.st_dev {
|
2019-01-12 09:20:08 +00:00
|
|
|
detect_fs_type(dir.as_raw_fd())?
|
2019-01-12 07:51:04 +00:00
|
|
|
} else {
|
|
|
|
magic
|
|
|
|
};
|
|
|
|
|
|
|
|
self.write_filename(&filename)?;
|
2019-08-09 07:46:49 +00:00
|
|
|
if let Some(ref mut catalog) = self.catalog {
|
|
|
|
catalog.start_directory(&filename)?;
|
|
|
|
}
|
2019-06-21 16:15:01 +00:00
|
|
|
self.encode_dir(&mut dir, &stat, child_magic, exclude_list)?;
|
2019-08-09 07:46:49 +00:00
|
|
|
if let Some(ref mut catalog) = self.catalog {
|
|
|
|
catalog.end_directory()?;
|
|
|
|
}
|
2019-05-27 11:59:17 +00:00
|
|
|
} else if is_reg_file(&stat) {
|
2019-03-16 10:02:12 +00:00
|
|
|
let mut hardlink_target = None;
|
|
|
|
|
2019-03-15 11:19:51 +00:00
|
|
|
if stat.st_nlink > 1 {
|
2019-08-22 10:49:07 +00:00
|
|
|
let link_info = HardLinkInfo {
|
|
|
|
st_dev: stat.st_dev,
|
|
|
|
st_ino: stat.st_ino,
|
|
|
|
};
|
2019-03-16 10:02:12 +00:00
|
|
|
hardlink_target = self.hardlinks.get(&link_info).map(|(v, offset)| {
|
|
|
|
let mut target = v.clone().into_os_string();
|
|
|
|
target.push("\0"); // add Nul byte
|
|
|
|
(target, (start_pos as u64) - offset)
|
|
|
|
});
|
|
|
|
if hardlink_target == None {
|
2019-08-22 10:49:07 +00:00
|
|
|
self.hardlinks
|
|
|
|
.insert(link_info, (self.relative_path.clone(), start_pos as u64));
|
2019-03-15 11:19:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-16 10:02:12 +00:00
|
|
|
if let Some((target, offset)) = hardlink_target {
|
2019-08-09 07:46:49 +00:00
|
|
|
if let Some(ref mut catalog) = self.catalog {
|
|
|
|
catalog.add_hardlink(&filename)?;
|
|
|
|
}
|
2019-03-16 10:02:12 +00:00
|
|
|
self.write_filename(&filename)?;
|
|
|
|
self.encode_hardlink(target.as_bytes(), offset)?;
|
2019-01-12 09:20:08 +00:00
|
|
|
} else {
|
2019-08-22 10:49:07 +00:00
|
|
|
let filefd = match nix::fcntl::openat(
|
|
|
|
rawfd,
|
|
|
|
filename.as_ref(),
|
|
|
|
OFlag::O_NOFOLLOW,
|
|
|
|
Mode::empty(),
|
|
|
|
) {
|
2019-03-16 10:02:12 +00:00
|
|
|
Ok(filefd) => filefd,
|
|
|
|
Err(nix::Error::Sys(Errno::ENOENT)) => {
|
|
|
|
self.report_vanished_file(&self.full_path())?;
|
2020-01-08 14:14:43 +00:00
|
|
|
self.relative_path.pop();
|
2019-03-16 10:02:12 +00:00
|
|
|
continue;
|
2019-08-22 10:49:07 +00:00
|
|
|
}
|
2020-01-08 14:14:44 +00:00
|
|
|
Err(nix::Error::Sys(Errno::EACCES)) => {
|
|
|
|
eprintln!(
|
|
|
|
"skipping {:?}: open file failed - EACCES",
|
|
|
|
self.full_path(),
|
|
|
|
);
|
|
|
|
self.relative_path.pop();
|
|
|
|
continue;
|
|
|
|
}
|
2019-03-16 10:02:12 +00:00
|
|
|
Err(err) => bail!("open file {:?} failed - {}", self.full_path(), err),
|
|
|
|
};
|
|
|
|
|
2019-08-09 07:46:49 +00:00
|
|
|
if let Some(ref mut catalog) = self.catalog {
|
|
|
|
catalog.add_file(&filename, stat.st_size as u64, stat.st_mtime as u64)?;
|
|
|
|
}
|
2019-03-16 10:02:12 +00:00
|
|
|
let child_magic = if dir_stat.st_dev != stat.st_dev {
|
|
|
|
detect_fs_type(filefd)?
|
|
|
|
} else {
|
|
|
|
magic
|
|
|
|
};
|
|
|
|
|
|
|
|
self.write_filename(&filename)?;
|
|
|
|
let res = self.encode_file(filefd, &stat, child_magic);
|
|
|
|
let _ = nix::unistd::close(filefd); // ignore close errors
|
|
|
|
res?;
|
|
|
|
}
|
2019-05-27 11:59:17 +00:00
|
|
|
} else if is_symlink(&stat) {
|
2019-05-22 13:02:16 +00:00
|
|
|
let mut buffer = vec::undefined(libc::PATH_MAX as usize);
|
2018-12-28 08:55:26 +00:00
|
|
|
|
2019-08-22 10:49:07 +00:00
|
|
|
let res = filename.with_nix_path(|cstr| unsafe {
|
|
|
|
libc::readlinkat(
|
|
|
|
rawfd,
|
|
|
|
cstr.as_ptr(),
|
|
|
|
buffer.as_mut_ptr() as *mut libc::c_char,
|
|
|
|
buffer.len() - 1,
|
|
|
|
)
|
2018-12-28 08:55:26 +00:00
|
|
|
})?;
|
|
|
|
|
|
|
|
match Errno::result(res) {
|
2018-12-28 13:51:43 +00:00
|
|
|
Ok(len) => {
|
2019-08-09 07:46:49 +00:00
|
|
|
if let Some(ref mut catalog) = self.catalog {
|
|
|
|
catalog.add_symlink(&filename)?;
|
|
|
|
}
|
2018-12-28 13:51:43 +00:00
|
|
|
buffer[len as usize] = 0u8; // add Nul byte
|
2019-01-12 07:51:04 +00:00
|
|
|
self.write_filename(&filename)?;
|
2019-08-22 10:49:07 +00:00
|
|
|
self.encode_symlink(&buffer[..((len + 1) as usize)], &stat)?
|
2018-12-28 13:51:43 +00:00
|
|
|
}
|
2019-01-12 07:51:04 +00:00
|
|
|
Err(nix::Error::Sys(Errno::ENOENT)) => {
|
2019-03-15 11:19:51 +00:00
|
|
|
self.report_vanished_file(&self.full_path())?;
|
2020-01-08 14:14:43 +00:00
|
|
|
self.relative_path.pop();
|
2019-01-12 07:51:04 +00:00
|
|
|
continue;
|
|
|
|
}
|
2019-03-15 11:19:51 +00:00
|
|
|
Err(err) => bail!("readlink {:?} failed - {}", self.full_path(), err),
|
2018-12-28 06:45:15 +00:00
|
|
|
}
|
2019-05-27 11:59:17 +00:00
|
|
|
} else if is_block_dev(&stat) || is_char_dev(&stat) {
|
2019-08-02 13:19:33 +00:00
|
|
|
if self.has_features(flags::WITH_DEVICE_NODES) {
|
2019-08-09 07:46:49 +00:00
|
|
|
if let Some(ref mut catalog) = self.catalog {
|
|
|
|
if is_block_dev(&stat) {
|
|
|
|
catalog.add_block_device(&filename)?;
|
|
|
|
} else {
|
|
|
|
catalog.add_char_device(&filename)?;
|
|
|
|
}
|
|
|
|
}
|
2019-08-01 15:51:59 +00:00
|
|
|
self.write_filename(&filename)?;
|
|
|
|
self.encode_device(&stat)?;
|
|
|
|
} else {
|
|
|
|
eprintln!("skip device node: {:?}", self.full_path());
|
2020-01-08 14:14:43 +00:00
|
|
|
self.relative_path.pop();
|
|
|
|
continue;
|
2019-08-01 15:51:59 +00:00
|
|
|
}
|
|
|
|
} else if is_fifo(&stat) {
|
2019-08-02 13:19:33 +00:00
|
|
|
if self.has_features(flags::WITH_FIFOS) {
|
2019-08-09 07:46:49 +00:00
|
|
|
if let Some(ref mut catalog) = self.catalog {
|
|
|
|
catalog.add_fifo(&filename)?;
|
|
|
|
}
|
2019-08-01 15:51:59 +00:00
|
|
|
self.write_filename(&filename)?;
|
|
|
|
self.encode_special(&stat)?;
|
|
|
|
} else {
|
|
|
|
eprintln!("skip fifo: {:?}", self.full_path());
|
2020-01-08 14:14:43 +00:00
|
|
|
self.relative_path.pop();
|
|
|
|
continue;
|
2019-08-01 15:51:59 +00:00
|
|
|
}
|
|
|
|
} else if is_socket(&stat) {
|
2019-08-02 13:19:33 +00:00
|
|
|
if self.has_features(flags::WITH_SOCKETS) {
|
2019-08-09 07:46:49 +00:00
|
|
|
if let Some(ref mut catalog) = self.catalog {
|
|
|
|
catalog.add_socket(&filename)?;
|
|
|
|
}
|
2019-08-01 15:51:59 +00:00
|
|
|
self.write_filename(&filename)?;
|
|
|
|
self.encode_special(&stat)?;
|
|
|
|
} else {
|
|
|
|
eprintln!("skip socket: {:?}", self.full_path());
|
2020-01-08 14:14:43 +00:00
|
|
|
self.relative_path.pop();
|
|
|
|
continue;
|
2019-08-01 15:51:59 +00:00
|
|
|
}
|
2018-12-27 13:24:31 +00:00
|
|
|
} else {
|
2019-08-22 10:49:07 +00:00
|
|
|
bail!(
|
|
|
|
"unsupported file type (mode {:o} {:?})",
|
|
|
|
stat.st_mode,
|
|
|
|
self.full_path()
|
|
|
|
);
|
2018-12-27 12:15:10 +00:00
|
|
|
}
|
2018-12-27 13:24:31 +00:00
|
|
|
|
2018-12-28 13:26:05 +00:00
|
|
|
let end_pos = self.writer_pos;
|
|
|
|
|
2019-08-02 13:19:34 +00:00
|
|
|
goodbye_items.push(PxarGoodbyeItem {
|
2018-12-28 13:26:05 +00:00
|
|
|
offset: start_pos as u64,
|
|
|
|
size: (end_pos - start_pos) as u64,
|
2018-12-30 16:32:52 +00:00
|
|
|
hash: compute_goodbye_hash(filename.to_bytes()),
|
2018-12-28 13:26:05 +00:00
|
|
|
});
|
|
|
|
|
2019-03-15 11:19:51 +00:00
|
|
|
self.relative_path.pop();
|
2018-12-28 10:48:47 +00:00
|
|
|
}
|
|
|
|
|
2019-03-15 11:19:51 +00:00
|
|
|
//println!("encode_dir: {:?} end {}", self.full_path(), self.writer_pos);
|
2018-12-27 13:24:31 +00:00
|
|
|
|
2018-12-29 16:26:32 +00:00
|
|
|
// fixup goodby item offsets
|
|
|
|
let goodbye_start = self.writer_pos as u64;
|
|
|
|
for item in &mut goodbye_items {
|
|
|
|
item.offset = goodbye_start - item.offset;
|
2018-12-28 13:26:05 +00:00
|
|
|
}
|
|
|
|
|
2018-12-29 16:26:32 +00:00
|
|
|
let goodbye_offset = self.writer_pos - dir_start_pos;
|
2018-12-28 13:26:05 +00:00
|
|
|
|
2018-12-31 09:11:28 +00:00
|
|
|
self.write_goodbye_table(goodbye_offset, &mut goodbye_items)?;
|
2020-01-10 11:50:06 +00:00
|
|
|
self.entry_counter -= num_entries;
|
2018-12-28 13:26:05 +00:00
|
|
|
|
2019-03-15 11:19:51 +00:00
|
|
|
//println!("encode_dir: {:?} end1 {}", self.full_path(), self.writer_pos);
|
2018-12-27 13:24:31 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2019-08-22 10:49:07 +00:00
|
|
|
fn encode_file(&mut self, filefd: RawFd, stat: &FileStat, magic: i64) -> Result<(), Error> {
|
2019-03-15 11:19:51 +00:00
|
|
|
//println!("encode_file: {:?}", self.full_path());
|
2018-12-27 13:24:31 +00:00
|
|
|
|
2019-01-11 08:20:10 +00:00
|
|
|
let mut entry = self.create_entry(&stat)?;
|
2018-12-28 09:44:12 +00:00
|
|
|
|
2019-01-11 08:20:10 +00:00
|
|
|
self.read_chattr(filefd, &mut entry)?;
|
2019-01-12 09:28:26 +00:00
|
|
|
self.read_fat_attr(filefd, magic, &mut entry)?;
|
2019-05-23 09:58:40 +00:00
|
|
|
let (xattrs, fcaps) = self.read_xattrs(filefd, &stat)?;
|
2019-05-23 16:08:56 +00:00
|
|
|
let acl_access = self.read_acl(filefd, &stat, acl::ACL_TYPE_ACCESS)?;
|
2019-05-29 12:34:05 +00:00
|
|
|
let projid = self.read_quota_project_id(filefd, magic, &stat)?;
|
2018-12-28 09:44:12 +00:00
|
|
|
|
2019-01-11 09:01:51 +00:00
|
|
|
self.write_entry(entry)?;
|
2019-05-23 09:58:40 +00:00
|
|
|
for xattr in xattrs {
|
|
|
|
self.write_xattr(xattr)?;
|
|
|
|
}
|
2019-05-17 12:23:34 +00:00
|
|
|
self.write_fcaps(fcaps)?;
|
2019-05-23 16:08:56 +00:00
|
|
|
for user in acl_access.users {
|
|
|
|
self.write_acl_user(user)?;
|
|
|
|
}
|
|
|
|
for group in acl_access.groups {
|
|
|
|
self.write_acl_group(group)?;
|
|
|
|
}
|
|
|
|
if let Some(group_obj) = acl_access.group_obj {
|
|
|
|
self.write_acl_group_obj(group_obj)?;
|
|
|
|
}
|
2019-05-29 12:34:05 +00:00
|
|
|
if let Some(projid) = projid {
|
|
|
|
self.write_quota_project_id(projid)?;
|
|
|
|
}
|
2018-12-28 09:44:12 +00:00
|
|
|
|
2019-03-08 07:14:26 +00:00
|
|
|
let include_payload;
|
2019-01-12 09:20:08 +00:00
|
|
|
if is_virtual_file_system(magic) {
|
2019-01-12 14:43:20 +00:00
|
|
|
include_payload = false;
|
2019-10-25 16:44:51 +00:00
|
|
|
} else if let Some(ref set) = &self.device_set {
|
|
|
|
include_payload = set.contains(&stat.st_dev);
|
2019-01-12 14:43:20 +00:00
|
|
|
} else {
|
2019-10-25 16:44:51 +00:00
|
|
|
include_payload = true;
|
2019-01-12 14:43:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if !include_payload {
|
2019-03-15 11:19:51 +00:00
|
|
|
eprintln!("skip content: {:?}", self.full_path());
|
2019-08-02 13:19:34 +00:00
|
|
|
self.write_header(PXAR_PAYLOAD, 0)?;
|
2019-01-12 09:20:08 +00:00
|
|
|
return Ok(());
|
|
|
|
}
|
|
|
|
|
2018-12-28 09:44:12 +00:00
|
|
|
let size = stat.st_size as u64;
|
|
|
|
|
2019-08-02 13:19:34 +00:00
|
|
|
self.write_header(PXAR_PAYLOAD, size)?;
|
2018-12-28 09:44:12 +00:00
|
|
|
|
|
|
|
let mut pos: u64 = 0;
|
|
|
|
loop {
|
|
|
|
let n = match nix::unistd::read(filefd, &mut self.file_copy_buffer) {
|
|
|
|
Ok(n) => n,
|
2019-08-22 10:49:07 +00:00
|
|
|
Err(nix::Error::Sys(Errno::EINTR)) => continue, /* try again */
|
|
|
|
Err(err) => bail!("read {:?} failed - {}", self.full_path(), err),
|
2018-12-28 09:44:12 +00:00
|
|
|
};
|
|
|
|
if n == 0 { // EOF
|
|
|
|
if pos != size {
|
|
|
|
// Note:: casync format cannot handle that
|
2019-08-22 10:49:07 +00:00
|
|
|
bail!(
|
|
|
|
"detected shrinked file {:?} ({} < {})",
|
|
|
|
self.full_path(),
|
|
|
|
pos,
|
|
|
|
size
|
|
|
|
);
|
2018-12-28 09:44:12 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut next = pos + (n as u64);
|
|
|
|
|
2019-08-22 10:49:07 +00:00
|
|
|
if next > size {
|
|
|
|
next = size;
|
|
|
|
}
|
2018-12-28 09:44:12 +00:00
|
|
|
|
|
|
|
let count = (next - pos) as usize;
|
|
|
|
|
2018-12-28 10:48:47 +00:00
|
|
|
self.flush_copy_buffer(count)?;
|
2018-12-28 09:44:12 +00:00
|
|
|
|
2018-12-28 14:02:02 +00:00
|
|
|
pos = next;
|
2018-12-28 09:44:12 +00:00
|
|
|
|
2019-08-22 10:49:07 +00:00
|
|
|
if pos >= size {
|
|
|
|
break;
|
|
|
|
}
|
2018-12-28 09:44:12 +00:00
|
|
|
}
|
2018-12-28 08:55:26 +00:00
|
|
|
|
2018-12-27 13:24:31 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2019-08-22 10:49:07 +00:00
|
|
|
fn encode_device(&mut self, stat: &FileStat) -> Result<(), Error> {
|
2019-01-12 07:51:04 +00:00
|
|
|
let entry = self.create_entry(&stat)?;
|
2019-01-11 11:22:00 +00:00
|
|
|
|
|
|
|
self.write_entry(entry)?;
|
|
|
|
|
|
|
|
let major = unsafe { libc::major(stat.st_rdev) } as u64;
|
|
|
|
let minor = unsafe { libc::minor(stat.st_rdev) } as u64;
|
|
|
|
|
2019-03-15 11:19:51 +00:00
|
|
|
//println!("encode_device: {:?} {} {} {}", self.full_path(), stat.st_rdev, major, minor);
|
2019-01-11 11:22:00 +00:00
|
|
|
|
2019-08-02 13:19:34 +00:00
|
|
|
self.write_header(PXAR_DEVICE, std::mem::size_of::<PxarDevice>() as u64)?;
|
|
|
|
self.write_item(PxarDevice { major, minor })?;
|
2019-01-11 11:22:00 +00:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2019-01-12 07:51:04 +00:00
|
|
|
// FIFO or Socket
|
2019-08-22 10:49:07 +00:00
|
|
|
fn encode_special(&mut self, stat: &FileStat) -> Result<(), Error> {
|
2019-01-12 07:51:04 +00:00
|
|
|
let entry = self.create_entry(&stat)?;
|
|
|
|
|
|
|
|
self.write_entry(entry)?;
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2019-08-22 10:49:07 +00:00
|
|
|
fn encode_symlink(&mut self, target: &[u8], stat: &FileStat) -> Result<(), Error> {
|
2019-03-15 11:19:51 +00:00
|
|
|
//println!("encode_symlink: {:?} -> {:?}", self.full_path(), target);
|
2018-12-27 12:15:10 +00:00
|
|
|
|
2019-01-11 08:20:10 +00:00
|
|
|
let entry = self.create_entry(&stat)?;
|
2019-01-11 09:01:51 +00:00
|
|
|
self.write_entry(entry)?;
|
2018-12-28 08:55:26 +00:00
|
|
|
|
2019-08-02 13:19:34 +00:00
|
|
|
self.write_header(PXAR_SYMLINK, target.len() as u64)?;
|
2018-12-28 10:48:47 +00:00
|
|
|
self.write(target)?;
|
2018-12-28 08:55:26 +00:00
|
|
|
|
2018-12-27 12:15:10 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
2018-12-28 06:45:15 +00:00
|
|
|
|
2019-08-22 10:49:07 +00:00
|
|
|
fn encode_hardlink(&mut self, target: &[u8], offset: u64) -> Result<(), Error> {
|
2019-03-16 10:02:12 +00:00
|
|
|
//println!("encode_hardlink: {:?} -> {:?}", self.full_path(), target);
|
|
|
|
|
|
|
|
// Note: HARDLINK replaces an ENTRY.
|
|
|
|
self.write_header(PXAR_FORMAT_HARDLINK, (target.len() as u64) + 8)?;
|
|
|
|
self.write_item(offset)?;
|
|
|
|
self.write(target)?;
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2019-08-22 10:49:07 +00:00
|
|
|
fn encode_pxar_exclude(
|
|
|
|
&mut self,
|
|
|
|
filefd: RawFd,
|
|
|
|
stat: &FileStat,
|
|
|
|
magic: i64,
|
|
|
|
content: &[u8],
|
|
|
|
) -> Result<(), Error> {
|
2019-06-21 16:15:01 +00:00
|
|
|
let mut entry = self.create_entry(&stat)?;
|
|
|
|
|
|
|
|
self.read_chattr(filefd, &mut entry)?;
|
|
|
|
self.read_fat_attr(filefd, magic, &mut entry)?;
|
|
|
|
let (xattrs, fcaps) = self.read_xattrs(filefd, &stat)?;
|
|
|
|
let acl_access = self.read_acl(filefd, &stat, acl::ACL_TYPE_ACCESS)?;
|
|
|
|
let projid = self.read_quota_project_id(filefd, magic, &stat)?;
|
|
|
|
|
|
|
|
self.write_entry(entry)?;
|
|
|
|
for xattr in xattrs {
|
|
|
|
self.write_xattr(xattr)?;
|
|
|
|
}
|
|
|
|
self.write_fcaps(fcaps)?;
|
|
|
|
for user in acl_access.users {
|
|
|
|
self.write_acl_user(user)?;
|
|
|
|
}
|
|
|
|
for group in acl_access.groups {
|
|
|
|
self.write_acl_group(group)?;
|
|
|
|
}
|
|
|
|
if let Some(group_obj) = acl_access.group_obj {
|
|
|
|
self.write_acl_group_obj(group_obj)?;
|
|
|
|
}
|
|
|
|
if let Some(projid) = projid {
|
|
|
|
self.write_quota_project_id(projid)?;
|
|
|
|
}
|
|
|
|
|
|
|
|
let include_payload;
|
|
|
|
if is_virtual_file_system(magic) {
|
|
|
|
include_payload = false;
|
2019-10-25 16:44:51 +00:00
|
|
|
} else if let Some(set) = &self.device_set {
|
|
|
|
include_payload = set.contains(&stat.st_dev);
|
2019-06-21 16:15:01 +00:00
|
|
|
} else {
|
2019-10-25 16:44:51 +00:00
|
|
|
include_payload = true;
|
2019-06-21 16:15:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if !include_payload {
|
|
|
|
eprintln!("skip content: {:?}", self.full_path());
|
2019-08-02 13:19:34 +00:00
|
|
|
self.write_header(PXAR_PAYLOAD, 0)?;
|
2019-06-21 16:15:01 +00:00
|
|
|
return Ok(());
|
|
|
|
}
|
|
|
|
|
|
|
|
let size = content.len();
|
2019-08-02 13:19:34 +00:00
|
|
|
self.write_header(PXAR_PAYLOAD, size as u64)?;
|
2019-06-21 16:15:01 +00:00
|
|
|
self.writer.write_all(content)?;
|
|
|
|
self.writer_pos += size;
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2019-10-22 13:07:50 +00:00
|
|
|
/// Encodes the excude match patterns passed via cli as file in the archive.
|
|
|
|
fn encode_pxar_exclude_cli(
|
|
|
|
&mut self,
|
|
|
|
uid: u32,
|
|
|
|
gid: u32,
|
|
|
|
mtime: u64,
|
|
|
|
content: &[u8],
|
|
|
|
) -> Result<(), Error> {
|
|
|
|
let entry = PxarEntry {
|
|
|
|
mode: (libc::S_IFREG | 0o600) as u64,
|
|
|
|
flags: 0,
|
|
|
|
uid,
|
|
|
|
gid,
|
|
|
|
mtime,
|
|
|
|
};
|
|
|
|
self.write_entry(entry)?;
|
|
|
|
let size = content.len();
|
|
|
|
self.write_header(PXAR_PAYLOAD, size as u64)?;
|
|
|
|
self.writer.write_all(content)?;
|
|
|
|
self.writer_pos += size;
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2018-12-28 06:45:15 +00:00
|
|
|
// the report_XXX method may raise and error - depending on encoder configuration
|
2018-12-28 08:55:26 +00:00
|
|
|
|
2018-12-28 06:45:15 +00:00
|
|
|
fn report_vanished_file(&self, path: &Path) -> Result<(), Error> {
|
|
|
|
eprintln!("WARNING: detected vanished file {:?}", path);
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
2018-12-28 13:26:05 +00:00
|
|
|
}
|
2019-01-11 08:20:10 +00:00
|
|
|
|
|
|
|
fn errno_is_unsupported(errno: Errno) -> bool {
|
|
|
|
match errno {
|
2019-08-22 10:49:07 +00:00
|
|
|
Errno::ENOTTY | Errno::ENOSYS | Errno::EBADF | Errno::EOPNOTSUPP | Errno::EINVAL => true,
|
2019-01-11 08:20:10 +00:00
|
|
|
_ => false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-12 09:20:08 +00:00
|
|
|
fn detect_fs_type(fd: RawFd) -> Result<i64, Error> {
|
2019-08-22 10:49:06 +00:00
|
|
|
let mut fs_stat = std::mem::MaybeUninit::uninit();
|
|
|
|
let res = unsafe { libc::fstatfs(fd, fs_stat.as_mut_ptr()) };
|
2019-01-12 09:20:08 +00:00
|
|
|
Errno::result(res)?;
|
2019-08-22 10:49:06 +00:00
|
|
|
let fs_stat = unsafe { fs_stat.assume_init() };
|
2019-01-12 07:51:04 +00:00
|
|
|
|
|
|
|
Ok(fs_stat.f_type)
|
|
|
|
}
|
|
|
|
|
2019-01-12 08:18:21 +00:00
|
|
|
#[inline(always)]
|
2019-01-30 17:25:37 +00:00
|
|
|
pub fn is_temporary_file_system(magic: i64) -> bool {
|
2019-08-03 07:28:57 +00:00
|
|
|
use proxmox::sys::linux::magic::*;
|
2019-01-12 08:18:21 +00:00
|
|
|
magic == RAMFS_MAGIC || magic == TMPFS_MAGIC
|
|
|
|
}
|
|
|
|
|
2019-01-30 17:25:37 +00:00
|
|
|
pub fn is_virtual_file_system(magic: i64) -> bool {
|
2019-08-03 07:28:57 +00:00
|
|
|
use proxmox::sys::linux::magic::*;
|
2019-01-12 08:18:21 +00:00
|
|
|
|
|
|
|
match magic {
|
|
|
|
BINFMTFS_MAGIC |
|
|
|
|
CGROUP2_SUPER_MAGIC |
|
|
|
|
CGROUP_SUPER_MAGIC |
|
|
|
|
CONFIGFS_MAGIC |
|
|
|
|
DEBUGFS_MAGIC |
|
|
|
|
DEVPTS_SUPER_MAGIC |
|
|
|
|
EFIVARFS_MAGIC |
|
|
|
|
FUSE_CTL_SUPER_MAGIC |
|
|
|
|
HUGETLBFS_MAGIC |
|
|
|
|
MQUEUE_MAGIC |
|
|
|
|
NFSD_MAGIC |
|
|
|
|
PROC_SUPER_MAGIC |
|
|
|
|
PSTOREFS_MAGIC |
|
|
|
|
RPCAUTH_GSSMAGIC |
|
|
|
|
SECURITYFS_MAGIC |
|
|
|
|
SELINUX_MAGIC |
|
|
|
|
SMACK_MAGIC |
|
|
|
|
SYSFS_MAGIC => true,
|
|
|
|
_ => false
|
|
|
|
}
|
|
|
|
}
|