changed positional argument behavior: directory name + sequence; added checksum verification
This commit is contained in:
parent
0805dba2a2
commit
1419fc7fac
|
|
@ -1,11 +1,14 @@
|
||||||
#!/bin/bash
|
#!/bin/bash
|
||||||
display_help() {
|
display_help() {
|
||||||
(>&2 echo "Usage: $(basename "$0") [-hv] [-f outfile] filename")
|
(>&2 echo "Usage: $(basename "$0") [-chqv] [-o outfile] indir iseq")
|
||||||
(>&2 echo "UCF tar packer")
|
(>&2 echo "UCF tar packer")
|
||||||
(>&2 echo)
|
(>&2 echo)
|
||||||
(>&2 echo " filename path to arbitrary file with correct sequence number")
|
(>&2 echo " indir path to input directory")
|
||||||
(>&2 echo " -f, --file output file (default: snapshot_XXXX.ucf.tar)")
|
(>&2 echo " iseq sequence number")
|
||||||
|
(>&2 echo " -c, --checksum compares checksums after the archive is created")
|
||||||
(>&2 echo " -h, --help display this help message")
|
(>&2 echo " -h, --help display this help message")
|
||||||
|
(>&2 echo " -o, --outfile output file (default: snapshot_XXXX.ucf.tar)")
|
||||||
|
(>&2 echo " -q, --quicksum same as --checksum, but compares only first bytes")
|
||||||
(>&2 echo " -v, --verbose verbose output")
|
(>&2 echo " -v, --verbose verbose output")
|
||||||
}
|
}
|
||||||
exit_script() {
|
exit_script() {
|
||||||
|
|
@ -21,6 +24,8 @@ if [ $# -eq 0 ]; then
|
||||||
fi
|
fi
|
||||||
fout=""
|
fout=""
|
||||||
verbose=0
|
verbose=0
|
||||||
|
checksum=0
|
||||||
|
quicksum=0
|
||||||
POSITIONAL=()
|
POSITIONAL=()
|
||||||
while [[ $# -gt 0 ]]
|
while [[ $# -gt 0 ]]
|
||||||
do
|
do
|
||||||
|
|
@ -31,7 +36,7 @@ case $key in
|
||||||
exit 0
|
exit 0
|
||||||
shift # past argument
|
shift # past argument
|
||||||
;;
|
;;
|
||||||
-f|--file)
|
-o|--outfile)
|
||||||
fout="$2"
|
fout="$2"
|
||||||
shift # past argument
|
shift # past argument
|
||||||
shift # past value
|
shift # past value
|
||||||
|
|
@ -40,6 +45,14 @@ case $key in
|
||||||
verbose=1
|
verbose=1
|
||||||
shift # past argument
|
shift # past argument
|
||||||
;;
|
;;
|
||||||
|
-c|--checksum)
|
||||||
|
checksum=1
|
||||||
|
shift # past argument
|
||||||
|
;;
|
||||||
|
-q|--quicksum)
|
||||||
|
quicksum=1
|
||||||
|
shift # past argument
|
||||||
|
;;
|
||||||
*) # unknown option
|
*) # unknown option
|
||||||
POSITIONAL+=("$1") # save it in an array for later
|
POSITIONAL+=("$1") # save it in an array for later
|
||||||
shift # past argument
|
shift # past argument
|
||||||
|
|
@ -49,13 +62,8 @@ done
|
||||||
set -- "${POSITIONAL[@]}"
|
set -- "${POSITIONAL[@]}"
|
||||||
|
|
||||||
# Parse input filename
|
# Parse input filename
|
||||||
din=$(dirname $1)
|
din=$1
|
||||||
fin=$(basename $1)
|
seqnum=$(printf %04d $2)
|
||||||
seqnum=$(echo $fin | grep -o '_[0-9]\+')
|
|
||||||
if [ -z "$seqnum" ]; then
|
|
||||||
(>&2 echo "[Error] No sequence number found in input filename.")
|
|
||||||
exit 1;
|
|
||||||
fi
|
|
||||||
|
|
||||||
# Construct output filename if not specified
|
# Construct output filename if not specified
|
||||||
if [ -z "$fout" ]; then
|
if [ -z "$fout" ]; then
|
||||||
|
|
@ -68,12 +76,12 @@ if [ $verbose -eq 1 ]; then
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# Check input files
|
# Check input files
|
||||||
fparam="parameters${seqnum}.asc"
|
fparam="parameters_${seqnum}.asc"
|
||||||
fgrid="grid${seqnum}.bin"
|
fgrid="grid_${seqnum}.bin"
|
||||||
fproc="proc${seqnum}.bin"
|
fproc="proc_${seqnum}.bin"
|
||||||
fpart="particles${seqnum}.bin"
|
fpart="particles_${seqnum}.bin"
|
||||||
fbuvwp="uvwp"
|
fbuvwp="uvwp_"
|
||||||
fbscal="scal"
|
fbscal="scal_"
|
||||||
|
|
||||||
# Check if obligatory files are present
|
# Check if obligatory files are present
|
||||||
if [ ! -s ${din}/${fparam} ]; then
|
if [ ! -s ${din}/${fparam} ]; then
|
||||||
|
|
@ -101,7 +109,7 @@ fi
|
||||||
|
|
||||||
fuvwp=()
|
fuvwp=()
|
||||||
for (( ii=0; ii<$nproc; ii++ )); do
|
for (( ii=0; ii<$nproc; ii++ )); do
|
||||||
ftmp=$(printf uvwp${seqnum}.%05d $ii)
|
ftmp=$(printf ${fbuvwp}${seqnum}.%05d $ii)
|
||||||
if [ ! -s ${din}/${ftmp} ]; then
|
if [ ! -s ${din}/${ftmp} ]; then
|
||||||
(>&2 echo "[Error] File not found or empty: ${din}/${ftmp}")
|
(>&2 echo "[Error] File not found or empty: ${din}/${ftmp}")
|
||||||
exit 1
|
exit 1
|
||||||
|
|
@ -113,7 +121,7 @@ done
|
||||||
if grep -q "scalar" "${din}/${fparam}"; then
|
if grep -q "scalar" "${din}/${fparam}"; then
|
||||||
fscal=()
|
fscal=()
|
||||||
for (( ii=0; ii<$nproc; ii++ )); do
|
for (( ii=0; ii<$nproc; ii++ )); do
|
||||||
ftmp=$(printf scal${seqnum}.%05d $ii)
|
ftmp=$(printf ${fbscal}${seqnum}.%05d $ii)
|
||||||
if [ ! -s ${din}/${ftmp} ]; then
|
if [ ! -s ${din}/${ftmp} ]; then
|
||||||
(>&2 echo "[Error] File not found or empty: ${din}/${ftmp}")
|
(>&2 echo "[Error] File not found or empty: ${din}/${ftmp}")
|
||||||
exit 1
|
exit 1
|
||||||
|
|
@ -136,16 +144,50 @@ if [ $verbose -eq 1 ]; then
|
||||||
fi
|
fi
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
# Create a full file list for the archive
|
||||||
|
flist=(${fparam} ${fgrid} ${fproc} ${fpart} ${fuvwp[@]} ${fscal[@]})
|
||||||
|
|
||||||
# Now tar them and remove seqence number from file names while doing so
|
# Now tar them and remove seqence number from file names while doing so
|
||||||
|
flagtar=""
|
||||||
if [ $verbose -eq 1 ]; then
|
if [ $verbose -eq 1 ]; then
|
||||||
flagtar="-v"
|
flagtar="$flagtar --verbose"
|
||||||
fi
|
fi
|
||||||
trap exit_script SIGINT SIGTERM
|
trap exit_script SIGINT SIGTERM
|
||||||
tar $flagtar --format ustar --transform="flags=r;s|$seqnum||" --directory=${din} -cf ${fout} ${fparam} ${fgrid} ${fproc} ${fpart} ${fuvwp[@]} ${fscal[@]}
|
tar $flagtar --format ustar --transform="flags=r;s|_$seqnum||" --directory=${din} -cf ${fout} ${flist[@]}
|
||||||
tarexit=$?
|
tarexit=$?
|
||||||
# Set exit status accoring to tar
|
# Set exit status accoring to tar
|
||||||
if [ $tarexit -ne 0 ]; then
|
if [ $tarexit -ne 0 ]; then
|
||||||
(>&2 echo "tar failed with exit code $tarexit")
|
(>&2 echo "tar failed with exit code $tarexit")
|
||||||
exit 254
|
exit 254
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
# Compare checksums (CNC32), if flag is set
|
||||||
|
#din="./archive/" #for testing
|
||||||
|
flistx=($(echo ${flist[@]} | sed s/"_$seqnum"/""/g))
|
||||||
|
if [ $checksum -eq 1 ]; then
|
||||||
|
for ii in "${!flistx[@]}"; do
|
||||||
|
if [ $verbose -eq 1 ]; then
|
||||||
|
(>&2 echo "Verifying checksum: ${flist[$ii]}")
|
||||||
|
fi
|
||||||
|
crcori=$(cksum ${din}/${flist[$ii]} | awk '{ print $1, $2 }')
|
||||||
|
crctar=$(tar --to-command='cksum -' -xf ${fout} ${flistx[$ii]} | awk '{ print $1, $2 }')
|
||||||
|
if [ "$crcori" != "$crctar" ]; then
|
||||||
|
(>&2 echo "Verification failed: ${flist[$ii]} ${flistx[$ii]}")
|
||||||
|
exit 5
|
||||||
|
fi
|
||||||
|
done
|
||||||
|
elif [ $quicksum -eq 1 ]; then
|
||||||
|
for ii in "${!flistx[@]}"; do
|
||||||
|
if [ $verbose -eq 1 ]; then
|
||||||
|
(>&2 echo "Verifying partial checksum: ${flist[$ii]}")
|
||||||
|
fi
|
||||||
|
crcori=$(head -c 1M ${din}/${flist[$ii]} | cksum -)
|
||||||
|
crctar=$(tar --to-command='head -c 1M' -xf ${fout} ${flistx[$ii]} | cksum -)
|
||||||
|
if [ "$crcori" != "$crctar" ]; then
|
||||||
|
(>&2 echo "Verification failed: ${flist[$ii]} ${flistx[$ii]}")
|
||||||
|
exit 5
|
||||||
|
fi
|
||||||
|
done
|
||||||
|
fi
|
||||||
|
|
||||||
exit 0
|
exit 0
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue