33 lines
951 B
Bash
Executable File
33 lines
951 B
Bash
Executable File
#!/bin/sh
|
|
|
|
destfile_pfx="$(cat /dev/urandom | tr -cd 'a-f0-9' | head -c 32)"
|
|
|
|
while [ -d "/tmp/doasedit/$destfile_pfx" ]; do
|
|
destfile_pfx="$(cat /dev/urandom | tr -cd 'a-f0-9' | head -c 32)"
|
|
done
|
|
|
|
mkdir -p /tmp/doasedit/$destfile_pfx
|
|
srcfile="$(realpath $1)"
|
|
|
|
if [ -f "$srcfile" ]; then
|
|
doas cp $srcfile /tmp/doasedit/$destfile_pfx/edit
|
|
doas chown -R $USER:$USER /tmp/doasedit/$destfile_pfx/edit
|
|
doas cp $srcfile /tmp/doasedit/$destfile_pfx/file
|
|
else
|
|
# create file with "regular" system permissions (root:root 644)
|
|
touch /tmp/doasedit/$destfile_pfx/file
|
|
doas chown root:root /tmp/doasedit/$destfile_pfx/file
|
|
fi
|
|
|
|
$EDITOR /tmp/doasedit/$destfile_pfx/edit
|
|
|
|
cat /tmp/doasedit/$destfile_pfx/edit | doas tee /tmp/doasedit/$destfile_pfx/file 1>/dev/null
|
|
|
|
if cmp -s "/tmp/doasedit/$destfile_pfx/file" "$srcfile"; then
|
|
echo "Skipping write; no changes."
|
|
else
|
|
doas mv -f /tmp/doasedit/$destfile_pfx/file $srcfile
|
|
fi
|
|
|
|
rm -rf /tmp/doasedit/$destfile_pfx
|