commit e48d5f75d4580eb43dc7dd18885a68ce5875e8aa
parent 1d45ee8005fab3ebaae525f481d106f72c7fb8c9
Author: vaplv <vaplv@posteo.net>
Date: Tue, 19 Nov 2024 21:41:31 +0100
Add the ecc command
ecc stands for Error Correction Code.
This is a simple wrapper for the par2 command line, making it easier to
use. It automatically creates par2 files as hidden files. In this way,
PAR2 files are always close to their associated file, without being too
invasive during normal file system exploration. In addition, it logs
error messages on stderr, which can then be redirected to a file,
keeping track of files that have been incorrectly scanned, checked or
repaired.
TODO: Its man page has not yet been written.
Diffstat:
| M | Makefile | | | 19 | +++++++++++++------ |
| A | ecc | | | 91 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
2 files changed, 104 insertions(+), 6 deletions(-)
diff --git a/Makefile b/Makefile
@@ -15,19 +15,26 @@
.POSIX:
PREFIX=/usr/local
+BINPREFIX=$(PREFIX)/bin
+MANPREFIX=$(PREFIX)/share/man/
default:
lint:
shellcheck -o all backup
+ shellcheck -o all ecc
mandoc -T lint backup.8
install:
- mkdir -p $(DESTDIR)$(PREFIX)/bin
- mkdir -p $(DESTDIR)$(PREFIX)/share/man/man8/
- cp backup $(DESTDIR)$(PREFIX)/bin
- cp backup.8 $(DESTDIR)$(PREFIX)/share/man/man8/
+ mkdir -p $(DESTDIR)$(BINPREFIX)
+ mkdir -p $(DESTDIR)$(MANPREFIX)/man8/
+ cp backup ecc $(DESTDIR)$(BINPREFIX)
+ chmod 755 $(DESTDIR)$(BINPREFIX)/backup
+ chmod 755 $(DESTDIR)$(BINPREFIX)/ecc
+ cp backup.8 $(DESTDIR)$(MANPREFIX)/man8/
+ chmod 644 $(DESTDIR)$(MANPREFIX)/man8/backup.8
uninstall:
- rm -f $(DESTDIR)$(PREFIX)/bin/backup
- rm -f $(DESTDIR)$(PREFIX)/share/man/man8/backup.8
+ rm -f $(DESTDIR)$(BINPREFIX)/backup
+ rm -f $(DESTDIR)$(BINPREFIX)/ecc
+ rm -f $(DESTDIR)$(MANPREFIX)/man8/backup.8
diff --git a/ecc b/ecc
@@ -0,0 +1,91 @@
+#!/bin/sh
+
+# Copyright (C) 2023 Vincent Forest (vaplv@posteo.net)
+#
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program. If not, see <http://www.gnu.org/licenses/>.
+
+set -e
+
+########################################################################
+# Helper functions
+########################################################################
+cleanup()
+{
+ rm "${par2%.*}"*.par2
+ exit "${1:-1}"
+}
+
+log()
+{
+ str="$1"
+ shift 1
+
+ date="$(date +"%b %d %Y %H:%M:%S")"
+ >&2 printf '%s '"${str}" "${date}" "$@"
+}
+
+create()
+{
+ par2="$1"
+ file="$2"
+
+ 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
+ fi
+ trap 1 INT TERM EXIT # Restore default
+}
+
+verify()
+{
+ par2="$1"
+
+ if ! par2 verify "${par2}"; then
+ log 'ecc verify error: %s\n' "${par2}"
+ exit 1
+ fi
+}
+
+repair()
+{
+ par2="$1"
+ file="$2"
+ if ! par2 repair "${par2}"; then
+ log 'ecc repair error: %s\n' "$1"
+ exit 1
+ fi
+}
+
+########################################################################
+# 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##*/}"
+
+ exit 1
+fi
+
+file="$(basename "$2")"
+dir="$(dirname "$2")"
+par2="${dir}/.${file}.par2" # Hide par2 files
+
+$1 "${par2}" "$2"