]> arthur.barton.de Git - bup.git/blobdiff - cmd/xstat-cmd.py
test-restore-map-owner: accommodate python 3 and test there
[bup.git] / cmd / xstat-cmd.py
index c9e3310a1b4ba198fdcba669d1b61bb798c31f59..626b4c7e9d2f82476346c01184c21aab764457f8 100755 (executable)
@@ -1,66 +1,78 @@
-#!/usr/bin/env python
+#!/bin/sh
+"""": # -*-python-*-
+bup_python="$(dirname "$0")/bup-python" || exit $?
+exec "$bup_python" "$0" ${1+"$@"}
+"""
+# end of bup preamble
 
 # Copyright (C) 2010 Rob Browning
 #
 # This code is covered under the terms of the GNU Library General
 # Public License as described in the bup LICENSE file.
 
-import posix1e
-import stat
-import sys
-from bup import metadata
-from bup import options
-from bup import xstat
-from bup.helpers import handle_ctrl_c, saved_errors
+from __future__ import absolute_import, print_function
+import sys, stat, errno
+
+from bup import metadata, options, xstat
+from bup.compat import argv_bytes
+from bup.helpers import add_error, handle_ctrl_c, parse_timestamp, saved_errors, \
+    add_error, log
+from bup.io import byte_stream
+
+
+def parse_timestamp_arg(field, value):
+    res = str(value) # Undo autoconversion.
+    try:
+        res = parse_timestamp(res)
+    except ValueError as ex:
+        if ex.args:
+            o.fatal('unable to parse %s resolution "%s" (%s)'
+                    % (field, value, ex))
+        else:
+            o.fatal('unable to parse %s resolution "%s"' % (field, value))
+
+    if res != 1 and res % 10:
+        o.fatal('%s resolution "%s" must be a power of 10' % (field, value))
+    return res
+
 
 optspec = """
-bup pathinfo [OPTION ...] <PATH ...>
+bup xstat pathinfo [OPTION ...] <PATH ...>
 --
 v,verbose       increase log output (can be used more than once)
 q,quiet         don't show progress meter
 exclude-fields= exclude comma-separated fields
 include-fields= include comma-separated fields (definitive if first)
+atime-resolution=  limit s, ms, us, ns, 10ns (value must be a power of 10) [ns]
+mtime-resolution=  limit s, ms, us, ns, 10ns (value must be a power of 10) [ns]
+ctime-resolution=  limit s, ms, us, ns, 10ns (value must be a power of 10) [ns]
 """
 
-target_filename = ''
-all_fields = frozenset(['path',
-                        'mode',
-                        'link-target',
-                        'rdev',
-                        'uid',
-                        'gid',
-                        'owner',
-                        'group',
-                        'atime',
-                        'mtime',
-                        'ctime',
-                        'linux-attr',
-                        'linux-xattr',
-                        'posix1e-acl'])
-active_fields = all_fields
+target_filename = b''
+active_fields = metadata.all_fields
 
 handle_ctrl_c()
 
-o = options.Options('bup pathinfo', optspec)
+o = options.Options(optspec)
 (opt, flags, remainder) = o.parse(sys.argv[1:])
 
+atime_resolution = parse_timestamp_arg('atime', opt.atime_resolution)
+mtime_resolution = parse_timestamp_arg('mtime', opt.mtime_resolution)
+ctime_resolution = parse_timestamp_arg('ctime', opt.ctime_resolution)
+
 treat_include_fields_as_definitive = True
 for flag, value in flags:
-    if flag == '--verbose' or flag == '-v':
-        metadata.verbose += 1
-    elif flag == '--quiet' or flag == '-q':
-        metadata.verbose = 0
-    elif flag == '--exclude-fields':
+    if flag == '--exclude-fields':
         exclude_fields = frozenset(value.split(','))
         for f in exclude_fields:
-            if not f in all_fields:
+            if not f in metadata.all_fields:
                 o.fatal(f + ' is not a valid field name')
         active_fields = active_fields - exclude_fields
         treat_include_fields_as_definitive = False
     elif flag == '--include-fields':
         include_fields = frozenset(value.split(','))
         for f in include_fields:
-            if not f in all_fields:
+            if not f in metadata.all_fields:
                 o.fatal(f + ' is not a valid field name')
         if treat_include_fields_as_definitive:
             active_fields = include_fields
@@ -68,45 +80,36 @@ for flag, value in flags:
         else:
             active_fields = active_fields | include_fields
 
+opt.verbose = opt.verbose or 0
+opt.quiet = opt.quiet or 0
+metadata.verbose = opt.verbose - opt.quiet
+
+sys.stdout.flush()
+out = byte_stream(sys.stdout)
+
+first_path = True
 for path in remainder:
-    m = metadata.from_path(path, archive_path = path)
-    if 'path' in active_fields:
-        print 'path:', m.path
-    if 'mode' in active_fields:
-        print 'mode:', hex(m.mode)
-    if 'link-target' in active_fields and stat.S_ISLNK(m.mode):
-        print 'link-target:', m.symlink_target
-    if 'rdev' in active_fields:
-        print 'rdev:', m.rdev
-    if 'uid' in active_fields:
-        print 'uid:', m.uid
-    if 'gid' in active_fields:
-        print 'gid:', m.gid
-    if 'owner' in active_fields:
-        print 'owner:', m.owner
-    if 'group' in active_fields:
-        print 'group:', m.group
-    if 'atime' in active_fields:
-        print 'atime: ' + '%d.%09d' % m.atime.secs_nsecs()
-    if 'mtime' in active_fields:
-        print 'mtime: ' + '%d.%09d' % m.mtime.secs_nsecs()
-    if 'ctime' in active_fields:
-        print 'ctime: ' + '%d.%09d' % m.ctime.secs_nsecs()
-    if 'linux-attr' in active_fields and m.linux_attr:
-        print 'linux-attr:', hex(m.linux_attr)
-    if 'linux-xattr' in active_fields and m.linux_xattr:
-        for name, value in m.linux_xattr:
-            print 'linux-xattr: %s -> %s' % (name, repr(value))
-    if 'posix1e-acl' in active_fields and m.posix1e_acl:
-        flags = posix1e.TEXT_ABBREVIATE
-        if stat.S_ISDIR(m.mode):
-            acl = m.posix1e_acl[0]
-            default_acl = m.posix1e_acl[2]
-            print acl.to_any_text('posix1e-acl: ', '\n', flags)
-            print acl.to_any_text('posix1e-acl-default: ', '\n', flags)
+    path = argv_bytes(path)
+    try:
+        m = metadata.from_path(path, archive_path = path)
+    except (OSError,IOError) as e:
+        if e.errno == errno.ENOENT:
+            add_error(e)
+            continue
         else:
-            acl = m.posix1e_acl[0]
-            print acl.to_any_text('posix1e-acl: ', '\n', flags)
+            raise
+    if metadata.verbose >= 0:
+        if not first_path:
+            out.write(b'\n')
+        if atime_resolution != 1:
+            m.atime = (m.atime / atime_resolution) * atime_resolution
+        if mtime_resolution != 1:
+            m.mtime = (m.mtime / mtime_resolution) * mtime_resolution
+        if ctime_resolution != 1:
+            m.ctime = (m.ctime / ctime_resolution) * ctime_resolution
+        out.write(metadata.detailed_bytes(m, active_fields))
+        out.write(b'\n')
+        first_path = False
 
 if saved_errors:
     log('WARNING: %d errors encountered.\n' % len(saved_errors))