1 # Copyright (C) 2010 Rob Browning
3 # This code is covered under the terms of the GNU Library General
4 # Public License as described in the bup LICENSE file.
6 from __future__ import absolute_import, print_function
8 import errno, stat, sys
10 from bup import compat, metadata, options, xstat
11 from bup.compat import argv_bytes
12 from bup.helpers import add_error, handle_ctrl_c, parse_timestamp, saved_errors, \
14 from bup.io import byte_stream
17 def parse_timestamp_arg(field, value):
18 res = str(value) # Undo autoconversion.
20 res = parse_timestamp(res)
21 except ValueError as ex:
23 o.fatal('unable to parse %s resolution "%s" (%s)'
26 o.fatal('unable to parse %s resolution "%s"' % (field, value))
28 if res != 1 and res % 10:
29 o.fatal('%s resolution "%s" must be a power of 10' % (field, value))
34 bup xstat pathinfo [OPTION ...] <PATH ...>
36 v,verbose increase log output (can be used more than once)
37 q,quiet don't show progress meter
38 exclude-fields= exclude comma-separated fields
39 include-fields= include comma-separated fields (definitive if first)
40 atime-resolution= limit s, ms, us, ns, 10ns (value must be a power of 10) [ns]
41 mtime-resolution= limit s, ms, us, ns, 10ns (value must be a power of 10) [ns]
42 ctime-resolution= limit s, ms, us, ns, 10ns (value must be a power of 10) [ns]
48 active_fields = metadata.all_fields
50 o = options.Options(optspec)
51 (opt, flags, remainder) = o.parse_bytes(argv[1:])
53 atime_resolution = parse_timestamp_arg('atime', opt.atime_resolution)
54 mtime_resolution = parse_timestamp_arg('mtime', opt.mtime_resolution)
55 ctime_resolution = parse_timestamp_arg('ctime', opt.ctime_resolution)
57 treat_include_fields_as_definitive = True
58 for flag, value in flags:
59 if flag == '--exclude-fields':
60 exclude_fields = frozenset(value.split(','))
61 for f in exclude_fields:
62 if not f in metadata.all_fields:
63 o.fatal(f + ' is not a valid field name')
64 active_fields = active_fields - exclude_fields
65 treat_include_fields_as_definitive = False
66 elif flag == '--include-fields':
67 include_fields = frozenset(value.split(','))
68 for f in include_fields:
69 if not f in metadata.all_fields:
70 o.fatal(f + ' is not a valid field name')
71 if treat_include_fields_as_definitive:
72 active_fields = include_fields
73 treat_include_fields_as_definitive = False
75 active_fields = active_fields | include_fields
77 opt.verbose = opt.verbose or 0
78 opt.quiet = opt.quiet or 0
79 metadata.verbose = opt.verbose - opt.quiet
82 out = byte_stream(sys.stdout)
85 for path in remainder:
86 path = argv_bytes(path)
88 m = metadata.from_path(path, archive_path = path)
89 except (OSError,IOError) as e:
90 if e.errno == errno.ENOENT:
95 if metadata.verbose >= 0:
98 if atime_resolution != 1:
99 m.atime = (m.atime / atime_resolution) * atime_resolution
100 if mtime_resolution != 1:
101 m.mtime = (m.mtime / mtime_resolution) * mtime_resolution
102 if ctime_resolution != 1:
103 m.ctime = (m.ctime / ctime_resolution) * ctime_resolution
104 out.write(metadata.detailed_bytes(m, active_fields))
109 log('WARNING: %d errors encountered.\n' % len(saved_errors))