encfs_mount.sh 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. #!/usr/bin/env bash
  2. # Decrypts and mounts EncFS directory and opens in file manager.
  3. # This does not support mounting multiple dirs at once. Feel free to improve
  4. # though.
  5. #
  6. # Requirements:
  7. # - sh/bash/ksh/zsh etc.
  8. # - encfs
  9. # - sudo or doas
  10. #
  11. # Usage:
  12. # You can run it with a directory:
  13. # $ this_script.sh ~/somesecretoremptydir
  14. # Or alternatively, put it on ~/.bash_aliases or "source" it:
  15. # $ . this_script.sh
  16. # or
  17. # $ source this_script.sh
  18. # Either of the above will add a "encfs_mount" alias. Then:
  19. # $ encfs_mount /path/to/somedir
  20. # Never manually change files in this directory. Only put files in the mounted
  21. # directory (e.g. /media/username/EncFS). If this encrypted directory does not
  22. # exist, EncFS will prompt you to create it. Don't forget to unmount when done
  23. # accessing files. Either use the eject button on your file manager sidebar
  24. # (you should see it if your system is configured) or run:
  25. # $ fusermount -u /media/username/EncFS
  26. # or
  27. # $ umount /media/username/EncFS
  28. #
  29. # Tested with: FreeBSD sh+bash+zsh; Void Linux sh+bash.
  30. # License: CC0
  31. # Compatibility note: "function" omitted, brackets added for sh.
  32. _func_encfs_mount() {
  33. if [ -z "$1" ]; then echo "error: provide an empty dir or already encrypted dir"; return 1; fi
  34. local d="/media/$USER/EncFS"
  35. # Compatibility note: '>/dev/null 2>&1' works with sh to silence any output.
  36. if mount | grep /media/user/EncFS >/dev/null 2>&1; then
  37. read -r -p "Another directory is mounted on $d. Do you want to unmount? [y/N] " answer
  38. # Compatibility note: explicitly used || instead of ~ for sh
  39. if [ "$answer" == 'y' ] || [ "$answer" == 'Y' ]; then
  40. fusermount -u "$d" >/dev/null 2>&1 || umount "$d"
  41. else
  42. return 3
  43. fi
  44. fi
  45. local subin=sudo
  46. type doas >/dev/null 2>&1 && subin=doas
  47. if ! type encfs >/dev/null 2>&1; then echo "error: encfs not installed"; return 2; fi
  48. if [ ! -d "$d" ]; then
  49. if [ -w "$(dirname "$d")" ]; then
  50. # Since /media/$USER is writable, we can just make a directory
  51. mkdir -p "$d"
  52. else
  53. # Create the /media/$USER dir only, so that it can be chowned before
  54. # creating the final dir (and hence automatically owning final dir)
  55. $subin mkdir -p "$(dirname "$d")"
  56. # So that user can create files without permission issues
  57. $subin chown "$USER" "$(dirname "$d")"
  58. mkdir -p "$d"
  59. fi
  60. fi
  61. if [ -n "$DISPLAY" ] && ( type xdg-open >/dev/null 2>&1 || type gio >/dev/null 2>&1 ); then
  62. encfs "$1" "$d" && echo -e "Unencrypted files mounted on $d"
  63. ( xdg-open "$d" >/dev/null 2>&1 || gio open "$d" >/dev/null 2>&1 ) && echo -e "Don't forget to unmount $d after use"'!! '"Either unmount 'EncFS' from your file manager sidebar or run 'fusermount -u $d' or 'umount $d'"
  64. else
  65. echo "will be mounting on $d... Ctrl+C later to unmount..."
  66. encfs -f "$1" "$d"
  67. fi
  68. }
  69. # Compatibility note: Only function doesn't make it executable on sh as bash
  70. # does, so code is separated on a function and it is called from this alias and
  71. # it works.
  72. alias encfs_mount='_func_encfs_mount'
  73. # When run directly passing an argument, without "source"-ing it first.
  74. if [ -n "$1" ]; then
  75. _func_encfs_mount "$1"
  76. fi