]> git.proxmox.com Git - proxmox-backup.git/blob - src/bin/pxar.rs
bcbce4ff2278ecafbe987132a20246f4558d7a83
[proxmox-backup.git] / src / bin / pxar.rs
1 extern crate proxmox_backup;
2
3 use failure::*;
4
5 use proxmox::{sortable, identity};
6 use proxmox::api::{ApiHandler, ApiMethod, RpcEnvironment};
7 use proxmox::api::schema::*;
8 use proxmox::api::cli::*;
9
10 use proxmox_backup::tools;
11
12 use serde_json::{Value};
13
14 use std::io::Write;
15 use std::path::{Path, PathBuf};
16 use std::fs::OpenOptions;
17 use std::ffi::OsStr;
18 use std::os::unix::fs::OpenOptionsExt;
19 use std::os::unix::io::AsRawFd;
20 use std::collections::HashSet;
21
22 use proxmox_backup::pxar;
23
24 fn dump_archive_from_reader<R: std::io::Read>(
25 reader: &mut R,
26 feature_flags: u64,
27 verbose: bool,
28 ) -> Result<(), Error> {
29 let mut decoder = pxar::SequentialDecoder::new(reader, feature_flags);
30
31 let stdout = std::io::stdout();
32 let mut out = stdout.lock();
33
34 let mut path = PathBuf::new();
35 decoder.dump_entry(&mut path, verbose, &mut out)?;
36
37 Ok(())
38 }
39
40 fn dump_archive(
41 param: Value,
42 _info: &ApiMethod,
43 _rpcenv: &mut dyn RpcEnvironment,
44 ) -> Result<Value, Error> {
45
46 let archive = tools::required_string_param(&param, "archive")?;
47 let verbose = param["verbose"].as_bool().unwrap_or(false);
48
49 let feature_flags = pxar::flags::DEFAULT;
50
51 if archive == "-" {
52 let stdin = std::io::stdin();
53 let mut reader = stdin.lock();
54 dump_archive_from_reader(&mut reader, feature_flags, verbose)?;
55 } else {
56 if verbose { println!("PXAR dump: {}", archive); }
57 let file = std::fs::File::open(archive)?;
58 let mut reader = std::io::BufReader::new(file);
59 dump_archive_from_reader(&mut reader, feature_flags, verbose)?;
60 }
61
62 Ok(Value::Null)
63 }
64
65 fn extract_archive_from_reader<R: std::io::Read>(
66 reader: &mut R,
67 target: &str,
68 feature_flags: u64,
69 allow_existing_dirs: bool,
70 verbose: bool,
71 pattern: Option<Vec<pxar::MatchPattern>>
72 ) -> Result<(), Error> {
73 let mut decoder = pxar::SequentialDecoder::new(reader, feature_flags);
74 decoder.set_callback(move |path| {
75 if verbose {
76 println!("{:?}", path);
77 }
78 Ok(())
79 });
80 decoder.set_allow_existing_dirs(allow_existing_dirs);
81
82 let pattern = pattern.unwrap_or_else(Vec::new);
83 decoder.restore(Path::new(target), &pattern)?;
84
85 Ok(())
86 }
87
88 fn extract_archive(
89 param: Value,
90 _info: &ApiMethod,
91 _rpcenv: &mut dyn RpcEnvironment,
92 ) -> Result<Value, Error> {
93
94 let archive = tools::required_string_param(&param, "archive")?;
95 let target = param["target"].as_str().unwrap_or(".");
96 let verbose = param["verbose"].as_bool().unwrap_or(false);
97 let no_xattrs = param["no-xattrs"].as_bool().unwrap_or(false);
98 let no_fcaps = param["no-fcaps"].as_bool().unwrap_or(false);
99 let no_acls = param["no-acls"].as_bool().unwrap_or(false);
100 let no_device_nodes = param["no-device-nodes"].as_bool().unwrap_or(false);
101 let no_fifos = param["no-fifos"].as_bool().unwrap_or(false);
102 let no_sockets = param["no-sockets"].as_bool().unwrap_or(false);
103 let allow_existing_dirs = param["allow-existing-dirs"].as_bool().unwrap_or(false);
104 let files_from = param["files-from"].as_str();
105 let empty = Vec::new();
106 let arg_pattern = param["pattern"].as_array().unwrap_or(&empty);
107
108 let mut feature_flags = pxar::flags::DEFAULT;
109 if no_xattrs {
110 feature_flags ^= pxar::flags::WITH_XATTRS;
111 }
112 if no_fcaps {
113 feature_flags ^= pxar::flags::WITH_FCAPS;
114 }
115 if no_acls {
116 feature_flags ^= pxar::flags::WITH_ACL;
117 }
118 if no_device_nodes {
119 feature_flags ^= pxar::flags::WITH_DEVICE_NODES;
120 }
121 if no_fifos {
122 feature_flags ^= pxar::flags::WITH_FIFOS;
123 }
124 if no_sockets {
125 feature_flags ^= pxar::flags::WITH_SOCKETS;
126 }
127
128 let mut pattern_list = Vec::new();
129 if let Some(filename) = files_from {
130 let dir = nix::dir::Dir::open("./", nix::fcntl::OFlag::O_RDONLY, nix::sys::stat::Mode::empty())?;
131 if let Some((mut pattern, _, _)) = pxar::MatchPattern::from_file(dir.as_raw_fd(), filename)? {
132 pattern_list.append(&mut pattern);
133 }
134 }
135
136 for s in arg_pattern {
137 let l = s.as_str().ok_or_else(|| format_err!("Invalid pattern string slice"))?;
138 let p = pxar::MatchPattern::from_line(l.as_bytes())?
139 .ok_or_else(|| format_err!("Invalid match pattern in arguments"))?;
140 pattern_list.push(p);
141 }
142
143 let pattern = if pattern_list.is_empty() {
144 None
145 } else {
146 Some(pattern_list)
147 };
148
149 if archive == "-" {
150 let stdin = std::io::stdin();
151 let mut reader = stdin.lock();
152 extract_archive_from_reader(&mut reader, target, feature_flags, allow_existing_dirs, verbose, pattern)?;
153 } else {
154 if verbose { println!("PXAR extract: {}", archive); }
155 let file = std::fs::File::open(archive)?;
156 let mut reader = std::io::BufReader::new(file);
157 extract_archive_from_reader(&mut reader, target, feature_flags, allow_existing_dirs, verbose, pattern)?;
158 }
159
160 Ok(Value::Null)
161 }
162
163 fn create_archive(
164 param: Value,
165 _info: &ApiMethod,
166 _rpcenv: &mut dyn RpcEnvironment,
167 ) -> Result<Value, Error> {
168
169 let archive = tools::required_string_param(&param, "archive")?;
170 let source = tools::required_string_param(&param, "source")?;
171 let verbose = param["verbose"].as_bool().unwrap_or(false);
172 let all_file_systems = param["all-file-systems"].as_bool().unwrap_or(false);
173 let no_xattrs = param["no-xattrs"].as_bool().unwrap_or(false);
174 let no_fcaps = param["no-fcaps"].as_bool().unwrap_or(false);
175 let no_acls = param["no-acls"].as_bool().unwrap_or(false);
176 let no_device_nodes = param["no-device-nodes"].as_bool().unwrap_or(false);
177 let no_fifos = param["no-fifos"].as_bool().unwrap_or(false);
178 let no_sockets = param["no-sockets"].as_bool().unwrap_or(false);
179 let empty = Vec::new();
180 let exclude_pattern = param["exclude"].as_array().unwrap_or(&empty);
181 let entries_max = param["entries-max"].as_u64().unwrap_or(pxar::ENCODER_MAX_ENTRIES as u64);
182
183 let devices = if all_file_systems { None } else { Some(HashSet::new()) };
184
185 let source = PathBuf::from(source);
186
187 let mut dir = nix::dir::Dir::open(
188 &source, nix::fcntl::OFlag::O_NOFOLLOW, nix::sys::stat::Mode::empty())?;
189
190 let file = OpenOptions::new()
191 .create_new(true)
192 .write(true)
193 .mode(0o640)
194 .open(archive)?;
195
196 let mut writer = std::io::BufWriter::with_capacity(1024*1024, file);
197 let mut feature_flags = pxar::flags::DEFAULT;
198 if no_xattrs {
199 feature_flags ^= pxar::flags::WITH_XATTRS;
200 }
201 if no_fcaps {
202 feature_flags ^= pxar::flags::WITH_FCAPS;
203 }
204 if no_acls {
205 feature_flags ^= pxar::flags::WITH_ACL;
206 }
207 if no_device_nodes {
208 feature_flags ^= pxar::flags::WITH_DEVICE_NODES;
209 }
210 if no_fifos {
211 feature_flags ^= pxar::flags::WITH_FIFOS;
212 }
213 if no_sockets {
214 feature_flags ^= pxar::flags::WITH_SOCKETS;
215 }
216
217 let mut pattern_list = Vec::new();
218 for s in exclude_pattern {
219 let l = s.as_str().ok_or_else(|| format_err!("Invalid pattern string slice"))?;
220 let p = pxar::MatchPattern::from_line(l.as_bytes())?
221 .ok_or_else(|| format_err!("Invalid match pattern in arguments"))?;
222 pattern_list.push(p);
223 }
224
225 let catalog = None::<&mut pxar::catalog::DummyCatalogWriter>;
226 pxar::Encoder::encode(
227 source,
228 &mut dir,
229 &mut writer,
230 catalog,
231 devices,
232 verbose,
233 false,
234 feature_flags,
235 pattern_list,
236 entries_max as usize,
237 )?;
238
239 writer.flush()?;
240
241 Ok(Value::Null)
242 }
243
244 /// Mount the archive to the provided mountpoint via FUSE.
245 fn mount_archive(
246 param: Value,
247 _info: &ApiMethod,
248 _rpcenv: &mut dyn RpcEnvironment,
249 ) -> Result<Value, Error> {
250 let archive = tools::required_string_param(&param, "archive")?;
251 let mountpoint = tools::required_string_param(&param, "mountpoint")?;
252 let verbose = param["verbose"].as_bool().unwrap_or(false);
253 let no_mt = param["no-mt"].as_bool().unwrap_or(false);
254
255 let archive = Path::new(archive);
256 let mountpoint = Path::new(mountpoint);
257 let options = OsStr::new("ro,default_permissions");
258 let mut session = pxar::fuse::Session::from_path(&archive, &options, verbose)
259 .map_err(|err| format_err!("pxar mount failed: {}", err))?;
260 // Mount the session and deamonize if verbose is not set
261 session.mount(&mountpoint, !verbose)?;
262 session.run_loop(!no_mt)?;
263
264 Ok(Value::Null)
265 }
266
267 #[sortable]
268 const API_METHOD_CREATE_ARCHIVE: ApiMethod = ApiMethod::new(
269 &ApiHandler::Sync(&create_archive),
270 &ObjectSchema::new(
271 "Create new .pxar archive.",
272 &sorted!([
273 (
274 "archive",
275 false,
276 &StringSchema::new("Archive name").schema()
277 ),
278 (
279 "source",
280 false,
281 &StringSchema::new("Source directory.").schema()
282 ),
283 (
284 "verbose",
285 true,
286 &BooleanSchema::new("Verbose output.")
287 .default(false)
288 .schema()
289 ),
290 (
291 "no-xattrs",
292 true,
293 &BooleanSchema::new("Ignore extended file attributes.")
294 .default(false)
295 .schema()
296 ),
297 (
298 "no-fcaps",
299 true,
300 &BooleanSchema::new("Ignore file capabilities.")
301 .default(false)
302 .schema()
303 ),
304 (
305 "no-acls",
306 true,
307 &BooleanSchema::new("Ignore access control list entries.")
308 .default(false)
309 .schema()
310 ),
311 (
312 "all-file-systems",
313 true,
314 &BooleanSchema::new("Include mounted sudirs.")
315 .default(false)
316 .schema()
317 ),
318 (
319 "no-device-nodes",
320 true,
321 &BooleanSchema::new("Ignore device nodes.")
322 .default(false)
323 .schema()
324 ),
325 (
326 "no-fifos",
327 true,
328 &BooleanSchema::new("Ignore fifos.")
329 .default(false)
330 .schema()
331 ),
332 (
333 "no-sockets",
334 true,
335 &BooleanSchema::new("Ignore sockets.")
336 .default(false)
337 .schema()
338 ),
339 (
340 "exclude",
341 true,
342 &ArraySchema::new(
343 "List of paths or pattern matching files to exclude.",
344 &StringSchema::new("Path or pattern matching files to restore.").schema()
345 ).schema()
346 ),
347 (
348 "entries-max",
349 true,
350 &IntegerSchema::new("Max number of entries loaded at once into memory")
351 .default(pxar::ENCODER_MAX_ENTRIES as isize)
352 .minimum(0)
353 .maximum(std::isize::MAX)
354 .schema()
355 ),
356 ]),
357 )
358 );
359
360 #[sortable]
361 const API_METHOD_EXTRACT_ARCHIVE: ApiMethod = ApiMethod::new(
362 &ApiHandler::Sync(&extract_archive),
363 &ObjectSchema::new(
364 "Extract an archive.",
365 &sorted!([
366 (
367 "archive",
368 false,
369 &StringSchema::new("Archive name.").schema()
370 ),
371 (
372 "pattern",
373 true,
374 &ArraySchema::new(
375 "List of paths or pattern matching files to restore",
376 &StringSchema::new("Path or pattern matching files to restore.").schema()
377 ).schema()
378 ),
379 (
380 "target",
381 true,
382 &StringSchema::new("Target directory.").schema()
383 ),
384 (
385 "verbose",
386 true,
387 &BooleanSchema::new("Verbose output.")
388 .default(false)
389 .schema()
390 ),
391 (
392 "no-xattrs",
393 true,
394 &BooleanSchema::new("Ignore extended file attributes.")
395 .default(false)
396 .schema()
397 ),
398 (
399 "no-fcaps",
400 true,
401 &BooleanSchema::new("Ignore file capabilities.")
402 .default(false)
403 .schema()
404 ),
405 (
406 "no-acls",
407 true,
408 &BooleanSchema::new("Ignore access control list entries.")
409 .default(false)
410 .schema()
411 ),
412 (
413 "allow-existing-dirs",
414 true,
415 &BooleanSchema::new("Allows directories to already exist on restore.")
416 .default(false)
417 .schema()
418 ),
419 (
420 "files-from",
421 true,
422 &StringSchema::new("Match pattern for files to restore.").schema()
423 ),
424 (
425 "no-device-nodes",
426 true,
427 &BooleanSchema::new("Ignore device nodes.")
428 .default(false)
429 .schema()
430 ),
431 (
432 "no-fifos",
433 true,
434 &BooleanSchema::new("Ignore fifos.")
435 .default(false)
436 .schema()
437 ),
438 (
439 "no-sockets",
440 true,
441 &BooleanSchema::new("Ignore sockets.")
442 .default(false)
443 .schema()
444 ),
445 ]),
446 )
447 );
448
449 #[sortable]
450 const API_METHOD_MOUNT_ARCHIVE: ApiMethod = ApiMethod::new(
451 &ApiHandler::Sync(&mount_archive),
452 &ObjectSchema::new(
453 "Mount the archive as filesystem via FUSE.",
454 &sorted!([
455 (
456 "archive",
457 false,
458 &StringSchema::new("Archive name.").schema()
459 ),
460 (
461 "mountpoint",
462 false,
463 &StringSchema::new("Mountpoint for the filesystem root.").schema()
464 ),
465 (
466 "verbose",
467 true,
468 &BooleanSchema::new("Verbose output, keeps process running in foreground (for debugging).")
469 .default(false)
470 .schema()
471 ),
472 (
473 "no-mt",
474 true,
475 &BooleanSchema::new("Run in single threaded mode (for debugging).")
476 .default(false)
477 .schema()
478 ),
479 ]),
480 )
481 );
482
483 #[sortable]
484 const API_METHOD_DUMP_ARCHIVE: ApiMethod = ApiMethod::new(
485 &ApiHandler::Sync(&dump_archive),
486 &ObjectSchema::new(
487 "List the contents of an archive.",
488 &sorted!([
489 ( "archive", false, &StringSchema::new("Archive name.").schema()),
490 ( "verbose", true, &BooleanSchema::new("Verbose output.")
491 .default(false)
492 .schema()
493 ),
494 ])
495 )
496 );
497
498 fn main() {
499
500 let cmd_def = CliCommandMap::new()
501 .insert("create", CliCommand::new(&API_METHOD_CREATE_ARCHIVE)
502 .arg_param(&["archive", "source"])
503 .completion_cb("archive", tools::complete_file_name)
504 .completion_cb("source", tools::complete_file_name)
505 )
506 .insert("extract", CliCommand::new(&API_METHOD_EXTRACT_ARCHIVE)
507 .arg_param(&["archive", "target"])
508 .completion_cb("archive", tools::complete_file_name)
509 .completion_cb("target", tools::complete_file_name)
510 .completion_cb("files-from", tools::complete_file_name)
511 )
512 .insert("mount", CliCommand::new(&API_METHOD_MOUNT_ARCHIVE)
513 .arg_param(&["archive", "mountpoint"])
514 .completion_cb("archive", tools::complete_file_name)
515 .completion_cb("mountpoint", tools::complete_file_name)
516 )
517 .insert("list", CliCommand::new(&API_METHOD_DUMP_ARCHIVE)
518 .arg_param(&["archive"])
519 .completion_cb("archive", tools::complete_file_name)
520 );
521
522 run_cli_command(cmd_def, None);
523 }