LINK(2) | System Calls Manual | LINK(2) |
link
, linkat
— make hard link to a file
#include
<unistd.h>
int
link
(const
char *name1, const char
*name2);
#include <fcntl.h>
#include <unistd.h>
int
linkat
(int
fd1, const char
*name1, int fd2,
const char *name2,
int flag);
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.
The
linkat
()
function is equivalent to link
() except that where
name1 or name2 specifies a
relative path, the directory entries linked are resolved relative to the
directories associated with file descriptors fd1 or
fd2 (respectively) instead of the current working
directory.
If
linkat
()
is passed the special value AT_FDCWD
(defined in
<fcntl.h>
) in the
fd1 or fd2 parameter, the
current working directory is used for resolving the respective
name1 or name2 argument.
The flag argument is the bitwise OR of zero or more of the following values:
AT_SYMLINK_FOLLOW
If the AT_SYMLINK_FOLLOW
flag is clear and
name1 names a symbolic link, a new link is created for
the symbolic link name1 and not its target.
Upon successful completion, the value 0 is returned; otherwise the value -1 is returned and the global variable errno is set to indicate the error.
link
() and
linkat
() will fail and no link will be created
if:
ENOTDIR
]ENAMETOOLONG
]NAME_MAX
characters, or an entire pathname (including the terminating NUL) exceeded
PATH_MAX
bytes.ENOENT
]EOPNOTSUPP
]EMLINK
]LINK_MAX
.EACCES
]EACCES
]ELOOP
]ENOENT
]EEXIST
]EPERM
]link
() on a
directory.EPERM
]EXDEV
]ENOSPC
]EDQUOT
]EIO
]EROFS
]EFAULT
]Additionally, linkat
() will fail if:
EINVAL
]AT_SYMLINK_FOLLOW
.EBADF
]AT_FDCWD
nor a valid file descriptor.ENOTDIR
]EACCES
]The link
() and
linkat
() functions are expected to conform to
IEEE Std 1003.1-2008 (“POSIX.1”).
The link
() system call first appeared in
Version 1 AT&T UNIX. The
linkat
() function appeared in
OpenBSD 5.0.
September 10, 2015 | OpenBSD-6.4 |