catar/encoder.rs: cleanups

This commit is contained in:
Dietmar Maurer 2019-01-11 10:01:51 +01:00
parent 4f6892eb74
commit 8c1dfa6c72
2 changed files with 24 additions and 14 deletions

View File

@ -74,11 +74,9 @@ impl <'a, W: Write> CaTarEncoder<'a, W> {
Ok(()) Ok(())
} }
fn write_item<T: Endian + Clone>(&mut self, item: &T) -> Result<(), Error> { fn write_item<T: Endian>(&mut self, item: T) -> Result<(), Error> {
let mut data: T = unsafe { std::mem::uninitialized() }; let data = item.to_le();
data = (*item).clone().to_le();
let buffer = unsafe { std::slice::from_raw_parts( let buffer = unsafe { std::slice::from_raw_parts(
&data as *const T as *const u8, &data as *const T as *const u8,
@ -99,7 +97,7 @@ impl <'a, W: Write> CaTarEncoder<'a, W> {
fn write_header(&mut self, htype: u64, size: u64) -> Result<(), Error> { fn write_header(&mut self, htype: u64, size: u64) -> Result<(), Error> {
let size = size + (std::mem::size_of::<CaFormatHeader>() as u64); let size = size + (std::mem::size_of::<CaFormatHeader>() as u64);
self.write_item(&CaFormatHeader { size, htype })?; self.write_item(CaFormatHeader { size, htype })?;
Ok(()) Ok(())
} }
@ -149,7 +147,7 @@ impl <'a, W: Write> CaTarEncoder<'a, W> {
Ok(()) Ok(())
} }
fn write_entry(&mut self, entry: &CaFormatEntry) -> Result<(), Error> { fn write_entry(&mut self, entry: CaFormatEntry) -> Result<(), Error> {
self.write_header(CA_FORMAT_ENTRY, std::mem::size_of::<CaFormatEntry>() as u64)?; self.write_header(CA_FORMAT_ENTRY, std::mem::size_of::<CaFormatEntry>() as u64)?;
self.write_item(entry)?; self.write_item(entry)?;
@ -210,7 +208,7 @@ impl <'a, W: Write> CaTarEncoder<'a, W> {
self.read_chattr(rawfd, &mut dir_entry)?; self.read_chattr(rawfd, &mut dir_entry)?;
self.write_entry(&dir_entry)?; self.write_entry(dir_entry)?;
let mut dir_count = 0; let mut dir_count = 0;
@ -327,7 +325,7 @@ impl <'a, W: Write> CaTarEncoder<'a, W> {
self.read_chattr(filefd, &mut entry)?; self.read_chattr(filefd, &mut entry)?;
self.write_entry(&entry)?; self.write_entry(entry)?;
let size = stat.st_size as u64; let size = stat.st_size as u64;
@ -369,7 +367,7 @@ impl <'a, W: Write> CaTarEncoder<'a, W> {
//println!("encode_symlink: {:?} -> {:?}", self.current_path, target); //println!("encode_symlink: {:?} -> {:?}", self.current_path, target);
let entry = self.create_entry(&stat)?; let entry = self.create_entry(&stat)?;
self.write_entry(&entry)?; self.write_entry(entry)?;
self.write_header(CA_FORMAT_SYMLINK, target.len() as u64)?; self.write_header(CA_FORMAT_SYMLINK, target.len() as u64)?;
self.write(target)?; self.write(target)?;
@ -398,6 +396,7 @@ fn errno_is_unsupported(errno: Errno) -> bool {
} }
use nix::{convert_ioctl_res, request_code_read, ioc}; use nix::{convert_ioctl_res, request_code_read, ioc};
// /usr/include/linux/fs.h: #define FS_IOC_GETFLAGS _IOR('f', 1, long) // /usr/include/linux/fs.h: #define FS_IOC_GETFLAGS _IOR('f', 1, long)
/// read Linux file system attributes (see man chattr) /// read Linux file system attributes (see man chattr)
nix::ioctl_read!(read_attr_fd, b'f', 1, usize); nix::ioctl_read!(read_attr_fd, b'f', 1, usize);

View File

@ -22,26 +22,38 @@ pub const CA_FORMAT_GOODBYE_TAIL_MARKER: u64 = 0x57446fa533702943;
// Feature flags // Feature flags
// DOS file flags /// DOS file flag `HIDDEN`
pub const CA_FORMAT_WITH_FLAG_HIDDEN: u64 = 0x2000; pub const CA_FORMAT_WITH_FLAG_HIDDEN: u64 = 0x2000;
/// DOS file flag `SYSTEM`
pub const CA_FORMAT_WITH_FLAG_SYSTEM: u64 = 0x4000; pub const CA_FORMAT_WITH_FLAG_SYSTEM: u64 = 0x4000;
/// DOS file flag `ARCHIVE`
pub const CA_FORMAT_WITH_FLAG_ARCHIVE: u64 = 0x8000; pub const CA_FORMAT_WITH_FLAG_ARCHIVE: u64 = 0x8000;
// chattr() flags // chattr() flags#
/// Linux file attribute `APPEND`
pub const CA_FORMAT_WITH_FLAG_APPEND: u64 = 0x10000; pub const CA_FORMAT_WITH_FLAG_APPEND: u64 = 0x10000;
/// Linux file attribute `NOATIME`
pub const CA_FORMAT_WITH_FLAG_NOATIME: u64 = 0x20000; pub const CA_FORMAT_WITH_FLAG_NOATIME: u64 = 0x20000;
/// Linux file attribute `COMPR`
pub const CA_FORMAT_WITH_FLAG_COMPR: u64 = 0x40000; pub const CA_FORMAT_WITH_FLAG_COMPR: u64 = 0x40000;
/// Linux file attribute `NOCOW`
pub const CA_FORMAT_WITH_FLAG_NOCOW: u64 = 0x80000; pub const CA_FORMAT_WITH_FLAG_NOCOW: u64 = 0x80000;
/// Linux file attribute `NODUMP`
pub const CA_FORMAT_WITH_FLAG_NODUMP: u64 = 0x100000; pub const CA_FORMAT_WITH_FLAG_NODUMP: u64 = 0x100000;
/// Linux file attribute `DIRSYNC`
pub const CA_FORMAT_WITH_FLAG_DIRSYNC: u64 = 0x200000; pub const CA_FORMAT_WITH_FLAG_DIRSYNC: u64 = 0x200000;
/// Linux file attribute `IMMUTABLE`
pub const CA_FORMAT_WITH_FLAG_IMMUTABLE: u64 = 0x400000; pub const CA_FORMAT_WITH_FLAG_IMMUTABLE: u64 = 0x400000;
/// Linux file attribute `SYNC`
pub const CA_FORMAT_WITH_FLAG_SYNC: u64 = 0x800000; pub const CA_FORMAT_WITH_FLAG_SYNC: u64 = 0x800000;
/// Linux file attribute `NOCOMP`
pub const CA_FORMAT_WITH_FLAG_NOCOMP: u64 = 0x1000000; pub const CA_FORMAT_WITH_FLAG_NOCOMP: u64 = 0x1000000;
/// Linux file attribute `PROJINHERIT`
pub const CA_FORMAT_WITH_FLAG_PROJINHERIT: u64 = 0x2000000; pub const CA_FORMAT_WITH_FLAG_PROJINHERIT: u64 = 0x2000000;
pub const CA_FORMAT_FEATURE_FLAGS_MAX: u64 = 0xb000_0001_ffef_fe26; // fixme: ? pub const CA_FORMAT_FEATURE_FLAGS_MAX: u64 = 0xb000_0001_ffef_fe26; // fixme: ?
#[derive(Endian,Clone)] #[derive(Endian)]
#[repr(C)] #[repr(C)]
pub struct CaFormatHeader { pub struct CaFormatHeader {
/// The size of the item, including the size of `CaFormatHeader`. /// The size of the item, including the size of `CaFormatHeader`.
@ -50,7 +62,7 @@ pub struct CaFormatHeader {
pub htype: u64, pub htype: u64,
} }
#[derive(Endian,Clone)] #[derive(Endian)]
#[repr(C)] #[repr(C)]
pub struct CaFormatEntry { pub struct CaFormatEntry {
pub feature_flags: u64, pub feature_flags: u64,
@ -147,6 +159,5 @@ pub fn ca_feature_flags_from_chattr(attr: u32) -> u64 {
if (attr & fs_flag) != 0 { flags = flags | ca_flag; } if (attr & fs_flag) != 0 { flags = flags | ca_flag; }
} }
flags flags
} }