2018-12-30 17:01:20 +00:00
|
|
|
extern crate proxmox_backup;
|
|
|
|
|
|
|
|
use failure::*;
|
|
|
|
|
2019-01-20 08:55:20 +00:00
|
|
|
use proxmox_backup::tools;
|
2019-02-21 08:07:25 +00:00
|
|
|
use proxmox_backup::cli::*;
|
2019-02-17 09:16:33 +00:00
|
|
|
use proxmox_backup::api_schema::*;
|
2019-02-17 08:59:20 +00:00
|
|
|
use proxmox_backup::api_schema::router::*;
|
2018-12-30 17:01:20 +00:00
|
|
|
|
|
|
|
use serde_json::{Value};
|
|
|
|
|
2019-03-14 16:43:11 +00:00
|
|
|
use std::io::Write;
|
2019-03-15 07:03:44 +00:00
|
|
|
use std::path::{Path, PathBuf};
|
2019-05-27 12:16:13 +00:00
|
|
|
use std::fs::OpenOptions;
|
|
|
|
use std::os::unix::fs::OpenOptionsExt;
|
2018-12-30 17:01:20 +00:00
|
|
|
|
2019-03-15 07:24:32 +00:00
|
|
|
use proxmox_backup::pxar;
|
2019-01-06 16:42:23 +00:00
|
|
|
|
2019-01-26 13:50:37 +00:00
|
|
|
fn print_filenames(
|
2019-03-14 16:43:11 +00:00
|
|
|
param: Value,
|
2019-01-26 13:50:37 +00:00
|
|
|
_info: &ApiMethod,
|
|
|
|
_rpcenv: &mut RpcEnvironment,
|
|
|
|
) -> Result<Value, Error> {
|
2019-01-06 16:42:23 +00:00
|
|
|
|
2019-01-26 13:50:37 +00:00
|
|
|
let archive = tools::required_string_param(¶m, "archive")?;
|
2019-01-06 16:42:23 +00:00
|
|
|
let file = std::fs::File::open(archive)?;
|
|
|
|
|
|
|
|
let mut reader = std::io::BufReader::new(file);
|
|
|
|
|
2019-05-23 11:10:20 +00:00
|
|
|
let mut feature_flags = pxar::CA_FORMAT_DEFAULT;
|
|
|
|
feature_flags ^= pxar::CA_FORMAT_WITH_XATTRS;
|
|
|
|
feature_flags ^= pxar::CA_FORMAT_WITH_FCAPS;
|
|
|
|
let mut decoder = pxar::SequentialDecoder::new(&mut reader, feature_flags);
|
2019-01-06 16:42:23 +00:00
|
|
|
|
|
|
|
let stdout = std::io::stdout();
|
|
|
|
let mut out = stdout.lock();
|
|
|
|
|
2019-03-14 16:43:11 +00:00
|
|
|
let mut path = PathBuf::from(".");
|
|
|
|
decoder.dump_entry(&mut path, false, &mut out)?;
|
2019-01-06 16:42:23 +00:00
|
|
|
|
|
|
|
Ok(Value::Null)
|
|
|
|
}
|
|
|
|
|
2019-01-26 13:50:37 +00:00
|
|
|
fn dump_archive(
|
|
|
|
param: Value,
|
|
|
|
_info: &ApiMethod,
|
|
|
|
_rpcenv: &mut RpcEnvironment,
|
|
|
|
) -> Result<Value, Error> {
|
2018-12-30 17:01:20 +00:00
|
|
|
|
2019-01-26 13:50:37 +00:00
|
|
|
let archive = tools::required_string_param(¶m, "archive")?;
|
2019-03-14 12:10:27 +00:00
|
|
|
let file = std::fs::File::open(archive)?;
|
2018-12-30 17:01:20 +00:00
|
|
|
|
2019-03-14 12:10:27 +00:00
|
|
|
let mut reader = std::io::BufReader::new(file);
|
2018-12-30 17:01:20 +00:00
|
|
|
|
2019-05-23 11:10:20 +00:00
|
|
|
let mut feature_flags = pxar::CA_FORMAT_DEFAULT;
|
|
|
|
feature_flags ^= pxar::CA_FORMAT_WITH_XATTRS;
|
|
|
|
feature_flags ^= pxar::CA_FORMAT_WITH_FCAPS;
|
|
|
|
let mut decoder = pxar::SequentialDecoder::new(&mut reader, feature_flags);
|
2018-12-30 17:01:20 +00:00
|
|
|
|
2019-03-14 12:10:27 +00:00
|
|
|
let stdout = std::io::stdout();
|
|
|
|
let mut out = stdout.lock();
|
2018-12-30 17:01:20 +00:00
|
|
|
|
2019-03-14 12:10:27 +00:00
|
|
|
println!("PXAR dump: {}", archive);
|
2018-12-30 17:01:20 +00:00
|
|
|
|
2019-03-14 16:43:11 +00:00
|
|
|
let mut path = PathBuf::new();
|
|
|
|
decoder.dump_entry(&mut path, true, &mut out)?;
|
2018-12-30 17:01:20 +00:00
|
|
|
|
|
|
|
Ok(Value::Null)
|
|
|
|
}
|
|
|
|
|
2019-03-15 07:03:44 +00:00
|
|
|
fn extract_archive(
|
|
|
|
param: Value,
|
|
|
|
_info: &ApiMethod,
|
|
|
|
_rpcenv: &mut RpcEnvironment,
|
|
|
|
) -> Result<Value, Error> {
|
|
|
|
|
|
|
|
let archive = tools::required_string_param(¶m, "archive")?;
|
|
|
|
let target = tools::required_string_param(¶m, "target")?;
|
|
|
|
let verbose = param["verbose"].as_bool().unwrap_or(false);
|
2019-05-22 15:50:43 +00:00
|
|
|
let no_xattrs = param["no-xattrs"].as_bool().unwrap_or(false);
|
|
|
|
let no_fcaps = param["no-fcaps"].as_bool().unwrap_or(false);
|
2019-03-15 07:03:44 +00:00
|
|
|
|
|
|
|
let file = std::fs::File::open(archive)?;
|
|
|
|
|
|
|
|
let mut reader = std::io::BufReader::new(file);
|
2019-05-23 11:10:20 +00:00
|
|
|
let mut feature_flags = pxar::CA_FORMAT_DEFAULT;
|
|
|
|
if no_xattrs {
|
|
|
|
feature_flags ^= pxar::CA_FORMAT_WITH_XATTRS;
|
|
|
|
}
|
|
|
|
if no_fcaps {
|
|
|
|
feature_flags ^= pxar::CA_FORMAT_WITH_FCAPS;
|
|
|
|
}
|
2019-03-15 07:03:44 +00:00
|
|
|
|
2019-05-23 11:10:20 +00:00
|
|
|
let mut decoder = pxar::SequentialDecoder::new(&mut reader, feature_flags);
|
2019-03-15 07:03:44 +00:00
|
|
|
|
|
|
|
decoder.restore(Path::new(target), & |path| {
|
|
|
|
if verbose {
|
|
|
|
println!("{:?}", path);
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
})?;
|
|
|
|
|
|
|
|
Ok(Value::Null)
|
|
|
|
}
|
|
|
|
|
2019-01-26 13:50:37 +00:00
|
|
|
fn create_archive(
|
|
|
|
param: Value,
|
|
|
|
_info: &ApiMethod,
|
|
|
|
_rpcenv: &mut RpcEnvironment,
|
|
|
|
) -> Result<Value, Error> {
|
2018-12-30 17:01:20 +00:00
|
|
|
|
2019-01-26 13:50:37 +00:00
|
|
|
let archive = tools::required_string_param(¶m, "archive")?;
|
|
|
|
let source = tools::required_string_param(¶m, "source")?;
|
2019-03-08 07:20:56 +00:00
|
|
|
let verbose = param["verbose"].as_bool().unwrap_or(false);
|
2019-03-08 08:28:12 +00:00
|
|
|
let all_file_systems = param["all-file-systems"].as_bool().unwrap_or(false);
|
2019-05-22 15:50:43 +00:00
|
|
|
let no_xattrs = param["no-xattrs"].as_bool().unwrap_or(false);
|
|
|
|
let no_fcaps = param["no-fcaps"].as_bool().unwrap_or(false);
|
2019-01-07 12:25:41 +00:00
|
|
|
|
2019-03-15 07:03:44 +00:00
|
|
|
let source = PathBuf::from(source);
|
2019-01-07 12:25:41 +00:00
|
|
|
|
|
|
|
let mut dir = nix::dir::Dir::open(
|
|
|
|
&source, nix::fcntl::OFlag::O_NOFOLLOW, nix::sys::stat::Mode::empty())?;
|
|
|
|
|
2019-05-27 12:16:13 +00:00
|
|
|
let file = OpenOptions::new()
|
2019-01-07 12:25:41 +00:00
|
|
|
.create_new(true)
|
|
|
|
.write(true)
|
2019-05-27 12:16:13 +00:00
|
|
|
.mode(0o640)
|
2019-01-07 12:25:41 +00:00
|
|
|
.open(archive)?;
|
|
|
|
|
|
|
|
let mut writer = std::io::BufWriter::with_capacity(1024*1024, file);
|
2019-05-23 11:10:20 +00:00
|
|
|
let mut feature_flags = pxar::CA_FORMAT_DEFAULT;
|
|
|
|
if no_xattrs {
|
|
|
|
feature_flags ^= pxar::CA_FORMAT_WITH_XATTRS;
|
|
|
|
}
|
|
|
|
if no_fcaps {
|
|
|
|
feature_flags ^= pxar::CA_FORMAT_WITH_FCAPS;
|
|
|
|
}
|
|
|
|
|
|
|
|
pxar::Encoder::encode(source, &mut dir, &mut writer, all_file_systems, verbose, feature_flags)?;
|
2018-12-30 17:01:20 +00:00
|
|
|
|
2019-01-07 12:25:41 +00:00
|
|
|
writer.flush()?;
|
2018-12-30 17:01:20 +00:00
|
|
|
|
|
|
|
Ok(Value::Null)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
|
|
|
|
let cmd_def = CliCommandMap::new()
|
|
|
|
.insert("create", CliCommand::new(
|
|
|
|
ApiMethod::new(
|
2019-01-07 12:25:41 +00:00
|
|
|
create_archive,
|
2019-03-14 09:54:09 +00:00
|
|
|
ObjectSchema::new("Create new .pxar archive.")
|
2018-12-30 17:01:20 +00:00
|
|
|
.required("archive", StringSchema::new("Archive name"))
|
|
|
|
.required("source", StringSchema::new("Source directory."))
|
2019-03-08 07:20:56 +00:00
|
|
|
.optional("verbose", BooleanSchema::new("Verbose output.").default(false))
|
2019-05-22 15:50:43 +00:00
|
|
|
.optional("no-xattrs", BooleanSchema::new("Ignore extended file attributes.").default(false))
|
|
|
|
.optional("no-fcaps", BooleanSchema::new("Ignore file capabilities.").default(false))
|
2019-03-08 08:28:12 +00:00
|
|
|
.optional("all-file-systems", BooleanSchema::new("Include mounted sudirs.").default(false))
|
|
|
|
))
|
2018-12-30 17:01:20 +00:00
|
|
|
.arg_param(vec!["archive", "source"])
|
2019-01-20 08:55:20 +00:00
|
|
|
.completion_cb("archive", tools::complete_file_name)
|
|
|
|
.completion_cb("source", tools::complete_file_name)
|
|
|
|
.into()
|
2018-12-30 17:01:20 +00:00
|
|
|
)
|
2019-03-15 07:03:44 +00:00
|
|
|
.insert("extract", CliCommand::new(
|
|
|
|
ApiMethod::new(
|
|
|
|
extract_archive,
|
|
|
|
ObjectSchema::new("Extract an archive.")
|
|
|
|
.required("archive", StringSchema::new("Archive name."))
|
|
|
|
.required("target", StringSchema::new("Target directory."))
|
|
|
|
.optional("verbose", BooleanSchema::new("Verbose output.").default(false))
|
2019-05-22 15:50:43 +00:00
|
|
|
.optional("no-xattrs", BooleanSchema::new("Ignore extended file attributes.").default(false))
|
|
|
|
.optional("no-fcaps", BooleanSchema::new("Ignore file capabilities.").default(false))
|
2019-03-15 07:03:44 +00:00
|
|
|
))
|
|
|
|
.arg_param(vec!["archive", "target"])
|
|
|
|
.completion_cb("archive", tools::complete_file_name)
|
|
|
|
.completion_cb("target", tools::complete_file_name)
|
|
|
|
.into()
|
|
|
|
)
|
2019-01-06 16:42:23 +00:00
|
|
|
.insert("list", CliCommand::new(
|
|
|
|
ApiMethod::new(
|
|
|
|
print_filenames,
|
|
|
|
ObjectSchema::new("List the contents of an archive.")
|
|
|
|
.required("archive", StringSchema::new("Archive name."))
|
|
|
|
))
|
|
|
|
.arg_param(vec!["archive"])
|
2019-01-20 08:55:20 +00:00
|
|
|
.completion_cb("archive", tools::complete_file_name)
|
2019-01-06 16:42:23 +00:00
|
|
|
.into()
|
|
|
|
)
|
2018-12-30 17:01:20 +00:00
|
|
|
.insert("dump", CliCommand::new(
|
|
|
|
ApiMethod::new(
|
|
|
|
dump_archive,
|
|
|
|
ObjectSchema::new("Textual dump of archive contents (debug toolkit).")
|
|
|
|
.required("archive", StringSchema::new("Archive name."))
|
|
|
|
))
|
|
|
|
.arg_param(vec!["archive"])
|
2019-01-20 08:55:20 +00:00
|
|
|
.completion_cb("archive", tools::complete_file_name)
|
2018-12-30 17:01:20 +00:00
|
|
|
.into()
|
|
|
|
);
|
|
|
|
|
2019-02-23 14:10:48 +00:00
|
|
|
run_cli_command(cmd_def.into());
|
2018-12-30 17:01:20 +00:00
|
|
|
}
|