fix remote file create

This commit is contained in:
zawz 2021-08-12 18:31:15 +02:00
parent 56321f84de
commit b97fdec199
3 changed files with 41 additions and 15 deletions

View file

@ -62,18 +62,50 @@ archive_exec()
# no argument # no argument
create_file() { create_file() {
if [ -f "$file" ] if [ -n "$remote_host" ] ; then
then file="$TMPDIR/zpass_$(filehash)$ZPASS_EXTENSION"
tmpfile=$file
if remote download "$datapath/$ZPASS_FILE$ZPASS_EXTENSION" "$file" >/dev/null 2>&1 ; then
local archive_tmpdir="$TMPDIR/zpass_$(randalnum 20)"
# unpack locally
remote_host= unpack "$archive_tmpdir" || {
rm -rf "$archive_tmpdir" "$file"
return 1
}
# pack and send
pack "$archive_tmpdir" || {
echo "Encryption error" >&2
rm -rf "$archive_tmpdir" "$file"
return 1
}
# cleanup
rm -rf "$archive_tmpdir" "$file"
else
# get key
[ -z "$ZPASS_KEY" ] && {
ZPASS_KEY=$(new_key_with_confirm) || { echo "Cancelled" >&2 && return 100 ; }
}
# create archive
tar -cf - -T /dev/null | encrypt "$ZPASS_KEY" > "$file" || {
echo "Encryption error" >&2
rm -f "$file"
return 1
}
ret=0
remote create "$file" "$datapath/$ZPASS_FILE$ZPASS_EXTENSION" || ret=$?
rm -f "$file"
return $ret
fi
elif [ -f "$file" ] ; then
archive_tmpdir="$TMPDIR/zpass_$(randalnum 20)" archive_tmpdir="$TMPDIR/zpass_$(randalnum 20)"
# pack n repack with no tmp key: create new # pack n repack with no tmp key: create new
unpack "$archive_tmpdir" || return $? unpack "$archive_tmpdir" || return $?
pack "$archive_tmpdir" || { echo "Encryption error" >&2 && return 1 ; } pack "$archive_tmpdir" || { echo "Encryption error" >&2 && return 1 ; }
rm -rf "$archive_tmpdir" rm -rf "$archive_tmpdir"
else else
# if remote: file tmp and try to get file
[ -n "$remote_host" ] && {
file="$TMPDIR/zpass_$(filehash)$ZPASS_EXTENSION"
}
# get key # get key
[ -z "$ZPASS_KEY" ] && { [ -z "$ZPASS_KEY" ] && {
ZPASS_KEY=$(new_key_with_confirm) || { echo "Cancelled" >&2 && return 100 ; } ZPASS_KEY=$(new_key_with_confirm) || { echo "Cancelled" >&2 && return 100 ; }
@ -81,17 +113,9 @@ create_file() {
# create archive # create archive
tar -cf - -T /dev/null | encrypt "$ZPASS_KEY" > "$file" || { tar -cf - -T /dev/null | encrypt "$ZPASS_KEY" > "$file" || {
echo "Encryption error" >&2 echo "Encryption error" >&2
# echo "$tmperr" >&2
rm "$file" rm "$file"
return 1 return 1
} }
# if is remote: create remote file
[ -n "$remote_host" ] && {
ret=0
remote create "$file" "$datapath/$ZPASS_FILE$ZPASS_EXTENSION" || ret=$?
rm -rf "$file" 2>/dev/null
return $ret
}
fi fi
return 0 return 0
} }

View file

@ -19,6 +19,7 @@ decrypt()
# get remote file # get remote file
[ -n "$remote_host" ] && { [ -n "$remote_host" ] && {
file="$TMPDIR/zpass_$(filehash)$ZPASS_EXTENSION" file="$TMPDIR/zpass_$(filehash)$ZPASS_EXTENSION"
tmpfile=$file
remote download "$datapath/$ZPASS_FILE$ZPASS_EXTENSION" "$file" >/dev/null || return $? remote download "$datapath/$ZPASS_FILE$ZPASS_EXTENSION" "$file" >/dev/null || return $?
} }
cat "$file" >/dev/null 2>&1 || { echo "File doesn't exist. Use 'zpass create' to create the file" >&2 && return 1; } # no file cat "$file" >/dev/null 2>&1 || { echo "File doesn't exist. Use 'zpass create' to create the file" >&2 && return 1; } # no file

View file

@ -1,8 +1,9 @@
#!/bin/lxsh #!/bin/lxsh
unset archive_tmpdir tmpfile
_stop() { _stop() {
stty echo stty echo
rm -rf "$archive_tmpdir" rm -rf "$archive_tmpdir" "$tmpfile"
} }
[ "$DEBUG" = true ] && set -x [ "$DEBUG" = true ] && set -x