apt-p2p-clean 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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. -a ARCHIVE, --archive=ARCHIVE set the APT archive directory (default:
  26. $_apt_archive)
  27. -c CACHE, --cache=CACHE set the Apt-P2P cache directory (default:
  28. $_apt_p2p_cache)
  29. -e, --execute clean up the Apt-P2P cache
  30. -h, --help show this help message and exit
  31. -n, --no-execute show what would be done to clean up the
  32. Apt-P2P cache
  33. -v, --version show version information and exit
  34. EOF
  35. exit $@
  36. }
  37. function version()
  38. {
  39. echo "$program $version"
  40. exit
  41. }
  42. program=`basename $0`
  43. version='1.0.4'
  44. apt_p2p_cache='/var/cache/apt-p2p/cache'
  45. _apt_p2p_cache="$apt_p2p_cache"
  46. apt_archive='/var/cache/apt/archives'
  47. _apt_archive="$apt_archive"
  48. args=`getopt -l 'archive:,cache:,execute,help,no-execute,version' -n "$program" -o 'a:c:ehnv' -- "$@"`
  49. value=$?
  50. [[ $value != 0 ]] && usage $value
  51. eval set -- "$args"
  52. unset args
  53. until [[ "$1" == -- ]]; do
  54. value=1
  55. case "$1" in
  56. (-a|--archive)
  57. apt_archive="$2"
  58. value=2
  59. ;;
  60. (-c|--cache)
  61. apt_p2p_cache="$2"
  62. value=2
  63. ;;
  64. (-e|--execute)
  65. execute=1
  66. ;;
  67. (-h|--help)
  68. usage
  69. ;;
  70. (-n|--no-execute)
  71. execute=0
  72. ;;
  73. (-v|--version)
  74. version
  75. ;;
  76. esac
  77. shift $value
  78. done
  79. [[ -z $execute ]] && usage
  80. unset value
  81. set -e
  82. for directory in "$apt_p2p_cache" "$apt_archive"; do
  83. if [[ ! -r "$directory" ]] || [[ ! -x "$directory" ]]; then
  84. echo "$program: $directory: Permission denied" 1>&2
  85. exit 1
  86. fi
  87. done
  88. if [[ $execute -eq 1 ]]; then
  89. echo 'Removing files:'
  90. else
  91. echo 'Would remove files:'
  92. fi
  93. find "$apt_p2p_cache" -name '*.deb' | (
  94. freed=0
  95. while read deb; do
  96. _deb="${deb##*/}"
  97. _deb="${_deb//%7e/~}"
  98. if [[ ! -f "$apt_archive/$_deb" ]]; then
  99. if dpkg-deb -W $deb >/dev/null 2>&1; then
  100. _deb=`dpkg-deb -W --showformat='${Package}_${Version}_${Architecture}.deb' $deb`
  101. fi
  102. if [[ ! -f "$apt_archive/${_deb//:/%3a}" ]]; then
  103. # XXX: '|| true' because bash believes ((freed += 0)) is bad
  104. ((freed += `du -B 1 $deb | cut -f 1`)) || true
  105. echo " $_deb"
  106. [[ $execute -eq 1 ]] && rm -f "$deb"
  107. fi
  108. fi
  109. done
  110. if [[ $execute -eq 1 ]]; then
  111. [[ $freed != 0 ]] && invoke-rc.d apt-p2p restart
  112. echo -n 'Freed'
  113. else
  114. echo -n 'Would free'
  115. fi
  116. echo " `bc <<-EOF
  117. freed=$freed
  118. scale=1
  119. 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
  120. EOF`B of disk space"
  121. )
  122. # Clean up the Apt-P2P cache