]> arthur.barton.de Git - bup.git/blob - t/hardlink-sets
test-restore-map-owner: accommodate python 3 and test there
[bup.git] / t / hardlink-sets
1 #!/bin/sh
2 """": # -*-python-*-
3 bup_python="$(dirname "$0")/../cmd/bup-python" || exit $?
4 exec "$bup_python" "$0" ${1+"$@"}
5 """
6 # end of bup preamble
7
8 from __future__ import absolute_import, print_function
9 import os, stat, sys
10
11 from bup.compat import argv_bytes
12 from bup.io import byte_stream
13
14
15 # Print the full paths of all the files in each hardlink set
16 # underneath one of the paths.  Separate sets with a blank line, sort
17 # the paths within each set, and sort the sets by their first path.
18
19 def usage():
20     print("Usage: hardlink-sets <paths ...>", file=sys.stderr)
21
22 if len(sys.argv) < 2:
23     usage()
24     sys.exit(1)
25
26 def on_walk_error(e):
27     raise e
28
29 sys.stdout.flush()
30 out = byte_stream(sys.stdout)
31
32 hardlink_set = {}
33
34 for p in (argv_bytes(x) for x in sys.argv[1:]):
35   for root, dirs, files in os.walk(p, onerror = on_walk_error):
36       for filename in files:
37           full_path = os.path.join(root, filename)
38           st = os.lstat(full_path)
39           if not stat.S_ISDIR(st.st_mode):
40               node = b'%d:%d' % (st.st_dev, st.st_ino)
41               link_paths = hardlink_set.get(node)
42               if link_paths:
43                   link_paths.append(full_path)
44               else:
45                   hardlink_set[node] = [full_path]
46
47 # Sort the link sets.
48 for node, link_paths in hardlink_set.items():
49     link_paths.sort()
50
51 first_set = True
52 for link_paths in sorted(hardlink_set.values(), key = lambda x : x[0]):
53     if len(link_paths) > 1:
54         if first_set:
55             first_set = False
56         else:
57             out.write(b'\n')
58         for p in sorted(link_paths):
59             out.write(p + b'\n')
60
61 sys.exit(0)