]> arthur.barton.de Git - bup.git/blob - cmd/xstat-cmd.py
Merge remote branch 'origin/master' into meta
[bup.git] / cmd / xstat-cmd.py
1 #!/usr/bin/env python
2
3 # Copyright (C) 2010 Rob Browning
4 #
5 # This code is covered under the terms of the GNU Library General
6 # Public License as described in the bup LICENSE file.
7
8 import errno
9 import posix1e
10 import stat
11 import sys
12 from bup import metadata
13 from bup import options
14 from bup import xstat
15 from bup.helpers import handle_ctrl_c, saved_errors, add_error, log
16
17
18 def fstimestr(fstime):
19     (s, ns) = fstime.secs_nsecs()
20     if ns == 0:
21         return '%d' % s
22     else:
23         return '%d.%09d' % (s, ns)
24
25
26 optspec = """
27 bup pathinfo [OPTION ...] <PATH ...>
28 --
29 v,verbose       increase log output (can be used more than once)
30 q,quiet         don't show progress meter
31 exclude-fields= exclude comma-separated fields
32 include-fields= include comma-separated fields (definitive if first)
33 """
34
35 target_filename = ''
36 all_fields = frozenset(['path',
37                         'mode',
38                         'link-target',
39                         'rdev',
40                         'uid',
41                         'gid',
42                         'owner',
43                         'group',
44                         'atime',
45                         'mtime',
46                         'ctime',
47                         'linux-attr',
48                         'linux-xattr',
49                         'posix1e-acl'])
50 active_fields = all_fields
51
52 handle_ctrl_c()
53
54 o = options.Options(optspec)
55 (opt, flags, remainder) = o.parse(sys.argv[1:])
56
57 treat_include_fields_as_definitive = True
58 for flag, value in flags:
59     if flag == '--verbose' or flag == '-v':
60         metadata.verbose += 1
61     elif flag == '--quiet' or flag == '-q':
62         metadata.verbose = 0
63     elif flag == '--exclude-fields':
64         exclude_fields = frozenset(value.split(','))
65         for f in exclude_fields:
66             if not f in all_fields:
67                 o.fatal(f + ' is not a valid field name')
68         active_fields = active_fields - exclude_fields
69         treat_include_fields_as_definitive = False
70     elif flag == '--include-fields':
71         include_fields = frozenset(value.split(','))
72         for f in include_fields:
73             if not f in all_fields:
74                 o.fatal(f + ' is not a valid field name')
75         if treat_include_fields_as_definitive:
76             active_fields = include_fields
77             treat_include_fields_as_definitive = False
78         else:
79             active_fields = active_fields | include_fields
80
81 for path in remainder:
82     try:
83         m = metadata.from_path(path, archive_path = path)
84     except IOError, e:
85         if e.errno == errno.ENOENT:
86             add_error(e)
87             continue
88         else:
89             raise
90     if 'path' in active_fields:
91         print 'path:', m.path
92     if 'mode' in active_fields:
93         print 'mode:', oct(m.mode)
94     if 'link-target' in active_fields and stat.S_ISLNK(m.mode):
95         print 'link-target:', m.symlink_target
96     if 'rdev' in active_fields:
97         print 'rdev:', m.rdev
98     if 'uid' in active_fields:
99         print 'uid:', m.uid
100     if 'gid' in active_fields:
101         print 'gid:', m.gid
102     if 'owner' in active_fields:
103         print 'owner:', m.owner
104     if 'group' in active_fields:
105         print 'group:', m.group
106     if 'atime' in active_fields:
107         print 'atime: ' + fstimestr(m.atime)
108     if 'mtime' in active_fields:
109         print 'mtime: ' + fstimestr(m.mtime)
110     if 'ctime' in active_fields:
111         print 'ctime: ' + fstimestr(m.ctime)
112     if 'linux-attr' in active_fields and m.linux_attr:
113         print 'linux-attr:', hex(m.linux_attr)
114     if 'linux-xattr' in active_fields and m.linux_xattr:
115         for name, value in m.linux_xattr:
116             print 'linux-xattr: %s -> %s' % (name, repr(value))
117     if 'posix1e-acl' in active_fields and m.posix1e_acl:
118         flags = posix1e.TEXT_ABBREVIATE
119         if stat.S_ISDIR(m.mode):
120             acl = m.posix1e_acl[0]
121             default_acl = m.posix1e_acl[2]
122             print acl.to_any_text('posix1e-acl: ', '\n', flags)
123             print acl.to_any_text('posix1e-acl-default: ', '\n', flags)
124         else:
125             acl = m.posix1e_acl[0]
126             print acl.to_any_text('posix1e-acl: ', '\n', flags)
127
128 if saved_errors:
129     log('WARNING: %d errors encountered.\n' % len(saved_errors))
130     sys.exit(1)
131 else:
132     sys.exit(0)