2018-12-27 12:15:10 +00:00
|
|
|
use failure::*;
|
|
|
|
|
|
|
|
use super::format_definition::*;
|
|
|
|
|
|
|
|
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-28 10:48:47 +00:00
|
|
|
const FILE_COPY_BUFFER_SIZE: usize = 1024*1024;
|
2018-12-28 09:44:12 +00:00
|
|
|
|
2018-12-27 12:15:10 +00:00
|
|
|
pub struct CaTarEncoder<W: Write> {
|
2018-12-28 06:45:15 +00:00
|
|
|
current_path: PathBuf, // used for error reporting
|
2018-12-27 12:15:10 +00:00
|
|
|
writer: W,
|
2018-12-28 10:48:47 +00:00
|
|
|
writer_pos: usize,
|
2018-12-27 12:15:10 +00:00
|
|
|
size: usize,
|
2018-12-28 10:48:47 +00:00
|
|
|
file_copy_buffer: Vec<u8>,
|
2018-12-27 12:15:10 +00:00
|
|
|
}
|
|
|
|
|
2018-12-28 13:26:05 +00:00
|
|
|
|
2018-12-27 12:15:10 +00:00
|
|
|
impl <W: Write> CaTarEncoder<W> {
|
|
|
|
|
2018-12-28 06:45:15 +00:00
|
|
|
pub fn encode(path: PathBuf, dir: &mut nix::dir::Dir, writer: W) -> Result<(), Error> {
|
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
|
|
|
let mut me = Self {
|
|
|
|
current_path: path,
|
|
|
|
writer: writer,
|
2018-12-28 10:48:47 +00:00
|
|
|
writer_pos: 0,
|
2018-12-27 12:15:10 +00:00
|
|
|
size: 0,
|
2018-12-28 10:48:47 +00:00
|
|
|
file_copy_buffer,
|
2018-12-27 12:15:10 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// todo: use scandirat??
|
2018-12-28 13:26:05 +00:00
|
|
|
me.encode_dir(dir)?;
|
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> {
|
|
|
|
self.writer.write(buf)?;
|
|
|
|
self.writer_pos += buf.len();
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn flush_copy_buffer(&mut self, size: usize) -> Result<(), Error> {
|
2018-12-28 13:26:05 +00:00
|
|
|
self.writer.write(&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> {
|
|
|
|
|
|
|
|
let mut buffer = [0u8; std::mem::size_of::<CaFormatHeader>()];
|
|
|
|
let mut header = crate::tools::map_struct_mut::<CaFormatHeader>(&mut buffer)?;
|
|
|
|
header.size = u64::to_le((std::mem::size_of::<CaFormatHeader>() as u64) + size);
|
2018-12-28 13:26:05 +00:00
|
|
|
header.htype = u64::to_le(htype);
|
2018-12-28 08:55:26 +00:00
|
|
|
|
2018-12-28 10:48:47 +00:00
|
|
|
self.write(&buffer)?;
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
fn write_entry(&mut self, stat: &FileStat) -> Result<(), Error> {
|
|
|
|
|
|
|
|
let mut buffer = [0u8; std::mem::size_of::<CaFormatHeader>() + std::mem::size_of::<CaFormatEntry>()];
|
|
|
|
let mut header = crate::tools::map_struct_mut::<CaFormatHeader>(&mut buffer)?;
|
|
|
|
header.size = u64::to_le((std::mem::size_of::<CaFormatHeader>() + std::mem::size_of::<CaFormatEntry>()) as u64);
|
2018-12-28 13:26:05 +00:00
|
|
|
header.htype = u64::to_le(CA_FORMAT_ENTRY);
|
2018-12-28 08:55:26 +00:00
|
|
|
|
|
|
|
let mut entry = crate::tools::map_struct_mut::<CaFormatEntry>(&mut buffer[std::mem::size_of::<CaFormatHeader>()..])?;
|
|
|
|
|
2018-12-28 13:26:05 +00:00
|
|
|
entry.feature_flags = u64::to_le(CA_FORMAT_FEATURE_FLAGS_MAX);
|
2018-12-28 08:55:26 +00:00
|
|
|
|
|
|
|
if (stat.st_mode & libc::S_IFMT) == libc::S_IFLNK {
|
|
|
|
entry.mode = u64::to_le((libc::S_IFLNK | 0o777) as u64);
|
|
|
|
} else {
|
|
|
|
let mode = stat.st_mode & (libc::S_IFMT | 0o7777);
|
|
|
|
entry.mode = u64::to_le(mode as u64);
|
|
|
|
}
|
|
|
|
|
|
|
|
entry.flags = 0; // todo: CHATTR, FAT_ATTRS, subvolume?
|
|
|
|
|
|
|
|
entry.uid = u64::to_le(stat.st_uid as u64);
|
|
|
|
entry.gid = u64::to_le(stat.st_gid as u64);
|
|
|
|
|
|
|
|
let mtime = stat.st_mtime * 1_000_000_000 + stat.st_mtime_nsec;
|
|
|
|
if mtime > 0 { entry.mtime = mtime 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 06:45:15 +00:00
|
|
|
|
2018-12-28 13:26:05 +00:00
|
|
|
fn encode_dir(&mut self, dir: &mut nix::dir::Dir) -> Result<(), Error> {
|
2018-12-27 12:15:10 +00:00
|
|
|
|
2018-12-28 10:48:47 +00:00
|
|
|
println!("encode_dir: {:?} start {}", self.current_path, self.writer_pos);
|
2018-12-27 12:15:10 +00:00
|
|
|
|
|
|
|
let mut name_list = vec![];
|
|
|
|
|
|
|
|
let rawfd = dir.as_raw_fd();
|
|
|
|
|
|
|
|
let dir_stat = match nix::sys::stat::fstat(rawfd) {
|
|
|
|
Ok(stat) => stat,
|
2018-12-28 06:14:12 +00:00
|
|
|
Err(err) => bail!("fstat {:?} failed - {}", self.current_path, err),
|
2018-12-27 12:15:10 +00:00
|
|
|
};
|
|
|
|
|
2018-12-28 09:44:12 +00:00
|
|
|
if (dir_stat.st_mode & libc::S_IFMT) != libc::S_IFDIR {
|
|
|
|
bail!("got unexpected file type {:?} (not a directory)", self.current_path);
|
|
|
|
}
|
|
|
|
|
2018-12-28 13:26:05 +00:00
|
|
|
let dir_start_pos = self.writer_pos;
|
2018-12-28 08:55:26 +00:00
|
|
|
|
2018-12-28 13:26:05 +00:00
|
|
|
self.write_entry(&dir_stat)?;
|
2018-12-28 10:48:47 +00:00
|
|
|
|
2018-12-27 12:15:10 +00:00
|
|
|
for entry in dir.iter() {
|
|
|
|
let entry = match entry {
|
|
|
|
Ok(entry) => entry,
|
2018-12-28 06:45:15 +00:00
|
|
|
Err(err) => bail!("readir {:?} failed - {}", self.current_path, err),
|
2018-12-27 12:15:10 +00:00
|
|
|
};
|
2018-12-27 13:24:31 +00:00
|
|
|
let filename = entry.file_name().to_owned();
|
2018-12-27 12:15:10 +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-28 06:45:15 +00:00
|
|
|
match nix::sys::stat::fstatat(rawfd, filename.as_ref(), nix::fcntl::AtFlags::AT_SYMLINK_NOFOLLOW) {
|
|
|
|
Ok(stat) => {
|
|
|
|
name_list.push((filename, stat));
|
|
|
|
}
|
|
|
|
Err(nix::Error::Sys(Errno::ENOENT)) => self.report_vanished_file(&self.current_path)?,
|
|
|
|
Err(err) => bail!("fstat {:?} failed - {}", self.current_path, err),
|
2018-12-27 12:15:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
name_list.sort_unstable_by(|a, b| a.0.cmp(&b.0));
|
|
|
|
|
2018-12-28 13:26:05 +00:00
|
|
|
let mut goodby_items = vec![];
|
|
|
|
|
2018-12-28 10:48:47 +00:00
|
|
|
for (filename, stat) in &name_list {
|
2018-12-27 13:24:31 +00:00
|
|
|
self.current_path.push(std::ffi::OsStr::from_bytes(filename.as_bytes()));
|
2018-12-27 12:15:10 +00:00
|
|
|
|
2018-12-28 13:26:05 +00:00
|
|
|
let start_pos = self.writer_pos;
|
|
|
|
|
|
|
|
self.write_filename(&filename)?;
|
|
|
|
|
2018-12-27 13:24:31 +00:00
|
|
|
if (stat.st_mode & libc::S_IFMT) == libc::S_IFDIR {
|
2018-12-28 13:26:05 +00:00
|
|
|
|
2018-12-28 06:45:15 +00:00
|
|
|
match nix::dir::Dir::openat(rawfd, filename.as_ref(), OFlag::O_NOFOLLOW, Mode::empty()) {
|
2018-12-28 13:26:05 +00:00
|
|
|
Ok(mut dir) => self.encode_dir(&mut dir)?,
|
2018-12-28 06:45:15 +00:00
|
|
|
Err(nix::Error::Sys(Errno::ENOENT)) => self.report_vanished_file(&self.current_path)?,
|
|
|
|
Err(err) => bail!("open dir {:?} failed - {}", self.current_path, err),
|
|
|
|
}
|
2018-12-28 08:55:26 +00:00
|
|
|
|
2018-12-27 13:24:31 +00:00
|
|
|
} else if (stat.st_mode & libc::S_IFMT) == libc::S_IFREG {
|
2018-12-28 06:45:15 +00:00
|
|
|
match nix::fcntl::openat(rawfd, filename.as_ref(), OFlag::O_NOFOLLOW, Mode::empty()) {
|
|
|
|
Ok(filefd) => {
|
2018-12-28 13:26:05 +00:00
|
|
|
let res = self.encode_file(filefd);
|
2018-12-28 06:45:15 +00:00
|
|
|
let _ = nix::unistd::close(filefd); // ignore close errors
|
|
|
|
res?;
|
|
|
|
}
|
|
|
|
Err(nix::Error::Sys(Errno::ENOENT)) => self.report_vanished_file(&self.current_path)?,
|
|
|
|
Err(err) => bail!("open file {:?} failed - {}", self.current_path, err),
|
|
|
|
}
|
2018-12-27 13:24:31 +00:00
|
|
|
} else if (stat.st_mode & libc::S_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
|
|
|
|
self.encode_symlink(&buffer[..((len+1) as usize)], &stat)?
|
|
|
|
}
|
2018-12-28 06:45:15 +00:00
|
|
|
Err(nix::Error::Sys(Errno::ENOENT)) => self.report_vanished_file(&self.current_path)?,
|
|
|
|
Err(err) => bail!("readlink {:?} failed - {}", self.current_path, err),
|
|
|
|
}
|
2018-12-27 13:24:31 +00:00
|
|
|
} else {
|
|
|
|
bail!("unsupported file type (mode {:o} {:?})", stat.st_mode, self.current_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;
|
|
|
|
|
|
|
|
goodby_items.push(CaFormatGoodbyeItem {
|
|
|
|
offset: start_pos as u64,
|
|
|
|
size: (end_pos - start_pos) as u64,
|
|
|
|
hash: compute_goodby_hash(&filename),
|
|
|
|
});
|
|
|
|
|
2018-12-27 13:24:31 +00:00
|
|
|
self.current_path.pop();
|
2018-12-28 10:48:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
println!("encode_dir: {:?} end {}", self.current_path, self.writer_pos);
|
2018-12-27 13:24:31 +00:00
|
|
|
|
2018-12-28 13:26:05 +00:00
|
|
|
let goodby_start = self.writer_pos as u64;
|
|
|
|
let goodby_table_size = (goodby_items.len() + 1)*std::mem::size_of::<CaFormatGoodbyeItem>();
|
|
|
|
|
|
|
|
for item in &mut goodby_items {
|
|
|
|
item.offset = goodby_start - item.offset;
|
|
|
|
}
|
|
|
|
|
|
|
|
// fixme: sort goodby_items (BST)
|
|
|
|
|
|
|
|
let goodby_offset = self.writer_pos - dir_start_pos;
|
|
|
|
|
|
|
|
// append CaFormatGoodbyeTail as last item
|
|
|
|
goodby_items.push(CaFormatGoodbyeItem {
|
|
|
|
offset: goodby_offset as u64,
|
|
|
|
size: (goodby_table_size + std::mem::size_of::<CaFormatHeader>()) as u64,
|
|
|
|
hash: CA_FORMAT_GOODBYE_TAIL_MARKER,
|
|
|
|
});
|
|
|
|
|
|
|
|
self.write_header(CA_FORMAT_GOODBYE, goodby_table_size as u64)?;
|
|
|
|
|
|
|
|
if goodby_table_size > FILE_COPY_BUFFER_SIZE {
|
|
|
|
bail!("goodby table too large ({} > {})", goodby_table_size, FILE_COPY_BUFFER_SIZE);
|
|
|
|
}
|
|
|
|
|
|
|
|
let buffer = &mut self.file_copy_buffer;
|
|
|
|
let buffer_ptr = buffer.as_ptr();
|
|
|
|
for (i, item) in goodby_items.iter().enumerate() {
|
|
|
|
unsafe {
|
|
|
|
*(buffer_ptr.add(i*std::mem::size_of::<CaFormatGoodbyeItem>()) as *mut u64) = u64::to_le(item.offset);
|
|
|
|
*(buffer_ptr.add(i*std::mem::size_of::<CaFormatGoodbyeItem>()+8) as *mut u64) = u64::to_le(item.size);
|
|
|
|
*(buffer_ptr.add(i*std::mem::size_of::<CaFormatGoodbyeItem>()+16) as *mut u64) = u64::to_le(item.hash);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
self.flush_copy_buffer(goodby_table_size)?;
|
|
|
|
|
|
|
|
println!("encode_dir: {:?} end1 {}", self.current_path, self.writer_pos);
|
2018-12-27 13:24:31 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2018-12-28 13:26:05 +00:00
|
|
|
fn encode_file(&mut self, filefd: RawFd) -> Result<(), Error> {
|
2018-12-27 13:24:31 +00:00
|
|
|
|
|
|
|
println!("encode_file: {:?}", self.current_path);
|
|
|
|
|
2018-12-28 09:44:12 +00:00
|
|
|
let stat = match nix::sys::stat::fstat(filefd) {
|
|
|
|
Ok(stat) => stat,
|
|
|
|
Err(err) => bail!("fstat {:?} failed - {}", self.current_path, err),
|
|
|
|
};
|
|
|
|
|
|
|
|
if (stat.st_mode & libc::S_IFMT) != libc::S_IFREG {
|
|
|
|
bail!("got unexpected file type {:?} (not a regular file)", self.current_path);
|
|
|
|
}
|
|
|
|
|
|
|
|
self.write_entry(&stat)?;
|
|
|
|
|
|
|
|
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 */,
|
|
|
|
Err(err) => bail!("read {:?} failed - {}", self.current_path, err),
|
|
|
|
};
|
|
|
|
if n == 0 { // EOF
|
|
|
|
if pos != size {
|
|
|
|
// Note:: casync format cannot handle that
|
|
|
|
bail!("detected shrinked file {:?} ({} < {})", self.current_path, pos, size);
|
|
|
|
}
|
|
|
|
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
|
|
|
|
|
|
|
pos += next;
|
|
|
|
|
|
|
|
if pos >= size { break; }
|
|
|
|
}
|
2018-12-28 08:55:26 +00:00
|
|
|
|
2018-12-27 13:24:31 +00:00
|
|
|
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
|
|
|
|
|
|
|
println!("encode_symlink: {:?} -> {:?}", self.current_path, target);
|
2018-12-27 12:15:10 +00:00
|
|
|
|
2018-12-28 08:55:26 +00:00
|
|
|
self.write_entry(stat)?;
|
|
|
|
|
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
|
|
|
|
|
|
|
// 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
|
|
|
}
|
|
|
|
|
|
|
|
fn compute_goodby_hash(name: &CStr) -> u64 {
|
2018-12-28 06:45:15 +00:00
|
|
|
|
2018-12-28 13:26:05 +00:00
|
|
|
use std::hash::Hasher;
|
|
|
|
let mut hasher = std::hash::SipHasher::new_with_keys(0x8574442b0f1d84b3, 0x2736ed30d1c22ec1);
|
|
|
|
hasher.write(name.to_bytes());
|
|
|
|
hasher.finish()
|
2018-12-27 12:15:10 +00:00
|
|
|
}
|