On Thu, 25 Sept 2025 at 03:01, Andreas Schwab <[email protected]> wrote:
> $ chmod-t() { local m=$(stat -c %a "$2"); chmod "$1" "$2"; sleep ${3}m;
> chmod $m "$2"; }
>
*[...]*
> $ chmod-t 777 myfile.txt 30 &
Yes but what about:
$ chmod-t u+w myfile.txt 30 & [ -w myfile.txt ] || die 'Race condition
while checking that chmod u+w worked'
But even if that's fixed by putting the '&' inside the function, there
remains a more general problem:
$ chmod-t() { local m=$(stat -c %a "$2"); chmod "$1" "$2"; local r=$? ; (
sleep ${3}m && chmod $m "$2" ) & return $r ; }
$ chmod-t a+w myfile 30
$ chmod-t a+x myfile 30
It's a non-trivial exercise to arrange for "undo" changes to stack rather
than queue, separately for each target.
-Martin
PS: not every "nice to have" idea is actually "nice" in practice; some of
them turn out to be nightmares.
PPS: I suggest looking at overlay filesystems. Or snapshotting filesystems.
Or temporary mounts with UID mapping.
In general this feels like a problem that would be better solved at a
filesystem level, not an extra feature for a low-level command.