Custom redirection of /dev/null

Renato Alves alves.rjc at gmail.com
Wed Dec 28 07:27:47 CET 2016


While debugging a strange problem with pass I felt the need to log all
output, including what gets sent to /dev/null

The attached patch uses a variable "DEBUG" as "sink".

DEBUG=/tmp/devnull.log pass ...

Will basically collect >/dev/null output into the specified file.


PS: As for the original problem, it turned out to be an incompatibility
of bash's exec -a on my current system. I don't yet know where the bug
is. I'll open another thread if it turns out to be something that needs
to be worked around in pass.

Cheers,
Renato
-------------- next part --------------
From 9bfc57de729bf529fae4fc032f54bc5ff04ceb36 Mon Sep 17 00:00:00 2001
From: Renato Alves <alves.rjc at gmail.com>
Date: Tue, 27 Dec 2016 22:49:19 +0100
Subject: [PATCH] Redirects to /dev/null now go via $DEBUG which defaults to
 /dev/null

This allows debuging any error messages that may be silenced to
/dev/null by calling pass as:

DEBUG=/tmp/devnull.log pass ...
---
 src/password-store.sh | 27 ++++++++++++++-------------
 1 file changed, 14 insertions(+), 13 deletions(-)

diff --git a/src/password-store.sh b/src/password-store.sh
index a5a8002..69295f0 100755
--- a/src/password-store.sh
+++ b/src/password-store.sh
@@ -6,10 +6,11 @@
 umask "${PASSWORD_STORE_UMASK:-077}"
 set -o pipefail
 
+DEBUG="${DEBUG:-/dev/null}"
 GPG_OPTS=( $PASSWORD_STORE_GPG_OPTS "--quiet" "--yes" "--compress-algo=none" "--no-encrypt-to" )
 GPG="gpg"
-export GPG_TTY="${GPG_TTY:-$(tty 2>/dev/null)}"
-which gpg2 &>/dev/null && GPG="gpg2"
+export GPG_TTY="${GPG_TTY:-$(tty 2>>"$DEBUG")}"
+which gpg2 &>>"$DEBUG" && GPG="gpg2"
 [[ -n $GPG_AGENT_INFO || $GPG == "gpg2" ]] && GPG_OPTS+=( "--batch" "--use-agent" )
 
 PREFIX="${PASSWORD_STORE_DIR:-$HOME/.password-store}"
@@ -52,7 +53,7 @@ die() {
 verify_file() {
 	[[ -n $PASSWORD_STORE_SIGNING_KEY ]] || return 0
 	[[ -f $1.sig ]] || die "Signature for $1 does not exist."
-	local fingerprints="$(gpg $PASSWORD_STORE_GPG_OPTS --verify --status-fd=1 "$1.sig" "$1" 2>/dev/null | sed -n 's/\[GNUPG:\] VALIDSIG \([A-F0-9]\{40\}\) .* \([A-F0-9]\{40\}\)$/\1\n\2/p')"
+	local fingerprints="$(gpg $PASSWORD_STORE_GPG_OPTS --verify --status-fd=1 "$1.sig" "$1" 2>>"$DEBUG" | sed -n 's/\[GNUPG:\] VALIDSIG \([A-F0-9]\{40\}\) .* \([A-F0-9]\{40\}\)$/\1\n\2/p')"
 	local fingerprint found=0
 	for fingerprint in $PASSWORD_STORE_SIGNING_KEY; do
 		[[ $fingerprint =~ ^[A-F0-9]{40}$ ]] || continue
@@ -149,8 +150,8 @@ clip() {
 	# variable. Specifically, it cannot store nulls nor (non-trivally) store
 	# trailing new lines.
 	local sleep_argv0="password store sleep on display $DISPLAY"
-	pkill -f "^$sleep_argv0" 2>/dev/null && sleep 0.5
-	local before="$(xclip -o -selection "$X_SELECTION" 2>/dev/null | base64)"
+	pkill -f "^$sleep_argv0" 2>>"$DEBUG" && sleep 0.5
+	local before="$(xclip -o -selection "$X_SELECTION" 2>>"$DEBUG" | base64)"
 	echo -n "$1" | xclip -selection "$X_SELECTION" || die "Error: Could not copy data to the clipboard"
 	(
 		( exec -a "$sleep_argv0" sleep "$CLIP_TIME" )
@@ -164,10 +165,10 @@ clip() {
 		#
 		# Clipboard managers frequently write their history out in plaintext,
 		# so we axe it here:
-		qdbus org.kde.klipper /klipper org.kde.klipper.klipper.clearClipboardHistory &>/dev/null
+		qdbus org.kde.klipper /klipper org.kde.klipper.klipper.clearClipboardHistory &>>"$DEBUG"
 
 		echo "$before" | base64 -d | xclip -selection "$X_SELECTION"
-	) 2>/dev/null & disown
+	) 2>>"$DEBUG" & disown
 	echo "Copied $2 to clipboard. Will clear in $CLIP_TIME seconds."
 }
 tmpdir() {
@@ -202,7 +203,7 @@ tmpdir() {
 GETOPT="getopt"
 SHRED="shred -f -z"
 
-source "$(dirname "$0")/platform/$(uname | cut -d _ -f 1 | tr '[:upper:]' '[:lower:]').sh" 2>/dev/null # PLATFORM_FUNCTION_FILE
+source "$(dirname "$0")/platform/$(uname | cut -d _ -f 1 | tr '[:upper:]' '[:lower:]').sh" 2>>"$DEBUG" # PLATFORM_FUNCTION_FILE
 
 #
 # END platform definable
@@ -297,7 +298,7 @@ cmd_init() {
 			git rm -qr "$gpg_id"
 			git_commit "Deinitialize ${gpg_id}${id_path:+ ($id_path)}."
 		fi
-		rmdir -p "${gpg_id%/*}" 2>/dev/null
+		rmdir -p "${gpg_id%/*}" 2>>"$DEBUG"
 	else
 		mkdir -v -p "$PREFIX/$id_path"
 		printf "%s\n" "$@" > "$gpg_id"
@@ -310,7 +311,7 @@ cmd_init() {
 				signing_keys+=( --default-key $key )
 			done
 			gpg "${GPG_OPTS[@]}" "${signing_keys[@]}" --detach-sign "$gpg_id" || die "Could not sign .gpg_id."
-			key="$(gpg --verify --status-fd=1 "$gpg_id.sig" "$gpg_id" 2>/dev/null | sed -n 's/\[GNUPG:\] VALIDSIG [A-F0-9]\{40\} .* \([A-F0-9]\{40\}\)$/\1/p')"
+			key="$(gpg --verify --status-fd=1 "$gpg_id.sig" "$gpg_id" 2>>"$DEBUG" | sed -n 's/\[GNUPG:\] VALIDSIG [A-F0-9]\{40\} .* \([A-F0-9]\{40\}\)$/\1/p')"
 			[[ -n $key ]] || die "Signing of .gpg_id unsuccessful."
 			git_add_file "$gpg_id.sig" "Signing new GPG id with ${key//[$IFS]/,}."
 		fi
@@ -449,7 +450,7 @@ cmd_edit() {
 	fi
 	${EDITOR:-vi} "$tmp_file"
 	[[ -f $tmp_file ]] || die "New password not saved."
-	$GPG -d -o - "${GPG_OPTS[@]}" "$passfile" 2>/dev/null | diff - "$tmp_file" &>/dev/null && die "Password unchanged."
+	$GPG -d -o - "${GPG_OPTS[@]}" "$passfile" 2>>"$DEBUG" | diff - "$tmp_file" &>>"$DEBUG" && die "Password unchanged."
 	while ! $GPG -e "${GPG_RECIPIENT_ARGS[@]}" -o "$passfile" "${GPG_OPTS[@]}" "$tmp_file"; do
 		yesno "GPG encryption failed. Would you like to try again?"
 	done
@@ -530,7 +531,7 @@ cmd_delete() {
 		git rm -qr "$passfile"
 		git_commit "Remove $path from store."
 	fi
-	rmdir -p "${passfile%/*}" 2>/dev/null
+	rmdir -p "${passfile%/*}" 2>>"$DEBUG"
 }
 
 cmd_copy_move() {
@@ -571,7 +572,7 @@ cmd_copy_move() {
 			git rm -qr "$old_path"
 			git_add_file "$new_path" "Rename ${1} to ${2}."
 		fi
-		rmdir -p "$old_dir" 2>/dev/null
+		rmdir -p "$old_dir" 2>>"$DEBUG"
 	else
 		cp $interactive -r -v "$old_path" "$new_path" || exit 1
 		[[ -e "$new_path" ]] && reencrypt_path "$new_path"
-- 
2.10.0



More information about the Password-Store mailing list