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, 15 Oct 2023 11:34:17 +0200 Message-ID: <87lec4cjqe.fsf@sappc2.fritz.box> References: <83h6mtq9t7.fsf@gnu.org> <8734ydx7x3.fsf@sappc2.fritz.box> <83cyxgqwjm.fsf@gnu.org> Reply-To: Jens Schmidt Mime-Version: 1.0 Content-Type: text/plain Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="26666"; 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 15 11:36:01 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 1qrxXU-0006jw-Hc for geb-bug-gnu-emacs@m.gmane-mx.org; Sun, 15 Oct 2023 11:36:00 +0200 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1qrxXA-0007Du-IS; Sun, 15 Oct 2023 05:35:40 -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 1qrxX8-0007Cg-Kj for bug-gnu-emacs@gnu.org; Sun, 15 Oct 2023 05:35:38 -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 1qrxX8-0001cC-CC for bug-gnu-emacs@gnu.org; Sun, 15 Oct 2023 05:35:38 -0400 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1qrxXW-0004W5-87 for bug-gnu-emacs@gnu.org; Sun, 15 Oct 2023 05:36: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: Sun, 15 Oct 2023 09:36: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.169736250517288 (code B ref 66546); Sun, 15 Oct 2023 09:36:02 +0000 Original-Received: (at 66546) by debbugs.gnu.org; 15 Oct 2023 09:35:05 +0000 Original-Received: from localhost ([127.0.0.1]:51198 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1qrxWa-0004Ul-0L for submit@debbugs.gnu.org; Sun, 15 Oct 2023 05:35:05 -0400 Original-Received: from mr5.vodafonemail.de ([145.253.228.165]:42974) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1qrxWV-0004UA-CT for 66546@debbugs.gnu.org; Sun, 15 Oct 2023 05:35:02 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=vodafonemail.de; s=vfde-mb-mr2-21dec; t=1697362469; bh=SolxOuGx93SCh6TiNSFdir7Z2j4i1QsTkz8zdXDq2tg=; h=From:To:Subject:References:Date:In-Reply-To:Message-ID:User-Agent: Content-Type:From; b=OCVB5vkYEh/jECL4A3zsOoUp6JDR0FniM0R5JuYfq9SejY4jyeeQW8I5D2lxGQLWC MJTG7sGPQzH4woE7xdwA34rdHvLk4ML0SPMW1JvVzgIjC57KQNWHLDFaTWpte9xR2c lSf1H9EuowbyR48eQup23QxCFlupScOl5+7xBRR8= 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 mr5.vodafonemail.de (Postfix) with ESMTPS id 4S7Znx2C5gz1y6W; Sun, 15 Oct 2023 09:34:29 +0000 (UTC) Original-Received: from sappc2 (port-92-194-212-150.dynamic.as20676.net [92.194.212.150]) (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 4S7Znn5HZBz9thg; Sun, 15 Oct 2023 09:34:18 +0000 (UTC) In-Reply-To: <83cyxgqwjm.fsf@gnu.org> (Eli Zaretskii's message of "Sun, 15 Oct 2023 08:33:49 +0300") X-purgate-type: clean X-purgate: clean X-purgate-size: 2375 X-purgate-ID: 155817::1697362464-C17FD816-CF6E6A47/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:272491 Archived-At: Eli Zaretskii writes: >> From: Jens Schmidt >> Cc: 66546@debbugs.gnu.org >> Date: Sat, 14 Oct 2023 22:31:36 +0200 >> >> > Please explain what happens with "C-0 C-x C-s", and why. I don't >> > think I understand that, given what you wrote. >> >> "C-0 C-x C-s" prompts me whether to write the modified buffer to the >> write-protected file. When I reply "yes", I expect Emacs to do the >> following: >> >> 1. Get the mode bits/extended attributes of "foo". >> 2. Remove the write-potection on "foo". >> 3. Write the buffer to "foo", not creating a backup file. >> 4. Restore the mode bits/extended attributes on "foo" to their previous >> state taken in step 1. > > So far, so good. > >> What happens instead: >> >> Emacs errors out that it cannot write to the file, even though it has >> the permissions and means to temporarily remove the write-protection on >> it. > > Why cannot Emacs write to the file, and what do the extended > attributes have to do with that? Let's check the working Emacs 23 code first, when there were no extended attributes: (setq setmodes (cons (file-modes buffer-file-name) buffer-file-name)) (set-file-modes buffer-file-name (logior (car setmodes) 128)) In procedural pseudo-code and without the cons, this does: setmodes = file-modes (buffer-file-name); set-file-modes (buffer-file-name, setmodes | 128); which implements step 2 above: It takes the existing mode bits, adds bit 0b10000000 (u+w), and sets the resulting mode bits on the file. The file gets writable. The following call to `write-region' succeeds. Now the Emacs 29 code. We simplify that in the assumption, that the extended attribute calls always succeed, and that we do not need the regular calls: (setq setmodes (list (file-extended-attributes buffer-file-name) buffer-file-name)) (set-file-extended-attributes buffer-file-name (nth 0 setmodes)) The pseudo-code here looks like this: setmodes = file-extended-attributes (buffer-file-name); set-file-extended-attributes (setmodes); That is, the original extended attributes of the file get written back to the file *unchanged*. No write permission is added to the file. Accordingly, the following call to `write-region' fails.