-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathextents-merge
executable file
·43 lines (36 loc) · 1 KB
/
extents-merge
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
#!/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
# list of extent start and finish pairs on stdin
# output list with overlapping and adjacent extents merged
# Make sure errors are passed back
set -o pipefail
if [ "$1" = "-s" ]
then
shift
$0 "$@" | extents-size
exit $?
fi
sort -n |
# Merge adjacent or overlapping extents
awk -e '
BEGIN {
start = -1;
end = -2;
}
{
if ($1 <= end + 1) {
# This extent overlaps the preceeding one, or is adjacent to it
# so update the end if it is further than the preceding one
if ($2 > end) end = $2;
} else {
# New extent which does not need to be merged
if (start >= 0) print start, end, end - start + 1;
start = $1; end = $2;
}
}
END {if (start >= 0) print start, end, end - start + 1}
'