]> arthur.barton.de Git - bup.git/blob - t/hardlink-sets
ftp: fix ls arguments for python3
[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")/../dev/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 sys.path[:0] = [os.path.dirname(os.path.realpath(__file__)) + '/../lib']
20
21 from bup import compat
22 from bup.io import byte_stream
23
24
25 # Print the full paths of all the files in each hardlink set
26 # underneath one of the paths.  Separate sets with a blank line, sort
27 # the paths within each set, and sort the sets by their first path.
28
29 def usage():
30     print("Usage: hardlink-sets <paths ...>", file=sys.stderr)
31
32 if len(compat.argv) < 2:
33     usage()
34     sys.exit(1)
35
36 def on_walk_error(e):
37     raise e
38
39 sys.stdout.flush()
40 out = byte_stream(sys.stdout)
41
42 hardlink_set = {}
43
44 for p in compat.argvb[1:]:
45   for root, dirs, files in os.walk(p, onerror = on_walk_error):
46       for filename in files:
47           full_path = os.path.join(root, filename)
48           st = os.lstat(full_path)
49           if not stat.S_ISDIR(st.st_mode):
50               node = b'%d:%d' % (st.st_dev, st.st_ino)
51               link_paths = hardlink_set.get(node)
52               if link_paths:
53                   link_paths.append(full_path)
54               else:
55                   hardlink_set[node] = [full_path]
56
57 # Sort the link sets.
58 for node, link_paths in hardlink_set.items():
59     link_paths.sort()
60
61 first_set = True
62 for link_paths in sorted(hardlink_set.values(), key = lambda x : x[0]):
63     if len(link_paths) > 1:
64         if first_set:
65             first_set = False
66         else:
67             out.write(b'\n')
68         for p in sorted(link_paths):
69             out.write(p + b'\n')
70
71 sys.exit(0)