proxmox-backup-client: implement upload to server

This commit is contained in:
Dietmar Maurer 2019-01-17 11:38:22 +01:00
parent 597641fdd5
commit 23bb8780d4
2 changed files with 39 additions and 37 deletions

View File

@ -7,49 +7,38 @@ use proxmox_backup::tools;
use proxmox_backup::cli::command::*; use proxmox_backup::cli::command::*;
use proxmox_backup::api::schema::*; use proxmox_backup::api::schema::*;
use proxmox_backup::api::router::*; use proxmox_backup::api::router::*;
use proxmox_backup::client::http_client::*;
use proxmox_backup::client::catar_backup_stream::*;
//use proxmox_backup::backup::chunk_store::*; //use proxmox_backup::backup::chunk_store::*;
//use proxmox_backup::backup::image_index::*; //use proxmox_backup::backup::image_index::*;
//use proxmox_backup::config::datastore; //use proxmox_backup::config::datastore;
use proxmox_backup::catar::encoder::*; //use proxmox_backup::catar::encoder::*;
use proxmox_backup::backup::datastore::*; use proxmox_backup::backup::datastore::*;
use serde_json::{Value}; use serde_json::{Value};
use hyper::Body;
fn required_string_param<'a>(param: &'a Value, name: &str) -> &'a str { fn required_string_param<'a>(param: &'a Value, name: &str) -> &'a str {
param[name].as_str().expect(&format!("missing parameter '{}'", name)) param[name].as_str().expect(&format!("missing parameter '{}'", name))
} }
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); fn backup_directory(body: Body, store: &str, archive_name: &str) -> Result<(), Error> {
if let Some(ext) = target.extension() { let client = HttpClient::new("localhost");
if ext != "aidx" {
bail!("got wrong file extension - expected '.aidx'");
}
} else {
target.set_extension("aidx");
}
let mut index = datastore.create_archive_writer(&target, chunk_size)?; let path = format!("api3/json/admin/datastore/{}/upload_catar?archive_name={}", store, archive_name);
let path = std::path::PathBuf::from(path); client.upload(body, &path)?;
CaTarEncoder::encode(path, dir, None, &mut index)?;
index.close()?; // commit changes
Ok(()) Ok(())
} }
/****
fn backup_image(datastore: &DataStore, file: &std::fs::File, size: usize, target: &str, chunk_size: usize) -> Result<(), Error> { fn backup_image(datastore: &DataStore, file: &std::fs::File, size: usize, target: &str, chunk_size: usize) -> Result<(), Error> {
let mut target = std::path::PathBuf::from(target); let mut target = PathBuf::from(target);
if let Some(ext) = target.extension() { if let Some(ext) = target.extension() {
if ext != "iidx" { if ext != "iidx" {
@ -70,6 +59,7 @@ fn backup_image(datastore: &DataStore, file: &std::fs::File, size: usize, target
Ok(()) Ok(())
} }
*/
fn create_backup(param: Value, _info: &ApiMethod) -> Result<Value, Error> { fn create_backup(param: Value, _info: &ApiMethod) -> Result<Value, Error> {
@ -89,26 +79,29 @@ fn create_backup(param: Value, _info: &ApiMethod) -> Result<Value, Error> {
} }
} }
let datastore = DataStore::open(store)?; let stat = match nix::sys::stat::stat(filename) {
Ok(s) => s,
let file = std::fs::File::open(filename)?; Err(err) => bail!("unable to access '{}' - {}", filename, err),
let rawfd = file.as_raw_fd(); };
let stat = nix::sys::stat::fstat(rawfd)?;
if (stat.st_mode & libc::S_IFDIR) != 0 { if (stat.st_mode & libc::S_IFDIR) != 0 {
println!("Backup directory '{}' to '{}'", filename, store); println!("Backup directory '{}' to '{}'", filename, store);
let mut dir = nix::dir::Dir::from_fd(rawfd)?; let stream = CaTarBackupStream::open(filename)?;
backup_dir(&datastore, &filename, &mut dir, &target, chunk_size)?; let body = Body::wrap_stream(stream);
backup_directory(body, store, target)?;
} else if (stat.st_mode & (libc::S_IFREG|libc::S_IFBLK)) != 0 { } else if (stat.st_mode & (libc::S_IFREG|libc::S_IFBLK)) != 0 {
println!("Backup file '{}' to '{}'", filename, store); println!("Backup image '{}' to '{}'", filename, store);
if stat.st_size <= 0 { bail!("got strange file size '{}'", stat.st_size); } if stat.st_size <= 0 { bail!("got strange file size '{}'", stat.st_size); }
let size = stat.st_size as usize; let size = stat.st_size as usize;
backup_image(&datastore, &file, size, &target, chunk_size)?; panic!("implement me");
//backup_image(&datastore, &file, size, &target, chunk_size)?;
// let idx = datastore.open_image_reader(target)?; // let idx = datastore.open_image_reader(target)?;
// idx.print_info(); // idx.print_info();

View File

@ -2,12 +2,14 @@ use failure::*;
use std::thread; use std::thread;
use std::os::unix::io::FromRawFd; use std::os::unix::io::FromRawFd;
use std::path::PathBuf;
use futures::{Async, Poll}; use futures::{Async, Poll};
use futures::stream::Stream; use futures::stream::Stream;
use nix::fcntl::OFlag; use nix::fcntl::OFlag;
use nix::sys::stat::Mode; use nix::sys::stat::Mode;
use nix::dir::Dir;
use crate::catar::encoder::*; use crate::catar::encoder::*;
@ -20,22 +22,21 @@ pub struct CaTarBackupStream {
impl Drop for CaTarBackupStream { impl Drop for CaTarBackupStream {
fn drop(&mut self) { fn drop(&mut self) {
println!("START DROP");
drop(self.pipe.take()); drop(self.pipe.take());
self.child.take().unwrap().join().unwrap(); self.child.take().unwrap().join().unwrap();
println!("END DROP");
} }
} }
impl CaTarBackupStream { impl CaTarBackupStream {
pub fn new(dirname: &str) -> Result<Self, Error> { pub fn new(mut dir: Dir, path: PathBuf) -> 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()); }
let (rx, tx) = nix::unistd::pipe()?; let (rx, tx) = nix::unistd::pipe()?;
let mut dir = nix::dir::Dir::open(dirname, OFlag::O_DIRECTORY, Mode::empty())?;
let path = std::path::PathBuf::from(dirname);
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) { if let Err(err) = CaTarEncoder::encode(path, &mut dir, None, &mut writer) {
@ -47,6 +48,14 @@ impl CaTarBackupStream {
Ok(Self { pipe: Some(pipe), buffer, child: Some(child) }) Ok(Self { pipe: Some(pipe), buffer, child: Some(child) })
} }
pub fn open(dirname: &str) -> Result<Self, Error> {
let mut dir = nix::dir::Dir::open(dirname, OFlag::O_DIRECTORY, Mode::empty())?;
let path = std::path::PathBuf::from(dirname);
Self::new(dir, path)
}
} }
impl Stream for CaTarBackupStream { impl Stream for CaTarBackupStream {