2022-09-03 15:57:10 +02:00
|
|
|
#!/bin/bash
|
|
|
|
# Share data to a running Shrlok instance.
|
|
|
|
|
2022-09-03 20:01:59 +02:00
|
|
|
socket_path="/run/shrlok/shr.sock"
|
|
|
|
|
2022-09-03 15:57:10 +02:00
|
|
|
usage() {
|
2022-09-03 20:01:59 +02:00
|
|
|
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"
|
2022-09-03 15:57:10 +02:00
|
|
|
}
|
|
|
|
|
2022-09-03 20:01:59 +02:00
|
|
|
header_args=
|
|
|
|
while getopts "hs:o:" OPTION; do
|
2022-09-03 15:57:10 +02:00
|
|
|
case $OPTION in
|
|
|
|
h) usage; exit 0 ;;
|
|
|
|
s) socket_path="$OPTARG" ;;
|
2022-09-03 20:01:59 +02:00
|
|
|
o) readarray -d "," -t header_args <<< "$OPTARG" ;;
|
2022-09-03 15:57:10 +02:00
|
|
|
*) 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
|
|
|
|
|
2022-09-03 20:01:59 +02:00
|
|
|
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
|
|
|
|
|
2022-09-03 15:57:10 +02:00
|
|
|
# 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.
|
2022-09-03 20:01:59 +02:00
|
|
|
header="{\"type\":\"$share_type\"$header_args_str}"
|
2022-09-03 15:57:10 +02:00
|
|
|
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
|