commit 3098078d5f4daf78d65ead1262d853840e9f2a95
parent 4bd27a18cddc0cf3893d4f9ebfb17d1e36ff897a
Author: vaplv <vaplv@posteo.net>
Date: Sat, 14 Dec 2024 14:24:45 +0100
ecc: add support for multiple input files
Do not return error if error occurs to continue processing remaining files.
Update man page accordingly. Fix a few errors such as date
formatting. Improve the examples section.
Diffstat:
| M | ecc | | | 51 | ++++++++++++++++++++++++++++++++------------------- |
| M | ecc.1 | | | 61 | ++++++++++++++++++++++++++++++++++++++++++++----------------- |
2 files changed, 76 insertions(+), 36 deletions(-)
diff --git a/ecc b/ecc
@@ -20,9 +20,16 @@ set -e
########################################################################
# Helper functions
########################################################################
+usage()
+{
+ >&2 printf 'usage: ecc create file ...\n'
+ >&2 printf ' ecc verify file ...\n'
+ >&2 printf ' ecc repair file ...\n'
+}
+
cleanup()
{
- rm "${par2%.*}"*.par2
+ rm -f "${par2%.*}"*.par2
exit "${1:-1}"
}
@@ -42,15 +49,13 @@ create()
if [ -e "${par2}" ]; then
log 'ecc create error: file already exists %s\n' "${par2}"
- exit 1
- fi
-
- trap cleanup INT TERM EXIT
- if ! par2 create -a "${par2}" "${file}"; then
- log 'ecc create error: %s\n' "$1"
- exit 1
+ else
+ trap cleanup INT TERM EXIT
+ if ! par2 create -a "${par2}" "${file}"; then
+ log 'ecc create error: %s\n' "$1"
+ fi
+ trap 1 INT TERM EXIT # Restore default
fi
- trap 1 INT TERM EXIT # Restore default
}
verify()
@@ -59,7 +64,6 @@ verify()
if ! par2 verify "${par2}"; then
log 'ecc verify error: %s\n' "${par2}"
- exit 1
fi
}
@@ -69,7 +73,6 @@ repair()
file="$2"
if ! par2 repair "${par2}"; then
log 'ecc repair error: %s\n' "$1"
- exit 1
fi
}
@@ -77,15 +80,25 @@ repair()
# The script
########################################################################
if [ $# -lt 1 ]; then
- >&2 printf 'usage: %s create filename\n' "${0##*/}"
- >&2 printf ' %s verify filename\n' "${0##*/}"
- >&2 printf ' %s repair filename\n' "${0##*/}"
-
+ usage
exit 1
fi
-file="$(basename "$2")"
-dir="$(dirname "$2")"
-par2="${dir}/.${file}.par2" # Hide par2 files
+case "$1" in
+ "create" | "verify" | "repair") ;;
+ *)
+ usage
+ exit 1
+ ;;
+esac
+
+cmd="$1"
+shift 1
+
+for i in "${@}"; do
+ file="$(basename "${i}")"
+ dir="$(dirname "${i}")"
+ par2="${dir}/.${file}.par2" # Hide par2 files
-$1 "${par2}" "$2"
+ "${cmd}" "${par2}" "${i}"
+done
diff --git a/ecc.1 b/ecc.1
@@ -23,19 +23,21 @@
.Sh SYNOPSIS
.Nm
.Cm create
-.Ar file
+.Ar file No ...
.Nm
.Cm verify
-.Ar file
+.Ar file No ...
.Nm
.Cm repair
-.Ar file
+.Ar file No ...
.\""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
.Sh DESCRIPTION
.Nm
calculates the error correction code for files to check their integrity
and repair them in the event of corruption.
-Internally, ecc simply invokes
+Internally,
+.Nm
+simply invokes
.Xr par2 1
and adds no new functionality to it.
Its sole purpose is to define a policy for using
@@ -56,46 +58,71 @@ messages displayed on standard output,
also lists error messages on standard error, providing a simple way of
listing files whose error code calculation, verification or repair has
failed.
-The error messages are formated as below:
+The error messages are formatted as follows:
.Bd -literal -offset Ds
-"ecc %s error: %s\\n", command, filepath
+"%s ecc %s error: %s\\n", date, command, filepath
.Ed
.Pp
with
.Ar command
the action
.Nm
-performed and
+performed,
.Ar filepath
-the path to the file on which the error occurred.
+the path to the file for which the error occurred and
+.Ar date
+the time at which the log was issued.
+It is displayed as in the
+following
+.Xr date 1
+command:
+.Bd -literal -offset Ds
+date +"%b %d %Y %H:%M:%S"
+.Ed
.\""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
.Sh EXIT STATUS
.Ex -std
.\""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
.Sh EXAMPLES
-Calculate the error correction code for all files found recursively in
-the current directory hierarchy, with the exception of error correction
-code files that have already been created.
+Calculate the error correction code for a file:
+.Bd -literal -offset Ds
+ecc create my_file.bin
+.Ed
+.Pp
+Calculate the error correction code for all pdf files in the current
+directory:
+.Bd -literal -offset Ds
+ecc create *.pdf
+.Ed
+.Pp
+Check all pdf files of the current directory.
Prints errors in the temporary
-.Pa log.err
+.Pa ecc.err
file:
.Bd -literal -offset Ds
+ecc verify *.pdf 2>> "${TMPDIR:-/tmp}/ecc.err"
+.Ed
+.Pp
+Calculate the error correction code for all files found recursively in
+the current directory hierarchy, with the exception of error correction
+code files that have already been created:
+.Bd -literal -offset Ds
find . \\( -type f ! -name "*.par2" \\) \\
--exec ecc create {} \\; 2> /tmp/log.err
+-exec ecc create {} \\; 2>> "${TMPDIR:-/tmp}/ecc.err"
.Ed
.Pp
Check all the files in the current directory:
.Bd -literal -offset Ds
find . \\( ! -path . -prune -type f ! -name "*.par2" \\) \\
--exec ecc verify {} \\; 2> /tmp/log.err
+-exec ecc verify {} \\; 2>> "${TMPDIR:-/tmp}/ecc.err"
.Ed
.Pp
-Repair all files previously detected as corrupt and listed in the
+Repair all files previously detected as corrupted and listed in the
temporary
-.Pa log.err
+.Pa ecc.err
file:
.Bd -literal -offset Ds
-sed -n 's/^ecc verify error: \\(.\\{1,\\}\\)$/\\1/p' /tmp/log.err \\
+sed -n 's/^.\\+error: \\(.\\+\\)$/\\1/p' "${TMPDIR:-/tmp}/ecc.err" \\
| xargs -I{} ecc repair "{}"
.Ed
.\""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""