From 888434d5d9a7d4bcc22aa900815d4aa48d88dd25 Mon Sep 17 00:00:00 2001 From: Ohad Livne Date: Thu, 25 Dec 2025 01:31:13 +0200 Subject: [PATCH 1/3] Write a run0-based alternative to sudoedit --- .local/bin/run0edit | 75 +++++++++++++++++++++++++++++++++++++++++++++ .local/bin/sudoedit | 1 + 2 files changed, 76 insertions(+) create mode 100755 .local/bin/run0edit create mode 120000 .local/bin/sudoedit diff --git a/.local/bin/run0edit b/.local/bin/run0edit new file mode 100755 index 0000000..0909e39 --- /dev/null +++ b/.local/bin/run0edit @@ -0,0 +1,75 @@ +#! /usr/bin/bash + +set -euo pipefail +IFS=$'\n\t' + +default_editor() { + if [ -n "${SUDO_EDITOR+set}" ]; then + echo "${SUDO_EDITOR}" + elif [ -n "${VISUAL+set}" ]; then + echo "${VISUAL}" + elif [ -n "${EDITOR+set}" ]; then + echo "${EDITOR}" + else + echo "No editor configured" 1>&2 + exit 1 + fi +} + +try_command() { + if "$@" 2> /dev/null; then + return + elif run0 "$@" > /dev/null; then + return + else + exit 2 + fi +} + +make_editable_copy() { + orig_file="$1" + tempfile="$2" + + try_command cp "${orig_file}" "${tempfile}" +} + +elevate_permissions() { + tempfile="$1" + orig_file="$2" + + try_command chown --reference "${orig_file}" "${tempfile}" + try_command chmod --reference "${orig_file}" "${tempfile}" +} + +update_file() { + tempfile="$1" + orig_file="$2" + + try_command mv --force "${tempfile}" "${orig_file}" +} + +clean_tempfiles() { + tempfile="$1" + + rm --force "${tempfile}" "${tempfile}.orig" +} + +editor_cmd="$(default_editor)" +echo "Editing using the command \"${editor_cmd}\"" + +IFS=' ' read -r -a editor <<< "${editor_cmd}" + +orig_file="$1" +tempfile="$(mktemp)" +echo "Using temporary file ${tempfile}" +make_editable_copy "${orig_file}" "${tempfile}" +cp "${tempfile}" "${tempfile}.orig" +"${editor[@]}" "${tempfile}" +if cmp --quiet "${tempfile}" "${tempfile}.orig"; then + echo "No change to file" +else + elevate_permissions "${tempfile}" "${orig_file}" + update_file "${tempfile}" "${orig_file}" +fi + +clean_tempfiles "${tempfile}" diff --git a/.local/bin/sudoedit b/.local/bin/sudoedit new file mode 120000 index 0000000..17feabe --- /dev/null +++ b/.local/bin/sudoedit @@ -0,0 +1 @@ +run0edit \ No newline at end of file From 4cfa7ed76b24fd3d7162293d1d5a10ed75c5ce39 Mon Sep 17 00:00:00 2001 From: Ohad Livne Date: Fri, 26 Dec 2025 00:01:52 +0200 Subject: [PATCH 2/3] Add input validation - the script only supports editing a single file --- .local/bin/run0edit | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/.local/bin/run0edit b/.local/bin/run0edit index 0909e39..279f419 100755 --- a/.local/bin/run0edit +++ b/.local/bin/run0edit @@ -54,6 +54,12 @@ clean_tempfiles() { rm --force "${tempfile}" "${tempfile}.orig" } +if [ "$#" -ne "1" ]; then + echo "Expected exactly one input file. Usage:" 1>&2 + echo " $0 FILE" 1>&2 + exit 1; +fi + editor_cmd="$(default_editor)" echo "Editing using the command \"${editor_cmd}\"" From 36d1221256009ba65b7ea74b68516f3f8434939c Mon Sep 17 00:00:00 2001 From: Ohad Livne Date: Fri, 26 Dec 2025 00:02:44 +0200 Subject: [PATCH 3/3] Parse flag-like inputs as literal names --- .local/bin/run0edit | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/.local/bin/run0edit b/.local/bin/run0edit index 279f419..8a8b033 100755 --- a/.local/bin/run0edit +++ b/.local/bin/run0edit @@ -30,7 +30,7 @@ make_editable_copy() { orig_file="$1" tempfile="$2" - try_command cp "${orig_file}" "${tempfile}" + try_command cp -- "${orig_file}" "${tempfile}" } elevate_permissions() { @@ -45,13 +45,13 @@ update_file() { tempfile="$1" orig_file="$2" - try_command mv --force "${tempfile}" "${orig_file}" + try_command mv --force -- "${tempfile}" "${orig_file}" } clean_tempfiles() { tempfile="$1" - rm --force "${tempfile}" "${tempfile}.orig" + rm --force -- "${tempfile}" "${tempfile}.orig" } if [ "$#" -ne "1" ]; then