123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118 |
- #!/usr/bin/env bash
- # Apt-P2P Clean
- #
- # Douglas Thrift
- #
- # $Id$
- # Copyright 2010 Douglas Thrift
- #
- # Licensed under the Apache License, Version 2.0 (the "License");
- # you may not use this file except in compliance with the License.
- # You may obtain a copy of the License at
- #
- # http://www.apache.org/licenses/LICENSE-2.0
- #
- # Unless required by applicable law or agreed to in writing, software
- # distributed under the License is distributed on an "AS IS" BASIS,
- # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- # See the License for the specific language governing permissions and
- # limitations under the License.
- function usage()
- {
- cat <<-EOF
- Usage: $program [options]
- Options:
- -e, --execute clean up the apt-p2p cache
- -h, --help show this help message and exit
- -n, --no-execute show what would be done to clean up the apt-p2p cache
- -v, --version show version information and exit
- EOF
- exit $@
- }
- function version()
- {
- echo "$program $version"
- exit
- }
- program=`basename $0`
- version='1.0'
- apt_p2p_cache='/var/cache/apt-p2p/cache'
- apt_archive='/var/cache/apt/archives'
- args=`getopt -l 'execute,help,no-execute,version' -n "$program" -o 'ehnv' -- "$@"`
- val=$?
- [[ $val != 0 ]] && usage $val
- eval set -- "$args"
- unset args val
- until [[ "$1" == -- ]]; do
- case "$1" in
- (-e|--execute)
- execute=1
- ;;
- (-h|--help)
- usage
- ;;
- (-n|--no-execute)
- execute=0
- ;;
- (-v|--version)
- version
- ;;
- esac
- shift
- done
- [[ -z $execute ]] && usage
- set -e
- for directory in "$apt_p2p_cache" "$apt_archive"; do
- if [[ ! -r "$directory" ]] || [[ ! -x "$directory" ]]; then
- echo "$program: $directory: Permission denied" 1>&2
- exit 1
- fi
- done
- if [[ $execute -eq 1 ]]; then
- echo 'Removing files:'
- else
- echo 'Would remove files:'
- fi
- find "$apt_p2p_cache" -name '*.deb' | (
- freed=0
-
- while read deb; do
- if [[ ! -f "$apt_archive/${deb##*/}" ]]; then
- ((freed += `du -B 1 $deb | cut -f 1`))
- echo " ${deb##*/}"
- [[ $execute -eq 1 ]] && rm -f "$deb"
- fi
- done
- if [[ $execute -eq 1 ]]; then
- [[ $freed != 0 ]] && invoke-rc.d apt-p2p restart
- echo -n 'Freed'
- else
- echo -n 'Would free'
- fi
- echo " `bc <<-EOF
- freed=$freed
- scale=1
- 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
- EOF`B of disk space"
- )
- # Clean up Apt-P2P cache
|