qubes.GetAppmenus 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. #!/bin/bash
  2. # shellcheck disable=SC2016
  3. # send .desktop files from directories persisting across VM restarts, specifically:
  4. # - any directory in case of "full" persistence
  5. # - directories stored on /rw in case of "rw-only" persistence
  6. # - nothing, otherwise
  7. # Reload scripts in /etc/profile.d/, in case they register additional
  8. # directories in XDG_DATA_DIRS and we forgot them
  9. # (e.g. because we are running under sudo).
  10. for i in /etc/profile.d/*.sh ; do
  11. if [ -r "$i" ]; then
  12. # shellcheck disable=SC1090
  13. . "$i" >/dev/null
  14. fi
  15. done
  16. if [ -z "$XDG_DATA_HOME" ]; then
  17. user="$(whoami)"
  18. # In case we are running under sudo, use default-user.
  19. if [ "$user" = "root" ]; then
  20. user="$(qubesdb-read /default-user || echo user)"
  21. fi
  22. home="$(eval echo "~$user")"
  23. XDG_DATA_HOME="$home/.local/share"
  24. fi
  25. if [ -z "$XDG_DATA_DIRS" ]; then
  26. XDG_DATA_DIRS="/usr/local/share/:/usr/share/"
  27. fi
  28. # if read fails for some reason, default to full
  29. persistence=$(qubesdb-read /qubes-vm-persistence || echo full)
  30. rw_devno=$(stat -c %D /rw)
  31. apps_dirs_to_consider=( "$XDG_DATA_HOME" )
  32. old_IFS="$IFS"
  33. IFS=:
  34. # shellcheck disable=SC2206
  35. apps_dirs_to_consider+=( $XDG_DATA_DIRS )
  36. IFS="$old_IFS"
  37. apps_dirs=()
  38. for dir in "${apps_dirs_to_consider[@]}"; do
  39. if [ "$persistence" = "full" ]; then
  40. apps_dirs+=( "$dir/applications" )
  41. elif [ "$persistence" = "rw-only" ] && \
  42. [ "$(stat -c %D "$dir")" = "$rw_devno" ]; then
  43. apps_dirs+=( "$dir/applications" )
  44. fi
  45. done
  46. if [ "${#apps_dirs[@]}" -eq "0" ]; then
  47. # nothing to send, exit early to not let `find` browse the current
  48. # directory
  49. exit 0
  50. fi
  51. find "${apps_dirs[@]}" -name '*.desktop' -print0 2>/dev/null | \
  52. xargs -0 awk '
  53. BEGINFILE { if (ERRNO) nextfile; entry="" }
  54. /^\[/ { if (tolower($0) != "\[desktop entry\]") nextfile }
  55. /^Exec=/ { entry = entry FILENAME ":Exec=qubes-desktop-run " FILENAME "\n"; next }
  56. /^NoDisplay *= *true$/ { entry=""; nextfile }
  57. /=/ { entry = entry FILENAME ":" $0 "\n" }
  58. ENDFILE { print entry }
  59. ' 2> /dev/null