apt-p2p-clean 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. #!/usr/bin/env bash
  2. # Apt-P2P Clean
  3. #
  4. # Douglas Thrift
  5. #
  6. # $Id$
  7. # Copyright 2010 Douglas Thrift
  8. #
  9. # Licensed under the Apache License, Version 2.0 (the "License");
  10. # you may not use this file except in compliance with the License.
  11. # You may obtain a copy of the License at
  12. #
  13. # http://www.apache.org/licenses/LICENSE-2.0
  14. #
  15. # Unless required by applicable law or agreed to in writing, software
  16. # distributed under the License is distributed on an "AS IS" BASIS,
  17. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  18. # See the License for the specific language governing permissions and
  19. # limitations under the License.
  20. function usage()
  21. {
  22. cat <<-EOF
  23. Usage: $program [options]
  24. Options:
  25. -e, --execute clean up the apt-p2p cache
  26. -h, --help show this help message and exit
  27. -n, --no-execute show what would be done to clean up the apt-p2p cache
  28. -v, --version show version information and exit
  29. EOF
  30. exit $@
  31. }
  32. function version()
  33. {
  34. echo "$program $version"
  35. exit
  36. }
  37. program=`basename $0`
  38. version='1.0'
  39. apt_p2p_cache='/var/cache/apt-p2p/cache'
  40. apt_archive='/var/cache/apt/archives'
  41. args=`getopt -l 'execute,help,no-execute,version' -n "$program" -o 'ehnv' -- "$@"`
  42. val=$?
  43. [[ $val != 0 ]] && usage $val
  44. eval set -- "$args"
  45. unset args val
  46. until [[ "$1" == -- ]]; do
  47. case "$1" in
  48. (-e|--execute)
  49. execute=1
  50. ;;
  51. (-h|--help)
  52. usage
  53. ;;
  54. (-n|--no-execute)
  55. execute=0
  56. ;;
  57. (-v|--version)
  58. version
  59. ;;
  60. esac
  61. shift
  62. done
  63. [[ -z $execute ]] && usage
  64. set -e
  65. for directory in "$apt_p2p_cache" "$apt_archive"; do
  66. if [[ ! -r "$directory" ]] || [[ ! -x "$directory" ]]; then
  67. echo "$program: $directory: Permission denied" 1>&2
  68. exit 1
  69. fi
  70. done
  71. if [[ $execute -eq 1 ]]; then
  72. echo 'Removing files:'
  73. else
  74. echo 'Would remove files:'
  75. fi
  76. find "$apt_p2p_cache" -name '*.deb' | (
  77. freed=0
  78. while read deb; do
  79. if [[ ! -f "$apt_archive/${deb##*/}" ]]; then
  80. ((freed += `du -B 1 $deb | cut -f 1`))
  81. echo " ${deb##*/}"
  82. [[ $execute -eq 1 ]] && rm -f "$deb"
  83. fi
  84. done
  85. if [[ $execute -eq 1 ]]; then
  86. [[ $freed != 0 ]] && invoke-rc.d apt-p2p restart
  87. echo -n 'Freed'
  88. else
  89. echo -n 'Would free'
  90. fi
  91. echo " `bc <<-EOF
  92. freed=$freed
  93. scale=1
  94. if (freed >= 1024 ^ 3) print freed / 1024 ^ 3, "G" else if (freed >= 1024 ^ 2) print freed / 1024 ^ 2, "M" else if (freed >= 1024) print freed / 1024, "K" else freed
  95. EOF`B of disk space"
  96. )
  97. # Clean up Apt-P2P cache