diff --git a/gocrypt.bash b/gocrypt.bash index e6c96f4..ae7dcac 100755 --- a/gocrypt.bash +++ b/gocrypt.bash @@ -6,6 +6,7 @@ readonly gocrypt_dir=".gocrypt" readonly gocrypt_dec_dir="gocrypt" readonly gocrypt_passwd_file="gocrypt-passwd" readonly gocrypt_needs_passphrase_marker=".gocrypt-needs-passphrase" +readonly gocrypt_close_timeout="300" gocrypt_sys_check() { which gocryptfs > /dev/null || gocrypt_die "gocryptfs not found in PATH" @@ -31,6 +32,25 @@ gocrypt_die() { exit 1 } +gocrypt_unique_task_identifier() { + echo "pass-gocrypt-$(sha256sum <<< "$PREFIX" | cut -d ' ' -f 1)" +} + +gocrypt_spawn_close_task() { + which systemd-run > /dev/null || return + which sha256sum > /dev/null || return + + local task_name="$(gocrypt_unique_task_identifier)" + # Cancel any previous task that might be present + systemctl --user stop "$task_name.timer" > /dev/null 2>1 + + # Create a new task + systemd-run --user --on-active=$gocrypt_close_timeout --unit="$task_name" /usr/bin/env bash -c \ + "fusermount -u '$PREFIX'/'$gocrypt_dec_dir' || fusermount -u -z '$PREFIX'/'$gocrypt_dec_dir'" + + echo "Will close the gocryptfs mount after $gocrypt_close_timeout seconds" +} + _cmd_git() { [ -d '.git' ] && cmd_git "$@" } @@ -117,11 +137,16 @@ gocrypt_open() { fi gocryptfs -passfile /dev/stdin "$gocrypt_dir" "$gocrypt_dec_dir" <<< "$gocrypt_passwd" + + gocrypt_spawn_close_task } gocrypt_close() { [ $# -eq 0 ] || gocrypt_die "Unexpected argument" gocrypt_open_check + # Remove the systemd task for closing if present + which systemctl > /dev/null && systemctl --user stop "$(gocrypt_unique_task_identifier).timer" > /dev/null 2>1 + fusermount -u "$gocrypt_dec_dir" || fusermount -u -z "$gocrypt_dec_dir" }