ecc (2201B)
1 #!/bin/sh 2 3 # Copyright (C) 2023, 2024 Vincent Forest (vaplv@posteo.net) 4 # 5 # This program is free software: you can redistribute it and/or modify 6 # it under the terms of the GNU General Public License as published by 7 # the Free Software Foundation, either version 3 of the License, or 8 # (at your option) any later version. 9 # 10 # This program is distributed in the hope that it will be useful, 11 # but WITHOUT ANY WARRANTY; without even the implied warranty of 12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 # GNU General Public License for more details. 14 # 15 # You should have received a copy of the GNU General Public License 16 # along with this program. If not, see <http://www.gnu.org/licenses/>. 17 18 set -e 19 20 ######################################################################## 21 # Helper functions 22 ######################################################################## 23 usage() 24 { 25 >&2 printf 'usage: ecc create file ...\n' 26 >&2 printf ' ecc verify file ...\n' 27 >&2 printf ' ecc repair file ...\n' 28 } 29 30 cleanup() 31 { 32 rm -f "${par2%.*}"*.par2 33 exit "${1:-1}" 34 } 35 36 log() 37 { 38 str="$1" 39 shift 1 40 41 date="$(date +"%b %d %Y %H:%M:%S")" 42 >&2 printf '%s '"${str}" "${date}" "$@" 43 } 44 45 create() 46 { 47 par2="$1" 48 file="$2" 49 50 if [ -e "${par2}" ]; then 51 log 'ecc create error: file already exists %s\n' "${par2}" 52 else 53 trap cleanup INT TERM EXIT 54 if ! par2 create -a "${par2}" "${file}"; then 55 log 'ecc create error: %s\n' "$1" 56 fi 57 trap 1 INT TERM EXIT # Restore default 58 fi 59 } 60 61 verify() 62 { 63 par2="$1" 64 65 if ! par2 verify "${par2}"; then 66 log 'ecc verify error: %s\n' "${par2}" 67 fi 68 } 69 70 repair() 71 { 72 par2="$1" 73 file="$2" 74 if ! par2 repair "${par2}"; then 75 log 'ecc repair error: %s\n' "$1" 76 fi 77 } 78 79 ######################################################################## 80 # The script 81 ######################################################################## 82 if [ $# -lt 1 ]; then 83 usage 84 exit 1 85 fi 86 87 case "$1" in 88 "create" | "verify" | "repair") ;; 89 *) 90 usage 91 exit 1 92 ;; 93 esac 94 95 cmd="$1" 96 shift 1 97 98 for i in "${@}"; do 99 file="$(basename "${i}")" 100 dir="$(dirname "${i}")" 101 par2="${dir}/.${file}.par2" # Hide par2 files 102 103 "${cmd}" "${par2}" "${i}" 104 done