src/catar/encoder.rs: add verbose flags

This commit is contained in:
Dietmar Maurer 2019-03-04 08:01:09 +01:00
parent e0b8ce6424
commit 219ef0e6ca
4 changed files with 22 additions and 8 deletions

View File

@ -142,7 +142,7 @@ fn create_archive(
let mut writer = std::io::BufWriter::with_capacity(1024*1024, file); let mut writer = std::io::BufWriter::with_capacity(1024*1024, file);
CaTarEncoder::encode(source, &mut dir, None, &mut writer)?; CaTarEncoder::encode(source, &mut dir, None, &mut writer, false)?;
writer.flush()?; writer.flush()?;

View File

@ -35,6 +35,7 @@ fn backup_directory<P: AsRef<Path>>(
archive_name: &str, archive_name: &str,
backup_time: DateTime<Utc>, backup_time: DateTime<Utc>,
chunk_size: Option<u64>, chunk_size: Option<u64>,
verbose: bool,
) -> Result<(), Error> { ) -> Result<(), Error> {
let mut param = json!({ let mut param = json!({
@ -52,7 +53,7 @@ fn backup_directory<P: AsRef<Path>>(
let path = format!("api2/json/admin/datastore/{}/catar?{}", repo.store, query); let path = format!("api2/json/admin/datastore/{}/catar?{}", repo.store, query);
let stream = CaTarBackupStream::open(dir_path.as_ref())?; let stream = CaTarBackupStream::open(dir_path.as_ref(), verbose)?;
let body = Body::wrap_stream(stream); let body = Body::wrap_stream(stream);
@ -300,6 +301,8 @@ fn create_backup(
let repo = BackupRepository::parse(repo_url)?; let repo = BackupRepository::parse(repo_url)?;
let verbose = param["verbose"].as_bool().unwrap_or(false);
let chunk_size_opt = param["chunk-size"].as_u64().map(|v| v*1024); let chunk_size_opt = param["chunk-size"].as_u64().map(|v| v*1024);
if let Some(size) = chunk_size_opt { if let Some(size) = chunk_size_opt {
@ -354,7 +357,7 @@ fn create_backup(
for (filename, target) in upload_list { for (filename, target) in upload_list {
flog!(log, "Upload '{}' to '{:?}' as {}", filename, repo, target); flog!(log, "Upload '{}' to '{:?}' as {}", filename, repo, target);
backup_directory(&mut client, &repo, &filename, &target, backup_time, chunk_size_opt)?; backup_directory(&mut client, &repo, &filename, &target, backup_time, chunk_size_opt, verbose)?;
} }
flog!(log, "Upload backup log"); flog!(log, "Upload backup log");
@ -432,6 +435,9 @@ fn main() {
backup_source_schema, backup_source_schema,
).min_length(1) ).min_length(1)
) )
.optional(
"verbose",
BooleanSchema::new("Verbose output.").default(false))
.optional( .optional(
"chunk-size", "chunk-size",
IntegerSchema::new("Chunk size in KB. Must be a power of 2.") IntegerSchema::new("Chunk size in KB. Must be a power of 2.")

View File

@ -35,6 +35,7 @@ pub struct CaTarEncoder<'a, W: Write> {
_size: usize, _size: usize,
file_copy_buffer: Vec<u8>, file_copy_buffer: Vec<u8>,
devices: Option<HashSet<u64>>, devices: Option<HashSet<u64>>,
verbose: bool,
} }
impl <'a, W: Write> CaTarEncoder<'a, W> { impl <'a, W: Write> CaTarEncoder<'a, W> {
@ -43,7 +44,8 @@ impl <'a, W: Write> CaTarEncoder<'a, W> {
path: PathBuf, path: PathBuf,
dir: &mut nix::dir::Dir, dir: &mut nix::dir::Dir,
device_list: Option<Vec<u64>>, device_list: Option<Vec<u64>>,
writer: &'a mut W writer: &'a mut W,
verbose: bool,
) -> Result<(), Error> { ) -> Result<(), Error> {
const FILE_COPY_BUFFER_SIZE: usize = 1024*1024; const FILE_COPY_BUFFER_SIZE: usize = 1024*1024;
@ -58,6 +60,7 @@ impl <'a, W: Write> CaTarEncoder<'a, W> {
_size: 0, _size: 0,
file_copy_buffer, file_copy_buffer,
devices: None, devices: None,
verbose,
}; };
// todo: use scandirat?? // todo: use scandirat??
@ -85,6 +88,8 @@ impl <'a, W: Write> CaTarEncoder<'a, W> {
me.devices = Some(devices); me.devices = Some(devices);
} }
if verbose { println!("{:?}", me.current_path); }
me.encode_dir(dir, &stat, magic)?; me.encode_dir(dir, &stat, magic)?;
Ok(()) Ok(())
@ -304,6 +309,8 @@ impl <'a, W: Write> CaTarEncoder<'a, W> {
for filename in &name_list { for filename in &name_list {
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 self.verbose { println!("{:?}", self.current_path); }
let stat = match nix::sys::stat::fstatat(rawfd, filename.as_ref(), nix::fcntl::AtFlags::AT_SYMLINK_NOFOLLOW) { let stat = match nix::sys::stat::fstatat(rawfd, filename.as_ref(), nix::fcntl::AtFlags::AT_SYMLINK_NOFOLLOW) {
Ok(stat) => stat, Ok(stat) => stat,
Err(nix::Error::Sys(Errno::ENOENT)) => { Err(nix::Error::Sys(Errno::ENOENT)) => {

View File

@ -36,7 +36,7 @@ impl Drop for CaTarBackupStream {
impl CaTarBackupStream { impl CaTarBackupStream {
pub fn new(mut dir: Dir, path: PathBuf) -> Result<Self, Error> { pub fn new(mut dir: Dir, path: PathBuf, verbose: bool) -> Result<Self, Error> {
let mut buffer = Vec::with_capacity(4096); let mut buffer = Vec::with_capacity(4096);
unsafe { buffer.set_len(buffer.capacity()); } unsafe { buffer.set_len(buffer.capacity()); }
@ -44,7 +44,8 @@ impl CaTarBackupStream {
let child = thread::spawn(move|| { let child = thread::spawn(move|| {
let mut writer = unsafe { std::fs::File::from_raw_fd(tx) }; let mut writer = unsafe { std::fs::File::from_raw_fd(tx) };
if let Err(err) = CaTarEncoder::encode(path, &mut dir, None, &mut writer) { let device_list = vec![];
if let Err(err) = CaTarEncoder::encode(path, &mut dir, Some(device_list), &mut writer, verbose) {
eprintln!("catar encode failed - {}", err); eprintln!("catar encode failed - {}", err);
} }
}); });
@ -54,12 +55,12 @@ impl CaTarBackupStream {
Ok(Self { pipe: Some(pipe), buffer, child: Some(child) }) Ok(Self { pipe: Some(pipe), buffer, child: Some(child) })
} }
pub fn open(dirname: &Path) -> Result<Self, Error> { pub fn open(dirname: &Path, verbose: bool) -> Result<Self, Error> {
let dir = nix::dir::Dir::open(dirname, OFlag::O_DIRECTORY, Mode::empty())?; let dir = nix::dir::Dir::open(dirname, OFlag::O_DIRECTORY, Mode::empty())?;
let path = std::path::PathBuf::from(dirname); let path = std::path::PathBuf::from(dirname);
Self::new(dir, path) Self::new(dir, path, verbose)
} }
} }