From ceadf84262ed4c048b17ae167f5ea9ed96d4d875 Mon Sep 17 00:00:00 2001 From: Steven Baltakatei Sandoval Date: Thu, 2 Jul 2020 17:53:03 +0000 Subject: [PATCH] feat(unitproc):Add checkTar. Makes sure a tar exists Added checkTar() Bash function template. It checks to see if file arg1 is a valid tar archive. If file exists but `tar` doesn't recognize it as valid, then rename it with a nonce and create an empty tar in its place. --- unitproc/bktemp-checkTar | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 unitproc/bktemp-checkTar diff --git a/unitproc/bktemp-checkTar b/unitproc/bktemp-checkTar new file mode 100644 index 0000000..2485f3a --- /dev/null +++ b/unitproc/bktemp-checkTar @@ -0,0 +1,35 @@ +#!/bin/bash + +# Desc: Checks that a valid tar archive exists, creates one otherwise + +# Yell, Die, Try Three-Fingered Claw technique +# Ref/Attrib: https://stackoverflow.com/a/25515370 +yell() { echo "$0: $*" >&2; } +die() { yell "$*"; exit 111; } +try() { "$@" || die "cannot $*"; } + +checkTar() { + # Desc: Checks that a valid tar archive exists, creates one otherwise + # Usage: checkTar [ path ] + # Input: arg1: path of tar archive + # Output: exit code 0: tar exists and is valid + # Depends: try, tar, date + local PATH_TAR="$1" + + # Check if file is a valid tar archive + if tar --list --file="$PATH_TAR" 1>/dev/null 2>&1; then + ## T1: return success + return 0; + else + ## F1: Check if file exists + if [[ -f "$PATH_TAR" ]]; then + ### T: Rename file + try mv "$PATH_TAR" "$PATH_TAR""--broken--""$(date +%Y%m%dT%H%M%S)"; + else + ### F: - + : + fi + ## F2: Create tar archive + try tar --create --file="$PATH_TAR" --files-from=/dev/null; + fi +} # checks if arg1 is tar; creates one otherwise -- 2.30.2