feat(remote): add file cache for remote

This commit is contained in:
zawz 2022-01-21 17:01:55 +01:00
parent dbe55ab7c0
commit 1eb7a078aa
3 changed files with 24 additions and 0 deletions

View file

@ -1,6 +1,10 @@
#!/bin/sh
## Cache functions
get_filecache() {
echo "$cachepath/$(filehash).tar.gpg"
}
clear_cache() {
rm "$cachepath"/*

View file

@ -35,6 +35,9 @@ ZPASS_UNK_OP_CALL=${ZPASS_UNK_OP_CALL-copy}
ZPASS_RAND_LEN=${ZPASS_RAND_LEN-20}
ZPASS_RAND_SET=${ZPASS_RAND_SET-'a-zA-Z0-9\!-.'}
RES_OPTIONS=${RES_OPTIONS-'retrans:1 retry:1 timeout:1 attempts:1'}
export RES_OPTIONS
# datapath resolution
# remove tildes
datapath="${datapath#\~/}"

View file

@ -38,6 +38,11 @@ upload() {
put "$2" "$3"
EOF
esac
[ $? -eq 0 ] || {
echo "ERROR: failed to upload" >&2
return 1
}
cp "$2" "$(get_filecache)"
}
# $1 = protocol, $2 = remote file , $3 = local file
@ -50,6 +55,18 @@ get "$2" "$3"
EOF
;;
esac
if [ $? -eq 0 ] ; then
# could download no problem
cached_file=$(get_filecache)
# copy only if different
diff "$3" "$cached_file" >/dev/null 2>&1 || cp "$3" "$cached_file"
return 0
else
# could not download: try cache
[ -f "$3" ] || return $?
echo "WARN: failed to download archive, using cache" >&2
cp "$(get_filecache)" "$3"
fi
}
# $1 = protocol