From unknown Sat Aug 16 15:56:11 2025 Content-Disposition: inline Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 X-Mailer: MIME-tools 5.509 (Entity 5.509) Content-Type: text/plain; charset=utf-8 From: bug#22769 <22769@debbugs.gnu.org> To: bug#22769 <22769@debbugs.gnu.org> Subject: Status: In-place sorting of a file Reply-To: bug#22769 <22769@debbugs.gnu.org> Date: Sat, 16 Aug 2025 22:56:11 +0000 retitle 22769 In-place sorting of a file reassign 22769 coreutils submitter 22769 Yanyan Jiang severity 22769 normal thanks From debbugs-submit-bounces@debbugs.gnu.org Mon Feb 22 11:01:11 2016 Received: (at submit) by debbugs.gnu.org; 22 Feb 2016 16:01:11 +0000 Received: from localhost ([127.0.0.1]:38239 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84) (envelope-from ) id 1aXsv9-0005r3-6H for submit@debbugs.gnu.org; Mon, 22 Feb 2016 11:01:11 -0500 Received: from eggs.gnu.org ([208.118.235.92]:46317) by debbugs.gnu.org with esmtp (Exim 4.84) (envelope-from ) id 1aXnd3-0000q2-C1 for submit@debbugs.gnu.org; Mon, 22 Feb 2016 05:22:09 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1aXncx-0004hr-8x for submit@debbugs.gnu.org; Mon, 22 Feb 2016 05:22:04 -0500 X-Spam-Checker-Version: SpamAssassin 3.3.2 (2011-06-06) on eggs.gnu.org X-Spam-Level: *** X-Spam-Status: No, score=3.3 required=5.0 tests=BAYES_50,FREEMAIL_FROM, HTML_MESSAGE,RECEIVED_FROM_WINDOWS_HOST autolearn=disabled version=3.3.2 Received: from lists.gnu.org ([2001:4830:134:3::11]:37824) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1aXncx-0004hm-5c for submit@debbugs.gnu.org; Mon, 22 Feb 2016 05:22:03 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:37264) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1aXncv-0006gn-Mi for bug-coreutils@gnu.org; Mon, 22 Feb 2016 05:22:03 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1aXncq-0004fM-O1 for bug-coreutils@gnu.org; Mon, 22 Feb 2016 05:22:01 -0500 Received: from blu004-omc3s31.hotmail.com ([65.55.116.106]:52113) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1aXncq-0004fI-Ib for bug-coreutils@gnu.org; Mon, 22 Feb 2016 05:21:56 -0500 Received: from BLU436-SMTP219 ([65.55.116.72]) by BLU004-OMC3S31.hotmail.com over TLS secured channel with Microsoft SMTPSVC(7.5.7601.23008); Mon, 22 Feb 2016 01:57:58 -0800 X-TMN: [f82+Wsb1n7FSPE7QU4zcVbM75Fx7VD09] X-Originating-Email: [jiangyy@outlook.com] Message-ID: From: Yanyan Jiang Content-Type: multipart/alternative; boundary="Apple-Mail=_402AA662-FAE3-489C-A2E3-DCCE6E9A605D" Subject: In-place sorting of a file Date: Mon, 22 Feb 2016 04:57:54 -0500 To: bug-coreutils@gnu.org MIME-Version: 1.0 (Mac OS X Mail 9.2 \(3112\)) X-Mailer: Apple Mail (2.3112) X-OriginalArrivalTime: 22 Feb 2016 09:57:56.0645 (UTC) FILETIME=[8120D550:01D16D57] X-detected-operating-system: by eggs.gnu.org: Windows 7 or 8 [fuzzy] X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.6.x X-Received-From: 2001:4830:134:3::11 X-Spam-Score: -4.0 (----) X-Debbugs-Envelope-To: submit X-Mailman-Approved-At: Mon, 22 Feb 2016 11:01:09 -0500 X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: debbugs-submit-bounces@debbugs.gnu.org Sender: "Debbugs-submit" X-Spam-Score: -4.0 (----) --Apple-Mail=_402AA662-FAE3-489C-A2E3-DCCE6E9A605D Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Hi, I found someone talking about sorting a file in-place: = http://stackoverflow.com/questions/9117274/do-i-need-to-generate-a-second-= file-to-sort-a-file = However, sort file.txt -o file.txt is NOT safe. A strace sequence = (sorting 1 2 3 4 5): read(3, "1\n2\n3\n4\n5\n\n", 4096) =3D 11 read(3, "", 4096) =3D 0 _llseek(3, 0, [11], SEEK_CUR) =3D 0 close(3) =3D 0 munmap(0xb7743000, 4096) =3D 0 ftruncate64(1, 0) =3D 0 = <---------------------- (*) fstat64(1, {st_mode=3DS_IFREG|0664, st_size=3D0, ...}) =3D 0 mmap2(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, = 0) =3D 0xb7743000 write(1, "\n1\n2\n3\n4\n5\n", 11) =3D 11 close(1) =3D 0 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)? Thank your for your attention. Regards, Yanyan Jiang =E8=92=8B=E7=82=8E=E5=B2=A9 Institute of Computer Software, Dept. of Computer Science, Nanjing University --Apple-Mail=_402AA662-FAE3-489C-A2E3-DCCE6E9A605D Content-Transfer-Encoding: quoted-printable Content-Type: text/html; charset="utf-8"
Hi,

I found someone talking about sorting a = file in-place:

http://stackoverflow.com/questions/9117274/do-i-need-to-generat= e-a-second-file-to-sort-a-file

However, sort file.txt -o file.txt is = NOT safe. A strace sequence (sorting 1 2 3 4 5):

read(3, = "1\n2\n3\n4\n5\n\n", 4096)      =3D 11
read(3, "", 4096)             =           =3D 0
_llseek(3, = 0, [11], SEEK_CUR)           =3D 0
close(3)               =                  =3D = 0
munmap(0xb7743000, 4096)       =          =3D 0
ftruncate64(1, 0)             =           =3D 0         =     <----------------------  (*)
fstat64(1, {st_mode=3DS_IFREG|0664, st_size=3D0, ...}) =3D = 0
mmap2(NULL, 4096, PROT_READ|PROT_WRITE, = MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) =3D 0xb7743000
write(1, "\n1\n2\n3\n4\n5\n", 11)       =3D = 11
close(1)           =                     =  =3D 0

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)?

Thank your for your attention.

Regards,
Yanyan = Jiang =E8=92=8B=E7=82=8E=E5=B2=A9
Institute of = Computer Software,
Dept. of Computer Science, = Nanjing University

= --Apple-Mail=_402AA662-FAE3-489C-A2E3-DCCE6E9A605D-- From debbugs-submit-bounces@debbugs.gnu.org Tue Feb 23 02:53:35 2016 Received: (at 22769-done) by debbugs.gnu.org; 23 Feb 2016 07:53:35 +0000 Received: from localhost ([127.0.0.1]:38925 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84) (envelope-from ) id 1aY7mo-0005Gr-Vc for submit@debbugs.gnu.org; Tue, 23 Feb 2016 02:53:35 -0500 Received: from zimbra.cs.ucla.edu ([131.179.128.68]:34267) by debbugs.gnu.org with esmtp (Exim 4.84) (envelope-from ) id 1aY7mm-0005Gc-St for 22769-done@debbugs.gnu.org; Tue, 23 Feb 2016 02:53:33 -0500 Received: from localhost (localhost [127.0.0.1]) by zimbra.cs.ucla.edu (Postfix) with ESMTP id 4E45D16083D; Mon, 22 Feb 2016 23:53:27 -0800 (PST) Received: from zimbra.cs.ucla.edu ([127.0.0.1]) by localhost (zimbra.cs.ucla.edu [127.0.0.1]) (amavisd-new, port 10032) with ESMTP id 86Dbyo58B9O9; Mon, 22 Feb 2016 23:53:26 -0800 (PST) Received: from localhost (localhost [127.0.0.1]) by zimbra.cs.ucla.edu (Postfix) with ESMTP id 20C28160FD3; Mon, 22 Feb 2016 23:53:26 -0800 (PST) X-Virus-Scanned: amavisd-new at zimbra.cs.ucla.edu Received: from zimbra.cs.ucla.edu ([127.0.0.1]) by localhost (zimbra.cs.ucla.edu [127.0.0.1]) (amavisd-new, port 10026) with ESMTP id 1suucLJqHpLA; Mon, 22 Feb 2016 23:53:26 -0800 (PST) Received: from [192.168.1.9] (pool-100-32-155-148.lsanca.fios.verizon.net [100.32.155.148]) by zimbra.cs.ucla.edu (Postfix) with ESMTPSA id F176D16083D; Mon, 22 Feb 2016 23:53:25 -0800 (PST) Subject: Re: bug#22769: In-place sorting of a file To: Yanyan Jiang , 22769-done@debbugs.gnu.org References: From: Paul Eggert Organization: UCLA Computer Science Department Message-ID: <56CC0FF5.5000908@cs.ucla.edu> Date: Mon, 22 Feb 2016 23:53:25 -0800 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:38.0) Gecko/20100101 Thunderbird/38.5.1 MIME-Version: 1.0 In-Reply-To: Content-Type: multipart/mixed; boundary="------------070603070009010906070902" X-Spam-Score: -0.0 (/) X-Debbugs-Envelope-To: 22769-done X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: debbugs-submit-bounces@debbugs.gnu.org Sender: "Debbugs-submit" X-Spam-Score: -0.0 (/) This is a multi-part message in MIME format. --------------070603070009010906070902 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit 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 --------------070603070009010906070902 Content-Type: text/plain; charset=UTF-8; name="0001-doc-document-sort-o-F-F-danger.txt" Content-Transfer-Encoding: base64 Content-Disposition: attachment; filename="0001-doc-document-sort-o-F-F-danger.txt" RnJvbSA3YjBhNWVlYTA5ZGY0OGZjNGEyNTg4NjMyYzE0MTA1NzBhMGM4OTA0I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BpdC4KLS0gCjIuNS4wCgo= --------------070603070009010906070902-- From unknown Sat Aug 16 15:56:11 2025 Received: (at fakecontrol) by fakecontrolmessage; To: internal_control@debbugs.gnu.org From: Debbugs Internal Request Subject: Internal Control Message-Id: bug archived. Date: Tue, 22 Mar 2016 11:24:04 +0000 User-Agent: Fakemail v42.6.9 # This is a fake control message. # # The action: # bug archived. thanks # This fakemail brought to you by your local debbugs # administrator