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 v2] Don't rewrite buffer contents after saving by rename Date: Tue, 30 Apr 2019 17:26:42 -0700 Message-ID: <20190501002641.71320-1-jktomer@google.com> References: <87h8afw5nt.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="97235"; 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 Wed May 01 02:28:18 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 1hLd6e-000P9B-MZ for geb-bug-gnu-emacs@m.gmane.org; Wed, 01 May 2019 02:28:17 +0200 Original-Received: from localhost ([127.0.0.1]:56868 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1hLd6d-0005bO-NU for geb-bug-gnu-emacs@m.gmane.org; Tue, 30 Apr 2019 20:28:15 -0400 Original-Received: from eggs.gnu.org ([209.51.188.92]:49184) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1hLd6T-0005b2-VM for bug-gnu-emacs@gnu.org; Tue, 30 Apr 2019 20:28:09 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1hLd6Q-0006cz-UJ for bug-gnu-emacs@gnu.org; Tue, 30 Apr 2019 20:28:05 -0400 Original-Received: from debbugs.gnu.org ([209.51.188.43]:57526) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1hLd6Q-0006cv-Nt for bug-gnu-emacs@gnu.org; Tue, 30 Apr 2019 20:28:02 -0400 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1hLd6Q-0000iw-BZ for bug-gnu-emacs@gnu.org; Tue, 30 Apr 2019 20:28:02 -0400 X-Loop: help-debbugs@gnu.org Resent-From: Jonathan Tomer Original-Sender: "Debbugs-submit" Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Wed, 01 May 2019 00:28: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.15566704502742 (code B ref 35497); Wed, 01 May 2019 00:28:02 +0000 Original-Received: (at 35497) by debbugs.gnu.org; 1 May 2019 00:27:30 +0000 Original-Received: from localhost ([127.0.0.1]:42837 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1hLd5t-0000i9-IA for submit@debbugs.gnu.org; Tue, 30 Apr 2019 20:27:29 -0400 Original-Received: from mail-qt1-f202.google.com ([209.85.160.202]:54324) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from <36ufIXAcKBckyz831t6v33v0t.r31IKJOMstqq9v7.v29.36v@flex--jktomer.bounces.google.com>) id 1hLd5r-0000hx-Ve for 35497@debbugs.gnu.org; Tue, 30 Apr 2019 20:27:28 -0400 Original-Received: by mail-qt1-f202.google.com with SMTP id f53so2772992qte.21 for <35497@debbugs.gnu.org>; Tue, 30 Apr 2019 17:27:27 -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=LgIFsCdye2xihXhY1+wM+W3ylnFT3SM7VkGDs+Z8t6Q=; b=O9MzM7Ix4ON7QS/As0oX43GobDEithaXnNmlX1x1Ge9bWK+kqe2YtOkaAGtlv78DNh L0HJ1qcrK5At1dXemQRsREgxJevkOqprjDElMc3SUX8tRjMf1wyn7VlrjpwZYbWmIUra XjS7IGYyaGZKRkAxJYDym6heUbSL6KjRbcyaru8A8IT0wGMaVyGKlf0aK2Rq98rvYR+9 bO85SSjTQlnr9N+PKrzZ0qaux/kPAFwmWQ0pO2NueylbfQ8lyO+kSoyuKIpD7WGavMbD wc9mLh7a2X1LDYh1VldZm2tR0SDtR9EGrlk/5mvXkVrz3Czhhkv1mKKf/uzDptbDSnOv 4/Zg== 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=LgIFsCdye2xihXhY1+wM+W3ylnFT3SM7VkGDs+Z8t6Q=; b=tcs1UwVsEa4u4ETskng90dxhPIiTCAtXr7nW/9WPuf3uVr0QRZaYt7R+/vUlD+B+gR 3roIWrd3lrTLTPrT8hILdjOINfDnfblh7UutbA9KMm+TIgKo0LAf9Jcngr0bHcbio/Ez UmUhupPYln5Xkn1u9FEc5amj3zPr0E3q70VwVhAa/cywMFawNB2cCckeaR5YkRpp0YBS RekZAPkd1740hHDFcuWDfCAb+ITCVPWJl3gCQqVXq0W8+QgSfY2ShF6SQNEZT8RZO+bx ziWf3UPCa14DxqofSjwnnuNxQBzL3su62Df9gsWwkdLNFPAtjUDJATsYsCoNq55sZRxj 0IYg== X-Gm-Message-State: APjAAAVjVBHzcCoDwaiyWhQDsw1BcL50JJFIeafpzw77ZIX582AZ6cwt i9OzmAy9AEBXCauD+PMDF5zh8AXMQcHotgtDWzdUDv9KVHQIwJCn7Yl5FJmmqOtO95t1sr4IRQX XWayEzznp/7clpSAov/i8r5JjRr0AnoS2nd8R9LUtH3plArE0dZvudZMaPlgv7/E= X-Google-Smtp-Source: APXvYqw6xx10NFuCzwHG8VW7pQACQ0aT7u65fJVPHjouyEBoDxWQHEv+cqWzFbR7c4XnF7cDqB/4lim7c9bp X-Received: by 2002:a0c:9666:: with SMTP id 35mr29615130qvy.214.1556670442350; Tue, 30 Apr 2019 17:27:22 -0700 (PDT) In-Reply-To: <87h8afw5nt.fsf@gmx.de> X-Mailer: git-send-email 2.21.0.593.g511ec345e18-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:158559 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-test46-file-precious-flag): Regression tests for this change. --- lisp/files.el | 4 ++-- test/lisp/files-tests.el | 26 ++++++++++++++++++++++++++ test/lisp/net/tramp-tests.el | 28 ++++++++++++++++++++++++++++ 3 files changed, 56 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..15f2a760c4 100644 --- a/test/lisp/files-tests.el +++ b/test/lisp/files-tests.el @@ -1244,5 +1244,31 @@ 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." + (files-tests--with-temp-file temp-file-name + (let* (temp-file-events + (watch (file-notify-add-watch + temp-file-name '(change) + (lambda (event) + (push (cadr event) temp-file-events))))) + (unwind-protect + (with-current-buffer (find-file-noselect temp-file-name) + (setq-local file-precious-flag t) + (insert "foobar") + (should (null (save-buffer))) + + ;; file-notify callbacks are triggered by input events, + ;; so we need to accept input before checking results. + (with-timeout (3.0 (ignore)) + (while (read-event nil nil 0.01) (ignore))) + + ;; 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 diff --git a/test/lisp/net/tramp-tests.el b/test/lisp/net/tramp-tests.el index cba697da18..1ca98520b3 100644 --- a/test/lisp/net/tramp-tests.el +++ b/test/lisp/net/tramp-tests.el @@ -44,6 +44,7 @@ (require 'dired) (require 'ert) (require 'ert-x) +(require 'filenotify) (require 'tramp) (require 'vc) (require 'vc-bzr) @@ -5741,6 +5742,33 @@ tramp--test-asynchronous-requests-timeout (ignore-errors (all-completions "tramp" (symbol-value x))) (ert-fail (format "Hook `%s' still contains Tramp function" x)))))) +(ert-deftest tramp-test46-file-precious-flag () + "Check that file-precious-flag is respected with Tramp in use." + (let* ((temp-file (make-temp-file "emacs")) + (remote-file (concat "/mock:localhost:" temp-file)) + temp-file-events + (watch + (file-notify-add-watch + temp-file '(change) + (lambda (event) + (push (cadr event) 'temp-file-events))))) + (unwind-protect + (with-current-buffer (find-file-noselect remote-file) + (setq-local file-precious-flag t) + (insert "foobar") + (should (null (save-buffer))) + + ;; file-notify callbacks are triggered by input events, so + ;; we need to accept input before checking results. + (with-timeout (3.0 (ignore)) + (while (read-event nil nil 0.01) (ignore))) + + ;; When file-precious-flag is set, the visited file should + ;; never be modified, only renamed-to. + (should (not (memq 'changed temp-file-events))))) + (file-notify-rm-watch watch) + (delete-file temp-file))) + (defun tramp-test-all (&optional interactive) "Run all tests for \\[tramp]." (interactive "p") -- 2.21.0.593.g511ec345e18-goog