0
votes

I have read some posts about symbolic links and hard links, but I would like to confirm my concept on this as there is a certain behaviour that is quite unclear to me. Consider this sequence of command line instructions.

$ echo abc >file1
$ ln file1 file2 OR $ ln -s file1 file2
$ rm file1
edit content file2 to be different, using vim
$ cat file1

If I follow this sequence of instructions for 2 cases, 1 which creates a hard link, and one which creates a symbolic link, I can explain why file1 will not exist after the sequence, but cannot really explain why file1 will exist after the sequence.

The following is my attempt.

Hard link case:

to is dereferenced from file1. Hence, file1 no longer exist but the inode that it is linked to still exists as file2 is still linked to it. Editing file 2 will only change its content.

Soft link case:

file1 is removed and the inode that file1 is linked to is dereferenced.(file2 doesn’t link to the inode but rather to file1) Hence, file1 no longer exist and the inode that it is linked to is freed and can be overwritten as there are no longer any files referencing it.

Editing file 2 would recreate file1 as file2 is still symbolically linked to file1, which would then link to a new inode which would have a pointer to the edited content.file1 is removed and the inode that file1 is linked

I’m not quite sure whether the logic of creation of the inode for symbolic links is correct, can someone validate my understanding?

1

1 Answers

0
votes

Almost; in your last case, opening "file2" O_CREAT:

  int fd = open("file2", O_CREAT|..., 0644);

will create "file1" as you have observed. It is not entirely transparent, as stat() and readlink() can uncover the dangling link. If your editor is well behaved (vi) it should work as you discovered. It does under linux and at least one UNIX.