View Single Post
  #6   (View Single Post)  
Old 6th February 2016
J65nko J65nko is offline
Administrator
 
Join Date: May 2008
Location: Budel - the Netherlands
Posts: 4,131
Default

There is a difference in behaviour if the target file already exists. rename(2) will overwrite an already existing file , while link(2) will refuse to create a new link (directory entry), if that filename/directory entry already exists.

From link(2):
Code:
NAME
     link - make hard link to a file

     int
          link(const char *name1, const char *name2);

DESCRIPTION

     The link() function atomically creates the specified directory entry
     (hard link) name2 with the attributes of the underlying object pointed at
     by name1.  If the link is successful: the link count of the underlying
     object is incremented; name1 and name2 share equal access and rights to
     the underlying object.

     If name1 is removed, the file name2 is not deleted and the link count of
     the underlying object is decremented.

     name1 must exist for the hard link to succeed and both name1 and name2
     must be in the same file system.  As mandated by POSIX.1 name1 may not be
     a directory.

ERRORS

     [snip]

     [EEXIST]           The link named by name2 does exist
Keep in mind that this is the C function. The ln(1) command line utility will also refuse to create a new directory entry, if one already exists. You will have to use the -f option to unlink an already existing file, allowing the link to be made:

Code:
$ echo Shere Khan >new/animal
$ echo Simba >temp/lion

$ ln temp/lion new/animal
ln: new/animal: File exists

$ ls -li new/animal temp/lion
571653 -rw-r--r--  1 adriaan  wheel  11 Feb  6 16:22 new/animal
571654 -rw-r--r--  1 adriaan  wheel   6 Feb  6 16:22 temp/lion

$ ln -f temp/lion new/animal

$ ls -li new/animal temp/lion
571654 -rw-r--r--  2 adriaan  wheel  6 Feb  6 16:22 new/animal
571654 -rw-r--r--  2 adriaan  wheel  6 Feb  6 16:22 temp/lion

$ stat -s new/animal
st_dev=0 st_ino=571654 st_mode=0100644 st_nlink=2 st_uid=1001 st_gid=0 
st_rdev=2292638 st_size=6 st_atime=1454772179 st_mtime=1454772179
st_ctime=1454772265 st_blksize=16384 st_blocks=4 st_flags=0

$ stat -s temp/lion
st_dev=0 st_ino=571654 st_mode=0100644 st_nlink=2 st_uid=1001 st_gid=0
st_rdev=2292638 st_size=6 st_atime=1454772179 st_mtime=1454772179
st_ctime=1454772265 st_blksize=16384 st_blocks=4 st_flags=0

$ cat new/animal
Simba
$ cat temp/lion
Simba

$ rm temp/lion

$ stat -s new/animal
st_dev=0 st_ino=571654 st_mode=0100644 st_nlink=1 st_uid=1001 st_gid=0
st_rdev=2292638 st_size=6 st_atime=1454772179 st_mtime=1454772179
st_ctime=1454772265 st_blksize=16384 st_blocks=4 st_flags=0
__________________
You don't need to be a genius to debug a pf.conf firewall ruleset, you just need the guts to run tcpdump
Reply With Quote