-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathrelink_symlinks
executable file
·48 lines (43 loc) · 1.42 KB
/
relink_symlinks
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
#!/bin/sh
# We cannot use "readlink -f" command because it doesn't know about $RPM_BUILD_ROOT
# and follows symlinks on real filesystem instead of $RPM_BUILD_ROOT
readlink_f() {
__symlink="$(readlink $1)"
if [ -z "$__symlink" ]; then
printf '%s\n' $1 | sed s,"$RPM_BUILD_ROOT",,
else
readlink_f "$RPM_BUILD_ROOT$__symlink"
fi
}
if [ -z "$RPM_BUILD_ROOT" ]; then
printf '%s\n' "No build root defined" >&2
exit 1
fi
if [ ! -d "$RPM_BUILD_ROOT" ]; then
printf '%s\n' "Invalid build root" >&2
exit 1
fi
find "$RPM_BUILD_ROOT" \
-type l -print0 | xargs --no-run-if-empty -0 ls 2>/dev/null |
while read symlink; do
path="$(readlink_f "$symlink")"
printf '%s\n' $path | grep -q -E '^(/dev|/sys|/proc)' && continue
# skip non-absolute path
if printf '%s\n' $path | grep -q -E '^/'; then
# absolute path needs to be made into an absolute path relative to buildroot
path="$RPM_BUILD_ROOT$path"
if stat "$path" &> /dev/null; then
rm "$symlink"
# ln will try follow symlink if source exists as symlink, so let's move
# it out of the way first, then back afterwards
stat "$path" &> /dev/null && mv "$path" "$path.origlink"
output="$(ln -svr "$path" "$symlink" 2>&1)"
stat "$path.origlink" &> /dev/null && mv "$path.origlink" "$path"
if ! stat "$symlink" &> /dev/null; then
printf '%s\n' "symlink relativization failed:" >&2
printf '%s\n' "$output" >&2
ls --color -l "$symlink" >&2
fi
fi
fi
done