2485f3a5614d8ed6593f21ead3a833983e8a6083
[BK-2020-03.git] / unitproc / bktemp-checkTar
1 #!/bin/bash
2
3 # Desc: Checks that a valid tar archive exists, creates one otherwise
4
5 # Yell, Die, Try Three-Fingered Claw technique
6 # Ref/Attrib: https://stackoverflow.com/a/25515370
7 yell() { echo "$0: $*" >&2; }
8 die() { yell "$*"; exit 111; }
9 try() { "$@" || die "cannot $*"; }
10
11 checkTar() {
12 # Desc: Checks that a valid tar archive exists, creates one otherwise
13 # Usage: checkTar [ path ]
14 # Input: arg1: path of tar archive
15 # Output: exit code 0: tar exists and is valid
16 # Depends: try, tar, date
17 local PATH_TAR="$1"
18
19 # Check if file is a valid tar archive
20 if tar --list --file="$PATH_TAR" 1>/dev/null 2>&1; then
21 ## T1: return success
22 return 0;
23 else
24 ## F1: Check if file exists
25 if [[ -f "$PATH_TAR" ]]; then
26 ### T: Rename file
27 try mv "$PATH_TAR" "$PATH_TAR""--broken--""$(date +%Y%m%dT%H%M%S)";
28 else
29 ### F: -
30 :
31 fi
32 ## F2: Create tar archive
33 try tar --create --file="$PATH_TAR" --files-from=/dev/null;
34 fi
35 } # checks if arg1 is tar; creates one otherwise