#!/bin/bash # echo loading bindfs export BFS_MOUNT_DIR=${BFS_MOUNT_DIR:-/mnt/bfs} if [ -v PS1 ]; then alias rbfsu="dir_rebind_user" alias rbfs="dir_rebind" alias bfsu="dir_bind_user" alias bfs="dir_bind" alias bfsum="dir_bind_unmount" alias bfse="bfs_vscode" sudo mkdir $BFS_MOUNT_DIR 2> /dev/null sudo chown $USER:$USER $BFS_MOUNT_DIR fi mounted () { mountpoint "$1" &> /dev/null && echo yes || return 1 } dir_bind_unmount () { local usesudo local mp=$1 [[ ! $(mounted $mp) ]] && echo no mountpoint at $mp && mp=$BFS_MOUNT_DIR/$(basename $mp) [[ ! $(mounted $mp) ]] && echo no mountpoint at $mp either, aborting && return 1 [[ $EUID -ne 0 ]] && usesudo=sudo if $usesudo fusermount -u $mp; then echo unmounted $mp, removing empty mountpoint directory rm -rf $mp else echo error, unable to unmount $mp fi } dir_bind_user () { local usesudo; local dir;local user;local group;local mp if [ $# -lt 3 ]; then echo "minimum 3 args needed to rebind " echo passed were $@ return 1 fi [[ $EUID -ne 0 ]] && usesudo=sudo [[ $(id -u $1 2> /dev/null) ]] || { echo user $1 does not exist can not continue; return 2; } dir=$(realpath $2) mp=$3 [[ $(mounted $mp) ]] && echo something already mounted at $mp, aborting && return 1 if ! $usesudo mkdir -p $mp; then echo unable to make mountpoint aborting; return 2; fi $usesudo chown $1:$1 $mp user=$(stat -c '%u' $dir) group=$(stat -c '%g' $dir) $usesudo bindfs --force-user=$1 --force-group=$1 --create-for-user=$user --create-for-group=$group --chown-ignore --chgrp-ignore $dir $mp [[ $? -gt 0 ]] && echo error in call to bindfs if [[ $(mounted $mp) ]]; then echo $dir has been mounted at $mp for user $1 echo "to unmount use: dir_bind_unmount $mp or bfsum $mp" else echo unable to mount $dir at $mp as user $user fi } dir_bind () { mp=${2:-$BFS_MOUNT_DIR/$(basename $1)} dir_bind_user $USER $1 $mp if [ -v PS1 ]; then echo enter \"u\" when you ready to unmount, otherwise any other key will leave mounted read -n1 ans echo -e "\n" [[ $ans == "u" ]] && dir_bind_unmount $mp fi } dir_rebind () { dir_bind_user $USER $1 $1 } dir_rebind_user () { dir_bind_user $1 $2 $2 } bfs_vscode () { mp=${2:-$BFS_MOUNT_DIR/$(basename $1)} dir_bind_user $USER $1 $mp /opt/bin/vscode $mp if [ -v PS1 ]; then echo when you ready to unmount FIRST close your vscode window then enter \"u\" echo otherwise any other key will leave mounted read -n1 ans echo -e "\n" [[ $ans == "u" ]] && dir_bind_unmount $mp fi } (return 0 2>/dev/null) || dir_bind_user $@