From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!.POSTED.blaine.gmane.org!not-for-mail From: Jonathan Tomer Newsgroups: gmane.emacs.bugs Subject: bug#35497: [PATCH] Don't rewrite buffer contents after saving by rename Date: Mon, 29 Apr 2019 16:20:09 -0700 Message-ID: <20190429232009.94549-1-jktomer@google.com> Mime-Version: 1.0 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Injection-Info: blaine.gmane.org; posting-host="blaine.gmane.org:195.159.176.226"; logging-data="23936"; mail-complaints-to="usenet@blaine.gmane.org" Cc: Jonathan Tomer To: 35497@debbugs.gnu.org Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane.org@gnu.org Tue Apr 30 01:32:14 2019 Return-path: Envelope-to: geb-bug-gnu-emacs@m.gmane.org Original-Received: from lists.gnu.org ([209.51.188.17]) by blaine.gmane.org with esmtps (TLS1.0:RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1hLFkq-00062C-PA for geb-bug-gnu-emacs@m.gmane.org; Tue, 30 Apr 2019 01:32:13 +0200 Original-Received: from localhost ([127.0.0.1]:36534 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1hLFkp-0007WL-Hp for geb-bug-gnu-emacs@m.gmane.org; Mon, 29 Apr 2019 19:32:11 -0400 Original-Received: from eggs.gnu.org ([209.51.188.92]:48854) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1hLFki-0007W9-Vb for bug-gnu-emacs@gnu.org; Mon, 29 Apr 2019 19:32:06 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1hLFkh-0001a3-QU for bug-gnu-emacs@gnu.org; Mon, 29 Apr 2019 19:32:04 -0400 Original-Received: from debbugs.gnu.org ([209.51.188.43]:54795) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1hLFkh-0001Zz-Kc for bug-gnu-emacs@gnu.org; Mon, 29 Apr 2019 19:32:03 -0400 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1hLFkh-0005Qg-EH for bug-gnu-emacs@gnu.org; Mon, 29 Apr 2019 19:32:03 -0400 X-Loop: help-debbugs@gnu.org Resent-From: Jonathan Tomer Original-Sender: "Debbugs-submit" Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Mon, 29 Apr 2019 23:32:01 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: report 35497 X-GNU-PR-Package: emacs X-GNU-PR-Keywords: patch X-Debbugs-Original-To: bug-gnu-emacs@gnu.org Original-Received: via spool by submit@debbugs.gnu.org id=B.155658067020798 (code B ref -1); Mon, 29 Apr 2019 23:32:01 +0000 Original-Received: (at submit) by debbugs.gnu.org; 29 Apr 2019 23:31:10 +0000 Original-Received: from localhost ([127.0.0.1]:40105 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1hLFjp-0005PN-M7 for submit@debbugs.gnu.org; Mon, 29 Apr 2019 19:31:10 -0400 Original-Received: from eggs.gnu.org ([209.51.188.92]:33203) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from <3r4bHXAcKDcYvw50yq3s00sxq.o0yn6s-sz6-qymo4sz6.03s@flex--jktomer.bounces.google.com>) id 1hLFZi-00058i-FG for submit@debbugs.gnu.org; Mon, 29 Apr 2019 19:20:43 -0400 Original-Received: from lists.gnu.org ([209.51.188.17]:32957) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from <3r4bHXAcKDcYvw50yq3s00sxq.o0yn6s-sz6-qymo4sz6.03s@flex--jktomer.bounces.google.com>) id 1hLFZT-00056y-Gz for submit@debbugs.gnu.org; Mon, 29 Apr 2019 19:20:30 -0400 Original-Received: from eggs.gnu.org ([209.51.188.92]:47575) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from <3r4bHXAcKDcYvw50yq3s00sxq.o0yn6s-sz6-qymo4sz6.03s@flex--jktomer.bounces.google.com>) id 1hLFZP-0006SA-AK for bug-gnu-emacs@gnu.org; Mon, 29 Apr 2019 19:20:27 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from <3r4bHXAcKDcYvw50yq3s00sxq.o0yn6s-sz6-qymo4sz6.03s@flex--jktomer.bounces.google.com>) id 1hLFZK-00055N-6c for bug-gnu-emacs@gnu.org; Mon, 29 Apr 2019 19:20:21 -0400 Original-Received: from mail-pl1-x64a.google.com ([2607:f8b0:4864:20::64a]:37153) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from <3r4bHXAcKDcYvw50yq3s00sxq.o0yn6s-sz6-qymo4sz6.03s@flex--jktomer.bounces.google.com>) id 1hLFZJ-00054w-UK for bug-gnu-emacs@gnu.org; Mon, 29 Apr 2019 19:20:18 -0400 Original-Received: by mail-pl1-x64a.google.com with SMTP id p12so1265252plk.4 for ; Mon, 29 Apr 2019 16:20:17 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=date:message-id:mime-version:subject:from:to:cc :content-transfer-encoding; bh=23+MnmGNCQq0suX0rRZ2Q8HjMvImFlaVYwRi96PX47M=; b=MUlTKFW8Ops4LkfGtUbFNUqerpO0Ui3ekXO9+tabOyr23oa0X++KWCKztAqrYyRf3a eEpo2dauPcc6paMby0hmt2odpXgHjqpvChqNjHhQusfyT8CH9qLDqSLC6mfWuajYIhcb yQCXQkwmePK7gZUjzx5OvxLvEyM6NhMwXytYoEZWoV10WzjFpNyGWisZv8dYAYope07k 0/kQdp19GrjqFMJypWehyFsgFG5ek0VbBQOQgKrVQZoxHCQdGHXvJ7MwVEIMwI7o9I2u 3vRtIIa2C7+CoguWMRYWYZectVdoQkDifhSCc9p/zQmV5sXQ9lHO83l8JTA+OT81S3AW Qm2g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:message-id:mime-version:subject:from:to:cc :content-transfer-encoding; bh=23+MnmGNCQq0suX0rRZ2Q8HjMvImFlaVYwRi96PX47M=; b=rwFcWdF3AoPpda2nNrZVRvt73Qzy4J48nYU8eLEQ3qI3ighQlhb/2ZFNRl5/OIypTy 4Jy4j7bmif5xQ0TuwKT5crl9B4T3o03ZY21UK0GTky02ne36R6xvWL9Y8OxYFeT92tM7 hgnUGTETTHfGCwWZNDmQHvn/9r9IuIs2mudwMSxSk5hKbgZetB9Nf5ZhIaWt0uzWYRei vilZx7wqzpl1nYIxm4w6eKJxIS5D1zWVUGxVvxGr+kuJ8VcG5tI72dZX25REg2kYpuFi nOghrPOcu/7Gq2aTo0CcorAPCy1rjr8sHa3LnNdRLztt0MCX82EHGNvhrPU3QJg4/J00 lHCA== X-Gm-Message-State: APjAAAUmqOtp/A9rAw/1tOLmSjdp8YeLUPwNNBZ32+e7dac7g3JA/BD9 f8bEwf/Urf+CMghexY3KGAP0UwjcG9YYOOat9R78PJNRtmwR2nD906bgFxypCKdpZ0JY/7T8i6Y bl9AGnNVXSrxdJEMQy3RsGuWUdFxsU/oZWL/lNcqPvMQ1qNSJIJe58pDPhWACB5g= X-Google-Smtp-Source: APXvYqy3pk283ywqhdhEoUWBaoinkUIArM2h0D+X3GsiSzIpKdPL4YSdsdDCCGeE95irY1J7eA30w0q/eprE X-Received: by 2002:a63:6cc7:: with SMTP id h190mr61182511pgc.350.1556580015771; Mon, 29 Apr 2019 16:20:15 -0700 (PDT) X-Mailer: git-send-email 2.21.0.593.g511ec345e18-goog X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.6.x X-Mailman-Approved-At: Mon, 29 Apr 2019 19:31:08 -0400 X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 209.51.188.43 X-BeenThere: bug-gnu-emacs@gnu.org List-Id: "Bug reports for GNU Emacs, the Swiss army knife of text editors" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane.org@gnu.org Original-Sender: "bug-gnu-emacs" Xref: news.gmane.org gmane.emacs.bugs:158464 Archived-At: When `file-precious-flag' is non-nil, files are saved by renaming a temporary file to the new name; this is an atomic operation on POSIX so other programs will not see the file in an intermediate state. Unfortunately, due to a paren-matching error introduced in change 574c05e219476912db3105fa164accd9ba12b35f, we would then write the contents again in the usual way after this rename. In addition to being wasteful, this is a serious bug: the whole point of `file-precious-flag' is to prevent race conditions with other programs that might otherwise see an empty file, but with this bug the race is actually much *more* likely to be visible: the rename will alert any inotify watchers of a change, and then the subsequent write is very likely to truncate the file just as those programs start to read it! * lisp/files.el (basic-save-buffer-2): Don't rewrite file contents after saving-by-renaming. * test/lisp/files-tests.el (files-tests-dont-rewrite-precious-files): Regression test for this change. --- etc/NEWS | 7 +++++++ lisp/files.el | 4 ++-- test/lisp/files-tests.el | 27 +++++++++++++++++++++++++++ 3 files changed, 36 insertions(+), 2 deletions(-) diff --git a/etc/NEWS b/etc/NEWS index 9b32d720b6..5bfadcbbd6 100644 --- a/etc/NEWS +++ b/etc/NEWS @@ -340,6 +340,13 @@ longer. ** Multicolor fonts such as "Noto Color Emoji" can be displayed on Emacs configured with Cairo drawing and linked with cairo >=3D 1.16.0. =20 +--- +** The file-precious-flag is now respected correctly. +A bug previously caused files to be saved twice when +`file-precious-flag' or `break-hardlinks-on-save' were specified: once +by renaming a temporary file to the destination name, and then again +by truncating and rewriting the file, which is exactly what +`file-precious-flag' is supposed to avoid. =0C * Editing Changes in Emacs 27.1 =20 diff --git a/lisp/files.el b/lisp/files.el index c05d70a00e..72518e8127 100644 --- a/lisp/files.el +++ b/lisp/files.el @@ -5256,7 +5256,7 @@ basic-save-buffer-2 (set-file-extended-attributes buffer-file-name (nth 1 setmodes))) (set-file-modes buffer-file-name - (logior (car setmodes) 128)))))) + (logior (car setmodes) 128))))) (let (success) (unwind-protect (progn @@ -5272,7 +5272,7 @@ basic-save-buffer-2 (and setmodes (not success) (progn (rename-file (nth 2 setmodes) buffer-file-name t) - (setq buffer-backed-up nil)))))) + (setq buffer-backed-up nil))))))) setmodes)) =20 (declare-function diff-no-select "diff" diff --git a/test/lisp/files-tests.el b/test/lisp/files-tests.el index ae8ea41a79..07012fea6e 100644 --- a/test/lisp/files-tests.el +++ b/test/lisp/files-tests.el @@ -1244,5 +1244,32 @@ files-tests-file-attributes-equal (executable-find (file-name-nondirectory tmpfile)))))) (delete-file tmpfile)))) =20 +(ert-deftest files-tests-dont-rewrite-precious-files () + "Test that `file-precious-flag' forces files to be saved by +renaming only, rather than modified in-place." + (files-tests--with-temp-file temp-file-name + (with-current-buffer (find-file-noselect temp-file-name) + (let* (temp-file-events watch) + (unwind-protect + (progn + (setq watch + (file-notify-add-watch + temp-file-name '(change) + (lambda (event) + (add-to-list 'temp-file-events (cadr event) 'append= )))) + (setq-local file-precious-flag t) + (insert "foobar") + (should (null (save-buffer))) + + ;; file-notify callbacks are input events, so we need to + ;; accept input before checking results. + (sit-for 0.1) + + ;; When file-precious-flag is set, the visited file + ;; should never be modified, only renamed-to (which may + ;; appear as "renamed" and/or "created" to file-notify). + (should (not (memq 'changed temp-file-events)))) + (file-notify-rm-watch watch)))))) + (provide 'files-tests) ;;; files-tests.el ends here --=20 2.21.0.593.g511ec345e18-goog