Preserve or restore the ownership of the target output file, if
possible.  This converges the behaviour of sponge a little bit more to
the one of 'tee' and shell redirections.

Bug-Debian: https://bugs.debian.org/920118
Signed-off-by: Nicolas Schier <nico...@fjasle.eu>
---
 sponge.c       | 7 +++++++
 sponge.docbook | 7 +++----
 2 files changed, 10 insertions(+), 4 deletions(-)

diff --git a/sponge.c b/sponge.c
index f852ad5..59e0a2f 100644
--- a/sponge.c
+++ b/sponge.c
@@ -379,6 +379,13 @@ int main (int argc, char **argv) {
                        }
                        copy_tmpfile(tmpfile, outfile, bufstart, bufsize);
                }
+
+               /* Attempt to change ownership according to the old file, but
+                * ignore errors, as most users will not have the rights to
+                * change ownership. */
+               if (exists) {
+                       chown(outname, statbuf.st_uid, statbuf.st_gid);
+               }
        }
        else {
                if (tmpfile_used) {
diff --git a/sponge.docbook b/sponge.docbook
index 303d6e6..0b34561 100644
--- a/sponge.docbook
+++ b/sponge.docbook
@@ -66,10 +66,9 @@ USA
                        <command>sponge</command> preserves the
                        permissions of the output file
                        if it already exists.  But in contrast to e.g.
-                       <command>tee</command>, <command>sponge</command> might
-                       not preserve or restore the original file ownership, no
-                       matter whether it has been called by a privileged (root)
-                       user.
+                       <command>tee</command>, <command>sponge</command> does 
only
+                       preserve or restore the original file ownership, if the 
current
+                       user has the necessary rights.
                </para>
                <para>
                        When possible, <command>sponge</command> creates or 
updates the
-- 
2.24.0

Reply via email to