Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
bee/src/bee-remove.sh.in
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
237 lines (200 sloc)
6.27 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# | |
# bee-remove - remove a installed bee-pkg | |
# | |
# Copyright (C) 2009-2016 | |
# Marius Tolzmann <m@rius.berlin> | |
# Tobias Dreyer <dreyer@molgen.mpg.de> | |
# and other bee developers | |
# | |
# This file is part of bee. | |
# | |
# bee is free software; you can redistribute it and/or modify | |
# it under the terms of the GNU General Public License as published by | |
# the Free Software Foundation, either version 3 of the License, or | |
# (at your option) any later version. | |
# | |
# This program is distributed in the hope that it will be useful, | |
# but WITHOUT ANY WARRANTY; without even the implied warranty of | |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
# GNU General Public License for more details. | |
# | |
# You should have received a copy of the GNU General Public License | |
# along with this program. If not, see <http://www.gnu.org/licenses/>. | |
# | |
if [ -z "${BEE_VERSION}" ] ; then | |
echo >&2 "BEE-ERROR: please call $0 from bee .." | |
exit 1 | |
fi | |
: ${BEE_BINDIR:=@BINDIR@} | |
: ${BEE_LIBEXECDIR:=@LIBEXECDIR@} | |
function bee-cache() { | |
${BEE_LIBEXECDIR}/bee/bee.d/bee-cache "${@}" | |
} | |
function bee-list() { | |
${BEE_LIBEXECDIR}/bee/bee.d/bee-list "${@}" | |
} | |
function bee-query() { | |
${BEE_LIBEXECDIR}/bee/bee.d/bee-query "${@}" | |
} | |
function pkg_remove_all() { | |
for pkg in "${@}" ; do | |
pkg_remove "${pkg}" | |
done | |
} | |
function pkg_remove() { | |
search=$1 | |
# pattern is a pkg in BEE_METADIR | |
if [ -d "${BEE_METADIR}/${search}" ] ; then | |
do_remove ${search} | |
return | |
fi | |
# pattern is no installed pkg | |
# show all pkgs that match pattern | |
echo >&2 "bee-remove: FAILED to remove '${search}': No such package." | |
echo >&2 "bee-remove: List of installed packages matching '${search}':" | |
bee-list -i "${search}" | sed -e 's,^, ,' >&2 | |
if [ ${PIPESTATUS[0]} != 0 ] ; then | |
echo >&2 " No packages found." | |
fi | |
} | |
# for each argument | |
# recursive get all dirnames that are directories on same mountpoint | |
# | |
# /path/to/somewhere -> /path/to/somewhere /path/to /path | |
# | |
function subdirs() { | |
local last | |
local mtabexists=0 | |
# checking for mountpoint boundaries only works if | |
# /etc/mtab exists since "stat" depends on it.. | |
if [ -e "/etc/mtab" ] ; then | |
mtabexists=1 | |
fi | |
for path in "${@}" ; do | |
last="" | |
while [ "${path:0:1}" = '/' -a "${path}" != "" ] ; do | |
if [ ! -d "${path}" ] ; then | |
continue 2 | |
fi | |
if [ "${mtabexists}" = "1" ] ; then | |
this=$(stat "${path}" --printf "%F%m") | |
if [ -n "${last}" -a "${last}" != "${this}" ] ; then | |
continue 2 | |
else | |
last="${this}" | |
fi | |
fi | |
if [ "${this:0:9}" != 'directory' ] ; then | |
continue 2 | |
fi | |
echo ${path} | |
path=${path%/*} | |
done | |
done | |
} | |
function do_remove() { | |
pkg=${1} | |
mv ${BEE_METADIR}/${pkg}/CONTENT{,.bee-remove} 2>/dev/null | |
if [ "$?" != "0" ] ; then | |
return 1 | |
fi | |
echo "removing ${pkg} .." | |
bee-cache update ${pkg} | |
if [ $? -ne 0 ] ; then | |
echo >&2 "bee-remove: ${pkg}: bee-cache update failed." | |
exit 1 | |
fi | |
run_hooks pre-remove ${pkg} ${BEE_METADIR}/${pkg}/CONTENT.bee-remove | |
# remove files that are uniq to the package.. | |
while read f ; do | |
if [ -d "${f}" -a ! -h "${f}" ] ; then | |
rmdir ${OPT_VERBOSE:+-v} "${f}" | |
else | |
rm -f ${OPT_VERBOSE:+-v} "${f}" | |
fi | |
done < <(bee-cache --tmpinstall "${pkg}" print-uniq-files "${pkg}") | |
run_hooks post-remove ${pkg} ${BEE_METADIR}/${pkg}/CONTENT.bee-remove | |
if [ -r "${BEE_METADIR}/${pkg}/META" ] ; then | |
. "${BEE_METADIR}/${pkg}/META" | |
fi | |
# removing empty basedirs | |
if [ "${BEEMETAFORMAT}" = "2" ] ; then | |
vars=( PREFIX EPREFIX BINDIR SBINDIR LIBEXECDIR SYSCONFDIR \ | |
SHAREDSTATEDIR LOCALSTATEDIR LIBDIR INCLUDEDIR \ | |
DATAROOTDIR DATADIR INFODIR LOCALEDIR MANDIR DOCDIR ) | |
dirs=( $(for var in ${vars[@]} ; do eval echo \${PKG_${var}} ; done) ) | |
for dir in $( subdirs ${dirs[@]} | sort -ur ) ; do | |
# if dir does is not part of package -> skip it | |
content_file="${BEE_METADIR}/${pkg}/CONTENT.bee-remove" | |
if ! grep -q -l -E ":file=${dir}(/|$)" "${content_file}" ; then | |
continue | |
fi | |
# if dir is not empty -> skip it | |
nlinks=$( stat "${dir}" --printf "%h" ) | |
if [ "${nlinks}" -gt 2 ] ; then | |
continue | |
fi | |
# dir is empty so let's check if it is still owned by some package | |
# isn't this always true since current package is still available? | |
# this may be fixed automatically once bee query doesn't scan the | |
# filesystem anymore and we have an index db 8) | |
# using bee dep here is currently to slow 8( | |
if [ -z "$(bee-query ${dir} | head -1)" ] ; then | |
rmdir ${OPT_VERBOSE:+-v} ${dir} | |
fi | |
done | |
fi | |
#cleanup meta directory | |
rm -fr ${OPT_VERBOSE:+-v} ${BEE_METADIR}/${pkg} | |
bee-cache update ${pkg} | |
} | |
function run_hooks() { | |
local action=${1} | |
local pkg=${2} | |
local content=${3} | |
: ${content:=${BEE_METADIR}/${pkg}/CONTENT} | |
for t in ${BEE_LIBEXECDIR}/bee/hooks.d/* ; do | |
${t} ${action} ${pkg} ${content} | |
done | |
} | |
function usage() { | |
cat <<-EOF | |
bee-remove v${BEE_VERSION} 2009-2016 | |
by Marius Tolzmann <m@rius.berlin> and Tobias Dreyer <dreyer@molgen.mpg.de> | |
Usage: bee remove [options] <package> | |
Options: | |
-h, --help display this help | |
-v, --verbose print removed files and directories | |
EOF | |
} | |
options=$(${BEE_BINDIR}/beegetopt --name bee-remove \ | |
--option verbose/v \ | |
--option help/h \ | |
-- "$@") | |
if [ $? != 0 ] ; then | |
usage | |
exit 1 | |
fi | |
eval set -- "${options}" | |
while true ; do | |
case "$1" in | |
--help) | |
usage | |
exit | |
;; | |
--verbose) | |
OPT_VERBOSE="yes" | |
shift | |
;; | |
--) | |
shift | |
if [ -z "${1}" ] ; then | |
usage | |
exit 1 | |
fi | |
pkg_remove_all "$@" | |
break | |
;; | |
esac | |
done |