unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
* enabling atomic-update more often, i.e., rewrite via rename
@ 2011-12-09 10:26 Jim Meyering
  2011-12-13 17:13 ` save-buffer: avoid data loss on interrupt Jim Meyering
  0 siblings, 1 reply; 11+ messages in thread
From: Jim Meyering @ 2011-12-09 10:26 UTC (permalink / raw)
  To: Emacs development discussions

TL;DR, for a regular file with no other hard links in a writable directory,
why is the default to rewrite in place (non-atomically), rather than to write
to temporary-in-same-dir and then to rename, thus updating atomically?
--------------------------------------

A few days ago I was editing my .procmailrc file with emacs, made a
small change and saved it.  I was dismayed to see that change provoke a
flurry of syntax errors from procmail as two or three incoming messages
made it parse the partially-rewritten .procmailrc file.  Emacs was rewriting
that file in place, and procmail was reading it at the same time.

This made me think there must be a way to configure emacs so that when
it writes a file, it updates that file atomically.

Sure enough, there is: the buffer-local file-precious-flag.
I've added these two lines to the top of .procmailrc, and
confirmed via strace that emacs now does indeed update via rename:

  # -*- file-precious-flag: t -*-
  # The above tells emacs to save by write-then-rename, i.e., atomically.

That solved my immediate problem, but why isn't this the default, I wondered?
Reading files.el, it seems that one reason is to avoid breaking hard links.
If you set break-hardlink-on-save to `t' and the file happens to have
two or more links, then it does happen by default.

Here's the relevant code from files.el:

    (let* ((dir (file-name-directory buffer-file-name))
           (dir-writable (file-writable-p dir)))
      (if (or (and file-precious-flag dir-writable)
              (and break-hardlink-on-save
                   (file-exists-p buffer-file-name)
                   (> (file-nlinks buffer-file-name) 1)
                   (or dir-writable
                       (error (concat (format
                                       "Directory %s write-protected; " dir)
                                      "cannot break hardlink when saving")))))
	  ;; Write temp name, then rename it.
	  ;; This requires write access to the containing dir,
	  ;; which is why we don't try it if we don't have that access.

But my .procmailrc has only one link and is in a writable directory,
so an atomic update would not cause any harm.

Has anyone considered making update-via-rename the default in that case?



^ permalink raw reply	[flat|nested] 11+ messages in thread

end of thread, other threads:[~2011-12-15 23:53 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2011-12-09 10:26 enabling atomic-update more often, i.e., rewrite via rename Jim Meyering
2011-12-13 17:13 ` save-buffer: avoid data loss on interrupt Jim Meyering
2011-12-13 20:03   ` Paul Eggert
2011-12-13 20:52     ` Jim Meyering
2011-12-13 21:47       ` Paul Eggert
2011-12-13 22:27         ` chad
2011-12-14 14:40         ` Jim Meyering
2011-12-14 18:51           ` Paul Eggert
2011-12-15 23:53             ` Stefan Monnier
2011-12-14  2:40   ` Stefan Monnier
2011-12-15 12:58   ` Jim Meyering

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).