You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

52 lines
1.6 KiB

#!/bin/bash
# Share data to a running Shrlok instance.
socket_path="/run/shrlok/shr.sock"
usage() {
echo "Usage: $0 [-s SOCKET_PATH] [-o key1=value1,key2=value2,…] TYPE FILE"
echo " -s override shrlok socket path (default: $socket_path)"
echo " -o comma separated "k=v" pairs to add to the shrlok header"
}
header_args=
while getopts "hs:o:" OPTION; do
case $OPTION in
h) usage; exit 0 ;;
s) socket_path="$OPTARG" ;;
o) readarray -d "," -t header_args <<< "$OPTARG" ;;
*) usage; exit 1 ;;
esac
done
shift $(( OPTIND - 1 ))
[ $# != 2 ] && usage && exit 1
share_type="$1"
input_file="$2"
if [ ! -S "$socket_path" ]; then
echo "$socket_path is not a valid Unix socket. Is shrlok running?"
exit 1
fi
header_args_str=""
if [[ -n "$header_args" ]]; then
for item in "${header_args[@]}"; do
readarray -d "=" -t kv <<< "$item"
key="${kv[0]}"
val="$(echo "${kv[1]}" | xargs)" # strip trailing LFs
header_args_str+=",\"$key\":\"$val\""
done
fi
# The first thing sent through the socket is the total packet length, as ASCII
# digits for the convenience of scripts. Build the header before hand to get its
# size, then get the file length and push everything to the socket with socat.
header="{\"type\":\"$share_type\"$header_args_str}"
header_length="${#header}"
file_length="$(wc -c "$input_file" | cut -d' ' -f1)"
full_length="$(( $header_length + 1 + $file_length ))"
cat <(printf "${full_length}\x00${header}\x00") "$input_file" \
| socat - UNIX-CONNECT:"$socket_path" ; echo