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