purge_distfiles.sh (2300B)
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 4 5 6purge_distfiles() { 7 readonly HASHLEN=64 8 if [ -z "$XBPS_SRCDISTDIR" ]; then 9 msg_error "The variable \$XBPS_SRCDISTDIR is not set." 10 exit 1 11 fi 12 # 13 # Scan all templates for their current distfiles and checksums (hashes) 14 # 15 declare -A my_hashes 16 templates=($(find srcpkgs -mindepth 1 -maxdepth 1 -type d -printf "srcpkgs/%f/template\n")) 17 max=${#templates[@]} 18 cur=0 19 if [ -z "$max" ]; then 20 msg_error "No srcpkgs/*/template files found. Wrong working directory?" 21 exit 1 22 fi 23 percent=-1 24 for template in ${templates[@]}; do 25 pkg=${template#*/} 26 pkg=${pkg%/*} 27 if [ ! -L "srcpkgs/$pkg" ]; then 28 checksum="$(grep -Ehrow [0-9a-f]{$HASHLEN} ${template}|sort|uniq|tr '\n' ' ')" 29 read -a _my_hashes <<< ${checksum} 30 i=0 31 while [ -n "${_my_hashes[$i]}" ]; do 32 hash="${_my_hashes[$i]}" 33 [ -z "${my_hashes[$hash]}" ] && my_hashes[$hash]=$template 34 i=$((i + 1)) 35 done 36 fi 37 cur=$((cur + 1)) 38 pnew=$((100 * cur / max)) 39 if [ $pnew -ne $percent ]; then 40 percent=$pnew 41 printf "\rScanning templates : %3d%% (%d/%d)" $percent $cur $max 42 fi 43 done 44 echo 45 echo "Number of hashes : ${#my_hashes[@]}" 46 47 # 48 # Collect inodes of all distfiles in $XBPS_SRCDISTDIR 49 # 50 declare -A inodes 51 distfiles=($XBPS_SRCDISTDIR/*/*) 52 max=${#distfiles[@]} 53 if [ -z "$max" ]; then 54 msg_error "No distfiles files found in '$XBPS_SRCDISTDIR'" 55 exit 1 56 fi 57 cur=0 58 percent=-1 59 for distfile in ${distfiles[@]}; do 60 inode=$(stat "$distfile" --printf "%i") 61 if [ -z "${inodes[$inode]}" ]; then 62 inodes[$inode]="$distfile" 63 else 64 inodes[$inode]+="|$distfile" 65 fi 66 cur=$((cur + 1)) 67 pnew=$((100 * cur / max)) 68 if [ $pnew -ne $percent ]; then 69 percent=$pnew 70 printf "\rCollecting inodes : %3d%% (%d/%d)" $percent $cur $max 71 fi 72 done 73 echo 74 75 hashes=($XBPS_SRCDISTDIR/by_sha256/*) 76 for file in ${hashes[@]}; do 77 hash_distfile=${file##*/} 78 hash=${hash_distfile:0:$HASHLEN} 79 [ -n "${my_hashes[$hash]}" ] && continue 80 inode=$(stat "$file" --printf "%i") 81 echo "Obsolete $hash (inode: $inode)" 82 ( IFS="|"; for f in ${inodes[$inode]}; do rm -vf "$f"; rmdir "${f%/*}" 2>/dev/null; done ) 83 done 84 echo "Done." 85}