FHLINK(2)
π FHLINK(2) β the hard-link-via-file-handle girlies π
okay so bestie, this is a System Calls Manual page for fhlink and fhlinkat β the make a hard file link duo. yes, it's giving "niche but iconic." let's get into it π
β¨ NAME β¨
fhlink, fhlinkat β make a hard file link. that's the whole vibe. π«Ά
π LIBRARY π
Standard C Library (libc, -lc). the classics, always.
πΈ SYNOPSIS πΈ
#include <unistd.h>
int
fhlink(fhandle_t *fhp, const char *to);
int
fhlinkat(fhandle_t *fhp, int tofd, const char *to);
look at those signatures. impeccable. no notes. π
π« DESCRIPTION π«
so the fhlink() system call atomically creates the specified directory entry (hard link) to with the attributes of the underlying object pointed at by fhp. like, it's one clean motion β no half-links, no messy in-betweens. we love atomic behavior π
if the link is successful:
- the link count of the underlying object is incremented β¨
fhpandtoshare equal access and rights to the underlying object π€
and if fhp is removed? important: the file to is not deleted, and the link count of the underlying object is decremented. so don't panic. it's fine. it's fine. π
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 fhp may not be a directory. sorry directory girlies, not today π
fhlinkat() is equivalent to fhlink except in the case where to is a relative path. 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-aware" π«
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(). unless flag contains the AT_SYMLINK_FOLLOW flag, if fhp names a symbolic link, a new link is created for the symbolic link fhp and not its target. no cap. π₯
π 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. the classic 0-or-meltdown situation. π
π ERRORS π
the fhlink() system call will fail and no link will be created if:
[ENOTDIR]β a component oftoprefix is not a directory.[ENAMETOOLONG]β a component oftoexceeded255characters, or entire length oftoname exceeded1023characters. like bestie that's so long π«[ENOENT]β a component oftoprefix does not exist.[EOPNOTSUPP]β the file system containing the file pointed at byfhpdoes not support links. rude honestly.[EMLINK]β the link count of the file pointed at byfhpwould exceed32767. that's a lot of links but okay.[EACCES]β a component oftoprefix 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. it's loops all the way down π΅βπ«[ENOENT]β the file pointed at byfhpdoes not exist.[EEXIST]β the link named bytodoes exist. already taken, babe.[EPERM]β the file pointed at byfhpis a directory.[EPERM]β the file pointed at byfhphas its immutable or append-only flag set, see thechflags(2)manual page for more information.[EPERM]β the parent directory of the file named bytohas its immutable flag set.[EXDEV]β the link named bytoand the file pointed at byfhpare on different file systems. long distance relationship = not happening π[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 handlefhpis no longer valid.
in addition to the errors returned by the fhlink(), the fhlinkat() system call may fail if:
[EBADF]β thefhportoargument does not specify an absolute path and thetofdargument, is notAT_FDCWDnor a valid file descriptor open for searching.[EINVAL]β the value of theflagargument is not valid.[ENOTDIR]β thefhportoargument is not an absolute path andtofdis notAT_FDCWDnor a file descriptor associated with a directory.
lowkey the error list is long but honestly these all just mean "the link did not happen, here's why." read the bracket, fix the thing, try again. you got this πͺ
π¦ SEE ALSO π¦
the whole file-handle friend group. go say hi π
π HISTORY π
the fhlink() and fhlinkat() system calls first appeared in FreeBSD 12.1. vintage-ish. we stan a documented origin story β¨
and that's the page!! mwah π this edition was brought to you by Debian, dated November 30, 2022, filed under FHLINK(2) β go create some hard links responsibly, bestie ππ
π¦ where this page comes from
- freebsd 14.5: base 14.5
- freebsd 15.1: base 15.1
- π the page's license: BSD-2-Clause
- Copyright (c) 2018 Gandi
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms