Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Why not use a hardlink?


Vim (and I believe most text editors) save files by doing (simplified):

  rename("myfile", "backup_file")
  openat(AT_FDCWD, "myfile", O_WRONLY|O_CREAT, 0644)
  unlink("backup_file")
I.e. unlink the old file and create a new one by the same name (with a backup step in between). So the other hard link would keep pointing to the old version of the file.


What version of Vim? Because I've never experienced this problem in over a decade of use.

Try it yourself:

    echo "test1" > test1
    ln test1 test2
    vim -c 's/1/2/' -c x test2
    cat test1
Edit: Oh, you can configure Vim to break hardlinks

https://vim.fandom.com/wiki/Editing_a_hard_link_to_a_file


Sorry, you're right. Sadly I cannot edit my comment since too long time has passed. I should of course tested with an added hard link. Vim does do the rename/unlink if there only is a single hard link to a file, which makes inotify behave different than expected, so my guess was that Vim would also be OK with breaking hard links. But it makes sense that it wouldn't, since hard links are a much more integral part of Unix.

  $ echo 1 > test1
  $ inotifywait --quiet --monitor test1 &
  [1] 14321
  $ vim -c 's/1/2/' -c x test1
  $ cat test1
  2
  $ <no output from inotifywait>
With multiple hard links, the inode is preserved (only difference is adding the call to ln):

  $ echo 1 > test1
  $ ln test1 test2
  $ inotifywait --quiet --monitor test1 &
  [1] 14361
  $ vim -c 's/1/2/' -c x test1
  $ cat test1
  2
  test1 OPEN 
  test1 ACCESS 
  test1 CLOSE_NOWRITE,CLOSE 
  $




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: