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 v5] Don't rewrite buffer contents after saving by rename Date: Mon, 6 May 2019 13:46:07 -0700 Message-ID: <20190506204605.158797-1-jktomer@google.com> References: <87o94k7z6a.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="41453"; 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 Mon May 06 22:48:15 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 1hNkX0-000Ael-OC for geb-bug-gnu-emacs@m.gmane.org; Mon, 06 May 2019 22:48:15 +0200 Original-Received: from localhost ([127.0.0.1]:33822 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1hNkWz-0000E2-Hr for geb-bug-gnu-emacs@m.gmane.org; Mon, 06 May 2019 16:48:13 -0400 Original-Received: from eggs.gnu.org ([209.51.188.92]:56018) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1hNkWp-0000Dh-UE for bug-gnu-emacs@gnu.org; Mon, 06 May 2019 16:48:05 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1hNkWo-0002Qp-N6 for bug-gnu-emacs@gnu.org; Mon, 06 May 2019 16:48:03 -0400 Original-Received: from debbugs.gnu.org ([209.51.188.43]:44039) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1hNkWo-0002PQ-Iy for bug-gnu-emacs@gnu.org; Mon, 06 May 2019 16:48:02 -0400 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1hNkWo-0001dx-DK for bug-gnu-emacs@gnu.org; Mon, 06 May 2019 16:48: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: Mon, 06 May 2019 20:48: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.15571756536279 (code B ref 35497); Mon, 06 May 2019 20:48:02 +0000 Original-Received: (at 35497) by debbugs.gnu.org; 6 May 2019 20:47:33 +0000 Original-Received: from localhost ([127.0.0.1]:57583 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1hNkWL-0001dC-CB for submit@debbugs.gnu.org; Mon, 06 May 2019 16:47:33 -0400 Original-Received: from mail-qt1-f202.google.com ([209.85.160.202]:34535) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from <3Wp3QXAcKBcMst2xvn0pxxpun.lxvCEDIGmnkk3p1.pw3.x0p@flex--jktomer.bounces.google.com>) id 1hNkWJ-0001cy-HB for 35497@debbugs.gnu.org; Mon, 06 May 2019 16:47:32 -0400 Original-Received: by mail-qt1-f202.google.com with SMTP id l10so10838888qtr.1 for <35497@debbugs.gnu.org>; Mon, 06 May 2019 13:47:31 -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=lLsbgx11+DiKIav1JSgqtN4a6Ys+5MbKJTSM+fOfgeg=; b=miy1BLujSmfTpvVbcD1tQuaRt2Ytw4awx3q2HoqQqP9uBDDSvSSZtMSHrOBWb6g7BD 5xJ4AHCEtoikj948OJdXDxMV94WrS3ihG6xv8fJACuVVSrwEHmknpT0JXCzvUGZ9z4lQ rL82AiEPgxlm7ogx8LK5K9Xb3pExoE8qYuaiCPZ26z3yKc9tCUWjqrr2NGKHPIzjKtpF 2jtEC6fs5gKpvcuHzN4NqfpyHL86NlPF8ynKLbwbK2DCCVrZmTTEu3aB5J2yV6N35g0H spR9uQnaMlgDoV4kVRZbw3fRtgN02ZG5IAylpb2TLy9rNHlE4/q3uJm/9SOUqT/MDwa+ KC4w== 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=lLsbgx11+DiKIav1JSgqtN4a6Ys+5MbKJTSM+fOfgeg=; b=lxe0IHY7dabyF7BnsPOxl4CEi7VJqIy9Qw6pba7CYQoUrU0r6rQH9q2nIaoc5W6iYK /FG8ocR2AWt/H3EE53mxaFfMzvWH+hnrw/tko7Fuef63ZAR9E7uAvkOTEy0e548hazVV GGmsXMGVvcegl0UjfDt7Xcp4HEzOgW5n0hAiQaZmPtAj0h578y+jcssQ9jLtk5W0B8yF IvfGI7teFUyQH1mx2a5furV9KR6GIxaO1gY7SIC7hk2SfMN76gMX0i162a1vZQzbPBeX GQl/evLm62573MR+BsVyy5Aa8oZX58LwwqbbSMUQDqpBCmn9iJ3AYfsMaWtuciRiEw4i wfOg== X-Gm-Message-State: APjAAAVDV65tzyaO9K+hGALXZJybc/QMMxLlGRaSg/4VSnH/k8043U6P 15Dgesub2hcLNrfQ16q6M3KEzcJoF9+4I7tmrIfLcWHOgqux9irOW09ikwZUGyuddBt01XyYeme a4W9WGYk0FhihlcddyEPxe7wb0f8BbKP5j2kT/adr0SGZ1wkcmFc2iOA+VJTBRWI= X-Google-Smtp-Source: APXvYqw/i3LiyHyP+lOqYx2IRxoO25UmqX8/IkTLHSQ80xTedIgk+/hv3yp11bh+SKc0DA+Ksq9Dm9EzVLyS X-Received: by 2002:a0c:b99c:: with SMTP id v28mr22653409qvf.10.1557175642693; Mon, 06 May 2019 13:47:22 -0700 (PDT) In-Reply-To: <87o94k7z6a.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:158845 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 | 27 +++++++++++++++++++++++++++ 3 files changed, 44 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..a18cb19c68 100644 --- a/test/lisp/net/tramp-tests.el +++ b/test/lisp/net/tramp-tests.el @@ -2270,6 +2270,33 @@ 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." + :tags '(:unstable) + (skip-unless (tramp--test-enabled)) + (skip-unless (tramp--test-sh-p)) + + (let* ((tmp-name (tramp--test-make-temp-name)) + (advice (lambda (_start _end filename &rest _r) + (should-not (string= filename tmp-name))))) + + (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)))) + + ;; 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