adopt everywhere -z test to check when variables are empty

check works both for empty ("") and non-existing vars and is a fix
for regression #398 to work on older Zsh versions. It is normalized
through all tomb's code.
This commit is contained in:
Jaromil 2020-12-15 18:22:38 +01:00
parent 5199bef4a6
commit b0de6e07b2

50
tomb
View File

@ -175,7 +175,7 @@ _cat() { local -a _arr;
_is_found() { _is_found() {
# returns 0 if binary is found in path # returns 0 if binary is found in path
[[ "$1" = "" ]] && return 1 [[ -z $1 ]] && return 1
command -v "$1" 1>/dev/null 2>/dev/null command -v "$1" 1>/dev/null 2>/dev/null
return $? return $?
} }
@ -209,9 +209,9 @@ _whoami() {
# Set username from UID or environment # Set username from UID or environment
_USER=$SUDO_USER _USER=$SUDO_USER
[[ "$_USER" = "" ]] && { _USER=$USERNAME } [[ -z $_USER ]] && { _USER=$USERNAME }
[[ "$_USER" = "" ]] && { _USER=$(id -u) } [[ -z $_USER ]] && { _USER=$(id -u) }
[[ "$_USER" = "" ]] && { [[ -z $_USER ]] && {
_failure "Failing to identify the user who is calling us" } _failure "Failing to identify the user who is calling us" }
# Get GID from option -G or the environment # Get GID from option -G or the environment
@ -390,7 +390,7 @@ ask_password() {
pass_asked=0 pass_asked=0
if [[ "$WAYLAND_DISPLAY" ]]; then if [[ ! -z $WAYLAND_DISPLAY ]]; then
_verbose "wayland display detected" _verbose "wayland display detected"
_is_found "pinentry-gnome3" && { _is_found "pinentry-gnome3" && {
_verbose "using pinentry-gnome3 on wayland" _verbose "using pinentry-gnome3 on wayland"
@ -398,7 +398,7 @@ ask_password() {
pass_asked=1 pass_asked=1
} }
fi fi
if [[ "$DISPLAY" ]] && [[ "$pass_asked" == 0 ]]; then if [[ ! -z $DISPLAY ]] && [[ -z $pass_asked ]]; then
_verbose "X11 display detected" _verbose "X11 display detected"
if _is_found "pinentry-gtk-2"; then if _is_found "pinentry-gtk-2"; then
_verbose "using pinentry-gtk2" _verbose "using pinentry-gtk2"
@ -422,7 +422,7 @@ ask_password() {
pass_asked=1 pass_asked=1
fi fi
fi fi
if [[ "$pass_asked" == 0 ]]; then if [[ -z $pass_asked ]]; then
_verbose "no display detected" _verbose "no display detected"
_is_found "pinentry-curses" && { _is_found "pinentry-curses" && {
_verbose "using pinentry-curses with no display" _verbose "using pinentry-curses with no display"
@ -431,7 +431,7 @@ ask_password() {
} }
fi fi
[[ "$pass_asked" == 0 ]] && [[ -z $pass_asked ]] &&
_failure "Cannot find any pinentry-curses and no DISPLAY detected." _failure "Cannot find any pinentry-curses and no DISPLAY detected."
# parse the pinentry output # parse the pinentry output
@ -445,7 +445,7 @@ ask_password() {
[[ "$i" =~ "^D .*" ]] && password="${i##D }"; [[ "$i" =~ "^D .*" ]] && password="${i##D }";
done done
[[ ! -z "$pinentry_error" ]] && [[ "$password" = "" ]] && { [[ ! -z $pinentry_error ]] && [[ -z $password ]] && {
_warning "Pinentry error: ::1 error::" ${pinentry_error} _warning "Pinentry error: ::1 error::" ${pinentry_error}
print "canceled" print "canceled"
return 1 return 1
@ -457,7 +457,7 @@ ask_password() {
password=$(sphinx_get_password "$password") password=$(sphinx_get_password "$password")
fi fi
[[ "$password" = "" ]] && { [[ -z $password ]] && {
_warning "Empty password" _warning "Empty password"
print "empty" print "empty"
return 1 return 1
@ -532,7 +532,7 @@ is_valid_tomb() {
_verbose "is_valid_tomb ::1 tomb file::" $1 _verbose "is_valid_tomb ::1 tomb file::" $1
# First argument must be the path to a tomb # First argument must be the path to a tomb
[[ $1 ]] || _failure "Tomb file is missing from arguments." [[ ! -z $1 ]] || _failure "Tomb file is missing from arguments."
local _fail=0 local _fail=0
# Tomb file must be a readable, writable, non-empty regular file. # Tomb file must be a readable, writable, non-empty regular file.
@ -580,7 +580,7 @@ is_valid_tomb() {
# hidden files (starting with a dot) and have no extension (only # hidden files (starting with a dot) and have no extension (only
# one dot in string) # one dot in string)
TOMBNAME=${TOMBNAME:-${TOMBFILE}} TOMBNAME=${TOMBNAME:-${TOMBFILE}}
[[ "$TOMBNAME" = "" ]] && [[ -z $TOMBNAME ]] &&
_failure "Tomb won't work without a TOMBNAME." _failure "Tomb won't work without a TOMBNAME."
# checks if Tomb already mounted (or we cannot alter it) # checks if Tomb already mounted (or we cannot alter it)
@ -1060,7 +1060,7 @@ _load_key() {
_verbose "load_key: ::1 key::" $TOMBKEYFILE _verbose "load_key: ::1 key::" $TOMBKEYFILE
[[ "$TOMBKEY" = "" ]] && { [[ -z $TOMBKEY ]] && {
# something went wrong, there is no key to load # something went wrong, there is no key to load
# this occurs especially when piping from stdin and aborted # this occurs especially when piping from stdin and aborted
_failure "Key not found, specify one using -k." _failure "Key not found, specify one using -k."
@ -1212,7 +1212,7 @@ ask_key_password() {
_message "A password is required to use key ::1 key::" $TOMBKEYFILE _message "A password is required to use key ::1 key::" $TOMBKEYFILE
passok=0 passok=0
tombpass="" tombpass=""
if [[ "$1" = "" ]]; then if [[ -z $1 ]]; then
for c in 1 2 3; do for c in 1 2 3; do
if [[ $c == 1 ]]; then if [[ $c == 1 ]]; then
@ -1409,7 +1409,7 @@ gen_key() {
# if sphinx mode is chosen, use the provided input # if sphinx mode is chosen, use the provided input
# as master password to generate the actual password # as master password to generate the actual password
if [[ "$sphx_host_tmp" != "" ]] || [[ "$sphx_user_tmp" != "" ]]; then if [[ ! -z $sphx_host_tmp ]] || [[ ! -z $sphx_user_tmp ]]; then
OPTS[--sphx-user]=$sphx_user_tmp OPTS[--sphx-user]=$sphx_user_tmp
OPTS[--sphx-host]=$sphx_host_tmp OPTS[--sphx-host]=$sphx_host_tmp
unset sphx_user_tmp unset sphx_user_tmp
@ -1586,7 +1586,7 @@ bury_key() {
# optional 2nd arg: the password to use (same as key, internal use) # optional 2nd arg: the password to use (same as key, internal use)
# optional 3rd arg: the key where to save the result (- for stdout) # optional 3rd arg: the key where to save the result (- for stdout)
exhume_key() { exhume_key() {
[[ "$1" = "" ]] && { [[ -z $1 ]] && {
_failure "Exhume failed, no image specified" } _failure "Exhume failed, no image specified" }
local imagefile="$1" # The image file where to look for the key local imagefile="$1" # The image file where to look for the key
@ -1713,9 +1713,9 @@ cloakify_key() {
# mandatory 2nd arg: the cipher to use # mandatory 2nd arg: the cipher to use
# optional 3rd arg: the key where to save the result (none for stdout) # optional 3rd arg: the key where to save the result (none for stdout)
decloakify_key() { decloakify_key() {
[[ "$1" = "" ]] && { [[ -z $1 ]] && {
_failure "Uncloak failed, no text file specified" } _failure "Uncloak failed, no text file specified" }
[[ "$2" = "" ]] && { [[ -z $2 ]] && {
_failure "Uncloak failed, no cipher file specified" } _failure "Uncloak failed, no cipher file specified" }
local textfile="$1" # The text file where to look for the key local textfile="$1" # The text file where to look for the key
@ -1820,7 +1820,7 @@ dig_tomb() {
_message "Commanded to dig tomb ::1 tomb path::" $tombpath _message "Commanded to dig tomb ::1 tomb path::" $tombpath
[[ $1 ]] || _failure "Missing path to tomb" [[ ! -z $1 ]] || _failure "Missing path to tomb"
[[ -n "$tombsize" ]] || _failure "Size argument missing, use -s" [[ -n "$tombsize" ]] || _failure "Size argument missing, use -s"
[[ $tombsize == <-> ]] || _failure "Size must be an integer (mebibytes)" [[ $tombsize == <-> ]] || _failure "Size must be an integer (mebibytes)"
[[ $tombsize -ge 10 ]] || _failure "Tombs can't be smaller than 10 mebibytes" [[ $tombsize -ge 10 ]] || _failure "Tombs can't be smaller than 10 mebibytes"
@ -2147,7 +2147,7 @@ _update_control_file() {
# stdin = contents # stdin = contents
# $1 = path to control file # $1 = path to control file
# $2 = contents # $2 = contents
[[ "$2" = "" ]] && return 1 [[ -z $2 ]] && return 1
_sudo touch "$1" _sudo touch "$1"
_sudo chown ${_UID}:${_GID} "$1" _sudo chown ${_UID}:${_GID} "$1"
print "$2" > "$1" print "$2" > "$1"
@ -2169,7 +2169,7 @@ mount_tomb() {
_load_key # Try loading new key from option -k and set TOMBKEYFILE _load_key # Try loading new key from option -k and set TOMBKEYFILE
tombmount="$2" tombmount="$2"
[[ "$tombmount" = "" ]] && { [[ -z $tombmount ]] && {
tombmount=/media/$TOMBNAME tombmount=/media/$TOMBNAME
[[ -d /media ]] || { # no /media found, adopting /run/media/$USER (udisks2 compat) [[ -d /media ]] || { # no /media found, adopting /run/media/$USER (udisks2 compat)
tombmount=/run/media/$_USER/$TOMBNAME tombmount=/run/media/$_USER/$TOMBNAME
@ -2352,8 +2352,8 @@ exec_safe_bind_hooks() {
for h in ${(f)_bindhooks}; do for h in ${(f)_bindhooks}; do
s="${h[(w)1]}" s="${h[(w)1]}"
d="${h[(w)2]}" d="${h[(w)2]}"
[[ "$s" = "" ]] && { _warning "bind-hooks file is broken"; return 1 } [[ -z $s ]] && { _warning "bind-hooks file is broken"; return 1 }
[[ "$d" = "" ]] && { _warning "bind-hooks file is broken"; return 1 } [[ -z $d ]] && { _warning "bind-hooks file is broken"; return 1 }
maps+=($s $d) maps+=($s $d)
_verbose "bind-hook found: $s -> $d" _verbose "bind-hook found: $s -> $d"
done done
@ -2921,7 +2921,7 @@ list_processes() {
found=0 found=0
mounted_tombs=(`list_tomb_mounts $1`) mounted_tombs=(`list_tomb_mounts $1`)
if [[ "${#mounted_tombs}" -gt 0 ]]; then if [[ "${#mounted_tombs}" -gt 0 ]]; then
if [[ "$1" = "" ]]; then if [[ -z $1 ]]; then
_success "Listing processes running inside all open tombs..." _success "Listing processes running inside all open tombs..."
else else
_success "Listing processes running inside tomb '::1 tombname::'..." "$1" _success "Listing processes running inside tomb '::1 tombname::'..." "$1"
@ -3154,7 +3154,7 @@ main() {
# permissions for the calling user and drop privileges # permissions for the calling user and drop privileges
_whoami # Reset _UID, _GID, _TTY _whoami # Reset _UID, _GID, _TTY
[[ "$PARAM" == "" ]] && { [[ -z $PARAM ]] && {
_verbose "Tomb command: ::1 subcommand::" $subcommand _verbose "Tomb command: ::1 subcommand::" $subcommand
} || { } || {
_verbose "Tomb command: ::1 subcommand:: ::2 param::" $subcommand $PARAM _verbose "Tomb command: ::1 subcommand:: ::2 param::" $subcommand $PARAM