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