-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathextents-difference
executable file
·97 lines (81 loc) · 2.13 KB
/
extents-difference
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#!/bin/bash
# SPDX-FileCopyrightText: 2016-2020 Graham R. Cobb
# SPDX-License-Identifier: GPL-3.0-or-later
#
# Copyright (C) 2016 Graham R. Cobb
# Released under GPL V3.0 -- see LICENSE
#
# extents-difference [-s] <filename>...
#
#
# Output the extents contained in the first named file, with the extents from the other files removed
# I.e. <file1> - ( Union of file2..fileN )
#
# Note: uses file descriptors 7 and 8
#
# Make sure errors are passed back
set -o pipefail
if [ "$1" = "-s" ]
then
shift
$0 "$@" | extents-size
exit $?
fi
if [ $# -lt 2 ]
then
echo "Usage: $0 <filename> <filename>..." >&2
exit 1
fi
copy_remainder() {
cat <&${IN1}-
}
diff2() {
# File descriptors
IN1=7
IN2=8
exec 7<$1 8<$2
read -u $IN1 start1 end1 restofline || return 0
if [ -z "$start1" -o -z "$end1" ] ; then return 0 ; fi
if ! read -u $IN2 start2 end2 restofline ; then copy_remainder ; return $? ; fi
if [ -z "$start2" -o -z "$end2" ] ; then copy_remainder ; return $? ; fi
while true
do
# echo "Outer loop: S1=$start1 E1=$end1 S2=$start2 E2=$end2"
while [ $start1 -gt $end2 ]
do
if ! read -u $IN2 start2 end2 restofline ; then copy_remainder ; return $?; fi
if [ -z "$start2" -o -z "$end2" ] ; then copy_remainder ; return $? ; fi
done
# Output any extents before extent2
# echo "Overlap?: S1=$start1 E1=$end1 S2=$start2 E2=$end2"
if [ $start1 -lt $start2 ]
then
if [ $end1 -lt $start2 ] ; then let end=end1 ; else let end=start2-1 ; fi
let length=end-start1+1
echo $start1 $end $length || return $?
fi
# Skip extents contained in extent2
let start1=end2+1
if [ $start1 -gt $end1 ]
then
# echo "Before read1: S1=$start1 E1=$end1 S2=$start2 E2=$end2"
read -u $IN1 start1 end1 restofline || return 0
if [ -z "$start1" -o -z "$end1" ] ; then return 0 ; fi
fi
done
}
if [ $# -eq 2 ]
then diff2 "$@"
else
file1=$1
file2=$2
shift 2
outfile="$(mktemp)" || exit $?
diff2 $file1 $file2 >$outfile
status=$?
if [ $status != 0 ] ; then rm $outfile ; exit $status ; fi
$0 $outfile "$@"
status=$?
rm $outfile
exit $status
fi