Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

Whenever I use mcedit to edit a file that is hard linked somewhere and I want to save the file, the editor asks me if I want to remove the hard links. Is that common behavior on Linux or is mcedit "special" in doing so? Why would regular applications (not fsck or other admin tools) care about hard links?

share|improve this question

3 Answers 3

up vote 4 down vote accepted

When you want to modify a file, you have two options, each with its benefits and drawbacks.

  • You can overwrite the file in place. This does not use any extra space, and conserves the hard links, permissions and any other attribute beyond the content of the existing file. The major drawback of doing this is that if anything happens while the file is being written (the application crashes, or the power goes out), you end up with a partially written file.
  • You can write the new version of the file to a new file with a different name, then move it into place. This uses more space and breaks hard links, and if you have write permissions on a file but not on the directory it contains, you can't do it at all. On the flip side, the old version of the file is atomically replaced by the new version, so at every point in time the file name points to a valid, complete version of the file.

Mcedit is asking you which strategy to choose. Strangely though, mcedit's default strategy, for files with a single directory entry, is to truncate the existing file, putting your data at risk. Only when the safe strategy would break a hard link does it give you the opportunity to use it. You can change this in the “Edit save mode” dialog from the Options menu: “quick save” means overwrite, “safe save” means save to a temporary file then rename. When safe mode is chosen, you don't get a choice not to break symbolic links.

(Observations made on mc 4.8.3. If this is still the case in the latest version, consider reporting it as a design bug — “safe mode” should be the default, and you should get an option not to break hard links in that case.)

Good editors such as Vim or Emacs let you choose the default strategy.

share|improve this answer

It is unusual; I suspect it was put in a by a developer who kept shooting himself in the foot.

If you write to file with more than one link (a "regular" file is linked to its directory), then you will be changing a file that may exist under another path. You'd have the same issue with symbolic links, but the linked to file doesn't know that it has symlinks pointing at it.

If you write to a multiply-linked file, it changes the contents seen through other file-paths; that's the whole point of multiple links.

share|improve this answer
    
That's not really the point — the point is that you have a choice of being resilient to failure or preserving hard links. Mcedit does have an odd way to make this choice. –  Gilles May 12 '13 at 23:26

This makes a serious difference when creating backup files before saving (which I assume mcedit does, too). Depending on the handling of hardlinks the other paths to that inode may point to the wrong file. See this.

If this is not about backup files then it may be just a warning, read it as: "Just in case you forgot: You are about to change not just /foo/bar but also /bar/baz."

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.