]> git.proxmox.com Git - mirror_linux-firmware.git/blob - copy-firmware.sh
Merge branch 'robot/pr-24-1707787287' into 'main'
[mirror_linux-firmware.git] / copy-firmware.sh
1 #!/bin/sh
2 # SPDX-License-Identifier: GPL-2.0
3 #
4 # Copy firmware files based on WHENCE list
5 #
6
7 verbose=:
8 prune=no
9 # shellcheck disable=SC2209
10 compress=cat
11 compext=
12 skip_dedup=0
13
14 while test $# -gt 0; do
15 case $1 in
16 -v | --verbose)
17 # shellcheck disable=SC2209
18 verbose=echo
19 shift
20 ;;
21
22 -P | --prune)
23 prune=yes
24 shift
25 ;;
26
27 --xz)
28 if test "$compext" = ".zst"; then
29 echo "ERROR: cannot mix XZ and ZSTD compression"
30 exit 1
31 fi
32 compress="xz --compress --quiet --stdout --check=crc32"
33 compext=".xz"
34 shift
35 ;;
36
37 --zstd)
38 if test "$compext" = ".xz"; then
39 echo "ERROR: cannot mix XZ and ZSTD compression"
40 exit 1
41 fi
42 # shellcheck disable=SC2209
43 compress="zstd --compress --quiet --stdout"
44 compext=".zst"
45 shift
46 ;;
47
48 --ignore-duplicates)
49 skip_dedup=1
50 shift
51 ;;
52
53 -*)
54 if test "$compress" = "cat"; then
55 echo "ERROR: unknown command-line option: $1"
56 exit 1
57 fi
58 compress="$compress $1"
59 shift
60 ;;
61 *)
62 if test "x$destdir" != "x"; then
63 echo "ERROR: unknown command-line options: $*"
64 exit 1
65 fi
66
67 destdir="$1"
68 shift
69 ;;
70 esac
71 done
72
73 if [ -z "$destdir" ]; then
74 echo "ERROR: destination directory was not specified"
75 exit 1
76 fi
77
78 if ! command -v rdfind >/dev/null; then
79 if [ "$skip_dedup" != 1 ]; then
80 echo "ERROR: rdfind is not installed. Pass --ignore-duplicates to skip deduplication"
81 exit 1
82 fi
83 fi
84
85 # shellcheck disable=SC2162 # file/folder name can include escaped symbols
86 grep -E '^(RawFile|File):' WHENCE | sed -E -e 's/^(RawFile|File): */\1 /;s/"//g' | while read k f; do
87 test -f "$f" || continue
88 install -d "$destdir/$(dirname "$f")"
89 $verbose "copying/compressing file $f$compext"
90 if test "$compress" != "cat" && test "$k" = "RawFile"; then
91 $verbose "compression will be skipped for file $f"
92 cat "$f" > "$destdir/$f"
93 else
94 $compress "$f" > "$destdir/$f$compext"
95 fi
96 done
97
98 if [ "$skip_dedup" != 1 ] ; then
99 $verbose "Finding duplicate files"
100 rdfind -makesymlinks true -makeresultsfile false "$destdir" >/dev/null
101 find "$destdir" -type l | while read -r l; do
102 target="$(realpath "$l")"
103 $verbose "Correcting path for $l"
104 ln -fs "$(realpath --relative-to="$(dirname "$(realpath -s "$l")")" "$target")" "$l"
105 done
106 fi
107
108 # shellcheck disable=SC2162 # file/folder name can include escaped symbols
109 grep -E '^Link:' WHENCE | sed -e 's/^Link: *//g;s/-> //g' | while read f d; do
110 if test -L "$f$compext"; then
111 test -f "$destdir/$f$compext" && continue
112 $verbose "copying link $f$compext"
113 install -d "$destdir/$(dirname "$f")"
114 cp -d "$f$compext" "$destdir/$f$compext"
115
116 if test "x$d" != "x"; then
117 target="$(readlink "$f")"
118
119 if test "x$target" != "x$d"; then
120 $verbose "WARNING: inconsistent symlink target: $target != $d"
121 else
122 if test "x$prune" != "xyes"; then
123 $verbose "WARNING: unneeded symlink detected: $f"
124 else
125 $verbose "WARNING: pruning unneeded symlink $f"
126 rm -f "$f$compext"
127 fi
128 fi
129 else
130 $verbose "WARNING: missing target for symlink $f"
131 fi
132 else
133 directory="$destdir/$(dirname "$f")"
134 install -d "$directory"
135 target="$(cd "$directory" && realpath -m -s "$d")"
136 if test -d "$target"; then
137 $verbose "creating link $f -> $d"
138 ln -s "$d" "$destdir/$f"
139 else
140 $verbose "creating link $f$compext -> $d$compext"
141 ln -s "$d$compext" "$destdir/$f$compext"
142 fi
143 fi
144 done
145
146 exit 0
147
148 # vim: et sw=4 sts=4 ts=4