Emacs works like this by default: when it saves a file, it moves the existing file to the backup file (
rename("foo", "foo~")), which unlinks the backup file, and it creates a new file to write to. This is controlled by the
backup-by-copying variable; the default value is
nil, meaning the rename-and-new strategy.
There are a number of circumstances when Emacs will instead copy the existing file to the backup file (
) then save the buffer to the existing file (open("foo
", O_TRUNC)), basically when Emacs thinks it won't be able to create a file with the same permissions or hard links.
Auto-save files are in your home directory by default, so they aren't a problem.
Seeing the bug, I suspect that your problem is not in fact that you cannot write the file, but rather that the filesystem advertises the file as read-only. If that's the case, then Emacs would believe that it can't re-create the file (which is a bit weird: Emacs applies the copy-and-overwrite strategy if it doesn't have the permission to overwrite the file!), which is one of the conditions that make it apply the copy-and-overwrite strategy.
Here's some heavy-handed code that forces Emacs to use the rename strategy in that case. Untested.
(around backup-buffer-by-renaming-on-megafuse activate)
(string-match megafuse-directory-regexp (file-chase-links buffer-file-name)))
(letf (((symbol-function 'backup-buffer-copy)
(lambda (from-name to-name modes extended-attributes)
(rename-file from-name to-name t))))