catar/encoder.rs: improve error handling
This commit is contained in:
parent
0ff559990c
commit
d2b03f2397
|
@ -6,19 +6,21 @@ use std::io::Write;
|
||||||
use std::os::unix::io::AsRawFd;
|
use std::os::unix::io::AsRawFd;
|
||||||
use std::os::unix::ffi::OsStrExt;
|
use std::os::unix::ffi::OsStrExt;
|
||||||
use std::os::unix::io::RawFd;
|
use std::os::unix::io::RawFd;
|
||||||
|
use std::path::{Path, PathBuf};
|
||||||
|
|
||||||
use nix::fcntl::OFlag;
|
use nix::fcntl::OFlag;
|
||||||
use nix::sys::stat::Mode;
|
use nix::sys::stat::Mode;
|
||||||
|
use nix::errno::Errno;
|
||||||
|
|
||||||
pub struct CaTarEncoder<W: Write> {
|
pub struct CaTarEncoder<W: Write> {
|
||||||
current_path: std::path::PathBuf, // used for error reporting
|
current_path: PathBuf, // used for error reporting
|
||||||
writer: W,
|
writer: W,
|
||||||
size: usize,
|
size: usize,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl <W: Write> CaTarEncoder<W> {
|
impl <W: Write> CaTarEncoder<W> {
|
||||||
|
|
||||||
pub fn encode(path: std::path::PathBuf, dir: &mut nix::dir::Dir, writer: W) -> Result<(), Error> {
|
pub fn encode(path: PathBuf, dir: &mut nix::dir::Dir, writer: W) -> Result<(), Error> {
|
||||||
let mut me = Self {
|
let mut me = Self {
|
||||||
current_path: path,
|
current_path: path,
|
||||||
writer: writer,
|
writer: writer,
|
||||||
|
@ -32,6 +34,8 @@ impl <W: Write> CaTarEncoder<W> {
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//fn report_vanished
|
||||||
|
|
||||||
fn encode_dir(&mut self, dir: &mut nix::dir::Dir) -> Result<(), Error> {
|
fn encode_dir(&mut self, dir: &mut nix::dir::Dir) -> Result<(), Error> {
|
||||||
|
|
||||||
println!("encode_dir: {:?}", self.current_path);
|
println!("encode_dir: {:?}", self.current_path);
|
||||||
|
@ -48,7 +52,7 @@ impl <W: Write> CaTarEncoder<W> {
|
||||||
for entry in dir.iter() {
|
for entry in dir.iter() {
|
||||||
let entry = match entry {
|
let entry = match entry {
|
||||||
Ok(entry) => entry,
|
Ok(entry) => entry,
|
||||||
Err(err) => bail!("readir failed - {}", err),
|
Err(err) => bail!("readir {:?} failed - {}", self.current_path, err),
|
||||||
};
|
};
|
||||||
let filename = entry.file_name().to_owned();
|
let filename = entry.file_name().to_owned();
|
||||||
|
|
||||||
|
@ -57,34 +61,44 @@ impl <W: Write> CaTarEncoder<W> {
|
||||||
if name_len == 2 && name[0] == b'.' && name[1] == 0u8 { continue; }
|
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; }
|
if name_len == 3 && name[0] == b'.' && name[1] == b'.' && name[2] == 0u8 { continue; }
|
||||||
|
|
||||||
if let Ok(stat) = nix::sys::stat::fstatat(rawfd, filename.as_ref(), nix::fcntl::AtFlags::AT_SYMLINK_NOFOLLOW) {
|
match nix::sys::stat::fstatat(rawfd, filename.as_ref(), nix::fcntl::AtFlags::AT_SYMLINK_NOFOLLOW) {
|
||||||
//println!("Found {:?}", filename);
|
Ok(stat) => {
|
||||||
name_list.push((filename, stat));
|
name_list.push((filename, stat));
|
||||||
} else {
|
}
|
||||||
bail!("fsstat failed");
|
Err(nix::Error::Sys(Errno::ENOENT)) => self.report_vanished_file(&self.current_path)?,
|
||||||
|
Err(err) => bail!("fstat {:?} failed - {}", self.current_path, err),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
name_list.sort_unstable_by(|a, b| a.0.cmp(&b.0));
|
name_list.sort_unstable_by(|a, b| a.0.cmp(&b.0));
|
||||||
|
|
||||||
for (filename, stat) in name_list {
|
for (filename, stat) in name_list {
|
||||||
//println!("SORTED {:?}", filename);
|
|
||||||
self.current_path.push(std::ffi::OsStr::from_bytes(filename.as_bytes()));
|
self.current_path.push(std::ffi::OsStr::from_bytes(filename.as_bytes()));
|
||||||
|
|
||||||
if (stat.st_mode & libc::S_IFMT) == libc::S_IFDIR {
|
if (stat.st_mode & libc::S_IFMT) == libc::S_IFDIR {
|
||||||
|
match nix::dir::Dir::openat(rawfd, filename.as_ref(), OFlag::O_NOFOLLOW, Mode::empty()) {
|
||||||
|
Ok(mut dir) => self.encode_dir(&mut dir)?,
|
||||||
|
Err(nix::Error::Sys(Errno::ENOENT)) => self.report_vanished_file(&self.current_path)?,
|
||||||
|
Err(err) => bail!("open dir {:?} failed - {}", self.current_path, err),
|
||||||
|
}
|
||||||
|
|
||||||
let mut dir = nix::dir::Dir::openat(
|
|
||||||
rawfd, filename.as_ref(), OFlag::O_NOFOLLOW, Mode::empty())?;
|
|
||||||
|
|
||||||
self.encode_dir(&mut dir)?;
|
|
||||||
} else if (stat.st_mode & libc::S_IFMT) == libc::S_IFREG {
|
} else if (stat.st_mode & libc::S_IFMT) == libc::S_IFREG {
|
||||||
let filefd = nix::fcntl::openat(rawfd, filename.as_ref(), OFlag::O_NOFOLLOW, Mode::empty())?;
|
match nix::fcntl::openat(rawfd, filename.as_ref(), OFlag::O_NOFOLLOW, Mode::empty()) {
|
||||||
self.encode_file(filefd)?;
|
Ok(filefd) => {
|
||||||
|
let res = self.encode_file(filefd);
|
||||||
let _ = nix::unistd::close(filefd); // ignore close errors
|
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),
|
||||||
|
}
|
||||||
} else if (stat.st_mode & libc::S_IFMT) == libc::S_IFLNK {
|
} else if (stat.st_mode & libc::S_IFMT) == libc::S_IFLNK {
|
||||||
let mut buffer = [0u8; libc::PATH_MAX as usize];
|
let mut buffer = [0u8; libc::PATH_MAX as usize];
|
||||||
let target = nix::fcntl::readlinkat(rawfd, filename.as_ref(), &mut buffer)?;
|
match nix::fcntl::readlinkat(rawfd, filename.as_ref(), &mut buffer) {
|
||||||
self.encode_symlink(&target)?;
|
Ok(target) => self.encode_symlink(&target)?,
|
||||||
|
Err(nix::Error::Sys(Errno::ENOENT)) => self.report_vanished_file(&self.current_path)?,
|
||||||
|
Err(err) => bail!("readlink {:?} failed - {}", self.current_path, err),
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
bail!("unsupported file type (mode {:o} {:?})", stat.st_mode, self.current_path);
|
bail!("unsupported file type (mode {:o} {:?})", stat.st_mode, self.current_path);
|
||||||
}
|
}
|
||||||
|
@ -108,4 +122,14 @@ impl <W: Write> CaTarEncoder<W> {
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// the report_XXX method may raise and error - depending on encoder configuration
|
||||||
|
|
||||||
|
fn report_vanished_file(&self, path: &Path) -> Result<(), Error> {
|
||||||
|
|
||||||
|
eprintln!("WARNING: detected vanished file {:?}", path);
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue