2018-12-30 16:43:53 +00:00
|
|
|
//! *catar* format decoder.
|
2019-01-06 16:27:57 +00:00
|
|
|
//!
|
|
|
|
//! This module contain the code to decode *catar* archive files.
|
2018-12-30 16:32:52 +00:00
|
|
|
|
2019-01-06 16:27:57 +00:00
|
|
|
use failure::*;
|
2019-01-07 18:07:03 +00:00
|
|
|
use endian_trait::Endian;
|
2018-12-30 16:32:52 +00:00
|
|
|
|
2019-01-06 16:27:57 +00:00
|
|
|
use super::format_definition::*;
|
|
|
|
|
2019-03-08 15:55:54 +00:00
|
|
|
use std::io::{Read, Write};
|
2019-01-07 18:07:03 +00:00
|
|
|
use std::path::{Path, PathBuf};
|
2019-01-06 16:27:57 +00:00
|
|
|
|
2019-01-08 16:17:55 +00:00
|
|
|
use std::os::unix::io::AsRawFd;
|
|
|
|
use std::os::unix::io::RawFd;
|
|
|
|
use std::os::unix::io::FromRawFd;
|
2019-03-08 15:55:54 +00:00
|
|
|
use std::os::unix::ffi::{OsStringExt};
|
2019-01-06 16:27:57 +00:00
|
|
|
use std::ffi::{OsStr, OsString};
|
|
|
|
|
2019-01-08 16:17:55 +00:00
|
|
|
use nix::fcntl::OFlag;
|
|
|
|
use nix::sys::stat::Mode;
|
|
|
|
use nix::errno::Errno;
|
|
|
|
use nix::NixPath;
|
|
|
|
|
2019-03-08 15:55:54 +00:00
|
|
|
// This one need Read, but works without Seek
|
|
|
|
pub struct CaTarDecoder<'a, R: Read> {
|
2019-01-06 16:27:57 +00:00
|
|
|
reader: &'a mut R,
|
2019-03-08 15:55:54 +00:00
|
|
|
skip_buffer: Vec<u8>,
|
2019-01-06 16:27:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const HEADER_SIZE: u64 = std::mem::size_of::<CaFormatHeader>() as u64;
|
|
|
|
|
2019-03-08 15:55:54 +00:00
|
|
|
impl <'a, R: Read> CaTarDecoder<'a, R> {
|
2019-01-06 16:27:57 +00:00
|
|
|
|
2019-03-08 15:55:54 +00:00
|
|
|
pub fn new(reader: &'a mut R) -> Self {
|
|
|
|
let mut skip_buffer = vec![0u8; 64*1024];
|
|
|
|
Self { reader, skip_buffer }
|
2019-01-06 16:27:57 +00:00
|
|
|
}
|
|
|
|
|
2019-01-07 18:07:03 +00:00
|
|
|
fn read_item<T: Endian>(&mut self) -> Result<T, Error> {
|
2019-01-06 16:27:57 +00:00
|
|
|
|
2019-01-07 18:07:03 +00:00
|
|
|
let mut result: T = unsafe { std::mem::uninitialized() };
|
2019-01-06 16:27:57 +00:00
|
|
|
|
2019-01-07 18:07:03 +00:00
|
|
|
let buffer = unsafe { std::slice::from_raw_parts_mut(
|
|
|
|
&mut result as *mut T as *mut u8,
|
|
|
|
std::mem::size_of::<T>()
|
|
|
|
)};
|
|
|
|
|
|
|
|
self.reader.read_exact(buffer)?;
|
|
|
|
|
|
|
|
Ok(result.from_le())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn read_symlink(&mut self, size: u64) -> Result<PathBuf, Error> {
|
|
|
|
if size < (HEADER_SIZE + 2) {
|
|
|
|
bail!("dectected short symlink target.");
|
2019-01-06 16:27:57 +00:00
|
|
|
}
|
2019-01-07 18:07:03 +00:00
|
|
|
let target_len = size - HEADER_SIZE;
|
2019-01-06 16:27:57 +00:00
|
|
|
|
2019-01-07 18:07:03 +00:00
|
|
|
if target_len > (libc::PATH_MAX as u64) {
|
|
|
|
bail!("symlink target too long ({}).", target_len);
|
|
|
|
}
|
2019-01-06 16:27:57 +00:00
|
|
|
|
2019-01-07 18:07:03 +00:00
|
|
|
let mut buffer = vec![0u8; target_len as usize];
|
|
|
|
self.reader.read_exact(&mut buffer)?;
|
2019-01-06 16:27:57 +00:00
|
|
|
|
2019-01-07 18:07:03 +00:00
|
|
|
let last_byte = buffer.pop().unwrap();
|
|
|
|
if last_byte != 0u8 {
|
|
|
|
bail!("symlink target not nul terminated.");
|
2019-01-06 16:27:57 +00:00
|
|
|
}
|
2019-01-07 18:07:03 +00:00
|
|
|
|
|
|
|
Ok(PathBuf::from(std::ffi::OsString::from_vec(buffer)))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn read_filename(&mut self, size: u64) -> Result<OsString, Error> {
|
|
|
|
if size < (HEADER_SIZE + 2) {
|
|
|
|
bail!("dectected short filename");
|
|
|
|
}
|
|
|
|
let name_len = size - HEADER_SIZE;
|
2019-01-06 16:27:57 +00:00
|
|
|
|
|
|
|
if name_len > ((libc::FILENAME_MAX as u64) + 1) {
|
2019-01-07 18:07:03 +00:00
|
|
|
bail!("filename too long ({}).", name_len);
|
2019-01-06 16:27:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
let mut buffer = vec![0u8; name_len as usize];
|
|
|
|
self.reader.read_exact(&mut buffer)?;
|
|
|
|
|
|
|
|
let last_byte = buffer.pop().unwrap();
|
|
|
|
if last_byte != 0u8 {
|
2019-01-07 18:07:03 +00:00
|
|
|
bail!("filename entry not nul terminated.");
|
|
|
|
}
|
2019-01-06 16:27:57 +00:00
|
|
|
|
2019-03-11 13:31:01 +00:00
|
|
|
if (buffer.len() == 1 && buffer[0] == b'.') || (buffer.len() == 2 && buffer[0] == b'.' && buffer[1] == b'.') {
|
|
|
|
bail!("found invalid filename with slashes.");
|
|
|
|
}
|
|
|
|
|
2019-01-11 11:22:00 +00:00
|
|
|
if buffer.iter().find(|b| (**b == b'/')).is_some() {
|
2019-01-09 11:17:23 +00:00
|
|
|
bail!("found invalid filename with slashes.");
|
|
|
|
}
|
2019-01-08 16:17:55 +00:00
|
|
|
|
2019-03-11 13:31:01 +00:00
|
|
|
let name = std::ffi::OsString::from_vec(buffer);
|
|
|
|
if name.is_empty() {
|
|
|
|
bail!("found empty filename.");
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(name)
|
2019-01-07 18:07:03 +00:00
|
|
|
}
|
|
|
|
|
2019-01-11 07:41:33 +00:00
|
|
|
fn restore_attributes(&mut self, _entry: &CaFormatEntry) -> Result<CaFormatHeader, Error> {
|
2019-01-09 13:44:00 +00:00
|
|
|
|
|
|
|
loop {
|
|
|
|
let head: CaFormatHeader = self.read_item()?;
|
|
|
|
match head.htype {
|
2019-01-11 07:41:33 +00:00
|
|
|
// fimxe: impl ...
|
2019-01-09 13:44:00 +00:00
|
|
|
_ => return Ok(head),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn restore_mode(&mut self, entry: &CaFormatEntry, fd: RawFd) -> Result<(), Error> {
|
|
|
|
|
|
|
|
let mode = Mode::from_bits_truncate((entry.mode as u32) & 0o7777);
|
|
|
|
|
|
|
|
nix::sys::stat::fchmod(fd, mode)?;
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn restore_mode_at(&mut self, entry: &CaFormatEntry, dirfd: RawFd, filename: &OsStr) -> Result<(), Error> {
|
|
|
|
|
|
|
|
let mode = Mode::from_bits_truncate((entry.mode as u32) & 0o7777);
|
|
|
|
|
2019-01-11 11:22:00 +00:00
|
|
|
// NOTE: we want :FchmodatFlags::NoFollowSymlink, but fchmodat does not support that
|
|
|
|
// on linux (see man fchmodat). Fortunately, we can simply avoid calling this on symlinks.
|
|
|
|
nix::sys::stat::fchmodat(Some(dirfd), filename, mode, nix::sys::stat::FchmodatFlags::FollowSymlink)?;
|
2019-01-09 13:44:00 +00:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn restore_ugid(&mut self, entry: &CaFormatEntry, fd: RawFd) -> Result<(), Error> {
|
|
|
|
|
|
|
|
let uid = entry.uid as u32;
|
|
|
|
let gid = entry.gid as u32;
|
|
|
|
|
|
|
|
let res = unsafe { libc::fchown(fd, uid, gid) };
|
|
|
|
Errno::result(res)?;
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn restore_ugid_at(&mut self, entry: &CaFormatEntry, dirfd: RawFd, filename: &OsStr) -> Result<(), Error> {
|
|
|
|
|
|
|
|
let uid = entry.uid as u32;
|
|
|
|
let gid = entry.gid as u32;
|
|
|
|
|
|
|
|
let res = filename.with_nix_path(|cstr| unsafe {
|
|
|
|
libc::fchownat(dirfd, cstr.as_ptr(), uid, gid, libc::AT_SYMLINK_NOFOLLOW)
|
|
|
|
})?;
|
|
|
|
Errno::result(res)?;
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn restore_mtime(&mut self, entry: &CaFormatEntry, fd: RawFd) -> Result<(), Error> {
|
|
|
|
|
|
|
|
let times = nsec_to_update_timespec(entry.mtime);
|
|
|
|
|
|
|
|
let res = unsafe { libc::futimens(fd, ×[0]) };
|
|
|
|
Errno::result(res)?;
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn restore_mtime_at(&mut self, entry: &CaFormatEntry, dirfd: RawFd, filename: &OsStr) -> Result<(), Error> {
|
|
|
|
|
|
|
|
let times = nsec_to_update_timespec(entry.mtime);
|
|
|
|
|
|
|
|
let res = filename.with_nix_path(|cstr| unsafe {
|
|
|
|
libc::utimensat(dirfd, cstr.as_ptr(), ×[0], libc::AT_SYMLINK_NOFOLLOW)
|
|
|
|
})?;
|
|
|
|
Errno::result(res)?;
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2019-01-11 11:22:00 +00:00
|
|
|
fn restore_device_at(&mut self, entry: &CaFormatEntry, dirfd: RawFd, filename: &OsStr, device: &CaFormatDevice) -> Result<(), Error> {
|
|
|
|
|
|
|
|
let rdev = nix::sys::stat::makedev(device.major, device.minor);
|
2019-01-11 12:12:55 +00:00
|
|
|
let mode = ((entry.mode as u32) & libc::S_IFMT) | 0o0600;
|
2019-01-11 11:22:00 +00:00
|
|
|
let res = filename.with_nix_path(|cstr| unsafe {
|
2019-01-11 12:12:55 +00:00
|
|
|
libc::mknodat(dirfd, cstr.as_ptr(), mode, rdev)
|
2019-01-11 11:22:00 +00:00
|
|
|
})?;
|
|
|
|
Errno::result(res)?;
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2019-01-11 12:26:05 +00:00
|
|
|
fn restore_socket_at(&mut self, dirfd: RawFd, filename: &OsStr) -> Result<(), Error> {
|
|
|
|
|
|
|
|
let mode = libc::S_IFSOCK | 0o0600;
|
|
|
|
let res = filename.with_nix_path(|cstr| unsafe {
|
|
|
|
libc::mknodat(dirfd, cstr.as_ptr(), mode, 0)
|
|
|
|
})?;
|
|
|
|
Errno::result(res)?;
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn restore_fifo_at(&mut self, dirfd: RawFd, filename: &OsStr) -> Result<(), Error> {
|
|
|
|
|
|
|
|
let mode = libc::S_IFIFO | 0o0600;
|
|
|
|
let res = filename.with_nix_path(|cstr| unsafe {
|
|
|
|
libc::mkfifoat(dirfd, cstr.as_ptr(), mode)
|
|
|
|
})?;
|
|
|
|
Errno::result(res)?;
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2019-03-11 13:31:01 +00:00
|
|
|
pub fn restore<F>(
|
|
|
|
&mut self,
|
|
|
|
path: &Path, // used for error reporting
|
|
|
|
callback: &F,
|
|
|
|
) -> Result<(), Error>
|
|
|
|
where F: Fn(&Path) -> Result<(), Error>
|
|
|
|
{
|
|
|
|
|
|
|
|
let _ = std::fs::create_dir(path);
|
|
|
|
|
|
|
|
let dir = match nix::dir::Dir::open(path, nix::fcntl::OFlag::O_DIRECTORY, nix::sys::stat::Mode::empty()) {
|
|
|
|
Ok(dir) => dir,
|
|
|
|
Err(err) => bail!("unable to open target directory {:?} - {}", path, err),
|
|
|
|
};
|
|
|
|
|
|
|
|
self.restore_sequential(&mut path.to_owned(), &OsString::new(), &dir, callback)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn restore_sequential<F>(
|
2019-01-07 18:07:03 +00:00
|
|
|
&mut self,
|
2019-03-08 15:55:54 +00:00
|
|
|
path: &mut PathBuf, // used for error reporting
|
2019-01-08 16:17:55 +00:00
|
|
|
filename: &OsStr, // repeats path last component
|
|
|
|
parent: &nix::dir::Dir,
|
2019-01-07 18:07:03 +00:00
|
|
|
callback: &F,
|
2019-03-11 07:14:28 +00:00
|
|
|
) -> Result<(), Error>
|
|
|
|
where F: Fn(&Path) -> Result<(), Error>
|
|
|
|
{
|
2019-01-07 18:07:03 +00:00
|
|
|
|
2019-01-08 16:17:55 +00:00
|
|
|
let parent_fd = parent.as_raw_fd();
|
|
|
|
|
2019-01-07 18:07:03 +00:00
|
|
|
// read ENTRY first
|
|
|
|
let head: CaFormatHeader = self.read_item()?;
|
|
|
|
check_ca_header::<CaFormatEntry>(&head, CA_FORMAT_ENTRY)?;
|
|
|
|
let entry: CaFormatEntry = self.read_item()?;
|
|
|
|
|
2019-01-07 18:18:59 +00:00
|
|
|
let mode = entry.mode as u32; //fixme: upper 32bits?
|
|
|
|
|
2019-01-09 11:17:23 +00:00
|
|
|
let ifmt = mode & libc::S_IFMT;
|
|
|
|
|
|
|
|
if ifmt == libc::S_IFDIR {
|
2019-03-11 13:31:01 +00:00
|
|
|
let dir;
|
|
|
|
if filename.is_empty() {
|
|
|
|
dir = nix::dir::Dir::openat(parent_fd, ".", OFlag::O_DIRECTORY, Mode::empty())?;
|
|
|
|
} else {
|
|
|
|
dir = match dir_mkdirat(parent_fd, filename, true) {
|
|
|
|
Ok(dir) => dir,
|
|
|
|
Err(err) => bail!("unable to open directory {:?} - {}", path, err),
|
|
|
|
};
|
|
|
|
}
|
2019-01-08 16:17:55 +00:00
|
|
|
|
2019-01-09 13:44:00 +00:00
|
|
|
let mut head = self.restore_attributes(&entry)?;
|
2019-01-08 16:17:55 +00:00
|
|
|
|
2019-01-09 13:44:00 +00:00
|
|
|
while head.htype == CA_FORMAT_FILENAME {
|
|
|
|
let name = self.read_filename(head.size)?;
|
|
|
|
path.push(&name);
|
|
|
|
println!("NAME: {:?}", path);
|
2019-03-11 06:27:45 +00:00
|
|
|
|
2019-03-11 13:31:01 +00:00
|
|
|
self.restore_sequential(path, &name, &dir, callback)?;
|
2019-01-09 13:44:00 +00:00
|
|
|
path.pop();
|
|
|
|
|
|
|
|
head = self.read_item()?;
|
|
|
|
}
|
|
|
|
|
|
|
|
if head.htype != CA_FORMAT_GOODBYE {
|
|
|
|
bail!("got unknown header type inside directory entry {:016x}", head.htype);
|
2019-01-08 16:17:55 +00:00
|
|
|
}
|
2019-01-09 13:44:00 +00:00
|
|
|
|
|
|
|
println!("Skip Goodbye");
|
|
|
|
if head.size < HEADER_SIZE { bail!("detected short goodbye table"); }
|
2019-03-08 15:55:54 +00:00
|
|
|
|
|
|
|
// self.reader.seek(SeekFrom::Current((head.size - HEADER_SIZE) as i64))?;
|
|
|
|
let mut done = 0;
|
|
|
|
let skip = (head.size - HEADER_SIZE) as usize;
|
|
|
|
while done < skip {
|
|
|
|
let todo = skip - done;
|
|
|
|
let n = if todo > self.skip_buffer.len() { self.skip_buffer.len() } else { todo };
|
|
|
|
let data = &mut self.skip_buffer[..n];
|
|
|
|
self.reader.read_exact(data)?;
|
|
|
|
done += n;
|
|
|
|
}
|
|
|
|
|
2019-01-09 13:44:00 +00:00
|
|
|
|
|
|
|
self.restore_mode(&entry, dir.as_raw_fd())?;
|
|
|
|
self.restore_mtime(&entry, dir.as_raw_fd())?;
|
|
|
|
self.restore_ugid(&entry, dir.as_raw_fd())?;
|
|
|
|
|
|
|
|
return Ok(());
|
2019-01-08 16:17:55 +00:00
|
|
|
}
|
2019-01-08 08:26:07 +00:00
|
|
|
|
2019-03-11 13:31:01 +00:00
|
|
|
if filename.is_empty() {
|
|
|
|
bail!("got empty file name at {:?}", path)
|
|
|
|
}
|
|
|
|
|
2019-01-09 11:17:23 +00:00
|
|
|
if ifmt == libc::S_IFLNK {
|
2019-01-08 16:17:55 +00:00
|
|
|
// fixme: create symlink
|
|
|
|
//fixme: restore permission, acls, xattr, ...
|
2019-01-09 13:44:00 +00:00
|
|
|
|
2019-01-07 18:07:03 +00:00
|
|
|
let head: CaFormatHeader = self.read_item()?;
|
|
|
|
match head.htype {
|
|
|
|
CA_FORMAT_SYMLINK => {
|
|
|
|
let target = self.read_symlink(head.size)?;
|
|
|
|
println!("TARGET: {:?}", target);
|
2019-01-08 16:17:55 +00:00
|
|
|
if let Err(err) = symlinkat(&target, parent_fd, filename) {
|
|
|
|
bail!("create symlink {:?} failed - {}", path, err);
|
2019-01-07 18:18:59 +00:00
|
|
|
}
|
|
|
|
}
|
2019-01-08 16:17:55 +00:00
|
|
|
_ => {
|
|
|
|
bail!("got unknown header type inside symlink entry {:016x}", head.htype);
|
|
|
|
}
|
|
|
|
}
|
2019-01-09 13:44:00 +00:00
|
|
|
|
|
|
|
// self.restore_mode_at(&entry, parent_fd, filename)?; //not supported on symlinks
|
|
|
|
self.restore_ugid_at(&entry, parent_fd, filename)?;
|
2019-01-11 12:26:05 +00:00
|
|
|
self.restore_mtime_at(&entry, parent_fd, filename)?;
|
|
|
|
|
|
|
|
return Ok(());
|
|
|
|
}
|
|
|
|
|
|
|
|
if ifmt == libc::S_IFSOCK {
|
|
|
|
|
|
|
|
self.restore_socket_at(parent_fd, filename)?;
|
|
|
|
|
|
|
|
self.restore_mode_at(&entry, parent_fd, filename)?;
|
|
|
|
self.restore_ugid_at(&entry, parent_fd, filename)?;
|
|
|
|
self.restore_mtime_at(&entry, parent_fd, filename)?;
|
|
|
|
|
|
|
|
return Ok(());
|
|
|
|
}
|
|
|
|
|
|
|
|
if ifmt == libc::S_IFIFO {
|
|
|
|
|
|
|
|
self.restore_fifo_at(parent_fd, filename)?;
|
|
|
|
|
|
|
|
self.restore_mode_at(&entry, parent_fd, filename)?;
|
|
|
|
self.restore_ugid_at(&entry, parent_fd, filename)?;
|
|
|
|
self.restore_mtime_at(&entry, parent_fd, filename)?;
|
2019-01-09 13:44:00 +00:00
|
|
|
|
2019-01-08 16:17:55 +00:00
|
|
|
return Ok(());
|
|
|
|
}
|
|
|
|
|
2019-01-11 11:22:00 +00:00
|
|
|
if (ifmt == libc::S_IFBLK) || (ifmt == libc::S_IFCHR) {
|
|
|
|
|
|
|
|
let head: CaFormatHeader = self.read_item()?;
|
|
|
|
match head.htype {
|
|
|
|
CA_FORMAT_DEVICE => {
|
|
|
|
let device: CaFormatDevice = self.read_item()?;
|
|
|
|
self.restore_device_at(&entry, parent_fd, filename, &device)?;
|
|
|
|
}
|
|
|
|
_ => {
|
|
|
|
bail!("got unknown header type inside device entry {:016x}", head.htype);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
self.restore_mode_at(&entry, parent_fd, filename)?;
|
|
|
|
self.restore_ugid_at(&entry, parent_fd, filename)?;
|
2019-01-11 12:26:05 +00:00
|
|
|
self.restore_mtime_at(&entry, parent_fd, filename)?;
|
2019-01-11 11:22:00 +00:00
|
|
|
|
|
|
|
return Ok(());
|
|
|
|
}
|
|
|
|
|
2019-01-09 11:17:23 +00:00
|
|
|
if ifmt == libc::S_IFREG {
|
2019-01-08 16:17:55 +00:00
|
|
|
|
|
|
|
let mut read_buffer: [u8; 64*1024] = unsafe { std::mem::uninitialized() };
|
|
|
|
|
|
|
|
let flags = OFlag::O_CREAT|OFlag::O_WRONLY|OFlag::O_EXCL;
|
|
|
|
let open_mode = Mode::from_bits_truncate(0o0600 | mode);
|
|
|
|
|
|
|
|
let mut file = match file_openat(parent_fd, filename, flags, open_mode) {
|
|
|
|
Ok(file) => file,
|
|
|
|
Err(err) => bail!("open file {:?} failed - {}", path, err),
|
|
|
|
};
|
|
|
|
|
2019-01-11 07:41:33 +00:00
|
|
|
let head = self.restore_attributes(&entry)?;
|
2019-01-08 16:17:55 +00:00
|
|
|
|
2019-01-09 13:44:00 +00:00
|
|
|
if head.htype != CA_FORMAT_PAYLOAD {
|
|
|
|
bail!("got unknown header type for file entry {:016x}", head.htype);
|
|
|
|
}
|
|
|
|
|
|
|
|
if head.size < HEADER_SIZE {
|
|
|
|
bail!("detected short payload");
|
|
|
|
}
|
|
|
|
let need = (head.size - HEADER_SIZE) as usize;
|
|
|
|
//self.reader.seek(SeekFrom::Current(need as i64))?;
|
|
|
|
|
|
|
|
let mut done = 0;
|
2019-01-11 07:41:33 +00:00
|
|
|
while done < need {
|
2019-01-09 13:44:00 +00:00
|
|
|
let todo = need - done;
|
|
|
|
let n = if todo > read_buffer.len() { read_buffer.len() } else { todo };
|
|
|
|
let data = &mut read_buffer[..n];
|
|
|
|
self.reader.read_exact(data)?;
|
|
|
|
file.write_all(data)?;
|
|
|
|
done += n;
|
2019-01-07 18:07:03 +00:00
|
|
|
}
|
2019-01-08 16:17:55 +00:00
|
|
|
|
2019-01-09 13:44:00 +00:00
|
|
|
self.restore_mode(&entry, file.as_raw_fd())?;
|
|
|
|
self.restore_mtime(&entry, file.as_raw_fd())?;
|
|
|
|
self.restore_ugid(&entry, file.as_raw_fd())?;
|
|
|
|
|
2019-01-08 16:17:55 +00:00
|
|
|
return Ok(());
|
2019-01-06 16:27:57 +00:00
|
|
|
}
|
|
|
|
|
2019-01-07 18:07:03 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
2019-01-06 16:27:57 +00:00
|
|
|
}
|
2019-01-08 16:17:55 +00:00
|
|
|
|
|
|
|
fn file_openat(parent: RawFd, filename: &OsStr, flags: OFlag, mode: Mode) -> Result<std::fs::File, Error> {
|
|
|
|
|
2019-01-11 07:41:33 +00:00
|
|
|
let fd = filename.with_nix_path(|cstr| {
|
2019-01-08 16:17:55 +00:00
|
|
|
nix::fcntl::openat(parent, cstr.as_ref(), flags, mode)
|
|
|
|
})??;
|
|
|
|
|
|
|
|
let file = unsafe { std::fs::File::from_raw_fd(fd) };
|
|
|
|
|
|
|
|
Ok(file)
|
|
|
|
}
|
|
|
|
|
2019-03-11 06:27:45 +00:00
|
|
|
fn dir_mkdirat(parent: RawFd, filename: &OsStr, create_new: bool) -> Result<nix::dir::Dir, nix::Error> {
|
2019-01-08 16:17:55 +00:00
|
|
|
|
|
|
|
// call mkdirat first
|
|
|
|
let res = filename.with_nix_path(|cstr| unsafe {
|
|
|
|
libc::mkdirat(parent, cstr.as_ptr(), libc::S_IRWXU)
|
|
|
|
})?;
|
2019-03-11 06:27:45 +00:00
|
|
|
|
|
|
|
match Errno::result(res) {
|
|
|
|
Ok(_) => {},
|
|
|
|
Err(err) => {
|
|
|
|
if err == nix::Error::Sys(nix::errno::Errno::EEXIST) {
|
|
|
|
if create_new { return Err(err); }
|
|
|
|
} else {
|
|
|
|
return Err(err);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-01-08 16:17:55 +00:00
|
|
|
|
|
|
|
let dir = nix::dir::Dir::openat(parent, filename, OFlag::O_DIRECTORY, Mode::empty())?;
|
|
|
|
|
|
|
|
Ok(dir)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn symlinkat(target: &Path, parent: RawFd, linkname: &OsStr) -> Result<(), Error> {
|
|
|
|
|
|
|
|
target.with_nix_path(|target| {
|
|
|
|
linkname.with_nix_path(|linkname| {
|
|
|
|
let res = unsafe { libc::symlinkat(target.as_ptr(), parent, linkname.as_ptr()) };
|
|
|
|
Errno::result(res)?;
|
|
|
|
Ok(())
|
|
|
|
})?
|
|
|
|
})?
|
|
|
|
}
|
2019-01-09 13:44:00 +00:00
|
|
|
|
|
|
|
fn nsec_to_update_timespec(mtime_nsec: u64) -> [libc::timespec; 2] {
|
|
|
|
|
|
|
|
// restore mtime
|
|
|
|
const UTIME_OMIT: i64 = ((1 << 30) - 2);
|
|
|
|
const NANOS_PER_SEC: i64 = 1_000_000_000;
|
|
|
|
|
|
|
|
let sec = (mtime_nsec as i64) / NANOS_PER_SEC;
|
|
|
|
let nsec = (mtime_nsec as i64) % NANOS_PER_SEC;
|
|
|
|
|
|
|
|
let times: [libc::timespec; 2] = [
|
|
|
|
libc::timespec { tv_sec: 0, tv_nsec: UTIME_OMIT },
|
|
|
|
libc::timespec { tv_sec: sec, tv_nsec: nsec },
|
|
|
|
];
|
|
|
|
|
|
|
|
times
|
|
|
|
}
|