2ab67382e7b6892a745f02ec4c8f84e6ac632204

Author
Rory Hardy <gneatgeek@gmail.com>
Committer
Marc Cornellà <marc.cornella@live.com>
Date

Message

Added perms plugin (#1752)

* Added perms plugin

* HTTPS -> HTTP for comment URL

* Added a README

* Updated TOF comment

* Pulled in updates by apjanke with minor tweaks

Diff

 1diff --git a/plugins/perms/README.md b/plugins/perms/README.md
 2new file mode 100644
 3index 0000000000000000000000000000000000000000..873c21d427069371870c2475348d33eec91ba1c6
 4--- /dev/null
 5+++ b/plugins/perms/README.md
 6@@ -0,0 +1,9 @@
 7+## Perms
 8+
 9+Plugin to handle some unix filesystem permissions quickly
10+
11+### Usage
12+
13+* `set755` recursively sets all directories located within the current working directory and sub directories to octal 755.
14+* `set644` recursively sets all files located within the current working directory and sub directories to octal 644.
15+* `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.
16diff --git a/plugins/perms/perms.plugin.zsh b/plugins/perms/perms.plugin.zsh
17new file mode 100644
18index 0000000000000000000000000000000000000000..81f56d577f7812c103bb383b8c8574b0a2f82607
19--- /dev/null
20+++ b/plugins/perms/perms.plugin.zsh
21@@ -0,0 +1,78 @@
22+# Some useful commands for setting permissions.
23+#
24+# Rory Hardy [GneatGeek]
25+# Andrew Janke [apjanke]
26+
27+### Aliases
28+
29+# Set all files' permissions to 644 recursively in a directory
30+alias set644='find . -type f -print0 | xargs -0 chmod 644'
31+
32+# Set all directories' permissions to 755 recursively in a directory
33+alias set755='find . -type d -print0 | xargs -0 chmod 755'
34+
35+### Functions
36+
37+# fixperms - fix permissions on files and directories, with confirmation
38+# Returns 0 on success, nonzero if any errors occurred
39+fixperms () {
40+  local opts confirm target exit_status chmod_opts use_slow_mode
41+  zparseopts -E -D -a opts -help -slow v+=chmod_opts
42+  if [[ $# > 1 || -n "${opts[(r)--help]}" ]]; then
43+    cat <<EOF
44+Usage: fixperms [-v] [--help] [--slow] [target]
45+
46+  target  is the file or directory to change permissions on. If omitted,
47+          the current directory is taken to be the target.
48+
49+  -v      enables verbose output (may be supplied multiple times)
50+
51+  --slow  will use a slower but more robust mode, which is effective if
52+          directories themselves have permissions that forbid you from
53+          traversing them.
54+
55+EOF
56+    exit_status=$(( $# > 1 ))
57+    return $exit_status
58+  fi
59+
60+  if [[ $# == 0 ]]; then
61+    target="."
62+  else
63+    target="$1"
64+  fi
65+  if [[ -n ${opts[(r)--slow]} ]]; then use_slow=true; else use_slow=false; fi
66+
67+  # Because this requires confirmation, bail in noninteractive shells
68+  if [[ ! -o interactive ]]; then
69+    echo "fixperms: cannot run in noninteractive shell"
70+    return 1
71+  fi
72+
73+  echo "Fixing perms on $target?"
74+  printf '%s' "Proceed? (y|n) "
75+  read confirm
76+  if [[ "$confirm" != y ]]; then
77+    # User aborted
78+    return 1
79+  fi
80+
81+  # This xargs form is faster than -exec chmod <N> {} \; but will encounter
82+  # issues if the directories themselves have permissions such that you can't
83+  # recurse in to them. If that happens, just rerun this a few times.
84+  exit_status=0;
85+  if [[ $use_slow == true ]]; then
86+    # Process directories first so non-traversable ones are fixed as we go
87+    find "$target" -type d -exec chmod $chmod_opts 755 {} \;
88+    if [[ $? != 0 ]]; then exit_status=$?; fi
89+    find "$target" -type f -exec chmod $chmod_opts 644 {} \;
90+    if [[ $? != 0 ]]; then exit_status=$?; fi
91+  else
92+    find "$target" -type d -print0 | xargs -0 chmod $chmod_opts 755
93+    if [[ $? != 0 ]]; then exit_status=$?; fi
94+    find "$target" -type f -print0 | xargs -0 chmod $chmod_opts 644
95+    if [[ $? != 0 ]]; then exit_status=$?; fi
96+  fi
97+  echo "Complete"
98+  return $exit_status
99+}