diff options
author | Aditya <bluenerd@protonmail.com> | 2023-02-27 20:04:56 +0530 |
---|---|---|
committer | Aditya <bluenerd@protonmail.com> | 2023-02-27 20:04:56 +0530 |
commit | edc449275b6c04445f58b108ca0937a87c1e8430 (patch) | |
tree | 9fd484d58145b616f29a78857cc0b1c8b1c18f05 /oh-my-zsh/plugins/perms | |
parent | 6f5424ca96c4221ef433f545642669e9c104d0ed (diff) |
add zsh
Diffstat (limited to 'oh-my-zsh/plugins/perms')
-rw-r--r-- | oh-my-zsh/plugins/perms/README.md | 25 | ||||
-rw-r--r-- | oh-my-zsh/plugins/perms/perms.plugin.zsh | 87 |
2 files changed, 112 insertions, 0 deletions
diff --git a/oh-my-zsh/plugins/perms/README.md b/oh-my-zsh/plugins/perms/README.md new file mode 100644 index 0000000..ae10fa6 --- /dev/null +++ b/oh-my-zsh/plugins/perms/README.md @@ -0,0 +1,25 @@ +# Perms plugin + +Plugin to handle some unix filesystem permissions quickly. + +To use it, add `perms` to the plugins array in your zshrc file: + +```zsh +plugins=(... perms) +``` + +## Usage + +> **CAUTION:** these functions are harmful if you don't know what they do. + +- `set755`: sets the permission to octal 755 for all given directories and their child directories (by default, starting from the current directory). + +- `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). + +- `resetperms` is a wrapper around `set755` and `set644` applied to a specified directory or the current directory otherwise. + It will set the permissions to 755 for directories, and 644 for files. + +## Reference + +- octal 644: _read and write_ for the owner, _read_ for the group and others users. +- octal 755: _read, write and execute_ permissions for the owner, and _read and execute_ for the group and others users. diff --git a/oh-my-zsh/plugins/perms/perms.plugin.zsh b/oh-my-zsh/plugins/perms/perms.plugin.zsh new file mode 100644 index 0000000..353b584 --- /dev/null +++ b/oh-my-zsh/plugins/perms/perms.plugin.zsh @@ -0,0 +1,87 @@ +# Some useful commands for setting permissions. +# +# Rory Hardy [GneatGeek] +# Andrew Janke [apjanke] + +### Aliases + +# Set all files' permissions to 644 recursively in a directory +function set644 { + find "${@:-.}" -type f ! -perm 644 -print0 | xargs -0 chmod 644 +} + +# Set all directories' permissions to 755 recursively in a directory +function set755 { + find "${@:-.}" -type d ! -perm 755 -print0 | xargs -0 chmod 755 +} + +### Functions + +# resetperms - fix permissions on files and directories, with confirmation +# Returns 0 on success, nonzero if any errors occurred +function resetperms { + local opts confirm target exit_status chmod_opts use_slow_mode + zparseopts -E -D -a opts -help -slow v+=chmod_opts + if [[ $# > 1 || -n "${opts[(r)--help]}" ]]; then + cat <<EOF +Usage: resetperms [-v] [--help] [--slow] [target] + + target is the file or directory to change permissions on. If omitted, + the current directory is taken to be the target. + + -v enables verbose output (may be supplied multiple times) + + --slow will use a slower but more robust mode, which is effective if + directories themselves have permissions that forbid you from + traversing them. + +EOF + exit_status=$(( $# > 1 )) + return $exit_status + fi + + if [[ $# -eq 0 ]]; then + target="." + else + target="$1" + fi + if [[ -n ${opts[(r)--slow]} ]]; then use_slow=true; else use_slow=false; fi + + # Because this requires confirmation, bail in noninteractive shells + if [[ ! -o interactive ]]; then + echo "resetperms: cannot run in noninteractive shell" + return 1 + fi + + echo "Fixing perms on $target?" + printf '%s' "Proceed? (y|n) " + read confirm + if [[ "$confirm" != y ]]; then + # User aborted + return 1 + fi + + # This xargs form is faster than -exec chmod <N> {} \; but will encounter + # issues if the directories themselves have permissions such that you can't + # recurse in to them. If that happens, just rerun this a few times. + exit_status=0; + if [[ $use_slow == true ]]; then + # Process directories first so non-traversable ones are fixed as we go + find "$target" -type d ! -perm 755 -exec chmod $chmod_opts 755 {} \; + if [[ $? -ne 0 ]]; then exit_status=$?; fi + find "$target" -type f ! -perm 644 -exec chmod $chmod_opts 644 {} \; + if [[ $? -ne 0 ]]; then exit_status=$?; fi + else + find "$target" -type d ! -perm 755 -print0 | xargs -0 chmod $chmod_opts 755 + if [[ $? -ne 0 ]]; then exit_status=$?; fi + find "$target" -type f ! -perm 644 -print0 | xargs -0 chmod $chmod_opts 644 + if [[ $? -ne 0 ]]; then exit_status=$?; fi + fi + echo "Complete" + return $exit_status +} + +function fixperms { + print -ru2 "fixperms has been deprecated. Use resetperms instead" + return 1 +} |