FHLINK(2)

okay bestie, welcome to the system calls manual ✨ today we're learning about fhlink and fhlinkat, and honestly? it's giving "make a hard file link" energy and i am obsessed πŸ’…


🫢 LIBRARY

Standard C Library (libc, -lc) β€” she's iconic, she's everywhere, she's literally the standard πŸŽ€


🌸 SYNOPSIS

#include <unistd.h>

int
fhlink(fhandle_t *fhp, const char *to);

int
fhlinkat(fhandle_t *fhp, int tofd, const char *to);

look at her. two functions. same vibe. no cap πŸ’œ


✨ DESCRIPTION ✨

The fhlink() system call atomically creates the specified directory entry (a hard link, periodt) named to, with the attributes of the underlying object pointed at by fhp. and if the link works out? bestie, it's a whole glow-up:

  • the link count of the underlying object gets incremented πŸ“ˆ
  • fhp and to now share equal access and rights to the underlying object 🫢

If fhp gets removed, the file to is not deleted, and the link count of the underlying object gets decremented. she stays. she's resilient. πŸ’…

The object pointed at by the fhp argument must exist for the hard link to succeed, and both fhp and to must be in the same file system. also, the fhp argument may not be a directory β€” nope, not happening, no exceptions, don't even try it 🚫

The fhlinkat() system call is equivalent to fhlink except in the case where to is a relative paths. in that case, a relative path to is interpreted relative to the directory associated with the file descriptor tofd instead of the current working directory. it's giving "context matters" ✨


πŸŽ€ the flags πŸŽ€

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

  • AT_SYMLINK_FOLLOW β€” if fhp names a symbolic link, a new link for the target of the symbolic link is created. (she follows the plot πŸ¦‹)
  • AT_BENEATH β€” only allow to link to a file which is beneath of the topping directory. See the description of the O_BENEATH flag in the open(2) manual page. πŸ”’

If fhlinkat() is passed the special value AT_FDCWD in the tofd parameter, the current working directory is used for the to argument. and if tofd has value AT_FDCWD, the behavior is identical to a call to link(). iconic crossover event 🌈

Unless flag contains the AT_SYMLINK_FOLLOW flag, if fhp names a symbolic link, a new link is created for the symbolic link fhp itself and not its target. iykyk β€” no following, no chasing, she links what she sees πŸ’€


πŸ”₯ 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 = slay. negative one = bestie something went wrong 😭


πŸ’€ ERRORS πŸ’€

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

  • [ENOTDIR] β€” a component of to prefix is not a directory.
  • [ENAMETOOLONG] β€” a component of to exceeded 255 characters, or entire length of to name exceeded 1023 characters. (she's too long. she's doing too much. πŸ₯²)
  • [ENOENT] β€” a component of to prefix does not exist.
  • [EOPNOTSUPP] β€” the file system containing the file pointed at by fhp does not support links.
  • [EMLINK] β€” the link count of the file pointed at by fhp would exceed 32767.
  • [EACCES] β€” a component of to 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. (she's in a loop, bestie, send help πŸŒ€)
  • [ENOENT] β€” the file pointed at by fhp does not exist.
  • [EEXIST] β€” the link named by to does exist.
  • [EPERM] β€” the file pointed at by fhp is a directory.
  • [EPERM] β€” the file pointed at by fhp 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 to has its immutable flag set.
  • [EXDEV] β€” the link named by to and the file pointed at by fhp are on different file systems. 🚫 cross-filesystem romance is not allowed
  • [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.
  • [ESTALE] β€” the file handle fhp is no longer valid

πŸ¦‹ and there's more πŸ¦‹

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

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

πŸ‘‘ SEE ALSO πŸ‘‘

fhstat(2), fhreadlink(2), fhopen(2),

go read those next, bestie, they're all part of the same friend group 🫢✨


and that's the whole page, fr fr πŸ’– this one's from the Debian crew, dated March 30, 2020, and it's filed under FHLINK(2) β€” no cap, you're now officially a hard-link girly. go forth and link responsibly πŸ’…πŸŒΈ

πŸ“¦ where this page comes from