]> arthur.barton.de Git - bup.git/blob - t/hardlink-sets
9dffe1410060be110e55a11a77b122ad190863bc
[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 import os, stat, sys
9
10 # Print the full paths of all the files in each hardlink set
11 # underneath one of the paths.  Separate sets with a blank line, sort
12 # the paths within each set, and sort the sets by their first path.
13
14 def usage():
15     print >> sys.stderr, "Usage: hardlink-sets <paths ...>"
16
17 if len(sys.argv) < 2:
18     usage()
19     sys.exit(1)
20
21 def on_walk_error(e):
22     raise e
23
24 hardlink_set = {}
25
26 for p in sys.argv[1:]:
27   for root, dirs, files in os.walk(p, onerror = on_walk_error):
28       for filename in files:
29           full_path = os.path.join(root, filename)
30           st = os.lstat(full_path)
31           if not stat.S_ISDIR(st.st_mode):
32               node = '%s:%s' % (st.st_dev, st.st_ino)
33               link_paths = hardlink_set.get(node)
34               if link_paths:
35                   link_paths.append(full_path)
36               else:
37                   hardlink_set[node] = [full_path]
38
39 # Sort the link sets.
40 for node, link_paths in hardlink_set.items():
41     link_paths.sort()
42
43 first_set = True
44 for link_paths in sorted(hardlink_set.values(), key = lambda x : x[0]):
45     if len(link_paths) > 1:
46         if first_set:
47             first_set = False
48         else:
49             print
50         for p in sorted(link_paths):
51             print p
52
53 sys.exit(0)