From d39b26b36a7079e288dc57611364ef146ce7f0fa Mon Sep 17 00:00:00 2001 From: kaotisk Date: Wed, 1 Feb 2023 03:19:20 +0200 Subject: Exits on failures, added check so we don't produce duplicate references to the same ipfs key --- bin/pack_z_block | 46 +++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 45 insertions(+), 1 deletion(-) (limited to 'bin') diff --git a/bin/pack_z_block b/bin/pack_z_block index 158a2cd..71aa4b7 100755 --- a/bin/pack_z_block +++ b/bin/pack_z_block @@ -37,7 +37,21 @@ usage(){ echo "zblocks directly from IPFS hashes refering to blocks." } + main(){ + # We check firstly if the encapsulated value of the "ipfs" key has already + # appeared in the zchain. + TO_CHECK="$(cat $MESSAGE | jq | grep ipfs | awk '{print $2}' | sed -e 's/"//g;s/,//g')" + enter | jq | grep ipfs | awk '{print $2}' | sed -e 's/"//g;s/,//g' | sort | uniq > tempisalreadythere + while IFS="" read -r p || [ -n "$p" ] + do + if [ "$p" == "$TO_CHECK" ] + then + logit "[ERROR]" "Value $TO_CHECK already mentioned on the zchain" + exit 1 + fi + done < tempisalreadythere + rm tempisalreadythere logit "[INFO]" "We are doing" $ACTION "with content" $MESSAGE # We add it to IPFS @@ -111,10 +125,40 @@ then # or for "offline" use echo $ZBLOCK > $ZLATEST ipfs name publish --key=zchain $ZBLOCK > /dev/null 2>&1 - ipfs files mkdir /zarchive > /dev/null 2>&1 + if [ "$?" -ne 0 ] + then + logit "[ERROR]" "Failed publishing ZBLOCK: $ZBLOCK" + exit 1 + fi + ipfs files ls /zarchive > /dev/null 2>&1 + if [ "$?" -ne 0 ] + then + logit "[WARNING]" "/zarchive does not exist" + ipfs files mkdir /zarchive > /dev/null 2>&1 + if [ "$?" -ne 0 ] + then + logit "[ERROR]" "Could not create /zarchive directory. Aborting." + exit 1 + fi + fi ipfs files cp /zlatest /zarchive/$(date -u +%s)-$(ipfs files stat /zlatest | head -n 1) > /dev/null 2>&1 + if [ "$?" -ne 0 ] + then + logit "[ERROR]" "Could not back up previous /zlatest" + exit 1 + fi ipfs files rm /zlatest > /dev/null 2>&1 + if [ "$?" -ne 0 ] + then + logit "[ERROR]" "Could not remove previous /zlatest" + exit 1 + fi ipfs files cp /ipfs/$ZBLOCK /zlatest > /dev/null 2>&1 + if [ "$?" -ne 0 ] + then + logit "[ERROR]" "Could not copy $ZBLOCK to /zlatest" + exit 1 + fi else usage exit 0 -- cgit v1.2.3