Subject: Re: bug#22769: In-place sorting of a file

From: Paul Eggert <[email-masked]>
Date: Mon, 22 Feb 2016 23:53:25 -0800

Yanyan Jiang wrote:
> If at this time, the system crashes (or later at write), the data is gone forever (or have a suffering recovery). Should we warn users of this, or add special treatment with this case (write a temp file and rename it)?

Thanks for reporting the issue. Writing to a temp file and renaming would have 
problems, as it might change the file's ownership or permissions or etc., and 
also won't behave as the user expects if the file has multiple hard links. We 
should at least warn users about the problem in the documentation, though, so I 
installed the attached patch on Savannah at:

http://git.savannah.gnu.org/cgit/coreutils.git/commit/?id=4c1995f1b673b0e1eae73a9db4e28016b86dee9e
From 7b0a5eea09df48fc4a2588632c1410570a0c8904 Mon Sep 17 00:00:00 2001
From: Paul Eggert <[email-masked]>
Date: Mon, 22 Feb 2016 23:48:36 -0800
Subject: [PATCH] doc: document sort -o F F danger
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Problem reported by Yanyan Jiang 蒋炎岩 in: http://bugs.gnu.org/22769
* doc/coreutils.texi (sort invocation):
Mention possibility of 'sort -o F F' data loss during crashes.
---
 doc/coreutils.texi | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)

diff --git a/doc/coreutils.texi b/doc/coreutils.texi
index 01791be..a07e46e 100644
--- a/doc/coreutils.texi
+++ b/doc/coreutils.texi
@@ -4384,9 +4384,12 @@ silently uses a smaller value.
 @cindex overwriting of input, allowed
 Write output to @var{output-file} instead of standard output.
 Normally, @command{sort} reads all input before opening
-@var{output-file}, so you can safely sort a file in place by using
+@var{output-file}, so you can sort a file in place by using
 commands like @code{sort -o F F} and @code{cat F | sort -o F}@.
-However, @command{sort} with @option{--merge} (@option{-m}) can open
+However, it is often safer to output to an otherwise-unused file, as
+data may be lost if the system crashes or @command{sort} encounters
+an I/O or other serious error while a file is being sorted in place.
+Also, @command{sort} with @option{--merge} (@option{-m}) can open
 the output file before reading all input, so a command like @code{cat
 F | sort -m -o F - G} is not safe as @command{sort} might start
 writing @file{F} before @command{cat} is done reading it.
-- 
2.5.0