compfix.zsh 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. # Handle completions insecurities (i.e., completion-dependent directories with
  2. # insecure ownership or permissions) by:
  3. #
  4. # * Human-readably notifying the user of these insecurities.
  5. # * Moving away all existing completion caches to a temporary directory. Since
  6. # any of these caches may have been generated from insecure directories, they
  7. # are all suspect now. Failing to do so typically causes subsequent compinit()
  8. # calls to fail with "command not found: compdef" errors. (That's bad.)
  9. function handle_completion_insecurities() {
  10. # List of the absolute paths of all unique insecure directories, split on
  11. # newline from compaudit()'s output resembling:
  12. #
  13. # There are insecure directories:
  14. # /usr/share/zsh/site-functions
  15. # /usr/share/zsh/5.0.6/functions
  16. # /usr/share/zsh
  17. # /usr/share/zsh/5.0.6
  18. #
  19. # Since the ignorable first line is printed to stderr and thus not captured,
  20. # stderr is squelched to prevent this output from leaking to the user.
  21. local -aU insecure_dirs
  22. insecure_dirs=( ${(f@):-"$(compaudit 2>/dev/null)"} )
  23. # If no such directories exist, get us out of here.
  24. if (( ! ${#insecure_dirs} )); then
  25. print "[oh-my-zsh] No insecure completion-dependent directories detected."
  26. return
  27. fi
  28. # List ownership and permissions of all insecure directories.
  29. print "[oh-my-zsh] Insecure completion-dependent directories detected:"
  30. ls -ld "${(@)insecure_dirs}"
  31. print "[oh-my-zsh] For safety, completions will be disabled until you manually fix all"
  32. print "[oh-my-zsh] insecure directory permissions and ownership and restart oh-my-zsh."
  33. print "[oh-my-zsh] See the above list for directories with group or other writability.\n"
  34. # Locally enable the "NULL_GLOB" option, thus removing unmatched filename
  35. # globs from argument lists *AND* printing no warning when doing so. Failing
  36. # to do so prints an unreadable warning if no completion caches exist below.
  37. setopt local_options null_glob
  38. # List of the absolute paths of all unique existing completion caches.
  39. local -aU zcompdump_files
  40. zcompdump_files=( "${ZSH_COMPDUMP}"(.) "${ZDOTDIR:-${HOME}}"/.zcompdump* )
  41. # Move such caches to a temporary directory.
  42. if (( ${#zcompdump_files} )); then
  43. # Absolute path of the directory to which such files will be moved.
  44. local ZSH_ZCOMPDUMP_BAD_DIR="${ZSH_CACHE_DIR}/zcompdump-bad"
  45. # List such files first.
  46. print "[oh-my-zsh] Insecure completion caches also detected:"
  47. ls -l "${(@)zcompdump_files}"
  48. # For safety, move rather than permanently remove such files.
  49. print "[oh-my-zsh] Moving to \"${ZSH_ZCOMPDUMP_BAD_DIR}/\"...\n"
  50. mkdir -p "${ZSH_ZCOMPDUMP_BAD_DIR}"
  51. mv "${(@)zcompdump_files}" "${ZSH_ZCOMPDUMP_BAD_DIR}/"
  52. fi
  53. }