I had an interview, where the interviewer asked what operations raise the link count of a file, besides ln and its underlying syscall, I didn't know. He stated that opening a file will increase the link count by one to prevent deletion of an opened file. I did not agree that he is correct, why would vi need the temp .swp files then? Is he correct? What operations can raise a file's link count other than ln?
1 Answer
He's wrong.
The only thing that increases a file's link count is the link system call.
(Or editing the raw file system with a hex editor.)
-
Indeed; it's easily demonstrated that it's easy to delete (remove from a directory) an open file. There is still a reference to the inode from the process holding the inode open, which prevents the file from being removed from the file system so that the data can still be accessed. Note however that
vireads the file but doesn't keep it open. It's also trivial to show that the link count isn't increased when editing it... The interviewer is clearly confused.wurtel– wurtel2015-03-11 10:58:19 +00:00Commented Mar 11, 2015 at 10:58 -
Thanks for your support, and for pointing out that
vidoesn't edit files in place, but modifies them only when you tell it to save (write). Yeah, the interviewer seems to have link count confused with in-memory inode reference count.Scott - Слава Україні– Scott - Слава Україні2015-03-11 17:13:02 +00:00Commented Mar 11, 2015 at 17:13