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: Sat, 21 Oct 2023 19:56:44 +0200 Message-ID: <87edhnamg3.fsf@sappc2.fritz.box> References: <83h6mtq9t7.fsf@gnu.org> <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> 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="19744"; 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 Sat Oct 21 19:57:56 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 1quGEU-0004m5-ON for geb-bug-gnu-emacs@m.gmane-mx.org; Sat, 21 Oct 2023 19:57:54 +0200 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1quGED-0003ST-5z; Sat, 21 Oct 2023 13:57:37 -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 1quGEB-0003S6-SB for bug-gnu-emacs@gnu.org; Sat, 21 Oct 2023 13:57:36 -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 1quGEA-00072z-JX for bug-gnu-emacs@gnu.org; Sat, 21 Oct 2023 13:57:35 -0400 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1quGEc-0006Wj-5M for bug-gnu-emacs@gnu.org; Sat, 21 Oct 2023 13:58:02 -0400 X-Loop: help-debbugs@gnu.org Resent-From: Jens Schmidt Original-Sender: "Debbugs-submit" Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Sat, 21 Oct 2023 17:58:02 +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.169791105925058 (code B ref 66546); Sat, 21 Oct 2023 17:58:02 +0000 Original-Received: (at 66546) by debbugs.gnu.org; 21 Oct 2023 17:57:39 +0000 Original-Received: from localhost ([127.0.0.1]:44981 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1quGEB-0006W2-K6 for submit@debbugs.gnu.org; Sat, 21 Oct 2023 13:57:39 -0400 Original-Received: from mr4.vodafonemail.de ([145.253.228.164]:41622) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1quGE5-0006Vi-I7 for 66546@debbugs.gnu.org; Sat, 21 Oct 2023 13:57:34 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=vodafonemail.de; s=vfde-mb-mr2-23sep; t=1697911016; bh=kTyB3Cix/qLl87PCUKabHo32wL5RUephTiPsYZySJTY=; h=From:To:Subject:References:Date:In-Reply-To:Message-ID:User-Agent: Content-Type:From; b=IwbSFlw4PgQ722XvX0mbkiP/ZOyyDzNWRie7u0zmorCNiS4v8n0dpK7N3XBKE6LNd RiBt8mYBviph3BXVa2P4864xjkpGj5ZiCDy3im50aU97kAzbQBDdEQFJRBB8hC6afq 8fv4TAfP+ub/ACd9MHRaxiUVwkrRbgdku7w8X1KA= 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 4SCTfw16JGz1y1t; Sat, 21 Oct 2023 17:56:56 +0000 (UTC) Original-Received: from sappc2 (port-92-194-212-157.dynamic.as20676.net [92.194.212.157]) (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 4SCTfm4c9tz9s5P; Sat, 21 Oct 2023 17:56:45 +0000 (UTC) In-Reply-To: <835y31u8t0.fsf@gnu.org> (Eli Zaretskii's message of "Fri, 20 Oct 2023 09:06:35 +0300") X-purgate-type: clean X-purgate: clean X-purgate-size: 6268 X-purgate-ID: 155817::1697911011-34FF4E22-DD5A108D/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:272923 Archived-At: --=-=-= Content-Type: text/plain Eli Zaretskii writes: >> From: Jens Schmidt >> Cc: 66546@debbugs.gnu.org >> Date: Thu, 19 Oct 2023 23:12:59 +0200 >> >> But probably it would be helpful to shortcut that look-up process, like >> this: > > I don't object, though a more detailed explanation (instead of sending > people to read the bug discussion) would be better. I added the reasons why you wanted to keep the call to `set-file-extended-attributes' in the attached patch. >> + ;; If we get an error writing the file which we >> + ;; previously made writable, attempt to undo the >> + ;; write-access. >> + ((and (eq tempsetmodes 'u+w) (not success)) > > Isn't it easier, safer, and more portable to compare buffer-file-name > with (nth 2 setmodes) instead? I wanted to make 100% sure that we execute that first cond-branch if and only if we previously changed the file mode. IOW, I feel I cannot exclude that by some strange configuration (equal buffer-file-name (nth 2 setmodes)) could also be true in other cases. I added more documentation and renamed the u+w symbol to something looking less Unix-specific. >> + (condition-case () >> + (unless >> + (with-demoted-errors "Error setting file modes: %S" >> + (set-file-modes buffer-file-name (car setmodes))) >> + (set-file-extended-attributes buffer-file-name >> + (nth 1 setmodes))) >> + (error nil))) > > Why do we need condition-case here if we use with-demoted-errors? We don't need it, I just copied that snippet from function `basic-save-buffer' without thinking about it. I changed that. As mentioned earlier, I can also add unit tests for both issues. If I remember correctly, you would prefer these in the same changeset. Please review the next version of the patch. Thanks. --=-=-= Content-Type: text/x-diff Content-Disposition: inline; filename=0001-Better-handle-errors-when-writing-r-o-files-without-.patch >From 32c7d0858df1e5576a25ef64bb5ab971d1018c85 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 | 42 ++++++++++++++++++++++++++++++++---------- 1 file changed, 32 insertions(+), 10 deletions(-) diff --git a/lisp/files.el b/lisp/files.el index adfe8bd44b9..22c4f845a9b 100644 --- a/lisp/files.el +++ b/lisp/files.el @@ -5856,6 +5856,10 @@ basic-save-buffer-1 ;; This returns a value (MODES EXTENDED-ATTRIBUTES BACKUPNAME), like ;; backup-buffer. (defun basic-save-buffer-2 () + ;; Variable `tempsetmodes' tracks temporary changes to the file + ;; modes: We set it to t if we (probably) need to make the file + ;; writable, and later to `reset-on-error' if we actually made the + ;; file writable. (let (tempsetmodes setmodes) (if (not (file-writable-p buffer-file-name)) (let ((dir (file-name-directory buffer-file-name))) @@ -5934,10 +5938,14 @@ 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)) + ;; Remember we made the file writable so that we can + ;; try to undo that in case of errors. + (setq tempsetmodes 'reset-on-error) ;; Change the mode back, after writing. (setq setmodes (list (file-modes buffer-file-name) @@ -5946,7 +5954,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 +5976,21 @@ 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 which we + ;; previously made writable, attempt to undo the + ;; write-access. + ((and (eq tempsetmodes 'reset-on-error) (not success)) + (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 --=-=-=--