sponge is pretty interesting.
My first thought is that sponge may benefit from a flock option. This
would allow file users to avoid accessing the file in an intermediate
state. For example sponge -l would obtain a read lock first, then collects
input, then upgrade to a write lock, then replace the file's content.
flock -s myfile -c cat myfile | sed -e s/foo/bar/g | sponge -l myfile
I don't have an outright objection to sponge, but I think there could
be a more memory-efficient form. You would instead arrange for a single
file replacement process that operates at both the source and the sink
of a pipeline. The replacement process would know how much of the lead
part of the file has been sent to stdout already, so it knows how much
of stdin it can write to the front of the file.
The replacement process wouldn't be 'sponge', but 'tee -r'. Combine it
with a shell syntax change that allows forward-declaration of pipes:
tee -r myfile <{|a} | sed -e s/foo/bar/g >{|a}
or the more cooler-looking
tee -r myfile |<>| sed -e s/foo/bar/g
The process graph would look like this
tee -r myfile
0: read end of pipe '|a'
1: write end of pipe to sed
sed
0: read end of pipe to sed
1: write end of pipe '|a'
Also, my flock question may be answered with a 'flock -x' prefix
on 'tee -r'.
I can't be the first person to imagine pipeloops, named pipes
and bash's coproc already exist, and the moretools project has
a 'connect' suggestion. Regardless, tee -r represents a more efficient
alternative to sponge by being both the source and sink of the file
content.
On Thu, 19 Jun 2025, Eric Blake wrote:
The Austin Group is debating whether to sponsor a proposal to add
sponge(1) to a future edition of POSIX:
https://www.austingroupbugs.net/view.php?id=1927
I note that at present, busybox does NOT have a sponge applet; but it
seems like the sort of applet that could be easily added if someone
were to contribute it. Many GNU/Linux systems currently have sponge
installed from the `moreutils` project, if that were to serve as a
starting point for deciding which features to match.
Does anyone on this list have any comments on why it might be a good
or a bad idea for POSIX to standardize sponge? Similarly, if busybox
were to implement sponge, that would make it a bit easier for POSIX
folks to point to multiple implementations as part of the
standardization process.
--
Eric Blake, Principal Software Engineer
Red Hat, Inc.
Virtualization: qemu.org | libguestfs.org
_______________________________________________
busybox mailing list
[email protected]
https://lists.busybox.net/mailman/listinfo/busybox
_______________________________________________
busybox mailing list
[email protected]
https://lists.busybox.net/mailman/listinfo/busybox