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.
|
2018-12-30 12:47:27 +00:00
|
|
|
|
2018-12-27 12:15:10 +00:00
|
|
|
use failure::*;
|
2019-01-11 08:20:10 +00:00
|
|
|
use endian_trait::Endian;
|
2019-03-15 11:19:51 +00:00
|
|
|
use std::collections::HashMap;
|
2018-12-27 12:15:10 +00:00
|
|
|
|
|
|
|
use super::format_definition::*;
|
2018-12-29 16:00:48 +00:00
|
|
|
use super::binary_search_tree::*;
|
2019-05-17 12:23:34 +00:00
|
|
|
use crate::tools::xattr;
|
2018-12-27 12:15:10 +00:00
|
|
|
|
|
|
|
use std::io::Write;
|
|
|
|
use std::os::unix::io::AsRawFd;
|
|
|
|
use std::os::unix::ffi::OsStrExt;
|
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
|
|
|
|
2018-12-28 13:26:05 +00:00
|
|
|
use std::ffi::CStr;
|
2018-12-28 10:48:47 +00:00
|
|
|
|
2018-12-28 09:44:12 +00:00
|
|
|
use nix::NixPath;
|
2018-12-28 06:14:12 +00:00
|
|
|
use nix::fcntl::OFlag;
|
|
|
|
use nix::sys::stat::Mode;
|
2018-12-28 06:45:15 +00:00
|
|
|
use nix::errno::Errno;
|
2018-12-28 08:55:26 +00:00
|
|
|
use nix::sys::stat::FileStat;
|
2018-12-27 12:15:10 +00:00
|
|
|
|
2018-12-30 13:09:59 +00:00
|
|
|
/// The format requires to build sorted directory lookup tables in
|
|
|
|
/// memory, so we restrict the number of allowed entries to limit
|
|
|
|
/// maximum memory usage.
|
|
|
|
pub const MAX_DIRECTORY_ENTRIES: usize = 256*1024;
|
2018-12-28 09:44:12 +00:00
|
|
|
|
2019-03-15 11:19:51 +00:00
|
|
|
#[derive(Eq, PartialEq, Hash)]
|
|
|
|
struct HardLinkInfo {
|
|
|
|
st_dev: u64,
|
|
|
|
st_ino: u64,
|
|
|
|
}
|
|
|
|
|
2019-03-15 07:24:32 +00:00
|
|
|
pub struct Encoder<'a, W: Write> {
|
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-01-30 17:25:37 +00:00
|
|
|
_size: usize,
|
2018-12-28 10:48:47 +00:00
|
|
|
file_copy_buffer: Vec<u8>,
|
2019-03-08 07:14:26 +00:00
|
|
|
all_file_systems: bool,
|
|
|
|
root_st_dev: u64,
|
2019-03-04 07:01:09 +00:00
|
|
|
verbose: bool,
|
2019-03-16 10:02:12 +00:00
|
|
|
hardlinks: HashMap<HardLinkInfo, (PathBuf, u64)>,
|
2018-12-27 12:15:10 +00:00
|
|
|
}
|
|
|
|
|
2019-03-15 07:24:32 +00:00
|
|
|
impl <'a, W: Write> Encoder<'a, W> {
|
2018-12-28 13:26:05 +00:00
|
|
|
|
2019-03-15 11:19:51 +00:00
|
|
|
// used for error reporting
|
|
|
|
fn full_path(&self) -> PathBuf {
|
|
|
|
self.base_path.join(&self.relative_path)
|
|
|
|
}
|
|
|
|
|
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-03-08 08:33:53 +00:00
|
|
|
all_file_systems: bool,
|
2019-03-04 07:01:09 +00:00
|
|
|
verbose: bool,
|
2019-01-12 14:43:20 +00:00
|
|
|
) -> Result<(), Error> {
|
2018-12-28 10:48:47 +00:00
|
|
|
|
2018-12-30 13:09:59 +00:00
|
|
|
const FILE_COPY_BUFFER_SIZE: usize = 1024*1024;
|
|
|
|
|
2018-12-28 10:48:47 +00:00
|
|
|
let mut file_copy_buffer = Vec::with_capacity(FILE_COPY_BUFFER_SIZE);
|
|
|
|
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();
|
|
|
|
let stat = match nix::sys::stat::fstat(dir_fd) {
|
2019-01-11 08:20:10 +00:00
|
|
|
Ok(stat) => stat,
|
2019-03-08 07:14:26 +00:00
|
|
|
Err(err) => bail!("fstat {:?} failed - {}", path, err),
|
2019-01-11 08:20:10 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
if (stat.st_mode & libc::S_IFMT) != libc::S_IFDIR {
|
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-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-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-03-08 07:14:26 +00:00
|
|
|
writer: writer,
|
|
|
|
writer_pos: 0,
|
|
|
|
_size: 0,
|
|
|
|
file_copy_buffer,
|
|
|
|
all_file_systems,
|
|
|
|
root_st_dev: stat.st_dev,
|
|
|
|
verbose,
|
2019-03-15 11:19:51 +00:00
|
|
|
hardlinks: HashMap::new(),
|
2019-03-08 07:14:26 +00:00
|
|
|
};
|
2019-01-12 14:43:20 +00:00
|
|
|
|
2019-03-15 11:19:51 +00:00
|
|
|
if verbose { println!("{:?}", me.full_path()); }
|
2019-03-04 07:01:09 +00:00
|
|
|
|
2019-01-12 07:51:04 +00:00
|
|
|
me.encode_dir(dir, &stat, magic)?;
|
2018-12-27 12:15:10 +00:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2018-12-28 10:48:47 +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-01-11 09:01:51 +00:00
|
|
|
fn write_item<T: Endian>(&mut self, item: T) -> Result<(), Error> {
|
2019-01-11 08:20:10 +00:00
|
|
|
|
2019-01-11 09:01:51 +00:00
|
|
|
let data = item.to_le();
|
2019-01-11 08:20:10 +00:00
|
|
|
|
|
|
|
let buffer = unsafe { std::slice::from_raw_parts(
|
|
|
|
&data as *const T as *const u8,
|
|
|
|
std::mem::size_of::<T>()
|
|
|
|
)};
|
|
|
|
|
|
|
|
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-01-11 08:20:10 +00:00
|
|
|
let size = size + (std::mem::size_of::<CaFormatHeader>() as u64);
|
2019-01-11 09:01:51 +00:00
|
|
|
self.write_item(CaFormatHeader { 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();
|
|
|
|
self.write_header(CA_FORMAT_FILENAME, buffer.len() as u64)?;
|
|
|
|
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-01-11 08:20:10 +00:00
|
|
|
fn create_entry(&self, stat: &FileStat) -> Result<CaFormatEntry, Error> {
|
2018-12-28 08:55:26 +00:00
|
|
|
|
2019-01-11 08:20:10 +00:00
|
|
|
let mode = if (stat.st_mode & libc::S_IFMT) == libc::S_IFLNK {
|
|
|
|
(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-01-11 08:20:10 +00:00
|
|
|
let entry = CaFormatEntry {
|
2019-01-12 15:49:02 +00:00
|
|
|
feature_flags: CA_FORMAT_DEFAULT, // fixme: ??
|
2019-01-11 08:20:10 +00:00
|
|
|
mode: mode,
|
|
|
|
flags: 0,
|
|
|
|
uid: stat.st_uid as u64,
|
|
|
|
gid: stat.st_gid as u64,
|
|
|
|
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-01-11 08:20:10 +00:00
|
|
|
fn read_chattr(&self, fd: RawFd, entry: &mut CaFormatEntry) -> Result<(), Error> {
|
2018-12-28 08:55:26 +00:00
|
|
|
|
2019-01-11 09:18:22 +00:00
|
|
|
let mut attr: usize = 0;
|
|
|
|
|
|
|
|
let res = unsafe { read_attr_fd(fd, &mut attr)};
|
|
|
|
if let Err(err) = res {
|
|
|
|
if let nix::Error::Sys(errno) = err {
|
|
|
|
if errno_is_unsupported(errno) { return Ok(()) };
|
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
|
|
|
let flags = ca_feature_flags_from_chattr(attr as u32);
|
|
|
|
entry.flags = entry.flags | flags;
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2019-01-12 09:28:26 +00:00
|
|
|
fn read_fat_attr(&self, fd: RawFd, magic: i64, entry: &mut CaFormatEntry) -> Result<(), Error> {
|
|
|
|
|
|
|
|
if magic != MSDOS_SUPER_MAGIC && magic != FUSE_SUPER_MAGIC { return Ok(()); }
|
2019-01-11 09:18:22 +00:00
|
|
|
|
|
|
|
let mut attr: u32 = 0;
|
|
|
|
|
|
|
|
let res = unsafe { read_fat_attr_fd(fd, &mut attr)};
|
|
|
|
if let Err(err) = res {
|
|
|
|
if let nix::Error::Sys(errno) = err {
|
|
|
|
if errno_is_unsupported(errno) { return Ok(()) };
|
|
|
|
}
|
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-01-11 09:18:22 +00:00
|
|
|
let flags = ca_feature_flags_from_fat_attr(attr);
|
|
|
|
entry.flags = entry.flags | flags;
|
|
|
|
|
2019-01-11 08:20:10 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
2018-12-28 08:55:26 +00:00
|
|
|
|
2019-05-17 12:23:34 +00:00
|
|
|
fn read_xattrs(&self, fd: RawFd, stat: &FileStat, entry: &CaFormatEntry) -> Result<(Vec<CaFormatXAttr>, Option<CaFormatFCaps>), Error> {
|
|
|
|
let mut xattrs = Vec::new();
|
|
|
|
let mut fcaps = None;
|
|
|
|
|
|
|
|
let flags = CA_FORMAT_WITH_XATTRS | CA_FORMAT_WITH_FCAPS;
|
|
|
|
if (entry.feature_flags & flags) == 0 { return Ok((xattrs, fcaps)); }
|
|
|
|
// Should never be called on symlinks, just in case check anyway
|
|
|
|
if (stat.st_mode & libc::S_IFMT) == libc::S_IFLNK { return Ok((xattrs, fcaps)); }
|
|
|
|
|
|
|
|
let xattr_names = match xattr::flistxattr(fd) {
|
|
|
|
Ok(names) => names,
|
|
|
|
Err(Errno::EOPNOTSUPP) => return Ok((xattrs, fcaps)),
|
|
|
|
Err(Errno::EBADF) => return Ok((xattrs, fcaps)),
|
|
|
|
Err(err) => bail!("read_xattrs failed for {:?} - {}", self.full_path(), err),
|
|
|
|
};
|
|
|
|
|
|
|
|
for name in xattr_names.split(|c| *c == '\0' as u8) {
|
|
|
|
// Only extract the relevant extended attributes
|
|
|
|
if !xattr::name_store(&name) { continue; }
|
|
|
|
|
|
|
|
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),
|
|
|
|
};
|
|
|
|
|
|
|
|
if xattr::security_capability(&name) {
|
|
|
|
// fcaps are stored in own format within the archive
|
|
|
|
fcaps = Some(CaFormatFCaps {
|
|
|
|
data: value,
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
xattrs.push(CaFormatXAttr {
|
|
|
|
name: name.to_vec(),
|
|
|
|
value: value,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
xattrs.sort();
|
|
|
|
|
|
|
|
Ok((xattrs, fcaps))
|
|
|
|
}
|
|
|
|
|
2019-01-11 09:01:51 +00:00
|
|
|
fn write_entry(&mut self, entry: CaFormatEntry) -> Result<(), Error> {
|
2019-01-11 08:20:10 +00:00
|
|
|
|
|
|
|
self.write_header(CA_FORMAT_ENTRY, std::mem::size_of::<CaFormatEntry>() as u64)?;
|
|
|
|
self.write_item(entry)?;
|
2018-12-28 08:55:26 +00:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
2018-12-28 06:45:15 +00:00
|
|
|
|
2019-05-17 12:23:34 +00:00
|
|
|
fn write_xattr(&mut self, xattr: CaFormatXAttr) -> Result<(), Error> {
|
|
|
|
let size = xattr.name.len() + xattr.value.len() + 1; // +1 for '\0' separating name and value
|
|
|
|
self.write_header(CA_FORMAT_XATTR, size as u64)?;
|
|
|
|
self.write(xattr.name.as_slice())?;
|
|
|
|
self.write(&[0])?;
|
|
|
|
self.write(xattr.value.as_slice())?;
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn write_fcaps(&mut self, fcaps: Option<CaFormatFCaps>) -> Result<(), Error> {
|
|
|
|
if let Some(fcaps) = fcaps {
|
|
|
|
let size = fcaps.data.len();
|
|
|
|
self.write_header(CA_FORMAT_FCAPS, size as u64)?;
|
|
|
|
self.write(fcaps.data.as_slice())?;
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2018-12-31 09:11:28 +00:00
|
|
|
fn write_goodbye_table(&mut self, goodbye_offset: usize, goodbye_items: &mut [CaFormatGoodbyeItem]) -> Result<(), Error> {
|
|
|
|
|
|
|
|
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();
|
|
|
|
|
|
|
|
let goodbye_table_size = (item_count + 1)*std::mem::size_of::<CaFormatGoodbyeItem>();
|
|
|
|
|
|
|
|
self.write_header(CA_FORMAT_GOODBYE, goodbye_table_size as u64)?;
|
|
|
|
|
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);
|
|
|
|
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];
|
|
|
|
let offset = d*std::mem::size_of::<CaFormatGoodbyeItem>();
|
|
|
|
let dest = crate::tools::map_struct_mut::<CaFormatGoodbyeItem>(&mut buffer[offset..]).unwrap();
|
|
|
|
dest.offset = u64::to_le(item.offset);
|
|
|
|
dest.size = u64::to_le(item.size);
|
|
|
|
dest.hash = u64::to_le(item.hash);
|
|
|
|
});
|
|
|
|
|
|
|
|
// append CaFormatGoodbyeTail as last item
|
|
|
|
let offset = item_count*std::mem::size_of::<CaFormatGoodbyeItem>();
|
|
|
|
let dest = crate::tools::map_struct_mut::<CaFormatGoodbyeItem>(&mut buffer[offset..]).unwrap();
|
|
|
|
dest.offset = u64::to_le(goodbye_offset as u64);
|
|
|
|
dest.size = u64::to_le((goodbye_table_size + std::mem::size_of::<CaFormatHeader>()) as u64);
|
|
|
|
dest.hash = u64::to_le(CA_FORMAT_GOODBYE_TAIL_MARKER);
|
|
|
|
|
|
|
|
self.flush_copy_buffer(goodbye_table_size)?;
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2019-01-12 07:51:04 +00:00
|
|
|
fn encode_dir(&mut self, dir: &mut nix::dir::Dir, dir_stat: &FileStat, magic: i64) -> Result<(), Error> {
|
2018-12-27 12:15:10 +00:00
|
|
|
|
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
|
|
|
|
|
|
|
let mut name_list = vec![];
|
|
|
|
|
|
|
|
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-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-05-17 12:23:34 +00:00
|
|
|
let (xattrs, fcaps) = self.read_xattrs(rawfd, &dir_stat, &dir_entry)?;
|
2018-12-28 08:55:26 +00:00
|
|
|
|
2019-01-11 09:01:51 +00:00
|
|
|
self.write_entry(dir_entry)?;
|
2019-05-17 12:23:34 +00:00
|
|
|
for xattr in xattrs { self.write_xattr(xattr)?; }
|
|
|
|
self.write_fcaps(fcaps)?;
|
2018-12-28 10:48:47 +00:00
|
|
|
|
2018-12-30 13:09:59 +00:00
|
|
|
let mut dir_count = 0;
|
|
|
|
|
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;
|
|
|
|
} else {
|
2019-03-08 07:14:26 +00:00
|
|
|
include_children = (self.root_st_dev == dir_stat.st_dev) || self.all_file_systems;
|
2019-01-12 14:43:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if include_children {
|
2019-01-12 09:20:08 +00:00
|
|
|
for entry in dir.iter() {
|
|
|
|
dir_count += 1;
|
|
|
|
if dir_count > MAX_DIRECTORY_ENTRIES {
|
|
|
|
bail!("too many directory items in {:?} (> {})",
|
2019-03-15 11:19:51 +00:00
|
|
|
self.full_path(), MAX_DIRECTORY_ENTRIES);
|
2019-01-12 09:20:08 +00:00
|
|
|
}
|
2018-12-30 13:09:59 +00:00
|
|
|
|
2019-01-12 09:20:08 +00:00
|
|
|
let entry = match entry {
|
|
|
|
Ok(entry) => entry,
|
2019-03-15 11:19:51 +00:00
|
|
|
Err(err) => bail!("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();
|
|
|
|
let name_len = name.len();
|
|
|
|
if name_len == 2 && name[0] == b'.' && name[1] == 0u8 { continue; }
|
|
|
|
if name_len == 3 && name[0] == b'.' && name[1] == b'.' && name[2] == 0u8 { continue; }
|
2018-12-27 12:15:10 +00:00
|
|
|
|
2019-01-12 09:20:08 +00:00
|
|
|
name_list.push(filename);
|
|
|
|
}
|
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-01-11 08:20:10 +00:00
|
|
|
name_list.sort_unstable_by(|a, b| a.cmp(&b));
|
2018-12-27 12:15:10 +00:00
|
|
|
|
2018-12-29 16:26:32 +00:00
|
|
|
let mut goodbye_items = vec![];
|
2018-12-28 13:26:05 +00:00
|
|
|
|
2019-01-11 08:20:10 +00:00
|
|
|
for filename in &name_list {
|
2019-03-15 11:19:51 +00:00
|
|
|
self.relative_path.push(std::ffi::OsStr::from_bytes(filename.as_bytes()));
|
2018-12-27 12:15:10 +00:00
|
|
|
|
2019-03-15 11:19:51 +00:00
|
|
|
if self.verbose { println!("{:?}", self.full_path()); }
|
2019-03-04 07:01:09 +00:00
|
|
|
|
2019-01-11 08:20:10 +00:00
|
|
|
let stat = match nix::sys::stat::fstatat(rawfd, filename.as_ref(), nix::fcntl::AtFlags::AT_SYMLINK_NOFOLLOW) {
|
|
|
|
Ok(stat) => stat,
|
|
|
|
Err(nix::Error::Sys(Errno::ENOENT)) => {
|
2019-03-15 11:19:51 +00:00
|
|
|
self.report_vanished_file(&self.full_path())?;
|
2019-01-11 08:20:10 +00:00
|
|
|
continue;
|
|
|
|
}
|
2019-03-15 11:19:51 +00:00
|
|
|
Err(err) => bail!("fstat {:?} failed - {}", self.full_path(), err),
|
2019-01-11 08:20:10 +00:00
|
|
|
};
|
|
|
|
|
2018-12-28 13:26:05 +00:00
|
|
|
let start_pos = self.writer_pos;
|
|
|
|
|
2019-01-11 11:22:00 +00:00
|
|
|
let ifmt = stat.st_mode & libc::S_IFMT;
|
|
|
|
|
|
|
|
if ifmt == libc::S_IFDIR {
|
2018-12-28 13:26:05 +00:00
|
|
|
|
2019-01-12 07:51:04 +00:00
|
|
|
let mut dir = match nix::dir::Dir::openat(rawfd, filename.as_ref(), OFlag::O_DIRECTORY|OFlag::O_NOFOLLOW, Mode::empty()) {
|
|
|
|
Ok(dir) => dir,
|
|
|
|
Err(nix::Error::Sys(Errno::ENOENT)) => {
|
2019-03-15 11:19:51 +00:00
|
|
|
self.report_vanished_file(&self.full_path())?;
|
2019-01-12 07:51:04 +00:00
|
|
|
continue; // fixme!!
|
|
|
|
},
|
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)?;
|
|
|
|
self.encode_dir(&mut dir, &stat, child_magic)?;
|
2018-12-28 08:55:26 +00:00
|
|
|
|
2019-01-11 11:22:00 +00:00
|
|
|
} else if ifmt == libc::S_IFREG {
|
2019-03-15 11:19:51 +00:00
|
|
|
|
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 {
|
|
|
|
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 {
|
|
|
|
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 {
|
|
|
|
|
|
|
|
self.write_filename(&filename)?;
|
|
|
|
self.encode_hardlink(target.as_bytes(), offset)?;
|
2019-01-12 07:51:04 +00:00
|
|
|
|
2019-01-12 09:20:08 +00:00
|
|
|
} else {
|
|
|
|
|
2019-03-16 10:02:12 +00:00
|
|
|
let filefd = match nix::fcntl::openat(rawfd, filename.as_ref(), OFlag::O_NOFOLLOW, Mode::empty()) {
|
|
|
|
Ok(filefd) => filefd,
|
|
|
|
Err(nix::Error::Sys(Errno::ENOENT)) => {
|
|
|
|
self.report_vanished_file(&self.full_path())?;
|
|
|
|
continue;
|
|
|
|
},
|
|
|
|
Err(err) => bail!("open file {:?} failed - {}", self.full_path(), err),
|
|
|
|
};
|
|
|
|
|
|
|
|
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-01-12 07:51:04 +00:00
|
|
|
|
2019-01-11 11:22:00 +00:00
|
|
|
} else if ifmt == libc::S_IFLNK {
|
2018-12-28 06:14:12 +00:00
|
|
|
let mut buffer = [0u8; libc::PATH_MAX as usize];
|
2018-12-28 08:55:26 +00:00
|
|
|
|
|
|
|
let res = filename.with_nix_path(|cstr| {
|
2018-12-28 13:51:43 +00:00
|
|
|
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) => {
|
|
|
|
buffer[len as usize] = 0u8; // add Nul byte
|
2019-01-12 07:51:04 +00:00
|
|
|
self.write_filename(&filename)?;
|
2018-12-28 13:51:43 +00:00
|
|
|
self.encode_symlink(&buffer[..((len+1) as usize)], &stat)?
|
|
|
|
}
|
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())?;
|
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-01-11 11:22:00 +00:00
|
|
|
} else if (ifmt == libc::S_IFBLK) || (ifmt == libc::S_IFCHR) {
|
2019-01-12 07:51:04 +00:00
|
|
|
self.write_filename(&filename)?;
|
2019-01-11 11:22:00 +00:00
|
|
|
self.encode_device(&stat)?;
|
2019-01-11 12:26:05 +00:00
|
|
|
} else if (ifmt == libc::S_IFIFO) || (ifmt == libc::S_IFSOCK) {
|
2019-01-12 07:51:04 +00:00
|
|
|
self.write_filename(&filename)?;
|
|
|
|
self.encode_special(&stat)?;
|
2018-12-27 13:24:31 +00:00
|
|
|
} else {
|
2019-03-15 11:19:51 +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;
|
|
|
|
|
2018-12-29 16:26:32 +00:00
|
|
|
goodbye_items.push(CaFormatGoodbyeItem {
|
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)?;
|
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-01-12 09:20:08 +00:00
|
|
|
fn encode_file(&mut self, filefd: RawFd, stat: &FileStat, magic: i64) -> Result<(), Error> {
|
2018-12-27 13:24:31 +00:00
|
|
|
|
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-17 12:23:34 +00:00
|
|
|
let (xattrs, fcaps) = self.read_xattrs(filefd, &stat, &entry)?;
|
2018-12-28 09:44:12 +00:00
|
|
|
|
2019-01-11 09:01:51 +00:00
|
|
|
self.write_entry(entry)?;
|
2019-05-17 12:23:34 +00:00
|
|
|
for xattr in xattrs { self.write_xattr(xattr)?; }
|
|
|
|
self.write_fcaps(fcaps)?;
|
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;
|
|
|
|
} else {
|
2019-03-08 07:14:26 +00:00
|
|
|
include_payload = (stat.st_dev == self.root_st_dev) || self.all_file_systems;
|
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-01-12 09:20:08 +00:00
|
|
|
self.write_header(CA_FORMAT_PAYLOAD, 0)?;
|
|
|
|
return Ok(());
|
|
|
|
}
|
|
|
|
|
2018-12-28 09:44:12 +00:00
|
|
|
let size = stat.st_size as u64;
|
|
|
|
|
|
|
|
self.write_header(CA_FORMAT_PAYLOAD, size)?;
|
|
|
|
|
|
|
|
let mut pos: u64 = 0;
|
|
|
|
loop {
|
|
|
|
let n = match nix::unistd::read(filefd, &mut self.file_copy_buffer) {
|
|
|
|
Ok(n) => n,
|
|
|
|
Err(nix::Error::Sys(Errno::EINTR)) => continue /* try again */,
|
2019-03-15 11:19:51 +00:00
|
|
|
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-03-15 11:19:51 +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);
|
|
|
|
|
|
|
|
if next > size { next = size; }
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
if pos >= size { break; }
|
|
|
|
}
|
2018-12-28 08:55:26 +00:00
|
|
|
|
2018-12-27 13:24:31 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2019-01-11 11:22:00 +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
|
|
|
|
|
|
|
self.write_header(CA_FORMAT_DEVICE, std::mem::size_of::<CaFormatDevice>() as u64)?;
|
|
|
|
self.write_item(CaFormatDevice { major, minor })?;
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2019-01-12 07:51:04 +00:00
|
|
|
// FIFO or Socket
|
|
|
|
fn encode_special(&mut self, stat: &FileStat) -> Result<(), Error> {
|
|
|
|
|
|
|
|
let entry = self.create_entry(&stat)?;
|
|
|
|
|
|
|
|
self.write_entry(entry)?;
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2018-12-28 13:26:05 +00:00
|
|
|
fn encode_symlink(&mut self, target: &[u8], stat: &FileStat) -> Result<(), Error> {
|
2018-12-27 13:24:31 +00:00
|
|
|
|
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
|
|
|
|
2018-12-28 09:44:12 +00:00
|
|
|
self.write_header(CA_FORMAT_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-03-16 10:02:12 +00:00
|
|
|
fn encode_hardlink(&mut self, target: &[u8], offset: u64) -> Result<(), Error> {
|
|
|
|
|
|
|
|
//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(())
|
|
|
|
}
|
|
|
|
|
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 {
|
|
|
|
Errno::ENOTTY | Errno::ENOSYS | Errno::EBADF | Errno::EOPNOTSUPP | Errno::EINVAL => {
|
|
|
|
true
|
|
|
|
}
|
|
|
|
_ => false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-12 09:20:08 +00:00
|
|
|
fn detect_fs_type(fd: RawFd) -> Result<i64, Error> {
|
2019-01-12 07:51:04 +00:00
|
|
|
let mut fs_stat: libc::statfs = unsafe { std::mem::uninitialized() };
|
2019-01-12 09:20:08 +00:00
|
|
|
let res = unsafe { libc::fstatfs(fd, &mut fs_stat) };
|
|
|
|
Errno::result(res)?;
|
2019-01-12 07:51:04 +00:00
|
|
|
|
|
|
|
Ok(fs_stat.f_type)
|
|
|
|
}
|
|
|
|
|
2019-01-11 08:20:10 +00:00
|
|
|
use nix::{convert_ioctl_res, request_code_read, ioc};
|
2019-01-11 09:01:51 +00:00
|
|
|
|
2019-01-11 08:20:10 +00:00
|
|
|
// /usr/include/linux/fs.h: #define FS_IOC_GETFLAGS _IOR('f', 1, long)
|
|
|
|
/// read Linux file system attributes (see man chattr)
|
|
|
|
nix::ioctl_read!(read_attr_fd, b'f', 1, usize);
|
|
|
|
|
2019-01-11 09:18:22 +00:00
|
|
|
// /usr/include/linux/msdos_fs.h: #define FAT_IOCTL_GET_ATTRIBUTES _IOR('r', 0x10, __u32)
|
|
|
|
// read FAT file system attributes
|
|
|
|
nix::ioctl_read!(read_fat_attr_fd, b'r', 0x10, u32);
|
2019-01-12 08:18:21 +00:00
|
|
|
|
|
|
|
|
|
|
|
// from /usr/include/linux/magic.h
|
|
|
|
// and from casync util.h
|
2019-01-30 17:25:37 +00:00
|
|
|
pub const BINFMTFS_MAGIC: i64 = 0x42494e4d;
|
|
|
|
pub const CGROUP2_SUPER_MAGIC: i64 = 0x63677270;
|
|
|
|
pub const CGROUP_SUPER_MAGIC: i64 = 0x0027e0eb;
|
|
|
|
pub const CONFIGFS_MAGIC: i64 = 0x62656570;
|
|
|
|
pub const DEBUGFS_MAGIC: i64 = 0x64626720;
|
|
|
|
pub const DEVPTS_SUPER_MAGIC: i64 = 0x00001cd1;
|
|
|
|
pub const EFIVARFS_MAGIC: i64 = 0xde5e81e4;
|
|
|
|
pub const FUSE_CTL_SUPER_MAGIC: i64 = 0x65735543;
|
|
|
|
pub const HUGETLBFS_MAGIC: i64 = 0x958458f6;
|
|
|
|
pub const MQUEUE_MAGIC: i64 = 0x19800202;
|
|
|
|
pub const NFSD_MAGIC: i64 = 0x6e667364;
|
|
|
|
pub const PROC_SUPER_MAGIC: i64 = 0x00009fa0;
|
|
|
|
pub const PSTOREFS_MAGIC: i64 = 0x6165676C;
|
|
|
|
pub const RPCAUTH_GSSMAGIC: i64 = 0x67596969;
|
|
|
|
pub const SECURITYFS_MAGIC: i64 = 0x73636673;
|
|
|
|
pub const SELINUX_MAGIC: i64 = 0xf97cff8c;
|
|
|
|
pub const SMACK_MAGIC: i64 = 0x43415d53;
|
|
|
|
pub const RAMFS_MAGIC: i64 = 0x858458f6;
|
|
|
|
pub const TMPFS_MAGIC: i64 = 0x01021994;
|
|
|
|
pub const SYSFS_MAGIC: i64 = 0x62656572;
|
|
|
|
pub const MSDOS_SUPER_MAGIC: i64 = 0x00004d44;
|
|
|
|
pub const FUSE_SUPER_MAGIC: i64 = 0x65735546;
|
2019-01-12 09:28:26 +00:00
|
|
|
|
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-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-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
|
|
|
|
}
|
|
|
|
}
|