From 1eb7a078aad722c6ab5b711a5320a48c5e1775c2 Mon Sep 17 00:00:00 2001 From: zawz Date: Fri, 21 Jan 2022 17:01:55 +0100 Subject: [PATCH] feat(remote): add file cache for remote --- src/cache.sh | 4 ++++ src/config.sh | 3 +++ src/remote.sh | 17 +++++++++++++++++ 3 files changed, 24 insertions(+) diff --git a/src/cache.sh b/src/cache.sh index 6fdeff1..db4b1c6 100644 --- a/src/cache.sh +++ b/src/cache.sh @@ -1,6 +1,10 @@ #!/bin/sh ## Cache functions +get_filecache() { + echo "$cachepath/$(filehash).tar.gpg" +} + clear_cache() { rm "$cachepath"/* diff --git a/src/config.sh b/src/config.sh index 8d03a93..9895f4c 100644 --- a/src/config.sh +++ b/src/config.sh @@ -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#\~/}" diff --git a/src/remote.sh b/src/remote.sh index 35f687a..6de48a1 100644 --- a/src/remote.sh +++ b/src/remote.sh @@ -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