proxmox-backup/src/bin/pxar.rs

348 lines
13 KiB
Rust
Raw Normal View History

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::*;
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};
use std::fs::OpenOptions;
use std::ffi::OsStr;
use std::sync::Arc;
use std::os::unix::fs::OpenOptionsExt;
use std::os::unix::io::AsRawFd;
use std::collections::HashSet;
2019-03-15 07:24:32 +00:00
use proxmox_backup::pxar;
2019-01-06 16:42:23 +00:00
fn dump_archive_from_reader<R: std::io::Read>(
reader: &mut R,
feature_flags: u64,
verbose: bool,
) -> Result<(), Error> {
let mut decoder = pxar::SequentialDecoder::new(reader, feature_flags, |_| Ok(()));
let stdout = std::io::stdout();
let mut out = stdout.lock();
let mut path = PathBuf::new();
decoder.dump_entry(&mut path, verbose, &mut out)?;
Ok(())
}
fn dump_archive(
param: Value,
_info: &ApiMethod,
_rpcenv: &mut dyn RpcEnvironment,
) -> Result<Value, Error> {
let archive = tools::required_string_param(&param, "archive")?;
let verbose = param["verbose"].as_bool().unwrap_or(false);
let feature_flags = pxar::flags::DEFAULT;
if archive == "-" {
let stdin = std::io::stdin();
let mut reader = stdin.lock();
dump_archive_from_reader(&mut reader, feature_flags, verbose)?;
} else {
if verbose { println!("PXAR dump: {}", archive); }
let file = std::fs::File::open(archive)?;
let mut reader = std::io::BufReader::new(file);
dump_archive_from_reader(&mut reader, feature_flags, verbose)?;
}
Ok(Value::Null)
}
fn extract_archive_from_reader<R: std::io::Read>(
reader: &mut R,
target: &str,
feature_flags: u64,
allow_existing_dirs: bool,
verbose: bool,
pattern: Option<Vec<pxar::MatchPattern>>
) -> Result<(), Error> {
let mut decoder = pxar::SequentialDecoder::new(reader, feature_flags, |path| {
if verbose {
println!("{:?}", path);
}
Ok(())
});
decoder.set_allow_existing_dirs(allow_existing_dirs);
2019-10-25 16:04:37 +00:00
let pattern = pattern.unwrap_or_else(Vec::new);
decoder.restore(Path::new(target), &pattern)?;
Ok(())
}
2019-03-15 07:03:44 +00:00
fn extract_archive(
param: Value,
_info: &ApiMethod,
_rpcenv: &mut dyn RpcEnvironment,
2019-03-15 07:03:44 +00:00
) -> Result<Value, Error> {
let archive = tools::required_string_param(&param, "archive")?;
let target = param["target"].as_str().unwrap_or(".");
2019-03-15 07:03:44 +00:00
let verbose = param["verbose"].as_bool().unwrap_or(false);
let no_xattrs = param["no-xattrs"].as_bool().unwrap_or(false);
let no_fcaps = param["no-fcaps"].as_bool().unwrap_or(false);
let no_acls = param["no-acls"].as_bool().unwrap_or(false);
let no_device_nodes = param["no-device-nodes"].as_bool().unwrap_or(false);
let no_fifos = param["no-fifos"].as_bool().unwrap_or(false);
let no_sockets = param["no-sockets"].as_bool().unwrap_or(false);
let allow_existing_dirs = param["allow-existing-dirs"].as_bool().unwrap_or(false);
let files_from = param["files-from"].as_str();
let empty = Vec::new();
let arg_pattern = param["pattern"].as_array().unwrap_or(&empty);
2019-03-15 07:03:44 +00:00
let mut feature_flags = pxar::flags::DEFAULT;
if no_xattrs {
feature_flags ^= pxar::flags::WITH_XATTRS;
}
if no_fcaps {
feature_flags ^= pxar::flags::WITH_FCAPS;
}
if no_acls {
feature_flags ^= pxar::flags::WITH_ACL;
}
if no_device_nodes {
feature_flags ^= pxar::flags::WITH_DEVICE_NODES;
}
if no_fifos {
feature_flags ^= pxar::flags::WITH_FIFOS;
}
if no_sockets {
feature_flags ^= pxar::flags::WITH_SOCKETS;
}
2019-03-15 07:03:44 +00:00
let mut pattern_list = Vec::new();
if let Some(filename) = files_from {
let dir = nix::dir::Dir::open("./", nix::fcntl::OFlag::O_RDONLY, nix::sys::stat::Mode::empty())?;
if let Some((mut pattern, _, _)) = pxar::MatchPattern::from_file(dir.as_raw_fd(), filename)? {
pattern_list.append(&mut pattern);
}
}
for s in arg_pattern {
let l = s.as_str().ok_or_else(|| format_err!("Invalid pattern string slice"))?;
let p = pxar::MatchPattern::from_line(l.as_bytes())?
.ok_or_else(|| format_err!("Invalid match pattern in arguments"))?;
pattern_list.push(p);
}
2019-10-25 16:04:37 +00:00
let pattern = if pattern_list.is_empty() {
None
2019-10-25 16:04:37 +00:00
} else {
Some(pattern_list)
};
if archive == "-" {
let stdin = std::io::stdin();
let mut reader = stdin.lock();
extract_archive_from_reader(&mut reader, target, feature_flags, allow_existing_dirs, verbose, pattern)?;
} else {
if verbose { println!("PXAR extract: {}", archive); }
let file = std::fs::File::open(archive)?;
let mut reader = std::io::BufReader::new(file);
extract_archive_from_reader(&mut reader, target, feature_flags, allow_existing_dirs, verbose, pattern)?;
}
2019-03-15 07:03:44 +00:00
Ok(Value::Null)
}
fn create_archive(
param: Value,
_info: &ApiMethod,
_rpcenv: &mut dyn RpcEnvironment,
) -> Result<Value, Error> {
let archive = tools::required_string_param(&param, "archive")?;
let source = tools::required_string_param(&param, "source")?;
let verbose = param["verbose"].as_bool().unwrap_or(false);
let all_file_systems = param["all-file-systems"].as_bool().unwrap_or(false);
let no_xattrs = param["no-xattrs"].as_bool().unwrap_or(false);
let no_fcaps = param["no-fcaps"].as_bool().unwrap_or(false);
let no_acls = param["no-acls"].as_bool().unwrap_or(false);
let no_device_nodes = param["no-device-nodes"].as_bool().unwrap_or(false);
let no_fifos = param["no-fifos"].as_bool().unwrap_or(false);
let no_sockets = param["no-sockets"].as_bool().unwrap_or(false);
let empty = Vec::new();
let exclude_pattern = param["exclude"].as_array().unwrap_or(&empty);
2019-01-07 12:25:41 +00:00
let devices = if all_file_systems { None } else { Some(HashSet::new()) };
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())?;
let file = OpenOptions::new()
2019-01-07 12:25:41 +00:00
.create_new(true)
.write(true)
.mode(0o640)
2019-01-07 12:25:41 +00:00
.open(archive)?;
let mut writer = std::io::BufWriter::with_capacity(1024*1024, file);
let mut feature_flags = pxar::flags::DEFAULT;
if no_xattrs {
feature_flags ^= pxar::flags::WITH_XATTRS;
}
if no_fcaps {
feature_flags ^= pxar::flags::WITH_FCAPS;
}
if no_acls {
feature_flags ^= pxar::flags::WITH_ACL;
}
if no_device_nodes {
feature_flags ^= pxar::flags::WITH_DEVICE_NODES;
}
if no_fifos {
feature_flags ^= pxar::flags::WITH_FIFOS;
}
if no_sockets {
feature_flags ^= pxar::flags::WITH_SOCKETS;
}
let mut pattern_list = Vec::new();
for s in exclude_pattern {
let l = s.as_str().ok_or_else(|| format_err!("Invalid pattern string slice"))?;
let p = pxar::MatchPattern::from_line(l.as_bytes())?
.ok_or_else(|| format_err!("Invalid match pattern in arguments"))?;
pattern_list.push(p);
}
let catalog = None::<&mut pxar::catalog::DummyCatalogWriter>;
pxar::Encoder::encode(
source,
&mut dir,
&mut writer,
catalog,
devices,
verbose,
false,
feature_flags,
pattern_list,
)?;
2019-01-07 12:25:41 +00:00
writer.flush()?;
Ok(Value::Null)
}
/// Mount the archive to the provided mountpoint via FUSE.
fn mount_archive(
param: Value,
_info: &ApiMethod,
_rpcenv: &mut dyn RpcEnvironment,
) -> Result<Value, Error> {
let archive = tools::required_string_param(&param, "archive")?;
let mountpoint = tools::required_string_param(&param, "mountpoint")?;
let verbose = param["verbose"].as_bool().unwrap_or(false);
let no_mt = param["no-mt"].as_bool().unwrap_or(false);
let archive = Path::new(archive);
let mountpoint = Path::new(mountpoint);
let options = OsStr::new("ro,default_permissions");
let mut session = pxar::fuse::Session::new(&archive, &options, verbose)
.map_err(|err| format_err!("pxar mount failed: {}", err))?;
// Mount the session and deamonize if verbose is not set
session.mount(&mountpoint, !verbose)?;
session.run_loop(!no_mt)?;
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,
ObjectSchema::new("Create new .pxar archive.")
.required("archive", StringSchema::new("Archive name"))
.required("source", StringSchema::new("Source directory."))
.optional("verbose", BooleanSchema::new("Verbose output.").default(false))
.optional("no-xattrs", BooleanSchema::new("Ignore extended file attributes.").default(false))
.optional("no-fcaps", BooleanSchema::new("Ignore file capabilities.").default(false))
.optional("no-acls", BooleanSchema::new("Ignore access control list entries.").default(false))
.optional("all-file-systems", BooleanSchema::new("Include mounted sudirs.").default(false))
.optional("no-device-nodes", BooleanSchema::new("Ignore device nodes.").default(false))
.optional("no-fifos", BooleanSchema::new("Ignore fifos.").default(false))
.optional("no-sockets", BooleanSchema::new("Ignore sockets.").default(false))
.optional("exclude", Arc::new(
ArraySchema::new(
"List of paths or pattern matching files to exclude.",
Arc::new(StringSchema::new("Path or pattern matching files to restore.").into())
).into()
))
))
.arg_param(vec!["archive", "source", "exclude"])
2019-01-20 08:55:20 +00:00
.completion_cb("archive", tools::complete_file_name)
.completion_cb("source", tools::complete_file_name)
.into()
)
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."))
.optional("pattern", Arc::new(
ArraySchema::new(
"List of paths or pattern matching files to restore",
Arc::new(StringSchema::new("Path or pattern matching files to restore.").into())
).into()
))
.optional("target", StringSchema::new("Target directory."))
2019-03-15 07:03:44 +00:00
.optional("verbose", BooleanSchema::new("Verbose output.").default(false))
.optional("no-xattrs", BooleanSchema::new("Ignore extended file attributes.").default(false))
.optional("no-fcaps", BooleanSchema::new("Ignore file capabilities.").default(false))
.optional("no-acls", BooleanSchema::new("Ignore access control list entries.").default(false))
.optional("allow-existing-dirs", BooleanSchema::new("Allows directories to already exist on restore.").default(false))
.optional("files-from", StringSchema::new("Match pattern for files to restore."))
.optional("no-device-nodes", BooleanSchema::new("Ignore device nodes.").default(false))
.optional("no-fifos", BooleanSchema::new("Ignore fifos.").default(false))
.optional("no-sockets", BooleanSchema::new("Ignore sockets.").default(false))
))
.arg_param(vec!["archive", "pattern"])
2019-03-15 07:03:44 +00:00
.completion_cb("archive", tools::complete_file_name)
.completion_cb("target", tools::complete_file_name)
.completion_cb("files-from", tools::complete_file_name)
2019-03-15 07:03:44 +00:00
.into()
)
.insert("mount", CliCommand::new(
ApiMethod::new(
mount_archive,
ObjectSchema::new("Mount the archive as filesystem via FUSE.")
.required("archive", StringSchema::new("Archive name."))
.required("mountpoint", StringSchema::new("Mountpoint for the filesystem root."))
.optional("verbose", BooleanSchema::new("Verbose output, keeps process running in foreground (for debugging).").default(false))
.optional("no-mt", BooleanSchema::new("Run in single threaded mode (for debugging).").default(false))
))
.arg_param(vec!["archive", "mountpoint"])
.completion_cb("archive", tools::complete_file_name)
.completion_cb("mountpoint", tools::complete_file_name)
.into()
)
2019-01-06 16:42:23 +00:00
.insert("list", CliCommand::new(
ApiMethod::new(
dump_archive,
ObjectSchema::new("List the contents of an archive.")
.required("archive", StringSchema::new("Archive name."))
.optional("verbose", BooleanSchema::new("Verbose output.").default(false))
))
.arg_param(vec!["archive"])
2019-01-20 08:55:20 +00:00
.completion_cb("archive", tools::complete_file_name)
.into()
);
2019-02-23 14:10:48 +00:00
run_cli_command(cmd_def.into());
}