
Jeff Schroeder
By: a guest on
Nov 13th, 2007 | syntax:
Bash | size: 2.12 KB | hits: 131 | expires: Never
#!/bin/sh
#########################################################
# Written Nov 13, 2007 and released under the GNU/GPLv2 ##
# by Jeff Schroeder (jeffschroeder@computer.org) # #
######################################################### #
# # #
# restoreperms - restore permissions and fix ownership # #
# on changed files from info in the rpm # #
# database. Uses / Abuses rpm -V # #
# # #
##########################################################
usage() {
cat << EOF >&2
Usage: $0 -p rpmpackagename <options>
-s set the permissions and file ownership back to what
they should be if anything has changed
-l list the permissions on every file from the rpmdb
-n dry-run mode. Don't actually do anything
EOF
exit 1
}
while getopts 'lfnp:' opt 2>/dev/null; do
case "$opt" in
l) LIST=true ;;
p) PACKAGE="$OPTARG" ;;
f) FIX=true ;;
n) DRYRUN=true ;;
*) usage ;;
esac
done
# If called with 0 arguments
test -z "$*" && usage
if [ "$DRYRUN" = "true" ]; then
echo "Simulating:"
RPM="echo /bin/rpm"
else
RPM=/bin/rpm
fi
if [ "$LIST" = "true" ]; then
(echo Listing of proper file permissions for $PACKAGE \('q' to exit\); \
rpm -q --qf '[%{FILENAMES}\t%{FILEMODES:octal}\n]' $PACKAGE) | less
fi
if (test "$FIX" = "true" && ! /bin/rpm -V $PACKAGE >/dev/null 2>&1); then
status=$(rpm -V $PACKAGE 2>&1 | awk '{print $1}' | grep -o '[A-Z]')
for opt in $status; do
case "$opt" in
M)
$RPM --setperms $PACKAGE
;;
U|G)
# No point in setting them twice if the user and group are
# both messed up. Be all intelligent and do it only once
if [ "$PERMS" != "fixed" ]; then
$RPM --setugids $PACKAGE && PERMS=fixed
fi
;;
esac
done
fi