]> arthur.barton.de Git - bup.git/blob - lib/cmd/xstat-cmd.py
297df7225e201ad7c1239af6dd80165d5ea11868
[bup.git] / lib / cmd / xstat-cmd.py
1 #!/bin/sh
2 """": # -*-python-*-
3 # https://sourceware.org/bugzilla/show_bug.cgi?id=26034
4 export "BUP_ARGV_0"="$0"
5 arg_i=1
6 for arg in "$@"; do
7     export "BUP_ARGV_${arg_i}"="$arg"
8     shift
9     arg_i=$((arg_i + 1))
10 done
11 # Here to end of preamble replaced during install
12 bup_python="$(dirname "$0")/bup-python" || exit $?
13 exec "$bup_python" "$0"
14 """
15 # end of bup preamble
16
17 # Copyright (C) 2010 Rob Browning
18 #
19 # This code is covered under the terms of the GNU Library General
20 # Public License as described in the bup LICENSE file.
21
22 from __future__ import absolute_import, print_function
23 import sys, stat, errno
24
25 from bup import compat, metadata, options, xstat
26 from bup.compat import argv_bytes
27 from bup.helpers import add_error, handle_ctrl_c, parse_timestamp, saved_errors, \
28     add_error, log
29 from bup.io import byte_stream
30
31
32 def parse_timestamp_arg(field, value):
33     res = str(value) # Undo autoconversion.
34     try:
35         res = parse_timestamp(res)
36     except ValueError as ex:
37         if ex.args:
38             o.fatal('unable to parse %s resolution "%s" (%s)'
39                     % (field, value, ex))
40         else:
41             o.fatal('unable to parse %s resolution "%s"' % (field, value))
42
43     if res != 1 and res % 10:
44         o.fatal('%s resolution "%s" must be a power of 10' % (field, value))
45     return res
46
47
48 optspec = """
49 bup xstat pathinfo [OPTION ...] <PATH ...>
50 --
51 v,verbose       increase log output (can be used more than once)
52 q,quiet         don't show progress meter
53 exclude-fields= exclude comma-separated fields
54 include-fields= include comma-separated fields (definitive if first)
55 atime-resolution=  limit s, ms, us, ns, 10ns (value must be a power of 10) [ns]
56 mtime-resolution=  limit s, ms, us, ns, 10ns (value must be a power of 10) [ns]
57 ctime-resolution=  limit s, ms, us, ns, 10ns (value must be a power of 10) [ns]
58 """
59
60 target_filename = b''
61 active_fields = metadata.all_fields
62
63 handle_ctrl_c()
64
65 o = options.Options(optspec)
66 (opt, flags, remainder) = o.parse(compat.argv[1:])
67
68 atime_resolution = parse_timestamp_arg('atime', opt.atime_resolution)
69 mtime_resolution = parse_timestamp_arg('mtime', opt.mtime_resolution)
70 ctime_resolution = parse_timestamp_arg('ctime', opt.ctime_resolution)
71
72 treat_include_fields_as_definitive = True
73 for flag, value in flags:
74     if flag == '--exclude-fields':
75         exclude_fields = frozenset(value.split(','))
76         for f in exclude_fields:
77             if not f in metadata.all_fields:
78                 o.fatal(f + ' is not a valid field name')
79         active_fields = active_fields - exclude_fields
80         treat_include_fields_as_definitive = False
81     elif flag == '--include-fields':
82         include_fields = frozenset(value.split(','))
83         for f in include_fields:
84             if not f in metadata.all_fields:
85                 o.fatal(f + ' is not a valid field name')
86         if treat_include_fields_as_definitive:
87             active_fields = include_fields
88             treat_include_fields_as_definitive = False
89         else:
90             active_fields = active_fields | include_fields
91
92 opt.verbose = opt.verbose or 0
93 opt.quiet = opt.quiet or 0
94 metadata.verbose = opt.verbose - opt.quiet
95
96 sys.stdout.flush()
97 out = byte_stream(sys.stdout)
98
99 first_path = True
100 for path in remainder:
101     path = argv_bytes(path)
102     try:
103         m = metadata.from_path(path, archive_path = path)
104     except (OSError,IOError) as e:
105         if e.errno == errno.ENOENT:
106             add_error(e)
107             continue
108         else:
109             raise
110     if metadata.verbose >= 0:
111         if not first_path:
112             out.write(b'\n')
113         if atime_resolution != 1:
114             m.atime = (m.atime / atime_resolution) * atime_resolution
115         if mtime_resolution != 1:
116             m.mtime = (m.mtime / mtime_resolution) * mtime_resolution
117         if ctime_resolution != 1:
118             m.ctime = (m.ctime / ctime_resolution) * ctime_resolution
119         out.write(metadata.detailed_bytes(m, active_fields))
120         out.write(b'\n')
121         first_path = False
122
123 if saved_errors:
124     log('WARNING: %d errors encountered.\n' % len(saved_errors))
125     sys.exit(1)
126 else:
127     sys.exit(0)