62929263fafd9e3c1da043bc9b40fa97fccfa7a1

Author
Carlo Sala <carlosalag@protonmail.com>
Committer
Marc Cornellà <hello@mcornella.com>
Date

Message

fix(perms)!: change function name from `fixperms` to `resetperms` and document caution (#10686)

BREAKING CHANGE: function `fixperms` has been renamed to the more accurate `resetperms`.
Please read the README carefully before using it as it may badly reset the permissions.

Fixes #10648
Closes #10686

Diff

  1diff --git a/plugins/perms/README.md b/plugins/perms/README.md
  2index ae7a36b9d78894e5b76455a19dcee0accf522b89..ae10fa659dc47c7edefe3c8662a3b5e02eef32e7 100644
  3--- a/plugins/perms/README.md
  4+++ b/plugins/perms/README.md
  5@@ -10,6 +10,16 @@ plugins=(... perms)
  6 
  7 ## Usage
  8 
  9-* `set755` recursively sets all given directories (default to .) to octal 755.
 10-* `set644` recursively sets all given files (default to .) to octal 644.
 11-* `fixperms` is a wrapper around `set755` and `set644` applied to a specified directory or the current directory otherwise. It also prompts prior to execution unlike the other two aliases.
 12+> **CAUTION:** these functions are harmful if you don't know what they do.
 13+
 14+- `set755`: sets the permission to octal 755 for all given directories and their child directories (by default, starting from the current directory).
 15+
 16+- `set644`: sets the permission to octal 644 for all files of the given directory (by default, the current directory), recursively. It will only affect regular files (no symlinks).
 17+
 18+- `resetperms` is a wrapper around `set755` and `set644` applied to a specified directory or the current directory otherwise.
 19+  It will set the permissions to 755 for directories, and 644 for files.
 20+
 21+## Reference
 22+
 23+- octal 644: _read and write_ for the owner, _read_ for the group and others users.
 24+- octal 755: _read, write and execute_ permissions for the owner, and _read and execute_ for the group and others users.
 25diff --git a/plugins/perms/perms.plugin.zsh b/plugins/perms/perms.plugin.zsh
 26index 1a7472c1cb9d8939fa16d957a02010cf0f12f04e..353b584119ee46cbba4695a3d8d43cfd84397d06 100644
 27--- a/plugins/perms/perms.plugin.zsh
 28+++ b/plugins/perms/perms.plugin.zsh
 29@@ -6,25 +6,25 @@
 30 ### Aliases
 31 
 32 # Set all files' permissions to 644 recursively in a directory
 33-set644() {
 34+function set644 {
 35 	find "${@:-.}" -type f ! -perm 644 -print0 | xargs -0 chmod 644
 36 }
 37 
 38 # Set all directories' permissions to 755 recursively in a directory
 39-set755() {
 40+function set755 {
 41 	find "${@:-.}" -type d ! -perm 755 -print0 | xargs -0 chmod 755
 42 }
 43 
 44 ### Functions
 45 
 46-# fixperms - fix permissions on files and directories, with confirmation
 47+# resetperms - fix permissions on files and directories, with confirmation
 48 # Returns 0 on success, nonzero if any errors occurred
 49-fixperms () {
 50+function resetperms {
 51   local opts confirm target exit_status chmod_opts use_slow_mode
 52   zparseopts -E -D -a opts -help -slow v+=chmod_opts
 53   if [[ $# > 1 || -n "${opts[(r)--help]}" ]]; then
 54     cat <<EOF
 55-Usage: fixperms [-v] [--help] [--slow] [target]
 56+Usage: resetperms [-v] [--help] [--slow] [target]
 57 
 58   target  is the file or directory to change permissions on. If omitted,
 59           the current directory is taken to be the target.
 60@@ -40,7 +40,7 @@ EOF
 61     return $exit_status
 62   fi
 63 
 64-  if [[ $# == 0 ]]; then
 65+  if [[ $# -eq 0 ]]; then
 66     target="."
 67   else
 68     target="$1"
 69@@ -49,7 +49,7 @@ EOF
 70 
 71   # Because this requires confirmation, bail in noninteractive shells
 72   if [[ ! -o interactive ]]; then
 73-    echo "fixperms: cannot run in noninteractive shell"
 74+    echo "resetperms: cannot run in noninteractive shell"
 75     return 1
 76   fi
 77 
 78@@ -68,15 +68,20 @@ EOF
 79   if [[ $use_slow == true ]]; then
 80     # Process directories first so non-traversable ones are fixed as we go
 81     find "$target" -type d ! -perm 755 -exec chmod $chmod_opts 755 {} \;
 82-    if [[ $? != 0 ]]; then exit_status=$?; fi
 83+    if [[ $? -ne 0 ]]; then exit_status=$?; fi
 84     find "$target" -type f ! -perm 644 -exec chmod $chmod_opts 644 {} \;
 85-    if [[ $? != 0 ]]; then exit_status=$?; fi
 86+    if [[ $? -ne 0 ]]; then exit_status=$?; fi
 87   else
 88     find "$target" -type d ! -perm 755 -print0 | xargs -0 chmod $chmod_opts 755
 89-    if [[ $? != 0 ]]; then exit_status=$?; fi
 90+    if [[ $? -ne 0 ]]; then exit_status=$?; fi
 91     find "$target" -type f ! -perm 644 -print0 | xargs -0 chmod $chmod_opts 644
 92-    if [[ $? != 0 ]]; then exit_status=$?; fi
 93+    if [[ $? -ne 0 ]]; then exit_status=$?; fi
 94   fi
 95   echo "Complete"
 96   return $exit_status
 97 }
 98+
 99+function fixperms {
100+  print -ru2 "fixperms has been deprecated. Use resetperms instead"
101+  return 1
102+}