]> arthur.barton.de Git - bup.git/blob - cmd/xstat-cmd.py
Use oct() rather than hex() when printing mode from bup xstat.
[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 posix1e
9 import stat
10 import sys
11 from bup import metadata
12 from bup import options
13 from bup import xstat
14 from bup.helpers import handle_ctrl_c, saved_errors
15
16 optspec = """
17 bup pathinfo [OPTION ...] <PATH ...>
18 --
19 v,verbose       increase log output (can be used more than once)
20 q,quiet         don't show progress meter
21 exclude-fields= exclude comma-separated fields
22 include-fields= include comma-separated fields (definitive if first)
23 """
24
25 target_filename = ''
26 all_fields = frozenset(['path',
27                         'mode',
28                         'link-target',
29                         'rdev',
30                         'uid',
31                         'gid',
32                         'owner',
33                         'group',
34                         'atime',
35                         'mtime',
36                         'ctime',
37                         'linux-attr',
38                         'linux-xattr',
39                         'posix1e-acl'])
40 active_fields = all_fields
41
42 handle_ctrl_c()
43
44 o = options.Options('bup pathinfo', optspec)
45 (opt, flags, remainder) = o.parse(sys.argv[1:])
46
47 treat_include_fields_as_definitive = True
48 for flag, value in flags:
49     if flag == '--verbose' or flag == '-v':
50         metadata.verbose += 1
51     elif flag == '--quiet' or flag == '-q':
52         metadata.verbose = 0
53     elif flag == '--exclude-fields':
54         exclude_fields = frozenset(value.split(','))
55         for f in exclude_fields:
56             if not f in all_fields:
57                 o.fatal(f + ' is not a valid field name')
58         active_fields = active_fields - exclude_fields
59         treat_include_fields_as_definitive = False
60     elif flag == '--include-fields':
61         include_fields = frozenset(value.split(','))
62         for f in include_fields:
63             if not f in all_fields:
64                 o.fatal(f + ' is not a valid field name')
65         if treat_include_fields_as_definitive:
66             active_fields = include_fields
67             treat_include_fields_as_definitive = False
68         else:
69             active_fields = active_fields | include_fields
70
71 for path in remainder:
72     m = metadata.from_path(path, archive_path = path)
73     if 'path' in active_fields:
74         print 'path:', m.path
75     if 'mode' in active_fields:
76         print 'mode:', oct(m.mode)
77     if 'link-target' in active_fields and stat.S_ISLNK(m.mode):
78         print 'link-target:', m.symlink_target
79     if 'rdev' in active_fields:
80         print 'rdev:', m.rdev
81     if 'uid' in active_fields:
82         print 'uid:', m.uid
83     if 'gid' in active_fields:
84         print 'gid:', m.gid
85     if 'owner' in active_fields:
86         print 'owner:', m.owner
87     if 'group' in active_fields:
88         print 'group:', m.group
89     if 'atime' in active_fields:
90         print 'atime: ' + '%d.%09d' % m.atime.secs_nsecs()
91     if 'mtime' in active_fields:
92         print 'mtime: ' + '%d.%09d' % m.mtime.secs_nsecs()
93     if 'ctime' in active_fields:
94         print 'ctime: ' + '%d.%09d' % m.ctime.secs_nsecs()
95     if 'linux-attr' in active_fields and m.linux_attr:
96         print 'linux-attr:', hex(m.linux_attr)
97     if 'linux-xattr' in active_fields and m.linux_xattr:
98         for name, value in m.linux_xattr:
99             print 'linux-xattr: %s -> %s' % (name, repr(value))
100     if 'posix1e-acl' in active_fields and m.posix1e_acl:
101         flags = posix1e.TEXT_ABBREVIATE
102         if stat.S_ISDIR(m.mode):
103             acl = m.posix1e_acl[0]
104             default_acl = m.posix1e_acl[2]
105             print acl.to_any_text('posix1e-acl: ', '\n', flags)
106             print acl.to_any_text('posix1e-acl-default: ', '\n', flags)
107         else:
108             acl = m.posix1e_acl[0]
109             print acl.to_any_text('posix1e-acl: ', '\n', flags)
110
111 if saved_errors:
112     log('WARNING: %d errors encountered.\n' % len(saved_errors))
113     sys.exit(1)
114 else:
115     sys.exit(0)