Previous: , Up: Saving   [Contents][Index]


14.3.2 Protection Against Simultaneous Editing

Simultaneous editing occurs when two users visit the same file, both make changes, and both save their changes. If no one was informed that this was happening, and you saved first, you would later find that your changes were lost. On some systems, Emacs notices immediately when the second user starts to change a file already being edited, and issues a warning. When this is not possible, or if the second user has started to change the file despite the warning, Emacs checks when the file is saved, and issues a second warning when a user is about to overwrite a file containing another user’s changes. If you are the user editing the file, you can take corrective action at this point and prevent actual loss of work.

When you make the first modification in an Emacs buffer that is visiting a file, Emacs records that you have locked the file. (It does this by writing another file in a directory reserved for this purpose.) The lock is removed when you save the changes. The idea is that the file is locked whenever the buffer is modified. If you begin to modify the buffer while the visited file is locked by someone else, this constitutes a collision, and Emacs asks you what to do. It does this by calling the Lisp function ask-user-about-lock, which you can redefine to customize what it does. The standard definition of this function asks you a question and accepts three possible answers:

s

Steal the lock. Whoever was already changing the file loses the lock, and you get the lock.

p

Proceed. Go ahead and edit the file despite its being locked by someone else.

q

Quit. This causes an error (file-locked) and the modification you were trying to make in the buffer does not actually take place.

Note that locking works on the basis of a file name; if a file has multiple names, Emacs does not realize that the two names are the same file and cannot prevent two users from editing it simultaneously under different names. However, basing locking on names means that Emacs can interlock the editing of new files that do not really exist until they are saved.

Some systems are not configured to allow Emacs to make locks. On these systems, Emacs cannot detect trouble in advance, but it can still detect it in time to prevent you from overwriting someone else’s changes.

Every time Emacs saves a buffer, it first checks the last-modification date of the existing file on disk to see that it has not changed since the file was last visited or saved. If the date does not match, it implies that changes were made in the file in some other way, and these changes are about to be lost if Emacs actually does save. To prevent this, Emacs prints a warning message and asks for confirmation before saving. Occasionally you will know why the file was changed and know that it does not matter; then you can answer yes and proceed. Otherwise, you should cancel the save with C-g and investigate the situation.

The first thing you should do when notified that simultaneous editing has already taken place is to list the directory with C-u C-x C-d (see Directory Listing). This will show the file’s current author. You should attempt to contact that person and ask him not to continue editing. Often the next step is to save the contents of your Emacs buffer under a different name, and use diff to compare the two files.

Simultaneous editing checks are also made when you visit a file that is already visited with C-x C-f and when you start to modify a file. This is not strictly necessary, but it is useful to find out about such a problem as early as possible, when corrective action takes less work.

Another way to protect your file is to set the read, write, and executable permissions for the file. Use the function set-default-file-modes to set the UNIX umask value to the nmask argument. The umask value is the default protection mode for new files.


Previous: , Up: Saving   [Contents][Index]