2
0

perms.plugin.zsh 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. # Some useful commands for setting permissions.
  2. #
  3. # Rory Hardy [GneatGeek]
  4. # Andrew Janke [apjanke]
  5. ### Aliases
  6. # Set all files' permissions to 644 recursively in a directory
  7. function set644 {
  8. find "${@:-.}" -type f ! -perm 644 -print0 | xargs -0 chmod 644
  9. }
  10. # Set all directories' permissions to 755 recursively in a directory
  11. function set755 {
  12. find "${@:-.}" -type d ! -perm 755 -print0 | xargs -0 chmod 755
  13. }
  14. ### Functions
  15. # resetperms - fix permissions on files and directories, with confirmation
  16. # Returns 0 on success, nonzero if any errors occurred
  17. function resetperms {
  18. local opts confirm target exit_status chmod_opts use_slow_mode
  19. zparseopts -E -D -a opts -help -slow v+=chmod_opts
  20. if [[ $# > 1 || -n "${opts[(r)--help]}" ]]; then
  21. cat <<EOF
  22. Usage: resetperms [-v] [--help] [--slow] [target]
  23. target is the file or directory to change permissions on. If omitted,
  24. the current directory is taken to be the target.
  25. -v enables verbose output (may be supplied multiple times)
  26. --slow will use a slower but more robust mode, which is effective if
  27. directories themselves have permissions that forbid you from
  28. traversing them.
  29. EOF
  30. exit_status=$(( $# > 1 ))
  31. return $exit_status
  32. fi
  33. if [[ $# -eq 0 ]]; then
  34. target="."
  35. else
  36. target="$1"
  37. fi
  38. if [[ -n ${opts[(r)--slow]} ]]; then use_slow=true; else use_slow=false; fi
  39. # Because this requires confirmation, bail in noninteractive shells
  40. if [[ ! -o interactive ]]; then
  41. echo "resetperms: cannot run in noninteractive shell"
  42. return 1
  43. fi
  44. echo "Fixing perms on $target?"
  45. printf '%s' "Proceed? (y|n) "
  46. read confirm
  47. if [[ "$confirm" != y ]]; then
  48. # User aborted
  49. return 1
  50. fi
  51. # This xargs form is faster than -exec chmod <N> {} \; but will encounter
  52. # issues if the directories themselves have permissions such that you can't
  53. # recurse in to them. If that happens, just rerun this a few times.
  54. exit_status=0;
  55. if [[ $use_slow == true ]]; then
  56. # Process directories first so non-traversable ones are fixed as we go
  57. find "$target" -type d ! -perm 755 -exec chmod $chmod_opts 755 {} \;
  58. if [[ $? -ne 0 ]]; then exit_status=$?; fi
  59. find "$target" -type f ! -perm 644 -exec chmod $chmod_opts 644 {} \;
  60. if [[ $? -ne 0 ]]; then exit_status=$?; fi
  61. else
  62. find "$target" -type d ! -perm 755 -print0 | xargs -0 chmod $chmod_opts 755
  63. if [[ $? -ne 0 ]]; then exit_status=$?; fi
  64. find "$target" -type f ! -perm 644 -print0 | xargs -0 chmod $chmod_opts 644
  65. if [[ $? -ne 0 ]]; then exit_status=$?; fi
  66. fi
  67. echo "Complete"
  68. return $exit_status
  69. }
  70. function fixperms {
  71. print -ru2 "fixperms has been deprecated. Use resetperms instead"
  72. return 1
  73. }