mvadkert / rpms / beakerlib

Forked from rpms/beakerlib 6 years ago
Clone
Blob Blame History Raw
From 3c1725d768874c069e78e792a4f538386acf5ab4 Mon Sep 17 00:00:00 2001
From: Dalibor Pospisil <dapospis@redhat.com>
Date: Wed, 18 Jun 2014 18:05:46 +0200
Subject: [PATCH 03/14] added rlHash and rlUnhash to unify hashing in various
 places

---
 src/infrastructure.sh | 91 ++++++++++++++++++++++++++++++++++++++++++++++++---
 src/libraries.sh      |  8 ++---
 2 files changed, 91 insertions(+), 8 deletions(-)

diff --git a/src/infrastructure.sh b/src/infrastructure.sh
index cf434d5..8c8d7c7 100644
--- a/src/infrastructure.sh
+++ b/src/infrastructure.sh
@@ -332,6 +332,89 @@ rlAssertMount() {
 }
 
 
+: <<'=cut'
+=pod
+
+=head3 rlHash, rlUnhash
+
+Hashes/Unhashes given string.
+
+    rlHash [--decode] [--algorithm HASH_ALG] --stdin|STRING
+    rlUnhash [--algorithm HASH_ALG] --stdin|STRING
+
+=over
+
+=item --decode
+
+Unhash given string.
+
+=item --algorithm
+
+Use given hash algorithm.
+Currently supported algorithms:
+  base64
+  hex
+
+Defaults to base64.
+Default algorithm can be override using global variable rlHashAlgorithm.
+
+=item --stdin
+
+Get the string from stdin.
+
+=item STRING
+
+String to be hashed/unhashed.
+
+=back
+
+Returns 0 if success.
+
+=head4 Example with --clean:
+
+    hash=rlHash "text"
+
+=cut
+
+rlHash() {
+  local GETOPT=$(getopt -q -o : -l decode,algorithm:,stdin -- "$@"); eval set -- "$GETOPT"
+  local decode=0 alg="$rlHashAlgorithm" stdin=0
+  while true; do
+    case $1 in
+      --)          shift; break ;;
+      --decode)    decode=1 ;;
+      --algorithm) shift; alg="$1" ;;
+      --stdin)     stdin=1 ;;
+    esac
+    shift
+  done
+  [[ "$alg" =~ ^(base64|hex)$ ]] || alg='base64'
+  local text="$1" command
+
+  case $alg in
+    base64)
+      if [[ $decode -eq 0 ]]; then
+        command="base64 --wrap 0 | tr '=' '.'"
+      else
+        command="tr '.' '=' | base64 --decode"
+      fi
+      ;;
+    hex)
+      if [[ $decode -eq 0 ]]; then
+        command="od -A n -t x1 -v | tr -d ' \n\t'"
+      else
+        command="sed 's/\([0-9a-zA-Z]\{2\}\)/\\\x\1/g' | echo -en \"\$(cat -)\""
+      fi
+      ;;
+  esac
+
+  eval "( [[ \$stdin -eq 1 ]] && cat - || echo -n \"\$text\" ) | $command"
+}
+
+rlUnhash() {
+  rlHash --decode "$@"
+}
+
 
 # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 # rlFileBackup
@@ -387,10 +470,10 @@ __INTERNAL_FILEBACKUP_NAMESPACE="rlFileBackupNamespace"
 
 __INTERNAL_FILEBACKUP_SET_PATH_CLEAN() {
   local path="$1"
-  local path_encoded="$( echo $1 | base64 )"
+  local path_encoded="$( rlHash "$1" )"
 
   local namespace="$2"
-  local namespace_encoded="$( echo $2 | base64 | tr "=" "." )"
+  local namespace_encoded="$( rlHash "$2" )"
 
   rlLogDebug "rlFileBackup: Setting up the cleaning lists"
   rlLogDebug "rlFileBackup: Path [$path] Encoded [$path_encoded]"
@@ -408,7 +491,7 @@ __INTERNAL_FILEBACKUP_SET_PATH_CLEAN() {
 
 __INTERNAL_FILEBACKUP_CLEAN_PATHS() {
   local namespace="$1"
-  local namespace_encoded="$( echo $1 | base64 | tr "=" "." )"
+  local namespace_encoded="$( rlHash "$1" )"
 
   rlLogDebug "rlFileRestore: Fetching clean-up lists for namespace: [$namespace] (encoded as [$namespace_encoded])"
 
@@ -419,7 +502,7 @@ __INTERNAL_FILEBACKUP_CLEAN_PATHS() {
   local path
   for path in $PATHS
   do
-    local path_decoded="$( echo $path | base64 -d )"
+    local path_decoded="$( rlUnhash "$path" )"
     if rm -rf "$path_decoded";
     then
       rlLogDebug "rlFileRestore: Cleaning $path_decoded successful"
diff --git a/src/libraries.sh b/src/libraries.sh
index 7dfd736..667b3da 100644
--- a/src/libraries.sh
+++ b/src/libraries.sh
@@ -271,8 +271,8 @@ rlImport() {
     local COMPONENT=$( echo $PROCESSING | cut -d '/' -f 1 )
     local LIBRARY=$( echo $PROCESSING | cut -d '/' -f 2 )
 
-    local COMPONENT_hash=$( echo -n "$COMPONENT" | od -A n -t x1 -v | tr -d ' \n\t' )
-    local LIBRARY_hash=$( echo -n "$LIBRARY" | od -A n -t x1 -v | tr -d ' \n\t' )
+    local COMPONENT_hash=$( rlHash --algorithm hex "$COMPONENT" )
+    local LIBRARY_hash=$( rlHash --algorithm hex "$LIBRARY" )
     local LOCATIONS_varname="__INTERNAL_LIBRARY_LOCATIONS_C${COMPONENT_hash}_L${LIBRARY_hash}"
     local IMPORTS_varname="__INTERNAL_LIBRARY_IMPORTS_C${COMPONENT_hash}_L${LIBRARY_hash}"
 
@@ -329,8 +329,8 @@ rlImport() {
   do
     local COMPONENT=$( echo $library | cut -d '/' -f 1 )
     local LIBRARY=$( echo $library | cut -d '/' -f 2 )
-    local COMPONENT_hash=$( echo -n "$COMPONENT" | od -A n -t x1 -v | tr -d ' \n\t' )
-    local LIBRARY_hash=$( echo -n "$LIBRARY" | od -A n -t x1 -v | tr -d ' \n\t' )
+    local COMPONENT_hash=$( rlHash --algorithm hex "$COMPONENT" )
+    local LIBRARY_hash=$( rlHash --algorithm hex "$LIBRARY" )
     local LOCATIONS_varname="__INTERNAL_LIBRARY_LOCATIONS_C${COMPONENT_hash}_L${LIBRARY_hash}"
     local IMPORTS_varname="__INTERNAL_LIBRARY_IMPORTS_C${COMPONENT_hash}_L${LIBRARY_hash}"
     [ "${!IMPORTS_varname}" != "LOC" ] && {
-- 
1.9.3