LINK(2)

✨ NAME ✨

link, linkat β€” make a hard file link πŸ”—πŸ’–

okay so hi bestie 🫢 this is the LINK(2) page out of the System Calls Manual, and we're gonna talk about hard links, which are genuinely one of the most iconic little tricks in all of Unix. no cap. let's get into it πŸ’…


πŸ“š LIBRARY πŸ“š

Standard C Library (libc, -lc) πŸŽ€


🌸 SYNOPSIS 🌸

#include <unistd.h>

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

int
linkat(int fd1, const char *name1, int fd2, const char *name2,
    int flag);

πŸ’… DESCRIPTION πŸ’…

The link() system call atomically creates the specified directory entry (hard link) name2 with the attributes of the underlying object pointed at by name1. and if that link goes through: the link count of the underlying object gets incremented, and name1 and name2 share equal access and rights to the underlying object. it's giving shared custody but make it filesystem 🫢

If name1 is removed, the file name2 is not deleted and the link count of the underlying object is decremented. she's still standing, periodt. πŸ‘‘

The object pointed at by the name1 argument must exist for the hard link to succeed, and both name1 and name2 must be in the same file system. and name1 may not be a directory. those are the rules, i don't make them, the kernel does 😭

The linkat() system call is equivalent to link except in the case where either name1 or name2 or both are relative paths. in that case a relative path name1 is interpreted relative to the directory associated with the file descriptor fd1 instead of the current working directory, and similarly for name2 and the file descriptor fd2. so basically: you get to pick the starting point. main character energy ✨

Values for flag are constructed by a bitwise-inclusive OR of flags from the following list, defined in <fcntl.h>:

  • AT_SYMLINK_FOLLOW β€” if name1 names a symbolic link, a new link for the target of the symbolic link is created. (so you're linking the thing it points at, not the symlink itself πŸ¦‹)

If linkat() is passed the special value AT_FDCWD in the fd1 or fd2 parameter, the current working directory is used for the respective name argument. If both fd1 and fd2 have value AT_FDCWD, the behavior is identical to a call to link(). Unless flag contains the AT_SYMLINK_FOLLOW flag, if name1 names a symbolic link, a new link is created for the symbolic link name1 and not its target. sneaky little default, iykyk πŸ™ˆ


πŸŽ€ RETURN VALUES πŸŽ€

The link() function returns the value 0 if successful; otherwise the value -1 is returned and the global variable errno is set to indicate the error. zero is a slay, negative one is a vibe check fail πŸ’€


😭 ERRORS 😭

The link() system call will fail and no link will be created if:

  • [ENOTDIR] β€” a component of either path prefix is not a directory.
  • [ENAMETOOLONG] β€” a component of either pathname exceeded 255 characters, or entire length of either path name exceeded 1023 characters.
  • [ENOENT] β€” a component of either path prefix does not exist.
  • [EOPNOTSUPP] β€” the file system containing the file named by name1 does not support links.
  • [EMLINK] β€” the link count of the file named by name1 would exceed 32767.
  • [EACCES] β€” a component of either path prefix denies search permission.
  • [EACCES] β€” the requested link requires writing in a directory with a mode that denies write permission.
  • [ELOOP] β€” too many symbolic links were encountered in translating one of the pathnames.
  • [ENOENT] β€” the file named by name1 does not exist.
  • [EEXIST] β€” the link named by name2 does exist.
  • [EPERM] β€” the file named by name1 is a directory.
  • [EPERM] β€” the file named by name1 has its immutable or append-only flag set, see the chflags(2) manual page for more information.
  • [EPERM] β€” the parent directory of the file named by name2 has its immutable flag set.
  • [EXDEV] β€” the link named by name2 and the file named by name1 are on different file systems.
  • [ENOSPC] β€” the directory in which the entry for the new link is being placed cannot be extended because there is no space left on the file system containing the directory.
  • [EDQUOT] β€” the directory in which the entry for the new link is being placed cannot be extended because the user's quota of disk blocks on the file system containing the directory has been exhausted.
  • [EIO] β€” an I/O error occurred while reading from or writing to the file system to make the directory entry.
  • [EINTEGRITY] β€” corrupted data was detected while reading from the file system.
  • [EROFS] β€” the requested link requires writing in a directory on a read-only file system.
  • [EFAULT] β€” one of the pathnames specified is outside the process's allocated address space.

In addition to the errors returned by the link(), the linkat() system call may fail if:

  • [EBADF] β€” the name1 or name2 argument does not specify an absolute path and the fd1 or fd2 argument, respectively, is neither AT_FDCWD nor a valid file descriptor open for searching.
  • [EINVAL] β€” the value of the flag argument is not valid.
  • [ENOTDIR] β€” the name1 or name2 argument is not an absolute path and fd1 or fd2, respectively, is neither AT_FDCWD nor a file descriptor associated with a directory.

πŸ¦‹ SEE ALSO πŸ¦‹

chflags(2), readlink(2), symlink(2), unlink(2)

go read those too, they're all part of the same friend group fr fr 🌈


πŸ‘‘ STANDARDS πŸ‘‘

The link() system call is expected to conform to ISO/IEC 9945-1:1990 (β€œPOSIX.1”). The linkat() system call follows The Open Group Extended API Set 2 specification. certified and standardized, as she should be πŸ’…


⭐ HISTORY ⭐

The link() function appeared in Version 1 AT&T UNIX. The linkat() system call appeared in FreeBSD 8.0. ancient lineage honestly, she's been iconic since forever.

The link() system call traditionally allows the super-user to link directories which corrupts the file system coherency. This implementation no longer permits it. yeah that was a mess, we don't do that anymore πŸ™ˆ


okay that's the whole page bestie, you're officially fluent in hard links now πŸŽ€βœ¨

and that's a wrap on the LINK(2) page from Debian, dated March 30, 2020 πŸ’– bye bestie, go make some links 🫢🌈

πŸ“¦ where this page comes from