Want more features on Pastebin? Sign Up, it's FREE!
Guest

Jeff Schroeder

By: a guest on Nov 13th, 2007  |  syntax: Bash  |  size: 2.12 KB  |  views: 132  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. #!/bin/sh
  2.  
  3.    #########################################################
  4.   # Written Nov 13, 2007 and released under the GNU/GPLv2 ##
  5.  # by Jeff Schroeder (jeffschroeder@computer.org)        # #
  6. #########################################################  #
  7. #                                                       #  #
  8. # restoreperms - restore permissions and fix ownership  #  #
  9. #                on changed files from info in the rpm  #  #
  10. #                database. Uses / Abuses rpm -V         #  #
  11. #                                                       # #
  12. ##########################################################
  13.  
  14. usage() {
  15. cat << EOF >&2
  16. Usage: $0 -p rpmpackagename <options>
  17.        -s   set the permissions and file ownership back to what
  18.             they should be if anything has changed
  19.        -l   list the permissions on every file from the rpmdb
  20.        -n   dry-run mode. Don't actually do anything
  21. EOF
  22. exit 1
  23. }
  24.  
  25. while getopts 'lfnp:' opt 2>/dev/null; do
  26.    case "$opt" in
  27.        l)    LIST=true ;;
  28.        p)    PACKAGE="$OPTARG" ;;
  29.        f)    FIX=true ;;
  30.        n)    DRYRUN=true ;;
  31.        *)    usage ;;
  32.    esac
  33. done
  34.  
  35. # If called with 0 arguments
  36. test -z "$*" && usage
  37.  
  38. if [ "$DRYRUN" = "true" ]; then
  39.        echo "Simulating:"
  40.        RPM="echo /bin/rpm"
  41. else
  42.        RPM=/bin/rpm
  43. fi
  44.  
  45. if [ "$LIST" = "true" ]; then
  46.    (echo Listing of proper file permissions for $PACKAGE \('q' to exit\); \
  47.    rpm -q --qf '[%{FILENAMES}\t%{FILEMODES:octal}\n]' $PACKAGE) | less
  48. fi
  49.  
  50. if (test "$FIX" = "true" && ! /bin/rpm -V $PACKAGE >/dev/null 2>&1); then
  51.    status=$(rpm -V $PACKAGE 2>&1 | awk '{print $1}' | grep -o '[A-Z]')
  52.    for opt in $status; do
  53.        case "$opt" in
  54.            M)
  55.                $RPM --setperms $PACKAGE
  56.            ;;
  57.            U|G)
  58.                # No point in setting them twice if the user and group are
  59.                # both messed up. Be all intelligent and do it only once
  60.                if [ "$PERMS" != "fixed" ]; then
  61.                        $RPM --setugids $PACKAGE && PERMS=fixed
  62.                fi
  63.            ;;
  64.        esac
  65.     done
  66. fi
clone this paste RAW Paste Data