From 910b70849b38ddcdc00bc33402c6bd0b67e990db Mon Sep 17 00:00:00 2001 From: Alexander Wainwright Date: Mon, 14 Jul 2025 17:48:32 +1000 Subject: [PATCH] Add cardboard function --- alex/fish/functions/cardboard.fish | 93 ++++++++++++++++++++++++++++++ alex/home.nix | 1 + 2 files changed, 94 insertions(+) create mode 100644 alex/fish/functions/cardboard.fish diff --git a/alex/fish/functions/cardboard.fish b/alex/fish/functions/cardboard.fish new file mode 100644 index 0000000..5a0dda3 --- /dev/null +++ b/alex/fish/functions/cardboard.fish @@ -0,0 +1,93 @@ +function cardboard + set -l mnt_option + set -l display_help + + for arg in $argv + switch $arg + case '-h' '--help' + set display_help 1 + case '*' + set mnt_option $arg + end + end + + if test "$display_help" + echo "Usage: cardboard [directory] [-h | --help]" + echo "" + echo "Mounts a tmpfs with read-write permissions for the user calling it and no permissions for anyone else." + echo "Automatically changes the current working directory to the mounted tmpfs directory." + echo "" + echo "Options:" + echo " -h, --help Show this help message and exit" + echo "" + echo "To unmount the tmpfs and return to your home directory, run 'uncardboard'." + echo "To securely delete all files inside the mount directory, run 'shred-cardboard'." + echo "To list the files that would be shredded without actually deleting them, run 'shred-cardboard-dry-run'." + return + end + + set user (whoami) + + if test -n "$mnt_option" + set mnt_dir $mnt_option + else + set mnt_dir "/home/$user/mnt/ramfs" + end + + set -gx CARDBOARD_MOUNT_DIR $mnt_dir + + if mount | grep -q " $mnt_dir " + echo "A tmpfs is already mounted at $mnt_dir." + return + end + + mkdir -p $mnt_dir + + # Mount the tmpfs so its root directory is owned by the current user + sudo mount -t tmpfs -o size=512M,mode=700,uid=(id -u),gid=(id -g) tmpfs $mnt_dir + + cd $mnt_dir + + function uncardboard + if test -z "$CARDBOARD_MOUNT_DIR" + echo "No cardboard mount directory found." + return + end + + if test "$PWD" = "$CARDBOARD_MOUNT_DIR" + cd /home/$user + end + + sudo umount $CARDBOARD_MOUNT_DIR + end + + function shred-cardboard + if test -z "$CARDBOARD_MOUNT_DIR" + echo "No cardboard mount directory found." + return + end + + if test "$PWD" = "$CARDBOARD_MOUNT_DIR" + echo "Please change to a different directory before running shred-cardboard." + return + end + + echo "Shredding all files in $CARDBOARD_MOUNT_DIR..." + find $CARDBOARD_MOUNT_DIR -type f -exec shred -u -v {} \; + echo "All files in $CARDBOARD_MOUNT_DIR have been securely deleted." + end + + function shred-cardboard-dry-run + if test -z "$CARDBOARD_MOUNT_DIR" + echo "No cardboard mount directory found." + return + end + + echo "Files that would be shredded in $CARDBOARD_MOUNT_DIR:" + find $CARDBOARD_MOUNT_DIR -type f + end + + echo "To unmount the tmpfs and return to your home directory, run 'uncardboard'." + echo "To securely delete all files inside the mount directory, run 'shred-cardboard'." + echo "To list the files that would be shredded without actually deleting them, run 'shred-cardboard-dry-run'." +end diff --git a/alex/home.nix b/alex/home.nix index c0c7a60..d83e090 100644 --- a/alex/home.nix +++ b/alex/home.nix @@ -174,6 +174,7 @@ programs.fish = { enable = true; interactiveShellInit = '' + source ~/.config/fish/functions/cardboard.fish source ~/.config/fish/functions/bangbang.fish source ~/.config/fish/functions/borg-h.fish