From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Jens Schmidt via "Bug reports for GNU Emacs, the Swiss army knife of text editors" Newsgroups: gmane.emacs.bugs Subject: bug#66546: 30.0.50; save-buffer to write-protected file without backup fails Date: Sun, 22 Oct 2023 11:45:52 +0200 Message-ID: <878r7v9ei7.fsf@sappc2.fritz.box> References: <8734ydx7x3.fsf@sappc2.fritz.box> <83cyxgqwjm.fsf@gnu.org> <87lec4cjqe.fsf@sappc2.fritz.box> <83ttqsp5x1.fsf@gnu.org> <87il78cdyf.fsf@sappc2.fritz.box> <83pm1gozi6.fsf@gnu.org> <87edhvd84h.fsf@sappc2.fritz.box> <838r82q0gi.fsf@gnu.org> <87wmvmfi68.fsf@sappc2.fritz.box> <83o7gxo771.fsf@gnu.org> <87ttqpgg8l.fsf@sappc2.fritz.box> <83cyxcnp2f.fsf@gnu.org> <87sf671xe3.fsf@sappc2.fritz.box> <83y1fzmdhm.fsf@gnu.org> <87zg0ez57o.fsf@sappc2.fritz.box> <835y31u8t0.fsf@gnu.org> <87edhnamg3.fsf@sappc2.fritz.box> <83a5sbre7x.fsf@gnu.org> <87bkcrafxt.fsf@sappc2.fritz.box> <834jijqmnd.fsf@gnu.org> Reply-To: Jens Schmidt Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="32134"; mail-complaints-to="usenet@ciao.gmane.io" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.1 (gnu/linux) Cc: 66546@debbugs.gnu.org To: Eli Zaretskii Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Sun Oct 22 11:46:52 2023 Return-path: Envelope-to: geb-bug-gnu-emacs@m.gmane-mx.org Original-Received: from lists.gnu.org ([209.51.188.17]) by ciao.gmane.io with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1quV2p-0008AB-RM for geb-bug-gnu-emacs@m.gmane-mx.org; Sun, 22 Oct 2023 11:46:51 +0200 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1quV2a-00077I-DM; Sun, 22 Oct 2023 05:46:36 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1quV2X-00076C-QK for bug-gnu-emacs@gnu.org; Sun, 22 Oct 2023 05:46:33 -0400 Original-Received: from debbugs.gnu.org ([2001:470:142:5::43]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1quV2X-0004OA-I8 for bug-gnu-emacs@gnu.org; Sun, 22 Oct 2023 05:46:33 -0400 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1quV2z-00018f-Km for bug-gnu-emacs@gnu.org; Sun, 22 Oct 2023 05:47:01 -0400 X-Loop: help-debbugs@gnu.org Resent-From: Jens Schmidt Original-Sender: "Debbugs-submit" Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Sun, 22 Oct 2023 09:47:01 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 66546 X-GNU-PR-Package: emacs Original-Received: via spool by 66546-submit@debbugs.gnu.org id=B66546.16979680024351 (code B ref 66546); Sun, 22 Oct 2023 09:47:01 +0000 Original-Received: (at 66546) by debbugs.gnu.org; 22 Oct 2023 09:46:42 +0000 Original-Received: from localhost ([127.0.0.1]:45477 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1quV2f-000186-KO for submit@debbugs.gnu.org; Sun, 22 Oct 2023 05:46:42 -0400 Original-Received: from mr4.vodafonemail.de ([145.253.228.164]:37482) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1quV2b-00017m-LS for 66546@debbugs.gnu.org; Sun, 22 Oct 2023 05:46:40 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=vodafonemail.de; s=vfde-mb-mr2-23sep; t=1697967963; bh=KR2ZacByfUogfEl4coyp/UtYFleKTN488f6LQDFmVa8=; h=From:To:Subject:References:Date:In-Reply-To:Message-ID:User-Agent: Content-Type:From; b=tuXrFAJdI02k2qjGzQ7/SCGsK/S7AUGNQc9r9/2C/HnW0ygEyuRnloS07aALfPLhU mJARZuoW4oJ+ITlFn3q7hVWLP2Ixj4B/smoFMMRZkgWvUYhjF0dLKSifOz2hqtKoWG WVI2STNmyoOF3zL9LEUIY88/oqwrfXH1teAol2wI= Original-Received: from smtp.vodafone.de (unknown [10.0.0.2]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by mr4.vodafonemail.de (Postfix) with ESMTPS id 4SCtk36DWgz1y3S; Sun, 22 Oct 2023 09:46:03 +0000 (UTC) Original-Received: from sappc2 (port-92-194-190-1.dynamic.as20676.net [92.194.190.1]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (prime256v1) server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by smtp.vodafone.de (Postfix) with ESMTPSA id 4SCtjw1Dvcz9rxZ; Sun, 22 Oct 2023 09:45:53 +0000 (UTC) In-Reply-To: <834jijqmnd.fsf@gnu.org> (Eli Zaretskii's message of "Sun, 22 Oct 2023 07:57:58 +0300") X-purgate-type: clean X-purgate: clean X-purgate-size: 4013 X-purgate-ID: 155817::1697967959-A27FF816-37FCAA63/0/0 X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list 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-mx.org@gnu.org Original-Sender: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Xref: news.gmane.io gmane.emacs.bugs:272966 Archived-At: --=-=-= Content-Type: text/plain Eli Zaretskii writes: >> From: Jens Schmidt >> Cc: 66546@debbugs.gnu.org >> Date: Sat, 21 Oct 2023 22:17:18 +0200 >> Makes sense, will do. In that case, would a plain `equal' be enough to >> compare file paths? > > Yes, I think so. Please review the next version, then. Thanks. --=-=-= Content-Type: text/x-diff Content-Disposition: inline; filename=0001-Better-handle-errors-when-writing-r-o-files-without-.patch >From 249a53e340c5c6cd4c9b8c405e5a9c6074876585 Mon Sep 17 00:00:00 2001 From: Jens Schmidt Date: Thu, 19 Oct 2023 23:00:32 +0200 Subject: [PATCH] Better handle errors when writing r-o files without backup * lisp/files.el (basic-save-buffer-2): Restore file permissions when writing read-only files without backup fails. (Bug#66546) --- lisp/files.el | 36 ++++++++++++++++++++++++++---------- 1 file changed, 26 insertions(+), 10 deletions(-) diff --git a/lisp/files.el b/lisp/files.el index adfe8bd44b9..1be5b374ae8 100644 --- a/lisp/files.el +++ b/lisp/files.el @@ -5934,9 +5934,10 @@ basic-save-buffer-2 t)) ;; If file not writable, see if we can make it writable ;; temporarily while we write it (its original modes will be - ;; restored in 'basic-save-buffer'). But no need to do so if - ;; we have just backed it up (setmodes is set) because that - ;; says we're superseding. + ;; restored in 'basic-save-buffer' or, in case of an error, in + ;; the `unwind-protect' below). But no need to do so if we + ;; have just backed it up (setmodes is set) because that says + ;; we're superseding. (cond ((and tempsetmodes (not setmodes)) ;; Change the mode back, after writing. (setq setmodes @@ -5946,7 +5947,12 @@ basic-save-buffer-2 (file-extended-attributes buffer-file-name)) buffer-file-name)) ;; If set-file-extended-attributes fails to make the - ;; file writable, fall back on set-file-modes. + ;; file writable, fall back on set-file-modes. Calling + ;; set-file-extended-attributes here may or may not be + ;; actually necessary. However, since its exact + ;; behavior is highly port-specific, since calling it + ;; does not do any harm, and since the call has a long + ;; history, we decided to leave it in (bug#66546). (with-demoted-errors "Error setting attributes: %s" (set-file-extended-attributes buffer-file-name (nth 1 setmodes))) @@ -5963,12 +5969,22 @@ basic-save-buffer-2 buffer-file-name nil t buffer-file-truename) (when save-silently (message nil)) (setq success t)) - ;; If we get an error writing the new file, and we made - ;; the backup by renaming, undo the backing-up. - (and setmodes (not success) - (progn - (rename-file (nth 2 setmodes) buffer-file-name t) - (setq buffer-backed-up nil))))))) + (cond + ;; If we get an error writing the file, and there is no + ;; backup file, then we (most likely) made that file + ;; writable above. Attempt to undo the write-access. + ((and setmodes (not success) + (equal (nth 2 setmodes) buffer-file-name)) + (with-demoted-errors "Error setting file modes: %S" + (set-file-modes buffer-file-name (car setmodes))) + (with-demoted-errors "Error setting attributes: %s" + (set-file-extended-attributes buffer-file-name + (nth 1 setmodes)))) + ;; If we get an error writing the new file, and we made + ;; the backup by renaming, undo the backing-up. + ((and setmodes (not success)) + (rename-file (nth 2 setmodes) buffer-file-name t) + (setq buffer-backed-up nil))))))) setmodes)) (declare-function diff-no-select "diff" -- 2.30.2 --=-=-=--