python-texttable: update to 1.3.1
[void-packages.git] / common / xbps-src / shutils / purge_distfiles.sh
blob73df13932bc3d3fbdadce7de28f5d7779c6250eb
1 # Scan srcpkgs/*/template for hashes and distfiles to determine
2 # obsolete sources/by_sha256 files and their corresponding
3 # sources/<pkgname>-<version> files that can be purged
6 purge_distfiles() {
7 # Ignore msg_error calls when sourcing templates
8 msg_error() {
11 if [ -z "$XBPS_SRCDISTDIR" ]; then
12 msg_error "The variable \$XBPS_SRCDISTDIR is not set."
13 exit 1
16 # Scan all templates for their current distfiles and checksums (hashes)
18 declare -A my_hashes
19 templates=(srcpkgs/*/template)
20 max=${#templates[@]}
21 cur=0
22 if [ -z "$max" ]; then
23 msg_error "No srcpkgs/*/template files found. Wrong working directory?"
24 exit 1
26 percent=-1
27 for template in ${templates[@]}; do
28 pkg=${template#*/}
29 pkg=${pkg%/*}
30 if [ ! -L "srcpkgs/$pkg" ]; then
31 unset checksum
32 source $template 2>/dev/null
33 read -a _my_hashes <<< ${checksum}
34 i=0
35 while [ -n "${_my_hashes[$i]}" ]; do
36 hash="${_my_hashes[$i]}"
37 [ -z "${my_hashes[$hash]}" ] && my_hashes[$hash]=$template
38 i=$((i + 1))
39 done
41 cur=$((cur + 1))
42 pnew=$((100 * cur / max))
43 if [ $pnew -ne $percent ]; then
44 percent=$pnew
45 printf "\rScanning templates : %3d%% (%d/%d)" $percent $cur $max
47 done
48 echo
49 echo "Number of hashes : ${#my_hashes[@]}"
52 # Collect inodes of all distfiles in $XBPS_SRCDISTDIR
54 declare -A inodes
55 distfiles=($XBPS_SRCDISTDIR/*/*)
56 max=${#distfiles[@]}
57 if [ -z "$max" ]; then
58 msg_error "No distfiles files found in '$XBPS_SRCDISTDIR'"
59 exit 1
61 cur=0
62 percent=-1
63 for distfile in ${distfiles[@]}; do
64 inode=$(stat "$distfile" --printf "%i")
65 if [ -z "${inodes[$inode]}" ]; then
66 inodes[$inode]="$distfile"
67 else
68 inodes[$inode]+="|$distfile"
70 cur=$((cur + 1))
71 pnew=$((100 * cur / max))
72 if [ $pnew -ne $percent ]; then
73 percent=$pnew
74 printf "\rCollecting inodes : %3d%% (%d/%d)" $percent $cur $max
76 done
77 echo
79 hashes=($XBPS_SRCDISTDIR/by_sha256/*)
80 readonly HASHLEN=64
81 for file in ${hashes[@]}; do
82 hash_distfile=${file##*/}
83 hash=${hash_distfile:0:$HASHLEN}
84 [ -n "${my_hashes[$hash]}" ] && continue
85 inode=$(stat "$file" --printf "%i")
86 echo "Obsolete $hash (inode: $inode)"
87 ( IFS="|"; for f in ${inodes[$inode]}; do rm -v "$f"; rmdir "${f%/*}" 2>/dev/null; done )
88 done
89 echo "Done."