catar/encoder.rs: first try
This commit is contained in:
		@ -10,6 +10,7 @@ use proxmox_backup::api::router::*;
 | 
			
		||||
//use proxmox_backup::backup::chunk_store::*;
 | 
			
		||||
//use proxmox_backup::backup::image_index::*;
 | 
			
		||||
//use proxmox_backup::config::datastore;
 | 
			
		||||
use proxmox_backup::catar::encoder::*;
 | 
			
		||||
use proxmox_backup::backup::datastore::*;
 | 
			
		||||
use serde_json::{Value};
 | 
			
		||||
 | 
			
		||||
@ -17,7 +18,13 @@ fn required_string_param<'a>(param: &'a Value, name: &str) -> &'a str {
 | 
			
		||||
    param[name].as_str().expect(&format!("missing parameter '{}'", name))
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
fn backup_dir(datastore: &DataStore, file: &std::fs::File, target: &str, chunk_size: usize) -> Result<(), Error> {
 | 
			
		||||
fn backup_dir(
 | 
			
		||||
    datastore: &DataStore,
 | 
			
		||||
    path: &str,
 | 
			
		||||
    dir: &mut nix::dir::Dir,
 | 
			
		||||
    target: &str,
 | 
			
		||||
    chunk_size: usize,
 | 
			
		||||
) -> Result<(), Error> {
 | 
			
		||||
 | 
			
		||||
    let mut target = std::path::PathBuf::from(target);
 | 
			
		||||
 | 
			
		||||
@ -29,7 +36,12 @@ fn backup_dir(datastore: &DataStore, file: &std::fs::File, target: &str, chunk_s
 | 
			
		||||
        target.set_extension("aidx");
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    bail!("not implemented");
 | 
			
		||||
    // fixme: implement chunked writer
 | 
			
		||||
    let writer = std::fs::File::create("mytest.catar")?;
 | 
			
		||||
 | 
			
		||||
    let path = std::path::PathBuf::from(path);
 | 
			
		||||
 | 
			
		||||
    CaTarEncoder::encode(path, dir, writer)?;
 | 
			
		||||
 | 
			
		||||
    Ok(())
 | 
			
		||||
}
 | 
			
		||||
@ -79,12 +91,15 @@ fn create_backup(param: Value, _info: &ApiMethod) -> Result<Value, Error> {
 | 
			
		||||
    let datastore = DataStore::open(store)?;
 | 
			
		||||
 | 
			
		||||
    let file = std::fs::File::open(filename)?;
 | 
			
		||||
    let stat = nix::sys::stat::fstat(file.as_raw_fd())?;
 | 
			
		||||
    let rawfd = file.as_raw_fd();
 | 
			
		||||
    let stat = nix::sys::stat::fstat(rawfd)?;
 | 
			
		||||
 | 
			
		||||
    if (stat.st_mode & libc::S_IFDIR) != 0 {
 | 
			
		||||
        println!("Backup directory '{}' to '{}'", filename, store);
 | 
			
		||||
 | 
			
		||||
        backup_dir(&datastore, &file, &target, chunk_size)?;
 | 
			
		||||
        let mut dir = nix::dir::Dir::from_fd(rawfd)?;
 | 
			
		||||
 | 
			
		||||
        backup_dir(&datastore, &filename, &mut dir, &target, chunk_size)?;
 | 
			
		||||
 | 
			
		||||
    } else if (stat.st_mode & (libc::S_IFREG|libc::S_IFBLK)) != 0 {
 | 
			
		||||
        println!("Backup file '{}' to '{}'", filename, store);
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										82
									
								
								src/catar/encoder.rs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										82
									
								
								src/catar/encoder.rs
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,82 @@
 | 
			
		||||
use failure::*;
 | 
			
		||||
 | 
			
		||||
use super::format_definition::*;
 | 
			
		||||
 | 
			
		||||
use std::io::Write;
 | 
			
		||||
use std::os::unix::io::AsRawFd;
 | 
			
		||||
use std::os::unix::ffi::OsStrExt;
 | 
			
		||||
 | 
			
		||||
pub struct CaTarEncoder<W: Write> {
 | 
			
		||||
    current_path: std::path::PathBuf, // used for error reporting
 | 
			
		||||
    writer: W,
 | 
			
		||||
    size: usize,
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
impl <W: Write> CaTarEncoder<W> {
 | 
			
		||||
 | 
			
		||||
    pub fn encode(mut path: std::path::PathBuf, dir: &mut nix::dir::Dir, writer: W) -> Result<(), Error> {
 | 
			
		||||
        let mut me = Self {
 | 
			
		||||
            current_path: path,
 | 
			
		||||
            writer: writer,
 | 
			
		||||
            size: 0,
 | 
			
		||||
        };
 | 
			
		||||
 | 
			
		||||
        // todo: use scandirat??
 | 
			
		||||
 | 
			
		||||
        me.encode_dir(dir)?;
 | 
			
		||||
 | 
			
		||||
        Ok(())
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    fn encode_dir(&mut self, dir: &mut nix::dir::Dir)  -> Result<(), Error> {
 | 
			
		||||
 | 
			
		||||
        println!("encode_dir: {:?}", self.current_path);
 | 
			
		||||
 | 
			
		||||
        let mut name_list = vec![];
 | 
			
		||||
 | 
			
		||||
        let rawfd = dir.as_raw_fd();
 | 
			
		||||
 | 
			
		||||
        let dir_stat = match nix::sys::stat::fstat(rawfd) {
 | 
			
		||||
            Ok(stat) => stat,
 | 
			
		||||
            Err(err) => bail!("fstat failed - {}", err),
 | 
			
		||||
        };
 | 
			
		||||
 | 
			
		||||
        for entry in dir.iter() {
 | 
			
		||||
            let entry = match entry {
 | 
			
		||||
                Ok(entry) => entry,
 | 
			
		||||
                Err(err) => bail!("readir failed - {}", err),
 | 
			
		||||
            };
 | 
			
		||||
            let mut filename = entry.file_name().to_owned();
 | 
			
		||||
 | 
			
		||||
            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; }
 | 
			
		||||
 | 
			
		||||
            if let Ok(stat) = nix::sys::stat::fstatat(rawfd, filename.as_ref(), nix::fcntl::AtFlags::AT_SYMLINK_NOFOLLOW) {
 | 
			
		||||
                //println!("Found {:?}", filename);
 | 
			
		||||
                name_list.push((filename, stat));
 | 
			
		||||
            } else {
 | 
			
		||||
                bail!("fsstat failed");
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        name_list.sort_unstable_by(|a, b| a.0.cmp(&b.0));
 | 
			
		||||
 | 
			
		||||
        for (filename, stat) in name_list {
 | 
			
		||||
            //println!("SORTED {:?}", filename);
 | 
			
		||||
 | 
			
		||||
            if (stat.st_mode & libc::S_IFDIR) != 0 {
 | 
			
		||||
 | 
			
		||||
                let mut dir = nix::dir::Dir::openat(
 | 
			
		||||
                    rawfd, filename.as_ref(), nix::fcntl::OFlag::O_NOFOLLOW, nix::sys::stat::Mode::empty())?;
 | 
			
		||||
 | 
			
		||||
                self.current_path.push(std::ffi::OsStr::from_bytes(filename.as_bytes()));
 | 
			
		||||
                self.encode_dir(&mut dir)?;
 | 
			
		||||
                self.current_path.pop();
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        Ok(())
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@ -33,6 +33,7 @@ pub mod server {
 | 
			
		||||
pub mod catar {
 | 
			
		||||
 | 
			
		||||
    pub mod format_definition;
 | 
			
		||||
    pub mod encoder;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
pub mod section_config;
 | 
			
		||||
 | 
			
		||||
		Reference in New Issue
	
	Block a user