23.3.2.3 Copying vs. Renaming
Backup files can be made by copying the old file or by renaming it.
This makes a difference when the old file has multiple names (hard
links). If the old file is renamed into the backup file, then the
alternate names become names for the backup file. If the old file is
copied instead, then the alternate names remain names for the file
that you are editing, and the contents accessed by those names will be
the new contents.
The method of making a backup file may also affect the file's owner
and group. If copying is used, these do not change. If renaming is used,
you become the file's owner, and the file's group becomes the default
(different operating systems have different defaults for the group).
Having the owner change is usually a good idea, because then the owner
always shows who last edited the file. Also, the owners of the backups
show who produced those versions. Occasionally there is a file whose
owner should not change; it is a good idea for such files to contain
local variable lists to set backup-by-copying-when-mismatch
locally (see File Variables).
The choice of renaming or copying is controlled by four variables.
Renaming is the default choice. If the variable
backup-by-copying
is non-nil
, copying is used. Otherwise,
if the variable backup-by-copying-when-linked
is non-nil
,
then copying is used for files that have multiple names, but renaming
may still be used when the file being edited has only one name. If the
variable backup-by-copying-when-mismatch
is non-nil
, then
copying is used if renaming would cause the file's owner or group to
change. backup-by-copying-when-mismatch
is t
by default
if you start Emacs as the superuser. The fourth variable,
backup-by-copying-when-privileged-mismatch
, gives the highest
numeric user-id for which backup-by-copying-when-mismatch
will be
forced on. This is useful when low-numbered user-ids are assigned to
special system users, such as root
, bin
, daemon
,
etc., which must maintain ownership of files.
When a file is managed with a version control system (see Version Control), Emacs does not normally make backups in the usual way for
that file. But check-in and check-out are similar in some ways to
making backups. One unfortunate similarity is that these operations
typically break hard links, disconnecting the file name you visited from
any alternate names for the same file. This has nothing to do with
Emacs—the version control system does it.