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-01-07 12:25:41 +00:00
|
|
|
use std::io::{Read, Write};
|
2019-01-06 16:42:23 +00:00
|
|
|
use std::path::PathBuf;
|
2018-12-30 17:01:20 +00:00
|
|
|
|
|
|
|
use proxmox_backup::catar::format_definition::*;
|
2019-01-07 12:25:41 +00:00
|
|
|
use proxmox_backup::catar::encoder::*;
|
2019-01-06 16:42:23 +00:00
|
|
|
use proxmox_backup::catar::decoder::*;
|
|
|
|
|
2018-12-30 17:01:20 +00:00
|
|
|
use proxmox_backup::tools::*;
|
|
|
|
|
|
|
|
fn print_goodby_entries(buffer: &[u8]) -> Result<(), Error> {
|
|
|
|
println!("GOODBY START: {}", buffer.len());
|
|
|
|
|
|
|
|
let entry_size = std::mem::size_of::<CaFormatGoodbyeItem>();
|
|
|
|
if (buffer.len() % entry_size) != 0 {
|
|
|
|
bail!("unexpected goodby item size ({})", entry_size);
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut pos = 0;
|
|
|
|
|
2018-12-31 09:54:25 +00:00
|
|
|
while pos < buffer.len() {
|
2018-12-30 17:01:20 +00:00
|
|
|
|
|
|
|
let item = map_struct::<CaFormatGoodbyeItem>(&buffer[pos..pos+entry_size])?;
|
|
|
|
|
|
|
|
if item.hash == CA_FORMAT_GOODBYE_TAIL_MARKER {
|
|
|
|
println!(" Entry Offset: {}", item.offset);
|
|
|
|
if item.size != (buffer.len() + 16) as u64 {
|
|
|
|
bail!("gut unexpected goodby entry size (tail marker)");
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
println!(" Offset: {}", item.offset);
|
|
|
|
println!(" Size: {}", item.size);
|
|
|
|
println!(" Hash: {:016x}", item.hash);
|
|
|
|
}
|
|
|
|
|
|
|
|
pos += entry_size;
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2019-01-26 13:50:37 +00:00
|
|
|
fn print_filenames(
|
2019-03-14 09:14:32 +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-03-08 15:55:54 +00:00
|
|
|
/* FIXME
|
|
|
|
|
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-03-08 15:55:54 +00:00
|
|
|
let mut decoder = CaTarDecoder::new(&mut reader)?;
|
2019-01-06 16:42:23 +00:00
|
|
|
|
|
|
|
let root = decoder.root();
|
|
|
|
|
|
|
|
let stdout = std::io::stdout();
|
|
|
|
let mut out = stdout.lock();
|
|
|
|
|
|
|
|
decoder.print_filenames(&mut out, &mut PathBuf::from("."), &root)?;
|
2019-03-08 15:55:54 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
panic!("not implemented");
|
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")?;
|
2018-12-30 17:01:20 +00:00
|
|
|
let mut file = std::fs::File::open(archive)?;
|
|
|
|
|
|
|
|
println!("CATAR {}", archive);
|
|
|
|
|
|
|
|
let mut buffer = [0u8; 16];
|
|
|
|
|
|
|
|
let mut nesting = 0;
|
|
|
|
|
|
|
|
loop {
|
|
|
|
file.read_exact(&mut buffer)?;
|
|
|
|
|
|
|
|
let header = map_struct::<CaFormatHeader>(&mut buffer)?;
|
|
|
|
|
|
|
|
println!("Type: {:016x}", header.htype);
|
|
|
|
println!("Size: {}", header.size);
|
|
|
|
|
|
|
|
let mut rest = vec![0u8; (header.size as usize) - 16];
|
|
|
|
file.read_exact(&mut rest)?;
|
|
|
|
|
|
|
|
if header.htype == CA_FORMAT_FILENAME {
|
|
|
|
let name = read_os_string(&rest);
|
|
|
|
let hash = compute_goodbye_hash(&rest[..rest.len()-1]);
|
|
|
|
println!("Name: {:?} {:016x}", name, hash);
|
|
|
|
}
|
|
|
|
|
|
|
|
if header.htype == CA_FORMAT_ENTRY {
|
|
|
|
let entry = map_struct::<CaFormatEntry>(&mut rest)?;
|
|
|
|
println!("Mode: {:08x} {:08x}", entry.mode, (entry.mode as u32) & libc::S_IFDIR);
|
|
|
|
if ((entry.mode as u32) & libc::S_IFMT) == libc::S_IFDIR {
|
|
|
|
nesting += 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if header.htype == CA_FORMAT_GOODBYE {
|
|
|
|
nesting -= 1;
|
|
|
|
print_goodby_entries(&rest)?;
|
|
|
|
if nesting == 0 { break; }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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-01-07 12:25:41 +00:00
|
|
|
|
|
|
|
let source = std::path::PathBuf::from(source);
|
|
|
|
|
|
|
|
let mut dir = nix::dir::Dir::open(
|
|
|
|
&source, nix::fcntl::OFlag::O_NOFOLLOW, nix::sys::stat::Mode::empty())?;
|
|
|
|
|
|
|
|
let file = std::fs::OpenOptions::new()
|
|
|
|
.create_new(true)
|
|
|
|
.write(true)
|
|
|
|
.open(archive)?;
|
|
|
|
|
|
|
|
let mut writer = std::io::BufWriter::with_capacity(1024*1024, file);
|
|
|
|
|
2019-03-08 08:33:53 +00:00
|
|
|
CaTarEncoder::encode(source, &mut dir, &mut writer, all_file_systems, verbose)?;
|
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,
|
2018-12-30 17:01:20 +00:00
|
|
|
ObjectSchema::new("Create new catar archive.")
|
|
|
|
.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-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-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
|
|
|
}
|