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 v7] Don't rewrite buffer contents after saving by rename Date: Tue, 7 May 2019 10:33:28 -0700 Message-ID: <20190507173328.140935-1-jktomer@google.com> References: <87r29a1hjh.fsf@gmx.de> Mime-Version: 1.0 Content-Type: text/plain; charset="UTF-8" Injection-Info: blaine.gmane.org; posting-host="blaine.gmane.org:195.159.176.226"; logging-data="194227"; mail-complaints-to="usenet@blaine.gmane.org" Cc: Jonathan Tomer To: 35497@debbugs.gnu.org, eliz@gnu.org, michael.albinus@gmx.de Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane.org@gnu.org Tue May 07 19:38:20 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 1hO42m-000oR9-9P for geb-bug-gnu-emacs@m.gmane.org; Tue, 07 May 2019 19:38:20 +0200 Original-Received: from localhost ([127.0.0.1]:50634 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1hO42l-00084y-7f for geb-bug-gnu-emacs@m.gmane.org; Tue, 07 May 2019 13:38:19 -0400 Original-Received: from eggs.gnu.org ([209.51.188.92]:35621) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1hO41h-0007Bo-1Y for bug-gnu-emacs@gnu.org; Tue, 07 May 2019 13:37:14 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1hO41d-0003tA-9b for bug-gnu-emacs@gnu.org; Tue, 07 May 2019 13:37:11 -0400 Original-Received: from debbugs.gnu.org ([209.51.188.43]:45971) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1hO41Y-0003rM-Q9 for bug-gnu-emacs@gnu.org; Tue, 07 May 2019 13:37:07 -0400 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1hO41W-0003X5-AU for bug-gnu-emacs@gnu.org; Tue, 07 May 2019 13:37:04 -0400 X-Loop: help-debbugs@gnu.org Resent-From: Jonathan Tomer Original-Sender: "Debbugs-submit" Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Tue, 07 May 2019 17:37:02 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 35497 X-GNU-PR-Package: emacs X-GNU-PR-Keywords: patch Original-Received: via spool by 35497-submit@debbugs.gnu.org id=B35497.155725058013521 (code B ref 35497); Tue, 07 May 2019 17:37:02 +0000 Original-Received: (at 35497) by debbugs.gnu.org; 7 May 2019 17:36:20 +0000 Original-Received: from localhost ([127.0.0.1]:59514 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1hO40q-0003W1-5p for submit@debbugs.gnu.org; Tue, 07 May 2019 13:36:20 -0400 Original-Received: from mail-qt1-f201.google.com ([209.85.160.201]:33539) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from <3CsLRXAcKBcEqr0vtlynvvnsl.jvtACBGEklii1nz.nu1.vyn@flex--jktomer.bounces.google.com>) id 1hO40m-0003Vh-3k for 35497@debbugs.gnu.org; Tue, 07 May 2019 13:36:18 -0400 Original-Received: by mail-qt1-f201.google.com with SMTP id n39so20105824qtn.0 for <35497@debbugs.gnu.org>; Tue, 07 May 2019 10:36:16 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=date:in-reply-to:message-id:mime-version:references:subject:from:to :cc; bh=sOHF+Cg8spTTqQQDmoYVtmAyRqaLdh9GswGuyDjXY6o=; b=GrExdCzsf2kn0p/MoN3co32AQAbQiO1Wd1YRugbemaKrXBzf/5uvJqRbPggddShuXl fTqSfwFuHTUBd73X9H8h6SsA4HRT1KaROryAh68ZiUp/hb8tU+og7DjcKWFsC8leKVVh HutNmRPYSFS1xHCLaOyeaN7AXGN/rSXfa1t6/XwL+PDOBbKBVMBWBWJnh6ghtomYsfEw xFP8tYZEH2D3BxSioQx/BA3PUZDtzGrJA4A+5UVWnBXaqgua+lHjTWtH+V87AWmSRG0C MxmpuvUxG2ejAv3w3cpJF/3/ZoaXrNAmnSvgpIHSn0Q1Y4L9mPG4OMpnZRrfijUeEbEH 46bw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=sOHF+Cg8spTTqQQDmoYVtmAyRqaLdh9GswGuyDjXY6o=; b=KvCjy2k6WlfQHKWPi8kEpwGQgEgy8a+gh2yzOBOty54j7zb4J1T2aZNu9e4A/S96K8 FI1J/VAuV6SR9X3UKCB9iM2qpJQ/ztDntKt+ocoCcEN5fKczC9IF+2Fum+utN4C9ASRR sQcOvcAEg2V07Yca9fKvtCgOZFoF3XL0zS7iF9E2PlPdQr0p2N5XxLHytjhY9OFop9/Q FAmXwoee+/W/td4W0m6BY5CWY/FERETPL26ORU5uRaMJBuQc2YsH6/OTzrJN0P66TTc1 qTZUTQXeH4SvKiwhy0wGkUExglZ6EsBcnl7VJ8U8dcFFqPjNG5U2KJqaBZC0yVqJILEh L3VA== X-Gm-Message-State: APjAAAV/z9AqYP+qHiCkjMb1YskwrxP84t8I/RnORi6Wj+GzRRn4322u LoSRvFp26oh3SqEolTPlrOS4jyXwU7B9OTwEJzRw0O0UQbCW4R33DEsR11U8chfjw8UG031ZGwC OcdFqd/v5gkrBcLBgi63wwI6vXZX/jWAYYKb9c7ggSgWNgkZ1LI6vkG4jHYnrUNg= X-Google-Smtp-Source: APXvYqxQJixvbeSBOrLet8MjS3mMH7uWdOqVtscFOf9YYcbKloxdIsZ1+WxFm0fl0JqN/vOeU+OIPqP0mtWC X-Received: by 2002:ac8:3701:: with SMTP id o1mr23112909qtb.98.1557250570386; Tue, 07 May 2019 10:36:10 -0700 (PDT) In-Reply-To: <87r29a1hjh.fsf@gmx.de> X-Mailer: git-send-email 2.21.0.1020.gf2820cf01a-goog 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:158885 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): * test/lisp/net/tramp-tests.el (tramp-test10-write-region-file-precious-flag): Regression tests for this change. --- lisp/files.el | 4 ++-- test/lisp/files-tests.el | 15 +++++++++++++++ test/lisp/net/tramp-tests.el | 29 +++++++++++++++++++++++++++++ 3 files changed, 46 insertions(+), 2 deletions(-) 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)) (declare-function diff-no-select "diff" diff --git a/test/lisp/files-tests.el b/test/lisp/files-tests.el index ae8ea41a79..fe2e958f1c 100644 --- a/test/lisp/files-tests.el +++ b/test/lisp/files-tests.el @@ -1244,5 +1244,20 @@ files-tests-file-attributes-equal (executable-find (file-name-nondirectory tmpfile)))))) (delete-file tmpfile)))) +(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." + (let* ((temp-file-name (make-temp-file "files-tests")) + (advice (lambda (_start _end filename &rest _r) + (should-not (string= filename temp-file-name))))) + (unwind-protect + (with-current-buffer (find-file-noselect temp-file-name) + (advice-add #'write-region :before advice) + (setq-local file-precious-flag t) + (insert "foobar") + (should (null (save-buffer)))) + (ignore-errors (advice-remove #'write-region advice)) + (ignore-errors (delete-file temp-file-name))))) + (provide 'files-tests) ;;; files-tests.el ends here diff --git a/test/lisp/net/tramp-tests.el b/test/lisp/net/tramp-tests.el index cba697da18..0c5ea03741 100644 --- a/test/lisp/net/tramp-tests.el +++ b/test/lisp/net/tramp-tests.el @@ -41,6 +41,7 @@ ;;; Code: +(require 'cl-seq) (require 'dired) (require 'ert) (require 'ert-x) @@ -2270,6 +2271,34 @@ tramp--test-print-duration ;; Cleanup. (ignore-errors (delete-file tmp-name)))))) +(ert-deftest tramp-test10-write-region-file-precious-flag () + "Check that `file-precious-flag' is respected with Tramp in use." + (skip-unless (tramp--test-enabled)) + (skip-unless (tramp--test-sh-p)) + + (let* ((tmp-name (tramp--test-make-temp-name)) + written-files + (advice (lambda (_start _end filename &rest _r) + (push filename written-files)))) + + (unwind-protect + (with-current-buffer (find-file-noselect tmp-name) + ;; Write initial contents. Adapt `visited-file-modtime' + ;; in order to suppress confirmation. + (insert "foo") + (write-region nil nil tmp-name) + (set-visited-file-modtime) + ;; Run the test. + (advice-add 'write-region :before advice) + (setq-local file-precious-flag t) + (insert "bar") + (should (null (save-buffer))) + (should-not (cl-member tmp-name written-files :test #'string=))) + + ;; Cleanup. + (ignore-errors (advice-remove 'write-region advice)) + (ignore-errors (delete-file tmp-name))))) + (ert-deftest tramp-test11-copy-file () "Check `copy-file'." (skip-unless (tramp--test-enabled)) -- 2.21.0.1020.gf2820cf01a-goog