While working through two active buffers in Emacs, sum.cpp
and template.cpp
, I accidentally shut down my laptop (Ubuntu 15.04 (Vivid Vervet)) without closing Emacs, and without saving these two files.
When I restarted the laptop and listed directory content in Emacs by C - x C - f /C++/
, I got the following.
What does the line mean after .#sum.cpp?
lrwxrwxrwx 1 anupam anupam 41 Aug 6 00:53 .#sum.cpp -> [email protected]:1438790786
I know Emacs saves the unsaved buffer by # by autosave
, but when accessing #sum.cpp
why is it showing the following?
file is a symlink to non-existent target?
The autosaved file is
#sum.cpp#
. The symlink.#sum.cpp
is used by Emacs to store some internal data (seems like user name, host name and some timestamp)As the target of a symlink is just some short text (that is resolved as a path if you access the symlink's content), symlinks are sometimes used to store small amounts of data. In that case the target most likely isn't a valid file path and you get an error if you try to access it.
That's because creating a symlink is an atomic operation: Either the symlink is created with full content or it's not created at all. In contrast creating a regular file and filling it with content is not atomic and in case of an error you may end up with an empty file or only partial content.
Symlinks - symbolic links - are files which contains text description of where the target file is relative to the symbolic link. In opposition to hard links, when you delete target files, symlinks are empty.
A symbolic link really is just a short text with special "magic" happening in the kernel when you open it. Programs can put whatever text they want in there. Whether that text is actually a valid path name doesn't matter when the symlink is being created. The existence of the target only matters when you try to open the symlink.
Emacs uses this in a slightly different way. It creates a symlink to indicate that the file is open and have unsaved changes. If you start another instance of emacs, it can then warn you about the previous instance already having the file open.
The reason for using symlinks for this instead of other methods is that the symlinks also work if the file is on NFS. This is also why the hostname is in the text, that way emacs will know if the file is currently open on this machine or on another machine.
I guess if you try to edit the file with emacs again, it will notice that the file is supposedly open by an emacs instance on the current machine, but that emacs instance no longer exists, so it must somehow have died. And then emacs can safely ignore the symlink and not give you a warning.