diff options
| author | tslil <tslil@posteo.de> | 2026-04-05 15:16:52 +0100 |
|---|---|---|
| committer | tslil <tslil@posteo.de> | 2026-04-05 15:17:11 +0100 |
| commit | fe1065e754bd6a86220c75f2e08508a81903f445 (patch) | |
| tree | 5572a5aaf9363f356324f12aacac46a17888970f | |
| parent | a8d4188f4ce67ddd7c5b5d16d4362409785201d7 (diff) | |
doasedit: track script
| -rwxr-xr-x | doasedit | 84 |
1 files changed, 84 insertions, 0 deletions
diff --git a/doasedit b/doasedit new file mode 100755 index 0000000..f2deae0 --- /dev/null +++ b/doasedit @@ -0,0 +1,84 @@ +#!/bin/sh +# Copy an existing text file to a temporary location, then edit the file. +# Attempt to then transfer the temporary file back to the original location if +# the temprary file has been altered. Conclude with a little clean-up. Try to +# avoid deleting any changes. + +if [ $# -lt 1 ] +then + echo "usage: $0 text-file" + exit 1 +fi + +if [ ! -f "$1" ] +then + echo "File does not exist or is a special file/link." + exit 2 +fi + +if [ -L "$1" ] +then + echo "File is a symbolic link. Refusing to edit." + exit 2 +fi + +if [ ! -r "$1" ] +then + echo "This user is unable to read the specified file." + exit 3 +fi + +temp_file=$(mktemp --tmpdir doasedit.XXXXXXXX) +if [ ! $? ] +then + echo "Could not create temporary file." + exit 4 +fi + +cp "$1" "$temp_file" +if [ ! $? ] +then + echo "Unable to copy file $1" + exit 5 +fi + +# Attempt to run editor on the file +${EDITOR:-ed} "$temp_file" +if [ ! $? ] +then + echo "Could not run editor $EDITOR" + echo "Please make sure the EDITOR variable is set." + rm -f "$temp_file" + exit 6 +fi + +# Check to see if the file has been changed. +cmp -s "$1" "$temp_file" +status=$? +if [ $status -eq 0 ] +then + echo "File unchanged. Not writing back to original location." + rm -f "$temp_file" + exit 0 +fi + +# At this point the file has been changed. Make sure it still exists. +if [ -f "$temp_file" ] +then + doas cp "$temp_file" "$1" + cmp -s "$temp_file" "$1" + status=$? + # If file fails to copy, do not do clean-up + while [ $status -ne 0 ] + do + echo "Copying file back to $1 failed. Press Ctrl-C to abort or Enter to try again." + read abc + doas cp "$temp_file" "$1" + cmp -s "$temp_file" "$1" + status=$? + done +fi + +# Clean up +rm -f "$temp_file" +exit 0 |
